diff options
author | phoebos <ben@bvnf.space> | 2021-07-18 18:52:50 +0100 |
---|---|---|
committer | phoebos <ben@bvnf.space> | 2021-07-18 18:52:50 +0100 |
commit | 5f9b81f1de231521d1922abb7026fc230c52b76f (patch) | |
tree | 1882b181ac66f847ba18f9425b12a7291c080f6a | |
parent | e6a1f14c205782f9ee8580575185cf6e74428bdc (diff) | |
download | kandr-5f9b81f1de231521d1922abb7026fc230c52b76f.tar.gz |
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.
-rw-r--r-- | base64.c | 7 |
1 files changed, 3 insertions, 4 deletions
diff --git a/base64.c b/base64.c index 6bf3b4b..0771e0d 100644 --- 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 */ |