| 1 |
// Copyright 2011 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 |
|
| 5 |
package snappy |
| 6 |
|
| 7 |
import "encoding/binary" |
| 8 |
|
| 9 |
// DecodeStrict returns the decoded form of src, like Decode. |
| 10 |
// |
| 11 |
// Unlike Decode, which delegates to the s2 decoder and accepts the s2 |
| 12 |
// "repeat last offset" extension (a copy whose encoded offset is 0), |
| 13 |
// DecodeStrict uses a strict standard-Snappy block decoder. Offset 0 is not |
| 14 |
// valid in the standard Snappy block format, so such a copy is rejected with |
| 15 |
// ErrCorrupt. This makes DecodeStrict match github.com/golang/snappy and the |
| 16 |
// C++ reference decoder, at the cost of being slower than Decode. |
| 17 |
// |
| 18 |
// The returned slice may be a sub-slice of dst if dst was large enough to |
| 19 |
// hold the entire decoded block. Otherwise, a newly allocated slice will be |
| 20 |
// returned. |
| 21 |
// |
| 22 |
// The dst and src must not overlap. It is valid to pass a nil dst. |
| 23 |
// |
| 24 |
// DecodeStrict handles the Snappy block format, not the Snappy stream format. |
| 25 |
func DecodeStrict(dst, src []byte) ([]byte, error) { |
| 26 |
v, n := binary.Uvarint(src) |
| 27 |
if n <= 0 || n > 5 || v > 0xffffffff { |
| 28 |
return nil, ErrCorrupt |
| 29 |
} |
| 30 |
const wordSize = 32 << (^uint(0) >> 32 & 1) |
| 31 |
if wordSize == 32 && v > 0x7fffffff { |
| 32 |
return nil, ErrTooLarge |
| 33 |
} |
| 34 |
dLen := int(v) |
| 35 |
if dLen <= cap(dst) { |
| 36 |
dst = dst[:dLen] |
| 37 |
} else { |
| 38 |
dst = make([]byte, dLen) |
| 39 |
} |
| 40 |
if decodeStrict(dst, src[n:]) != 0 { |
| 41 |
return nil, ErrCorrupt |
| 42 |
} |
| 43 |
return dst, nil |
| 44 |
} |
| 45 |
|
| 46 |
func decodeStrict(dst, src []byte) int { |
| 47 |
var d, s, offset, length int |
| 48 |
for s < len(src) { |
| 49 |
switch src[s] & 0x03 { |
| 50 |
case 0x00: |
| 51 |
x := uint32(src[s] >> 2) |
| 52 |
switch { |
| 53 |
case x < 60: |
| 54 |
s++ |
| 55 |
case x == 60: |
| 56 |
s += 2 |
| 57 |
if uint(s) > uint(len(src)) { |
| 58 |
return 1 |
| 59 |
} |
| 60 |
x = uint32(src[s-1]) |
| 61 |
case x == 61: |
| 62 |
s += 3 |
| 63 |
if uint(s) > uint(len(src)) { |
| 64 |
return 1 |
| 65 |
} |
| 66 |
x = uint32(src[s-2]) | uint32(src[s-1])<<8 |
| 67 |
case x == 62: |
| 68 |
s += 4 |
| 69 |
if uint(s) > uint(len(src)) { |
| 70 |
return 1 |
| 71 |
} |
| 72 |
x = uint32(src[s-3]) | uint32(src[s-2])<<8 | uint32(src[s-1])<<16 |
| 73 |
case x == 63: |
| 74 |
s += 5 |
| 75 |
if uint(s) > uint(len(src)) { |
| 76 |
return 1 |
| 77 |
} |
| 78 |
x = uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24 |
| 79 |
} |
| 80 |
length = int(x) + 1 |
| 81 |
if length <= 0 || length > len(dst)-d || length > len(src)-s { |
| 82 |
return 1 |
| 83 |
} |
| 84 |
copy(dst[d:], src[s:s+length]) |
| 85 |
d += length |
| 86 |
s += length |
| 87 |
continue |
| 88 |
case 0x01: |
| 89 |
s += 2 |
| 90 |
if uint(s) > uint(len(src)) { |
| 91 |
return 1 |
| 92 |
} |
| 93 |
length = 4 + int(src[s-2])>>2&0x7 |
| 94 |
offset = int(uint32(src[s-2])&0xe0<<3 | uint32(src[s-1])) |
| 95 |
case 0x02: |
| 96 |
s += 3 |
| 97 |
if uint(s) > uint(len(src)) { |
| 98 |
return 1 |
| 99 |
} |
| 100 |
length = 1 + int(src[s-3])>>2 |
| 101 |
offset = int(uint32(src[s-2]) | uint32(src[s-1])<<8) |
| 102 |
case 0x03: |
| 103 |
s += 5 |
| 104 |
if uint(s) > uint(len(src)) { |
| 105 |
return 1 |
| 106 |
} |
| 107 |
length = 1 + int(src[s-5])>>2 |
| 108 |
offset = int(uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24) |
| 109 |
} |
| 110 |
if offset <= 0 || d < offset || length > len(dst)-d { |
| 111 |
return 1 |
| 112 |
} |
| 113 |
if offset >= length { |
| 114 |
copy(dst[d:d+length], dst[d-offset:]) |
| 115 |
d += length |
| 116 |
continue |
| 117 |
} |
| 118 |
a := dst[d : d+length] |
| 119 |
b := dst[d-offset:] |
| 120 |
b = b[:len(a)] |
| 121 |
for i := range a { |
| 122 |
a[i] = b[i] |
| 123 |
} |
| 124 |
d += length |
| 125 |
} |
| 126 |
if d != len(dst) { |
| 127 |
return 1 |
| 128 |
} |
| 129 |
return 0 |
| 130 |
} |