table_unmarshal.go 54 KB

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