added byte order handling
authorkramm <kramm>
Thu, 13 Dec 2001 09:54:59 +0000 (09:54 +0000)
committerkramm <kramm>
Thu, 13 Dec 2001 09:54:59 +0000 (09:54 +0000)
lib/rfxswf.c
lib/rfxswf.h

index 7754ba9..5f49939 100644 (file)
@@ -545,12 +545,15 @@ TAG * RFXSWF_ReadTag(int handle,TAG * prev)
   int id;
 
   if (read(handle,&raw,2)!=2) return NULL;
+  raw = SWAP16(raw);
 
   len = raw&0x3f;
   id  = raw>>6;
 
   if (len==0x3f)
-  { if (read(handle,&len,4)!=4) return NULL;
+  {
+      if (read(handle,&len,4)!=4) return NULL;
+      len = SWAP32(len);
   }
 
   if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
@@ -610,7 +613,7 @@ int RFXSWF_WriteTag(int handle,TAG * t)
 
   if (handle>=0)
   { if (short_tag)
-    { raw[0] = len|((t->id&0x3ff)<<6);
+    { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
       if (write(handle,raw,2)!=2)
       {
         #ifdef DEBUG_RFXSWF
@@ -620,13 +623,21 @@ int RFXSWF_WriteTag(int handle,TAG * t)
       }
     }
     else
-    { raw[0] = (t->id<<6)|0x3f;
-      raw[1] = (U16)(len&0xffff);
-      raw[2] = (U16)(len>>16);
-      if (write(handle,raw,6)!=6)
+    {
+      raw[0] = SWAP16((t->id<<6)|0x3f);
+      if (write(handle,raw,2)!=2)
+      {
+#ifdef DEBUG_RFXSWF
+          fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
+#endif
+         return -1;
+      }
+      
+      len = SWAP32(len);
+      if (write(handle,&len,4)!=4)
       {
         #ifdef DEBUG_RFXSWF
-          fprintf(stderr,"WriteTag() failed: Long Header.\n");
+          fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
         #endif
         return -1;
       }
index 016f8e3..81ccf9f 100644 (file)
 #define FALSE (0)
 #endif
 
+#ifdef WORDS_BIGENDIAN
+#define SWAP16(s) ((U16) ((U8*)&s)[0] | ((U16) ((U8*)&s)[1] << 8))
+#define SWAP32(s) ((U32) ((U8*)&s)[0] | ((U32) ((U8*)&s)[1] << 8) | ((U32) ((U8*)&s)[2] << 16) | ((U32) ((U8*)&s)[3] << 24))
+#else
+#define SWAP16(x) x
+#define SWAP32(x) x
+#endif
+
 // SWF Types
 
 typedef         unsigned long   U32;