本帖最后由 tubaba 于 2017-2-24 09:16 编辑
去年编写过一个脚本迷惑工具,论坛研究这个的不多,我用的办法比较笨,正则运用不够熟练.我是这么操作的,下面三个函数是处理区域注释以及空行..希望可以帮到你
_ScriptPretreatment($sPath, $Flag = 0, $EncodeMode = 512) ;脚本预处理
If $StopFlag = True Then Return SetError(1, 0, False)
If $Flag Then _GUICtrlListView_SetItemText($hTab3_ListView1, 1, '正在执行', 2)
If $LogFlag Then _Log('正在进行脚本预处理...')
Local $hFile = FileOpen($sPath)
Local $sSource = FileRead($hFile)
FileClose($hFile)
Local $TempLineStr = $sSource
Local $Pattern3 = '(?<!\r)\n' ;处理换行
Local $Pattern1 = '(' & "'|" & '")(.*?)(\1)' ;成对引号
Local $Pattern2 = '([\x1c|\x1d]+)(\x02\x22|\x02\x27)'
Local $Pattern4 = '(\[[^\[\],]*)(,)' ;中括号内的逗号替换为chr(1)
$TempLineStr = StringRegExpReplace($TempLineStr, $Pattern3, @CRLF)
$TempLineStr = StringReplace(StringReplace($TempLineStr, Chr(39) & Chr(39), Chr(29)), Chr(34) & Chr(34), Chr(28)) ;相邻双引号或单引号替换其它符号,为下步做准备
$TempLineStr = StringRegExpReplace($TempLineStr, $Pattern1, Chr(2) & '${1}${2}${3}' & Chr(3)) ;加入字符串起止标志
$TempLineStr = StringRegExpReplace($TempLineStr, $Pattern2, '${2}${1}') ;将起始标志前的成对相邻双引号或成对相邻单引号移动到起始标志后
Local $n = 0
Do
$n += 1
If $StopFlag = True Then Return SetError(1, 0, False)
$TempLineStr = StringRegExpReplace($TempLineStr, $Pattern4, '${1}' & Chr(1)) ;中括号内的逗号替换为chr(1)
Until @error = 0 And @extended = 0
Local $hFile = FileOpen($sPath, 2 + $EncodeMode)
FileWrite($hFile, $TempLineStr)
FileClose($hFile)
If $Flag Then _GUICtrlListView_SetItemText($hTab3_ListView1, 1, '已完成', 2)
EndFunc ;==>_ScriptPretreatment
Func _RemoveFromScript($sOutPath, $Flag = 0, $EncodeMode = 512)
If $StopFlag = True Then Return SetError(1, 0, False)
_FilePretreatment($sOutPath, $Flag, $EncodeMode, 0) ;去除注释
If $StopFlag = True Then Return SetError(1, 0, False)
_FilePretreatment($sOutPath, $Flag, $EncodeMode, 1) ;去除空行
If $StopFlag = True Then Return SetError(1, 0, False)
EndFunc ;==>_RemoveFromScript
Func _FilePretreatment($sInPutFilePath, $MFlag, $EncodeMode, $Flag)
If $StopFlag = True Then Return SetError(1, 0, False)
Local $hFile = FileOpen($sInPutFilePath)
Local $sStr = FileRead($hFile)
FileClose($hFile)
Switch $Flag
Case 0 ;去除注释
If $MFlag Then _GUICtrlListView_SetItemText($hTab3_ListView1, 2, '正在执行', 2)
If $LogFlag Then _Log('正在清除区域注释...')
GUICtrlSetData($Label1, '正在清除区域注释...')
Local $Pattern1 = '(?m)(?<=^)\h*;.*(\r\n)|(?<=^)\h*;.*' ;开头是";"的行去除
$sStr = StringRegExpReplace($sStr, $Pattern1, '')
Local $aRead = StringSplit($sStr, @CRLF, 1 + 2)
Local $Pattern2 = '(.*?)(\h*;.*)'
Local $Pattern3 = '\x02[^\x02\x03]+?\x03'
Local $Pattern4 = '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)'
Local $Pattern5 = '(?s)(?i)(?:\r?\n\h*#cs|\r?\n\h*#comments-start).+?(?:(?:\r?\n\h*#cs|\r?\n\h*#comments-start).+?(?:\r?\n\h*#ce|\r?\n\h*#comments-end))*(?:\s*(?:\r?\n\h*#ce|\r?\n\h*#comments-end))+[^\r\n]*\r?\n' ;区域注释
Local $CountLine = UBound($aRead)
For $i = 0 To UBound($aRead) - 1
If $StopFlag = True Then Return SetError(1, 0, False)
Local $TempLineStr = $aRead[$i], $TempStr, $SerchStr = ''
If StringRegExp($TempLineStr, $Pattern3) Then
$TempLineStr = StringRegExpReplace($TempLineStr, $Pattern4, '\\${1}') ;转义特殊字符
$TempLineStr = StringRegExpReplace($TempLineStr, $Pattern3, '\\x02.+?\\x03') ;替换为表达式
$SerchStr = StringRegExpReplace($TempLineStr, $Pattern2, '${2}')
If @extended Then $aRead[$i] = StringRegExpReplace($aRead[$i], $SerchStr, '')
Else
$SerchStr = StringRegExpReplace($aRead[$i], $Pattern2, '${2}')
If @extended Then $aRead[$i] = StringReplace($aRead[$i], $SerchStr, '', -1, 1)
EndIf
If $MFlag And Mod($i, 50) = 0 Then _GUICtrlListView_SetItemText($hTab3_ListView1, 2, '已完成' & Chr(32) & $i & '/' & $CountLine, 2)
Next
$hFile = FileOpen($sInPutFilePath, 2 + $EncodeMode)
FileWriteLine($hFile, $sHead)
For $i = 0 To UBound($aRead) - 1
FileWriteLine($hFile, $aRead[$i])
Next
FileClose($hFile)
_GUICtrlListView_SetItemText($hTab3_ListView1, 2, '正在清除区域注释...', 2)
Local $hFile = FileOpen($sInPutFilePath)
Local $sStr = FileRead($hFile)
FileClose($hFile)
$sStr = StringRegExpReplace(@CRLF & $sStr, $Pattern5, @CRLF)
$hFile = FileOpen($sInPutFilePath, 2 + $EncodeMode)
FileWrite($hFile, $sStr)
FileClose($hFile)
If $MFlag Then _GUICtrlListView_SetItemText($hTab3_ListView1, 2, '已完成', 2)
GUICtrlSetData($Label1, '清除完成...')
Case 1 ;去除空行
If $MFlag Then _GUICtrlListView_SetItemText($hTab3_ListView1, 3, '正在执行', 2)
If $LogFlag Then _Log('正在清除空行及行首行未空白字符...')
GUICtrlSetData($Label1, '正在清除空行及行首行未空白字符...')
Local $Pattern1 = '(?m)(?<=^)\h*(.+)'
Local $Pattern2 = '(?<!\r)\n' ;处理换行
Local $Pattern3 = '\r\n(?=\r\n)|\r\n$'
Local $Pattern4 = '^\r\n'
$sStr = StringRegExpReplace($sStr, $Pattern1, '${1}') ;删除行首空白符
$sStr = StringRegExpReplace($sStr, $Pattern2, @CRLF)
$sStr = StringRegExpReplace(@CRLF & $sStr, $Pattern3, '') ;删除空行
$sStr = StringRegExpReplace($sStr, $Pattern4, '') ;删除空行
$hFile = FileOpen($sInPutFilePath, 2 + $EncodeMode)
FileWrite($hFile, $sStr)
FileClose($hFile)
If $MFlag Then _GUICtrlListView_SetItemText($hTab3_ListView1, 3, '已完成', 2)
GUICtrlSetData($Label1, '清除完成...')
EndSwitch
EndFunc ;==>_FilePretreatment
|