I noticed that using WinAPI I can't do this because lstrcpyn only copies a certain amount of bytes starting at the first position. Like lets say the string is "ABCDE" how would you get "CD"
I assume the first byte of the text (in this example which is 1) as the 1st position not the 0th position(which is the case on most string functions). Like this:
Position: 12345678
Text: 12345678
Remember there are no error checkings here whether if it exceeds the text buffer or not. Also remember the reverse way of calling strdel. (look at my comments below)[size=9]format PE GUI 4.0
entry start
INCLUDE "C:\dev\fasm\include\kernel.inc"
INCLUDE "C:\dev\fasm\include\user.inc"
INCLUDE "C:\dev\fasm\include\macro\stdcall.inc"
INCLUDE "C:\dev\fasm\include\macro\import.inc"
SECTION ".data" DATA READABLE WRITEABLE
text DB "12345678", 0
SECTION ".code" CODE READABLE EXECUTABLE
strdel:
push esi
push edi
mov esi, [esp+12]
mov eax, [esp+16]
add esi, eax
dec esi
mov edx, [esp+20]
sub edx, eax
inc edx
mov edi, esi
add edi, edx
invoke lstrcpy, esi, edi
pop edi
pop esi
retn 12
start:
push 3 ;endpos
push 1 ;startpos
push text ;address of text
call strdel ;function: strdel
invoke MessageBox, HWND_DESKTOP, text, NULL, MB_OK
invoke ExitProcess, 0
SECTION ".idata" IMPORT DATA READABLE WRITEABLE
library kernel, "kernel32.dll", \
user, "user32.dll"
kernel:
import ExitProcess, "ExitProcess", \
lstrlen, "lstrlenA", \
lstrcpy, "lstrcpyA"
user:
import MessageBox, "MessageBoxA"[/size]
You'll get "45678" on this example.sorry, I think I've misunderstood the question...
Do you want to extract a text or do you want to delete a text.
You said:
To extract is to get "CD" only and discard the rest. The final text is "CD"
To delete is to remove/discard "CD" only and the rest is the final text. This means on the string "ABCDE" to strdelete 3rd and 4th position("CD"), you'll get a final result of "ABE".
:confused:
Do you want to extract a text or do you want to delete a text.
You said:
how would you get "CD"
but you want an strdelete :confused: I'm confused. :)
To extract is to get "CD" only and discard the rest. The final text is "CD"
To delete is to remove/discard "CD" only and the rest is the final text. This means on the string "ABCDE" to strdelete 3rd and 4th position("CD"), you'll get a final result of "ABE".
:confused:
strdelete proc pszString:PTR BYTE, dwStartPos:DWORD, dwEndPos:DWORD
push esi
push edi
xor eax, eax
xor ecx, ecx
mov esi, [pszString]
mov edi, [pszString]
add esi, [dwEndPos]
add edi, [dwStartPos]
inc esi
@@copy: inc ecx
lodsb
test eax, eax
stosb
jnz @@copy
add ecx, [dwStartPos]
pop edi
pop esi
ret
strdelete endp
Practical demo attached.
string equ esp+4+8*4
startpos equ esp+8+8*4
endpos equ esp+12+8*4
strdel:
pushad
mov esi,[string]
mov edi,esi
add edi,[startpos]
add esi,[endpos]
@@cpy:
lodsb
stosb
test al,al
jnz @@cpy
popad
ret 3*4
ancev
Thanks guys, to get "CD" I meant deleting "AB", and "E", leaving "CD".
First byte of string is the 0th position. There were further calculations included here since we will be in trouble if the starting position starts at 0.
[size=9]strxtract:
push esi
push edi
mov esi, [esp+12]
mov edi, esi
add esi, [esp+16]
mov ecx, edi
add ecx, [esp+20]
sub ecx, esi
xor edx, edx
inc ecx
__1:
mov al, [esi+edx]
mov [edi+edx], al
inc edx
dec ecx
jnz __1
mov BYTE [edi+edx], 0
pop edi
pop esi
retn 12
start:
push 3 ;endpos
push 0 ;startpos
push text ;address of text
call strxtract[/size]
To get CD use 2 as startpos and 3 as endpos. :)Why not to use sometimes the functions of crtdll.dll? :)
Regards,
Vortex
Regards,
Vortex