Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/Compression.cpp
Line
Count
Source (jump to first uncovered line)
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 == 1 && HAVE_ZLIB_H
21
#include <zlib.h>
22
#endif
23
24
using namespace llvm;
25
26
#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
27
10
static Error createError(StringRef Err) {
28
10
  return make_error<StringError>(Err, inconvertibleErrorCode());
29
10
}
30
31
10
static StringRef convertZlibCodeToString(int Code) {
32
10
  switch (Code) {
33
10
  case Z_MEM_ERROR:
34
0
    return "zlib error: Z_MEM_ERROR";
35
10
  case Z_BUF_ERROR:
36
8
    return "zlib error: Z_BUF_ERROR";
37
10
  case Z_STREAM_ERROR:
38
0
    return "zlib error: Z_STREAM_ERROR";
39
10
  case Z_DATA_ERROR:
40
2
    return "zlib error: Z_DATA_ERROR";
41
10
  case Z_OK:
42
0
  default:
43
0
    llvm_unreachable("unknown or unexpected zlib status code");
44
10
  }
45
10
}
46
47
12.5k
bool zlib::isAvailable() { return true; }
48
49
Error zlib::compress(StringRef InputBuffer,
50
5.61k
                     SmallVectorImpl<char> &CompressedBuffer, int Level) {
51
5.61k
  unsigned long CompressedSize = ::compressBound(InputBuffer.size());
52
5.61k
  CompressedBuffer.reserve(CompressedSize);
53
5.61k
  int Res =
54
5.61k
      ::compress2((Bytef *)CompressedBuffer.data(), &CompressedSize,
55
5.61k
                  (const Bytef *)InputBuffer.data(), InputBuffer.size(), Level);
56
5.61k
  // Tell MemorySanitizer that zlib output buffer is fully initialized.
57
5.61k
  // This avoids a false report when running LLVM with uninstrumented ZLib.
58
5.61k
  __msan_unpoison(CompressedBuffer.data(), CompressedSize);
59
5.61k
  CompressedBuffer.set_size(CompressedSize);
60
5.61k
  return Res ? 
createError(convertZlibCodeToString(Res))0
: Error::success();
61
5.61k
}
62
63
Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
64
6.96k
                       size_t &UncompressedSize) {
65
6.96k
  int Res =
66
6.96k
      ::uncompress((Bytef *)UncompressedBuffer, (uLongf *)&UncompressedSize,
67
6.96k
                   (const Bytef *)InputBuffer.data(), InputBuffer.size());
68
6.96k
  // Tell MemorySanitizer that zlib output buffer is fully initialized.
69
6.96k
  // This avoids a false report when running LLVM with uninstrumented ZLib.
70
6.96k
  __msan_unpoison(UncompressedBuffer, UncompressedSize);
71
6.96k
  return Res ? 
createError(convertZlibCodeToString(Res))10
:
Error::success()6.95k
;
72
6.96k
}
73
74
Error zlib::uncompress(StringRef InputBuffer,
75
                       SmallVectorImpl<char> &UncompressedBuffer,
76
6.91k
                       size_t UncompressedSize) {
77
6.91k
  UncompressedBuffer.resize(UncompressedSize);
78
6.91k
  Error E =
79
6.91k
      uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize);
80
6.91k
  UncompressedBuffer.resize(UncompressedSize);
81
6.91k
  return E;
82
6.91k
}
83
84
1
uint32_t zlib::crc32(StringRef Buffer) {
85
1
  return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
86
1
}
87
88
#else
89
bool zlib::isAvailable() { return false; }
90
Error zlib::compress(StringRef InputBuffer,
91
                     SmallVectorImpl<char> &CompressedBuffer, int Level) {
92
  llvm_unreachable("zlib::compress is unavailable");
93
}
94
Error zlib::uncompress(StringRef InputBuffer, char *UncompressedBuffer,
95
                       size_t &UncompressedSize) {
96
  llvm_unreachable("zlib::uncompress is unavailable");
97
}
98
Error zlib::uncompress(StringRef InputBuffer,
99
                       SmallVectorImpl<char> &UncompressedBuffer,
100
                       size_t UncompressedSize) {
101
  llvm_unreachable("zlib::uncompress is unavailable");
102
}
103
uint32_t zlib::crc32(StringRef Buffer) {
104
  llvm_unreachable("zlib::crc32 is unavailable");
105
}
106
#endif