3 from cStringIO import StringIO
6 from dbparse import DBParser
11 def create_rules(countries):
13 for c in countries.itervalues():
14 for rule in c.permissions:
18 def create_collections(countries):
20 for c in countries.itervalues():
21 result[c.permissions] = 1
25 def be32(output, val):
26 output.write(struct.pack('>I', val))
29 def __init__(self, output):
31 self._pos = output.tell()
32 be32(output, 0xFFFFFFFF)
34 def set(self, val=None):
36 val = self._output.tell()
38 pos = self._output.tell()
39 self._output.seek(self._pos)
40 be32(self._output, val)
41 self._output.seek(pos)
47 countries = p.parse(file('db.txt'))
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))
63 # struct regdb_file_header
66 reg_country_ptr = PTR(output)
67 # add number of countries
68 be32(output, len(countries))
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))
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))
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],
95 reg_rules_collections = {}
97 for coll in collections:
98 reg_rules_collections[coll] = output.tell()
99 # struct regdb_file_reg_rules_collection
101 be32(output, len(coll))
102 coll.sort(cmp=lambda x, y: cmp(x.freqband, y.freqband))
104 be32(output, reg_rules[regrule])
106 # update country pointer now!
107 reg_country_ptr.set()
109 countrynames = countries.keys()
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]))
116 # Load RSA only now so people can use this script
117 # without having those libraries installed to verify
119 from M2Crypto import RSA
121 # determine signature length
122 key = RSA.load_key('key.priv.pem')
124 hash.update(output.getvalue())
125 sig = key.sign(hash.digest())
130 hash.update(output.getvalue())
131 sig = key.sign(hash.digest())
135 outfile = open('regulatory.bin', 'w')
136 outfile.write(output.getvalue())