comparison libnumdb/readbin.c @ 1:6534965175dd

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