SkippableFrame.cpp 962 B

123456789101112131415161718192021222324252627282930
  1. /*
  2. * Copyright (c) 2016-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  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).
  8. */
  9. #include "SkippableFrame.h"
  10. #include "mem.h"
  11. #include "utils/Range.h"
  12. #include <cstdio>
  13. using namespace pzstd;
  14. SkippableFrame::SkippableFrame(std::uint32_t size) : frameSize_(size) {
  15. MEM_writeLE32(data_.data(), kSkippableFrameMagicNumber);
  16. MEM_writeLE32(data_.data() + 4, kFrameContentsSize);
  17. MEM_writeLE32(data_.data() + 8, frameSize_);
  18. }
  19. /* static */ std::size_t SkippableFrame::tryRead(ByteRange bytes) {
  20. if (bytes.size() < SkippableFrame::kSize ||
  21. MEM_readLE32(bytes.begin()) != kSkippableFrameMagicNumber ||
  22. MEM_readLE32(bytes.begin() + 4) != kFrameContentsSize) {
  23. return 0;
  24. }
  25. return MEM_readLE32(bytes.begin() + 8);
  26. }