github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/Godeps/_workspace/src/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  	"fmt"
    31  	"io"
    32  )
    33  
    34  func Skip(data []byte) (n int, err error) {
    35  	l := len(data)
    36  	index := 0
    37  	for index < l {
    38  		var wire uint64
    39  		for shift := uint(0); ; shift += 7 {
    40  			if index >= l {
    41  				return 0, io.ErrUnexpectedEOF
    42  			}
    43  			b := data[index]
    44  			index++
    45  			wire |= (uint64(b) & 0x7F) << shift
    46  			if b < 0x80 {
    47  				break
    48  			}
    49  		}
    50  		wireType := int(wire & 0x7)
    51  		switch wireType {
    52  		case 0:
    53  			for {
    54  				if index >= l {
    55  					return 0, io.ErrUnexpectedEOF
    56  				}
    57  				index++
    58  				if data[index-1] < 0x80 {
    59  					break
    60  				}
    61  			}
    62  			return index, nil
    63  		case 1:
    64  			index += 8
    65  			return index, nil
    66  		case 2:
    67  			var length int
    68  			for shift := uint(0); ; shift += 7 {
    69  				if index >= l {
    70  					return 0, io.ErrUnexpectedEOF
    71  				}
    72  				b := data[index]
    73  				index++
    74  				length |= (int(b) & 0x7F) << shift
    75  				if b < 0x80 {
    76  					break
    77  				}
    78  			}
    79  			index += length
    80  			return index, nil
    81  		case 3:
    82  			for {
    83  				var wire uint64
    84  				var start int = index
    85  				for shift := uint(0); ; shift += 7 {
    86  					if index >= l {
    87  						return 0, io.ErrUnexpectedEOF
    88  					}
    89  					b := data[index]
    90  					index++
    91  					wire |= (uint64(b) & 0x7F) << shift
    92  					if b < 0x80 {
    93  						break
    94  					}
    95  				}
    96  				wireType := int(wire & 0x7)
    97  				if wireType == 4 {
    98  					break
    99  				}
   100  				next, err := Skip(data[start:])
   101  				if err != nil {
   102  					return 0, err
   103  				}
   104  				index = start + next
   105  			}
   106  			return index, nil
   107  		case 4:
   108  			return index, nil
   109  		case 5:
   110  			index += 4
   111  			return index, nil
   112  		default:
   113  			return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
   114  		}
   115  	}
   116  	panic("unreachable")
   117  }