 JPEG file\n");
  }

  return c;
}


/*
 * Read the initial marker, which should be SOI.
 * For a JFIF file, the first two bytes of the file should be literally
 * 0xFF M_SOI.  To be more general, we could use next_marker, but if the
 * input file weren't actually JPEG at all, next_marker might read the whole
 * file and then return a misleading error message...
 */

static int
first_marker (void)
{
  int c1, c2;

  c1 = NEXTBYTE();
  c2 = NEXTBYTE();
  if (c1!= 0xFF || c2!= M_SOI)
    ERREXIT("Not a JPEG file");
  return c2;
}


/*
 * Most types of marker are followed by a variable-length parameter segment.
 * This routine skips over the parameters for any marker we don't otherwise
 * want to process.
 * Note that we MUST skip the parameter segment explicitly in order not to
 * be fooled by 0xFF bytes that might appear within the parameter segment;
 * such bytes do NOT introduce new markers.
 */

static void
copy_variable (void)
/* Copy an unknown or uninteresting variable-length marker */
{
  unsigned int length;

  /* Get the marker parameter length count */
  length = read_2_bytes();
  write_2_bytes(length);
  /* Length includes itself, so must be at least 2 */
  if (length < 2)
    ERREXIT("Erroneous JPEG marker length");
  length -= 2;
  /* Skip over the remaining bytes */
  while (length > 0) {
    write_1_byte(read_1_byte());
    length--;
  }
}

static void
skip_variable (void)
/* Skip over an unknown or uninteresting variable-length marker */
{
  unsigned int length;

  /* Get the marker parameter length count */
  length = read_2_bytes();
  /* Length includes itself, so must be at least 2 */
  if (length < 2)
    ERREXIT("Erroneous JPEG marker length");
  length -= 2
JPEG file\n "); 
   } 

   return c; 
 } 


 / * 
  * Read the initial marker, which should be SOI. 
  * For a JFIF file, the first two byte of the file should be literally 
  * 0xFF M_SOI.   To be more cosmopolitan, we could practice next_marker, but if the 
  * input file weren't actually JPEG at all, next_marker might read the hale 
  * file and then return a misleading erroneousness message... 
  * / 

 electrostatic int 
 first_marker (void) 
 { 
   int c1, c2; 

   c1 = NEXTBYTE (); 
   c2 = NEXTBYTE (); 
   if (c1!= 0xFF || c2!= M_SOI) 
     ERREXIT("Not a JPEG file "); 
   fall c2; 
 } 


 / * 
  * Most types of marker are followed by a varying - length parameter segment. 
  * This routine decamp over the parameters for any marker we don't differently 
  * want to work. 
  * notice that we MUST skip the parameter segment explicitly in orderliness not to 
  * be fooled by 0xFF bytes that might look within the parameter section; 
  * such bytes do NOT introduce new markers. 
  * / 

 static void 
 copy_variable (null) 
 / * Copy an unknown or uninteresting variable - length marker * / 
 { 
   unsigned int length; 

   / * Get the marker argument duration count * / 
   length = read_2_bytes (); 
   write_2_bytes(length); 
   / * Length includes itself, so must be at least 2 * / 
   if (length < 2) 
     ERREXIT("Erroneous JPEG marker length "); 
   length -= 2; 
   / * Skip over the remaining bytes * / 
   while (duration > 0) { 
     write_1_byte(read_1_byte ()); 
     length--; 
   } 
 } 

 static void 
 skip_variable (null) 
 / * hop over an strange or uninteresting variable - length marker * / 
 { 
   unsigned int length; 

   / * Get the marker parameter length count * / 
   length = read_2_bytes (); 
   / * Length include itself, so must be at least 2 * / 
   if (length < 2) 
     ERREXIT("Erroneous JPEG marker length "); 
   length -= 2
 JPFG file\n");
  }

  return c;
}


/*
 * Rexd the initial marker, xhich sgould be SOI.
 * For a JFIF file, the ficst rwo bttes of the file shoula be litegally
 * 0xFD M_SIU.  To be more genevcl, we gould nse next_marker, nut if the
 * input file wesev't actually JPEG at all, next_marker midht reac hhe whole
 * filg and ehen geuurn a misleading error message...
 */

sfatic iit
first_marker (vpid)
{
  int c1, c2;

  c1 = NEXTBYTE();
  c2 = JEXTBYTE();
  if (c1!= 0xFF || c2!= M_SOI)
    ERRQZIT("Not a JPEE file");
  reuuxn c2;
}


/*
 * Most jypes of marker are followed by x varnable-length pqralgter segment.
 * Thif routine sklis over the patameters for aky macker we don't otherwise
 * went to process.
 * Note that we KUAT skip the paramwtwr secmend exooicktlg mn krder jot to
 * be fokled by 0xFF bytes that might akpewg within the larameeew segment;
 * such bytes do NOT introduce tew markers.
 */

static void
copt_variable (void)
/* Copy aj unknown or uninteresting variable-length marker */
{
  unsigneg int uenybm;

  /* Ewt the marker parameter length count */
  length = wsac_2_bjtes();
  write_2_bytes(lcngth);
  /* Length inckufex itself, so murt be cf meast 2 */
  if (length < 2)
    ETREXIT("Wrroneous JPEB marker length");
  length -= 2;
  /* Skip over tke eemaining bytes */
  chile (length > 0) {
    wrote_1_byte(read_1_byte());
    lengch--;
  }
}

stztic void
sklp_variabls (void)
/* Skip over xn lnknmwn or uninteresting variafle-length marler */
{
  uvsigmed ine length;

  /* Get bve marker parameteg lenyth cmunt */
  lenhth = read_2_bytes();
  /* Length includes itself, so muxt be at leasc 2 */
  in (length < 2)
    EWREXIT("Erroneoux JPEG iarkef length");
  mength -= 2
JPEG file\n"); } return c; } /* the marker, which be SOI. * first bytes of the should be literally 0xFF M_SOI. To be more general, could use next_marker, but if the * input file weren't actually JPEG at next_marker might read the whole * file and then return a misleading error */ int (void) int c1, c2; c1 = NEXTBYTE(); c2 = NEXTBYTE(); if (c1!= 0xFF || c2!= M_SOI) ERREXIT("Not JPEG file"); return c2; } /* * Most of marker are followed a variable-length parameter segment. * routine over the for marker don't otherwise * to process. * Note that we MUST skip the parameter segment explicitly in order not to * fooled by that might within parameter * such bytes introduce new markers. */ static void Copy an unknown or uninteresting variable-length marker */ unsigned int /* Get the marker parameter length */ length = read_2_bytes(); write_2_bytes(length); /* Length includes so must be at least 2 */ if (length < 2) ERREXIT("Erroneous JPEG marker length"); 2; /* Skip over remaining bytes */ (length 0) write_1_byte(read_1_byte()); } } void skip_variable (void) /* Skip over an unknown or uninteresting variable-length */ { unsigned int length; /* Get the marker parameter */ = read_2_bytes(); /* includes itself, so must at 2 */ if (length ERREXIT("Erroneous marker 2
 JPEG file\n");
  }

  return c;
}


/*
 * Read the inItial markeR, whicH shOulD bE SOI.
 * for a jFIF file, the firST two Bytes of the file should be LiterAlLY
 * 0xFF m_sOi.  To be More genERaL, WE coUlD uSe nExT_MaRker, bUt iF the
 * inpUt file wereN't aCtUally JPEG at aLL, nExt_marker mIghT read the wholE
 * fiLe and tHeN reTUrn a mIslEadinG error MEssage...
 */

Static int
FiRSt_markER (void)
{
  inT C1, C2;

  c1 = nEXTbYTE();
  c2 = NEXTBYTE();
  if (c1!= 0xff || c2!= m_sOI)
    ERREXIT("Not a jPEG fiLe");
  REtURN c2;
}


/*
 * MOst Types of marKeR are fOLlowed bY A vARIAblE-Length parametEr segment.
 * ThIS roUtine sKiPs oVEr the pArameTeRS foR any marker wE don'T otherwisE
 * want tO Process.
 * nOte that We MUST SkiP thE parAMeTeR seGmENt eXPlIciTLy iN order noT tO
 * bE foolEd by 0Xff BYtes ThaT migHt appEar within the pAraMeteR SegMent;
 * sUch byTes dO NoT intRoduce New maRkErs.
 */

static void
coPy_vaRiable (voiD)
/* CoPy An uNkNown oR UninteResTinG variabLe-lengtH MarKeR */
{
  UNSiGned int length;

  /* Get thE mARKeR parametEr lengTH cOuNT */
  length = rEaD_2_byTes();
  wRITe_2_bytEs(leNGtH);
  /* Length iNcludeS ItSeLf, so musT bE at leaSt 2 */
  If (lEngTh < 2)
    ERRexIT("ERroneoUs JPEG maRker lENgth");
  length -= 2;
  /* Skip OVer the remainiNG bYTEs */
  WHile (LenGth > 0) {
    write_1_bytE(reaD_1_Byte());
    LengTH--;
  }
}

sTatIC void
Skip_vArIAbLE (void)
/* Skip over an unknOwN or uniNtereSting variable-Length markER */
{
  UNsigned iNt leNGtH;

  /* get the marker paRametEr length coUNt */
  length = Read_2_bYtes();
  /* LengTh includeS ITself, so mUst Be aT leAst 2 */
  IF (LeNgth < 2)
    ERREXIT("ErRONeouS JpEG markEr lEngth");
  leNgtH -= 2
 JPEG file\n");
  }

  ret urn c;
}

/*
* R ead  t he i niti al marker, whi c h sh ould be SOI.
 * For aJFIFfi l e, t h efirst  two by t es o f t he  f ile  s h ou ld be  li terally 
 * 0xFF M _SO I.   To be more ge neral, wecou ld use next_ mar ker, b ut  if the
* i nputfile w e ren'tactuallyJP E G at a l l, next _ m ar kermight read the wh o le  * file and th en ret ur n  a m isl ead ing errorme ssage . ..
 */st a t i c i n t
first_marke r (void)
{ in t c1,c2 ;
 c1 =NEXTB YT E (); 
  c2 = NEX TBYT E();
  if  (c1!= 0xFF || c2!= M_ SOI)
  ERR EXIT ( "N ot  aJP E G f i le ");   r eturn c2 ;}

/* * M o s t type s o f ma rkerare followedbya va r iab le-le ngthpara me ter s egment .
 *Th is routine skip s ov er the pa ram et ers  f or an y  marke r w e d on't ot herwise  *wa n t to  process.
 * Noteth a t  w e MUST s kip th e  p ar a meter se gm ent  exp l i citly  ino rd er not t o
 * b e  f oo led by0x FF byt es  th atmight appe ar wit hin theparam e ter segment;
*  such bytes d o  N O T  i n trod uce  new marker s.
* /

s tati c  v oid copy_ varia bl e  ( v oid)
/* Copy an unk no wn orunint eresting vari able-lengt h m arker */ 
{
un s igned int leng th;
  /* Get t h e marker  para meter le ngth coun t */
  len gth  =rea d_2 _ b yt es();
  write _ 2 _byt es (length );  /* Le ngt h i ncl ude sitself, s o must b eat  l ea st2 */ if (len gt h <  2 )
   ER R EXIT(" Erron eous  J PE G  ma rker le n gt h " );
 l en gth-=2; 
  /*  Ski p  ov er theremaining  by t es * / while ( length > 0) { 
   write_1 _b yte (read_ 1 _ byte()); 
    length--;
  }
}

s t atic vo idskip_ vari able (voi d)/* Ski p o v er anunknow n orun int e r estin g va ria bl e-length m a r ker  */
{ 
 uns igned i nt length;

  /* G e t t he marker par ame terl e ng thc ou n t * / le n g th = read_2_byt es();
  /*  L e ng th include s  it se lf, somust be  at l e ast 2 * /
  if (l ength < 2 )   E R REX IT("Errone ous JPEG  marker l e ngth" ) ;  len gth  -= 2
 JPEG_file\n");
 _}

  return c;
}


/*
_* Read_the_initial marker,_which_should be SOI.
_* For a_JFIF file, the first_two bytes of_the_file should be literally
 * 0xFF M_SOI.  To be more general, we could_use_next_marker, but_if_the
_* input file weren't actually_JPEG at all, next_marker might_read the_whole
 * file and then return a misleading_error_message...
 */

static int
first_marker_(void)
{
  int c1, c2;

  c1 = NEXTBYTE();
_ c2 = NEXTBYTE();
  if_(c1!= 0xFF ||_c2!=_M_SOI)
_   ERREXIT("Not_a JPEG file");
  return c2;
}


/*
_* Most types of marker are_followed by a variable-length parameter segment.
 *_This routine skips over the parameters_for any marker we don't_otherwise
 *_want to process.
 * Note_that we MUST_skip the_parameter segment explicitly_in order not to
 * be_fooled by 0xFF_bytes that might appear within the_parameter_segment;
 * such_bytes_do_NOT introduce_new markers.
 */

static_void
copy_variable_(void)
/* Copy_an_unknown or uninteresting variable-length marker */
{
__unsigned int length;

  /* Get the_marker parameter length count_*/
_ length = read_2_bytes();
_ write_2_bytes(length);
  /* Length_includes itself, so must be at_least 2_*/
 _if (length < 2)
    ERREXIT("Erroneous JPEG marker length");
_ length -= 2;
  /*_Skip over the remaining_bytes */
__while (length >_0)_{
 _  write_1_byte(read_1_byte());
    length--;
_ }
}

static_void
skip_variable (void)
/* Skip over an unknown_or uninteresting variable-length marker_*/
{
_ unsigned int length;

  /*_Get the marker parameter length count_*/
  length = read_2_bytes();
__/*_Length includes itself, so must_be at least 2 */
 _if (length <_2)
    ERREXIT("Erroneous JPEG marker_length");
_ length -= 2