changelog
[platform/upstream/freerdp.git] / scripts / specBytesToCode.py
1 #!/usr/bin/python
2 #
3 #  A script to convert blob from the MS spec to array of byte to use in unitary tests
4 #
5 #       00000000 c7 01 00 01 20 54 e2
6 #       00000008 c7 01 00 01 20 54 e2
7 #    taken from the spec, will give:
8 #       0xc7, 0x01, 0x00, 0x01, 0x20, 0x54, 0xe2,
9 #       0xc7, 0x01, 0x00, 0x01, 0x20, 0x54, 0xe2,
10 #
11 #   Notes:
12 #       * the script reads the two first lines to detect the number of items per lines, so you need a blob with at least 2 lines
13 #       * the script detects if items are hex values by searching for + or -
14 #
15 #    sample usage:
16 #     $ python scripts/specBytesToCode.py < image.txt > image.c
17 #     then go edit image.c and paste that in your code 
18 import sys
19
20
21 def getOffset(l):
22     token = l.split(' ')[0]
23     return int(token, 16) 
24     
25 def isHex(l):
26     return l.find('+') == -1 and l.find('-') == -1
27
28 if __name__ == '__main__':
29     
30     lines = []
31     itemPerLine = 16
32     doHex = True
33     
34     # parse the offset to know how many items per line we have
35     l1 = sys.stdin.readline().strip()
36     l2 = sys.stdin.readline().strip()
37     itemsPerLine = getOffset(l2) - getOffset(l1)
38     
39     #
40     doHex = isHex(l1)
41     
42     for l in [l1, l2] + sys.stdin.readlines():  
43         # 00000000 c7 01 00 01 20 54 e2 cc 00 jh.kjkjhkhk
44         l = l.strip() # in case we have spaces before the offset
45         pos = l.find(' ')
46         l = l[pos+1:]
47         items = []
48         
49         tokens = l.strip().split(' ')
50         ntokens = 0
51         for t in tokens:
52             if not t: # empty token
53                 continue
54             
55             if ntokens == itemPerLine:
56                 break
57             
58             item = ''
59             if doHex:
60                 item += '0x'
61             item += t
62             
63             items.append(item)
64             
65             ntokens += 1
66     
67         lines.append(', '.join(items))
68     
69     print(",\n".join(lines))