home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 403.lha / VediSrc / unpacker.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-06  |  2.0 KB  |  59 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.  *----------------------------------------------------------------------*/
  14. #include "iff/packer.h"
  15. #include "functions.h"
  16.  
  17. /*----------- UnPackRow ------------------------------------------------*/
  18.  
  19. #define UGetByte()    (*source++)
  20. #define UPutByte(c)   (*dest++ = (c))
  21.  
  22. /* Given POINTERS to POINTER variables, unpacks one row, updating the source
  23.  * and destination pointers until it produces dstBytes bytes. */
  24. BOOL UnPackRow(pSource, pDest, srcBytes0, dstBytes0)
  25.    BYTE **pSource, **pDest;  WORD srcBytes0, dstBytes0; {
  26.     register BYTE *source = *pSource;
  27.     register BYTE *dest   = *pDest;
  28.     register WORD n;
  29.     register BYTE c;
  30.     register WORD srcBytes = srcBytes0, dstBytes = dstBytes0;
  31.     BOOL error = TRUE;   /* assume error until we make it through the loop */
  32.     WORD minus128 = -128L; /* get the compiler to generate a CMP.W */
  33.  
  34.     while( dstBytes > 0L )  {
  35.         if ( (srcBytes -= 1L) < 0L )  goto ErrorExit;
  36.         n = UGetByte();
  37.  
  38.         if (n >= 0L) {
  39.             n += 1L;
  40.             if ( (srcBytes -= n) < 0L )  goto ErrorExit;
  41.             if ( (dstBytes -= n) < 0L )  goto ErrorExit;
  42.             do {  UPutByte(UGetByte());  } while (--n > 0L);
  43.             }
  44.  
  45.         else if (n != minus128) {
  46.             n = -n + 1L;
  47.             if ( (srcBytes -= 1L) < 0L )  goto ErrorExit;
  48.             if ( (dstBytes -= n ) < 0L )  goto ErrorExit;
  49.             c = UGetByte();
  50.             do {  UPutByte(c);  } while (--n > 0L);
  51.             }
  52.         }
  53.     error = FALSE;      /* success! */
  54.  
  55.   ErrorExit:
  56.     *pSource = source;  *pDest = dest;
  57.     return(error);
  58.     }
  59.