⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 explode.c

📁 MIDI解码程序(用VC编写)
💻 C
📖 第 1 页 / 共 2 页
字号:
static long explode_lit4(ExplodeHandler decoder, char *buff, long size)/* Decompress the imploded data using coded literals and a 4K sliding   window. */{    long s;               /* bytes to decompress */    register unsigned e;  /* table entry flag/number of extra bits */    unsigned n, d;        /* length and index for copy */    unsigned w;           /* current window position */    struct huft *t;       /* pointer to table entry */    unsigned u;           /* true if unflushed */    long j;    struct huft *tb, *tl, *td;	/* literal, length, and distance tables */    int bb, bl, bd;		/* number of bits decoded by those */    tb = decoder->tb;    tl = decoder->tl;    td = decoder->td;    bb = decoder->bb;    bl = decoder->bl;    bd = decoder->bd;  /* explode the coded data */    s = decoder->s;    w = decoder->w;    u = decoder->u;    j = 0;    while(s > 0)                 /* do until ucsize bytes uncompressed */    {	NEEDBITS(1);	if(decoder->bit_buf & 1)                  /* then literal--decode it */	{	    DUMPBITS(1);	    s--;	    NEEDBITS((unsigned)bb);    /* get coded literal */	    t = tb + IGETBITS(bb);	    e = t->e;	    while(e > 16)	    {		if(e == 99)		    return -1;		DUMPBITS(t->b);		e -= 16;		NEEDBITS(e);		t = t->v.t + IGETBITS(e);	    }	    DUMPBITS(t->b);	    buff[j++] = decoder->slide[w++] = (uch)t->v.n;	    if(w == WSIZE)		w = u = 0;	    if(j == size)	    {		decoder->u = u;		decoder->w = w;		decoder->s = s;		return size;	    }	}	else                        /* else distance/length */	{	    DUMPBITS(1);	    NEEDBITS(6);               /* get distance low bits */	    d = GETBITS(6);	    DUMPBITS(6);	    NEEDBITS((unsigned)bd);    /* get coded distance high bits */	    t = td + IGETBITS(bd);	    e = t->e;	    while(e > 16)	    {		if(e == 99)		    return -1;		DUMPBITS(t->b);		e -= 16;		NEEDBITS(e);		t = t->v.t + IGETBITS(e);		e = t->e;	    }	    DUMPBITS(t->b);	    d = w - d - t->v.n;       /* construct offset */	    NEEDBITS((unsigned)bl);    /* get coded length */	    t = tl + IGETBITS(bl);	    e = t->e;	    while(e > 16)	    {		if(e == 99)		    return -1;		DUMPBITS(t->b);		e -= 16;		NEEDBITS(e);		t = t->v.t + IGETBITS(e);		e = t->e;	    }	    DUMPBITS(t->b);	    n = t->v.n;	    if(e)                    /* get length extra bits */	    {		NEEDBITS(8);		n += GETBITS(8);		DUMPBITS(8);	    }	    /* do the copy */	    s -= n;	    while(n > 0 && j < size)	    {		n--;		d &= WSIZE - 1;		w &= WSIZE - 1;		if(u && w <= d)		{		    buff[j++] = 0;		    w++;		    d++;		}		else		    buff[j++] = decoder->slide[w++] = decoder->slide[d++];		if(w == WSIZE)		    w = u = 0;	    }	    if(j == size)	    {		decoder->u = u;		decoder->n = n;		decoder->d = d;		decoder->w = w;		decoder->s = s;		return size;	    }	    decoder->n = 0;	}    }    decoder->n = 0;    decoder->w = 0;    decoder->eof = 1;    return j;}static long explode_nolit8(ExplodeHandler decoder, char *buff, long size)/* Decompress the imploded data using uncoded literals and an 8K sliding   window. */{    long s;               /* bytes to decompress */    register unsigned e;  /* table entry flag/number of extra bits */    unsigned n, d;        /* length and index for copy */    unsigned w;           /* current window position */    struct huft *t;       /* pointer to table entry */    unsigned u;           /* true if unflushed */    long j;    struct huft *tl, *td;   /* length and distance decoder tables */    int bl, bd;             /* number of bits decoded by tl[] and td[] */    tl = decoder->tl;    td = decoder->td;    bl = decoder->bl;    bd = decoder->bd;  /* explode the coded data */#if 0  b = k = w = 0;                /* initialize bit buffer, window */  u = 1;                        /* buffer unflushed */  ml = mask_bits[bl];           /* precompute masks for speed */  md = mask_bits[bd];  s = G.ucsize;#endif    s = decoder->s;    w = decoder->w;    u = decoder->u;    j = 0;    while(s > 0)                 /* do until ucsize bytes uncompressed */    {	NEEDBITS(1);	if(decoder->bit_buf & 1) /* then literal--get eight bits */	{	    DUMPBITS(1);	    s--;	    NEEDBITS(8);	    buff[j++] = decoder->slide[w++] = (uch)decoder->bit_buf;;	    DUMPBITS(8);	    if(w == WSIZE)		w = u = 0;	    if(j == size)	    {		decoder->u = u;		decoder->w = w;		decoder->s = s;		return size;	    }	}	else                        /* else distance/length */	{	    DUMPBITS(1);	    NEEDBITS(7);               /* get distance low bits */	    d = GETBITS(7);	    DUMPBITS(7);	    NEEDBITS((unsigned)bd);    /* get coded distance high bits */	    t = td + IGETBITS(bd);	    e = t->e;	    while(e > 16)	    {		if(e == 99)		    return -1;		DUMPBITS(t->b);		e -= 16;		NEEDBITS(e);		t = t->v.t + IGETBITS(e);		e = t->e;	    }	    DUMPBITS(t->b);	    d = w - d - t->v.n;       /* construct offset */	    NEEDBITS((unsigned)bl);    /* get coded length */	    t = tl + IGETBITS(bl);	    e = t->e;	    while(e > 16)	    {		if(e == 99)		    return -1;		DUMPBITS(t->b);		e -= 16;		NEEDBITS(e);		t = t->v.t + IGETBITS(e);		e = t->e;	    }	    DUMPBITS(t->b);	    n = t->v.n;	    if(e)                    /* get length extra bits */	    {		NEEDBITS(8);		n += GETBITS(8);		DUMPBITS(8);	    }	    /* do the copy */	    s -= n;	    while(n > 0 && j < size)	    {		n--;		d &= WSIZE - 1;		w &= WSIZE - 1;		if(u && w <= d)		{		    buff[j++] = 0;		    w++;		    d++;		}		else		    buff[j++] = decoder->slide[w++] = decoder->slide[d++];		if(w == WSIZE)		    w = u = 0;	    }	    if(j == size)	    {		decoder->u = u;		decoder->n = n;		decoder->d = d;		decoder->w = w;		decoder->s = s;		return size;	    }	    decoder->n = 0;	}    }    decoder->n = 0;    decoder->w = 0;    decoder->eof = 1;    return j;}static long explode_nolit4(ExplodeHandler decoder, char *buff, long size)/* Decompress the imploded data using uncoded literals and a 4K sliding   window. */{    long s;               /* bytes to decompress */    register unsigned e;  /* table entry flag/number of extra bits */    unsigned n, d;        /* length and index for copy */    unsigned w;           /* current window position */    struct huft *t;       /* pointer to table entry */    unsigned u;           /* true if unflushed */    long j;    struct huft *tl, *td;   /* length and distance decoder tables */    int bl, bd;             /* number of bits decoded by tl[] and td[] */    tl = decoder->tl;    td = decoder->td;    bl = decoder->bl;    bd = decoder->bd;  /* explode the coded data */#if 0  b = k = w = 0;                /* initialize bit buffer, window */  u = 1;                        /* buffer unflushed */  ml = mask_bits[bl];           /* precompute masks for speed */  md = mask_bits[bd];  s = G.ucsize;#endif    s = decoder->s;    w = decoder->w;    u = decoder->u;    j = 0;    while(s > 0)                 /* do until ucsize bytes uncompressed */    {	NEEDBITS(1);	if(decoder->bit_buf & 1) /* then literal--get eight bits */	{	    DUMPBITS(1);	    s--;	    NEEDBITS(8);	    buff[j++] = decoder->slide[w++] = (uch)decoder->bit_buf;	    DUMPBITS(8);	    if(w == WSIZE)		w = u = 0;	    if(j == size)	    {		decoder->u = u;		decoder->w = w;		decoder->s = s;		return size;	    }	}	else                        /* else distance/length */	{	    DUMPBITS(1);	    NEEDBITS(6);               /* get distance low bits */#if 0	    d = (unsigned)b & 0x3f;#else	    d = GETBITS(6);#endif	    DUMPBITS(6);	    NEEDBITS((unsigned)bd);    /* get coded distance high bits */	    t = td + IGETBITS(bd);	    e = t->e;	    while(e > 16)	    {		if(e == 99)		    return -1;		DUMPBITS(t->b);		e -= 16;		NEEDBITS(e);		t = t->v.t + IGETBITS(e);		e = t->e;	    }	    DUMPBITS(t->b);	    d = w - d - t->v.n;       /* construct offset */	    NEEDBITS((unsigned)bl);    /* get coded length */	    /*t =*/ t = tl + IGETBITS(bl);	    e = t->e;	    while(e > 16)	    {		if(e == 99)		    return -1;		DUMPBITS(t->b);		e -= 16;		NEEDBITS(e);		t = t->v.t + IGETBITS(e);		e = t->e;	    }	    DUMPBITS(t->b);	    n = t->v.n;	    if(e)                    /* get length extra bits */	    {		NEEDBITS(8);		n += GETBITS(8);		DUMPBITS(8);	    }	    /* do the copy */	    s -= n;	    while(n > 0 && j < size)	    {		n--;		d &= WSIZE - 1;		w &= WSIZE - 1;		if(u && w <= d)		{		    buff[j++] = 0;		    w++;		    d++;		}		else		    buff[j++] = decoder->slide[w++] = decoder->slide[d++];		if(w == WSIZE)		    w = u = 0;	    }	    if(j == size)	    {		decoder->u = u;		decoder->n = n;		decoder->d = d;		decoder->w = w;		decoder->s = s;		return size;	    }	    decoder->n = 0;	}    }    decoder->n = 0;    decoder->w = 0;    decoder->eof = 1;    return j;}long explode(ExplodeHandler decoder, char *buff, long size)/* Explode an imploded compressed stream.  Based on the general purpose   bit flag, decide on coded or uncoded literals, and an 8K or 4K sliding   window.  Construct the literal (if any), length, and distance codes and   the tables needed to decode them (using huft_build() from inflate.c),   and call the appropriate routine for the type of data in the remainder   of the stream.  The four routines are nearly identical, differing only   in whether the literal is decoded or simply read in, and in how many   bits are read in, uncoded, for the low distance bits. */{    long j, i;    if(size <= 0)	return size;    if(!decoder->initflag)    {	decoder->initflag = 1;	if(explode_start(decoder) != 0)	    return 0;    }    j = 0;    while(j < size)    {	if(decoder->n > 0) /* do the copy */	{	    unsigned u, n, w, d;	    u = decoder->u;	    n = decoder->n;	    d = decoder->d;	    w = decoder->w;	    while(n > 0 && j < size)	    {		n--;		d &= WSIZE - 1;		w &= WSIZE - 1;		if(u && w <= d)		{		    buff[j++] = 0;		    w++;		    d++;		}		else		    buff[j++] = decoder->slide[w++] = decoder->slide[d++];		if(w == WSIZE)		    w = u = 0;	    }	    decoder->u = u;	    decoder->n = n;	    decoder->d = d;	    decoder->w = w;	    if(j == size)		return size;	}	/* decoder->n == 0 */	if(decoder->eof)	    return j;	switch(decoder->method)	{	  case EXPLODE_LIT8:	    i = explode_lit8(decoder, buff + j, size - j);	    break;	  case EXPLODE_LIT4:	    i = explode_lit4(decoder, buff + j, size - j);	    break;	  case EXPLODE_NOLIT8:	    i = explode_nolit8(decoder, buff + j, size - j);	    break;	  case EXPLODE_NOLIT4:	    i = explode_nolit4(decoder, buff + j, size - j);	    break;	  default:	    i = -1;	    break;	}	if(i == -1)	    return -1;	j += i;    }    return j;}static int fill_inbuf(ExplodeHandler decoder){    int len;    /* Read as much as possible */    decoder->insize = 0;    errno = 0;    do {	len = decoder->read_func((char*)decoder->inbuf + decoder->insize,				 (long)(INBUFSIZ - decoder->insize),				 decoder->user_val);	if(len == 0 || len == EOF) break;	decoder->insize += len;    } while(decoder->insize < INBUFSIZ);    if(decoder->insize == 0)	return EOF;    decoder->inptr = 1;    return decoder->inbuf[0];}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -