FreeCalypso > hg > themwi-system-sw
annotate libnumdb/readbin.c @ 184:f8c40090a0a8
librtpalloc: new library for talking to themwi-rtp-mgr
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 11 Mar 2023 23:48:14 -0800 |
parents | ffd48df829a7 |
children |
rev | line source |
---|---|
8
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This library module contains the code that reads /var/gsm/number-db.bin, |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * as well as definitions of global variables into which the booty is read. |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/stat.h> |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdio.h> |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdint.h> |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdlib.h> |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <syslog.h> |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include "../include/number_db_file.h" |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 char numdb_pathname[] = "/var/gsm/number-db.bin"; |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 struct stat numdb_file_stat; |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 struct numdb_file_hdr numdb_hdr; |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 uint64_t *numdb_owned_numbers; |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 struct short_number_map *numdb_short_numbers; |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 read_number_db() |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 { |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 FILE *inf; |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 inf = fopen(numdb_pathname, "r"); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 if (!inf) { |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 syslog(LOG_CRIT, "open %s: %m", numdb_pathname); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 return(-1); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 } |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 fstat(fileno(inf), &numdb_file_stat); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 if (!S_ISREG(numdb_file_stat.st_mode)) { |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 syslog(LOG_CRIT, "invalid %s: not a regular file", |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 numdb_pathname); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 fclose(inf); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 return(-1); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 } |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 if (fread(&numdb_hdr, sizeof numdb_hdr, 1, inf) != 1) { |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 read_err: syslog(LOG_CRIT, "error reading from %s: %m", numdb_pathname); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 fclose(inf); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 return(-1); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 } |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 if (numdb_hdr.owned_number_count) { |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 numdb_owned_numbers = malloc(numdb_hdr.owned_number_count * |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 sizeof(uint64_t)); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 if (!numdb_owned_numbers) { |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 syslog(LOG_CRIT, "malloc for owned number db: %m"); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 fclose(inf); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 return(-1); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 if (fread(numdb_owned_numbers, sizeof(uint64_t), |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 numdb_hdr.owned_number_count, inf) != |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 numdb_hdr.owned_number_count) |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 goto read_err; |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 if (numdb_hdr.short_number_count) { |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 numdb_short_numbers = malloc(numdb_hdr.short_number_count * |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 sizeof(struct short_number_map)); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 if (!numdb_short_numbers) { |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 syslog(LOG_CRIT, "malloc for short number db: %m"); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 fclose(inf); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 return(-1); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (fread(numdb_short_numbers, sizeof(struct short_number_map), |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 numdb_hdr.short_number_count, inf) != |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 numdb_hdr.short_number_count) |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 goto read_err; |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 } |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 fclose(inf); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 return(0); |
ffd48df829a7
beginning of libnumdb: reading the binary file
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 } |