home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / space / software / unix / xanim / unpacker.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-04  |  1.9 KB  |  64 lines

  1. /*----------------------------------------------------------------------*
  2.  * unpacker.c Convert data from "cmpByteRun1" run compression. 11/15/85
  3.  *
  4.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  5.  * This software is in the public domain.
  6.  *
  7.  *    control bytes:
  8.  *     [0..127]   : followed by n+1 bytes of data.
  9.  *     [-1..-127] : followed by byte to be repeated (-n)+1 times.
  10.  *     -128       : NOOP.
  11.  *
  12.  * This version for the Commodore-Amiga computer.
  13.  * Modified for use with Unix
  14.  *----------------------------------------------------------------------*/
  15. #include <stdio.h>
  16. #include "mytypes.h"
  17.  
  18.  
  19. /*----------- UnPackRow ------------------------------------------------*/
  20.  
  21. #define UGetByte()    (*source++)
  22. #define UPutByte(c)    (*dest++ = (c))
  23.  
  24. /* Given POINTERS to POINTER variables, unpacks one row, updating the source
  25.  * and destination pointers until it produces dstBytes bytes. */
  26.  
  27. int UnPackRow(pSource, pDest, srcBytes0, dstBytes0)
  28. BYTE  **pSource, **pDest;  LONG *srcBytes0, *dstBytes0; 
  29. {
  30.     register BYTE *source = *pSource; 
  31.     register BYTE *dest   = *pDest;
  32.     register int n;
  33.     register BYTE c;
  34.     register LONG srcBytes = *srcBytes0, dstBytes = *dstBytes0;
  35.     int error = TRUE;    /* assume error until we make it through the loop */
  36.  
  37.     while( dstBytes > 0 )  {
  38.     if ( (srcBytes -= 1) < 0 )  {error=1; goto ErrorExit;}
  39.         n = UGetByte() & 0xff;
  40.  
  41.         if (!(n & 0x80)) {
  42.         n += 1;
  43.         if ( (srcBytes -= n) < 0 )  {error=2; goto ErrorExit;}
  44.         if ( (dstBytes -= n) < 0 )  {error=3; goto ErrorExit;}
  45.         do {  UPutByte(UGetByte());  } while (--n > 0);
  46.         }
  47.  
  48.         else if (n != 0x80) {
  49.         n = (256-n) + 1;
  50.         if ( (srcBytes -= 1) < 0 )  {error=4; goto ErrorExit;}
  51.         if ( (dstBytes -= n) < 0 )  {error=5; goto ErrorExit;}
  52.         c = UGetByte();
  53.         do {  UPutByte(c);  } while (--n > 0);
  54.         }
  55.     }
  56.     error = FALSE;    /* success! */
  57.  
  58.   ErrorExit:
  59.     *pSource = source;  *pDest = dest; 
  60.     *srcBytes0 = srcBytes; *dstBytes0 = dstBytes;
  61.     return(error);
  62.     }
  63.  
  64.