encode.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // http://code.google.com/p/goprotobuf/
  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. /*
  33. * Routines for encoding data into the wire format for protocol buffers.
  34. */
  35. import (
  36. "errors"
  37. "fmt"
  38. "reflect"
  39. "sort"
  40. )
  41. // RequiredNotSetError is the error returned if Marshal is called with
  42. // a protocol buffer struct whose required fields have not
  43. // all been initialized. It is also the error returned if Unmarshal is
  44. // called with an encoded protocol buffer that does not include all the
  45. // required fields.
  46. //
  47. // When printed, RequiredNotSetError reports the first unset required field in a
  48. // message. If the field cannot be precisely determined, it is reported as
  49. // "{Unknown}".
  50. type RequiredNotSetError struct {
  51. field string
  52. }
  53. func (e *RequiredNotSetError) Error() string {
  54. return fmt.Sprintf("proto: required field %q not set", e.field)
  55. }
  56. var (
  57. // ErrRepeatedHasNil is the error returned if Marshal is called with
  58. // a struct with a repeated field containing a nil element.
  59. ErrRepeatedHasNil = errors.New("proto: repeated field has nil element")
  60. // ErrNil is the error returned if Marshal is called with nil.
  61. ErrNil = errors.New("proto: Marshal called with nil")
  62. )
  63. // The fundamental encoders that put bytes on the wire.
  64. // Those that take integer types all accept uint64 and are
  65. // therefore of type valueEncoder.
  66. const maxVarintBytes = 10 // maximum length of a varint
  67. // EncodeVarint returns the varint encoding of x.
  68. // This is the format for the
  69. // int32, int64, uint32, uint64, bool, and enum
  70. // protocol buffer types.
  71. // Not used by the package itself, but helpful to clients
  72. // wishing to use the same encoding.
  73. func EncodeVarint(x uint64) []byte {
  74. var buf [maxVarintBytes]byte
  75. var n int
  76. for n = 0; x > 127; n++ {
  77. buf[n] = 0x80 | uint8(x&0x7F)
  78. x >>= 7
  79. }
  80. buf[n] = uint8(x)
  81. n++
  82. return buf[0:n]
  83. }
  84. // EncodeVarint writes a varint-encoded integer to the Buffer.
  85. // This is the format for the
  86. // int32, int64, uint32, uint64, bool, and enum
  87. // protocol buffer types.
  88. func (p *Buffer) EncodeVarint(x uint64) error {
  89. for x >= 1<<7 {
  90. p.buf = append(p.buf, uint8(x&0x7f|0x80))
  91. x >>= 7
  92. }
  93. p.buf = append(p.buf, uint8(x))
  94. return nil
  95. }
  96. func sizeVarint(x uint64) (n int) {
  97. for {
  98. n++
  99. x >>= 7
  100. if x == 0 {
  101. break
  102. }
  103. }
  104. return n
  105. }
  106. // EncodeFixed64 writes a 64-bit integer to the Buffer.
  107. // This is the format for the
  108. // fixed64, sfixed64, and double protocol buffer types.
  109. func (p *Buffer) EncodeFixed64(x uint64) error {
  110. p.buf = append(p.buf,
  111. uint8(x),
  112. uint8(x>>8),
  113. uint8(x>>16),
  114. uint8(x>>24),
  115. uint8(x>>32),
  116. uint8(x>>40),
  117. uint8(x>>48),
  118. uint8(x>>56))
  119. return nil
  120. }
  121. func sizeFixed64(x uint64) int {
  122. return 8
  123. }
  124. // EncodeFixed32 writes a 32-bit integer to the Buffer.
  125. // This is the format for the
  126. // fixed32, sfixed32, and float protocol buffer types.
  127. func (p *Buffer) EncodeFixed32(x uint64) error {
  128. p.buf = append(p.buf,
  129. uint8(x),
  130. uint8(x>>8),
  131. uint8(x>>16),
  132. uint8(x>>24))
  133. return nil
  134. }
  135. func sizeFixed32(x uint64) int {
  136. return 4
  137. }
  138. // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
  139. // to the Buffer.
  140. // This is the format used for the sint64 protocol buffer type.
  141. func (p *Buffer) EncodeZigzag64(x uint64) error {
  142. // use signed number to get arithmetic right shift.
  143. return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  144. }
  145. func sizeZigzag64(x uint64) int {
  146. return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  147. }
  148. // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
  149. // to the Buffer.
  150. // This is the format used for the sint32 protocol buffer type.
  151. func (p *Buffer) EncodeZigzag32(x uint64) error {
  152. // use signed number to get arithmetic right shift.
  153. return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  154. }
  155. func sizeZigzag32(x uint64) int {
  156. return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  157. }
  158. // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
  159. // This is the format used for the bytes protocol buffer
  160. // type and for embedded messages.
  161. func (p *Buffer) EncodeRawBytes(b []byte) error {
  162. p.EncodeVarint(uint64(len(b)))
  163. p.buf = append(p.buf, b...)
  164. return nil
  165. }
  166. func sizeRawBytes(b []byte) int {
  167. return sizeVarint(uint64(len(b))) +
  168. len(b)
  169. }
  170. // EncodeStringBytes writes an encoded string to the Buffer.
  171. // This is the format used for the proto2 string type.
  172. func (p *Buffer) EncodeStringBytes(s string) error {
  173. p.EncodeVarint(uint64(len(s)))
  174. p.buf = append(p.buf, s...)
  175. return nil
  176. }
  177. func sizeStringBytes(s string) int {
  178. return sizeVarint(uint64(len(s))) +
  179. len(s)
  180. }
  181. // Marshaler is the interface representing objects that can marshal themselves.
  182. type Marshaler interface {
  183. Marshal() ([]byte, error)
  184. }
  185. // Marshal takes the protocol buffer
  186. // and encodes it into the wire format, returning the data.
  187. func Marshal(pb Message) ([]byte, error) {
  188. // Can the object marshal itself?
  189. if m, ok := pb.(Marshaler); ok {
  190. return m.Marshal()
  191. }
  192. p := NewBuffer(nil)
  193. err := p.Marshal(pb)
  194. var state errorState
  195. if err != nil && !state.shouldContinue(err, nil) {
  196. return nil, err
  197. }
  198. if p.buf == nil && err == nil {
  199. // Return a non-nil slice on success.
  200. return []byte{}, nil
  201. }
  202. return p.buf, err
  203. }
  204. // Marshal takes the protocol buffer
  205. // and encodes it into the wire format, writing the result to the
  206. // Buffer.
  207. func (p *Buffer) Marshal(pb Message) error {
  208. // Can the object marshal itself?
  209. if m, ok := pb.(Marshaler); ok {
  210. data, err := m.Marshal()
  211. if err != nil {
  212. return err
  213. }
  214. p.buf = append(p.buf, data...)
  215. return nil
  216. }
  217. t, base, err := getbase(pb)
  218. if structPointer_IsNil(base) {
  219. return ErrNil
  220. }
  221. if err == nil {
  222. err = p.enc_struct(t.Elem(), GetProperties(t.Elem()), base)
  223. }
  224. if collectStats {
  225. stats.Encode++
  226. }
  227. return err
  228. }
  229. // Size returns the encoded size of a protocol buffer.
  230. func Size(pb Message) (n int) {
  231. // Can the object marshal itself? If so, Size is slow.
  232. // TODO: add Size to Marshaler, or add a Sizer interface.
  233. if m, ok := pb.(Marshaler); ok {
  234. b, _ := m.Marshal()
  235. return len(b)
  236. }
  237. t, base, err := getbase(pb)
  238. if structPointer_IsNil(base) {
  239. return 0
  240. }
  241. if err == nil {
  242. n = size_struct(t.Elem(), GetProperties(t.Elem()), base)
  243. }
  244. if collectStats {
  245. stats.Size++
  246. }
  247. return
  248. }
  249. // Individual type encoders.
  250. // Encode a bool.
  251. func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
  252. v := *structPointer_Bool(base, p.field)
  253. if v == nil {
  254. return ErrNil
  255. }
  256. x := 0
  257. if *v {
  258. x = 1
  259. }
  260. o.buf = append(o.buf, p.tagcode...)
  261. p.valEnc(o, uint64(x))
  262. return nil
  263. }
  264. func size_bool(p *Properties, base structPointer) int {
  265. v := *structPointer_Bool(base, p.field)
  266. if v == nil {
  267. return 0
  268. }
  269. return len(p.tagcode) + 1 // each bool takes exactly one byte
  270. }
  271. // Encode an int32.
  272. func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
  273. v := structPointer_Word32(base, p.field)
  274. if word32_IsNil(v) {
  275. return ErrNil
  276. }
  277. x := word32_Get(v)
  278. o.buf = append(o.buf, p.tagcode...)
  279. p.valEnc(o, uint64(x))
  280. return nil
  281. }
  282. func size_int32(p *Properties, base structPointer) (n int) {
  283. v := structPointer_Word32(base, p.field)
  284. if word32_IsNil(v) {
  285. return 0
  286. }
  287. x := word32_Get(v)
  288. n += len(p.tagcode)
  289. n += p.valSize(uint64(x))
  290. return
  291. }
  292. // Encode an int64.
  293. func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
  294. v := structPointer_Word64(base, p.field)
  295. if word64_IsNil(v) {
  296. return ErrNil
  297. }
  298. x := word64_Get(v)
  299. o.buf = append(o.buf, p.tagcode...)
  300. p.valEnc(o, x)
  301. return nil
  302. }
  303. func size_int64(p *Properties, base structPointer) (n int) {
  304. v := structPointer_Word64(base, p.field)
  305. if word64_IsNil(v) {
  306. return 0
  307. }
  308. x := word64_Get(v)
  309. n += len(p.tagcode)
  310. n += p.valSize(x)
  311. return
  312. }
  313. // Encode a string.
  314. func (o *Buffer) enc_string(p *Properties, base structPointer) error {
  315. v := *structPointer_String(base, p.field)
  316. if v == nil {
  317. return ErrNil
  318. }
  319. x := *v
  320. o.buf = append(o.buf, p.tagcode...)
  321. o.EncodeStringBytes(x)
  322. return nil
  323. }
  324. func size_string(p *Properties, base structPointer) (n int) {
  325. v := *structPointer_String(base, p.field)
  326. if v == nil {
  327. return 0
  328. }
  329. x := *v
  330. n += len(p.tagcode)
  331. n += sizeStringBytes(x)
  332. return
  333. }
  334. // All protocol buffer fields are nillable, but be careful.
  335. func isNil(v reflect.Value) bool {
  336. switch v.Kind() {
  337. case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  338. return v.IsNil()
  339. }
  340. return false
  341. }
  342. // Encode a message struct.
  343. func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
  344. var state errorState
  345. structp := structPointer_GetStructPointer(base, p.field)
  346. if structPointer_IsNil(structp) {
  347. return ErrNil
  348. }
  349. // Can the object marshal itself?
  350. if p.isMarshaler {
  351. m := structPointer_Interface(structp, p.stype).(Marshaler)
  352. data, err := m.Marshal()
  353. if err != nil && !state.shouldContinue(err, nil) {
  354. return err
  355. }
  356. o.buf = append(o.buf, p.tagcode...)
  357. o.EncodeRawBytes(data)
  358. return nil
  359. }
  360. o.buf = append(o.buf, p.tagcode...)
  361. return o.enc_len_struct(p.stype, p.sprop, structp, &state)
  362. }
  363. func size_struct_message(p *Properties, base structPointer) int {
  364. structp := structPointer_GetStructPointer(base, p.field)
  365. if structPointer_IsNil(structp) {
  366. return 0
  367. }
  368. // Can the object marshal itself?
  369. if p.isMarshaler {
  370. m := structPointer_Interface(structp, p.stype).(Marshaler)
  371. data, _ := m.Marshal()
  372. n0 := len(p.tagcode)
  373. n1 := sizeRawBytes(data)
  374. return n0 + n1
  375. }
  376. n0 := len(p.tagcode)
  377. n1 := size_struct(p.stype, p.sprop, structp)
  378. n2 := sizeVarint(uint64(n1)) // size of encoded length
  379. return n0 + n1 + n2
  380. }
  381. // Encode a group struct.
  382. func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
  383. var state errorState
  384. b := structPointer_GetStructPointer(base, p.field)
  385. if structPointer_IsNil(b) {
  386. return ErrNil
  387. }
  388. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  389. err := o.enc_struct(p.stype, p.sprop, b)
  390. if err != nil && !state.shouldContinue(err, nil) {
  391. return err
  392. }
  393. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  394. return state.err
  395. }
  396. func size_struct_group(p *Properties, base structPointer) (n int) {
  397. b := structPointer_GetStructPointer(base, p.field)
  398. if structPointer_IsNil(b) {
  399. return 0
  400. }
  401. n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup))
  402. n += size_struct(p.stype, p.sprop, b)
  403. n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
  404. return
  405. }
  406. // Encode a slice of bools ([]bool).
  407. func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
  408. s := *structPointer_BoolSlice(base, p.field)
  409. l := len(s)
  410. if l == 0 {
  411. return ErrNil
  412. }
  413. for _, x := range s {
  414. o.buf = append(o.buf, p.tagcode...)
  415. v := uint64(0)
  416. if x {
  417. v = 1
  418. }
  419. p.valEnc(o, v)
  420. }
  421. return nil
  422. }
  423. func size_slice_bool(p *Properties, base structPointer) int {
  424. s := *structPointer_BoolSlice(base, p.field)
  425. l := len(s)
  426. if l == 0 {
  427. return 0
  428. }
  429. return l * (len(p.tagcode) + 1) // each bool takes exactly one byte
  430. }
  431. // Encode a slice of bools ([]bool) in packed format.
  432. func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
  433. s := *structPointer_BoolSlice(base, p.field)
  434. l := len(s)
  435. if l == 0 {
  436. return ErrNil
  437. }
  438. o.buf = append(o.buf, p.tagcode...)
  439. o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
  440. for _, x := range s {
  441. v := uint64(0)
  442. if x {
  443. v = 1
  444. }
  445. p.valEnc(o, v)
  446. }
  447. return nil
  448. }
  449. func size_slice_packed_bool(p *Properties, base structPointer) (n int) {
  450. s := *structPointer_BoolSlice(base, p.field)
  451. l := len(s)
  452. if l == 0 {
  453. return 0
  454. }
  455. n += len(p.tagcode)
  456. n += sizeVarint(uint64(l))
  457. n += l // each bool takes exactly one byte
  458. return
  459. }
  460. // Encode a slice of bytes ([]byte).
  461. func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
  462. s := *structPointer_Bytes(base, p.field)
  463. if s == nil {
  464. return ErrNil
  465. }
  466. o.buf = append(o.buf, p.tagcode...)
  467. o.EncodeRawBytes(s)
  468. return nil
  469. }
  470. func size_slice_byte(p *Properties, base structPointer) (n int) {
  471. s := *structPointer_Bytes(base, p.field)
  472. if s == nil {
  473. return 0
  474. }
  475. n += len(p.tagcode)
  476. n += sizeRawBytes(s)
  477. return
  478. }
  479. // Encode a slice of int32s ([]int32).
  480. func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
  481. s := structPointer_Word32Slice(base, p.field)
  482. l := s.Len()
  483. if l == 0 {
  484. return ErrNil
  485. }
  486. for i := 0; i < l; i++ {
  487. o.buf = append(o.buf, p.tagcode...)
  488. x := s.Index(i)
  489. p.valEnc(o, uint64(x))
  490. }
  491. return nil
  492. }
  493. func size_slice_int32(p *Properties, base structPointer) (n int) {
  494. s := structPointer_Word32Slice(base, p.field)
  495. l := s.Len()
  496. if l == 0 {
  497. return 0
  498. }
  499. for i := 0; i < l; i++ {
  500. n += len(p.tagcode)
  501. x := s.Index(i)
  502. n += p.valSize(uint64(x))
  503. }
  504. return
  505. }
  506. // Encode a slice of int32s ([]int32) in packed format.
  507. func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
  508. s := structPointer_Word32Slice(base, p.field)
  509. l := s.Len()
  510. if l == 0 {
  511. return ErrNil
  512. }
  513. // TODO: Reuse a Buffer.
  514. buf := NewBuffer(nil)
  515. for i := 0; i < l; i++ {
  516. p.valEnc(buf, uint64(s.Index(i)))
  517. }
  518. o.buf = append(o.buf, p.tagcode...)
  519. o.EncodeVarint(uint64(len(buf.buf)))
  520. o.buf = append(o.buf, buf.buf...)
  521. return nil
  522. }
  523. func size_slice_packed_int32(p *Properties, base structPointer) (n int) {
  524. s := structPointer_Word32Slice(base, p.field)
  525. l := s.Len()
  526. if l == 0 {
  527. return 0
  528. }
  529. var bufSize int
  530. for i := 0; i < l; i++ {
  531. bufSize += p.valSize(uint64(s.Index(i)))
  532. }
  533. n += len(p.tagcode)
  534. n += sizeVarint(uint64(bufSize))
  535. n += bufSize
  536. return
  537. }
  538. // Encode a slice of int64s ([]int64).
  539. func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
  540. s := structPointer_Word64Slice(base, p.field)
  541. l := s.Len()
  542. if l == 0 {
  543. return ErrNil
  544. }
  545. for i := 0; i < l; i++ {
  546. o.buf = append(o.buf, p.tagcode...)
  547. p.valEnc(o, s.Index(i))
  548. }
  549. return nil
  550. }
  551. func size_slice_int64(p *Properties, base structPointer) (n int) {
  552. s := structPointer_Word64Slice(base, p.field)
  553. l := s.Len()
  554. if l == 0 {
  555. return 0
  556. }
  557. for i := 0; i < l; i++ {
  558. n += len(p.tagcode)
  559. n += p.valSize(s.Index(i))
  560. }
  561. return
  562. }
  563. // Encode a slice of int64s ([]int64) in packed format.
  564. func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
  565. s := structPointer_Word64Slice(base, p.field)
  566. l := s.Len()
  567. if l == 0 {
  568. return ErrNil
  569. }
  570. // TODO: Reuse a Buffer.
  571. buf := NewBuffer(nil)
  572. for i := 0; i < l; i++ {
  573. p.valEnc(buf, s.Index(i))
  574. }
  575. o.buf = append(o.buf, p.tagcode...)
  576. o.EncodeVarint(uint64(len(buf.buf)))
  577. o.buf = append(o.buf, buf.buf...)
  578. return nil
  579. }
  580. func size_slice_packed_int64(p *Properties, base structPointer) (n int) {
  581. s := structPointer_Word64Slice(base, p.field)
  582. l := s.Len()
  583. if l == 0 {
  584. return 0
  585. }
  586. var bufSize int
  587. for i := 0; i < l; i++ {
  588. bufSize += p.valSize(s.Index(i))
  589. }
  590. n += len(p.tagcode)
  591. n += sizeVarint(uint64(bufSize))
  592. n += bufSize
  593. return
  594. }
  595. // Encode a slice of slice of bytes ([][]byte).
  596. func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
  597. ss := *structPointer_BytesSlice(base, p.field)
  598. l := len(ss)
  599. if l == 0 {
  600. return ErrNil
  601. }
  602. for i := 0; i < l; i++ {
  603. o.buf = append(o.buf, p.tagcode...)
  604. o.EncodeRawBytes(ss[i])
  605. }
  606. return nil
  607. }
  608. func size_slice_slice_byte(p *Properties, base structPointer) (n int) {
  609. ss := *structPointer_BytesSlice(base, p.field)
  610. l := len(ss)
  611. if l == 0 {
  612. return 0
  613. }
  614. n += l * len(p.tagcode)
  615. for i := 0; i < l; i++ {
  616. n += sizeRawBytes(ss[i])
  617. }
  618. return
  619. }
  620. // Encode a slice of strings ([]string).
  621. func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
  622. ss := *structPointer_StringSlice(base, p.field)
  623. l := len(ss)
  624. for i := 0; i < l; i++ {
  625. o.buf = append(o.buf, p.tagcode...)
  626. o.EncodeStringBytes(ss[i])
  627. }
  628. return nil
  629. }
  630. func size_slice_string(p *Properties, base structPointer) (n int) {
  631. ss := *structPointer_StringSlice(base, p.field)
  632. l := len(ss)
  633. n += l * len(p.tagcode)
  634. for i := 0; i < l; i++ {
  635. n += sizeStringBytes(ss[i])
  636. }
  637. return
  638. }
  639. // Encode a slice of message structs ([]*struct).
  640. func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
  641. var state errorState
  642. s := structPointer_StructPointerSlice(base, p.field)
  643. l := s.Len()
  644. for i := 0; i < l; i++ {
  645. structp := s.Index(i)
  646. if structPointer_IsNil(structp) {
  647. return ErrRepeatedHasNil
  648. }
  649. // Can the object marshal itself?
  650. if p.isMarshaler {
  651. m := structPointer_Interface(structp, p.stype).(Marshaler)
  652. data, err := m.Marshal()
  653. if err != nil && !state.shouldContinue(err, nil) {
  654. return err
  655. }
  656. o.buf = append(o.buf, p.tagcode...)
  657. o.EncodeRawBytes(data)
  658. continue
  659. }
  660. o.buf = append(o.buf, p.tagcode...)
  661. err := o.enc_len_struct(p.stype, p.sprop, structp, &state)
  662. if err != nil && !state.shouldContinue(err, nil) {
  663. if err == ErrNil {
  664. return ErrRepeatedHasNil
  665. }
  666. return err
  667. }
  668. }
  669. return state.err
  670. }
  671. func size_slice_struct_message(p *Properties, base structPointer) (n int) {
  672. s := structPointer_StructPointerSlice(base, p.field)
  673. l := s.Len()
  674. n += l * len(p.tagcode)
  675. for i := 0; i < l; i++ {
  676. structp := s.Index(i)
  677. if structPointer_IsNil(structp) {
  678. return // return the size up to this point
  679. }
  680. // Can the object marshal itself?
  681. if p.isMarshaler {
  682. m := structPointer_Interface(structp, p.stype).(Marshaler)
  683. data, _ := m.Marshal()
  684. n += len(p.tagcode)
  685. n += sizeRawBytes(data)
  686. continue
  687. }
  688. n0 := size_struct(p.stype, p.sprop, structp)
  689. n1 := sizeVarint(uint64(n0)) // size of encoded length
  690. n += n0 + n1
  691. }
  692. return
  693. }
  694. // Encode a slice of group structs ([]*struct).
  695. func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
  696. var state errorState
  697. s := structPointer_StructPointerSlice(base, p.field)
  698. l := s.Len()
  699. for i := 0; i < l; i++ {
  700. b := s.Index(i)
  701. if structPointer_IsNil(b) {
  702. return ErrRepeatedHasNil
  703. }
  704. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  705. err := o.enc_struct(p.stype, p.sprop, b)
  706. if err != nil && !state.shouldContinue(err, nil) {
  707. if err == ErrNil {
  708. return ErrRepeatedHasNil
  709. }
  710. return err
  711. }
  712. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  713. }
  714. return state.err
  715. }
  716. func size_slice_struct_group(p *Properties, base structPointer) (n int) {
  717. s := structPointer_StructPointerSlice(base, p.field)
  718. l := s.Len()
  719. n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup))
  720. n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup))
  721. for i := 0; i < l; i++ {
  722. b := s.Index(i)
  723. if structPointer_IsNil(b) {
  724. return // return size up to this point
  725. }
  726. n += size_struct(p.stype, p.sprop, b)
  727. }
  728. return
  729. }
  730. // Encode an extension map.
  731. func (o *Buffer) enc_map(p *Properties, base structPointer) error {
  732. v := *structPointer_ExtMap(base, p.field)
  733. if err := encodeExtensionMap(v); err != nil {
  734. return err
  735. }
  736. // Fast-path for common cases: zero or one extensions.
  737. if len(v) <= 1 {
  738. for _, e := range v {
  739. o.buf = append(o.buf, e.enc...)
  740. }
  741. return nil
  742. }
  743. // Sort keys to provide a deterministic encoding.
  744. keys := make([]int, 0, len(v))
  745. for k := range v {
  746. keys = append(keys, int(k))
  747. }
  748. sort.Ints(keys)
  749. for _, k := range keys {
  750. o.buf = append(o.buf, v[int32(k)].enc...)
  751. }
  752. return nil
  753. }
  754. func size_map(p *Properties, base structPointer) int {
  755. v := *structPointer_ExtMap(base, p.field)
  756. return sizeExtensionMap(v)
  757. }
  758. // Encode a struct.
  759. func (o *Buffer) enc_struct(t reflect.Type, prop *StructProperties, base structPointer) error {
  760. var state errorState
  761. // Encode fields in tag order so that decoders may use optimizations
  762. // that depend on the ordering.
  763. // http://code.google.com/apis/protocolbuffers/docs/encoding.html#order
  764. for _, i := range prop.order {
  765. p := prop.Prop[i]
  766. if p.enc != nil {
  767. err := p.enc(o, p, base)
  768. if err != nil {
  769. if err == ErrNil {
  770. if p.Required && state.err == nil {
  771. state.err = &RequiredNotSetError{p.Name}
  772. }
  773. } else if !state.shouldContinue(err, p) {
  774. return err
  775. }
  776. }
  777. }
  778. }
  779. // Add unrecognized fields at the end.
  780. if prop.unrecField.IsValid() {
  781. v := *structPointer_Bytes(base, prop.unrecField)
  782. if len(v) > 0 {
  783. o.buf = append(o.buf, v...)
  784. }
  785. }
  786. return state.err
  787. }
  788. func size_struct(t reflect.Type, prop *StructProperties, base structPointer) (n int) {
  789. for _, i := range prop.order {
  790. p := prop.Prop[i]
  791. if p.size != nil {
  792. n += p.size(p, base)
  793. }
  794. }
  795. // Add unrecognized fields at the end.
  796. if prop.unrecField.IsValid() {
  797. v := *structPointer_Bytes(base, prop.unrecField)
  798. n += len(v)
  799. }
  800. return
  801. }
  802. var zeroes [20]byte // longer than any conceivable sizeVarint
  803. // Encode a struct, preceded by its encoded length (as a varint).
  804. func (o *Buffer) enc_len_struct(t reflect.Type, prop *StructProperties, base structPointer, state *errorState) error {
  805. iLen := len(o.buf)
  806. o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
  807. iMsg := len(o.buf)
  808. err := o.enc_struct(t, prop, base)
  809. if err != nil && !state.shouldContinue(err, nil) {
  810. return err
  811. }
  812. lMsg := len(o.buf) - iMsg
  813. lLen := sizeVarint(uint64(lMsg))
  814. switch x := lLen - (iMsg - iLen); {
  815. case x > 0: // actual length is x bytes larger than the space we reserved
  816. // Move msg x bytes right.
  817. o.buf = append(o.buf, zeroes[:x]...)
  818. copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
  819. case x < 0: // actual length is x bytes smaller than the space we reserved
  820. // Move msg x bytes left.
  821. copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
  822. o.buf = o.buf[:len(o.buf)+x] // x is negative
  823. }
  824. // Encode the length in the reserved space.
  825. o.buf = o.buf[:iLen]
  826. o.EncodeVarint(uint64(lMsg))
  827. o.buf = o.buf[:len(o.buf)+lMsg]
  828. return state.err
  829. }
  830. // errorState maintains the first error that occurs and updates that error
  831. // with additional context.
  832. type errorState struct {
  833. err error
  834. }
  835. // shouldContinue reports whether encoding should continue upon encountering the
  836. // given error. If the error is RequiredNotSetError, shouldContinue returns true
  837. // and, if this is the first appearance of that error, remembers it for future
  838. // reporting.
  839. //
  840. // If prop is not nil, it may update any error with additional context about the
  841. // field with the error.
  842. func (s *errorState) shouldContinue(err error, prop *Properties) bool {
  843. // Ignore unset required fields.
  844. reqNotSet, ok := err.(*RequiredNotSetError)
  845. if !ok {
  846. return false
  847. }
  848. if s.err == nil {
  849. if prop != nil {
  850. err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
  851. }
  852. s.err = err
  853. }
  854. return true
  855. }