sqlmap/lib/core/convert.py

105 lines
2.5 KiB
Python
Raw Normal View History

2008-10-15 19:38:22 +04:00
#!/usr/bin/env python
"""
2008-10-15 19:56:32 +04:00
$Id$
2008-10-15 19:38:22 +04:00
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
2010-10-15 03:18:29 +04:00
See the file 'doc/COPYING' for copying permission
2008-10-15 19:38:22 +04:00
"""
try:
import hashlib
except:
import md5
import sha
import pickle
import sys
2008-10-15 19:38:22 +04:00
import struct
import urllib
from lib.core.data import conf
def base64decode(value):
return value.decode("base64")
2008-10-15 19:38:22 +04:00
def base64encode(value):
return value.encode("base64")[:-1].replace("\n", "")
def base64pickle(value):
return base64encode(pickle.dumps(value))
def base64unpickle(value):
return pickle.loads(base64decode(value))
2008-10-15 19:38:22 +04:00
def hexdecode(value):
value = value.lower()
2008-10-15 19:38:22 +04:00
if value.startswith("0x"):
value = value[2:]
2008-10-15 19:38:22 +04:00
return value.decode("hex")
def hexencode(value):
return value.encode("hex")
2008-10-15 19:38:22 +04:00
def md5hash(value):
if sys.modules.has_key('hashlib'):
return hashlib.md5(value).hexdigest()
else:
return md5.new(value).hexdigest()
2008-10-15 19:38:22 +04:00
def orddecode(value):
packedString = struct.pack("!"+"I" * len(value), *value)
2008-10-15 19:38:22 +04:00
return "".join([chr(char) for char in struct.unpack("!"+"I"*(len(packedString)/4), packedString)])
def ordencode(value):
return tuple([ord(char) for char in value])
2008-10-15 19:38:22 +04:00
def sha1hash(value):
if sys.modules.has_key('hashlib'):
return hashlib.sha1(value).hexdigest()
else:
return sha.new(value).hexdigest()
2008-10-15 19:38:22 +04:00
def urldecode(value):
result = None
if value:
try:
# for cases like T%C3%BCrk%C3%A7e
value = str(value)
result = utf8decode(urllib.unquote_plus(value))
except ValueError:
result = urllib.unquote_plus(value)
2008-10-15 19:38:22 +04:00
return result
2008-10-15 19:38:22 +04:00
def urlencode(value, safe=":/?%&=", convall=False):
if conf.direct or "POSTxml" in conf.paramDict:
return value
result = None
if value is None:
return result
2008-10-15 19:38:22 +04:00
if convall:
result = urllib.quote(utf8encode(value)) # Reference: http://old.nabble.com/Re:-Problem:-neither-urllib2.quote-nor-urllib.quote-encode-the--unicode-strings-arguments-p19823144.html
else:
result = urllib.quote(utf8encode(value), safe)
return result
def utf8encode(value):
return value.encode("utf-8")
def utf8decode(value):
return value.decode("utf-8")
def htmlescape(value):
return value.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;').replace(' ', '&nbsp;')
def htmlunescape(value):
return value.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('&quot;', '"').replace('&#39;', "'").replace('&nbsp;', ' ')