encode.go 15 KB

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