table_marshal.go 73 KB

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