54 #include "allheaders.h" 57 static const l_int32 MAX_BASE64_LINE = 72;
58 static const char *tablechar64 =
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 60 "abcdefghijklmnopqrstuvwxyz" 63 static l_int32 isBase64(
char);
64 static l_int32 *genReverseTab64(
void);
65 static void byteConvert3to4(l_uint8 *in3, l_uint8 *out4);
66 static void byteConvert4to3(l_uint8 *in4, l_uint8 *out3);
69 static const l_int32 MAX_ASCII85_LINE = 64;
70 static const l_uint32 power85[5] = {1,
76 static l_int32 convertChunkToAscii85(l_uint8 *inarray, l_int32 insize,
77 l_int32 *pindex,
char *outbuf,
100 encodeBase64(l_uint8 *inarray,
106 l_uint8 array3[3], array4[4];
107 l_int32 outsize, i, j, index, linecount;
109 PROCNAME(
"encodeBase64");
112 return (
char *)ERROR_PTR(
"&outsize not defined", procName, NULL);
115 return (
char *)ERROR_PTR(
"inarray not defined", procName, NULL);
117 return (
char *)ERROR_PTR(
"insize not > 0", procName, NULL);
122 outsize = 4 * ((insize + 2) / 3);
123 outsize += outsize / MAX_BASE64_LINE + 4;
124 if ((chara = (
char *)LEPT_CALLOC(outsize,
sizeof(
char))) == NULL)
125 return (
char *)ERROR_PTR(
"chara not made", procName, NULL);
129 i = index = linecount = 0;
132 if (linecount == MAX_BASE64_LINE) {
133 chara[index++] =
'\n';
136 array3[i++] = *bytea++;
138 byteConvert3to4(array3, array4);
139 for (j = 0; j < 4; j++)
140 chara[index++] = tablechar64[array4[j]];
154 for (j = i; j < 3; j++)
156 byteConvert3to4(array3, array4);
157 for (j = 0; j <= i; j++)
158 chara[index++] = tablechar64[array4[j]];
159 for (j = i + 1; j < 4; j++)
160 chara[index++] =
'=';
188 decodeBase64(
const char *inarray,
194 l_uint8 array3[3], array4[4];
196 l_int32 i, j, outsize, in_index, out_index;
198 PROCNAME(
"decodeBase64");
201 return (l_uint8 *)ERROR_PTR(
"&outsize not defined", procName, NULL);
204 return (l_uint8 *)ERROR_PTR(
"inarray not defined", procName, NULL);
206 return (l_uint8 *)ERROR_PTR(
"insize not > 0", procName, NULL);
209 for (i = 0; i < insize; i++) {
211 if (inchar ==
'\n')
continue;
212 if (isBase64(inchar) == 0 && inchar !=
'=')
213 return (l_uint8 *)ERROR_PTR(
"invalid char in inarray",
223 outsize = 3 * ((insize + 3) / 4) + 4;
224 if ((bytea = (l_uint8 *)LEPT_CALLOC(outsize,
sizeof(l_uint8))) == NULL)
225 return (l_uint8 *)ERROR_PTR(
"bytea not made", procName, NULL);
233 rtable64 = genReverseTab64();
234 i = in_index = out_index = 0;
235 for (in_index = 0; in_index < insize; in_index++) {
236 inchar = inarray[in_index];
237 if (inchar ==
'\n')
continue;
238 if (inchar ==
'=')
break;
239 array4[i++] = rtable64[(
unsigned char)inchar];
243 byteConvert4to3(array4, array3);
244 for (j = 0; j < 3; j++)
245 bytea[out_index++] = array3[j];
254 for (j = i; j < 4; j++)
256 byteConvert4to3(array4, array3);
257 for (j = 0; j < i - 1; j++)
258 bytea[out_index++] = array3[j];
260 *poutsize = out_index;
273 return (isalnum(((
int)c)) || ((c) ==
'+') || ((c) ==
'/')) ? 1 : 0;
285 rtable64 = (l_int32 *)LEPT_CALLOC(128,
sizeof(l_int32));
286 for (i = 0; i < 64; i++) {
287 rtable64[(
unsigned char)tablechar64[i]] = i;
296 byteConvert3to4(l_uint8 *in3,
299 out4[0] = in3[0] >> 2;
300 out4[1] = ((in3[0] & 0x03) << 4) | (in3[1] >> 4);
301 out4[2] = ((in3[1] & 0x0f) << 2) | (in3[2] >> 6);
302 out4[3] = in3[2] & 0x3f;
310 byteConvert4to3(l_uint8 *in4,
313 out3[0] = (in4[0] << 2) | (in4[1] >> 4);
314 out3[1] = ((in4[1] & 0x0f) << 4) | (in4[2] >> 2);
315 out3[2] = ((in4[2] & 0x03) << 6) | in4[3];
339 encodeAscii85(l_uint8 *inarray,
345 l_int32 maxsize, i, index, outindex, linecount, nbout, eof;
347 PROCNAME(
"encodeAscii85");
350 return (
char *)ERROR_PTR(
"&outsize not defined", procName, NULL);
353 return (
char *)ERROR_PTR(
"inarray not defined", procName, NULL);
355 return (
char *)ERROR_PTR(
"insize not > 0", procName, NULL);
358 maxsize = (l_int32)(80. + (insize * 5. / 4.) *
359 (1. + 2. / MAX_ASCII85_LINE));
360 if ((chara = (
char *)LEPT_CALLOC(maxsize,
sizeof(
char))) == NULL)
361 return (
char *)ERROR_PTR(
"chara not made", procName, NULL);
367 eof = convertChunkToAscii85(inarray, insize, &index, outbuf, &nbout);
368 for (i = 0; i < nbout; i++) {
369 chara[outindex++] = outbuf[i];
371 if (linecount >= MAX_ASCII85_LINE) {
372 chara[outindex++] =
'\n';
378 chara[outindex++] =
'\n';
379 chara[outindex++] =
'~';
380 chara[outindex++] =
'>';
381 chara[outindex++] =
'\n';
386 *poutsize = outindex;
408 convertChunkToAscii85(l_uint8 *inarray,
415 l_uint32 inword, val;
416 l_int32 eof, index, nread, nbout, i;
420 nread = L_MIN(4, (insize - index));
421 if (insize == index + nread)
427 for (i = 0; i < nread; i++) {
428 inbyte = inarray[index + i];
429 inword += inbyte << (8 * (3 - i));
433 fprintf(stderr,
"index = %d, nread = %d\n", index, nread);
434 fprintf(stderr,
"inword = %x\n", inword);
435 fprintf(stderr,
"eof = %d\n", eof);
443 for (i = 4; i >= 4 - nread; i--) {
444 val = inword / power85[i];
445 outbuf[4 - i] = (l_uint8)(val +
'!');
446 inword -= val * power85[i];
473 decodeAscii85(
char *inarray,
481 l_int32 maxsize, ocount, bytecount, index;
484 PROCNAME(
"decodeAscii85");
487 return (l_uint8 *)ERROR_PTR(
"&outsize not defined", procName, NULL);
490 return (l_uint8 *)ERROR_PTR(
"inarray not defined", procName, NULL);
492 return (l_uint8 *)ERROR_PTR(
"insize not > 0", procName, NULL);
495 maxsize = (l_int32)(80. + (insize * 4. / 5.));
496 if ((outa = (l_uint8 *)LEPT_CALLOC(maxsize,
sizeof(l_uint8))) == NULL)
497 return (l_uint8 *)ERROR_PTR(
"outa not made", procName, NULL);
502 for (index = 0, bytecount = 0; index < insize; index++, pin++) {
505 if (inc ==
' ' || inc ==
'\t' || inc ==
'\n' ||
506 inc ==
'\f' || inc ==
'\r' || inc ==
'\v')
511 oword = oword * 85 + val;
515 outa[ocount] = (oword >> 24) & 0xff;
516 outa[ocount + 1] = (oword >> 16) & 0xff;
517 outa[ocount + 2] = (oword >> 8) & 0xff;
518 outa[ocount + 3] = oword & 0xff;
523 }
else if (inc ==
'z' && bytecount == 0) {
525 outa[ocount + 1] = 0;
526 outa[ocount + 2] = 0;
527 outa[ocount + 3] = 0;
529 }
else if (inc ==
'~') {
530 L_INFO(
" %d extra bytes output\n", procName, bytecount - 1);
536 oword = oword * power85[3] + 0xffffff;
537 outa[ocount] = (oword >> 24) & 0xff;
540 oword = oword * power85[2] + 0xffff;
541 outa[ocount] = (oword >> 24) & 0xff;
542 outa[ocount + 1] = (oword >> 16) & 0xff;
545 oword = oword * 85 + 0xff;
546 outa[ocount] = (oword >> 24) & 0xff;
547 outa[ocount + 1] = (oword >> 16) & 0xff;
548 outa[ocount + 2] = (oword >> 8) & 0xff;
552 ocount += (bytecount - 1);
587 reformatPacked64(
char *inarray,
595 l_int32 i, j, flatindex, flatsize, outindex,
nlines, linewithpad, linecount;
597 PROCNAME(
"reformatPacked64");
600 return (
char *)ERROR_PTR(
"&outsize not defined", procName, NULL);
603 return (
char *)ERROR_PTR(
"inarray not defined", procName, NULL);
605 return (
char *)ERROR_PTR(
"insize not > 0", procName, NULL);
607 return (
char *)ERROR_PTR(
"leadspace must be >= 0", procName, NULL);
609 return (
char *)ERROR_PTR(
"linechars % 4 must be 0", procName, NULL);
612 if ((flata = (
char *)LEPT_CALLOC(insize,
sizeof(
char))) == NULL)
613 return (
char *)ERROR_PTR(
"flata not made", procName, NULL);
614 for (i = 0, flatindex = 0; i < insize; i++) {
615 if (isBase64(inarray[i]) || inarray[i] ==
'=')
616 flata[flatindex++] = inarray[i];
620 flatsize = flatindex;
621 nlines = (flatsize + linechars - 1) / linechars;
622 linewithpad = leadspace + linechars + 1;
623 if (addquotes) linewithpad += 2;
624 if ((outa = (
char *)LEPT_CALLOC((
size_t)
nlines * linewithpad,
625 sizeof(
char))) == NULL) {
627 return (
char *)ERROR_PTR(
"outa not made", procName, NULL);
629 for (j = 0, outindex = 0; j < leadspace; j++)
630 outa[outindex++] =
' ';
631 if (addquotes) outa[outindex++] =
'"';
632 for (i = 0, linecount = 0; i < flatsize; i++) {
633 if (linecount == linechars) {
634 if (addquotes) outa[outindex++] =
'"';
635 outa[outindex++] =
'\n';
636 for (j = 0; j < leadspace; j++)
637 outa[outindex++] =
' ';
638 if (addquotes) outa[outindex++] =
'"';
641 outa[outindex++] = flata[i];
644 if (addquotes) outa[outindex++] =
'"';
645 *poutsize = outindex;