compress.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * This file is part of SCFS from UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. * Copyright (C) 2006, 2007 University of Szeged, Hungary
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Authors: Adrian Hunter
  21. * Artem Bityutskiy (Битюцкий Артём)
  22. * Zoltan Sogor
  23. */
  24. /*
  25. * This file provides a single place to access to compression and
  26. * decompression.
  27. */
  28. #include <linux/crypto.h>
  29. #include "scfs.h"
  30. /* Fake description object for the "none" compressor */
  31. static struct scfs_compressor none_compr = {
  32. .compr_type = SCFS_COMP_NONE,
  33. .name = "none",
  34. // .capi_name = "",
  35. };
  36. static DEFINE_MUTEX(lzo_mutex);
  37. static struct scfs_compressor lzo_compr = {
  38. .compr_type = SCFS_COMP_LZO,
  39. .comp_mutex = &lzo_mutex,
  40. .name = "lzo",
  41. .capi_name = "lzo",
  42. };
  43. static DEFINE_MUTEX(deflate_mutex);
  44. static DEFINE_MUTEX(inflate_mutex);
  45. static struct scfs_compressor zlib_compr = {
  46. .compr_type = SCFS_COMP_ZLIB,
  47. .comp_mutex = &deflate_mutex,
  48. .decomp_mutex = &inflate_mutex,
  49. .name = "zlib",
  50. .capi_name = "deflate",
  51. };
  52. /* All SCFS compressors */
  53. struct scfs_compressor *scfs_compressors[SCFS_COMP_TOTAL_TYPES];
  54. int scfs_compress_crypto(const void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
  55. int compr_type)
  56. {
  57. int err = 0;
  58. struct scfs_compressor *compr = scfs_compressors[compr_type];
  59. unsigned int tmp_len;
  60. if (compr_type == SCFS_COMP_NONE)
  61. goto no_compr;
  62. if (compr->comp_mutex)
  63. mutex_lock(compr->comp_mutex);
  64. tmp_len = (unsigned int)*out_len;
  65. err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf,
  66. &tmp_len);
  67. *out_len = (size_t)tmp_len;
  68. if (compr->comp_mutex)
  69. mutex_unlock(compr->comp_mutex);
  70. if (unlikely(err)) {
  71. SCFS_PRINT_ERROR("cannot compress %d bytes, compressor %s, "
  72. "error %d, leave data uncompressed",
  73. in_len, compr->name, err);
  74. err = 0;
  75. goto no_compr;
  76. }
  77. return err;
  78. no_compr:
  79. memcpy(out_buf, in_buf, in_len);
  80. *out_len = in_len;
  81. // *compr_type = SCFS_COMP_NONE;
  82. return err;
  83. }
  84. int scfs_decompress_crypto(const void *in_buf, size_t in_len, void *out_buf,
  85. size_t *out_len, int compr_type)
  86. {
  87. int err;
  88. struct scfs_compressor *compr;
  89. unsigned int tmp_len;
  90. if (unlikely(compr_type < 0 || compr_type >= SCFS_COMP_TOTAL_TYPES)) {
  91. SCFS_PRINT_ERROR("invalid compression type %d", compr_type);
  92. return -EINVAL;
  93. }
  94. compr = scfs_compressors[compr_type];
  95. if (unlikely(!compr->capi_name)) {
  96. SCFS_PRINT_ERROR("%s compression is not compiled in", compr->name);
  97. return -EINVAL;
  98. }
  99. if (compr_type == SCFS_COMP_NONE) {
  100. memcpy(out_buf, in_buf, in_len);
  101. *out_len = in_len;
  102. return 0;
  103. }
  104. if (compr->decomp_mutex)
  105. mutex_lock(compr->decomp_mutex);
  106. tmp_len = (unsigned int)*out_len;
  107. err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
  108. &tmp_len);
  109. *out_len = (size_t)tmp_len;
  110. if (compr->decomp_mutex)
  111. mutex_unlock(compr->decomp_mutex);
  112. if (err)
  113. SCFS_PRINT_ERROR("cannot decompress %d bytes, compressor %s, "
  114. "error %d", in_len, compr->name, err);
  115. return err;
  116. }
  117. /**
  118. * compr_init - initialize a compressor.
  119. * @compr: compressor description object
  120. *
  121. * This function initializes the requested compressor and returns zero in case
  122. * of success or a negative error code in case of failure.
  123. */
  124. static int compr_init(struct scfs_compressor *compr)
  125. {
  126. if (compr->capi_name) {
  127. compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
  128. if (IS_ERR(compr->cc)) {
  129. SCFS_PRINT_ERROR("cannot initialize compressor %s, error %ld",
  130. compr->name, PTR_ERR(compr->cc));
  131. return PTR_ERR(compr->cc);
  132. }
  133. }
  134. scfs_compressors[compr->compr_type] = compr;
  135. SCFS_PRINT("compr name %s(%d) got cc(%x)\n",
  136. compr->capi_name, compr->compr_type, compr->cc);
  137. return 0;
  138. }
  139. /**
  140. * compr_exit - de-initialize a compressor.
  141. * @compr: compressor description object
  142. */
  143. static void compr_exit(struct scfs_compressor *compr)
  144. {
  145. if (compr->capi_name)
  146. crypto_free_comp(compr->cc);
  147. return;
  148. }
  149. int scfs_compressors_init(void)
  150. {
  151. int err;
  152. err = compr_init(&lzo_compr);
  153. if (err)
  154. return err;
  155. err = compr_init(&zlib_compr);
  156. if (err)
  157. goto out_lzo;
  158. scfs_compressors[SCFS_COMP_NONE] = &none_compr;
  159. return 0;
  160. out_lzo:
  161. compr_exit(&lzo_compr);
  162. return err;
  163. }
  164. void scfs_compressors_exit(void)
  165. {
  166. compr_exit(&lzo_compr);
  167. compr_exit(&zlib_compr);
  168. }