• Home
  • Raw
  • Download

Lines Matching refs:bb

42   struct bitbuf *bb = xzalloc(sizeof(struct bitbuf)+size);  in bitbuf_init()  local
44 bb->max = size; in bitbuf_init()
45 bb->fd = fd; in bitbuf_init()
47 return bb; in bitbuf_init()
53 int bitbuf_skip(struct bitbuf *bb, int bits) in bitbuf_skip() argument
55 int pos = bb->bitpos + bits + (bits<0), len; in bitbuf_skip()
57 while (pos >= (len = bb->len<<3)) { in bitbuf_skip()
59 if (1 > (bb->len = read(bb->fd, bb->buf, bb->max))) { in bitbuf_skip()
60 if (!bb->len && !bits) break; in bitbuf_skip()
64 bb->bitpos = pos; in bitbuf_skip()
70 static inline int bitbuf_bit(struct bitbuf *bb) in bitbuf_bit() argument
72 int bufpos = bb->bitpos>>3; in bitbuf_bit()
74 if (bufpos == bb->len) { in bitbuf_bit()
75 bitbuf_skip(bb, -1); in bitbuf_bit()
79 return (bb->buf[bufpos]>>(bb->bitpos++&7))&1; in bitbuf_bit()
83 unsigned bitbuf_get(struct bitbuf *bb, int bits) in bitbuf_get() argument
88 int click = bb->bitpos >> 3, blow, blen; in bitbuf_get()
91 if (click == bb->len) { in bitbuf_get()
92 bitbuf_skip(bb, -1); in bitbuf_get()
97 blow = bb->bitpos & 7; in bitbuf_get()
100 result |= ((bb->buf[click] >> blow) & ((1<<blen)-1)) << offset; in bitbuf_get()
103 bb->bitpos += blen; in bitbuf_get()
109 void bitbuf_flush(struct bitbuf *bb) in bitbuf_flush() argument
111 if (!bb->bitpos) return; in bitbuf_flush()
113 xwrite(bb->fd, bb->buf, (bb->bitpos+7)>>3); in bitbuf_flush()
114 memset(bb->buf, 0, bb->max); in bitbuf_flush()
115 bb->bitpos = 0; in bitbuf_flush()
118 void bitbuf_put(struct bitbuf *bb, int data, int len) in bitbuf_put() argument
121 int click = bb->bitpos >> 3, blow, blen; in bitbuf_put()
124 if (click == bb->max) { in bitbuf_put()
125 bitbuf_flush(bb); in bitbuf_put()
128 blow = bb->bitpos & 7; in bitbuf_put()
131 bb->buf[click] |= data << blow; in bitbuf_put()
132 bb->bitpos += blen; in bitbuf_put()
185 static unsigned huff_and_puff(struct bitbuf *bb, struct huff *huff) in huff_and_puff() argument
192 offset = (offset << 1) | bitbuf_bit(bb); in huff_and_puff()
202 static void inflate(struct deflate *dd, struct bitbuf *bb) in inflate() argument
210 final = bitbuf_get(bb, 1); in inflate()
211 type = bitbuf_get(bb, 2); in inflate()
220 bitbuf_skip(bb, (8-bb->bitpos)&7); in inflate()
221 len = bitbuf_get(bb, 16); in inflate()
222 nlen = bitbuf_get(bb, 16); in inflate()
227 int pos = bb->bitpos >> 3, bblen = bb->len - pos; in inflate()
228 char *p = bb->buf+pos; in inflate()
234 bitbuf_skip(bb, bblen << 3); in inflate()
250 litlen = bitbuf_get(bb, 5)+257; // max 288 in inflate()
251 distlen = bitbuf_get(bb, 5)+1; // max 32 in inflate()
252 hufflen = bitbuf_get(bb, 4)+4; // max 19 in inflate()
259 for (i=0; i<hufflen; i++) bits[hufflen_order[i]] = bitbuf_get(bb, 3); in inflate()
264 int sym = huff_and_puff(bb, h2); in inflate()
272 len = bitbuf_get(bb, sym-14+len+(len>>1)) + 3 + (len<<2); in inflate()
290 int sym = huff_and_puff(bb, lithuff); in inflate()
300 len = dd->lenbase[sym] + bitbuf_get(bb, dd->lenbits[sym]); in inflate()
301 sym = huff_and_puff(bb, disthuff); in inflate()
302 dist = dd->distbase[sym] + bitbuf_get(bb, dd->distbits[sym]); in inflate()
324 static void deflate(struct deflate *dd, struct bitbuf *bb) in deflate() argument
340 bitbuf_put(bb, final, 1); in deflate()
341 bitbuf_put(bb, 0, 1); in deflate()
343 bitbuf_put(bb, 0, (8-bb->bitpos)&7); in deflate()
344 bitbuf_put(bb, len, 16); in deflate()
345 bitbuf_put(bb, 0xffff & ~len, 16); in deflate()
351 bitbuf_put(bb, data[pos], 8); in deflate()
357 bitbuf_flush(bb); in deflate()
405 static int is_gzip(struct bitbuf *bb) in is_gzip() argument
410 if (bitbuf_get(bb, 24) != 0x088b1f || (flags = bitbuf_get(bb, 8)) > 31) in is_gzip()
412 bitbuf_skip(bb, 6*8); in is_gzip()
415 if (flags & 4) bitbuf_skip(bb, 16); in is_gzip()
416 if (flags & 8) while (bitbuf_get(bb, 8)); in is_gzip()
417 if (flags & 16) while (bitbuf_get(bb, 8)); in is_gzip()
418 if (flags & 2) bitbuf_skip(bb, 16); in is_gzip()
449 struct bitbuf *bb = bitbuf_init(outfd, 4096); in gzip_fd() local
459 xwrite(bb->fd, "\x1f\x8b\x08\0\0\0\0\0\x02\xff", 10); in gzip_fd()
465 deflate(dd, bb); in gzip_fd()
469 bitbuf_put(bb, 0, (8-bb->bitpos)&7); in gzip_fd()
470 bitbuf_put(bb, ~dd->crc, 32); in gzip_fd()
471 bitbuf_put(bb, dd->len, 32); in gzip_fd()
474 bitbuf_flush(bb); in gzip_fd()
475 free(bb); in gzip_fd()
483 struct bitbuf *bb = bitbuf_init(infd, 4096); in gunzip_fd() local
493 if (!is_gzip(bb)) error_exit("not gzip"); in gunzip_fd()
495 inflate(dd, bb); in gunzip_fd()
498 bitbuf_skip(bb, (8-bb->bitpos)&7); in gunzip_fd()
499 if (~dd->crc != bitbuf_get(bb, 32) || dd->len != bitbuf_get(bb, 32)) in gunzip_fd()
503 bitbuf_skip(bb, (8-bb->bitpos)&7); in gunzip_fd()
505 } while (bitbuf_skip(bb, 0)); in gunzip_fd()
506 free(bb); in gunzip_fd()