sqlmap/lib/core/bigarray.py

205 lines
5.9 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
2012-02-16 13:46:41 +04:00
"""
2023-01-03 01:24:59 +03:00
Copyright (c) 2006-2023 sqlmap developers (https://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
2012-02-16 13:46:41 +04:00
"""
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
import zlib
2012-02-16 13:46:41 +04:00
2019-03-28 18:04:38 +03:00
from lib.core.compat import xrange
2016-05-31 14:02:26 +03:00
from lib.core.enums import MKSTEMP_PREFIX
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
from lib.core.settings import BIGARRAY_COMPRESS_LEVEL
2012-02-16 13:46:41 +04:00
try:
DEFAULT_SIZE_OF = sys.getsizeof(object())
except TypeError:
DEFAULT_SIZE_OF = 16
2015-01-07 12:53:57 +03:00
def _size_of(instance):
2015-01-07 11:21:02 +03:00
"""
Returns total size of a given instance / object (in bytes)
2015-01-07 11:21:02 +03:00
"""
retval = sys.getsizeof(instance, DEFAULT_SIZE_OF)
2017-10-31 13:05:25 +03:00
if isinstance(instance, dict):
retval += sum(_size_of(_) for _ in itertools.chain.from_iterable(instance.items()))
elif hasattr(instance, "__iter__"):
retval += sum(_size_of(_) for _ in instance if _ != instance)
2017-10-31 13:05:25 +03:00
2015-01-07 12:46:06 +03:00
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)
2019-07-03 11:56:05 +03:00
>>> _ = BigArray(xrange(100000))
>>> _[20] = 0
>>> _[99999]
99999
2021-05-19 19:20:39 +03:00
>>> _ += [0]
>>> _[100000]
0
>>> _ = _ + [1]
>>> _[-1]
1
2012-02-16 13:46:41 +04:00
"""
2019-05-30 23:40:51 +03:00
def __init__(self, items=None):
2012-02-16 13:46:41 +04:00
self.chunks = [[]]
2019-05-02 11:22:44 +03:00
self.chunk_length = sys.maxsize
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
2019-05-30 23:40:51 +03:00
for item in (items or []):
2017-12-12 12:48:19 +03:00
self.append(item)
2021-05-19 19:20:39 +03:00
def __add__(self, value):
retval = BigArray(self)
for _ in value:
retval.append(_)
return retval
def __iadd__(self, value):
for _ in value:
self.append(_)
return self
2012-02-16 13:46:41 +04:00
def append(self, value):
self.chunks[-1].append(value)
2017-10-31 13:05:25 +03:00
2019-05-02 11:22:44 +03:00
if self.chunk_length == sys.maxsize:
2015-01-07 11:21:02 +03:00
self._size_counter += _size_of(value)
if self._size_counter >= BIGARRAY_CHUNK_SIZE:
self.chunk_length = len(self.chunks[-1])
self._size_counter = None
2017-10-31 13:05:25 +03:00
2015-01-07 11:21:02 +03:00
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 f:
self.chunks[-1] = pickle.loads(zlib.decompress(f.read()))
2019-01-22 02:40:48 +03:00
except IOError as ex:
2014-12-23 10:36:00 +03:00
errMsg = "exception occurred while retrieving data "
2019-05-03 00:51:54 +03:00
errMsg += "from a temporary file ('%s')" % ex
2018-03-13 13:13:38 +03:00
raise SqlmapSystemException(errMsg)
2017-10-31 13:05:25 +03:00
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
2017-10-31 13:05:25 +03:00
2012-02-16 13:46:41 +04:00
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:
2016-05-31 14:02:26 +03:00
handle, filename = tempfile.mkstemp(prefix=MKSTEMP_PREFIX.BIG_ARRAY)
2014-11-16 16:25:44 +03:00
self.filenames.add(filename)
os.close(handle)
with open(filename, "w+b") as f:
f.write(zlib.compress(pickle.dumps(chunk, pickle.HIGHEST_PROTOCOL), BIGARRAY_COMPRESS_LEVEL))
2014-11-16 16:25:44 +03:00
return filename
2019-01-22 02:40:48 +03:00
except (OSError, IOError) as ex:
2014-11-16 16:25:44 +03:00
errMsg = "exception occurred while storing data "
2019-05-03 00:51:54 +03:00
errMsg += "to a temporary file ('%s'). Please " % ex
2015-01-14 15:53:38 +03:00
errMsg += "make sure that there is enough disk space left. If problem persists, "
errMsg += "try to set environment variable 'TEMP' to a location "
errMsg += "writeable by the current user"
2018-03-13 13:13:38 +03:00
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
2017-10-31 13:05:25 +03:00
2012-06-14 13:10:28 +04:00
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 f:
self.cache = Cache(index, pickle.loads(zlib.decompress(f.read())), False)
2019-01-22 02:40:48 +03:00
except Exception as ex:
2014-12-23 10:36:00 +03:00
errMsg = "exception occurred while retrieving data "
2019-05-03 00:51:54 +03:00
errMsg += "from a temporary file ('%s')" % ex
2018-03-13 13:13:38 +03:00
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-02-16 13:46:41 +04:00
def __getitem__(self, y):
2021-05-19 19:20:39 +03:00
while y < 0:
y += len(self)
2017-10-31 13:05:25 +03:00
2019-01-22 04:29:52 +03:00
index = y // self.chunk_length
2015-01-07 11:21:02 +03:00
offset = y % self.chunk_length
2012-02-16 13:46:41 +04:00
chunk = self.chunks[index]
2017-10-31 13:05:25 +03:00
2012-02-16 13:46:41 +04:00
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):
2019-01-22 04:29:52 +03:00
index = y // self.chunk_length
2015-01-07 11:21:02 +03:00
offset = y % self.chunk_length
2012-02-16 13:46:41 +04:00
chunk = self.chunks[index]
2017-10-31 13:05:25 +03:00
2012-02-16 13:46:41 +04:00
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])