c_zlib.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* $MirOS: contrib/hosted/fwcf/c_zlib.c,v 1.4 2006/09/23 23:46:35 tg Exp $ */
  2. /*-
  3. * Copyright (c) 2006
  4. * Thorsten Glaser <tg@mirbsd.de>
  5. *
  6. * Licensee is hereby permitted to deal in this work without restric-
  7. * tion, including unlimited rights to use, publicly perform, modify,
  8. * merge, distribute, sell, give away or sublicence, provided all co-
  9. * pyright notices above, these terms and the disclaimer are retained
  10. * in all redistributions or reproduced in accompanying documentation
  11. * or other materials provided with binary redistributions.
  12. *
  13. * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
  14. * express, or implied, to the maximum extent permitted by applicable
  15. * law, without malicious intent or gross negligence; in no event may
  16. * licensor, an author or contributor be held liable for any indirect
  17. * or other damage, or direct damage except proven a consequence of a
  18. * direct error of said person and intended use of this work, loss or
  19. * other issues arising in any way out of its use, even if advised of
  20. * the possibility of such damage or existence of a defect.
  21. */
  22. #include <sys/param.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <zlib.h>
  26. #include "defs.h"
  27. #include "compress.h"
  28. __RCSID("$MirOS: contrib/hosted/fwcf/c_zlib.c,v 1.4 2006/09/23 23:46:35 tg Exp $");
  29. static void c_zlib_load(void) __attribute__((constructor));
  30. static int c_init(void);
  31. static int c_compress(char **, char *, size_t)
  32. __attribute__((bounded (string, 2, 3)));
  33. static int c_decompress(char *, size_t, char *, size_t)
  34. __attribute__((bounded (string, 1, 2)))
  35. __attribute__((bounded (string, 3, 4)));
  36. static fwcf_compressor c_zlib = {
  37. c_init, /* init */
  38. c_compress, /* compress */
  39. c_decompress, /* decompress */
  40. "zlib", /* name */
  41. 0x01 /* code */
  42. };
  43. static void
  44. c_zlib_load(void)
  45. {
  46. if (compress_register(&c_zlib))
  47. fputs("warning: cannot register compressor 'zlib'!\n", stderr);
  48. }
  49. static int
  50. c_init(void)
  51. {
  52. return (0);
  53. }
  54. static int
  55. c_compress(char **dst, char *src, size_t len)
  56. {
  57. uLongf dstlen;
  58. if (dst == NULL)
  59. return (-1);
  60. dstlen = compressBound(len);
  61. if ((*dst = malloc(dstlen)) == NULL)
  62. return (-1);
  63. return ((compress2((uint8_t *)*dst, &dstlen, (uint8_t *)src, len,
  64. 9) == Z_OK) ? (int)dstlen : -1);
  65. }
  66. static int
  67. c_decompress(char *dst, size_t dstlen, char *src, size_t srclen)
  68. {
  69. uLongf len = dstlen;
  70. return ((uncompress((uint8_t *)dst, &len, (uint8_t *)src,
  71. srclen) == Z_OK) ? (int)len : -1);
  72. }