FreeCalypso > hg > freecalypso-tools
annotate uptools/libcoding/hexdecode.c @ 992:c4c400c1b10c
fc-loadtool flash: no flash2 ppb-erase-all
All PPBs from across the entire flash chip can only be erased together
as a unit, spanning across both banks on 16 MiB chips - therefore,
we shall disallow 'flash2 ppb-erase-all', and accept 'flash ppb-erase-all'
only.
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 03 Dec 2023 04:47:31 +0000 |
parents | 18c692984549 |
children |
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 } |