Debian Codegraph @

s2/decode_other.go

raw
1 // Copyright 2016 The Snappy-Go Authors. All rights reserved.
2 // Copyright (c) 2019 Klaus Post. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 //go:build (!amd64 && !arm64) || appengine || !gc || noasm
7
8 package s2
9
10 import (
11 "fmt"
12 "strconv"
13
14 "github.com/klauspost/compress/internal/le"
15 )
16
17 // decode writes the decoding of src to dst. It assumes that the varint-encoded
18 // length of the decompressed bytes has already been read, and that len(dst)
19 // equals that length.
20 //
21 // It returns 0 on success or a decodeErrCodeXxx error code on failure.
22 func s2Decode(dst, src []byte) int {
23 const debug = false
24 if debug {
25 fmt.Println("Starting decode, dst len:", len(dst))
26 }
27 var d, s, length int
28 offset := 0
29
30 // As long as we can read at least 5 bytes...
31 for s < len(src)-5 {
32 // Removing bounds checks is SLOWER, when if doing
33 // in := src[s:s+5]
34 // Checked on Go 1.18
35 switch src[s] & 0x03 {
36 case tagLiteral:
37 x := uint32(src[s] >> 2)
38 switch {
39 case x < 60:
40 s++
41 case x == 60:
42 x = uint32(src[s+1])
43 s += 2
44 case x == 61:
45 x = uint32(le.Load16(src, s+1))
46 s += 3
47 case x == 62:
48 // Load as 32 bit and shift down.
49 x = le.Load32(src, s)
50 x >>= 8
51 s += 4
52 case x == 63:
53 x = le.Load32(src, s+1)
54 s += 5
55 }
56 length = int(x) + 1
57 if length > len(dst)-d || length > len(src)-s || (strconv.IntSize == 32 && length <= 0) {
58 if debug {
59 fmt.Println("corrupt: lit size", length)
60 }
61 return decodeErrCodeCorrupt
62 }
63 if debug {
64 fmt.Println("literals, length:", length, "d-after:", d+length)
65 }
66
67 copy(dst[d:], src[s:s+length])
68 d += length
69 s += length
70 continue
71
72 case tagCopy1:
73 s += 2
74 toffset := int(uint32(src[s-2])&0xe0<<3 | uint32(src[s-1]))
75 length = int(src[s-2]) >> 2 & 0x7
76 if toffset == 0 {
77 if debug {
78 fmt.Print("(repeat) ")
79 }
80 // keep last offset
81 switch length {
82 case 5:
83 length = int(src[s]) + 4
84 s += 1
85 case 6:
86 length = int(le.Load16(src, s)) + 1<<8
87 s += 2
88 case 7:
89 in := src[s : s+3]
90 length = int((uint32(in[2])<<16)|(uint32(in[1])<<8)|uint32(in[0])) + (1 << 16)
91 s += 3
92 default: // 0-> 4
93 }
94 } else {
95 offset = toffset
96 }
97 length += 4
98 case tagCopy2:
99 offset = int(le.Load16(src, s+1))
100 length = 1 + int(src[s])>>2
101 s += 3
102
103 case tagCopy4:
104 offset = int(le.Load32(src, s+1))
105 length = 1 + int(src[s])>>2
106 s += 5
107 }
108
109 if offset <= 0 || d < offset || length > len(dst)-d {
110 if debug {
111 fmt.Println("corrupt: match, length", length, "offset:", offset, "dst avail:", len(dst)-d, "dst pos:", d)
112 }
113
114 return decodeErrCodeCorrupt
115 }
116
117 if debug {
118 fmt.Println("copy, length:", length, "offset:", offset, "d-after:", d+length)
119 }
120
121 // Copy from an earlier sub-slice of dst to a later sub-slice.
122 // If no overlap, use the built-in copy:
123 if offset > length {
124 copy(dst[d:d+length], dst[d-offset:])
125 d += length
126 continue
127 }
128
129 // Unlike the built-in copy function, this byte-by-byte copy always runs
130 // forwards, even if the slices overlap. Conceptually, this is:
131 //
132 // d += forwardCopy(dst[d:d+length], dst[d-offset:])
133 //
134 // We align the slices into a and b and show the compiler they are the same size.
135 // This allows the loop to run without bounds checks.
136 a := dst[d : d+length]
137 b := dst[d-offset:]
138 b = b[:len(a)]
139 for i := range a {
140 a[i] = b[i]
141 }
142 d += length
143 }
144
145 // Remaining with extra checks...
146 for s < len(src) {
147 switch src[s] & 0x03 {
148 case tagLiteral:
149 x := uint32(src[s] >> 2)
150 switch {
151 case x < 60:
152 s++
153 case x == 60:
154 s += 2
155 if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
156 return decodeErrCodeCorrupt
157 }
158 x = uint32(src[s-1])
159 case x == 61:
160 s += 3
161 if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
162 return decodeErrCodeCorrupt
163 }
164 x = uint32(src[s-2]) | uint32(src[s-1])<<8
165 case x == 62:
166 s += 4
167 if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
168 return decodeErrCodeCorrupt
169 }
170 x = uint32(src[s-3]) | uint32(src[s-2])<<8 | uint32(src[s-1])<<16
171 case x == 63:
172 s += 5
173 if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
174 return decodeErrCodeCorrupt
175 }
176 x = uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24
177 }
178 length = int(x) + 1
179 if length > len(dst)-d || length > len(src)-s || (strconv.IntSize == 32 && length <= 0) {
180 if debug {
181 fmt.Println("corrupt: lit size", length)
182 }
183 return decodeErrCodeCorrupt
184 }
185 if debug {
186 fmt.Println("literals, length:", length, "d-after:", d+length)
187 }
188
189 copy(dst[d:], src[s:s+length])
190 d += length
191 s += length
192 continue
193
194 case tagCopy1:
195 s += 2
196 if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
197 return decodeErrCodeCorrupt
198 }
199 length = int(src[s-2]) >> 2 & 0x7
200 toffset := int(uint32(src[s-2])&0xe0<<3 | uint32(src[s-1]))
201 if toffset == 0 {
202 if debug {
203 fmt.Print("(repeat) ")
204 }
205 // keep last offset
206 switch length {
207 case 5:
208 s += 1
209 if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
210 return decodeErrCodeCorrupt
211 }
212 length = int(uint32(src[s-1])) + 4
213 case 6:
214 s += 2
215 if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
216 return decodeErrCodeCorrupt
217 }
218 length = int(uint32(src[s-2])|(uint32(src[s-1])<<8)) + (1 << 8)
219 case 7:
220 s += 3
221 if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
222 return decodeErrCodeCorrupt
223 }
224 length = int(uint32(src[s-3])|(uint32(src[s-2])<<8)|(uint32(src[s-1])<<16)) + (1 << 16)
225 default: // 0-> 4
226 }
227 } else {
228 offset = toffset
229 }
230 length += 4
231 case tagCopy2:
232 s += 3
233 if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
234 return decodeErrCodeCorrupt
235 }
236 length = 1 + int(src[s-3])>>2
237 offset = int(uint32(src[s-2]) | uint32(src[s-1])<<8)
238
239 case tagCopy4:
240 s += 5
241 if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
242 return decodeErrCodeCorrupt
243 }
244 length = 1 + int(src[s-5])>>2
245 offset = int(uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24)
246 }
247
248 if offset <= 0 || d < offset || length > len(dst)-d {
249 if debug {
250 fmt.Println("corrupt: match, length", length, "offset:", offset, "dst avail:", len(dst)-d, "dst pos:", d)
251 }
252 return decodeErrCodeCorrupt
253 }
254
255 if debug {
256 fmt.Println("copy, length:", length, "offset:", offset, "d-after:", d+length)
257 }
258
259 // Copy from an earlier sub-slice of dst to a later sub-slice.
260 // If no overlap, use the built-in copy:
261 if offset > length {
262 copy(dst[d:d+length], dst[d-offset:])
263 d += length
264 continue
265 }
266
267 // Unlike the built-in copy function, this byte-by-byte copy always runs
268 // forwards, even if the slices overlap. Conceptually, this is:
269 //
270 // d += forwardCopy(dst[d:d+length], dst[d-offset:])
271 //
272 // We align the slices into a and b and show the compiler they are the same size.
273 // This allows the loop to run without bounds checks.
274 a := dst[d : d+length]
275 b := dst[d-offset:]
276 b = b[:len(a)]
277 for i := range a {
278 a[i] = b[i]
279 }
280 d += length
281 }
282
283 if d != len(dst) {
284 return decodeErrCodeCorrupt
285 }
286 return 0
287 }