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 
26 #if LLVM_ENABLE_ZLIB
27 static Error createError(StringRef Err) {
28   return make_error<StringError>(Err, inconvertibleErrorCode());
29 }
30 
31 static StringRef convertZlibCodeToString(int Code) {
32   switch (Code) {
33   case Z_MEM_ERROR:
34     return "zlib error: Z_MEM_ERROR";
35   case Z_BUF_ERROR:
36     return "zlib error: Z_BUF_ERROR";
37   case Z_STREAM_ERROR:
38     return "zlib error: Z_STREAM_ERROR";
39   case Z_DATA_ERROR:
40     return "zlib error: Z_DATA_ERROR";
41   case Z_OK:
42   default:
43     llvm_unreachable("unknown or unexpected zlib status code");
44   }
45 }
46 
47 bool zlib::isAvailable() { return true; }
48 
49 void zlib::compress(StringRef InputBuffer,
50                     SmallVectorImpl<char> &CompressedBuffer, int Level) {
51   unsigned long CompressedSize = ::compressBound(InputBuffer.size());
52   CompressedBuffer.resize_for_overwrite(CompressedSize);
53   int Res =
54       ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
55                   (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level);
56   if (Res == Z_MEM_ERROR)
57     report_bad_alloc_error("Allocation failed");
58   assert(Res == Z_OK);
59   // Tell MemorySanitizer that zlib output buffer is fully initialized.
60   // This avoids a false report when running LLVM with uninstrumented ZLib.
61   __msan_unpoison(CompressedBuffer.data(), CompressedSize);
62   CompressedBuffer.truncate(CompressedSize);
63 }
64 
65 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
66                        size_t &UncompressedSize) {
67   int Res =
68       ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
69                    (const Bytef *)InputBuffer.data(), InputBuffer.size());
70   // Tell MemorySanitizer that zlib output buffer is fully initialized.
71   // This avoids a false report when running LLVM with uninstrumented ZLib.
72   __msan_unpoison(UncompressedBuffer, UncompressedSize);
73   return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
74 }
75 
76 Error zlib::uncompress(StringRef InputBuffer,
77                        SmallVectorImpl<char> &UncompressedBuffer,
78                        size_t UncompressedSize) {
79   UncompressedBuffer.resize_for_overwrite(UncompressedSize);
80   Error E =
81       uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
82   UncompressedBuffer.truncate(UncompressedSize);
83   return E;
84 }
85 
86 uint32_t zlib::crc32(StringRef Buffer) {
87   return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
88 }
89 
90 #else
91 bool zlib::isAvailable() { return false; }
92 void zlib::compress(StringRef InputBuffer,
93                     SmallVectorImpl<char> &CompressedBuffer, int Level) {
94   llvm_unreachable("zlib::compress is unavailable");
95 }
96 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
97                        size_t &UncompressedSize) {
98   llvm_unreachable("zlib::uncompress is unavailable");
99 }
100 Error zlib::uncompress(StringRef InputBuffer,
101                        SmallVectorImpl<char> &UncompressedBuffer,
102                        size_t UncompressedSize) {
103   llvm_unreachable("zlib::uncompress is unavailable");
104 }
105 uint32_t zlib::crc32(StringRef Buffer) {
106   llvm_unreachable("zlib::crc32 is unavailable");
107 }
108 #endif
109