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/OwningPtr.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 #include "llvm/Support/MemoryBuffer.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
encodeZlibCompressionLevel(zlib::CompressionLevel Level)28 static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) {
29 switch (Level) {
30 case zlib::NoCompression: return 0;
31 case zlib::BestSpeedCompression: return 1;
32 case zlib::DefaultCompression: return Z_DEFAULT_COMPRESSION;
33 case zlib::BestSizeCompression: return 9;
34 }
35 llvm_unreachable("Invalid zlib::CompressionLevel!");
36 }
37
encodeZlibReturnValue(int ReturnValue)38 static zlib::Status encodeZlibReturnValue(int ReturnValue) {
39 switch (ReturnValue) {
40 case Z_OK: return zlib::StatusOK;
41 case Z_MEM_ERROR: return zlib::StatusOutOfMemory;
42 case Z_BUF_ERROR: return zlib::StatusBufferTooShort;
43 case Z_STREAM_ERROR: return zlib::StatusInvalidArg;
44 case Z_DATA_ERROR: return zlib::StatusInvalidData;
45 default: llvm_unreachable("unknown zlib return status!");
46 }
47 }
48
isAvailable()49 bool zlib::isAvailable() { return true; }
compress(StringRef InputBuffer,OwningPtr<MemoryBuffer> & CompressedBuffer,CompressionLevel Level)50 zlib::Status zlib::compress(StringRef InputBuffer,
51 OwningPtr<MemoryBuffer> &CompressedBuffer,
52 CompressionLevel Level) {
53 unsigned long CompressedSize = ::compressBound(InputBuffer.size());
54 OwningArrayPtr<char> TmpBuffer(new char[CompressedSize]);
55 int CLevel = encodeZlibCompressionLevel(Level);
56 Status Res = encodeZlibReturnValue(::compress2(
57 (Bytef *)TmpBuffer.get(), &CompressedSize,
58 (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
59 if (Res == StatusOK) {
60 CompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
61 StringRef(TmpBuffer.get(), CompressedSize)));
62 // Tell MSan that memory initialized by zlib is valid.
63 __msan_unpoison(CompressedBuffer->getBufferStart(), CompressedSize);
64 }
65 return Res;
66 }
67
uncompress(StringRef InputBuffer,OwningPtr<MemoryBuffer> & UncompressedBuffer,size_t UncompressedSize)68 zlib::Status zlib::uncompress(StringRef InputBuffer,
69 OwningPtr<MemoryBuffer> &UncompressedBuffer,
70 size_t UncompressedSize) {
71 OwningArrayPtr<char> TmpBuffer(new char[UncompressedSize]);
72 Status Res = encodeZlibReturnValue(
73 ::uncompress((Bytef *)TmpBuffer.get(), (uLongf *)&UncompressedSize,
74 (const Bytef *)InputBuffer.data(), InputBuffer.size()));
75 if (Res == StatusOK) {
76 UncompressedBuffer.reset(MemoryBuffer::getMemBufferCopy(
77 StringRef(TmpBuffer.get(), UncompressedSize)));
78 // Tell MSan that memory initialized by zlib is valid.
79 __msan_unpoison(UncompressedBuffer->getBufferStart(), UncompressedSize);
80 }
81 return Res;
82 }
83
84 #else
isAvailable()85 bool zlib::isAvailable() { return false; }
compress(StringRef InputBuffer,OwningPtr<MemoryBuffer> & CompressedBuffer,CompressionLevel Level)86 zlib::Status zlib::compress(StringRef InputBuffer,
87 OwningPtr<MemoryBuffer> &CompressedBuffer,
88 CompressionLevel Level) {
89 return zlib::StatusUnsupported;
90 }
uncompress(StringRef InputBuffer,OwningPtr<MemoryBuffer> & UncompressedBuffer,size_t UncompressedSize)91 zlib::Status zlib::uncompress(StringRef InputBuffer,
92 OwningPtr<MemoryBuffer> &UncompressedBuffer,
93 size_t UncompressedSize) {
94 return zlib::StatusUnsupported;
95 }
96 #endif
97
98