help with c translation

robby handsomerobby at fakemail.com
Thu Jul 2 02:08:25 PDT 2009


can anybody help me translate this c function to d, im having a problem with the data types. thanks.


//-------------------------
unsigned int BLZCC blz_pack(const void *source,
                            void *destination,
                            unsigned int length,
                            void *workmem)
{
   BLZPACKDATA ud;
   const unsigned char **lookup = workmem;
   const unsigned char *backptr = source;

   /* check for length == 0 */
   if (length == 0) return 0;

   /* init lookup[] */
   {
      int i;
      for (i = 0; i < BLZ_WORKMEM_SIZE/4; ++i) lookup[i] = 0;
   }

   ud.source = source;
   ud.destination = destination;

   /* first byte verbatim */
   *ud.destination++ = *ud.source++;

   /* check for length == 1 */
   if (--length == 0) return 1;

   /* init first tag */
   ud.tagpos = ud.destination;
   ud.destination += 2;
   ud.tag = 0;
   ud.bitcount = 16;

   /* main compression loop */
   while (length > 4)
   {
      const unsigned char *ppos;
      unsigned int len = 0;

      /* update lookup[] up to current position */
      while (backptr < ud.source)
      {
	 lookup[blz_hash4(backptr)] = backptr;
	 backptr++;
      }

      /* look up current position */
      ppos = lookup[blz_hash4(ud.source)];

      /* check match */
      if (ppos)
      {
	 while ((len < length) &&
		(*(ppos + len) == *(ud.source + len))) ++len;
      }

      /* output match or literal */
      if (len > 3)
      {
	 unsigned int pos = ud.source - ppos - 1;

	 /* output match tag */
	 blz_putbit(&ud, 1);

	 /* output length */
	 blz_putgamma(&ud, len - 2);

	 /* output position */
	 blz_putgamma(&ud, (pos >> 8) + 2);
	 *ud.destination++ = pos & 0x00ff;

	 ud.source += len;
	 length -= len;

      } else {

	 /* output literal tag */
	 blz_putbit(&ud, 0);

	 /* copy literal */
	 *ud.destination++ = *ud.source++;
	 length--;
      }
   }

   /* output any remaining literals */
   while (length > 0)
   {
      /* output literal tag */
      blz_putbit(&ud, 0);

      /* copy literal */
      *ud.destination++ = *ud.source++;
      length--;
   }

   /* shift last tag into position and store */
   ud.tag <<= ud.bitcount;
   ud.tagpos[0] = ud.tag & 0x00ff;
   ud.tagpos[1] = (ud.tag >> 8) & 0x00ff;

   /* return compressed length */
   return ud.destination - (unsigned char *)destination;
}




More information about the Digitalmars-d-learn mailing list