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 

[Lib] binGetString - Get textual part of binary {binfile()}

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Thu Mar 11, 2010 4:38 pm    Post subject: [Lib] binGetString - Get textual part of binary {binfile()} Reply with quote

Ahk Compatible: Vanilla / Mainstream
License: New BSD License

Download (~11 kb)
    The archive contains the library, a demo script and the documentation. Also all required modules are included.

Online Documentation

Description

I tried to make a function to read textual portion of binary files. After searching the forum, I did not find any solution (that day). The function can be used to read out headers etc.

Examples

Code:
MsgBox % str := binGetString(bindata, 800, 64, filename)


Code:
#Include binGetString.ahk
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
SetBatchLines, -1

; Any positiv number is the size of first bytes to load.
sizeToRead := 5000

; Specify how many characters to leave before
; beginning to work with file.
seek := 0

; Select source file
FileSelectFile, filepath, %A_WorkingDir%,, Select binary file
If (ErrorLevel = 0)
{
   ; Read file into memory
   FileRead, bindata, %filepath%
   
   ; Strip non printable ASCII characters from file
   str := binGetString(bindata, sizeToRead, seek, filepath)
   
   ; Show that content now
   MsgBox,, %filepath%, %str%
}


Edit: Name changed from binfile to binGetString. In general, it is the same function. In addition, it does leave out the first 31 ascii characters too.
_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Last edited by Tuncay on Wed Sep 29, 2010 9:30 am; edited 5 times in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Thu Mar 11, 2010 5:23 pm    Post subject: Reply with quote

The expression
Code:
Chr(NumGet(_data, _offset + A_Index - 1, "UChar"))
gives you the char already in _data, so you just copy it to str, except NUL characters are dropped. The ByRef _data already contains the result, in full. If you know there is no NUL in the _data, SubStr is easier to extract the desired bytes.

Last edited by Laszlo on Thu Mar 11, 2010 5:36 pm; edited 1 time in total
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Thu Mar 11, 2010 5:34 pm    Post subject: Reply with quote

I am not understanding why? The data contains nulls like in say mp3 or wma file. There IS nul in the data and this function converts to string representation. It is not desired for reading text files. Shocked
_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Thu Mar 11, 2010 5:39 pm    Post subject: Reply with quote

When you append NUL {=chr(0)} to a string, nothing changes. This is why NUL characters are removed from str, which you return. Its length will be smaller, and you will not know, where the original NULs were.
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Thu Mar 11, 2010 5:49 pm    Post subject: Reply with quote

This is why I made this. Its more for like reading header of mp3 or such a thing.
_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Thu Mar 11, 2010 6:01 pm    Post subject: Reply with quote

I see. The script description does not tell that you wanted to extract ANSI text from binary files, or drop NUL chars. If you want only ANSI text, you could exclude other funny characters, too, (with code <32 or >126) making the result even more readable.
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Thu Mar 11, 2010 6:23 pm    Post subject: Reply with quote

Ok I have renamed the function from binary_read() to binfile() and changed topic.
You are right about stripping characters above 126, but with those of below 32, there are some characters like tab, which should not be removed. I have to look which codes can be eliminated. But I think this should not be really neccessary, because a correct usage of this function assumes the user knows the offsets and size of data to extract.

Edit: Stripping all codes above 126, is this correct?
_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Thu Mar 11, 2010 7:51 pm    Post subject: Reply with quote

If you are expecting diacritical letters, or other special characters you need to filter several intervals, or use "if var is alpha" for letters, and intervals for symbols. The script is useful for finding error message texts in program files, to determine the version of a document stored in binary form, to figure what SW created a file, etc. In these cases you might not know the offset, where to look.
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Wed Sep 29, 2010 9:31 am    Post subject: Reply with quote

Updated and license changed.
_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
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