diff options
author | rsc <devnull@localhost> | 2003-09-30 17:47:41 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2003-09-30 17:47:41 +0000 |
commit | b2cfc4e2e71d0f0a5113ddfbd93c8285cc4d74e4 (patch) | |
tree | f382987fec33cf639d75b1fe1b5d76b8f155d074 /src/lib9/u16.c | |
parent | 5f7d5e8d1899f41b0e5366c0251530ea1dc753d0 (diff) | |
download | plan9port-b2cfc4e2e71d0f0a5113ddfbd93c8285cc4d74e4.tar.gz plan9port-b2cfc4e2e71d0f0a5113ddfbd93c8285cc4d74e4.tar.bz2 plan9port-b2cfc4e2e71d0f0a5113ddfbd93c8285cc4d74e4.zip |
Initial revision
Diffstat (limited to 'src/lib9/u16.c')
-rw-r--r-- | src/lib9/u16.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/lib9/u16.c b/src/lib9/u16.c new file mode 100644 index 00000000..d9f41e46 --- /dev/null +++ b/src/lib9/u16.c @@ -0,0 +1,52 @@ +#include <lib9.h> +static char t16e[] = "0123456789ABCDEF"; + +int +dec16(uchar *out, int lim, char *in, int n) +{ + int c, w = 0, i = 0; + uchar *start = out; + uchar *eout = out + lim; + + while(n-- > 0){ + c = *in++; + if('0' <= c && c <= '9') + c = c - '0'; + else if('a' <= c && c <= 'z') + c = c - 'a' + 10; + else if('A' <= c && c <= 'Z') + c = c - 'A' + 10; + else + continue; + w = (w<<4) + c; + i++; + if(i == 2){ + if(out + 1 > eout) + goto exhausted; + *out++ = w; + w = 0; + i = 0; + } + } +exhausted: + return out - start; +} + +int +enc16(char *out, int lim, uchar *in, int n) +{ + uint c; + char *eout = out + lim; + char *start = out; + + while(n-- > 0){ + c = *in++; + if(out + 2 >= eout) + goto exhausted; + *out++ = t16e[c>>4]; + *out++ = t16e[c&0xf]; + } +exhausted: + *out = 0; + return out - start; +} |