SkippableFrame.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #pragma once
  10. #include "utils/Range.h"
  11. #include <array>
  12. #include <cstddef>
  13. #include <cstdint>
  14. #include <cstdio>
  15. namespace pzstd {
  16. /**
  17. * We put a skippable frame before each frame.
  18. * It contains a skippable frame magic number, the size of the skippable frame,
  19. * and the size of the next frame.
  20. * Each skippable frame is exactly 12 bytes in little endian format.
  21. * The first 8 bytes are for compatibility with the ZSTD format.
  22. * If we have N threads, the output will look like
  23. *
  24. * [0x184D2A50|4|size1] [frame1 of size size1]
  25. * [0x184D2A50|4|size2] [frame2 of size size2]
  26. * ...
  27. * [0x184D2A50|4|sizeN] [frameN of size sizeN]
  28. *
  29. * Each sizeX is 4 bytes.
  30. *
  31. * These skippable frames should allow us to skip through the compressed file
  32. * and only load at most N pages.
  33. */
  34. class SkippableFrame {
  35. public:
  36. static constexpr std::size_t kSize = 12;
  37. private:
  38. std::uint32_t frameSize_;
  39. std::array<std::uint8_t, kSize> data_;
  40. static constexpr std::uint32_t kSkippableFrameMagicNumber = 0x184D2A50;
  41. // Could be improved if the size fits in less bytes
  42. static constexpr std::uint32_t kFrameContentsSize = kSize - 8;
  43. public:
  44. // Write the skippable frame to data_ in LE format.
  45. explicit SkippableFrame(std::uint32_t size);
  46. // Read the skippable frame from bytes in LE format.
  47. static std::size_t tryRead(ByteRange bytes);
  48. ByteRange data() const {
  49. return {data_.data(), data_.size()};
  50. }
  51. // Size of the next frame.
  52. std::size_t frameSize() const {
  53. return frameSize_;
  54. }
  55. };
  56. }