Skip to content
View in the app

A better way to browse. Learn more.

ResHax

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
Help us keep the site running.

reading and writing 64bit integer of binary in C

Featured Replies

  • Author
  • Localization

Shokoniraya, posted Mon Sep 13, 2021 7:38 pm (66345)


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 , 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)
  • Author
  • Localization

aluigi, posted Tue Sep 14, 2021 9:29 am (66356)


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.
  • Author
  • Localization

Shokoniraya, posted Tue Sep 14, 2021 4:35 pm (66371)


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!!!!!!!!!!!

wwise_bnk_tool.c

  • Author
  • Localization

aluigi, posted Tue Sep 14, 2021 5:45 pm (66373)


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("xx". (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).
  • Author
  • Localization

Shokoniraya, posted Tue Sep 14, 2021 5:59 pm (66376)


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
  • Author
  • Localization

aluigi, posted Tue Sep 14, 2021 6:03 pm (66377)


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.
  • Author
  • Localization

Shokoniraya, posted Tue Sep 14, 2021 6:14 pm (66378)


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
  • Author
  • Localization

aluigi, posted Tue Sep 14, 2021 6:30 pm (66380)


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
  • Author
  • Localization

Shokoniraya, posted Tue Sep 14, 2021 8:58 pm (66389)


thank you, by using with 8192, now it has a worderful speed, 140mb in 3sec!
  • Author
  • Localization

Shokoniraya, posted Tue Sep 14, 2021 11:11 pm (66394)


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

inejct_file.c

Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.