Consolidate lib/rac's error messages

This commit is contained in:
Nigel Tao committed 2019-08-08 16:05:58 +10:00
1 parent 46a14c25f5
commit 685cf410bd
5 files changed
+59 -50

No files matched your search

+15 -16
View File
@@ -15,7 +15,6 @@
package rac package rac
import ( import (
"errors"
"hash/crc32" "hash/crc32"
"io" "io"
"sort" "sort"
@@ -161,11 +160,11 @@ type ChunkWriter struct {
func (w *ChunkWriter) checkParameters() error { func (w *ChunkWriter) checkParameters() error {
if w.Writer == nil { if w.Writer == nil {
w.err = errors.New("rac: invalid Writer") w.err = errInvalidWriter
return w.err return w.err
} }
if !isZeroOrAPowerOf2(w.CPageSize) || (w.CPageSize > MaxSize) { if !isZeroOrAPowerOf2(w.CPageSize) || (w.CPageSize > MaxSize) {
w.err = errors.New("rac: invalid CPageSize") w.err = errInvalidCPageSize
return w.err return w.err
} else if w.CPageSize > 0 { } else if w.CPageSize > 0 {
for w.log2CPageSize = 1; w.CPageSize != (1 << w.log2CPageSize); w.log2CPageSize++ { for w.log2CPageSize = 1; w.CPageSize != (1 << w.log2CPageSize); w.log2CPageSize++ {
@@ -199,14 +198,14 @@ func (w *ChunkWriter) initialize() error {
switch w.IndexLocation { switch w.IndexLocation {
case IndexLocationAtEnd: case IndexLocationAtEnd:
if w.TempFile != nil { if w.TempFile != nil {
w.err = errors.New("rac: IndexLocationAtEnd requires a nil TempFile") w.err = errILAEndTempFile
return w.err return w.err
} }
return w.write(indexLocationAtEndMagic) return w.write(indexLocationAtEndMagic)
case IndexLocationAtStart: case IndexLocationAtStart:
if w.TempFile == nil { if w.TempFile == nil {
w.err = errors.New("rac: IndexLocationAtStart requires a non-nil TempFile") w.err = errILAStartTempFile
return w.err return w.err
} }
} }
@@ -220,7 +219,7 @@ func (w *ChunkWriter) write(data []byte) error {
} }
if uint64(len(data)) > MaxSize { if uint64(len(data)) > MaxSize {
w.err = errors.New("rac: too much input") w.err = errTooMuchInput
return w.err return w.err
} }
if w.CPageSize > 0 { if w.CPageSize > 0 {
@@ -234,7 +233,7 @@ func (w *ChunkWriter) write(data []byte) error {
} }
w.dataSize += uint64(len(data)) w.dataSize += uint64(len(data))
if w.dataSize > MaxSize { if w.dataSize > MaxSize {
w.err = errors.New("rac: too much input") w.err = errTooMuchInput
return w.err return w.err
} }
return nil return nil
@@ -285,7 +284,7 @@ func (w *ChunkWriter) padToPageSize(ioWriter io.Writer, offset uint64) error {
remaining -= uint64(n) remaining -= uint64(n)
w.dataSize += uint64(n) w.dataSize += uint64(n)
if w.dataSize > MaxSize { if w.dataSize > MaxSize {
w.err = errors.New("rac: too much input") w.err = errTooMuchInput
return w.err return w.err
} }
} }
@@ -307,7 +306,7 @@ func (w *ChunkWriter) roundUpToCPageBoundary(x uint64) uint64 {
// The caller may modify resource's contents after this method returns. // The caller may modify resource's contents after this method returns.
func (w *ChunkWriter) AddResource(resource []byte) (OptResource, error) { func (w *ChunkWriter) AddResource(resource []byte) (OptResource, error) {
if len(w.resourcesCOffCLens) >= (1 << 30) { if len(w.resourcesCOffCLens) >= (1 << 30) {
w.err = errors.New("rac: too many resources") w.err = errTooManyResources
return 0, w.err return 0, w.err
} }
@@ -345,15 +344,15 @@ func (w *ChunkWriter) AddChunk(
return nil return nil
} }
if (dRangeSize > MaxSize) || ((w.dFileSize + dRangeSize) > MaxSize) { if (dRangeSize > MaxSize) || ((w.dFileSize + dRangeSize) > MaxSize) {
w.err = errors.New("rac: too much input") w.err = errTooMuchInput
return w.err return w.err
} }
if len(w.leafNodes) >= (1 << 30) { if len(w.leafNodes) >= (1 << 30) {
w.err = errors.New("rac: too many chunks") w.err = errTooManyChunks
return w.err return w.err
} }
if codec == 0 { if codec == 0 {
w.err = errors.New("rac: invalid Codec") w.err = errInvalidCodec
return w.err return w.err
} }
@@ -428,7 +427,7 @@ func (w *ChunkWriter) Close() error {
nw.cFileSize = nw.dataCOffset + w.dataSize nw.cFileSize = nw.dataCOffset + w.dataSize
} }
if nw.cFileSize > MaxSize { if nw.cFileSize > MaxSize {
w.err = errors.New("rac: too much input") w.err = errTooMuchInput
return w.err return w.err
} }
@@ -473,12 +472,12 @@ func (w *ChunkWriter) Close() error {
w.err = err w.err = err
return err return err
} else if uint64(n) != expectedTempFileSize { } else if uint64(n) != expectedTempFileSize {
w.err = errors.New("rac: inconsistent compressed data size") w.err = errInconsistentCompressedSize
return w.err return w.err
} }
} }
w.err = errors.New("rac: Writer is closed") w.err = errWriterIsClosed
return nil return nil
} }
@@ -554,7 +553,7 @@ func (w *nodeWriter) writeIndex(n *wNode, rootAndIsAtEnd bool) error {
buf, dPtr := w.buffer[:], uint64(0) buf, dPtr := w.buffer[:], uint64(0)
arity := uint64(len(n.children) + len(n.resources)) arity := uint64(len(n.children) + len(n.resources))
if arity > 0xFF { if arity > 0xFF {
return errors.New("rac: internal error: arity is too large") return errInternalArityIsTooLarge
} }
// DPtr's. We write resources before regular children (non-resources), so // DPtr's. We write resources before regular children (non-resources), so
+7 -12
View File
@@ -15,15 +15,10 @@
package rac package rac
import ( import (
"errors"
"hash/crc32" "hash/crc32"
"io" "io"
) )
var (
errInvalidIndexNode = errors.New("rac: invalid index node")
)
func u48LE(b []byte) int64 { func u48LE(b []byte) int64 {
_ = b[7] // Early bounds check to guarantee safety of reads below. _ = b[7] // Early bounds check to guarantee safety of reads below.
u := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | u := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
@@ -304,11 +299,11 @@ type Parser struct {
func (r *Parser) checkParameters() error { func (r *Parser) checkParameters() error {
if r.ReadSeeker == nil { if r.ReadSeeker == nil {
r.err = errors.New("rac: invalid ReadSeeker") r.err = errInvalidReadSeeker
return r.err return r.err
} }
if r.CompressedSize < 32 { if r.CompressedSize < 32 {
r.err = errors.New("rac: invalid CompressedSize") r.err = errInvalidCompressedSize
return r.err return r.err
} }
return nil return nil
@@ -331,7 +326,7 @@ func (r *Parser) initialize() error {
return err return err
} }
if r.currNode.version() != 1 { if r.currNode.version() != 1 {
r.err = errors.New("rac: unsupported RAC file version") r.err = errUnsupportedRACFileVersion
return r.err return r.err
} }
return nil return nil
@@ -346,7 +341,7 @@ func (r *Parser) findRootNode() error {
if (r.currNode[0] != magic[0]) || if (r.currNode[0] != magic[0]) ||
(r.currNode[1] != magic[1]) || (r.currNode[1] != magic[1]) ||
(r.currNode[2] != magic[2]) { (r.currNode[2] != magic[2]) {
r.err = errors.New("rac: invalid input: missing magic bytes at the start of file") r.err = errInvalidInputMissingMagicBytes
return r.err return r.err
} }
if found, err := r.tryRootNode(r.currNode[3], false); err != nil { if found, err := r.tryRootNode(r.currNode[3], false); err != nil {
@@ -366,7 +361,7 @@ func (r *Parser) findRootNode() error {
return nil return nil
} }
return errors.New("rac: invalid input: missing index root node") return errInvalidInputMissingRootNode
} }
func (r *Parser) tryRootNode(arity uint8, fromEnd bool) (found bool, ioErr error) { func (r *Parser) tryRootNode(arity uint8, fromEnd bool) (found bool, ioErr error) {
@@ -402,7 +397,7 @@ func (r *Parser) tryRootNode(arity uint8, fromEnd bool) (found bool, ioErr error
// that it is valid. // that it is valid.
func (r *Parser) load(cOffset int64, arity uint8) error { func (r *Parser) load(cOffset int64, arity uint8) error {
if arity == 0 { if arity == 0 {
r.err = errors.New("rac: internal error: inconsistent arity") r.err = errInternalInconsistentArity
return r.err return r.err
} }
size := nodeSize(arity) size := nodeSize(arity)
@@ -473,7 +468,7 @@ func (r *Parser) SeekToChunkContaining(dSpaceOffset int64) error {
return err return err
} }
if dSpaceOffset < 0 { if dSpaceOffset < 0 {
r.err = errors.New("rac: seek: negative position") r.err = errSeekToNegativePosition
return r.err return r.err
} }
r.needToResolveSeekPosition = true r.needToResolveSeekPosition = true
+35
View File
@@ -25,6 +25,10 @@
// https://github.com/google/wuffs/blob/master/doc/spec/rac-spec.md // https://github.com/google/wuffs/blob/master/doc/spec/rac-spec.md
package rac package rac
import (
"errors"
)
const ( const (
// MaxSize is the maximum RAC file size (in both CSpace and DSpace). // MaxSize is the maximum RAC file size (in both CSpace and DSpace).
MaxSize = (1 << 48) - 1 MaxSize = (1 << 48) - 1
@@ -63,3 +67,34 @@ var indexLocationAtEndMagic = []byte("\x72\xC3\x63\x00")
// //
// Zero means that the option is not taken: no shared resource is used. // Zero means that the option is not taken: no shared resource is used.
type OptResource uint32 type OptResource uint32
var (
errCChunkSizeIsTooSmall = errors.New("rac: CChunkSize is too small")
errILAEndTempFile = errors.New("rac: IndexLocationAtEnd requires a nil TempFile")
errILAStartTempFile = errors.New("rac: IndexLocationAtStart requires a non-nil TempFile")
errInconsistentCompressedSize = errors.New("rac: inconsistent compressed size")
errInvalidCPageSize = errors.New("rac: invalid CPageSize")
errInvalidChunk = errors.New("rac: invalid chunk")
errInvalidChunkTooLarge = errors.New("rac: invalid chunk (too large)")
errInvalidChunkTruncated = errors.New("rac: invalid chunk (truncated)")
errInvalidCodec = errors.New("rac: invalid Codec")
errInvalidCodecWriter = errors.New("rac: invalid CodecWriter")
errInvalidCompressedSize = errors.New("rac: invalid CompressedSize")
errInvalidIndexNode = errors.New("rac: invalid index node")
errInvalidInputMissingMagicBytes = errors.New("rac: invalid input: missing magic bytes")
errInvalidInputMissingRootNode = errors.New("rac: invalid input: missing root node")
errInvalidReadSeeker = errors.New("rac: invalid ReadSeeker")
errInvalidWriter = errors.New("rac: invalid Writer")
errSeekToInvalidWhence = errors.New("rac: seek to invalid whence")
errSeekToNegativePosition = errors.New("rac: seek to negative position")
errTooManyChunks = errors.New("rac: too many chunks")
errTooManyResources = errors.New("rac: too many resources")
errTooMuchInput = errors.New("rac: too much input")
errUnsupportedRACFileVersion = errors.New("rac: unsupported RAC file version")
errWriterIsClosed = errors.New("rac: Writer is closed")
errInternalArityIsTooLarge = errors.New("rac: internal error: arity is too large")
errInternalInconsistentArity = errors.New("rac: internal error: inconsistent arity")
errInternalInconsistentPosition = errors.New("rac: internal error: inconsistent position")
errInternalShortCSize = errors.New("rac: internal error: short CSize")
)
+2 -12
View File
@@ -15,20 +15,10 @@
package rac package rac
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
) )
var (
errInvalidChunk = errors.New("rac: invalid chunk")
errInvalidChunkTooLarge = errors.New("rac: invalid chunk (too large)")
errInvalidChunkTruncated = errors.New("rac: invalid chunk (truncated)")
errInvalidReadSeeker = errors.New("rac: invalid ReadSeeker")
errInternalInconsistentPosition = errors.New("rac: internal error: inconsistent position")
)
// ReaderContext contains the decoded Codec-specific metadata (non-primary // ReaderContext contains the decoded Codec-specific metadata (non-primary
// data) associated with a RAC chunk. // data) associated with a RAC chunk.
type ReaderContext struct { type ReaderContext struct {
@@ -377,12 +367,12 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) {
} }
pos = end + offset pos = end + offset
default: default:
return 0, errors.New("rac.Reader.Seek: invalid whence") return 0, errSeekToInvalidWhence
} }
if r.pos != pos { if r.pos != pos {
if pos < 0 { if pos < 0 {
r.err = errors.New("rac.Reader.Seek: negative position") r.err = errSeekToNegativePosition
return 0, r.err return 0, r.err
} }
if err := r.parser.SeekToChunkContaining(pos); err != nil { if err := r.parser.SeekToChunkContaining(pos); err != nil {
-10
View File
@@ -17,7 +17,6 @@ package rac
// TODO: API for shared dictionaries. // TODO: API for shared dictionaries.
import ( import (
"errors"
"io" "io"
) )
@@ -28,15 +27,6 @@ const (
maxTargetDChunkSize = 1 << 31 // 2 GiB. maxTargetDChunkSize = 1 << 31 // 2 GiB.
) )
var (
errCChunkSizeIsTooSmall = errors.New("rac: CChunkSize is too small")
errInvalidCodecWriter = errors.New("rac: invalid CodecWriter")
errInvalidWriter = errors.New("rac: invalid Writer")
errWriterIsClosed = errors.New("rac: Writer is closed")
errInternalShortCSize = errors.New("rac: internal error: short CSize")
)
func startingTargetDChunkSize(cChunkSize uint64) uint64 { return 2 * cChunkSize } func startingTargetDChunkSize(cChunkSize uint64) uint64 { return 2 * cChunkSize }
// stripTrailingZeroes returns b shorn of any trailing '\x00' bytes. The RAC // stripTrailingZeroes returns b shorn of any trailing '\x00' bytes. The RAC