home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 004.lha / Dpaintx_source / unpacker.c < prev   
C/C++ Source or Header  |  1986-03-02  |  2KB  |  64 lines

  1. /*----------------------------------------------------------------------*
  2.  * unpacker.c Convert data from "cmpByteRun1" run compression. 11/11/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.  * modified by C. Scheppner
  15.  *    expects packer.h in include/iff/
  16.  *----------------------------------------------------------------------*/
  17. #include "exec/types.h"
  18. #include "iff/packer.h"
  19.  
  20.  
  21. /*----------- UnPackRow ------------------------------------------------*/
  22.  
  23. #define UGetByte()   (*source++)
  24. #define UPutByte(c)   (*dest++ = (c))
  25.  
  26. /* Given POINTERS to POINTER variables, unpacks one row, updating the source
  27.  * and destination pointers until it produces dstBytes bytes. */
  28. BOOL UnPackRow(pSource, pDest, srcBytes0, dstBytes0)
  29.    BYTE **pSource, **pDest;  WORD srcBytes0, dstBytes0; {
  30.     register BYTE *source = *pSource;
  31.     register BYTE *dest   = *pDest;
  32.     register WORD n;
  33.     register BYTE c;
  34.     register WORD srcBytes = srcBytes0, dstBytes = dstBytes0;
  35.     BOOL error = TRUE;   /* assume error until we make it through the loop */
  36.     WORD minus128 = -128;  /* get the compiler to generate a CMP.W */
  37.  
  38.     while( dstBytes > 0 )  {
  39.    if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  40.        n = UGetByte();
  41.  
  42.        if (n >= 0) {
  43.        n += 1;
  44.        if ( (srcBytes -= n) < 0 )  goto ErrorExit;
  45.        if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  46.        do {  UPutByte(UGetByte());  } while (--n > 0);
  47.        }
  48.  
  49.        else if (n != minus128) {
  50.        n = -n + 1;
  51.        if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  52.        if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  53.        c = UGetByte();
  54.        do {  UPutByte(c);  } while (--n > 0);
  55.        }
  56.    }
  57.     error = FALSE;   /* success! */
  58.  
  59.   ErrorExit:
  60.     *pSource = source;  *pDest = dest;
  61.     return(error);
  62.     }
  63.  
  64.