Lines 54-61
static const char base64_alphabet[] =
Link Here
|
54 |
* |
54 |
* |
55 |
* The output buffer must be large enough to fit all the data that will |
55 |
* The output buffer must be large enough to fit all the data that will |
56 |
* be written to it. Due to the way base64 encodes you will need |
56 |
* be written to it. Due to the way base64 encodes you will need |
57 |
* at least: @len * 4 / 3 + 6 bytes. If you enable line-breaking you will |
57 |
* at least: (@len / 3 + 1) * 4 + 4 bytes (+ 4 may be needed in case of |
58 |
* need at least: @len * 4 / 3 + @len * 4 / (3 * 72) + 7 bytes. |
58 |
* non-zero state). If you enable line-breaking you will need at least: |
|
|
59 |
* ((@len / 3 + 1) * 4 + 4) / 72 + 1 bytes of extra space. |
59 |
* |
60 |
* |
60 |
* @break_lines is typically used when putting base64-encoded data in emails. |
61 |
* @break_lines is typically used when putting base64-encoded data in emails. |
61 |
* It breaks the lines at 72 columns instead of putting all of the text on |
62 |
* It breaks the lines at 72 columns instead of putting all of the text on |
Lines 233-240
g_base64_encode (const guchar *data,
Link Here
|
233 |
g_return_val_if_fail (data != NULL, NULL); |
234 |
g_return_val_if_fail (data != NULL, NULL); |
234 |
g_return_val_if_fail (len > 0, NULL); |
235 |
g_return_val_if_fail (len > 0, NULL); |
235 |
|
236 |
|
236 |
/* We can use a smaller limit here, since we know the saved state is 0 */ |
237 |
/* We can use a smaller limit here, since we know the saved state is 0, |
237 |
out = g_malloc (len * 4 / 3 + 4); |
238 |
+1 is needed for trailing \0, also check for unlikely integer overflow */ |
|
|
239 |
if (len >= ((G_MAXSIZE - 1) / 4 - 1) * 3) |
240 |
g_error("%s: input too large for Base64 encoding (%"G_GSIZE_FORMAT" chars)", |
241 |
G_STRLOC, len); |
242 |
|
243 |
out = g_malloc ((len / 3 + 1) * 4 + 1); |
244 |
|
238 |
outlen = g_base64_encode_step (data, len, FALSE, out, &state, &save); |
245 |
outlen = g_base64_encode_step (data, len, FALSE, out, &state, &save); |
239 |
outlen += g_base64_encode_close (FALSE, out + outlen, &state, &save); |
246 |
outlen += g_base64_encode_close (FALSE, out + outlen, &state, &save); |
240 |
out[outlen] = '\0'; |
247 |
out[outlen] = '\0'; |
Lines 275-281
static const unsigned char mime_base64_r
Link Here
|
275 |
* |
282 |
* |
276 |
* The output buffer must be large enough to fit all the data that will |
283 |
* The output buffer must be large enough to fit all the data that will |
277 |
* be written to it. Since base64 encodes 3 bytes in 4 chars you need |
284 |
* be written to it. Since base64 encodes 3 bytes in 4 chars you need |
278 |
* at least: @len * 3 / 4 bytes. |
285 |
* at least: (@len / 4) * 3 + 3 bytes (+ 3 may be needed in case of non-zero |
|
|
286 |
* state). |
279 |
* |
287 |
* |
280 |
* Return value: The number of bytes of output that was written |
288 |
* Return value: The number of bytes of output that was written |
281 |
* |
289 |
* |
Lines 358-364
g_base64_decode (const gchar *text,
Link Here
|
358 |
gsize *out_len) |
366 |
gsize *out_len) |
359 |
{ |
367 |
{ |
360 |
guchar *ret; |
368 |
guchar *ret; |
361 |
gint input_length, state = 0; |
369 |
gsize input_length; |
|
|
370 |
gint state = 0; |
362 |
guint save = 0; |
371 |
guint save = 0; |
363 |
|
372 |
|
364 |
g_return_val_if_fail (text != NULL, NULL); |
373 |
g_return_val_if_fail (text != NULL, NULL); |
Lines 368-374
g_base64_decode (const gchar *text,
Link Here
|
368 |
|
377 |
|
369 |
g_return_val_if_fail (input_length > 1, NULL); |
378 |
g_return_val_if_fail (input_length > 1, NULL); |
370 |
|
379 |
|
371 |
ret = g_malloc0 (input_length * 3 / 4); |
380 |
/* We can use a smaller limit here, since we know the saved state is 0, |
|
|
381 |
+1 used to avoid calling g_malloc0(0), and hence retruning NULL */ |
382 |
ret = g_malloc0 ((input_length / 4) * 3 + 1); |
372 |
|
383 |
|
373 |
*out_len = g_base64_decode_step (text, input_length, ret, &state, &save); |
384 |
*out_len = g_base64_decode_step (text, input_length, ret, &state, &save); |
374 |
|
385 |
|