blob: 300abb652c4fc3ca32213e51609f2b04e5822a51 [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <lib/zbitl/decompress.h>
#include <zircon/assert.h>
#include <functional>
#include <zstd/zstd.h>
namespace zbitl::decompress {
using namespace std::literals;
static_assert(kReadMinimum >= ZSTD_FRAMEHEADERSIZE_MAX);
size_t OneShot::GetScratchSize() {
// zstd requires its buffer to be aligned to 8 bytes.
static_assert(__STDCPP_DEFAULT_NEW_ALIGNMENT__ >= 8);
return ZSTD_estimateDCtxSize();
OneShot::Context* OneShot::Init(void* scratch_space, size_t size) {
return reinterpret_cast<Context*>(ZSTD_initStaticDCtx(scratch_space, size));
fitx::result<std::string_view> OneShot::DecompressImpl(Context* ctx, cpp20::span<std::byte> out,
ByteView in) {
// All-in-one mode. This will be the only call made.
auto dctx = reinterpret_cast<ZSTD_DCtx*>(ctx);
size_t result = ZSTD_decompressDCtx(dctx,, out.size(),, in.size());
if (ZSTD_isError(result)) {
return fitx::error{std::string_view{ZSTD_getErrorName(result)}};
ZX_ASSERT_MSG(result <= out.size(), "ZSTD_decompressDCtx wrote %zu into a buffer of %zu", result,
if (result != out.size()) {
return fitx::error{"decompression produced too little data"sv};
return fitx::ok();
} // namespace zbitl::decompress