1 //===--- Compression.cpp - Compression implementation ---------------------===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements compression functions. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Support/Compression.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/Config/config.h" 17 #include "llvm/Support/Compiler.h" 18 #include "llvm/Support/Error.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #if LLVM_ENABLE_ZLIB 21 #include <zlib.h> 22 #endif 23 24 using namespace llvm; 25 using namespace llvm::compression; 26 27 #if LLVM_ENABLE_ZLIB 28 29 static StringRef convertZlibCodeToString(int Code) { 30 switch (Code) { 31 case Z_MEM_ERROR: 32 return "zlib error: Z_MEM_ERROR"; 33 case Z_BUF_ERROR: 34 return "zlib error: Z_BUF_ERROR"; 35 case Z_STREAM_ERROR: 36 return "zlib error: Z_STREAM_ERROR"; 37 case Z_DATA_ERROR: 38 return "zlib error: Z_DATA_ERROR"; 39 case Z_OK: 40 default: 41 llvm_unreachable("unknown or unexpected zlib status code"); 42 } 43 } 44 45 bool zlib::isAvailable() { return true; } 46 47 void zlib::compress(StringRef InputBuffer, 48 SmallVectorImpl<char> &CompressedBuffer, int Level) { 49 unsigned long CompressedSize = ::compressBound(InputBuffer.size()); 50 CompressedBuffer.resize_for_overwrite(CompressedSize); 51 int Res = 52 ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize, 53 (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level); 54 if (Res == Z_MEM_ERROR) 55 report_bad_alloc_error("Allocation failed"); 56 assert(Res == Z_OK); 57 // 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); 60 CompressedBuffer.truncate(CompressedSize); 61 } 62 63 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer, 64 size_t &UncompressedSize) { 65 int Res = 66 ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize, 67 (const Bytef *)InputBuffer.data(), InputBuffer.size()); 68 // 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); 71 return Res ? make_error<StringError>(convertZlibCodeToString(Res), 72 inconvertibleErrorCode()) 73 : Error::success(); 74 } 75 76 Error zlib::uncompress(StringRef InputBuffer, 77 SmallVectorImpl<char> &UncompressedBuffer, 78 size_t UncompressedSize) { 79 UncompressedBuffer.resize_for_overwrite(UncompressedSize); 80 Error E = zlib::uncompress(InputBuffer, UncompressedBuffer.data(), 81 UncompressedSize); 82 UncompressedBuffer.truncate(UncompressedSize); 83 return E; 84 } 85 86 #else 87 bool zlib::isAvailable() { return false; } 88 void zlib::compress(StringRef InputBuffer, 89 SmallVectorImpl<char> &CompressedBuffer, int Level) { 90 llvm_unreachable("zlib::compress is unavailable"); 91 } 92 Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer, 93 size_t &UncompressedSize) { 94 llvm_unreachable("zlib::uncompress is unavailable"); 95 } 96 Error zlib::uncompress(StringRef InputBuffer, 97 SmallVectorImpl<char> &UncompressedBuffer, 98 size_t UncompressedSize) { 99 llvm_unreachable("zlib::uncompress is unavailable"); 100 } 101 #endif 102