table_unmarshal.go 47 KB

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