encode.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 Google Inc. 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. "os"
  37. "reflect"
  38. "runtime"
  39. "unsafe"
  40. )
  41. // ErrRequiredNotSet 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. type ErrRequiredNotSet struct {
  47. t reflect.Type
  48. }
  49. func (e *ErrRequiredNotSet) String() string {
  50. return "proto: required fields not set in " + e.t.String()
  51. }
  52. var (
  53. // ErrRepeatedHasNil is the error returned if Marshal is called with
  54. // a struct with a repeated field containing a nil element.
  55. ErrRepeatedHasNil = os.NewError("proto: repeated field has nil")
  56. // ErrNil is the error returned if Marshal is called with nil.
  57. ErrNil = os.NewError("proto: Marshal called with nil")
  58. // ErrNotPtr is the error returned if Marshal is called with a non-pointer.
  59. ErrNotPtr = os.NewError("proto: Marshal called with a non-pointer")
  60. )
  61. // The fundamental encoders that put bytes on the wire.
  62. // Those that take integer types all accept uint64 and are
  63. // therefore of type valueEncoder.
  64. const maxVarintBytes = 10 // maximum length of a varint
  65. // EncodeVarint returns the varint encoding of x.
  66. // This is the format for the
  67. // int32, int64, uint32, uint64, bool, and enum
  68. // protocol buffer types.
  69. // Not used by the package itself, but helpful to clients
  70. // wishing to use the same encoding.
  71. func EncodeVarint(x uint64) []byte {
  72. var buf [maxVarintBytes]byte
  73. var n int
  74. for n = 0; x > 127; n++ {
  75. buf[n] = 0x80 | uint8(x&0x7F)
  76. x >>= 7
  77. }
  78. buf[n] = uint8(x)
  79. n++
  80. return buf[0:n]
  81. }
  82. var emptyBytes [maxVarintBytes]byte
  83. // EncodeVarint writes a varint-encoded integer to the Buffer.
  84. // This is the format for the
  85. // int32, int64, uint32, uint64, bool, and enum
  86. // protocol buffer types.
  87. func (p *Buffer) EncodeVarint(x uint64) os.Error {
  88. l := len(p.buf)
  89. p.buf = append(p.buf, emptyBytes[:]...)
  90. for x >= 1<<7 {
  91. p.buf[l] = uint8(x&0x7f | 0x80)
  92. l++
  93. x >>= 7
  94. }
  95. p.buf[l] = uint8(x)
  96. p.buf = p.buf[:l+1]
  97. return nil
  98. }
  99. // EncodeFixed64 writes a 64-bit integer to the Buffer.
  100. // This is the format for the
  101. // fixed64, sfixed64, and double protocol buffer types.
  102. func (p *Buffer) EncodeFixed64(x uint64) os.Error {
  103. const fixed64Bytes = 8
  104. l := len(p.buf)
  105. p.buf = append(p.buf, emptyBytes[:fixed64Bytes]...)
  106. p.buf[l] = uint8(x)
  107. p.buf[l+1] = uint8(x >> 8)
  108. p.buf[l+2] = uint8(x >> 16)
  109. p.buf[l+3] = uint8(x >> 24)
  110. p.buf[l+4] = uint8(x >> 32)
  111. p.buf[l+5] = uint8(x >> 40)
  112. p.buf[l+6] = uint8(x >> 48)
  113. p.buf[l+7] = uint8(x >> 56)
  114. return nil
  115. }
  116. // EncodeFixed32 writes a 32-bit integer to the Buffer.
  117. // This is the format for the
  118. // fixed32, sfixed32, and float protocol buffer types.
  119. func (p *Buffer) EncodeFixed32(x uint64) os.Error {
  120. const fixed32Bytes = 4
  121. l := len(p.buf)
  122. p.buf = append(p.buf, emptyBytes[:fixed32Bytes]...)
  123. p.buf[l] = uint8(x)
  124. p.buf[l+1] = uint8(x >> 8)
  125. p.buf[l+2] = uint8(x >> 16)
  126. p.buf[l+3] = uint8(x >> 24)
  127. return nil
  128. }
  129. // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
  130. // to the Buffer.
  131. // This is the format used for the sint64 protocol buffer type.
  132. func (p *Buffer) EncodeZigzag64(x uint64) os.Error {
  133. // use signed number to get arithmetic right shift.
  134. return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  135. }
  136. // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
  137. // to the Buffer.
  138. // This is the format used for the sint32 protocol buffer type.
  139. func (p *Buffer) EncodeZigzag32(x uint64) os.Error {
  140. // use signed number to get arithmetic right shift.
  141. return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  142. }
  143. // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
  144. // This is the format used for the bytes protocol buffer
  145. // type and for embedded messages.
  146. func (p *Buffer) EncodeRawBytes(b []byte) os.Error {
  147. lb := len(b)
  148. p.EncodeVarint(uint64(lb))
  149. p.buf = append(p.buf, b...)
  150. return nil
  151. }
  152. // EncodeStringBytes writes an encoded string to the Buffer.
  153. // This is the format used for the proto2 string type.
  154. func (p *Buffer) EncodeStringBytes(s string) os.Error {
  155. // this works because strings and slices are the same.
  156. y := *(*[]byte)(unsafe.Pointer(&s))
  157. p.EncodeRawBytes(y)
  158. return nil
  159. }
  160. // Marshaler is the interface representing objects that can marshal themselves.
  161. type Marshaler interface {
  162. Marshal() ([]byte, os.Error)
  163. }
  164. // Marshal takes the protocol buffer struct represented by pb
  165. // and encodes it into the wire format, returning the data.
  166. func Marshal(pb interface{}) ([]byte, os.Error) {
  167. // Can the object marshal itself?
  168. if m, ok := pb.(Marshaler); ok {
  169. return m.Marshal()
  170. }
  171. p := NewBuffer(nil)
  172. err := p.Marshal(pb)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return p.buf, err
  177. }
  178. // Marshal takes the protocol buffer struct represented by pb
  179. // and encodes it into the wire format, writing the result to the
  180. // Buffer.
  181. func (p *Buffer) Marshal(pb interface{}) os.Error {
  182. // Can the object marshal itself?
  183. if m, ok := pb.(Marshaler); ok {
  184. data, err := m.Marshal()
  185. if err != nil {
  186. return err
  187. }
  188. p.buf = append(p.buf, data...)
  189. return nil
  190. }
  191. mstat := runtime.MemStats.Mallocs
  192. t, b, err := getbase(pb)
  193. if t.Kind() != reflect.Ptr {
  194. return ErrNotPtr
  195. }
  196. if err == nil {
  197. err = p.enc_struct(t.Elem(), b)
  198. }
  199. mstat = runtime.MemStats.Mallocs - mstat
  200. stats.Emalloc += mstat
  201. stats.Encode++
  202. return err
  203. }
  204. // Individual type encoders.
  205. // Encode a bool.
  206. func (o *Buffer) enc_bool(p *Properties, base uintptr) os.Error {
  207. v := *(**uint8)(unsafe.Pointer(base + p.offset))
  208. if v == nil {
  209. return ErrNil
  210. }
  211. x := *v
  212. if x != 0 {
  213. x = 1
  214. }
  215. o.buf = append(o.buf, p.tagcode...)
  216. p.valEnc(o, uint64(x))
  217. return nil
  218. }
  219. // Encode an int32.
  220. func (o *Buffer) enc_int32(p *Properties, base uintptr) os.Error {
  221. v := *(**uint32)(unsafe.Pointer(base + p.offset))
  222. if v == nil {
  223. return ErrNil
  224. }
  225. x := *v
  226. o.buf = append(o.buf, p.tagcode...)
  227. p.valEnc(o, uint64(x))
  228. return nil
  229. }
  230. // Encode an int64.
  231. func (o *Buffer) enc_int64(p *Properties, base uintptr) os.Error {
  232. v := *(**uint64)(unsafe.Pointer(base + p.offset))
  233. if v == nil {
  234. return ErrNil
  235. }
  236. x := *v
  237. o.buf = append(o.buf, p.tagcode...)
  238. p.valEnc(o, uint64(x))
  239. return nil
  240. }
  241. // Encode a string.
  242. func (o *Buffer) enc_string(p *Properties, base uintptr) os.Error {
  243. v := *(**string)(unsafe.Pointer(base + p.offset))
  244. if v == nil {
  245. return ErrNil
  246. }
  247. x := *v
  248. o.buf = append(o.buf, p.tagcode...)
  249. o.EncodeStringBytes(x)
  250. return nil
  251. }
  252. // All protocol buffer fields are nillable, but be careful.
  253. func isNil(v reflect.Value) bool {
  254. switch v.Kind() {
  255. case reflect.Map, reflect.Ptr, reflect.Slice:
  256. return v.IsNil()
  257. }
  258. return false
  259. }
  260. // Encode a message struct.
  261. func (o *Buffer) enc_struct_message(p *Properties, base uintptr) os.Error {
  262. // Can the object marshal itself?
  263. iv := unsafe.Unreflect(p.stype, unsafe.Pointer(base+p.offset))
  264. if m, ok := iv.(Marshaler); ok {
  265. if isNil(reflect.ValueOf(iv)) {
  266. return ErrNil
  267. }
  268. data, err := m.Marshal()
  269. if err != nil {
  270. return err
  271. }
  272. o.buf = append(o.buf, p.tagcode...)
  273. o.EncodeRawBytes(data)
  274. return nil
  275. }
  276. v := *(**struct{})(unsafe.Pointer(base + p.offset))
  277. if v == nil {
  278. return ErrNil
  279. }
  280. // need the length before we can write out the message itself,
  281. // so marshal into a separate byte buffer first.
  282. obuf := o.buf
  283. o.buf = o.bufalloc()
  284. b := uintptr(unsafe.Pointer(v))
  285. typ := p.stype.Elem()
  286. err := o.enc_struct(typ, b)
  287. nbuf := o.buf
  288. o.buf = obuf
  289. if err != nil {
  290. o.buffree(nbuf)
  291. return err
  292. }
  293. o.buf = append(o.buf, p.tagcode...)
  294. o.EncodeRawBytes(nbuf)
  295. o.buffree(nbuf)
  296. return nil
  297. }
  298. // Encode a group struct.
  299. func (o *Buffer) enc_struct_group(p *Properties, base uintptr) os.Error {
  300. v := *(**struct{})(unsafe.Pointer(base + p.offset))
  301. if v == nil {
  302. return ErrNil
  303. }
  304. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  305. b := uintptr(unsafe.Pointer(v))
  306. typ := p.stype.Elem()
  307. err := o.enc_struct(typ, b)
  308. if err != nil {
  309. return err
  310. }
  311. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  312. return nil
  313. }
  314. // Encode a slice of bools ([]bool).
  315. func (o *Buffer) enc_slice_bool(p *Properties, base uintptr) os.Error {
  316. s := *(*[]uint8)(unsafe.Pointer(base + p.offset))
  317. l := len(s)
  318. if l == 0 {
  319. return ErrNil
  320. }
  321. for _, x := range s {
  322. o.buf = append(o.buf, p.tagcode...)
  323. if x != 0 {
  324. x = 1
  325. }
  326. p.valEnc(o, uint64(x))
  327. }
  328. return nil
  329. }
  330. // Encode a slice of bools ([]bool) in packed format.
  331. func (o *Buffer) enc_slice_packed_bool(p *Properties, base uintptr) os.Error {
  332. s := *(*[]uint8)(unsafe.Pointer(base + p.offset))
  333. l := len(s)
  334. if l == 0 {
  335. return ErrNil
  336. }
  337. o.buf = append(o.buf, p.tagcode...)
  338. o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
  339. for _, x := range s {
  340. if x != 0 {
  341. x = 1
  342. }
  343. p.valEnc(o, uint64(x))
  344. }
  345. return nil
  346. }
  347. // Encode a slice of bytes ([]byte).
  348. func (o *Buffer) enc_slice_byte(p *Properties, base uintptr) os.Error {
  349. s := *(*[]uint8)(unsafe.Pointer(base + p.offset))
  350. if s == nil {
  351. return ErrNil
  352. }
  353. o.buf = append(o.buf, p.tagcode...)
  354. o.EncodeRawBytes(s)
  355. return nil
  356. }
  357. // Encode a slice of int32s ([]int32).
  358. func (o *Buffer) enc_slice_int32(p *Properties, base uintptr) os.Error {
  359. s := *(*[]uint32)(unsafe.Pointer(base + p.offset))
  360. l := len(s)
  361. if l == 0 {
  362. return ErrNil
  363. }
  364. for i := 0; i < l; i++ {
  365. o.buf = append(o.buf, p.tagcode...)
  366. x := s[i]
  367. p.valEnc(o, uint64(x))
  368. }
  369. return nil
  370. }
  371. // Encode a slice of int32s ([]int32) in packed format.
  372. func (o *Buffer) enc_slice_packed_int32(p *Properties, base uintptr) os.Error {
  373. s := *(*[]uint32)(unsafe.Pointer(base + p.offset))
  374. l := len(s)
  375. if l == 0 {
  376. return ErrNil
  377. }
  378. // TODO: Reuse a Buffer.
  379. buf := NewBuffer(nil)
  380. for i := 0; i < l; i++ {
  381. p.valEnc(buf, uint64(s[i]))
  382. }
  383. o.buf = append(o.buf, p.tagcode...)
  384. o.EncodeVarint(uint64(len(buf.buf)))
  385. o.buf = append(o.buf, buf.buf...)
  386. return nil
  387. }
  388. // Encode a slice of int64s ([]int64).
  389. func (o *Buffer) enc_slice_int64(p *Properties, base uintptr) os.Error {
  390. s := *(*[]uint64)(unsafe.Pointer(base + p.offset))
  391. l := len(s)
  392. if l == 0 {
  393. return ErrNil
  394. }
  395. for i := 0; i < l; i++ {
  396. o.buf = append(o.buf, p.tagcode...)
  397. x := s[i]
  398. p.valEnc(o, uint64(x))
  399. }
  400. return nil
  401. }
  402. // Encode a slice of int64s ([]int64) in packed format.
  403. func (o *Buffer) enc_slice_packed_int64(p *Properties, base uintptr) os.Error {
  404. s := *(*[]uint64)(unsafe.Pointer(base + p.offset))
  405. l := len(s)
  406. if l == 0 {
  407. return ErrNil
  408. }
  409. // TODO: Reuse a Buffer.
  410. buf := NewBuffer(nil)
  411. for i := 0; i < l; i++ {
  412. p.valEnc(buf, s[i])
  413. }
  414. o.buf = append(o.buf, p.tagcode...)
  415. o.EncodeVarint(uint64(len(buf.buf)))
  416. o.buf = append(o.buf, buf.buf...)
  417. return nil
  418. }
  419. // Encode a slice of slice of bytes ([][]byte).
  420. func (o *Buffer) enc_slice_slice_byte(p *Properties, base uintptr) os.Error {
  421. ss := *(*[][]uint8)(unsafe.Pointer(base + p.offset))
  422. l := len(ss)
  423. if l == 0 {
  424. return ErrNil
  425. }
  426. for i := 0; i < l; i++ {
  427. o.buf = append(o.buf, p.tagcode...)
  428. s := ss[i]
  429. o.EncodeRawBytes(s)
  430. }
  431. return nil
  432. }
  433. // Encode a slice of strings ([]string).
  434. func (o *Buffer) enc_slice_string(p *Properties, base uintptr) os.Error {
  435. ss := *(*[]string)(unsafe.Pointer(base + p.offset))
  436. l := len(ss)
  437. for i := 0; i < l; i++ {
  438. o.buf = append(o.buf, p.tagcode...)
  439. s := ss[i]
  440. o.EncodeStringBytes(s)
  441. }
  442. return nil
  443. }
  444. // Encode a slice of message structs ([]*struct).
  445. func (o *Buffer) enc_slice_struct_message(p *Properties, base uintptr) os.Error {
  446. s := *(*[]*struct{})(unsafe.Pointer(base + p.offset))
  447. l := len(s)
  448. typ := p.stype.Elem()
  449. for i := 0; i < l; i++ {
  450. v := s[i]
  451. if v == nil {
  452. return ErrRepeatedHasNil
  453. }
  454. // Can the object marshal itself?
  455. iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&s[i]))
  456. if m, ok := iv.(Marshaler); ok {
  457. if isNil(reflect.ValueOf(iv)) {
  458. return ErrNil
  459. }
  460. data, err := m.Marshal()
  461. if err != nil {
  462. return err
  463. }
  464. o.buf = append(o.buf, p.tagcode...)
  465. o.EncodeRawBytes(data)
  466. continue
  467. }
  468. obuf := o.buf
  469. o.buf = o.bufalloc()
  470. b := uintptr(unsafe.Pointer(v))
  471. err := o.enc_struct(typ, b)
  472. nbuf := o.buf
  473. o.buf = obuf
  474. if err != nil {
  475. o.buffree(nbuf)
  476. if err == ErrNil {
  477. return ErrRepeatedHasNil
  478. }
  479. return err
  480. }
  481. o.buf = append(o.buf, p.tagcode...)
  482. o.EncodeRawBytes(nbuf)
  483. o.buffree(nbuf)
  484. }
  485. return nil
  486. }
  487. // Encode a slice of group structs ([]*struct).
  488. func (o *Buffer) enc_slice_struct_group(p *Properties, base uintptr) os.Error {
  489. s := *(*[]*struct{})(unsafe.Pointer(base + p.offset))
  490. l := len(s)
  491. typ := p.stype.Elem()
  492. for i := 0; i < l; i++ {
  493. v := s[i]
  494. if v == nil {
  495. return ErrRepeatedHasNil
  496. }
  497. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  498. b := uintptr(unsafe.Pointer(v))
  499. err := o.enc_struct(typ, b)
  500. if err != nil {
  501. if err == ErrNil {
  502. return ErrRepeatedHasNil
  503. }
  504. return err
  505. }
  506. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  507. }
  508. return nil
  509. }
  510. // Encode an extension map.
  511. func (o *Buffer) enc_map(p *Properties, base uintptr) os.Error {
  512. v := *(*map[int32]Extension)(unsafe.Pointer(base + p.offset))
  513. if err := encodeExtensionMap(v); err != nil {
  514. return err
  515. }
  516. for _, e := range v {
  517. o.buf = append(o.buf, e.enc...)
  518. }
  519. return nil
  520. }
  521. // Encode a struct.
  522. func (o *Buffer) enc_struct(t reflect.Type, base uintptr) os.Error {
  523. prop := GetProperties(t)
  524. required := prop.reqCount
  525. for _, p := range prop.Prop {
  526. if p.enc != nil {
  527. err := p.enc(o, p, base)
  528. if err != nil {
  529. if err != ErrNil {
  530. return err
  531. }
  532. } else if p.Required {
  533. required--
  534. }
  535. }
  536. }
  537. // See if we encoded all required fields.
  538. if required > 0 {
  539. return &ErrRequiredNotSet{t}
  540. }
  541. return nil
  542. }