# HG changeset patch # User Mychaela Falconia # Date 1721642458 0 # Node ID 183e81c8f6c048b4865d18bcea9d473a01ee793b # Parent 1a852266ba741e66672e027d78ae2d5bdc2633ac hr-bits: sid-count program written diff -r 1a852266ba74 -r 183e81c8f6c0 .hgignore --- a/.hgignore Fri May 24 21:19:59 2024 +0000 +++ b/.hgignore Mon Jul 22 10:00:58 2024 +0000 @@ -36,6 +36,8 @@ ^frbl/reconst/[A-Za-z_0-9]*\.obj$ ^frbl/test/frbl2test$ +^hr-bits/sid-count$ + ^leo-obj/.*\.ctypes$ ^leo-obj/.*\.disasm$ ^leo-obj/.*\.symtab$ diff -r 1a852266ba74 -r 183e81c8f6c0 hr-bits/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hr-bits/Makefile Mon Jul 22 10:00:58 2024 +0000 @@ -0,0 +1,14 @@ +CC= gcc +CFLAGS= -O2 +STD= sid-count +PROGS= ${STD} + +all: ${PROGS} + +${STD}: + ${CC} ${CFLAGS} -o $@ $@.c + +sid-count: sid-count.c + +clean: + rm -f ${PROGS} *.o *errs *.out diff -r 1a852266ba74 -r 183e81c8f6c0 hr-bits/sid-count.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hr-bits/sid-count.c Mon Jul 22 10:00:58 2024 +0000 @@ -0,0 +1,181 @@ +/* + * This program reads a TCH/HS DL capture file made with FC tools + * and analyzes the hex string of payload bits in every frame. + * For each frame, this program counts how many bits in the SID field + * are set (both class 1 and class 2. All input lines are passed + * through to the output, but at the end of each line this program + * appends bit counts that match the conventions used in GSM 06.06 + * swSidDetection() function. + */ + +#include +#include +#include +#include +#include +#include + +static const uint8_t class1_mask[12] = {0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xF9, + 0x3F, 0xC0, 0x0F, 0x78, 0x0E, 0x06}; +static const uint8_t class2_mask[3] = {0x1F, 0xFF, 0xF0}; + +static char *infname, *outfname; +static FILE *inf, *outf; +static int lineno; +static char linebuf[80]; +static uint8_t frame[15]; + +static int +is_hex_string_spc(str, expect_len) + char *str; + unsigned expect_len; +{ + char *cp, *endp; + + endp = str + expect_len; + for (cp = str; cp < endp; cp++) { + if (!isxdigit(*cp)) + return 0; + } + if (*cp == ' ') + return 1; + else + return 0; +} + +static int +decode_hex_digit(c) +{ + int d; + + if (isdigit(c)) + d = c - '0'; + else if (isupper(c)) + d = c - 'A' + 10; + else + d = c - 'a' + 10; + return d; +} + +static void +frame_from_hex() +{ + char *cp; + uint8_t *dp; + unsigned n, u, l; + + cp = linebuf + 18; + dp = frame; + for (n = 0; n < 15; n++) { + u = decode_hex_digit(*cp++); + l = decode_hex_digit(*cp++); + *dp++ = (u << 4) | l; + } +} + +static unsigned +ones_in_byte(byte) + unsigned byte; +{ + unsigned count; + + count = 0; + for (; byte; byte >>= 1) { + if (byte & 1) + count++; + } + return count; +} + +static unsigned +count_1_bits(data, mask, nbytes) + uint8_t *data, *mask; + unsigned nbytes; +{ + unsigned n, and, accum; + + accum = 0; + for (n = 0; n < nbytes; n++) { + and = *data++ & *mask++; + accum += ones_in_byte(and); + } + return accum; +} + +static void +process_line() +{ + char *cp; + unsigned class1_ones, class1_zeros; + unsigned class2_ones, class2_zeros; + unsigned n1n2; + + cp = index(linebuf, '\n'); + if (!cp) { + fprintf(stderr, "%s line %d: too long or missing newline\n", + infname, lineno); + exit(1); + } + *cp = '\0'; + if (strncmp(linebuf, "HR ", 3)) { +inv: fprintf(stderr, + "%s line %d: format does not match HR DL capture\n", + infname, lineno); + exit(1); + } + if (!is_hex_string_spc(linebuf + 3, 4)) + goto inv; + if (!is_hex_string_spc(linebuf + 8, 4)) + goto inv; + if (!is_hex_string_spc(linebuf + 13, 4)) + goto inv; + if (!is_hex_string_spc(linebuf + 18, 30)) + goto inv; + frame_from_hex(); + + class1_ones = count_1_bits(frame, class1_mask, 12); + class1_zeros = 62 - class1_ones; + class2_ones = count_1_bits(frame + 12, class2_mask, 3); + class2_zeros = 17 - class2_ones; + n1n2 = class1_zeros + class2_zeros; + + fprintf(outf, "%s %u %u", linebuf, class1_zeros, n1n2); + if (n1n2 >= 11 && n1n2 < 16) + fputs(" SID-1-diff", outf); + if (n1n2 >= 16 && n1n2 <= 25) + fputs(" BCI-to-BFI", outf); + putc('\n', outf); +} + +main(argc, argv) + char **argv; +{ + if (argc >= 2) { + infname = argv[1]; + inf = fopen(infname, "r"); + if (!inf) { + perror(infname); + exit(1); + } + } else { + inf = stdin; + infname = "stdin"; + } + + if (argc >= 3) { + outfname = argv[2]; + outf = fopen(outfname, "w"); + if (!outf) { + perror(outfname); + exit(1); + } + } else { + outf = stdout; + outfname = "stdout"; + } + + for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) + process_line(); + + exit(0); +}