encode_other.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // emitCopy writes a copy 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 <= offset && offset <= 65535
  11. // 4 <= length && length <= 65535
  12. func emitCopy(dst []byte, offset, length int) int {
  13. i := 0
  14. // The maximum length for a single tagCopy1 or tagCopy2 op is 64 bytes. The
  15. // threshold for this loop is a little higher (at 68 = 64 + 4), and the
  16. // length emitted down below is is a little lower (at 60 = 64 - 4), because
  17. // it's shorter to encode a length 67 copy as a length 60 tagCopy2 followed
  18. // by a length 7 tagCopy1 (which encodes as 3+2 bytes) than to encode it as
  19. // a length 64 tagCopy2 followed by a length 3 tagCopy2 (which encodes as
  20. // 3+3 bytes). The magic 4 in the 64±4 is because the minimum length for a
  21. // tagCopy1 op is 4 bytes, which is why a length 3 copy has to be an
  22. // encodes-as-3-bytes tagCopy2 instead of an encodes-as-2-bytes tagCopy1.
  23. for length >= 68 {
  24. // Emit a length 64 copy, encoded as 3 bytes.
  25. dst[i+0] = 63<<2 | tagCopy2
  26. dst[i+1] = uint8(offset)
  27. dst[i+2] = uint8(offset >> 8)
  28. i += 3
  29. length -= 64
  30. }
  31. if length > 64 {
  32. // Emit a length 60 copy, encoded as 3 bytes.
  33. dst[i+0] = 59<<2 | tagCopy2
  34. dst[i+1] = uint8(offset)
  35. dst[i+2] = uint8(offset >> 8)
  36. i += 3
  37. length -= 60
  38. }
  39. if length >= 12 || offset >= 2048 {
  40. // Emit the remaining copy, encoded as 3 bytes.
  41. dst[i+0] = uint8(length-1)<<2 | tagCopy2
  42. dst[i+1] = uint8(offset)
  43. dst[i+2] = uint8(offset >> 8)
  44. return i + 3
  45. }
  46. // Emit the remaining copy, encoded as 2 bytes.
  47. dst[i+0] = uint8(offset>>8)<<5 | uint8(length-4)<<2 | tagCopy1
  48. dst[i+1] = uint8(offset)
  49. return i + 2
  50. }
  51. // extendMatch returns the largest k such that k <= len(src) and that
  52. // src[i:i+k-j] and src[j:k] have the same contents.
  53. //
  54. // It assumes that:
  55. // 0 <= i && i < j && j <= len(src)
  56. func extendMatch(src []byte, i, j int) int {
  57. for ; j < len(src) && src[i] == src[j]; i, j = i+1, j+1 {
  58. }
  59. return j
  60. }