• 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/ErrorHandling.h"
20 #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
21 #include <zlib.h>
22 #endif
23 
24 using namespace llvm;
25 
26 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
encodeZlibCompressionLevel(zlib::CompressionLevel Level)27 static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) {
28   switch (Level) {
29     case zlib::NoCompression: return 0;
30     case zlib::BestSpeedCompression: return 1;
31     case zlib::DefaultCompression: return Z_DEFAULT_COMPRESSION;
32     case zlib::BestSizeCompression: return 9;
33   }
34   llvm_unreachable("Invalid zlib::CompressionLevel!");
35 }
36 
encodeZlibReturnValue(int ReturnValue)37 static zlib::Status encodeZlibReturnValue(int ReturnValue) {
38   switch (ReturnValue) {
39     case Z_OK: return zlib::StatusOK;
40     case Z_MEM_ERROR: return zlib::StatusOutOfMemory;
41     case Z_BUF_ERROR: return zlib::StatusBufferTooShort;
42     case Z_STREAM_ERROR: return zlib::StatusInvalidArg;
43     case Z_DATA_ERROR: return zlib::StatusInvalidData;
44     default: llvm_unreachable("unknown zlib return status!");
45   }
46 }
47 
isAvailable()48 bool zlib::isAvailable() { return true; }
compress(StringRef InputBuffer,SmallVectorImpl<char> & CompressedBuffer,CompressionLevel Level)49 zlib::Status zlib::compress(StringRef InputBuffer,
50                             SmallVectorImpl<char> &CompressedBuffer,
51                             CompressionLevel Level) {
52   unsigned long CompressedSize = ::compressBound(InputBuffer.size());
53   CompressedBuffer.resize(CompressedSize);
54   int CLevel = encodeZlibCompressionLevel(Level);
55   Status Res = encodeZlibReturnValue(::compress2(
56       (Bytef *)CompressedBuffer.data(), &CompressedSize,
57       (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
58   // Tell MemorySanitizer that zlib output buffer is fully initialized.
59   // This avoids a false report when running LLVM with uninstrumented ZLib.
60   __msan_unpoison(CompressedBuffer.data(), CompressedSize);
61   CompressedBuffer.resize(CompressedSize);
62   return Res;
63 }
64 
uncompress(StringRef InputBuffer,SmallVectorImpl<char> & UncompressedBuffer,size_t UncompressedSize)65 zlib::Status zlib::uncompress(StringRef InputBuffer,
66                               SmallVectorImpl<char> &UncompressedBuffer,
67                               size_t UncompressedSize) {
68   UncompressedBuffer.resize(UncompressedSize);
69   Status Res = encodeZlibReturnValue(::uncompress(
70       (Bytef *)UncompressedBuffer.data(), (uLongf *)&UncompressedSize,
71       (const Bytef *)InputBuffer.data(), InputBuffer.size()));
72   // Tell MemorySanitizer that zlib output buffer is fully initialized.
73   // This avoids a false report when running LLVM with uninstrumented ZLib.
74   __msan_unpoison(UncompressedBuffer.data(), UncompressedSize);
75   UncompressedBuffer.resize(UncompressedSize);
76   return Res;
77 }
78 
crc32(StringRef Buffer)79 uint32_t zlib::crc32(StringRef Buffer) {
80   return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
81 }
82 
83 #else
isAvailable()84 bool zlib::isAvailable() { return false; }
compress(StringRef InputBuffer,SmallVectorImpl<char> & CompressedBuffer,CompressionLevel Level)85 zlib::Status zlib::compress(StringRef InputBuffer,
86                             SmallVectorImpl<char> &CompressedBuffer,
87                             CompressionLevel Level) {
88   return zlib::StatusUnsupported;
89 }
uncompress(StringRef InputBuffer,SmallVectorImpl<char> & UncompressedBuffer,size_t UncompressedSize)90 zlib::Status zlib::uncompress(StringRef InputBuffer,
91                               SmallVectorImpl<char> &UncompressedBuffer,
92                               size_t UncompressedSize) {
93   return zlib::StatusUnsupported;
94 }
crc32(StringRef Buffer)95 uint32_t zlib::crc32(StringRef Buffer) {
96   llvm_unreachable("zlib::crc32 is unavailable");
97 }
98 #endif
99 
100