flat assembler
Message board for the users of flat assembler.

Index > Windows > Handling strings of unknown length

Author
Thread Post new topic Reply to topic
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch 01 Aug 2006, 18:58
Thanks to help I/others have gotten on these forums I've been able to work out how to handle strings of a fixed length with FASM. I've also been able to figure out how to condense strings using a buffer (of a fixed size).

But I've been unable to understand how I should handle strings of an unknown length (as they could, after all, be bigger than the buffer I allocated to store the string in), could you help?

Thanks,

Killswitch
Post 01 Aug 2006, 18:58
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi 02 Aug 2006, 01:09
Code:
format PE GUI 4.0
include 'win32a.inc'
entry $
push    0
push    0
push    0
call    [HeapCreate]
mov     [hHeap], eax

push    [dwBufferSize]
push    0x08 ;HEAP_ZERO_MEMORY
push    [hHeap]
call    [HeapAlloc]
mov     [pBuffer], eax
;______________________________________
cld
mov     esi, pString
mov     edi, [pBuffer]
mov     ecx, [dwBufferSize]
xor     eax, eax
.copy:
;'lodsb' could be used instead
;of mov / inc, but I've been
;told that it's slower to use
;than mov / inc combination...
mov     al, byte [esi]
inc     esi
test    al, al
jz      .zero
;cmp    al, 'a'
;jz     @f
stosb
;@@:
loopd   .copy
imul    ecx, [dwBufferSize], 2 ;double the buffersize 
push    ecx
push    [pBuffer]
push    0x08 ;HEAP_ZERO_MEMORY
push    [hHeap]
call    [HeapReAlloc] ;replace params/function with w/e you use for reallocing
jmp     .copy
.zero:
push    0
call    [ExitProcess]
;______________________________________
data import
library kernel32,'kernel32'
import  kernel32,\
        ExitProcess,'ExitProcess',\
        HeapAlloc,'HeapAlloc',\
        HeapCreate,'HeapCreate',\
        HeapReAlloc,'HeapReAlloc'
end data
dwBufferSize dd 512
;pString rd 1
hHeap   rd 1
pBuffer rd 1
pString:
; a random file that has 'string'
; longer than 512 bytes.
; db 0x0D,0x0A = CRLF could be used
; instead of 0x00-byte...
; just create .txt file with random
; lines in it to get actual filesize
; over 512bytes
file 'random.txt'    

I hope this is atleast a bit what you are looking for, of course lstrlenA from kernel32 could help too Wink

edit:
eh, forgot totally about _handling_ of the strings Surprised
added string-'handling' as commented, parses a-chars Smile

sry for my english Embarassed
Post 02 Aug 2006, 01:09
View user's profile Send private message MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 02 Aug 2006, 07:43
check out FASMLIB, next part of tutorial will describe it's string library, which is capable of working with strings with non-fixed length

also same library is used in Fresh project

for now, see file /fasmlib/str.inc
Post 02 Aug 2006, 07:43
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch 02 Aug 2006, 13:59
Thanks for your help, I just want to make sure I know what's going on:

Code:
push    [dwBufferSize] 
push    0x08 ;HEAP_ZERO_MEMORY 
push    [hHeap] 
call    [HeapAlloc] 
mov     [pBuffer], eax 
    


- Allocates a buffer of 512 bytes initially


Code:
mov     al, byte [esi] 
inc     esi 
test    al, al 
jz      .zero 
    


-Starts copying bytes to pBuffer upto the number of bytes indicated by dwBufferSize, or untill a terminating null byte is encounterded (in which case it jumps to the end)

Code:
loopd   .copy 
imul    ecx, [dwBufferSize], 2 ;double the buffersize  
push    ecx 
push    [pBuffer] 
push    0x08 ;HEAP_ZERO_MEMORY 
push    [hHeap] 
call    [HeapReAlloc] ;replace params/function with w/e you use for reallocing 
jmp     .copy
    


-Increases the size of pBuffer if there's stuff left to copy over, but there wasn't enough space

Is that the gist?
Post 02 Aug 2006, 13:59
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi 03 Aug 2006, 10:23
second part is actually this, but other than that, correct.

Code:
.copy:
mov     al, byte [esi]
inc     esi
test    al, al
jz      .zero
stosb
loopd   .copy    
Post 03 Aug 2006, 10:23
View user's profile Send private message MSN Messenger Reply with quote
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch 03 Aug 2006, 15:17
Thank you Smile

Edit:

How could I turn that code into a macro, and also have it start adding characters from the end of the Buffer (so I can call the macro once, and make pBuffer = 'hello' then call the macro again so that ' world' gets added to pBuffer)?
Post 03 Aug 2006, 15:17
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi 04 Aug 2006, 12:24
Killswitch wrote:
Thank you Smile

Edit:

How could I turn that code into a macro, and also have it start adding characters from the end of the Buffer (so I can call the macro once, and make pBuffer = 'hello' then call the macro again so that ' world' gets added to pBuffer)?


You should make it a proc
I'll work on example soon.

edit:
Code:
format PE GUI 4.0
include 'win32a.inc'
entry $
        push    szString3
        push    szString2
        push    szString
        call    szAppend
        
        push    0
        push    szCaption
        push    szString3
        push    0
        call    [MessageBox]
        
        push    0
        call    [ExitProcess]
;______________________________________
proc szAppend, pString1, pString2, pBufOut
        cld
        mov     esi, [pString1]
        mov     edi, [pBufOut]
        xor     eax, eax
        mov     ecx, 1
.copy:
        mov     al, byte [esi]
        inc     esi
        test    al, al
        jz      .zero
        stosb
        jmp     .copy
.zero:
        test    ecx, ecx
        jz      .ret
        mov     esi, [pString2]
        xor     ecx, ecx
        jmp     .copy
.ret:   
        ret
endp
;______________________________________
data import
library kernel32,'kernel32',\
        user32,'user32'
import  kernel32,\
        ExitProcess,'ExitProcess'
import  user32,\
        MessageBox,'MessageBoxA'
end data
szCaption       db      'humm',0
szString        db      'Hello',0
szString2       db      ' World',0
szString3       rb      0x200    

_________________
When We Ride On Our Enemies
support reverse smileys |:
Post 04 Aug 2006, 12:24
View user's profile Send private message MSN Messenger Reply with quote
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch 05 Aug 2006, 12:35
You're a genious mate, thank you!
Post 05 Aug 2006, 12:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.