Binary file search with RegExp

Find, replace, find in files, replace in files, regular expressions

Binary file search with RegExp

Postby Bernard » Fri Jun 16, 2006 4:39 pm

Hello!

As I could read trough the Forum, searching in binaries is very limited, so I hope there is a solution for my problem.

I have a BIN file with many merged bitmaps of PNG format. I want to make Macro to split it to standalone files. All PNG files start with "‰PNG"

Code: Select all
00000000h: 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 ; ‰PNG........IHDR

and ends with "IEND®B`"

Code: Select all
00000640h: 49 45 4E 44 AE 42 60 00 00 00 00 00 00 00 00 00 ; IEND®B`.........

I've tried to use Find with RegExp by Perl synthax ‰PNG.*IEND®B` to select the whole PNG file, but I can't get it to work. Has someone clue where do I go wrong?? :cry: I have vers. 12.10.

Thanks !
User avatar
Bernard
Newbie
 
Posts: 2
Joined: Thu Jun 15, 2006 11:00 pm

Re: Binary file search with RegExp

Postby Mofi » Sat Jun 17, 2006 7:02 am

Regular expressions are not possible in hex mode. But it is not needed for splitting your merged PNGs. The following macro will do it. You should enable the macro property Continue if a Find with Replace not found or the clearing of clipboard 9 and switch back to windows clipboard will not be executed.

InsertMode
ColumnModeOff
HexOn
UnixReOff
Clipboard 9
Loop
Find "49454E44AE426082"
IfNotFound
ExitLoop
EndIf
Key LEFT ARROW
Key RIGHT ARROW
SelectToTop
Cut
NewFile
UnicodeToASCII
" "
HexOn
Key LEFT ARROW
Key LEFT ARROW
HexDelete 1
Paste
NextWindow
EndLoop
ClearClipboard
Clipboard 0


How it works:

It searches for the end marker of PNG in hex. If found (and selected), the selection is removed (key left/right) and every byte from current position to top of the file (= start of the PNG image) is selected and CUT to the clipboard.

Then a new ASCII file is created with a space to be able to switch to hex mode. The space is deleted and the PNG image content is pasted from the clipboard. With the last command in the loop the source file gets the focus and the loop continues until the source file is empty.

After macro execution you will have an empty source file. Use File - Revert to Saved to restore the content or close it without save. And you will have new files in the order of the PNGs inside the source file which you have to save now manually with an appropriate name.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Binary file search with RegExp

Postby Bernard » Sun Jun 18, 2006 6:45 pm

Mofi, thanks alot for this comprehensive answer and hints. It's so simple but didn't cross my mind ...

I had to make some additional modifications to remove the trash content between 2 files that was left on the top of data after the cut-paste process, and some minor additional editing... I can post the final macro if it's interesting to someone...

Also I would suggest to developers of UE, The Ultimate Editor, to expand the RegEx support to hex mode, I'm sure many of us would use it alot!
User avatar
Bernard
Newbie
 
Posts: 2
Joined: Thu Jun 15, 2006 11:00 pm


Return to Find/Replace/Regular Expressions