blob: 95261d4aad239b84cde6e85d9554db1e72e97d78 [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
George Rimar167ca4a2017-01-17 15:45:07 +000032static StringRef convertZlibCodeToString(int Code) {
33 switch (Code) {
34 case Z_MEM_ERROR:
35 return "zlib error: Z_MEM_ERROR";
36 case Z_BUF_ERROR:
37 return "zlib error: Z_BUF_ERROR";
38 case Z_STREAM_ERROR:
39 return "zlib error: Z_STREAM_ERROR";
40 case Z_DATA_ERROR:
41 return "zlib error: Z_DATA_ERROR";
42 case Z_OK:
43 default:
44 llvm_unreachable("unknown or unexpected zlib status code");
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000045 }
46}
47
48bool zlib::isAvailable() { return true; }
George Rimar167ca4a2017-01-17 15:45:07 +000049
50Error zlib::compress(StringRef InputBuffer,
Rui Ueyama2c97adc2018-08-04 00:13:13 +000051 SmallVectorImpl<char> &CompressedBuffer, int Level) {
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000052 unsigned long CompressedSize = ::compressBound(InputBuffer.size());
Fangrui Song23310a82018-08-03 19:37:49 +000053 CompressedBuffer.reserve(CompressedSize);
Rui Ueyama2c97adc2018-08-04 00:13:13 +000054 int Res =
55 ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
56 (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level);
Evgeniy Stepanov28cacae2014-11-25 15:24:07 +000057 // 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);
Fangrui Song23310a82018-08-03 19:37:49 +000060 CompressedBuffer.set_size(CompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000061 return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000062}
63
George Rimar167ca4a2017-01-17 15:45:07 +000064Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
65 size_t &UncompressedSize) {
66 int Res =
Rafael Espindola46cdcb02016-09-09 19:32:36 +000067 ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
George Rimar167ca4a2017-01-17 15:45:07 +000068 (const Bytef *)InputBuffer.data(), InputBuffer.size());
Rafael Espindola46cdcb02016-09-09 19:32:36 +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(UncompressedBuffer, UncompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000072 return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
Rafael Espindola46cdcb02016-09-09 19:32:36 +000073}
74
George Rimar167ca4a2017-01-17 15:45:07 +000075Error zlib::uncompress(StringRef InputBuffer,
76 SmallVectorImpl<char> &UncompressedBuffer,
77 size_t UncompressedSize) {
David Blaikiea505f242014-04-05 21:26:44 +000078 UncompressedBuffer.resize(UncompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000079 Error E =
Rafael Espindola46cdcb02016-09-09 19:32:36 +000080 uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
David Blaikiea505f242014-04-05 21:26:44 +000081 UncompressedBuffer.resize(UncompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000082 return E;
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000083}
84
Alexey Samsonov6ede7062013-08-14 16:03:29 +000085uint32_t zlib::crc32(StringRef Buffer) {
86 return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
87}
88
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000089#else
90bool zlib::isAvailable() { return false; }
George Rimar167ca4a2017-01-17 15:45:07 +000091Error zlib::compress(StringRef InputBuffer,
Rui Ueyamae9798f72018-08-04 00:23:37 +000092 SmallVectorImpl<char> &CompressedBuffer, int Level) {
George Rimar167ca4a2017-01-17 15:45:07 +000093 llvm_unreachable("zlib::compress is unavailable");
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000094}
George Rimar167ca4a2017-01-17 15:45:07 +000095Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
96 size_t &UncompressedSize) {
97 llvm_unreachable("zlib::uncompress is unavailable");
Rafael Espindola74941232016-09-12 13:00:51 +000098}
George Rimar167ca4a2017-01-17 15:45:07 +000099Error zlib::uncompress(StringRef InputBuffer,
100 SmallVectorImpl<char> &UncompressedBuffer,
101 size_t UncompressedSize) {
102 llvm_unreachable("zlib::uncompress is unavailable");
Alexey Samsonov2fb337e2013-04-23 08:28:39 +0000103}
Alexey Samsonov6ede7062013-08-14 16:03:29 +0000104uint32_t zlib::crc32(StringRef Buffer) {
105 llvm_unreachable("zlib::crc32 is unavailable");
106}
Alexey Samsonov2fb337e2013-04-23 08:28:39 +0000107#endif