%    Copyright (C) 1989, 1996 Aladdin Enterprises.  All rights reserved.
% 
% This file is part of GNU Ghostscript.
% 
% GNU Ghostscript is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
% to anyone for the consequences of using it or for whether it serves any
% particular purpose or works at all, unless he says so in writing.  Refer
% to the GNU General Public License for full details.
% 
% Everyone is granted permission to copy, modify and redistribute GNU
% Ghostscript, but only under the conditions described in the GNU General
% Public License.  A copy of this license is supposed to have been given
% to you along with GNU Ghostscript so you can know your rights and
% responsibilities.  It should be in a file named COPYING.  Among other
% things, the copyright notice and this notice must be preserved on all
% copies.
% 
% Aladdin Enterprises supports the work of the GNU Project, but is not
% affiliated with the Free Software Foundation or the GNU Project.  GNU
% Ghostscript, as distributed by Aladdin Enterprises, does not require any
% GNU software to build or run it.

% $Id: gs_epsf.ps,v 1.1 2000/12/11 02:31:34 bhinkle Exp $
% Allow the interpreter to recognize MS-DOS EPSF file headers, and skip to
% the PostScript section of the file.

/.runnoepsf /run load def
/.epsfheader <C5D0D3C6> def
/run
 { dup type /filetype ne { (r) file } if
		% Check for MS-DOS EPSF file (see Red Book p. 729).
   true exch 0 1 3
    {		% Stack: true file index
      1 index read dup { pop dup .epsfheader 3 index get eq } if
       { pop pop }	% if matched, don't need the character
       {	% unread characters (wasn't EPSF)
	 2 index exch unread     % unread mismatch character
	 dup {   % loop unreading backwards in .epsfheader
	   1 sub dup .epsfheader exch get 2 index exch unread
	 } repeat pop
	 exch not exch exit	% change true to false
       }
      ifelse
    }
   for exch	% Stack: file true/false
    {		% This block is executed if the file is MS-DOS EPSF.
		% Build up the little-endian byte offset and length.
      2
	{ 1 0 4
	   { 2 index read not { pop exit } if % if EOF, let error happen
	     2 index mul add exch 256 mul exch
	   }
	  repeat exch pop exch
	}
      repeat
		% Stack: offset length file
		% Use flushfile to skip quickly to the start of the
		% PostScript section.
      dup 4 -1 roll 12 sub () /SubFileDecode filter flushfile
		% Now interpret the PostScript.
      exch () /SubFileDecode filter cvx .runexec
    }
    { .runnoepsf
    }
   ifelse
 } odef
