table_unmarshal.go 47 KB

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