|
| 1 | +;============================================================================== |
| 2 | +; |
| 3 | +; UASM64 Library |
| 4 | +; |
| 5 | +; https://github.com/mrfearless/UASM64-Library |
| 6 | +; |
| 7 | +;============================================================================== |
| 8 | +.686 |
| 9 | +.MMX |
| 10 | +.XMM |
| 11 | +.x64 |
| 12 | + |
| 13 | +option casemap : none |
| 14 | +option win64 : 11 |
| 15 | +option frame : auto |
| 16 | +option stackbase : rsp |
| 17 | + |
| 18 | +_WIN64 EQU 1 |
| 19 | +WINVER equ 0501h |
| 20 | + |
| 21 | +include UASM64.inc |
| 22 | + |
| 23 | +.DATA |
| 24 | + |
| 25 | +align 8 |
| 26 | +abntbl \ |
| 27 | + db 2,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0 |
| 28 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 29 | + db 1,0,3,0,0,0,0,0,0,0,0,0,1,0,0,0 |
| 30 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 31 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 32 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 33 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 34 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 35 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 36 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 37 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 38 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 39 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 40 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 41 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 42 | + db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 43 | + |
| 44 | + ; 0 = OK char |
| 45 | + ; 1 = delimiting characters tab LF CR space "," |
| 46 | + ; |
| 47 | + ; 2 = ASCII zero This should not be changed in the table |
| 48 | + ; 3 = quotation This should not be changed in the table |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +.CODE |
| 53 | + |
| 54 | +UASM64_ALIGN |
| 55 | +;------------------------------------------------------------------------------ |
| 56 | +; Arg_GetArgument |
| 57 | +; |
| 58 | +; Return the contents of an argument from an argument list by its number. |
| 59 | +; |
| 60 | +; Parameters: |
| 61 | +; |
| 62 | +; * lpszArgumentList - The address of the zero terminated argument list. |
| 63 | +; |
| 64 | +; * lpszDestination - The address of the destination buffer. |
| 65 | +; |
| 66 | +; * nArgument - The number of the argument to return. |
| 67 | +; |
| 68 | +; * qwArgumentListReadOffset - The next read offset in the source address. |
| 69 | +; |
| 70 | +; Returns: |
| 71 | +; |
| 72 | +; The return value is the updated next read offset in the source if it is |
| 73 | +; greater than zero. |
| 74 | +; |
| 75 | +; The three possible return values are: |
| 76 | +; > 1 = The next read offset in the source. |
| 77 | +; 0 = The end of the argument list has been reached. |
| 78 | +; -1 = A non matching quotation error has occurred in the source. |
| 79 | +; |
| 80 | +; Notes: |
| 81 | +; |
| 82 | +; This function supports double quoted text and it is delimited by the space |
| 83 | +; character, a tab or a comma or any combination of these three. It may be used |
| 84 | +; in two separate modes, single argument mode and streaming mode. |
| 85 | +; |
| 86 | +; In separate argument mode you specify the argument you wish to obtain with |
| 87 | +; the nArgument parameter and you set the qwArgumentListReadOffset parameter to |
| 88 | +; zero. |
| 89 | +; |
| 90 | +; In streaming mode you set a variable to zero and pass it as the 4th parameter |
| 91 | +; qwArgumentListReadOffset and save the RAX return value back into this variable |
| 92 | +; for the next call to the function. The nArgument parameter in streaming mode |
| 93 | +; should be set to one "1" |
| 94 | +; |
| 95 | +; To support the notation of an empty pair of double quotes in an argument list |
| 96 | +; the parser in this algorithm return an empty destination buffer that has an |
| 97 | +; ascii zero as it first character. |
| 98 | +; |
| 99 | +;------------------------------------------------------------------------------ |
| 100 | +Arg_GetArgument PROC FRAME USES RBX RCX RDX RDI RSI lpszArgumentList:QWORD,lpszDestination:QWORD,nArgument:QWORD,qwArgumentListReadOffset:QWORD |
| 101 | + |
| 102 | +; ----------------------------------------------- |
| 103 | + |
| 104 | +; Return values in EAX |
| 105 | +; -------------------- |
| 106 | +; >0 = there is a higher nArgumentber argument available |
| 107 | +; 0 = end of command line has been found |
| 108 | +; -1 = a non matching quotation error occurred |
| 109 | +; |
| 110 | +; conditions of 0 or greater can have argument |
| 111 | +; content which can be tested as follows. |
| 112 | +; |
| 113 | +; Test Argument for content |
| 114 | +; ------------------------- |
| 115 | +; If the argument is empty, the first BYTE in the |
| 116 | +; destination buffer is set to zero |
| 117 | +; |
| 118 | +; mov eax, destbuffer ; load destination buffer |
| 119 | +; cmp BYTE PTR [eax], 0 ; test its first BYTE |
| 120 | +; je no_arg ; branch to no arg handler |
| 121 | +; print destbuffer,13,10 ; display the argument |
| 122 | +; |
| 123 | +; NOTE : A pair of empty quotes "" returns ascii 0 |
| 124 | +; in the destination buffer |
| 125 | + |
| 126 | +; ----------------------------------------------- * |
| 127 | + |
| 128 | + mov rsi, lpszArgumentList |
| 129 | + add rsi, qwArgumentListReadOffset |
| 130 | + mov rdi, lpszDestination |
| 131 | + |
| 132 | + mov BYTE PTR [rdi], 0 ; set destination buffer to zero length |
| 133 | + |
| 134 | + xor rbx, rbx |
| 135 | + |
| 136 | + ; ********* |
| 137 | + ; scan args |
| 138 | + ; ********* |
| 139 | + bcscan: |
| 140 | + movzx rax, BYTE PTR [rsi] |
| 141 | + add rsi, 1 |
| 142 | + lea rcx, abntbl |
| 143 | + cmp BYTE PTR [rcx+rax], 1 ; delimiting character |
| 144 | + je bcscan |
| 145 | + cmp BYTE PTR [rcx+rax], 2 ; ASCII zero |
| 146 | + je quit |
| 147 | + |
| 148 | + sub rsi, 1 |
| 149 | + add rbx, 1 |
| 150 | + cmp rbx, nArgument ; copy next argument if nArgument matches |
| 151 | + je cparg |
| 152 | + |
| 153 | + gcscan: |
| 154 | + movzx rax, BYTE PTR [rsi] |
| 155 | + add rsi, 1 |
| 156 | + lea rcx, abntbl |
| 157 | + cmp BYTE PTR [rcx+rax], 0 ; OK character |
| 158 | + je gcscan |
| 159 | + cmp BYTE PTR [rcx+rax], 2 ; ASCII zero |
| 160 | + je quit |
| 161 | + cmp BYTE PTR [rcx+rax], 3 ; quotation |
| 162 | + je dblquote |
| 163 | + jmp bcscan ; return to delimiters |
| 164 | + |
| 165 | + dblquote: |
| 166 | + add rsi, 1 |
| 167 | + cmp BYTE PTR [rsi], 0 |
| 168 | + je qterror |
| 169 | + cmp BYTE PTR [rsi], 34 |
| 170 | + jne dblquote |
| 171 | + add rsi, 1 |
| 172 | + jmp bcscan ; return to delimiters |
| 173 | + |
| 174 | + ; ******** |
| 175 | + ; copy arg |
| 176 | + ; ******** |
| 177 | + cparg: |
| 178 | + xor rax, rax |
| 179 | + xor rdx, rdx |
| 180 | + cmp BYTE PTR [rsi+rdx], 34 ; if 1st char is a quote |
| 181 | + je cpquote ; jump to quote copy |
| 182 | + @@: |
| 183 | + mov al, [rsi+rdx] ; copy normal argument to buffer |
| 184 | + mov [rdi+rdx], al |
| 185 | + test rax, rax |
| 186 | + jz quit |
| 187 | + add rdx, 1 |
| 188 | + lea rcx, abntbl |
| 189 | + cmp BYTE PTR [rcx+rax], 1 |
| 190 | + jl @B |
| 191 | + mov BYTE PTR [rdi+rdx-1], 0 ; append terminator |
| 192 | + jmp next_read |
| 193 | + |
| 194 | + ; ******************** |
| 195 | + ; copy quoted argument |
| 196 | + ; ******************** |
| 197 | + cpquote: |
| 198 | + add rsi, 1 |
| 199 | + @@: |
| 200 | + mov al, [rsi+rdx] ; strip quotes and copy contents to buffer |
| 201 | + test al, al |
| 202 | + jz qterror |
| 203 | + cmp al, 34 |
| 204 | + je write_zero |
| 205 | + mov [rdi+rdx], al |
| 206 | + add rdx, 1 |
| 207 | + jmp @B |
| 208 | + |
| 209 | + write_zero: |
| 210 | + add rdx, 1 ; correct EDX for final exit position |
| 211 | + mov BYTE PTR [rdi+rdx-1], 0 ; append terminator |
| 212 | + |
| 213 | + jmp next_read ; jump to next read setup |
| 214 | + ; ***************** |
| 215 | + ; set return values |
| 216 | + ; ***************** |
| 217 | + qterror: |
| 218 | + mov rax, -1 ; quotation error |
| 219 | + jmp rstack |
| 220 | + |
| 221 | + quit: |
| 222 | + xor rax, rax ; set EAX to zero for end of source buffer |
| 223 | + jmp rstack |
| 224 | + |
| 225 | + next_read: |
| 226 | + mov rax, rsi |
| 227 | + add rax, rdx |
| 228 | + sub rax, lpszArgumentList |
| 229 | + |
| 230 | + rstack: |
| 231 | + |
| 232 | + ret |
| 233 | +Arg_GetArgument ENDP |
| 234 | + |
| 235 | + |
| 236 | +END |
| 237 | + |
0 commit comments