From: Wilfried Göesgens Date: Mon, 16 Mar 2009 21:25:19 +0000 (+0000) Subject: * foolproof the hex-decoder X-Git-Tag: v7.86~1349 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=9c8cce9a9b7e3152c58718d2e8bb82f08d83c686 * foolproof the hex-decoder * some inline handling to fixup mips --- diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index c6454f210..223ddf134 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -249,7 +249,7 @@ int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t n unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator); long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator); int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator); -inline int StrBufNum_tokens(const StrBuf *source, char tok); +int StrBufNum_tokens(const StrBuf *source, char tok); int StrBufRemove_token(StrBuf *Source, int parmnum, char separator); int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pStart, char separator); @@ -327,7 +327,7 @@ unsigned long extract_unsigned_long(const char *source, int parmnum); void CtdlInitBase64Table(void); size_t CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks); int CtdlDecodeBase64(char *dest, const char *source, size_t length); -inline unsigned int decode_hex(char *Source); +unsigned int decode_hex(char *Source); int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen); void striplt(char *); int haschar(const char *st, int ch); diff --git a/libcitadel/lib/mime_parser.c b/libcitadel/lib/mime_parser.c index 66f8c0c78..85f955c0c 100644 --- a/libcitadel/lib/mime_parser.c +++ b/libcitadel/lib/mime_parser.c @@ -71,35 +71,41 @@ char *fixed_partnum(char *supplied_partnum) { } -unsigned int decode_hex(char *Source) +static inline unsigned int _decode_hex(char *Source) { int ret = 0; - if (*Source < 'A') { + if ((*Source >= '0') && (*Source <= '9')) { ret += (*Source - '0'); } - else if (*Source > 'Z') { + else if ((*Source >= 'A') && (*Source <= 'F')) { + ret += (*Source - 'A' + 10); + } + else if ((*Source >= 'a') && (*Source <= 'f')) { ret += (*Source - 'a' + 10); } else { - ret += (*Source - 'A' + 10); + return '?'; } - + ret = ret << 4; - - if (*(Source + 1) < 'A') { - ret += (*(Source + 1) - '0'); + + if ((*(Source + 1) >= '0') && (*(Source + 1) <= '9')) { + ret |= (*(Source + 1) - '0'); } - else if (*(Source + 1) > 'Z') { - ret += (*(Source + 1) - 'a' + 10); + else if ((*(Source + 1) >= 'A') && (*(Source + 1) <= 'F')) { + ret |= (*(Source + 1) - 'A' + 10); } - else { - ret += (*(Source + 1) - 'A' + 10); + else if ((*(Source + 1) >= 'a') && (*(Source + 1) <= 'f')) { + ret |= (*(Source + 1) - 'a' + 10); } - if (ret > 255) + else { return '?'; + } return ret; } +unsigned int decode_hex(char *Source) {return _decode_hex(Source);} + /* * Convert "quoted-printable" to binary. Returns number of bytes decoded. * according to RFC2045 section 6.7 @@ -122,7 +128,7 @@ int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) { else if (encoded[pos] == '=') { ch = 0; - ch = decode_hex(&encoded[pos+1]); + ch = _decode_hex(&encoded[pos+1]); pos += 3; decoded[decoded_length++] = ch; } diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index b7dc903c8..f64ae13b0 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -806,7 +806,7 @@ void StrBufPrintf(StrBuf *Buf, const char *format, ...) * \param tok Tokenizer char to count * \returns numbers of tokenizer chars found */ -inline int StrBufNum_tokens(const StrBuf *source, char tok) +int StrBufNum_tokens(const StrBuf *source, char tok) { if (source == NULL) return 0;