Skip to content

Instantly share code, notes, and snippets.

@riking
Created March 18, 2016 20:12
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 riking/cd875948f21ae508271b to your computer and use it in GitHub Desktop.
Save riking/cd875948f21ae508271b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
import fileinput
import base64
import ast
rgx = re.compile("\"(\w+)\":")
compact = re.compile("^([A-Za-z0-9_=-]*)\\.[A-Za-z0-9_-]*=?=?.[A-Za-z0-9_-]*=?=?$")
def processjson(line):
found = set()
foundany = False
for m in re.finditer(rgx, line):
foundany = True
s = m.group(1)
if s.lower() != s:
print("uppercase %s: %s" % (s, line))
if s in found:
print("duplicate %s: %s" % (s, line))
found.add(s)
return foundany
for line in fileinput.input():
line = ast.literal_eval(line)
foundany = False
m = compact.match(line)
if m:
b64 = m.group(1)
if len(b64) % 4 != 0:
b64 = b64 + "="
if len(b64) % 4 != 0:
b64 = b64 + "="
#print(line)
#print(b64)
hdr = base64.b64decode(b64)
#print(hdr)
foundany = processjson(hdr)
if not foundany:
print("no matches: %s" % hdr)
else:
foundany = processjson(line)
if not foundany:
print("no matches: %s" % line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment