comparison common/strhelp.c @ 160:bf4bfd8843bd

Add memory allocating base64 decode function
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 25 Mar 2014 10:07:12 +0000
parents b026e6d2a161
children 4def8b263dd3
comparison
equal deleted inserted replaced
159:37c9653b8755 160:bf4bfd8843bd
2 #include <stdbool.h> 2 #include <stdbool.h>
3 #include <stdio.h> 3 #include <stdio.h>
4 #include <stdlib.h> 4 #include <stdlib.h>
5 #include <string.h> 5 #include <string.h>
6 #include <assert.h> 6 #include <assert.h>
7
8 #include <polarssl/base64.h>
7 9
8 /* Remarks regarding the "Flawfinder: ignore" comments in this file: 10 /* Remarks regarding the "Flawfinder: ignore" comments in this file:
9 * 11 *
10 * - strlen: 12 * - strlen:
11 * 13 *
148 i = strlen(*s); /* Flawfinder: ignore */ 150 i = strlen(*s); /* Flawfinder: ignore */
149 while (isspace((*s)[--i])) 151 while (isspace((*s)[--i]))
150 (*s)[i] = '\0'; 152 (*s)[i] = '\0';
151 } 153 }
152 } 154 }
155
156 int str_base64_decode(char **dst, size_t *dst_size, char *src,
157 size_t src_size)
158 {
159 int ret = -1;
160
161 if (!dst || *dst) {
162 return -1;
163 }
164
165 /* Check the needed size for the buffer */
166 ret = base64_decode(NULL, dst_size,
167 (unsigned char *)src, src_size);
168
169 if (ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) {
170 return ret;
171 }
172
173 *dst = xmalloc(*dst_size);
174 memset (*dst, 0, *dst_size);
175
176 ret = base64_decode((unsigned char *)*dst, dst_size,
177 (unsigned char *)src, src_size);
178 if (!ret) {
179 free (*dst);
180 *dst = NULL;
181 dst_size = 0;
182 }
183 return ret;
184 }

http://wald.intevation.org/projects/trustbridge/