home *** CD-ROM | disk | FTP | other *** search
/ Dream 44 / Amiga_Dream_44.iso / Linux / Apps / xanim.tgz / xanim / xanim27064 / unpacker.c < prev    next >
C/C++ Source or Header  |  1997-01-26  |  2KB  |  80 lines

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