From 694a10271dd33aeba7a1cf3d72caf4f1384d611a Mon Sep 17 00:00:00 2001 From: bptato Date: Sun, 4 Apr 2021 17:57:13 +0200 Subject: Return Str from base64_encode, fix extraction of first gif frame for animations in put_image_kitty --- etc.c | 57 ++++++++++++++++++++++++--------------------------------- 1 file changed, 24 insertions(+), 33 deletions(-) (limited to 'etc.c') diff --git a/etc.c b/etc.c index bc72005..70735db 100644 --- a/etc.c +++ b/etc.c @@ -2008,32 +2008,25 @@ void (*mySignal(int signal_number, void (*action) (int))) (int) { static char Base64Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -char * +Str base64_encode(const unsigned char *src, size_t len) { - unsigned char *w, *at; + Str dest; const unsigned char *in, *endw; unsigned long j; size_t k; - - if (!len) - return NULL; - k = len; if (k % 3) - k += 3 - (k % 3); + k += 3 - (k % 3); + k = k / 3 * 4; - if (k + 1 < len) - return NULL; + if (!len || k + 1 < len) + return Strnew(); - w = GC_MALLOC_ATOMIC(k + 1); - if (!w) - return NULL; - w[k] = 0; + dest = Strnew_size(k); - at = w; in = src; endw = src + len - 2; @@ -2043,30 +2036,28 @@ base64_encode(const unsigned char *src, size_t len) j = j << 8 | *in++; j = j << 8 | *in++; - *at++ = Base64Table[(j >> 18) & 0x3f]; - *at++ = Base64Table[(j >> 12) & 0x3f]; - *at++ = Base64Table[(j >> 6) & 0x3f]; - *at++ = Base64Table[j & 0x3f]; + 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]); } - if (in - src - len) { - if (in - src - len == 1) { - j = *in++; - j = j << 8; + if (src + len - in) { + j = *in++; + if (src + len - in) { + j = j << 8 | *in++; j = j << 8; - *at++ = Base64Table[(j >> 18) & 0x3f]; - *at++ = Base64Table[(j >> 12) & 0x3f]; - *at++ = '='; - *at++ = '='; + Strcat_char(dest, Base64Table[(j >> 18) & 0x3f]); + Strcat_char(dest, Base64Table[(j >> 12) & 0x3f]); + Strcat_char(dest, Base64Table[(j >> 6) & 0x3f]); } else { - j = *in++; - j = j << 8 | *in++; j = j << 8; - *at++ = Base64Table[(j >> 18) & 0x3f]; - *at++ = Base64Table[(j >> 12) & 0x3f]; - *at++ = Base64Table[(j >> 6) & 0x3f]; - *at++ = '='; + j = j << 8; + Strcat_char(dest, Base64Table[(j >> 18) & 0x3f]); + Strcat_char(dest, Base64Table[(j >> 12) & 0x3f]); + Strcat_char(dest, '='); } + Strcat_char(dest, '='); } - return (char *)w; + return dest; } -- cgit v1.2.3