Yann Collet | e9dc204 | 2017-08-31 11:24:54 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
Nick Terrell | c932520 | 2016-09-01 15:22:19 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
Yann Collet | e9dc204 | 2017-08-31 11:24:54 -0700 | [diff] [blame] | 5 | * 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 Terrell | c932520 | 2016-09-01 15:22:19 -0700 | [diff] [blame] | 8 | */ |
| 9 | #include "SkippableFrame.h" |
Nick Terrell | 1e2f6a1 | 2016-09-02 12:23:49 -0700 | [diff] [blame] | 10 | #include "mem.h" |
Nick Terrell | c932520 | 2016-09-01 15:22:19 -0700 | [diff] [blame] | 11 | #include "utils/Range.h" |
| 12 | |
| 13 | #include <cstdio> |
| 14 | |
| 15 | using namespace pzstd; |
| 16 | |
| 17 | SkippableFrame::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 | } |