|
@@ -16,6 +16,7 @@
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
#include "llvm/Config/config.h"
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
+#include "llvm/Support/Error.h"
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
#if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
|
|
|
#include <zlib.h>
|
|
@@ -24,6 +25,10 @@
|
|
|
using namespace llvm;
|
|
|
|
|
|
#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
|
|
|
+static Error createError(StringRef Err) {
|
|
|
+ return make_error<StringError>(Err, inconvertibleErrorCode());
|
|
|
+}
|
|
|
+
|
|
|
static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) {
|
|
|
switch (Level) {
|
|
|
case zlib::NoCompression: return 0;
|
|
@@ -34,53 +39,59 @@ static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) {
|
|
|
llvm_unreachable("Invalid zlib::CompressionLevel!");
|
|
|
}
|
|
|
|
|
|
-static zlib::Status encodeZlibReturnValue(int ReturnValue) {
|
|
|
- switch (ReturnValue) {
|
|
|
- case Z_OK: return zlib::StatusOK;
|
|
|
- case Z_MEM_ERROR: return zlib::StatusOutOfMemory;
|
|
|
- case Z_BUF_ERROR: return zlib::StatusBufferTooShort;
|
|
|
- case Z_STREAM_ERROR: return zlib::StatusInvalidArg;
|
|
|
- case Z_DATA_ERROR: return zlib::StatusInvalidData;
|
|
|
- default: llvm_unreachable("unknown zlib return status!");
|
|
|
+static StringRef convertZlibCodeToString(int Code) {
|
|
|
+ switch (Code) {
|
|
|
+ case Z_MEM_ERROR:
|
|
|
+ return "zlib error: Z_MEM_ERROR";
|
|
|
+ case Z_BUF_ERROR:
|
|
|
+ return "zlib error: Z_BUF_ERROR";
|
|
|
+ case Z_STREAM_ERROR:
|
|
|
+ return "zlib error: Z_STREAM_ERROR";
|
|
|
+ case Z_DATA_ERROR:
|
|
|
+ return "zlib error: Z_DATA_ERROR";
|
|
|
+ case Z_OK:
|
|
|
+ default:
|
|
|
+ llvm_unreachable("unknown or unexpected zlib status code");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool zlib::isAvailable() { return true; }
|
|
|
-zlib::Status zlib::compress(StringRef InputBuffer,
|
|
|
- SmallVectorImpl<char> &CompressedBuffer,
|
|
|
- CompressionLevel Level) {
|
|
|
+
|
|
|
+Error zlib::compress(StringRef InputBuffer,
|
|
|
+ SmallVectorImpl<char> &CompressedBuffer,
|
|
|
+ CompressionLevel Level) {
|
|
|
unsigned long CompressedSize = ::compressBound(InputBuffer.size());
|
|
|
CompressedBuffer.resize(CompressedSize);
|
|
|
int CLevel = encodeZlibCompressionLevel(Level);
|
|
|
- Status Res = encodeZlibReturnValue(::compress2(
|
|
|
- (Bytef *)CompressedBuffer.data(), &CompressedSize,
|
|
|
- (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
|
|
|
+ int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
|
|
|
+ (const Bytef *)InputBuffer.data(), InputBuffer.size(),
|
|
|
+ CLevel);
|
|
|
// Tell MemorySanitizer that zlib output buffer is fully initialized.
|
|
|
// This avoids a false report when running LLVM with uninstrumented ZLib.
|
|
|
__msan_unpoison(CompressedBuffer.data(), CompressedSize);
|
|
|
CompressedBuffer.resize(CompressedSize);
|
|
|
- return Res;
|
|
|
+ return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
|
|
|
}
|
|
|
|
|
|
-zlib::Status zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
|
|
|
- size_t &UncompressedSize) {
|
|
|
- Status Res = encodeZlibReturnValue(
|
|
|
+Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
|
|
|
+ size_t &UncompressedSize) {
|
|
|
+ int Res =
|
|
|
::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
|
|
|
- (const Bytef *)InputBuffer.data(), InputBuffer.size()));
|
|
|
+ (const Bytef *)InputBuffer.data(), InputBuffer.size());
|
|
|
// Tell MemorySanitizer that zlib output buffer is fully initialized.
|
|
|
// This avoids a false report when running LLVM with uninstrumented ZLib.
|
|
|
__msan_unpoison(UncompressedBuffer, UncompressedSize);
|
|
|
- return Res;
|
|
|
+ return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
|
|
|
}
|
|
|
|
|
|
-zlib::Status zlib::uncompress(StringRef InputBuffer,
|
|
|
- SmallVectorImpl<char> &UncompressedBuffer,
|
|
|
- size_t UncompressedSize) {
|
|
|
+Error zlib::uncompress(StringRef InputBuffer,
|
|
|
+ SmallVectorImpl<char> &UncompressedBuffer,
|
|
|
+ size_t UncompressedSize) {
|
|
|
UncompressedBuffer.resize(UncompressedSize);
|
|
|
- Status Res =
|
|
|
+ Error E =
|
|
|
uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
|
|
|
UncompressedBuffer.resize(UncompressedSize);
|
|
|
- return Res;
|
|
|
+ return E;
|
|
|
}
|
|
|
|
|
|
uint32_t zlib::crc32(StringRef Buffer) {
|
|
@@ -89,19 +100,19 @@ uint32_t zlib::crc32(StringRef Buffer) {
|
|
|
|
|
|
#else
|
|
|
bool zlib::isAvailable() { return false; }
|
|
|
-zlib::Status zlib::compress(StringRef InputBuffer,
|
|
|
- SmallVectorImpl<char> &CompressedBuffer,
|
|
|
- CompressionLevel Level) {
|
|
|
- return zlib::StatusUnsupported;
|
|
|
+Error zlib::compress(StringRef InputBuffer,
|
|
|
+ SmallVectorImpl<char> &CompressedBuffer,
|
|
|
+ CompressionLevel Level) {
|
|
|
+ llvm_unreachable("zlib::compress is unavailable");
|
|
|
}
|
|
|
-zlib::Status zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
|
|
|
- size_t &UncompressedSize) {
|
|
|
- return zlib::StatusUnsupported;
|
|
|
+Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
|
|
|
+ size_t &UncompressedSize) {
|
|
|
+ llvm_unreachable("zlib::uncompress is unavailable");
|
|
|
}
|
|
|
-zlib::Status zlib::uncompress(StringRef InputBuffer,
|
|
|
- SmallVectorImpl<char> &UncompressedBuffer,
|
|
|
- size_t UncompressedSize) {
|
|
|
- return zlib::StatusUnsupported;
|
|
|
+Error zlib::uncompress(StringRef InputBuffer,
|
|
|
+ SmallVectorImpl<char> &UncompressedBuffer,
|
|
|
+ size_t UncompressedSize) {
|
|
|
+ llvm_unreachable("zlib::uncompress is unavailable");
|
|
|
}
|
|
|
uint32_t zlib::crc32(StringRef Buffer) {
|
|
|
llvm_unreachable("zlib::crc32 is unavailable");
|