compression.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * BTRFS filesystem implementation for U-Boot
  3. *
  4. * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include "btrfs.h"
  9. #include <linux/lzo.h>
  10. #include <u-boot/zlib.h>
  11. static u32 decompress_lzo(const u8 *cbuf, u32 clen, u8 *dbuf, u32 dlen)
  12. {
  13. u32 tot_len, in_len, res;
  14. size_t out_len;
  15. int ret;
  16. if (clen < 4)
  17. return -1;
  18. tot_len = le32_to_cpu(*(u32 *) cbuf);
  19. cbuf += 4;
  20. clen -= 4;
  21. tot_len -= 4;
  22. if (tot_len == 0 && dlen)
  23. return -1;
  24. if (tot_len < 4)
  25. return -1;
  26. res = 0;
  27. while (tot_len > 4) {
  28. in_len = le32_to_cpu(*(u32 *) cbuf);
  29. cbuf += 4;
  30. clen -= 4;
  31. if (in_len > clen || tot_len < 4 + in_len)
  32. return -1;
  33. tot_len -= 4 + in_len;
  34. out_len = dlen;
  35. ret = lzo1x_decompress_safe(cbuf, in_len, dbuf, &out_len);
  36. if (ret != LZO_E_OK)
  37. return -1;
  38. cbuf += in_len;
  39. clen -= in_len;
  40. dbuf += out_len;
  41. dlen -= out_len;
  42. res += out_len;
  43. }
  44. return res;
  45. }
  46. /* from zutil.h */
  47. #define PRESET_DICT 0x20
  48. static u32 decompress_zlib(const u8 *_cbuf, u32 clen, u8 *dbuf, u32 dlen)
  49. {
  50. int wbits = MAX_WBITS, ret = -1;
  51. z_stream stream;
  52. u8 *cbuf;
  53. u32 res;
  54. memset(&stream, 0, sizeof(stream));
  55. cbuf = (u8 *) _cbuf;
  56. stream.total_in = 0;
  57. stream.next_out = dbuf;
  58. stream.avail_out = dlen;
  59. stream.total_out = 0;
  60. /* skip adler32 check if deflate and no dictionary */
  61. if (clen > 2 && !(cbuf[1] & PRESET_DICT) &&
  62. ((cbuf[0] & 0x0f) == Z_DEFLATED) &&
  63. !(((cbuf[0] << 8) + cbuf[1]) % 31)) {
  64. wbits = -((cbuf[0] >> 4) + 8);
  65. cbuf += 2;
  66. clen -= 2;
  67. }
  68. if (Z_OK != inflateInit2(&stream, wbits))
  69. return -1;
  70. while (stream.total_in < clen) {
  71. stream.next_in = cbuf + stream.total_in;
  72. stream.avail_in = min((u32) (clen - stream.total_in),
  73. (u32) btrfs_info.sb.sectorsize);
  74. ret = inflate(&stream, Z_NO_FLUSH);
  75. if (ret != Z_OK)
  76. break;
  77. }
  78. res = stream.total_out;
  79. inflateEnd(&stream);
  80. if (ret != Z_STREAM_END)
  81. return -1;
  82. return res;
  83. }
  84. u32 btrfs_decompress(u8 type, const char *c, u32 clen, char *d, u32 dlen)
  85. {
  86. u32 res;
  87. const u8 *cbuf;
  88. u8 *dbuf;
  89. cbuf = (const u8 *) c;
  90. dbuf = (u8 *) d;
  91. switch (type) {
  92. case BTRFS_COMPRESS_NONE:
  93. res = dlen < clen ? dlen : clen;
  94. memcpy(dbuf, cbuf, res);
  95. return res;
  96. case BTRFS_COMPRESS_ZLIB:
  97. return decompress_zlib(cbuf, clen, dbuf, dlen);
  98. case BTRFS_COMPRESS_LZO:
  99. return decompress_lzo(cbuf, clen, dbuf, dlen);
  100. default:
  101. printf("%s: Unsupported compression in extent: %i\n", __func__,
  102. type);
  103. return -1;
  104. }
  105. }