From 501fa9a9c7fdb3098415edda298e332341224c38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francis=20P=C3=A9rot?= Date: Mon, 24 Aug 2026 13:55:28 +0200 Subject: [PATCH] [CLOUDTRUST-9140] Migrate to Go 1.27 --- crypto.go | 7 ++----- go.mod | 8 ++++++++ go.sum | 4 ++++ reader_test.go | 8 ++++---- struct.go | 2 +- writer_test.go | 6 +++--- zip_test.go | 12 ++++++------ zipcrypto.go | 22 +++++++++++----------- 8 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/crypto.go b/crypto.go index 1c9f33a..2a7f3e1 100644 --- a/crypto.go +++ b/crypto.go @@ -140,11 +140,8 @@ func (x *ctr) XORKeyStream(dst, src []byte) { } func xorBytes(dst, a, b []byte) int { - n := len(a) - if len(b) < n { - n = len(b) - } - for i := 0; i < n; i++ { + n := min(len(b), len(a)) + for i := range n { dst[i] = a[i] ^ b[i] } return n diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f0c8bf2 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/cloudtrust/zip + +go 1.27.0 + +require ( + github.com/yeka/zip v0.0.0-20231116150916-03d6312748a9 + golang.org/x/crypto v0.55.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b7df4ed --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/yeka/zip v0.0.0-20231116150916-03d6312748a9 h1:K8gF0eekWPEX+57l30ixxzGhHH/qscI3JCnuhbN6V4M= +github.com/yeka/zip v0.0.0-20231116150916-03d6312748a9/go.mod h1:9BnoKCcgJ/+SLhfAXj15352hTOuVmG5Gzo8xNRINfqI= +golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M= +golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis= diff --git a/reader_test.go b/reader_test.go index 547dd39..5d80c6b 100644 --- a/reader_test.go +++ b/reader_test.go @@ -324,7 +324,7 @@ func readTestZip(t *testing.T, zt ZipTest) { // test simultaneous reads n := 0 done := make(chan bool) - for i := 0; i < 5; i++ { + for range 5 { for j, ft := range zt.File { go func(j int, ft ZipTestFile) { readTestFile(t, zt, ft, z.File[j]) @@ -383,7 +383,7 @@ func readTestFile(t *testing.T, zt ZipTest, ft ZipTestFile, f *File) { var c []byte if ft.Content != nil { c = ft.Content - } else if c, err = ioutil.ReadFile("testdata/" + ft.File); err != nil { + } else if c, err = os.ReadFile("testdata/" + ft.File); err != nil { t.Error(err) return } @@ -433,7 +433,7 @@ func TestInvalidFiles(t *testing.T) { } func messWith(fileName string, corrupter func(b []byte)) (r io.ReaderAt, size int64) { - data, err := ioutil.ReadFile(filepath.Join("testdata", fileName)) + data, err := os.ReadFile(filepath.Join("testdata", fileName)) if err != nil { panic("Error reading " + fileName + ": " + err.Error()) } @@ -599,7 +599,7 @@ func TestIssue11146(t *testing.T) { if err != nil { t.Fatal(err) } - _, err = ioutil.ReadAll(r) + _, err = io.ReadAll(r) if err != io.ErrUnexpectedEOF { t.Errorf("File[0] error = %v; want io.ErrUnexpectedEOF", err) } diff --git a/struct.go b/struct.go index 7e50a5c..e5bd319 100644 --- a/struct.go +++ b/struct.go @@ -127,7 +127,7 @@ func (fi headerFileInfo) Size() int64 { func (fi headerFileInfo) IsDir() bool { return fi.Mode().IsDir() } func (fi headerFileInfo) ModTime() time.Time { return fi.fh.ModTime() } func (fi headerFileInfo) Mode() os.FileMode { return fi.fh.Mode() } -func (fi headerFileInfo) Sys() interface{} { return fi.fh } +func (fi headerFileInfo) Sys() any { return fi.fh } // FileInfoHeader creates a partially-populated FileHeader from an // os.FileInfo. diff --git a/writer_test.go b/writer_test.go index 01b63f2..7eedfae 100644 --- a/writer_test.go +++ b/writer_test.go @@ -7,7 +7,7 @@ package zip import ( "bytes" "io" - "io/ioutil" + "math/rand" "os" "testing" @@ -167,7 +167,7 @@ func testReadFile(t *testing.T, f *File, wt *WriteTest) { if err != nil { t.Fatal("opening:", err) } - b, err := ioutil.ReadAll(rc) + b, err := io.ReadAll(rc) if err != nil { t.Fatal("reading:", err) } @@ -187,7 +187,7 @@ func BenchmarkCompressedZipGarbage(b *testing.B) { for i := 0; i < b.N; i++ { buf.Reset() zw := NewWriter(&buf) - for j := 0; j < 3; j++ { + for range 3 { w, _ := zw.CreateHeader(&FileHeader{ Name: "foo", Method: Deflate, diff --git a/zip_test.go b/zip_test.go index f00ff47..769dad8 100644 --- a/zip_test.go +++ b/zip_test.go @@ -11,7 +11,7 @@ import ( "fmt" "hash" "io" - "io/ioutil" + "sort" "strings" "testing" @@ -22,7 +22,7 @@ func TestOver65kFiles(t *testing.T) { buf := new(bytes.Buffer) w := NewWriter(buf) const nFiles = (1 << 16) + 42 - for i := 0; i < nFiles; i++ { + for i := range nFiles { _, err := w.CreateHeader(&FileHeader{ Name: fmt.Sprintf("%d.dat", i), Method: Store, // avoid Issue 6136 and Issue 6138 @@ -42,7 +42,7 @@ func TestOver65kFiles(t *testing.T) { if got := len(zr.File); got != nFiles { t.Fatalf("File contains %d files, want %d", got, nFiles) } - for i := 0; i < nFiles; i++ { + for i := range nFiles { want := fmt.Sprintf("%d.dat", i) if zr.File[i].Name != want { t.Fatalf("File(%d) = %q, want %q", i, zr.File[i].Name, want) @@ -251,7 +251,7 @@ func testZip64(t testing.TB, size int64) *rleBuffer { for i := range chunk { chunk[i] = '.' } - for i := 0; i < chunks; i++ { + for range chunks { _, err := f.Write(chunk) if err != nil { t.Fatal("write chunk:", err) @@ -277,13 +277,13 @@ func testZip64(t testing.TB, size int64) *rleBuffer { t.Fatal("opening:", err) } rc.(*checksumReader).hash = fakeHash32{} - for i := 0; i < chunks; i++ { + for range chunks { _, err := io.ReadFull(rc, chunk) if err != nil { t.Fatal("read:", err) } } - gotEnd, err := ioutil.ReadAll(rc) + gotEnd, err := io.ReadAll(rc) if err != nil { t.Fatal("read end:", err) } diff --git a/zipcrypto.go b/zipcrypto.go index 309bc32..a2bc02f 100644 --- a/zipcrypto.go +++ b/zipcrypto.go @@ -1,14 +1,14 @@ package zip import ( - "io" "bytes" "hash/crc32" + "io" ) type ZipCrypto struct { password []byte - Keys [3]uint32 + Keys [3]uint32 } func NewZipCrypto(passphrase []byte) *ZipCrypto { @@ -29,10 +29,10 @@ func (z *ZipCrypto) init() { } func (z *ZipCrypto) updateKeys(byteValue byte) { - z.Keys[0] = crc32update(z.Keys[0], byteValue); - z.Keys[1] += z.Keys[0] & 0xff; - z.Keys[1] = z.Keys[1] * 134775813 + 1; - z.Keys[2] = crc32update(z.Keys[2], (byte) (z.Keys[1] >> 24)); + z.Keys[0] = crc32update(z.Keys[0], byteValue) + z.Keys[1] += z.Keys[0] & 0xff + z.Keys[1] = z.Keys[1]*134775813 + 1 + z.Keys[2] = crc32update(z.Keys[2], (byte)(z.Keys[1]>>24)) } func (z *ZipCrypto) magicByte() byte { @@ -43,7 +43,7 @@ func (z *ZipCrypto) magicByte() byte { func (z *ZipCrypto) Encrypt(data []byte) []byte { length := len(data) chiper := make([]byte, length) - for i := 0; i < length; i++ { + for i := range length { v := data[i] chiper[i] = v ^ z.magicByte() z.updateKeys(v) @@ -55,7 +55,7 @@ func (z *ZipCrypto) Decrypt(chiper []byte) []byte { length := len(chiper) plain := make([]byte, length) for i, c := range chiper { - v := c ^ z.magicByte(); + v := c ^ z.magicByte() z.updateKeys(v) plain[i] = v } @@ -63,7 +63,7 @@ func (z *ZipCrypto) Decrypt(chiper []byte) []byte { } func crc32update(pCrc32 uint32, bval byte) uint32 { - return crc32.IEEETable[(pCrc32 ^ uint32(bval)) & 0xff] ^ (pCrc32 >> 8) + return crc32.IEEETable[(pCrc32^uint32(bval))&0xff] ^ (pCrc32 >> 8) } func ZipCryptoDecryptor(r *io.SectionReader, password []byte) (*io.SectionReader, error) { @@ -102,8 +102,8 @@ func (z *zipCryptoWriter) Write(p []byte) (n int, err error) { return } -func ZipCryptoEncryptor(i io.Writer, pass passwordFn, fw *fileWriter) (io.Writer, error) { +func ZipCryptoEncryptor(i io.Writer, pass passwordFn, fw *fileWriter) (io.Writer, error) { z := NewZipCrypto(pass()) zc := &zipCryptoWriter{i, z, true, fw} return zc, nil -} \ No newline at end of file +}