1
0
mirror of https://codeberg.org/polarisfm/youtube-dl synced 2024-12-02 05:07:55 +01:00

[youtube_dl/utils] add support for octal escape sequences to js_to_json()

This commit is contained in:
uno20001 2019-11-02 16:25:27 +01:00
parent 4c95fcf9e8
commit 300a9da662

View File

@ -3979,18 +3979,27 @@ def js_to_json(code):
def fix_kv(m): def fix_kv(m):
v = m.group(0) v = m.group(0)
if v in ('true', 'false', 'null'): if v in ('true', 'false', 'null'):
return v return v
elif v.startswith('/*') or v.startswith('//') or v == ',': elif v.startswith('/*') or v.startswith('//') or v == ',':
return "" return ""
if v[0] in ("'", '"'): if v[0] in ("'", '"'):
v = v[1:-1]
# convert Javascript's octal escape sequences (and '\0')
# into valid JSON escape sequences ( e.g '\347' => '\u00e7', '\0' => '\u0000' )
# regex based on https://mathiasbynens.be/notes/javascript-escapes
v = re.sub(r'\\([0-7]{1,3})', lambda x: "\\u%04x" % int(x.group(1), 8), v)
v = re.sub(r'(?s)\\.|"', lambda m: { v = re.sub(r'(?s)\\.|"', lambda m: {
'"': '\\"', '"': '\\"',
"\\'": "'", "\\'": "'",
'\\\n': '', '\\\n': '',
'\\x': '\\u00', '\\x': '\\u00',
}.get(m.group(0), m.group(0)), v[1:-1]) }.get(m.group(0), m.group(0)), v)
for regex, base in INTEGER_TABLE: for regex, base in INTEGER_TABLE:
im = re.match(regex, v) im = re.match(regex, v)
@ -4001,8 +4010,8 @@ def js_to_json(code):
return '"%s"' % v return '"%s"' % v
return re.sub(r'''(?sx) return re.sub(r'''(?sx)
"(?:[^"\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^"\\]*"| "(?:[^"\\]*(?:\\\\|\\['"nurtbfx/\n]?))*[^"\\]*"|
'(?:[^'\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^'\\]*'| '(?:[^'\\]*(?:\\\\|\\['"nurtbfx/\n]?))*[^'\\]*'|
{comment}|,(?={skip}[\]}}])| {comment}|,(?={skip}[\]}}])|
(?:(?<![0-9])[eE]|[a-df-zA-DF-Z_])[.a-zA-Z_0-9]*| (?:(?<![0-9])[eE]|[a-df-zA-DF-Z_])[.a-zA-Z_0-9]*|
\b(?:0[xX][0-9a-fA-F]+|0+[0-7]+)(?:{skip}:)?| \b(?:0[xX][0-9a-fA-F]+|0+[0-7]+)(?:{skip}:)?|