Pack Files into Compiled Exe

Just starting out? Need help? Post your questions and find answers here.
armixeight
New User
New User
Posts: 9
Joined: Sun Sep 13, 2009 4:34 am

Pack Files into Compiled Exe

Post by armixeight »

Hi was wondering if there is way to somehow include a type of file into a EXE compiled by PureBasic.

The Complete Scenario is:
I have an EXE that is designed to execute certain functions by interpreting a text file example (my basic idea):

Code: Select all

ReadFile( #FILE, "packed_file.txt" )
LINE = ReadLine( #FILE )

If LINE = "hello"
  MessageBox("The file said hello somewhere in it")
Endif

End
The problem is I don't know how to do this and if it is actually possible.
I would imagine that the Include Binary functions work when it comes to including a file and compiling it with the EXE but I'm after a way to externally add a file into the interpreter EXE which will then work like above. I think I need some sort of pointer inside the interpreter's code that will identify the file added and read from it but honestly i don't know.

Code: Select all

[EXTERNAL PROGRAM] == PACK FILE == >> [INTERPRETER] == READ TEXT FROM PACKED TEXT FILE == >> [OUTPUT].
Essentially I need to know how to Pack a file [external program code] and how to interpret the packed file [interpreter program code].

All of your help with be greatly appreciated. Thank you in advance.

~ArmixEIGHT
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Pack Files into Compiled Exe

Post by ts-soft »

PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Pack Files into Compiled Exe

Post by Nituvious »

I think this is exactly what I posted about but said in a better way! I am very interested in this topic too.
How do you pack an already compiled program with an external file filled with instructions for an interpreter?
▓▓▓▓▓▒▒▒▒▒░░░░░
armixeight
New User
New User
Posts: 9
Joined: Sun Sep 13, 2009 4:34 am

Re: Pack Files into Compiled Exe

Post by armixeight »

ts-soft
Thank you oh so very much for your beautiful code, that is exactly what I needed!
My project can now continue. :D

Nituvious
Nituvious wrote: I think this is exactly what I posted about but said in a better way! I am very interested in this topic too.
How do you pack an already compiled program with an external file filled with instructions for an interpreter?
I took the liberty of translating the German version into English to help others with this question. I hope you don't mind ts-soft. :D

All credit of course goes to ts-soft and his original post and pre-compiled example can be found here PureBasic German

[EXTERNAL PROGRAM]

Code: Select all

EnableExplicit

DataSection
  stubs:
  IncludeBinary "stubs\stubs.exe"
  stubs_end:
EndDataSection

Define.s TextFile, ExeFile
Define FF, *mem, length

TextFile = OpenFileRequester("Please choose a text file:", "readme.txt", "Text (*.txt)|*.txt", 0)
If TextFile
  FF = ReadFile(#PB_Any, TextFile)
  If FF
    length = Lof(FF)
    *mem = AllocateMemory(length)
    If *mem
      ReadData(FF, *mem, length)
    EndIf
    CloseFile(FF)
  EndIf
  
  ExeFile = SaveFileRequester("Please choose an output file:", "readme.exe", "Exe (*.exe)|*.exe", 0)
  
  If ExeFile
    FF = CreateFile(#PB_Any, ExeFile)
    If FF
      WriteData(FF, ?stubs, ?stubs_end - ?stubs)
      WriteData(FF, *mem, length)
      WriteQuad(FF, length)
      CloseFile(FF)
      
      RunProgram(ExeFile)
    EndIf
  EndIf
EndIf
[INTERPRETER]

Code: Select all

EnableExplicit

Define FF, length.q, *mem, Text.s

FF = ReadFile(#PB_Any, ProgramFilename())

If FF
  FileSeek(FF, Lof(FF) - SizeOf(Quad)) ; am Ende die länge des Angehängten lesen
  length = ReadQuad(FF)
  FileSeek(FF, Lof(FF) - SizeOf(Quad) - length) ; zum start des Angehängten
  If length
    *mem = AllocateMemory(length)
    If *mem
      ReadData(FF, *mem, length) ; Angehängtes in den Speicher
    EndIf
  EndIf
  CloseFile(FF)
  
  If *mem
    Text = PeekS(*mem)
    FreeMemory(*mem)
  EndIf
  
  
  OpenWindow(0, #PB_Ignore, #PB_Ignore, 640, 480, "Text2Exe", #PB_Window_SystemMenu)
  EditorGadget(0, 5, 5, 630, 470, #PB_Editor_ReadOnly)
  SetGadgetText(0, Text)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf
User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Re: Pack Files into Compiled Exe

Post by ultralazor »

use image struct to get offset of resource section, create and entry that will be scanned for and increase size in pe header if needed. This is 'proper' as it'll never break anything even in fragile embedded and .NET PE, unless it has crypto checks on itself..

it takes too much binary scanning and ordering to just whip one of for a post.
so many ideas so little time..
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Pack Files into Compiled Exe

Post by ts-soft »

This line goes to the end of file - quad (here is the length stored) - length (here begins your stuff).
This line is very simple :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Pack Files into Compiled Exe

Post by ts-soft »

Louise wrote:Thanks.
If I want a non-text files to store and use what parts do I need to change.?
For example :
Exe file or Bmp file.
There is nothing to change. You create a empty File with CreateFile and writes the datas
and close the file.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Re: Pack Files into Compiled Exe

Post by ultralazor »

FYI: The right way to do this, where it won't break if resource section of PE isn't the last or if relocation are handled differently, is to use the PE head information and append a resource struct according to PE spec.

I've done it where I can load inline asm from a resource.
so many ideas so little time..
Post Reply