remove "max IR" from database
[platform/upstream/crda.git] / db2bin.py
1 #!/usr/bin/env python
2
3 from cStringIO import StringIO
4 import struct
5 import sha
6 from dbparse import DBParser
7
8 MAGIC = 0x52474442
9 VERSION = 19
10
11 def create_rules(countries):
12     result = {}
13     for c in countries.itervalues():
14         for rule in c.permissions:
15             result[rule] = 1
16     return result.keys()
17
18 def create_collections(countries):
19     result = {}
20     for c in countries.itervalues():
21         result[c.permissions] = 1
22     return result.keys()
23
24
25 def be32(output, val):
26     output.write(struct.pack('>I', val))
27
28 class PTR(object):
29     def __init__(self, output):
30         self._output = output
31         self._pos = output.tell()
32         be32(output, 0xFFFFFFFF)
33
34     def set(self, val=None):
35         if val is None:
36             val = self._output.tell()
37         self._offset = val
38         pos = self._output.tell()
39         self._output.seek(self._pos)
40         be32(self._output, val)
41         self._output.seek(pos)
42
43     def get(self):
44         return self._offset
45
46 p = DBParser()
47 countries = p.parse(file('db.txt'))
48 power = []
49 bands = []
50 for c in countries.itervalues():
51     for perm in c.permissions:
52         if not perm.freqband in bands:
53             bands.append(perm.freqband)
54         if not perm.power in power:
55             power.append(perm.power)
56 rules = create_rules(countries)
57 rules.sort(cmp=lambda x, y: cmp(x.freqband, y.freqband))
58 collections = create_collections(countries)
59 collections.sort(cmp=lambda x, y: cmp(x[0].freqband, y[0].freqband))
60
61 output = StringIO()
62
63 # struct regdb_file_header
64 be32(output, MAGIC)
65 be32(output, VERSION)
66 reg_country_ptr = PTR(output)
67 # add number of countries
68 be32(output, len(countries))
69 siglen = PTR(output)
70
71 power_rules = {}
72 for pr in power:
73     power_rules[pr] = output.tell()
74     pr = [int(v * 100.0) for v in (pr.max_ant_gain, pr.max_eirp)]
75     # struct regdb_file_power_rule
76     output.write(struct.pack('>II', *pr))
77
78 freq_ranges = {}
79 for fr in bands:
80     freq_ranges[fr] = output.tell()
81     fr = [int(f * 1000.0) for f in (fr.start, fr.end, fr.maxbw)]
82     # struct regdb_file_freq_range
83     output.write(struct.pack('>III', *fr))
84
85
86 reg_rules = {}
87 for reg_rule in rules:
88     freq_range, power_rule = reg_rule.freqband, reg_rule.power
89     reg_rules[reg_rule] = output.tell()
90     # struct regdb_file_reg_rule
91     output.write(struct.pack('>III', freq_ranges[freq_range], power_rules[power_rule],
92                              reg_rule.flags))
93
94
95 reg_rules_collections = {}
96
97 for coll in collections:
98     reg_rules_collections[coll] = output.tell()
99     # struct regdb_file_reg_rules_collection
100     coll = list(coll)
101     be32(output, len(coll))
102     coll.sort(cmp=lambda x, y: cmp(x.freqband, y.freqband))
103     for regrule in coll:
104         be32(output, reg_rules[regrule])
105
106 # update country pointer now!
107 reg_country_ptr.set()
108
109 countrynames = countries.keys()
110 countrynames.sort()
111 for alpha2 in countrynames:
112     coll = countries[alpha2]
113     # struct regdb_file_reg_country
114     output.write(struct.pack('>ccxxI', str(alpha2[0]), str(alpha2[1]), reg_rules_collections[coll.permissions]))
115
116 # Load RSA only now so people can use this script
117 # without having those libraries installed to verify
118 # their SQL changes
119 from M2Crypto import RSA
120
121 # determine signature length
122 key = RSA.load_key('key.priv.pem')
123 hash = sha.new()
124 hash.update(output.getvalue())
125 sig = key.sign(hash.digest())
126 # write it to file
127 siglen.set(len(sig))
128 # sign again
129 hash = sha.new()
130 hash.update(output.getvalue())
131 sig = key.sign(hash.digest())
132
133 output.write(sig)
134
135 outfile = open('regulatory.bin', 'w')
136 outfile.write(output.getvalue())