READ/WRITE binary files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
einstein1969
Expert
Posts: 763
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#16 Post by einstein1969 » 26 Jan 2014 18:28

Aacini wrote:
einstein1969 wrote:
Aacini wrote:Anyway, fsutil is needed for the reading part of binary files...

Antonio


why? in what way?

einstein1969

The method to read a binary file (that I borrowed from this aGerman's post) consist in create a file with binary zeros of the same size of the original file and then use FC /B command to get the Ascii hexadecimal representation of all bytes different than zero; bytes with zero value are inserted in the "holes" of this representation.

I don't know if a different method to read a binary file in Batch exist that does not use FSUTIL...

Antonio


the hexdump utility of dbenham can read without fsutil?

einstein1969

penpen
Expert
Posts: 1725
Joined: 23 Jun 2013 06:15
Location: Germany

Re: READ/WRITE binary files

#17 Post by penpen » 26 Jan 2014 18:32

You also may also write non null bytes followed by single null bytes using "cmd /U" and ">>", so you don't always need to use the "copy":
Create a file with content {\0x61, \0x00}:

Code: Select all

copy nul test.txt
cmd /U /C set /P "=a" < nul >> test.txt

penpen

einstein1969
Expert
Posts: 763
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#18 Post by einstein1969 » 26 Jan 2014 18:35

penpen wrote:You also may also write non null bytes followed by single null bytes using "cmd /U" and ">>", so you don't always need to use the "copy":
Create a file with content {\0x61, \0x00}:

Code: Select all

copy nul test.txt
cmd /U /C set /P "=a" < nul >> test.txt

penpen


slow (i thinks that will have to be used multiple times), but a good alternative at fsutils / copy!

EDIT:but there is a problem for contiguous null!

einstein1969
Last edited by einstein1969 on 26 Jan 2014 18:51, edited 2 times in total.

Aacini
Expert
Posts: 1617
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: READ/WRITE binary files

#19 Post by Aacini » 26 Jan 2014 18:41

einstein1969 wrote:the hexdump utility of dbenham can read without fsutil?

einstein1969


Yes, my mistake! I mean that fsutil is the fastest way to create the comparison file... I am biased by the fact that I use this method to encode/decode files that may be very large!

Anyway, a possible solution is to create the zero.txt file just once and include it besides the program that read/write the binary files.

Antonio

einstein1969
Expert
Posts: 763
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#20 Post by einstein1969 » 26 Jan 2014 18:50

Aacini wrote:
einstein1969 wrote:the hexdump utility of dbenham can read without fsutil?

einstein1969


Yes, my mistake! I mean that fsutil is the fastest way to create the comparison file... I am biased by the fact that I use this method to encode/decode files that may be very large!

Anyway, a possible solution is to create the zero.txt file just once and include it besides the program that read/write the binary files.

Antonio


thanks Antonio.

einstein1969

npocmaka_
Posts: 481
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: READ/WRITE binary files

#21 Post by npocmaka_ » 27 Jan 2014 08:21

one more way writing...

Code: Select all

mshta vbscript:execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(Chr(01) & Chr(00) & Chr(04) & Chr(13) & Chr(10) & Chr(8)):Close")>testfile


Unfortunately javascript has a problem with some extended ascii characters."GetStandardStream" should be used for dislpaying to console (with find or more or something like that) so probably javascript could use adodb streams for binary writing.

einstein1969
Expert
Posts: 763
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#22 Post by einstein1969 » 27 Jan 2014 10:38

npocmaka_ wrote:one more way writing...

Code: Select all

mshta vbscript:execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(Chr(01) & Chr(00) & Chr(04) & Chr(13) & Chr(10) & Chr(8)):Close")>testfile


Unfortunately javascript has a problem with some extended ascii characters."GetStandardStream" should be used for dislpaying to console (with find or more or something like that) so probably javascript could use adodb streams for binary writing.


thanks npocmaka_ for your contribute.

@all
the most difficult problem to be solved (without using vbscrit / jscript / mshta / external utility / compiled / non-standard software) seems to be to create the files with null bytes.

I understand that the "only" utility that can create and which is common to all systems from XP is "fsutil", right?

And by the way might need administrative rights.

The solution proposed by Penpen creates a pair of bytes.
we can do the splits and isolate the null?

What other methods exist to create the file with the null byte (0)?

einstein1969

npocmaka_
Posts: 481
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: READ/WRITE binary files

#23 Post by npocmaka_ » 27 Jan 2014 11:04

einstein1969 wrote:
npocmaka_ wrote:one more way writing...



thanks npocmaka_ for your contribute.

@all
the most difficult problem to be solved (without using vbscrit / jscript / mshta / external utility / compiled / non-standard software) seems to be to create the files with null bytes.

I understand that the "only" utility that can create and which is common to all systems from XP is "fsutil", right?

And by the way might need administrative rights.

The solution proposed by Penpen creates a pair of bytes.
we can do the splits and isolate the null?

What other methods exist to create the file with the null byte (0)?

einstein1969


Can a null written in registry be extracted into a file?
Some 'special files' like .exe-s and .zip-s contains nulls as boms/metadata or whatever it is.which can easy be created with makecab/iexpress but I don't if the rest data can be stripped (but I'll try - in all cases it will require additional temp file)

Squashman
Expert
Posts: 4113
Joined: 23 Dec 2011 13:59

Re: READ/WRITE binary files

#24 Post by Squashman » 27 Jan 2014 11:51

Couple of interesting ideas over here about writing nul character to a file.
http://stackoverflow.com/questions/2730 ... batch-scri

Been reading the link in the last post. pretty interesting stuff.

carlos
Expert
Posts: 489
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: READ/WRITE binary files

#25 Post by carlos » 27 Jan 2014 11:53

I think I found a way of create the nul using makecab. I' developing it.
Edit: Here is: http://www.dostips.com/forum/viewtopic.php?f=3&t=5326

penpen
Expert
Posts: 1725
Joined: 23 Jun 2013 06:15
Location: Germany

Re: READ/WRITE binary files

#26 Post by penpen » 27 Jan 2014 15:14

The makecab seems to be not needed anymore, so now you only need the "external tools" "cmd.exe" and "findstr.exe" to create a byte containing a nul byte.
Posted on carlos linked topic.

penpen

einstein1969
Expert
Posts: 763
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#27 Post by einstein1969 » 27 Jan 2014 15:59

Wow!

:D

einstein1969

carlos
Expert
Posts: 489
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: READ/WRITE binary files

#28 Post by carlos » 28 Jan 2014 17:32

@einstein1969: Now, you can write a utility for write binary data.
Here: http://www.dostips.com/forum/viewtopic.php?p=32164#p32164
a code that create the 256 ascii characters. The only that you need is use copy /b destinationfile+letter.chr

You can use for example the for command:

Code: Select all

for %%a in (0D 0A FF ...) Do Copy /b dest.bin+%%a.chr


Also, is possible that the technique and codification for generate the character files would be improved. But currently with this you can write binary data directly using batch, not vbs.

I would not write this utility for write binary using the .chr files, because I wasted many time writing bhx utility. :D

einstein1969
Expert
Posts: 763
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: READ/WRITE binary files

#29 Post by einstein1969 » 29 Jan 2014 14:01

thanks you very much carlos.

All that has been developed is very constructive. Writing fast track again via dos is now the target.
I'll try to throw something down even if I do not have much experience. Thanks again to you and to all.

Einstein1969

carlos
Expert
Posts: 489
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: READ/WRITE binary files

#30 Post by carlos » 14 Feb 2014 15:30

@einstein1969.
Coming soon i will upload new feature to bhx.
Using com files is still valid in 32 bit systems. And com files with only printable characters also valid.

I write a 16 bit utility call wrichr.com for create the .chr files:
Note: this will not work on 64 bits system, only 32 bits.

Edit: wrichr.asm revisited: reduced bytes from 55,201 (original, encoded) to 53,199 (new original, new encoded).

Code: Select all

@Echo Off

Del wrichr.com
(
Echo hD1X-s0P_kUHP0UxGWX4ax1y1ieimnfeinklddmemkjanmndnadmndnpbbn
Echo hhpbbnpljhoxolnhaigidpllnbkdnhlkfhlflefblffahfUebdfahhfkokh
Echo v17/@yEh9/@/20My/E652hs4/eDAwl/UubnT6/cukMTt//hz8jys79Ah5/c
Echo yzLb/o9EnVAQn.
) > wrichr.com

Cmd /A /c "For /l %%# in (0,1,255) do wrichr.com %%#>%%#.chr"



The original .com program have 53 bytes, but encoded (using latest convert.bat from Herbert Kleebauer) for have only printable characters the size is 199 bytes.

This is the source for fasm:
wrichr.asm

Code: Select all

;wrichr.asm
;print to screen one byte
;arguments: <int>0-255
;author: Carlos Montiers
;date: 15-feb-2014

org 100h

;set in cl the length of command line
mov bx,80h
mov cx,[bx]

;assign 10 to byte source for multiplication
mov ch,10
;set char to 0
mov al,0

;set bx to 81 for next inc left bx = 82h
;pointing to first character of command line
inc bx

get_char:
;if none char available go to print
cmp cl, 0
je print

;get letter
inc bx
mov dx, [bx]

;if is not digit continue like c keyword
cmp dl, '0'
jl continue
cmp dl, '9'
jg continue

;convert char to digit
sub dl, '0'
;multiply by 10
mul ch
;sum digit
add al, dl

continue:
dec cl
jmp get_char

print:

mov dl,al
;direct console output
mov ah, 06h

cmp dl,255
jne commit
;if character is 255 use other interrupt
;write character to standard output
mov ah, 02h

commit:
int 21h
ret

Last edited by carlos on 05 May 2014 21:18, edited 6 times in total.

Post Reply