annotate smsc-sendmt/hexdecode.c @ 12:7543aa173634

proto-smsc-sendmt program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 27 Aug 2023 16:36:28 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This library module implements decoding of long hex strings,
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * such as SMS PDUs.
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <ctype.h>
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdint.h>
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 int decode_hex_line(const char *inbuf, uint8_t *outbuf, unsigned outmax)
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 {
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 const char *inp = inbuf;
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 uint8_t *outp = outbuf;
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 unsigned outcnt = 0;
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 int c, d[2], i;
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 while (*inp) {
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 if (!isxdigit(inp[0]) || !isxdigit(inp[1]))
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 return(-1);
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 if (outcnt >= outmax)
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 break;
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 for (i = 0; i < 2; i++) {
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 c = *inp++;
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 if (isdigit(c))
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 d[i] = c - '0';
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 else if (isupper(c))
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 d[i] = c - 'A' + 10;
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 else
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 d[i] = c - 'a' + 10;
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 }
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 *outp++ = (d[0] << 4) | d[1];
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 outcnt++;
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 }
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 return outcnt;
7543aa173634 proto-smsc-sendmt program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }