annotate libnumdb/readbin.c @ 8:db3c657efafa

themwi-short-dial: old source as starting point
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 13 Dec 2023 03:39:48 +0000
parents 6534965175dd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This library module contains the code that reads /var/gsm/number-db2.bin,
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * as well as definitions of global variables into which the booty is read.
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/stat.h>
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdint.h>
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <syslog.h>
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <themwi/nanp/number_db_v2.h>
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <themwi/nanp/number_lookup.h>
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 const char numdb_pathname[] = "/var/gsm/number-db2.bin";
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 struct stat numdb_file_stat;
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 struct numdb_file_hdr numdb_hdr;
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 struct owned_number_rec *numdb_owned_numbers;
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 struct short_number_rec *numdb_short_numbers;
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 int read_number_db(void)
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 {
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 FILE *inf;
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 inf = fopen(numdb_pathname, "r");
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (!inf) {
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 syslog(LOG_CRIT, "open %s: %m", numdb_pathname);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 return(-1);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 }
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 fstat(fileno(inf), &numdb_file_stat);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (!S_ISREG(numdb_file_stat.st_mode)) {
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 syslog(LOG_CRIT, "invalid %s: not a regular file",
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 numdb_pathname);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 fclose(inf);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 return(-1);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 }
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (fread(&numdb_hdr, sizeof numdb_hdr, 1, inf) != 1) {
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 read_err: syslog(LOG_CRIT, "error reading from %s: %m", numdb_pathname);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 fclose(inf);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 return(-1);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (numdb_hdr.owned_number_count) {
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 numdb_owned_numbers = malloc(numdb_hdr.owned_number_count *
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 sizeof(struct owned_number_rec));
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (!numdb_owned_numbers) {
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 syslog(LOG_CRIT, "malloc for owned number db: %m");
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 fclose(inf);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 return(-1);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 if (fread(numdb_owned_numbers, sizeof(struct owned_number_rec),
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 numdb_hdr.owned_number_count, inf) !=
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 numdb_hdr.owned_number_count)
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 goto read_err;
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (numdb_hdr.short_number_count) {
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 numdb_short_numbers = malloc(numdb_hdr.short_number_count *
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 sizeof(struct short_number_rec));
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 if (!numdb_short_numbers) {
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 syslog(LOG_CRIT, "malloc for short number db: %m");
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 fclose(inf);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 return(-1);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 }
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (fread(numdb_short_numbers, sizeof(struct short_number_rec),
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 numdb_hdr.short_number_count, inf) !=
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 numdb_hdr.short_number_count)
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 goto read_err;
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 }
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 fclose(inf);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 return(0);
6534965175dd libnumdb ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }