home *** CD-ROM | disk | FTP | other *** search
- { Copyright (c) 1994-96 by Piotr Warezak and Rafal Wierzbicki, Lodz, Poland
-
- Check_if_packed function; for Turbo Pascal 5.X, 6.0 and 7.0.
-
- =========================================================================
-
- Incompatibility problems please report to: awarezak@krysia.uni.lodz.pl
- or to any WWPACK official distribution site.
-
- This program is public domain and may be freely distributed.
-
- =========================================================================
-
- function check_if_packed(var f:file);
- checks if the assigned and opened file is compressed with WWPACK.
-
- Function returns byte:
-
- when the file is compressed with WWPACK function returns
- WWPACK version and compression command:
- 3.00 3.01 3.02 3.03 3.04
- PR 9 9 14 17 21
- P 10 12 15 18 22
- PU 11 13 16 19 23
- PP n/a n/a n/a 20 24
-
- or:
- 0 - not packed with WWPACK
- 1 - not an EXE file
- 2 - unrecognized WWPACK version
- 3 - error while reading the file
-
- ========================================================================= }
-
- function check_if_packed(var f:file):byte; { Warning: 'f' must be }
- var start,size,old_position:longint; { assigned and opened }
- header:array [1..16]of word; { EXE file. }
- buf:array [1..75] of Byte;
- ver:word;
- begin
- {$I-}
- ver:=0;
-
- {*** store old FilePosition ***}
- old_position:=filepos(f);
- seek(f,0);size:=filesize(f);
- if ioresult<>0 then
- begin
- check_if_packed:=3;seek(f,old_position);ver:=ioresult;exit;
- end;
-
- {*** check if EXE file ***}
- if size<32 then
- begin
- check_if_packed:=1;seek(f,old_position);exit;
- end;
- blockread(f,header,32);
- if (header[1]<>ord('M')+256*ord('Z')) and (header[1]<>ord('Z')+256*ord('M')) then
- begin
- check_if_packed:=1;seek(f,old_position);exit;
- end;
- if ioresult<>0 then
- begin
- check_if_packed:=3;seek(f,old_position);ver:=ioresult;exit;
- end;
-
- {*** jump to the begin of the code (jump to CS:IP address) ***}
- start:=longint(header[12])*16+header[11]+header[5]*16;
- if start>=65536*16 then dec(start,65536*16);
- if start+74<size then
- begin
-
- {*** read first 75 bytes of the code ***}
- Seek(f,start-2);BlockRead(f,buf[1],75);
- if ioresult<>0 then
- begin
- check_if_packed:=3;seek(f,old_position);ver:=ioresult;exit;
- end;
-
- {*** check if WWPACK 3.00/3.01 PR code ***}
- if (buf[3]=$be) and (buf[6]=$ba) and (buf[9]=$bf) and (buf[12]=$b9) then
- begin
- if buf[2]=9 then ver:=9
- else
- begin
- check_if_packed:=2;
- seek(f,old_position);
- exit;
- end;
- end;
-
- {*** check if WWPACK 3.02/3.03/3.04 PR code ***}
- if (buf[3]=$be) and (buf[6]=$bf) and (buf[9]=$b9) and (buf[12]=$8c) and
- (buf[13]=$cd) and (buf[14]=$81) and (buf[15]=$ed) and (buf[18]=$8b) and
- (buf[19]=$dd) then
- begin
- buf[2]:=buf[2]+14;ver:=buf[2];
- if (ver<>14) and (ver<>17) and (ver<>21) then
- begin
- check_if_packed:=2;seek(f,old_position);exit;
- end;
- end;
-
- {*** check if WWPACK 3.0x P/PU/PP code ***}
- if (buf[3]=$b8) and (buf[6]=$8c) and (buf[7]=$ca) and (buf[8]=$03) and
- (buf[9]=$d0) and (buf[10]=$8c) and (buf[11]=$c9) and (buf[12]=$81) and
- (buf[16]=$51) then
- begin
- ver:=buf[2];
- if (ver<10) or (ver>24) or (ver=14) or (ver=17) or (ver=21) then
- begin
- check_if_packed:=2;seek(f,old_position);exit;
- end;
- end;
- end;
-
- {*** restore old FilePosition and return WWPACK's version ***}
- check_if_packed:=ver;seek(f,old_position);
- {$I+}
- end;