2019-03-21 14:00:09 +01:00
|
|
|
#!/usr/bin/env python2
|
2012-07-30 11:21:32 +02:00
|
|
|
|
|
|
|
|
"""
|
2019-01-05 21:38:52 +01:00
|
|
|
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
|
2017-10-11 14:50:46 +02:00
|
|
|
See the file 'LICENSE' for copying permission
|
2012-07-30 11:21:32 +02:00
|
|
|
"""
|
|
|
|
|
|
2018-12-28 18:25:56 +01:00
|
|
|
import functools
|
2017-12-24 23:54:43 +01:00
|
|
|
import hashlib
|
2019-01-29 23:44:58 +01:00
|
|
|
import threading
|
2017-12-24 23:54:43 +01:00
|
|
|
|
2019-01-29 23:44:58 +01:00
|
|
|
from lib.core.settings import MAX_CACHE_ITEMS
|
2019-04-30 13:20:31 +02:00
|
|
|
from lib.core.settings import UNICODE_ENCODING
|
2019-01-29 23:44:58 +01:00
|
|
|
from lib.core.datatype import LRUDict
|
2018-04-01 12:45:47 +02:00
|
|
|
from lib.core.threads import getCurrentThreadData
|
|
|
|
|
|
2019-01-29 23:44:58 +01:00
|
|
|
_lock = threading.Lock()
|
|
|
|
|
|
|
|
|
|
def cachedmethod(f, cache=LRUDict(capacity=MAX_CACHE_ITEMS)):
|
2012-07-30 11:21:32 +02:00
|
|
|
"""
|
|
|
|
|
Method with a cached content
|
|
|
|
|
|
|
|
|
|
Reference: http://code.activestate.com/recipes/325205-cache-decorator-in-python-24/
|
|
|
|
|
"""
|
2013-01-30 10:38:11 +01:00
|
|
|
|
2018-12-28 18:25:56 +01:00
|
|
|
@functools.wraps(f)
|
2012-07-30 10:06:14 +02:00
|
|
|
def _(*args, **kwargs):
|
2019-04-30 13:20:31 +02:00
|
|
|
key = int(hashlib.md5("|".join(str(_) for _ in (f, args, kwargs)).encode(UNICODE_ENCODING)).hexdigest(), 16) & 0x7fffffffffffffff
|
2016-05-14 14:18:34 +02:00
|
|
|
|
2019-02-09 23:18:08 +01:00
|
|
|
try:
|
2019-02-12 10:49:47 +01:00
|
|
|
with _lock:
|
|
|
|
|
result = cache[key]
|
2019-02-09 23:18:08 +01:00
|
|
|
except KeyError:
|
|
|
|
|
result = f(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
with _lock:
|
|
|
|
|
cache[key] = result
|
|
|
|
|
|
|
|
|
|
return result
|
2013-01-30 10:38:11 +01:00
|
|
|
|
2012-07-30 10:06:14 +02:00
|
|
|
return _
|
2018-04-01 12:45:47 +02:00
|
|
|
|
|
|
|
|
def stackedmethod(f):
|
2018-12-28 18:25:56 +01:00
|
|
|
"""
|
|
|
|
|
Method using pushValue/popValue functions (fallback function for stack realignment)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@functools.wraps(f)
|
2018-04-01 12:45:47 +02:00
|
|
|
def _(*args, **kwargs):
|
|
|
|
|
threadData = getCurrentThreadData()
|
|
|
|
|
originalLevel = len(threadData.valueStack)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
result = f(*args, **kwargs)
|
|
|
|
|
finally:
|
|
|
|
|
if len(threadData.valueStack) > originalLevel:
|
|
|
|
|
threadData.valueStack = threadData.valueStack[:originalLevel]
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
2018-06-09 23:38:00 +02:00
|
|
|
return _
|