int rf_htoi(char *p) { int val = 0; for (; ISHEXCHAR(*p); p++) val = 16 * val + HC2INT(*p); return (val); }
// Decode Q encoding (RFC 2047). // static char *DecodeQ(const char *in, PRUint32 length) { char *out, *dest = 0; out = dest = (char *)PR_Calloc(length + 1, sizeof(char)); if (dest == nsnull) return nsnull; while (length > 0) { PRUintn c = 0; switch (*in) { case '=': // check if |in| in the form of '=hh' where h is [0-9a-fA-F]. if (length < 3 || !ISHEXCHAR(in[1]) || !ISHEXCHAR(in[2])) goto badsyntax; PR_sscanf(in + 1, "%2X", &c); *out++ = (char) c; in += 3; length -= 3; break; case '_': *out++ = ' '; in++; length--; break; default: if (*in & 0x80) goto badsyntax; *out++ = *in++; length--; } } *out++ = '\0'; for (out = dest; *out ; ++out) { if (*out == '\t') *out = ' '; } return dest; badsyntax: PR_Free(dest); return nsnull; }
int hex2bin2(const char *hex, unsigned char *buf, size_t *len) { size_t count = 0; char debugString[2048]; while (*hex != '\0' && count < *len) { // skip all that is not a hex char while (!ISHEXCHAR(*hex) && *hex != '\0') hex++; if (*hex == '\0') break; // The nex char should be a hex char too if (ISHEXCHAR(hex[1])) buf[count++] = (unsigned char)(16 * hexchar2bin(*hex) + hexchar2bin(hex[1])); else { printf("ERR: invalid input in hex string: expected a hex char after '%c'\n", *hex); sprintf_s(debugString, 2047, "ERR: invalid input in hex string: expected a hex char after '%c'\n", *hex); DebugMessage(debugString); return -1; } hex += 2; } if (count >= *len) { printf("ERR: hex string too large (should be max %d bytes\n", (int) *len); sprintf_s(debugString, 2047, "ERR: hex string too large (should be max %d bytes\n", *len); DebugMessage(debugString); return -1; } *len = count; return 0; }