decompressor.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Squashfs - a compressed read only filesystem for Linux
  4. *
  5. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
  6. * Phillip Lougher <[email protected]>
  7. *
  8. * decompressor.c
  9. */
  10. #include <linux/types.h>
  11. #include <linux/mutex.h>
  12. #include <linux/slab.h>
  13. #include <linux/buffer_head.h>
  14. #include "squashfs_fs.h"
  15. #include "squashfs_fs_sb.h"
  16. #include "decompressor.h"
  17. #include "squashfs.h"
  18. #include "page_actor.h"
  19. /*
  20. * This file (and decompressor.h) implements a decompressor framework for
  21. * Squashfs, allowing multiple decompressors to be easily supported
  22. */
  23. static const struct squashfs_decompressor squashfs_lzma_unsupported_comp_ops = {
  24. NULL, NULL, NULL, NULL, LZMA_COMPRESSION, "lzma", 0
  25. };
  26. #ifndef CONFIG_SQUASHFS_LZ4
  27. static const struct squashfs_decompressor squashfs_lz4_comp_ops = {
  28. NULL, NULL, NULL, NULL, LZ4_COMPRESSION, "lz4", 0
  29. };
  30. #endif
  31. #ifndef CONFIG_SQUASHFS_LZO
  32. static const struct squashfs_decompressor squashfs_lzo_comp_ops = {
  33. NULL, NULL, NULL, NULL, LZO_COMPRESSION, "lzo", 0
  34. };
  35. #endif
  36. #ifndef CONFIG_SQUASHFS_XZ
  37. static const struct squashfs_decompressor squashfs_xz_comp_ops = {
  38. NULL, NULL, NULL, NULL, XZ_COMPRESSION, "xz", 0
  39. };
  40. #endif
  41. #ifndef CONFIG_SQUASHFS_ZLIB
  42. static const struct squashfs_decompressor squashfs_zlib_comp_ops = {
  43. NULL, NULL, NULL, NULL, ZLIB_COMPRESSION, "zlib", 0
  44. };
  45. #endif
  46. #ifndef CONFIG_SQUASHFS_ZSTD
  47. static const struct squashfs_decompressor squashfs_zstd_comp_ops = {
  48. NULL, NULL, NULL, NULL, ZSTD_COMPRESSION, "zstd", 0
  49. };
  50. #endif
  51. static const struct squashfs_decompressor squashfs_unknown_comp_ops = {
  52. NULL, NULL, NULL, NULL, 0, "unknown", 0
  53. };
  54. static const struct squashfs_decompressor *decompressor[] = {
  55. &squashfs_zlib_comp_ops,
  56. &squashfs_lz4_comp_ops,
  57. &squashfs_lzo_comp_ops,
  58. &squashfs_xz_comp_ops,
  59. &squashfs_lzma_unsupported_comp_ops,
  60. &squashfs_zstd_comp_ops,
  61. &squashfs_unknown_comp_ops
  62. };
  63. const struct squashfs_decompressor *squashfs_lookup_decompressor(int id)
  64. {
  65. int i;
  66. for (i = 0; decompressor[i]->id; i++)
  67. if (id == decompressor[i]->id)
  68. break;
  69. return decompressor[i];
  70. }
  71. static void *get_comp_opts(struct super_block *sb, unsigned short flags)
  72. {
  73. struct squashfs_sb_info *msblk = sb->s_fs_info;
  74. void *buffer = NULL, *comp_opts;
  75. struct squashfs_page_actor *actor = NULL;
  76. int length = 0;
  77. /*
  78. * Read decompressor specific options from file system if present
  79. */
  80. if (SQUASHFS_COMP_OPTS(flags)) {
  81. buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
  82. if (buffer == NULL) {
  83. comp_opts = ERR_PTR(-ENOMEM);
  84. goto out;
  85. }
  86. actor = squashfs_page_actor_init(&buffer, 1, 0);
  87. if (actor == NULL) {
  88. comp_opts = ERR_PTR(-ENOMEM);
  89. goto out;
  90. }
  91. length = squashfs_read_data(sb,
  92. sizeof(struct squashfs_super_block), 0, NULL, actor);
  93. if (length < 0) {
  94. comp_opts = ERR_PTR(length);
  95. goto out;
  96. }
  97. }
  98. comp_opts = squashfs_comp_opts(msblk, buffer, length);
  99. out:
  100. kfree(actor);
  101. kfree(buffer);
  102. return comp_opts;
  103. }
  104. void *squashfs_decompressor_setup(struct super_block *sb, unsigned short flags)
  105. {
  106. struct squashfs_sb_info *msblk = sb->s_fs_info;
  107. void *stream, *comp_opts = get_comp_opts(sb, flags);
  108. if (IS_ERR(comp_opts))
  109. return comp_opts;
  110. stream = squashfs_decompressor_create(msblk, comp_opts);
  111. if (IS_ERR(stream))
  112. kfree(comp_opts);
  113. return stream;
  114. }