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(StringRef InputBuffer,
48                     SmallVectorImpl<char> &CompressedBuffer, int Level) {
49   unsigned long CompressedSize = ::compressBound(InputBuffer.size());
50   CompressedBuffer.resize_for_overwrite(CompressedSize);
51   int Res =
52       ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
53                   (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level);
54   if (Res == Z_MEM_ERROR)
55     report_bad_alloc_error("Allocation failed");
56   assert(Res == Z_OK);
57   // Tell MemorySanitizer that zlib output buffer is fully initialized.
58   // This avoids a false report when running LLVM with uninstrumented ZLib.
59   __msan_unpoison(CompressedBuffer.data(), CompressedSize);
60   if (CompressedSize < CompressedBuffer.size())
61     CompressedBuffer.truncate(CompressedSize);
62 }
63 
64 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
65                        size_t &UncompressedSize) {
66   int Res =
67       ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
68                    (const Bytef *)InputBuffer.data(), InputBuffer.size());
69   // Tell MemorySanitizer that zlib output buffer is fully initialized.
70   // This avoids a false report when running LLVM with uninstrumented ZLib.
71   __msan_unpoison(UncompressedBuffer, UncompressedSize);
72   return Res ? make_error<StringError>(convertZlibCodeToString(Res),
73                                        inconvertibleErrorCode())
74              : Error::success();
75 }
76 
77 Error zlib::uncompress(StringRef InputBuffer,
78                        SmallVectorImpl<char> &UncompressedBuffer,
79                        size_t UncompressedSize) {
80   UncompressedBuffer.resize_for_overwrite(UncompressedSize);
81   Error E = zlib::uncompress(InputBuffer, UncompressedBuffer.data(),
82                              UncompressedSize);
83   if (UncompressedSize < UncompressedBuffer.size())
84     UncompressedBuffer.truncate(UncompressedSize);
85   return E;
86 }
87 
88 #else
89 bool zlib::isAvailable() { return false; }
90 void zlib::compress(StringRef InputBuffer,
91                     SmallVectorImpl<char> &CompressedBuffer, int Level) {
92   llvm_unreachable("zlib::compress is unavailable");
93 }
94 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
95                        size_t &UncompressedSize) {
96   llvm_unreachable("zlib::uncompress is unavailable");
97 }
98 Error zlib::uncompress(StringRef InputBuffer,
99                        SmallVectorImpl<char> &UncompressedBuffer,
100                        size_t UncompressedSize) {
101   llvm_unreachable("zlib::uncompress is unavailable");
102 }
103 #endif
104