base64: Reformat bin_to_base64()

Fixes T188

Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
(cherry picked from commit b5160ce9e0)
This commit is contained in:
Andreas Schneider
2019-10-31 16:30:17 +01:00
parent c98d024b9a
commit a011f853ed

View File

@@ -270,25 +270,26 @@ static void _bin_to_base64(uint8_t *dest,
* *
* @returns the converted string * @returns the converted string
*/ */
unsigned char *bin_to_base64(const unsigned char *source, int len) { unsigned char *bin_to_base64(const unsigned char *source, int len)
unsigned char *base64; {
unsigned char *ptr; unsigned char *base64;
int flen = len + (3 - (len % 3)); /* round to upper 3 multiple */ unsigned char *ptr;
flen = (4 * flen) / 3 + 1; int flen = len + (3 - (len % 3)); /* round to upper 3 multiple */
flen = (4 * flen) / 3 + 1;
base64 = malloc(flen); base64 = malloc(flen);
if (base64 == NULL) { if (base64 == NULL) {
return NULL; return NULL;
} }
ptr = base64; ptr = base64;
while(len > 0){ while(len > 0){
_bin_to_base64(ptr, source, len > 3 ? 3 : len); _bin_to_base64(ptr, source, len > 3 ? 3 : len);
ptr += 4; ptr += 4;
source += 3; source += 3;
len -= 3; len -= 3;
} }
ptr[0] = '\0'; ptr[0] = '\0';
return base64; return base64;
} }