Browse code

fix trailing bytes in base64 decoding (bb #1388, thanks Thiyaga <mthiyaga*corp.untd.com>)

git-svn: trunk@4974

Török Edvin authored on 2009/03/23 01:55:41
Showing 4 changed files
... ...
@@ -1,3 +1,9 @@
1
+Sun Mar 22 18:15:01 EET 2009 (edwin)
2
+------------------------------------
3
+ * libclamav/libclamav.map, libclamav/message.c,
4
+ unit_tests/check_str.c: fix trailing bytes in base64 decoding (bb #1388,
5
+			 thanks Thiyaga <mthiyaga*corp.untd.com>).
6
+
1 7
 Sun Mar 22 17:35:31 CET 2009 (acab)
2 8
 -----------------------------------
3 9
  * contrib/init: removed
... ...
@@ -138,6 +138,10 @@ CLAMAV_PRIVATE {
138 138
     sha256_final;
139 139
     cli_url_canon;
140 140
     cli_strerror;
141
+    decodeLine;
142
+    messageCreate;
143
+    messageDestroy;
144
+    base64Flush;
141 145
   local:
142 146
     *;
143 147
 };
... ...
@@ -2326,7 +2326,7 @@ decode(message *m, const char *in, unsigned char *out, unsigned char (*decoder)(
2326 2326
 			case 4:
2327 2327
 				*out++ = (b1 << 2) | ((b2 >> 4) & 0x3);
2328 2328
 				*out++ = (b2 << 4) | ((b3 >> 2) & 0xF);
2329
-				if((nbytes == 4) || b3)
2329
+				if((nbytes == 4) || (b3&0x3))
2330 2330
 					*out++ = (b3 << 6) | (b4 & 0x3F);
2331 2331
 				break;
2332 2332
 			case 2:
... ...
@@ -32,6 +32,8 @@
32 32
 #include "../libclamav/clamav.h"
33 33
 #include "../libclamav/others.h"
34 34
 #include "../libclamav/str.h"
35
+#include "../libclamav/mbox.h"
36
+#include "../libclamav/message.h"
35 37
 #include "../libclamav/jsparse/textbuf.h"
36 38
 #include "checks.h"
37 39
 
... ...
@@ -162,10 +164,56 @@ START_TEST (hex2str)
162 162
 }
163 163
 END_TEST
164 164
 
165
+#ifdef CHECK_HAVE_LOOPS
166
+static struct base64lines {
167
+    const char *line;
168
+    unsigned char *decoded;
169
+    unsigned int   len;
170
+} base64tests[] = {
171
+    {"", "", 0},
172
+    {"Zg==", "f", 1},
173
+    {"Zm8=", "fo", 2},
174
+    {"Zm9v", "foo", 3},
175
+    {"Zm9vYg==", "foob", 4},
176
+    {"Zm9vYmFy", "foobar", 6},
177
+    /* with missing padding */
178
+    {"Zg","f", 1},
179
+    {"Zm8", "fo", 2},
180
+    {"Zm9vYg", "foob", 4}
181
+};
182
+
183
+START_TEST (test_base64)
184
+{
185
+    unsigned char *ret, *ret2;
186
+    unsigned len;
187
+    unsigned char buf[1024];
188
+    const struct base64lines *test = &base64tests[_i];
189
+    message *m = messageCreate();
190
+    fail_unless(!!m, "Unable to create message");
191
+
192
+    ret = decodeLine(m, BASE64, test->line, buf, sizeof(buf));
193
+    fail_unless(!!ret, "unable to decode line");
194
+
195
+    ret2 = base64Flush(m, ret);
196
+
197
+    if (!ret2)
198
+	ret2 = ret;
199
+    *ret2 = '\0';
200
+    len = ret2 - buf;
201
+    fail_unless_fmt(len == test->len, "invalid base64 decoded length: %u expected %u (%s)\n",
202
+		    len, test->len, buf);
203
+    fail_unless_fmt(!memcmp(buf, test->decoded, test->len),
204
+		    "invalid base64 decoded data: %s, expected:%s\n",
205
+		    buf, test->decoded);
206
+    messageDestroy(m);
207
+}
208
+END_TEST
209
+#endif
210
+
165 211
 Suite *test_str_suite(void)
166 212
 {
167 213
     Suite *s = suite_create("str");
168
-    TCase *tc_cli_unescape, *tc_tbuf, *tc_str;
214
+    TCase *tc_cli_unescape, *tc_tbuf, *tc_str, *tc_decodeline;
169 215
 
170 216
     tc_cli_unescape = tcase_create("cli_unescape");
171 217
     suite_add_tcase (s, tc_cli_unescape);
... ...
@@ -185,6 +233,11 @@ Suite *test_str_suite(void)
185 185
     suite_add_tcase (s, tc_str);
186 186
     tcase_add_test(tc_str, hex2str);
187 187
 
188
+    tc_decodeline = tcase_create("decodeline");
189
+    suite_add_tcase (s, tc_decodeline);
190
+#ifdef CHECK_HAVE_LOOPS
191
+    tcase_add_loop_test(tc_decodeline, test_base64, 0, sizeof(base64tests)/sizeof(base64tests[0]));
192
+#endif
188 193
     return s;
189 194
 }
190 195