deflateblock.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. blread(void *vb, void *buf, int n)
  20. {
  21. Block *b;
  22. b = vb;
  23. if(n > b->limit - b->pos)
  24. n = b->limit - b->pos;
  25. memmove(buf, b->pos, n);
  26. b->pos += n;
  27. return n;
  28. }
  29. static int
  30. blwrite(void *vb, void *buf, int n)
  31. {
  32. Block *b;
  33. b = vb;
  34. if(n > b->limit - b->pos)
  35. n = b->limit - b->pos;
  36. memmove(b->pos, buf, n);
  37. b->pos += n;
  38. return n;
  39. }
  40. int
  41. deflateblock(uint8_t *dst, int dsize, uint8_t *src, int ssize, int level,
  42. int debug)
  43. {
  44. Block bd, bs;
  45. int ok;
  46. bs.pos = src;
  47. bs.limit = src + ssize;
  48. bd.pos = dst;
  49. bd.limit = dst + dsize;
  50. ok = deflate(&bd, blwrite, &bs, blread, level, debug);
  51. if(ok != FlateOk)
  52. return ok;
  53. return bd.pos - dst;
  54. }