void XzCompress(ArrayRef<const uint8_t> src,
std::vector<uint8_t>* dst, int level,
size_t block_size) { // Configure the compression library.
XzInitCrc();
CLzma2EncProps lzma2Props;
Lzma2EncProps_Init(&lzma2Props);
lzma2Props.lzmaProps.level = level;
lzma2Props.lzmaProps.reduceSize = src.size(); // Size of data that will be compressed.
lzma2Props.blockSize = block_size;
Lzma2EncProps_Normalize(&lzma2Props);
CXzProps props;
XzProps_Init(&props);
props.lzma2Props = lzma2Props; // Implement the required interface for communication (written in C so no virtual methods). struct XzCallbacks : public ISeqInStream, public ISeqOutStream, public ICompressProgress { static SRes ReadImpl(const ISeqInStream* p, void* buf, size_t* size) { auto* ctx = static_cast<XzCallbacks*>(const_cast<ISeqInStream*>(p));
*size = std::min(*size, ctx->src_.size() - ctx->src_pos_);
memcpy(buf, ctx->src_.data() + ctx->src_pos_, *size);
ctx->src_pos_ += *size; return SZ_OK;
} static size_t WriteImpl(const ISeqOutStream* p, constvoid* buf, size_t size) { auto* ctx = static_cast<const XzCallbacks*>(p); const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf);
ctx->dst_->insert(ctx->dst_->end(), buffer, buffer + size); return size;
} static SRes ProgressImpl(const ICompressProgress* , UInt64, UInt64) { return SZ_OK;
}
size_t src_pos_;
ArrayRef<const uint8_t> src_;
std::vector<uint8_t>* dst_;
};
XzCallbacks callbacks;
callbacks.Read = XzCallbacks::ReadImpl;
callbacks.Write = XzCallbacks::WriteImpl;
callbacks.Progress = XzCallbacks::ProgressImpl;
callbacks.src_pos_ = 0;
callbacks.src_ = src;
callbacks.dst_ = dst; // Compress.
SRes res = Xz_Encode(&callbacks, &callbacks, &props, &callbacks);
CHECK_EQ(res, SZ_OK);
// Decompress the data back and check that we get the original. if (kIsDebugBuild) {
std::vector<uint8_t> decompressed;
XzDecompress(ArrayRef<const uint8_t>(*dst), &decompressed);
DCHECK_EQ(decompressed.size(), src.size());
DCHECK_EQ(memcmp(decompressed.data(), src.data(), src.size()), 0);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.