xref: /OK3568_Linux_fs/buildroot/package/libb64/0001-Integer-overflows.patch (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1Fix integer overflows. Will not work on compilers with unsigned char
2as the default.
3
4Fetched from: https://sources.debian.org/patches/libb64/1.2-5/
5
6Combined "integer overflows.diff" and "off by one.diff" and adapted
7for version 1.2.1.
8
9Signed-off-by: Mikael Eliasson <mikael@robomagi.com>
10
11diff --git a/src/cdecode.c b/src/cdecode.c
12index a6c0a42..45da4e1 100644
13--- a/src/cdecode.c
14+++ b/src/cdecode.c
15@@ -9,10 +9,11 @@ For details, see http://sourceforge.net/projects/libb64
16
17 int base64_decode_value(char value_in)
18 {
19-	static const char decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51};
20+	static const signed char decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51};
21 	static const char decoding_size = sizeof(decoding);
22+	if (value_in < 43) return -1;
23 	value_in -= 43;
24-	if (value_in < 0 || value_in >= decoding_size) return -1;
25+	if (value_in >= decoding_size) return -1;
26 	return decoding[(int)value_in];
27 }
28
29@@ -26,7 +27,7 @@ int base64_decode_block(const char* code_in, const int length_in, char* plaintex
30 {
31 	const char* codechar = code_in;
32 	char* plainchar = plaintext_out;
33-	char fragment;
34+	int fragment;
35
36 	*plainchar = state_in->plainchar;
37
38@@ -42,7 +43,7 @@ int base64_decode_block(const char* code_in, const int length_in, char* plaintex
39 					state_in->plainchar = *plainchar;
40 					return plainchar - plaintext_out;
41 				}
42-				fragment = (char)base64_decode_value(*codechar++);
43+				fragment = base64_decode_value(*codechar++);
44 			} while (fragment < 0);
45 			*plainchar    = (fragment & 0x03f) << 2;
46 	case step_b:
47@@ -53,7 +54,7 @@ int base64_decode_block(const char* code_in, const int length_in, char* plaintex
48 					state_in->plainchar = *plainchar;
49 					return plainchar - plaintext_out;
50 				}
51-				fragment = (char)base64_decode_value(*codechar++);
52+				fragment = base64_decode_value(*codechar++);
53 			} while (fragment < 0);
54 			*plainchar++ |= (fragment & 0x030) >> 4;
55 			*plainchar    = (fragment & 0x00f) << 4;
56@@ -65,7 +66,7 @@ int base64_decode_block(const char* code_in, const int length_in, char* plaintex
57 					state_in->plainchar = *plainchar;
58 					return plainchar - plaintext_out;
59 				}
60-				fragment = (char)base64_decode_value(*codechar++);
61+				fragment = base64_decode_value(*codechar++);
62 			} while (fragment < 0);
63 			*plainchar++ |= (fragment & 0x03c) >> 2;
64 			*plainchar    = (fragment & 0x003) << 6;
65@@ -77,7 +78,7 @@ int base64_decode_block(const char* code_in, const int length_in, char* plaintex
66 					state_in->plainchar = *plainchar;
67 					return plainchar - plaintext_out;
68 				}
69-				fragment = (char)base64_decode_value(*codechar++);
70+				fragment = base64_decode_value(*codechar++);
71 			} while (fragment < 0);
72 			*plainchar++   |= (fragment & 0x03f);
73 		}
74