sqlmap/lib/core/bigarray.py

143 lines
4.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2012-02-16 13:46:41 +04:00
"""
2014-01-13 21:24:49 +04:00
Copyright (c) 2006-2014 sqlmap developers (http://sqlmap.org/)
2012-02-16 13:46:41 +04:00
See the file 'doc/COPYING' for copying permission
"""
try:
import cPickle as pickle
except:
import pickle
2012-02-16 13:54:29 +04:00
import os
2012-02-16 13:46:41 +04:00
import tempfile
2014-11-16 16:25:44 +03:00
from lib.core.exception import SqlmapSystemException
2012-02-16 13:46:41 +04:00
from lib.core.settings import BIGARRAY_CHUNK_LENGTH
class Cache(object):
2012-06-14 13:10:28 +04:00
"""
Auxiliary class used for storing cached chunks
"""
def __init__(self, index, data, dirty):
self.index = index
self.data = data
self.dirty = dirty
2012-02-16 13:46:41 +04:00
class BigArray(list):
"""
2012-06-14 13:10:28 +04:00
List-like class used for storing large amounts of data (disk cached)
2012-02-16 13:46:41 +04:00
"""
def __init__(self):
self.chunks = [[]]
self.cache = None
self.filenames = set()
2014-11-09 20:40:49 +03:00
self.protected = False
self._os_remove = os.remove
2012-02-16 13:46:41 +04:00
def append(self, value):
self.chunks[-1].append(value)
if len(self.chunks[-1]) >= BIGARRAY_CHUNK_LENGTH:
filename = self._dump(self.chunks[-1])
del(self.chunks[-1][:])
self.chunks[-1] = filename
self.chunks.append([])
2012-12-18 19:03:35 +04:00
def extend(self, value):
for _ in value:
self.append(_)
2012-02-16 13:46:41 +04:00
def pop(self):
if len(self.chunks[-1]) < 1:
self.chunks.pop()
2012-06-14 13:10:28 +04:00
with open(self.chunks[-1], "rb") as fp:
2012-02-16 13:46:41 +04:00
self.chunks[-1] = pickle.load(fp)
return self.chunks[-1].pop()
def index(self, value):
for index in xrange(len(self)):
if self[index] == value:
return index
return ValueError, "%s is not in list" % value
def _dump(self, value):
2014-11-16 16:25:44 +03:00
try:
handle, filename = tempfile.mkstemp(prefix="sqlmapba-")
self.filenames.add(filename)
os.close(handle)
with open(filename, "w+b") as fp:
pickle.dump(value, fp, pickle.HIGHEST_PROTOCOL)
return filename
except IOError, ex:
errMsg = "exception occurred while storing data "
errMsg += "to a temporary file ('%s')" % ex
raise SqlmapSystemException, errMsg
2012-02-16 13:46:41 +04:00
def _checkcache(self, index):
2012-06-14 13:10:28 +04:00
if (self.cache and self.cache.index != index and self.cache.dirty):
filename = self._dump(self.cache.data)
self.chunks[self.cache.index] = filename
if not (self.cache and self.cache.index == index):
with open(self.chunks[index], "rb") as fp:
2012-06-14 16:46:32 +04:00
self.cache = Cache(index, pickle.load(fp), False)
2012-02-16 13:46:41 +04:00
2014-11-09 20:40:49 +03:00
def __getstate__(self):
self.protected = True
return self.chunks, self.filenames, self.protected
def __setstate__(self, state):
self.__init__()
self.chunks, self.filenames, self.protected = state
2012-07-06 14:24:55 +04:00
def __getslice__(self, i, j):
retval = BigArray()
i = max(0, len(self) + i if i < 0 else i)
j = min(len(self), len(self) + j if j < 0 else j)
for _ in xrange(i, j):
retval.append(self[_])
return retval
2012-02-16 13:46:41 +04:00
def __getitem__(self, y):
if y < 0:
y += len(self)
2012-02-16 13:46:41 +04:00
index = y / BIGARRAY_CHUNK_LENGTH
offset = y % BIGARRAY_CHUNK_LENGTH
chunk = self.chunks[index]
if isinstance(chunk, list):
return chunk[offset]
else:
self._checkcache(index)
2012-06-14 13:10:28 +04:00
return self.cache.data[offset]
2012-02-16 13:46:41 +04:00
def __setitem__(self, y, value):
index = y / BIGARRAY_CHUNK_LENGTH
offset = y % BIGARRAY_CHUNK_LENGTH
chunk = self.chunks[index]
if isinstance(chunk, list):
chunk[offset] = value
else:
self._checkcache(index)
2012-06-14 13:10:28 +04:00
self.cache.data[offset] = value
self.cache.dirty = True
2012-02-16 13:46:41 +04:00
def __repr__(self):
return "%s%s" % ("..." if len(self.chunks) > 1 else "", self.chunks[-1].__repr__())
def __iter__(self):
for i in xrange(len(self)):
yield self[i]
def __len__(self):
return len(self.chunks[-1]) if len(self.chunks) == 1 else (len(self.chunks) - 1) * BIGARRAY_CHUNK_LENGTH + len(self.chunks[-1])
def __del__(self):
2014-11-09 20:40:49 +03:00
if not self.protected:
for filename in self.filenames:
try:
self._os_remove(filename)
except:
pass