diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 6206927..45c6b54 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -14,6 +14,13 @@ All notable changes to this project will be documented in this file. The format is based on https://keepachangelog.com/[Keep a Changelog], and this project adheres to https://semver.org/[Semantic Versioning]. +== {compare-url}/v0.1.0\...HEAD[Unreleased] + +=== Changed + +* Export constants regarding the dictionary size and the member size + ({pull-request-url}/3[#3]) + == {project-url}/releases/tag/v0.1.0[0.1.0] - 2024-04-04 === Added diff --git a/export_test.go b/export_test.go index 8d5463b..d921945 100644 --- a/export_test.go +++ b/export_test.go @@ -5,22 +5,14 @@ package lzip const ( - MagicSize = magicSize HeaderSize = headerSize TrailerSize = trailerSize ) const Magic = magic +const MagicSize = magicSize const ( Version0 = version0 Version1 = version1 ) - -const ( - MinDictSize = minDictSize - MaxDictSize = maxDictSize - DefaultDictSize = defaultDictSize -) - -const MaxMemberSize = maxMemberSize diff --git a/lzip.go b/lzip.go index fc22aee..764e840 100644 --- a/lzip.go +++ b/lzip.go @@ -18,12 +18,12 @@ import ( ) const ( - magicSize = 4 headerSize = 6 trailerSize = 20 ) const magic = "LZIP" +const magicSize = 4 type version uint8 @@ -33,12 +33,18 @@ const ( ) const ( - minDictSize = lzma.MinDictCap - maxDictSize = 1 << 29 - defaultDictSize = 0x800000 + // MinDictSize is the minimum dictionary size, which is 4 KiB. + MinDictSize = lzma.MinDictCap + + // MaxDictSize is the maximum dictionary size, which is 512 MiB. + MaxDictSize = 1 << 29 + + // DefaultDictSize is the default dictionary size, which is 8 MiB. + DefaultDictSize = 0x800000 ) -const maxMemberSize = 0x8000000000000 +// MaxMemberSize is the maximum member size, which is 2 PiB. +const MaxMemberSize = 0x8000000000000 type header struct { magic [magicSize]byte @@ -49,7 +55,7 @@ type header struct { func newHeader(dictSize int) *header { ds := bits.Len(uint(dictSize - 1)) - if dictSize > minDictSize { + if dictSize > MinDictSize { base := 1 << dictSize frac := base / 16 diff --git a/lzip_test.go b/lzip_test.go index ce974c5..2a14ba6 100644 --- a/lzip_test.go +++ b/lzip_test.go @@ -11,14 +11,6 @@ import ( "github.com/sorairolake/lzip-go" ) -func TestMagicSize(t *testing.T) { - t.Parallel() - - if size := lzip.MagicSize; size != 4 { - t.Errorf("expected magic number size `%v`, got `%v`", 4, size) - } -} - func TestHeaderSize(t *testing.T) { t.Parallel() @@ -43,6 +35,14 @@ func TestMagic(t *testing.T) { } } +func TestMagicSize(t *testing.T) { + t.Parallel() + + if size := lzip.MagicSize; size != 4 { + t.Errorf("expected magic number size `%v`, got `%v`", 4, size) + } +} + func TestVersion(t *testing.T) { t.Parallel() diff --git a/reader.go b/reader.go index 04a1f29..8ba1db1 100644 --- a/reader.go +++ b/reader.go @@ -49,9 +49,9 @@ func NewReader(r io.Reader) (*Reader, error) { dictSize -= (dictSize / 16) * int((header[5]>>5)&0x07) switch { - case dictSize < minDictSize: + case dictSize < MinDictSize: return nil, ErrDictSizeTooSmall - case dictSize > maxDictSize: + case dictSize > MaxDictSize: return nil, ErrDictSizeTooLarge } @@ -67,7 +67,7 @@ func NewReader(r io.Reader) (*Reader, error) { copy(lzmaHeader[5:], rb[len(rb)-16:len(rb)-8]) z.trailer.memberSize = uint64(headerSize + len(rb)) - if z.trailer.memberSize > maxMemberSize { + if z.trailer.memberSize > MaxMemberSize { return nil, ErrMemberSizeTooLarge } diff --git a/writer.go b/writer.go index 74f4408..1fa296d 100644 --- a/writer.go +++ b/writer.go @@ -32,7 +32,7 @@ type WriterOptions struct { } func newWriterOptions() *WriterOptions { - opt := &WriterOptions{defaultDictSize} + opt := &WriterOptions{DefaultDictSize} return opt } @@ -40,9 +40,9 @@ func newWriterOptions() *WriterOptions { // Verify checks if [WriterOptions] is valid. func (o *WriterOptions) Verify() error { switch { - case o.DictSize < minDictSize: + case o.DictSize < MinDictSize: return ErrDictSizeTooSmall - case o.DictSize > maxDictSize: + case o.DictSize > MaxDictSize: return ErrDictSizeTooLarge } @@ -137,7 +137,7 @@ func (z *Writer) Close() error { binary.LittleEndian.PutUint64(trailer[4:12], z.trailer.dataSize) binary.LittleEndian.PutUint64(trailer[12:], headerSize+uint64(len(cb))+trailerSize) - if binary.LittleEndian.Uint64(trailer[12:]) > maxMemberSize { + if binary.LittleEndian.Uint64(trailer[12:]) > MaxMemberSize { return ErrMemberSizeTooLarge }