sqfs_decompressor.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Bootlin
  4. *
  5. * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
  6. */
  7. #include <errno.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #if IS_ENABLED(CONFIG_LZO)
  12. #include <linux/lzo.h>
  13. #endif
  14. #if IS_ENABLED(CONFIG_ZLIB)
  15. #include <u-boot/zlib.h>
  16. #endif
  17. #if IS_ENABLED(CONFIG_ZSTD)
  18. #include <linux/zstd.h>
  19. #endif
  20. #include "sqfs_decompressor.h"
  21. #include "sqfs_utils.h"
  22. int sqfs_decompressor_init(struct squashfs_ctxt *ctxt)
  23. {
  24. u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
  25. switch (comp_type) {
  26. #if IS_ENABLED(CONFIG_LZO)
  27. case SQFS_COMP_LZO:
  28. break;
  29. #endif
  30. #if IS_ENABLED(CONFIG_ZLIB)
  31. case SQFS_COMP_ZLIB:
  32. break;
  33. #endif
  34. #if IS_ENABLED(CONFIG_ZSTD)
  35. case SQFS_COMP_ZSTD:
  36. ctxt->zstd_workspace = malloc(ZSTD_DCtxWorkspaceBound());
  37. if (!ctxt->zstd_workspace)
  38. return -ENOMEM;
  39. break;
  40. #endif
  41. default:
  42. printf("Error: unknown compression type.\n");
  43. return -EINVAL;
  44. }
  45. return 0;
  46. }
  47. void sqfs_decompressor_cleanup(struct squashfs_ctxt *ctxt)
  48. {
  49. u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
  50. switch (comp_type) {
  51. #if IS_ENABLED(CONFIG_LZO)
  52. case SQFS_COMP_LZO:
  53. break;
  54. #endif
  55. #if IS_ENABLED(CONFIG_ZLIB)
  56. case SQFS_COMP_ZLIB:
  57. break;
  58. #endif
  59. #if IS_ENABLED(CONFIG_ZSTD)
  60. case SQFS_COMP_ZSTD:
  61. free(ctxt->zstd_workspace);
  62. break;
  63. #endif
  64. }
  65. }
  66. #if IS_ENABLED(CONFIG_ZLIB)
  67. static void zlib_decompression_status(int ret)
  68. {
  69. switch (ret) {
  70. case Z_BUF_ERROR:
  71. printf("Error: 'dest' buffer is not large enough.\n");
  72. break;
  73. case Z_DATA_ERROR:
  74. printf("Error: corrupted compressed data.\n");
  75. break;
  76. case Z_MEM_ERROR:
  77. printf("Error: insufficient memory.\n");
  78. break;
  79. }
  80. }
  81. #endif
  82. #if IS_ENABLED(CONFIG_ZSTD)
  83. static int sqfs_zstd_decompress(struct squashfs_ctxt *ctxt, void *dest,
  84. unsigned long dest_len, void *source, u32 src_len)
  85. {
  86. ZSTD_DCtx *ctx;
  87. size_t wsize;
  88. int ret;
  89. wsize = ZSTD_DCtxWorkspaceBound();
  90. ctx = ZSTD_initDCtx(ctxt->zstd_workspace, wsize);
  91. ret = ZSTD_decompressDCtx(ctx, dest, dest_len, source, src_len);
  92. return ZSTD_isError(ret);
  93. }
  94. #endif /* CONFIG_ZSTD */
  95. int sqfs_decompress(struct squashfs_ctxt *ctxt, void *dest,
  96. unsigned long *dest_len, void *source, u32 src_len)
  97. {
  98. u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
  99. int ret = 0;
  100. switch (comp_type) {
  101. #if IS_ENABLED(CONFIG_LZO)
  102. case SQFS_COMP_LZO: {
  103. size_t lzo_dest_len = *dest_len;
  104. ret = lzo1x_decompress_safe(source, src_len, dest, &lzo_dest_len);
  105. if (ret) {
  106. printf("LZO decompression failed. Error code: %d\n", ret);
  107. return -EINVAL;
  108. }
  109. break;
  110. }
  111. #endif
  112. #if IS_ENABLED(CONFIG_ZLIB)
  113. case SQFS_COMP_ZLIB:
  114. ret = uncompress(dest, dest_len, source, src_len);
  115. if (ret) {
  116. zlib_decompression_status(ret);
  117. return -EINVAL;
  118. }
  119. break;
  120. #endif
  121. #if IS_ENABLED(CONFIG_ZSTD)
  122. case SQFS_COMP_ZSTD:
  123. ret = sqfs_zstd_decompress(ctxt, dest, *dest_len, source, src_len);
  124. if (ret) {
  125. printf("ZSTD Error code: %d\n", ZSTD_getErrorCode(ret));
  126. return -EINVAL;
  127. }
  128. break;
  129. #endif
  130. default:
  131. printf("Error: unknown compression type.\n");
  132. return -EINVAL;
  133. }
  134. return ret;
  135. }