github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/snappy/decode.go (about)

     1  // Copyright 2011 The Snappy-Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package snappy
     6  
     7  import (
     8  	"encoding/binary"
     9  	"errors"
    10  	"io"
    11  )
    12  
    13  var (
    14  	// ErrCorrupt reports that the input is invalid.
    15  	ErrCorrupt = errors.New("snappy: corrupt input")
    16  	// ErrTooLarge reports that the uncompressed length is too large.
    17  	ErrTooLarge = errors.New("snappy: decoded block is too large")
    18  	// ErrUnsupported reports that the input isn't supported.
    19  	ErrUnsupported = errors.New("snappy: unsupported input")
    20  
    21  	errUnsupportedCopy4Tag      = errors.New("snappy: unsupported COPY_4 tag")
    22  	errUnsupportedLiteralLength = errors.New("snappy: unsupported literal length")
    23  )
    24  
    25  // DecodedLen returns the length of the decoded block.
    26  func DecodedLen(src []byte) (int, error) {
    27  	v, _, err := decodedLen(src)
    28  	return v, err
    29  }
    30  
    31  // decodedLen returns the length of the decoded block and the number of bytes
    32  // that the length header occupied.
    33  func decodedLen(src []byte) (blockLen, headerLen int, err error) {
    34  	v, n := binary.Uvarint(src)
    35  	if n <= 0 || v > 0xffffffff {
    36  		return 0, 0, ErrCorrupt
    37  	}
    38  
    39  	const wordSize = 32 << (^uint(0) >> 32 & 1)
    40  	if wordSize == 32 && v > 0x7fffffff {
    41  		return 0, 0, ErrTooLarge
    42  	}
    43  	return int(v), n, nil
    44  }
    45  
    46  const (
    47  	decodeErrCodeCorrupt                  = 1
    48  	decodeErrCodeUnsupportedLiteralLength = 2
    49  	decodeErrCodeUnsupportedCopy4Tag      = 3
    50  )
    51  
    52  // Decode returns the decoded form of src. The returned slice may be a sub-
    53  // slice of dst if dst was large enough to hold the entire decoded block.
    54  // Otherwise, a newly allocated slice will be returned.
    55  //
    56  // The dst and src must not overlap. It is valid to pass a nil dst.
    57  func Decode(dst, src []byte) ([]byte, error) {
    58  	dLen, s, err := decodedLen(src)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	if dLen <= len(dst) {
    63  		dst = dst[:dLen]
    64  	} else {
    65  		dst = make([]byte, dLen)
    66  	}
    67  	switch decode(dst, src[s:]) {
    68  	case 0:
    69  		return dst, nil
    70  	case decodeErrCodeUnsupportedLiteralLength:
    71  		return nil, errUnsupportedLiteralLength
    72  	case decodeErrCodeUnsupportedCopy4Tag:
    73  		return nil, errUnsupportedCopy4Tag
    74  	}
    75  	return nil, ErrCorrupt
    76  }
    77  
    78  // NewReader returns a new Reader that decompresses from r, using the framing
    79  // format described at
    80  // https://yougam/libraries/google/snappy/blob/master/framing_format.txt
    81  func NewReader(r io.Reader) *Reader {
    82  	return &Reader{
    83  		r:       r,
    84  		decoded: make([]byte, maxBlockSize),
    85  		buf:     make([]byte, maxEncodedLenOfMaxBlockSize+checksumSize),
    86  	}
    87  }
    88  
    89  // Reader is an io.Reader that can read Snappy-compressed bytes.
    90  type Reader struct {
    91  	r       io.Reader
    92  	err     error
    93  	decoded []byte
    94  	buf     []byte
    95  	// decoded[i:j] contains decoded bytes that have not yet been passed on.
    96  	i, j       int
    97  	readHeader bool
    98  }
    99  
   100  // Reset discards any buffered data, resets all state, and switches the Snappy
   101  // reader to read from r. This permits reusing a Reader rather than allocating
   102  // a new one.
   103  func (r *Reader) Reset(reader io.Reader) {
   104  	r.r = reader
   105  	r.err = nil
   106  	r.i = 0
   107  	r.j = 0
   108  	r.readHeader = false
   109  }
   110  
   111  func (r *Reader) readFull(p []byte, allowEOF bool) (ok bool) {
   112  	if _, r.err = io.ReadFull(r.r, p); r.err != nil {
   113  		if r.err == io.ErrUnexpectedEOF || (r.err == io.EOF && !allowEOF) {
   114  			r.err = ErrCorrupt
   115  		}
   116  		return false
   117  	}
   118  	return true
   119  }
   120  
   121  // Read satisfies the io.Reader interface.
   122  func (r *Reader) Read(p []byte) (int, error) {
   123  	if r.err != nil {
   124  		return 0, r.err
   125  	}
   126  	for {
   127  		if r.i < r.j {
   128  			n := copy(p, r.decoded[r.i:r.j])
   129  			r.i += n
   130  			return n, nil
   131  		}
   132  		if !r.readFull(r.buf[:4], true) {
   133  			return 0, r.err
   134  		}
   135  		chunkType := r.buf[0]
   136  		if !r.readHeader {
   137  			if chunkType != chunkTypeStreamIdentifier {
   138  				r.err = ErrCorrupt
   139  				return 0, r.err
   140  			}
   141  			r.readHeader = true
   142  		}
   143  		chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16
   144  		if chunkLen > len(r.buf) {
   145  			r.err = ErrUnsupported
   146  			return 0, r.err
   147  		}
   148  
   149  		// The chunk types are specified at
   150  		// https://yougam/libraries/google/snappy/blob/master/framing_format.txt
   151  		switch chunkType {
   152  		case chunkTypeCompressedData:
   153  			// Section 4.2. Compressed data (chunk type 0x00).
   154  			if chunkLen < checksumSize {
   155  				r.err = ErrCorrupt
   156  				return 0, r.err
   157  			}
   158  			buf := r.buf[:chunkLen]
   159  			if !r.readFull(buf, false) {
   160  				return 0, r.err
   161  			}
   162  			checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
   163  			buf = buf[checksumSize:]
   164  
   165  			n, err := DecodedLen(buf)
   166  			if err != nil {
   167  				r.err = err
   168  				return 0, r.err
   169  			}
   170  			if n > len(r.decoded) {
   171  				r.err = ErrCorrupt
   172  				return 0, r.err
   173  			}
   174  			if _, err := Decode(r.decoded, buf); err != nil {
   175  				r.err = err
   176  				return 0, r.err
   177  			}
   178  			if crc(r.decoded[:n]) != checksum {
   179  				r.err = ErrCorrupt
   180  				return 0, r.err
   181  			}
   182  			r.i, r.j = 0, n
   183  			continue
   184  
   185  		case chunkTypeUncompressedData:
   186  			// Section 4.3. Uncompressed data (chunk type 0x01).
   187  			if chunkLen < checksumSize {
   188  				r.err = ErrCorrupt
   189  				return 0, r.err
   190  			}
   191  			buf := r.buf[:checksumSize]
   192  			if !r.readFull(buf, false) {
   193  				return 0, r.err
   194  			}
   195  			checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
   196  			// Read directly into r.decoded instead of via r.buf.
   197  			n := chunkLen - checksumSize
   198  			if n > len(r.decoded) {
   199  				r.err = ErrCorrupt
   200  				return 0, r.err
   201  			}
   202  			if !r.readFull(r.decoded[:n], false) {
   203  				return 0, r.err
   204  			}
   205  			if crc(r.decoded[:n]) != checksum {
   206  				r.err = ErrCorrupt
   207  				return 0, r.err
   208  			}
   209  			r.i, r.j = 0, n
   210  			continue
   211  
   212  		case chunkTypeStreamIdentifier:
   213  			// Section 4.1. Stream identifier (chunk type 0xff).
   214  			if chunkLen != len(magicBody) {
   215  				r.err = ErrCorrupt
   216  				return 0, r.err
   217  			}
   218  			if !r.readFull(r.buf[:len(magicBody)], false) {
   219  				return 0, r.err
   220  			}
   221  			for i := 0; i < len(magicBody); i++ {
   222  				if r.buf[i] != magicBody[i] {
   223  					r.err = ErrCorrupt
   224  					return 0, r.err
   225  				}
   226  			}
   227  			continue
   228  		}
   229  
   230  		if chunkType <= 0x7f {
   231  			// Section 4.5. Reserved unskippable chunks (chunk types 0x02-0x7f).
   232  			r.err = ErrUnsupported
   233  			return 0, r.err
   234  		}
   235  		// Section 4.4 Padding (chunk type 0xfe).
   236  		// Section 4.6. Reserved skippable chunks (chunk types 0x80-0xfd).
   237  		if !r.readFull(r.buf[:chunkLen], false) {
   238  			return 0, r.err
   239  		}
   240  	}
   241  }