sl@0
|
1 |
/* crypto/evp/encode.c */
|
sl@0
|
2 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This package is an SSL implementation written
|
sl@0
|
6 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
7 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
10 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
11 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
13 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
14 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
17 |
* the code are not to be removed.
|
sl@0
|
18 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
19 |
* as the author of the parts of the library used.
|
sl@0
|
20 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
21 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
24 |
* modification, are permitted provided that the following conditions
|
sl@0
|
25 |
* are met:
|
sl@0
|
26 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
27 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
30 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
31 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
32 |
* must display the following acknowledgement:
|
sl@0
|
33 |
* "This product includes cryptographic software written by
|
sl@0
|
34 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
35 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
36 |
* being used are not cryptographic related :-).
|
sl@0
|
37 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
38 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
39 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
40 |
*
|
sl@0
|
41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
51 |
* SUCH DAMAGE.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
55 |
* copied and put under another distribution licence
|
sl@0
|
56 |
* [including the GNU Public Licence.]
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
/*
|
sl@0
|
59 |
© Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
|
sl@0
|
60 |
*/
|
sl@0
|
61 |
|
sl@0
|
62 |
|
sl@0
|
63 |
#include <stdio.h>
|
sl@0
|
64 |
#include "cryptlib.h"
|
sl@0
|
65 |
#include <openssl/evp.h>
|
sl@0
|
66 |
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
|
sl@0
|
67 |
#include "libcrypto_wsd_macros.h"
|
sl@0
|
68 |
#include "libcrypto_wsd.h"
|
sl@0
|
69 |
#endif
|
sl@0
|
70 |
|
sl@0
|
71 |
#ifndef CHARSET_EBCDIC
|
sl@0
|
72 |
#define conv_bin2ascii(a) (data_bin2ascii[(a)&0x3f])
|
sl@0
|
73 |
#define conv_ascii2bin(a) (data_ascii2bin[(a)&0x7f])
|
sl@0
|
74 |
#else
|
sl@0
|
75 |
/* We assume that PEM encoded files are EBCDIC files
|
sl@0
|
76 |
* (i.e., printable text files). Convert them here while decoding.
|
sl@0
|
77 |
* When encoding, output is EBCDIC (text) format again.
|
sl@0
|
78 |
* (No need for conversion in the conv_bin2ascii macro, as the
|
sl@0
|
79 |
* underlying textstring data_bin2ascii[] is already EBCDIC)
|
sl@0
|
80 |
*/
|
sl@0
|
81 |
#define conv_bin2ascii(a) (data_bin2ascii[(a)&0x3f])
|
sl@0
|
82 |
#define conv_ascii2bin(a) (data_ascii2bin[os_toascii[a]&0x7f])
|
sl@0
|
83 |
#endif
|
sl@0
|
84 |
|
sl@0
|
85 |
/* 64 char lines
|
sl@0
|
86 |
* pad input with 0
|
sl@0
|
87 |
* left over chars are set to =
|
sl@0
|
88 |
* 1 byte => xx==
|
sl@0
|
89 |
* 2 bytes => xxx=
|
sl@0
|
90 |
* 3 bytes => xxxx
|
sl@0
|
91 |
*/
|
sl@0
|
92 |
#define BIN_PER_LINE (64/4*3)
|
sl@0
|
93 |
#define CHUNKS_PER_LINE (64/4)
|
sl@0
|
94 |
#define CHAR_PER_LINE (64+1)
|
sl@0
|
95 |
|
sl@0
|
96 |
#ifndef EMULATOR
|
sl@0
|
97 |
static unsigned char data_bin2ascii[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZ\
|
sl@0
|
98 |
abcdefghijklmnopqrstuvwxyz0123456789+/";
|
sl@0
|
99 |
#else
|
sl@0
|
100 |
static const unsigned char data_bin2ascii[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZ\
|
sl@0
|
101 |
abcdefghijklmnopqrstuvwxyz0123456789+/";
|
sl@0
|
102 |
#endif
|
sl@0
|
103 |
|
sl@0
|
104 |
/* 0xF0 is a EOLN
|
sl@0
|
105 |
* 0xF1 is ignore but next needs to be 0xF0 (for \r\n processing).
|
sl@0
|
106 |
* 0xF2 is EOF
|
sl@0
|
107 |
* 0xE0 is ignore at start of line.
|
sl@0
|
108 |
* 0xFF is error
|
sl@0
|
109 |
*/
|
sl@0
|
110 |
|
sl@0
|
111 |
#define B64_EOLN 0xF0
|
sl@0
|
112 |
#define B64_CR 0xF1
|
sl@0
|
113 |
#define B64_EOF 0xF2
|
sl@0
|
114 |
#define B64_WS 0xE0
|
sl@0
|
115 |
#define B64_ERROR 0xFF
|
sl@0
|
116 |
#define B64_NOT_BASE64(a) (((a)|0x13) == 0xF3)
|
sl@0
|
117 |
|
sl@0
|
118 |
#ifndef EMULATOR
|
sl@0
|
119 |
static unsigned char data_ascii2bin[128]={
|
sl@0
|
120 |
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
121 |
0xFF,0xE0,0xF0,0xFF,0xFF,0xF1,0xFF,0xFF,
|
sl@0
|
122 |
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
123 |
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
124 |
0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
125 |
0xFF,0xFF,0xFF,0x3E,0xFF,0xF2,0xFF,0x3F,
|
sl@0
|
126 |
0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,
|
sl@0
|
127 |
0x3C,0x3D,0xFF,0xFF,0xFF,0x00,0xFF,0xFF,
|
sl@0
|
128 |
0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
|
sl@0
|
129 |
0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,
|
sl@0
|
130 |
0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
|
sl@0
|
131 |
0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
132 |
0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,
|
sl@0
|
133 |
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
|
sl@0
|
134 |
0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,
|
sl@0
|
135 |
0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
136 |
};
|
sl@0
|
137 |
|
sl@0
|
138 |
#else
|
sl@0
|
139 |
static const unsigned char data_ascii2bin[128]={
|
sl@0
|
140 |
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
141 |
0xFF,0xE0,0xF0,0xFF,0xFF,0xF1,0xFF,0xFF,
|
sl@0
|
142 |
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
143 |
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
144 |
0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
145 |
0xFF,0xFF,0xFF,0x3E,0xFF,0xF2,0xFF,0x3F,
|
sl@0
|
146 |
0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,
|
sl@0
|
147 |
0x3C,0x3D,0xFF,0xFF,0xFF,0x00,0xFF,0xFF,
|
sl@0
|
148 |
0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
|
sl@0
|
149 |
0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,
|
sl@0
|
150 |
0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
|
sl@0
|
151 |
0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
152 |
0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,
|
sl@0
|
153 |
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
|
sl@0
|
154 |
0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,
|
sl@0
|
155 |
0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF,
|
sl@0
|
156 |
};
|
sl@0
|
157 |
#endif
|
sl@0
|
158 |
|
sl@0
|
159 |
EXPORT_C void EVP_EncodeInit(EVP_ENCODE_CTX *ctx)
|
sl@0
|
160 |
{
|
sl@0
|
161 |
ctx->length=48;
|
sl@0
|
162 |
ctx->num=0;
|
sl@0
|
163 |
ctx->line_num=0;
|
sl@0
|
164 |
}
|
sl@0
|
165 |
|
sl@0
|
166 |
EXPORT_C void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
|
sl@0
|
167 |
const unsigned char *in, int inl)
|
sl@0
|
168 |
{
|
sl@0
|
169 |
int i,j;
|
sl@0
|
170 |
unsigned int total=0;
|
sl@0
|
171 |
|
sl@0
|
172 |
*outl=0;
|
sl@0
|
173 |
if (inl == 0) return;
|
sl@0
|
174 |
OPENSSL_assert(ctx->length <= (int)sizeof(ctx->enc_data));
|
sl@0
|
175 |
if ((ctx->num+inl) < ctx->length)
|
sl@0
|
176 |
{
|
sl@0
|
177 |
memcpy(&(ctx->enc_data[ctx->num]),in,inl);
|
sl@0
|
178 |
ctx->num+=inl;
|
sl@0
|
179 |
return;
|
sl@0
|
180 |
}
|
sl@0
|
181 |
if (ctx->num != 0)
|
sl@0
|
182 |
{
|
sl@0
|
183 |
i=ctx->length-ctx->num;
|
sl@0
|
184 |
memcpy(&(ctx->enc_data[ctx->num]),in,i);
|
sl@0
|
185 |
in+=i;
|
sl@0
|
186 |
inl-=i;
|
sl@0
|
187 |
j=EVP_EncodeBlock(out,ctx->enc_data,ctx->length);
|
sl@0
|
188 |
ctx->num=0;
|
sl@0
|
189 |
out+=j;
|
sl@0
|
190 |
*(out++)='\n';
|
sl@0
|
191 |
*out='\0';
|
sl@0
|
192 |
total=j+1;
|
sl@0
|
193 |
}
|
sl@0
|
194 |
while (inl >= ctx->length)
|
sl@0
|
195 |
{
|
sl@0
|
196 |
j=EVP_EncodeBlock(out,in,ctx->length);
|
sl@0
|
197 |
in+=ctx->length;
|
sl@0
|
198 |
inl-=ctx->length;
|
sl@0
|
199 |
out+=j;
|
sl@0
|
200 |
*(out++)='\n';
|
sl@0
|
201 |
*out='\0';
|
sl@0
|
202 |
total+=j+1;
|
sl@0
|
203 |
}
|
sl@0
|
204 |
if (inl != 0)
|
sl@0
|
205 |
memcpy(&(ctx->enc_data[0]),in,inl);
|
sl@0
|
206 |
ctx->num=inl;
|
sl@0
|
207 |
*outl=total;
|
sl@0
|
208 |
}
|
sl@0
|
209 |
|
sl@0
|
210 |
EXPORT_C void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl)
|
sl@0
|
211 |
{
|
sl@0
|
212 |
unsigned int ret=0;
|
sl@0
|
213 |
|
sl@0
|
214 |
if (ctx->num != 0)
|
sl@0
|
215 |
{
|
sl@0
|
216 |
ret=EVP_EncodeBlock(out,ctx->enc_data,ctx->num);
|
sl@0
|
217 |
out[ret++]='\n';
|
sl@0
|
218 |
out[ret]='\0';
|
sl@0
|
219 |
ctx->num=0;
|
sl@0
|
220 |
}
|
sl@0
|
221 |
*outl=ret;
|
sl@0
|
222 |
}
|
sl@0
|
223 |
|
sl@0
|
224 |
EXPORT_C int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int dlen)
|
sl@0
|
225 |
{
|
sl@0
|
226 |
int i,ret=0;
|
sl@0
|
227 |
unsigned long l;
|
sl@0
|
228 |
|
sl@0
|
229 |
for (i=dlen; i > 0; i-=3)
|
sl@0
|
230 |
{
|
sl@0
|
231 |
if (i >= 3)
|
sl@0
|
232 |
{
|
sl@0
|
233 |
l= (((unsigned long)f[0])<<16L)|
|
sl@0
|
234 |
(((unsigned long)f[1])<< 8L)|f[2];
|
sl@0
|
235 |
*(t++)=conv_bin2ascii(l>>18L);
|
sl@0
|
236 |
*(t++)=conv_bin2ascii(l>>12L);
|
sl@0
|
237 |
*(t++)=conv_bin2ascii(l>> 6L);
|
sl@0
|
238 |
*(t++)=conv_bin2ascii(l );
|
sl@0
|
239 |
}
|
sl@0
|
240 |
else
|
sl@0
|
241 |
{
|
sl@0
|
242 |
l=((unsigned long)f[0])<<16L;
|
sl@0
|
243 |
if (i == 2) l|=((unsigned long)f[1]<<8L);
|
sl@0
|
244 |
|
sl@0
|
245 |
*(t++)=conv_bin2ascii(l>>18L);
|
sl@0
|
246 |
*(t++)=conv_bin2ascii(l>>12L);
|
sl@0
|
247 |
*(t++)=(i == 1)?'=':conv_bin2ascii(l>> 6L);
|
sl@0
|
248 |
*(t++)='=';
|
sl@0
|
249 |
}
|
sl@0
|
250 |
ret+=4;
|
sl@0
|
251 |
f+=3;
|
sl@0
|
252 |
}
|
sl@0
|
253 |
|
sl@0
|
254 |
*t='\0';
|
sl@0
|
255 |
return(ret);
|
sl@0
|
256 |
}
|
sl@0
|
257 |
|
sl@0
|
258 |
EXPORT_C void EVP_DecodeInit(EVP_ENCODE_CTX *ctx)
|
sl@0
|
259 |
{
|
sl@0
|
260 |
ctx->length=30;
|
sl@0
|
261 |
ctx->num=0;
|
sl@0
|
262 |
ctx->line_num=0;
|
sl@0
|
263 |
ctx->expect_nl=0;
|
sl@0
|
264 |
}
|
sl@0
|
265 |
|
sl@0
|
266 |
/* -1 for error
|
sl@0
|
267 |
* 0 for last line
|
sl@0
|
268 |
* 1 for full line
|
sl@0
|
269 |
*/
|
sl@0
|
270 |
EXPORT_C int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
|
sl@0
|
271 |
const unsigned char *in, int inl)
|
sl@0
|
272 |
{
|
sl@0
|
273 |
int seof= -1,eof=0,rv= -1,ret=0,i,v,tmp,n,ln,tmp2,exp_nl;
|
sl@0
|
274 |
unsigned char *d;
|
sl@0
|
275 |
|
sl@0
|
276 |
n=ctx->num;
|
sl@0
|
277 |
d=ctx->enc_data;
|
sl@0
|
278 |
ln=ctx->line_num;
|
sl@0
|
279 |
exp_nl=ctx->expect_nl;
|
sl@0
|
280 |
|
sl@0
|
281 |
/* last line of input. */
|
sl@0
|
282 |
if ((inl == 0) || ((n == 0) && (conv_ascii2bin(in[0]) == B64_EOF)))
|
sl@0
|
283 |
{ rv=0; goto end; }
|
sl@0
|
284 |
|
sl@0
|
285 |
/* We parse the input data */
|
sl@0
|
286 |
for (i=0; i<inl; i++)
|
sl@0
|
287 |
{
|
sl@0
|
288 |
/* If the current line is > 80 characters, scream alot */
|
sl@0
|
289 |
if (ln >= 80) { rv= -1; goto end; }
|
sl@0
|
290 |
|
sl@0
|
291 |
/* Get char and put it into the buffer */
|
sl@0
|
292 |
tmp= *(in++);
|
sl@0
|
293 |
v=conv_ascii2bin(tmp);
|
sl@0
|
294 |
/* only save the good data :-) */
|
sl@0
|
295 |
if (!B64_NOT_BASE64(v))
|
sl@0
|
296 |
{
|
sl@0
|
297 |
OPENSSL_assert(n < (int)sizeof(ctx->enc_data));
|
sl@0
|
298 |
d[n++]=tmp;
|
sl@0
|
299 |
ln++;
|
sl@0
|
300 |
}
|
sl@0
|
301 |
else if (v == B64_ERROR)
|
sl@0
|
302 |
{
|
sl@0
|
303 |
rv= -1;
|
sl@0
|
304 |
goto end;
|
sl@0
|
305 |
}
|
sl@0
|
306 |
|
sl@0
|
307 |
/* have we seen a '=' which is 'definitly' the last
|
sl@0
|
308 |
* input line. seof will point to the character that
|
sl@0
|
309 |
* holds it. and eof will hold how many characters to
|
sl@0
|
310 |
* chop off. */
|
sl@0
|
311 |
if (tmp == '=')
|
sl@0
|
312 |
{
|
sl@0
|
313 |
if (seof == -1) seof=n;
|
sl@0
|
314 |
eof++;
|
sl@0
|
315 |
}
|
sl@0
|
316 |
|
sl@0
|
317 |
if (v == B64_CR)
|
sl@0
|
318 |
{
|
sl@0
|
319 |
ln = 0;
|
sl@0
|
320 |
if (exp_nl)
|
sl@0
|
321 |
continue;
|
sl@0
|
322 |
}
|
sl@0
|
323 |
|
sl@0
|
324 |
/* eoln */
|
sl@0
|
325 |
if (v == B64_EOLN)
|
sl@0
|
326 |
{
|
sl@0
|
327 |
ln=0;
|
sl@0
|
328 |
if (exp_nl)
|
sl@0
|
329 |
{
|
sl@0
|
330 |
exp_nl=0;
|
sl@0
|
331 |
continue;
|
sl@0
|
332 |
}
|
sl@0
|
333 |
}
|
sl@0
|
334 |
exp_nl=0;
|
sl@0
|
335 |
|
sl@0
|
336 |
/* If we are at the end of input and it looks like a
|
sl@0
|
337 |
* line, process it. */
|
sl@0
|
338 |
if (((i+1) == inl) && (((n&3) == 0) || eof))
|
sl@0
|
339 |
{
|
sl@0
|
340 |
v=B64_EOF;
|
sl@0
|
341 |
/* In case things were given us in really small
|
sl@0
|
342 |
records (so two '=' were given in separate
|
sl@0
|
343 |
updates), eof may contain the incorrect number
|
sl@0
|
344 |
of ending bytes to skip, so let's redo the count */
|
sl@0
|
345 |
eof = 0;
|
sl@0
|
346 |
if (d[n-1] == '=') eof++;
|
sl@0
|
347 |
if (d[n-2] == '=') eof++;
|
sl@0
|
348 |
/* There will never be more than two '=' */
|
sl@0
|
349 |
}
|
sl@0
|
350 |
|
sl@0
|
351 |
if ((v == B64_EOF && (n&3) == 0) || (n >= 64))
|
sl@0
|
352 |
{
|
sl@0
|
353 |
/* This is needed to work correctly on 64 byte input
|
sl@0
|
354 |
* lines. We process the line and then need to
|
sl@0
|
355 |
* accept the '\n' */
|
sl@0
|
356 |
if ((v != B64_EOF) && (n >= 64)) exp_nl=1;
|
sl@0
|
357 |
tmp2=v;
|
sl@0
|
358 |
if (n > 0)
|
sl@0
|
359 |
{
|
sl@0
|
360 |
v=EVP_DecodeBlock(out,d,n);
|
sl@0
|
361 |
n=0;
|
sl@0
|
362 |
if (v < 0) { rv=0; goto end; }
|
sl@0
|
363 |
ret+=(v-eof);
|
sl@0
|
364 |
}
|
sl@0
|
365 |
else
|
sl@0
|
366 |
{
|
sl@0
|
367 |
eof=1;
|
sl@0
|
368 |
v=0;
|
sl@0
|
369 |
}
|
sl@0
|
370 |
|
sl@0
|
371 |
/* This is the case where we have had a short
|
sl@0
|
372 |
* but valid input line */
|
sl@0
|
373 |
if ((v < ctx->length) && eof)
|
sl@0
|
374 |
{
|
sl@0
|
375 |
rv=0;
|
sl@0
|
376 |
goto end;
|
sl@0
|
377 |
}
|
sl@0
|
378 |
else
|
sl@0
|
379 |
ctx->length=v;
|
sl@0
|
380 |
|
sl@0
|
381 |
if (seof >= 0) { rv=0; goto end; }
|
sl@0
|
382 |
out+=v;
|
sl@0
|
383 |
}
|
sl@0
|
384 |
}
|
sl@0
|
385 |
rv=1;
|
sl@0
|
386 |
end:
|
sl@0
|
387 |
*outl=ret;
|
sl@0
|
388 |
ctx->num=n;
|
sl@0
|
389 |
ctx->line_num=ln;
|
sl@0
|
390 |
ctx->expect_nl=exp_nl;
|
sl@0
|
391 |
return(rv);
|
sl@0
|
392 |
}
|
sl@0
|
393 |
|
sl@0
|
394 |
EXPORT_C int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n)
|
sl@0
|
395 |
{
|
sl@0
|
396 |
int i,ret=0,a,b,c,d;
|
sl@0
|
397 |
unsigned long l;
|
sl@0
|
398 |
|
sl@0
|
399 |
/* trim white space from the start of the line. */
|
sl@0
|
400 |
while ((conv_ascii2bin(*f) == B64_WS) && (n > 0))
|
sl@0
|
401 |
{
|
sl@0
|
402 |
f++;
|
sl@0
|
403 |
n--;
|
sl@0
|
404 |
}
|
sl@0
|
405 |
|
sl@0
|
406 |
/* strip off stuff at the end of the line
|
sl@0
|
407 |
* ascii2bin values B64_WS, B64_EOLN, B64_EOLN and B64_EOF */
|
sl@0
|
408 |
while ((n > 3) && (B64_NOT_BASE64(conv_ascii2bin(f[n-1]))))
|
sl@0
|
409 |
n--;
|
sl@0
|
410 |
|
sl@0
|
411 |
if (n%4 != 0) return(-1);
|
sl@0
|
412 |
|
sl@0
|
413 |
for (i=0; i<n; i+=4)
|
sl@0
|
414 |
{
|
sl@0
|
415 |
a=conv_ascii2bin(*(f++));
|
sl@0
|
416 |
b=conv_ascii2bin(*(f++));
|
sl@0
|
417 |
c=conv_ascii2bin(*(f++));
|
sl@0
|
418 |
d=conv_ascii2bin(*(f++));
|
sl@0
|
419 |
if ( (a & 0x80) || (b & 0x80) ||
|
sl@0
|
420 |
(c & 0x80) || (d & 0x80))
|
sl@0
|
421 |
return(-1);
|
sl@0
|
422 |
l=( (((unsigned long)a)<<18L)|
|
sl@0
|
423 |
(((unsigned long)b)<<12L)|
|
sl@0
|
424 |
(((unsigned long)c)<< 6L)|
|
sl@0
|
425 |
(((unsigned long)d) ));
|
sl@0
|
426 |
*(t++)=(unsigned char)(l>>16L)&0xff;
|
sl@0
|
427 |
*(t++)=(unsigned char)(l>> 8L)&0xff;
|
sl@0
|
428 |
*(t++)=(unsigned char)(l )&0xff;
|
sl@0
|
429 |
ret+=3;
|
sl@0
|
430 |
}
|
sl@0
|
431 |
return(ret);
|
sl@0
|
432 |
}
|
sl@0
|
433 |
|
sl@0
|
434 |
EXPORT_C int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl)
|
sl@0
|
435 |
{
|
sl@0
|
436 |
int i;
|
sl@0
|
437 |
|
sl@0
|
438 |
*outl=0;
|
sl@0
|
439 |
if (ctx->num != 0)
|
sl@0
|
440 |
{
|
sl@0
|
441 |
i=EVP_DecodeBlock(out,ctx->enc_data,ctx->num);
|
sl@0
|
442 |
if (i < 0) return(-1);
|
sl@0
|
443 |
ctx->num=0;
|
sl@0
|
444 |
*outl=i;
|
sl@0
|
445 |
return(1);
|
sl@0
|
446 |
}
|
sl@0
|
447 |
else
|
sl@0
|
448 |
return(1);
|
sl@0
|
449 |
}
|
sl@0
|
450 |
|
sl@0
|
451 |
#ifdef undef
|
sl@0
|
452 |
EXPORT_C int EVP_DecodeValid(unsigned char *buf, int len)
|
sl@0
|
453 |
{
|
sl@0
|
454 |
int i,num=0,bad=0;
|
sl@0
|
455 |
|
sl@0
|
456 |
if (len == 0) return(-1);
|
sl@0
|
457 |
while (conv_ascii2bin(*buf) == B64_WS)
|
sl@0
|
458 |
{
|
sl@0
|
459 |
buf++;
|
sl@0
|
460 |
len--;
|
sl@0
|
461 |
if (len == 0) return(-1);
|
sl@0
|
462 |
}
|
sl@0
|
463 |
|
sl@0
|
464 |
for (i=len; i >= 4; i-=4)
|
sl@0
|
465 |
{
|
sl@0
|
466 |
if ( (conv_ascii2bin(buf[0]) >= 0x40) ||
|
sl@0
|
467 |
(conv_ascii2bin(buf[1]) >= 0x40) ||
|
sl@0
|
468 |
(conv_ascii2bin(buf[2]) >= 0x40) ||
|
sl@0
|
469 |
(conv_ascii2bin(buf[3]) >= 0x40))
|
sl@0
|
470 |
return(-1);
|
sl@0
|
471 |
buf+=4;
|
sl@0
|
472 |
num+=1+(buf[2] != '=')+(buf[3] != '=');
|
sl@0
|
473 |
}
|
sl@0
|
474 |
if ((i == 1) && (conv_ascii2bin(buf[0]) == B64_EOLN))
|
sl@0
|
475 |
return(num);
|
sl@0
|
476 |
if ((i == 2) && (conv_ascii2bin(buf[0]) == B64_EOLN) &&
|
sl@0
|
477 |
(conv_ascii2bin(buf[0]) == B64_EOLN))
|
sl@0
|
478 |
return(num);
|
sl@0
|
479 |
return(1);
|
sl@0
|
480 |
}
|
sl@0
|
481 |
#endif
|