Export binary from datasection as file?

Just starting out? Need help? Post your questions and find answers here.
highend
Enthusiast
Enthusiast
Posts: 162
Joined: Tue Jun 17, 2014 4:49 pm

Export binary from datasection as file?

Post 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!
User avatar
NicTheQuick
Addict
Addict
Posts: 1503
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Export binary from datasection as file?

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
highend
Enthusiast
Enthusiast
Posts: 162
Joined: Tue Jun 17, 2014 4:49 pm

Re: Export binary from datasection as file?

Post 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
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: Export binary from datasection as file?

Post 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
Post Reply