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