inflateblock.c 686 B

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