2011-09-26 00:36:32 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
Copyright (c) 2006-2011 sqlmap developers (http://www.sqlmap.org/)
|
|
|
|
See the file 'doc/COPYING' for copying permission
|
|
|
|
"""
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
from lib.core.settings import UNICODE_ENCODING
|
2011-09-26 17:36:08 +04:00
|
|
|
from lib.core.threads import getCurrentThreadData
|
2011-09-26 00:36:32 +04:00
|
|
|
|
2011-09-26 17:36:08 +04:00
|
|
|
class HashDB(object):
|
2011-09-26 00:36:32 +04:00
|
|
|
def __init__(self, filepath):
|
2011-09-26 17:36:08 +04:00
|
|
|
self.filepath = filepath
|
|
|
|
|
|
|
|
def _get_cursor(self):
|
|
|
|
threadData = getCurrentThreadData()
|
|
|
|
|
|
|
|
if threadData.hashDBCursor is None:
|
2011-10-10 18:23:17 +04:00
|
|
|
connection = sqlite3.connect(self.filepath, timeout=3, isolation_level=None)
|
2011-09-26 17:36:08 +04:00
|
|
|
threadData.hashDBCursor = connection.cursor()
|
|
|
|
threadData.hashDBCursor.execute("CREATE TABLE IF NOT EXISTS storage (id INTEGER PRIMARY KEY, value TEXT)")
|
|
|
|
|
|
|
|
return threadData.hashDBCursor
|
|
|
|
|
|
|
|
cursor = property(_get_cursor)
|
2011-09-26 00:36:32 +04:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
try:
|
2011-09-26 17:36:08 +04:00
|
|
|
self.cursor.connection.close()
|
2011-09-26 00:36:32 +04:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def hashKey(self, key):
|
|
|
|
key = key.encode(UNICODE_ENCODING) if isinstance(key, unicode) else repr(key)
|
|
|
|
retVal = int(hashlib.md5(key).hexdigest()[:8], 16)
|
|
|
|
return retVal
|
|
|
|
|
|
|
|
def retrieve(self, key):
|
|
|
|
retVal = None
|
|
|
|
if key:
|
|
|
|
hash_ = self.hashKey(key)
|
2011-10-10 18:23:17 +04:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
for row in self.cursor.execute("SELECT value FROM storage WHERE id=?", (hash_,)):
|
|
|
|
retVal = row[0]
|
|
|
|
except sqlite3.OperationalError, ex:
|
|
|
|
if not 'locked' in ex.message:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
break
|
2011-09-26 00:36:32 +04:00
|
|
|
return retVal
|
|
|
|
|
|
|
|
def write(self, key, value):
|
|
|
|
if key:
|
|
|
|
hash_ = self.hashKey(key)
|
2011-10-10 18:23:17 +04:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.cursor.execute("INSERT INTO storage VALUES (?, ?)", (hash_, value,))
|
|
|
|
except sqlite3.IntegrityError:
|
|
|
|
self.cursor.execute("UPDATE storage SET value=? WHERE id=?", (value, hash_,))
|
|
|
|
except sqlite3.OperationalError, ex:
|
|
|
|
if not 'locked' in ex.message:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
break
|