57 #include <config_auto.h>
62 #include "allheaders.h"
65 static const l_int32 MAX_BASE64_LINE = 72;
66 static const char *tablechar64 =
67 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
68 "abcdefghijklmnopqrstuvwxyz"
71 static l_int32 isBase64(
char);
72 static l_int32 *genReverseTab64(
void);
73 static void byteConvert3to4(l_uint8 *in3, l_uint8 *out4);
74 static void byteConvert4to3(l_uint8 *in4, l_uint8 *out3);
77 static const l_int32 MAX_ASCII85_LINE = 64;
78 static const l_uint32 power85[5] = {1,
84 static l_int32 convertChunkToAscii85(
const l_uint8 *inarray,
size_t insize,
85 l_int32 *pindex,
char *outbuf,
107 encodeBase64(
const l_uint8 *inarray,
112 const l_uint8 *bytea;
113 l_uint8 array3[3], array4[4];
114 l_int32 outsize, i, j, index, linecount;
116 PROCNAME(
"encodeBase64");
119 return (
char *)ERROR_PTR(
"&outsize not defined", procName, NULL);
122 return (
char *)ERROR_PTR(
"inarray not defined", procName, NULL);
124 return (
char *)ERROR_PTR(
"insize not > 0", procName, NULL);
129 outsize = 4 * ((insize + 2) / 3);
130 outsize += outsize / MAX_BASE64_LINE + 4;
131 if ((chara = (
char *)LEPT_CALLOC(outsize,
sizeof(
char))) == NULL)
132 return (
char *)ERROR_PTR(
"chara not made", procName, NULL);
136 i = index = linecount = 0;
139 if (linecount == MAX_BASE64_LINE) {
140 chara[index++] =
'\n';
143 array3[i++] = *bytea++;
145 byteConvert3to4(array3, array4);
146 for (j = 0; j < 4; j++)
147 chara[index++] = tablechar64[array4[j]];
161 for (j = i; j < 3; j++)
163 byteConvert3to4(array3, array4);
164 for (j = 0; j <= i; j++)
165 chara[index++] = tablechar64[array4[j]];
166 for (j = i + 1; j < 4; j++)
167 chara[index++] =
'=';
195 decodeBase64(
const char *inarray,
201 l_uint8 array3[3], array4[4];
203 l_int32 i, j, outsize, in_index, out_index;
205 PROCNAME(
"decodeBase64");
208 return (l_uint8 *)ERROR_PTR(
"&outsize not defined", procName, NULL);
211 return (l_uint8 *)ERROR_PTR(
"inarray not defined", procName, NULL);
213 return (l_uint8 *)ERROR_PTR(
"insize not > 0", procName, NULL);
216 for (i = 0; i < insize; i++) {
218 if (inchar ==
'\n')
continue;
219 if (isBase64(inchar) == 0 && inchar !=
'=')
220 return (l_uint8 *)ERROR_PTR(
"invalid char in inarray",
230 outsize = 3 * ((insize + 3) / 4) + 4;
231 if ((bytea = (l_uint8 *)LEPT_CALLOC(outsize,
sizeof(l_uint8))) == NULL)
232 return (l_uint8 *)ERROR_PTR(
"bytea not made", procName, NULL);
240 rtable64 = genReverseTab64();
241 i = in_index = out_index = 0;
242 for (in_index = 0; in_index < insize; in_index++) {
243 inchar = inarray[in_index];
244 if (inchar ==
'\n')
continue;
245 if (inchar ==
'=')
break;
246 array4[i++] = rtable64[(
unsigned char)inchar];
250 byteConvert4to3(array4, array3);
251 for (j = 0; j < 3; j++)
252 bytea[out_index++] = array3[j];
261 for (j = i; j < 4; j++)
263 byteConvert4to3(array4, array3);
264 for (j = 0; j < i - 1; j++)
265 bytea[out_index++] = array3[j];
267 *poutsize = out_index;
280 return (isalnum(((
int)c)) || ((c) ==
'+') || ((c) ==
'/')) ? 1 : 0;
292 rtable64 = (l_int32 *)LEPT_CALLOC(128,
sizeof(l_int32));
293 for (i = 0; i < 64; i++) {
294 rtable64[(
unsigned char)tablechar64[i]] = i;
303 byteConvert3to4(l_uint8 *in3,
306 out4[0] = in3[0] >> 2;
307 out4[1] = ((in3[0] & 0x03) << 4) | (in3[1] >> 4);
308 out4[2] = ((in3[1] & 0x0f) << 2) | (in3[2] >> 6);
309 out4[3] = in3[2] & 0x3f;
317 byteConvert4to3(l_uint8 *in4,
320 out3[0] = (in4[0] << 2) | (in4[1] >> 4);
321 out3[1] = ((in4[1] & 0x0f) << 4) | (in4[2] >> 2);
322 out3[2] = ((in4[2] & 0x03) << 6) | in4[3];
346 encodeAscii85(
const l_uint8 *inarray,
352 l_int32 maxsize, i, index, linecount, nbout, eof;
355 PROCNAME(
"encodeAscii85");
358 return (
char *)ERROR_PTR(
"&outsize not defined", procName, NULL);
361 return (
char *)ERROR_PTR(
"inarray not defined", procName, NULL);
363 return (
char *)ERROR_PTR(
"insize not > 0", procName, NULL);
366 maxsize = (l_int32)(80. + (insize * 5. / 4.) *
367 (1. + 2. / MAX_ASCII85_LINE));
368 if ((chara = (
char *)LEPT_CALLOC(maxsize,
sizeof(
char))) == NULL)
369 return (
char *)ERROR_PTR(
"chara not made", procName, NULL);
375 eof = convertChunkToAscii85(inarray, insize, &index, outbuf, &nbout);
376 for (i = 0; i < nbout; i++) {
377 chara[outindex++] = outbuf[i];
379 if (linecount >= MAX_ASCII85_LINE) {
380 chara[outindex++] =
'\n';
386 chara[outindex++] =
'\n';
387 chara[outindex++] =
'~';
388 chara[outindex++] =
'>';
389 chara[outindex++] =
'\n';
394 *poutsize = outindex;
416 convertChunkToAscii85(
const l_uint8 *inarray,
423 l_uint32 inword, val;
424 l_int32 eof, index, nread, nbout, i;
428 nread = L_MIN(4, (insize - index));
429 if (insize == index + nread)
435 for (i = 0; i < nread; i++) {
436 inbyte = inarray[index + i];
437 inword += (l_uint32)inbyte << (8 * (3 - i));
441 lept_stderr(
"index = %d, nread = %d\n", index, nread);
451 for (i = 4; i >= 4 - nread; i--) {
452 val = inword / power85[i];
453 outbuf[4 - i] = (l_uint8)(val +
'!');
454 inword -= val * power85[i];
481 decodeAscii85(
const char *inarray,
489 l_int32 maxsize, ocount, bytecount, index;
492 PROCNAME(
"decodeAscii85");
495 return (l_uint8 *)ERROR_PTR(
"&outsize not defined", procName, NULL);
498 return (l_uint8 *)ERROR_PTR(
"inarray not defined", procName, NULL);
500 return (l_uint8 *)ERROR_PTR(
"insize not > 0", procName, NULL);
503 maxsize = (l_int32)(80. + (insize * 4. / 5.));
504 if ((outa = (l_uint8 *)LEPT_CALLOC(maxsize,
sizeof(l_uint8))) == NULL)
505 return (l_uint8 *)ERROR_PTR(
"outa not made", procName, NULL);
510 for (index = 0, bytecount = 0; index < insize; index++, pin++) {
513 if (inc ==
' ' || inc ==
'\t' || inc ==
'\n' ||
514 inc ==
'\f' || inc ==
'\r' || inc ==
'\v')
519 oword = oword * 85 + val;
523 outa[ocount] = (oword >> 24) & 0xff;
524 outa[ocount + 1] = (oword >> 16) & 0xff;
525 outa[ocount + 2] = (oword >> 8) & 0xff;
526 outa[ocount + 3] = oword & 0xff;
531 }
else if (inc ==
'z' && bytecount == 0) {
533 outa[ocount + 1] = 0;
534 outa[ocount + 2] = 0;
535 outa[ocount + 3] = 0;
537 }
else if (inc ==
'~') {
538 L_INFO(
" %d extra bytes output\n", procName, bytecount - 1);
544 oword = oword * power85[3] + 0xffffff;
545 outa[ocount] = (oword >> 24) & 0xff;
548 oword = oword * power85[2] + 0xffff;
549 outa[ocount] = (oword >> 24) & 0xff;
550 outa[ocount + 1] = (oword >> 16) & 0xff;
553 oword = oword * 85 + 0xff;
554 outa[ocount] = (oword >> 24) & 0xff;
555 outa[ocount + 1] = (oword >> 16) & 0xff;
556 outa[ocount + 2] = (oword >> 8) & 0xff;
560 ocount += (bytecount - 1);
586 encodeAscii85WithComp(
const l_uint8 *indata,
594 PROCNAME(
"encodeAscii85WithComp");
597 return (
char *)ERROR_PTR(
"&outsize not defined", procName, NULL);
600 return (
char *)ERROR_PTR(
"indata not defined", procName, NULL);
602 if ((data1 =
zlibCompress(indata, insize, &size1)) == NULL)
603 return (
char *)ERROR_PTR(
"data1 not made", procName, NULL);
604 outstr = encodeAscii85(data1, size1, poutsize);
627 decodeAscii85WithComp(
const char *instr,
632 l_uint8 *data1, *outdata;
634 PROCNAME(
"decodeAscii85WithComp");
637 return (l_uint8 *)ERROR_PTR(
"&outsize not defined", procName, NULL);
640 return (l_uint8 *)ERROR_PTR(
"instr not defined", procName, NULL);
642 if (insize == 0) insize = strlen(instr);
643 if ((data1 = decodeAscii85(instr, insize, &size1)) == NULL)
644 return (l_uint8 *)ERROR_PTR(
"data1 not made", procName, NULL);
676 reformatPacked64(
const char *inarray,
684 l_int32 i, j, flatindex, flatsize, outindex, nlines, linewithpad, linecount;
686 PROCNAME(
"reformatPacked64");
689 return (
char *)ERROR_PTR(
"&outsize not defined", procName, NULL);
692 return (
char *)ERROR_PTR(
"inarray not defined", procName, NULL);
694 return (
char *)ERROR_PTR(
"insize not > 0", procName, NULL);
696 return (
char *)ERROR_PTR(
"leadspace must be >= 0", procName, NULL);
698 return (
char *)ERROR_PTR(
"linechars % 4 must be 0", procName, NULL);
701 if ((flata = (
char *)LEPT_CALLOC(insize,
sizeof(
char))) == NULL)
702 return (
char *)ERROR_PTR(
"flata not made", procName, NULL);
703 for (i = 0, flatindex = 0; i < insize; i++) {
704 if (isBase64(inarray[i]) || inarray[i] ==
'=')
705 flata[flatindex++] = inarray[i];
709 flatsize = flatindex;
710 nlines = (flatsize + linechars - 1) / linechars;
711 linewithpad = leadspace + linechars + 1;
712 if (addquotes) linewithpad += 2;
713 if ((outa = (
char *)LEPT_CALLOC((
size_t)nlines * linewithpad,
714 sizeof(
char))) == NULL) {
716 return (
char *)ERROR_PTR(
"outa not made", procName, NULL);
718 for (j = 0, outindex = 0; j < leadspace; j++)
719 outa[outindex++] =
' ';
720 if (addquotes) outa[outindex++] =
'"';
721 for (i = 0, linecount = 0; i < flatsize; i++) {
722 if (linecount == linechars) {
723 if (addquotes) outa[outindex++] =
'"';
724 outa[outindex++] =
'\n';
725 for (j = 0; j < leadspace; j++)
726 outa[outindex++] =
' ';
727 if (addquotes) outa[outindex++] =
'"';
730 outa[outindex++] = flata[i];
733 if (addquotes) outa[outindex++] =
'"';
734 *poutsize = outindex;
void lept_stderr(const char *fmt,...)
lept_stderr()
l_uint8 * zlibUncompress(const l_uint8 *datain, size_t nin, size_t *pnout)
zlibUncompress()
l_uint8 * zlibCompress(const l_uint8 *datain, size_t nin, size_t *pnout)
zlibCompress()