/sqlmaparch/lib/core/convert.py

https://bitbucket.org/acaceres/mr-injector · Python · 182 lines · 167 code · 3 blank · 12 comment · 1 complexity · ffa1b3d9697c84d163a38e8a6fb5a23e MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. Copyright (c) 2006-2013 sqlmap developers (http://sqlmap.org/)
  4. See the file 'doc/COPYING' for copying permission
  5. """
  6. import json
  7. import pickle
  8. import sys
  9. from lib.core.settings import IS_WIN
  10. from lib.core.settings import UNICODE_ENCODING
  11. def base64decode(value):
  12. """
  13. Decodes string value from Base64 to plain format
  14. >>> base64decode('Zm9vYmFy')
  15. 'foobar'
  16. """
  17. return value.decode("base64")
  18. def base64encode(value):
  19. """
  20. Encodes string value from plain to Base64 format
  21. >>> base64encode('foobar')
  22. 'Zm9vYmFy'
  23. """
  24. return value.encode("base64")[:-1].replace("\n", "")
  25. def base64pickle(value):
  26. """
  27. Serializes (with pickle) and encodes to Base64 format supplied (binary) value
  28. >>> base64pickle('foobar')
  29. 'gAJVBmZvb2JhcnEALg=='
  30. """
  31. retVal = None
  32. try:
  33. retVal = base64encode(pickle.dumps(value, pickle.HIGHEST_PROTOCOL))
  34. except:
  35. warnMsg = "problem occurred while serializing "
  36. warnMsg += "instance of a type '%s'" % type(value)
  37. singleTimeWarnMessage(warnMsg)
  38. retVal = base64encode(pickle.dumps(str(value), pickle.HIGHEST_PROTOCOL))
  39. return retVal
  40. def base64unpickle(value):
  41. """
  42. Decodes value from Base64 to plain format and deserializes (with pickle) its content
  43. >>> base64unpickle('gAJVBmZvb2JhcnEALg==')
  44. 'foobar'
  45. """
  46. return pickle.loads(base64decode(value))
  47. def hexdecode(value):
  48. """
  49. Decodes string value from hex to plain format
  50. >>> hexdecode('666f6f626172')
  51. 'foobar'
  52. """
  53. value = value.lower()
  54. return (value[2:] if value.startswith("0x") else value).decode("hex")
  55. def hexencode(value):
  56. """
  57. Encodes string value from plain to hex format
  58. >>> hexencode('foobar')
  59. '666f6f626172'
  60. """
  61. return utf8encode(value).encode("hex")
  62. def unicodeencode(value, encoding=None):
  63. """
  64. Returns 8-bit string representation of the supplied unicode value
  65. >>> unicodeencode(u'foobar')
  66. 'foobar'
  67. """
  68. retVal = value
  69. if isinstance(value, unicode):
  70. try:
  71. retVal = value.encode(encoding or UNICODE_ENCODING)
  72. except UnicodeEncodeError:
  73. retVal = value.encode(UNICODE_ENCODING, "replace")
  74. return retVal
  75. def utf8encode(value):
  76. """
  77. Returns 8-bit string representation of the supplied UTF-8 value
  78. >>> utf8encode(u'foobar')
  79. 'foobar'
  80. """
  81. return unicodeencode(value, "utf-8")
  82. def utf8decode(value):
  83. """
  84. Returns UTF-8 representation of the supplied 8-bit string representation
  85. >>> utf8decode('foobar')
  86. u'foobar'
  87. """
  88. return value.decode("utf-8")
  89. def htmlunescape(value):
  90. """
  91. Returns (basic conversion) HTML unescaped value
  92. >>> htmlunescape('a<b')
  93. 'a<b'
  94. """
  95. retVal = value
  96. if value and isinstance(value, basestring):
  97. codes = (('&lt;', '<'), ('&gt;', '>'), ('&quot;', '"'), ('&nbsp;', ' '), ('&amp;', '&'))
  98. retVal = reduce(lambda x, y: x.replace(y[0], y[1]), codes, retVal)
  99. return retVal
  100. def singleTimeWarnMessage(message): # Cross-linked function
  101. pass
  102. def stdoutencode(data):
  103. retVal = None
  104. try:
  105. # Reference: http://bugs.python.org/issue1602
  106. if IS_WIN:
  107. output = data.encode("ascii", "replace")
  108. if output != data:
  109. warnMsg = "cannot properly display Unicode characters "
  110. warnMsg += "inside Windows OS command prompt "
  111. warnMsg += "(http://bugs.python.org/issue1602). All "
  112. warnMsg += "unhandled occurances will result in "
  113. warnMsg += "replacement with '?' character. Please, find "
  114. warnMsg += "proper character representation inside "
  115. warnMsg += "corresponding output files. "
  116. singleTimeWarnMessage(warnMsg)
  117. retVal = output
  118. else:
  119. retVal = data.encode(sys.stdout.encoding)
  120. except:
  121. retVal = data.encode(UNICODE_ENCODING)
  122. return retVal
  123. def jsonize(data):
  124. """
  125. Returns JSON serialized data
  126. >>> jsonize({'foo':'bar'})
  127. '{\\n "foo": "bar"\\n}'
  128. """
  129. return json.dumps(data, sort_keys=False, indent=4)
  130. def dejsonize(data):
  131. """
  132. Returns JSON deserialized data
  133. >>> dejsonize('{\\n "foo": "bar"\\n}')
  134. {u'foo': u'bar'}
  135. """
  136. return json.loads(data)