blob: c279d10f6c614b7c0285739f289b3208fe6be866 [file] [log] [blame]
Alexey Samsonov2fb337e2013-04-23 08:28:39 +00001//===--- 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"
Benjamin Kramer16132e62015-03-23 18:07:13 +000015#include "llvm/ADT/SmallVector.h"
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000016#include "llvm/ADT/StringRef.h"
17#include "llvm/Config/config.h"
Alexey Samsonov0c9f1bf2013-04-23 12:17:46 +000018#include "llvm/Support/Compiler.h"
George Rimar167ca4a2017-01-17 15:45:07 +000019#include "llvm/Support/Error.h"
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000020#include "llvm/Support/ErrorHandling.h"
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000021#if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
22#include <zlib.h>
23#endif
24
25using namespace llvm;
26
Alexey Samsonov28acf052013-04-23 08:57:30 +000027#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
George Rimar167ca4a2017-01-17 15:45:07 +000028static Error createError(StringRef Err) {
29 return make_error<StringError>(Err, inconvertibleErrorCode());
30}
31
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000032static 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 }
Hans Wennborg63761d4b2013-04-23 10:12:16 +000039 llvm_unreachable("Invalid zlib::CompressionLevel!");
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000040}
41
George Rimar167ca4a2017-01-17 15:45:07 +000042static 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");
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000055 }
56}
57
58bool zlib::isAvailable() { return true; }
George Rimar167ca4a2017-01-17 15:45:07 +000059
60Error zlib::compress(StringRef InputBuffer,
61 SmallVectorImpl<char> &CompressedBuffer,
62 CompressionLevel Level) {
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000063 unsigned long CompressedSize = ::compressBound(InputBuffer.size());
David Blaikie857497b2014-04-05 21:53:04 +000064 CompressedBuffer.resize(CompressedSize);
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000065 int CLevel = encodeZlibCompressionLevel(Level);
George Rimar167ca4a2017-01-17 15:45:07 +000066 int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
67 (const Bytef *)InputBuffer.data(), InputBuffer.size(),
68 CLevel);
Evgeniy Stepanov28cacae2014-11-25 15:24:07 +000069 // 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);
David Blaikie857497b2014-04-05 21:53:04 +000072 CompressedBuffer.resize(CompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000073 return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000074}
75
George Rimar167ca4a2017-01-17 15:45:07 +000076Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
77 size_t &UncompressedSize) {
78 int Res =
Rafael Espindola46cdcb02016-09-09 19:32:36 +000079 ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
George Rimar167ca4a2017-01-17 15:45:07 +000080 (const Bytef *)InputBuffer.data(), InputBuffer.size());
Rafael Espindola46cdcb02016-09-09 19:32:36 +000081 // 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);
George Rimar167ca4a2017-01-17 15:45:07 +000084 return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
Rafael Espindola46cdcb02016-09-09 19:32:36 +000085}
86
George Rimar167ca4a2017-01-17 15:45:07 +000087Error zlib::uncompress(StringRef InputBuffer,
88 SmallVectorImpl<char> &UncompressedBuffer,
89 size_t UncompressedSize) {
David Blaikiea505f242014-04-05 21:26:44 +000090 UncompressedBuffer.resize(UncompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000091 Error E =
Rafael Espindola46cdcb02016-09-09 19:32:36 +000092 uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
David Blaikiea505f242014-04-05 21:26:44 +000093 UncompressedBuffer.resize(UncompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000094 return E;
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000095}
96
Alexey Samsonov6ede7062013-08-14 16:03:29 +000097uint32_t zlib::crc32(StringRef Buffer) {
98 return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
99}
100
Alexey Samsonov2fb337e2013-04-23 08:28:39 +0000101#else
102bool zlib::isAvailable() { return false; }
George Rimar167ca4a2017-01-17 15:45:07 +0000103Error zlib::compress(StringRef InputBuffer,
104 SmallVectorImpl<char> &CompressedBuffer,
105 CompressionLevel Level) {
106 llvm_unreachable("zlib::compress is unavailable");
Alexey Samsonov2fb337e2013-04-23 08:28:39 +0000107}
George Rimar167ca4a2017-01-17 15:45:07 +0000108Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
109 size_t &UncompressedSize) {
110 llvm_unreachable("zlib::uncompress is unavailable");
Rafael Espindola74941232016-09-12 13:00:51 +0000111}
George Rimar167ca4a2017-01-17 15:45:07 +0000112Error zlib::uncompress(StringRef InputBuffer,
113 SmallVectorImpl<char> &UncompressedBuffer,
114 size_t UncompressedSize) {
115 llvm_unreachable("zlib::uncompress is unavailable");
Alexey Samsonov2fb337e2013-04-23 08:28:39 +0000116}
Alexey Samsonov6ede7062013-08-14 16:03:29 +0000117uint32_t zlib::crc32(StringRef Buffer) {
118 llvm_unreachable("zlib::crc32 is unavailable");
119}
Alexey Samsonov2fb337e2013-04-23 08:28:39 +0000120#endif
121