plan9port

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

inflatezlibblock.c (1057B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <flate.h>
      4 #include "zlib.h"
      5 
      6 typedef struct Block	Block;
      7 
      8 struct Block
      9 {
     10 	uchar	*pos;
     11 	uchar	*limit;
     12 };
     13 
     14 static int
     15 blgetc(void *vb)
     16 {
     17 	Block *b;
     18 
     19 	b = vb;
     20 	if(b->pos >= b->limit)
     21 		return -1;
     22 	return *b->pos++;
     23 }
     24 
     25 static int
     26 blwrite(void *vb, void *buf, int n)
     27 {
     28 	Block *b;
     29 
     30 	b = vb;
     31 
     32 	if(n > b->limit - b->pos)
     33 		n = b->limit - b->pos;
     34 	memmove(b->pos, buf, n);
     35 	b->pos += n;
     36 	return n;
     37 }
     38 
     39 int
     40 inflatezlibblock(uchar *dst, int dsize, uchar *src, int ssize)
     41 {
     42 	Block bd, bs;
     43 	int ok;
     44 
     45 	if(ssize < 6)
     46 		return FlateInputFail;
     47 
     48 	if(((src[0] << 8) | src[1]) % 31)
     49 		return FlateCorrupted;
     50 	if((src[0] & ZlibMeth) != ZlibDeflate
     51 	|| (src[0] & ZlibCInfo) > ZlibWin32k)
     52 		return FlateCorrupted;
     53 
     54 	bs.pos = src + 2;
     55 	bs.limit = src + ssize - 4;
     56 
     57 	bd.pos = dst;
     58 	bd.limit = dst + dsize;
     59 
     60 	ok = inflate(&bd, blwrite, &bs, blgetc);
     61 	if(ok != FlateOk)
     62 		return ok;
     63 
     64 	if(adler32(1, dst, bd.pos - dst) != ((bs.pos[0] << 24) | (bs.pos[1] << 16) | (bs.pos[2] << 8) | bs.pos[3]))
     65 		return FlateCorrupted;
     66 
     67 	return bd.pos - dst;
     68 }