compr_lzo.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * JFFS2 LZO Compression Interface.
  3. *
  4. * Copyright (C) 2007 Nokia Corporation. All rights reserved.
  5. *
  6. * Author: Richard Purdie <rpurdie@openedhand.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * 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
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #ifndef WITHOUT_LZO
  27. #include <asm/types.h>
  28. #include <linux/jffs2.h>
  29. #include <lzo/lzo1x.h>
  30. #include "compr.h"
  31. extern int page_size;
  32. static void *lzo_mem;
  33. static void *lzo_compress_buf;
  34. /*
  35. * Note about LZO compression.
  36. *
  37. * We want to use the _999_ compression routine which gives better compression
  38. * rates at the expense of time. Decompression time is unaffected. We might as
  39. * well use the standard lzo library routines for this but they will overflow
  40. * the destination buffer since they don't check the destination size.
  41. *
  42. * We therefore compress to a temporary buffer and copy if it will fit.
  43. *
  44. */
  45. static int jffs2_lzo_cmpr(unsigned char *data_in, unsigned char *cpage_out,
  46. uint32_t *sourcelen, uint32_t *dstlen)
  47. {
  48. lzo_uint compress_size;
  49. int ret;
  50. ret = lzo1x_999_compress(data_in, *sourcelen, lzo_compress_buf, &compress_size, lzo_mem);
  51. if (ret != LZO_E_OK)
  52. return -1;
  53. if (compress_size > *dstlen)
  54. return -1;
  55. memcpy(cpage_out, lzo_compress_buf, compress_size);
  56. *dstlen = compress_size;
  57. return 0;
  58. }
  59. static int jffs2_lzo_decompress(unsigned char *data_in, unsigned char *cpage_out,
  60. uint32_t srclen, uint32_t destlen)
  61. {
  62. int ret;
  63. lzo_uint dl;
  64. dl = destlen;
  65. ret = lzo1x_decompress_safe(data_in,srclen,cpage_out,&dl,NULL);
  66. if (ret != LZO_E_OK || dl != destlen)
  67. return -1;
  68. return 0;
  69. }
  70. static struct jffs2_compressor jffs2_lzo_comp = {
  71. .priority = JFFS2_LZO_PRIORITY,
  72. .name = "lzo",
  73. .compr = JFFS2_COMPR_LZO,
  74. .compress = &jffs2_lzo_cmpr,
  75. .decompress = &jffs2_lzo_decompress,
  76. .disabled = 1,
  77. };
  78. int jffs2_lzo_init(void)
  79. {
  80. int ret;
  81. lzo_mem = malloc(LZO1X_999_MEM_COMPRESS);
  82. if (!lzo_mem)
  83. return -1;
  84. /* Worse case LZO compression size from their FAQ */
  85. lzo_compress_buf = malloc(page_size + (page_size / 16) + 64 + 3);
  86. if (!lzo_compress_buf) {
  87. free(lzo_mem);
  88. return -1;
  89. }
  90. ret = jffs2_register_compressor(&jffs2_lzo_comp);
  91. if (ret < 0) {
  92. free(lzo_compress_buf);
  93. free(lzo_mem);
  94. }
  95. return ret;
  96. }
  97. void jffs2_lzo_exit(void)
  98. {
  99. jffs2_unregister_compressor(&jffs2_lzo_comp);
  100. free(lzo_compress_buf);
  101. free(lzo_mem);
  102. }
  103. #else
  104. int jffs2_lzo_init(void)
  105. {
  106. return 0;
  107. }
  108. void jffs2_lzo_exit(void)
  109. {
  110. }
  111. #endif