Skip to content

Commit

Permalink
Add public constants
Browse files Browse the repository at this point in the history
Export constants regarding the dictionary size and the member size.
  • Loading branch information
sorairolake committed Apr 4, 2024
1 parent 328b3cc commit cc4c76c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 30 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 12 additions & 6 deletions lzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
)

const (
magicSize = 4
headerSize = 6
trailerSize = 20
)

const magic = "LZIP"
const magicSize = 4

type version uint8

Expand All @@ -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
Expand All @@ -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

Expand Down
16 changes: 8 additions & 8 deletions lzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ type WriterOptions struct {
}

func newWriterOptions() *WriterOptions {
opt := &WriterOptions{defaultDictSize}
opt := &WriterOptions{DefaultDictSize}

return opt
}

// 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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit cc4c76c

Please sign in to comment.