MCPcopy Create free account
hub / github.com/creatale/node-dv / encodeBase64

Function encodeBase64

deps/leptonica/src/encoding.c:99–165  ·  view source on GitHub ↗

-------------------------------------------------------------* * Utility for encoding and decoding data with base64 * *-------------------------------------------------------------*/ ! * \brief encodeBase64() * * \param[in] inarray input binary data * \param[in] insize number of bytes in input array * \param[out] poutsize number of bytes in output char array * \return c

Source from the content-addressed store, hash-verified

97 * </pre>
98 */
99char *
100encodeBase64(l_uint8 *inarray,
101 l_int32 insize,
102 l_int32 *poutsize)
103{
104char *chara;
105l_uint8 *bytea;
106l_uint8 array3[3], array4[4];
107l_int32 outsize, i, j, index, linecount;
108
109 PROCNAME("encodeBase64");
110
111 if (!poutsize)
112 return (char *)ERROR_PTR("&outsize not defined", procName, NULL);
113 *poutsize = 0;
114 if (!inarray)
115 return (char *)ERROR_PTR("inarray not defined", procName, NULL);
116 if (insize <= 0)
117 return (char *)ERROR_PTR("insize not > 0", procName, NULL);
118
119 /* The output array is padded to a multiple of 4 bytes, not
120 * counting the newlines. We just need to allocate a large
121 * enough array, and add 4 bytes to make sure it is big enough. */
122 outsize = 4 * ((insize + 2) / 3); /* without newlines */
123 outsize += outsize / MAX_BASE64_LINE + 4; /* with the newlines */
124 if ((chara = (char *)LEPT_CALLOC(outsize, sizeof(char))) == NULL)
125 return (char *)ERROR_PTR("chara not made", procName, NULL);
126
127 /* Read all the input data, and convert in sets of 3 input
128 * bytes --> 4 output bytes. */
129 i = index = linecount = 0;
130 bytea = inarray;
131 while (insize--) {
132 if (linecount == MAX_BASE64_LINE) {
133 chara[index++] = '\n';
134 linecount = 0;
135 }
136 array3[i++] = *bytea++;
137 if (i == 3) { /* convert 3 to 4 and save */
138 byteConvert3to4(array3, array4);
139 for (j = 0; j < 4; j++)
140 chara[index++] = tablechar64[array4[j]];
141 i = 0;
142 linecount += 4;
143 }
144 }
145
146 /* Suppose 1 or 2 bytes has been read but not yet processed.
147 * If 1 byte has been read, this will generate 2 bytes of
148 * output, with 6 bits to the first byte and 2 bits to the second.
149 * We will add two bytes of '=' for padding.
150 * If 2 bytes has been read, this will generate 3 bytes of output,
151 * with 6 bits to the first 2 bytes and 4 bits to the third, and
152 * we add a fourth padding byte ('='). */
153 if (i > 0) { /* left-over 1 or 2 input bytes */
154 for (j = i; j < 3; j++)
155 array3[j] = '\0'; /* zero the remaining input bytes */
156 byteConvert3to4(array3, array4);

Callers 1

l_genDataStringFunction · 0.85

Calls 1

byteConvert3to4Function · 0.85

Tested by

no test coverage detected