12fb337e7SAlexey Samsonov //===--- Compression.cpp - Compression implementation ---------------------===//
22fb337e7SAlexey Samsonov //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62fb337e7SAlexey Samsonov //
72fb337e7SAlexey Samsonov //===----------------------------------------------------------------------===//
82fb337e7SAlexey Samsonov //
92fb337e7SAlexey Samsonov //  This file implements compression functions.
102fb337e7SAlexey Samsonov //
112fb337e7SAlexey Samsonov //===----------------------------------------------------------------------===//
122fb337e7SAlexey Samsonov 
132fb337e7SAlexey Samsonov #include "llvm/Support/Compression.h"
1416132e6fSBenjamin Kramer #include "llvm/ADT/SmallVector.h"
152fb337e7SAlexey Samsonov #include "llvm/ADT/StringRef.h"
162fb337e7SAlexey Samsonov #include "llvm/Config/config.h"
170c9f1bfaSAlexey Samsonov #include "llvm/Support/Compiler.h"
18167ca4aeSGeorge Rimar #include "llvm/Support/Error.h"
192fb337e7SAlexey Samsonov #include "llvm/Support/ErrorHandling.h"
2031e5f712SPetr Hosek #if LLVM_ENABLE_ZLIB
212fb337e7SAlexey Samsonov #include <zlib.h>
222fb337e7SAlexey Samsonov #endif
232fb337e7SAlexey Samsonov 
242fb337e7SAlexey Samsonov using namespace llvm;
25ea61750cSCole Kissane using namespace llvm::compression;
262fb337e7SAlexey Samsonov 
2731e5f712SPetr Hosek #if LLVM_ENABLE_ZLIB
28167ca4aeSGeorge Rimar 
29167ca4aeSGeorge Rimar static StringRef convertZlibCodeToString(int Code) {
30167ca4aeSGeorge Rimar   switch (Code) {
31167ca4aeSGeorge Rimar   case Z_MEM_ERROR:
32167ca4aeSGeorge Rimar     return "zlib error: Z_MEM_ERROR";
33167ca4aeSGeorge Rimar   case Z_BUF_ERROR:
34167ca4aeSGeorge Rimar     return "zlib error: Z_BUF_ERROR";
35167ca4aeSGeorge Rimar   case Z_STREAM_ERROR:
36167ca4aeSGeorge Rimar     return "zlib error: Z_STREAM_ERROR";
37167ca4aeSGeorge Rimar   case Z_DATA_ERROR:
38167ca4aeSGeorge Rimar     return "zlib error: Z_DATA_ERROR";
39167ca4aeSGeorge Rimar   case Z_OK:
40167ca4aeSGeorge Rimar   default:
41167ca4aeSGeorge Rimar     llvm_unreachable("unknown or unexpected zlib status code");
422fb337e7SAlexey Samsonov   }
432fb337e7SAlexey Samsonov }
442fb337e7SAlexey Samsonov 
452fb337e7SAlexey Samsonov bool zlib::isAvailable() { return true; }
46167ca4aeSGeorge Rimar 
47*e690137dSFangrui Song void zlib::compress(ArrayRef<uint8_t> Input,
48*e690137dSFangrui Song                     SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
49*e690137dSFangrui Song   unsigned long CompressedSize = ::compressBound(Input.size());
50cd7bc0e0SDuncan P. N. Exon Smith   CompressedBuffer.resize_for_overwrite(CompressedSize);
51*e690137dSFangrui Song   int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
52*e690137dSFangrui Song                         (const Bytef *)Input.data(), Input.size(), Level);
53407c721cSFangrui Song   if (Res == Z_MEM_ERROR)
54407c721cSFangrui Song     report_bad_alloc_error("Allocation failed");
55407c721cSFangrui Song   assert(Res == Z_OK);
5628cacae2SEvgeniy Stepanov   // Tell MemorySanitizer that zlib output buffer is fully initialized.
5728cacae2SEvgeniy Stepanov   // This avoids a false report when running LLVM with uninstrumented ZLib.
5828cacae2SEvgeniy Stepanov   __msan_unpoison(CompressedBuffer.data(), CompressedSize);
596b618a62SCole Kissane   if (CompressedSize < CompressedBuffer.size())
60cd7bc0e0SDuncan P. N. Exon Smith     CompressedBuffer.truncate(CompressedSize);
612fb337e7SAlexey Samsonov }
622fb337e7SAlexey Samsonov 
63*e690137dSFangrui Song Error zlib::uncompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
6446cdcb03SRafael Espindola                        size_t &UncompressedSize) {
65167ca4aeSGeorge Rimar   int Res =
6646cdcb03SRafael Espindola       ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
67*e690137dSFangrui Song                    (const Bytef *)Input.data(), Input.size());
6846cdcb03SRafael Espindola   // Tell MemorySanitizer that zlib output buffer is fully initialized.
6946cdcb03SRafael Espindola   // This avoids a false report when running LLVM with uninstrumented ZLib.
7046cdcb03SRafael Espindola   __msan_unpoison(UncompressedBuffer, UncompressedSize);
71ea61750cSCole Kissane   return Res ? make_error<StringError>(convertZlibCodeToString(Res),
72ea61750cSCole Kissane                                        inconvertibleErrorCode())
73ea61750cSCole Kissane              : Error::success();
7446cdcb03SRafael Espindola }
7546cdcb03SRafael Espindola 
76*e690137dSFangrui Song Error zlib::uncompress(ArrayRef<uint8_t> Input,
77*e690137dSFangrui Song                        SmallVectorImpl<uint8_t> &UncompressedBuffer,
782fb337e7SAlexey Samsonov                        size_t UncompressedSize) {
79cd7bc0e0SDuncan P. N. Exon Smith   UncompressedBuffer.resize_for_overwrite(UncompressedSize);
80*e690137dSFangrui Song   Error E =
81*e690137dSFangrui Song       zlib::uncompress(Input, UncompressedBuffer.data(), UncompressedSize);
826b618a62SCole Kissane   if (UncompressedSize < UncompressedBuffer.size())
83cd7bc0e0SDuncan P. N. Exon Smith     UncompressedBuffer.truncate(UncompressedSize);
84167ca4aeSGeorge Rimar   return E;
852fb337e7SAlexey Samsonov }
862fb337e7SAlexey Samsonov 
872fb337e7SAlexey Samsonov #else
882fb337e7SAlexey Samsonov bool zlib::isAvailable() { return false; }
89407c721cSFangrui Song void zlib::compress(StringRef InputBuffer,
90e9798f78SRui Ueyama                     SmallVectorImpl<char> &CompressedBuffer, int Level) {
91167ca4aeSGeorge Rimar   llvm_unreachable("zlib::compress is unavailable");
922fb337e7SAlexey Samsonov }
93167ca4aeSGeorge Rimar Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
9474941239SRafael Espindola                        size_t &UncompressedSize) {
95167ca4aeSGeorge Rimar   llvm_unreachable("zlib::uncompress is unavailable");
9674941239SRafael Espindola }
97167ca4aeSGeorge Rimar Error zlib::uncompress(StringRef InputBuffer,
98857497b9SDavid Blaikie                        SmallVectorImpl<char> &UncompressedBuffer,
992fb337e7SAlexey Samsonov                        size_t UncompressedSize) {
100167ca4aeSGeorge Rimar   llvm_unreachable("zlib::uncompress is unavailable");
1012fb337e7SAlexey Samsonov }
1022fb337e7SAlexey Samsonov #endif
103