2019-05-08 13:47:52 +03:00
|
|
|
#!/usr/bin/env python
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
"""
|
2020-12-31 13:46:27 +03:00
|
|
|
Copyright (c) 2006-2021 sqlmap developers (http://sqlmap.org/)
|
2017-10-11 15:50:46 +03:00
|
|
|
See the file 'LICENSE' for copying permission
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
|
2016-10-26 23:33:04 +03:00
|
|
|
try:
|
2016-10-29 01:13:04 +03:00
|
|
|
import cPickle as pickle
|
2016-10-26 23:33:04 +03:00
|
|
|
except:
|
2016-10-29 01:13:04 +03:00
|
|
|
import pickle
|
2016-10-26 23:33:04 +03:00
|
|
|
|
2015-01-15 19:32:07 +03:00
|
|
|
import base64
|
2019-05-08 13:28:50 +03:00
|
|
|
import binascii
|
2019-05-03 14:20:15 +03:00
|
|
|
import codecs
|
2013-01-10 02:08:50 +04:00
|
|
|
import json
|
2016-01-08 13:47:12 +03:00
|
|
|
import re
|
2010-02-23 11:54:33 +03:00
|
|
|
import sys
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2019-06-26 12:02:43 +03:00
|
|
|
from lib.core.bigarray import BigArray
|
2019-12-09 13:35:22 +03:00
|
|
|
from lib.core.compat import xrange
|
2019-05-06 01:54:21 +03:00
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
2019-05-03 14:20:15 +03:00
|
|
|
from lib.core.settings import INVALID_UNICODE_PRIVATE_AREA
|
2019-05-21 15:37:55 +03:00
|
|
|
from lib.core.settings import IS_TTY
|
2012-07-31 13:03:44 +04:00
|
|
|
from lib.core.settings import IS_WIN
|
2019-05-06 01:54:21 +03:00
|
|
|
from lib.core.settings import NULL
|
2019-05-02 12:26:31 +03:00
|
|
|
from lib.core.settings import PICKLE_PROTOCOL
|
2019-05-03 14:20:15 +03:00
|
|
|
from lib.core.settings import SAFE_HEX_MARKER
|
2011-03-03 13:39:04 +03:00
|
|
|
from lib.core.settings import UNICODE_ENCODING
|
2019-03-28 15:53:54 +03:00
|
|
|
from thirdparty import six
|
2019-05-15 11:57:22 +03:00
|
|
|
from thirdparty.six import unichr as _unichr
|
2021-01-15 11:31:57 +03:00
|
|
|
from thirdparty.six.moves import collections_abc as _collections
|
2010-03-27 02:23:25 +03:00
|
|
|
|
2019-12-02 12:10:58 +03:00
|
|
|
try:
|
|
|
|
from html import escape as htmlEscape
|
|
|
|
except ImportError:
|
|
|
|
from cgi import escape as htmlEscape
|
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def base64pickle(value):
|
2013-03-11 17:58:05 +04:00
|
|
|
"""
|
|
|
|
Serializes (with pickle) and encodes to Base64 format supplied (binary) value
|
|
|
|
|
2019-05-02 17:54:54 +03:00
|
|
|
>>> base64unpickle(base64pickle([1, 2, 3])) == [1, 2, 3]
|
|
|
|
True
|
2013-03-11 17:58:05 +04:00
|
|
|
"""
|
|
|
|
|
2012-11-26 14:16:59 +04:00
|
|
|
retVal = None
|
2014-11-13 12:52:33 +03:00
|
|
|
|
2012-11-26 14:16:59 +04:00
|
|
|
try:
|
2020-09-09 17:15:23 +03:00
|
|
|
retVal = encodeBase64(pickle.dumps(value, PICKLE_PROTOCOL), binary=False)
|
2012-11-26 14:16:59 +04:00
|
|
|
except:
|
|
|
|
warnMsg = "problem occurred while serializing "
|
|
|
|
warnMsg += "instance of a type '%s'" % type(value)
|
|
|
|
singleTimeWarnMessage(warnMsg)
|
|
|
|
|
2014-05-29 11:33:14 +04:00
|
|
|
try:
|
2020-09-09 17:15:23 +03:00
|
|
|
retVal = encodeBase64(pickle.dumps(value), binary=False)
|
2014-05-29 11:33:14 +04:00
|
|
|
except:
|
2020-09-09 17:15:23 +03:00
|
|
|
retVal = encodeBase64(pickle.dumps(str(value), PICKLE_PROTOCOL), binary=False)
|
2014-05-29 11:33:14 +04:00
|
|
|
|
2012-11-26 14:16:59 +04:00
|
|
|
return retVal
|
2010-04-12 13:35:20 +04:00
|
|
|
|
2019-03-27 03:28:34 +03:00
|
|
|
def base64unpickle(value):
|
2013-03-11 17:58:05 +04:00
|
|
|
"""
|
2013-03-26 17:11:17 +04:00
|
|
|
Decodes value from Base64 to plain format and deserializes (with pickle) its content
|
2013-03-11 17:58:05 +04:00
|
|
|
|
2019-05-02 17:54:54 +03:00
|
|
|
>>> type(base64unpickle('gAJjX19idWlsdGluX18Kb2JqZWN0CnEBKYFxAi4=')) == object
|
|
|
|
True
|
2013-03-11 17:58:05 +04:00
|
|
|
"""
|
|
|
|
|
2014-11-13 12:52:33 +03:00
|
|
|
retVal = None
|
|
|
|
|
|
|
|
try:
|
2019-05-03 14:20:15 +03:00
|
|
|
retVal = pickle.loads(decodeBase64(value))
|
2018-03-13 15:45:42 +03:00
|
|
|
except TypeError:
|
2019-05-03 14:20:15 +03:00
|
|
|
retVal = pickle.loads(decodeBase64(bytes(value)))
|
2019-05-02 17:54:54 +03:00
|
|
|
|
|
|
|
return retVal
|
2010-10-01 12:03:39 +04:00
|
|
|
|
2019-05-20 12:24:43 +03:00
|
|
|
def htmlUnescape(value):
|
2013-03-11 17:58:05 +04:00
|
|
|
"""
|
|
|
|
Returns (basic conversion) HTML unescaped value
|
|
|
|
|
2019-06-01 00:32:28 +03:00
|
|
|
>>> htmlUnescape('a<b') == 'a<b'
|
|
|
|
True
|
2013-03-11 17:58:05 +04:00
|
|
|
"""
|
|
|
|
|
2011-12-21 18:25:39 +04:00
|
|
|
retVal = value
|
2019-05-29 00:44:27 +03:00
|
|
|
|
2019-03-28 15:53:54 +03:00
|
|
|
if value and isinstance(value, six.string_types):
|
2019-04-30 14:20:31 +03:00
|
|
|
replacements = (("<", '<'), (">", '>'), (""", '"'), (" ", ' '), ("&", '&'), ("'", "'"))
|
|
|
|
for code, value in replacements:
|
|
|
|
retVal = retVal.replace(code, value)
|
|
|
|
|
2016-01-09 01:10:32 +03:00
|
|
|
try:
|
2019-06-01 00:32:28 +03:00
|
|
|
retVal = re.sub(r"&#x([^ ;]+);", lambda match: _unichr(int(match.group(1), 16)), retVal)
|
2020-03-09 12:30:24 +03:00
|
|
|
except (ValueError, OverflowError):
|
2016-01-09 01:10:32 +03:00
|
|
|
pass
|
2019-05-29 00:44:27 +03:00
|
|
|
|
2011-04-15 01:36:13 +04:00
|
|
|
return retVal
|
2012-07-31 13:03:44 +04:00
|
|
|
|
2018-03-21 16:29:54 +03:00
|
|
|
def singleTimeWarnMessage(message): # Cross-referenced function
|
2014-11-30 01:33:24 +03:00
|
|
|
sys.stdout.write(message)
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
sys.stdout.flush()
|
2012-07-31 13:03:44 +04:00
|
|
|
|
2019-05-06 01:54:21 +03:00
|
|
|
def filterNone(values): # Cross-referenced function
|
2021-01-15 11:31:57 +03:00
|
|
|
return [_ for _ in values if _] if isinstance(values, _collections.Iterable) else values
|
2019-05-06 01:54:21 +03:00
|
|
|
|
|
|
|
def isListLike(value): # Cross-referenced function
|
2019-06-26 12:02:43 +03:00
|
|
|
return isinstance(value, (list, tuple, set, BigArray))
|
2019-05-06 01:54:21 +03:00
|
|
|
|
2019-05-21 15:18:14 +03:00
|
|
|
def shellExec(cmd): # Cross-referenced function
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2013-01-10 02:08:50 +04:00
|
|
|
def jsonize(data):
|
2013-03-11 17:58:05 +04:00
|
|
|
"""
|
|
|
|
Returns JSON serialized data
|
|
|
|
|
|
|
|
>>> jsonize({'foo':'bar'})
|
|
|
|
'{\\n "foo": "bar"\\n}'
|
|
|
|
"""
|
|
|
|
|
2013-01-10 02:08:50 +04:00
|
|
|
return json.dumps(data, sort_keys=False, indent=4)
|
2013-01-29 05:39:27 +04:00
|
|
|
|
|
|
|
def dejsonize(data):
|
2013-03-11 17:58:05 +04:00
|
|
|
"""
|
|
|
|
Returns JSON deserialized data
|
|
|
|
|
2019-05-02 17:54:54 +03:00
|
|
|
>>> dejsonize('{\\n "foo": "bar"\\n}') == {u'foo': u'bar'}
|
|
|
|
True
|
2013-03-11 17:58:05 +04:00
|
|
|
"""
|
|
|
|
|
2013-01-29 05:39:27 +04:00
|
|
|
return json.loads(data)
|
2019-05-03 14:20:15 +03:00
|
|
|
|
|
|
|
def decodeHex(value, binary=True):
|
|
|
|
"""
|
|
|
|
Returns a decoded representation of provided hexadecimal value
|
|
|
|
|
|
|
|
>>> decodeHex("313233") == b"123"
|
|
|
|
True
|
|
|
|
>>> decodeHex("313233", binary=False) == u"123"
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
|
|
|
|
retVal = value
|
|
|
|
|
|
|
|
if isinstance(value, six.binary_type):
|
2019-05-06 01:54:21 +03:00
|
|
|
value = getText(value)
|
2019-05-03 14:20:15 +03:00
|
|
|
|
|
|
|
if value.lower().startswith("0x"):
|
|
|
|
value = value[2:]
|
|
|
|
|
2019-05-08 13:28:50 +03:00
|
|
|
try:
|
|
|
|
retVal = codecs.decode(value, "hex")
|
|
|
|
except LookupError:
|
|
|
|
retVal = binascii.unhexlify(value)
|
2019-05-03 14:20:15 +03:00
|
|
|
|
|
|
|
if not binary:
|
|
|
|
retVal = getText(retVal)
|
|
|
|
|
|
|
|
return retVal
|
|
|
|
|
|
|
|
def encodeHex(value, binary=True):
|
|
|
|
"""
|
|
|
|
Returns a encoded representation of provided string value
|
|
|
|
|
|
|
|
>>> encodeHex(b"123") == b"313233"
|
|
|
|
True
|
|
|
|
>>> encodeHex("123", binary=False)
|
|
|
|
'313233'
|
2019-07-04 12:18:55 +03:00
|
|
|
>>> encodeHex(b"123"[0]) == b"31"
|
|
|
|
True
|
2019-05-03 14:20:15 +03:00
|
|
|
"""
|
|
|
|
|
2019-07-04 12:18:55 +03:00
|
|
|
if isinstance(value, int):
|
|
|
|
value = six.unichr(value)
|
|
|
|
|
2019-05-03 14:20:15 +03:00
|
|
|
if isinstance(value, six.text_type):
|
|
|
|
value = value.encode(UNICODE_ENCODING)
|
|
|
|
|
2019-05-08 13:28:50 +03:00
|
|
|
try:
|
|
|
|
retVal = codecs.encode(value, "hex")
|
|
|
|
except LookupError:
|
|
|
|
retVal = binascii.hexlify(value)
|
2019-05-03 14:20:15 +03:00
|
|
|
|
|
|
|
if not binary:
|
|
|
|
retVal = getText(retVal)
|
|
|
|
|
|
|
|
return retVal
|
|
|
|
|
2019-09-16 20:29:38 +03:00
|
|
|
def decodeBase64(value, binary=True, encoding=None):
|
2019-05-03 14:20:15 +03:00
|
|
|
"""
|
|
|
|
Returns a decoded representation of provided Base64 value
|
|
|
|
|
|
|
|
>>> decodeBase64("MTIz") == b"123"
|
|
|
|
True
|
|
|
|
>>> decodeBase64("MTIz", binary=False)
|
|
|
|
'123'
|
2020-08-10 23:26:03 +03:00
|
|
|
>>> decodeBase64("A-B_CDE") == decodeBase64("A+B/CDE")
|
2020-08-10 22:54:58 +03:00
|
|
|
True
|
2020-07-01 12:56:24 +03:00
|
|
|
>>> decodeBase64(b"MTIzNA") == b"1234"
|
|
|
|
True
|
|
|
|
>>> decodeBase64("MTIzNA") == b"1234"
|
|
|
|
True
|
|
|
|
>>> decodeBase64("MTIzNA==") == b"1234"
|
|
|
|
True
|
2019-05-03 14:20:15 +03:00
|
|
|
"""
|
|
|
|
|
2020-08-10 22:54:58 +03:00
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
2020-07-01 12:56:24 +03:00
|
|
|
padding = b'=' if isinstance(value, bytes) else '='
|
|
|
|
|
|
|
|
# Reference: https://stackoverflow.com/a/49459036
|
|
|
|
if not value.endswith(padding):
|
|
|
|
value += 3 * padding
|
|
|
|
|
2020-08-10 22:54:58 +03:00
|
|
|
# Reference: https://en.wikipedia.org/wiki/Base64#URL_applications
|
|
|
|
# Reference: https://perldoc.perl.org/MIME/Base64.html
|
|
|
|
if isinstance(value, bytes):
|
|
|
|
value = value.replace(b'-', b'+').replace(b'_', b'/')
|
|
|
|
else:
|
|
|
|
value = value.replace('-', '+').replace('_', '/')
|
|
|
|
|
2019-05-03 14:20:15 +03:00
|
|
|
retVal = base64.b64decode(value)
|
|
|
|
|
|
|
|
if not binary:
|
2019-09-16 20:29:38 +03:00
|
|
|
retVal = getText(retVal, encoding)
|
2019-05-03 14:20:15 +03:00
|
|
|
|
|
|
|
return retVal
|
|
|
|
|
2020-08-10 23:26:03 +03:00
|
|
|
def encodeBase64(value, binary=True, encoding=None, padding=True, safe=False):
|
2019-05-03 14:20:15 +03:00
|
|
|
"""
|
|
|
|
Returns a decoded representation of provided Base64 value
|
|
|
|
|
|
|
|
>>> encodeBase64(b"123") == b"MTIz"
|
|
|
|
True
|
2020-08-10 22:54:58 +03:00
|
|
|
>>> encodeBase64(u"1234", binary=False)
|
|
|
|
'MTIzNA=='
|
|
|
|
>>> encodeBase64(u"1234", binary=False, padding=False)
|
|
|
|
'MTIzNA'
|
2020-08-10 23:26:03 +03:00
|
|
|
>>> encodeBase64(decodeBase64("A-B_CDE"), binary=False, safe=True)
|
|
|
|
'A-B_CDE'
|
2019-05-03 14:20:15 +03:00
|
|
|
"""
|
|
|
|
|
2020-08-10 22:54:58 +03:00
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
2019-05-03 14:20:15 +03:00
|
|
|
if isinstance(value, six.text_type):
|
2019-09-16 20:29:38 +03:00
|
|
|
value = value.encode(encoding or UNICODE_ENCODING)
|
2019-05-03 14:20:15 +03:00
|
|
|
|
|
|
|
retVal = base64.b64encode(value)
|
|
|
|
|
|
|
|
if not binary:
|
2019-09-16 20:29:38 +03:00
|
|
|
retVal = getText(retVal, encoding)
|
2019-05-03 14:20:15 +03:00
|
|
|
|
2020-08-10 23:26:03 +03:00
|
|
|
if safe:
|
|
|
|
padding = False
|
|
|
|
|
|
|
|
# Reference: https://en.wikipedia.org/wiki/Base64#URL_applications
|
|
|
|
# Reference: https://perldoc.perl.org/MIME/Base64.html
|
|
|
|
if isinstance(retVal, bytes):
|
|
|
|
retVal = retVal.replace(b'+', b'-').replace(b'/', b'_')
|
|
|
|
else:
|
|
|
|
retVal = retVal.replace('+', '-').replace('/', '_')
|
|
|
|
|
2020-08-10 22:54:58 +03:00
|
|
|
if not padding:
|
|
|
|
retVal = retVal.rstrip(b'=' if isinstance(retVal, bytes) else '=')
|
|
|
|
|
2019-05-03 14:20:15 +03:00
|
|
|
return retVal
|
|
|
|
|
2020-01-09 13:59:50 +03:00
|
|
|
def getBytes(value, encoding=None, errors="strict", unsafe=True):
|
2019-05-03 14:20:15 +03:00
|
|
|
"""
|
|
|
|
Returns byte representation of provided Unicode value
|
|
|
|
|
|
|
|
>>> getBytes(u"foo\\\\x01\\\\x83\\\\xffbar") == b"foo\\x01\\x83\\xffbar"
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
|
|
|
|
retVal = value
|
|
|
|
|
2020-01-09 13:59:50 +03:00
|
|
|
if encoding is None:
|
2020-01-16 00:47:06 +03:00
|
|
|
encoding = conf.get("encoding") or UNICODE_ENCODING
|
2020-01-09 13:59:50 +03:00
|
|
|
|
2019-12-01 01:10:20 +03:00
|
|
|
try:
|
|
|
|
codecs.lookup(encoding)
|
2020-01-09 13:59:50 +03:00
|
|
|
except (LookupError, TypeError):
|
2019-12-01 01:10:20 +03:00
|
|
|
encoding = UNICODE_ENCODING
|
|
|
|
|
2019-05-03 14:20:15 +03:00
|
|
|
if isinstance(value, six.text_type):
|
|
|
|
if INVALID_UNICODE_PRIVATE_AREA:
|
2019-05-07 17:43:01 +03:00
|
|
|
if unsafe:
|
|
|
|
for char in xrange(0xF0000, 0xF00FF + 1):
|
2019-05-15 11:57:22 +03:00
|
|
|
value = value.replace(_unichr(char), "%s%02x" % (SAFE_HEX_MARKER, char - 0xF0000))
|
2019-05-03 14:20:15 +03:00
|
|
|
|
|
|
|
retVal = value.encode(encoding, errors)
|
2019-05-07 17:43:01 +03:00
|
|
|
|
|
|
|
if unsafe:
|
|
|
|
retVal = re.sub(r"%s([0-9a-f]{2})" % SAFE_HEX_MARKER, lambda _: decodeHex(_.group(1)), retVal)
|
2019-05-03 14:20:15 +03:00
|
|
|
else:
|
2020-05-27 16:39:03 +03:00
|
|
|
try:
|
|
|
|
retVal = value.encode(encoding, errors)
|
|
|
|
except UnicodeError:
|
|
|
|
retVal = value.encode(UNICODE_ENCODING, errors="replace")
|
2019-05-07 17:43:01 +03:00
|
|
|
|
|
|
|
if unsafe:
|
|
|
|
retVal = re.sub(b"\\\\x([0-9a-f]{2})", lambda _: decodeHex(_.group(1)), retVal)
|
2019-05-03 14:20:15 +03:00
|
|
|
|
|
|
|
return retVal
|
|
|
|
|
|
|
|
def getOrds(value):
|
|
|
|
"""
|
|
|
|
Returns ORD(...) representation of provided string value
|
|
|
|
|
|
|
|
>>> getOrds(u'fo\\xf6bar')
|
|
|
|
[102, 111, 246, 98, 97, 114]
|
|
|
|
>>> getOrds(b"fo\\xc3\\xb6bar")
|
|
|
|
[102, 111, 195, 182, 98, 97, 114]
|
|
|
|
"""
|
|
|
|
|
|
|
|
return [_ if isinstance(_, int) else ord(_) for _ in value]
|
|
|
|
|
2019-05-06 01:54:21 +03:00
|
|
|
def getUnicode(value, encoding=None, noneToNull=False):
|
|
|
|
"""
|
2019-10-29 17:00:53 +03:00
|
|
|
Returns the unicode representation of the supplied value
|
2019-05-06 01:54:21 +03:00
|
|
|
|
|
|
|
>>> getUnicode('test') == u'test'
|
|
|
|
True
|
|
|
|
>>> getUnicode(1) == u'1'
|
|
|
|
True
|
2021-01-05 16:50:54 +03:00
|
|
|
>>> getUnicode(None) == 'None'
|
|
|
|
True
|
2019-05-06 01:54:21 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
if noneToNull and value is None:
|
|
|
|
return NULL
|
|
|
|
|
|
|
|
if isinstance(value, six.text_type):
|
|
|
|
return value
|
|
|
|
elif isinstance(value, six.binary_type):
|
|
|
|
# Heuristics (if encoding not explicitly specified)
|
|
|
|
candidates = filterNone((encoding, kb.get("pageEncoding") if kb.get("originalPage") else None, conf.get("encoding"), UNICODE_ENCODING, sys.getfilesystemencoding()))
|
|
|
|
if all(_ in value for _ in (b'<', b'>')):
|
|
|
|
pass
|
|
|
|
elif any(_ in value for _ in (b":\\", b'/', b'.')) and b'\n' not in value:
|
|
|
|
candidates = filterNone((encoding, sys.getfilesystemencoding(), kb.get("pageEncoding") if kb.get("originalPage") else None, UNICODE_ENCODING, conf.get("encoding")))
|
|
|
|
elif conf.get("encoding") and b'\n' not in value:
|
|
|
|
candidates = filterNone((encoding, conf.get("encoding"), kb.get("pageEncoding") if kb.get("originalPage") else None, sys.getfilesystemencoding(), UNICODE_ENCODING))
|
|
|
|
|
|
|
|
for candidate in candidates:
|
|
|
|
try:
|
|
|
|
return six.text_type(value, candidate)
|
2020-01-08 02:11:13 +03:00
|
|
|
except (UnicodeDecodeError, LookupError):
|
2019-05-06 01:54:21 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
return six.text_type(value, encoding or (kb.get("pageEncoding") if kb.get("originalPage") else None) or UNICODE_ENCODING)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
return six.text_type(value, UNICODE_ENCODING, errors="reversible")
|
|
|
|
elif isListLike(value):
|
|
|
|
value = list(getUnicode(_, encoding, noneToNull) for _ in value)
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
return six.text_type(value)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
return six.text_type(str(value), errors="ignore") # encoding ignored for non-basestring instances
|
|
|
|
|
2019-09-16 20:29:38 +03:00
|
|
|
def getText(value, encoding=None):
|
2019-05-03 14:20:15 +03:00
|
|
|
"""
|
|
|
|
Returns textual value of a given value (Note: not necessary Unicode on Python2)
|
|
|
|
|
|
|
|
>>> getText(b"foobar")
|
|
|
|
'foobar'
|
|
|
|
>>> isinstance(getText(u"fo\\u2299bar"), six.text_type)
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
|
|
|
|
retVal = value
|
|
|
|
|
|
|
|
if isinstance(value, six.binary_type):
|
2019-09-16 20:29:38 +03:00
|
|
|
retVal = getUnicode(value, encoding)
|
2019-05-03 14:20:15 +03:00
|
|
|
|
|
|
|
if six.PY2:
|
|
|
|
try:
|
|
|
|
retVal = str(retVal)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return retVal
|
2019-05-25 01:22:27 +03:00
|
|
|
|
|
|
|
def stdoutEncode(value):
|
|
|
|
"""
|
|
|
|
Returns binary representation of a given Unicode value safe for writing to stdout
|
|
|
|
"""
|
|
|
|
|
|
|
|
value = value or ""
|
|
|
|
|
|
|
|
if IS_WIN and IS_TTY and kb.get("codePage", -1) is None:
|
|
|
|
output = shellExec("chcp")
|
|
|
|
match = re.search(r": (\d{3,})", output or "")
|
|
|
|
|
|
|
|
if match:
|
|
|
|
try:
|
|
|
|
candidate = "cp%s" % match.group(1)
|
|
|
|
codecs.lookup(candidate)
|
|
|
|
except LookupError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
kb.codePage = candidate
|
|
|
|
|
|
|
|
kb.codePage = kb.codePage or ""
|
|
|
|
|
|
|
|
if isinstance(value, six.text_type):
|
2019-06-05 15:23:30 +03:00
|
|
|
encoding = kb.get("codePage") or getattr(sys.stdout, "encoding", None) or UNICODE_ENCODING
|
2019-05-25 01:22:27 +03:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
retVal = value.encode(encoding)
|
|
|
|
break
|
|
|
|
except UnicodeEncodeError as ex:
|
|
|
|
value = value[:ex.start] + "?" * (ex.end - ex.start) + value[ex.end:]
|
|
|
|
|
|
|
|
warnMsg = "cannot properly display (some) Unicode characters "
|
|
|
|
warnMsg += "inside your terminal ('%s') environment. All " % encoding
|
|
|
|
warnMsg += "unhandled occurrences will result in "
|
|
|
|
warnMsg += "replacement with '?' character. Please, find "
|
|
|
|
warnMsg += "proper character representation inside "
|
|
|
|
warnMsg += "corresponding output files"
|
|
|
|
singleTimeWarnMessage(warnMsg)
|
|
|
|
|
|
|
|
if six.PY3:
|
|
|
|
retVal = getUnicode(retVal, encoding)
|
|
|
|
|
|
|
|
else:
|
|
|
|
retVal = value
|
|
|
|
|
|
|
|
return retVal
|
2019-10-29 17:00:53 +03:00
|
|
|
|
|
|
|
def getConsoleLength(value):
|
|
|
|
"""
|
|
|
|
Returns console width of unicode values
|
|
|
|
|
|
|
|
>>> getConsoleLength("abc")
|
|
|
|
3
|
|
|
|
>>> getConsoleLength(u"\\u957f\\u6c5f")
|
|
|
|
4
|
|
|
|
"""
|
|
|
|
|
|
|
|
if isinstance(value, six.text_type):
|
|
|
|
retVal = sum((2 if ord(_) >= 0x3000 else 1) for _ in value)
|
|
|
|
else:
|
|
|
|
retVal = len(value)
|
|
|
|
|
2019-12-01 01:10:20 +03:00
|
|
|
return retVal
|