diff options
Diffstat (limited to 'hex.c')
-rw-r--r-- | hex.c | 70 |
1 files changed, 70 insertions, 0 deletions
@@ -98,3 +98,73 @@ int hex2bin(char *s) return ((a<<4) + b); } /*@ -charint +shiftimplementation @*/ + +ssize_t hex_escapes(/*@out@*/char *cooked, const char *raw) +/* interpret C-style hex escapes */ +{ + char c, *cookend; + + /*@ +charint -mustdefine -compdef @*/ + for (cookend = cooked; *raw != '\0'; raw++) + if (*raw != '\\') + *cookend++ = *raw; + else { + switch(*++raw) { + case 'b': *cookend++ = '\b'; break; + case 'e': *cookend++ = '\x1b'; break; + case 'f': *cookend++ = '\f'; break; + case 'n': *cookend++ = '\n'; break; + case 'r': *cookend++ = '\r'; break; + case 't': *cookend++ = '\r'; break; + case 'v': *cookend++ = '\v'; break; + case 'x': + switch(*++raw) { + case '0': c = 0x00; break; + case '1': c = 0x10; break; + case '2': c = 0x20; break; + case '3': c = 0x30; break; + case '4': c = 0x40; break; + case '5': c = 0x50; break; + case '6': c = 0x60; break; + case '7': c = 0x70; break; + case '8': c = 0x80; break; + case '9': c = 0x90; break; + case 'A': case 'a': c = 0xa0; break; + case 'B': case 'b': c = 0xb0; break; + case 'C': case 'c': c = 0xc0; break; + case 'D': case 'd': c = 0xd0; break; + case 'E': case 'e': c = 0xe0; break; + case 'F': case 'f': c = 0xf0; break; + default: + return -1; + } + switch(*++raw) { + case '0': c += 0x00; break; + case '1': c += 0x01; break; + case '2': c += 0x02; break; + case '3': c += 0x03; break; + case '4': c += 0x04; break; + case '5': c += 0x05; break; + case '6': c += 0x06; break; + case '7': c += 0x07; break; + case '8': c += 0x08; break; + case '9': c += 0x09; break; + case 'A': case 'a': c += 0x0a; break; + case 'B': case 'b': c += 0x0b; break; + case 'C': case 'c': c += 0x0c; break; + case 'D': case 'd': c += 0x0d; break; + case 'E': case 'e': c += 0x0e; break; + case 'F': case 'f': c += 0x0f; break; + default: + return -2; + } + *cookend++ = c; + break; + case '\\': *cookend++ = '\\'; break; + default: + return -3; + } + } + return (ssize_t)(cookend - cooked); + /*@ +charint +mustdefine +compdef @*/ +} |