http urls monitor.

table_unmarshal.go 49KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2016 The Go Authors. All rights reserved.
  4. // https://github.com/golang/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. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto
  32. import (
  33. "errors"
  34. "fmt"
  35. "io"
  36. "math"
  37. "reflect"
  38. "strconv"
  39. "strings"
  40. "sync"
  41. "sync/atomic"
  42. "unicode/utf8"
  43. )
  44. // Unmarshal is the entry point from the generated .pb.go files.
  45. // This function is not intended to be used by non-generated code.
  46. // This function is not subject to any compatibility guarantee.
  47. // msg contains a pointer to a protocol buffer struct.
  48. // b is the data to be unmarshaled into the protocol buffer.
  49. // a is a pointer to a place to store cached unmarshal information.
  50. func (a *InternalMessageInfo) Unmarshal(msg Message, b []byte) error {
  51. // Load the unmarshal information for this message type.
  52. // The atomic load ensures memory consistency.
  53. u := atomicLoadUnmarshalInfo(&a.unmarshal)
  54. if u == nil {
  55. // Slow path: find unmarshal info for msg, update a with it.
  56. u = getUnmarshalInfo(reflect.TypeOf(msg).Elem())
  57. atomicStoreUnmarshalInfo(&a.unmarshal, u)
  58. }
  59. // Then do the unmarshaling.
  60. err := u.unmarshal(toPointer(&msg), b)
  61. return err
  62. }
  63. type unmarshalInfo struct {
  64. typ reflect.Type // type of the protobuf struct
  65. // 0 = only typ field is initialized
  66. // 1 = completely initialized
  67. initialized int32
  68. lock sync.Mutex // prevents double initialization
  69. dense []unmarshalFieldInfo // fields indexed by tag #
  70. sparse map[uint64]unmarshalFieldInfo // fields indexed by tag #
  71. reqFields []string // names of required fields
  72. reqMask uint64 // 1<<len(reqFields)-1
  73. unrecognized field // offset of []byte to put unrecognized data (or invalidField if we should throw it away)
  74. extensions field // offset of extensions field (of type proto.XXX_InternalExtensions), or invalidField if it does not exist
  75. oldExtensions field // offset of old-form extensions field (of type map[int]Extension)
  76. extensionRanges []ExtensionRange // if non-nil, implies extensions field is valid
  77. isMessageSet bool // if true, implies extensions field is valid
  78. }
  79. // An unmarshaler takes a stream of bytes and a pointer to a field of a message.
  80. // It decodes the field, stores it at f, and returns the unused bytes.
  81. // w is the wire encoding.
  82. // b is the data after the tag and wire encoding have been read.
  83. type unmarshaler func(b []byte, f pointer, w int) ([]byte, error)
  84. type unmarshalFieldInfo struct {
  85. // location of the field in the proto message structure.
  86. field field
  87. // function to unmarshal the data for the field.
  88. unmarshal unmarshaler
  89. // if a required field, contains a single set bit at this field's index in the required field list.
  90. reqMask uint64
  91. name string // name of the field, for error reporting
  92. }
  93. var (
  94. unmarshalInfoMap = map[reflect.Type]*unmarshalInfo{}
  95. unmarshalInfoLock sync.Mutex
  96. )
  97. // getUnmarshalInfo returns the data structure which can be
  98. // subsequently used to unmarshal a message of the given type.
  99. // t is the type of the message (note: not pointer to message).
  100. func getUnmarshalInfo(t reflect.Type) *unmarshalInfo {
  101. // It would be correct to return a new unmarshalInfo
  102. // unconditionally. We would end up allocating one
  103. // per occurrence of that type as a message or submessage.
  104. // We use a cache here just to reduce memory usage.
  105. unmarshalInfoLock.Lock()
  106. defer unmarshalInfoLock.Unlock()
  107. u := unmarshalInfoMap[t]
  108. if u == nil {
  109. u = &unmarshalInfo{typ: t}
  110. // Note: we just set the type here. The rest of the fields
  111. // will be initialized on first use.
  112. unmarshalInfoMap[t] = u
  113. }
  114. return u
  115. }
  116. // unmarshal does the main work of unmarshaling a message.
  117. // u provides type information used to unmarshal the message.
  118. // m is a pointer to a protocol buffer message.
  119. // b is a byte stream to unmarshal into m.
  120. // This is top routine used when recursively unmarshaling submessages.
  121. func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error {
  122. if atomic.LoadInt32(&u.initialized) == 0 {
  123. u.computeUnmarshalInfo()
  124. }
  125. if u.isMessageSet {
  126. return UnmarshalMessageSet(b, m.offset(u.extensions).toExtensions())
  127. }
  128. var reqMask uint64 // bitmask of required fields we've seen.
  129. var errLater error
  130. for len(b) > 0 {
  131. // Read tag and wire type.
  132. // Special case 1 and 2 byte varints.
  133. var x uint64
  134. if b[0] < 128 {
  135. x = uint64(b[0])
  136. b = b[1:]
  137. } else if len(b) >= 2 && b[1] < 128 {
  138. x = uint64(b[0]&0x7f) + uint64(b[1])<<7
  139. b = b[2:]
  140. } else {
  141. var n int
  142. x, n = decodeVarint(b)
  143. if n == 0 {
  144. return io.ErrUnexpectedEOF
  145. }
  146. b = b[n:]
  147. }
  148. tag := x >> 3
  149. wire := int(x) & 7
  150. // Dispatch on the tag to one of the unmarshal* functions below.
  151. var f unmarshalFieldInfo
  152. if tag < uint64(len(u.dense)) {
  153. f = u.dense[tag]
  154. } else {
  155. f = u.sparse[tag]
  156. }
  157. if fn := f.unmarshal; fn != nil {
  158. var err error
  159. b, err = fn(b, m.offset(f.field), wire)
  160. if err == nil {
  161. reqMask |= f.reqMask
  162. continue
  163. }
  164. if r, ok := err.(*RequiredNotSetError); ok {
  165. // Remember this error, but keep parsing. We need to produce
  166. // a full parse even if a required field is missing.
  167. if errLater == nil {
  168. errLater = r
  169. }
  170. reqMask |= f.reqMask
  171. continue
  172. }
  173. if err != errInternalBadWireType {
  174. if err == errInvalidUTF8 {
  175. if errLater == nil {
  176. fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name
  177. errLater = &invalidUTF8Error{fullName}
  178. }
  179. continue
  180. }
  181. return err
  182. }
  183. // Fragments with bad wire type are treated as unknown fields.
  184. }
  185. // Unknown tag.
  186. if !u.unrecognized.IsValid() {
  187. // Don't keep unrecognized data; just skip it.
  188. var err error
  189. b, err = skipField(b, wire)
  190. if err != nil {
  191. return err
  192. }
  193. continue
  194. }
  195. // Keep unrecognized data around.
  196. // maybe in extensions, maybe in the unrecognized field.
  197. z := m.offset(u.unrecognized).toBytes()
  198. var emap map[int32]Extension
  199. var e Extension
  200. for _, r := range u.extensionRanges {
  201. if uint64(r.Start) <= tag && tag <= uint64(r.End) {
  202. if u.extensions.IsValid() {
  203. mp := m.offset(u.extensions).toExtensions()
  204. emap = mp.extensionsWrite()
  205. e = emap[int32(tag)]
  206. z = &e.enc
  207. break
  208. }
  209. if u.oldExtensions.IsValid() {
  210. p := m.offset(u.oldExtensions).toOldExtensions()
  211. emap = *p
  212. if emap == nil {
  213. emap = map[int32]Extension{}
  214. *p = emap
  215. }
  216. e = emap[int32(tag)]
  217. z = &e.enc
  218. break
  219. }
  220. panic("no extensions field available")
  221. }
  222. }
  223. // Use wire type to skip data.
  224. var err error
  225. b0 := b
  226. b, err = skipField(b, wire)
  227. if err != nil {
  228. return err
  229. }
  230. *z = encodeVarint(*z, tag<<3|uint64(wire))
  231. *z = append(*z, b0[:len(b0)-len(b)]...)
  232. if emap != nil {
  233. emap[int32(tag)] = e
  234. }
  235. }
  236. if reqMask != u.reqMask && errLater == nil {
  237. // A required field of this message is missing.
  238. for _, n := range u.reqFields {
  239. if reqMask&1 == 0 {
  240. errLater = &RequiredNotSetError{n}
  241. }
  242. reqMask >>= 1
  243. }
  244. }
  245. return errLater
  246. }
  247. // computeUnmarshalInfo fills in u with information for use
  248. // in unmarshaling protocol buffers of type u.typ.
  249. func (u *unmarshalInfo) computeUnmarshalInfo() {
  250. u.lock.Lock()
  251. defer u.lock.Unlock()
  252. if u.initialized != 0 {
  253. return
  254. }
  255. t := u.typ
  256. n := t.NumField()
  257. // Set up the "not found" value for the unrecognized byte buffer.
  258. // This is the default for proto3.
  259. u.unrecognized = invalidField
  260. u.extensions = invalidField
  261. u.oldExtensions = invalidField
  262. // List of the generated type and offset for each oneof field.
  263. type oneofField struct {
  264. ityp reflect.Type // interface type of oneof field
  265. field field // offset in containing message
  266. }
  267. var oneofFields []oneofField
  268. for i := 0; i < n; i++ {
  269. f := t.Field(i)
  270. if f.Name == "XXX_unrecognized" {
  271. // The byte slice used to hold unrecognized input is special.
  272. if f.Type != reflect.TypeOf(([]byte)(nil)) {
  273. panic("bad type for XXX_unrecognized field: " + f.Type.Name())
  274. }
  275. u.unrecognized = toField(&f)
  276. continue
  277. }
  278. if f.Name == "XXX_InternalExtensions" {
  279. // Ditto here.
  280. if f.Type != reflect.TypeOf(XXX_InternalExtensions{}) {
  281. panic("bad type for XXX_InternalExtensions field: " + f.Type.Name())
  282. }
  283. u.extensions = toField(&f)
  284. if f.Tag.Get("protobuf_messageset") == "1" {
  285. u.isMessageSet = true
  286. }
  287. continue
  288. }
  289. if f.Name == "XXX_extensions" {
  290. // An older form of the extensions field.
  291. if f.Type != reflect.TypeOf((map[int32]Extension)(nil)) {
  292. panic("bad type for XXX_extensions field: " + f.Type.Name())
  293. }
  294. u.oldExtensions = toField(&f)
  295. continue
  296. }
  297. if f.Name == "XXX_NoUnkeyedLiteral" || f.Name == "XXX_sizecache" {
  298. continue
  299. }
  300. oneof := f.Tag.Get("protobuf_oneof")
  301. if oneof != "" {
  302. oneofFields = append(oneofFields, oneofField{f.Type, toField(&f)})
  303. // The rest of oneof processing happens below.
  304. continue
  305. }
  306. tags := f.Tag.Get("protobuf")
  307. tagArray := strings.Split(tags, ",")
  308. if len(tagArray) < 2 {
  309. panic("protobuf tag not enough fields in " + t.Name() + "." + f.Name + ": " + tags)
  310. }
  311. tag, err := strconv.Atoi(tagArray[1])
  312. if err != nil {
  313. panic("protobuf tag field not an integer: " + tagArray[1])
  314. }
  315. name := ""
  316. for _, tag := range tagArray[3:] {
  317. if strings.HasPrefix(tag, "name=") {
  318. name = tag[5:]
  319. }
  320. }
  321. // Extract unmarshaling function from the field (its type and tags).
  322. unmarshal := fieldUnmarshaler(&f)
  323. // Required field?
  324. var reqMask uint64
  325. if tagArray[2] == "req" {
  326. bit := len(u.reqFields)
  327. u.reqFields = append(u.reqFields, name)
  328. reqMask = uint64(1) << uint(bit)
  329. // TODO: if we have more than 64 required fields, we end up
  330. // not verifying that all required fields are present.
  331. // Fix this, perhaps using a count of required fields?
  332. }
  333. // Store the info in the correct slot in the message.
  334. u.setTag(tag, toField(&f), unmarshal, reqMask, name)
  335. }
  336. // Find any types associated with oneof fields.
  337. // TODO: XXX_OneofFuncs returns more info than we need. Get rid of some of it?
  338. fn := reflect.Zero(reflect.PtrTo(t)).MethodByName("XXX_OneofFuncs")
  339. if fn.IsValid() {
  340. res := fn.Call(nil)[3] // last return value from XXX_OneofFuncs: []interface{}
  341. for i := res.Len() - 1; i >= 0; i-- {
  342. v := res.Index(i) // interface{}
  343. tptr := reflect.ValueOf(v.Interface()).Type() // *Msg_X
  344. typ := tptr.Elem() // Msg_X
  345. f := typ.Field(0) // oneof implementers have one field
  346. baseUnmarshal := fieldUnmarshaler(&f)
  347. tags := strings.Split(f.Tag.Get("protobuf"), ",")
  348. fieldNum, err := strconv.Atoi(tags[1])
  349. if err != nil {
  350. panic("protobuf tag field not an integer: " + tags[1])
  351. }
  352. var name string
  353. for _, tag := range tags {
  354. if strings.HasPrefix(tag, "name=") {
  355. name = strings.TrimPrefix(tag, "name=")
  356. break
  357. }
  358. }
  359. // Find the oneof field that this struct implements.
  360. // Might take O(n^2) to process all of the oneofs, but who cares.
  361. for _, of := range oneofFields {
  362. if tptr.Implements(of.ityp) {
  363. // We have found the corresponding interface for this struct.
  364. // That lets us know where this struct should be stored
  365. // when we encounter it during unmarshaling.
  366. unmarshal := makeUnmarshalOneof(typ, of.ityp, baseUnmarshal)
  367. u.setTag(fieldNum, of.field, unmarshal, 0, name)
  368. }
  369. }
  370. }
  371. }
  372. // Get extension ranges, if any.
  373. fn = reflect.Zero(reflect.PtrTo(t)).MethodByName("ExtensionRangeArray")
  374. if fn.IsValid() {
  375. if !u.extensions.IsValid() && !u.oldExtensions.IsValid() {
  376. panic("a message with extensions, but no extensions field in " + t.Name())
  377. }
  378. u.extensionRanges = fn.Call(nil)[0].Interface().([]ExtensionRange)
  379. }
  380. // Explicitly disallow tag 0. This will ensure we flag an error
  381. // when decoding a buffer of all zeros. Without this code, we
  382. // would decode and skip an all-zero buffer of even length.
  383. // [0 0] is [tag=0/wiretype=varint varint-encoded-0].
  384. u.setTag(0, zeroField, func(b []byte, f pointer, w int) ([]byte, error) {
  385. return nil, fmt.Errorf("proto: %s: illegal tag 0 (wire type %d)", t, w)
  386. }, 0, "")
  387. // Set mask for required field check.
  388. u.reqMask = uint64(1)<<uint(len(u.reqFields)) - 1
  389. atomic.StoreInt32(&u.initialized, 1)
  390. }
  391. // setTag stores the unmarshal information for the given tag.
  392. // tag = tag # for field
  393. // field/unmarshal = unmarshal info for that field.
  394. // reqMask = if required, bitmask for field position in required field list. 0 otherwise.
  395. // name = short name of the field.
  396. func (u *unmarshalInfo) setTag(tag int, field field, unmarshal unmarshaler, reqMask uint64, name string) {
  397. i := unmarshalFieldInfo{field: field, unmarshal: unmarshal, reqMask: reqMask, name: name}
  398. n := u.typ.NumField()
  399. if tag >= 0 && (tag < 16 || tag < 2*n) { // TODO: what are the right numbers here?
  400. for len(u.dense) <= tag {
  401. u.dense = append(u.dense, unmarshalFieldInfo{})
  402. }
  403. u.dense[tag] = i
  404. return
  405. }
  406. if u.sparse == nil {
  407. u.sparse = map[uint64]unmarshalFieldInfo{}
  408. }
  409. u.sparse[uint64(tag)] = i
  410. }
  411. // fieldUnmarshaler returns an unmarshaler for the given field.
  412. func fieldUnmarshaler(f *reflect.StructField) unmarshaler {
  413. if f.Type.Kind() == reflect.Map {
  414. return makeUnmarshalMap(f)
  415. }
  416. return typeUnmarshaler(f.Type, f.Tag.Get("protobuf"))
  417. }
  418. // typeUnmarshaler returns an unmarshaler for the given field type / field tag pair.
  419. func typeUnmarshaler(t reflect.Type, tags string) unmarshaler {
  420. tagArray := strings.Split(tags, ",")
  421. encoding := tagArray[0]
  422. name := "unknown"
  423. proto3 := false
  424. validateUTF8 := true
  425. for _, tag := range tagArray[3:] {
  426. if strings.HasPrefix(tag, "name=") {
  427. name = tag[5:]
  428. }
  429. if tag == "proto3" {
  430. proto3 = true
  431. }
  432. }
  433. validateUTF8 = validateUTF8 && proto3
  434. // Figure out packaging (pointer, slice, or both)
  435. slice := false
  436. pointer := false
  437. if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
  438. slice = true
  439. t = t.Elem()
  440. }
  441. if t.Kind() == reflect.Ptr {
  442. pointer = true
  443. t = t.Elem()
  444. }
  445. // We'll never have both pointer and slice for basic types.
  446. if pointer && slice && t.Kind() != reflect.Struct {
  447. panic("both pointer and slice for basic type in " + t.Name())
  448. }
  449. switch t.Kind() {
  450. case reflect.Bool:
  451. if pointer {
  452. return unmarshalBoolPtr
  453. }
  454. if slice {
  455. return unmarshalBoolSlice
  456. }
  457. return unmarshalBoolValue
  458. case reflect.Int32:
  459. switch encoding {
  460. case "fixed32":
  461. if pointer {
  462. return unmarshalFixedS32Ptr
  463. }
  464. if slice {
  465. return unmarshalFixedS32Slice
  466. }
  467. return unmarshalFixedS32Value
  468. case "varint":
  469. // this could be int32 or enum
  470. if pointer {
  471. return unmarshalInt32Ptr
  472. }
  473. if slice {
  474. return unmarshalInt32Slice
  475. }
  476. return unmarshalInt32Value
  477. case "zigzag32":
  478. if pointer {
  479. return unmarshalSint32Ptr
  480. }
  481. if slice {
  482. return unmarshalSint32Slice
  483. }
  484. return unmarshalSint32Value
  485. }
  486. case reflect.Int64:
  487. switch encoding {
  488. case "fixed64":
  489. if pointer {
  490. return unmarshalFixedS64Ptr
  491. }
  492. if slice {
  493. return unmarshalFixedS64Slice
  494. }
  495. return unmarshalFixedS64Value
  496. case "varint":
  497. if pointer {
  498. return unmarshalInt64Ptr
  499. }
  500. if slice {
  501. return unmarshalInt64Slice
  502. }
  503. return unmarshalInt64Value
  504. case "zigzag64":
  505. if pointer {
  506. return unmarshalSint64Ptr
  507. }
  508. if slice {
  509. return unmarshalSint64Slice
  510. }
  511. return unmarshalSint64Value
  512. }
  513. case reflect.Uint32:
  514. switch encoding {
  515. case "fixed32":
  516. if pointer {
  517. return unmarshalFixed32Ptr
  518. }
  519. if slice {
  520. return unmarshalFixed32Slice
  521. }
  522. return unmarshalFixed32Value
  523. case "varint":
  524. if pointer {
  525. return unmarshalUint32Ptr
  526. }
  527. if slice {
  528. return unmarshalUint32Slice
  529. }
  530. return unmarshalUint32Value
  531. }
  532. case reflect.Uint64:
  533. switch encoding {
  534. case "fixed64":
  535. if pointer {
  536. return unmarshalFixed64Ptr
  537. }
  538. if slice {
  539. return unmarshalFixed64Slice
  540. }
  541. return unmarshalFixed64Value
  542. case "varint":
  543. if pointer {
  544. return unmarshalUint64Ptr
  545. }
  546. if slice {
  547. return unmarshalUint64Slice
  548. }
  549. return unmarshalUint64Value
  550. }
  551. case reflect.Float32:
  552. if pointer {
  553. return unmarshalFloat32Ptr
  554. }
  555. if slice {
  556. return unmarshalFloat32Slice
  557. }
  558. return unmarshalFloat32Value
  559. case reflect.Float64:
  560. if pointer {
  561. return unmarshalFloat64Ptr
  562. }
  563. if slice {
  564. return unmarshalFloat64Slice
  565. }
  566. return unmarshalFloat64Value
  567. case reflect.Map:
  568. panic("map type in typeUnmarshaler in " + t.Name())
  569. case reflect.Slice:
  570. if pointer {
  571. panic("bad pointer in slice case in " + t.Name())
  572. }
  573. if slice {
  574. return unmarshalBytesSlice
  575. }
  576. return unmarshalBytesValue
  577. case reflect.String:
  578. if validateUTF8 {
  579. if pointer {
  580. return unmarshalUTF8StringPtr
  581. }
  582. if slice {
  583. return unmarshalUTF8StringSlice
  584. }
  585. return unmarshalUTF8StringValue
  586. }
  587. if pointer {
  588. return unmarshalStringPtr
  589. }
  590. if slice {
  591. return unmarshalStringSlice
  592. }
  593. return unmarshalStringValue
  594. case reflect.Struct:
  595. // message or group field
  596. if !pointer {
  597. panic(fmt.Sprintf("message/group field %s:%s without pointer", t, encoding))
  598. }
  599. switch encoding {
  600. case "bytes":
  601. if slice {
  602. return makeUnmarshalMessageSlicePtr(getUnmarshalInfo(t), name)
  603. }
  604. return makeUnmarshalMessagePtr(getUnmarshalInfo(t), name)
  605. case "group":
  606. if slice {
  607. return makeUnmarshalGroupSlicePtr(getUnmarshalInfo(t), name)
  608. }
  609. return makeUnmarshalGroupPtr(getUnmarshalInfo(t), name)
  610. }
  611. }
  612. panic(fmt.Sprintf("unmarshaler not found type:%s encoding:%s", t, encoding))
  613. }
  614. // Below are all the unmarshalers for individual fields of various types.
  615. func unmarshalInt64Value(b []byte, f pointer, w int) ([]byte, error) {
  616. if w != WireVarint {
  617. return b, errInternalBadWireType
  618. }
  619. x, n := decodeVarint(b)
  620. if n == 0 {
  621. return nil, io.ErrUnexpectedEOF
  622. }
  623. b = b[n:]
  624. v := int64(x)
  625. *f.toInt64() = v
  626. return b, nil
  627. }
  628. func unmarshalInt64Ptr(b []byte, f pointer, w int) ([]byte, error) {
  629. if w != WireVarint {
  630. return b, errInternalBadWireType
  631. }
  632. x, n := decodeVarint(b)
  633. if n == 0 {
  634. return nil, io.ErrUnexpectedEOF
  635. }
  636. b = b[n:]
  637. v := int64(x)
  638. *f.toInt64Ptr() = &v
  639. return b, nil
  640. }
  641. func unmarshalInt64Slice(b []byte, f pointer, w int) ([]byte, error) {
  642. if w == WireBytes { // packed
  643. x, n := decodeVarint(b)
  644. if n == 0 {
  645. return nil, io.ErrUnexpectedEOF
  646. }
  647. b = b[n:]
  648. if x > uint64(len(b)) {
  649. return nil, io.ErrUnexpectedEOF
  650. }
  651. res := b[x:]
  652. b = b[:x]
  653. for len(b) > 0 {
  654. x, n = decodeVarint(b)
  655. if n == 0 {
  656. return nil, io.ErrUnexpectedEOF
  657. }
  658. b = b[n:]
  659. v := int64(x)
  660. s := f.toInt64Slice()
  661. *s = append(*s, v)
  662. }
  663. return res, nil
  664. }
  665. if w != WireVarint {
  666. return b, errInternalBadWireType
  667. }
  668. x, n := decodeVarint(b)
  669. if n == 0 {
  670. return nil, io.ErrUnexpectedEOF
  671. }
  672. b = b[n:]
  673. v := int64(x)
  674. s := f.toInt64Slice()
  675. *s = append(*s, v)
  676. return b, nil
  677. }
  678. func unmarshalSint64Value(b []byte, f pointer, w int) ([]byte, error) {
  679. if w != WireVarint {
  680. return b, errInternalBadWireType
  681. }
  682. x, n := decodeVarint(b)
  683. if n == 0 {
  684. return nil, io.ErrUnexpectedEOF
  685. }
  686. b = b[n:]
  687. v := int64(x>>1) ^ int64(x)<<63>>63
  688. *f.toInt64() = v
  689. return b, nil
  690. }
  691. func unmarshalSint64Ptr(b []byte, f pointer, w int) ([]byte, error) {
  692. if w != WireVarint {
  693. return b, errInternalBadWireType
  694. }
  695. x, n := decodeVarint(b)
  696. if n == 0 {
  697. return nil, io.ErrUnexpectedEOF
  698. }
  699. b = b[n:]
  700. v := int64(x>>1) ^ int64(x)<<63>>63
  701. *f.toInt64Ptr() = &v
  702. return b, nil
  703. }
  704. func unmarshalSint64Slice(b []byte, f pointer, w int) ([]byte, error) {
  705. if w == WireBytes { // packed
  706. x, n := decodeVarint(b)
  707. if n == 0 {
  708. return nil, io.ErrUnexpectedEOF
  709. }
  710. b = b[n:]
  711. if x > uint64(len(b)) {
  712. return nil, io.ErrUnexpectedEOF
  713. }
  714. res := b[x:]
  715. b = b[:x]
  716. for len(b) > 0 {
  717. x, n = decodeVarint(b)
  718. if n == 0 {
  719. return nil, io.ErrUnexpectedEOF
  720. }
  721. b = b[n:]
  722. v := int64(x>>1) ^ int64(x)<<63>>63
  723. s := f.toInt64Slice()
  724. *s = append(*s, v)
  725. }
  726. return res, nil
  727. }
  728. if w != WireVarint {
  729. return b, errInternalBadWireType
  730. }
  731. x, n := decodeVarint(b)
  732. if n == 0 {
  733. return nil, io.ErrUnexpectedEOF
  734. }
  735. b = b[n:]
  736. v := int64(x>>1) ^ int64(x)<<63>>63
  737. s := f.toInt64Slice()
  738. *s = append(*s, v)
  739. return b, nil
  740. }
  741. func unmarshalUint64Value(b []byte, f pointer, w int) ([]byte, error) {
  742. if w != WireVarint {
  743. return b, errInternalBadWireType
  744. }
  745. x, n := decodeVarint(b)
  746. if n == 0 {
  747. return nil, io.ErrUnexpectedEOF
  748. }
  749. b = b[n:]
  750. v := uint64(x)
  751. *f.toUint64() = v
  752. return b, nil
  753. }
  754. func unmarshalUint64Ptr(b []byte, f pointer, w int) ([]byte, error) {
  755. if w != WireVarint {
  756. return b, errInternalBadWireType
  757. }
  758. x, n := decodeVarint(b)
  759. if n == 0 {
  760. return nil, io.ErrUnexpectedEOF
  761. }
  762. b = b[n:]
  763. v := uint64(x)
  764. *f.toUint64Ptr() = &v
  765. return b, nil
  766. }
  767. func unmarshalUint64Slice(b []byte, f pointer, w int) ([]byte, error) {
  768. if w == WireBytes { // packed
  769. x, n := decodeVarint(b)
  770. if n == 0 {
  771. return nil, io.ErrUnexpectedEOF
  772. }
  773. b = b[n:]
  774. if x > uint64(len(b)) {
  775. return nil, io.ErrUnexpectedEOF
  776. }
  777. res := b[x:]
  778. b = b[:x]
  779. for len(b) > 0 {
  780. x, n = decodeVarint(b)
  781. if n == 0 {
  782. return nil, io.ErrUnexpectedEOF
  783. }
  784. b = b[n:]
  785. v := uint64(x)
  786. s := f.toUint64Slice()
  787. *s = append(*s, v)
  788. }
  789. return res, nil
  790. }
  791. if w != WireVarint {
  792. return b, errInternalBadWireType
  793. }
  794. x, n := decodeVarint(b)
  795. if n == 0 {
  796. return nil, io.ErrUnexpectedEOF
  797. }
  798. b = b[n:]
  799. v := uint64(x)
  800. s := f.toUint64Slice()
  801. *s = append(*s, v)
  802. return b, nil
  803. }
  804. func unmarshalInt32Value(b []byte, f pointer, w int) ([]byte, error) {
  805. if w != WireVarint {
  806. return b, errInternalBadWireType
  807. }
  808. x, n := decodeVarint(b)
  809. if n == 0 {
  810. return nil, io.ErrUnexpectedEOF
  811. }
  812. b = b[n:]
  813. v := int32(x)
  814. *f.toInt32() = v
  815. return b, nil
  816. }
  817. func unmarshalInt32Ptr(b []byte, f pointer, w int) ([]byte, error) {
  818. if w != WireVarint {
  819. return b, errInternalBadWireType
  820. }
  821. x, n := decodeVarint(b)
  822. if n == 0 {
  823. return nil, io.ErrUnexpectedEOF
  824. }
  825. b = b[n:]
  826. v := int32(x)
  827. f.setInt32Ptr(v)
  828. return b, nil
  829. }
  830. func unmarshalInt32Slice(b []byte, f pointer, w int) ([]byte, error) {
  831. if w == WireBytes { // packed
  832. x, n := decodeVarint(b)
  833. if n == 0 {
  834. return nil, io.ErrUnexpectedEOF
  835. }
  836. b = b[n:]
  837. if x > uint64(len(b)) {
  838. return nil, io.ErrUnexpectedEOF
  839. }
  840. res := b[x:]
  841. b = b[:x]
  842. for len(b) > 0 {
  843. x, n = decodeVarint(b)
  844. if n == 0 {
  845. return nil, io.ErrUnexpectedEOF
  846. }
  847. b = b[n:]
  848. v := int32(x)
  849. f.appendInt32Slice(v)
  850. }
  851. return res, nil
  852. }
  853. if w != WireVarint {
  854. return b, errInternalBadWireType
  855. }
  856. x, n := decodeVarint(b)
  857. if n == 0 {
  858. return nil, io.ErrUnexpectedEOF
  859. }
  860. b = b[n:]
  861. v := int32(x)
  862. f.appendInt32Slice(v)
  863. return b, nil
  864. }
  865. func unmarshalSint32Value(b []byte, f pointer, w int) ([]byte, error) {
  866. if w != WireVarint {
  867. return b, errInternalBadWireType
  868. }
  869. x, n := decodeVarint(b)
  870. if n == 0 {
  871. return nil, io.ErrUnexpectedEOF
  872. }
  873. b = b[n:]
  874. v := int32(x>>1) ^ int32(x)<<31>>31
  875. *f.toInt32() = v
  876. return b, nil
  877. }
  878. func unmarshalSint32Ptr(b []byte, f pointer, w int) ([]byte, error) {
  879. if w != WireVarint {
  880. return b, errInternalBadWireType
  881. }
  882. x, n := decodeVarint(b)
  883. if n == 0 {
  884. return nil, io.ErrUnexpectedEOF
  885. }
  886. b = b[n:]
  887. v := int32(x>>1) ^ int32(x)<<31>>31
  888. f.setInt32Ptr(v)
  889. return b, nil
  890. }
  891. func unmarshalSint32Slice(b []byte, f pointer, w int) ([]byte, error) {
  892. if w == WireBytes { // packed
  893. x, n := decodeVarint(b)
  894. if n == 0 {
  895. return nil, io.ErrUnexpectedEOF
  896. }
  897. b = b[n:]
  898. if x > uint64(len(b)) {
  899. return nil, io.ErrUnexpectedEOF
  900. }
  901. res := b[x:]
  902. b = b[:x]
  903. for len(b) > 0 {
  904. x, n = decodeVarint(b)
  905. if n == 0 {
  906. return nil, io.ErrUnexpectedEOF
  907. }
  908. b = b[n:]
  909. v := int32(x>>1) ^ int32(x)<<31>>31
  910. f.appendInt32Slice(v)
  911. }
  912. return res, nil
  913. }
  914. if w != WireVarint {
  915. return b, errInternalBadWireType
  916. }
  917. x, n := decodeVarint(b)
  918. if n == 0 {
  919. return nil, io.ErrUnexpectedEOF
  920. }
  921. b = b[n:]
  922. v := int32(x>>1) ^ int32(x)<<31>>31
  923. f.appendInt32Slice(v)
  924. return b, nil
  925. }
  926. func unmarshalUint32Value(b []byte, f pointer, w int) ([]byte, error) {
  927. if w != WireVarint {
  928. return b, errInternalBadWireType
  929. }
  930. x, n := decodeVarint(b)
  931. if n == 0 {
  932. return nil, io.ErrUnexpectedEOF
  933. }
  934. b = b[n:]
  935. v := uint32(x)
  936. *f.toUint32() = v
  937. return b, nil
  938. }
  939. func unmarshalUint32Ptr(b []byte, f pointer, w int) ([]byte, error) {
  940. if w != WireVarint {
  941. return b, errInternalBadWireType
  942. }
  943. x, n := decodeVarint(b)
  944. if n == 0 {
  945. return nil, io.ErrUnexpectedEOF
  946. }
  947. b = b[n:]
  948. v := uint32(x)
  949. *f.toUint32Ptr() = &v
  950. return b, nil
  951. }
  952. func unmarshalUint32Slice(b []byte, f pointer, w int) ([]byte, error) {
  953. if w == WireBytes { // packed
  954. x, n := decodeVarint(b)
  955. if n == 0 {
  956. return nil, io.ErrUnexpectedEOF
  957. }
  958. b = b[n:]
  959. if x > uint64(len(b)) {
  960. return nil, io.ErrUnexpectedEOF
  961. }
  962. res := b[x:]
  963. b = b[:x]
  964. for len(b) > 0 {
  965. x, n = decodeVarint(b)
  966. if n == 0 {
  967. return nil, io.ErrUnexpectedEOF
  968. }
  969. b = b[n:]
  970. v := uint32(x)
  971. s := f.toUint32Slice()
  972. *s = append(*s, v)
  973. }
  974. return res, nil
  975. }
  976. if w != WireVarint {
  977. return b, errInternalBadWireType
  978. }
  979. x, n := decodeVarint(b)
  980. if n == 0 {
  981. return nil, io.ErrUnexpectedEOF
  982. }
  983. b = b[n:]
  984. v := uint32(x)
  985. s := f.toUint32Slice()
  986. *s = append(*s, v)
  987. return b, nil
  988. }
  989. func unmarshalFixed64Value(b []byte, f pointer, w int) ([]byte, error) {
  990. if w != WireFixed64 {
  991. return b, errInternalBadWireType
  992. }
  993. if len(b) < 8 {
  994. return nil, io.ErrUnexpectedEOF
  995. }
  996. v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  997. *f.toUint64() = v
  998. return b[8:], nil
  999. }
  1000. func unmarshalFixed64Ptr(b []byte, f pointer, w int) ([]byte, error) {
  1001. if w != WireFixed64 {
  1002. return b, errInternalBadWireType
  1003. }
  1004. if len(b) < 8 {
  1005. return nil, io.ErrUnexpectedEOF
  1006. }
  1007. v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  1008. *f.toUint64Ptr() = &v
  1009. return b[8:], nil
  1010. }
  1011. func unmarshalFixed64Slice(b []byte, f pointer, w int) ([]byte, error) {
  1012. if w == WireBytes { // packed
  1013. x, n := decodeVarint(b)
  1014. if n == 0 {
  1015. return nil, io.ErrUnexpectedEOF
  1016. }
  1017. b = b[n:]
  1018. if x > uint64(len(b)) {
  1019. return nil, io.ErrUnexpectedEOF
  1020. }
  1021. res := b[x:]
  1022. b = b[:x]
  1023. for len(b) > 0 {
  1024. if len(b) < 8 {
  1025. return nil, io.ErrUnexpectedEOF
  1026. }
  1027. v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  1028. s := f.toUint64Slice()
  1029. *s = append(*s, v)
  1030. b = b[8:]
  1031. }
  1032. return res, nil
  1033. }
  1034. if w != WireFixed64 {
  1035. return b, errInternalBadWireType
  1036. }
  1037. if len(b) < 8 {
  1038. return nil, io.ErrUnexpectedEOF
  1039. }
  1040. v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  1041. s := f.toUint64Slice()
  1042. *s = append(*s, v)
  1043. return b[8:], nil
  1044. }
  1045. func unmarshalFixedS64Value(b []byte, f pointer, w int) ([]byte, error) {
  1046. if w != WireFixed64 {
  1047. return b, errInternalBadWireType
  1048. }
  1049. if len(b) < 8 {
  1050. return nil, io.ErrUnexpectedEOF
  1051. }
  1052. v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
  1053. *f.toInt64() = v
  1054. return b[8:], nil
  1055. }
  1056. func unmarshalFixedS64Ptr(b []byte, f pointer, w int) ([]byte, error) {
  1057. if w != WireFixed64 {
  1058. return b, errInternalBadWireType
  1059. }
  1060. if len(b) < 8 {
  1061. return nil, io.ErrUnexpectedEOF
  1062. }
  1063. v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
  1064. *f.toInt64Ptr() = &v
  1065. return b[8:], nil
  1066. }
  1067. func unmarshalFixedS64Slice(b []byte, f pointer, w int) ([]byte, error) {
  1068. if w == WireBytes { // packed
  1069. x, n := decodeVarint(b)
  1070. if n == 0 {
  1071. return nil, io.ErrUnexpectedEOF
  1072. }
  1073. b = b[n:]
  1074. if x > uint64(len(b)) {
  1075. return nil, io.ErrUnexpectedEOF
  1076. }
  1077. res := b[x:]
  1078. b = b[:x]
  1079. for len(b) > 0 {
  1080. if len(b) < 8 {
  1081. return nil, io.ErrUnexpectedEOF
  1082. }
  1083. v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
  1084. s := f.toInt64Slice()
  1085. *s = append(*s, v)
  1086. b = b[8:]
  1087. }
  1088. return res, nil
  1089. }
  1090. if w != WireFixed64 {
  1091. return b, errInternalBadWireType
  1092. }
  1093. if len(b) < 8 {
  1094. return nil, io.ErrUnexpectedEOF
  1095. }
  1096. v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
  1097. s := f.toInt64Slice()
  1098. *s = append(*s, v)
  1099. return b[8:], nil
  1100. }
  1101. func unmarshalFixed32Value(b []byte, f pointer, w int) ([]byte, error) {
  1102. if w != WireFixed32 {
  1103. return b, errInternalBadWireType
  1104. }
  1105. if len(b) < 4 {
  1106. return nil, io.ErrUnexpectedEOF
  1107. }
  1108. v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  1109. *f.toUint32() = v
  1110. return b[4:], nil
  1111. }
  1112. func unmarshalFixed32Ptr(b []byte, f pointer, w int) ([]byte, error) {
  1113. if w != WireFixed32 {
  1114. return b, errInternalBadWireType
  1115. }
  1116. if len(b) < 4 {
  1117. return nil, io.ErrUnexpectedEOF
  1118. }
  1119. v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  1120. *f.toUint32Ptr() = &v
  1121. return b[4:], nil
  1122. }
  1123. func unmarshalFixed32Slice(b []byte, f pointer, w int) ([]byte, error) {
  1124. if w == WireBytes { // packed
  1125. x, n := decodeVarint(b)
  1126. if n == 0 {
  1127. return nil, io.ErrUnexpectedEOF
  1128. }
  1129. b = b[n:]
  1130. if x > uint64(len(b)) {
  1131. return nil, io.ErrUnexpectedEOF
  1132. }
  1133. res := b[x:]
  1134. b = b[:x]
  1135. for len(b) > 0 {
  1136. if len(b) < 4 {
  1137. return nil, io.ErrUnexpectedEOF
  1138. }
  1139. v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  1140. s := f.toUint32Slice()
  1141. *s = append(*s, v)
  1142. b = b[4:]
  1143. }
  1144. return res, nil
  1145. }
  1146. if w != WireFixed32 {
  1147. return b, errInternalBadWireType
  1148. }
  1149. if len(b) < 4 {
  1150. return nil, io.ErrUnexpectedEOF
  1151. }
  1152. v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  1153. s := f.toUint32Slice()
  1154. *s = append(*s, v)
  1155. return b[4:], nil
  1156. }
  1157. func unmarshalFixedS32Value(b []byte, f pointer, w int) ([]byte, error) {
  1158. if w != WireFixed32 {
  1159. return b, errInternalBadWireType
  1160. }
  1161. if len(b) < 4 {
  1162. return nil, io.ErrUnexpectedEOF
  1163. }
  1164. v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
  1165. *f.toInt32() = v
  1166. return b[4:], nil
  1167. }
  1168. func unmarshalFixedS32Ptr(b []byte, f pointer, w int) ([]byte, error) {
  1169. if w != WireFixed32 {
  1170. return b, errInternalBadWireType
  1171. }
  1172. if len(b) < 4 {
  1173. return nil, io.ErrUnexpectedEOF
  1174. }
  1175. v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
  1176. f.setInt32Ptr(v)
  1177. return b[4:], nil
  1178. }
  1179. func unmarshalFixedS32Slice(b []byte, f pointer, w int) ([]byte, error) {
  1180. if w == WireBytes { // packed
  1181. x, n := decodeVarint(b)
  1182. if n == 0 {
  1183. return nil, io.ErrUnexpectedEOF
  1184. }
  1185. b = b[n:]
  1186. if x > uint64(len(b)) {
  1187. return nil, io.ErrUnexpectedEOF
  1188. }
  1189. res := b[x:]
  1190. b = b[:x]
  1191. for len(b) > 0 {
  1192. if len(b) < 4 {
  1193. return nil, io.ErrUnexpectedEOF
  1194. }
  1195. v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
  1196. f.appendInt32Slice(v)
  1197. b = b[4:]
  1198. }
  1199. return res, nil
  1200. }
  1201. if w != WireFixed32 {
  1202. return b, errInternalBadWireType
  1203. }
  1204. if len(b) < 4 {
  1205. return nil, io.ErrUnexpectedEOF
  1206. }
  1207. v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
  1208. f.appendInt32Slice(v)
  1209. return b[4:], nil
  1210. }
  1211. func unmarshalBoolValue(b []byte, f pointer, w int) ([]byte, error) {
  1212. if w != WireVarint {
  1213. return b, errInternalBadWireType
  1214. }
  1215. // Note: any length varint is allowed, even though any sane
  1216. // encoder will use one byte.
  1217. // See https://github.com/golang/protobuf/issues/76
  1218. x, n := decodeVarint(b)
  1219. if n == 0 {
  1220. return nil, io.ErrUnexpectedEOF
  1221. }
  1222. // TODO: check if x>1? Tests seem to indicate no.
  1223. v := x != 0
  1224. *f.toBool() = v
  1225. return b[n:], nil
  1226. }
  1227. func unmarshalBoolPtr(b []byte, f pointer, w int) ([]byte, error) {
  1228. if w != WireVarint {
  1229. return b, errInternalBadWireType
  1230. }
  1231. x, n := decodeVarint(b)
  1232. if n == 0 {
  1233. return nil, io.ErrUnexpectedEOF
  1234. }
  1235. v := x != 0
  1236. *f.toBoolPtr() = &v
  1237. return b[n:], nil
  1238. }
  1239. func unmarshalBoolSlice(b []byte, f pointer, w int) ([]byte, error) {
  1240. if w == WireBytes { // packed
  1241. x, n := decodeVarint(b)
  1242. if n == 0 {
  1243. return nil, io.ErrUnexpectedEOF
  1244. }
  1245. b = b[n:]
  1246. if x > uint64(len(b)) {
  1247. return nil, io.ErrUnexpectedEOF
  1248. }
  1249. res := b[x:]
  1250. b = b[:x]
  1251. for len(b) > 0 {
  1252. x, n = decodeVarint(b)
  1253. if n == 0 {
  1254. return nil, io.ErrUnexpectedEOF
  1255. }
  1256. v := x != 0
  1257. s := f.toBoolSlice()
  1258. *s = append(*s, v)
  1259. b = b[n:]
  1260. }
  1261. return res, nil
  1262. }
  1263. if w != WireVarint {
  1264. return b, errInternalBadWireType
  1265. }
  1266. x, n := decodeVarint(b)
  1267. if n == 0 {
  1268. return nil, io.ErrUnexpectedEOF
  1269. }
  1270. v := x != 0
  1271. s := f.toBoolSlice()
  1272. *s = append(*s, v)
  1273. return b[n:], nil
  1274. }
  1275. func unmarshalFloat64Value(b []byte, f pointer, w int) ([]byte, error) {
  1276. if w != WireFixed64 {
  1277. return b, errInternalBadWireType
  1278. }
  1279. if len(b) < 8 {
  1280. return nil, io.ErrUnexpectedEOF
  1281. }
  1282. v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
  1283. *f.toFloat64() = v
  1284. return b[8:], nil
  1285. }
  1286. func unmarshalFloat64Ptr(b []byte, f pointer, w int) ([]byte, error) {
  1287. if w != WireFixed64 {
  1288. return b, errInternalBadWireType
  1289. }
  1290. if len(b) < 8 {
  1291. return nil, io.ErrUnexpectedEOF
  1292. }
  1293. v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
  1294. *f.toFloat64Ptr() = &v
  1295. return b[8:], nil
  1296. }
  1297. func unmarshalFloat64Slice(b []byte, f pointer, w int) ([]byte, error) {
  1298. if w == WireBytes { // packed
  1299. x, n := decodeVarint(b)
  1300. if n == 0 {
  1301. return nil, io.ErrUnexpectedEOF
  1302. }
  1303. b = b[n:]
  1304. if x > uint64(len(b)) {
  1305. return nil, io.ErrUnexpectedEOF
  1306. }
  1307. res := b[x:]
  1308. b = b[:x]
  1309. for len(b) > 0 {
  1310. if len(b) < 8 {
  1311. return nil, io.ErrUnexpectedEOF
  1312. }
  1313. v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
  1314. s := f.toFloat64Slice()
  1315. *s = append(*s, v)
  1316. b = b[8:]
  1317. }
  1318. return res, nil
  1319. }
  1320. if w != WireFixed64 {
  1321. return b, errInternalBadWireType
  1322. }
  1323. if len(b) < 8 {
  1324. return nil, io.ErrUnexpectedEOF
  1325. }
  1326. v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
  1327. s := f.toFloat64Slice()
  1328. *s = append(*s, v)
  1329. return b[8:], nil
  1330. }
  1331. func unmarshalFloat32Value(b []byte, f pointer, w int) ([]byte, error) {
  1332. if w != WireFixed32 {
  1333. return b, errInternalBadWireType
  1334. }
  1335. if len(b) < 4 {
  1336. return nil, io.ErrUnexpectedEOF
  1337. }
  1338. v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
  1339. *f.toFloat32() = v
  1340. return b[4:], nil
  1341. }
  1342. func unmarshalFloat32Ptr(b []byte, f pointer, w int) ([]byte, error) {
  1343. if w != WireFixed32 {
  1344. return b, errInternalBadWireType
  1345. }
  1346. if len(b) < 4 {
  1347. return nil, io.ErrUnexpectedEOF
  1348. }
  1349. v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
  1350. *f.toFloat32Ptr() = &v
  1351. return b[4:], nil
  1352. }
  1353. func unmarshalFloat32Slice(b []byte, f pointer, w int) ([]byte, error) {
  1354. if w == WireBytes { // packed
  1355. x, n := decodeVarint(b)
  1356. if n == 0 {
  1357. return nil, io.ErrUnexpectedEOF
  1358. }
  1359. b = b[n:]
  1360. if x > uint64(len(b)) {
  1361. return nil, io.ErrUnexpectedEOF
  1362. }
  1363. res := b[x:]
  1364. b = b[:x]
  1365. for len(b) > 0 {
  1366. if len(b) < 4 {
  1367. return nil, io.ErrUnexpectedEOF
  1368. }
  1369. v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
  1370. s := f.toFloat32Slice()
  1371. *s = append(*s, v)
  1372. b = b[4:]
  1373. }
  1374. return res, nil
  1375. }
  1376. if w != WireFixed32 {
  1377. return b, errInternalBadWireType
  1378. }
  1379. if len(b) < 4 {
  1380. return nil, io.ErrUnexpectedEOF
  1381. }
  1382. v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
  1383. s := f.toFloat32Slice()
  1384. *s = append(*s, v)
  1385. return b[4:], nil
  1386. }
  1387. func unmarshalStringValue(b []byte, f pointer, w int) ([]byte, error) {
  1388. if w != WireBytes {
  1389. return b, errInternalBadWireType
  1390. }
  1391. x, n := decodeVarint(b)
  1392. if n == 0 {
  1393. return nil, io.ErrUnexpectedEOF
  1394. }
  1395. b = b[n:]
  1396. if x > uint64(len(b)) {
  1397. return nil, io.ErrUnexpectedEOF
  1398. }
  1399. v := string(b[:x])
  1400. *f.toString() = v
  1401. return b[x:], nil
  1402. }
  1403. func unmarshalStringPtr(b []byte, f pointer, w int) ([]byte, error) {
  1404. if w != WireBytes {
  1405. return b, errInternalBadWireType
  1406. }
  1407. x, n := decodeVarint(b)
  1408. if n == 0 {
  1409. return nil, io.ErrUnexpectedEOF
  1410. }
  1411. b = b[n:]
  1412. if x > uint64(len(b)) {
  1413. return nil, io.ErrUnexpectedEOF
  1414. }
  1415. v := string(b[:x])
  1416. *f.toStringPtr() = &v
  1417. return b[x:], nil
  1418. }
  1419. func unmarshalStringSlice(b []byte, f pointer, w int) ([]byte, error) {
  1420. if w != WireBytes {
  1421. return b, errInternalBadWireType
  1422. }
  1423. x, n := decodeVarint(b)
  1424. if n == 0 {
  1425. return nil, io.ErrUnexpectedEOF
  1426. }
  1427. b = b[n:]
  1428. if x > uint64(len(b)) {
  1429. return nil, io.ErrUnexpectedEOF
  1430. }
  1431. v := string(b[:x])
  1432. s := f.toStringSlice()
  1433. *s = append(*s, v)
  1434. return b[x:], nil
  1435. }
  1436. func unmarshalUTF8StringValue(b []byte, f pointer, w int) ([]byte, error) {
  1437. if w != WireBytes {
  1438. return b, errInternalBadWireType
  1439. }
  1440. x, n := decodeVarint(b)
  1441. if n == 0 {
  1442. return nil, io.ErrUnexpectedEOF
  1443. }
  1444. b = b[n:]
  1445. if x > uint64(len(b)) {
  1446. return nil, io.ErrUnexpectedEOF
  1447. }
  1448. v := string(b[:x])
  1449. *f.toString() = v
  1450. if !utf8.ValidString(v) {
  1451. return b[x:], errInvalidUTF8
  1452. }
  1453. return b[x:], nil
  1454. }
  1455. func unmarshalUTF8StringPtr(b []byte, f pointer, w int) ([]byte, error) {
  1456. if w != WireBytes {
  1457. return b, errInternalBadWireType
  1458. }
  1459. x, n := decodeVarint(b)
  1460. if n == 0 {
  1461. return nil, io.ErrUnexpectedEOF
  1462. }
  1463. b = b[n:]
  1464. if x > uint64(len(b)) {
  1465. return nil, io.ErrUnexpectedEOF
  1466. }
  1467. v := string(b[:x])
  1468. *f.toStringPtr() = &v
  1469. if !utf8.ValidString(v) {
  1470. return b[x:], errInvalidUTF8
  1471. }
  1472. return b[x:], nil
  1473. }
  1474. func unmarshalUTF8StringSlice(b []byte, f pointer, w int) ([]byte, error) {
  1475. if w != WireBytes {
  1476. return b, errInternalBadWireType
  1477. }
  1478. x, n := decodeVarint(b)
  1479. if n == 0 {
  1480. return nil, io.ErrUnexpectedEOF
  1481. }
  1482. b = b[n:]
  1483. if x > uint64(len(b)) {
  1484. return nil, io.ErrUnexpectedEOF
  1485. }
  1486. v := string(b[:x])
  1487. s := f.toStringSlice()
  1488. *s = append(*s, v)
  1489. if !utf8.ValidString(v) {
  1490. return b[x:], errInvalidUTF8
  1491. }
  1492. return b[x:], nil
  1493. }
  1494. var emptyBuf [0]byte
  1495. func unmarshalBytesValue(b []byte, f pointer, w int) ([]byte, error) {
  1496. if w != WireBytes {
  1497. return b, errInternalBadWireType
  1498. }
  1499. x, n := decodeVarint(b)
  1500. if n == 0 {
  1501. return nil, io.ErrUnexpectedEOF
  1502. }
  1503. b = b[n:]
  1504. if x > uint64(len(b)) {
  1505. return nil, io.ErrUnexpectedEOF
  1506. }
  1507. // The use of append here is a trick which avoids the zeroing
  1508. // that would be required if we used a make/copy pair.
  1509. // We append to emptyBuf instead of nil because we want
  1510. // a non-nil result even when the length is 0.
  1511. v := append(emptyBuf[:], b[:x]...)
  1512. *f.toBytes() = v
  1513. return b[x:], nil
  1514. }
  1515. func unmarshalBytesSlice(b []byte, f pointer, w int) ([]byte, error) {
  1516. if w != WireBytes {
  1517. return b, errInternalBadWireType
  1518. }
  1519. x, n := decodeVarint(b)
  1520. if n == 0 {
  1521. return nil, io.ErrUnexpectedEOF
  1522. }
  1523. b = b[n:]
  1524. if x > uint64(len(b)) {
  1525. return nil, io.ErrUnexpectedEOF
  1526. }
  1527. v := append(emptyBuf[:], b[:x]...)
  1528. s := f.toBytesSlice()
  1529. *s = append(*s, v)
  1530. return b[x:], nil
  1531. }
  1532. func makeUnmarshalMessagePtr(sub *unmarshalInfo, name string) unmarshaler {
  1533. return func(b []byte, f pointer, w int) ([]byte, error) {
  1534. if w != WireBytes {
  1535. return b, errInternalBadWireType
  1536. }
  1537. x, n := decodeVarint(b)
  1538. if n == 0 {
  1539. return nil, io.ErrUnexpectedEOF
  1540. }
  1541. b = b[n:]
  1542. if x > uint64(len(b)) {
  1543. return nil, io.ErrUnexpectedEOF
  1544. }
  1545. // First read the message field to see if something is there.
  1546. // The semantics of multiple submessages are weird. Instead of
  1547. // the last one winning (as it is for all other fields), multiple
  1548. // submessages are merged.
  1549. v := f.getPointer()
  1550. if v.isNil() {
  1551. v = valToPointer(reflect.New(sub.typ))
  1552. f.setPointer(v)
  1553. }
  1554. err := sub.unmarshal(v, b[:x])
  1555. if err != nil {
  1556. if r, ok := err.(*RequiredNotSetError); ok {
  1557. r.field = name + "." + r.field
  1558. } else {
  1559. return nil, err
  1560. }
  1561. }
  1562. return b[x:], err
  1563. }
  1564. }
  1565. func makeUnmarshalMessageSlicePtr(sub *unmarshalInfo, name string) unmarshaler {
  1566. return func(b []byte, f pointer, w int) ([]byte, error) {
  1567. if w != WireBytes {
  1568. return b, errInternalBadWireType
  1569. }
  1570. x, n := decodeVarint(b)
  1571. if n == 0 {
  1572. return nil, io.ErrUnexpectedEOF
  1573. }
  1574. b = b[n:]
  1575. if x > uint64(len(b)) {
  1576. return nil, io.ErrUnexpectedEOF
  1577. }
  1578. v := valToPointer(reflect.New(sub.typ))
  1579. err := sub.unmarshal(v, b[:x])
  1580. if err != nil {
  1581. if r, ok := err.(*RequiredNotSetError); ok {
  1582. r.field = name + "." + r.field
  1583. } else {
  1584. return nil, err
  1585. }
  1586. }
  1587. f.appendPointer(v)
  1588. return b[x:], err
  1589. }
  1590. }
  1591. func makeUnmarshalGroupPtr(sub *unmarshalInfo, name string) unmarshaler {
  1592. return func(b []byte, f pointer, w int) ([]byte, error) {
  1593. if w != WireStartGroup {
  1594. return b, errInternalBadWireType
  1595. }
  1596. x, y := findEndGroup(b)
  1597. if x < 0 {
  1598. return nil, io.ErrUnexpectedEOF
  1599. }
  1600. v := f.getPointer()
  1601. if v.isNil() {
  1602. v = valToPointer(reflect.New(sub.typ))
  1603. f.setPointer(v)
  1604. }
  1605. err := sub.unmarshal(v, b[:x])
  1606. if err != nil {
  1607. if r, ok := err.(*RequiredNotSetError); ok {
  1608. r.field = name + "." + r.field
  1609. } else {
  1610. return nil, err
  1611. }
  1612. }
  1613. return b[y:], err
  1614. }
  1615. }
  1616. func makeUnmarshalGroupSlicePtr(sub *unmarshalInfo, name string) unmarshaler {
  1617. return func(b []byte, f pointer, w int) ([]byte, error) {
  1618. if w != WireStartGroup {
  1619. return b, errInternalBadWireType
  1620. }
  1621. x, y := findEndGroup(b)
  1622. if x < 0 {
  1623. return nil, io.ErrUnexpectedEOF
  1624. }
  1625. v := valToPointer(reflect.New(sub.typ))
  1626. err := sub.unmarshal(v, b[:x])
  1627. if err != nil {
  1628. if r, ok := err.(*RequiredNotSetError); ok {
  1629. r.field = name + "." + r.field
  1630. } else {
  1631. return nil, err
  1632. }
  1633. }
  1634. f.appendPointer(v)
  1635. return b[y:], err
  1636. }
  1637. }
  1638. func makeUnmarshalMap(f *reflect.StructField) unmarshaler {
  1639. t := f.Type
  1640. kt := t.Key()
  1641. vt := t.Elem()
  1642. unmarshalKey := typeUnmarshaler(kt, f.Tag.Get("protobuf_key"))
  1643. unmarshalVal := typeUnmarshaler(vt, f.Tag.Get("protobuf_val"))
  1644. return func(b []byte, f pointer, w int) ([]byte, error) {
  1645. // The map entry is a submessage. Figure out how big it is.
  1646. if w != WireBytes {
  1647. return nil, fmt.Errorf("proto: bad wiretype for map field: got %d want %d", w, WireBytes)
  1648. }
  1649. x, n := decodeVarint(b)
  1650. if n == 0 {
  1651. return nil, io.ErrUnexpectedEOF
  1652. }
  1653. b = b[n:]
  1654. if x > uint64(len(b)) {
  1655. return nil, io.ErrUnexpectedEOF
  1656. }
  1657. r := b[x:] // unused data to return
  1658. b = b[:x] // data for map entry
  1659. // Note: we could use #keys * #values ~= 200 functions
  1660. // to do map decoding without reflection. Probably not worth it.
  1661. // Maps will be somewhat slow. Oh well.
  1662. // Read key and value from data.
  1663. var nerr nonFatal
  1664. k := reflect.New(kt)
  1665. v := reflect.New(vt)
  1666. for len(b) > 0 {
  1667. x, n := decodeVarint(b)
  1668. if n == 0 {
  1669. return nil, io.ErrUnexpectedEOF
  1670. }
  1671. wire := int(x) & 7
  1672. b = b[n:]
  1673. var err error
  1674. switch x >> 3 {
  1675. case 1:
  1676. b, err = unmarshalKey(b, valToPointer(k), wire)
  1677. case 2:
  1678. b, err = unmarshalVal(b, valToPointer(v), wire)
  1679. default:
  1680. err = errInternalBadWireType // skip unknown tag
  1681. }
  1682. if nerr.Merge(err) {
  1683. continue
  1684. }
  1685. if err != errInternalBadWireType {
  1686. return nil, err
  1687. }
  1688. // Skip past unknown fields.
  1689. b, err = skipField(b, wire)
  1690. if err != nil {
  1691. return nil, err
  1692. }
  1693. }
  1694. // Get map, allocate if needed.
  1695. m := f.asPointerTo(t).Elem() // an addressable map[K]T
  1696. if m.IsNil() {
  1697. m.Set(reflect.MakeMap(t))
  1698. }
  1699. // Insert into map.
  1700. m.SetMapIndex(k.Elem(), v.Elem())
  1701. return r, nerr.E
  1702. }
  1703. }
  1704. // makeUnmarshalOneof makes an unmarshaler for oneof fields.
  1705. // for:
  1706. // message Msg {
  1707. // oneof F {
  1708. // int64 X = 1;
  1709. // float64 Y = 2;
  1710. // }
  1711. // }
  1712. // typ is the type of the concrete entry for a oneof case (e.g. Msg_X).
  1713. // ityp is the interface type of the oneof field (e.g. isMsg_F).
  1714. // unmarshal is the unmarshaler for the base type of the oneof case (e.g. int64).
  1715. // Note that this function will be called once for each case in the oneof.
  1716. func makeUnmarshalOneof(typ, ityp reflect.Type, unmarshal unmarshaler) unmarshaler {
  1717. sf := typ.Field(0)
  1718. field0 := toField(&sf)
  1719. return func(b []byte, f pointer, w int) ([]byte, error) {
  1720. // Allocate holder for value.
  1721. v := reflect.New(typ)
  1722. // Unmarshal data into holder.
  1723. // We unmarshal into the first field of the holder object.
  1724. var err error
  1725. var nerr nonFatal
  1726. b, err = unmarshal(b, valToPointer(v).offset(field0), w)
  1727. if !nerr.Merge(err) {
  1728. return nil, err
  1729. }
  1730. // Write pointer to holder into target field.
  1731. f.asPointerTo(ityp).Elem().Set(v)
  1732. return b, nerr.E
  1733. }
  1734. }
  1735. // Error used by decode internally.
  1736. var errInternalBadWireType = errors.New("proto: internal error: bad wiretype")
  1737. // skipField skips past a field of type wire and returns the remaining bytes.
  1738. func skipField(b []byte, wire int) ([]byte, error) {
  1739. switch wire {
  1740. case WireVarint:
  1741. _, k := decodeVarint(b)
  1742. if k == 0 {
  1743. return b, io.ErrUnexpectedEOF
  1744. }
  1745. b = b[k:]
  1746. case WireFixed32:
  1747. if len(b) < 4 {
  1748. return b, io.ErrUnexpectedEOF
  1749. }
  1750. b = b[4:]
  1751. case WireFixed64:
  1752. if len(b) < 8 {
  1753. return b, io.ErrUnexpectedEOF
  1754. }
  1755. b = b[8:]
  1756. case WireBytes:
  1757. m, k := decodeVarint(b)
  1758. if k == 0 || uint64(len(b)-k) < m {
  1759. return b, io.ErrUnexpectedEOF
  1760. }
  1761. b = b[uint64(k)+m:]
  1762. case WireStartGroup:
  1763. _, i := findEndGroup(b)
  1764. if i == -1 {
  1765. return b, io.ErrUnexpectedEOF
  1766. }
  1767. b = b[i:]
  1768. default:
  1769. return b, fmt.Errorf("proto: can't skip unknown wire type %d", wire)
  1770. }
  1771. return b, nil
  1772. }
  1773. // findEndGroup finds the index of the next EndGroup tag.
  1774. // Groups may be nested, so the "next" EndGroup tag is the first
  1775. // unpaired EndGroup.
  1776. // findEndGroup returns the indexes of the start and end of the EndGroup tag.
  1777. // Returns (-1,-1) if it can't find one.
  1778. func findEndGroup(b []byte) (int, int) {
  1779. depth := 1
  1780. i := 0
  1781. for {
  1782. x, n := decodeVarint(b[i:])
  1783. if n == 0 {
  1784. return -1, -1
  1785. }
  1786. j := i
  1787. i += n
  1788. switch x & 7 {
  1789. case WireVarint:
  1790. _, k := decodeVarint(b[i:])
  1791. if k == 0 {
  1792. return -1, -1
  1793. }
  1794. i += k
  1795. case WireFixed32:
  1796. if len(b)-4 < i {
  1797. return -1, -1
  1798. }
  1799. i += 4
  1800. case WireFixed64:
  1801. if len(b)-8 < i {
  1802. return -1, -1
  1803. }
  1804. i += 8
  1805. case WireBytes:
  1806. m, k := decodeVarint(b[i:])
  1807. if k == 0 {
  1808. return -1, -1
  1809. }
  1810. i += k
  1811. if uint64(len(b)-i) < m {
  1812. return -1, -1
  1813. }
  1814. i += int(m)
  1815. case WireStartGroup:
  1816. depth++
  1817. case WireEndGroup:
  1818. depth--
  1819. if depth == 0 {
  1820. return j, i
  1821. }
  1822. default:
  1823. return -1, -1
  1824. }
  1825. }
  1826. }
  1827. // encodeVarint appends a varint-encoded integer to b and returns the result.
  1828. func encodeVarint(b []byte, x uint64) []byte {
  1829. for x >= 1<<7 {
  1830. b = append(b, byte(x&0x7f|0x80))
  1831. x >>= 7
  1832. }
  1833. return append(b, byte(x))
  1834. }
  1835. // decodeVarint reads a varint-encoded integer from b.
  1836. // Returns the decoded integer and the number of bytes read.
  1837. // If there is an error, it returns 0,0.
  1838. func decodeVarint(b []byte) (uint64, int) {
  1839. var x, y uint64
  1840. if len(b) <= 0 {
  1841. goto bad
  1842. }
  1843. x = uint64(b[0])
  1844. if x < 0x80 {
  1845. return x, 1
  1846. }
  1847. x -= 0x80
  1848. if len(b) <= 1 {
  1849. goto bad
  1850. }
  1851. y = uint64(b[1])
  1852. x += y << 7
  1853. if y < 0x80 {
  1854. return x, 2
  1855. }
  1856. x -= 0x80 << 7
  1857. if len(b) <= 2 {
  1858. goto bad
  1859. }
  1860. y = uint64(b[2])
  1861. x += y << 14
  1862. if y < 0x80 {
  1863. return x, 3
  1864. }
  1865. x -= 0x80 << 14
  1866. if len(b) <= 3 {
  1867. goto bad
  1868. }
  1869. y = uint64(b[3])
  1870. x += y << 21
  1871. if y < 0x80 {
  1872. return x, 4
  1873. }
  1874. x -= 0x80 << 21
  1875. if len(b) <= 4 {
  1876. goto bad
  1877. }
  1878. y = uint64(b[4])
  1879. x += y << 28
  1880. if y < 0x80 {
  1881. return x, 5
  1882. }
  1883. x -= 0x80 << 28
  1884. if len(b) <= 5 {
  1885. goto bad
  1886. }
  1887. y = uint64(b[5])
  1888. x += y << 35
  1889. if y < 0x80 {
  1890. return x, 6
  1891. }
  1892. x -= 0x80 << 35
  1893. if len(b) <= 6 {
  1894. goto bad
  1895. }
  1896. y = uint64(b[6])
  1897. x += y << 42
  1898. if y < 0x80 {
  1899. return x, 7
  1900. }
  1901. x -= 0x80 << 42
  1902. if len(b) <= 7 {
  1903. goto bad
  1904. }
  1905. y = uint64(b[7])
  1906. x += y << 49
  1907. if y < 0x80 {
  1908. return x, 8
  1909. }
  1910. x -= 0x80 << 49
  1911. if len(b) <= 8 {
  1912. goto bad
  1913. }
  1914. y = uint64(b[8])
  1915. x += y << 56
  1916. if y < 0x80 {
  1917. return x, 9
  1918. }
  1919. x -= 0x80 << 56
  1920. if len(b) <= 9 {
  1921. goto bad
  1922. }
  1923. y = uint64(b[9])
  1924. x += y << 63
  1925. if y < 2 {
  1926. return x, 10
  1927. }
  1928. bad:
  1929. return 0, 0
  1930. }