Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ded2844

Browse files
committed
minor fixes and refactoring regarding safecharencoding
1 parent 866cdb4 commit ded2844

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

lib/core/convert.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import md5
1414
import sha
1515

16+
import binascii
1617
import pickle
1718
import re
1819
import sys
@@ -23,6 +24,7 @@
2324
from lib.core.data import conf
2425
from lib.core.data import logger
2526
from lib.core.settings import HEX_ENCODED_CHAR_REGEX
27+
from lib.core.settings import SAFE_ENCODE_SLASH_REPLACEMENTS
2628
from lib.core.settings import UNICODE_ENCODING
2729
from lib.core.settings import URLENCODE_CHAR_LIMIT
2830
from lib.core.settings import URLENCODE_FAILSAFE_CHARS
@@ -148,10 +150,11 @@ def safecharencode(value):
148150
retVal = value
149151

150152
if isinstance(value, basestring):
151-
retVal = reduce(lambda x, y: x + (y if (y in string.printable or ord(y) > 255) else '\%02x' % ord(y)), value, unicode())
152-
for char in "\t\n\r\x0b\x0c":
153+
for char in SAFE_ENCODE_SLASH_REPLACEMENTS:
153154
retVal = retVal.replace(char, repr(char).strip('\''))
154155

156+
retVal = reduce(lambda x, y: x + (y if (y in string.printable or ord(y) > 255) else '\%02x' % ord(y)), retVal, unicode())
157+
155158
elif isinstance(value, list):
156159
for i in xrange(len(value)):
157160
retVal[i] = safecharencode(value[i])
@@ -165,18 +168,18 @@ def safechardecode(value):
165168

166169
retVal = value
167170
if isinstance(value, basestring):
168-
for char in "\t\n\r\x0b\x0c":
169-
retVal = retVal.replace(repr(char).strip('\''), char)
170-
171171
regex = re.compile(HEX_ENCODED_CHAR_REGEX)
172172

173173
while True:
174174
match = regex.search(retVal)
175175
if match:
176-
retVal = retVal.replace(match.group("result"), unhexlify(value.lstrip('\\')))
176+
retVal = retVal.replace(match.group("result"), binascii.unhexlify(match.group("result").lstrip('\\')))
177177
else:
178178
break
179179

180+
for char in SAFE_ENCODE_SLASH_REPLACEMENTS[::-1]:
181+
retVal = retVal.replace(repr(char).strip('\''), char)
182+
180183
elif isinstance(value, (list, tuple)):
181184
for i in xrange(len(value)):
182185
retVal[i] = safechardecode(value[i])

lib/core/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,6 @@
307307

308308
# Regex used for recognition of hex encoded characters
309309
HEX_ENCODED_CHAR_REGEX = r"(?P<result>\\[0-9A-Fa-f]{2})"
310+
311+
# Raw chars that will be safe encoded to their slash (\) representations (e.g. newline to \n)
312+
SAFE_ENCODE_SLASH_REPLACEMENTS = "\\\t\n\r\x0b\x0c"

0 commit comments

Comments
 (0)