AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

simple binary file read/write functions
Goto page Previous  1, 2, 3, 4, 5  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
mare



Joined: 01 Aug 2008
Posts: 18

PostPosted: Fri Aug 08, 2008 4:34 pm    Post subject: Reply with quote

Thanks for the explaination, i'm begining to understand DllCalling but for now i'll skip that part, i need more practise.

I have managed to split encrypted data in two pieces, first written where data was read, second somewhere in the file, but now i have some extra bytes that i would like to delete.
I tried to write blank value on that position with offset setting, it only writes 0000 0000...

How can i delete those bytes, i want to write NULL ?
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4077
Location: Pittsburgh

PostPosted: Fri Aug 08, 2008 4:40 pm    Post subject: Reply with quote

If you have hex buffers, write 0x00... there. In binary buffers use NumPut(0,...), before writing to disk.
Back to top
View user's profile Send private message
mare



Joined: 01 Aug 2008
Posts: 18

PostPosted: Fri Aug 08, 2008 5:41 pm    Post subject: Reply with quote

I dont know what hex buffers are, if this is what you meant, from Hex Editor:
Code:

0700 ; this are last 4 hex numbers from original file
0700 6C2E 546D 6B79 4E30 ; 4 hex from original and 16 i want to delete


This is what i tried:
Code:
del := ""
;Bin2Hex(b,del)
res := BinWrite(file,del,8,-64)
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4077
Location: Pittsburgh

PostPosted: Fri Aug 08, 2008 6:15 pm    Post subject: Reply with quote

I meant by Hex Buffer the AHK variable containing hex digits, which are used in the file I/O in the functions in this thread.

The BinWrite function does not do anything in your code, because there is no data (empty string). If you want to write 8 NUL bytes, set "del = 0000000000000000" (16 0-chars).
Back to top
View user's profile Send private message
mare



Joined: 01 Aug 2008
Posts: 18

PostPosted: Fri Aug 08, 2008 6:34 pm    Post subject: Reply with quote

No no no, i want to delete those 8 bytes, remove them compleately from file,
I'm trying to write 0x00 and again it overwrites them to 0000...

Again, i want to delete those 8 bytes.
That's why i tried to write empty value.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4077
Location: Pittsburgh

PostPosted: Fri Aug 08, 2008 6:45 pm    Post subject: Reply with quote

Read the file in a (hex or binary) buffer, delete the unwanted entries from the buffer and write the whole buffer back to disk. In case of the hex buffer you need StringLeft/Right, in case of the binary buffer you need DllCall("RtlMoveMemory"...
Back to top
View user's profile Send private message
mare



Joined: 01 Aug 2008
Posts: 18

PostPosted: Fri Aug 08, 2008 6:53 pm    Post subject: Reply with quote

Yes i know that but i dont know how to delete unwanted entries.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4077
Location: Pittsburgh

PostPosted: Fri Aug 08, 2008 7:07 pm    Post subject: Reply with quote

Code:
string = 123456789
start = 2
stop = 5

cut := SubStr(string,1,start-1) . SubStr(string,stop+1)
MsgBox %cut% ; works with strings

VarSetCapacity(res,5)
DllCall("RtlMoveMemory", Uint,&res, UInt,&string, Uint,start-1)
DllCall("RtlMoveMemory", Uint,&res+start-1, UInt,&string+stop, Uint,StrLen(string)-stop)
MsgBox %res% ; works with binary data in string
Back to top
View user's profile Send private message
mare



Joined: 01 Aug 2008
Posts: 18

PostPosted: Sat Aug 09, 2008 1:20 pm    Post subject: Reply with quote

Either i dont understand your script or you dont understand my problem.
I was able to split 123456 to 123 and 456 for example, but i have an extra bytes allready written in the file that i want to delete.

Original file is 200 bytes, encrypted is 208 bytes, so i want to delete those 8 bytes.
Code:
0700 ; this are last 4 hex numbers from original file
0700 6C2E 546D 6B79 4E30 ; 4 hex from original and 16 i want to delete


Numbers marked with red are the one i want to delete, so when i delete them i want the last number to be 0700 like in the original file.

Is it posibble to do that with your Bin read/write script ?
I tried to write 0x00 and then it looks like this:
Code:
0700 0000 0000 0000 0000

But again file size is 208 bytes.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4077
Location: Pittsburgh

PostPosted: Sat Aug 09, 2008 2:06 pm    Post subject: Reply with quote

I don’t understand your problem. I said: "Read the file in a (hex or binary) buffer, delete the unwanted entries from the buffer and write the buffer back to disk." If the unwanted entries are in the end of the buffer, you only need to specify a shorter length (n), otherwise you need to copy pieces of the buffer to another (or the same) variable, and write that one to disk.
Back to top
View user's profile Send private message
mare



Joined: 01 Aug 2008
Posts: 18

PostPosted: Sat Aug 09, 2008 2:50 pm    Post subject: Reply with quote

I think i understand you now, you meant, read whole file, delete unwanted, and write file again from begining to end.
Something like this perhaps:
Code:

res :=BinRead(file,data)
start := res - 16
stop := res
cut := SubStr(string,1,start-1) . SubStr(string,stop+1)
Hex2Bin(b,cut)
BinWrite(file,b)
; or BinWrite(file,b,start) ?


I meant, read only bytes for deletion and simply delete them.

EDIT: This worked i think, but i need to write it to another file, right?
Because those 8 extra bytes still exist.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4077
Location: Pittsburgh

PostPosted: Sat Aug 09, 2008 3:12 pm    Post subject: Reply with quote

read-file, delete it, process data, write same file

If you want to truncate the file, you could do it faster with Windows system calls, but you have to browse MSDN for the details
Back to top
View user's profile Send private message
Guest






PostPosted: Sat Aug 09, 2008 3:44 pm    Post subject: Reply with quote

Laszlo wrote:
read-file, delete it, process data, write same file

If you want to truncate the file, you could do it faster with Windows system calls, but you have to browse MSDN for the details


Tuncate File using system calls by PhiLho
Back to top
mare



Joined: 01 Aug 2008
Posts: 18

PostPosted: Sat Aug 09, 2008 4:36 pm    Post subject: Reply with quote

Thanks, it seems that those 8 extra bytes of 0000 does not have effect on file, file is working properly, file size is changed only, but that is not really a problem.
Back to top
View user's profile Send private message
xazax



Joined: 12 Aug 2008
Posts: 2

PostPosted: Tue Aug 12, 2008 9:18 pm    Post subject: Reply with quote

I have trouble with this function.

res := BinWrite(file,v,0,%k%)

where file: %A_WorkingDir%\Filename
v : 8b
k: 25E59F

I want to write a byte on offset 25E59F from anything to 8B

The Function returns here:
Code:

   h := DllCall("CreateFile","str",file,"Uint",0x40000000,"Uint",0,"UInt",0,"UInt",4,"Uint",0,"UInt",0)
   IfEqual h,-1, SetEnv, ErrorLevel, -1
   IfNotEqual ErrorLevel,0,Return,0 ; couldn't create the file


With errorlevel -1

What did i wrong?

P.S.: Sorry for my bad english
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5  Next
Page 4 of 5

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group