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