table_unmarshal.go 54 KB

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