Ignore more binary files.
[platform/upstream/crda.git] / dump.c
1 #include <errno.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <arpa/inet.h> /* ntohl */
8
9 #include "regdb.h"
10
11 static void *get_file_ptr(__u8 *db, int dblen, int structlen, __be32 ptr)
12 {
13         __u32 p = ntohl(ptr);
14
15         if (p > dblen - structlen) {
16                 fprintf(stderr, "Invalid database file, bad pointer!\n");
17                 exit(3);
18         }
19
20         return (void *)(db + p);
21 }
22
23 static void print_reg_rule(__u8 *db, int dblen, __be32 ruleptr)
24 {
25         struct regdb_file_reg_rule *rule;
26         struct regdb_file_freq_range *freq;
27         struct regdb_file_power_rule *power;
28         __u32 flags;
29
30         rule = get_file_ptr(db, dblen, sizeof(*rule), ruleptr);
31         freq = get_file_ptr(db, dblen, sizeof(*freq), rule->freq_range_ptr);
32         power = get_file_ptr(db, dblen, sizeof(*power), rule->power_rule_ptr);
33
34         printf("\t(%.3f - %.3f @ %.3f), ",
35                ((float)ntohl(freq->start_freq))/1000.0,
36                ((float)ntohl(freq->end_freq))/1000.0,
37                ((float)ntohl(freq->max_bandwidth))/1000.0);
38
39         printf("(");
40
41         if (power->max_antenna_gain)
42                 printf("%.2f, ", ((float)ntohl(power->max_antenna_gain)/100.0));
43         else
44                 printf("N/A, ");
45
46         if (power->max_eirp)
47                 printf("%.2f)", ((float)ntohl(power->max_eirp)/100.0));
48         else
49                 printf("N/A)");
50
51         flags = ntohl(rule->flags);
52         if (flags & RRF_NO_OFDM)
53                 printf(", NO-OFDM");
54         if (flags & RRF_NO_CCK)
55                 printf(", NO-CCK");
56         if (flags & RRF_NO_INDOOR)
57                 printf(", NO-INDOOR");
58         if (flags & RRF_NO_OUTDOOR)
59                 printf(", NO-OUTDOOR");
60         if (flags & RRF_DFS)
61                 printf(", DFS");
62         if (flags & RRF_PTP_ONLY)
63                 printf(", PTP-ONLY");
64         if (flags & RRF_PTMP_ONLY)
65                 printf(", PTMP-ONLY");
66         if (flags & RRF_PASSIVE_SCAN)
67                 printf(", PASSIVE-SCAN");
68         if (flags & RRF_NO_IBSS)
69                 printf(", NO-IBSS");
70
71         printf("\n");
72 }
73
74 int main(int argc, char **argv)
75 {
76         int fd;
77         struct stat stat;
78         __u8 *db;
79         struct regdb_file_header *header;
80         struct regdb_file_reg_country *countries;
81         int dblen, siglen, num_countries, i, j;
82
83         if (argc != 2) {
84                 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
85                 return 2;
86         }
87
88         fd = open(argv[1], O_RDONLY);
89         if (fd < 0) {
90                 perror("failed to open db file");
91                 return 2;
92         }
93
94         if (fstat(fd, &stat)) {
95                 perror("failed to fstat db file");
96                 return 2;
97         }
98
99         dblen = stat.st_size;
100
101         db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
102         if (db == MAP_FAILED) {
103                 perror("failed to mmap db file");
104                 return 2;
105         }
106
107         header = get_file_ptr(db, dblen, sizeof(*header), 0);
108
109         if (ntohl(header->magic) != REGDB_MAGIC) {
110                 fprintf(stderr, "Invalid database magic\n");
111                 return 2;
112         }
113
114         if (ntohl(header->version) != REGDB_VERSION) {
115                 fprintf(stderr, "Invalid database version\n");
116                 return 2;
117         }
118
119         siglen = ntohl(header->signature_length);
120         /* adjust dblen so later sanity checks don't run into the signature */
121         dblen -= siglen;
122
123         if (dblen <= sizeof(*header)) {
124                 fprintf(stderr, "Invalid signature length %d\n", siglen);
125                 return 2;
126         }
127
128         /* verify signature */
129         if (!crda_verify_db_signature(db, dblen, siglen))
130                 return -EINVAL;
131
132         num_countries = ntohl(header->reg_country_num);
133         countries = get_file_ptr(db, dblen,
134                                  sizeof(struct regdb_file_reg_country) * num_countries,
135                                  header->reg_country_ptr);
136
137         for (i = 0; i < num_countries; i++) {
138                 struct regdb_file_reg_rules_collection *rcoll;
139                 struct regdb_file_reg_country *country = countries + i;
140                 int num_rules;
141
142                 printf("country %.2s:\n", country->alpha2);
143                 rcoll = get_file_ptr(db, dblen, sizeof(*rcoll), country->reg_collection_ptr);
144                 num_rules = ntohl(rcoll->reg_rule_num);
145                 /* re-get pointer with sanity checking for num_rules */
146                 rcoll = get_file_ptr(db, dblen,
147                                      sizeof(*rcoll) + num_rules * sizeof(__be32),
148                                      country->reg_collection_ptr);
149                 for (j = 0; j < num_rules; j++)
150                         print_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j]);
151                 printf("\n");
152         }
153
154         return 0;
155 }