encode.go 5.1 KB

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