table_unmarshal.go 49 KB

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