NO

Author Topic: Get file extension  (Read 4069 times)

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Get file extension
« on: April 27, 2012, 08:30:17 PM »
Here is a quick example to get the extension of a filename :

Code: [Select]
.386
.model flat,stdcall
option casemap:none

StrLen PROTO :DWORD

.code

GetFileExtension PROC USES esi edi pFileName:DWORD

    mov     esi,pFileName
    mov     edi,1
    invoke  StrLen,esi
    mov     cl,BYTE PTR [esi]
    push    ecx
    mov     BYTE PTR [esi],'.'
    add     eax,esi
@@:
    sub     eax,edi
    cmp     BYTE PTR [eax],'.'
    jne     @b
    pop     ecx
    mov     BYTE PTR [esi],cl
    ret

GetFileExtension ENDP

END

The procedure returns the address of the string if there is no file extension.

Code: [Select]
include Demo.inc

BUFFER_SIZE equ 128

.data

file db 'C:\WINDOWS\system32\kernel32.dll',0
format1 db 'Extension = %s',0

.data?

buffer db BUFFER_SIZE dup(?)

.code

start:

    invoke  GetFileExtension,ADDR file
    invoke  wsprintf,ADDR buffer,ADDR format1,eax
    invoke  StdOut,ADDR buffer
    invoke  ExitProcess,0

END start
« Last Edit: April 27, 2012, 08:37:08 PM by Vortex »
Code it... That's all...

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: Get file extension
« Reply #1 on: April 28, 2012, 11:24:00 AM »
- EndOfStr, a modified version of the StrLen function determines the end of a NULL terminated string.
- Removed unnecessary preservation of ecx

Code: [Select]
.386
.model flat,stdcall
option casemap:none

EndOfStr PROTO :DWORD

.code

GetFileExtension PROC USES esi edi pFileName:DWORD

    mov     esi,pFileName
    mov     edi,1
    invoke  EndOfStr,esi
    movzx   ecx,BYTE PTR [esi]
    mov     BYTE PTR [esi],'.'
@@:
    sub     eax,edi
    cmp     BYTE PTR [eax],'.'
    jne     @b
   
    mov     BYTE PTR [esi],cl
    ret

GetFileExtension ENDP

END
Code it... That's all...