• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- Compression.cpp - Compression implementation ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements compression functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/Compression.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Config/config.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
22 #include <zlib.h>
23 #endif
24 
25 using namespace llvm;
26 
27 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
createError(StringRef Err)28 static Error createError(StringRef Err) {
29   return make_error<StringError>(Err, inconvertibleErrorCode());
30 }
31 
encodeZlibCompressionLevel(zlib::CompressionLevel Level)32 static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) {
33   switch (Level) {
34     case zlib::NoCompression: return 0;
35     case zlib::BestSpeedCompression: return 1;
36     case zlib::DefaultCompression: return Z_DEFAULT_COMPRESSION;
37     case zlib::BestSizeCompression: return 9;
38   }
39   llvm_unreachable("Invalid zlib::CompressionLevel!");
40 }
41 
convertZlibCodeToString(int Code)42 static StringRef convertZlibCodeToString(int Code) {
43   switch (Code) {
44   case Z_MEM_ERROR:
45     return "zlib error: Z_MEM_ERROR";
46   case Z_BUF_ERROR:
47     return "zlib error: Z_BUF_ERROR";
48   case Z_STREAM_ERROR:
49     return "zlib error: Z_STREAM_ERROR";
50   case Z_DATA_ERROR:
51     return "zlib error: Z_DATA_ERROR";
52   case Z_OK:
53   default:
54     llvm_unreachable("unknown or unexpected zlib status code");
55   }
56 }
57 
isAvailable()58 bool zlib::isAvailable() { return true; }
59 
compress(StringRef InputBuffer,SmallVectorImpl<char> & CompressedBuffer,CompressionLevel Level)60 Error zlib::compress(StringRef InputBuffer,
61                      SmallVectorImpl<char> &CompressedBuffer,
62                      CompressionLevel Level) {
63   unsigned long CompressedSize = ::compressBound(InputBuffer.size());
64   CompressedBuffer.resize(CompressedSize);
65   int CLevel = encodeZlibCompressionLevel(Level);
66   int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
67                         (const Bytef *)InputBuffer.data(), InputBuffer.size(),
68                         CLevel);
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(CompressedBuffer.data(), CompressedSize);
72   CompressedBuffer.resize(CompressedSize);
73   return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
74 }
75 
uncompress(StringRef InputBuffer,char * UncompressedBuffer,size_t & UncompressedSize)76 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
77                        size_t &UncompressedSize) {
78   int Res =
79       ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
80                    (const Bytef *)InputBuffer.data(), InputBuffer.size());
81   // Tell MemorySanitizer that zlib output buffer is fully initialized.
82   // This avoids a false report when running LLVM with uninstrumented ZLib.
83   __msan_unpoison(UncompressedBuffer, UncompressedSize);
84   return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
85 }
86 
uncompress(StringRef InputBuffer,SmallVectorImpl<char> & UncompressedBuffer,size_t UncompressedSize)87 Error zlib::uncompress(StringRef InputBuffer,
88                        SmallVectorImpl<char> &UncompressedBuffer,
89                        size_t UncompressedSize) {
90   UncompressedBuffer.resize(UncompressedSize);
91   Error E =
92       uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
93   UncompressedBuffer.resize(UncompressedSize);
94   return E;
95 }
96 
crc32(StringRef Buffer)97 uint32_t zlib::crc32(StringRef Buffer) {
98   return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
99 }
100 
101 #else
isAvailable()102 bool zlib::isAvailable() { return false; }
compress(StringRef InputBuffer,SmallVectorImpl<char> & CompressedBuffer,CompressionLevel Level)103 Error zlib::compress(StringRef InputBuffer,
104                      SmallVectorImpl<char> &CompressedBuffer,
105                      CompressionLevel Level) {
106   llvm_unreachable("zlib::compress is unavailable");
107 }
uncompress(StringRef InputBuffer,char * UncompressedBuffer,size_t & UncompressedSize)108 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
109                        size_t &UncompressedSize) {
110   llvm_unreachable("zlib::uncompress is unavailable");
111 }
uncompress(StringRef InputBuffer,SmallVectorImpl<char> & UncompressedBuffer,size_t UncompressedSize)112 Error zlib::uncompress(StringRef InputBuffer,
113                        SmallVectorImpl<char> &UncompressedBuffer,
114                        size_t UncompressedSize) {
115   llvm_unreachable("zlib::uncompress is unavailable");
116 }
crc32(StringRef Buffer)117 uint32_t zlib::crc32(StringRef Buffer) {
118   llvm_unreachable("zlib::crc32 is unavailable");
119 }
120 #endif
121 
122