Mercurial > masqmail
comparison src/base64/base64.c @ 312:c74adb7c4f50
null-terminated the decoded base64 strings
The returned size is still the same. I only alloced one byte more and
filled it with zero.
TODO: I'm not sure if the allocated size is exact. It's large enough
but maybe too large.
author | meillo@marmaro.de |
---|---|
date | Sun, 24 Apr 2011 20:13:47 +0200 |
parents | 589c365d90b1 |
children | 41958685480d |
comparison
equal
deleted
inserted
replaced
311:e230bcd0f1c6 | 312:c74adb7c4f50 |
---|---|
38 for (; i < 62; i++) | 38 for (; i < 62; i++) |
39 enc[i] = (gchar) ('0' + j++); | 39 enc[i] = (gchar) ('0' + j++); |
40 enc[i++] = '+'; | 40 enc[i++] = '+'; |
41 enc[i++] = '/'; | 41 enc[i++] = '/'; |
42 | 42 |
43 outbuf = g_malloc(((len + 3) * 8) / 6); | 43 outbuf = g_malloc(((len + 3) * 8) / 6 +1); |
44 memset(outbuf, 0, ((len + 3) * 8) / 6 +1); | |
44 q = outbuf; | 45 q = outbuf; |
45 | 46 |
46 i = 0; | 47 i = 0; |
47 while (i < len - 2) { | 48 while (i < len - 2) { |
48 in0 = buf[i++]; | 49 in0 = buf[i++]; |
76 gchar *base64_decode(gchar * buf, gint * size) | 77 gchar *base64_decode(gchar * buf, gint * size) |
77 { | 78 { |
78 guchar *p = buf, *q; | 79 guchar *p = buf, *q; |
79 guint in[4]; | 80 guint in[4]; |
80 /* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */ | 81 /* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */ |
81 gchar *out = g_malloc((strlen(buf) + 3) + 1); | 82 gchar *out = g_malloc((strlen(buf) + 3) + 1 +1); |
83 memset(out, 0, (strlen(buf) + 3) + 1 +1); | |
82 | 84 |
83 q = out; | 85 q = out; |
84 *size = 0; | 86 *size = 0; |
85 | 87 |
86 *q = 0; | 88 *q = 0; |
123 } else { | 125 } else { |
124 *size += 3; | 126 *size += 3; |
125 } | 127 } |
126 } | 128 } |
127 } | 129 } |
130 out[*size] = '\0'; | |
128 return out; | 131 return out; |
129 } | 132 } |