// implementation
-static inline char __hexToChar(char c) {
+static inline signed char __hexToChar(char c) {
if ((c > 47) && (c < 58)) // '0' .. '9'
return c - 48;
if ((c > 64) && (c < 71)) // 'A' .. 'F'
}
int NGDecodeQuotedPrintable(const char *_src, unsigned _srcLen,
- char *_dest, unsigned _destLen) {
+ char *_dest, unsigned _destLen)
+{
+ /*
+ Eg: "Hello=20World" => "Hello World"
+
+ =XY where XY is a hex encoded byte. In addition '_' is decoded as 0x20
+ (not as space!, this depends on the charset, see RFC 2047 4.2).
+ */
unsigned cnt = 0;
unsigned destCnt = 0;
for (cnt = 0; ((cnt < _srcLen) && (destCnt < _destLen)); cnt++) {
if (_src[cnt] != '=') {
- _dest[destCnt++] = _src[cnt];
+ _dest[destCnt] = _src[cnt] == '_' ? 0x20 : _src[cnt];
+ destCnt++;
}
else {
if ((_srcLen - cnt) > 1) {
signed char c1, c2;
- c1 = _src[++cnt];
-
+ cnt++; // skip '='
+ c1 = _src[cnt]; // first hex digit
+
if (c1 == '\r' || c1 == '\n') {
- if (_src[cnt+1] == '\r' || _src[cnt+1] == '\n' )
+ if (_src[cnt + 1] == '\r' || _src[cnt + 1] == '\n' )
cnt++;
continue;
}
c1 = __hexToChar(c1);
- c2 = __hexToChar(_src[++cnt]);
+
+ cnt++; // skip first hex digit
+ c2 = __hexToChar(_src[cnt]);
if ((c1 == -1) || (c2 == -1)) {
if ((_destLen - destCnt) > 1) {
- _dest[destCnt++] = _src[cnt - 1];
- _dest[destCnt++] = _src[cnt];
+ _dest[destCnt] = _src[cnt - 1]; destCnt++;
+ _dest[destCnt] = _src[cnt]; destCnt++;
}
else
break;
}
else {
- char c = ((c1 << 4) | c2);
- _dest[destCnt++] = c;
+ register unsigned char c = ((c1 << 4) | c2);
+ _dest[destCnt] = c;
+ destCnt++;
}
}
else