ZenHAX

Free Game Research Forum | Official QuickBMS support | twitter @zenhax | SSL HTTPS://zenhax.com
It is currently Sun Nov 28, 2021 5:51 pm

All times are UTC




Post new topic  Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Mon Sep 13, 2021 7:38 pm 
User avatar

Joined: Sat Sep 15, 2018 5:22 am
Posts: 406
currently, im working with C (not C++ or C#). it's fast and good
my problem is that how can i read 64_int blocks in C
i can use read(); and write(); of <fcntl.h>, but they can't do the job for 64bit value!

my main question is that is there any io library or header file that i can use it? or i missed something about 64 bit stuffs? (im using 32bit version of tcc)

and one more question, how can i replace a few bytes in binary with c? (or remove some bytes and inject new bytes in middle of file)


Top
   
PostPosted: Tue Sep 14, 2021 9:29 am 
Site Admin
User avatar

Joined: Wed Jul 30, 2014 9:32 pm
Posts: 12568
Isn't this enough?
Code:
long long var;
fread(&var, 1, sizeof(var), fd);


Enlarging or shrinking a file by putting/removing something in the middle is not easy, it's ok to do in memory (some work of memmove) and then saving to file.


Top
   
PostPosted: Tue Sep 14, 2021 4:35 pm 
User avatar

Joined: Sat Sep 15, 2018 5:22 am
Posts: 406
fread function with long long var; can read 64 blocks, but not showing 64 integer number in output, is that possible that 32bit compiler can't deal with it?




i attached my code, it will read 16 byte and trying to inject "test.bin" to input file
but it's super slow, and storing a 400 mg file in memory and write it to file with my old pc is really annoying and no make any sense
is there any way to do it in a optimize way? in this case, i mean overwriting fast
like overwritng binary of a file to a specified offset of another file
use: exe_name -e file

slow parts commented with /////START OF SUPER SLOW!!!!!!!!!!! and ends on /////END OF SUPER SLOW!!!!!!!!!!!


Attachments:
wwise_bnk_tool.c [5.44 KiB]
Downloaded 37 times
Top
   
PostPosted: Tue Sep 14, 2021 5:45 pm 
Site Admin
User avatar

Joined: Wed Jul 30, 2014 9:32 pm
Posts: 12568
Do you mean not showing to output when using printf?
In that case, yeah printf needs a special type to display decimal 64bit numbers, but they depend by your compiler: %I64d, %lld and so on.
If you need hexadecimal view and want something easy use: printf("%08x%08x". (int)(num64 >> 32), (int)num64);

Regarding the slowness, consider that working on the memory and dumping it to file is faster than shriking and enlarging the file itself because a memmove is thousands times faster than a block-by-block or byte-per-byte operation on file for reserving/removing space + ftruncate if shrinked... it's a mess (indeed quickbms does reimport3 on memory and then back to file if I remember correctly).


Top
   
PostPosted: Tue Sep 14, 2021 5:59 pm 
User avatar

Joined: Sat Sep 15, 2018 5:22 am
Posts: 406
how about this?
Code:
write(target_file, &porting_file, 4096);


i found out that it will dump buffers from memory(not porting_file), if there will be a way to change it, it can be done


Top
   
PostPosted: Tue Sep 14, 2021 6:03 pm 
Site Admin
User avatar

Joined: Wed Jul 30, 2014 9:32 pm
Posts: 12568
I don't think I understand what you mean.

If your goal is "replacing" some bytes, that's ok since it's just seek+write.

The problem is removing/adding because it requires to move portions of the file.


Top
   
PostPosted: Tue Sep 14, 2021 6:14 pm 
User avatar

Joined: Sat Sep 15, 2018 5:22 am
Posts: 406
i just need a way to fast writing (not byte per byte by script)

Code:
write(target_file, &porting_file, 4096);

this function can read and write as well, but this two thing will explain what i mean

1) write(target_file, &porting_file, 4096);
this command can write, but when i set &porting_file, it will dump real memory to target_file, but it suppose to be porting_file content, not memory ram content, it writes memory ram content to target_file instead of porting_file binary to target_file (maybe &porting_file is wrong, what should i do?)

2) my script can enlarge and shrink file, but main problem is that writing byte per byte is not fast


Top
   
PostPosted: Tue Sep 14, 2021 6:30 pm 
Site Admin
User avatar

Joined: Wed Jul 30, 2014 9:32 pm
Posts: 12568
Ok so you want to copy the file without copying it whole in memory and without doing it byte-per-byte.
You need to copy it using blocks of memory, for example:
Code:
    static unsigned char    tmp[4096];
    for(len = 0; len < size; len += t) {
        int t = sizeof(tmp);
        if((size - len) < t) t = size - len;
        t = fread(ftmp, 1, t, file_input);
        if(t <= 0) break;
        t = fwrite(tmp, 1, t, file_output);
        if(t <= 0) break;
    }

size is the size of file_input


Top
   
PostPosted: Tue Sep 14, 2021 8:58 pm 
User avatar

Joined: Sat Sep 15, 2018 5:22 am
Posts: 406
thank you, by using with 8192, now it has a worderful speed, 140mb in 3sec!


Top
   
PostPosted: Tue Sep 14, 2021 11:11 pm 
User avatar

Joined: Sat Sep 15, 2018 5:22 am
Posts: 406
thank you sir aluigi, you are great!

by the way, i attached my file, in case if someone need it

note: function name is inject_binary

usage: inject_binary(fd, bnk_name, "test.bin", 4 );
fd= name of your target file [open it with open(); function of fcntl.h (check io.h for more information)]
bnk_name= your target file that you want to do inject or replace in it, same as fd, but you have to use multi-char for it
"test.bin"= name of the file that will inject or replace to your target file
4= you can put any size that fat32 supports it, 0 size means inject, and none-zero size is for selecting specified bye (in this example, it's 4), and replace your target 4 byte with your "test.bin"

important note: injecting/replacing offset is current location of fd


Attachments:
inejct_file.c [6.56 KiB]
Downloaded 41 times
Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 10 posts ] 

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Limited