sqfs_decompressor.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_ZLIB)
  12. #include <u-boot/zlib.h>
  13. #endif
  14. #include "sqfs_decompressor.h"
  15. #include "sqfs_utils.h"
  16. int sqfs_decompressor_init(struct squashfs_ctxt *ctxt)
  17. {
  18. u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
  19. switch (comp_type) {
  20. #if IS_ENABLED(CONFIG_ZLIB)
  21. case SQFS_COMP_ZLIB:
  22. break;
  23. #endif
  24. default:
  25. printf("Error: unknown compression type.\n");
  26. return -EINVAL;
  27. }
  28. return 0;
  29. }
  30. void sqfs_decompressor_cleanup(struct squashfs_ctxt *ctxt)
  31. {
  32. u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
  33. switch (comp_type) {
  34. #if IS_ENABLED(CONFIG_ZLIB)
  35. case SQFS_COMP_ZLIB:
  36. break;
  37. #endif
  38. }
  39. }
  40. #if IS_ENABLED(CONFIG_ZLIB)
  41. static void zlib_decompression_status(int ret)
  42. {
  43. switch (ret) {
  44. case Z_BUF_ERROR:
  45. printf("Error: 'dest' buffer is not large enough.\n");
  46. break;
  47. case Z_DATA_ERROR:
  48. printf("Error: corrupted compressed data.\n");
  49. break;
  50. case Z_MEM_ERROR:
  51. printf("Error: insufficient memory.\n");
  52. break;
  53. }
  54. }
  55. #endif
  56. int sqfs_decompress(struct squashfs_ctxt *ctxt, void *dest,
  57. unsigned long *dest_len, void *source, u32 src_len)
  58. {
  59. u16 comp_type = get_unaligned_le16(&ctxt->sblk->compression);
  60. int ret = 0;
  61. switch (comp_type) {
  62. #if IS_ENABLED(CONFIG_ZLIB)
  63. case SQFS_COMP_ZLIB:
  64. ret = uncompress(dest, dest_len, source, src_len);
  65. if (ret) {
  66. zlib_decompression_status(ret);
  67. return -EINVAL;
  68. }
  69. break;
  70. #endif
  71. default:
  72. printf("Error: unknown compression type.\n");
  73. return -EINVAL;
  74. }
  75. return ret;
  76. }