News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

New command tail algo.

Started by hutch--, November 27, 2015, 01:11:55 PM

Previous topic - Next topic

hutch--

I thought someone may like this, its a command tail algo that handles both quoted and unquoted file names which it removes and feeds back the command tail if there is one attacked to the command line.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    GetCmdTail PROTO

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    ;;; inkey
    exit

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

main proc

    LOCAL pBuf  :DWORD
    LOCAL lBuf  :DWORD

    fn GetCmdTail
    mov pBuf, eax                   ; pointer command tail
    mov lBuf, ecx                   ; command tail length

    .if lBuf == 0                   ; 0 = no command tail
      print "No command tail",13,10
    .else
      print pBuf,13,10
    .endif

    fn GlobalFree,pBuf

    ret

main endp

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

GetCmdTail proc

    LOCAL pCmd  :DWORD      ; pointer command line
    LOCAL pBuf  :DWORD      ; allocated memory pointer
    LOCAL cLen  :DWORD      ; command line length variable

    fn GetCommandLine
    mov pCmd, eax           ; pointer to command line

    mov cLen, len(pCmd)     ; full command line length
    add cLen, 16            ; some padding

    fn GlobalAlloc,GMEM_FIXED   or GMEM_ZEROINIT, cLen
    mov pBuf, eax

  ; ***************************************************************
  ; ANSI quoted and unquoted file name version to get command tail.
  ; ***************************************************************
    mov ecx, pCmd
    mov edx, pBuf
    cmp BYTE PTR [ecx], 34      ; test for double quote character
    jne noquote                 ; if not 34 branch to noquote handler

  ; -------------------------------------------------------------
  ; strip the quoted file name from the start of the command line
  ; -------------------------------------------------------------
  quoted:
    add ecx, 1
    cmp BYTE PTR [ecx], 34      ; test for 2nd double quote character
    jne quoted
    jmp ftrim

  ; ---------------------------------------------------------------
  ; strip the unquoted file name from the start of the command line
  ; ---------------------------------------------------------------
  noquote:
    add ecx, 1
    cmp BYTE PTR [ecx], 32
    jg noquote                  ; if > 32, throw it away
    sub ecx, 1

  ; ------------------------------------------------
  ; trim any leading white space from command tail
  ; ------------------------------------------------
  ftrim:
    add ecx, 1
    cmp BYTE PTR [ecx], 32
    je ftrim
    cmp BYTE PTR [ecx], 9
    je ftrim

  ; ------------------------------------------
  ; copy the command tail to the output buffer
  ; ------------------------------------------
  copytail:
    movzx eax, BYTE PTR [ecx]
    mov [edx], al
    add ecx, 1
    add edx, 1
    test eax, eax
    jnz copytail
    mov BYTE PTR [edx], 0       ; append a terminator
    sub edx, 1

    mov eax, pBuf               ; return the buffer address in EAX
    mov ecx, edx                ; return the tail length in ECX

    ret

GetCmdTail endp

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

end start