github.com/matm/etcd@v0.3.1-0.20140328024009-5b4a473f1453/third_party/code.google.com/p/gogoprotobuf/proto/skip_gogo.go (about)

     1  // Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
     2  // http://code.google.com/p/gogoprotobuf/gogoproto
     3  //
     4  // Redistribution and use in source and binary forms, with or without
     5  // modification, are permitted provided that the following conditions are
     6  // met:
     7  //
     8  //     * Redistributions of source code must retain the above copyright
     9  // notice, this list of conditions and the following disclaimer.
    10  //     * Redistributions in binary form must reproduce the above
    11  // copyright notice, this list of conditions and the following disclaimer
    12  // in the documentation and/or other materials provided with the
    13  // distribution.
    14  //
    15  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    16  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    17  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    18  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    19  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    20  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    21  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    22  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    23  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    24  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    25  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    26  
    27  package proto
    28  
    29  import (
    30  	"io"
    31  )
    32  
    33  func Skip(data []byte) (n int, err error) {
    34  	l := len(data)
    35  	index := 0
    36  	for index < l {
    37  		var wire uint64
    38  		for shift := uint(0); ; shift += 7 {
    39  			if index >= l {
    40  				return 0, io.ErrUnexpectedEOF
    41  			}
    42  			b := data[index]
    43  			index++
    44  			wire |= (uint64(b) & 0x7F) << shift
    45  			if b < 0x80 {
    46  				break
    47  			}
    48  		}
    49  		wireType := int(wire & 0x7)
    50  		switch wireType {
    51  		case 0:
    52  			for {
    53  				if index >= l {
    54  					return 0, io.ErrUnexpectedEOF
    55  				}
    56  				index++
    57  				if data[index-1] < 0x80 {
    58  					break
    59  				}
    60  			}
    61  			return index, nil
    62  		case 1:
    63  			index += 8
    64  			return index, nil
    65  		case 2:
    66  			var length int
    67  			for shift := uint(0); ; shift += 7 {
    68  				if index >= l {
    69  					return 0, io.ErrUnexpectedEOF
    70  				}
    71  				b := data[index]
    72  				index++
    73  				length |= (int(b) & 0x7F) << shift
    74  				if b < 0x80 {
    75  					break
    76  				}
    77  			}
    78  			index += length
    79  			return index, nil
    80  		case 3:
    81  			for {
    82  				var wire uint64
    83  				var start int = index
    84  				for shift := uint(0); ; shift += 7 {
    85  					if index >= l {
    86  						return 0, io.ErrUnexpectedEOF
    87  					}
    88  					b := data[index]
    89  					index++
    90  					wire |= (uint64(b) & 0x7F) << shift
    91  					if b < 0x80 {
    92  						break
    93  					}
    94  				}
    95  				wireType := int(wire & 0x7)
    96  				if wireType == 4 {
    97  					break
    98  				}
    99  				next, err := Skip(data[start:])
   100  				if err != nil {
   101  					return 0, err
   102  				}
   103  				index = start + next
   104  			}
   105  			return index, nil
   106  		case 4:
   107  			return index, nil
   108  		case 5:
   109  			index += 4
   110  			return index, nil
   111  		default:
   112  			return 0, ErrWrongType
   113  		}
   114  	}
   115  	panic("unreachable")
   116  }