本帖最后由 xowen 于 2011-1-29 10:22 编辑
以下代码主要功能是记录软件使用次数,并把次数加密注入exe,然后在解密字符,并运行的判断次数是否等于最大限制次数。流程如下:
1、将$Endtime终止次数与软件运行次数Times以pcxu加密注入exe-->然后通过解密还原出$Endtime、Times。
2、通过StringMid截取$Endtime最后3位数,即300.
3、对比Times是否大于300,大于则退出程序,小于则在Times的基础上加1,,并注入exe。
错误:$Endtime限制软件最大能运行300次,但每次运行到第4次时就提示已到最大次数,求解,为何代码会在"StringMid($RINFO,11,3)<$RTimes"判断时会计算出"300<4"?(以下代码需要先编译后运行)#Include <_StringEncrypt.au3>
#include <Binfileinstall.au3>
;_StringEncrypt(1, $x, $Key, 1)==>使用$Key加密字符$x。
;_StringEncrypt(0, $x, $Key, 1)==>使用$Key解密字符$x。
Dim $Key = 'pcxu', $Serialtxt = 'SerialNO.ini',$Endtime=2012121200300
$SerialReadInfo = _Readfileinfo($BackupFilepath)
If Not IsArray($SerialReadInfo) Then ;===>第一次运行,设定次数为0。
_UpdataInTxtfile($Serialtxt, '[CONF]' & @CRLF & 'Times=' & _StringEncrypt(1, '1', $Key, 1) & @CRLF & 'INFO=' & _StringEncrypt(1, $Endtime, $Key, 1) & @CRLF)
$SerialReadInfo = _Readfileinfo($BackupFilepath)
EndIf
$SerialInfo=StringSplit($SerialReadInfo[1],'|')
$RTimes = _StringEncrypt(0, _memoryiniread($Serialtxt, $SerialInfo[3], $SerialInfo[2], 'CONF', 'Times'), $Key, 1)
$RINFO = _StringEncrypt(0, _memoryiniread($Serialtxt, $SerialInfo[3], $SerialInfo[2], 'CONF', 'INFO'), $Key, 1)
MsgBox(0,StringMid($RINFO,11,3)&'-'&$RTimes,'执行'&_StringEncrypt(0,_memoryiniread($Serialtxt,$SerialInfo[3],$SerialInfo[2],'CONF','Times'),$Key,1)&'次')
If StringMid($RINFO,11,3)<$RTimes Then
MsgBox(0,'','使用次数大于限制次数!')
Exit
EndIf
$RTimes=_StringEncrypt(1,_StringEncrypt(0,_memoryiniread($Serialtxt,$SerialInfo[3],$SerialInfo[2],'CONF','Times'),$Key,1)+1,$Key,1)
_UpdataInTxtfile($Serialtxt,'[CONF]'&@CRLF&@CRLF&'Times='&$RTimes&@CRLF&'INFO='&_StringEncrypt(1,$Endtime,$Key,1))
Exit
请先将以下两个函数库复制到AU3的Include文件下后在编译。
_StringEncrypt.au3函数库代码如下:Func _StringEncrypt($i_Encrypt, $s_EncryptText, $s_EncryptPassword, $i_EncryptLevel = 1, $i_Flag = 1)
If $i_Encrypt <> 0 And $i_Encrypt <> 1 Then
SetError(1)
Return ''
ElseIf $s_EncryptText = '' Or $s_EncryptPassword = '' Then
SetError(1)
Return ''
Else
If Number($i_EncryptLevel) <= 0 Or Int($i_EncryptLevel) <> $i_EncryptLevel Then $i_EncryptLevel = 1
Local $b_EncryptText
Local $b_EncryptPassword
Local $v_EncryptModified
Local $i_EncryptCountH
Local $i_EncryptCountG
Local $v_EncryptSwap
Local $av_EncryptBox[256][2]
Local $i_EncryptCountA
Local $i_EncryptCountB
Local $i_EncryptCountC
Local $i_EncryptCountD
Local $i_EncryptCountE
Local $v_EncryptCipher
Local $v_EncryptCipherBy
$b_EncryptText = StringToBinary($s_EncryptText, $i_Flag)
$b_EncryptPassword = StringToBinary($s_EncryptPassword, $i_Flag)
If $i_Encrypt = 1 Then
For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
$i_EncryptCountG = ''
$i_EncryptCountH = ''
$v_EncryptModified = Binary('')
For $i_EncryptCountG = 1 To BinaryLen($b_EncryptText)
If $i_EncryptCountH = BinaryLen($b_EncryptPassword) Then
$i_EncryptCountH = 1
Else
$i_EncryptCountH += 1
EndIf
$v_EncryptModified = Binary($v_EncryptModified) & Binary('0x' & Hex(BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountG, 1), BinaryMid($b_EncryptPassword, $i_EncryptCountH, 1), 255),2))
Next
$b_EncryptText = $v_EncryptModified
$i_EncryptCountA = ''
$i_EncryptCountB = 0
$i_EncryptCountC = ''
$i_EncryptCountD = ''
$i_EncryptCountE = ''
$v_EncryptCipherBy = ''
$v_EncryptCipher = ''
$v_EncryptSwap = ''
$av_EncryptBox = ''
Local $av_EncryptBox[256][2]
For $i_EncryptCountA = 0 To 255
$av_EncryptBox[$i_EncryptCountA][1] = Dec(StringTrimLeft(BinaryMid($b_EncryptPassword, Mod($i_EncryptCountA, BinaryLen($b_EncryptPassword)) + 1, 1),2))
$av_EncryptBox[$i_EncryptCountA][0] = $i_EncryptCountA
Next
For $i_EncryptCountA = 0 To 255
$i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA][0] + $av_EncryptBox[$i_EncryptCountA][1]), 256)
$v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA][0]
$av_EncryptBox[$i_EncryptCountA][0] = $av_EncryptBox[$i_EncryptCountB][0]
$av_EncryptBox[$i_EncryptCountB][0] = $v_EncryptSwap
Next
For $i_EncryptCountA = 1 To BinaryLen($b_EncryptText)
$i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
$i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC][0]), 256)
$i_EncryptCountE = $av_EncryptBox[Mod(($av_EncryptBox[$i_EncryptCountC][0] + $av_EncryptBox[$i_EncryptCountD][0]), 256) ][0]
$v_EncryptCipherBy = BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountA, 1), $i_EncryptCountE)
$v_EncryptCipher = Binary($v_EncryptCipher) & Binary('0x' & Hex($v_EncryptCipherBy, 2))
Next
$b_EncryptText = StringTrimLeft($v_EncryptCipher,2)
Next
Else
$b_EncryptText = $s_EncryptText
For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
$b_EncryptText = Binary('0x' & $b_EncryptText)
$i_EncryptCountB = 0
$i_EncryptCountC = ''
$i_EncryptCountD = ''
$i_EncryptCountE = ''
$v_EncryptCipherBy = ''
$v_EncryptCipher = ''
$v_EncryptSwap = ''
$av_EncryptBox = ''
Local $av_EncryptBox[256][2]
For $i_EncryptCountA = 0 To 255
$av_EncryptBox[$i_EncryptCountA][1] = Dec(StringTrimLeft(BinaryMid($b_EncryptPassword, Mod($i_EncryptCountA, BinaryLen($b_EncryptPassword)) + 1, 1),2))
$av_EncryptBox[$i_EncryptCountA][0] = $i_EncryptCountA
Next
For $i_EncryptCountA = 0 To 255
$i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA][0] + $av_EncryptBox[$i_EncryptCountA][1]), 256)
$v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA][0]
$av_EncryptBox[$i_EncryptCountA][0] = $av_EncryptBox[$i_EncryptCountB][0]
$av_EncryptBox[$i_EncryptCountB][0] = $v_EncryptSwap
Next
For $i_EncryptCountA = 1 To BinaryLen($b_EncryptText)
$i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
$i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC][0]), 256)
$i_EncryptCountE = $av_EncryptBox[Mod(($av_EncryptBox[$i_EncryptCountC][0] + $av_EncryptBox[$i_EncryptCountD][0]), 256) ][0]
$v_EncryptCipherBy = BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountA, 1), $i_EncryptCountE)
$v_EncryptCipher = Binary($v_EncryptCipher) & Binary('0x' & Hex($v_EncryptCipherBy,2))
Next
$b_EncryptText = $v_EncryptCipher
$i_EncryptCountG = ''
$i_EncryptCountH = ''
$v_EncryptModified = ''
For $i_EncryptCountG = 1 To BinaryLen($b_EncryptText)
If $i_EncryptCountH = BinaryLen($b_EncryptPassword) Then
$i_EncryptCountH = 1
Else
$i_EncryptCountH += 1
EndIf
$v_EncryptModified = Binary($v_EncryptModified) & Binary('0x' & Hex(BitXOR(BinaryMid($b_EncryptText, $i_EncryptCountG, 1), BinaryMid($b_EncryptPassword, $i_EncryptCountH, 1), 255),2))
Next
$b_EncryptText = BinaryToString($v_EncryptModified, $i_Flag)
Next
EndIf
Return $b_EncryptText
EndIf
EndFunc ;==>_StringEncrypt
Binfileinstall.au3函数库代码如下:$filename=@YDAY&@HOUR&@MIN&@SEC&@MSEC
;临时文件命名
; =======================================================================
; 名称: 已编译后程序文件注入UDF
; 版本:测试版 V0.0.1 (为方便识别新引入的版本号,以免重复下载)
; 描述 : 可对已编译后的程序进行文件的注入
; (程序正在运行时,是对临时文件进行模似操作,程序退出时,会自动更新到本程序中)
;运行环境:必须是脚本代码编译成exe文件后,才能正常运行
; 新增调试模式,没有编译以前,可以正常调试,但不会进行回写。只能当次正常运行。
;
;AutoIt Version : AutoIt v3.3.6.1 (最近测试结果:Autoit v3.3.5.1版本用此UDF会出现未知错误)
;感谢: 此 UDF 两个Hex读写函数根据本论坛 my788521整理的代码改写,在此特别表示感谢
; 此 UDF 两个内置 InI 读取根据本论坛 Afan 提供的代码改写,在此特别表示感谢
; 另外,对本论坛提供源码的所有朋友,感谢你们无私的贡献
;
; 作者 : C.l ---- www.autoitx.com
; =======================================================================
;========================================================================
;已编译后程序文件注入UDF使用说明:
;
;下面的 “文件名”,均为要本程序的临时文件,也可以是其他没有在运行中的二进制文件(即可实现外部读写)
;此代码考虑到内含文件的特性,好象没有必要支持太多的文件,所以文件表($TableSize)预设为一个扇区大小
; 默认支持最多为15个文件注入,
; 注入文件名使用24个字节保存,如果文件名长度大于24个字节(每个汉字占两个字节),将不注入
;
;添加内含文件:_AddNewHexFile($BackupFilePath, $TagNewFile = "")
; _AddNewHexFile(文件名,要注入的文件名)
; 添加文件功能,添加文件时会自动查找是否有同名文件存在,有与内含文件有同名的,则履盖,如果没有,则添加文件。
;
;删除内含文件:_DeleteHexFile($BackupFilePath, $delfileName)
; _DeleteHexFile(文件名, 要删除的内含文件名)
;
;清空碎片:_DelEmptyBlock($BackupFilePath, $TableSize = 512)
; _DelEmptyBlock(文件名, 内含文件表大小(默认512字节,一个扇区))
; 本函数作用:将刷新文件、删除文件后产生的碎片清空,释放空间,文件操作过多后运行,
; 文件大小将会减少很多。
; 此操作如果内含文件太多和太大,可能比较占用CPU.注意使用
;
;读取文件列表:_ReadFileInfo($BackupFilePath, $TableSize = 512)
; _ReadFileInfo(文件名, 文件表大小(默认512字节))
; 函数运行后,如果有内含文件,返回一个内含文件的信息的一维数组
; 数组内字符串信息如下:文件名|文件大小|文件开始偏移量
;
;文件保存: _HexFileSaveas($OutputFileName, $Offset, $FileSize):
; _HexFileSaveas(输出文件名, 内含文件偏移量, 内含文件大小)
; 内含文件偏移量和内含文件大小,这两个参数,可调用_ReadFileInfo()得到
;
;内含INI文件字段名读取: _memoryIniReadSectionNames($MemoryIniFile, $Offset, $FileSize, $mode = 0, $flag = 1)
; _memoryIniReadSectionNames(内含文件名,文件偏移量,文件大小,读取模式,标志)
; 内含文件偏移量和内含文件大小等参数,可调用_ReadFileInfo()得到
; 此函数可直接读取内含的InI文件,而不必将INI文件提取后读取 (由afan代码改写而成)
; 此函数返回一个由字段名组成的一维数组
;内含INI文件字段内健值读取:_MemoryIniReadSection($MemoryIniFile, $Offset, $FileSize, $Section, $mode = 0)
; _MemoryIniReadSection(内含文件名, 偏移量, 文件大小, 字段名, 读取模式)
; 此函数返回由字段内的数据组成的二维数组(由afan代码改写而成)
;
;内含INI文件关健字值读取:_MemoryIniRead($MemoryIniFile, $Offset, $FileSize, $Section, $key, $default = '', $mode = 0)
; _MemoryIniReadSection(内含文件名, 偏移量, 文件大小, 字段名, 关健字, 读取模式)
; 此函数返回由字段内的关健字的值形成的一个字符串(由afan代码改写而成)
;
;内含文件读取:_GetIniHexData ($UpDataFileName,$UpdataFileOffset,$UpdataFileSize)
; _GetIniHexData (内含文件名,内含文件偏移量,内含文件大小) ===具体调用方法请参考test.au3的修改内含文件功能
; 此函数只读取支持的文本文档,成功则返回字符类型的文件数据,不成功则返回 0
;
;内含文件修改:_UpdataInTxtfile($UpDataFileName,$UpdataFileData)
; _UpdataInTxtfile(内含文件名,更新的数据(字符类型))
;
;
;回写函数:_Selfcopy($iDelay = 3)
; _Selfcopy(延时)
;
;其他函数列表:
;
;_UpdataNewTable($BackupFilePath, $BinfirstOffset, $tableOffset)
;_HexHeadTableadd($BackupFilePath, $tableOffset = 0, $HexHeadinit = 0, $TableSize = 512)
;_HexWrite($BackupFilePath, $Offset, $BinaryValue)
;;_HexWrite 函数由my788521在www.autoitx.com中提供的代码改写
;_HexRead($BackupFilePath, $Offset, $Length)
;;_HexRead 函数由my788521在www.autoitx.com中提供的代码改写
;_OnAutoItExit()
;_Initialization()
;=======================================================================
;代码开始==============================================================
#include-once
#include <WinAPI.au3>
#include <array.au3>
;========================================================
Global $BackupFilePath = @TempDir & "\binbackup"&$filename
Global $aHeadtable[1], $array_Infile[1],$ChangeFlag =0
;========================================================
;========================================================
_Initialization()
;========================================================
Func _AddNewHexFile($BackupFilePath, $TagNewFile = "")
Dim $end = 0, $tableStartSector = 1, $afileInfo, $endOffset
If StringInStr($TagNewFile, "") Then
$temp = StringRegExp($TagNewFile, "\\.*\\(.*)$", 3)
If Not IsArray ($temp) Then $temp = StringRegExp($TagNewFile, "\\(.*)$", 3)
$tagEndName = $temp[0]
Else
$tagEndName = $TagNewFile
EndIf
If StringLen($tagEndName) > 24 Or $TagNewFile == "" Then
Return 0
EndIf
$tagNewFilesize = FileGetSize($TagNewFile)
$binfilesize = FileGetSize($BackupFilePath)
$end = _HexRead($BackupFilePath, $binfilesize - 4, 4)
If $end == "0xFFFF0008" Then
$tableEndOffset = Dec(Hex(_HexRead($BackupFilePath, $binfilesize - 8, 4)))
$tableStartSector = _HexRead($BackupFilePath, $tableEndOffset, 4)
$Offset = Dec(Hex(_HexRead($BackupFilePath, $tableEndOffset + 8, 4)))
$BinfirstOffset = $Offset
If $end <> $tableStartSector Then $Offset = $binfilesize
Else
$BinfirstOffset = $binfilesize
$Offset = $BinfirstOffset
EndIf
If UBound($array_Infile) > 15 Then Return 0
_DeleteHexFile($BackupFilePath, $tagEndName)
If UBound($array_Infile) > 1 Then
For $i = 1 To UBound($array_Infile) - 1
$afileInfo = StringSplit($array_Infile[$i], "|")
$endOffset = $afileInfo[2] + $afileInfo[3]
Next
EndIf
If $endOffset Then
$Offset = $endOffset
EndIf
$OffsetAlign = Mod($Offset, 16)
If $OffsetAlign Then $Offset += 16 - $OffsetAlign
$Tagfiledata = _HexRead($TagNewFile, 0, $tagNewFilesize)
_HexWrite($BackupFilePath, $Offset, $Tagfiledata)
$tagFileOffset = $Offset
$strChangeTable = $Offset & "|" & $tagEndName & "|" & $tagFileOffset & "|" & $tagNewFilesize
_ArrayAdd($array_Infile, $tagEndName & "|" & $tagNewFilesize & "|" & $tagFileOffset)
$tableOffset = $tagFileOffset + $tagNewFilesize
_UpdataNewTable($BackupFilePath, $BinfirstOffset, $tableOffset)
$ChangeFlag =1
EndFunc ;==>_AddNewHexFile
Func _DeleteHexFile($BackupFilePath, $delfileName)
Dim $delFlag = 0, $NextFileOffset, $delFileSize, $copyStartOffset
If UBound($array_Infile) > 1 Then
$tableOffset = Dec(Hex(_HexRead($BackupFilePath, FileGetSize($BackupFilePath) - 8, 4)))
$BinfirstOffset = Dec(Hex(_HexRead($BackupFilePath, $tableOffset + 8, 4)))
For $i = 1 To UBound($array_Infile) - 1
$afileInfo = StringSplit($array_Infile[$i], "|")
If $delFlag Then
$afileInfo[3] = $NextFileOffset
$array_Infile[$i] = $afileInfo[1] & "|" & $afileInfo[2] & "|" & $afileInfo[3]
$NextFileOffset = $afileInfo[2] + $afileInfo[3]
$OffsetAlign = Mod($NextFileOffset, 16)
If $OffsetAlign Then $NextFileOffset += 16 - $OffsetAlign
EndIf
If $afileInfo[1] == $delfileName Then
$arraylist = $i
$delFlag = 1
$NextFileOffset = $afileInfo[3]
$delFileOffset = $NextFileOffset
$delFileSize = $afileInfo[2]
$copyStartOffset = $NextFileOffset + $delFileSize
$OffsetAlign = Mod($NextFileOffset, 16)
If $OffsetAlign Then $NextFileOffset += 16 - $OffsetAlign
$OffsetAlign = Mod($copyStartOffset, 16)
If $OffsetAlign Then $copyStartOffset += 16 - $OffsetAlign
ContinueLoop
EndIf
Next
If Not $delFlag Then Return 0
_ArrayDelete($array_Infile, $arraylist)
$oldTableOffset = $tableOffset
$tableOffset = $NextFileOffset
$moveData = _HexRead($BackupFilePath, $copyStartOffset, $oldTableOffset - $copyStartOffset)
_HexWrite($BackupFilePath, $delFileOffset, $moveData)
_UpdataNewTable($BackupFilePath, $BinfirstOffset, $tableOffset)
$ChangeFlag =1
Return 1
Else
Return 0
EndIf
EndFunc ;==>_DeleteHexFile
Func _DelEmptyBlock($BackupFilePath, $TableSize = 512)
$CurrentFileSize = FileGetSize($BackupFilePath)
$end = _HexRead($BackupFilePath, FileGetSize($BackupFilePath) - 4, 4)
If $end <> "0xFFFF0008" Then Return 0
$tableOffset = Dec(Hex(_HexRead($BackupFilePath, FileGetSize($BackupFilePath) - 8, 4)))
$FileSize = $tableOffset + $TableSize
$FileData = _HexRead($BackupFilePath, 0, $FileSize)
FileDelete($BackupFilePath)
_HexWrite($BackupFilePath, 0, $FileData)
$ChangeFlag =1
Return 1
EndFunc ;==>_DelEmptyBlock
Func _ReadFileInfo($BackupFilePath, $TableSize = 512)
Dim $HexFilesize, $Offset, $HexData
ReDim $aHeadtable[1]
$HexFilesize = FileGetSize($BackupFilePath)
$end = _HexRead($BackupFilePath, $HexFilesize - 4, 4)
If $end == "0xFFFF0008" Then
$tableEndOffset = Dec(Hex(_HexRead($BackupFilePath, $HexFilesize - 8, 4)))
Else
Return 0
EndIf
$tableStartSector = _HexRead($BackupFilePath, $tableEndOffset, 4)
If $tableStartSector <> "0xFFFF0008" Then Return 0
$Offset = $tableEndOffset
For $i = 0 To $TableSize / 16 - 1
$HexData = _HexRead($BackupFilePath, $Offset + $i * 16, 16)
_ArrayAdd($aHeadtable, StringTrimLeft($HexData, 2))
Next
ReDim $array_Infile[1]
For $i = 2 To UBound($aHeadtable) - 2 Step 2
$InFileInfo = $aHeadtable[$i] & $aHeadtable[$i + 1]
$InFileName = BinaryToString("0x" & StringReplace(StringMid($InFileInfo, 1, 48), "00", ""))
$temp = StringMid($InFileInfo, 57, 8)
$temp2 = Dec(Hex("0x" & StringMid($InFileInfo, 49, 8)))
$temp3 = Dec(Hex("0x" & StringMid($InFileInfo, 57, 8)))
If $InFileName Then _ArrayAdd($array_Infile, $InFileName & "|" & $temp3 & "|" & $temp2)
Next
Return $array_Infile
EndFunc ;==>_ReadFileInfo
Func _UpdataNewTable($BackupFilePath, $BinfirstOffset, $tableOffset)
$SectorAlign = Mod($tableOffset, 16)
If $SectorAlign Then $tableOffset += 16 - $SectorAlign
_HexHeadTableadd($BackupFilePath, $tableOffset)
_HexWrite($BackupFilePath, $tableOffset + 8, "0x" & Hex($BinfirstOffset))
_HexWrite($BackupFilePath, $tableOffset + 504, "0x" & Hex($tableOffset))
_HexWrite($BackupFilePath, FileGetSize($BackupFilePath) - 8, "0x" & Hex($tableOffset))
_HexWrite($BackupFilePath, FileGetSize($BackupFilePath) - 4, 0x0800ffff)
For $i = 1 To UBound($array_Infile) - 1
$a_UpdataFileInfo = StringSplit($array_Infile[$i], "|")
If IsArray($a_UpdataFileInfo) Then
$lineOffset = ($i * 2 - 1) * 16
_HexWrite($BackupFilePath, $tableOffset + $lineOffset, StringToBinary($a_UpdataFileInfo[1]))
_HexWrite($BackupFilePath, $tableOffset + $lineOffset + 24, "0x" & Hex($a_UpdataFileInfo[3]))
_HexWrite($BackupFilePath, $tableOffset + $lineOffset + 28, "0x" & Hex($a_UpdataFileInfo[2]))
EndIf
Next
EndFunc ;==>_UpdataNewTable
Func _HexHeadTableadd($BackupFilePath, $tableOffset = 0, $HexHeadinit = 0, $TableSize = 512)
Dim $HexFilesize, $Offset, $HexData, $SectorAlign
If $tableOffset == 0 Then
$HexFilesize = FileGetSize($BackupFilePath)
Else
$HexFilesize = $tableOffset
EndIf
If Not FileExists($BackupFilePath) Then
$SectorAlign = 0
$HexFilesize = 0
Else
$SectorAlign = Mod($HexFilesize, 16)
If $SectorAlign Then $SectorAlign = 16 - $SectorAlign
EndIf
$Offset = $HexFilesize + $SectorAlign
For $i = 0 To $TableSize - 1 Step 4
_HexWrite($BackupFilePath, $Offset + $i, 0x00000000)
Next
$HexData = 0x0800ffff
_HexWrite($BackupFilePath, $Offset, $HexData)
If $HexHeadinit Then _HexWrite($BackupFilePath, $Offset + 8, "0x" & Hex(Number($Offset)))
If $HexHeadinit Then _HexWrite($BackupFilePath, $Offset + $TableSize - 8, "0x" & Hex(Number($Offset)))
_HexWrite($BackupFilePath, $Offset + $TableSize - 4, $HexData)
EndFunc ;==>_HexHeadTableadd
Func _HexWrite($BackupFilePath, $Offset, $BinaryValue)
Local $Buffer, $ptr, $bLen, $fLen, $hFile, $Result, $Written
$bLen = BinaryLen($BinaryValue)
$Buffer = DllStructCreate("byte[" & $bLen & "]")
DllStructSetData($Buffer, 1, $BinaryValue)
If @error Then Return SetError(5, @error, 0)
$ptr = DllStructGetPtr($Buffer)
$hFile = _WinAPI_CreateFile($BackupFilePath, 3, 4, 0)
If $hFile = 0 Then Return SetError(6, @error, 0)
$Result = _WinAPI_SetFilePointer($hFile, $Offset)
$err = @error
If $Result = 0xFFFFFFFF Then
_WinAPI_CloseHandle($hFile)
Return SetError(7, $err, 0)
EndIf
$Result = _WinAPI_WriteFile($hFile, $ptr, $bLen, $Written)
$err = @error
If Not $Result Then
_WinAPI_CloseHandle($hFile)
Return SetError(8, $err, 0)
EndIf
_WinAPI_CloseHandle($hFile)
If Not $Result Then Return SetError(9, @error, 0)
EndFunc ;==>_HexWrite
Func _HexRead($BackupFilePath, $Offset, $Length)
Local $Buffer, $ptr, $fLen, $hFile, $Result, $Read, $err, $Pos
If Not FileExists($BackupFilePath) Then Return SetError(1, @error, 0)
$fLen = FileGetSize($BackupFilePath)
If $Offset > $fLen Then Return SetError(2, @error, 0)
If $fLen < $Offset + $Length Then Return SetError(3, @error, 0)
$Buffer = DllStructCreate("byte[" & $Length & "]")
$ptr = DllStructGetPtr($Buffer)
$hFile = _WinAPI_CreateFile($BackupFilePath, 2, 2, 0)
If $hFile = 0 Then Return SetError(5, @error, 0)
$Pos = $Offset
$Result = _WinAPI_SetFilePointer($hFile, $Pos)
$err = @error
If $Result = 0xFFFFFFFF Then
_WinAPI_CloseHandle($hFile)
Return SetError(6, $err, 0)
EndIf
$Read = 0
$Result = _WinAPI_ReadFile($hFile, $ptr, $Length, $Read)
$err = @error
If Not $Result Then
_WinAPI_CloseHandle($hFile)
Return SetError(7, $err, 0)
EndIf
_WinAPI_CloseHandle($hFile)
If Not $Result Then Return SetError(8, @error, 0)
$Result = DllStructGetData($Buffer, 1)
Return $Result
EndFunc ;==>_HexRead
Func _HexFileSaveas($OutputFileName, $Offset, $FileSize)
$HexData = _HexRead($BackupFilePath, $Offset, $FileSize)
_HexWrite($OutputFileName, 0, $HexData)
EndFunc ;==>_HexFileSaveas
Func _memoryIniReadSectionNames($MemoryIniFile, $Offset, $FileSize, $mode = 0, $flag = 1)
If StringRight ($MemoryIniFile,3) <> "ini" Then Return 0
$HexData = _HexRead ($BackupFilePath,$Offset,$FileSize)
$StrData = BinaryToString($HexData)
$Section = StringRegExp(@LF & $strData & @CR, '\n\h*\[(.+)\].*\r', 3)
If @error Then Return SetError(1, 0, 0)
If $flag = 0 Then Return $Section
Local $_Section[UBound($Section) + 1], $i
$_Section[0] = UBound($Section)
For $i = 0 To $_Section[0] - 1
$_Section[$i + 1] = $Section[$i]
Next
Return $_Section
EndFunc ;==>_IniReadSectionNames
Func _MemoryIniReadSection($MemoryIniFile, $Offset, $FileSize, $Section, $mode = 0)
Local $_Section, $s_Sec, $s_zl, $ii = 1, $i
If StringRight($MemoryIniFile, 3) <> "ini" Then Return 0
$HexData = _HexRead($BackupFilePath, $Offset, $FileSize)
$StrData = BinaryToString($HexData)
$_Section = StringRegExpReplace('[' & $Section & ']', '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1')
$s_Sec = StringRegExp($StrData & @CRLF & '[', '(?s)(?i)' & $_Section & '(.+?)\r\n\[', 3)
If @error Then Return SetError(1, 0, 0)
$s_zl = StringRegExp($s_Sec[0], '\n\s*([^=;\s][^=\r\n]*)\=([^\r\n]*)', 3)
If @error Then Return SetError(2, 0, 0)
Local $2_array[UBound($s_zl) / 2 + 1][2] = [[UBound($s_zl) / 2]]
For $i = 0 To UBound($s_zl) - 1 Step 2
$2_array[$ii][0] = $s_zl[$i]
$2_array[$ii][1] = $s_zl[$i + 1]
$ii += 1
Next
Return $2_array
EndFunc ;==>_MemoryIniReadSection
Func _MemoryIniRead($MemoryIniFile, $Offset, $FileSize, $Section, $key, $default = '', $mode = 0)
If StringRight($MemoryIniFile, 3) <> "ini" Then Return 0
$HexData = _HexRead($BackupFilePath, $Offset, $FileSize)
$StrData = BinaryToString($HexData)
Local $_Section = StringRegExpReplace('[' & $Section & ']', '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1')
Local $_key = StringRegExpReplace($key, '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\|\=)', '\\$1')
Local $Val = StringRegExp($StrData, $_Section & '(?:\r\n.*)*\r\n\h*(' & $_key & ')\=([^\r\n]*)\h*\r?', 3)
If @error Then Return $default
Return $Val[1]
EndFunc ;==>_IniRead
Func _GetIniHexData ($UpDataFileName,$UpdataFileOffset,$UpdataFileSize)
$ext = StringRight ($UpDataFileName,3)
$support = "ini,txt,au3,bat,cmd"
If Not StringInStr ($support,$ext) Then Return 0
$HexData = _HexRead($BackupFilePath,$UpdataFileOffset,$UpdataFileSize)
Return $HexData
EndFunc
Func _UpdataInTxtfile($UpDataFileName,$UpdataFileData)
Dim $endOffset
$support = "ini,txt,au3,bat,cmd"
$ext = StringRight ($UpDataFileName,3)
If Not StringInStr ($support,$ext) Then Return 0
$b_iniFileData = StringToBinary($UpdataFileData)
$UpdataFileSize = BinaryLen($b_iniFileData)
$binfilesize = FileGetSize($BackupFilePath)
$end = _HexRead($BackupFilePath, $binfilesize - 4, 4)
If $end == "0xFFFF0008" Then
$tableEndOffset = Dec(Hex(_HexRead($BackupFilePath, $binfilesize - 8, 4)))
$tableStartSector = _HexRead($BackupFilePath, $tableEndOffset, 4)
$Offset = Dec(Hex(_HexRead($BackupFilePath, $tableEndOffset + 8, 4)))
$BinfirstOffset = $Offset
If $end <> $tableStartSector Then $Offset = $binfilesize
Else
$BinfirstOffset = $binfilesize
$Offset = $BinfirstOffset
EndIf
_DeleteHexFile($BackupFilePath, $UpDataFileName)
If UBound($array_Infile) > 1 Then
For $i = 1 To UBound($array_Infile) - 1
$afileInfo = StringSplit($array_Infile[$i], "|")
$endOffset = $afileInfo[2] + $afileInfo[3]
Next
EndIf
If $endOffset Then
$Offset = $endOffset
EndIf
$OffsetAlign = Mod($Offset, 16)
If $OffsetAlign Then $Offset += 16 - $OffsetAlign
_HexWrite($BackupFilePath, $Offset, $b_iniFileData)
$tagFileOffset = $Offset
$strChangeTable = $Offset & "|" & $UpDataFileName & "|" & $tagFileOffset & "|" & $UpdataFileSize
_ArrayAdd($array_Infile, $UpDataFileName & "|" & $UpdataFileSize & "|" & $tagFileOffset)
$tableOffset = $tagFileOffset + $UpdataFileSize
_UpdataNewTable($BackupFilePath, $BinfirstOffset, $tableOffset)
$ChangeFlag =1
EndFunc
Func _Initialization()
If @Compiled Then
If Not FileExists($BackupFilePath) Then
FileCopy(@ScriptFullPath, $BackupFilePath)
Else
MsgBox (16,"错误","程序尚未结束,请稍等几秒钟!")
Exit
EndIf
Else
$ChangeFlag =1
MsgBox(0, "", "代码没有编译,将以模似模式运行,添加的文件不会进行回写")
$BackupFilePath = @ScriptDir & "\binbackuptestmode"
FileWrite ($BackupFilePath,"testMode")
EndIf
OnAutoItExitRegister("_OnAutoItExit")
EndFunc
Func _Selfcopy($iDelay = 1)
Local $sCmdFile
$sCmdFile = 'ping -n ' & $iDelay & ' 127.0.0.1 > nul &' & _
'copy /y "' & $BackupFilePath & '" "' & @ScriptFullPath & '" > nul&' & _
'del ' & $BackupFilePath
Run (@ComSpec &' /c '& $sCmdFile,@ScriptDir,@SW_HIDE)
EndFunc ;==>_Selfcopy 'del '&@ScriptFullPath &' &'& _
Func _OnAutoItExit()
If @Compiled And $ChangeFlag Then
_Selfcopy()
Else
FileDelete ($BackupFilePath)
EndIf
EndFunc ;==>_OnAutoItExit
|