------------------------------ -- -- -- Initialize FileEx Plugin -- -- -- ------------------------------ FileEx = {} _FileBank_ = {} function Dec2Hex(nValue) if type(nValue) == "string" then nValue = String.ToNumber(nValue); end nHexVal = string.format("%08X", nValue); sHexVal = tostring(nHexVal); return sHexVal; end FileEx.Open = function(File) _File = Memory.Allocate(String.Length(File)) Memory.PutString(_File, File, -1) handle = tonumber(DLL.CallFunction("kernel32.dll", "CreateFileA", _File..", 3221225472, 3, 0, 4, 128, 0", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) if handle == -1 then _tblErrorMessages[8080] = "Failed to open clipboard." return nil else _FileBank_[handle] = File return handle end end FileEx.Close = function(Handle) local ret ret = tonumber(DLL.CallFunction("kernel32.dll", "CloseHandle", Handle, DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) if (ret == 1) and _FileBank_[Handle] then Table.Remove(_FileBank_, Handle) _tblErrorMessages[8080] = "Failed to Open File." return true else _tblErrorMessages[8081] = "Failed to Close File." return false end end FileEx.Create = function(File, Size) _File = Memory.Allocate(String.Length(File)+1) _Size = Memory.Allocate(1024) local handle Memory.PutString(_File, File, -1) Memory.PutInt64(_Size, Size) handle = tonumber(DLL.CallFunction("kernel32.dll", "CreateFileA", _File..", 3221225472, 3, 0, 2, 128, 0", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) if (handle ~= -1) then if tonumber(DLL.CallFunction("kernel32.dll", "SetFilePointer", handle..", "..Size..", "..(_Size + 4)..", 0", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) then if tonumber(DLL.CallFunction("kernel32.dll", "SetEndOfFile", handle, DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) then if tonumber(DLL.CallFunction("kernel32.dll", "CloseHandle", handle, DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) then return true else _tblErrorMessages[8082] = "Failed to Create File." return false end else _tblErrorMessages[8082] = "Failed to Create File." return false end else _tblErrorMessages[8082] = "Failed to Create File." return false end else _tblErrorMessages[8082] = "Failed to Create File." return false end end FileEx.Seek = function(Handle, Position) if type(Position) == "string" then Position = Math.HexToNumber(Position) end if not tonumber(DLL.CallFunction("kernel32.dll", "SetFilePointer", Handle..", "..Position..", 0, 0", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) then _tblErrorMessages[8083] = "Failed to Seek File." return false else return true end end FileEx.ReadByte = function(Handle) buf = Memory.Allocate(512) count = Memory.Allocate(512) if tonumber(DLL.CallFunction("kernel32.dll", "ReadFile", Handle..", "..buf..", 512, "..count..", 0", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) then byte = Memory.GetByte(buf) Memory.Free(buf) Memory.Free(count) byte = Dec2Hex(byte) if String.Length(byte) < 2 then return "0"..byte elseif String.Length(byte) > 2 then return String.Right(byte, 2) else return byte end else Memory.Free(buf) Memory.Free(count) _tblErrorMessages[8084] = "Failed to Read One Byte From File." return "" end end FileEx.WriteByte = function(Handle, Byte) buf = Memory.Allocate(512) count = Memory.Allocate(512) if type(Byte) == "string" then Byte = Math.HexToNumber(Byte) end Memory.PutByte(buf, Byte) if tonumber(DLL.CallFunction("kernel32.dll", "WriteFile", Handle..", "..buf..", 1, "..count..", 0", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) then Memory.Free(buf) Memory.Free(count) return true else Memory.Free(buf) Memory.Free(count) _tblErrorMessages[8085] = "Failed to Get One Byte From File." return false end end FileEx.FindByte = function(Handle, Byte, StartPosition, EndPosition) buf = Memory.Allocate(512) count = Memory.Allocate(512) local Out = {} local num = 0 if type(Byte) == "number" then Byte = Dec2Hex(Byte) end if String.Length(Byte) < 2 then Byte = "0"..Byte else Byte = String.Right(Byte, 2) end if type(StartPosition) == "string" then StartPosition = Math.HexToNumber(StartPosition) end if type(EndPosition) == "string" then EndPosition = Math.HexToNumber(EndPosition) end if EndPosition == -1 then EndPosition = File.GetSize(_FileBank_[Handle])-1 end if StartPosition == EndPosition and StartPosition == 0 then EndPosition = File.GetSize(_FileBank_[Handle])-1 end for Pos = StartPosition, EndPosition do DLL.CallFunction("kernel32.dll", "SetFilePointer", Handle..", "..Pos..", 0, 0", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL) DLL.CallFunction("kernel32.dll", "ReadFile", Handle..", "..buf..", 512, "..count..", 0", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL) ret = Dec2Hex(Memory.GetByte(buf)) if String.Length(ret) < 2 then ret = "0"..ret else ret = String.Right(ret, 2) end if ret == Byte then num = num + 1 Out[num] = Dec2Hex(Pos+1) end end if num == 0 then return nil else return Out end end FileEx.ReadString = function(Handle, Size) if Size == -1 then Size = File.GetSize(_FileBank_[Handle])+1 end buf = Memory.Allocate(Size) count = Memory.Allocate(512) local ret if tonumber(DLL.CallFunction("kernel32.dll", "ReadFile", Handle..", "..buf..", "..(Size-1)..", "..count..", 0", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) then Size = Memory.GetInt64(count) ret = Memory.GetString(buf, Size) Memory.Free(buf) Memory.Free(count) return ret else Memory.Free(buf) Memory.Free(count) return "" end end FileEx.WriteString = function(Handle, Text) buf = Memory.Allocate(String.Length(Text)+1) count = Memory.Allocate(512) Memory.PutString(buf, Text, -1) if tonumber(DLL.CallFunction("kernel32.dll", "WriteFile", Handle..", "..buf..", "..String.Length(Text)..", "..count..", 0", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) then Memory.Free(buf) Memory.Free(count) return true else Memory.Free(buf) Memory.Free(count) return false end end