encode_other.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2016 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. // +build !amd64 appengine !gc noasm
  5. package snappy
  6. // emitLiteral writes a literal chunk and returns the number of bytes written.
  7. //
  8. // It assumes that:
  9. // dst is long enough to hold the encoded bytes
  10. // 1 <= len(lit) && len(lit) <= 65536
  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. default:
  22. dst[0] = 61<<2 | tagLiteral
  23. dst[1] = uint8(n)
  24. dst[2] = uint8(n >> 8)
  25. i = 3
  26. }
  27. return i + copy(dst[i:], lit)
  28. }
  29. // emitCopy writes a copy chunk and returns the number of bytes written.
  30. //
  31. // It assumes that:
  32. // dst is long enough to hold the encoded bytes
  33. // 1 <= offset && offset <= 65535
  34. // 4 <= length && length <= 65535
  35. func emitCopy(dst []byte, offset, length int) int {
  36. i := 0
  37. // The maximum length for a single tagCopy1 or tagCopy2 op is 64 bytes. The
  38. // threshold for this loop is a little higher (at 68 = 64 + 4), and the
  39. // length emitted down below is is a little lower (at 60 = 64 - 4), because
  40. // it's shorter to encode a length 67 copy as a length 60 tagCopy2 followed
  41. // by a length 7 tagCopy1 (which encodes as 3+2 bytes) than to encode it as
  42. // a length 64 tagCopy2 followed by a length 3 tagCopy2 (which encodes as
  43. // 3+3 bytes). The magic 4 in the 64±4 is because the minimum length for a
  44. // tagCopy1 op is 4 bytes, which is why a length 3 copy has to be an
  45. // encodes-as-3-bytes tagCopy2 instead of an encodes-as-2-bytes tagCopy1.
  46. for length >= 68 {
  47. // Emit a length 64 copy, encoded as 3 bytes.
  48. dst[i+0] = 63<<2 | tagCopy2
  49. dst[i+1] = uint8(offset)
  50. dst[i+2] = uint8(offset >> 8)
  51. i += 3
  52. length -= 64
  53. }
  54. if length > 64 {
  55. // Emit a length 60 copy, encoded as 3 bytes.
  56. dst[i+0] = 59<<2 | tagCopy2
  57. dst[i+1] = uint8(offset)
  58. dst[i+2] = uint8(offset >> 8)
  59. i += 3
  60. length -= 60
  61. }
  62. if length >= 12 || offset >= 2048 {
  63. // Emit the remaining copy, encoded as 3 bytes.
  64. dst[i+0] = uint8(length-1)<<2 | tagCopy2
  65. dst[i+1] = uint8(offset)
  66. dst[i+2] = uint8(offset >> 8)
  67. return i + 3
  68. }
  69. // Emit the remaining copy, encoded as 2 bytes.
  70. dst[i+0] = uint8(offset>>8)<<5 | uint8(length-4)<<2 | tagCopy1
  71. dst[i+1] = uint8(offset)
  72. return i + 2
  73. }
  74. // extendMatch returns the largest k such that k <= len(src) and that
  75. // src[i:i+k-j] and src[j:k] have the same contents.
  76. //
  77. // It assumes that:
  78. // 0 <= i && i < j && j <= len(src)
  79. func extendMatch(src []byte, i, j int) int {
  80. for ; j < len(src) && src[i] == src[j]; i, j = i+1, j+1 {
  81. }
  82. return j
  83. }