Jump to content


Photo

Get Size Of File


  • Please log in to reply
14 replies to this topic

#1 Wireless

Wireless

    GMC Member

  • GMC Member
  • 164 posts
  • Version:GM:Studio

Posted 10 January 2010 - 08:26 PM

As the title an the description says, this is a dll to simply check size of a file
without having to use the file_bin_open functions.

Why do I need this?
Well, if you ever need to check size of a used/busy file, then this functions can
be really helpful. This is also much faster, and if you want to index a folder with
files and find their size, this can be useful. Also, if the file is in use in that folder
you want to index, you can't because file_bin_open can't open busy files, but
this function can still read the size.

General information.
This dll was originally written by me, but because of extremely huge file size
(my fault), and a few bugs with file bigger than 4gb. score_under rewrote the
dll in asm, so now it supports files over 4gb and the dll size itself is under 1kb.

The dll file contains only one function witch is:

get_file_size(filename);

Download:
Posted Image


The zipfile comes with an example of how to use it (for whatever reason you
would need that..), the filesize.dll and the gml script.

Enjoy, and there is no need to give credits if used.

Edited by Wireless, 12 January 2010 - 02:26 PM.

  • 0

#2 Hockeyflyers

Hockeyflyers

    Hockeyplayer Games

  • New Member
  • 1108 posts
  • Version:Unknown

Posted 10 January 2010 - 08:53 PM

//file_size(name)
var f,r;
f = file_bin_open(argument0,0);
r = file_bin_size(f);
file_bin_close(f);
return r;
what's wrong with that script?

Edited by Hockeyflyers, 10 January 2010 - 08:54 PM.

  • 0

#3 Wireless

Wireless

    GMC Member

  • GMC Member
  • 164 posts
  • Version:GM:Studio

Posted 10 January 2010 - 08:56 PM

//file_size(name)
  var f,r;
  f = file_bin_open(argument0,0);
  r = file_bin_size(f);
  file_bin_close(f);
  return r;
what's wrong with that script?


Do I have to repeat my self?


Well, if you ever need to check size of a used/busy file, then this functions can be really helpful.
And you do not need to know whether it is a text file or a binary file.


  • 0

#4 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 10 January 2010 - 10:48 PM

And you do not need to know whether it is a text file or a binary file. 

There is no difference. Both are stored as a series of bytes. What makes a plain text file different is it only uses bytes that (in ASCII) represent the characters A-Z, a-z, 0-9 and symbols `~!@#$%^&*()_+-= ;:'",<>/?\|.
As opposed to binary files, which simply store bytes as a number 0-255, which is why a binary file will look like mostly junk if opened in a text editor.

In other words, both can be (more) easily read using binary file handling.

Why this is actually useful is because file_bin_size scrolls to the end of the file in memory to return the size, which can be slow for larger files. This might be quicker.

Edited by PlasticineGuy, 10 January 2010 - 10:52 PM.

  • 0

#5 Wireless

Wireless

    GMC Member

  • GMC Member
  • 164 posts
  • Version:GM:Studio

Posted 11 January 2010 - 07:38 AM

Why this is actually useful is because file_bin_size scrolls to the end of the file in memory to return the size, which can be slow for larger files. This might be quicker.


True, it is much faster, and if you want to index a folder with files and find their size, this can be usefull.
Also, if the file is in use in that folder you want index, you can't because file_bin_open can't open busy files, but my function can still read the size. :D
  • 0

#6 score_under

score_under

    Least kawaii

  • GMC Member
  • 1319 posts

Posted 11 January 2010 - 08:08 PM

844KB? That's insane! :(

I wrote a quick drop-in replacement (i.e. paste over original file). The difference is that it was programmed in ASM, supports files >4GB, and uses Windows API. It returns -1 on error.
Download - 918 bytes. Yes, bytes. :D (Yes, this does mean that the original is over 940x larger.)

Also, your DLL is cdecl, not stdcall.

(What I mean by >4GB is this. My DLL says:

Size of f:\donotdelete2.iso is 10737418240 bytes

Your original version says

Size of f:\donotdelete2.iso is -2147483648 bytes

The original file was 10GB. It's my second truecrypt drive.)

The ASM source (FASM, Flat Assembler format) is:
format PE GUI 4.0 DLLentry dllepinclude 'win32a.inc'section '.code' code import data executable readable writeablelibrary k,'kernel32.dll',n,'ntdll.dll'import k,CreateFile,'CreateFileA',CloseHandle,'CloseHandle',\         GetFileSize,'GetFileSize'import n,GetLastError,'RtlGetLastWin32Error'dllep:  xor eax,eax  inc eax  retn 0x10gfs:  push edi  push ebp  mov ebp,esp  push 0  push 0  push OPEN_EXISTING  push 0  push FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE  push 0x80;FILE_READ_ATTRIBUTES  push dword[ebp+0xC]  call [CreateFile]  inc eax  jz fail  lea edi,[eax-1]  push 0  push esp  push edi  call [GetFileSize]  inc eax  jnz nofail  push eax  call [GetLastError]  test eax,eax  jnz fail2  pop eaxnofail:  dec eax  push eax  fild qword[esp]  push edi  call [CloseHandle]  leave  pop edi  retnfail2:  push edi  call [CloseHandle]fail:  fldz  fld1  fsubp st1,st  leave  pop edi  retnsection 'export' export data readableexport 'GetFileSize.dll',gfs,'get_file_size'section 'reloc' fixups data discardable
(Ech, I know a FLD1+FCHS would have sufficed for the -1...)
I used a little PE hackery to reduce the size by more than half, though.

And as for this being useful, GM's binary functions couldn't open my truecrypt drive for obvious reasons :unsure:

Edited by score_under, 11 January 2010 - 10:02 PM.

  • 0

#7 Wireless

Wireless

    GMC Member

  • GMC Member
  • 164 posts
  • Version:GM:Studio

Posted 11 January 2010 - 10:49 PM

And as for this being useful, GM's binary functions couldn't open my truecrypt drive for obvious reasons  :D

Lol! Thanks for the replay btw.

Good thing you reacted on the file size.. I didn't realize it before now. The reason for the big file size is that I forgot to remove all the unused libraries I was using to find a way to create this dll, when I removed them I got 18kb, but that can not compete to you're 800bytes dll anyway xD

And for the strange output you get when files is over 4gb, isn't it possible to set the function I used to 64bit or something?

Edited by Wireless, 11 January 2010 - 10:56 PM.

  • 0

#8 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 12 January 2010 - 12:04 AM

If you used C++, you can use a long long. Not sure about how to return it though.

(offtopic)@score_under: I always wanted to learn asm. How hard is it?
  • 0

#9 LoopStan

LoopStan

    North-See Developer

  • GMC Member
  • 1398 posts

Posted 12 January 2010 - 01:06 AM

Its hard. Just go to wiki or google and find some tutorials. Great dll but i would have to go with the ASM made one, sorry. I can't be lugging around that much.
  • 0

#10 Clam

Clam

    GMC Member

  • New Member
  • 55 posts

Posted 12 January 2010 - 01:32 AM

While we're offering alternatives:
kernel32 = gffi_dll_load("kernel32.dll",gffi.STDCALL);
GetFileAttributesEx = gffi_function_define(kernel32,"GetFileAttributesExA",gffi.INT,gffi.STRING,gffi.INT,gffi.POINTER)
FILETIME = gffi_struct_def_create(gffi.UINT32,gffi.UINT32)
WIN32_FILE_ATTRIBUTE_DATA = gffi_struct_def_create(gffi.UINT32,FILETIME,FILETIME,FILETIME,gffi.UI
NT32,gffi.UINT32)
result = gffi_struct_create(WIN32_FILE_ATTRIBUTE_DATA)
gffi_function_call(GetFileAttributesEx,"gffi.gmk",0,gffi_struct_pointer(result))
size = gffi_struct_get(result,5) | gffi_struct_get(result,4) << 32
gffi.dll is 55kB, but it's also reusable B-).

edit: Also, the original was 844kB because it was compiled with debug symbols, stripping it took it down to 268

Edited by Clam, 12 January 2010 - 01:37 AM.

  • 0

#11 Wireless

Wireless

    GMC Member

  • GMC Member
  • 164 posts
  • Version:GM:Studio

Posted 12 January 2010 - 11:11 AM

As I said over, I've managed to strip it down to 18kb, I can re-upload, but it seems stupid because of the asm version.
  • 0

#12 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 12 January 2010 - 01:31 PM

Yeah.. asm is efficient. Insanely so. But very hard to program (so far I haven't done anything other than make a console program that asks you for your name and says Hi, <name>!).
  • 0

#13 Wireless

Wireless

    GMC Member

  • GMC Member
  • 164 posts
  • Version:GM:Studio

Posted 12 January 2010 - 02:27 PM

Okay, so I re-uploaded it with score_under's asm dll, and added additional info.
  • 0

#14 score_under

score_under

    Least kawaii

  • GMC Member
  • 1319 posts

Posted 12 January 2010 - 06:09 PM

If you used C++, you can use a long long. Not sure about how to return it though.

And for the strange output you get when files is over 4gb, isn't it possible to set the function I used to 64bit or something?


GetFileSize returns the lower part, and takes a pointer to the upper part. I exploited the properties of the stack to load it as a 64-bit integer into the FPU registers with almost no effort at all.

(offtopic)@score_under: I always wanted to learn asm. How hard is it?

Depends on how you code B-)

I mean, people say it's hard, but really:
C:
int __fastcall Factorial(int x)
{
  for(i=1;x>1;x--)i*=x;
  return i;
}
ASM: http://pastebin.com/f4314af22 (The codebox here messes up the prettiness :P I also added 2 versions there.)
That wasn't very easy to follow, I admit, but ECX was "x", and EAX was "i". MUL multiplies EAX by the argument you give it, and stores the lower 32-bit part of the result in EAX and the higher part in EDX.
It's considered "acceptable" for most functions to modify the EAX, ECX, and EDX variables when they return - others, like EBP and EDI, should be saved (pushed at the beginning, popped at the end).

It does take a lot of memorizing opcodes though, but that takes a little time, a few lookups, and a lot of messing in OllyDBG and FASM. :P

FASM also seems to have a fairly "unique" syntax at times, but you have to love it ;)
http://flatassembler...?article=manual
http://flatassembler...php?article=faq

ASM always seems hard at first, but as you learn more it gets much easier.
  • 0

#15 Funk E. Gamez

Funk E. Gamez

    GMC Member

  • GMC Member
  • 177 posts

Posted 31 January 2010 - 03:16 PM

Thank you so much, this is precisely what I needed! :whistle:
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users