Debian Codegraph @

snappy/xerial/xerial.go

raw
1 package xerial
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "errors"
7
8 "github.com/klauspost/compress/s2"
9 )
10
11 var (
12 xerialHeader = []byte{130, 83, 78, 65, 80, 80, 89, 0}
13
14 // This is xerial version 1 and minimally compatible with version 1
15 xerialVersionInfo = []byte{0, 0, 0, 1, 0, 0, 0, 1}
16
17 // ErrMalformed is returned by the decoder when the xerial framing
18 // is malformed
19 ErrMalformed = errors.New("malformed xerial framing")
20 )
21
22 // Encode *appends* to the specified 'dst' the compressed
23 // 'src' in xerial framing format. If 'dst' does not have enough
24 // capacity, then a new slice will be allocated. If 'dst' has
25 // non-zero length, then if *must* have been built using this function.
26 func Encode(dst, src []byte) []byte {
27 if len(dst) == 0 {
28 dst = append(dst, xerialHeader...)
29 dst = append(dst, xerialVersionInfo...)
30 }
31
32 // Snappy encode in blocks of maximum 32KB
33 var (
34 max = len(src)
35 blockSize = 32 * 1024
36 pos = 0
37 chunk []byte
38 )
39
40 for pos < max {
41 newPos := min(pos+blockSize, max)
42 // Find maximum length we need
43 needLen := s2.MaxEncodedLen(newPos-pos) + 4
44 if cap(dst)-len(dst) >= needLen {
45 // Encode directly into dst
46 dstStart := len(dst) + 4 // Start offset in dst
47 dstSizePos := dst[len(dst):dstStart] // Reserve space for compressed size
48 dstEnd := len(dst) + needLen // End offset in dst
49 // Compress into dst and get actual size.
50 actual := s2.EncodeSnappy(dst[dstStart:dstEnd], src[pos:newPos])
51 // Update dst size
52 dst = dst[:dstStart+len(actual)]
53 // Store compressed size
54 binary.BigEndian.PutUint32(dstSizePos, uint32(len(actual)))
55 } else {
56 chunk = s2.EncodeSnappy(chunk[:cap(chunk)], src[pos:newPos])
57 origLen := len(dst)
58 // First encode the compressed size (big-endian)
59 // Put* panics if the buffer is too small, so pad 4 bytes first
60 dst = append(dst, dst[0:4]...)
61 binary.BigEndian.PutUint32(dst[origLen:], uint32(len(chunk)))
62 // And now the compressed data
63 dst = append(dst, chunk...)
64 }
65 pos = newPos
66 }
67 return dst
68 }
69
70 // EncodeBetter *appends* to the specified 'dst' the compressed
71 // 'src' in xerial framing format. If 'dst' does not have enough
72 // capacity, then a new slice will be allocated. If 'dst' has
73 // non-zero length, then if *must* have been built using this function.
74 func EncodeBetter(dst, src []byte) []byte {
75 if len(dst) == 0 {
76 dst = append(dst, xerialHeader...)
77 dst = append(dst, xerialVersionInfo...)
78 }
79
80 // Snappy encode in blocks of maximum 32KB
81 var (
82 max = len(src)
83 blockSize = 32 * 1024
84 pos = 0
85 chunk []byte
86 )
87
88 for pos < max {
89 newPos := min(pos+blockSize, max)
90 // Find maximum length we need
91 needLen := s2.MaxEncodedLen(newPos-pos) + 4
92 if cap(dst)-len(dst) >= needLen {
93 // Encode directly into dst
94 dstStart := len(dst) + 4 // Start offset in dst
95 dstSizePos := dst[len(dst):dstStart] // Reserve space for compressed size
96 dstEnd := len(dst) + needLen // End offset in dst
97 // Compress into dst and get actual size.
98 actual := s2.EncodeSnappyBetter(dst[dstStart:dstEnd], src[pos:newPos])
99 // Update dst size
100 dst = dst[:dstStart+len(actual)]
101 // Store compressed size
102 binary.BigEndian.PutUint32(dstSizePos, uint32(len(actual)))
103 } else {
104 chunk = s2.EncodeSnappyBetter(chunk[:cap(chunk)], src[pos:newPos])
105 origLen := len(dst)
106 // First encode the compressed size (big-endian)
107 // Put* panics if the buffer is too small, so pad 4 bytes first
108 dst = append(dst, dst[0:4]...)
109 binary.BigEndian.PutUint32(dst[origLen:], uint32(len(chunk)))
110 // And now the compressed data
111 dst = append(dst, chunk...)
112 }
113 pos = newPos
114 }
115 return dst
116 }
117
118 const (
119 sizeOffset = 16
120 sizeBytes = 4
121 )
122
123 // Decode decodes snappy data whether it is traditional unframed
124 // or includes the xerial framing format.
125 func Decode(src []byte) ([]byte, error) {
126 return DecodeInto(nil, src)
127 }
128
129 // DecodeInto decodes snappy data whether it is traditional unframed
130 // or includes the xerial framing format into the specified `dst`.
131 // It is assumed that the entirety of `dst` including all capacity is available
132 // for use by this function. If `dst` is nil *or* insufficiently large to hold
133 // the decoded `src`, new space will be allocated.
134 // To never allocate bigger destination, use DecodeCapped.
135 func DecodeInto(dst, src []byte) ([]byte, error) {
136 var max = len(src)
137
138 if max < len(xerialHeader) || !bytes.Equal(src[:8], xerialHeader) {
139 dst, err := s2.Decode(dst[:cap(dst)], src)
140 if err != nil {
141 return dst, ErrMalformed
142 }
143 return dst, nil
144 }
145 if max == sizeOffset {
146 return []byte{}, nil
147 }
148 if max < sizeOffset+sizeBytes {
149 return nil, ErrMalformed
150 }
151 if len(dst) > 0 {
152 dst = dst[:0]
153 }
154 var (
155 pos = sizeOffset
156 chunk []byte
157 )
158
159 for pos+sizeBytes <= max {
160 size := int(binary.BigEndian.Uint32(src[pos : pos+sizeBytes]))
161 pos += sizeBytes
162
163 nextPos := pos + size
164 // On architectures where int is 32-bytes wide size + pos could
165 // overflow so we need to check the low bound as well as the
166 // high
167 if nextPos < pos || nextPos > max {
168 return nil, ErrMalformed
169 }
170 nextLen, err := s2.DecodedLen(src[pos:nextPos])
171 if err != nil {
172 return nil, err
173 }
174 if cap(dst)-len(dst) >= nextLen {
175 // Decode directly into dst
176 dstStart := len(dst)
177 dstEnd := dstStart + nextLen
178 _, err = s2.Decode(dst[dstStart:dstEnd], src[pos:nextPos])
179 if err != nil {
180 return nil, err
181 }
182 dst = dst[:dstEnd]
183 } else {
184 chunk, err = s2.Decode(chunk[:cap(chunk)], src[pos:nextPos])
185 if err != nil {
186 return nil, err
187 }
188 dst = append(dst, chunk...)
189 }
190 pos = nextPos
191 }
192 return dst, nil
193 }
194
195 var ErrDstTooSmall = errors.New("destination buffer too small")
196
197 // DecodeCapped decodes snappy data whether it is traditional unframed
198 // or includes the xerial framing format into the specified `dst`.
199 // It is assumed that the entirety of `dst` including all capacity is available
200 // for use by this function. If `dst` is nil *or* insufficiently large to hold
201 // the decoded `src`, ErrDstTooSmall is returned.
202 func DecodeCapped(dst, src []byte) ([]byte, error) {
203 var max = len(src)
204 if dst == nil {
205 return nil, ErrDstTooSmall
206 }
207 if max < len(xerialHeader) || !bytes.Equal(src[:8], xerialHeader) {
208 l, err := s2.DecodedLen(src)
209 if err != nil {
210 return nil, ErrMalformed
211 }
212 if l > cap(dst) {
213 return nil, ErrDstTooSmall
214 }
215 return s2.Decode(dst[:cap(dst)], src)
216 }
217 dst = dst[:0]
218 if max == sizeOffset {
219 return dst, nil
220 }
221 if max < sizeOffset+sizeBytes {
222 return nil, ErrMalformed
223 }
224 pos := sizeOffset
225
226 for pos+sizeBytes <= max {
227 size := int(binary.BigEndian.Uint32(src[pos : pos+sizeBytes]))
228 pos += sizeBytes
229
230 nextPos := pos + size
231 // On architectures where int is 32-bytes wide size + pos could
232 // overflow so we need to check the low bound as well as the
233 // high
234 if nextPos < pos || nextPos > max {
235 return nil, ErrMalformed
236 }
237 nextLen, err := s2.DecodedLen(src[pos:nextPos])
238 if err != nil {
239 return nil, err
240 }
241 if cap(dst)-len(dst) < nextLen {
242 return nil, ErrDstTooSmall
243 }
244 // Decode directly into dst
245 dstStart := len(dst)
246 dstEnd := dstStart + nextLen
247 _, err = s2.Decode(dst[dstStart:dstEnd], src[pos:nextPos])
248 if err != nil {
249 return nil, err
250 }
251 dst = dst[:dstEnd]
252 pos = nextPos
253 }
254 return dst, nil
255 }