view utils/themwi-dump-numdb2.c @ 227:a349ae9d90fa

number db v2: implement themwi-dump-numdb2
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 10:18:50 -0800
parents utils/themwi-dump-numdb.c@7749ae8b6414
children 78c6e30f5234
line wrap: on
line source

/*
 * This program is a debug utility: it reads and dumps the compiled
 * binary form of ThemWi number database version 2.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "../include/number_db_v2.h"

static char binfile_default_pathname[] = "/var/gsm/number-db2.bin";
static char *binfile_pathname;
static FILE *inf;
static struct numdb_file_hdr hdr;

static void
dump_owned_numbers()
{
	unsigned count;
	struct owned_number_rec 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);
		}
		printf("Owned NANP number: %03u-%03u-%04u\n",
			rec.number[0], rec.number[1], rec.number[2]);
		printf("  Number flags 0x%02X, usage byte 0x%02X\n",
			rec.number_flags, rec.usage);
		if ((rec.usage & NUMBER_USAGE_MASK) == NUMBER_USAGE_TYPE_ALIAS)
			printf("  Alias maps to: %03u-%03u-%04u\n",
				rec.remap[0], rec.remap[1], rec.remap[2]);
		if (rec.usage & NUMBER_USAGE_FLAG_E911_VIA)
			printf("  E911 route via: %03u-%03u-%04u\n",
				rec.remap[0], rec.remap[1], rec.remap[2]);
	}
}

static void
dump_short_numbers()
{
	unsigned count;
	struct short_number_rec 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);
		}
		printf("Short number %04u is of type 0x%02X\n", rec.short_num,
			rec.short_num_type);
		if (rec.short_num_type == SHORT_NUM_TYPE_ABBREV) {
			printf("  Abbrev maps to: %03u-%03u-%04u\n",
				rec.fullnum_prefix[0], rec.fullnum_prefix[1],
				rec.short_num);
			printf("  Full number flags: 0x%02X\n",
				rec.fullnum_flags);
		}
	}
}

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);
}