help with c translation
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Thu Jul 2 02:39:36 PDT 2009
robby wrote:
> 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;
> }
I'm no C expert, but I'll give it a shot.
unsigned int -> uint (or size_t, if it is the length of an array)
unsigned char -> ubyte
For future reference, a handy table of C-to-D type correspondences can
be found here:
http://www.digitalmars.com/d/2.0/htomodule.html
You didn't say whether you use D1 or D2. If it is D1 I believe you
should also replace "const void *" by just "void*" in the function
declaration.
Hope this helps,
-Lars
More information about the Digitalmars-d-learn
mailing list