flat assembler
Message board for the users of flat assembler.

Index > Windows > Newbie? Question (On File Operations)

Author
Thread Post new topic Reply to topic
sekigun



Joined: 23 Dec 2004
Posts: 18
Location: Japan
sekigun 08 Aug 2005, 04:46
I am struggling again this time in Japan with the Windows APIs. I am writting a program to XOR two files. The thing is i know how to XOR the bytes once they are in memory, but knowing and willing to know nothing about C i get often stuck with the APIs because the documentation is either limited and or uses "self explanatory" C samples. The code I use to load the files is:
Code:
invoke CreateFile,[filename],READ_WRITE,SHARE,NULL,OPEN_ALWAYS,NULL
mov [fileh001],eax
invoke GetFileSize,[fileh001],NULL
mov [fsize],eax
invoke CreateFileMapping,[fileh001],NULL,PAGE_READWRITE,0,0,NULL
mov [file1h],eax
invoke MapViewOfFile,[file1h],FILE_MAP_WRITE,0,0,0
mov [file1],eax
    


Of course the filename label is correct(I had to write a parser for the Unicode command line so I am pretty sure it works) and it is a full path, I am also able to write to the files using the WriteFile instruction.
I don't care if I have to use other APIs but buffers are out of the question I already have a working version using buffers, but I thought using the OS to load the whole file to memory and then operating over memory would speed up things.
I must admit I tried first to load the file myself but then I didn't know how to allocate memory for my process to use. Anything I would load would try to overwrite other processes memory and die miserably.


Last edited by sekigun on 08 Aug 2005, 15:21; edited 2 times in total
Post 08 Aug 2005, 04:46
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 08 Aug 2005, 07:15
catch the return values on every api call you use, and lookup error text (there some api for that too, GetErrorText or something like that). and do this always so you can find error easier.
Post 08 Aug 2005, 07:15
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
sekigun



Joined: 23 Dec 2004
Posts: 18
Location: Japan
sekigun 08 Aug 2005, 07:31
vid wrote:
catch the return values on every api call you use, and lookup error text (there some api for that too, GetErrorText or something like that). and do this always so you can find error easier.


If something can possibly return an error code I do, but I don't see the use in error catching when I check in other parts of the code if the file exists and if it's in use and so on. There must be a bug in the code I posted but I don't have any clue.
I will what you recommend, but I think the error code will leave me with some Microsoft documentation pages and the feared C source.
Post 08 Aug 2005, 07:31
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 08 Aug 2005, 08:17
you don't know windows. error can pop out anywhere, not only where it should. I used to have macros for error catching, it shortens code nicely
Post 08 Aug 2005, 08:17
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
sekigun



Joined: 23 Dec 2004
Posts: 18
Location: Japan
sekigun 08 Aug 2005, 13:29
vid wrote:
you don't know windows. error can pop out anywhere, not only where it should. I used to have macros for error catching, it shortens code nicely


Okay, I get the idea, thank you. I think I should be able to write the macros myself. That is f the error catching APIs are better documented than the error causing ones. Also if anyone could help me with te code I posted I still need help with that...
Post 08 Aug 2005, 13:29
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 08 Aug 2005, 14:22
it is not easy figure out your problem clear to me, so:
0.read carefully about functions you are using and check well parameters you are passing to them. esp. check if you are using zero instead of pointer to empty asciiz string and pointer to var where written bytes number will be stored.
1.split your code into parts, comment them though it has valid prolog and epilog, then compile and run, and check if there an error appeared: eg. open file and immediatelly close it. if no error there - unkomment next part, etc. - with this lazy way you can catch where your program starts crashing even without debugger.
2.just use debugger - ollydebug shows description of most errors
3.if this will not help: i guess your project is not too huge - zip it whole and post here so we can check it (also include small example files you are going to xor)

regards!
Post 08 Aug 2005, 14:22
View user's profile Send private message Visit poster's website Reply with quote
sekigun



Joined: 23 Dec 2004
Posts: 18
Location: Japan
sekigun 08 Aug 2005, 15:06
shoorick wrote:
it is not easy figure out your problem clear to me, so:
0.read carefully about functions you are using and check well parameters you are passing to them. esp. check if you are using zero instead of pointer to empty asciiz string and pointer to var where written bytes number will be stored.
1.split your code into parts, comment them though it has valid prolog and epilog, then compile and run, and check if there an error appeared: eg. open file and immediatelly close it. if no error there - unkomment next part, etc. - with this lazy way you can catch where your program starts crashing even without debugger.
2.just use debugger - ollydebug shows description of most errors
3.if this will not help: i guess your project is not too huge - zip it whole and post here so we can check it (also include small example files you are going to xor)

regards!


Thank you, I was already doing 1. The crash was on the MapViewOfFile, but the reason became clear when the CreateFileMapping function returned zero. As it's not as obvious as I thought I will try to solve it myself following your advice. I think it could be what you say. I will check the zeros. I don't always understand the meaning of Microsoft's definition of function parameters.
Post 08 Aug 2005, 15:06
View user's profile Send private message Reply with quote
sekigun



Joined: 23 Dec 2004
Posts: 18
Location: Japan
sekigun 08 Aug 2005, 15:19
Oh f....... It turns out it was obvious... I copied code from another assembler with a different syntax. [] brackets missing. This solves the error code. But I'm still crashing when I try to poke the memory. I'll will check better.
EDIT: OllyDbg saved me this time, I don't fully understand why some things were wrong. I suppose I introduced stupid new bugs while trying to solve the original one. The posted version should have no errors now.
Post 08 Aug 2005, 15:19
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-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.