forked from cofford/PacketDecoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharstuffer.py
More file actions
43 lines (29 loc) · 925 Bytes
/
Copy pathcharstuffer.py
File metadata and controls
43 lines (29 loc) · 925 Bytes
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
#stuffs and unstuffs messages for HXr
import crcmod
import binascii
import struct
flagbyte = '7e'
stuffedflag = '7d5e'
escbyte = '7d'
stuffedbytes = '7d5d'
def unstuff(payload):
#remove flags and unstuff payload
x = payload[2:-2].replace (stuffedflag, flagbyte)
y = x.replace (stuffedbytes, escbyte)
return y
def stuff(payload):
#define checksum function
crc_func = crcmod.predefined.mkCrcFun('x-25')
#calculate checksum
crc = crc_func(binascii.unhexlify(payload))
#swap checksum bits
checksum = hex(struct.unpack("<H", struct.pack(">H", crc))[0])[2:]
payload = (payload + checksum)
#stuff characters
x = payload.replace (escbyte, stuffedbytes)
y = x.replace (flagbyte, stuffedflag)
length = len(y)%2
if length != 0:
y='00'
return (flagbyte + y + flagbyte)
#bytedata=bytedata.replace(b'\x00', b'\x00\x00')