Ticket #32: isfencodings.2.py

File isfencodings.2.py, 2.4 kB (added by kevincampbell, 11 months ago)

functions to decode/encode multibyte integers

Line 
1from array import array
2
3'''Decodes an unsigned 32 bit multi-byte integer'''
4def dec_mbuint32(values,location=0):
5        temp = 0
6        bitcount = 0
7        next = True
8        while next:
9                byte = values[location]
10                num = byte & 0x7f
11                temp = temp * pow(2,7)
12                temp = temp | num
13                bitcount += 7
14                location += 1
15                if byte > 0x7f and bitcount < 29:
16                        next = True
17                else:
18                        next = False
19        return temp, location
20
21'''Encodes an unsigned 32 bit multi-byte integer'''
22def enc_mbuint32(value):
23        if value > 0xfffffff or value < 0:
24                return None
25        new = True
26        cont = False
27        num = 0
28        a = array('B')
29        while new or cont:
30                for i in range(7):
31                        bit = value % 2
32                        value = int(value / 2)
33                        if bit and i < 7:
34                                num += pow(2,i)
35                        if value > 0:
36                                cont = True
37                        else:
38                                cont = False
39                if new:
40                        new = False
41                else:
42                        num += pow(2,7)
43                a.insert(0,num)
44                print hex(num)
45                num = 0
46        return a
47
48'''Decodes a signed 32 bit multi-byte integer'''
49def dec_mbsint32(values,location=0):
50        temp, location = dec_mbuint64(values, location)
51        sign = temp & 1
52        temp = temp / 2
53        if sign:
54                temp = temp * -1
55        return temp, location
56
57'''Encodes a signed 32 bit multi-byte integer'''
58def enc_mbsint32(value):
59        values = enc_mbuint64(abs(value)*2)
60        if value < 0 and values:
61                values[len(values)-1] += 1
62        return values
63       
64
65'''Decodes an unsigned 64 bit multi-byte integer'''
66def dec_mbuint64(values,location=0):
67        temp = 0
68        bitcount = 0
69        next = True
70        while next:
71                byte = values[location]
72                num = byte & 0x7f
73                temp = temp * pow(2,7)
74                temp = temp | num
75                bitcount += 7
76                location += 1
77                if byte > 0x7f and bitcount < 57:
78                        next = True
79                else:
80                        next = False
81        return temp, location
82
83'''Encodes an unsigned 64 bit multi-byte integer'''
84def enc_mbuint64(value):
85        if value > 0xffffffffffffff:
86                return None
87        new = True
88        cont = False
89        num = 0
90        a = array('B')
91        while new or cont:
92                for i in range(7):
93                        bit = value % 2
94                        value = int(value / 2)
95                        if bit and i < 7:
96                                num += pow(2,i)
97                        if value > 0:
98                                cont = True
99                        else:
100                                cont = False
101                if new:
102                        new = False
103                else:
104                        num += pow(2,7)
105                a.insert(0,num)
106                print hex(num)
107                num = 0
108        return a
109
110'''Decodes packet data from an array of values'''
111def dec_packet(values):
112        unknown = values[0]
113        tags = values[1]
114        if tags > 0x7f:
115                compression = 'HUFFMAN'
116        else:
117                compression = 'GORILLA'
118        index = tags & 0x1f
119        print '\tTags', hex(tags), 'Unknown', int(unknown), 'Compression =', compression, 'Index', index, '\n\tData:'
120        for i in values:
121                print '\t\t', hex(i)
122        return None, None
123
124
125