From 99d11d347ced2a1757f6362a46755c3126772295 Mon Sep 17 00:00:00 2001 From: Tatsuya Kinoshita Date: Mon, 5 Apr 2021 22:37:33 +0900 Subject: Use Strcatc and Strnulterm in base64_encode --- etc.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'etc.c') diff --git a/etc.c b/etc.c index 70735db..7cdd220 100644 --- a/etc.c +++ b/etc.c @@ -2026,6 +2026,10 @@ base64_encode(const unsigned char *src, size_t len) return Strnew(); dest = Strnew_size(k); + if (dest->area_size <= k) { + Strfree(dest); + return Strnew(); + } in = src; @@ -2036,10 +2040,10 @@ base64_encode(const unsigned char *src, size_t len) j = j << 8 | *in++; j = j << 8 | *in++; - Strcat_char(dest, Base64Table[(j >> 18) & 0x3f]); - Strcat_char(dest, Base64Table[(j >> 12) & 0x3f]); - Strcat_char(dest, Base64Table[(j >> 6) & 0x3f]); - Strcat_char(dest, Base64Table[j & 0x3f]); + Strcatc(dest, Base64Table[(j >> 18) & 0x3f]); + Strcatc(dest, Base64Table[(j >> 12) & 0x3f]); + Strcatc(dest, Base64Table[(j >> 6) & 0x3f]); + Strcatc(dest, Base64Table[j & 0x3f]); } if (src + len - in) { @@ -2047,17 +2051,18 @@ base64_encode(const unsigned char *src, size_t len) if (src + len - in) { j = j << 8 | *in++; j = j << 8; - Strcat_char(dest, Base64Table[(j >> 18) & 0x3f]); - Strcat_char(dest, Base64Table[(j >> 12) & 0x3f]); - Strcat_char(dest, Base64Table[(j >> 6) & 0x3f]); + Strcatc(dest, Base64Table[(j >> 18) & 0x3f]); + Strcatc(dest, Base64Table[(j >> 12) & 0x3f]); + Strcatc(dest, Base64Table[(j >> 6) & 0x3f]); } else { j = j << 8; j = j << 8; - Strcat_char(dest, Base64Table[(j >> 18) & 0x3f]); - Strcat_char(dest, Base64Table[(j >> 12) & 0x3f]); - Strcat_char(dest, '='); + Strcatc(dest, Base64Table[(j >> 18) & 0x3f]); + Strcatc(dest, Base64Table[(j >> 12) & 0x3f]); + Strcatc(dest, '='); } - Strcat_char(dest, '='); + Strcatc(dest, '='); } + Strnulterm(dest); return dest; } -- cgit v1.2.3