blob: 97d5ffaadf8273dd42955fb570282253c1027e3f [file] [log] [blame]
Alexey Samsonov2fb337e2013-04-23 08:28:39 +00001//===--- Compression.cpp - Compression implementation ---------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Samsonov2fb337e2013-04-23 08:28:39 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements compression functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/Compression.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000014#include "llvm/ADT/SmallVector.h"
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000015#include "llvm/ADT/StringRef.h"
16#include "llvm/Config/config.h"
Alexey Samsonov0c9f1bf2013-04-23 12:17:46 +000017#include "llvm/Support/Compiler.h"
George Rimar167ca4a2017-01-17 15:45:07 +000018#include "llvm/Support/Error.h"
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000019#include "llvm/Support/ErrorHandling.h"
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000020#if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
21#include <zlib.h>
22#endif
23
24using namespace llvm;
25
Alexey Samsonov28acf052013-04-23 08:57:30 +000026#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
George Rimar167ca4a2017-01-17 15:45:07 +000027static Error createError(StringRef Err) {
28 return make_error<StringError>(Err, inconvertibleErrorCode());
29}
30
George Rimar167ca4a2017-01-17 15:45:07 +000031static StringRef convertZlibCodeToString(int Code) {
32 switch (Code) {
33 case Z_MEM_ERROR:
34 return "zlib error: Z_MEM_ERROR";
35 case Z_BUF_ERROR:
36 return "zlib error: Z_BUF_ERROR";
37 case Z_STREAM_ERROR:
38 return "zlib error: Z_STREAM_ERROR";
39 case Z_DATA_ERROR:
40 return "zlib error: Z_DATA_ERROR";
41 case Z_OK:
42 default:
43 llvm_unreachable("unknown or unexpected zlib status code");
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000044 }
45}
46
47bool zlib::isAvailable() { return true; }
George Rimar167ca4a2017-01-17 15:45:07 +000048
49Error zlib::compress(StringRef InputBuffer,
Rui Ueyama2c97adc2018-08-04 00:13:13 +000050 SmallVectorImpl<char> &CompressedBuffer, int Level) {
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000051 unsigned long CompressedSize = ::compressBound(InputBuffer.size());
Fangrui Song23310a82018-08-03 19:37:49 +000052 CompressedBuffer.reserve(CompressedSize);
Rui Ueyama2c97adc2018-08-04 00:13:13 +000053 int Res =
54 ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
55 (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level);
Evgeniy Stepanov28cacae2014-11-25 15:24:07 +000056 // Tell MemorySanitizer that zlib output buffer is fully initialized.
57 // This avoids a false report when running LLVM with uninstrumented ZLib.
58 __msan_unpoison(CompressedBuffer.data(), CompressedSize);
Fangrui Song23310a82018-08-03 19:37:49 +000059 CompressedBuffer.set_size(CompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000060 return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000061}
62
George Rimar167ca4a2017-01-17 15:45:07 +000063Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
64 size_t &UncompressedSize) {
65 int Res =
Rafael Espindola46cdcb02016-09-09 19:32:36 +000066 ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
George Rimar167ca4a2017-01-17 15:45:07 +000067 (const Bytef *)InputBuffer.data(), InputBuffer.size());
Rafael Espindola46cdcb02016-09-09 19:32:36 +000068 // Tell MemorySanitizer that zlib output buffer is fully initialized.
69 // This avoids a false report when running LLVM with uninstrumented ZLib.
70 __msan_unpoison(UncompressedBuffer, UncompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000071 return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
Rafael Espindola46cdcb02016-09-09 19:32:36 +000072}
73
George Rimar167ca4a2017-01-17 15:45:07 +000074Error zlib::uncompress(StringRef InputBuffer,
75 SmallVectorImpl<char> &UncompressedBuffer,
76 size_t UncompressedSize) {
David Blaikiea505f242014-04-05 21:26:44 +000077 UncompressedBuffer.resize(UncompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000078 Error E =
Rafael Espindola46cdcb02016-09-09 19:32:36 +000079 uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
David Blaikiea505f242014-04-05 21:26:44 +000080 UncompressedBuffer.resize(UncompressedSize);
George Rimar167ca4a2017-01-17 15:45:07 +000081 return E;
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000082}
83
Alexey Samsonov6ede7062013-08-14 16:03:29 +000084uint32_t zlib::crc32(StringRef Buffer) {
85 return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
86}
87
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000088#else
89bool zlib::isAvailable() { return false; }
George Rimar167ca4a2017-01-17 15:45:07 +000090Error zlib::compress(StringRef InputBuffer,
Rui Ueyamae9798f72018-08-04 00:23:37 +000091 SmallVectorImpl<char> &CompressedBuffer, int Level) {
George Rimar167ca4a2017-01-17 15:45:07 +000092 llvm_unreachable("zlib::compress is unavailable");
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000093}
George Rimar167ca4a2017-01-17 15:45:07 +000094Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
95 size_t &UncompressedSize) {
96 llvm_unreachable("zlib::uncompress is unavailable");
Rafael Espindola74941232016-09-12 13:00:51 +000097}
George Rimar167ca4a2017-01-17 15:45:07 +000098Error zlib::uncompress(StringRef InputBuffer,
99 SmallVectorImpl<char> &UncompressedBuffer,
100 size_t UncompressedSize) {
101 llvm_unreachable("zlib::uncompress is unavailable");
Alexey Samsonov2fb337e2013-04-23 08:28:39 +0000102}
Alexey Samsonov6ede7062013-08-14 16:03:29 +0000103uint32_t zlib::crc32(StringRef Buffer) {
104 llvm_unreachable("zlib::crc32 is unavailable");
105}
Alexey Samsonov2fb337e2013-04-23 08:28:39 +0000106#endif