c_lzo1x1.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* $MirOS: contrib/hosted/fwcf/c_lzo1x1.c,v 1.5 2007/03/13 18:31:07 tg Exp $ */
  2. /*-
  3. * MiniLZO (LZO1X-1) compression plug-in for FWCF
  4. * Copyright (c) 2006
  5. * Thorsten Glaser <tg@mirbsd.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 1, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
  22. * the utmost extent permitted by applicable law, neither express nor
  23. * implied; without malicious intent or gross negligence. In no event
  24. * may a licensor, author or contributor be held liable for indirect,
  25. * direct, other damage, loss, or other issues arising in any way out
  26. * of dealing in the work, even if advised of the possibility of such
  27. * damage or existence of a defect, except proven that it results out
  28. * of said person's immediate fault when using the work as intended.
  29. */
  30. #include <sys/param.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "defs.h"
  35. #include "compress.h"
  36. __RCSID("$MirOS: contrib/hosted/fwcf/c_lzo1x1.c,v 1.5 2007/03/13 18:31:07 tg Exp $");
  37. #define C_LZO1X1_T1(a,b) c_lzo1x1_ ## a ## _ ## b
  38. #define C_LZO1X1_T2(a,b) C_LZO1X1_T1(a,b)
  39. #define LZO_COMPILE_TIME_ASSERT_HEADER(e) \
  40. extern int C_LZO1X1_T2(__LINE__,__lzo_cta)[1-2*!(e)];
  41. #define MINILZO_CFG_SKIP_LZO1X_DECOMPRESS 1
  42. #define MINILZO_CFG_SKIP_LZO_PTR 1
  43. #define MINILZO_CFG_SKIP_LZO_STRING 1
  44. #include "minilzo.c"
  45. static void c_lzo1x1_load(void) __attribute__((constructor));
  46. static int c_init(void);
  47. static int c_compress(char **, char *, size_t)
  48. __attribute__((bounded (string, 2, 3)));
  49. static int c_decompress(char *, size_t, char *, size_t)
  50. __attribute__((bounded (string, 1, 2)))
  51. __attribute__((bounded (string, 3, 4)));
  52. static fwcf_compressor c_lzo1x1 = {
  53. c_init, /* init */
  54. c_compress, /* compress */
  55. c_decompress, /* decompress */
  56. "lzo1x1", /* name */
  57. 0x10 /* code */
  58. };
  59. /* Work-memory needed for compression. Allocate memory in units
  60. * of `lzo_align_t' (instead of `char') to make sure it is properly aligned.
  61. */
  62. #define HEAP_ALLOC(var,size) \
  63. lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
  64. static HEAP_ALLOC(wrkmem,LZO1X_1_MEM_COMPRESS);
  65. static void
  66. c_lzo1x1_load(void)
  67. {
  68. if (compress_register(&c_lzo1x1))
  69. fputs("warning: cannot register compressor 'lzo1x1'!\n", stderr);
  70. }
  71. static int
  72. c_init(void)
  73. {
  74. return ((lzo_init() == LZO_E_OK) ? 0 : 1);
  75. }
  76. static int
  77. c_compress(char **dst, char *src, size_t len)
  78. {
  79. lzo_bytep ldst;
  80. lzo_uint ldlen = len + (len / 16) + 64 + 3;
  81. if ((ldst = malloc(ldlen)) == NULL)
  82. return (-1);
  83. #ifdef DEBUG
  84. fprintf(stderr, "LZO1X-1 compress %lu bytes -> (%lu)",
  85. (u_long)len, (u_long)ldlen);
  86. #endif
  87. lzo1x_1_compress((lzo_bytep)src, len, ldst, &ldlen, wrkmem);
  88. #ifdef DEBUG
  89. fprintf(stderr, " %lu bytes\n", (u_long)ldlen);
  90. #endif
  91. *dst = (char *)ldst;
  92. return (ldlen);
  93. }
  94. static int
  95. c_decompress(char *dst, size_t dstlen, char *src, size_t srclen)
  96. {
  97. lzo_uint ldlen = dstlen;
  98. int i;
  99. #ifdef DEBUG
  100. fprintf(stderr, "LZO1X decompress %lu -> %lu bytes\n",
  101. (u_long)srclen, (u_long)dstlen);
  102. #endif
  103. if (((i = lzo1x_decompress_safe((lzo_bytep)src, srclen, (lzo_bytep)dst,
  104. &ldlen, wrkmem)) == LZO_E_OK) || (i == LZO_E_INPUT_NOT_CONSUMED))
  105. return (ldlen);
  106. #ifdef DEBUG
  107. fprintf(stderr, "LZO1X decompress error code %d\n", i);
  108. #endif
  109. return (-1);
  110. }