libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 5133b28b095aa37938ff6f7a82b4235918057d40
parent 5c009667f0ac9fdc89ba19dfb09d4cc10cb7936f
Author: Christian Grothoff <christian@grothoff.org>
Date:   Wed, 22 Aug 2012 22:44:35 +0000

fixing #2511

Diffstat:
Msrc/daemon/base64.c | 88++++++++++++++++++++++++++++++++++++++++---------------------------------------
Msrc/daemon/base64.h | 14+++++++++++---
Msrc/daemon/md5.c | 86+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Msrc/daemon/md5.h | 24++++++++++++++++++++++--
Msrc/include/platform.h | 3+++
5 files changed, 126 insertions(+), 89 deletions(-)

diff --git a/src/daemon/base64.c b/src/daemon/base64.c @@ -6,55 +6,57 @@ * @brief This code implements the BASE64 algorithm * @author Matthieu Speder */ - -#include <memory.h> -#include <stdlib.h> #include "base64.h" static const char base64_chars[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -static const char base64_digits[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, - 0, 0, 0, -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, - 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, - 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, - 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char base64_digits[] = + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, + 0, 0, 0, -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, + 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, + 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -char* BASE64Decode(const char* src) { - unsigned int in_len = strlen(src); - char* dest; - char* result; - if (in_len % 4) { - /* Wrong base64 string length */ - return NULL; - } - result = dest = malloc(in_len / 4 * 3 + 1); - if (result == NULL) - return NULL; /* out of memory */ - while (*src) { - char a = base64_digits[(unsigned char)*(src++)]; - char b = base64_digits[(unsigned char)*(src++)]; - char c = base64_digits[(unsigned char)*(src++)]; - char d = base64_digits[(unsigned char)*(src++)]; - *(dest++) = (a << 2) | ((b & 0x30) >> 4); - if (c == (char)-1) - break; - *(dest++) = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2); - if (d == (char)-1) - break; - *(dest++) = ((c & 0x03) << 6) | d; - } - *dest = 0; - return result; +char * +BASE64Decode(const char* src) +{ + size_t in_len = strlen (src); + char* dest; + char* result; + + if (in_len % 4) + { + /* Wrong base64 string length */ + return NULL; + } + result = dest = malloc(in_len / 4 * 3 + 1); + if (result == NULL) + return NULL; /* out of memory */ + while (*src) { + char a = base64_digits[(unsigned char)*(src++)]; + char b = base64_digits[(unsigned char)*(src++)]; + char c = base64_digits[(unsigned char)*(src++)]; + char d = base64_digits[(unsigned char)*(src++)]; + *(dest++) = (a << 2) | ((b & 0x30) >> 4); + if (c == (char)-1) + break; + *(dest++) = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2); + if (d == (char)-1) + break; + *(dest++) = ((c & 0x03) << 6) | d; + } + *dest = 0; + return result; } + /* end of base64.c */ diff --git a/src/daemon/base64.h b/src/daemon/base64.h @@ -1,9 +1,17 @@ +/* + * This code implements the BASE64 algorithm. + * This code is in the public domain; do with it what you wish. + * + * @file base64.c + * @brief This code implements the BASE64 algorithm + * @author Matthieu Speder + */ #ifndef BASE64_H #define BASE64_H -#include "MHD_config.h" +#include "platform.h" -char* BASE64Encode(const char* src); -char* BASE64Decode(const char* src); +char * +BASE64Decode(const char* src); #endif /* !BASE64_H */ diff --git a/src/daemon/md5.c b/src/daemon/md5.c @@ -19,10 +19,9 @@ prototypes) to maintain the tradition that Netfone will compile with Sun's original "cc". */ -#include <memory.h> -#include <stdint.h> #include "md5.h" + #ifndef HIGHFIRST #define byteReverse(buf, len) /* Nothing */ #else @@ -151,7 +150,8 @@ MD5Transform(uint32_t buf[4], * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious * initialization constants. */ -void MD5Init(struct MD5Context *ctx) +void +MD5Init(struct MD5Context *ctx) { ctx->buf[0] = 0x67452301; ctx->buf[1] = 0xefcdab89; @@ -218,46 +218,50 @@ MD5Update(struct MD5Context *ctx, * Final wrapup - pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ -void MD5Final(unsigned char digest[16], - struct MD5Context *ctx) +void +MD5Final(unsigned char digest[16], + struct MD5Context *ctx) { - unsigned count; - unsigned char *p; - - /* Compute number of bytes mod 64 */ - count = (ctx->bits[0] >> 3) & 0x3F; - - /* Set the first char of padding to 0x80. This is safe since there is - always at least one byte free */ - p = ctx->in + count; - *p++ = 0x80; - - /* Bytes of padding needed to make 64 bytes */ - count = 64 - 1 - count; - - /* Pad out to 56 mod 64 */ - if (count < 8) { - /* Two lots of padding: Pad the first block to 64 bytes */ - memset(p, 0, count); - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (uint32_t *) ctx->in); - - /* Now fill the next block with 56 bytes */ - memset(ctx->in, 0, 56); - } else { - /* Pad block to 56 bytes */ - memset(p, 0, count - 8); + unsigned count; + unsigned char *p; + + /* Compute number of bytes mod 64 */ + count = (ctx->bits[0] >> 3) & 0x3F; + + /* Set the first char of padding to 0x80. This is safe since there is + always at least one byte free */ + p = ctx->in + count; + *p++ = 0x80; + + /* Bytes of padding needed to make 64 bytes */ + count = 64 - 1 - count; + + /* Pad out to 56 mod 64 */ + if (count < 8) + { + /* Two lots of padding: Pad the first block to 64 bytes */ + memset(p, 0, count); + byteReverse(ctx->in, 16); + MD5Transform(ctx->buf, (uint32_t *) ctx->in); + + /* Now fill the next block with 56 bytes */ + memset(ctx->in, 0, 56); + } + else + { + /* Pad block to 56 bytes */ + memset(p, 0, count - 8); } - byteReverse(ctx->in, 14); - - /* Append length in bits and transform */ - ((uint32_t *) ctx->in)[14] = ctx->bits[0]; - ((uint32_t *) ctx->in)[15] = ctx->bits[1]; - - MD5Transform(ctx->buf, (uint32_t *) ctx->in); - byteReverse((unsigned char *) ctx->buf, 4); - memcpy(digest, ctx->buf, 16); - memset(ctx, 0, sizeof(struct MD5Context)); /* In case it's sensitive */ + byteReverse(ctx->in, 14); + + /* Append length in bits and transform */ + ((uint32_t *) ctx->in)[14] = ctx->bits[0]; + ((uint32_t *) ctx->in)[15] = ctx->bits[1]; + + MD5Transform(ctx->buf, (uint32_t *) ctx->in); + byteReverse((unsigned char *) ctx->buf, 4); + memcpy(digest, ctx->buf, 16); + memset(ctx, 0, sizeof(struct MD5Context)); /* In case it's sensitive */ } /* end of md5.c */ diff --git a/src/daemon/md5.h b/src/daemon/md5.h @@ -1,8 +1,28 @@ +/* + * This code implements the MD5 message-digest algorithm. + * The algorithm is due to Ron Rivest. This code was + * written by Colin Plumb in 1993, no copyright is claimed. + * This code is in the public domain; do with it what you wish. + * + * Equivalent code is available from RSA Data Security, Inc. + * This code has been tested against that, and is equivalent, + * except that you don't need to include two pages of legalese + * with every copy. + * + * To compute the message digest of a chunk of bytes, declare an + * MD5Context structure, pass it to MD5Init, call MD5Update as + * needed on buffers full of bytes, and then call MD5Final, which + * will fill a supplied 16-byte array with the digest. + */ + +/* Brutally hacked by John Walker back from ANSI C to K&R (no + prototypes) to maintain the tradition that Netfone will compile + with Sun's original "cc". */ + #ifndef MD5_H #define MD5_H -#include "MHD_config.h" - +#include "platform.h" #ifdef WORDS_BIGENDIAN #define HIGHFIRST #endif diff --git a/src/include/platform.h b/src/include/platform.h @@ -74,6 +74,9 @@ #include <sys/mman.h> #define RESTRICT __restrict__ #endif +#if HAVE_MEMORY_H +#include <memory.h> +#endif #if HAVE_SYS_SELECT_H #include <sys/select.h>