@@ -3,41 +3,48 @@ package bz2
33
44// ////////////////////////////////////////////////////////////////////////////////// //
55// //
6- // Copyright (c) 2025 ESSENTIAL KAOS //
6+ // Copyright (c) 2026 ESSENTIAL KAOS //
77// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
88// //
99// ////////////////////////////////////////////////////////////////////////////////// //
1010
1111import (
1212 "bufio"
1313 "compress/bzip2"
14- "fmt"
1514 "io"
1615 "os"
1716 "path/filepath"
1817 "strings"
1918
20- "github.com/essentialkaos/npck/utils"
19+ "github.com/essentialkaos/npck/v2/ utils"
2120)
2221
2322// ////////////////////////////////////////////////////////////////////////////////// //
2423
25- // MaxReadLimit is the maximum read limit for decompression bomb
26- // protection (default: 1GB)
27- var MaxReadLimit int64 = 1024 * 1024 * 1024
24+ // DEFAULT_MAX_READ_LIMIT is default the maximum read limit (1GB)
25+ const DEFAULT_MAX_READ_LIMIT int64 = 1024 * 1024 * 1024
26+
27+ // ////////////////////////////////////////////////////////////////////////////////// //
28+
29+ // Options is reader options
30+ type Options struct {
31+ // MaxReadLimit is the maximum read limit for decompression bomb
32+ // protection (default: 1GB)
33+ MaxReadLimit int64
34+ }
2835
2936// ////////////////////////////////////////////////////////////////////////////////// //
3037
3138var (
32- ErrNilReader = fmt . Errorf ( "Reader can not be nil" )
33- ErrEmptyInput = fmt . Errorf ( "Path to input file can not be empty" )
34- ErrEmptyOutput = fmt . Errorf ( "Path to output file can not be empty" )
39+ ErrNilReader = utils . ErrNilReader
40+ ErrEmptyInput = utils . ErrEmptyInput
41+ ErrEmptyOutput = utils . ErrEmptyOutput
3542)
3643
3744// ////////////////////////////////////////////////////////////////////////////////// //
3845
39- // Unpacks file to given directory
40- func Unpack (file , dir string ) error {
46+ // Unpack unpacks archive file to given directory
47+ func Unpack (file , dir string , options Options ) error {
4148 switch {
4249 case file == "" :
4350 return ErrEmptyInput
@@ -54,20 +61,20 @@ func Unpack(file, dir string) error {
5461 return err
5562 }
5663
57- fd , err := os .OpenFile (file , os . O_RDONLY , 0 )
64+ fd , err := os .Open (file )
5865
5966 if err != nil {
6067 return err
6168 }
6269
6370 defer fd .Close ()
6471
65- return Read (bufio .NewReader (fd ), path )
72+ return Read (bufio .NewReader (fd ), path , options )
6673}
6774
6875// Read reads compressed data using given reader and unpacks it to
6976// the given directory
70- func Read (r io.Reader , output string ) error {
77+ func Read (r io.Reader , output string , options Options ) error {
7178 switch {
7279 case r == nil :
7380 return ErrNilReader
@@ -81,11 +88,20 @@ func Read(r io.Reader, output string) error {
8188 return err
8289 }
8390
91+ defer fd .Close ()
92+
93+ limit := options .MaxReadLimit
94+
95+ if limit == 0 {
96+ limit = DEFAULT_MAX_READ_LIMIT
97+ }
98+
8499 bw := bufio .NewWriter (fd )
85- _ , err = io .Copy (bw , io .LimitReader (bzip2 .NewReader (r ), MaxReadLimit ))
100+ _ , err = io .Copy (bw , io .LimitReader (bzip2 .NewReader (r ), limit ))
86101
87- bw .Flush ()
88- fd .Close ()
102+ if err != nil {
103+ return err
104+ }
89105
90- return err
106+ return bw . Flush ()
91107}
0 commit comments