Skip to content

Commit

Permalink
Merge pull request #18 from bodgit/lint
Browse files Browse the repository at this point in the history
Lint
  • Loading branch information
bodgit authored Apr 30, 2022
2 parents 98a7850 + bae1442 commit 43a6529
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 105 deletions.
15 changes: 15 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
---
linters:
enable-all: true
disable:
- cyclop
- exhaustivestruct
- funlen
- gochecknoglobals
- gochecknoinits
- gocognit
- gocyclo
- goerr113
- gomnd
- godox
- ireturn
- nestif
- varnamelen
- wrapcheck
6 changes: 6 additions & 0 deletions internal/aes7z/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (rc *readCloser) Close() error {
err = rc.rc.Close()
rc.rc, rc.br = nil, nil
}

return err
}

Expand All @@ -32,17 +33,21 @@ func (rc *readCloser) Password(p string) error {
if err != nil {
return err
}

rc.br = cipherio.NewBlockReader(rc.rc, cipher.NewCBCDecrypter(block, rc.iv))

return nil
}

func (rc *readCloser) Read(p []byte) (int, error) {
if rc.rc == nil {
return 0, errors.New("aes7z: Read after Close")
}

if rc.br == nil {
return 0, errors.New("aes7z: no password set")
}

return rc.br.Read(p)
}

Expand All @@ -67,6 +72,7 @@ func NewReader(p []byte, _ uint64, readers []io.ReadCloser) (io.ReadCloser, erro

salt := p[0]>>7&1 + p[1]>>4
iv := p[0]>>6&1 + p[1]&0x0f

if len(p) != int(2+salt+iv) {
return nil, errProperties
}
Expand Down
20 changes: 15 additions & 5 deletions internal/bcj2/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func NewReader(_ []byte, _ uint64, readers []io.ReadCloser) (io.ReadCloser, erro
if _, err := io.ReadFull(rc.rd, b); err != nil {
return nil, err
}

for _, x := range b {
rc.code = (rc.code << 8) | uint(x)
}
Expand All @@ -90,6 +91,7 @@ func (rc *readCloser) Close() error {
if rc.main != nil {
err = multierror.Append(err, rc.main.Close(), rc.call.Close(), rc.jump.Close(), rc.rd.Close())
}

return err.ErrorOrNil()
}

Expand All @@ -98,7 +100,7 @@ func (rc *readCloser) Read(p []byte) (int, error) {
return 0, errors.New("bcj2: Read after Close")
}

if err := rc.read(); err != nil && err != io.EOF {
if err := rc.read(); err != nil && !errors.Is(err, io.EOF) {
return 0, err
}

Expand All @@ -111,6 +113,7 @@ func (rc *readCloser) update() error {
if err != nil {
return err
}

rc.code = (rc.code << 8) | uint(b)
rc.nrange <<= 8
}
Expand All @@ -124,24 +127,30 @@ func (rc *readCloser) decode(i int) (bool, error) {
if rc.code < newBound {
rc.nrange = newBound
rc.sd[i] += (bitModelTotal - rc.sd[i]) >> numMoveBits

if err := rc.update(); err != nil {
return false, nil
return false, err
}

return false, nil
}

rc.nrange -= newBound
rc.code -= newBound
rc.sd[i] -= rc.sd[i] >> numMoveBits

if err := rc.update(); err != nil {
return false, nil
return false, err
}

return true, nil
}

func (rc *readCloser) read() error {
var b byte
var err error
var (
b byte
err error
)

for {
if b, err = rc.main.ReadByte(); err != nil {
Expand All @@ -154,6 +163,7 @@ func (rc *readCloser) read() error {
if isJ(rc.previous, b) {
break
}

rc.previous = b

if rc.buf.Len() == rc.buf.Cap() {
Expand Down
2 changes: 2 additions & 0 deletions internal/bzip2/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ func (rc *readCloser) Close() error {
err = rc.c.Close()
rc.c, rc.r = nil, nil
}

return err
}

func (rc *readCloser) Read(p []byte) (int, error) {
if rc.r == nil {
return 0, errors.New("bzip2: Read after Close")
}

return rc.r.Read(p)
}

Expand Down
13 changes: 12 additions & 1 deletion internal/deflate/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,30 @@ type readCloser struct {
func (rc *readCloser) Close() error {
rc.mu.Lock()
defer rc.mu.Unlock()

var err error

if rc.c != nil {
if err = rc.fr.Close(); err != nil {
return err
}

flateReaderPool.Put(rc.fr)
err = rc.c.Close()
rc.c, rc.fr = nil, nil
}

return err
}

func (rc *readCloser) Read(p []byte) (int, error) {
rc.mu.Lock()
defer rc.mu.Unlock()

if rc.fr == nil {
return 0, errors.New("deflate: Read after Close")
}

return rc.fr.Read(p)
}

Expand All @@ -47,7 +53,12 @@ func NewReader(_ []byte, _ uint64, readers []io.ReadCloser) (io.ReadCloser, erro

fr, ok := flateReaderPool.Get().(io.ReadCloser)
if ok {
fr.(flate.Resetter).Reset(readers[0], nil)
frf, ok := fr.(flate.Resetter)
if ok {
if err := frf.Reset(readers[0], nil); err != nil {
return nil, err
}
}
} else {
fr = flate.NewReader(readers[0])
}
Expand Down
8 changes: 6 additions & 2 deletions internal/delta/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (rc *readCloser) Close() (err error) {
err = rc.rc.Close()
rc.rc = nil
}

return
}

Expand All @@ -33,10 +34,13 @@ func (rc *readCloser) Read(p []byte) (int, error) {
return n, err
}

var buffer [stateSize]byte
var (
buffer [stateSize]byte
j int
)

copy(buffer[:], rc.state[:rc.delta])

var j int
for i := 0; i < n; {
for j = 0; j < rc.delta && i < n; i++ {
p[i] = buffer[j] + p[i]
Expand Down
2 changes: 2 additions & 0 deletions internal/lzma/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ func (rc *readCloser) Close() error {
err = rc.c.Close()
rc.c, rc.r = nil, nil
}

return err
}

func (rc *readCloser) Read(p []byte) (int, error) {
if rc.r == nil {
return 0, errors.New("lzma: Read after Close")
}

return rc.r.Read(p)
}

Expand Down
2 changes: 2 additions & 0 deletions internal/lzma2/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ func (rc *readCloser) Close() error {
err = rc.c.Close()
rc.c, rc.r = nil, nil
}

return err
}

func (rc *readCloser) Read(p []byte) (int, error) {
if rc.r == nil {
return 0, errors.New("lzma2: Read after Close")
}

return rc.r.Read(p)
}

Expand Down
8 changes: 8 additions & 0 deletions internal/util/checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package util

import "bytes"

// CRC32Equal compares CRC32 checksums.
func CRC32Equal(b []byte, c uint32) bool {
return bytes.Equal(b, []byte{byte(0xff & (c >> 24)), byte(0xff & (c >> 16)), byte(0xff & (c >> 8)), byte(0xff & c)})
}
4 changes: 4 additions & 0 deletions internal/util/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ type byteReadCloser struct {

func (rc *byteReadCloser) ReadByte() (byte, error) {
var b [1]byte

n, err := rc.Read(b[:])
if err != nil {
return 0, err
}

if n == 0 {
return 0, io.ErrNoProgress
}

return b[0], nil
}

Expand All @@ -50,5 +53,6 @@ func ByteReadCloser(r io.ReadCloser) ReadCloser {
if rc, ok := r.(ReadCloser); ok {
return rc
}

return &byteReadCloser{r}
}
Loading

0 comments on commit 43a6529

Please sign in to comment.