kandr

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 5f9b81f1de231521d1922abb7026fc230c52b76f
parent e6a1f14c205782f9ee8580575185cf6e74428bdc
Author: phoebos <ben@bvnf.space>
Date:   Sun, 18 Jul 2021 18:52:50 +0100

base64: use unsigned chars

With signed chars, if the highest bit is set, a left-shift causes
the new highest bit to be set also, which makes the result 128 larger
than it should be. This is only a problem for chars > 127, which
excludes ascii. Therefore, this commit *should* make the program work
with unicode and binary data.

Diffstat:
Mbase64.c | 7+++----
1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/base64.c b/base64.c @@ -58,13 +58,12 @@ static void usage(const char *name) { fprintf(stderr, "usage: %s [-d] [FILE]\n", name); } -char *base64(char *i, ssize_t length, unsigned dflg){ - /* TODO: support Unicode */ +char *base64(unsigned char *i, ssize_t length, unsigned dflg){ if (dflg) return NULL; char *out = malloc(length/3 * 4 + 100); int o = 0; /* index of position in out */ while (length > 0) { - char i1, i2; + unsigned char i1, i2; i1 = i2 = 0; length -= 3; if (length >= -2) { @@ -139,7 +138,7 @@ int main(int argc, char **argv) { } } ssize_t bytes; - char buf[BUF_SIZE] = {'\0'}; + unsigned char buf[BUF_SIZE] = {'\0'}; while (1){ if ((bytes = read(fd, buf, BUF_SIZE)) <= 0 ) { if (bytes == 0) break; /* read returns 0 when EOF has been reached */