-
Notifications
You must be signed in to change notification settings - Fork 60
/
fill_v_value.py
72 lines (63 loc) · 1.74 KB
/
fill_v_value.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
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# encoding: utf-8
# Author: Liu DongMiao <[email protected]>
# Created : Sun 20 May 2018 12:04:18 AM CST
# Modified : Wed 04 May 2022 12:35:38 AM CST
import sys
k = sys.argv[1]
v = sys.argv[2]
l = len(v)
P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137]
def find_m(x):
p = P[:-2]
low = 0
high = len(p) - 1
if x > p[high]:
return p[high]
elif x <= p[low]:
return p[low]
while low <= high:
mid = int(low + (high - low) / 2)
if x > p[mid]:
if x < p[mid + 1]:
return p[mid]
else:
low = mid + 1
elif x < p[mid]:
high = mid - 1
else:
return p[mid - 1]
else:
raise
m = find_m(l)
method = ''
if '(' in v or ';' in v:
method = 'signature'
else:
method = v.replace('/', '_').replace('%', '').replace(' ', '_')
if len(sys.argv) > 3:
method = sys.argv[3]
if '(' in v or ';' in v and not method.endswith('_signature'):
method += '_signature'
print('static inline void fill_%s(char %s[]) {' % (method, k))
print(' // %s' % v)
print(' static unsigned int m = 0;')
print('''
if (m == 0) {
m = %d;
} else if (m == %d) {
m = %d;
}
''' % (m, P[P.index(m) + 1], P[P.index(m) + 2]))
for x in range(l):
o = ord(v[x]) ^ ((x + l) % m)
s = repr(chr(o))
if s == '''"'"''':
s = "'\\''"
print(" %s[0x%x] = %s%s;" % (k, x, o > 127 and "(char) " or "", s))
print(''' for (unsigned int i = 0; i < 0x%x; ++i) {
%s[i] ^= ((i + 0x%x) %% m);
}
%s[0x%x] = '\\0';''' % (l, k, l, k, l))
print('}')
# vim: set sta sw=4 et: