FreeCalypso > hg > sms-coding-utils
annotate libcoding/hexdecode.c @ 31:19476164c54d
doc/SMS-PDU-decoding: document imported utilities
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 14 Jun 2024 18:48:58 +0000 |
parents | 7418ca2e9949 |
children |
rev | line source |
---|---|
27
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This library module implements decoding of long hex strings, |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * such as SMS PDUs. |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <ctype.h> |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 decode_hex_line(inbuf, outbuf, outmax) |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 char *inbuf; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 u_char *outbuf; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 unsigned outmax; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 { |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 char *inp = inbuf; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 u_char *outp = outbuf; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 unsigned outcnt = 0; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 int c, d[2], i; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 while (*inp) { |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 if (!isxdigit(inp[0]) || !isxdigit(inp[1])) |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 return(-1); |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 if (outcnt >= outmax) |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 break; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 for (i = 0; i < 2; i++) { |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 c = *inp++; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 if (isdigit(c)) |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 d[i] = c - '0'; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 else if (isupper(c)) |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 d[i] = c - 'A' + 10; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 else |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 d[i] = c - 'a' + 10; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 } |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 *outp++ = (d[0] << 4) | d[1]; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 outcnt++; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 } |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 return outcnt; |
7418ca2e9949
libcoding: add functions from freecalypso-tools/uptools/libcoding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 } |