Skip to content

Instantly share code, notes, and snippets.

@smarek
Forked from ryardley/plant_uml_decoder.py
Created August 27, 2023 08:44
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 smarek/2aff30b9e5c96315cecbf1f07832463f to your computer and use it in GitHub Desktop.
Save smarek/2aff30b9e5c96315cecbf1f07832463f to your computer and use it in GitHub Desktop.
PlantUML url encryption decoder
#!/usr/bin/env python3
import zlib
import base64
import string
import sys
plantuml_alphabet = (
string.digits + string.ascii_uppercase + string.ascii_lowercase + "-_"
)
base64_alphabet = string.ascii_uppercase + string.ascii_lowercase + string.digits + "+/"
b64_to_plantuml = bytes.maketrans(
base64_alphabet.encode("utf-8"), plantuml_alphabet.encode("utf-8")
)
plantuml_to_b64 = bytes.maketrans(
plantuml_alphabet.encode("utf-8"), base64_alphabet.encode("utf-8")
)
def plantuml_encode(plantuml_text):
"""zlib compress the plantuml text and encode it for the plantuml server"""
zlibbed_str = zlib.compress(plantuml_text.encode("utf-8"))
compressed_string = zlibbed_str[2:-4]
return (
base64.b64encode(compressed_string).translate(b64_to_plantuml).decode("utf-8")
)
def plantuml_decode(plantuml_url):
"""decode plantuml encoded url back to plantuml text"""
data = base64.b64decode(plantuml_url.translate(plantuml_to_b64).encode("utf-8"))
dec = zlib.decompressobj() # without check the crc.
header = b"x\x9c"
return dec.decompress(header + data).decode("utf-8")
if __name__ == "__main__":
print(plantuml_decode(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment