The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: bomz on August 22, 2010, 11:41:02 AM

Title: HELP for file operation
Post by: bomz on August 22, 2010, 11:41:02 AM
Quote
.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

.data
File_Read          db "QH.HLP",0
File_Write          db "QH_01.HLP",0

.data?
Handle_Read         HANDLE ?
Handle_Write         HANDLE ?
bytesRead          dd ?
bytesWrite          dd ?
buffer db 32768 dup(?)
hMemory            dd ?

.code
start:
invoke CreateFile,addr File_Read,GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL OR FILE_FLAG_NO_BUFFERING,0
mov Handle_Read, eax
invoke CreateFile,addr File_Write,GENERIC_WRITE,0,0,CREATE_NEW,FILE_ATTRIBUTE_NORMAL OR FILE_FLAG_NO_BUFFERING,0
;invoke CreateFile,addr File_Write,GENERIC_WRITE,0,0,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,0
mov Handle_Write, eax

;invoke ReadFile, Handle_Read,addr buffer,sizeof buffer,addr bytesRead,NULL
;invoke WriteFile,Handle_Write,addr buffer,bytesRead, addr bytesWrite,NULL
;cmp bytesWrite, 32768
;je Next

invoke LocalAlloc,LMEM_FIXED,33554432
mov  hMemory,eax
Next:
invoke ReadFile,Handle_Read,hMemory,33554432,addr bytesRead,NULL
invoke WriteFile,Handle_Write,hMemory,bytesRead, addr bytesWrite,NULL
cmp bytesWrite, 33554432
je Next
invoke LocalFree,hMemory

invoke CloseHandle, Handle_Read
invoke CloseHandle, Handle_Write

exit:
invoke ExitProcess,0
end start

This simple programe can copy any files except HLP. You may change name of HLP file to RAR or IMA... - not copy. You may change name of any not HLP file to HLP it's copy good.
Whats the reason? Try it. --- link removed --- this is the help of MASM 6.11, I try some other to
If copy using simple buffer - all is OK, if no write cache - all is OK, you may copy any file including HLP. I leave this strings for trying (http://i065.radikal.ru/0812/31/5ab1d197f5d9.gif)

-- image removed ---

PS A little progress - it's not copy COM files to. But not EXE
PPS and BAT files
Title: Re: HELP for file operation
Post by: Twister on August 22, 2010, 04:07:23 PM
Link & image has been removed? Why? :eek

Well, the thing I can see is that you are misusing 'ADDR'.

Wouldn't it be best to use the CopyFile Function? http://msdn.microsoft.com/en-us/library/aa363851%28VS.85%29.aspx
Title: Re: HELP for file operation
Post by: bomz on August 22, 2010, 04:41:20 PM
if I miss ADDR - any files not copied. This construction copies files two time quickly and more safely for hard disk. Prevent "cache writing" gets 3 second on each Gygabyte
Title: Re: HELP for file operation
Post by: tenkey on August 22, 2010, 05:09:31 PM
The code, as posted, is able to read 32 MB into a 32 KB buffer.
Check if file sizes make a difference.
Title: Re: HELP for file operation
Post by: bomz on August 22, 2010, 11:29:46 PM
How, in your opinion, puting 32 mb in 32 kb buffers affects reading and writing of COM files?
There is no errors in this code, at least simple.

Lack of writing cache (FILE_FLAG_NO_BUFFERING as FILE_FLAG_WRITE_THROUGH) makes impossible for some files type (HLP COM BAT but not IMA ISO AVI RAR ZIP) to move FilePointer (invoke SetFilePointer) accept the case when you read to buffer in the same memory block which system allocate for code.
Title: Re: HELP for file operation
Post by: donkey on August 23, 2010, 02:47:58 AM
Quote from: tenkey on August 22, 2010, 05:09:31 PM
The code, as posted, is able to read 32 MB into a 32 KB buffer.
Check if file sizes make a difference.

Not sure where you see that, he uses a global buffer for the 32mb read/write and has commented out the 32 KB read/write.

Anyway,

Are there any error messages after CreateFile or Read/Write file ? Perhaps there is an issue with security software blocking the creation of the .exe file then the handle would be invalid and should throw an error. And yes I know that .com files are executable but it is still a good idea to check the error codes after your major API calls. For example you have not even checked to see if the files were created before reading and writing, you should at least check the return for a valid handle.

Edgar
Title: Re: HELP for file operation
Post by: bomz on August 23, 2010, 02:52:09 AM
It's only part of my code that I make to highlight the problem. my "global" code treat all possible error events. now I may shurely say that filepointer not move in the case of HLP BAT COM files. I can't create ZERO FILE of the same file size in the case of HLP BAT COM files. but IMA ISO AVI RAR ZIP filepointer move OK
Title: Re: HELP for file operation
Post by: donkey on August 23, 2010, 02:59:59 AM
Quote from: bomz on August 23, 2010, 02:52:09 AM
It's only part of my code that I make to highlight the problem. my "global" code treat all possible error events.

So are there any error codes returned directly after CreateFile ?

Is the file created and left empty ? Is there no file created ? You have not said what the problem is just left it to us to guess and when the code looks fine that a pretty hard thing to do. Also if your error checking is inline with the posted code it may be affecting the execution path of the program, if it is not inline then how can you tell where the failure is. Lots of things here that need to be addressed before you'll get a useful answer.
Title: Re: HELP for file operation
Post by: bomz on August 23, 2010, 03:05:28 AM


The global question - how to prevent cache writing it gives 3-5% speed of copying
Intuitively it's possible to note that HLP COM BAT files are "the systems files", but how correctly use prevent cache writing flags?
Title: Re: HELP for file operation
Post by: donkey on August 23, 2010, 03:33:04 AM
You need to work out your error trapping a bit more, the WriteFile operation was returning the error "Error 87 > The parameter is incorrect.", you can solve this by removing FILE_FLAG_NO_BUFFERING from the output CreateFile. Also FILE_ATTRIBUTE_NORMAL is not really necessary.

invoke CreateFile,addr File_Write,GENERIC_WRITE,0,0,CREATE_NEW,0,0

Before using the FILE_FLAG_NO_BUFFERING flag you should have read this:

http://msdn.microsoft.com/en-us/library/cc644950%28v=VS.85%29.aspx

Edgar
Title: Re: HELP for file operation
Post by: bomz on August 23, 2010, 03:37:17 AM
FILE_ATTRIBUTE_NORMAL - I try all possible flags including NULL
before WriteFile function in full code SetFilePointer create full size ZERO file

AVI files copy good up too 3 gygabyte size - I haven't biger for trying. HASH summe correct I use torrent rehash
Title: Re: HELP for file operation
Post by: donkey on August 23, 2010, 03:39:37 AM
Quote from: bomz on August 23, 2010, 03:37:17 AM
FILE_ATTRIBUTE_NORMAL - I try all possible flags including NULL
before WriteFile function in full code SetFilePointer create full size ZERO file

AVI files copy good up too 3 gygabyte size - I haven't biger for trying. HASH summe correct I use torrent rehash

I tried your code as is and got the same results as you, I removed the FILE_FLAG_NO_BUFFERING flag and every file I tested copied OK, the data in the destination file was complete and faithful to the original.

So is your problem solved ?

Edgar
Title: Re: HELP for file operation
Post by: bomz on August 23, 2010, 03:47:43 AM
files atribute the same in all cases, of course
using overlapped structure for handly set FilePointer not decide the problem
Title: Re: HELP for file operation
Post by: bomz on August 23, 2010, 03:48:55 AM
NOT SOLVED - how to prevent writing cache? Or how to use prevent buffering flags for writefile

buffer db 1048576 dup(?) - compiling time more than 15 minutes it's impossible to wait bigger buffer
Title: Re: HELP for file operation
Post by: donkey on August 23, 2010, 03:51:07 AM
Quote from: bomz on August 23, 2010, 03:48:55 AM
NOT SOLVED - how to prevent writing cache? Or how to use prevent buffering flags for writefile

I posted a link to the article at MSDN that explains how to use the flag. From how I read it you must calculate your buffer size as a multiple of the sector size of the drive you are writing to, first get the sector size then use a multiple of that as your buffer size.
Title: Re: HELP for file operation
Post by: bomz on August 23, 2010, 03:52:41 AM
Thank's I read it. But it's not very quickly
Title: Re: HELP for file operation
Post by: donkey on August 23, 2010, 03:59:30 AM
Quote from: bomz on August 23, 2010, 03:52:41 AM
Thank's I read it. But it's not very quickly

Yeah, its a bit hard to read, but in my opinion you're better off using FILE_FLAG_WRITE_THROUGH if you don't want the data cached during a write operation, not sure why you picked FILE_FLAG_NO_BUFFERING, I assume you needed it for your applications purposes.
Title: Re: HELP for file operation
Post by: bomz on August 23, 2010, 04:01:06 AM
if the sector size is 512 bytes, an application can request reads and writes of 512, 1,024, 1,536, or 2,048 bytes, but not of 335, 981, or 7,171 bytes...........33554432=32 mb


in other case there is not only any writing but reading too

FILE_FLAG_WRITE_THROUGH cause the same result as NO_BUFFERING

VirtualAlloc and Heap - the same result in all flag combination
Title: Re: HELP for file operation
Post by: donkey on August 23, 2010, 04:08:07 AM
Quote from: bomz on August 23, 2010, 04:01:06 AM
if the sector size is 512 bytes, an application can request reads and writes of 512, 1,024, 1,536, or 2,048 bytes, but not of 335, 981, or 7,171 bytes...........33554432=32 mb


in other case there is not only any writing but reading too

FILE_FLAG_WRITE_THROUGH cause the same result as NO_BUFFERING

No, the 32MB buffer size you are using may be correct however the write request on the only write (if the file is under 32MB) is for a portion of that and will cause the write to fail, the read succeeds because the buffer size is the complete 32MB. For example if ReadFile reads 1057 bytes that is what your write request is, the actual size of the buffer is not important, your request is not aligned because you are telling WriteFile the size of the buffer is 1057 bytes. The way your program is written it will always fail unless the actual file size is a multiple of the sector size, which would be common in many executable files but in others would not be.

Edgar
Title: Re: HELP for file operation
Post by: bomz on August 23, 2010, 04:09:20 AM
but how it cause copying AVI files and not copy HLP files?
Title: Re: HELP for file operation
Post by: donkey on August 23, 2010, 04:11:24 AM
Quote from: bomz on August 23, 2010, 04:09:20 AM
but how it cause copying AVI files and not copy HLP files?

Again check the file size. Is the AVI a multiple of sector size ? Is the HLP file ? When you subtract 3MB is the remainder a multiple of sector size ? The flag you selected is a specialty flag that requires you only use buffers and write sizes within very strict parameters.
Title: Re: HELP for file operation
Post by: bomz on August 23, 2010, 04:16:32 AM
yes you right the reason is in the size
Title: Re: HELP for file operation
Post by: bomz on August 23, 2010, 04:18:37 AM
THANKS! (http://s53.radikal.ru/i140/0907/58/e88a909600d5.gif)

Title: Re: HELP for file operation
Post by: donkey on August 23, 2010, 04:20:08 AM
My pleasure bomz.