Lines Matching refs:bd
93 static unsigned int get_bits(struct bunzip_data *bd, char bits_wanted)
99 while (bd->inbufBitCount < bits_wanted) {
102 if (bd->inbufPos == bd->inbufCount) {
103 if (0 >= (bd->inbufCount = read(bd->in_fd, bd->inbuf, IOBUF_SIZE)))
105 bd->inbufPos = 0;
109 if (bd->inbufBitCount>=24) {
110 bits = bd->inbufBits&((1<<bd->inbufBitCount)-1);
111 bits_wanted -= bd->inbufBitCount;
113 bd->inbufBitCount = 0;
117 bd->inbufBits = (bd->inbufBits<<8) | bd->inbuf[bd->inbufPos++];
118 bd->inbufBitCount += 8;
122 bd->inbufBitCount -= bits_wanted;
123 bits |= (bd->inbufBits>>bd->inbufBitCount) & ((1<<bits_wanted)-1);
144 static int read_block_header(struct bunzip_data *bd, struct bwdata *bw)
151 ii = get_bits(bd, 24);
152 jj = get_bits(bd, 24);
153 bw->headerCRC = get_bits(bd,32);
162 if (get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT;
163 if ((bw->origPtr = get_bits(bd,24)) > bd->dbufSize) return RETVAL_DATA_ERROR;
170 hh = get_bits(bd, 16);
171 bd->symTotal = 0;
174 kk = get_bits(bd, 16);
177 bd->symToByte[bd->symTotal++] = (16 * ii) + jj;
182 bd->groupCount = get_bits(bd,3);
183 if (bd->groupCount<2 || bd->groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR;
192 if (!(bd->nSelectors = get_bits(bd, 15))) return RETVAL_DATA_ERROR;
193 for (ii=0; ii<bd->groupCount; ii++) bd->mtfSymbol[ii] = ii;
194 for (ii=0; ii<bd->nSelectors; ii++) {
197 for(jj=0;get_bits(bd,1);jj++)
198 if (jj>=bd->groupCount) return RETVAL_DATA_ERROR;
201 uc = bd->mtfSymbol[jj];
202 memmove(bd->mtfSymbol+1, bd->mtfSymbol, jj);
203 bd->mtfSymbol[0] = bd->selectors[ii] = uc;
208 symCount = bd->symTotal+2;
209 for (jj=0; jj<bd->groupCount; jj++) {
215 hh = get_bits(bd, 5);
223 kk = get_bits(bd, 2);
226 bd->inbufBitCount++;
255 hufGroup = bd->groups+jj;
302 static int read_huffman_data(struct bunzip_data *bd, struct bwdata *bw)
318 bd->mtfSymbol[ii] = ii;
335 if (selector >= bd->nSelectors) return RETVAL_DATA_ERROR;
336 hufGroup = bd->groups + bd->selectors[selector++];
343 jj = get_bits(bd, ii);
350 kk = bd->inbufBitCount
351 ? (bd->inbufBits >> --(bd->inbufBitCount)) & 1 : get_bits(bd, 1);
387 if (dbufCount+hh > bd->dbufSize) return RETVAL_DATA_ERROR;
389 uc = bd->symToByte[bd->mtfSymbol[0]];
395 if (nextSym>bd->symTotal) break;
403 if (dbufCount>=bd->dbufSize) return RETVAL_DATA_ERROR;
405 uc = bd->mtfSymbol[ii];
408 while(ii--) bd->mtfSymbol[ii+1] = bd->mtfSymbol[ii];
409 bd->mtfSymbol[0] = uc;
410 uc = bd->symToByte[uc];
424 void flush_bunzip_outbuf(struct bunzip_data *bd, int out_fd)
426 if (bd->outbufPos) {
427 if (write(out_fd, bd->outbuf, bd->outbufPos) != bd->outbufPos)
429 bd->outbufPos = 0;
433 void burrows_wheeler_prep(struct bunzip_data *bd, struct bwdata *bw)
477 int read_bunzip_data(struct bunzip_data *bd)
479 int rc = read_block_header(bd, bd->bwdata);
480 if (!rc) rc=read_huffman_data(bd, bd->bwdata);
483 burrows_wheeler_prep(bd, bd->bwdata);
496 int write_bunzip_data(struct bunzip_data *bd, struct bwdata *bw, int out_fd, char *outbuf, int len)
507 int i = read_bunzip_data(bd);
525 if (len && bd->outbufPos >= len) goto dataus_interruptus;
547 if (bd->outbufPos == IOBUF_SIZE) flush_bunzip_outbuf(bd, out_fd);
548 bd->outbuf[bd->outbufPos++] = outbyte;
550 ^ bd->crc32Table[(bw->dataCRC >> 24) ^ outbyte];
557 bd->totalCRC = ((bd->totalCRC << 1) | (bd->totalCRC >> 31)) ^ bw->dataCRC;
561 bd->totalCRC = bw->headerCRC+1;
568 gotcount += bd->outbufPos;
569 memcpy(outbuf, bd->outbuf, len);
572 if ((len -= bd->outbufPos)<1) {
573 bd->outbufPos -= len;
574 if (bd->outbufPos) memmove(bd->outbuf, bd->outbuf+len, bd->outbufPos);
589 struct bunzip_data *bd;
597 bd = *bdp = xzalloc(i);
599 bd->inbuf = inbuf;
600 bd->inbufCount = len;
601 bd->in_fd = -1;
603 bd->inbuf = (char *)(bd+1);
604 bd->in_fd = src_fd;
607 crc_init(bd->crc32Table, 0);
610 for (i=0;i<3;i++) if (get_bits(bd,8)!="BZh"[i]) return RETVAL_NOT_BZIP_DATA;
614 i = get_bits(bd, 8);
616 bd->dbufSize = 100000*(i-'0')*THREADS;
618 bd->bwdata[i].dbuf = xmalloc(bd->dbufSize * sizeof(int));
627 struct bunzip_data *bd;
631 if (!(i = start_bunzip(&bd,src_fd, 0, 0))) {
632 i = write_bunzip_data(bd,bd->bwdata, dst_fd, 0, 0);
633 if (i==RETVAL_LAST_BLOCK && bd->bwdata[0].headerCRC==bd->totalCRC) i = 0;
635 flush_bunzip_outbuf(bd, dst_fd);
637 for (j=0; j<THREADS; j++) free(bd->bwdata[j].dbuf);
638 free(bd);