3 from cStringIO import StringIO
6 from dbparse import DBParser, create_rules, create_collections
11 def be32(output, val):
12 output.write(struct.pack('>I', val))
15 def __init__(self, output):
17 self._pos = output.tell()
18 be32(output, 0xFFFFFFFF)
20 def set(self, val=None):
22 val = self._output.tell()
24 pos = self._output.tell()
25 self._output.seek(self._pos)
26 be32(self._output, val)
27 self._output.seek(pos)
33 bands, power, countries = p.parse(file('db.txt'))
34 rules = create_rules(countries)
35 rules.sort(cmp=lambda x, y: cmp(bands[x[0]], bands[y[0]]))
36 collections = create_collections(countries)
37 collections.sort(cmp=lambda x, y: cmp(bands[x[0][0]], bands[y[0][0]]))
41 # struct regdb_file_header
44 reg_country_ptr = PTR(output)
45 # add number of countries
46 be32(output, len(countries))
50 pi = [(i, p) for i, p in power.iteritems()]
51 pi.sort(cmp=lambda x, y: cmp(x[1], y[1]))
52 for power_rule_id, pr in pi:
54 pr = [int(v * 100) for v in pr[1:]]
55 power_rules[power_rule_id] = output.tell()
56 # struct regdb_file_power_rule
57 output.write(struct.pack('>cxxxIIIII', str(environ), *pr))
60 bi = [(f, i) for f, i in bands.iteritems()]
61 bi.sort(cmp=lambda x, y: cmp(x[1], y[1]))
62 for freq_range_id, fr in bands.iteritems():
63 freq_ranges[freq_range_id] = output.tell()
65 fr = [int(f * 1000) for f in fr[:3]]
66 # struct regdb_file_freq_range
67 output.write(struct.pack('>IIIII', fr[0], fr[1], fr[2], fl, 0))
71 for reg_rule in rules:
72 freq_range_id, power_rule_id = reg_rule
73 reg_rules[reg_rule] = output.tell()
74 # struct regdb_file_reg_rule
75 output.write(struct.pack('>II', freq_ranges[freq_range_id], power_rules[power_rule_id]))
78 reg_rules_collections = {}
80 for coll in collections:
81 reg_rules_collections[coll] = output.tell()
82 # struct regdb_file_reg_rules_collection
83 be32(output, len(coll))
85 coll.sort(cmp=lambda x, y: cmp(bands[x[0]], bands[y[0]]))
87 be32(output, reg_rules[regrule])
89 # update country pointer now!
92 countrynames = countries.keys()
94 for alpha2 in countrynames:
95 coll = countries[alpha2]
96 # struct regdb_file_reg_country
97 output.write(struct.pack('>ccxxI', str(alpha2[0]), str(alpha2[1]), reg_rules_collections[coll]))
99 # Load RSA only now so people can use this script
100 # without having those libraries installed to verify
102 from M2Crypto import RSA
104 # determine signature length
105 key = RSA.load_key('key.priv.pem')
107 hash.update(output.getvalue())
108 sig = key.sign(hash.digest())
113 hash.update(output.getvalue())
114 sig = key.sign(hash.digest())
118 outfile = open('regulatory.bin', 'w')
119 outfile.write(output.getvalue())