Compression.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Support/Compression.h ---Compression----------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains basic functions for compression/decompression.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_COMPRESSION_H
  18. #define LLVM_SUPPORT_COMPRESSION_H
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/Support/DataTypes.h"
  21. namespace llvm {
  22. template <typename T> class SmallVectorImpl;
  23. class Error;
  24. // None indicates no compression. The other members are a subset of
  25. // compression::Format, which is used for compressed debug sections in some
  26. // object file formats (e.g. ELF). This is a separate class as we may add new
  27. // compression::Format members for non-debugging purposes.
  28. enum class DebugCompressionType {
  29. None, ///< No compression
  30. Zlib, ///< zlib
  31. Zstd, ///< Zstandard
  32. };
  33. namespace compression {
  34. namespace zlib {
  35. constexpr int NoCompression = 0;
  36. constexpr int BestSpeedCompression = 1;
  37. constexpr int DefaultCompression = 6;
  38. constexpr int BestSizeCompression = 9;
  39. bool isAvailable();
  40. void compress(ArrayRef<uint8_t> Input,
  41. SmallVectorImpl<uint8_t> &CompressedBuffer,
  42. int Level = DefaultCompression);
  43. Error decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
  44. size_t &UncompressedSize);
  45. Error decompress(ArrayRef<uint8_t> Input, SmallVectorImpl<uint8_t> &Output,
  46. size_t UncompressedSize);
  47. } // End of namespace zlib
  48. namespace zstd {
  49. constexpr int NoCompression = -5;
  50. constexpr int BestSpeedCompression = 1;
  51. constexpr int DefaultCompression = 5;
  52. constexpr int BestSizeCompression = 12;
  53. bool isAvailable();
  54. void compress(ArrayRef<uint8_t> Input,
  55. SmallVectorImpl<uint8_t> &CompressedBuffer,
  56. int Level = DefaultCompression);
  57. Error decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
  58. size_t &UncompressedSize);
  59. Error decompress(ArrayRef<uint8_t> Input, SmallVectorImpl<uint8_t> &Output,
  60. size_t UncompressedSize);
  61. } // End of namespace zstd
  62. enum class Format {
  63. Zlib,
  64. Zstd,
  65. };
  66. inline Format formatFor(DebugCompressionType Type) {
  67. switch (Type) {
  68. case DebugCompressionType::None:
  69. llvm_unreachable("not a compression type");
  70. case DebugCompressionType::Zlib:
  71. return Format::Zlib;
  72. case DebugCompressionType::Zstd:
  73. return Format::Zstd;
  74. }
  75. llvm_unreachable("");
  76. }
  77. struct Params {
  78. constexpr Params(Format F)
  79. : format(F), level(F == Format::Zlib ? zlib::DefaultCompression
  80. : zstd::DefaultCompression) {}
  81. Params(DebugCompressionType Type) : Params(formatFor(Type)) {}
  82. Format format;
  83. int level;
  84. // This may support multi-threading for zstd in the future. Note that
  85. // different threads may produce different output, so be careful if certain
  86. // output determinism is desired.
  87. };
  88. // Return nullptr if LLVM was built with support (LLVM_ENABLE_ZLIB,
  89. // LLVM_ENABLE_ZSTD) for the specified compression format; otherwise
  90. // return a string literal describing the reason.
  91. const char *getReasonIfUnsupported(Format F);
  92. // Compress Input with the specified format P.Format. If Level is -1, use
  93. // *::DefaultCompression for the format.
  94. void compress(Params P, ArrayRef<uint8_t> Input,
  95. SmallVectorImpl<uint8_t> &Output);
  96. // Decompress Input. The uncompressed size must be available.
  97. Error decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
  98. uint8_t *Output, size_t UncompressedSize);
  99. Error decompress(Format F, ArrayRef<uint8_t> Input,
  100. SmallVectorImpl<uint8_t> &Output, size_t UncompressedSize);
  101. Error decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
  102. SmallVectorImpl<uint8_t> &Output, size_t UncompressedSize);
  103. } // End of namespace compression
  104. } // End of namespace llvm
  105. #endif
  106. #ifdef __GNUC__
  107. #pragma GCC diagnostic pop
  108. #endif