table_unmarshal.go 48 KB

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