inflateblock.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <flate.h>
  12. typedef struct Block Block;
  13. struct Block
  14. {
  15. uint8_t *pos;
  16. uint8_t *limit;
  17. };
  18. static int
  19. blgetc(void *vb)
  20. {
  21. Block *b;
  22. b = vb;
  23. if(b->pos >= b->limit)
  24. return -1;
  25. return *b->pos++;
  26. }
  27. static int
  28. blwrite(void *vb, void *buf, int n)
  29. {
  30. Block *b;
  31. b = vb;
  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. int
  39. inflateblock(uint8_t *dst, int dsize, uint8_t *src, int ssize)
  40. {
  41. Block bd, bs;
  42. int ok;
  43. bs.pos = src;
  44. bs.limit = src + ssize;
  45. bd.pos = dst;
  46. bd.limit = dst + dsize;
  47. ok = inflate(&bd, blwrite, &bs, blgetc);
  48. if(ok != FlateOk)
  49. return ok;
  50. return bd.pos - dst;
  51. }