Load Data and Memory

BlitzMax Forums/BlitzMax Programming/Load Data and Memory

BLaBZ(Posted 2013) [#1]
Hi All -

I need to 1. Unzip a file, 2. Decrypt it, 3. Load it via a string location

I know you can reference objects via "Incbin::" is there some sort of way to load things into incbin or something similar at runtime?

Thanks!


xlsior(Posted 2013) [#2]
incbinned filles are merged into the executable directly -- so unless you embedded the file at compilation time, you can't access 'new' files with incbin.

As far as zip files are concerned: there are several module sfor blitzmax that allow it to load file sdirectly from zipfiles, whether they are incbinned or exist in a normal folder somewhere:

there's Koriolis' zipstream module: http://blitzbasic.com/Community/posts.php?topic=71734

and gman's ZipEngine module (don't have a link handy, but google is your friend)

All you have to do after including the module, is do a loadfile"zip::myfile.zip/somefileinside.txt" or similar, and you can read/write directly to/from the zip. IIRC both also support password protected, encrypted zip files.


Brucey(Posted 2013) [#3]
Incbin is for embedding files inside your executable. There's no reason you'd want to do the same at runtime. You can simply load a file from the filesystem and process it how you need to.

If you mean, "how do I use incbin to load a file?", then you just need to import the files you want embedded, and then load them prefixed with "incbin::".

For example :
Import "mydir/myfile.txt"

..

Local s:String = LoadString("incbin::mydir/myfile.txt")

If you are using a Framework (which you should), you will need to include BRL.RamStream, to enable the "incbin::" functionality.

And like what xlsior says.


Midimaster(Posted 2013) [#4]
You can combine ZIP and INCBIN with koriolis zipstream module. I'm doing this always to have only one file for the user:

SuperStrict   
Import koriolis.zipstream
Incbin "ZipData.ZIP"
....
'DataPath="Source/"
DataPath="ZIP::Incbin::ZipData.ZIP//"

' if ZIP is password protected:
SetZipStreamPassword("ZipData.ZIP", "12345")

Background=LoadImage(DataPath + "Back.png")


With my variable DataPath$ it is easy to switch between original file datas and zipped datas during development.

Koriolis ZipStream is not able to read encrypted ZIPs, but password protected yes!

But normally there is no use for encryption anymore, because the ZIP is not longer a extern file, but part of the EXE because of the combination with INCBIN.


BLaBZ(Posted 2013) [#5]
Lets say I have an encrypted zip file, using the encryption found here:

http://www.blitzbasic.com/codearcs/codearcs.php?code=1711

In memory, I want to 1. Decrypt the Zip file, 2. Extract files from it

Example - The issue im having is in step 4

Strict

Import zipengine.zipengine

Local key:String = "09832hvb3@32inf32238hr#@NO#@Ndcq"


''''''''''''''''''''''''''''''''''
''''''SAVE AND ENCRYPT''''''''''''
''''''''''''''''''''''''''''''''''
'1.Zip file
Local zw:ZipWriter = New ZipWriter
zw.OpenZip("construction.zip",False)
zw.AddFile("construction.b3d")
zw.CloseZip()
'2.Encrypt Zip
SaveString(RC4(LoadString("construction.zip"),key),"construction.rc4")

''''''''''''''''''''''''''''''''''
''''''LOAD AND DECRYPT''''''''''''
''''''''''''''''''''''''''''''''''
'3. Load and Decrypt zip file
Local zFile:String = LoadString(RC4(LoadString("construction.rc4"),key))

'4. How to Turn zFile into a zip file I can extract files from in memory?
Local zr:ZipReader = New ZipReader
zr.OpenZip("con.zip")
Local data:TRamStream = zr.ExtractFile("con.rc4")


' New version
Function RC4$(inp$, key$)
    Local S%[512+Ceil(inp.Length*.55)]
    Local i%, j%, t%, x%
    Local outbuf@@ Ptr = Short Ptr(Varptr s[512])
    
    j = 0
    For i = 0 To 255
        S[i] = i
        If j > (Key.length-1) Then
            j = 0
        EndIf
        S[256+i] = key[j]&$ff
        j:+ 1
    Next
    
    j = 0
    For i = 0 To 255
        j = (j + S[i] + S[256+i]) & $ff
        t = S[i]
        S[i] = S[j]
        S[j] = t
    Next
    
    i = 0
    j = 0
    For Local x% = 0 To inp.Length-1
        i = (i + 1) & $ff
        j = (j + S[i]) & $ff
        t = S[i]
        S[i] = S[j]
        S[j] = t
        t = (S[i] + S[j]) & $ff
        outbuf[x] = (inp[x] ~ S[t])
    Next
    
    Return String.FromShorts(outbuf, inp.Length)
End Function