comparison libcoding/hexdecode.c @ 27:7418ca2e9949

libcoding: add functions from freecalypso-tools/uptools/libcoding that are needed for sms-pdu-decode & pcm-sms-decode
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 13 Jun 2024 02:29:29 +0000
parents
children
comparison
equal deleted inserted replaced
26:c8cb05b69118 27:7418ca2e9949
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 }