compr_zlib.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
  7. *
  8. * The original JFFS, from which the design for JFFS2 was derived,
  9. * was designed and implemented by Axis Communications AB.
  10. *
  11. * The contents of this file are subject to the Red Hat eCos Public
  12. * License Version 1.1 (the "Licence"); you may not use this file
  13. * except in compliance with the Licence. You may obtain a copy of
  14. * the Licence at http://www.redhat.com/
  15. *
  16. * Software distributed under the Licence is distributed on an "AS IS"
  17. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
  18. * See the Licence for the specific language governing rights and
  19. * limitations under the Licence.
  20. *
  21. * The Original Code is JFFS2 - Journalling Flash File System, version 2
  22. *
  23. * Alternatively, the contents of this file may be used under the
  24. * terms of the GNU General Public License version 2 (the "GPL"), in
  25. * which case the provisions of the GPL are applicable instead of the
  26. * above. If you wish to allow the use of your version of this file
  27. * only under the terms of the GPL and not to allow others to use your
  28. * version of this file under the RHEPL, indicate your decision by
  29. * deleting the provisions above and replace them with the notice and
  30. * other provisions required by the GPL. If you do not delete the
  31. * provisions above, a recipient may use your version of this file
  32. * under either the RHEPL or the GPL.
  33. */
  34. #define PROGRAM_NAME "compr_zlib"
  35. #include <stdint.h>
  36. #define crc32 __zlib_crc32
  37. #include <zlib.h>
  38. #undef crc32
  39. #include <stdio.h>
  40. #include <asm/types.h>
  41. #include <linux/jffs2.h>
  42. #include "common.h"
  43. #include "compr.h"
  44. /* Plan: call deflate() with avail_in == *sourcelen,
  45. avail_out = *dstlen - 12 and flush == Z_FINISH.
  46. If it doesn't manage to finish, call it again with
  47. avail_in == 0 and avail_out set to the remaining 12
  48. bytes for it to clean up.
  49. Q: Is 12 bytes sufficient?
  50. */
  51. #define STREAM_END_SPACE 12
  52. static int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
  53. uint32_t *sourcelen, uint32_t *dstlen)
  54. {
  55. z_stream strm;
  56. int ret;
  57. if (*dstlen <= STREAM_END_SPACE)
  58. return -1;
  59. strm.zalloc = (void *)0;
  60. strm.zfree = (void *)0;
  61. if (Z_OK != deflateInit(&strm, 3)) {
  62. return -1;
  63. }
  64. strm.next_in = data_in;
  65. strm.total_in = 0;
  66. strm.next_out = cpage_out;
  67. strm.total_out = 0;
  68. while (strm.total_out < *dstlen - STREAM_END_SPACE && strm.total_in < *sourcelen) {
  69. strm.avail_out = *dstlen - (strm.total_out + STREAM_END_SPACE);
  70. strm.avail_in = min((unsigned)(*sourcelen-strm.total_in), strm.avail_out);
  71. ret = deflate(&strm, Z_PARTIAL_FLUSH);
  72. if (ret != Z_OK) {
  73. deflateEnd(&strm);
  74. return -1;
  75. }
  76. }
  77. strm.avail_out += STREAM_END_SPACE;
  78. strm.avail_in = 0;
  79. ret = deflate(&strm, Z_FINISH);
  80. if (ret != Z_STREAM_END) {
  81. deflateEnd(&strm);
  82. return -1;
  83. }
  84. deflateEnd(&strm);
  85. if (strm.total_out >= strm.total_in)
  86. return -1;
  87. *dstlen = strm.total_out;
  88. *sourcelen = strm.total_in;
  89. return 0;
  90. }
  91. static int jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
  92. uint32_t srclen, uint32_t destlen)
  93. {
  94. z_stream strm;
  95. int ret;
  96. strm.zalloc = (void *)0;
  97. strm.zfree = (void *)0;
  98. if (Z_OK != inflateInit(&strm)) {
  99. return 1;
  100. }
  101. strm.next_in = data_in;
  102. strm.avail_in = srclen;
  103. strm.total_in = 0;
  104. strm.next_out = cpage_out;
  105. strm.avail_out = destlen;
  106. strm.total_out = 0;
  107. while((ret = inflate(&strm, Z_FINISH)) == Z_OK)
  108. ;
  109. inflateEnd(&strm);
  110. return 0;
  111. }
  112. static struct jffs2_compressor jffs2_zlib_comp = {
  113. .priority = JFFS2_ZLIB_PRIORITY,
  114. .name = "zlib",
  115. .disabled = 0,
  116. .compr = JFFS2_COMPR_ZLIB,
  117. .compress = &jffs2_zlib_compress,
  118. .decompress = &jffs2_zlib_decompress,
  119. };
  120. int jffs2_zlib_init(void)
  121. {
  122. return jffs2_register_compressor(&jffs2_zlib_comp);
  123. }
  124. void jffs2_zlib_exit(void)
  125. {
  126. jffs2_unregister_compressor(&jffs2_zlib_comp);
  127. }