comparison libtest/tw5reader.c @ 522:4d2cccaeb4a7

libtest: implement TW-TS-005 reader function Spec reference: https://www.freecalypso.org/specs/tw-ts-005-v010002.txt
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 19 Sep 2024 03:08:47 +0000
parents libtest/binreader.c@820d88b97924
children
comparison
equal deleted inserted replaced
521:68fe269b4316 522:4d2cccaeb4a7
1 /*
2 * Here we implement our twts005_read_frame() function.
3 */
4
5 #include <ctype.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <strings.h>
10 #include "tw5reader.h"
11
12 static int decode_hex_digit(char c)
13 {
14 if (isdigit(c))
15 return c - '0';
16 else if (isupper(c))
17 return c - 'A' + 10;
18 else
19 return c - 'a' + 10;
20 }
21
22 int twts005_read_frame(FILE *hexf, unsigned *lineno, uint8_t *frame,
23 unsigned *lenp)
24 {
25 char linebuf[81];
26 char *cp, *np;
27 uint8_t *dp;
28 unsigned len;
29
30 for (;;) {
31 if (!fgets(linebuf, sizeof linebuf, hexf))
32 return 0;
33 (*lineno)++;
34 if (!index(linebuf, '\n'))
35 return -2;
36 for (cp = linebuf; isspace(*cp); cp++)
37 ;
38 if (*cp != '\0' && *cp != '#')
39 break;
40 }
41 for (np = cp; *cp && !isspace(*cp); cp++)
42 ;
43 if (*cp)
44 *cp++ = '\0';
45 while (isspace(*cp))
46 cp++;
47 if (*cp != '\0' && *cp != '#')
48 return -1;
49 if (!strcasecmp(np, "NULL")) {
50 *lenp = 0;
51 return 1;
52 }
53
54 dp = frame;
55 len = 0;
56 for (cp = np; *cp; cp += 2) {
57 if (!isxdigit(cp[0]) || !isxdigit(cp[1]))
58 return -1;
59 *dp++ = (decode_hex_digit(cp[0]) << 4) |
60 decode_hex_digit(cp[1]);
61 len++;
62 }
63 *lenp = len;
64 return 1;
65 }