Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* efax08a fix
@ 1997-02-28  0:11 David Mosberger-Tang
  0 siblings, 0 replies; only message in thread
From: David Mosberger-Tang @ 1997-02-28  0:11 UTC (permalink / raw)


Here is a patch that fixes efax08a so it works on 64-bit platforms too
(such as Linux/Alpha).  The code isn't as optimized as it could be,
but I opted for readability & portability instead (performance isn't
much of an issue anyhow in the affected code).

With the patch in place, a fax received on a 64 bit system can be
processed with xv and efix.  The code should continue to work fine on
32-bit platforms and big-endian machines, though I can't test those
cases easily.

	--david

--
--- efax08a/efaxlib.c	Sun Aug 18 19:06:52 1996
+++ efax08a-alpha/efaxlib.c	Thu Feb 27 16:42:26 1997
@@ -37,25 +37,6 @@
 
 short short256 = 256 ;
 
-void swapl( unsigned long *x )
-{
-  u_char c[4] ;
-  c[0] = ( (u_char*) x ) [3] ;
-  c[1] = ( (u_char*) x ) [2] ;
-  c[2] = ( (u_char*) x ) [1] ;
-  c[3] = ( (u_char*) x ) [0] ;
-  *x = * (unsigned long*) c ;
-}
-
-void swaps( unsigned short *x )
-{
-  u_char c[2] ;
-  c[0] = ( (u_char*) x ) [1] ;
-  c[1] = ( (u_char*) x ) [0] ;
-  *x = * (unsigned short*) c ;
-}
-
-
 /* Make sure printf strings have only %d escapes and n or fewer
    of them.  Returns 0 if OK, 1 on error. */
 
@@ -539,6 +520,90 @@
 }
 
 
+/* Read 16 bits off of F taking into account the endianness of the file.  */
+static int read16( unsigned short *sp, IFILE *f )
+{
+  u_char c[2] ;
+
+  if ( ifread ( c, 2, f) != 2 )
+    return 1 ;
+
+  if ( f->bigend )
+    *sp = c[1] | c[0] << 8 ;
+  else
+    *sp = c[0] | c[1] << 8 ;
+  return 0;
+}
+
+/* Read 32 bits off of F taking into account the endianness of the file.  */
+static int read32( unsigned long *lp, IFILE *f )
+{
+  u_char c[4] ;
+
+  if ( ifread ( c, 4, f) != 4 )
+    return 1 ;
+
+  if ( f->bigend )
+    *lp = c[3] | c[2] << 8 | c[1] << 16 | c[0] << 24;
+  else
+    *lp = c[0] | c[1] << 8 | c[2] << 16 | c[3] << 24;
+  return 0;
+}
+
+/* Write 16 bits to F in the host byte-order.  */
+static void write16( unsigned short s, OFILE *f )
+{
+  u_char c[2] ;
+
+  if (bigendian) {
+    c[0] = (s >> 8) & 0xff ; c[1] = s & 0xff ;
+  } else {
+    c[0] = s & 0xff ; c[1] = (s >> 8) & 0xff ;
+  }
+  fwrite ( c, 2, 1, f->f );
+}
+
+static void write32( unsigned long l, OFILE *f )
+{
+  u_char c[4] ;
+
+  if (bigendian) {
+    c[0] = (l >> 24) & 0xff ; c[1] = (l >> 16) & 0xff ; 
+    c[2] = (l >>  8) & 0xff ; c[3] = (l >>  0) & 0xff ; 
+  } else {
+    c[0] = (l >>  0) & 0xff ; c[1] = (l >>  8) & 0xff ;
+    c[2] = (l >> 16) & 0xff ; c[3] = (l >> 24) & 0xff ;
+  }
+  fwrite ( c, 4, 1, f->f );
+}
+
+static void write64( unsigned long l0, unsigned long l1, OFILE *f )
+{
+  if (bigendian) {
+    write32 (l1, f); write32 (l0, f);
+  } else {
+    write32 (l0, f); write32 (l1, f);
+  }
+}
+
+/* Read a TIFF tag from file F.  */
+static int readtag( tifftag *t, IFILE *f )
+{
+  if ( read16 (&t->tag, f) || read16 (&t->type, f)
+       || read32 (&t->count, f) || read32(&t->offset, f))
+    return 1;
+  return 0 ;
+}
+
+/* Write a TIFF tag to file F.  */
+static void writetag( tifftag *t, OFILE *f )
+{
+  write16 (t->tag, f);
+  write16 (t->type, f);
+  write32 (t->count, f);
+  write32 (t->offset, f);
+}
+
 /* Read run lengths for one scan line from T.4-coded IFILE f into buffer
    runs using decoder state d.  If pointer pels is not null it is used to
    save pixel count.  Returns number of runs stored, EOF on RTC, or -2 on
@@ -748,36 +813,28 @@
   msg ( "F+ TIFF directory at %ld", iftell ( f ) ) ;
 
   if ( ! err ) {
-    if ( ifread ( (u_char*) &ntag, 2, f ) != 2 ) {
+    if ( read16 ( &ntag, f ) ) {
       msg ( "E2can't read TIFF tag count" ) ;
     } else {
-      if ( f->bigend != bigendian ) swaps ( &ntag ) ;
       msg ( "F+  with %d tags", (int) ntag ) ;
     }
   }
 
   while ( ! err && ntag-- > 0 ) {
 
-    if ( ifread ( (u_char*) &t, sizeof(t), f ) != sizeof(t) ) {
+    if ( readtag ( &t, f ) ) {
       err = msg ( "E2can't read TIFF tag" ) ;
       continue ;
     }
 
-    if ( f->bigend != bigendian ) {
-      swaps ( &t.tag ) ;
-      swaps ( &t.type ) ;
-      swapl ( &t.count ) ;
-      swapl ( &t.offset ) ;
-    }
-    
     switch ( t.type ) {
     case 1 : tv = * (u_char*) &t.offset ; break ;
     case 2 : break ; 
     case 3 : tv = * (short* ) &t.offset ; break ;
-    case 4 : tv = * (long*  ) &t.offset ; break ;
+    case 4 : tv = t.offset ; break ;
     case 5 : 
       if ( ( where = iftell ( f ) ) < 0 || ifseek ( f, t.offset ) || 
-	  ifread ( (u_char*) l, 8, f ) != 8 || ifseek ( f, where ) ) {
+	  read32 ( &l[0], f ) || read32 ( &l[1], f ) || ifseek ( f, where ) ) {
 	err = msg ( "E2can't read/seek tag data" ) ;
       } else {
 	ftv = (float) l[0] / ( l[1] ? l[1] : 1 ) ;
@@ -848,7 +905,7 @@
   
   if ( ! err ) {
   
-    if ( ifread ( (u_char*) &f->next, 4, f ) != 4 ) {
+    if ( read32 ( &f->next, f ) ) {
       err = msg ( "E2can't read offset to next TIFF directory" ) ;
       f->next = -1 ;
     } else {
@@ -970,17 +1027,13 @@
 
 int tiff_first ( IFILE *f )
 {
-  short magic, version ;
+  short version ;
+  u_char magic[2] ;
 
-  ifread ( (u_char*) &magic,  2, f ) ;
-  f->bigend = ( *(u_char*) &magic == 'M' ) ? 1 : 0 ;
-  ifread ( (u_char*) &version, 2, f ) ;
-  ifread ( (u_char*) &f->next, 4, f ) ;
-  
-  if ( f->bigend != bigendian ) {
-    swaps ( &version ) ;
-    swapl ( &f->next ) ;
-  }
+  ifread ( magic,  2, f ) ;
+  f->bigend = ( magic[0] == 'M' ) ? 1 : 0 ;
+  read16 ( &version, f ) ;
+  read32 ( &f->next, f ) ;
 
   msg ( "F TIFF version %d.%d file (%s-endian)",
        version/10, version%10, f->bigend ? "big" : "little" ) ;
@@ -1517,7 +1570,6 @@
   int err=0, compr ;
   short magic, version, ntags ;
   long diroff, tdoff, doff ;
-  long b[2] ;
   tifftag t ;
 
   /* compute header values and offsets within file */
@@ -1526,8 +1578,8 @@
   version = 42 ;
   diroff = 8 ;
   ntags = NTAGS ;
-  tdoff = diroff + 2 + NTAGS*sizeof(t) + 4 ;
-  doff = tdoff + NRATIO*sizeof(b) ;
+  tdoff = diroff + 2 + NTAGS*12 + 4 ;
+  doff = tdoff + NRATIO*8 ;
 
   switch ( f->format ) {
   case TIFF_RAW: compr = 1 ; break ;
@@ -1539,19 +1591,24 @@
 
   /* 0 ==> (start of TIFF file) */
 
-  fwrite ( &magic,   2, 1, f->f ) ;
-  fwrite ( &version, 2, 1, f->f ) ;
-  fwrite ( &diroff,  4, 1, f->f ) ;
+  write16 ( magic,   f ) ;
+  write16 ( version, f ) ;
+  write32 ( diroff,  f ) ;
 
   /* 8 ==> diroff (directory offset) */
 
-  fwrite ( &ntags,   2, 1, f->f ) ;
+  write16 ( ntags,   f ) ;
 
 #define tagl(a,b,c,d) t.tag=a ; t.type=b ; t.count=c ; t.offset=d ; \
-  fwrite ( &t, sizeof(t), 1, f->f ) ;
+  writetag ( &t, f ) ;
 
-#define tags(a,b,c,d) t.tag=a ; t.type=b ; t.count=c ; *(short*)&t.offset=d ; \
-  fwrite ( &t, sizeof(t), 1, f->f ) ;
+#define tags(a,b,c,d)				\
+  t.tag=a ; t.type=b ; t.count=c ;		\
+  if (bigendian)				\
+    t.offset=d << 16;				\
+  else						\
+    t.offset=d ;				\
+  writetag ( &t, f ) ;
 
   tagl( 254, 4, 1, 1 ) ;	/* subfile long */
   tagl( 256, 4, 1, f->w ) ;	/* width long */
@@ -1576,13 +1633,12 @@
   tags( 296, 3, 1, 2 ) ;	/* resolution units(2=in,3=cm) short */
   tags( 327, 3, 1, 0 ) ;	/* clean fax(0=clean/1=regen/2=errors) short */
 
-  diroff=0 ;
-  fwrite ( &diroff, 4, 1, f->f ) ;
+  write32 ( 0, f ) ;
 
   /* ==> tdoff (tag data offset) */
 
-  b[0]=f->xres+0.5 ; b[1]=1; fwrite ( b, 8, 1, f->f ) ;
-  b[0]=f->yres+0.5 ; b[1]=1; fwrite ( b, 8, 1, f->f ) ;
+  write64 (f->xres + 0.5, 1, f);
+  write64 (f->yres + 0.5, 1, f);
 
   /* ==> doff (strip data offset) */
 

--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject.  Do not send it to axp-list@redhat.com

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1997-02-28  0:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-02-28  0:11 efax08a fix David Mosberger-Tang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox