diff options
author | snowdrop | 2006-02-25 10:09:28 +0000 |
---|---|---|
committer | snowdrop | 2006-02-25 10:09:28 +0000 |
commit | 76c6367d7d0d105aed35c714b45b6883ec8577f7 (patch) | |
tree | a864293d6c814f688783f8d706ef4279c1dc2a01 /nanohttp | |
parent | 3b814aa35d921d779662bb9d0b6ec1bf428c0fa3 (diff) | |
download | csoap-76c6367d7d0d105aed35c714b45b6883ec8577f7.tar.gz csoap-76c6367d7d0d105aed35c714b45b6883ec8577f7.tar.bz2 |
patches by Heiko. See mailinglist (archive 25.02.06)
Diffstat (limited to 'nanohttp')
-rw-r--r-- | nanohttp/nanohttp-base64.c | 156 |
1 files changed, 84 insertions, 72 deletions
diff --git a/nanohttp/nanohttp-base64.c b/nanohttp/nanohttp-base64.c index f81039c..8abee35 100644 --- a/nanohttp/nanohttp-base64.c +++ b/nanohttp/nanohttp-base64.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-base64.c,v 1.1 2006/02/19 22:22:41 snowdrop Exp $ +* $Id: nanohttp-base64.c,v 1.2 2006/02/25 10:09:29 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -25,6 +25,8 @@ #include <config.h> #endif +#include "nanohttp-base64.h" + static const char cb64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char cd64[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq"; @@ -34,12 +36,13 @@ static const char cd64[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$ */ static void encodeblock(unsigned char in[3], unsigned char out[4], int len) { - out[0] = cb64[in[0] >> 2]; - out[1] = cb64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)]; - out[2] = (unsigned char)(len > 1 ? cb64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)] : '='); - out[3] = (unsigned char)(len > 2 ? cb64[in[2] & 0x3f] : '='); - return; + out[0] = cb64[in[0] >> 2]; + out[1] = cb64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)]; + out[2] = (unsigned char)(len > 1 ? cb64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)] : '='); + out[3] = (unsigned char)(len > 2 ? cb64[in[2] & 0x3f] : '='); + + return; } /** @@ -47,28 +50,27 @@ static void encodeblock(unsigned char in[3], unsigned char out[4], int len) */ void base64_encode(const unsigned char *instr, unsigned char *outstr) { - unsigned char in[3], out[4]; - int i, len; - - while (*instr) { - - len = 0; - for (i = 0; i < 3; i++) { - in[i] = (unsigned char)*instr++; - if (*instr) { - len++; - } - else { - in[i] = 0; - } - } - if (len) { - encodeblock(in, out, len); - for (i = 0; i < 4; i++) { - *outstr++ = out[i]; - } - } - } + unsigned char in[3], out[4]; + int i, len; + + while (*instr) + { + len = 0; + for (i = 0; i < 3; i++) + { + if ((in[i] = (unsigned char)*instr)) + { + len++; + instr++; + } + } + if (len) + { + encodeblock(in, out, len); + for (i = 0; i < 4; i++) + *outstr++ = out[i]; + } + } } /** @@ -76,11 +78,11 @@ void base64_encode(const unsigned char *instr, unsigned char *outstr) */ static void decodeblock(unsigned char in[4], unsigned char out[3]) { - out[0] = (unsigned char)(in[0] << 2 | in[1] >> 4); - out[1] = (unsigned char)(in[1] << 4 | in[2] >> 2); - out[2] = (unsigned char)(((in[2] << 6) & 0xc0) | in[3]); + out[0] = (unsigned char)(in[0] << 2 | in[1] >> 4); + out[1] = (unsigned char)(in[1] << 4 | in[2] >> 2); + out[2] = (unsigned char)(((in[2] << 6) & 0xc0) | in[3]); - return; + return; } /** @@ -88,56 +90,66 @@ static void decodeblock(unsigned char in[4], unsigned char out[3]) */ void base64_decode(const unsigned char *instr, unsigned char *outstr) { - unsigned char in[4], out[3], v; - int i, len; - - while (*instr) { - for (len = 0, i = 0; i < 4 && *instr; i++) { - v = 0; - while (*instr && v == 0) { - v = *instr++; - v = (unsigned char)((v < 43 || v > 122) ? 0 : cd64[v - 43]); - if (v) { - v = (unsigned char)((v == '$') ? 0 : v - 61); - } - } - if (*instr) { - len++; - if (v) { - in[i] = (unsigned char)(v - 1); - } - } - else { - in[i] = 0; - } - } - if (len) { - decodeblock(in, out); - for (i = 0; i < len - 1; i++) { - *outstr++ = out[i]; - } - } - } + unsigned char in[4], out[3], v; + int i, len; + + while (*instr) + { + for (len = 0, i = 0; i < 4 && *instr; i++) + { + v = 0; + while (*instr && v == 0) + { + v = *instr++; + v = (unsigned char)((v < 43 || v > 122) ? 0 : cd64[v - 43]); + if (v) + v = (unsigned char)((v == '$') ? 0 : v - 61); + } + if (*instr) + { + len++; + if (v) + in[i] = (unsigned char)(v - 1); + } + else + { + in[i] = 0; + } + } + if (len) + { + decodeblock(in, out); + for (i = 0; i < len - 1; i++) + *outstr++ = out[i]; + } + } } #ifdef BASE64_TEST_CASE_FROM_RFC2617 +#include <stdio.h> int main(int argc, char **argv) { + unsigned char *instr = "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; + unsigned char *result = "Aladdin:open sesame"; + unsigned char instr2[80]; + unsigned char outstr[80]; - unsigned char *instr = "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; - unsigned char instr2[80]; - unsigned char outstr[80]; - - bzero(outstr, 80); + bzero(outstr, 80); + base64_decode(instr, outstr); - base64_decode(instr, outstr); + printf("\"%s\" => \"%s\"\n", instr, outstr); + if (strcmp(outstr, result)) + printf("base64_decode failed\n"); - printf("\"%s\" => \"%s\"\n", instr, outstr); + strcpy(instr2, outstr); - strcpy(instr2, outstr); + bzero(outstr, 80); + base64_encode(instr2, outstr); - base64_encode(instr2, outstr); + printf("\"%s\" => \"%s\"\n", instr2, outstr); + if (strcmp(outstr, instr)) + printf("base64_encode failed\n"); - return printf("\"%s\" => \"%s\"\n", instr2, outstr); + return 0; } #endif |