Skip to content

Instantly share code, notes, and snippets.

@brant-ruan
Created October 28, 2023 13:29
Show Gist options
  • Save brant-ruan/e5fc4ae634caf0fd41e38d5816d2c1e0 to your computer and use it in GitHub Desktop.
Save brant-ruan/e5fc4ae634caf0fd41e38d5816d2c1e0 to your computer and use it in GitHub Desktop.
The Fuzzing Book - cgi_decode.py
def cgi_decode(s: str) -> str:
"""Decode the CGI-encoded string `s`:
* replace '+' by ' '
* replace "%xx" by the character with hex number xx.
Return the decoded string. Raise `ValueError` for invalid inputs."""
# Mapping of hex digits to their integer values
hex_values = {
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
'5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15,
'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15,
}
t = ""
i = 0
while i < len(s):
c = s[i]
if c == '+':
t += ' '
elif c == '%':
digit_high, digit_low = s[i + 1], s[i + 2]
i += 2
if digit_high in hex_values and digit_low in hex_values:
v = hex_values[digit_high] * 16 + hex_values[digit_low]
t += chr(v)
else:
raise ValueError("Invalid encoding")
else:
t += c
i += 1
return t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment