www.pudn.com > com_dllold.rar > com_download.cpp
#include "stdafx.h"
#include "com.h"
#include "resource.h"
#define BUFFER_MAX (20*1024)
#define PACKET_SIZE (4*1024)
int total_size, loaded_size;
unsigned char buffer_input[BUFFER_MAX], buffer_output[BUFFER_MAX];
BOOL system_load_finished;
BOOL process_load_finished;
BOOL load_file_by_packet(HANDLE h_file, BOOL *finished)
{
unsigned char ch;
static int count;
unsigned long readed_size;
bool file_end=false;
unsigned long cur_pos;
cur_pos=SetFilePointer(h_file, 0, NULL, FILE_CURRENT);
if(cur_pos==0)
{
buffer_output[0]='S';
com_putpacket(buffer_output,1);
}
*finished=false;
com_getpacket(buffer_input, BUFFER_MAX);
if(buffer_input[0]=='R')
com_putpacket(buffer_output,count);
else
if(buffer_input[0]=='N')
{
count=0;
buffer_output[count++]='D';
while(file_end==false && count<=PACKET_SIZE) //NOTE: it must be <=, because we send a 'D'
{ //the count=1 now, if only < ,we only send nK-1B
if(ReadFile(h_file, &ch, 1, &readed_size, NULL)!=0 && readed_size==0)
{
file_end=true;
*finished=true;
}
else
{
loaded_size++;
buffer_output[count++]=ch;
}
}
com_putpacket(buffer_output,count);
}
else
if(buffer_input[0]=='W')
return false;
if(file_end==true)
{
com_getpacket(buffer_input, BUFFER_MAX);
buffer_output[0]='E';
com_putpacket(buffer_output,1);
}
return true;
}
__declspec(dllexport) void com_load_initialize()
{
system_load_finished=true;
process_load_finished=true;
}
__declspec(dllexport) int com_load_system(char **filename)
{
static HANDLE h_file[3];
static HANDLE *p_hf;
BOOL return_value;
BOOL finished;
int percent=-1;
if(system_load_finished==true)
{
if(filename[0]==NULL || filename[1]==NULL)
return -1;
else
{
if((h_file[0]=CreateFile( filename[0], GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
NULL,
NULL ))==INVALID_HANDLE_VALUE)
return -1;
if((h_file[1]=CreateFile( filename[1], GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
NULL,
NULL ))==INVALID_HANDLE_VALUE)
return -1;
h_file[2]=NULL;
p_hf=h_file;
total_size=0;
while(*p_hf!=NULL)
{
total_size+=GetFileSize(*p_hf, NULL);
p_hf++;
}
if(!is_com_opened())
{
CloseHandle(h_file[0]);
CloseHandle(h_file[1]);
return -1;
}
loaded_size=0;
p_hf=h_file;
return_value=load_file_by_packet(*p_hf, &finished);
if(finished)
p_hf++;
percent=loaded_size*100/total_size;
system_load_finished=false;
}
}
else
{
return_value=load_file_by_packet(*p_hf, &finished);
percent=loaded_size*100/total_size;
if(finished)
{
p_hf++;
if(*p_hf==NULL)
{
percent=100;
system_load_finished=true;
CloseHandle(h_file[0]);
CloseHandle(h_file[1]);
com_notify_over();
}
else
com_notify_again();
}
}
return percent;
}
__declspec(dllexport) int com_load_process(char **filename)
{
static HANDLE h_file;
bool file_end;
unsigned long readed_size;
int count;
unsigned char ch;
BOOL finished;
BOOL return_value;
int percent=-1;
if(process_load_finished==true)
{
if(filename[0]==NULL)
return -1;
if((h_file=CreateFile( filename[0], GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
NULL,
NULL ))==INVALID_HANDLE_VALUE)
return -1;
total_size=GetFileSize(h_file, NULL);
if(!is_com_opened())
{
CloseHandle(h_file);
return -1;
}
count=0;
file_end=false;
while(file_end==false && count<64) //NOTE: it must be <=, because we send a 'D'
{ //the count=1 now, if only < ,we can just send nK-1B
if(ReadFile(h_file, &ch, 1, &readed_size, NULL)!=0 && readed_size==0)
file_end=true;
else
buffer_output[count++]=ch;
}
if(strncmp((char *)buffer_output, "ARTSOS386", 9)!=0)
*(int *)buffer_output=total_size;
com_putpacket(buffer_output, count);
loaded_size=0;
SetFilePointer(h_file, 0, NULL, FILE_BEGIN);
return_value=load_file_by_packet(h_file, &finished);
if(finished)
{
percent=100;
process_load_finished=true;
com_notify_over();
CloseHandle(h_file);
}
else
percent=loaded_size*100/total_size;
process_load_finished=false;
}
else
{
return_value=load_file_by_packet(h_file, &finished);
if(finished)
{
percent=100;
process_load_finished=true;
com_notify_over();
CloseHandle(h_file);
}
else
percent=loaded_size*100/total_size;
}
return percent;
}