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;
117 return (
char *)ERROR_PTR(
"&outsize not defined", __func__, NULL);
120 return (
char *)ERROR_PTR(
"inarray not defined", __func__, NULL);
122 return (
char *)ERROR_PTR(
"insize not > 0", __func__, NULL);
127 outsize = 4 * ((insize + 2) / 3);
128 outsize += outsize / MAX_BASE64_LINE + 4;
129 if ((chara = (
char *)LEPT_CALLOC(outsize,
sizeof(
char))) == NULL)
130 return (
char *)ERROR_PTR(
"chara not made", __func__, NULL);
134 i = index = linecount = 0;
137 if (linecount == MAX_BASE64_LINE) {
138 chara[index++] =
'\n';
141 array3[i++] = *bytea++;
143 byteConvert3to4(array3, array4);
144 for (j = 0; j < 4; j++)
145 chara[index++] = tablechar64[array4[j]];
159 for (j = i; j < 3; j++)
161 byteConvert3to4(array3, array4);
162 for (j = 0; j <= i; j++)
163 chara[index++] = tablechar64[array4[j]];
164 for (j = i + 1; j < 4; j++)
165 chara[index++] =
'=';
193 decodeBase64(
const char *inarray,
199 l_uint8 array3[3], array4[4];
201 l_int32 i, j, outsize, in_index, out_index;
204 return (l_uint8 *)ERROR_PTR(
"&outsize not defined", __func__, NULL);
207 return (l_uint8 *)ERROR_PTR(
"inarray not defined", __func__, NULL);
209 return (l_uint8 *)ERROR_PTR(
"insize not > 0", __func__, NULL);
212 for (i = 0; i < insize; i++) {
214 if (inchar ==
'\n')
continue;
215 if (isBase64(inchar) == 0 && inchar !=
'=')
216 return (l_uint8 *)ERROR_PTR(
"invalid char in inarray",
226 outsize = 3 * ((insize + 3) / 4) + 4;
227 if ((bytea = (l_uint8 *)LEPT_CALLOC(outsize,
sizeof(l_uint8))) == NULL)
228 return (l_uint8 *)ERROR_PTR(
"bytea not made", __func__, NULL);
236 rtable64 = genReverseTab64();
237 i = in_index = out_index = 0;
238 for (in_index = 0; in_index < insize; in_index++) {
239 inchar = inarray[in_index];
240 if (inchar ==
'\n')
continue;
241 if (inchar ==
'=')
break;
242 array4[i++] = rtable64[(
unsigned char)inchar];
246 byteConvert4to3(array4, array3);
247 for (j = 0; j < 3; j++)
248 bytea[out_index++] = array3[j];
257 for (j = i; j < 4; j++)
259 byteConvert4to3(array4, array3);
260 for (j = 0; j < i - 1; j++)
261 bytea[out_index++] = array3[j];
263 *poutsize = out_index;
276 return (isalnum(((
int)c)) || ((c) ==
'+') || ((c) ==
'/')) ? 1 : 0;
288 rtable64 = (l_int32 *)LEPT_CALLOC(128,
sizeof(l_int32));
289 for (i = 0; i < 64; i++) {
290 rtable64[(
unsigned char)tablechar64[i]] = i;
299 byteConvert3to4(l_uint8 *in3,
302 out4[0] = in3[0] >> 2;
303 out4[1] = ((in3[0] & 0x03) << 4) | (in3[1] >> 4);
304 out4[2] = ((in3[1] & 0x0f) << 2) | (in3[2] >> 6);
305 out4[3] = in3[2] & 0x3f;
313 byteConvert4to3(l_uint8 *in4,
316 out3[0] = (in4[0] << 2) | (in4[1] >> 4);
317 out3[1] = ((in4[1] & 0x0f) << 4) | (in4[2] >> 2);
318 out3[2] = ((in4[2] & 0x03) << 6) | in4[3];
342 encodeAscii85(
const l_uint8 *inarray,
348 l_int32 maxsize, i, index, linecount, nbout, eof;
352 return (
char *)ERROR_PTR(
"&outsize not defined", __func__, NULL);
355 return (
char *)ERROR_PTR(
"inarray not defined", __func__, NULL);
357 return (
char *)ERROR_PTR(
"insize not > 0", __func__, NULL);
360 maxsize = (l_int32)(80. + (insize * 5. / 4.) *
361 (1. + 2. / MAX_ASCII85_LINE));
362 if ((chara = (
char *)LEPT_CALLOC(maxsize,
sizeof(
char))) == NULL)
363 return (
char *)ERROR_PTR(
"chara not made", __func__, NULL);
369 eof = convertChunkToAscii85(inarray, insize, &index, outbuf, &nbout);
370 for (i = 0; i < nbout; i++) {
371 chara[outindex++] = outbuf[i];
373 if (linecount >= MAX_ASCII85_LINE) {
374 chara[outindex++] =
'\n';
380 chara[outindex++] =
'\n';
381 chara[outindex++] =
'~';
382 chara[outindex++] =
'>';
383 chara[outindex++] =
'\n';
388 *poutsize = outindex;
410 convertChunkToAscii85(
const l_uint8 *inarray,
417 l_uint32 inword, val;
418 l_int32 eof, index, nread, nbout, i;
422 nread = L_MIN(4, (insize - index));
423 if (insize == index + nread)
429 for (i = 0; i < nread; i++) {
430 inbyte = inarray[index + i];
431 inword += (l_uint32)inbyte << (8 * (3 - i));
435 lept_stderr(
"index = %d, nread = %d\n", index, nread);
445 for (i = 4; i >= 4 - nread; i--) {
446 val = inword / power85[i];
447 outbuf[4 - i] = (l_uint8)(val +
'!');
448 inword -= val * power85[i];
475 decodeAscii85(
const char *inarray,
483 l_int32 maxsize, ocount, bytecount, index;
487 return (l_uint8 *)ERROR_PTR(
"&outsize not defined", __func__, NULL);
490 return (l_uint8 *)ERROR_PTR(
"inarray not defined", __func__, NULL);
492 return (l_uint8 *)ERROR_PTR(
"insize not > 0", __func__, 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", __func__, 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", __func__, 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);
578 encodeAscii85WithComp(
const l_uint8 *indata,
587 return (
char *)ERROR_PTR(
"&outsize not defined", __func__, NULL);
590 return (
char *)ERROR_PTR(
"indata not defined", __func__, NULL);
592 if ((data1 =
zlibCompress(indata, insize, &size1)) == NULL)
593 return (
char *)ERROR_PTR(
"data1 not made", __func__, NULL);
594 outstr = encodeAscii85(data1, size1, poutsize);
617 decodeAscii85WithComp(
const char *instr,
622 l_uint8 *data1, *outdata;
625 return (l_uint8 *)ERROR_PTR(
"&outsize not defined", __func__, NULL);
628 return (l_uint8 *)ERROR_PTR(
"instr not defined", __func__, NULL);
630 if (insize == 0) insize = strlen(instr);
631 if ((data1 = decodeAscii85(instr, insize, &size1)) == NULL)
632 return (l_uint8 *)ERROR_PTR(
"data1 not made", __func__, NULL);
664 reformatPacked64(
const char *inarray,
672 l_int32 i, j, flatindex, flatsize, outindex, nlines, linewithpad, linecount;
675 return (
char *)ERROR_PTR(
"&outsize not defined", __func__, NULL);
678 return (
char *)ERROR_PTR(
"inarray not defined", __func__, NULL);
680 return (
char *)ERROR_PTR(
"insize not > 0", __func__, NULL);
682 return (
char *)ERROR_PTR(
"leadspace must be >= 0", __func__, NULL);
684 return (
char *)ERROR_PTR(
"linechars % 4 must be 0", __func__, NULL);
687 if ((flata = (
char *)LEPT_CALLOC(insize,
sizeof(
char))) == NULL)
688 return (
char *)ERROR_PTR(
"flata not made", __func__, NULL);
689 for (i = 0, flatindex = 0; i < insize; i++) {
690 if (isBase64(inarray[i]) || inarray[i] ==
'=')
691 flata[flatindex++] = inarray[i];
695 flatsize = flatindex;
696 nlines = (flatsize + linechars - 1) / linechars;
697 linewithpad = leadspace + linechars + 1;
698 if (addquotes) linewithpad += 2;
699 if ((outa = (
char *)LEPT_CALLOC((
size_t)nlines * linewithpad,
700 sizeof(
char))) == NULL) {
702 return (
char *)ERROR_PTR(
"outa not made", __func__, NULL);
704 for (j = 0, outindex = 0; j < leadspace; j++)
705 outa[outindex++] =
' ';
706 if (addquotes) outa[outindex++] =
'"';
707 for (i = 0, linecount = 0; i < flatsize; i++) {
708 if (linecount == linechars) {
709 if (addquotes) outa[outindex++] =
'"';
710 outa[outindex++] =
'\n';
711 for (j = 0; j < leadspace; j++)
712 outa[outindex++] =
' ';
713 if (addquotes) outa[outindex++] =
'"';
716 outa[outindex++] = flata[i];
719 if (addquotes) outa[outindex++] =
'"';
720 *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()