Skip to content

Commit 2b14cef

Browse files
committed
internal/zstd: replace hardcoded block size with constant
Replace the hardcoded maximum block size (128<<10) with a named constant (blockMaximumSize). This improves code readability and maintainability by using a single source of truth for the maximum block size value. Also, introduce constants for magic numbers related to zstd stream identification and skippable frames, enhancing code clarity.
1 parent 22278e3 commit 2b14cef

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/internal/zstd/block.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func (r *Reader) execSeqs(data block, off int, litbuf []byte, seqCount int) erro
273273

274274
seq := 0
275275
for seq < seqCount {
276-
if len(r.buffer)+len(litbuf) > 128<<10 {
276+
if len(r.buffer)+len(litbuf) > blockMaximumSize {
277277
return rbr.makeError("uncompressed size too big")
278278
}
279279

src/internal/zstd/literals.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (r *Reader) readRawRLELiterals(data block, off int, hdr byte, outbuf []byte
5353
// We are going to use the entire literal block in the output.
5454
// The maximum size of one decompressed block is 128K,
5555
// so we can't have more literals than that.
56-
if regeneratedSize > 128<<10 {
56+
if regeneratedSize > blockMaximumSize {
5757
return 0, nil, r.makeError(off, "literal size too large")
5858
}
5959

@@ -121,7 +121,7 @@ func (r *Reader) readHuffLiterals(data block, off int, hdr byte, outbuf []byte)
121121
// We are going to use the entire literal block in the output.
122122
// The maximum size of one decompressed block is 128K,
123123
// so we can't have more literals than that.
124-
if regeneratedSize > 128<<10 {
124+
if regeneratedSize > blockMaximumSize {
125125
return 0, nil, r.makeError(off, "literal size too large")
126126
}
127127

src/internal/zstd/zstd.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ import (
1717
// This is used to reject cases where we don't match zstd.
1818
var fuzzing = false
1919

20+
const (
21+
// magicNumber is the magic number used to identify a zstd stream.
22+
// It is the first 4 bytes of a zstd stream.
23+
magicNumber = 0xFD2FB528
24+
// all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame
25+
magicSkippableStart = 0x184D2A50
26+
magicSkippableMask = 0xFFFFFFF0
27+
28+
// Maximum block size is smaller of window size and 128K
29+
blockMaximumSize = 128 << 10
30+
)
31+
2032
// Reader implements [io.Reader] to read a zstd compressed stream.
2133
type Reader struct {
2234
// The underlying Reader.
@@ -176,8 +188,8 @@ retry:
176188
return r.wrapError(relativeOffset, err)
177189
}
178190

179-
if magic := binary.LittleEndian.Uint32(r.scratch[:4]); magic != 0xfd2fb528 {
180-
if magic >= 0x184d2a50 && magic <= 0x184d2a5f {
191+
if magic := binary.LittleEndian.Uint32(r.scratch[:4]); magic != magicNumber {
192+
if magic&magicSkippableMask == magicSkippableStart {
181193
// This is a skippable frame.
182194
r.blockOffset += int64(relativeOffset) + 4
183195
if err := r.skipFrame(); err != nil {
@@ -292,8 +304,9 @@ retry:
292304
}
293305

294306
// RFC 8878 3.1.1.1.1.2. permits us to set an 8M max on window size.
295-
if windowSize > 8<<20 {
296-
windowSize = 8 << 20
307+
const windowMaxSize = 8 << 20
308+
if windowSize > windowMaxSize {
309+
windowSize = windowMaxSize
297310
}
298311

299312
relativeOffset += headerSize
@@ -382,7 +395,7 @@ func (r *Reader) readBlock() error {
382395
// Maximum block size is smaller of window size and 128K.
383396
// We don't record the window size for a single segment frame,
384397
// so just use 128K. RFC 3.1.1.2.3, 3.1.1.2.4.
385-
if blockSize > 128<<10 || (r.window.size > 0 && blockSize > r.window.size) {
398+
if blockSize > blockMaximumSize || (r.window.size > 0 && blockSize > r.window.size) {
386399
return r.makeError(relativeOffset, "block size too large")
387400
}
388401

0 commit comments

Comments
 (0)