Browse Source

exported errors
fixed lz4stream/gen

Pierre.Curto 5 years ago
parent
commit
690d69886a
6 changed files with 133 additions and 48 deletions
  1. 2 7
      .travis.yml
  2. 1 0
      go.sum
  3. 10 22
      internal/lz4errors/errors.go
  4. 68 16
      internal/lz4stream/frame_gen.go
  5. 25 3
      internal/lz4stream/gen.go
  6. 27 0
      lz4.go

+ 2 - 7
.travis.yml

@@ -4,16 +4,11 @@ env:
   - GO111MODULE=off
 
 go:
-  - 1.9.x
-  - 1.10.x
-  - 1.11.x
-  - 1.12.x
-  - master
+  - 1.13.x
+  - 1.14.x
 
 matrix:
  fast_finish: true
- allow_failures:
-   - go: master
 
 sudo: false
 

+ 1 - 0
go.sum

@@ -18,6 +18,7 @@ github.com/schollz/progressbar v1.0.0/go.mod h1:/l9I7PC3L3erOuz54ghIRKUEFcosiWfL
 github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
+golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
 golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
 golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=

+ 10 - 22
internal/lz4errors/errors.go

@@ -5,27 +5,15 @@ type Error string
 func (e Error) Error() string { return string(e) }
 
 const (
-	// ErrInvalidSourceShortBuffer is returned by UncompressBlock or CompressBLock when a compressed
-	// block is corrupted or the destination buffer is not large enough for the uncompressed data.
-	ErrInvalidSourceShortBuffer Error = "lz4: invalid source or destination buffer too short"
-	// ErrInvalidFrame is returned when reading an invalid LZ4 archive.
-	ErrInvalidFrame Error = "lz4: bad magic number"
-	// ErrInternalUnhandledState is an internal error.
-	ErrInternalUnhandledState Error = "lz4: unhandled state"
-	// ErrInvalidHeaderChecksum is returned when reading a frame.
-	ErrInvalidHeaderChecksum Error = "lz4: invalid header checksum"
-	// ErrInvalidBlockChecksum is returned when reading a frame.
-	ErrInvalidBlockChecksum Error = "lz4: invalid block checksum"
-	// ErrInvalidFrameChecksum is returned when reading a frame.
-	ErrInvalidFrameChecksum Error = "lz4: invalid frame checksum"
-	// ErrOptionInvalidCompressionLevel is returned when the supplied compression level is invalid.
+	ErrInvalidSourceShortBuffer      Error = "lz4: invalid source or destination buffer too short"
+	ErrInvalidFrame                  Error = "lz4: bad magic number"
+	ErrInternalUnhandledState        Error = "lz4: unhandled state"
+	ErrInvalidHeaderChecksum         Error = "lz4: invalid header checksum"
+	ErrInvalidBlockChecksum          Error = "lz4: invalid block checksum"
+	ErrInvalidFrameChecksum          Error = "lz4: invalid frame checksum"
 	ErrOptionInvalidCompressionLevel Error = "lz4: invalid compression level"
-	// ErrOptionClosedOrError is returned when an option is applied to a closed or in error object.
-	ErrOptionClosedOrError Error = "lz4: cannot apply options on closed or in error object"
-	// ErrOptionInvalidBlockSize is returned when
-	ErrOptionInvalidBlockSize Error = "lz4: invalid block size"
-	// ErrOptionNotApplicable is returned when trying to apply an option to an object not supporting it.
-	ErrOptionNotApplicable Error = "lz4: option not applicable"
-	// ErrWriterNotClosed is returned when attempting to reset an unclosed writer.
-	ErrWriterNotClosed Error = "lz4: writer not closed"
+	ErrOptionClosedOrError           Error = "lz4: cannot apply options on closed or in error object"
+	ErrOptionInvalidBlockSize        Error = "lz4: invalid block size"
+	ErrOptionNotApplicable           Error = "lz4: option not applicable"
+	ErrWriterNotClosed               Error = "lz4: writer not closed"
 )

+ 68 - 16
internal/lz4stream/frame_gen.go

@@ -19,33 +19,85 @@ import "github.com/pierrec/lz4/internal/lz4block"
 type DescriptorFlags uint16
 
 // Getters.
-func (x DescriptorFlags) ContentChecksum() bool              { return x>>2&1 != 0 }
-func (x DescriptorFlags) Size() bool                         { return x>>3&1 != 0 }
-func (x DescriptorFlags) BlockChecksum() bool                { return x>>4&1 != 0 }
-func (x DescriptorFlags) BlockIndependence() bool            { return x>>5&1 != 0 }
-func (x DescriptorFlags) Version() uint16                    { return uint16(x>>6&0x3) }
-func (x DescriptorFlags) BlockSizeIndex() lz4block.BlockSizeIndex { return lz4block.BlockSizeIndex(x>>12&0x7) }
+func (x DescriptorFlags) ContentChecksum() bool   { return x>>2&1 != 0 }
+func (x DescriptorFlags) Size() bool              { return x>>3&1 != 0 }
+func (x DescriptorFlags) BlockChecksum() bool     { return x>>4&1 != 0 }
+func (x DescriptorFlags) BlockIndependence() bool { return x>>5&1 != 0 }
+func (x DescriptorFlags) Version() uint16         { return uint16(x >> 6 & 0x3) }
+func (x DescriptorFlags) BlockSizeIndex() lz4block.BlockSizeIndex {
+	return lz4block.BlockSizeIndex(x >> 12 & 0x7)
+}
 
 // Setters.
-func (x *DescriptorFlags) ContentChecksumSet(v bool) *DescriptorFlags { const b = 1<<2; if v { *x = *x&^b | b } else { *x &^= b }; return x }
-func (x *DescriptorFlags) SizeSet(v bool) *DescriptorFlags { const b = 1<<3; if v { *x = *x&^b | b } else { *x &^= b }; return x }
-func (x *DescriptorFlags) BlockChecksumSet(v bool) *DescriptorFlags { const b = 1<<4; if v { *x = *x&^b | b } else { *x &^= b }; return x }
-func (x *DescriptorFlags) BlockIndependenceSet(v bool) *DescriptorFlags { const b = 1<<5; if v { *x = *x&^b | b } else { *x &^= b }; return x }
-func (x *DescriptorFlags) VersionSet(v uint16) *DescriptorFlags { *x = *x&^(0x3<<6) | (DescriptorFlags(v)&0x3<<6); return x }
-func (x *DescriptorFlags) BlockSizeIndexSet(v lz4block.BlockSizeIndex) *DescriptorFlags { *x = *x&^(0x7<<12) | (DescriptorFlags(v)&0x7<<12); return x }
+func (x *DescriptorFlags) ContentChecksumSet(v bool) *DescriptorFlags {
+	const b = 1 << 2
+	if v {
+		*x = *x&^b | b
+	} else {
+		*x &^= b
+	}
+	return x
+}
+func (x *DescriptorFlags) SizeSet(v bool) *DescriptorFlags {
+	const b = 1 << 3
+	if v {
+		*x = *x&^b | b
+	} else {
+		*x &^= b
+	}
+	return x
+}
+func (x *DescriptorFlags) BlockChecksumSet(v bool) *DescriptorFlags {
+	const b = 1 << 4
+	if v {
+		*x = *x&^b | b
+	} else {
+		*x &^= b
+	}
+	return x
+}
+func (x *DescriptorFlags) BlockIndependenceSet(v bool) *DescriptorFlags {
+	const b = 1 << 5
+	if v {
+		*x = *x&^b | b
+	} else {
+		*x &^= b
+	}
+	return x
+}
+func (x *DescriptorFlags) VersionSet(v uint16) *DescriptorFlags {
+	*x = *x&^(0x3<<6) | (DescriptorFlags(v) & 0x3 << 6)
+	return x
+}
+func (x *DescriptorFlags) BlockSizeIndexSet(v lz4block.BlockSizeIndex) *DescriptorFlags {
+	*x = *x&^(0x7<<12) | (DescriptorFlags(v) & 0x7 << 12)
+	return x
+}
+
 // Code generated by `gen.exe`. DO NOT EDIT.
 
 // DataBlockSize is defined as follow:
 //   field         bits
 //   -----         ----
 //   size          31
-//   uncompressed  1
+//   Uncompressed  1
 type DataBlockSize uint32
 
 // Getters.
-func (x DataBlockSize) size() int          { return int(x&0x7FFFFFFF) }
+func (x DataBlockSize) size() int          { return int(x & 0x7FFFFFFF) }
 func (x DataBlockSize) Uncompressed() bool { return x>>31&1 != 0 }
 
 // Setters.
-func (x *DataBlockSize) sizeSet(v int) *DataBlockSize { *x = *x&^0x7FFFFFFF | DataBlockSize(v)&0x7FFFFFFF; return x }
-func (x *DataBlockSize) UncompressedSet(v bool) *DataBlockSize { const b = 1<<31; if v { *x = *x&^b | b } else { *x &^= b }; return x }
+func (x *DataBlockSize) sizeSet(v int) *DataBlockSize {
+	*x = *x&^0x7FFFFFFF | DataBlockSize(v)&0x7FFFFFFF
+	return x
+}
+func (x *DataBlockSize) UncompressedSet(v bool) *DataBlockSize {
+	const b = 1 << 31
+	if v {
+		*x = *x&^b | b
+	} else {
+		*x &^= b
+	}
+	return x
+}

+ 25 - 3
internal/lz4stream/gen.go

@@ -3,10 +3,15 @@
 package main
 
 import (
+	"bytes"
+	"fmt"
+	"io"
 	"log"
 	"os"
 
+	"github.com/pierrec/lz4/internal/lz4block"
 	"github.com/pierrec/packer"
+	"golang.org/x/tools/imports"
 )
 
 type DescriptorFlags struct {
@@ -29,22 +34,39 @@ type DataBlockSize struct {
 }
 
 func main() {
-	out, err := os.Create("frame_gen.go")
+	err := do()
 	if err != nil {
 		log.Fatal(err)
 	}
+}
+
+func do() error {
+	out, err := os.Create("frame_gen.go")
+	if err != nil {
+		return err
+	}
 	defer out.Close()
 
 	pkg := "lz4stream"
+	buf := new(bytes.Buffer)
 	for i, t := range []interface{}{
 		DescriptorFlags{}, DataBlockSize{},
 	} {
 		if i > 0 {
 			pkg = ""
 		}
-		err := packer.GenPackedStruct(out, &packer.Config{PkgName: pkg}, t)
+		err := packer.GenPackedStruct(buf, &packer.Config{PkgName: pkg}, t)
 		if err != nil {
-			log.Fatalf("%T: %v", t, err)
+			return fmt.Errorf("%T: %v", t, err)
 		}
 	}
+	// Resolve imports.
+	code, err := imports.Process("", buf.Bytes(), nil)
+	if err != nil {
+		// Output without imports.
+		_, _ = io.Copy(out, buf)
+		return err
+	}
+	_, err = io.Copy(out, bytes.NewReader(code))
+	return err
 }

+ 27 - 0
lz4.go

@@ -2,6 +2,7 @@ package lz4
 
 import (
 	"github.com/pierrec/lz4/internal/lz4block"
+	"github.com/pierrec/lz4/internal/lz4errors"
 )
 
 func _() {
@@ -60,3 +61,29 @@ func CompressBlock(src, dst []byte, hashTable []int) (_ int, err error) {
 func CompressBlockHC(src, dst []byte, depth CompressionLevel, hashTable []int) (_ int, err error) {
 	return lz4block.CompressBlockHC(src, dst, lz4block.CompressionLevel(depth), hashTable)
 }
+
+const (
+	// ErrInvalidSourceShortBuffer is returned by UncompressBlock or CompressBLock when a compressed
+	// block is corrupted or the destination buffer is not large enough for the uncompressed data.
+	ErrInvalidSourceShortBuffer = lz4errors.ErrInvalidSourceShortBuffer
+	// ErrInvalidFrame is returned when reading an invalid LZ4 archive.
+	ErrInvalidFrame = lz4errors.ErrInvalidFrame
+	// ErrInternalUnhandledState is an internal error.
+	ErrInternalUnhandledState = lz4errors.ErrInternalUnhandledState
+	// ErrInvalidHeaderChecksum is returned when reading a frame.
+	ErrInvalidHeaderChecksum = lz4errors.ErrInvalidHeaderChecksum
+	// ErrInvalidBlockChecksum is returned when reading a frame.
+	ErrInvalidBlockChecksum = lz4errors.ErrInvalidBlockChecksum
+	// ErrInvalidFrameChecksum is returned when reading a frame.
+	ErrInvalidFrameChecksum = lz4errors.ErrInvalidFrameChecksum
+	// ErrOptionInvalidCompressionLevel is returned when the supplied compression level is invalid.
+	ErrOptionInvalidCompressionLevel = lz4errors.ErrOptionInvalidCompressionLevel
+	// ErrOptionClosedOrError is returned when an option is applied to a closed or in error object.
+	ErrOptionClosedOrError = lz4errors.ErrOptionClosedOrError
+	// ErrOptionInvalidBlockSize is returned when
+	ErrOptionInvalidBlockSize = lz4errors.ErrOptionInvalidBlockSize
+	// ErrOptionNotApplicable is returned when trying to apply an option to an object not supporting it.
+	ErrOptionNotApplicable = lz4errors.ErrOptionNotApplicable
+	// ErrWriterNotClosed is returned when attempting to reset an unclosed writer.
+	ErrWriterNotClosed = lz4errors.ErrWriterNotClosed
+)