1 ; Mereo - An HTTP server.
2 ; Copyright (C) 2008 Jelle Geerts
4 ; This program is free software: you can redistribute it and/or modify
5 ; it under the terms of the GNU General Public License as published by
6 ; the Free Software Foundation, either version 3 of the License, or
7 ; (at your option) any later version.
9 ; This program is distributed in the hope that it will be useful,
10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ; GNU General Public License for more details.
14 ; You should have received a copy of the GNU General Public License
15 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
17 ; Returns pointer to commandline arguments if a commandline has been found.
18 ; Returns 0 if no commandline arguments found.
19 GetCmdLineArgs
proc uses
edi
27 mov ecx, eax ; ECX = string length of commandline
28 cld ; Clear Direction Flag (search forward)
29 cmp byte ptr [edi], '"'
30 jnz @@CMDLINE_SEARCH_SPACE ; Search next space (use as separator)
31 mov eax, '"' ; Else, search for next " character (use as separator)
32 inc edi ; +1 to search after the first " character
33 repnz scasb ; Do a search
34 jnz @@CMDLINE_NO_ARGS ; Not found?!
36 @@CMDLINE_SEARCH_SPACE:
37 ; ECX = string length of commandline
43 cmp byte ptr [edi], 0 ; Commandline ends already?
45 inc edi ; +1 to point to first argument
46 cmp byte ptr [edi], 0 ; Commandline ends already?
47 jz @@CMDLINE_NO_ARGS ; No arguments passed on commandline
53 xor eax, eax ; Indicate no arguments found
60 ; Returns a pointer to the requested argument from the commandline.
61 ; Everything between the '"' characters not parsed. e.g. "test".
63 ; Also every argument (i.e. "test test2", separated by spaces) not
64 ; starting with the first character of the argument to search for will
65 ; not be parsed. This is done to prevent the parser from thinking that
66 ; an argument has been found within another argument.
67 ; e.g. "-h .\ta-del" would otherwise let the parser think that "-de" has
68 ; been found when we were searching for "-h" instead.
70 ; Returns the pointer on success, or -1 on failure.
71 GetCmdLineArgPtr
proc uses
ebx esi edi lpszCommandLine:DWORD, lpszArg:DWORD
72 LOCAL fDontParse
:DWORD
74 mov [fDontParse
], FALSE
75 mov esi, [lpszCommandLine
]
77 ; Loop through all characters until a matching character was found
88 ; Don't parse characters between '"' characters. e.g.: "test" (here, test is not parsed)
90 xor [fDontParse
], TRUE
; Set DontParse flag to TRUE if it was FALSE, and FALSE if it was TRUE
91 .elseif
[fDontParse
] == FALSE
92 ; Check if the argument starts with the first character of the argument that we are in search of.
93 invoke StrLen
, [lpszArg
]
105 mov eax, [lpszCommandLine
]
106 cmp byte ptr [eax], dl
108 cmp dl, byte ptr [edi+1]
117 dec dword ptr [esp] ; one less because "lodsb" increased ESI
120 ; now check if a space or NULL byte follows the argument (should be)
124 cmp byte ptr [edx-1], 0
126 cmp byte ptr [edx-1], ' '
131 .while
byte ptr [edi] == ' '
135 dec eax ; one less because "lodsb" increased ESI
136 sub eax, [lpszCommandLine
]
144 GetCmdLineArgPtr
endp
146 ; Returns the index of where the string after the requested argument
147 ; from the commandline.was found.
149 ; Returns the index in EAX and the length of the string in ECX on success.
150 ; On failure (i.e. argument or string not found) a value of -1 is returned.
151 GetCmdLineArgString
proc uses
esi edi lpszCommandLine:DWORD, lpszArg:DWORD
152 invoke GetCmdLineArgPtr
, [lpszCommandLine
], [lpszArg
]
156 add eax, [lpszCommandLine
]
159 cmp byte ptr [edi], 0 ; If null ..
160 jz @@PARSING_ERROR ; .. then the argument is invalid
165 cmp byte ptr [edi], '"'
172 cmp byte ptr [edi], 0
173 lea edi, [edi+1] ; EDI = EDI + 1 (without modifying CPU flags)
175 dec edi ; Decrease if not NULL (means not last argument and length already calculated correctly)
193 sub eax, [lpszCommandLine
]
196 inc eax ; preserve one NULL byte (always needed, we can't garantuee it's already
197 ; preserved because the string not always ends with a '"' character)
198 invoke GlobalAlloc
, GPTR
, eax
204 invoke lstrcpyn
, eax, esi, ecx
213 GetCmdLineArgString
endp
215 ; Retrieves the integer after the requested argument from the commandline.
216 ; Returns the pointer on success, or -1 on failure.
217 GetCmdLineArgInt
proc uses
ebx esi edi lpszCommandLine:DWORD, lpszArg:DWORD
218 LOCAL lpIntAscii
[16] :BYTE
220 invoke GetCmdLineArgPtr
, [lpszCommandLine
], [lpszArg
]
224 mov ebx, [lpszCommandLine
]
226 cmp byte ptr [ebx+eax], ' ' ; Check if there is a space between the port and the argument
229 add ebx, eax ; EBX = pointer to port number
237 jz @@GET_INT_LENGTH_TILL_SPACE
243 @@GET_INT_LENGTH_TILL_SPACE:
249 invoke lstrcpyn
, ADDR lpIntAscii
, ebx, ecx
250 invoke atodw
, ADDR lpIntAscii
258 GetCmdLineArgInt
endp