encode.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2014 The zappy 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. // Copyright 2011 The Snappy-Go Authors. All rights reserved.
  5. // Use of this source code is governed by a BSD-style
  6. // license that can be found in the SNAPPY-GO-LICENSE file.
  7. package zappy
  8. import (
  9. "encoding/binary"
  10. )
  11. // We limit how far copy back-references can go, the same as the snappy C++
  12. // code.
  13. const maxOffset = 1 << 20
  14. // emitCopy writes a copy chunk and returns the number of bytes written.
  15. func emitCopy(dst []byte, offset, length int) (n int) {
  16. n = binary.PutVarint(dst, int64(-length))
  17. n += binary.PutUvarint(dst[n:], uint64(offset))
  18. return
  19. }
  20. // emitLiteral writes a literal chunk and returns the number of bytes written.
  21. func emitLiteral(dst, lit []byte) (n int) {
  22. n = binary.PutVarint(dst, int64(len(lit)-1))
  23. n += copy(dst[n:], lit)
  24. return
  25. }
  26. // MaxEncodedLen returns the maximum length of a zappy block, given its
  27. // uncompressed length.
  28. func MaxEncodedLen(srcLen int) int {
  29. return 10 + srcLen + (srcLen+1)/2
  30. }