Lines Matching refs:bd
110 static unsigned int get_bits(struct bunzip_data *bd, char bits_wanted) in get_bits() argument
116 while (bd->inbufBitCount < bits_wanted) { in get_bits()
119 if (bd->inbufPos == bd->inbufCount) { in get_bits()
120 if (0 >= (bd->inbufCount = read(bd->in_fd, bd->inbuf, IOBUF_SIZE))) in get_bits()
122 bd->inbufPos = 0; in get_bits()
126 if (bd->inbufBitCount>=24) { in get_bits()
127 bits = bd->inbufBits&((1<<bd->inbufBitCount)-1); in get_bits()
128 bits_wanted -= bd->inbufBitCount; in get_bits()
130 bd->inbufBitCount = 0; in get_bits()
134 bd->inbufBits = (bd->inbufBits<<8) | bd->inbuf[bd->inbufPos++]; in get_bits()
135 bd->inbufBitCount += 8; in get_bits()
139 bd->inbufBitCount -= bits_wanted; in get_bits()
140 bits |= (bd->inbufBits>>bd->inbufBitCount) & ((1<<bits_wanted)-1); in get_bits()
161 static int read_block_header(struct bunzip_data *bd, struct bwdata *bw) in read_block_header() argument
168 ii = get_bits(bd, 24); in read_block_header()
169 jj = get_bits(bd, 24); in read_block_header()
170 bw->headerCRC = get_bits(bd,32); in read_block_header()
179 if (get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT; in read_block_header()
180 if ((bw->origPtr = get_bits(bd,24)) > bd->dbufSize) return RETVAL_DATA_ERROR; in read_block_header()
187 hh = get_bits(bd, 16); in read_block_header()
188 bd->symTotal = 0; in read_block_header()
191 kk = get_bits(bd, 16); in read_block_header()
194 bd->symToByte[bd->symTotal++] = (16 * ii) + jj; in read_block_header()
199 bd->groupCount = get_bits(bd,3); in read_block_header()
200 if (bd->groupCount<2 || bd->groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR; in read_block_header()
209 if (!(bd->nSelectors = get_bits(bd, 15))) return RETVAL_DATA_ERROR; in read_block_header()
210 for (ii=0; ii<bd->groupCount; ii++) bd->mtfSymbol[ii] = ii; in read_block_header()
211 for (ii=0; ii<bd->nSelectors; ii++) { in read_block_header()
214 for(jj=0;get_bits(bd,1);jj++) in read_block_header()
215 if (jj>=bd->groupCount) return RETVAL_DATA_ERROR; in read_block_header()
218 uc = bd->mtfSymbol[jj]; in read_block_header()
219 memmove(bd->mtfSymbol+1, bd->mtfSymbol, jj); in read_block_header()
220 bd->mtfSymbol[0] = bd->selectors[ii] = uc; in read_block_header()
225 symCount = bd->symTotal+2; in read_block_header()
226 for (jj=0; jj<bd->groupCount; jj++) { in read_block_header()
232 hh = get_bits(bd, 5); in read_block_header()
240 kk = get_bits(bd, 2); in read_block_header()
243 bd->inbufBitCount++; in read_block_header()
272 hufGroup = bd->groups+jj; in read_block_header()
319 static int read_huffman_data(struct bunzip_data *bd, struct bwdata *bw) in read_huffman_data() argument
335 bd->mtfSymbol[ii] = ii; in read_huffman_data()
352 if (selector >= bd->nSelectors) return RETVAL_DATA_ERROR; in read_huffman_data()
353 hufGroup = bd->groups + bd->selectors[selector++]; in read_huffman_data()
360 jj = get_bits(bd, ii); in read_huffman_data()
367 kk = bd->inbufBitCount in read_huffman_data()
368 ? (bd->inbufBits >> --(bd->inbufBitCount)) & 1 : get_bits(bd, 1); in read_huffman_data()
405 if (hh>bd->dbufSize || dbufCount+hh>bd->dbufSize) in read_huffman_data()
408 uc = bd->symToByte[bd->mtfSymbol[0]]; in read_huffman_data()
414 if (nextSym>bd->symTotal) break; in read_huffman_data()
422 if (dbufCount>=bd->dbufSize) return RETVAL_DATA_ERROR; in read_huffman_data()
424 uc = bd->mtfSymbol[ii]; in read_huffman_data()
427 while(ii--) bd->mtfSymbol[ii+1] = bd->mtfSymbol[ii]; in read_huffman_data()
428 bd->mtfSymbol[0] = uc; in read_huffman_data()
429 uc = bd->symToByte[uc]; in read_huffman_data()
443 static void flush_bunzip_outbuf(struct bunzip_data *bd, int out_fd) in flush_bunzip_outbuf() argument
445 if (bd->outbufPos) { in flush_bunzip_outbuf()
446 if (write(out_fd, bd->outbuf, bd->outbufPos) != bd->outbufPos) in flush_bunzip_outbuf()
448 bd->outbufPos = 0; in flush_bunzip_outbuf()
452 static void burrows_wheeler_prep(struct bunzip_data *bd, struct bwdata *bw) in burrows_wheeler_prep() argument
493 static int read_bunzip_data(struct bunzip_data *bd) in read_bunzip_data() argument
495 int rc = read_block_header(bd, bd->bwdata); in read_bunzip_data()
496 if (!rc) rc=read_huffman_data(bd, bd->bwdata); in read_bunzip_data()
499 burrows_wheeler_prep(bd, bd->bwdata); in read_bunzip_data()
512 static int write_bunzip_data(struct bunzip_data *bd, struct bwdata *bw, in write_bunzip_data() argument
524 int i = read_bunzip_data(bd); in write_bunzip_data()
542 if (len && bd->outbufPos >= len) goto dataus_interruptus; in write_bunzip_data()
564 if (bd->outbufPos == IOBUF_SIZE) flush_bunzip_outbuf(bd, out_fd); in write_bunzip_data()
565 bd->outbuf[bd->outbufPos++] = outbyte; in write_bunzip_data()
567 ^ bd->crc32Table[(bw->dataCRC >> 24) ^ outbyte]; in write_bunzip_data()
574 bd->totalCRC = ((bd->totalCRC << 1) | (bd->totalCRC >> 31)) ^ bw->dataCRC; in write_bunzip_data()
578 bd->totalCRC = bw->headerCRC+1; in write_bunzip_data()
585 gotcount += bd->outbufPos; in write_bunzip_data()
586 memcpy(outbuf, bd->outbuf, len); in write_bunzip_data()
589 if ((len -= bd->outbufPos)<1) { in write_bunzip_data()
590 bd->outbufPos -= len; in write_bunzip_data()
591 if (bd->outbufPos) memmove(bd->outbuf, bd->outbuf+len, bd->outbufPos); in write_bunzip_data()
607 struct bunzip_data *bd; in start_bunzip() local
615 bd = *bdp = xzalloc(i); in start_bunzip()
617 bd->inbuf = inbuf; in start_bunzip()
618 bd->inbufCount = len; in start_bunzip()
619 bd->in_fd = -1; in start_bunzip()
621 bd->inbuf = (char *)(bd+1); in start_bunzip()
622 bd->in_fd = src_fd; in start_bunzip()
625 crc_init(bd->crc32Table, 0); in start_bunzip()
628 for (i=0;i<3;i++) if (get_bits(bd,8)!="BZh"[i]) return RETVAL_NOT_BZIP_DATA; in start_bunzip()
632 i = get_bits(bd, 8); in start_bunzip()
634 bd->dbufSize = 100000*(i-'0')*THREADS; in start_bunzip()
636 bd->bwdata[i].dbuf = xmalloc(bd->dbufSize * sizeof(int)); in start_bunzip()
645 struct bunzip_data *bd; in bunzipStream() local
649 if (!(i = start_bunzip(&bd,src_fd, 0, 0))) { in bunzipStream()
650 i = write_bunzip_data(bd,bd->bwdata, dst_fd, 0, 0); in bunzipStream()
652 if (bd->bwdata[0].headerCRC==bd->totalCRC) i = 0; in bunzipStream()
656 flush_bunzip_outbuf(bd, dst_fd); in bunzipStream()
658 for (j=0; j<THREADS; j++) free(bd->bwdata[j].dbuf); in bunzipStream()
659 free(bd); in bunzipStream()