This repository was archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmodp_json_gen.py
executable file
·60 lines (50 loc) · 1.58 KB
/
modp_json_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
def intarray_to_c(ary, name):
parts = []
parts.append("static const size_t {0}[{1}] = {{".format(name,len(ary)))
count = 0
for val in ary:
if (count % 10 == 0):
parts.append("\n ")
parts.append("{0}, ".format(val))
count += 1
parts.append("\n};\n")
return ''.join(parts)
def chararray_to_c(ary, name):
parts = []
parts.append("static const uint8_t {0}[{1}] = {{".format(name,
len(ary)))
count = 0;
for val in ary:
if (count % 10 == 0):
parts.append("\n ")
parts.append("{0:>6}".format("'" + val + "',"))
count += 1
parts.append("\n};\n")
return ''.join(parts)
def json_encodelen_map():
lenmap = [ 6 for i in range(256) ]
for i in range(32,128):
lenmap[i] = 1
for i in [0x08, 0x09, 0x0a, 0x0c, 0x0d, 0x5c, 0x22]:
lenmap[i] = 2
return lenmap
def json_encode_map():
encodemap = [ 'u' for i in range(256) ]
# things to quad-hex encode \u00XY
for i in range(32,128):
encodemap[i] = 'a';
# special escapes
encodemap[0x08] = 'b'
encodemap[0x09] = 't'
encodemap[0x0a] = 'n'
encodemap[0x0c] = 'f'
encodemap[0x0d] = 'r'
encodemap[0x5c] = "\\\\"
encodemap[0x22] = "\""
return encodemap
if __name__ == '__main__':
with open('modp_json_data.h', 'w') as fd:
fd.write(chararray_to_c(json_encode_map(), "gsJSONEncodeMap"))
fd.write("\n")
fd.write(intarray_to_c(json_encodelen_map(), "gsJSONEncodeLenMap"))