summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2017-01-11 03:49:31 +0100
committerGravatar Nikias Bassen2017-01-11 03:49:31 +0100
commit3a55ddd3c4c11ce75a86afbefd085d8d397ff957 (patch)
tree997586b5e152a8b46c01fd494b0f51f73409ed07 /src
parentbbd33793d62ef9c9dbd6f69aa2fd0ced1b163e2f (diff)
downloadlibplist-3a55ddd3c4c11ce75a86afbefd085d8d397ff957.tar.gz
libplist-3a55ddd3c4c11ce75a86afbefd085d8d397ff957.tar.bz2
base64: Rework base64decode to handle split encoded data correctly
Diffstat (limited to 'src')
-rw-r--r--src/base64.c68
1 files changed, 27 insertions, 41 deletions
diff --git a/src/base64.c b/src/base64.c
index 7870a79..ba8acca 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -28,7 +28,7 @@ static const signed char base64_table[256] = {
28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 30 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
31 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, 31 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1,
32 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 32 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
33 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, 33 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
34 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 34 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
@@ -71,38 +71,6 @@ size_t base64encode(char *outbuf, const unsigned char *buf, size_t size)
71 return m; 71 return m;
72} 72}
73 73
74static int base64decode_block(unsigned char *target, const char *data, size_t data_size)
75{
76 int w1,w2,w3,w4;
77 int i;
78 size_t n;
79
80 if (!data || (data_size <= 0)) {
81 return 0;
82 }
83
84 n = 0;
85 i = 0;
86 while (n < data_size-3) {
87 w1 = base64_table[(int)data[n]];
88 w2 = base64_table[(int)data[n+1]];
89 w3 = base64_table[(int)data[n+2]];
90 w4 = base64_table[(int)data[n+3]];
91
92 if (w2 >= 0) {
93 target[i++] = (char)((w1*4 + (w2 >> 4)) & 255);
94 }
95 if (w3 >= 0) {
96 target[i++] = (char)((w2*16 + (w3 >> 2)) & 255);
97 }
98 if (w4 >= 0) {
99 target[i++] = (char)((w3*64 + w4) & 255);
100 }
101 n+=4;
102 }
103 return i;
104}
105
106unsigned char *base64decode(const char *buf, size_t *size) 74unsigned char *base64decode(const char *buf, size_t *size)
107{ 75{
108 if (!buf || !size) return NULL; 76 if (!buf || !size) return NULL;
@@ -111,19 +79,37 @@ unsigned char *base64decode(const char *buf, size_t *size)
111 unsigned char *outbuf = (unsigned char*)malloc((len/4)*3+3); 79 unsigned char *outbuf = (unsigned char*)malloc((len/4)*3+3);
112 const char *ptr = buf; 80 const char *ptr = buf;
113 int p = 0; 81 int p = 0;
114 size_t l = 0; 82 int wv, w1, w2, w3, w4;
83 int tmpval[4];
84 int tmpcnt = 0;
115 85
116 do { 86 do {
117 ptr += strspn(ptr, "\r\n\t "); 87 while (ptr < buf+len && (*ptr == ' ' || *ptr == '\t' || *ptr == '\n' || *ptr == '\r')) {
88 ptr++;
89 }
118 if (*ptr == '\0' || ptr >= buf+len) { 90 if (*ptr == '\0' || ptr >= buf+len) {
119 break; 91 break;
120 } 92 }
121 l = strcspn(ptr, "\r\n\t "); 93 if ((wv = base64_table[(int)(unsigned char)*ptr++]) == -1) {
122 if (l > 3 && ptr+l <= buf+len) { 94 continue;
123 p+=base64decode_block(outbuf+p, ptr, l); 95 }
124 ptr += l; 96 tmpval[tmpcnt++] = wv;
125 } else { 97 if (tmpcnt == 4) {
126 break; 98 tmpcnt = 0;
99 w1 = tmpval[0];
100 w2 = tmpval[1];
101 w3 = tmpval[2];
102 w4 = tmpval[3];
103
104 if (w2 >= 0) {
105 outbuf[p++] = (unsigned char)(((w1 << 2) + (w2 >> 4)) & 0xFF);
106 }
107 if (w3 >= 0) {
108 outbuf[p++] = (unsigned char)(((w2 << 4) + (w3 >> 2)) & 0xFF);
109 }
110 if (w4 >= 0) {
111 outbuf[p++] = (unsigned char)(((w3 << 6) + w4) & 0xFF);
112 }
127 } 113 }
128 } while (1); 114 } while (1);
129 115