Sign in to follow this  
Followers 0
wolf9228

MEM_INSERT

1 post in this topic

Mem.au3

Func MEM_INSERT($PTR,$PTR_SIZE _
                ,$INSERT_PTR,$INSERT_PTR_SIZE _
                ,$FROM_INSERT_POINT _
                ,$TO_INSERT_POINT )

; $FROM_INSERT_POINT [ BYTE_Remove ] $TO_INSERT_POINT
;BYTE_Remove = [$TO_INSERT_POINT - $FROM_INSERT_POINT]
; IF $TO_INSERT_POINT = $FROM_INSERT_POINT ==> BYTE_Remove = 0

;IF $TO_INSERT_POINT > ($FROM_INSERT_POINT + $INSERT_PTR_SIZE) ;
;   $TO_INSERT_POINT = ($FROM_INSERT_POINT + $INSERT_PTR_SIZE) ;
;==============================================================;

;IF $FROM_INSERT_POINT  > $PTR_SIZE ;
;   $FROM_INSERT_POINT = $PTR_SIZE  ;
;===================================;

Local $MEM_STRUCT , $MEM_PTR , $ALLOC , $NEWSIZE , $MEM_ARRAY[2]
$ALLOC = VirtualAlloc(0, 1, 0x1000, 0x40)
$MEM_STRUCT = DllStructCreate("BYTE[1]",$ALLOC)
$MEM_PTR = DllStructGetPtr($MEM_STRUCT)
Select
Case $PTR_SIZE >= $TO_INSERT_POINT
MoveMemory($PTR,$MEM_PTR,$FROM_INSERT_POINT)
MoveMemory($INSERT_PTR,$MEM_PTR + $FROM_INSERT_POINT, $INSERT_PTR_SIZE)
MoveMemory($PTR + $TO_INSERT_POINT, _
$MEM_PTR + ($FROM_INSERT_POINT + $INSERT_PTR_SIZE), ($PTR_SIZE - $TO_INSERT_POINT))
$NEWSIZE = (($FROM_INSERT_POINT + $INSERT_PTR_SIZE) + ($PTR_SIZE - $TO_INSERT_POINT))
Case $PTR_SIZE < $TO_INSERT_POINT
IF ($PTR_SIZE < $FROM_INSERT_POINT) Then $FROM_INSERT_POINT = $PTR_SIZE
MoveMemory($PTR,$MEM_PTR,$FROM_INSERT_POINT)
MoveMemory($INSERT_PTR,$MEM_PTR + $FROM_INSERT_POINT, $INSERT_PTR_SIZE)
MoveMemory($PTR + $FROM_INSERT_POINT,$MEM_PTR + _
($FROM_INSERT_POINT + $INSERT_PTR_SIZE), ($PTR_SIZE - $FROM_INSERT_POINT))
$NEWSIZE = ($FROM_INSERT_POINT + $INSERT_PTR_SIZE + ($PTR_SIZE - $FROM_INSERT_POINT))
EndSelect
$MEM_ARRAY[0] = $MEM_PTR
$MEM_ARRAY[1] = $NEWSIZE
Return $MEM_ARRAY
EndFunc

Func MEM_FILE_INSERT($sFileName _
                     ,$INSERT_PTR _
                     ,$INSERT_PTR_SIZE _
                     ,$FROM_INSERT_POINT _
                     ,$TO_INSERT_POINT )

; $FROM_INSERT_POINT [ BYTE_Remove ] $TO_INSERT_POINT
;BYTE_Remove = [$TO_INSERT_POINT - $FROM_INSERT_POINT]
; IF $TO_INSERT_POINT = $FROM_INSERT_POINT ==> BYTE_Remove = 0

;IF $TO_INSERT_POINT > ($FROM_INSERT_POINT + SIZEOF $sFileName) ;
;   $TO_INSERT_POINT = ($FROM_INSERT_POINT + SIZEOF $sFileName) ;
;==============================================================;

;IF $FROM_INSERT_POINT  > $PTR_SIZE ;
;   $FROM_INSERT_POINT = $PTR_SIZE  ;
;===================================;
$PTR_SIZE = FileGetSize($sFileName)
$hFile = OpenFile($sFileName,0x00000002 )
If Not($hFile) Then Return -1

if $PTR_SIZE >= $TO_INSERT_POINT Then
$NEWSIZE = (($FROM_INSERT_POINT + $INSERT_PTR_SIZE) + ($PTR_SIZE - $TO_INSERT_POINT))
Else
IF ($PTR_SIZE < $FROM_INSERT_POINT) Then $FROM_INSERT_POINT = $PTR_SIZE
$NEWSIZE = ($FROM_INSERT_POINT + $INSERT_PTR_SIZE + ($PTR_SIZE - $FROM_INSERT_POINT))
EndIf

if $PTR_SIZE < $NEWSIZE Then
$Plus_size = $NEWSIZE - $PTR_SIZE
$Plus_STRUCT = DllStructCreate("BYTE[" & $Plus_size & "]")
$Plus_PTR = DllStructGetPtr($Plus_STRUCT)
SetFilePointer($hFile,$PTR_SIZE,0)
WriteFile($hFile,$Plus_PTR,$Plus_size)
SetFilePointer($hFile,0,0)
EndIf

$Mapping = CreateFileMapping($hFile,0x04,0,0,0)
If Not($Mapping) Then Return -2
$PTR = MapViewOfFile($Mapping,0xf001f,0,0,$PTR_SIZE)
If Not($Mapping) Then Return -3

$MEM_STRUCT = DllStructCreate("BYTE[" & $NEWSIZE & "]",$PTR)
$MEM_PTR = DllStructGetPtr($MEM_STRUCT)
Select
Case $PTR_SIZE >= $TO_INSERT_POINT
MoveMemory($PTR + $TO_INSERT_POINT, _
$MEM_PTR + ($FROM_INSERT_POINT + $INSERT_PTR_SIZE), ($PTR_SIZE - $TO_INSERT_POINT))
MoveMemory($INSERT_PTR,$MEM_PTR + $FROM_INSERT_POINT, $INSERT_PTR_SIZE)
MoveMemory($PTR,$MEM_PTR,$FROM_INSERT_POINT)
Case $PTR_SIZE < $TO_INSERT_POINT
MoveMemory($PTR + $FROM_INSERT_POINT,$MEM_PTR + _
($FROM_INSERT_POINT + $INSERT_PTR_SIZE), ($PTR_SIZE - $FROM_INSERT_POINT))
MoveMemory($INSERT_PTR,$MEM_PTR + $FROM_INSERT_POINT, $INSERT_PTR_SIZE)
MoveMemory($PTR,$MEM_PTR,$FROM_INSERT_POINT)
EndSelect
UnmapViewOfFile($MEM_PTR)
CloseHandle($hFile)
CloseHandle($Mapping)
EndFunc

Func MEM_INSERT_BYTE($PTR,$PTR_SIZE _
                ,$INSERT_BYTE_SIZE _
                ,$FROM_INSERT_POINT _
                ,$TO_INSERT_POINT )

; $FROM_INSERT_POINT [ BYTE_Remove ] $TO_INSERT_POINT
;BYTE_Remove = [$TO_INSERT_POINT - $FROM_INSERT_POINT]
; IF $TO_INSERT_POINT = $FROM_INSERT_POINT ==> BYTE_Remove = 0

;IF $TO_INSERT_POINT > ($FROM_INSERT_POINT + $INSERT_BYTE_SIZE) ;
;   $TO_INSERT_POINT = ($FROM_INSERT_POINT + $INSERT_BYTE_SIZE) ;
;==============================================================;

;IF $FROM_INSERT_POINT  > $PTR_SIZE ;
;   $FROM_INSERT_POINT = $PTR_SIZE  ;
;===================================;

Local $MEM_STRUCT , $MEM_PTR , $ALLOC , $NEWSIZE , $MEM_ARRAY[2] , _
$INSERT_BYTE_STRUCT , $INSERT_BYTE_PRT
$INSERT_BYTE_STRUCT = DllStructCreate("BYTE[" & $INSERT_BYTE_SIZE & "]")
$INSERT_BYTE_PRT = DllStructGetPtr($INSERT_BYTE_STRUCT)
$ALLOC = VirtualAlloc(0, 1, 0x1000, 0x40)
$MEM_STRUCT = DllStructCreate("BYTE[1]",$ALLOC)
$MEM_PTR = DllStructGetPtr($MEM_STRUCT)
Select
Case $PTR_SIZE >= $TO_INSERT_POINT
MoveMemory($PTR,$MEM_PTR,$FROM_INSERT_POINT)
MoveMemory($INSERT_BYTE_PRT,$MEM_PTR + $FROM_INSERT_POINT, $INSERT_BYTE_SIZE)
MoveMemory($PTR + $TO_INSERT_POINT, _
$MEM_PTR + ($FROM_INSERT_POINT + $INSERT_BYTE_SIZE), ($PTR_SIZE - $TO_INSERT_POINT))
$NEWSIZE = (($FROM_INSERT_POINT + $INSERT_BYTE_SIZE) + ($PTR_SIZE - $TO_INSERT_POINT))
Case $PTR_SIZE < $TO_INSERT_POINT
IF ($PTR_SIZE < $FROM_INSERT_POINT) Then $FROM_INSERT_POINT = $PTR_SIZE
MoveMemory($PTR,$MEM_PTR,$FROM_INSERT_POINT)
MoveMemory($INSERT_BYTE_PRT,$MEM_PTR + $FROM_INSERT_POINT, $INSERT_BYTE_SIZE)
MoveMemory($PTR + $FROM_INSERT_POINT,$MEM_PTR + _
($FROM_INSERT_POINT + $INSERT_BYTE_SIZE), ($PTR_SIZE - $FROM_INSERT_POINT))
$NEWSIZE = ($FROM_INSERT_POINT + $INSERT_BYTE_SIZE + ($PTR_SIZE - $FROM_INSERT_POINT))
EndSelect
$MEM_ARRAY[0] = $MEM_PTR
$MEM_ARRAY[1] = $NEWSIZE
Return $MEM_ARRAY
EndFunc

Func MEM_INSERT_SPACE($PTR,$PTR_SIZE _
                ,$INSERT_SPACE_SIZE _
                ,$FROM_INSERT_POINT _
                ,$TO_INSERT_POINT )

; $FROM_INSERT_POINT [ BYTE_Remove ] $TO_INSERT_POINT
;BYTE_Remove = [$TO_INSERT_POINT - $FROM_INSERT_POINT]
; IF $TO_INSERT_POINT = $FROM_INSERT_POINT ==> BYTE_Remove = 0

;IF $TO_INSERT_POINT > ($FROM_INSERT_POINT + $INSERT_SPACE_SIZE) ;
;   $TO_INSERT_POINT = ($FROM_INSERT_POINT + $INSERT_SPACE_SIZE) ;
;==============================================================;

;IF $FROM_INSERT_POINT  > $PTR_SIZE ;
;   $FROM_INSERT_POINT = $PTR_SIZE  ;
;===================================;

Local $MEM_STRUCT , $MEM_PTR , $ALLOC , $NEWSIZE , $MEM_ARRAY[2] , _
$INSERT_SPACE_STRUCT , $INSERT_SPACE_PRT ,$TXT = ""
$INSERT_SPACE_STRUCT = DllStructCreate("BYTE[" & $INSERT_SPACE_SIZE & "]")
For $I = 1 To $INSERT_SPACE_SIZE
$TXT &= " "
Next
DllStructSetData($INSERT_SPACE_STRUCT,1,$TXT)
$INSERT_SPACE_PRT = DllStructGetPtr($INSERT_SPACE_STRUCT)
$ALLOC = VirtualAlloc(0, 1, 0x1000, 0x40)
$MEM_STRUCT = DllStructCreate("BYTE[1]",$ALLOC)
$MEM_PTR = DllStructGetPtr($MEM_STRUCT)
Select
Case $PTR_SIZE >= $TO_INSERT_POINT
MoveMemory($PTR,$MEM_PTR,$FROM_INSERT_POINT)
MoveMemory($INSERT_SPACE_PRT,$MEM_PTR + $FROM_INSERT_POINT, $INSERT_SPACE_SIZE)
MoveMemory($PTR + $TO_INSERT_POINT, _
$MEM_PTR + ($FROM_INSERT_POINT + $INSERT_SPACE_SIZE), ($PTR_SIZE - $TO_INSERT_POINT))
$NEWSIZE = (($FROM_INSERT_POINT + $INSERT_SPACE_SIZE) + ($PTR_SIZE - $TO_INSERT_POINT))
Case $PTR_SIZE < $TO_INSERT_POINT
IF ($PTR_SIZE < $FROM_INSERT_POINT) Then $FROM_INSERT_POINT = $PTR_SIZE
MoveMemory($PTR,$MEM_PTR,$FROM_INSERT_POINT)
MoveMemory($INSERT_SPACE_PRT,$MEM_PTR + $FROM_INSERT_POINT, $INSERT_SPACE_SIZE)
MoveMemory($PTR + $FROM_INSERT_POINT,$MEM_PTR + _
($FROM_INSERT_POINT + $INSERT_SPACE_SIZE), ($PTR_SIZE - $FROM_INSERT_POINT))
$NEWSIZE = ($FROM_INSERT_POINT + $INSERT_SPACE_SIZE + ($PTR_SIZE - $FROM_INSERT_POINT))
EndSelect
$MEM_ARRAY[0] = $MEM_PTR
$MEM_ARRAY[1] = $NEWSIZE
Return $MEM_ARRAY
EndFunc


Func MEM_ARRAY_TO_CHAR_ARRAY($MEM_ARRAY)
Local $CHAR_STRUCT , $CHAR_ARRAY[1]
$CHAR_STRUCT =  DllStructCreate("CHAR[" & $MEM_ARRAY[1] & "]",$MEM_ARRAY[0])
For $I = 1 To $MEM_ARRAY[1]
$CHAR_ARRAY[UBound($CHAR_ARRAY) - 1] = DllStructGetData($CHAR_STRUCT,1,$I)
ReDim $CHAR_ARRAY[UBound($CHAR_ARRAY) + 1]
Next
ReDim $CHAR_ARRAY[UBound($CHAR_ARRAY) - 1]
Return $CHAR_ARRAY
EndFunc

Func MEM_ARRAY_TO_BYTE_ARRAY($MEM_ARRAY)
Local $BYTE_STRUCT , $BYTE_ARRAY[1]
$BYTE_STRUCT =  DllStructCreate("BYTE[" & $MEM_ARRAY[1] & "]",$MEM_ARRAY[0])
For $I = 1 To $MEM_ARRAY[1]
$BYTE_ARRAY[UBound($BYTE_ARRAY) - 1] = DllStructGetData($BYTE_STRUCT,1,$I)
ReDim $BYTE_ARRAY[UBound($BYTE_ARRAY) + 1]
Next
ReDim $BYTE_ARRAY[UBound($BYTE_ARRAY) - 1]
Return $BYTE_ARRAY
EndFunc

Func MEM_ARRAY_TO_BYTE($MEM_ARRAY)
Local $BYTE_STRUCT , $TEXT = ""
$BYTE_STRUCT =  DllStructCreate("BYTE[" & $MEM_ARRAY[1] & "]",$MEM_ARRAY[0])
For $I = 1 To $MEM_ARRAY[1]
$TEXT &= DllStructGetData($BYTE_STRUCT,1,$I)
Next
Return $TEXT
EndFunc

Func MEM_ARRAY_TO_CHAR($MEM_ARRAY)
Local $CHAR_STRUCT , $TEXT = ""
$CHAR_STRUCT =  DllStructCreate("CHAR[" & $MEM_ARRAY[1] & "]",$MEM_ARRAY[0])
For $I = 1 To $MEM_ARRAY[1]
$TEXT &= DllStructGetData($CHAR_STRUCT,1,$I)
Next
Return $TEXT
EndFunc

Func MEM_ARRAY_TO_BYTE_STRUCT($MEM_ARRAY)
Return DllStructCreate("BYTE[" & $MEM_ARRAY[1] & "]",$MEM_ARRAY[0])
EndFunc

Func MEM_ARRAY_TO_CHAR_STRUCT($MEM_ARRAY)
Return DllStructCreate("CHAR[" & $MEM_ARRAY[1] & "]",$MEM_ARRAY[0])
EndFunc

Func MoveMemory($destPtr,$sourcePtr,$Size)
DllCall("Kernel32.dll","none","RtlMoveMemory" _
,"ptr",$sourcePtr,"ptr",$destPtr,"dword",$Size)
EndFunc

Func VirtualAlloc($lpAddress,$dwSize,$flAllocationType,$flProtect)
$LPVOID = DllCall("Kernel32.dll","PTR","VirtualAlloc","PTR",$lpAddress,"LONG",$dwSize, _
"DWORD",$flAllocationType,"DWORD",$flProtect)
if Not @error Then Return $LPVOID[0]
Return 0
EndFunc

Func OpenFile($lpFileName,$uStyle)
    $Tag_OFSTRUCT = _
    "byte cBytes;" & _
    "byte fFixedDisk;" &  _
    "ushort nErrCode;" &  _
    "ushort Reserved1;" &  _
    "ushort Reserved2;" &  _
    "char szPathName[260]"
    $OFSTRUCT = DllStructCreate($Tag_OFSTRUCT)
    $lpReOpenBuff = DllStructGetPtr($OFSTRUCT)
    $HFILE = DllCall("Kernel32.dll","hwnd","OpenFile","str",$lpFileName,"ptr",$lpReOpenBuff,"long",$uStyle)
    if Not @error Then Return $HFILE[0]
    Return 0
EndFunc

Func CreateFileMapping($hFile,$flProtect,$dwMaximumSizeHigh,$dwMaximumSizeLow,$lpName)
$HANDLE = DllCall("Kernel32.dll","HANDLE","CreateFileMappingW","HANDLE",$hFile,"PTR",0,"DWORD",$flProtect,"DWORD", _
$dwMaximumSizeHigh,"DWORD",$dwMaximumSizeLow,"STR",$lpName)
if Not @error Then Return $HANDLE[0]
Return 0
EndFunc

Func MapViewOfFile($hFileMappingObject,$dwDesiredAccess,$dwFileOffsetHigh, _
$dwFileOffsetLow,$dwNumberOfBytesToMap)
$LPVOID = DllCall("Kernel32.dll","PTR","MapViewOfFile","HANDLE",$hFileMappingObject,"DWORD" _
,$dwDesiredAccess,"DWORD",$dwFileOffsetHigh,"DWORD",$dwFileOffsetLow,"LONG",$dwNumberOfBytesToMap)
if Not @error Then Return $LPVOID[0]
Return 0
EndFunc

Func UnmapViewOfFile($lpBaseAddress)
$BOOL = DllCall("Kernel32.dll","INT","UnmapViewOfFile","PTR",$lpBaseAddress)
if Not @error Then Return $BOOL[0]
Return 0
EndFunc

Func WriteFile($hFile,$lpBuffer,$nNumberOfBytesToWrite,$lpOverlapped = 0)
$BOOL = DllCall("Kernel32.dll","INT","WriteFile","HANDLE",$hFile,"PTR",$lpBuffer, _
"DWORD",$nNumberOfBytesToWrite,"LONG*",0,"PTR",$lpOverlapped)
if Not @error Then Return $BOOL[0]
Return 0
EndFunc

Func SetFilePointer($hFile,$lDistanceToMove,$dwMoveMethod)
$Newpointer = DllCall("Kernel32.dll","DWORD","SetFilePointer","HANDLE",$hFile,"LONG",$lDistanceToMove , _
"ptr",0,"DWORD",$dwMoveMethod)
if Not @error Then Return $Newpointer[0]
Return 0
EndFunc

Func CloseHandle($hObject)
$BOOL = DllCall("Kernel32.dll","INT","CloseHandle","HANDLE",$hObject)
if Not @error Then Return $BOOL[0]
Return 0
EndFunc

MEM_INSERT_EXAMPLE.au3

#Include <Array.au3>
#Include "Mem.AU3"

$sText = "12389"
$sText2 = "4567"

$PTR_SIZE = StringLen($sText)
$SPTR = DllStructCreate("CHAR[" & $PTR_SIZE & "]")
DllStructSetData($SPTR,1,$sText)
$PTR = DllStructGetPtr($SPTR)

$INSERT_PTR_SIZE = StringLen($sText2)
$SINSERT_PTR = DllStructCreate("CHAR[" & $INSERT_PTR_SIZE & "]")
DllStructSetData($SINSERT_PTR,1,$sText2)
$INSERT_PTR = DllStructGetPtr($SINSERT_PTR)

$MEM_ARRAY = MEM_INSERT($PTR,$PTR_SIZE _
                ,$INSERT_PTR,$INSERT_PTR_SIZE _
                ,3 _ ; FROM POINT 3
                ,3 ) ; TO POINT 3 ; BYTE_Remove 3 - 3 = 0

$CHAR_ARRAY = MEM_ARRAY_TO_CHAR_ARRAY($MEM_ARRAY)
_ArrayDisplay($CHAR_ARRAY,"")

$sText = "123456789"
$sText2 = "WWW"

$PTR_SIZE = StringLen($sText)
$SPTR = DllStructCreate("CHAR[" & $PTR_SIZE & "]")
DllStructSetData($SPTR,1,$sText)
$PTR = DllStructGetPtr($SPTR)

$INSERT_PTR_SIZE = StringLen($sText2)
$SINSERT_PTR = DllStructCreate("CHAR[" & $INSERT_PTR_SIZE & "]")
DllStructSetData($SINSERT_PTR,1,$sText2)
$INSERT_PTR = DllStructGetPtr($SINSERT_PTR)

$MEM_ARRAY = MEM_INSERT($PTR,$PTR_SIZE _
                ,$INSERT_PTR,$INSERT_PTR_SIZE _
                ,3 _ ; FROM POINT 3
                ,6 ) ; TO POINT 6 ; BYTE_Remove 6 - 3 = 3

$CHAR_ARRAY = MEM_ARRAY_TO_CHAR_ARRAY($MEM_ARRAY)
_ArrayDisplay($CHAR_ARRAY,"")

MEM_FILE_INSERT_EXAMPLE.au3

#Include <Array.au3>
#Include "Mem.AU3"

$sText = "12389"
$sText2 = "4567"

$FILE = FileOpen("INSERT_FILE.TXT",2)
FileWrite($FILE,$sText)
FileClose($FILE)

$INSERT_PTR_SIZE = StringLen($sText2)
$SINSERT_PTR = DllStructCreate("CHAR[" & $INSERT_PTR_SIZE & "]")
DllStructSetData($SINSERT_PTR,1,$sText2)
$INSERT_PTR = DllStructGetPtr($SINSERT_PTR)



  MEM_FILE_INSERT("INSERT_FILE.TXT" _
                     ,$INSERT_PTR _
                     ,$INSERT_PTR_SIZE _
                     ,3 _ ; FROM POINT 3
                     ,3)  ; TO POINT 3 ; BYTE_Remove 3 - 3 = 0

MEM_INSERT_SPACE_EXAMPLE.au3

#Include <Array.au3>
#Include "Mem.AU3"

$sText = "123456789"

$PTR_SIZE = StringLen($sText)
$SPTR = DllStructCreate("BYTE[" & $PTR_SIZE & "]")
DllStructSetData($SPTR,1,$sText)
$PTR = DllStructGetPtr($SPTR)

$MEM_ARRAY =  MEM_INSERT_SPACE($PTR,$PTR_SIZE _
                ,5 _ ; 5 SPACE
                ,8 _ ; FROM POINT 8
                ,8 ) ; TO POINT 8 ; BYTE_Remove 8 - 8 = 0

$CHAR_ARRAY = MEM_ARRAY_TO_CHAR_ARRAY($MEM_ARRAY)
_ArrayDisplay($CHAR_ARRAY,"")

MEM_INSERT_BYTE_EXAMPLE.au3

#Include <Array.au3>
#Include "Mem.AU3"

$sText = "123456789"

$PTR_SIZE = StringLen($sText)
$SPTR = DllStructCreate("BYTE[" & $PTR_SIZE & "]")
DllStructSetData($SPTR,1,$sText)
$PTR = DllStructGetPtr($SPTR)

$MEM_ARRAY =  MEM_INSERT_BYTE($PTR,$PTR_SIZE _
                ,5 _ ; 5 BYTE
                ,8 _ ; FROM POINT 8
                ,8 ) ; TO POINT 8 ; BYTE_Remove 8 - 8 = 0

$CHAR_ARRAY = MEM_ARRAY_TO_CHAR_ARRAY($MEM_ARRAY)
_ArrayDisplay($CHAR_ARRAY,"")

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0