You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.9 KiB

  1. // Copyright 2011 The Snappy-Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package snappy
  5. import (
  6. "encoding/binary"
  7. )
  8. // We limit how far copy back-references can go, the same as the C++ code.
  9. const maxOffset = 1 << 15
  10. // emitLiteral writes a literal chunk and returns the number of bytes written.
  11. func emitLiteral(dst, lit []byte) int {
  12. i, n := 0, uint(len(lit)-1)
  13. switch {
  14. case n < 60:
  15. dst[0] = uint8(n)<<2 | tagLiteral
  16. i = 1
  17. case n < 1<<8:
  18. dst[0] = 60<<2 | tagLiteral
  19. dst[1] = uint8(n)
  20. i = 2
  21. case n < 1<<16:
  22. dst[0] = 61<<2 | tagLiteral
  23. dst[1] = uint8(n)
  24. dst[2] = uint8(n >> 8)
  25. i = 3
  26. case n < 1<<24:
  27. dst[0] = 62<<2 | tagLiteral
  28. dst[1] = uint8(n)
  29. dst[2] = uint8(n >> 8)
  30. dst[3] = uint8(n >> 16)
  31. i = 4
  32. case int64(n) < 1<<32:
  33. dst[0] = 63<<2 | tagLiteral
  34. dst[1] = uint8(n)
  35. dst[2] = uint8(n >> 8)
  36. dst[3] = uint8(n >> 16)
  37. dst[4] = uint8(n >> 24)
  38. i = 5
  39. default:
  40. panic("snappy: source buffer is too long")
  41. }
  42. if copy(dst[i:], lit) != len(lit) {
  43. panic("snappy: destination buffer is too short")
  44. }
  45. return i + len(lit)
  46. }
  47. // emitCopy writes a copy chunk and returns the number of bytes written.
  48. func emitCopy(dst []byte, offset, length int) int {
  49. i := 0
  50. for length > 0 {
  51. x := length - 4
  52. if 0 <= x && x < 1<<3 && offset < 1<<11 {
  53. dst[i+0] = uint8(offset>>8)&0x07<<5 | uint8(x)<<2 | tagCopy1
  54. dst[i+1] = uint8(offset)
  55. i += 2
  56. break
  57. }
  58. x = length
  59. if x > 1<<6 {
  60. x = 1 << 6
  61. }
  62. dst[i+0] = uint8(x-1)<<2 | tagCopy2
  63. dst[i+1] = uint8(offset)
  64. dst[i+2] = uint8(offset >> 8)
  65. i += 3
  66. length -= x
  67. }
  68. return i
  69. }
  70. // Encode returns the encoded form of src. The returned slice may be a sub-
  71. // slice of dst if dst was large enough to hold the entire encoded block.
  72. // Otherwise, a newly allocated slice will be returned.
  73. // It is valid to pass a nil dst.
  74. func Encode(dst, src []byte) ([]byte, error) {
  75. if n := MaxEncodedLen(len(src)); len(dst) < n {
  76. dst = make([]byte, n)
  77. }
  78. // The block starts with the varint-encoded length of the decompressed bytes.
  79. d := binary.PutUvarint(dst, uint64(len(src)))
  80. // Return early if src is short.
  81. if len(src) <= 4 {
  82. if len(src) != 0 {
  83. d += emitLiteral(dst[d:], src)
  84. }
  85. return dst[:d], nil
  86. }
  87. // Initialize the hash table. Its size ranges from 1<<8 to 1<<14 inclusive.
  88. const maxTableSize = 1 << 14
  89. shift, tableSize := uint(32-8), 1<<8
  90. for tableSize < maxTableSize && tableSize < len(src) {
  91. shift--
  92. tableSize *= 2
  93. }
  94. var table [maxTableSize]int
  95. // Iterate over the source bytes.
  96. var (
  97. s int // The iterator position.
  98. t int // The last position with the same hash as s.
  99. lit int // The start position of any pending literal bytes.
  100. )
  101. for s+3 < len(src) {
  102. // Update the hash table.
  103. b0, b1, b2, b3 := src[s], src[s+1], src[s+2], src[s+3]
  104. h := uint32(b0) | uint32(b1)<<8 | uint32(b2)<<16 | uint32(b3)<<24
  105. p := &table[(h*0x1e35a7bd)>>shift]
  106. // We need to to store values in [-1, inf) in table. To save
  107. // some initialization time, (re)use the table's zero value
  108. // and shift the values against this zero: add 1 on writes,
  109. // subtract 1 on reads.
  110. t, *p = *p-1, s+1
  111. // If t is invalid or src[s:s+4] differs from src[t:t+4], accumulate a literal byte.
  112. if t < 0 || s-t >= maxOffset || b0 != src[t] || b1 != src[t+1] || b2 != src[t+2] || b3 != src[t+3] {
  113. s++
  114. continue
  115. }
  116. // Otherwise, we have a match. First, emit any pending literal bytes.
  117. if lit != s {
  118. d += emitLiteral(dst[d:], src[lit:s])
  119. }
  120. // Extend the match to be as long as possible.
  121. s0 := s
  122. s, t = s+4, t+4
  123. for s < len(src) && src[s] == src[t] {
  124. s++
  125. t++
  126. }
  127. // Emit the copied bytes.
  128. d += emitCopy(dst[d:], s-t, s-s0)
  129. lit = s
  130. }
  131. // Emit any final pending literal bytes and return.
  132. if lit != len(src) {
  133. d += emitLiteral(dst[d:], src[lit:])
  134. }
  135. return dst[:d], nil
  136. }
  137. // MaxEncodedLen returns the maximum length of a snappy block, given its
  138. // uncompressed length.
  139. func MaxEncodedLen(srcLen int) int {
  140. // Compressed data can be defined as:
  141. // compressed := item* literal*
  142. // item := literal* copy
  143. //
  144. // The trailing literal sequence has a space blowup of at most 62/60
  145. // since a literal of length 60 needs one tag byte + one extra byte
  146. // for length information.
  147. //
  148. // Item blowup is trickier to measure. Suppose the "copy" op copies
  149. // 4 bytes of data. Because of a special check in the encoding code,
  150. // we produce a 4-byte copy only if the offset is < 65536. Therefore
  151. // the copy op takes 3 bytes to encode, and this type of item leads
  152. // to at most the 62/60 blowup for representing literals.
  153. //
  154. // Suppose the "copy" op copies 5 bytes of data. If the offset is big
  155. // enough, it will take 5 bytes to encode the copy op. Therefore the
  156. // worst case here is a one-byte literal followed by a five-byte copy.
  157. // That is, 6 bytes of input turn into 7 bytes of "compressed" data.
  158. //
  159. // This last factor dominates the blowup, so the final estimate is:
  160. return 32 + srcLen + srcLen/6
  161. }