encode.go 15 KB

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