xref: /llvm-project/llvm/lib/Support/Compression.cpp (revision 5ecb161c647ba5703ec3351becc6f349b92edaa2)
1 //===--- Compression.cpp - Compression implementation ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file implements compression functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/Compression.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #if LLVM_ENABLE_ZLIB
21 #include <zlib.h>
22 #endif
23 
24 using namespace llvm;
25 using namespace llvm::compression;
26 
27 #if LLVM_ENABLE_ZLIB
28 
29 static StringRef convertZlibCodeToString(int Code) {
30   switch (Code) {
31   case Z_MEM_ERROR:
32     return "zlib error: Z_MEM_ERROR";
33   case Z_BUF_ERROR:
34     return "zlib error: Z_BUF_ERROR";
35   case Z_STREAM_ERROR:
36     return "zlib error: Z_STREAM_ERROR";
37   case Z_DATA_ERROR:
38     return "zlib error: Z_DATA_ERROR";
39   case Z_OK:
40   default:
41     llvm_unreachable("unknown or unexpected zlib status code");
42   }
43 }
44 
45 bool zlib::isAvailable() { return true; }
46 
47 void zlib::compress(ArrayRef<uint8_t> Input,
48                     SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
49   unsigned long CompressedSize = ::compressBound(Input.size());
50   CompressedBuffer.resize_for_overwrite(CompressedSize);
51   int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
52                         (const Bytef *)Input.data(), Input.size(), Level);
53   if (Res == Z_MEM_ERROR)
54     report_bad_alloc_error("Allocation failed");
55   assert(Res == Z_OK);
56   // Tell MemorySanitizer that zlib output buffer is fully initialized.
57   // This avoids a false report when running LLVM with uninstrumented ZLib.
58   __msan_unpoison(CompressedBuffer.data(), CompressedSize);
59   if (CompressedSize < CompressedBuffer.size())
60     CompressedBuffer.truncate(CompressedSize);
61 }
62 
63 Error zlib::uncompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
64                        size_t &UncompressedSize) {
65   int Res =
66       ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
67                    (const Bytef *)Input.data(), Input.size());
68   // Tell MemorySanitizer that zlib output buffer is fully initialized.
69   // This avoids a false report when running LLVM with uninstrumented ZLib.
70   __msan_unpoison(UncompressedBuffer, UncompressedSize);
71   return Res ? make_error<StringError>(convertZlibCodeToString(Res),
72                                        inconvertibleErrorCode())
73              : Error::success();
74 }
75 
76 Error zlib::uncompress(ArrayRef<uint8_t> Input,
77                        SmallVectorImpl<uint8_t> &UncompressedBuffer,
78                        size_t UncompressedSize) {
79   UncompressedBuffer.resize_for_overwrite(UncompressedSize);
80   Error E =
81       zlib::uncompress(Input, UncompressedBuffer.data(), UncompressedSize);
82   if (UncompressedSize < UncompressedBuffer.size())
83     UncompressedBuffer.truncate(UncompressedSize);
84   return E;
85 }
86 
87 #else
88 bool zlib::isAvailable() { return false; }
89 void zlib::compress(ArrayRef<uint8_t> Input,
90                     SmallVectorImpl<uint8_t> &CompressedBuffer, int Level) {
91   llvm_unreachable("zlib::compress is unavailable");
92 }
93 Error zlib::uncompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
94                        size_t &UncompressedSize) {
95   llvm_unreachable("zlib::uncompress is unavailable");
96 }
97 Error zlib::uncompress(ArrayRef<uint8_t> Input,
98                        SmallVectorImpl<uint8_t> &UncompressedBuffer,
99                        size_t UncompressedSize) {
100   llvm_unreachable("zlib::uncompress is unavailable");
101 }
102 #endif
103