table_marshal.go 73 KB

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