table_unmarshal.go 49 KB

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