FreeCalypso > hg > freecalypso-tools
comparison uptools/libcoding/hexdecode.c @ 329:18c692984549
uptools/libcoding: hex line decoding implemented
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sat, 03 Feb 2018 20:37:14 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 328:978571e23318 | 329:18c692984549 |
|---|---|
| 1 /* | |
| 2 * This library module implements decoding of long hex strings, | |
| 3 * such as SMS PDUs. | |
| 4 */ | |
| 5 | |
| 6 #include <sys/types.h> | |
| 7 #include <ctype.h> | |
| 8 | |
| 9 decode_hex_line(inbuf, outbuf, outmax) | |
| 10 char *inbuf; | |
| 11 u_char *outbuf; | |
| 12 unsigned outmax; | |
| 13 { | |
| 14 char *inp = inbuf; | |
| 15 u_char *outp = outbuf; | |
| 16 unsigned outcnt = 0; | |
| 17 int c, d[2], i; | |
| 18 | |
| 19 while (*inp) { | |
| 20 if (!isxdigit(inp[0]) || !isxdigit(inp[1])) | |
| 21 return(-1); | |
| 22 if (outcnt >= outmax) | |
| 23 break; | |
| 24 for (i = 0; i < 2; i++) { | |
| 25 c = *inp++; | |
| 26 if (isdigit(c)) | |
| 27 d[i] = c - '0'; | |
| 28 else if (isupper(c)) | |
| 29 d[i] = c - 'A' + 10; | |
| 30 else | |
| 31 d[i] = c - 'a' + 10; | |
| 32 } | |
| 33 *outp++ = (d[0] << 4) | d[1]; | |
| 34 outcnt++; | |
| 35 } | |
| 36 return outcnt; | |
| 37 } |
