1*0b57cec5SDimitry Andric //===--- Compression.cpp - Compression implementation ---------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements compression functions.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric
13*0b57cec5SDimitry Andric #include "llvm/Support/Compression.h"
14*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
15*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
16*0b57cec5SDimitry Andric #include "llvm/Config/config.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
18*0b57cec5SDimitry Andric #include "llvm/Support/Error.h"
19*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
20*0b57cec5SDimitry Andric #if LLVM_ENABLE_ZLIB
21*0b57cec5SDimitry Andric #include <zlib.h>
22*0b57cec5SDimitry Andric #endif
23*0b57cec5SDimitry Andric #if LLVM_ENABLE_ZSTD
24*0b57cec5SDimitry Andric #include <zstd.h>
25*0b57cec5SDimitry Andric #endif
26*0b57cec5SDimitry Andric
27*0b57cec5SDimitry Andric using namespace llvm;
28*0b57cec5SDimitry Andric using namespace llvm::compression;
29*0b57cec5SDimitry Andric
getReasonIfUnsupported(compression::Format F)30*0b57cec5SDimitry Andric const char *compression::getReasonIfUnsupported(compression::Format F) {
31*0b57cec5SDimitry Andric switch (F) {
32*0b57cec5SDimitry Andric case compression::Format::Zlib:
33*0b57cec5SDimitry Andric if (zlib::isAvailable())
34*0b57cec5SDimitry Andric return nullptr;
35*0b57cec5SDimitry Andric return "LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at "
36*0b57cec5SDimitry Andric "build time";
37*0b57cec5SDimitry Andric case compression::Format::Zstd:
38*0b57cec5SDimitry Andric if (zstd::isAvailable())
39*0b57cec5SDimitry Andric return nullptr;
40*0b57cec5SDimitry Andric return "LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at "
41*0b57cec5SDimitry Andric "build time";
42*0b57cec5SDimitry Andric }
43*0b57cec5SDimitry Andric llvm_unreachable("");
44*0b57cec5SDimitry Andric }
45*0b57cec5SDimitry Andric
compress(Params P,ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & Output)46*0b57cec5SDimitry Andric void compression::compress(Params P, ArrayRef<uint8_t> Input,
47*0b57cec5SDimitry Andric SmallVectorImpl<uint8_t> &Output) {
48*0b57cec5SDimitry Andric switch (P.format) {
49*0b57cec5SDimitry Andric case compression::Format::Zlib:
50*0b57cec5SDimitry Andric zlib::compress(Input, Output, P.level);
51*0b57cec5SDimitry Andric break;
52*0b57cec5SDimitry Andric case compression::Format::Zstd:
53*0b57cec5SDimitry Andric zstd::compress(Input, Output, P.level);
54*0b57cec5SDimitry Andric break;
55*0b57cec5SDimitry Andric }
56*0b57cec5SDimitry Andric }
57*0b57cec5SDimitry Andric
decompress(DebugCompressionType T,ArrayRef<uint8_t> Input,uint8_t * Output,size_t UncompressedSize)58*0b57cec5SDimitry Andric Error compression::decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
59*0b57cec5SDimitry Andric uint8_t *Output, size_t UncompressedSize) {
60*0b57cec5SDimitry Andric switch (formatFor(T)) {
61*0b57cec5SDimitry Andric case compression::Format::Zlib:
62*0b57cec5SDimitry Andric return zlib::decompress(Input, Output, UncompressedSize);
63*0b57cec5SDimitry Andric case compression::Format::Zstd:
64*0b57cec5SDimitry Andric return zstd::decompress(Input, Output, UncompressedSize);
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric llvm_unreachable("");
67*0b57cec5SDimitry Andric }
68*0b57cec5SDimitry Andric
decompress(compression::Format F,ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & Output,size_t UncompressedSize)69*0b57cec5SDimitry Andric Error compression::decompress(compression::Format F, ArrayRef<uint8_t> Input,
70*0b57cec5SDimitry Andric SmallVectorImpl<uint8_t> &Output,
71*0b57cec5SDimitry Andric size_t UncompressedSize) {
72*0b57cec5SDimitry Andric switch (F) {
73*0b57cec5SDimitry Andric case compression::Format::Zlib:
74*0b57cec5SDimitry Andric return zlib::decompress(Input, Output, UncompressedSize);
75*0b57cec5SDimitry Andric case compression::Format::Zstd:
76*0b57cec5SDimitry Andric return zstd::decompress(Input, Output, UncompressedSize);
77*0b57cec5SDimitry Andric }
78*0b57cec5SDimitry Andric llvm_unreachable("");
79*0b57cec5SDimitry Andric }
80*0b57cec5SDimitry Andric
decompress(DebugCompressionType T,ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & Output,size_t UncompressedSize)81*0b57cec5SDimitry Andric Error compression::decompress(DebugCompressionType T, ArrayRef<uint8_t> Input,
82*0b57cec5SDimitry Andric SmallVectorImpl<uint8_t> &Output,
83*0b57cec5SDimitry Andric size_t UncompressedSize) {
84*0b57cec5SDimitry Andric return decompress(formatFor(T), Input, Output, UncompressedSize);
85*0b57cec5SDimitry Andric }
86*0b57cec5SDimitry Andric
87*0b57cec5SDimitry Andric #if LLVM_ENABLE_ZLIB
88*0b57cec5SDimitry Andric
convertZlibCodeToString(int Code)89*0b57cec5SDimitry Andric static StringRef convertZlibCodeToString(int Code) {
90*0b57cec5SDimitry Andric switch (Code) {
91*0b57cec5SDimitry Andric case Z_MEM_ERROR:
92*0b57cec5SDimitry Andric return "zlib error: Z_MEM_ERROR";
93*0b57cec5SDimitry Andric case Z_BUF_ERROR:
94*0b57cec5SDimitry Andric return "zlib error: Z_BUF_ERROR";
95*0b57cec5SDimitry Andric case Z_STREAM_ERROR:
96*0b57cec5SDimitry Andric return "zlib error: Z_STREAM_ERROR";
97*0b57cec5SDimitry Andric case Z_DATA_ERROR:
98*0b57cec5SDimitry Andric return "zlib error: Z_DATA_ERROR";
99*0b57cec5SDimitry Andric case Z_OK:
100*0b57cec5SDimitry Andric default:
101*0b57cec5SDimitry Andric llvm_unreachable("unknown or unexpected zlib status code");
102*0b57cec5SDimitry Andric }
103*0b57cec5SDimitry Andric }
104*0b57cec5SDimitry Andric
isAvailable()105*0b57cec5SDimitry Andric bool zlib::isAvailable() { return true; }
106*0b57cec5SDimitry Andric
compress(ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & CompressedBuffer,int Level)107 void zlib::compress(ArrayRef<uint8_t> Input,
108 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
109 unsigned long CompressedSize = ::compressBound(Input.size());
110 CompressedBuffer.resize_for_overwrite(CompressedSize);
111 int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
112 (const Bytef *)Input.data(), Input.size(), Level);
113 if (Res == Z_MEM_ERROR)
114 report_bad_alloc_error("Allocation failed");
115 assert(Res == Z_OK);
116 // Tell MemorySanitizer that zlib output buffer is fully initialized.
117 // This avoids a false report when running LLVM with uninstrumented ZLib.
118 __msan_unpoison(CompressedBuffer.data(), CompressedSize);
119 if (CompressedSize < CompressedBuffer.size())
120 CompressedBuffer.truncate(CompressedSize);
121 }
122
decompress(ArrayRef<uint8_t> Input,uint8_t * Output,size_t & UncompressedSize)123 Error zlib::decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
124 size_t &UncompressedSize) {
125 int Res = ::uncompress((Bytef *)Output, (uLongf *)&UncompressedSize,
126 (const Bytef *)Input.data(), Input.size());
127 // Tell MemorySanitizer that zlib output buffer is fully initialized.
128 // This avoids a false report when running LLVM with uninstrumented ZLib.
129 __msan_unpoison(Output, UncompressedSize);
130 return Res ? make_error<StringError>(convertZlibCodeToString(Res),
131 inconvertibleErrorCode())
132 : Error::success();
133 }
134
decompress(ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & Output,size_t UncompressedSize)135 Error zlib::decompress(ArrayRef<uint8_t> Input,
136 SmallVectorImpl<uint8_t> &Output,
137 size_t UncompressedSize) {
138 Output.resize_for_overwrite(UncompressedSize);
139 Error E = zlib::decompress(Input, Output.data(), UncompressedSize);
140 if (UncompressedSize < Output.size())
141 Output.truncate(UncompressedSize);
142 return E;
143 }
144
145 #else
isAvailable()146 bool zlib::isAvailable() { return false; }
compress(ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & CompressedBuffer,int Level)147 void zlib::compress(ArrayRef<uint8_t> Input,
148 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
149 llvm_unreachable("zlib::compress is unavailable");
150 }
decompress(ArrayRef<uint8_t> Input,uint8_t * UncompressedBuffer,size_t & UncompressedSize)151 Error zlib::decompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
152 size_t &UncompressedSize) {
153 llvm_unreachable("zlib::decompress is unavailable");
154 }
decompress(ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & UncompressedBuffer,size_t UncompressedSize)155 Error zlib::decompress(ArrayRef<uint8_t> Input,
156 SmallVectorImpl<uint8_t> &UncompressedBuffer,
157 size_t UncompressedSize) {
158 llvm_unreachable("zlib::decompress is unavailable");
159 }
160 #endif
161
162 #if LLVM_ENABLE_ZSTD
163
isAvailable()164 bool zstd::isAvailable() { return true; }
165
compress(ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & CompressedBuffer,int Level)166 void zstd::compress(ArrayRef<uint8_t> Input,
167 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
168 unsigned long CompressedBufferSize = ::ZSTD_compressBound(Input.size());
169 CompressedBuffer.resize_for_overwrite(CompressedBufferSize);
170 unsigned long CompressedSize =
171 ::ZSTD_compress((char *)CompressedBuffer.data(), CompressedBufferSize,
172 (const char *)Input.data(), Input.size(), Level);
173 if (ZSTD_isError(CompressedSize))
174 report_bad_alloc_error("Allocation failed");
175 // Tell MemorySanitizer that zstd output buffer is fully initialized.
176 // This avoids a false report when running LLVM with uninstrumented ZLib.
177 __msan_unpoison(CompressedBuffer.data(), CompressedSize);
178 if (CompressedSize < CompressedBuffer.size())
179 CompressedBuffer.truncate(CompressedSize);
180 }
181
decompress(ArrayRef<uint8_t> Input,uint8_t * Output,size_t & UncompressedSize)182 Error zstd::decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
183 size_t &UncompressedSize) {
184 const size_t Res = ::ZSTD_decompress(
185 Output, UncompressedSize, (const uint8_t *)Input.data(), Input.size());
186 UncompressedSize = Res;
187 // Tell MemorySanitizer that zstd output buffer is fully initialized.
188 // This avoids a false report when running LLVM with uninstrumented ZLib.
189 __msan_unpoison(Output, UncompressedSize);
190 return ZSTD_isError(Res) ? make_error<StringError>(ZSTD_getErrorName(Res),
191 inconvertibleErrorCode())
192 : Error::success();
193 }
194
decompress(ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & Output,size_t UncompressedSize)195 Error zstd::decompress(ArrayRef<uint8_t> Input,
196 SmallVectorImpl<uint8_t> &Output,
197 size_t UncompressedSize) {
198 Output.resize_for_overwrite(UncompressedSize);
199 Error E = zstd::decompress(Input, Output.data(), UncompressedSize);
200 if (UncompressedSize < Output.size())
201 Output.truncate(UncompressedSize);
202 return E;
203 }
204
205 #else
isAvailable()206 bool zstd::isAvailable() { return false; }
compress(ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & CompressedBuffer,int Level)207 void zstd::compress(ArrayRef<uint8_t> Input,
208 SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
209 llvm_unreachable("zstd::compress is unavailable");
210 }
decompress(ArrayRef<uint8_t> Input,uint8_t * Output,size_t & UncompressedSize)211 Error zstd::decompress(ArrayRef<uint8_t> Input, uint8_t *Output,
212 size_t &UncompressedSize) {
213 llvm_unreachable("zstd::decompress is unavailable");
214 }
decompress(ArrayRef<uint8_t> Input,SmallVectorImpl<uint8_t> & Output,size_t UncompressedSize)215 Error zstd::decompress(ArrayRef<uint8_t> Input,
216 SmallVectorImpl<uint8_t> &Output,
217 size_t UncompressedSize) {
218 llvm_unreachable("zstd::decompress is unavailable");
219 }
220 #endif
221