FreeCalypso > hg > themwi-system-sw
comparison utils/themwi-dump-numdb.c @ 6:030143a95fb5
themwi-dump-numdb utility written, compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 26 Jun 2022 12:17:36 -0800 |
parents | |
children | 7749ae8b6414 |
comparison
equal
deleted
inserted
replaced
5:9f1ce81522ea | 6:030143a95fb5 |
---|---|
1 /* | |
2 * This program is a debug utility: it reads and dumps the compiled | |
3 * binary form of ThemWi number database. | |
4 */ | |
5 | |
6 #include <stdio.h> | |
7 #include <stdint.h> | |
8 #include <stdlib.h> | |
9 #include "../include/number_db_file.h" | |
10 | |
11 static char binfile_default_pathname[] = "/var/gsm/number-db.bin"; | |
12 static char *binfile_pathname; | |
13 static FILE *inf; | |
14 static struct numdb_file_hdr hdr; | |
15 | |
16 static void | |
17 dump_owned_numbers() | |
18 { | |
19 unsigned count; | |
20 uint64_t rec; | |
21 | |
22 for (count = 0; count < hdr.owned_number_count; count++) { | |
23 if (fread(&rec, sizeof rec, 1, inf) != 1) { | |
24 fprintf(stderr, "error reading record from %s\n", | |
25 binfile_pathname); | |
26 exit(1); | |
27 } | |
28 if (rec < 2000000000ULL || rec > 9999999999ULL) { | |
29 fprintf(stderr, | |
30 "owned number record #%u: uint64_t value out of valid range\n", | |
31 count); | |
32 exit(1); | |
33 } | |
34 printf("Owned NANP number: +1-%llu\n", rec); | |
35 } | |
36 } | |
37 | |
38 static void | |
39 dump_short_numbers() | |
40 { | |
41 unsigned count; | |
42 struct short_number_map rec; | |
43 | |
44 for (count = 0; count < hdr.short_number_count; count++) { | |
45 if (fread(&rec, sizeof rec, 1, inf) != 1) { | |
46 fprintf(stderr, "error reading record from %s\n", | |
47 binfile_pathname); | |
48 exit(1); | |
49 } | |
50 if (rec.short_code > 9999) { | |
51 fprintf(stderr, | |
52 "short number record #%u: short_code field out of valid range\n", | |
53 count); | |
54 exit(1); | |
55 } | |
56 if (!rec.prefix) { | |
57 printf("Short number %04u is an ITN\n", rec.short_code); | |
58 continue; | |
59 } | |
60 if (rec.prefix < 200000 || rec.prefix > 999999) { | |
61 fprintf(stderr, | |
62 "short number record #%u: prefix field out of valid range\n", | |
63 count); | |
64 exit(1); | |
65 } | |
66 printf("Short number %04u maps to +1-%06u%04u\n", | |
67 rec.short_code, rec.prefix, rec.short_code); | |
68 } | |
69 } | |
70 | |
71 main(argc, argv) | |
72 char **argv; | |
73 { | |
74 if (argc > 2) { | |
75 fprintf(stderr, "usage: %s [binfile]\n", argv[0]); | |
76 exit(1); | |
77 } | |
78 if (argv[1]) | |
79 binfile_pathname = argv[1]; | |
80 else | |
81 binfile_pathname = binfile_default_pathname; | |
82 inf = fopen(binfile_pathname, "r"); | |
83 if (!inf) { | |
84 perror(binfile_pathname); | |
85 exit(1); | |
86 } | |
87 if (fread(&hdr, sizeof hdr, 1, inf) != 1) { | |
88 fprintf(stderr, "error reading header from %s\n", | |
89 binfile_pathname); | |
90 exit(1); | |
91 } | |
92 printf("Count of owned NANP numbers: %u\n", hdr.owned_number_count); | |
93 printf("Count of defined short numbers: %u\n", hdr.short_number_count); | |
94 dump_owned_numbers(); | |
95 dump_short_numbers(); | |
96 exit(0); | |
97 } |