sqlmap/lib/core/bigarray.py

164 lines
5.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2012-02-16 13:46:41 +04:00
"""
2015-01-06 17:02:16 +03:00
Copyright (c) 2006-2015 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
2015-01-07 12:49:15 +03:00
import itertools
2012-02-16 13:54:29 +04:00
import os
2015-01-07 11:21:02 +03:00
import sys
2012-02-16 13:46:41 +04:00
import tempfile
2014-11-16 16:25:44 +03:00
from lib.core.exception import SqlmapSystemException
2015-01-07 11:21:02 +03:00
from lib.core.settings import BIGARRAY_CHUNK_SIZE
2014-12-01 02:29:25 +03:00
from lib.core.settings import BIGARRAY_TEMP_PREFIX
2012-02-16 13:46:41 +04:00
2015-01-07 11:21:02 +03:00
def _size_of(object):
"""
Returns total size of a given object (in bytes)
"""
2015-01-07 12:46:06 +03:00
retval = sys.getsizeof(object)
2015-01-07 12:49:15 +03:00
if isinstance(object, dict):
retval += sum(_size_of(_) for _ in itertools.chain.from_iterable(object.items()))
elif hasattr(object, "__iter__"):
2015-01-07 12:46:06 +03:00
retval += sum(_size_of(_) for _ in object)
return retval
2015-01-07 11:21:02 +03:00
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 = [[]]
2015-01-07 11:21:02 +03:00
self.chunk_length = sys.maxint
2012-02-16 13:46:41 +04:00
self.cache = None
self.filenames = set()
self._os_remove = os.remove
2015-01-07 11:21:02 +03:00
self._size_counter = 0
2012-02-16 13:46:41 +04:00
def append(self, value):
self.chunks[-1].append(value)
2015-01-07 11:21:02 +03:00
if self.chunk_length == sys.maxint:
self._size_counter += _size_of(value)
if self._size_counter >= BIGARRAY_CHUNK_SIZE:
self.chunk_length = len(self.chunks[-1])
self._size_counter = None
if len(self.chunks[-1]) >= self.chunk_length:
2012-02-16 13:46:41 +04:00
filename = self._dump(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()
2014-12-23 10:36:00 +03:00
try:
with open(self.chunks[-1], "rb") as fp:
2015-01-07 11:21:02 +03:00
self.chunks[-1] = pickle.load(fp)
2014-12-23 10:36:00 +03:00
except IOError, ex:
errMsg = "exception occurred while retrieving data "
errMsg += "from a temporary file ('%s')" % ex
raise SqlmapSystemException, errMsg
2012-02-16 13:46:41 +04:00
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
2015-01-07 04:04:10 +03:00
def _dump(self, chunk):
2014-11-16 16:25:44 +03:00
try:
2014-12-01 02:29:25 +03:00
handle, filename = tempfile.mkstemp(prefix=BIGARRAY_TEMP_PREFIX)
2014-11-16 16:25:44 +03:00
self.filenames.add(filename)
os.close(handle)
2015-01-07 11:21:02 +03:00
with open(filename, "w+b") as fp:
pickle.dump(chunk, fp, pickle.HIGHEST_PROTOCOL)
2014-11-16 16:25:44 +03:00
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):
2014-12-23 10:36:00 +03:00
try:
with open(self.chunks[index], "rb") as fp:
2015-01-07 11:21:02 +03:00
self.cache = Cache(index, pickle.load(fp), False)
2014-12-23 10:36:00 +03:00
except IOError, ex:
errMsg = "exception occurred while retrieving data "
errMsg += "from a temporary file ('%s')" % ex
raise SqlmapSystemException, errMsg
2012-02-16 13:46:41 +04:00
2014-11-09 20:40:49 +03:00
def __getstate__(self):
2014-12-01 02:29:25 +03:00
return self.chunks, self.filenames
2014-11-09 20:40:49 +03:00
def __setstate__(self, state):
self.__init__()
2014-12-01 02:29:25 +03:00
self.chunks, self.filenames = state
2014-11-09 20:40:49 +03:00
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)
2015-01-07 11:21:02 +03:00
index = y / self.chunk_length
offset = y % self.chunk_length
2012-02-16 13:46:41 +04:00
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):
2015-01-07 11:21:02 +03:00
index = y / self.chunk_length
offset = y % self.chunk_length
2012-02-16 13:46:41 +04:00
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):
2015-01-07 11:21:02 +03:00
return len(self.chunks[-1]) if len(self.chunks) == 1 else (len(self.chunks) - 1) * self.chunk_length + len(self.chunks[-1])