This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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