#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.6.1 Author: sprasad Script Function: SFX module for e2open b2bc #ce ---------------------------------------------------------------------------- #include "err.au3" #include #Include ; Need to use Enum, Fix later ; Zlib related constants Const $ZIP_GP_FLAGS = 0, $ZIP_COMPRESS_METHOD = 1, $ZIP_CRC32 = 2 Const $ZIP_COMPRESSED_SIZE = 3, $ZIP_UNCOMPRESSED_SIZE = 4, $ZIP_FILE_NAME = 5 Const $ZIP_ENTRY_ARRAY_SIZE = 6 Const $ZIP_BUFFER = 0, $ZIP_ENTRY_COUNT = 1, $ZIP_CD_OFFSET = 2 Const $ZIP_ZLIB_HANDLE = 3, $ZIP_ZLIB_VERSION = 4, $ZIP_OUT_BUFFER = 5 Const $ZIP_CD_INFO = 6, $ZIP_ZLIB_STREAM_INFO = 7 Const $ZIP_INFO_ARRAY_SIZE = 8 ; E2open Binary Add-On related contstants Const $END_MARKER = "-- E2open End Marker @0000000000@0000@ --" Const $END_MARKER_START = "-- E2open End Marker @" Const $END_MARKER_END = "@ --" Const $START_MARKER = "-- E2open Start Marker --" Const $FILE_LINE = "File: " Const $SIZE_LINE = "Size: " ; ; Extract e2open binary addons, download specific components ; Func _ExtractorOpen($srcFile) Local $buffer, $blockSize, $line, $fileName, $fileSize $fileSize = FileGetSize($srcFile) $file = FileOpen($srcFile, 16) If $file == -1 Then Return "EXOPEN" FileSetPos($file, -1 * (StringLen($END_MARKER) + 1), 2) $buffer = FileRead($file) $buffer = StringStripWS(BinaryToString($buffer), 2) If StringRegExp($buffer, $END_MARKER_START & "[0-9]+@[0-9]+" & _ $END_MARKER_END) == 0 Then Return "EXENDMRK" $blockSize = StringMid($buffer, StringLen($END_MARKER_START) + 1, 10) $entryCount = StringMid($buffer, StringLen($END_MARKER_START) + 12, 4) FileSetPos($file, $fileSize - $blockSize, 0) $buffer = FileRead($file, StringLen($START_MARKER) + 2) $buffer = StringStripWS(BinaryToString($buffer), 3) If StringCompare($buffer, $START_MARKER) <> 0 Then Return "EXSTRTMRK" Local $extractInfo[3] $extractInfo[0] = $file $extractInfo[1] = $entryCount return $extractInfo EndFunc Func _ExtractorGetEntryCount(ByRef $extractInfo) Return $extractInfo[1] EndFunc Func _ExtractEntry(ByRef $extractInfo, $destinationDir) Local $line, $file = $extractInfo[0] $line = FileReadLine($file) If StringInStr($line, $END_MARKER_START) == 1 Then Return "EXENDMRK" If StringInStr($line, $FILE_LINE) <> 1 Then Return "EXFLLN" $fileName = StringRight($line, StringLen($line) - StringLen($FILE_LINE)) $fileName = StringReplace($fileName, "/", "\") $line = FileReadLine($file) If StringInStr($line, $SIZE_LINE) <> 1 Then Return "EXSZLN" $fileSize = StringRight($line, StringLen($line) - StringLen($SIZE_LINE)) $buffer = FileRead($file, $fileSize) $path = $destinationDir & "\" & $fileName __WriteToFile($path, $buffer) FileRead($file, 1) Return "OK" EndFunc Func _ExtractorClose(ByRef $extractInfo) FileClose($extractInfo[0]) $extractInfo[0] = 0 $extractInfo[1] = 0 EndFunc Func __WriteToFile(ByRef $path, ByRef $contents) Local $file = FileOpen($path, 26) FileWrite($file, $contents) FileClose($file) EndFunc Func _ZipOpen($fileName) Local $outSize, $headerIndex = 0 $fileSize = FileGetSize($fileName) $buffer = DllStructCreate("byte[" & $fileSize & "]") $bufferPtr = DllStructGetPtr($buffer) $fileHndl = _WinAPI_CreateFile($fileName, 2, 2) If $fileHndl == 0 Then Return "ZOPFL" $retValue = _WinAPI_ReadFile($fileHndl, $bufferPtr, $fileSize, $outSize) If $retValue == False Then Return "ZRDFL" _WinAPI_CloseHandle($fileHndl) $intHolder = DllStructCreate("uint") $intHolderPtr = DllStructGetPtr($intHolder) ; Read through the data from the end looking for the ; Zip CD end record signature PK0506 For $pos = $fileSize - 3 To 1 Step -1 _MemMoveMemory($bufferPtr + $pos, $intHolderPtr, 4) If DllStructGetData($intHolder, 1) == 0x06054B50 Then $headerIndex = $pos ExitLoop EndIf Next ; Did we find the CD end record ? If $headerIndex == 0 Then Return "ZHINF" ; Get the number of entries in the CD table $shortHolder = DllStructCreate("ushort") _MemMoveMemory($bufferPtr + $headerIndex + 10, _ DllStructGetPtr($shortHolder), 2) $entryCount = DllStructGetData($shortHolder, 1) ; Find the start of the CD table, offset from the start of the file _MemMoveMemory($bufferPtr + $headerIndex + 16, $intHolderPtr, 4) $cdOffset = DllStructGetData($intHolder, 1) If $cdOffset > $fileSize Or $cdOffset > $headerIndex Then Return "ZCDOFER" EndIf Local $zipInfo[$ZIP_INFO_ARRAY_SIZE] $zipInfo[$ZIP_BUFFER] = $buffer $zipInfo[$ZIP_ENTRY_COUNT] = $entryCount $zipInfo[$ZIP_CD_OFFSET] = $cdOffset Local $zlibDll = DllOpen("zlib1.dll") $zipInfo[$ZIP_ZLIB_HANDLE] = $zlibDll Local $retValue = DllCall($zlibDll, "str:cdecl", "zlibVersion") $zipInfo[$ZIP_ZLIB_VERSION] = $retValue[0] ; Allocate a cool 50Meg as Inflate buffer $zipInfo[$ZIP_OUT_BUFFER] = DllStructCreate( _ "byte[" & 50 * 1024 * 1024 & "]") $cdInfoHolder = DllStructCreate("align 1; uint sig; ushort createVer; " & _ "ushort minVer; ushort gpf; ushort method; ushort lmt; " & _ "ushort lmd; uint crc; uint csize; uint ucsize; ushort fnl; " & _ "ushort efl; ushort fcl; ushort dn; ushort ifa; uint efa; uint lfho"); $zipInfo[$ZIP_CD_INFO] = $cdInfoHolder $zStreamHolder = DllStructCreate("ptr next_in; uint avail_in; " & _ "ulong total_in; ptr next_out; ulong avail_out; uint total_out; " & _ "ptr msg; ptr internal; ptr ig1; ptr ig2; ptr ig3; " & _ "int data_type; ulong adler; ulong rcvd") $zipInfo[$ZIP_ZLIB_STREAM_INFO] = $zStreamHolder Return $zipInfo EndFunc ; ; Primarily for us to delete the tmp dir created, IF the dll is open ; we cannot delete it :-) ; Func _ZipClose(ByRef $zipHandle) DllClose($zipHandle[$ZIP_ZLIB_HANDLE]) $zipHandle[$ZIP_ZLIB_HANDLE] = 0 $zipHandle[$ZIP_BUFFER] = 0 $zipHandle[$ZIP_OUT_BUFFER] = 0 $zipHandle[$ZIP_CD_INFO] = 0 $zipHandle[$ZIP_ZLIB_STREAM_INFO] = 0 EndFunc Func _ZipExtract(ByRef $extractDir, ByRef $zipHandle) Local $retValue = False Local $buffer = $zipHandle[$ZIP_BUFFER] Local $recordIndex = $zipHandle[$ZIP_CD_OFFSET] Local $cdInfo = $zipHandle[$ZIP_CD_INFO] Local $bufferPtr = DllStructGetPtr($buffer) _MemMoveMemory($bufferPtr + $recordIndex, DllStructGetPtr($cdInfo), _ DllStructGetSize($cdInfo)) $fileNameLength = DllStructGetData($cdInfo, "fnl") $extraFieldLength = DllStructGetData($cdInfo, "efl") $fileCommentLength = DllStructGetData($cdInfo, "fcl") $fileNameHolder = DllStructCreate("byte[" & $fileNameLength & "]") _MemMoveMemory($bufferPtr + $recordIndex + DllStructGetSize($cdInfo), _ DllStructGetPtr($fileNameHolder), $fileNameLength) $fileName = BinaryToString(DllStructGetData($fileNameHolder, 1)) $fileName = $extractDir & "\" & StringReplace($fileName, "/", "\") If StringRight($fileName, 1) == "\" Then $retValue = DirCreate($fileName) If $retValue == 0 Then Return "ZDRCR" $retValue = "OK" Else $dataOffset = DllStructGetData($cdInfo, "lfho") + 30 + _ $fileNameLength + $extraFieldLength $retValue = __ZipExtractEntry($zipHandle, $fileName, $dataOffset) EndIf $zipHandle[$ZIP_CD_OFFSET] += DllStructGetSize($cdInfo) + _ $fileNameLength + $extraFieldLength + $fileCommentLength Return $retValue EndFunc Func _ZipGetEntryCount(ByRef $zipHanlde) Return $zipHandle[$ZIP_ENTRY_COUNT] EndFunc Func __ZipExtractEntry(ByRef $zipHandle, ByRef $fileName, $dataOffset) Local $buffer = $zipHandle[$ZIP_BUFFER] Local $retValue = False Local $cdInfo = $zipHandle[$ZIP_CD_INFO] Local $bufferPtr = DllStructGetPtr($buffer) $method = DllStructGetData($cdInfo, "method") $compressedSize = DllStructGetData($cdInfo, "csize") $uncompressedSize =DllStructGetData($cdInfo, "ucsize") $dataPtr = $bufferPtr + $dataOffset Switch $method Case 0 $retValue = __WinWriteToFile($fileName, $dataPtr, $compressedSize) Case 8 $retValue = __ZipCallDllUncompress($zipHandle, $dataPtr, _ $compressedSize, $uncompressedSize) If $retValue <> "OK" Then Return $retValue Local $ucData = $zipHandle[$ZIP_OUT_BUFFER] $retValue = __WinWriteToFile($fileName, DllStructGetPtr($ucData), _ $uncompressedSize) EndSwitch Return $retValue EndFunc Func __ZipCallDllUncompress(ByRef $zipHandle, ByRef $inBufferPtr, _ $compressedSize, $uncompressedSize) Local $outBufferArr, $zStreamArr, $retValue $outBufferArr = $zipHandle[$ZIP_OUT_BUFFER] $zStreamPtr = $zipHandle[$ZIP_ZLIB_STREAM_INFO] DllStructSetData($zStreamPtr, "next_in", $inBufferPtr) DllStructSetData($zStreamPtr, "avail_in", $compressedSize) DllStructSetData($zStreamPtr, "next_out", DllStructGetPtr($outBufferArr)) DllStructSetData($zStreamPtr, "avail_out", $uncompressedSize) $retValue = DllCall($zipHandle[$ZIP_ZLIB_HANDLE], "int:cdecl", _ "inflateInit2_", "ptr", DllStructGetPtr($zStreamPtr), "int", _ -15, "str", $zipHandle[$ZIP_ZLIB_VERSION], "int", _ DllStructGetSize($zStreamPtr)) If $retValue[0] <> 0 Then Return "ZINITFL" $retValue = DllCall($zipHandle[$ZIP_ZLIB_HANDLE], "int:cdecl", _ "inflate", "ptr", DllStructGetPtr($zStreamPtr), "uint", 0) If $retValue[0] <> 1 Then Return "ZUNCFL" DllCall($zipHandle[$ZIP_ZLIB_HANDLE], "int:cdecl", _ "inflateEnd", "ptr", DllStructGetPtr($zStreamPtr)) Return "OK" EndFunc Func __WinWriteToFile($fileName, $ptr, $size) Local $file, $outSize $file = _WinAPI_CreateFile($fileName, 1, 4) If $file == 0 Then Return "ZFCRFL" $retValue = _WinAPI_WriteFile($file, $ptr, $size, $outSize) If $retValue == False Then Return "ZFWRFL" _WinAPI_CloseHandle($file) Return "OK" EndFunc Func __ShowData(ByRef $zipHandle, $offset, $size) Local $buffer = $zipHandle[$ZIP_BUFFER] Local $bufferPtr = DllStructGetPtr($buffer) $dataHolder = DllStructCreate("byte[" & $size & "]") _MemMoveMemory($bufferPtr + $size, DllStructGetPtr($dataHolder), $size) $data = String(DllStructGetData($dataHolder, 1)) MsgBox(0, "Data", $data) EndFunc