File: specBytesToCode.py

package info (click to toggle)
freerdp2 2.0.0~git20190204.1.2693389a+dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 22,772 kB
  • sloc: ansic: 307,908; xml: 1,676; sh: 770; perl: 231; makefile: 158; python: 65
file content (69 lines) | stat: -rw-r--r-- 1,888 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python
#
#  A script to convert blob from the MS spec to array of byte to use in unitary tests
#
#       00000000 c7 01 00 01 20 54 e2
#       00000008 c7 01 00 01 20 54 e2
#    taken from the spec, will give:
#       0xc7, 0x01, 0x00, 0x01, 0x20, 0x54, 0xe2,
#       0xc7, 0x01, 0x00, 0x01, 0x20, 0x54, 0xe2,
#
#   Notes:
#       * 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
#       * the script detects if items are hex values by searching for + or -
#
#    sample usage:
#     $ python scripts/specBytesToCode.py < image.txt > image.c
#     then go edit image.c and paste that in your code 
import sys


def getOffset(l):
    token = l.split(' ')[0]
    return int(token, 16) 
    
def isHex(l):
    return l.find('+') == -1 and l.find('-') == -1

if __name__ == '__main__':
    
    lines = []
    itemPerLine = 16
    doHex = True
    
    # parse the offset to know how many items per line we have
    l1 = sys.stdin.readline().strip()
    l2 = sys.stdin.readline().strip()
    itemsPerLine = getOffset(l2) - getOffset(l1)
    
    #
    doHex = isHex(l1)
    
    for l in [l1, l2] + sys.stdin.readlines():  
        # 00000000 c7 01 00 01 20 54 e2 cc 00 jh.kjkjhkhk
        l = l.strip() # in case we have spaces before the offset
        pos = l.find(' ')
        l = l[pos+1:]
        items = []
        
        tokens = l.strip().split(' ')
        ntokens = 0
        for t in tokens:
            if not t: # empty token
                continue
            
            if ntokens == itemPerLine:
                break
            
            item = ''
            if doHex:
                item += '0x'
            item += t
            
            items.append(item)
            
            ntokens += 1
    
        lines.append(', '.join(items))
    
    print(",\n".join(lines))