1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
| //===-- LZMA.cpp ------------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Host/Config.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#if LLDB_ENABLE_LZMA
#include <lzma.h>
#endif // LLDB_ENABLE_LZMA
namespace lldb_private {
namespace lzma {
#if !LLDB_ENABLE_LZMA
bool isAvailable() { return false; }
llvm::Expected<uint64_t>
getUncompressedSize(llvm::ArrayRef<uint8_t> InputBuffer) {
llvm_unreachable("lzma::getUncompressedSize is unavailable");
}
llvm::Error uncompress(llvm::ArrayRef<uint8_t> InputBuffer,
llvm::SmallVectorImpl<uint8_t> &Uncompressed) {
llvm_unreachable("lzma::uncompress is unavailable");
}
#else // LLDB_ENABLE_LZMA
bool isAvailable() { return true; }
static const char *convertLZMACodeToString(lzma_ret Code) {
switch (Code) {
case LZMA_STREAM_END:
return "lzma error: LZMA_STREAM_END";
case LZMA_NO_CHECK:
return "lzma error: LZMA_NO_CHECK";
case LZMA_UNSUPPORTED_CHECK:
return "lzma error: LZMA_UNSUPPORTED_CHECK";
case LZMA_GET_CHECK:
return "lzma error: LZMA_GET_CHECK";
case LZMA_MEM_ERROR:
return "lzma error: LZMA_MEM_ERROR";
case LZMA_MEMLIMIT_ERROR:
return "lzma error: LZMA_MEMLIMIT_ERROR";
case LZMA_FORMAT_ERROR:
return "lzma error: LZMA_FORMAT_ERROR";
case LZMA_OPTIONS_ERROR:
return "lzma error: LZMA_OPTIONS_ERROR";
case LZMA_DATA_ERROR:
return "lzma error: LZMA_DATA_ERROR";
case LZMA_BUF_ERROR:
return "lzma error: LZMA_BUF_ERROR";
case LZMA_PROG_ERROR:
return "lzma error: LZMA_PROG_ERROR";
default:
llvm_unreachable("unknown or unexpected lzma status code");
}
}
llvm::Expected<uint64_t>
getUncompressedSize(llvm::ArrayRef<uint8_t> InputBuffer) {
lzma_stream_flags opts{};
if (InputBuffer.size() < LZMA_STREAM_HEADER_SIZE) {
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"size of xz-compressed blob (%lu bytes) is smaller than the "
"LZMA_STREAM_HEADER_SIZE (%lu bytes)",
InputBuffer.size(), LZMA_STREAM_HEADER_SIZE);
}
// Decode xz footer.
lzma_ret xzerr = lzma_stream_footer_decode(
&opts, InputBuffer.take_back(LZMA_STREAM_HEADER_SIZE).data());
if (xzerr != LZMA_OK) {
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"lzma_stream_footer_decode()=%s",
convertLZMACodeToString(xzerr));
}
if (InputBuffer.size() < (opts.backward_size + LZMA_STREAM_HEADER_SIZE)) {
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"xz-compressed buffer size (%lu bytes) too small (required at "
"least %lu bytes) ",
InputBuffer.size(), (opts.backward_size + LZMA_STREAM_HEADER_SIZE));
}
// Decode xz index.
lzma_index *xzindex;
uint64_t memlimit(UINT64_MAX);
size_t inpos = 0;
xzerr = lzma_index_buffer_decode(
&xzindex, &memlimit, nullptr,
InputBuffer.take_back(LZMA_STREAM_HEADER_SIZE + opts.backward_size)
.data(),
&inpos, InputBuffer.size());
if (xzerr != LZMA_OK) {
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"lzma_index_buffer_decode()=%s",
convertLZMACodeToString(xzerr));
}
// Get size of uncompressed file to construct an in-memory buffer of the
// same size on the calling end (if needed).
uint64_t uncompressedSize = lzma_index_uncompressed_size(xzindex);
// Deallocate xz index as it is no longer needed.
lzma_index_end(xzindex, nullptr);
return uncompressedSize;
}
llvm::Error uncompress(llvm::ArrayRef<uint8_t> InputBuffer,
llvm::SmallVectorImpl<uint8_t> &Uncompressed) {
llvm::Expected<uint64_t> uncompressedSize = getUncompressedSize(InputBuffer);
if (auto err = uncompressedSize.takeError())
return err;
Uncompressed.resize(*uncompressedSize);
// Decompress xz buffer to buffer.
uint64_t memlimit = UINT64_MAX;
size_t inpos = 0;
size_t outpos = 0;
lzma_ret ret = lzma_stream_buffer_decode(
&memlimit, 0, nullptr, InputBuffer.data(), &inpos, InputBuffer.size(),
Uncompressed.data(), &outpos, Uncompressed.size());
if (ret != LZMA_OK) {
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"lzma_stream_buffer_decode()=%s",
convertLZMACodeToString(ret));
}
return llvm::Error::success();
}
#endif // LLDB_ENABLE_LZMA
} // end of namespace lzma
} // namespace lldb_private
|