| 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 |
"errors" |
| 14 |
) |
| 15 |
|
| 16 |
// ErrCorrupt reports that the input is invalid. |
| 17 |
var ErrCorrupt = errors.New("zappy: corrupt input") |
| 18 |
|
| 19 |
// DecodedLen returns the length of the decoded block. |
| 20 |
func DecodedLen(src []byte) (int, error) { |
| 21 |
v, _, err := decodedLen(src) |
| 22 |
return v, err |
| 23 |
} |
| 24 |
|
| 25 |
// decodedLen returns the length of the decoded block and the number of bytes |
| 26 |
// that the length header occupied. |
| 27 |
func decodedLen(src []byte) (blockLen, headerLen int, err error) { |
| 28 |
v, n := binary.Uvarint(src) |
| 29 |
if n == 0 { |
| 30 |
return 0, 0, ErrCorrupt |
| 31 |
} |
| 32 |
|
| 33 |
if uint64(int(v)) != v { |
| 34 |
return 0, 0, errors.New("zappy: decoded block is too large") |
| 35 |
} |
| 36 |
|
| 37 |
return int(v), n, nil |
| 38 |
} |