blob: 3bea4eb65bfd07de54ae23b942812559311313d8 [file] [log] [blame]
Yann Collete9dc2042017-08-31 11:24:54 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Nick Terrellc9325202016-09-01 15:22:19 -07003 * All rights reserved.
4 *
Yann Collete9dc2042017-08-31 11:24:54 -07005 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
Nick Terrellc9325202016-09-01 15:22:19 -07008 */
9#include "SkippableFrame.h"
Nick Terrell1e2f6a12016-09-02 12:23:49 -070010#include "mem.h"
Nick Terrellc9325202016-09-01 15:22:19 -070011#include "utils/Range.h"
12
13#include <cstdio>
14
15using namespace pzstd;
16
17SkippableFrame::SkippableFrame(std::uint32_t size) : frameSize_(size) {
18 MEM_writeLE32(data_.data(), kSkippableFrameMagicNumber);
19 MEM_writeLE32(data_.data() + 4, kFrameContentsSize);
20 MEM_writeLE32(data_.data() + 8, frameSize_);
21}
22
23/* static */ std::size_t SkippableFrame::tryRead(ByteRange bytes) {
24 if (bytes.size() < SkippableFrame::kSize ||
25 MEM_readLE32(bytes.begin()) != kSkippableFrameMagicNumber ||
26 MEM_readLE32(bytes.begin() + 4) != kFrameContentsSize) {
27 return 0;
28 }
29 return MEM_readLE32(bytes.begin() + 8);
30}