Creating files with offsets values

Doubts, help and support about QuickBMS and other game research tools
Racial
Posts: 12
Joined: Thu May 05, 2016 10:53 pm

Creating files with offsets values

Post by Racial »

Hi, ive got a file that contains a lot of files inside, generally files taked from PS2 games. The number of files inside its marked in the first 4 bytes, as little endian. The file has too a final offset to show the finish.
Each file got a marked size, calculating with the next offset start, or finish byte.

I want to create a plugin to export each file with an order number (decimal byte position on offset) and make a recompiler taking the file names (position on offset) to create a new file with all inside.

Or there is something similar?


Example of header.
Image
Red its number of files inside
Green are files start offsets (the first for example its 0x40 = 64 bytes decimal)
Blue its final of the file.

Thanks a lot for reading :)
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Creating files with offsets values

Post by aluigi »

Try this:

Code: Select all

get FILES long
    get OFFSET long
for i = 0 < FILES
    savepos TMP
    math TMP - 4
    get NEXT_OFFSET long
    math SIZE = NEXT_OFFSET
    math SIZE - OFFSET
    string NAME p "%d" TMP
    log NAME OFFSET SIZE
    math OFFSET = NEXT_OFFSET
next i

*edit* new script
Racial
Posts: 12
Joined: Thu May 05, 2016 10:53 pm

Re: Creating files with offsets values

Post by Racial »

Thanks for help me
There are troubles with that script. It exports the first file in all cases so name of all files are all equal (and worng).
And the other problem its the name, its the offset in decimal, i want to put the order of file or byte where offset its placed in file.

For the name in the example
40 00 00 00 = 0004
00 20 00 00 = 0008
80 47 00 00 = 0012
80 4A 00 00 = 0016

With that names, i can get the info to rebuild the file with the separated ones
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Creating files with offsets values

Post by aluigi »

Script fixed.
Racial
Posts: 12
Joined: Thu May 05, 2016 10:53 pm

Re: Creating files with offsets values

Post by Racial »

Perfect, really thanks. Export that amount of files manually takes a long time It seems to work on extraction of the file. Even i tried with a long file (about 400 files inside) and it works.

But for rebuild the file, taking the names of the files to an offset index i used reimport tool of QuickBMS but doesn't work, takes me an error on the first file.

I see this code, but doesn't create the file index at start (number of files and start-finish of each one)

Code: Select all

get SIZE asize  # takes the size of the current file
append # enable the append mode for concatenating them
log "new.bin" 0 SIZE
append # disable concatenation (not necessary if its there are no other operations)


what can i add to that code to build it?

PD: Even if QuickBMS doesn't recognise the amount of files and i need to put the number of entries in the .BMS isn't a big problem, there is about 5 or 6 types of files that contain the same amount on each one, so edit a number for each one isn't a big problem
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Creating files with offsets values

Post by aluigi »

The reimport mode of quickbms works like a reinjector replacing the original bytes with the new ones, everything is writtein in section 3 of quickbms.txt.

Probably what you want is a rebuilder that takes the files and recreated the archive from scratch.
It's very easy for that simple archive format so I leave it to you or anyone else interested as exercise :)
Racial
Posts: 12
Joined: Thu May 05, 2016 10:53 pm

Re: Creating files with offsets values

Post by Racial »

Ok, i will try it.

Do you suggest me to use base of that script or make a completly new one? Sorry but im noob xD even if you got a tutorial it will be helpfull :)

EDIT: No matter i will see examples and the quickbms.txt help
Thanks again :)
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Creating files with offsets values

Post by aluigi »

Using quickbms for building stuff requires some skills so I suggest you to use a real programming language.
Racial
Posts: 12
Joined: Thu May 05, 2016 10:53 pm

Re: Creating files with offsets values

Post by Racial »

Exactly, maybe the best idea is to use a real language but also accept a challenge can be more rewarding.
I was planning that the script will do, i think that it's the first step.

For start i get the merged files so i can treat that file as a temporary that will put after the first (index) that i want to do or save as 2 files an then merge.

To make the index first of all i need to write FILES long.
Then need to put math FILES long * 4 + 4 (to put the start of the first file calculating index size)
To calculate the next value i need to take file SIZE + PREVIOUS_OFFSET
And finish in last file.

But i dont know how commands use to treat that type of data.

Thats creates me the merge files
get SIZE asize
append
log "Data.file" 0 SIZE
append

If i want to create a data for each file i dont know how to do it, how to tell to the program when he need to give back a value.

Yes, i will see it too with a friend who knows java, but this is personal with that files lol
I really appreciate your help, sorry for my poor english and poor knowledge
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Creating files with offsets values

Post by aluigi »

I have found a topic that may be of interest for you:
viewtopic.php?f=9&t=1104
Racial
Posts: 12
Joined: Thu May 05, 2016 10:53 pm

Re: Creating files with offsets values

Post by Racial »

I see it, uses some information that i need to manage. But what about the order? It get first 100, then 1000, 1000 ones, 104 and continues.

Chinese basic for me xDDDD lol i know, this isn't for me
I need to see it with more patience :)
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Creating files with offsets values

Post by aluigi »

Code: Select all

# 1) script.bms
# 2) the input file is ignored, select the same script
# 3) the output folder is the folder containing the files to pack

set OUTPUT_ARCHIVE string "dump.pak"

# init the buffers
log MEMORY_FILE  0 0
log MEMORY_FILE2 0 0

for FILES = 0

    # copy&paste code
    scanDir "." NAME SIZE       # get the file from the current folder
    if NAME == ""               # no other files are available so append index
        break
    endif
    open "." NAME
    string NAME << 2            # remove ".\"
    if NAME != OUTPUT_ARCHIVE   # in case the archive already exists
        # end of copy&paste code

        # add information to the index table
        string NAME R \ /
        strlen NAMESZ NAME
        get OFFSET asize MEMORY_FILE2
        putarray 0 FILES OFFSET

        # add the file to the archive
        append
        log MEMORY_FILE2 0 SIZE
        padding 16 MEMORY_FILE2
        append
        math FILES + 1
    endif

next

        # last offset
        get OFFSET asize MEMORY_FILE2
        putarray 0 FILES OFFSET

put FILES long MEMORY_FILE
xmath BASE_OFF "4 + (FILES + 1) * 4"
math BASE_OFF x 16
for i = 0 <= FILES
    getarray OFFSET 0 i
    math OFFSET + BASE_OFF
    put OFFSET long MEMORY_FILE
next i
for
    get TMP asize MEMORY_FILE
    if TMP == BASE_OFF
        break
    endif
    put 0 long MEMORY_FILE
next

get SIZE asize MEMORY_FILE2
append
log MEMORY_FILE 0 SIZE MEMORY_FILE2
append

get SIZE asize MEMORY_FILE
log OUTPUT_ARCHIVE 0 SIZE MEMORY_FILE

I want to remember to anyone that quickbms is not the best option for generating files... but it works.
Racial
Posts: 12
Joined: Thu May 05, 2016 10:53 pm

Re: Creating files with offsets values

Post by Racial »

Sorry if i bore you, and i will know if you don't want to help me, but...
There is a way to add to the last code the size of each file?

Example
Image

It's for AFS File format.

Greets
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Creating files with offsets values

Post by aluigi »

Yes, after the first "putarray" add:
putarray 1 FILES SIZE

After the "put OFFSET" add:
getarray SIZE 1 i
put SIZE long MEMORY_FILE

*edit* fixed
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Creating files with offsets values

Post by aluigi »

There was a bug in the previous patch, now fixed.