encode.go 15 KB

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