s2.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2011 The Snappy-Go Authors. All rights reserved.
  2. // Copyright (c) 2019 Klaus Post. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. // Package s2 implements the S2 compression format.
  6. //
  7. // S2 is an extension of Snappy. Similar to Snappy S2 is aimed for high throughput,
  8. // which is why it features concurrent compression for bigger payloads.
  9. //
  10. // Decoding is compatible with Snappy compressed content,
  11. // but content compressed with S2 cannot be decompressed by Snappy.
  12. //
  13. // For more information on Snappy/S2 differences see README in: https://github.com/klauspost/compress/tree/master/s2
  14. //
  15. // There are actually two S2 formats: block and stream. They are related,
  16. // but different: trying to decompress block-compressed data as a S2 stream
  17. // will fail, and vice versa. The block format is the Decode and Encode
  18. // functions and the stream format is the Reader and Writer types.
  19. //
  20. // A "better" compression option is available. This will trade some compression
  21. // speed
  22. //
  23. // The block format, the more common case, is used when the complete size (the
  24. // number of bytes) of the original data is known upfront, at the time
  25. // compression starts. The stream format, also known as the framing format, is
  26. // for when that isn't always true.
  27. //
  28. // Blocks to not offer much data protection, so it is up to you to
  29. // add data validation of decompressed blocks.
  30. //
  31. // Streams perform CRC validation of the decompressed data.
  32. // Stream compression will also be performed on multiple CPU cores concurrently
  33. // significantly improving throughput.
  34. package s2
  35. import (
  36. "bytes"
  37. "hash/crc32"
  38. )
  39. /*
  40. Each encoded block begins with the varint-encoded length of the decoded data,
  41. followed by a sequence of chunks. Chunks begin and end on byte boundaries. The
  42. first byte of each chunk is broken into its 2 least and 6 most significant bits
  43. called l and m: l ranges in [0, 4) and m ranges in [0, 64). l is the chunk tag.
  44. Zero means a literal tag. All other values mean a copy tag.
  45. For literal tags:
  46. - If m < 60, the next 1 + m bytes are literal bytes.
  47. - Otherwise, let n be the little-endian unsigned integer denoted by the next
  48. m - 59 bytes. The next 1 + n bytes after that are literal bytes.
  49. For copy tags, length bytes are copied from offset bytes ago, in the style of
  50. Lempel-Ziv compression algorithms. In particular:
  51. - For l == 1, the offset ranges in [0, 1<<11) and the length in [4, 12).
  52. The length is 4 + the low 3 bits of m. The high 3 bits of m form bits 8-10
  53. of the offset. The next byte is bits 0-7 of the offset.
  54. - For l == 2, the offset ranges in [0, 1<<16) and the length in [1, 65).
  55. The length is 1 + m. The offset is the little-endian unsigned integer
  56. denoted by the next 2 bytes.
  57. - For l == 3, the offset ranges in [0, 1<<32) and the length in
  58. [1, 65). The length is 1 + m. The offset is the little-endian unsigned
  59. integer denoted by the next 4 bytes.
  60. */
  61. const (
  62. tagLiteral = 0x00
  63. tagCopy1 = 0x01
  64. tagCopy2 = 0x02
  65. tagCopy4 = 0x03
  66. )
  67. const (
  68. checksumSize = 4
  69. chunkHeaderSize = 4
  70. magicChunk = "\xff\x06\x00\x00" + magicBody
  71. magicChunkSnappy = "\xff\x06\x00\x00" + magicBodySnappy
  72. magicBodySnappy = "sNaPpY"
  73. magicBody = "S2sTwO"
  74. // maxBlockSize is the maximum size of the input to encodeBlock.
  75. //
  76. // For the framing format (Writer type instead of Encode function),
  77. // this is the maximum uncompressed size of a block.
  78. maxBlockSize = 4 << 20
  79. // minBlockSize is the minimum size of block setting when creating a writer.
  80. minBlockSize = 4 << 10
  81. skippableFrameHeader = 4
  82. maxChunkSize = 1<<24 - 1 // 16777215
  83. // Default block size
  84. defaultBlockSize = 1 << 20
  85. // maxSnappyBlockSize is the maximum snappy block size.
  86. maxSnappyBlockSize = 1 << 16
  87. obufHeaderLen = checksumSize + chunkHeaderSize
  88. )
  89. const (
  90. chunkTypeCompressedData = 0x00
  91. chunkTypeUncompressedData = 0x01
  92. ChunkTypeIndex = 0x99
  93. chunkTypePadding = 0xfe
  94. chunkTypeStreamIdentifier = 0xff
  95. )
  96. var crcTable = crc32.MakeTable(crc32.Castagnoli)
  97. // crc implements the checksum specified in section 3 of
  98. // https://github.com/google/snappy/blob/master/framing_format.txt
  99. func crc(b []byte) uint32 {
  100. c := crc32.Update(0, crcTable, b)
  101. return c>>15 | c<<17 + 0xa282ead8
  102. }
  103. // literalExtraSize returns the extra size of encoding n literals.
  104. // n should be >= 0 and <= math.MaxUint32.
  105. func literalExtraSize(n int64) int64 {
  106. if n == 0 {
  107. return 0
  108. }
  109. switch {
  110. case n < 60:
  111. return 1
  112. case n < 1<<8:
  113. return 2
  114. case n < 1<<16:
  115. return 3
  116. case n < 1<<24:
  117. return 4
  118. default:
  119. return 5
  120. }
  121. }
  122. type byter interface {
  123. Bytes() []byte
  124. }
  125. var _ byter = &bytes.Buffer{}