annotate uptools/libcoding/hexdecode.c @ 1011:6d9b10633f10

etmsync Pirelli IMEI retrieval: fix poor use of printf() Bug reported by Vadim Yanitskiy <fixeria@osmocom.org>: the construct where a static-allocated string was passed to printf() without any format arguments causes newer compilers to report a security problem. Given that formatted output is not needed here, just fixed string output, change printf() to fputs(), and direct the error message to stderr while at it.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 23 May 2024 17:29:57 +0000
parents 18c692984549
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
329
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This library module implements decoding of long hex strings,
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * such as SMS PDUs.
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <ctype.h>
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 decode_hex_line(inbuf, outbuf, outmax)
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 char *inbuf;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 u_char *outbuf;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 unsigned outmax;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 {
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 char *inp = inbuf;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 u_char *outp = outbuf;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 unsigned outcnt = 0;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 int c, d[2], i;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 while (*inp) {
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 if (!isxdigit(inp[0]) || !isxdigit(inp[1]))
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 return(-1);
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 if (outcnt >= outmax)
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 break;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 for (i = 0; i < 2; i++) {
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 c = *inp++;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (isdigit(c))
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 d[i] = c - '0';
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 else if (isupper(c))
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 d[i] = c - 'A' + 10;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 else
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 d[i] = c - 'a' + 10;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 }
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 *outp++ = (d[0] << 4) | d[1];
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 outcnt++;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 return outcnt;
18c692984549 uptools/libcoding: hex line decoding implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 }