plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

inflateblock.c (686B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <flate.h>
      4 
      5 typedef struct Block	Block;
      6 
      7 struct Block
      8 {
      9 	uchar	*pos;
     10 	uchar	*limit;
     11 };
     12 
     13 static int
     14 blgetc(void *vb)
     15 {
     16 	Block *b;
     17 
     18 	b = vb;
     19 	if(b->pos >= b->limit)
     20 		return -1;
     21 	return *b->pos++;
     22 }
     23 
     24 static int
     25 blwrite(void *vb, void *buf, int n)
     26 {
     27 	Block *b;
     28 
     29 	b = vb;
     30 
     31 	if(n > b->limit - b->pos)
     32 		n = b->limit - b->pos;
     33 	memmove(b->pos, buf, n);
     34 	b->pos += n;
     35 	return n;
     36 }
     37 
     38 int
     39 inflateblock(uchar *dst, int dsize, uchar *src, int ssize)
     40 {
     41 	Block bd, bs;
     42 	int ok;
     43 
     44 	bs.pos = src;
     45 	bs.limit = src + ssize;
     46 
     47 	bd.pos = dst;
     48 	bd.limit = dst + dsize;
     49 
     50 	ok = inflate(&bd, blwrite, &bs, blgetc);
     51 	if(ok != FlateOk)
     52 		return ok;
     53 	return bd.pos - dst;
     54 }