You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2776 lines
73 KiB

  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. "math"
  36. "reflect"
  37. "sort"
  38. "strconv"
  39. "strings"
  40. "sync"
  41. "sync/atomic"
  42. "unicode/utf8"
  43. )
  44. // a sizer takes a pointer to a field and the size of its tag, computes the size of
  45. // the encoded data.
  46. type sizer func(pointer, int) int
  47. // a marshaler takes a byte slice, a pointer to a field, and its tag (in wire format),
  48. // marshals the field to the end of the slice, returns the slice and error (if any).
  49. type marshaler func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error)
  50. // marshalInfo is the information used for marshaling a message.
  51. type marshalInfo struct {
  52. typ reflect.Type
  53. fields []*marshalFieldInfo
  54. unrecognized field // offset of XXX_unrecognized
  55. extensions field // offset of XXX_InternalExtensions
  56. v1extensions field // offset of XXX_extensions
  57. sizecache field // offset of XXX_sizecache
  58. initialized int32 // 0 -- only typ is set, 1 -- fully initialized
  59. messageset bool // uses message set wire format
  60. hasmarshaler bool // has custom marshaler
  61. sync.RWMutex // protect extElems map, also for initialization
  62. extElems map[int32]*marshalElemInfo // info of extension elements
  63. }
  64. // marshalFieldInfo is the information used for marshaling a field of a message.
  65. type marshalFieldInfo struct {
  66. field field
  67. wiretag uint64 // tag in wire format
  68. tagsize int // size of tag in wire format
  69. sizer sizer
  70. marshaler marshaler
  71. isPointer bool
  72. required bool // field is required
  73. name string // name of the field, for error reporting
  74. oneofElems map[reflect.Type]*marshalElemInfo // info of oneof elements
  75. }
  76. // marshalElemInfo is the information used for marshaling an extension or oneof element.
  77. type marshalElemInfo struct {
  78. wiretag uint64 // tag in wire format
  79. tagsize int // size of tag in wire format
  80. sizer sizer
  81. marshaler marshaler
  82. isptr bool // elem is pointer typed, thus interface of this type is a direct interface (extension only)
  83. deref bool // dereference the pointer before operating on it; implies isptr
  84. }
  85. var (
  86. marshalInfoMap = map[reflect.Type]*marshalInfo{}
  87. marshalInfoLock sync.Mutex
  88. )
  89. // getMarshalInfo returns the information to marshal a given type of message.
  90. // The info it returns may not necessarily initialized.
  91. // t is the type of the message (NOT the pointer to it).
  92. func getMarshalInfo(t reflect.Type) *marshalInfo {
  93. marshalInfoLock.Lock()
  94. u, ok := marshalInfoMap[t]
  95. if !ok {
  96. u = &marshalInfo{typ: t}
  97. marshalInfoMap[t] = u
  98. }
  99. marshalInfoLock.Unlock()
  100. return u
  101. }
  102. // Size is the entry point from generated code,
  103. // and should be ONLY called by generated code.
  104. // It computes the size of encoded data of msg.
  105. // a is a pointer to a place to store cached marshal info.
  106. func (a *InternalMessageInfo) Size(msg Message) int {
  107. u := getMessageMarshalInfo(msg, a)
  108. ptr := toPointer(&msg)
  109. if ptr.isNil() {
  110. // We get here if msg is a typed nil ((*SomeMessage)(nil)),
  111. // so it satisfies the interface, and msg == nil wouldn't
  112. // catch it. We don't want crash in this case.
  113. return 0
  114. }
  115. return u.size(ptr)
  116. }
  117. // Marshal is the entry point from generated code,
  118. // and should be ONLY called by generated code.
  119. // It marshals msg to the end of b.
  120. // a is a pointer to a place to store cached marshal info.
  121. func (a *InternalMessageInfo) Marshal(b []byte, msg Message, deterministic bool) ([]byte, error) {
  122. u := getMessageMarshalInfo(msg, a)
  123. ptr := toPointer(&msg)
  124. if ptr.isNil() {
  125. // We get here if msg is a typed nil ((*SomeMessage)(nil)),
  126. // so it satisfies the interface, and msg == nil wouldn't
  127. // catch it. We don't want crash in this case.
  128. return b, ErrNil
  129. }
  130. return u.marshal(b, ptr, deterministic)
  131. }
  132. func getMessageMarshalInfo(msg interface{}, a *InternalMessageInfo) *marshalInfo {
  133. // u := a.marshal, but atomically.
  134. // We use an atomic here to ensure memory consistency.
  135. u := atomicLoadMarshalInfo(&a.marshal)
  136. if u == nil {
  137. // Get marshal information from type of message.
  138. t := reflect.ValueOf(msg).Type()
  139. if t.Kind() != reflect.Ptr {
  140. panic(fmt.Sprintf("cannot handle non-pointer message type %v", t))
  141. }
  142. u = getMarshalInfo(t.Elem())
  143. // Store it in the cache for later users.
  144. // a.marshal = u, but atomically.
  145. atomicStoreMarshalInfo(&a.marshal, u)
  146. }
  147. return u
  148. }
  149. // size is the main function to compute the size of the encoded data of a message.
  150. // ptr is the pointer to the message.
  151. func (u *marshalInfo) size(ptr pointer) int {
  152. if atomic.LoadInt32(&u.initialized) == 0 {
  153. u.computeMarshalInfo()
  154. }
  155. // If the message can marshal itself, let it do it, for compatibility.
  156. // NOTE: This is not efficient.
  157. if u.hasmarshaler {
  158. m := ptr.asPointerTo(u.typ).Interface().(Marshaler)
  159. b, _ := m.Marshal()
  160. return len(b)
  161. }
  162. n := 0
  163. for _, f := range u.fields {
  164. if f.isPointer && ptr.offset(f.field).getPointer().isNil() {
  165. // nil pointer always marshals to nothing
  166. continue
  167. }
  168. n += f.sizer(ptr.offset(f.field), f.tagsize)
  169. }
  170. if u.extensions.IsValid() {
  171. e := ptr.offset(u.extensions).toExtensions()
  172. if u.messageset {
  173. n += u.sizeMessageSet(e)
  174. } else {
  175. n += u.sizeExtensions(e)
  176. }
  177. }
  178. if u.v1extensions.IsValid() {
  179. m := *ptr.offset(u.v1extensions).toOldExtensions()
  180. n += u.sizeV1Extensions(m)
  181. }
  182. if u.unrecognized.IsValid() {
  183. s := *ptr.offset(u.unrecognized).toBytes()
  184. n += len(s)
  185. }
  186. // cache the result for use in marshal
  187. if u.sizecache.IsValid() {
  188. atomic.StoreInt32(ptr.offset(u.sizecache).toInt32(), int32(n))
  189. }
  190. return n
  191. }
  192. // cachedsize gets the size from cache. If there is no cache (i.e. message is not generated),
  193. // fall back to compute the size.
  194. func (u *marshalInfo) cachedsize(ptr pointer) int {
  195. if u.sizecache.IsValid() {
  196. return int(atomic.LoadInt32(ptr.offset(u.sizecache).toInt32()))
  197. }
  198. return u.size(ptr)
  199. }
  200. // marshal is the main function to marshal a message. It takes a byte slice and appends
  201. // the encoded data to the end of the slice, returns the slice and error (if any).
  202. // ptr is the pointer to the message.
  203. // If deterministic is true, map is marshaled in deterministic order.
  204. func (u *marshalInfo) marshal(b []byte, ptr pointer, deterministic bool) ([]byte, error) {
  205. if atomic.LoadInt32(&u.initialized) == 0 {
  206. u.computeMarshalInfo()
  207. }
  208. // If the message can marshal itself, let it do it, for compatibility.
  209. // NOTE: This is not efficient.
  210. if u.hasmarshaler {
  211. m := ptr.asPointerTo(u.typ).Interface().(Marshaler)
  212. b1, err := m.Marshal()
  213. b = append(b, b1...)
  214. return b, err
  215. }
  216. var err, errLater error
  217. // The old marshaler encodes extensions at beginning.
  218. if u.extensions.IsValid() {
  219. e := ptr.offset(u.extensions).toExtensions()
  220. if u.messageset {
  221. b, err = u.appendMessageSet(b, e, deterministic)
  222. } else {
  223. b, err = u.appendExtensions(b, e, deterministic)
  224. }
  225. if err != nil {
  226. return b, err
  227. }
  228. }
  229. if u.v1extensions.IsValid() {
  230. m := *ptr.offset(u.v1extensions).toOldExtensions()
  231. b, err = u.appendV1Extensions(b, m, deterministic)
  232. if err != nil {
  233. return b, err
  234. }
  235. }
  236. for _, f := range u.fields {
  237. if f.required {
  238. if ptr.offset(f.field).getPointer().isNil() {
  239. // Required field is not set.
  240. // We record the error but keep going, to give a complete marshaling.
  241. if errLater == nil {
  242. errLater = &RequiredNotSetError{f.name}
  243. }
  244. continue
  245. }
  246. }
  247. if f.isPointer && ptr.offset(f.field).getPointer().isNil() {
  248. // nil pointer always marshals to nothing
  249. continue
  250. }
  251. b, err = f.marshaler(b, ptr.offset(f.field), f.wiretag, deterministic)
  252. if err != nil {
  253. if err1, ok := err.(*RequiredNotSetError); ok {
  254. // Required field in submessage is not set.
  255. // We record the error but keep going, to give a complete marshaling.
  256. if errLater == nil {
  257. errLater = &RequiredNotSetError{f.name + "." + err1.field}
  258. }
  259. continue
  260. }
  261. if err == errRepeatedHasNil {
  262. err = errors.New("proto: repeated field " + f.name + " has nil element")
  263. }
  264. if err == errInvalidUTF8 {
  265. if errLater == nil {
  266. fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name
  267. errLater = &invalidUTF8Error{fullName}
  268. }
  269. continue
  270. }
  271. return b, err
  272. }
  273. }
  274. if u.unrecognized.IsValid() {
  275. s := *ptr.offset(u.unrecognized).toBytes()
  276. b = append(b, s...)
  277. }
  278. return b, errLater
  279. }
  280. // computeMarshalInfo initializes the marshal info.
  281. func (u *marshalInfo) computeMarshalInfo() {
  282. u.Lock()
  283. defer u.Unlock()
  284. if u.initialized != 0 { // non-atomic read is ok as it is protected by the lock
  285. return
  286. }
  287. t := u.typ
  288. u.unrecognized = invalidField
  289. u.extensions = invalidField
  290. u.v1extensions = invalidField
  291. u.sizecache = invalidField
  292. // If the message can marshal itself, let it do it, for compatibility.
  293. // NOTE: This is not efficient.
  294. if reflect.PtrTo(t).Implements(marshalerType) {
  295. u.hasmarshaler = true
  296. atomic.StoreInt32(&u.initialized, 1)
  297. return
  298. }
  299. // get oneof implementers
  300. var oneofImplementers []interface{}
  301. switch m := reflect.Zero(reflect.PtrTo(t)).Interface().(type) {
  302. case oneofFuncsIface:
  303. _, _, _, oneofImplementers = m.XXX_OneofFuncs()
  304. case oneofWrappersIface:
  305. oneofImplementers = m.XXX_OneofWrappers()
  306. }
  307. n := t.NumField()
  308. // deal with XXX fields first
  309. for i := 0; i < t.NumField(); i++ {
  310. f := t.Field(i)
  311. if !strings.HasPrefix(f.Name, "XXX_") {
  312. continue
  313. }
  314. switch f.Name {
  315. case "XXX_sizecache":
  316. u.sizecache = toField(&f)
  317. case "XXX_unrecognized":
  318. u.unrecognized = toField(&f)
  319. case "XXX_InternalExtensions":
  320. u.extensions = toField(&f)
  321. u.messageset = f.Tag.Get("protobuf_messageset") == "1"
  322. case "XXX_extensions":
  323. u.v1extensions = toField(&f)
  324. case "XXX_NoUnkeyedLiteral":
  325. // nothing to do
  326. default:
  327. panic("unknown XXX field: " + f.Name)
  328. }
  329. n--
  330. }
  331. // normal fields
  332. fields := make([]marshalFieldInfo, n) // batch allocation
  333. u.fields = make([]*marshalFieldInfo, 0, n)
  334. for i, j := 0, 0; i < t.NumField(); i++ {
  335. f := t.Field(i)
  336. if strings.HasPrefix(f.Name, "XXX_") {
  337. continue
  338. }
  339. field := &fields[j]
  340. j++
  341. field.name = f.Name
  342. u.fields = append(u.fields, field)
  343. if f.Tag.Get("protobuf_oneof") != "" {
  344. field.computeOneofFieldInfo(&f, oneofImplementers)
  345. continue
  346. }
  347. if f.Tag.Get("protobuf") == "" {
  348. // field has no tag (not in generated message), ignore it
  349. u.fields = u.fields[:len(u.fields)-1]
  350. j--
  351. continue
  352. }
  353. field.computeMarshalFieldInfo(&f)
  354. }
  355. // fields are marshaled in tag order on the wire.
  356. sort.Sort(byTag(u.fields))
  357. atomic.StoreInt32(&u.initialized, 1)
  358. }
  359. // helper for sorting fields by tag
  360. type byTag []*marshalFieldInfo
  361. func (a byTag) Len() int { return len(a) }
  362. func (a byTag) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  363. func (a byTag) Less(i, j int) bool { return a[i].wiretag < a[j].wiretag }
  364. // getExtElemInfo returns the information to marshal an extension element.
  365. // The info it returns is initialized.
  366. func (u *marshalInfo) getExtElemInfo(desc *ExtensionDesc) *marshalElemInfo {
  367. // get from cache first
  368. u.RLock()
  369. e, ok := u.extElems[desc.Field]
  370. u.RUnlock()
  371. if ok {
  372. return e
  373. }
  374. t := reflect.TypeOf(desc.ExtensionType) // pointer or slice to basic type or struct
  375. tags := strings.Split(desc.Tag, ",")
  376. tag, err := strconv.Atoi(tags[1])
  377. if err != nil {
  378. panic("tag is not an integer")
  379. }
  380. wt := wiretype(tags[0])
  381. if t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct {
  382. t = t.Elem()
  383. }
  384. sizer, marshaler := typeMarshaler(t, tags, false, false)
  385. var deref bool
  386. if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
  387. t = reflect.PtrTo(t)
  388. deref = true
  389. }
  390. e = &marshalElemInfo{
  391. wiretag: uint64(tag)<<3 | wt,
  392. tagsize: SizeVarint(uint64(tag) << 3),
  393. sizer: sizer,
  394. marshaler: marshaler,
  395. isptr: t.Kind() == reflect.Ptr,
  396. deref: deref,
  397. }
  398. // update cache
  399. u.Lock()
  400. if u.extElems == nil {
  401. u.extElems = make(map[int32]*marshalElemInfo)
  402. }
  403. u.extElems[desc.Field] = e
  404. u.Unlock()
  405. return e
  406. }
  407. // computeMarshalFieldInfo fills up the information to marshal a field.
  408. func (fi *marshalFieldInfo) computeMarshalFieldInfo(f *reflect.StructField) {
  409. // parse protobuf tag of the field.
  410. // tag has format of "bytes,49,opt,name=foo,def=hello!"
  411. tags := strings.Split(f.Tag.Get("protobuf"), ",")
  412. if tags[0] == "" {
  413. return
  414. }
  415. tag, err := strconv.Atoi(tags[1])
  416. if err != nil {
  417. panic("tag is not an integer")
  418. }
  419. wt := wiretype(tags[0])
  420. if tags[2] == "req" {
  421. fi.required = true
  422. }
  423. fi.setTag(f, tag, wt)
  424. fi.setMarshaler(f, tags)
  425. }
  426. func (fi *marshalFieldInfo) computeOneofFieldInfo(f *reflect.StructField, oneofImplementers []interface{}) {
  427. fi.field = toField(f)
  428. fi.wiretag = math.MaxInt32 // Use a large tag number, make oneofs sorted at the end. This tag will not appear on the wire.
  429. fi.isPointer = true
  430. fi.sizer, fi.marshaler = makeOneOfMarshaler(fi, f)
  431. fi.oneofElems = make(map[reflect.Type]*marshalElemInfo)
  432. ityp := f.Type // interface type
  433. for _, o := range oneofImplementers {
  434. t := reflect.TypeOf(o)
  435. if !t.Implements(ityp) {
  436. continue
  437. }
  438. sf := t.Elem().Field(0) // oneof implementer is a struct with a single field
  439. tags := strings.Split(sf.Tag.Get("protobuf"), ",")
  440. tag, err := strconv.Atoi(tags[1])
  441. if err != nil {
  442. panic("tag is not an integer")
  443. }
  444. wt := wiretype(tags[0])
  445. sizer, marshaler := typeMarshaler(sf.Type, tags, false, true) // oneof should not omit any zero value
  446. fi.oneofElems[t.Elem()] = &marshalElemInfo{
  447. wiretag: uint64(tag)<<3 | wt,
  448. tagsize: SizeVarint(uint64(tag) << 3),
  449. sizer: sizer,
  450. marshaler: marshaler,
  451. }
  452. }
  453. }
  454. // wiretype returns the wire encoding of the type.
  455. func wiretype(encoding string) uint64 {
  456. switch encoding {
  457. case "fixed32":
  458. return WireFixed32
  459. case "fixed64":
  460. return WireFixed64
  461. case "varint", "zigzag32", "zigzag64":
  462. return WireVarint
  463. case "bytes":
  464. return WireBytes
  465. case "group":
  466. return WireStartGroup
  467. }
  468. panic("unknown wire type " + encoding)
  469. }
  470. // setTag fills up the tag (in wire format) and its size in the info of a field.
  471. func (fi *marshalFieldInfo) setTag(f *reflect.StructField, tag int, wt uint64) {
  472. fi.field = toField(f)
  473. fi.wiretag = uint64(tag)<<3 | wt
  474. fi.tagsize = SizeVarint(uint64(tag) << 3)
  475. }
  476. // setMarshaler fills up the sizer and marshaler in the info of a field.
  477. func (fi *marshalFieldInfo) setMarshaler(f *reflect.StructField, tags []string) {
  478. switch f.Type.Kind() {
  479. case reflect.Map:
  480. // map field
  481. fi.isPointer = true
  482. fi.sizer, fi.marshaler = makeMapMarshaler(f)
  483. return
  484. case reflect.Ptr, reflect.Slice:
  485. fi.isPointer = true
  486. }
  487. fi.sizer, fi.marshaler = typeMarshaler(f.Type, tags, true, false)
  488. }
  489. // typeMarshaler returns the sizer and marshaler of a given field.
  490. // t is the type of the field.
  491. // tags is the generated "protobuf" tag of the field.
  492. // If nozero is true, zero value is not marshaled to the wire.
  493. // If oneof is true, it is a oneof field.
  494. func typeMarshaler(t reflect.Type, tags []string, nozero, oneof bool) (sizer, marshaler) {
  495. encoding := tags[0]
  496. pointer := false
  497. slice := false
  498. if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
  499. slice = true
  500. t = t.Elem()
  501. }
  502. if t.Kind() == reflect.Ptr {
  503. pointer = true
  504. t = t.Elem()
  505. }
  506. packed := false
  507. proto3 := false
  508. validateUTF8 := true
  509. for i := 2; i < len(tags); i++ {
  510. if tags[i] == "packed" {
  511. packed = true
  512. }
  513. if tags[i] == "proto3" {
  514. proto3 = true
  515. }
  516. }
  517. validateUTF8 = validateUTF8 && proto3
  518. switch t.Kind() {
  519. case reflect.Bool:
  520. if pointer {
  521. return sizeBoolPtr, appendBoolPtr
  522. }
  523. if slice {
  524. if packed {
  525. return sizeBoolPackedSlice, appendBoolPackedSlice
  526. }
  527. return sizeBoolSlice, appendBoolSlice
  528. }
  529. if nozero {
  530. return sizeBoolValueNoZero, appendBoolValueNoZero
  531. }
  532. return sizeBoolValue, appendBoolValue
  533. case reflect.Uint32:
  534. switch encoding {
  535. case "fixed32":
  536. if pointer {
  537. return sizeFixed32Ptr, appendFixed32Ptr
  538. }
  539. if slice {
  540. if packed {
  541. return sizeFixed32PackedSlice, appendFixed32PackedSlice
  542. }
  543. return sizeFixed32Slice, appendFixed32Slice
  544. }
  545. if nozero {
  546. return sizeFixed32ValueNoZero, appendFixed32ValueNoZero
  547. }
  548. return sizeFixed32Value, appendFixed32Value
  549. case "varint":
  550. if pointer {
  551. return sizeVarint32Ptr, appendVarint32Ptr
  552. }
  553. if slice {
  554. if packed {
  555. return sizeVarint32PackedSlice, appendVarint32PackedSlice
  556. }
  557. return sizeVarint32Slice, appendVarint32Slice
  558. }
  559. if nozero {
  560. return sizeVarint32ValueNoZero, appendVarint32ValueNoZero
  561. }
  562. return sizeVarint32Value, appendVarint32Value
  563. }
  564. case reflect.Int32:
  565. switch encoding {
  566. case "fixed32":
  567. if pointer {
  568. return sizeFixedS32Ptr, appendFixedS32Ptr
  569. }
  570. if slice {
  571. if packed {
  572. return sizeFixedS32PackedSlice, appendFixedS32PackedSlice
  573. }
  574. return sizeFixedS32Slice, appendFixedS32Slice
  575. }
  576. if nozero {
  577. return sizeFixedS32ValueNoZero, appendFixedS32ValueNoZero
  578. }
  579. return sizeFixedS32Value, appendFixedS32Value
  580. case "varint":
  581. if pointer {
  582. return sizeVarintS32Ptr, appendVarintS32Ptr
  583. }
  584. if slice {
  585. if packed {
  586. return sizeVarintS32PackedSlice, appendVarintS32PackedSlice
  587. }
  588. return sizeVarintS32Slice, appendVarintS32Slice
  589. }
  590. if nozero {
  591. return sizeVarintS32ValueNoZero, appendVarintS32ValueNoZero
  592. }
  593. return sizeVarintS32Value, appendVarintS32Value
  594. case "zigzag32":
  595. if pointer {
  596. return sizeZigzag32Ptr, appendZigzag32Ptr
  597. }
  598. if slice {
  599. if packed {
  600. return sizeZigzag32PackedSlice, appendZigzag32PackedSlice
  601. }
  602. return sizeZigzag32Slice, appendZigzag32Slice
  603. }
  604. if nozero {
  605. return sizeZigzag32ValueNoZero, appendZigzag32ValueNoZero
  606. }
  607. return sizeZigzag32Value, appendZigzag32Value
  608. }
  609. case reflect.Uint64:
  610. switch encoding {
  611. case "fixed64":
  612. if pointer {
  613. return sizeFixed64Ptr, appendFixed64Ptr
  614. }
  615. if slice {
  616. if packed {
  617. return sizeFixed64PackedSlice, appendFixed64PackedSlice
  618. }
  619. return sizeFixed64Slice, appendFixed64Slice
  620. }
  621. if nozero {
  622. return sizeFixed64ValueNoZero, appendFixed64ValueNoZero
  623. }
  624. return sizeFixed64Value, appendFixed64Value
  625. case "varint":
  626. if pointer {
  627. return sizeVarint64Ptr, appendVarint64Ptr
  628. }
  629. if slice {
  630. if packed {
  631. return sizeVarint64PackedSlice, appendVarint64PackedSlice
  632. }
  633. return sizeVarint64Slice, appendVarint64Slice
  634. }
  635. if nozero {
  636. return sizeVarint64ValueNoZero, appendVarint64ValueNoZero
  637. }
  638. return sizeVarint64Value, appendVarint64Value
  639. }
  640. case reflect.Int64:
  641. switch encoding {
  642. case "fixed64":
  643. if pointer {
  644. return sizeFixedS64Ptr, appendFixedS64Ptr
  645. }
  646. if slice {
  647. if packed {
  648. return sizeFixedS64PackedSlice, appendFixedS64PackedSlice
  649. }
  650. return sizeFixedS64Slice, appendFixedS64Slice
  651. }
  652. if nozero {
  653. return sizeFixedS64ValueNoZero, appendFixedS64ValueNoZero
  654. }
  655. return sizeFixedS64Value, appendFixedS64Value
  656. case "varint":
  657. if pointer {
  658. return sizeVarintS64Ptr, appendVarintS64Ptr
  659. }
  660. if slice {
  661. if packed {
  662. return sizeVarintS64PackedSlice, appendVarintS64PackedSlice
  663. }
  664. return sizeVarintS64Slice, appendVarintS64Slice
  665. }
  666. if nozero {
  667. return sizeVarintS64ValueNoZero, appendVarintS64ValueNoZero
  668. }
  669. return sizeVarintS64Value, appendVarintS64Value
  670. case "zigzag64":
  671. if pointer {
  672. return sizeZigzag64Ptr, appendZigzag64Ptr
  673. }
  674. if slice {
  675. if packed {
  676. return sizeZigzag64PackedSlice, appendZigzag64PackedSlice
  677. }
  678. return sizeZigzag64Slice, appendZigzag64Slice
  679. }
  680. if nozero {
  681. return sizeZigzag64ValueNoZero, appendZigzag64ValueNoZero
  682. }
  683. return sizeZigzag64Value, appendZigzag64Value
  684. }
  685. case reflect.Float32:
  686. if pointer {
  687. return sizeFloat32Ptr, appendFloat32Ptr
  688. }
  689. if slice {
  690. if packed {
  691. return sizeFloat32PackedSlice, appendFloat32PackedSlice
  692. }
  693. return sizeFloat32Slice, appendFloat32Slice
  694. }
  695. if nozero {
  696. return sizeFloat32ValueNoZero, appendFloat32ValueNoZero
  697. }
  698. return sizeFloat32Value, appendFloat32Value
  699. case reflect.Float64:
  700. if pointer {
  701. return sizeFloat64Ptr, appendFloat64Ptr
  702. }
  703. if slice {
  704. if packed {
  705. return sizeFloat64PackedSlice, appendFloat64PackedSlice
  706. }
  707. return sizeFloat64Slice, appendFloat64Slice
  708. }
  709. if nozero {
  710. return sizeFloat64ValueNoZero, appendFloat64ValueNoZero
  711. }
  712. return sizeFloat64Value, appendFloat64Value
  713. case reflect.String:
  714. if validateUTF8 {
  715. if pointer {
  716. return sizeStringPtr, appendUTF8StringPtr
  717. }
  718. if slice {
  719. return sizeStringSlice, appendUTF8StringSlice
  720. }
  721. if nozero {
  722. return sizeStringValueNoZero, appendUTF8StringValueNoZero
  723. }
  724. return sizeStringValue, appendUTF8StringValue
  725. }
  726. if pointer {
  727. return sizeStringPtr, appendStringPtr
  728. }
  729. if slice {
  730. return sizeStringSlice, appendStringSlice
  731. }
  732. if nozero {
  733. return sizeStringValueNoZero, appendStringValueNoZero
  734. }
  735. return sizeStringValue, appendStringValue
  736. case reflect.Slice:
  737. if slice {
  738. return sizeBytesSlice, appendBytesSlice
  739. }
  740. if oneof {
  741. // Oneof bytes field may also have "proto3" tag.
  742. // We want to marshal it as a oneof field. Do this
  743. // check before the proto3 check.
  744. return sizeBytesOneof, appendBytesOneof
  745. }
  746. if proto3 {
  747. return sizeBytes3, appendBytes3
  748. }
  749. return sizeBytes, appendBytes
  750. case reflect.Struct:
  751. switch encoding {
  752. case "group":
  753. if slice {
  754. return makeGroupSliceMarshaler(getMarshalInfo(t))
  755. }
  756. return makeGroupMarshaler(getMarshalInfo(t))
  757. case "bytes":
  758. if slice {
  759. return makeMessageSliceMarshaler(getMarshalInfo(t))
  760. }
  761. return makeMessageMarshaler(getMarshalInfo(t))
  762. }
  763. }
  764. panic(fmt.Sprintf("unknown or mismatched type: type: %v, wire type: %v", t, encoding))
  765. }
  766. // Below are functions to size/marshal a specific type of a field.
  767. // They are stored in the field's info, and called by function pointers.
  768. // They have type sizer or marshaler.
  769. func sizeFixed32Value(_ pointer, tagsize int) int {
  770. return 4 + tagsize
  771. }
  772. func sizeFixed32ValueNoZero(ptr pointer, tagsize int) int {
  773. v := *ptr.toUint32()
  774. if v == 0 {
  775. return 0
  776. }
  777. return 4 + tagsize
  778. }
  779. func sizeFixed32Ptr(ptr pointer, tagsize int) int {
  780. p := *ptr.toUint32Ptr()
  781. if p == nil {
  782. return 0
  783. }
  784. return 4 + tagsize
  785. }
  786. func sizeFixed32Slice(ptr pointer, tagsize int) int {
  787. s := *ptr.toUint32Slice()
  788. return (4 + tagsize) * len(s)
  789. }
  790. func sizeFixed32PackedSlice(ptr pointer, tagsize int) int {
  791. s := *ptr.toUint32Slice()
  792. if len(s) == 0 {
  793. return 0
  794. }
  795. return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
  796. }
  797. func sizeFixedS32Value(_ pointer, tagsize int) int {
  798. return 4 + tagsize
  799. }
  800. func sizeFixedS32ValueNoZero(ptr pointer, tagsize int) int {
  801. v := *ptr.toInt32()
  802. if v == 0 {
  803. return 0
  804. }
  805. return 4 + tagsize
  806. }
  807. func sizeFixedS32Ptr(ptr pointer, tagsize int) int {
  808. p := ptr.getInt32Ptr()
  809. if p == nil {
  810. return 0
  811. }
  812. return 4 + tagsize
  813. }
  814. func sizeFixedS32Slice(ptr pointer, tagsize int) int {
  815. s := ptr.getInt32Slice()
  816. return (4 + tagsize) * len(s)
  817. }
  818. func sizeFixedS32PackedSlice(ptr pointer, tagsize int) int {
  819. s := ptr.getInt32Slice()
  820. if len(s) == 0 {
  821. return 0
  822. }
  823. return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
  824. }
  825. func sizeFloat32Value(_ pointer, tagsize int) int {
  826. return 4 + tagsize
  827. }
  828. func sizeFloat32ValueNoZero(ptr pointer, tagsize int) int {
  829. v := math.Float32bits(*ptr.toFloat32())
  830. if v == 0 {
  831. return 0
  832. }
  833. return 4 + tagsize
  834. }
  835. func sizeFloat32Ptr(ptr pointer, tagsize int) int {
  836. p := *ptr.toFloat32Ptr()
  837. if p == nil {
  838. return 0
  839. }
  840. return 4 + tagsize
  841. }
  842. func sizeFloat32Slice(ptr pointer, tagsize int) int {
  843. s := *ptr.toFloat32Slice()
  844. return (4 + tagsize) * len(s)
  845. }
  846. func sizeFloat32PackedSlice(ptr pointer, tagsize int) int {
  847. s := *ptr.toFloat32Slice()
  848. if len(s) == 0 {
  849. return 0
  850. }
  851. return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
  852. }
  853. func sizeFixed64Value(_ pointer, tagsize int) int {
  854. return 8 + tagsize
  855. }
  856. func sizeFixed64ValueNoZero(ptr pointer, tagsize int) int {
  857. v := *ptr.toUint64()
  858. if v == 0 {
  859. return 0
  860. }
  861. return 8 + tagsize
  862. }
  863. func sizeFixed64Ptr(ptr pointer, tagsize int) int {
  864. p := *ptr.toUint64Ptr()
  865. if p == nil {
  866. return 0
  867. }
  868. return 8 + tagsize
  869. }
  870. func sizeFixed64Slice(ptr pointer, tagsize int) int {
  871. s := *ptr.toUint64Slice()
  872. return (8 + tagsize) * len(s)
  873. }
  874. func sizeFixed64PackedSlice(ptr pointer, tagsize int) int {
  875. s := *ptr.toUint64Slice()
  876. if len(s) == 0 {
  877. return 0
  878. }
  879. return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
  880. }
  881. func sizeFixedS64Value(_ pointer, tagsize int) int {
  882. return 8 + tagsize
  883. }
  884. func sizeFixedS64ValueNoZero(ptr pointer, tagsize int) int {
  885. v := *ptr.toInt64()
  886. if v == 0 {
  887. return 0
  888. }
  889. return 8 + tagsize
  890. }
  891. func sizeFixedS64Ptr(ptr pointer, tagsize int) int {
  892. p := *ptr.toInt64Ptr()
  893. if p == nil {
  894. return 0
  895. }
  896. return 8 + tagsize
  897. }
  898. func sizeFixedS64Slice(ptr pointer, tagsize int) int {
  899. s := *ptr.toInt64Slice()
  900. return (8 + tagsize) * len(s)
  901. }
  902. func sizeFixedS64PackedSlice(ptr pointer, tagsize int) int {
  903. s := *ptr.toInt64Slice()
  904. if len(s) == 0 {
  905. return 0
  906. }
  907. return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
  908. }
  909. func sizeFloat64Value(_ pointer, tagsize int) int {
  910. return 8 + tagsize
  911. }
  912. func sizeFloat64ValueNoZero(ptr pointer, tagsize int) int {
  913. v := math.Float64bits(*ptr.toFloat64())
  914. if v == 0 {
  915. return 0
  916. }
  917. return 8 + tagsize
  918. }
  919. func sizeFloat64Ptr(ptr pointer, tagsize int) int {
  920. p := *ptr.toFloat64Ptr()
  921. if p == nil {
  922. return 0
  923. }
  924. return 8 + tagsize
  925. }
  926. func sizeFloat64Slice(ptr pointer, tagsize int) int {
  927. s := *ptr.toFloat64Slice()
  928. return (8 + tagsize) * len(s)
  929. }
  930. func sizeFloat64PackedSlice(ptr pointer, tagsize int) int {
  931. s := *ptr.toFloat64Slice()
  932. if len(s) == 0 {
  933. return 0
  934. }
  935. return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
  936. }
  937. func sizeVarint32Value(ptr pointer, tagsize int) int {
  938. v := *ptr.toUint32()
  939. return SizeVarint(uint64(v)) + tagsize
  940. }
  941. func sizeVarint32ValueNoZero(ptr pointer, tagsize int) int {
  942. v := *ptr.toUint32()
  943. if v == 0 {
  944. return 0
  945. }
  946. return SizeVarint(uint64(v)) + tagsize
  947. }
  948. func sizeVarint32Ptr(ptr pointer, tagsize int) int {
  949. p := *ptr.toUint32Ptr()
  950. if p == nil {
  951. return 0
  952. }
  953. return SizeVarint(uint64(*p)) + tagsize
  954. }
  955. func sizeVarint32Slice(ptr pointer, tagsize int) int {
  956. s := *ptr.toUint32Slice()
  957. n := 0
  958. for _, v := range s {
  959. n += SizeVarint(uint64(v)) + tagsize
  960. }
  961. return n
  962. }
  963. func sizeVarint32PackedSlice(ptr pointer, tagsize int) int {
  964. s := *ptr.toUint32Slice()
  965. if len(s) == 0 {
  966. return 0
  967. }
  968. n := 0
  969. for _, v := range s {
  970. n += SizeVarint(uint64(v))
  971. }
  972. return n + SizeVarint(uint64(n)) + tagsize
  973. }
  974. func sizeVarintS32Value(ptr pointer, tagsize int) int {
  975. v := *ptr.toInt32()
  976. return SizeVarint(uint64(v)) + tagsize
  977. }
  978. func sizeVarintS32ValueNoZero(ptr pointer, tagsize int) int {
  979. v := *ptr.toInt32()
  980. if v == 0 {
  981. return 0
  982. }
  983. return SizeVarint(uint64(v)) + tagsize
  984. }
  985. func sizeVarintS32Ptr(ptr pointer, tagsize int) int {
  986. p := ptr.getInt32Ptr()
  987. if p == nil {
  988. return 0
  989. }
  990. return SizeVarint(uint64(*p)) + tagsize
  991. }
  992. func sizeVarintS32Slice(ptr pointer, tagsize int) int {
  993. s := ptr.getInt32Slice()
  994. n := 0
  995. for _, v := range s {
  996. n += SizeVarint(uint64(v)) + tagsize
  997. }
  998. return n
  999. }
  1000. func sizeVarintS32PackedSlice(ptr pointer, tagsize int) int {
  1001. s := ptr.getInt32Slice()
  1002. if len(s) == 0 {
  1003. return 0
  1004. }
  1005. n := 0
  1006. for _, v := range s {
  1007. n += SizeVarint(uint64(v))
  1008. }
  1009. return n + SizeVarint(uint64(n)) + tagsize
  1010. }
  1011. func sizeVarint64Value(ptr pointer, tagsize int) int {
  1012. v := *ptr.toUint64()
  1013. return SizeVarint(v) + tagsize
  1014. }
  1015. func sizeVarint64ValueNoZero(ptr pointer, tagsize int) int {
  1016. v := *ptr.toUint64()
  1017. if v == 0 {
  1018. return 0
  1019. }
  1020. return SizeVarint(v) + tagsize
  1021. }
  1022. func sizeVarint64Ptr(ptr pointer, tagsize int) int {
  1023. p := *ptr.toUint64Ptr()
  1024. if p == nil {
  1025. return 0
  1026. }
  1027. return SizeVarint(*p) + tagsize
  1028. }
  1029. func sizeVarint64Slice(ptr pointer, tagsize int) int {
  1030. s := *ptr.toUint64Slice()
  1031. n := 0
  1032. for _, v := range s {
  1033. n += SizeVarint(v) + tagsize
  1034. }
  1035. return n
  1036. }
  1037. func sizeVarint64PackedSlice(ptr pointer, tagsize int) int {
  1038. s := *ptr.toUint64Slice()
  1039. if len(s) == 0 {
  1040. return 0
  1041. }
  1042. n := 0
  1043. for _, v := range s {
  1044. n += SizeVarint(v)
  1045. }
  1046. return n + SizeVarint(uint64(n)) + tagsize
  1047. }
  1048. func sizeVarintS64Value(ptr pointer, tagsize int) int {
  1049. v := *ptr.toInt64()
  1050. return SizeVarint(uint64(v)) + tagsize
  1051. }
  1052. func sizeVarintS64ValueNoZero(ptr pointer, tagsize int) int {
  1053. v := *ptr.toInt64()
  1054. if v == 0 {
  1055. return 0
  1056. }
  1057. return SizeVarint(uint64(v)) + tagsize
  1058. }
  1059. func sizeVarintS64Ptr(ptr pointer, tagsize int) int {
  1060. p := *ptr.toInt64Ptr()
  1061. if p == nil {
  1062. return 0
  1063. }
  1064. return SizeVarint(uint64(*p)) + tagsize
  1065. }
  1066. func sizeVarintS64Slice(ptr pointer, tagsize int) int {
  1067. s := *ptr.toInt64Slice()
  1068. n := 0
  1069. for _, v := range s {
  1070. n += SizeVarint(uint64(v)) + tagsize
  1071. }
  1072. return n
  1073. }
  1074. func sizeVarintS64PackedSlice(ptr pointer, tagsize int) int {
  1075. s := *ptr.toInt64Slice()
  1076. if len(s) == 0 {
  1077. return 0
  1078. }
  1079. n := 0
  1080. for _, v := range s {
  1081. n += SizeVarint(uint64(v))
  1082. }
  1083. return n + SizeVarint(uint64(n)) + tagsize
  1084. }
  1085. func sizeZigzag32Value(ptr pointer, tagsize int) int {
  1086. v := *ptr.toInt32()
  1087. return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
  1088. }
  1089. func sizeZigzag32ValueNoZero(ptr pointer, tagsize int) int {
  1090. v := *ptr.toInt32()
  1091. if v == 0 {
  1092. return 0
  1093. }
  1094. return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
  1095. }
  1096. func sizeZigzag32Ptr(ptr pointer, tagsize int) int {
  1097. p := ptr.getInt32Ptr()
  1098. if p == nil {
  1099. return 0
  1100. }
  1101. v := *p
  1102. return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
  1103. }
  1104. func sizeZigzag32Slice(ptr pointer, tagsize int) int {
  1105. s := ptr.getInt32Slice()
  1106. n := 0
  1107. for _, v := range s {
  1108. n += SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
  1109. }
  1110. return n
  1111. }
  1112. func sizeZigzag32PackedSlice(ptr pointer, tagsize int) int {
  1113. s := ptr.getInt32Slice()
  1114. if len(s) == 0 {
  1115. return 0
  1116. }
  1117. n := 0
  1118. for _, v := range s {
  1119. n += SizeVarint(uint64((uint32(v) << 1) ^ uint32((int32(v) >> 31))))
  1120. }
  1121. return n + SizeVarint(uint64(n)) + tagsize
  1122. }
  1123. func sizeZigzag64Value(ptr pointer, tagsize int) int {
  1124. v := *ptr.toInt64()
  1125. return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
  1126. }
  1127. func sizeZigzag64ValueNoZero(ptr pointer, tagsize int) int {
  1128. v := *ptr.toInt64()
  1129. if v == 0 {
  1130. return 0
  1131. }
  1132. return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
  1133. }
  1134. func sizeZigzag64Ptr(ptr pointer, tagsize int) int {
  1135. p := *ptr.toInt64Ptr()
  1136. if p == nil {
  1137. return 0
  1138. }
  1139. v := *p
  1140. return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
  1141. }
  1142. func sizeZigzag64Slice(ptr pointer, tagsize int) int {
  1143. s := *ptr.toInt64Slice()
  1144. n := 0
  1145. for _, v := range s {
  1146. n += SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
  1147. }
  1148. return n
  1149. }
  1150. func sizeZigzag64PackedSlice(ptr pointer, tagsize int) int {
  1151. s := *ptr.toInt64Slice()
  1152. if len(s) == 0 {
  1153. return 0
  1154. }
  1155. n := 0
  1156. for _, v := range s {
  1157. n += SizeVarint(uint64(v<<1) ^ uint64((int64(v) >> 63)))
  1158. }
  1159. return n + SizeVarint(uint64(n)) + tagsize
  1160. }
  1161. func sizeBoolValue(_ pointer, tagsize int) int {
  1162. return 1 + tagsize
  1163. }
  1164. func sizeBoolValueNoZero(ptr pointer, tagsize int) int {
  1165. v := *ptr.toBool()
  1166. if !v {
  1167. return 0
  1168. }
  1169. return 1 + tagsize
  1170. }
  1171. func sizeBoolPtr(ptr pointer, tagsize int) int {
  1172. p := *ptr.toBoolPtr()
  1173. if p == nil {
  1174. return 0
  1175. }
  1176. return 1 + tagsize
  1177. }
  1178. func sizeBoolSlice(ptr pointer, tagsize int) int {
  1179. s := *ptr.toBoolSlice()
  1180. return (1 + tagsize) * len(s)
  1181. }
  1182. func sizeBoolPackedSlice(ptr pointer, tagsize int) int {
  1183. s := *ptr.toBoolSlice()
  1184. if len(s) == 0 {
  1185. return 0
  1186. }
  1187. return len(s) + SizeVarint(uint64(len(s))) + tagsize
  1188. }
  1189. func sizeStringValue(ptr pointer, tagsize int) int {
  1190. v := *ptr.toString()
  1191. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1192. }
  1193. func sizeStringValueNoZero(ptr pointer, tagsize int) int {
  1194. v := *ptr.toString()
  1195. if v == "" {
  1196. return 0
  1197. }
  1198. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1199. }
  1200. func sizeStringPtr(ptr pointer, tagsize int) int {
  1201. p := *ptr.toStringPtr()
  1202. if p == nil {
  1203. return 0
  1204. }
  1205. v := *p
  1206. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1207. }
  1208. func sizeStringSlice(ptr pointer, tagsize int) int {
  1209. s := *ptr.toStringSlice()
  1210. n := 0
  1211. for _, v := range s {
  1212. n += len(v) + SizeVarint(uint64(len(v))) + tagsize
  1213. }
  1214. return n
  1215. }
  1216. func sizeBytes(ptr pointer, tagsize int) int {
  1217. v := *ptr.toBytes()
  1218. if v == nil {
  1219. return 0
  1220. }
  1221. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1222. }
  1223. func sizeBytes3(ptr pointer, tagsize int) int {
  1224. v := *ptr.toBytes()
  1225. if len(v) == 0 {
  1226. return 0
  1227. }
  1228. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1229. }
  1230. func sizeBytesOneof(ptr pointer, tagsize int) int {
  1231. v := *ptr.toBytes()
  1232. return len(v) + SizeVarint(uint64(len(v))) + tagsize
  1233. }
  1234. func sizeBytesSlice(ptr pointer, tagsize int) int {
  1235. s := *ptr.toBytesSlice()
  1236. n := 0
  1237. for _, v := range s {
  1238. n += len(v) + SizeVarint(uint64(len(v))) + tagsize
  1239. }
  1240. return n
  1241. }
  1242. // appendFixed32 appends an encoded fixed32 to b.
  1243. func appendFixed32(b []byte, v uint32) []byte {
  1244. b = append(b,
  1245. byte(v),
  1246. byte(v>>8),
  1247. byte(v>>16),
  1248. byte(v>>24))
  1249. return b
  1250. }
  1251. // appendFixed64 appends an encoded fixed64 to b.
  1252. func appendFixed64(b []byte, v uint64) []byte {
  1253. b = append(b,
  1254. byte(v),
  1255. byte(v>>8),
  1256. byte(v>>16),
  1257. byte(v>>24),
  1258. byte(v>>32),
  1259. byte(v>>40),
  1260. byte(v>>48),
  1261. byte(v>>56))
  1262. return b
  1263. }
  1264. // appendVarint appends an encoded varint to b.
  1265. func appendVarint(b []byte, v uint64) []byte {
  1266. // TODO: make 1-byte (maybe 2-byte) case inline-able, once we
  1267. // have non-leaf inliner.
  1268. switch {
  1269. case v < 1<<7:
  1270. b = append(b, byte(v))
  1271. case v < 1<<14:
  1272. b = append(b,
  1273. byte(v&0x7f|0x80),
  1274. byte(v>>7))
  1275. case v < 1<<21:
  1276. b = append(b,
  1277. byte(v&0x7f|0x80),
  1278. byte((v>>7)&0x7f|0x80),
  1279. byte(v>>14))
  1280. case v < 1<<28:
  1281. b = append(b,
  1282. byte(v&0x7f|0x80),
  1283. byte((v>>7)&0x7f|0x80),
  1284. byte((v>>14)&0x7f|0x80),
  1285. byte(v>>21))
  1286. case v < 1<<35:
  1287. b = append(b,
  1288. byte(v&0x7f|0x80),
  1289. byte((v>>7)&0x7f|0x80),
  1290. byte((v>>14)&0x7f|0x80),
  1291. byte((v>>21)&0x7f|0x80),
  1292. byte(v>>28))
  1293. case v < 1<<42:
  1294. b = append(b,
  1295. byte(v&0x7f|0x80),
  1296. byte((v>>7)&0x7f|0x80),
  1297. byte((v>>14)&0x7f|0x80),
  1298. byte((v>>21)&0x7f|0x80),
  1299. byte((v>>28)&0x7f|0x80),
  1300. byte(v>>35))
  1301. case v < 1<<49:
  1302. b = append(b,
  1303. byte(v&0x7f|0x80),
  1304. byte((v>>7)&0x7f|0x80),
  1305. byte((v>>14)&0x7f|0x80),
  1306. byte((v>>21)&0x7f|0x80),
  1307. byte((v>>28)&0x7f|0x80),
  1308. byte((v>>35)&0x7f|0x80),
  1309. byte(v>>42))
  1310. case v < 1<<56:
  1311. b = append(b,
  1312. byte(v&0x7f|0x80),
  1313. byte((v>>7)&0x7f|0x80),
  1314. byte((v>>14)&0x7f|0x80),
  1315. byte((v>>21)&0x7f|0x80),
  1316. byte((v>>28)&0x7f|0x80),
  1317. byte((v>>35)&0x7f|0x80),
  1318. byte((v>>42)&0x7f|0x80),
  1319. byte(v>>49))
  1320. case v < 1<<63:
  1321. b = append(b,
  1322. byte(v&0x7f|0x80),
  1323. byte((v>>7)&0x7f|0x80),
  1324. byte((v>>14)&0x7f|0x80),
  1325. byte((v>>21)&0x7f|0x80),
  1326. byte((v>>28)&0x7f|0x80),
  1327. byte((v>>35)&0x7f|0x80),
  1328. byte((v>>42)&0x7f|0x80),
  1329. byte((v>>49)&0x7f|0x80),
  1330. byte(v>>56))
  1331. default:
  1332. b = append(b,
  1333. byte(v&0x7f|0x80),
  1334. byte((v>>7)&0x7f|0x80),
  1335. byte((v>>14)&0x7f|0x80),
  1336. byte((v>>21)&0x7f|0x80),
  1337. byte((v>>28)&0x7f|0x80),
  1338. byte((v>>35)&0x7f|0x80),
  1339. byte((v>>42)&0x7f|0x80),
  1340. byte((v>>49)&0x7f|0x80),
  1341. byte((v>>56)&0x7f|0x80),
  1342. 1)
  1343. }
  1344. return b
  1345. }
  1346. func appendFixed32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1347. v := *ptr.toUint32()
  1348. b = appendVarint(b, wiretag)
  1349. b = appendFixed32(b, v)
  1350. return b, nil
  1351. }
  1352. func appendFixed32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1353. v := *ptr.toUint32()
  1354. if v == 0 {
  1355. return b, nil
  1356. }
  1357. b = appendVarint(b, wiretag)
  1358. b = appendFixed32(b, v)
  1359. return b, nil
  1360. }
  1361. func appendFixed32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1362. p := *ptr.toUint32Ptr()
  1363. if p == nil {
  1364. return b, nil
  1365. }
  1366. b = appendVarint(b, wiretag)
  1367. b = appendFixed32(b, *p)
  1368. return b, nil
  1369. }
  1370. func appendFixed32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1371. s := *ptr.toUint32Slice()
  1372. for _, v := range s {
  1373. b = appendVarint(b, wiretag)
  1374. b = appendFixed32(b, v)
  1375. }
  1376. return b, nil
  1377. }
  1378. func appendFixed32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1379. s := *ptr.toUint32Slice()
  1380. if len(s) == 0 {
  1381. return b, nil
  1382. }
  1383. b = appendVarint(b, wiretag&^7|WireBytes)
  1384. b = appendVarint(b, uint64(4*len(s)))
  1385. for _, v := range s {
  1386. b = appendFixed32(b, v)
  1387. }
  1388. return b, nil
  1389. }
  1390. func appendFixedS32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1391. v := *ptr.toInt32()
  1392. b = appendVarint(b, wiretag)
  1393. b = appendFixed32(b, uint32(v))
  1394. return b, nil
  1395. }
  1396. func appendFixedS32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1397. v := *ptr.toInt32()
  1398. if v == 0 {
  1399. return b, nil
  1400. }
  1401. b = appendVarint(b, wiretag)
  1402. b = appendFixed32(b, uint32(v))
  1403. return b, nil
  1404. }
  1405. func appendFixedS32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1406. p := ptr.getInt32Ptr()
  1407. if p == nil {
  1408. return b, nil
  1409. }
  1410. b = appendVarint(b, wiretag)
  1411. b = appendFixed32(b, uint32(*p))
  1412. return b, nil
  1413. }
  1414. func appendFixedS32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1415. s := ptr.getInt32Slice()
  1416. for _, v := range s {
  1417. b = appendVarint(b, wiretag)
  1418. b = appendFixed32(b, uint32(v))
  1419. }
  1420. return b, nil
  1421. }
  1422. func appendFixedS32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1423. s := ptr.getInt32Slice()
  1424. if len(s) == 0 {
  1425. return b, nil
  1426. }
  1427. b = appendVarint(b, wiretag&^7|WireBytes)
  1428. b = appendVarint(b, uint64(4*len(s)))
  1429. for _, v := range s {
  1430. b = appendFixed32(b, uint32(v))
  1431. }
  1432. return b, nil
  1433. }
  1434. func appendFloat32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1435. v := math.Float32bits(*ptr.toFloat32())
  1436. b = appendVarint(b, wiretag)
  1437. b = appendFixed32(b, v)
  1438. return b, nil
  1439. }
  1440. func appendFloat32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1441. v := math.Float32bits(*ptr.toFloat32())
  1442. if v == 0 {
  1443. return b, nil
  1444. }
  1445. b = appendVarint(b, wiretag)
  1446. b = appendFixed32(b, v)
  1447. return b, nil
  1448. }
  1449. func appendFloat32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1450. p := *ptr.toFloat32Ptr()
  1451. if p == nil {
  1452. return b, nil
  1453. }
  1454. b = appendVarint(b, wiretag)
  1455. b = appendFixed32(b, math.Float32bits(*p))
  1456. return b, nil
  1457. }
  1458. func appendFloat32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1459. s := *ptr.toFloat32Slice()
  1460. for _, v := range s {
  1461. b = appendVarint(b, wiretag)
  1462. b = appendFixed32(b, math.Float32bits(v))
  1463. }
  1464. return b, nil
  1465. }
  1466. func appendFloat32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1467. s := *ptr.toFloat32Slice()
  1468. if len(s) == 0 {
  1469. return b, nil
  1470. }
  1471. b = appendVarint(b, wiretag&^7|WireBytes)
  1472. b = appendVarint(b, uint64(4*len(s)))
  1473. for _, v := range s {
  1474. b = appendFixed32(b, math.Float32bits(v))
  1475. }
  1476. return b, nil
  1477. }
  1478. func appendFixed64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1479. v := *ptr.toUint64()
  1480. b = appendVarint(b, wiretag)
  1481. b = appendFixed64(b, v)
  1482. return b, nil
  1483. }
  1484. func appendFixed64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1485. v := *ptr.toUint64()
  1486. if v == 0 {
  1487. return b, nil
  1488. }
  1489. b = appendVarint(b, wiretag)
  1490. b = appendFixed64(b, v)
  1491. return b, nil
  1492. }
  1493. func appendFixed64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1494. p := *ptr.toUint64Ptr()
  1495. if p == nil {
  1496. return b, nil
  1497. }
  1498. b = appendVarint(b, wiretag)
  1499. b = appendFixed64(b, *p)
  1500. return b, nil
  1501. }
  1502. func appendFixed64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1503. s := *ptr.toUint64Slice()
  1504. for _, v := range s {
  1505. b = appendVarint(b, wiretag)
  1506. b = appendFixed64(b, v)
  1507. }
  1508. return b, nil
  1509. }
  1510. func appendFixed64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1511. s := *ptr.toUint64Slice()
  1512. if len(s) == 0 {
  1513. return b, nil
  1514. }
  1515. b = appendVarint(b, wiretag&^7|WireBytes)
  1516. b = appendVarint(b, uint64(8*len(s)))
  1517. for _, v := range s {
  1518. b = appendFixed64(b, v)
  1519. }
  1520. return b, nil
  1521. }
  1522. func appendFixedS64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1523. v := *ptr.toInt64()
  1524. b = appendVarint(b, wiretag)
  1525. b = appendFixed64(b, uint64(v))
  1526. return b, nil
  1527. }
  1528. func appendFixedS64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1529. v := *ptr.toInt64()
  1530. if v == 0 {
  1531. return b, nil
  1532. }
  1533. b = appendVarint(b, wiretag)
  1534. b = appendFixed64(b, uint64(v))
  1535. return b, nil
  1536. }
  1537. func appendFixedS64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1538. p := *ptr.toInt64Ptr()
  1539. if p == nil {
  1540. return b, nil
  1541. }
  1542. b = appendVarint(b, wiretag)
  1543. b = appendFixed64(b, uint64(*p))
  1544. return b, nil
  1545. }
  1546. func appendFixedS64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1547. s := *ptr.toInt64Slice()
  1548. for _, v := range s {
  1549. b = appendVarint(b, wiretag)
  1550. b = appendFixed64(b, uint64(v))
  1551. }
  1552. return b, nil
  1553. }
  1554. func appendFixedS64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1555. s := *ptr.toInt64Slice()
  1556. if len(s) == 0 {
  1557. return b, nil
  1558. }
  1559. b = appendVarint(b, wiretag&^7|WireBytes)
  1560. b = appendVarint(b, uint64(8*len(s)))
  1561. for _, v := range s {
  1562. b = appendFixed64(b, uint64(v))
  1563. }
  1564. return b, nil
  1565. }
  1566. func appendFloat64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1567. v := math.Float64bits(*ptr.toFloat64())
  1568. b = appendVarint(b, wiretag)
  1569. b = appendFixed64(b, v)
  1570. return b, nil
  1571. }
  1572. func appendFloat64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1573. v := math.Float64bits(*ptr.toFloat64())
  1574. if v == 0 {
  1575. return b, nil
  1576. }
  1577. b = appendVarint(b, wiretag)
  1578. b = appendFixed64(b, v)
  1579. return b, nil
  1580. }
  1581. func appendFloat64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1582. p := *ptr.toFloat64Ptr()
  1583. if p == nil {
  1584. return b, nil
  1585. }
  1586. b = appendVarint(b, wiretag)
  1587. b = appendFixed64(b, math.Float64bits(*p))
  1588. return b, nil
  1589. }
  1590. func appendFloat64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1591. s := *ptr.toFloat64Slice()
  1592. for _, v := range s {
  1593. b = appendVarint(b, wiretag)
  1594. b = appendFixed64(b, math.Float64bits(v))
  1595. }
  1596. return b, nil
  1597. }
  1598. func appendFloat64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1599. s := *ptr.toFloat64Slice()
  1600. if len(s) == 0 {
  1601. return b, nil
  1602. }
  1603. b = appendVarint(b, wiretag&^7|WireBytes)
  1604. b = appendVarint(b, uint64(8*len(s)))
  1605. for _, v := range s {
  1606. b = appendFixed64(b, math.Float64bits(v))
  1607. }
  1608. return b, nil
  1609. }
  1610. func appendVarint32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1611. v := *ptr.toUint32()
  1612. b = appendVarint(b, wiretag)
  1613. b = appendVarint(b, uint64(v))
  1614. return b, nil
  1615. }
  1616. func appendVarint32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1617. v := *ptr.toUint32()
  1618. if v == 0 {
  1619. return b, nil
  1620. }
  1621. b = appendVarint(b, wiretag)
  1622. b = appendVarint(b, uint64(v))
  1623. return b, nil
  1624. }
  1625. func appendVarint32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1626. p := *ptr.toUint32Ptr()
  1627. if p == nil {
  1628. return b, nil
  1629. }
  1630. b = appendVarint(b, wiretag)
  1631. b = appendVarint(b, uint64(*p))
  1632. return b, nil
  1633. }
  1634. func appendVarint32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1635. s := *ptr.toUint32Slice()
  1636. for _, v := range s {
  1637. b = appendVarint(b, wiretag)
  1638. b = appendVarint(b, uint64(v))
  1639. }
  1640. return b, nil
  1641. }
  1642. func appendVarint32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1643. s := *ptr.toUint32Slice()
  1644. if len(s) == 0 {
  1645. return b, nil
  1646. }
  1647. b = appendVarint(b, wiretag&^7|WireBytes)
  1648. // compute size
  1649. n := 0
  1650. for _, v := range s {
  1651. n += SizeVarint(uint64(v))
  1652. }
  1653. b = appendVarint(b, uint64(n))
  1654. for _, v := range s {
  1655. b = appendVarint(b, uint64(v))
  1656. }
  1657. return b, nil
  1658. }
  1659. func appendVarintS32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1660. v := *ptr.toInt32()
  1661. b = appendVarint(b, wiretag)
  1662. b = appendVarint(b, uint64(v))
  1663. return b, nil
  1664. }
  1665. func appendVarintS32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1666. v := *ptr.toInt32()
  1667. if v == 0 {
  1668. return b, nil
  1669. }
  1670. b = appendVarint(b, wiretag)
  1671. b = appendVarint(b, uint64(v))
  1672. return b, nil
  1673. }
  1674. func appendVarintS32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1675. p := ptr.getInt32Ptr()
  1676. if p == nil {
  1677. return b, nil
  1678. }
  1679. b = appendVarint(b, wiretag)
  1680. b = appendVarint(b, uint64(*p))
  1681. return b, nil
  1682. }
  1683. func appendVarintS32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1684. s := ptr.getInt32Slice()
  1685. for _, v := range s {
  1686. b = appendVarint(b, wiretag)
  1687. b = appendVarint(b, uint64(v))
  1688. }
  1689. return b, nil
  1690. }
  1691. func appendVarintS32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1692. s := ptr.getInt32Slice()
  1693. if len(s) == 0 {
  1694. return b, nil
  1695. }
  1696. b = appendVarint(b, wiretag&^7|WireBytes)
  1697. // compute size
  1698. n := 0
  1699. for _, v := range s {
  1700. n += SizeVarint(uint64(v))
  1701. }
  1702. b = appendVarint(b, uint64(n))
  1703. for _, v := range s {
  1704. b = appendVarint(b, uint64(v))
  1705. }
  1706. return b, nil
  1707. }
  1708. func appendVarint64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1709. v := *ptr.toUint64()
  1710. b = appendVarint(b, wiretag)
  1711. b = appendVarint(b, v)
  1712. return b, nil
  1713. }
  1714. func appendVarint64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1715. v := *ptr.toUint64()
  1716. if v == 0 {
  1717. return b, nil
  1718. }
  1719. b = appendVarint(b, wiretag)
  1720. b = appendVarint(b, v)
  1721. return b, nil
  1722. }
  1723. func appendVarint64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1724. p := *ptr.toUint64Ptr()
  1725. if p == nil {
  1726. return b, nil
  1727. }
  1728. b = appendVarint(b, wiretag)
  1729. b = appendVarint(b, *p)
  1730. return b, nil
  1731. }
  1732. func appendVarint64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1733. s := *ptr.toUint64Slice()
  1734. for _, v := range s {
  1735. b = appendVarint(b, wiretag)
  1736. b = appendVarint(b, v)
  1737. }
  1738. return b, nil
  1739. }
  1740. func appendVarint64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1741. s := *ptr.toUint64Slice()
  1742. if len(s) == 0 {
  1743. return b, nil
  1744. }
  1745. b = appendVarint(b, wiretag&^7|WireBytes)
  1746. // compute size
  1747. n := 0
  1748. for _, v := range s {
  1749. n += SizeVarint(v)
  1750. }
  1751. b = appendVarint(b, uint64(n))
  1752. for _, v := range s {
  1753. b = appendVarint(b, v)
  1754. }
  1755. return b, nil
  1756. }
  1757. func appendVarintS64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1758. v := *ptr.toInt64()
  1759. b = appendVarint(b, wiretag)
  1760. b = appendVarint(b, uint64(v))
  1761. return b, nil
  1762. }
  1763. func appendVarintS64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1764. v := *ptr.toInt64()
  1765. if v == 0 {
  1766. return b, nil
  1767. }
  1768. b = appendVarint(b, wiretag)
  1769. b = appendVarint(b, uint64(v))
  1770. return b, nil
  1771. }
  1772. func appendVarintS64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1773. p := *ptr.toInt64Ptr()
  1774. if p == nil {
  1775. return b, nil
  1776. }
  1777. b = appendVarint(b, wiretag)
  1778. b = appendVarint(b, uint64(*p))
  1779. return b, nil
  1780. }
  1781. func appendVarintS64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1782. s := *ptr.toInt64Slice()
  1783. for _, v := range s {
  1784. b = appendVarint(b, wiretag)
  1785. b = appendVarint(b, uint64(v))
  1786. }
  1787. return b, nil
  1788. }
  1789. func appendVarintS64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1790. s := *ptr.toInt64Slice()
  1791. if len(s) == 0 {
  1792. return b, nil
  1793. }
  1794. b = appendVarint(b, wiretag&^7|WireBytes)
  1795. // compute size
  1796. n := 0
  1797. for _, v := range s {
  1798. n += SizeVarint(uint64(v))
  1799. }
  1800. b = appendVarint(b, uint64(n))
  1801. for _, v := range s {
  1802. b = appendVarint(b, uint64(v))
  1803. }
  1804. return b, nil
  1805. }
  1806. func appendZigzag32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1807. v := *ptr.toInt32()
  1808. b = appendVarint(b, wiretag)
  1809. b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
  1810. return b, nil
  1811. }
  1812. func appendZigzag32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1813. v := *ptr.toInt32()
  1814. if v == 0 {
  1815. return b, nil
  1816. }
  1817. b = appendVarint(b, wiretag)
  1818. b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
  1819. return b, nil
  1820. }
  1821. func appendZigzag32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1822. p := ptr.getInt32Ptr()
  1823. if p == nil {
  1824. return b, nil
  1825. }
  1826. b = appendVarint(b, wiretag)
  1827. v := *p
  1828. b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
  1829. return b, nil
  1830. }
  1831. func appendZigzag32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1832. s := ptr.getInt32Slice()
  1833. for _, v := range s {
  1834. b = appendVarint(b, wiretag)
  1835. b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
  1836. }
  1837. return b, nil
  1838. }
  1839. func appendZigzag32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1840. s := ptr.getInt32Slice()
  1841. if len(s) == 0 {
  1842. return b, nil
  1843. }
  1844. b = appendVarint(b, wiretag&^7|WireBytes)
  1845. // compute size
  1846. n := 0
  1847. for _, v := range s {
  1848. n += SizeVarint(uint64((uint32(v) << 1) ^ uint32((int32(v) >> 31))))
  1849. }
  1850. b = appendVarint(b, uint64(n))
  1851. for _, v := range s {
  1852. b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
  1853. }
  1854. return b, nil
  1855. }
  1856. func appendZigzag64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1857. v := *ptr.toInt64()
  1858. b = appendVarint(b, wiretag)
  1859. b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
  1860. return b, nil
  1861. }
  1862. func appendZigzag64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1863. v := *ptr.toInt64()
  1864. if v == 0 {
  1865. return b, nil
  1866. }
  1867. b = appendVarint(b, wiretag)
  1868. b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
  1869. return b, nil
  1870. }
  1871. func appendZigzag64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1872. p := *ptr.toInt64Ptr()
  1873. if p == nil {
  1874. return b, nil
  1875. }
  1876. b = appendVarint(b, wiretag)
  1877. v := *p
  1878. b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
  1879. return b, nil
  1880. }
  1881. func appendZigzag64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1882. s := *ptr.toInt64Slice()
  1883. for _, v := range s {
  1884. b = appendVarint(b, wiretag)
  1885. b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
  1886. }
  1887. return b, nil
  1888. }
  1889. func appendZigzag64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1890. s := *ptr.toInt64Slice()
  1891. if len(s) == 0 {
  1892. return b, nil
  1893. }
  1894. b = appendVarint(b, wiretag&^7|WireBytes)
  1895. // compute size
  1896. n := 0
  1897. for _, v := range s {
  1898. n += SizeVarint(uint64(v<<1) ^ uint64((int64(v) >> 63)))
  1899. }
  1900. b = appendVarint(b, uint64(n))
  1901. for _, v := range s {
  1902. b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
  1903. }
  1904. return b, nil
  1905. }
  1906. func appendBoolValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1907. v := *ptr.toBool()
  1908. b = appendVarint(b, wiretag)
  1909. if v {
  1910. b = append(b, 1)
  1911. } else {
  1912. b = append(b, 0)
  1913. }
  1914. return b, nil
  1915. }
  1916. func appendBoolValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1917. v := *ptr.toBool()
  1918. if !v {
  1919. return b, nil
  1920. }
  1921. b = appendVarint(b, wiretag)
  1922. b = append(b, 1)
  1923. return b, nil
  1924. }
  1925. func appendBoolPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1926. p := *ptr.toBoolPtr()
  1927. if p == nil {
  1928. return b, nil
  1929. }
  1930. b = appendVarint(b, wiretag)
  1931. if *p {
  1932. b = append(b, 1)
  1933. } else {
  1934. b = append(b, 0)
  1935. }
  1936. return b, nil
  1937. }
  1938. func appendBoolSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1939. s := *ptr.toBoolSlice()
  1940. for _, v := range s {
  1941. b = appendVarint(b, wiretag)
  1942. if v {
  1943. b = append(b, 1)
  1944. } else {
  1945. b = append(b, 0)
  1946. }
  1947. }
  1948. return b, nil
  1949. }
  1950. func appendBoolPackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1951. s := *ptr.toBoolSlice()
  1952. if len(s) == 0 {
  1953. return b, nil
  1954. }
  1955. b = appendVarint(b, wiretag&^7|WireBytes)
  1956. b = appendVarint(b, uint64(len(s)))
  1957. for _, v := range s {
  1958. if v {
  1959. b = append(b, 1)
  1960. } else {
  1961. b = append(b, 0)
  1962. }
  1963. }
  1964. return b, nil
  1965. }
  1966. func appendStringValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1967. v := *ptr.toString()
  1968. b = appendVarint(b, wiretag)
  1969. b = appendVarint(b, uint64(len(v)))
  1970. b = append(b, v...)
  1971. return b, nil
  1972. }
  1973. func appendStringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1974. v := *ptr.toString()
  1975. if v == "" {
  1976. return b, nil
  1977. }
  1978. b = appendVarint(b, wiretag)
  1979. b = appendVarint(b, uint64(len(v)))
  1980. b = append(b, v...)
  1981. return b, nil
  1982. }
  1983. func appendStringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1984. p := *ptr.toStringPtr()
  1985. if p == nil {
  1986. return b, nil
  1987. }
  1988. v := *p
  1989. b = appendVarint(b, wiretag)
  1990. b = appendVarint(b, uint64(len(v)))
  1991. b = append(b, v...)
  1992. return b, nil
  1993. }
  1994. func appendStringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  1995. s := *ptr.toStringSlice()
  1996. for _, v := range s {
  1997. b = appendVarint(b, wiretag)
  1998. b = appendVarint(b, uint64(len(v)))
  1999. b = append(b, v...)
  2000. }
  2001. return b, nil
  2002. }
  2003. func appendUTF8StringValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2004. var invalidUTF8 bool
  2005. v := *ptr.toString()
  2006. if !utf8.ValidString(v) {
  2007. invalidUTF8 = true
  2008. }
  2009. b = appendVarint(b, wiretag)
  2010. b = appendVarint(b, uint64(len(v)))
  2011. b = append(b, v...)
  2012. if invalidUTF8 {
  2013. return b, errInvalidUTF8
  2014. }
  2015. return b, nil
  2016. }
  2017. func appendUTF8StringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2018. var invalidUTF8 bool
  2019. v := *ptr.toString()
  2020. if v == "" {
  2021. return b, nil
  2022. }
  2023. if !utf8.ValidString(v) {
  2024. invalidUTF8 = true
  2025. }
  2026. b = appendVarint(b, wiretag)
  2027. b = appendVarint(b, uint64(len(v)))
  2028. b = append(b, v...)
  2029. if invalidUTF8 {
  2030. return b, errInvalidUTF8
  2031. }
  2032. return b, nil
  2033. }
  2034. func appendUTF8StringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2035. var invalidUTF8 bool
  2036. p := *ptr.toStringPtr()
  2037. if p == nil {
  2038. return b, nil
  2039. }
  2040. v := *p
  2041. if !utf8.ValidString(v) {
  2042. invalidUTF8 = true
  2043. }
  2044. b = appendVarint(b, wiretag)
  2045. b = appendVarint(b, uint64(len(v)))
  2046. b = append(b, v...)
  2047. if invalidUTF8 {
  2048. return b, errInvalidUTF8
  2049. }
  2050. return b, nil
  2051. }
  2052. func appendUTF8StringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2053. var invalidUTF8 bool
  2054. s := *ptr.toStringSlice()
  2055. for _, v := range s {
  2056. if !utf8.ValidString(v) {
  2057. invalidUTF8 = true
  2058. }
  2059. b = appendVarint(b, wiretag)
  2060. b = appendVarint(b, uint64(len(v)))
  2061. b = append(b, v...)
  2062. }
  2063. if invalidUTF8 {
  2064. return b, errInvalidUTF8
  2065. }
  2066. return b, nil
  2067. }
  2068. func appendBytes(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2069. v := *ptr.toBytes()
  2070. if v == nil {
  2071. return b, nil
  2072. }
  2073. b = appendVarint(b, wiretag)
  2074. b = appendVarint(b, uint64(len(v)))
  2075. b = append(b, v...)
  2076. return b, nil
  2077. }
  2078. func appendBytes3(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2079. v := *ptr.toBytes()
  2080. if len(v) == 0 {
  2081. return b, nil
  2082. }
  2083. b = appendVarint(b, wiretag)
  2084. b = appendVarint(b, uint64(len(v)))
  2085. b = append(b, v...)
  2086. return b, nil
  2087. }
  2088. func appendBytesOneof(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2089. v := *ptr.toBytes()
  2090. b = appendVarint(b, wiretag)
  2091. b = appendVarint(b, uint64(len(v)))
  2092. b = append(b, v...)
  2093. return b, nil
  2094. }
  2095. func appendBytesSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
  2096. s := *ptr.toBytesSlice()
  2097. for _, v := range s {
  2098. b = appendVarint(b, wiretag)
  2099. b = appendVarint(b, uint64(len(v)))
  2100. b = append(b, v...)
  2101. }
  2102. return b, nil
  2103. }
  2104. // makeGroupMarshaler returns the sizer and marshaler for a group.
  2105. // u is the marshal info of the underlying message.
  2106. func makeGroupMarshaler(u *marshalInfo) (sizer, marshaler) {
  2107. return func(ptr pointer, tagsize int) int {
  2108. p := ptr.getPointer()
  2109. if p.isNil() {
  2110. return 0
  2111. }
  2112. return u.size(p) + 2*tagsize
  2113. },
  2114. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  2115. p := ptr.getPointer()
  2116. if p.isNil() {
  2117. return b, nil
  2118. }
  2119. var err error
  2120. b = appendVarint(b, wiretag) // start group
  2121. b, err = u.marshal(b, p, deterministic)
  2122. b = appendVarint(b, wiretag+(WireEndGroup-WireStartGroup)) // end group
  2123. return b, err
  2124. }
  2125. }
  2126. // makeGroupSliceMarshaler returns the sizer and marshaler for a group slice.
  2127. // u is the marshal info of the underlying message.
  2128. func makeGroupSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
  2129. return func(ptr pointer, tagsize int) int {
  2130. s := ptr.getPointerSlice()
  2131. n := 0
  2132. for _, v := range s {
  2133. if v.isNil() {
  2134. continue
  2135. }
  2136. n += u.size(v) + 2*tagsize
  2137. }
  2138. return n
  2139. },
  2140. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  2141. s := ptr.getPointerSlice()
  2142. var err error
  2143. var nerr nonFatal
  2144. for _, v := range s {
  2145. if v.isNil() {
  2146. return b, errRepeatedHasNil
  2147. }
  2148. b = appendVarint(b, wiretag) // start group
  2149. b, err = u.marshal(b, v, deterministic)
  2150. b = appendVarint(b, wiretag+(WireEndGroup-WireStartGroup)) // end group
  2151. if !nerr.Merge(err) {
  2152. if err == ErrNil {
  2153. err = errRepeatedHasNil
  2154. }
  2155. return b, err
  2156. }
  2157. }
  2158. return b, nerr.E
  2159. }
  2160. }
  2161. // makeMessageMarshaler returns the sizer and marshaler for a message field.
  2162. // u is the marshal info of the message.
  2163. func makeMessageMarshaler(u *marshalInfo) (sizer, marshaler) {
  2164. return func(ptr pointer, tagsize int) int {
  2165. p := ptr.getPointer()
  2166. if p.isNil() {
  2167. return 0
  2168. }
  2169. siz := u.size(p)
  2170. return siz + SizeVarint(uint64(siz)) + tagsize
  2171. },
  2172. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  2173. p := ptr.getPointer()
  2174. if p.isNil() {
  2175. return b, nil
  2176. }
  2177. b = appendVarint(b, wiretag)
  2178. siz := u.cachedsize(p)
  2179. b = appendVarint(b, uint64(siz))
  2180. return u.marshal(b, p, deterministic)
  2181. }
  2182. }
  2183. // makeMessageSliceMarshaler returns the sizer and marshaler for a message slice.
  2184. // u is the marshal info of the message.
  2185. func makeMessageSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
  2186. return func(ptr pointer, tagsize int) int {
  2187. s := ptr.getPointerSlice()
  2188. n := 0
  2189. for _, v := range s {
  2190. if v.isNil() {
  2191. continue
  2192. }
  2193. siz := u.size(v)
  2194. n += siz + SizeVarint(uint64(siz)) + tagsize
  2195. }
  2196. return n
  2197. },
  2198. func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
  2199. s := ptr.getPointerSlice()
  2200. var err error
  2201. var nerr nonFatal
  2202. for _, v := range s {
  2203. if v.isNil() {
  2204. return b, errRepeatedHasNil
  2205. }
  2206. b = appendVarint(b, wiretag)
  2207. siz := u.cachedsize(v)
  2208. b = appendVarint(b, uint64(siz))
  2209. b, err = u.marshal(b, v, deterministic)
  2210. if !nerr.Merge(err) {
  2211. if err == ErrNil {
  2212. err = errRepeatedHasNil
  2213. }
  2214. return b, err
  2215. }
  2216. }
  2217. return b, nerr.E
  2218. }
  2219. }
  2220. // makeMapMarshaler returns the sizer and marshaler for a map field.
  2221. // f is the pointer to the reflect data structure of the field.
  2222. func makeMapMarshaler(f *reflect.StructField) (sizer, marshaler) {
  2223. // figure out key and value type
  2224. t := f.Type
  2225. keyType := t.Key()
  2226. valType := t.Elem()
  2227. keyTags := strings.Split(f.Tag.Get("protobuf_key"), ",")
  2228. valTags := strings.Split(f.Tag.Get("protobuf_val"), ",")
  2229. keySizer, keyMarshaler := typeMarshaler(keyType, keyTags, false, false) // don't omit zero value in map
  2230. valSizer, valMarshaler := typeMarshaler(valType, valTags, false, false) // don't omit zero value in map
  2231. keyWireTag := 1<<3 | wiretype(keyTags[0])
  2232. valWireTag := 2<<3 | wiretype(valTags[0])
  2233. // We create an interface to get the addresses of the map key and value.
  2234. // If value is pointer-typed, the interface is a direct interface, the
  2235. // idata itself is the value. Otherwise, the idata is the pointer to the
  2236. // value.
  2237. // Key cannot be pointer-typed.
  2238. valIsPtr := valType.Kind() == reflect.Ptr
  2239. // If value is a message with nested maps, calling
  2240. // valSizer in marshal may be quadratic. We should use
  2241. // cached version in marshal (but not in size).
  2242. // If value is not message type, we don't have size cache,
  2243. // but it cannot be nested either. Just use valSizer.
  2244. valCachedSizer := valSizer
  2245. if valIsPtr && valType.Elem().Kind() == reflect.Struct {
  2246. u := getMarshalInfo(valType.Elem())
  2247. valCachedSizer = func(ptr pointer, tagsize int) int {
  2248. // Same as message sizer, but use cache.
  2249. p := ptr.getPointer()
  2250. if p.isNil() {
  2251. return 0
  2252. }
  2253. siz := u.cachedsize(p)
  2254. return siz + SizeVarint(uint64(siz)) + tagsize
  2255. }
  2256. }
  2257. return func(ptr pointer, tagsize int) int {
  2258. m := ptr.asPointerTo(t).Elem() // the map
  2259. n := 0
  2260. for _, k := range m.MapKeys() {
  2261. ki := k.Interface()
  2262. vi := m.MapIndex(k).Interface()
  2263. kaddr := toAddrPointer(&ki, false, false) // pointer to key
  2264. vaddr := toAddrPointer(&vi, valIsPtr, false) // pointer to value
  2265. siz := keySizer(kaddr, 1) + valSizer(vaddr, 1) // tag of key = 1 (size=1), tag of val = 2 (size=1)
  2266. n += siz + SizeVarint(uint64(siz)) + tagsize
  2267. }
  2268. return n
  2269. },
  2270. func(b []byte, ptr pointer, tag uint64, deterministic bool) ([]byte, error) {
  2271. m := ptr.asPointerTo(t).Elem() // the map
  2272. var err error
  2273. keys := m.MapKeys()
  2274. if len(keys) > 1 && deterministic {
  2275. sort.Sort(mapKeys(keys))
  2276. }
  2277. var nerr nonFatal
  2278. for _, k := range keys {
  2279. ki := k.Interface()
  2280. vi := m.MapIndex(k).Interface()
  2281. kaddr := toAddrPointer(&ki, false, false) // pointer to key
  2282. vaddr := toAddrPointer(&vi, valIsPtr, false) // pointer to value
  2283. b = appendVarint(b, tag)
  2284. siz := keySizer(kaddr, 1) + valCachedSizer(vaddr, 1) // tag of key = 1 (size=1), tag of val = 2 (size=1)
  2285. b = appendVarint(b, uint64(siz))
  2286. b, err = keyMarshaler(b, kaddr, keyWireTag, deterministic)
  2287. if !nerr.Merge(err) {
  2288. return b, err
  2289. }
  2290. b, err = valMarshaler(b, vaddr, valWireTag, deterministic)
  2291. if err != ErrNil && !nerr.Merge(err) { // allow nil value in map
  2292. return b, err
  2293. }
  2294. }
  2295. return b, nerr.E
  2296. }
  2297. }
  2298. // makeOneOfMarshaler returns the sizer and marshaler for a oneof field.
  2299. // fi is the marshal info of the field.
  2300. // f is the pointer to the reflect data structure of the field.
  2301. func makeOneOfMarshaler(fi *marshalFieldInfo, f *reflect.StructField) (sizer, marshaler) {
  2302. // Oneof field is an interface. We need to get the actual data type on the fly.
  2303. t := f.Type
  2304. return func(ptr pointer, _ int) int {
  2305. p := ptr.getInterfacePointer()
  2306. if p.isNil() {
  2307. return 0
  2308. }
  2309. v := ptr.asPointerTo(t).Elem().Elem().Elem() // *interface -> interface -> *struct -> struct
  2310. telem := v.Type()
  2311. e := fi.oneofElems[telem]
  2312. return e.sizer(p, e.tagsize)
  2313. },
  2314. func(b []byte, ptr pointer, _ uint64, deterministic bool) ([]byte, error) {
  2315. p := ptr.getInterfacePointer()
  2316. if p.isNil() {
  2317. return b, nil
  2318. }
  2319. v := ptr.asPointerTo(t).Elem().Elem().Elem() // *interface -> interface -> *struct -> struct
  2320. telem := v.Type()
  2321. if telem.Field(0).Type.Kind() == reflect.Ptr && p.getPointer().isNil() {
  2322. return b, errOneofHasNil
  2323. }
  2324. e := fi.oneofElems[telem]
  2325. return e.marshaler(b, p, e.wiretag, deterministic)
  2326. }
  2327. }
  2328. // sizeExtensions computes the size of encoded data for a XXX_InternalExtensions field.
  2329. func (u *marshalInfo) sizeExtensions(ext *XXX_InternalExtensions) int {
  2330. m, mu := ext.extensionsRead()
  2331. if m == nil {
  2332. return 0
  2333. }
  2334. mu.Lock()
  2335. n := 0
  2336. for _, e := range m {
  2337. if e.value == nil || e.desc == nil {
  2338. // Extension is only in its encoded form.
  2339. n += len(e.enc)
  2340. continue
  2341. }
  2342. // We don't skip extensions that have an encoded form set,
  2343. // because the extension value may have been mutated after
  2344. // the last time this function was called.
  2345. ei := u.getExtElemInfo(e.desc)
  2346. v := e.value
  2347. p := toAddrPointer(&v, ei.isptr, ei.deref)
  2348. n += ei.sizer(p, ei.tagsize)
  2349. }
  2350. mu.Unlock()
  2351. return n
  2352. }
  2353. // appendExtensions marshals a XXX_InternalExtensions field to the end of byte slice b.
  2354. func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, deterministic bool) ([]byte, error) {
  2355. m, mu := ext.extensionsRead()
  2356. if m == nil {
  2357. return b, nil
  2358. }
  2359. mu.Lock()
  2360. defer mu.Unlock()
  2361. var err error
  2362. var nerr nonFatal
  2363. // Fast-path for common cases: zero or one extensions.
  2364. // Don't bother sorting the keys.
  2365. if len(m) <= 1 {
  2366. for _, e := range m {
  2367. if e.value == nil || e.desc == nil {
  2368. // Extension is only in its encoded form.
  2369. b = append(b, e.enc...)
  2370. continue
  2371. }
  2372. // We don't skip extensions that have an encoded form set,
  2373. // because the extension value may have been mutated after
  2374. // the last time this function was called.
  2375. ei := u.getExtElemInfo(e.desc)
  2376. v := e.value
  2377. p := toAddrPointer(&v, ei.isptr, ei.deref)
  2378. b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
  2379. if !nerr.Merge(err) {
  2380. return b, err
  2381. }
  2382. }
  2383. return b, nerr.E
  2384. }
  2385. // Sort the keys to provide a deterministic encoding.
  2386. // Not sure this is required, but the old code does it.
  2387. keys := make([]int, 0, len(m))
  2388. for k := range m {
  2389. keys = append(keys, int(k))
  2390. }
  2391. sort.Ints(keys)
  2392. for _, k := range keys {
  2393. e := m[int32(k)]
  2394. if e.value == nil || e.desc == nil {
  2395. // Extension is only in its encoded form.
  2396. b = append(b, e.enc...)
  2397. continue
  2398. }
  2399. // We don't skip extensions that have an encoded form set,
  2400. // because the extension value may have been mutated after
  2401. // the last time this function was called.
  2402. ei := u.getExtElemInfo(e.desc)
  2403. v := e.value
  2404. p := toAddrPointer(&v, ei.isptr, ei.deref)
  2405. b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
  2406. if !nerr.Merge(err) {
  2407. return b, err
  2408. }
  2409. }
  2410. return b, nerr.E
  2411. }
  2412. // message set format is:
  2413. // message MessageSet {
  2414. // repeated group Item = 1 {
  2415. // required int32 type_id = 2;
  2416. // required string message = 3;
  2417. // };
  2418. // }
  2419. // sizeMessageSet computes the size of encoded data for a XXX_InternalExtensions field
  2420. // in message set format (above).
  2421. func (u *marshalInfo) sizeMessageSet(ext *XXX_InternalExtensions) int {
  2422. m, mu := ext.extensionsRead()
  2423. if m == nil {
  2424. return 0
  2425. }
  2426. mu.Lock()
  2427. n := 0
  2428. for id, e := range m {
  2429. n += 2 // start group, end group. tag = 1 (size=1)
  2430. n += SizeVarint(uint64(id)) + 1 // type_id, tag = 2 (size=1)
  2431. if e.value == nil || e.desc == nil {
  2432. // Extension is only in its encoded form.
  2433. msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
  2434. siz := len(msgWithLen)
  2435. n += siz + 1 // message, tag = 3 (size=1)
  2436. continue
  2437. }
  2438. // We don't skip extensions that have an encoded form set,
  2439. // because the extension value may have been mutated after
  2440. // the last time this function was called.
  2441. ei := u.getExtElemInfo(e.desc)
  2442. v := e.value
  2443. p := toAddrPointer(&v, ei.isptr, ei.deref)
  2444. n += ei.sizer(p, 1) // message, tag = 3 (size=1)
  2445. }
  2446. mu.Unlock()
  2447. return n
  2448. }
  2449. // appendMessageSet marshals a XXX_InternalExtensions field in message set format (above)
  2450. // to the end of byte slice b.
  2451. func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, deterministic bool) ([]byte, error) {
  2452. m, mu := ext.extensionsRead()
  2453. if m == nil {
  2454. return b, nil
  2455. }
  2456. mu.Lock()
  2457. defer mu.Unlock()
  2458. var err error
  2459. var nerr nonFatal
  2460. // Fast-path for common cases: zero or one extensions.
  2461. // Don't bother sorting the keys.
  2462. if len(m) <= 1 {
  2463. for id, e := range m {
  2464. b = append(b, 1<<3|WireStartGroup)
  2465. b = append(b, 2<<3|WireVarint)
  2466. b = appendVarint(b, uint64(id))
  2467. if e.value == nil || e.desc == nil {
  2468. // Extension is only in its encoded form.
  2469. msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
  2470. b = append(b, 3<<3|WireBytes)
  2471. b = append(b, msgWithLen...)
  2472. b = append(b, 1<<3|WireEndGroup)
  2473. continue
  2474. }
  2475. // We don't skip extensions that have an encoded form set,
  2476. // because the extension value may have been mutated after
  2477. // the last time this function was called.
  2478. ei := u.getExtElemInfo(e.desc)
  2479. v := e.value
  2480. p := toAddrPointer(&v, ei.isptr, ei.deref)
  2481. b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic)
  2482. if !nerr.Merge(err) {
  2483. return b, err
  2484. }
  2485. b = append(b, 1<<3|WireEndGroup)
  2486. }
  2487. return b, nerr.E
  2488. }
  2489. // Sort the keys to provide a deterministic encoding.
  2490. keys := make([]int, 0, len(m))
  2491. for k := range m {
  2492. keys = append(keys, int(k))
  2493. }
  2494. sort.Ints(keys)
  2495. for _, id := range keys {
  2496. e := m[int32(id)]
  2497. b = append(b, 1<<3|WireStartGroup)
  2498. b = append(b, 2<<3|WireVarint)
  2499. b = appendVarint(b, uint64(id))
  2500. if e.value == nil || e.desc == nil {
  2501. // Extension is only in its encoded form.
  2502. msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
  2503. b = append(b, 3<<3|WireBytes)
  2504. b = append(b, msgWithLen...)
  2505. b = append(b, 1<<3|WireEndGroup)
  2506. continue
  2507. }
  2508. // We don't skip extensions that have an encoded form set,
  2509. // because the extension value may have been mutated after
  2510. // the last time this function was called.
  2511. ei := u.getExtElemInfo(e.desc)
  2512. v := e.value
  2513. p := toAddrPointer(&v, ei.isptr, ei.deref)
  2514. b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic)
  2515. b = append(b, 1<<3|WireEndGroup)
  2516. if !nerr.Merge(err) {
  2517. return b, err
  2518. }
  2519. }
  2520. return b, nerr.E
  2521. }
  2522. // sizeV1Extensions computes the size of encoded data for a V1-API extension field.
  2523. func (u *marshalInfo) sizeV1Extensions(m map[int32]Extension) int {
  2524. if m == nil {
  2525. return 0
  2526. }
  2527. n := 0
  2528. for _, e := range m {
  2529. if e.value == nil || e.desc == nil {
  2530. // Extension is only in its encoded form.
  2531. n += len(e.enc)
  2532. continue
  2533. }
  2534. // We don't skip extensions that have an encoded form set,
  2535. // because the extension value may have been mutated after
  2536. // the last time this function was called.
  2537. ei := u.getExtElemInfo(e.desc)
  2538. v := e.value
  2539. p := toAddrPointer(&v, ei.isptr, ei.deref)
  2540. n += ei.sizer(p, ei.tagsize)
  2541. }
  2542. return n
  2543. }
  2544. // appendV1Extensions marshals a V1-API extension field to the end of byte slice b.
  2545. func (u *marshalInfo) appendV1Extensions(b []byte, m map[int32]Extension, deterministic bool) ([]byte, error) {
  2546. if m == nil {
  2547. return b, nil
  2548. }
  2549. // Sort the keys to provide a deterministic encoding.
  2550. keys := make([]int, 0, len(m))
  2551. for k := range m {
  2552. keys = append(keys, int(k))
  2553. }
  2554. sort.Ints(keys)
  2555. var err error
  2556. var nerr nonFatal
  2557. for _, k := range keys {
  2558. e := m[int32(k)]
  2559. if e.value == nil || e.desc == nil {
  2560. // Extension is only in its encoded form.
  2561. b = append(b, e.enc...)
  2562. continue
  2563. }
  2564. // We don't skip extensions that have an encoded form set,
  2565. // because the extension value may have been mutated after
  2566. // the last time this function was called.
  2567. ei := u.getExtElemInfo(e.desc)
  2568. v := e.value
  2569. p := toAddrPointer(&v, ei.isptr, ei.deref)
  2570. b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
  2571. if !nerr.Merge(err) {
  2572. return b, err
  2573. }
  2574. }
  2575. return b, nerr.E
  2576. }
  2577. // newMarshaler is the interface representing objects that can marshal themselves.
  2578. //
  2579. // This exists to support protoc-gen-go generated messages.
  2580. // The proto package will stop type-asserting to this interface in the future.
  2581. //
  2582. // DO NOT DEPEND ON THIS.
  2583. type newMarshaler interface {
  2584. XXX_Size() int
  2585. XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
  2586. }
  2587. // Size returns the encoded size of a protocol buffer message.
  2588. // This is the main entry point.
  2589. func Size(pb Message) int {
  2590. if m, ok := pb.(newMarshaler); ok {
  2591. return m.XXX_Size()
  2592. }
  2593. if m, ok := pb.(Marshaler); ok {
  2594. // If the message can marshal itself, let it do it, for compatibility.
  2595. // NOTE: This is not efficient.
  2596. b, _ := m.Marshal()
  2597. return len(b)
  2598. }
  2599. // in case somehow we didn't generate the wrapper
  2600. if pb == nil {
  2601. return 0
  2602. }
  2603. var info InternalMessageInfo
  2604. return info.Size(pb)
  2605. }
  2606. // Marshal takes a protocol buffer message
  2607. // and encodes it into the wire format, returning the data.
  2608. // This is the main entry point.
  2609. func Marshal(pb Message) ([]byte, error) {
  2610. if m, ok := pb.(newMarshaler); ok {
  2611. siz := m.XXX_Size()
  2612. b := make([]byte, 0, siz)
  2613. return m.XXX_Marshal(b, false)
  2614. }
  2615. if m, ok := pb.(Marshaler); ok {
  2616. // If the message can marshal itself, let it do it, for compatibility.
  2617. // NOTE: This is not efficient.
  2618. return m.Marshal()
  2619. }
  2620. // in case somehow we didn't generate the wrapper
  2621. if pb == nil {
  2622. return nil, ErrNil
  2623. }
  2624. var info InternalMessageInfo
  2625. siz := info.Size(pb)
  2626. b := make([]byte, 0, siz)
  2627. return info.Marshal(b, pb, false)
  2628. }
  2629. // Marshal takes a protocol buffer message
  2630. // and encodes it into the wire format, writing the result to the
  2631. // Buffer.
  2632. // This is an alternative entry point. It is not necessary to use
  2633. // a Buffer for most applications.
  2634. func (p *Buffer) Marshal(pb Message) error {
  2635. var err error
  2636. if m, ok := pb.(newMarshaler); ok {
  2637. siz := m.XXX_Size()
  2638. p.grow(siz) // make sure buf has enough capacity
  2639. p.buf, err = m.XXX_Marshal(p.buf, p.deterministic)
  2640. return err
  2641. }
  2642. if m, ok := pb.(Marshaler); ok {
  2643. // If the message can marshal itself, let it do it, for compatibility.
  2644. // NOTE: This is not efficient.
  2645. b, err := m.Marshal()
  2646. p.buf = append(p.buf, b...)
  2647. return err
  2648. }
  2649. // in case somehow we didn't generate the wrapper
  2650. if pb == nil {
  2651. return ErrNil
  2652. }
  2653. var info InternalMessageInfo
  2654. siz := info.Size(pb)
  2655. p.grow(siz) // make sure buf has enough capacity
  2656. p.buf, err = info.Marshal(p.buf, pb, p.deterministic)
  2657. return err
  2658. }
  2659. // grow grows the buffer's capacity, if necessary, to guarantee space for
  2660. // another n bytes. After grow(n), at least n bytes can be written to the
  2661. // buffer without another allocation.
  2662. func (p *Buffer) grow(n int) {
  2663. need := len(p.buf) + n
  2664. if need <= cap(p.buf) {
  2665. return
  2666. }
  2667. newCap := len(p.buf) * 2
  2668. if newCap < need {
  2669. newCap = need
  2670. }
  2671. p.buf = append(make([]byte, 0, newCap), p.buf...)
  2672. }