table_unmarshal.go 49 KB

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