Page 1 of 1

Export binary from datasection as file?

Posted: Fri Feb 07, 2025 12:19 pm
by highend
Hi.

My datasection(s) are all defined like this:

Code: Select all

    DataSection
      RustDesk_Client:
      IncludeBinary "..\#includes_files\rustdesk-1.3.7-x86_64.exe"
      Data.c 0 ; Add a terminating null-character

      RustDesk_Toml:
      IncludeBinary "..\#includes_files\RustDesk.toml"
      Data.c 0

      RustDesk_Toml2:
      IncludeBinary "..\#includes_files\RustDesk2.toml"
      Data.c 0
    EndDataSection
How would the easiest and fastest function like:

Code: Select all

ExportFromDatasectionAsFile(filePath.s, label.s)
look like?

Thanks!

Re: Export binary from datasection as file?

Posted: Fri Feb 07, 2025 12:29 pm
by NicTheQuick
First of all. A terminating null characters makes no sense after binary data because binary data most likely will contain null characters on their own.
If you want to extract the binary data you always have to use a label before and after the data and calculate the difference in bytes like so:

Code: Select all

DataSection
	RustDesk_Client_begin:
	IncludeBinary "..\#includes_files\rustdesk-1.3.7-x86_64.exe"
	RustDesk_Client_end:
	
	RustDesk_Toml_begin:
	IncludeBinary "..\#includes_files\RustDesk.toml"
	RustDesk_Toml_end:
	
	RustDesk_Toml2_begin:
	IncludeBinary "..\#includes_files\RustDesk2.toml"
	RustDesk_Toml2_end:
EndDataSection

Procedure ExportFromDataSectionAsFile(filePath.s, *begin, *end)
	Protected hFile.i
	
	hFile = CreateFile(#PB_Any, filePath)
	If Not hFile
		ProcedureReturn #False
	EndIf
	WriteData(hFile, *begin, *end - *begin)
	CloseFile(hFile)
	ProcedureReturn #True
EndProcedure

; Example call
ExportFromDataSectionAsFile("C:\rustdesk.exe", ?RustDesk_Client_begin, ?RustDesk_Client_end)
And as you can see you can not handle the labels as strings because at runtime these names are gone. You directly have to use their value which is a pointer to the data.

Re: Export binary from datasection as file?

Posted: Fri Feb 07, 2025 1:49 pm
by highend
Thank you, NicTheQuick!

I'll rewrite my datasection(s) then and yes, it absolutely makes sense that the label isn't a string anymore at compile time... :mrgreen:

Regards,
highend

Re: Export binary from datasection as file?

Posted: Fri Feb 07, 2025 2:07 pm
by AZJIO

Code: Select all

Procedure SaveFile_Buff(File.s, *Buff, Size)
	Protected Result = #False
	Protected ID = CreateFile(#PB_Any, File)
	If ID
		If WriteData(ID, *Buff, Size) = Size
			Result = #True
		EndIf
		CloseFile(ID)
	EndIf
	ProcedureReturn Result
EndProcedure

SaveFile_Buff(ini$, ?ini, ?iniend - ?ini)

DataSection
	ini:
	IncludeBinary "sample.ini"
	iniend:
EndDataSection