unit serv_param;

interface

function IsThereParams:Boolean;

implementation

uses Windows, SysUtils, Classes, Service;

function RawDiskToFile(const sourceDisk, targetFile:String):boolean;
const
  _bufSize = 10 * 1024;
var
  _handleSource, _handleTarget :Integer;
  _countRead : LongWord;
  _buffer:array[0.._bufSize] of byte;
begin
  _handleSource:=CreateFile(PChar(Format('\\.\%s',[sourceDisk])),GENERIC_READ or GENERIC_WRITE,0,nil,OPEN_EXISTING,
    FILE_FLAG_SEQUENTIAL_SCAN,0);
  Result := _handleSource = INVALID_HANDLE_VALUE;
  if not Result then
    begin
       _handleTarget := FileCreate(targetFile);
       if _handleTarget <> -1 then
        begin
          _countRead := _bufSize;
          while _countRead = _bufSize do
            begin
              ReadFile(_handleSource,_buffer,_bufSize,_countRead,nil);
              FileWrite(_handleTarget,_buffer,_countRead);
            end;
        end
       else
        Result := false;
    end;
  if _handleSource <> INVALID_HANDLE_VALUE then CloseHandle(_handleSource);
  if _handleTarget <> -1 then FileClose(_handleTarget);
end;

procedure CreateFileSize(const fname:String;fsize:LongWord);
var
   fHandle:Integer;
   _res:Integer;
begin
  fHandle:=CreateFile(PChar(fname),  GENERIC_WRITE, 0 , 0, CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
  if fHandle = $FFFFFFFF then
    BX_PANIC('can''t open cd drive')
  else
    begin
      _res:=SetFilePointer(fHandle,fsize,nil,FILE_BEGIN);
      CloseHandle(fHandle);
    end;

end;

function IsThereParams:Boolean;
var
  _s,_s1,_s2:String;
begin
  Result:=True;
  _s:=ParamStr(1);
  if _s[1]<>'-' then
    result:=false
  else
    begin
      _s:=Copy(ParamStr(1),2,1);
      case _s[1] of
        'p':
          begin
            _s1:=ParamStr(2);
            _s2:=ParamStr(3);
            assert((Length(_s1)>0) and ((Length(_s2)>0)));
            RawDiskToFile(_s1,_s2);
          end;
        'd':
          begin
            _s1:=ParamStr(2);
            _s2:=ParamStr(3);
            assert((Length(_s1)>0) and ((Length(_s2)>0)));
            CreateFileSize(_s1,StrToInt(_s2));
          end;
      end;
    end;  
end;

end.
