changeset 6:030143a95fb5

themwi-dump-numdb utility written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 26 Jun 2022 12:17:36 -0800
parents 9f1ce81522ea
children 7749ae8b6414
files .hgignore utils/Makefile utils/themwi-dump-numdb.c
diffstat 3 files changed, 102 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Jun 26 11:40:47 2022 -0800
+++ b/.hgignore	Sun Jun 26 12:17:36 2022 -0800
@@ -2,4 +2,5 @@
 
 \.[oa]$
 
+^utils/themwi-dump-numdb$
 ^utils/themwi-update-numdb$
--- a/utils/Makefile	Sun Jun 26 11:40:47 2022 -0800
+++ b/utils/Makefile	Sun Jun 26 12:17:36 2022 -0800
@@ -1,11 +1,14 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	themwi-update-numdb
+PROGS=	themwi-dump-numdb themwi-update-numdb
 LIBUTIL=../libutil/libutil.a
 INSTBIN=/usr/local/bin
 
 all:	${PROGS}
 
+themwi-dump-numdb:	themwi-dump-numdb.c
+	${CC} ${CFLAGS} -o $@ $@.c
+
 themwi-update-numdb:	themwi-update-numdb.o ${LIBUTIL}
 	${CC} ${CFLAGS} -o $@ $@.o ${LIBUTIL}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils/themwi-dump-numdb.c	Sun Jun 26 12:17:36 2022 -0800
@@ -0,0 +1,97 @@
+/*
+ * This program is a debug utility: it reads and dumps the compiled
+ * binary form of ThemWi number database.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include "../include/number_db_file.h"
+
+static char binfile_default_pathname[] = "/var/gsm/number-db.bin";
+static char *binfile_pathname;
+static FILE *inf;
+static struct numdb_file_hdr hdr;
+
+static void
+dump_owned_numbers()
+{
+	unsigned count;
+	uint64_t rec;
+
+	for (count = 0; count < hdr.owned_number_count; count++) {
+		if (fread(&rec, sizeof rec, 1, inf) != 1) {
+			fprintf(stderr, "error reading record from %s\n",
+				binfile_pathname);
+			exit(1);
+		}
+		if (rec < 2000000000ULL || rec > 9999999999ULL) {
+			fprintf(stderr,
+		"owned number record #%u: uint64_t value out of valid range\n",
+				count);
+			exit(1);
+		}
+		printf("Owned NANP number: +1-%llu\n", rec);
+	}
+}
+
+static void
+dump_short_numbers()
+{
+	unsigned count;
+	struct short_number_map rec;
+
+	for (count = 0; count < hdr.short_number_count; count++) {
+		if (fread(&rec, sizeof rec, 1, inf) != 1) {
+			fprintf(stderr, "error reading record from %s\n",
+				binfile_pathname);
+			exit(1);
+		}
+		if (rec.short_code > 9999) {
+			fprintf(stderr,
+	"short number record #%u: short_code field out of valid range\n",
+				count);
+			exit(1);
+		}
+		if (!rec.prefix) {
+			printf("Short number %04u is an ITN\n", rec.short_code);
+			continue;
+		}
+		if (rec.prefix < 200000 || rec.prefix > 999999) {
+			fprintf(stderr,
+		"short number record #%u: prefix field out of valid range\n",
+				count);
+			exit(1);
+		}
+		printf("Short number %04u maps to +1-%06u%04u\n",
+			rec.short_code, rec.prefix, rec.short_code);
+	}
+}
+
+main(argc, argv)
+	char **argv;
+{
+	if (argc > 2) {
+		fprintf(stderr, "usage: %s [binfile]\n", argv[0]);
+		exit(1);
+	}
+	if (argv[1])
+		binfile_pathname = argv[1];
+	else
+		binfile_pathname = binfile_default_pathname;
+	inf = fopen(binfile_pathname, "r");
+	if (!inf) {
+		perror(binfile_pathname);
+		exit(1);
+	}
+	if (fread(&hdr, sizeof hdr, 1, inf) != 1) {
+		fprintf(stderr, "error reading header from %s\n",
+			binfile_pathname);
+		exit(1);
+	}
+	printf("Count of owned NANP numbers: %u\n", hdr.owned_number_count);
+	printf("Count of defined short numbers: %u\n", hdr.short_number_count);
+	dump_owned_numbers();
+	dump_short_numbers();
+	exit(0);
+}