Skip to content

Instantly share code, notes, and snippets.

@keichi
Created June 9, 2023 06:38
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 keichi/bae95d1fd54dfcbb74eb6cb3ed0abf24 to your computer and use it in GitHub Desktop.
Save keichi/bae95d1fd54dfcbb74eb6cb3ed0abf24 to your computer and use it in GitHub Desktop.
import json
import sys
BASE64_ALPHABET = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
"0123456789+/")
BASE64_TABLE = dict(zip(BASE64_ALPHABET, range(64)))
def decode_vlq(s):
octets = [BASE64_TABLE[c] for c in s]
i = 0
res = []
while i < len(octets):
cur = 0
cont = True
shift = 0
while cont:
cont = octets[i] & 0x20
val = octets[i] & 0x1f
cur |= val << shift
i += 1
shift += 5
neg = cur & 1
cur >>= 1
if neg:
res.append(-cur)
else:
res.append(cur)
print(s, octets, res)
return res
def main():
with open(sys.argv[1]) as f:
source_map = json.load(f)
mappings = source_map["mappings"]
sources = source_map["sources"]
mapping = [0] * 5
for line, line_mapping in enumerate(mappings.split(";")):
mapping[0] = 0
for col_mapping in line_mapping.split(","):
res = decode_vlq(col_mapping)
res += [0] * (5 - len(res))
mapping = [x + y for x, y in zip(mapping, res)]
print(f"{line}:{mapping[0]} -> "
f"{sources[mapping[1]]}:{mapping[2]}:{mapping[3]}")
print("-----------------------")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment