#!/usr/bin/env python

"""
Copyright (c) 2006-2024 sqlmap developers (https://sqlmap.org/)
See the file 'LICENSE' for copying permission
"""

# 导入xrange和PRIORITY
from lib.core.compat import xrange
from lib.core.enums import PRIORITY

# 定义优先级为LOW
__priority__ = PRIORITY.LOW

# 定义依赖函数
def dependencies():
    pass

def tamper(payload, **kwargs):
    """
    Replaces space character (' ') with plus ('+')

    Notes:
        * Is this any useful? The plus get's url-encoded by sqlmap engine invalidating the query afterwards
        * This tamper script works against all databases

    >>> tamper('SELECT id FROM users')
    'SELECT+id+FROM+users'
    """

    retVal = payload

    # 如果payload不为空
    if payload:
        retVal = ""
        quote, doublequote, firstspace = False, False, False

        # 遍历payload的每个字符
        for i in xrange(len(payload)):
            # 如果第一个字符不是空格
            if not firstspace:
                # 如果当前字符是空格
                if payload[i].isspace():
                    firstspace = True
                    retVal += "+"
                    continue

            # 如果当前字符是单引号
            elif payload[i] == '\'':
                quote = not quote

            # 如果当前字符是双引号
            elif payload[i] == '"':
                doublequote = not doublequote

            # 如果当前字符是空格，并且不在双引号和单引号中
            elif payload[i] == " " and not doublequote and not quote:
                retVal += "+"
                continue

            # 将当前字符添加到retVal中
            retVal += payload[i]

    return retVal
