Also someone can want to get 64-bit file size in 32 bit mode.
program PE32_CUI 'readable_f_size';
#include 'platform_def.zir';
#include 'console.zir';
#include 'zirutils.zir';
#include 'strutils.zir';
byte units[] = [
0x62, 0x00, 0x00, 0x00, // byte // b
0x6B, 0x62, 0x00, 0x00, // kilobyte // kb
0x4D, 0x62, 0x00, 0x00, // Megabyte // Mb
0x47, 0x62, 0x00, 0x00, // Gigabyte // Gb
0x54, 0x62, 0x00, 0x00, // Terabyte // Tb
0x50, 0x62, 0x00, 0x00, // Petabyte // Pb
0x45, 0x62, 0x00, 0x00 // Exabyte // Eb
];
// this function returns valid values for
// sizes from 0 to 0xFFFFFFFFFFFFFDFF bytes (16 Exabytes - 512 bytes)
//
// eax += 512; adc edx, 0; << need to avoid
// rounding errors i.e. if 1700 bytes then
// return 2 kb(1.66 round to 2)
function ReadableFileSize64(dword bytesLo, bytesHi; char* buffer) {
uses ebx;
eax = bytesLo; edx = bytesHi; ecx = 0;
while ((eax => 1024) OR (edx <> 0)) {
eax += 512; adc edx, 0;
shrd eax, edx, $log2i(1024);
shr edx, $log2i(1024);
ecx++;
}
ebx = @[ecx*4 + @units];
dw2str(eax, buffer);
strAppend(buffer, ebx, -1);
return buffer;
}
char buf[32];
inline procedure Test($bytesHi, $bytesLo) {
eax = ReadableFileSize64($bytesLo, $bytesHi, @buf);
println([char*]eax);
}
// (hi_dword, lo_dword) // low and hi parts of qword filesize
test(0, 0);
test(0, 500);
test(0, 1000);
test(0, 1023);
test(0, 1024);
test(0, 1025);
test(0, 2000);
test(0, 2000000);
test(0, 1000000000);
test(0, 1073741824);
test(0, 2051234567);
test(0x1, 0xC0000000);
test(0x30FF, 0xFFFFFFE8);
test(0xD0000000, 0x00000000);
test(0xFFFFFFFF, 0xFFFFFDFF);
wait_key();
ExitProcess(0);
It also can be more readable when you fix some bugs(also bug with qword) :)