github.com/vipernet-xyz/tm@v0.34.24/libs/protoio/reader.go (about)

     1  // Protocol Buffers for Go with Gadgets
     2  //
     3  // Copyright (c) 2013, The GoGo Authors. All rights reserved.
     4  // http://github.com/gogo/protobuf
     5  //
     6  // Redistribution and use in source and binary forms, with or without
     7  // modification, are permitted provided that the following conditions are
     8  // met:
     9  //
    10  //     * Redistributions of source code must retain the above copyright
    11  // notice, this list of conditions and the following disclaimer.
    12  //     * Redistributions in binary form must reproduce the above
    13  // copyright notice, this list of conditions and the following disclaimer
    14  // in the documentation and/or other materials provided with the
    15  // distribution.
    16  //
    17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    28  //
    29  // Modified from original GoGo Protobuf to not buffer the reader.
    30  
    31  package protoio
    32  
    33  import (
    34  	"bytes"
    35  	"encoding/binary"
    36  	"fmt"
    37  	"io"
    38  
    39  	"github.com/gogo/protobuf/proto"
    40  )
    41  
    42  // NewDelimitedReader reads varint-delimited Protobuf messages from a reader.
    43  // Unlike the gogoproto NewDelimitedReader, this does not buffer the reader,
    44  // which may cause poor performance but is necessary when only reading single
    45  // messages (e.g. in the p2p package). It also returns the number of bytes
    46  // read, which is necessary for the p2p package.
    47  func NewDelimitedReader(r io.Reader, maxSize int) ReadCloser {
    48  	var closer io.Closer
    49  	if c, ok := r.(io.Closer); ok {
    50  		closer = c
    51  	}
    52  	return &varintReader{r, nil, maxSize, closer}
    53  }
    54  
    55  type varintReader struct {
    56  	r       io.Reader
    57  	buf     []byte
    58  	maxSize int
    59  	closer  io.Closer
    60  }
    61  
    62  func (r *varintReader) ReadMsg(msg proto.Message) (int, error) {
    63  	// ReadUvarint needs an io.ByteReader, and we also need to keep track of the
    64  	// number of bytes read, so we use our own byteReader. This can't be
    65  	// buffered, so the caller should pass a buffered io.Reader to avoid poor
    66  	// performance.
    67  	byteReader := newByteReader(r.r)
    68  	l, err := binary.ReadUvarint(byteReader)
    69  	n := byteReader.bytesRead
    70  	if err != nil {
    71  		return n, err
    72  	}
    73  
    74  	// Make sure length doesn't overflow the native int size (e.g. 32-bit),
    75  	// and that the returned sum of n+length doesn't overflow either.
    76  	length := int(l)
    77  	if l >= uint64(^uint(0)>>1) || length < 0 || n+length < 0 {
    78  		return n, fmt.Errorf("invalid out-of-range message length %v", l)
    79  	}
    80  	if length > r.maxSize {
    81  		return n, fmt.Errorf("message exceeds max size (%v > %v)", length, r.maxSize)
    82  	}
    83  
    84  	if len(r.buf) < length {
    85  		r.buf = make([]byte, length)
    86  	}
    87  	buf := r.buf[:length]
    88  	nr, err := io.ReadFull(r.r, buf)
    89  	n += nr
    90  	if err != nil {
    91  		return n, err
    92  	}
    93  	return n, proto.Unmarshal(buf, msg)
    94  }
    95  
    96  func (r *varintReader) Close() error {
    97  	if r.closer != nil {
    98  		return r.closer.Close()
    99  	}
   100  	return nil
   101  }
   102  
   103  func UnmarshalDelimited(data []byte, msg proto.Message) error {
   104  	_, err := NewDelimitedReader(bytes.NewReader(data), len(data)).ReadMsg(msg)
   105  	return err
   106  }