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