table_unmarshal.go 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  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. }