kelp7
(Starting to like KiXtart)
2020-09-16 10:00 PM
Binary File Prepend

Hi,

Slightly away from my usual sys admin related queries, this is more regarding a hobby project. I need to read a binary data file into memory, prepend 128 bytes to it and then write it back out again. Is this possible with Kix?

Thanks!


Arend_
(MM club member)
2020-09-23 09:38 PM
Re: Binary File Prepend

It's possible, I did something like this in the past to flip bytes.
You can use the "ADODB.Stream" COM-object for those purposes.
This might be of some help.


kelp7
(Starting to like KiXtart)
2020-09-24 04:45 PM
Re: Binary File Prepend

Thanks!

AndreLuiz
(Getting the hang of it)
2020-10-04 07:49 AM
Re: Binary File Prepend

[PT-BR]
Olá, eu tinha isso a um tempo atrás, mas não sabia se funcionava, mas funciona hahahah faz um teste.

[ENG]
Hello, I had this a while ago but did not know if it worked but works hahahah makes a test.

 Code:
include "KixWithClass.udf"	; http://www.kixtart.org/UDF/UDF_lister.php?what=post&code=213741
include "AlsArray.udf"		; http://www.kixtart.org/UDF/UDF_lister.php?what=post&code=213557
$include_fbin = includeClass("fbin", "FileBin.udf") ; UDF FileBin.udf
$fbin = newClass($include_fbin)
$path_i = @curdir+"\cmd.exe"
$path_o = @curdir+"\cmd_o.exe"

;; Reading file and returned an array of ascii code
"Reading file..."?
$bin = $fbin.ReadBin($path_i)
"Read file!"??

;; Writing to a binary array
$bin = array_merge($bin,
	stringToByteArray("hello") ;; transforming text em array from ascii
)


;; Writing the binary file array.
"Writing in the file..."?
$fbin.WriteBin($path_o, $bin)
"Written file!"??

;; Writing at the end of the file
$fbin.WriteBin_append($path_o,
	stringToByteArray("Hello Brasil!!") ;; transforming text em array from ascii
)

get$



Udf FileBin Kix2Vbs
 Code:
public function ReadBin($path)
	$objFSO = CreateObject( "Scripting.FileSystemObject" )
	Set $objBinFile = $objFSO.GetFile( $path )
	Set $objBinStream = $objBinFile.OpenAsTextStream( 1, 0 )
	$i = -1
	
	Do
		$i = $i + 1
		redim preserve $strLine[$i+1]
		$intByte = Asc( $objBinStream.Read(1) )
		$strLine[$i] = $intByte
	Loop Until $objBinStream.AtEndOfStream
	
	$ReadBin = $strLine
endfunction

public sub WriteBin($path, $ArrayByte)
	
	$objFSO  = CreateObject( "Scripting.FileSystemObject" )
	Set $objFile = $objFSO.CreateTextFile( $path, 0, 0 )
	
	For $i = 0 To UBound( $ArrayByte )
		$objFile.Write( Chr( $ArrayByte[$i] ))
	Next
	
	$objFile.Close()
	
	$objShell  = CreateObject( "Shell.Application" )
	Set $objParent = $objShell.NameSpace( $objFSO.GetParentFolderName( $objFSO.GetAbsolutePathName( $path ) ) )
	Set $objFile   = $objParent.ParseName( $path )
	$objFile.ModifyDate  = Date() & " " & Time()
	Set $objFile   = Nothing
	Set $objFSO    = Nothing
	Set $objShell  = Nothing

endsub

public sub WriteBin_append($file, $ArrayByte)

	$objFSO  = CreateObject( "Scripting.FileSystemObject" )
	Set $objFile = $objFSO.OpenTextFile( $file,  8, True )
	
	For $i = 0 To UBound( $ArrayByte )
		$objFile.Write( Chr( $ArrayByte[$i] ))
	Next
	
	$objFile.Close()
	$objBinFile.Close()

endsub


Arend_
(MM club member)
2020-10-05 01:02 AM
Re: Binary File Prepend

Kix2vbs is not really a failsafe thing, more of a guideline.
Try this (untested):
 Code:
function ReadBin($path)
  $objFSO = CreateObject("Scripting.FileSystemObject")
  $objBinFile = $objFSO.GetFile($path)
  $objBinStream = $objBinFile.OpenAsTextStream(1, 0)
  $i = -1
  Do
    $i = $i + 1
    redim preserve $strLine[$i+1]
    $intByte = Asc($objBinStream.Read(1))
    $strLine[$i] = $intByte
  Until $objBinStream.AtEndOfStream
  $ReadBin = $strLine
endfunction

Function WriteBin($path, $ArrayByte)
  $objFSO = CreateObject("Scripting.FileSystemObject")
  $objFile = $objFSO.CreateTextFile($path, 0, 0)
  For $i = 0 To UBound($ArrayByte)
    $objFile.Write(Chr($ArrayByte[$i]))
  Next
  $objFile.Close()
  $objShell = CreateObject("Shell.Application")
  $objParent = $objShell.NameSpace( $objFSO.GetParentFolderName($objFSO.GetAbsolutePathName($path)))
  $objFile = $objParent.ParseName($path)
  $objFile.ModifyDate  = @DATE + " " + @TIME
  $objFile = ""
  $objFSO = ""
  $objShell = ""
endfunction

Function WriteBin_append($file, $ArrayByte)
  $objFSO = CreateObject("Scripting.FileSystemObject")
  $objFile = $objFSO.OpenTextFile($file,  8, 1)
  For $i = 0 To UBound($ArrayByte)
    $objFile.Write(Chr($ArrayByte[$i]))
  Next
  $objFile.Close()
  $objBinFile.Close()
endfunction


AndreLuiz
(Getting the hang of it)
2020-10-05 04:08 AM
Re: Binary File Prepend

[Pt-BR]
Criei um script kix que pega o .exe dividi ele em partes, apenas para teste.
Para não desconsiderar a seu modo de operar, criei uma versão usando essa udf, fiz apenas algumas modificações nada que vá mudar o núcleo da coisa!

Mas veja que não vale apena trabalhar como ele diretamente, pois ele demora um século para ler o .exe, mas tem um versão funcional usando o KixWithClass(Que usa Kix2Vbs).

Como é bastante arquivos não consegui colocar tudo aqui, então estará tudo compactado em .zip


Arquivos separados da seguinte maneira:

split_file.kix ; Divide o arquivo em um subpasta chamada "Teste".
OnlyKix_split_file.kix ; Faz o mesmo acima, porém usando apenas kix.

join_file.kix ; Junta as partes de arquivos em um .exe funcional novamente dentro da subpasta "Teste".
OnlyKix_join_file.kix ; Faz o mesmo acima, porém usando apenas kix.



[ENG(GOOGLE TRANSLATE)]
I created a kix script that takes the .exe i divided it into parts, just for testing.
Not to disregard the way you operate, I created a version using this udf, I made just a few modifications nothing that will change the core of the thing!

But you see that it's not worth working like it directly, as it takes a century to read .exe, but it has a functional version using KixWithClass(which uses Kix2Vbs).


As it is enough files I could not put everything here, so it will all be compressed in .zip

split_file.kix ; Divides the file into a subfolder called "Test."
OnlyKix_split_file.kix ; It does the same above, however using only kix.

join_file.kix ; Joins the parts of files into a functional .exe again within the "Test" subfolder.
OnlyKix_join_file.kix ; It does the same above, however using only kix.