LCOV - code coverage report
Current view: top level - lib/Support - Compression.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 33 40 82.5 %
Date: 2017-09-14 15:23:50 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===--- 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"
      15             : #include "llvm/ADT/SmallVector.h"
      16             : #include "llvm/ADT/StringRef.h"
      17             : #include "llvm/Config/config.h"
      18             : #include "llvm/Support/Compiler.h"
      19             : #include "llvm/Support/Error.h"
      20             : #include "llvm/Support/ErrorHandling.h"
      21             : #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
      22             : #include <zlib.h>
      23             : #endif
      24             : 
      25             : using namespace llvm;
      26             : 
      27             : #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
      28          10 : static Error createError(StringRef Err) {
      29          20 :   return make_error<StringError>(Err, inconvertibleErrorCode());
      30             : }
      31             : 
      32             : static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) {
      33        3774 :   switch (Level) {
      34             :     case zlib::NoCompression: return 0;
      35           2 :     case zlib::BestSpeedCompression: return 1;
      36        3626 :     case zlib::DefaultCompression: return Z_DEFAULT_COMPRESSION;
      37         144 :     case zlib::BestSizeCompression: return 9;
      38             :   }
      39           0 :   llvm_unreachable("Invalid zlib::CompressionLevel!");
      40             : }
      41             : 
      42          10 : static StringRef convertZlibCodeToString(int Code) {
      43          10 :   switch (Code) {
      44           0 :   case Z_MEM_ERROR:
      45           0 :     return "zlib error: Z_MEM_ERROR";
      46           9 :   case Z_BUF_ERROR:
      47           9 :     return "zlib error: Z_BUF_ERROR";
      48           0 :   case Z_STREAM_ERROR:
      49           0 :     return "zlib error: Z_STREAM_ERROR";
      50           1 :   case Z_DATA_ERROR:
      51           1 :     return "zlib error: Z_DATA_ERROR";
      52           0 :   case Z_OK:
      53             :   default:
      54           0 :     llvm_unreachable("unknown or unexpected zlib status code");
      55             :   }
      56             : }
      57             : 
      58        8920 : bool zlib::isAvailable() { return true; }
      59             : 
      60        3774 : Error zlib::compress(StringRef InputBuffer,
      61             :                      SmallVectorImpl<char> &CompressedBuffer,
      62             :                      CompressionLevel Level) {
      63        3774 :   unsigned long CompressedSize = ::compressBound(InputBuffer.size());
      64        3774 :   CompressedBuffer.resize(CompressedSize);
      65        3774 :   int CLevel = encodeZlibCompressionLevel(Level);
      66        7548 :   int Res = ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
      67        3774 :                         (const Bytef *)InputBuffer.data(), InputBuffer.size(),
      68        3774 :                         CLevel);
      69             :   // 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);
      72        3774 :   CompressedBuffer.resize(CompressedSize);
      73       11322 :   return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
      74             : }
      75             : 
      76        5165 : Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
      77             :                        size_t &UncompressedSize) {
      78             :   int Res =
      79        5165 :       ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
      80        5165 :                    (const Bytef *)InputBuffer.data(), InputBuffer.size());
      81             :   // 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);
      84       15485 :   return Res ? createError(convertZlibCodeToString(Res)) : Error::success();
      85             : }
      86             : 
      87        5126 : Error zlib::uncompress(StringRef InputBuffer,
      88             :                        SmallVectorImpl<char> &UncompressedBuffer,
      89             :                        size_t UncompressedSize) {
      90        5126 :   UncompressedBuffer.resize(UncompressedSize);
      91             :   Error E =
      92       10252 :       uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
      93        5126 :   UncompressedBuffer.resize(UncompressedSize);
      94        5126 :   return E;
      95             : }
      96             : 
      97           4 : uint32_t zlib::crc32(StringRef Buffer) {
      98           8 :   return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
      99             : }
     100             : 
     101             : #else
     102             : bool zlib::isAvailable() { return false; }
     103             : Error zlib::compress(StringRef InputBuffer,
     104             :                      SmallVectorImpl<char> &CompressedBuffer,
     105             :                      CompressionLevel Level) {
     106             :   llvm_unreachable("zlib::compress is unavailable");
     107             : }
     108             : Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
     109             :                        size_t &UncompressedSize) {
     110             :   llvm_unreachable("zlib::uncompress is unavailable");
     111             : }
     112             : Error zlib::uncompress(StringRef InputBuffer,
     113             :                        SmallVectorImpl<char> &UncompressedBuffer,
     114             :                        size_t UncompressedSize) {
     115             :   llvm_unreachable("zlib::uncompress is unavailable");
     116             : }
     117             : uint32_t zlib::crc32(StringRef Buffer) {
     118             :   llvm_unreachable("zlib::crc32 is unavailable");
     119             : }
     120             : #endif
     121             : 

Generated by: LCOV version 1.13