view utils/themwi-dump-numdb.c @ 4:b280d93e8bc1

themwi-dump-numdb: old source as starting point
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 13 Dec 2023 02:18:07 +0000
parents
children 2729f94f38fb
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: flags 0x%02X, usage 0x%02X\n",
			rec.number[0], rec.number[1], rec.number[2],
			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, full number flags 0x%02X\n",
				rec.fullnum_prefix[0], rec.fullnum_prefix[1],
				rec.short_num, 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);
}