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