view utils/themwi-dump-numdb.c @ 147:94b5831c017f

sip-manual-out code: split bye_in.c from uas.c
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 08 Oct 2022 23:01:32 -0800
parents 7749ae8b6414
children
line wrap: on
line source

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