Author Topic: Trouble with file length under Win64  (Read 3860 times)

Vortex

  • Member
  • *****
  • Posts: 2588
Re: Trouble with file length under Win64
« Reply #15 on: February 12, 2021, 04:22:23 AM »
Hi Gunther,

Reading this message :

http://masm32.com/board/index.php?topic=2769.msg29205#msg29205

Could you try the size prefixe %I64u ?

Code: [Select]
include     \masm32\include64\masm64rt.inc

EXTERN      ParseCmdLine:PROC

.data

msg1        db 'Size of the file %s = %I64u',13,10,0
msg2        db 'GetFileSize.exe filename.ext',13,10,0

.data?

cmdline     db 512 dup(?)

.code

start PROC

    call    main
    invoke  ExitProcess,0

start ENDP

main PROC

LOCAL .rsi:QWORD
LOCAL hFile:QWORD
LOCAL lpFileSizeHigh:QWORD

    mov     .rsi,rsi
    mov     rsi,OFFSET cmdline
    invoke  ParseCmdLine,rsi
    cmp     rax,2
    je      @f

    invoke  vc_printf,ADDR msg2
    ret
@@:   
    invoke  CreateFile,QWORD PTR [rsi+8],\
            GENERIC_READ,\
            0,0,OPEN_EXISTING,0,0

    mov     hFile,rax
   
    lea     rdx,lpFileSizeHigh
    invoke  GetFileSize,rax,rdx
   
    mov     rdx,lpFileSizeHigh
    shl     rdx,32
    add     rax,rdx
   
    invoke  vc_printf,ADDR msg1,\
            QWORD PTR [rsi+8],rax
           
    mov     rsi,.rsi
    ret

main ENDP

END

Gunther

  • Member
  • *****
  • Posts: 3738
  • Forgive your enemies, but never forget their names
Re: Trouble with file length under Win64
« Reply #16 on: February 12, 2021, 09:59:16 AM »
Erol,

Could you try the size prefixe %I64u ?

yes, but it didn't work. Here is the output:
Code: [Select]
Value as unsigned long long = 5368717488
Value as long long          = 5368717488
Press any key to continue...
The source and EXE is attached in the zip archive.

Reading this message :

http://masm32.com/board/index.php?topic=2769.msg29205#msg29205

Interesting thread, by the way. Why didn't Jochen mention that one?

Gunther
Get your facts first, and then you can distort them.

jj2007

  • Member
  • *****
  • Posts: 11583
  • Assembler is fun ;-)
    • MasmBasic
Re: Trouble with file length under Win64
« Reply #17 on: February 12, 2021, 11:33:46 AM »
Interesting thread, by the way. Why didn't Jochen mention that one?

> googling for "llu" format specifier "msvcrt.dll" reveals that it's a KnownBug in a KnownDLL ;-)

That's over 7 years ago, I can't remember all the times when we tortured the CRT :biggrin:


Vortex

  • Member
  • *****
  • Posts: 2588
Re: Trouble with file length under Win64
« Reply #18 on: February 12, 2021, 06:44:20 PM »
Hi Gunther,

Replacing ADDR Value with only Value :

Code: [Select]
        invoke     vc_printf,ADDR uint64_format,Value     ;  ADDR Value
        invoke     vc_printf,ADDR str_format,ADDR NL

        ; print as long long

        invoke     vc_printf,ADDR str_format,ADDR msg1
        invoke     vc_printf,ADDR int64_format,Value      ; ADDR Value

Building and running the application :

Code: [Select]
Value as unsigned long long = 18446744073709551615
Value as long long          = -1
Press any key to continue...

TouEnMasm

  • Member
  • *****
  • Posts: 1805
    • EditMasm
Re: Trouble with file length under Win64
« Reply #19 on: February 12, 2021, 08:56:48 PM »

a sample in 64:
Copy paste the full path of the file (console mode)

Code: [Select]
include sdk64.inc
include stdio.sdk
include conio.sdk

;https://docs.microsoft.com/en-us/windows/win32/secauthz/creating-a-security-descriptor-for-a-new-object-in-c--


;-c -nologo -coff -Zi4 -Zd
.LISTALL
.const
;------------------------------------MACROS--------------------------------------------------

PuPo MACRO M1,M2
Push M2
Pop M1
ENDM
PASSESPACETAB MACRO register
@@:
.if byte ptr [register] == " " || byte ptr [register] == 9
inc register
jmp @B
.endif
ENDM
;----------------------------------------------------------------------------------------------
.data
;--------------------------------------------------------------------------------------------------------------------
InfosFichiers WIN32_FIND_DATA <>
;key data

;--------------------------------------------
chaine db MAX_PATH dup(0)
szfile db MAX_PATH dup(0)
;-------------------------------------------------------------------------------------------------------------------
.code
include affiche_error_msg.inc
Import_File PROC FRAME
Local Hsearch:QWORD,retour:QWORD
mov retour,0
    invoke printf,TXT(" Copy paste the name of the file (full path) : ")
    invoke scanf,TXT("%s"),addr chaine
lea r11,chaine
PASSESPACETAB r11
invoke lstrcpy,addr szfile,R11
;test if the  file  exist and get the size
invoke FindFirstFile,addr szfile,addr InfosFichiers
          mov Hsearch,rax
.if rax != INVALID_HANDLE_VALUE
          invoke FindClose,Hsearch
mov retour,1
.endif
mov rax,retour
ret
Import_File endp

main proc ;c(32) or not c(64 fastcall) must be tested,not using main(standard entry crt) can need additionnal libraries
invoke Import_File
.if rax == 1
invoke printf,TXT(" Size of the file %u "),InfosFichiers.nFileSizeLow
.else
invoke printf,TXT(" File not found ")
.endif
invoke _getch

mov rax,0
ret
main endp

end

Fa is a musical note to play with CL

Gunther

  • Member
  • *****
  • Posts: 3738
  • Forgive your enemies, but never forget their names
Re: Trouble with file length under Win64
« Reply #20 on: February 12, 2021, 10:59:42 PM »
Thanks Yves.

a sample in 64:
Copy paste the full path of the file (console mode)

At the moment it is about the errors of the CRT when printing unsigned long long and long long.

Gunther
Get your facts first, and then you can distort them.

Gunther

  • Member
  • *****
  • Posts: 3738
  • Forgive your enemies, but never forget their names
Re: Trouble with file length under Win64
« Reply #21 on: February 12, 2021, 11:05:49 PM »
Erol,

Replacing ADDR Value with only Value :

that works fine. But that's a little strange, isn't it.

Jochen,

> googling for "llu" format specifier "msvcrt.dll" reveals that it's a KnownBug in a KnownDLL ;-)

That's over 7 years ago, I can't remember all the times when we tortured the CRT :biggrin:

in other words, you're just like me - becoming forgetful. This is quite normal in old age.  :cool:

Gunther
Get your facts first, and then you can distort them.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8539
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Trouble with file length under Win64
« Reply #22 on: February 12, 2021, 11:52:29 PM »
I have not been following this topic in detail but here is a quick test piece that drops the correct number for a file larger than 4 gig.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 entry_point proc

    LOCAL hFile :QWORD
    LOCAL flen  :QWORD
    LOCAL hiwd  :QWORD
    LOCAL lowd  :QWORD

    mov hFile, flopen("m:\$big\bigger.mp4")     ; 5,940,221,382 bytes

    invoke GetFileSize,hFile,ptr$(hiwd)
    mov flen, rax

    conout str$(flen),lf,lf
    conout str$(hiwd),lf,lf

    mov rax, flen
    mov rcx, 4294967296
    add rax, rcx                                ; add 4 gig

    conout str$(rax),lf

    flclose hFile

    waitkey
    .exit

 entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Gunther

  • Member
  • *****
  • Posts: 3738
  • Forgive your enemies, but never forget their names
Re: Trouble with file length under Win64
« Reply #23 on: February 13, 2021, 12:01:54 AM »
Steve,

I have not been following this topic in detail but here is a quick test piece that drops the correct number for a file larger than 4 gig.

yes, I've made a similar approach. conout (mighty macro) will do the job. Is it possible to print with conout long long integers, too?

Gunther 
Get your facts first, and then you can distort them.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8539
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Trouble with file length under Win64
« Reply #24 on: February 13, 2021, 12:16:54 AM »
Here is a PROC version that seems to work OK.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 entry_point proc

    rcall LargeFileSize,"m:\$big\bigger.mp4"
    conout str$(rax),lf

    waitkey
    .exit

 entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

LargeFileSize proc pName:QWORD

    USING r12
    LOCAL hFile :QWORD
    ; LOCAL flen  :QWORD
    LOCAL hiwd  :QWORD
    ; LOCAL lowd  :QWORD

    SaveRegs
    mov hFile, flopen(pName)

    invoke GetFileSize,hFile,ptr$(hiwd)
    cmp hiwd, 0
    je past

    mov rcx, 4294967296                         ; on the fly optimisation :)
  @@:
    add rax, rcx                                ; add 4 gig each iteration
    sub hiwd, 1
    jnz @B

  past:
    mov r12, rax                                ; preserve rax
    flclose hFile
    mov rax, r12                                ; restore rax
    RestoreRegs

    ret

LargeFileSize endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

TimoVJL

  • Member
  • ****
  • Posts: 732
Re: Trouble with file length under Win64
« Reply #25 on: February 13, 2021, 12:27:09 AM »
for big files and using long long
Code: [Select]
BOOL GetFileSizeEx(  HANDLE         hFile,  PLARGE_INTEGER lpFileSize);
May the source be with you

jj2007

  • Member
  • *****
  • Posts: 11583
  • Assembler is fun ;-)
    • MasmBasic
Re: Trouble with file length under Win64
« Reply #26 on: February 13, 2021, 12:31:57 AM »
for big files and using long long
Code: [Select]
BOOL GetFileSizeEx(  HANDLE         hFile,  PLARGE_INTEGER lpFileSize);

Good find :thumbsup:

However, some of the really big files are system files that CreateProcess can't handle, so FindFirst* is still the better choice...

GetFileSize is no good for certain hidden files (CreateFile fails), as I noted when testing for a large file:
Code: [Select]
include \masm32\MasmBasic\MasmBasic.inc
  Init
  .if Exist("C:\hiberfil.sys")
Inkey Str$("File size is %3f GB", edx::GfSize(-1)/1073741824)
  .else
Inkey "no such file"
  .endif
EndOfCode

File size is 4.39 GB (GfSize uses FindFirstFile instead of GetFileSize)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8539
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Trouble with file length under Win64
« Reply #27 on: February 13, 2021, 12:47:28 AM »
 :biggrin:

Never could resist the temptation to optimise an algo.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

LargeFileSize proc pName:QWORD

    USING r12, r13                              ; specify the registers to preserve
    LOCAL hiwd :QWORD                           ; allocate a LOCAL memory operand

    SaveRegs                                    ; save the regs specified in USING
    mov r13, flopen(pName)                      ; r13 is the file handle

    invoke GetFileSize,r13,ptr$(hiwd)
    cmp hiwd, 0                                 ; if hiwd = 0, bypass loop
    je past

    mov rcx, 4294967296                         ; load rcx with a 4 gig immediate
  @@:
    add rax, rcx                                ; add 4 gig each iteration
    sub hiwd, 1
    jnz @B

  past:
    mov r12, rax                                ; preserve rax
    flclose r13                                 ; close the file
    mov rax, r12                                ; restore rax
    RestoreRegs                                 ; restore the regs specified in USING

    ret

LargeFileSize endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Vortex

  • Member
  • *****
  • Posts: 2588
Re: Trouble with file length under Win64
« Reply #28 on: February 13, 2021, 12:49:53 AM »
Hi Gunther,

There is nothing strange. You would like to print the value of the variable named Value not it's address. Disassembling the object module :

Code: [Select]
        lea     rcx, [str_format]                       
        lea     rdx, [msg0]                             
        call    qword ptr [__imp_printf]               
        lea     rcx, [uint64_format]                   
        mov     rdx, qword ptr [Value]                 
        call    qword ptr [__imp_printf]               
        lea     rcx, [str_format]                       
        lea     rdx, [NL]                               
        call    qword ptr [__imp_printf]               
        lea     rcx, [str_format]                       
        lea     rdx, [msg1]                             
        call    qword ptr [__imp_printf]               
        lea     rcx, [int64_format]                     
        mov     rdx, qword ptr [Value]                 
        call    qword ptr [__imp_printf]               
        lea     rcx, [str_format]                       
        lea     rdx, [NL]                               
        call    qword ptr [__imp_printf] 

Printing Value :

Code: [Select]
        invoke     vc_printf,ADDR uint64_format,Value
This will not work because it prints the address of the variable Value :

Code: [Select]
        invoke     vc_printf,ADDR uint64_format,ADDR Value

Gunther

  • Member
  • *****
  • Posts: 3738
  • Forgive your enemies, but never forget their names
Re: Trouble with file length under Win64
« Reply #29 on: February 13, 2021, 07:29:46 AM »
Erol,

Printing Value :

Code: [Select]
        invoke     vc_printf,ADDR uint64_format,Value
This will not work because it prints the address of the variable Value :

Code: [Select]
        invoke     vc_printf,ADDR uint64_format,ADDR Value

you're right. It was my thought error.

Gunther
Get your facts first, and then you can distort them.