Skip to content

Commit c77d82d

Browse files
committed
feat(Issue 121): Adding lz4 as compression option.
Adding lz4 dependency and adding to the options for compression. Adding lz4 logic in for decompression and compression. Unit tests still need to be written.
1 parent 74ffbc4 commit c77d82d

File tree

7 files changed

+33
-4
lines changed

7 files changed

+33
-4
lines changed

cmd/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func init() {
173173
initCmd.Flags().StringVar(&hashAlgorithm, "hash", "sha256", "Hash algorithm (sha256, blake3)")
174174

175175
// Compression vars
176-
initCmd.Flags().StringVar(&compressionType, "compression", "none", "Compression type (none, gzip, zstd)")
176+
initCmd.Flags().StringVar(&compressionType, "compression", "none", "Compression type (none, gzip, zstd, lz4)")
177177

178178
// Sync vars
179179
initCmd.Flags().StringVar(&syncMode, "sync-mode", "manual", "Synchronization mode (manual, auto)")

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ require (
121121
github.com/inconshreveable/mousetrap v1.1.0 // indirect
122122
github.com/libp2p/go-libp2p v0.41.1
123123
github.com/multiformats/go-multiaddr v0.15.0
124+
github.com/pierrec/lz4/v4 v4.1.22
124125
github.com/spf13/cobra v1.9.1
125126
github.com/spf13/pflag v1.0.6
126127
golang.org/x/sys v0.36.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ github.com/opencontainers/runtime-spec v1.2.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/
232232
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
233233
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
234234
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
235+
github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU=
236+
github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
235237
github.com/pion/datachannel v1.5.10 h1:ly0Q26K1i6ZkGf42W7D4hQYR90pZwzFOjTq5AuCKk4o=
236238
github.com/pion/datachannel v1.5.10/go.mod h1:p/jJfC9arb29W7WrxyKbepTU20CFgyx5oLo8Rs4Py/M=
237239
github.com/pion/dtls/v2 v2.2.7/go.mod h1:8WiMkebSHFD0T+dIU+UeBaoV7kDhOW5oDCzZ7WZ/F9s=

internal/chunk/prompt.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func PromptCompressionConfig(configuration *config.VaultConfig) error {
9999
// Compression prompt with descriptions
100100
compressionPrompt := promptui.Select{
101101
Label: "Compression algorithm",
102-
Items: []string{"none", "gzip", "zstd"},
102+
Items: []string{"none", "gzip", "zstd", "lz4"},
103103
Templates: &promptui.SelectTemplates{
104104
Selected: "Compression: {{ . }}",
105105
Active: "▸ {{ . }}",
@@ -108,7 +108,8 @@ func PromptCompressionConfig(configuration *config.VaultConfig) error {
108108
{{ "Details:" | faint }}
109109
{{ if eq . "none" }}No compression (faster but larger files)
110110
{{ else if eq . "gzip" }}Gzip compression (good balance of speed/compression)
111-
{{ else if eq . "zstd" }}Zstandard compression (better compression but slower){{ end }}
111+
{{ else if eq . "zstd" }}Zstandard compression (better compression but slower)
112+
{{ else if eq . "lz4" }}LZ4 compression (very fast but lower compression ratio){{ end }}
112113
`,
113114
},
114115
}

internal/compression/Compressor.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88

99
"github.com/klauspost/compress/zstd"
10+
"github.com/pierrec/lz4/v4"
1011
"github.com/substantialcattle5/sietch/internal/constants"
1112
)
1213

@@ -32,6 +33,16 @@ func CompressData(data []byte, algorithm string) ([]byte, error) {
3233
}
3334
defer encoder.Close()
3435
return encoder.EncodeAll(data, nil), nil
36+
case constants.CompressionTypeLz4:
37+
var buf bytes.Buffer
38+
writer := lz4.NewWriter(&buf)
39+
if _, err := writer.Write(data); err != nil {
40+
return nil, fmt.Errorf("failed to write lz4 data: %w", err)
41+
}
42+
if err := writer.Close(); err != nil {
43+
return nil, fmt.Errorf("failed to close lz4 writer: %w", err)
44+
}
45+
return buf.Bytes(), nil
3546
default:
3647
return nil, fmt.Errorf("unsupported compression algorithm: %s", algorithm)
3748
}
@@ -83,6 +94,19 @@ func DecompressData(data []byte, algorithm string) ([]byte, error) {
8394
}
8495

8596
return decompressed, nil
97+
98+
case constants.CompressionTypeLz4:
99+
reader := lz4.NewReader(bytes.NewReader(data))
100+
if reader.Size() >= constants.MaxDecompressionSize {
101+
return nil, fmt.Errorf("decompressed data exceeds maximum size limit (%d bytes) - potential decompression bomb", constants.MaxDecompressionSize)
102+
}
103+
var buf bytes.Buffer
104+
_, err := io.Copy(&buf, reader)
105+
if err != nil && err != io.EOF {
106+
return nil, fmt.Errorf("failed to decompress lz4 data: %w", err)
107+
}
108+
return buf.Bytes(), nil
109+
86110
default:
87111
return nil, fmt.Errorf("unsupported compression algorithm: %s", algorithm)
88112
}

internal/config/vault.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ type ChunkRef struct {
171171
Index int `yaml:"index"` // Position in the file
172172
Deduplicated bool `yaml:"deduplicated,omitempty"` // Whether this chunk was deduplicated
173173
Compressed bool `yaml:"compressed,omitempty"` // Whether this chunk was compressed
174-
CompressionType string `yaml:"compression_type,omitempty"` // Compression algorithm used (e.g., "gzip", "zstd", "none")
174+
CompressionType string `yaml:"compression_type,omitempty"` // Compression algorithm used (e.g., "gzip", "zstd", "lz4", "none")
175175
IV string `yaml:"iv,omitempty"` // Per-chunk IV if used
176176
Integrity string `yaml:"integrity,omitempty"` // Integrity check value (e.g., HMAC)
177177
}

internal/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const (
8282
//** Constants for compression
8383
CompressionTypeGzip = "gzip"
8484
CompressionTypeZstd = "zstd"
85+
CompressionTypeLz4 = "lz4"
8586
CompressionTypeNone = "none"
8687

8788
// Maximum decompression size to prevent decompression bombs

0 commit comments

Comments
 (0)