Skip to content

Instantly share code, notes, and snippets.

@ThomasHabets
Created November 18, 2023 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasHabets/8a546a56720dc820f654bec5ca850e26 to your computer and use it in GitHub Desktop.
Save ThomasHabets/8a546a56720dc820f654bec5ca850e26 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
"""
Mostly written by GPT-4 using prompt:
'Write python script to HDLC escape a packet'
"""
def hdlc_escape(packet):
FLAG_BYTE = 0x7E
ESCAPE_BYTE = 0x7D
XOR_BYTE = 0x20
escaped_packet = bytearray()
for byte in packet:
if byte == FLAG_BYTE or byte == ESCAPE_BYTE:
escaped_packet.append(ESCAPE_BYTE)
escaped_packet.append(byte ^ XOR_BYTE)
else:
escaped_packet.append(byte)
return escaped_packet
if False:
# Example usage
original_packet = bytearray([0x7E, 0x11, 0x7D, 0x7E, 0x22, 0x33])
escaped_packet = hdlc_escape(original_packet)
print("Original Packet:", original_packet)
print("Escaped Packet: ", escaped_packet)
open("/dev/stdout", "wb").write(bytes(
bytearray([0xC0, 0])
+ hdlc_escape(open("/dev/stdin", 'rb').read())
+ bytearray([0xC0])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment