encode.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. var ErrRequiredNotSet = os.NewError("required fields not set")
  48. // ErrRepeatedHasNil is the error returned if Marshal is called with
  49. // a protocol buffer struct with a repeated field containing a nil element.
  50. var ErrRepeatedHasNil = os.NewError("repeated field has nil")
  51. // ErrNil is the error returned if Marshal is called with nil.
  52. var ErrNil = os.NewError("marshal called with nil")
  53. // The fundamental encoders that put bytes on the wire.
  54. // Those that take integer types all accept uint64 and are
  55. // therefore of type valueEncoder.
  56. // EncodeVarint returns the varint encoding of x.
  57. // This is the format for the
  58. // int32, int64, uint32, uint64, bool, and enum
  59. // protocol buffer types.
  60. // Not used by the package itself, but helpful to clients
  61. // wishing to use the same encoding.
  62. func EncodeVarint(x uint64) []byte {
  63. var buf [16]byte
  64. var n int
  65. for n = 0; x > 127; n++ {
  66. buf[n] = 0x80 | uint8(x&0x7F)
  67. x >>= 7
  68. }
  69. buf[n] = uint8(x)
  70. n++
  71. return buf[0:n]
  72. }
  73. // EncodeVarint writes a varint-encoded integer to the Buffer.
  74. // This is the format for the
  75. // int32, int64, uint32, uint64, bool, and enum
  76. // protocol buffer types.
  77. func (p *Buffer) EncodeVarint(x uint64) os.Error {
  78. l := len(p.buf)
  79. c := cap(p.buf)
  80. if l+10 > c {
  81. c += c/2 + 10
  82. obuf := make([]byte, c)
  83. copy(obuf, p.buf)
  84. p.buf = obuf
  85. }
  86. p.buf = p.buf[0:c]
  87. for {
  88. if x < 1<<7 {
  89. break
  90. }
  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[0 : 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. l := len(p.buf)
  104. c := cap(p.buf)
  105. if l+8 > c {
  106. c += c/2 + 8
  107. obuf := make([]byte, c)
  108. copy(obuf, p.buf)
  109. p.buf = obuf
  110. }
  111. p.buf = p.buf[0 : l+8]
  112. p.buf[l] = uint8(x)
  113. p.buf[l+1] = uint8(x >> 8)
  114. p.buf[l+2] = uint8(x >> 16)
  115. p.buf[l+3] = uint8(x >> 24)
  116. p.buf[l+4] = uint8(x >> 32)
  117. p.buf[l+5] = uint8(x >> 40)
  118. p.buf[l+6] = uint8(x >> 48)
  119. p.buf[l+7] = uint8(x >> 56)
  120. return nil
  121. }
  122. // EncodeFixed32 writes a 32-bit integer to the Buffer.
  123. // This is the format for the
  124. // fixed32, sfixed32, and float protocol buffer types.
  125. func (p *Buffer) EncodeFixed32(x uint64) os.Error {
  126. l := len(p.buf)
  127. c := cap(p.buf)
  128. if l+4 > c {
  129. c += c/2 + 4
  130. obuf := make([]byte, c)
  131. copy(obuf, p.buf)
  132. p.buf = obuf
  133. }
  134. p.buf = p.buf[0 : l+4]
  135. p.buf[l] = uint8(x)
  136. p.buf[l+1] = uint8(x >> 8)
  137. p.buf[l+2] = uint8(x >> 16)
  138. p.buf[l+3] = uint8(x >> 24)
  139. return nil
  140. }
  141. // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
  142. // to the Buffer.
  143. // This is the format used for the sint64 protocol buffer type.
  144. func (p *Buffer) EncodeZigzag64(x uint64) os.Error {
  145. // use signed number to get arithmetic right shift.
  146. return p.EncodeVarint(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) os.Error {
  152. // use signed number to get arithmetic right shift.
  153. return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  154. }
  155. // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
  156. // This is the format used for the bytes protocol buffer
  157. // type and for embedded messages.
  158. func (p *Buffer) EncodeRawBytes(b []byte) os.Error {
  159. lb := len(b)
  160. p.EncodeVarint(uint64(lb))
  161. p.buf = bytes.Add(p.buf, b)
  162. return nil
  163. }
  164. // EncodeStringBytes writes an encoded string to the Buffer.
  165. // This is the format used for the proto2 string type.
  166. func (p *Buffer) EncodeStringBytes(s string) os.Error {
  167. // this works because strings and slices are the same.
  168. y := *(*[]byte)(unsafe.Pointer(&s))
  169. p.EncodeRawBytes(y)
  170. return nil
  171. }
  172. // Marshaler is the interface representing objects that can marshal themselves.
  173. type Marshaler interface {
  174. Marshal() ([]byte, os.Error)
  175. }
  176. // Marshal takes the protocol buffer struct represented by pb
  177. // and encodes it into the wire format, returning the data.
  178. func Marshal(pb interface{}) ([]byte, os.Error) {
  179. // Can the object marshal itself?
  180. if m, ok := pb.(Marshaler); ok {
  181. return m.Marshal()
  182. }
  183. p := NewBuffer(nil)
  184. err := p.Marshal(pb)
  185. if err != nil {
  186. return nil, err
  187. }
  188. return p.buf, err
  189. }
  190. // Marshal takes the protocol buffer struct represented by pb
  191. // and encodes it into the wire format, writing the result to the
  192. // Buffer.
  193. func (p *Buffer) Marshal(pb interface{}) os.Error {
  194. // Can the object marshal itself?
  195. if m, ok := pb.(Marshaler); ok {
  196. data, err := m.Marshal()
  197. if err != nil {
  198. return err
  199. }
  200. p.buf = bytes.Add(p.buf, data)
  201. return nil
  202. }
  203. mstat := runtime.MemStats.Mallocs
  204. t, b, err := getbase(pb)
  205. if err == nil {
  206. err = p.enc_struct(t.Elem().(*reflect.StructType), b)
  207. }
  208. mstat = runtime.MemStats.Mallocs - mstat
  209. stats.Emalloc += mstat
  210. stats.Encode++
  211. return err
  212. }
  213. // Individual type encoders.
  214. // Encode a bool.
  215. func (o *Buffer) enc_bool(p *Properties, base uintptr) os.Error {
  216. v := *(**uint8)(unsafe.Pointer(base + p.offset))
  217. if v == nil {
  218. return ErrNil
  219. }
  220. x := *v
  221. if x != 0 {
  222. x = 1
  223. }
  224. o.buf = bytes.Add(o.buf, p.tagcode)
  225. p.valEnc(o, uint64(x))
  226. return nil
  227. }
  228. // Encode an int32.
  229. func (o *Buffer) enc_int32(p *Properties, base uintptr) os.Error {
  230. v := *(**uint32)(unsafe.Pointer(base + p.offset))
  231. if v == nil {
  232. return ErrNil
  233. }
  234. x := *v
  235. o.buf = bytes.Add(o.buf, p.tagcode)
  236. p.valEnc(o, uint64(x))
  237. return nil
  238. }
  239. // Encode an int64.
  240. func (o *Buffer) enc_int64(p *Properties, base uintptr) os.Error {
  241. v := *(**uint64)(unsafe.Pointer(base + p.offset))
  242. if v == nil {
  243. return ErrNil
  244. }
  245. x := *v
  246. o.buf = bytes.Add(o.buf, p.tagcode)
  247. p.valEnc(o, uint64(x))
  248. return nil
  249. }
  250. // Encode a string.
  251. func (o *Buffer) enc_string(p *Properties, base uintptr) os.Error {
  252. v := *(**string)(unsafe.Pointer(base + p.offset))
  253. if v == nil {
  254. return ErrNil
  255. }
  256. x := *v
  257. o.buf = bytes.Add(o.buf, p.tagcode)
  258. o.EncodeStringBytes(x)
  259. return nil
  260. }
  261. // Encode a message struct.
  262. func (o *Buffer) enc_struct_message(p *Properties, base uintptr) os.Error {
  263. // Can the object marshal itself?
  264. iv := unsafe.Unreflect(p.stype, unsafe.Pointer(base+p.offset))
  265. if m, ok := iv.(Marshaler); ok {
  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 bytes ([]byte).
  329. func (o *Buffer) enc_slice_byte(p *Properties, base uintptr) os.Error {
  330. s := *(*[]uint8)(unsafe.Pointer(base + p.offset))
  331. // if the field is required, we must send something, even if it's an empty array.
  332. if !p.Required {
  333. l := len(s)
  334. if l == 0 {
  335. return ErrNil
  336. }
  337. // check default
  338. if l == len(p.Default) {
  339. same := true
  340. for i := 0; i < len(p.Default); i++ {
  341. if p.Default[i] != s[i] {
  342. same = false
  343. break
  344. }
  345. }
  346. if same {
  347. return ErrNil
  348. }
  349. }
  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 int64s ([]int64).
  370. func (o *Buffer) enc_slice_int64(p *Properties, base uintptr) os.Error {
  371. s := *(*[]uint64)(unsafe.Pointer(base + p.offset))
  372. l := len(s)
  373. if l == 0 {
  374. return ErrNil
  375. }
  376. for i := 0; i < l; i++ {
  377. o.buf = bytes.Add(o.buf, p.tagcode)
  378. x := s[i]
  379. p.valEnc(o, uint64(x))
  380. }
  381. return nil
  382. }
  383. // Encode a slice of slice of bytes ([][]byte).
  384. func (o *Buffer) enc_slice_slice_byte(p *Properties, base uintptr) os.Error {
  385. ss := *(*[][]uint8)(unsafe.Pointer(base + p.offset))
  386. l := len(ss)
  387. if l == 0 {
  388. return ErrNil
  389. }
  390. for i := 0; i < l; i++ {
  391. o.buf = bytes.Add(o.buf, p.tagcode)
  392. s := ss[i]
  393. o.EncodeRawBytes(s)
  394. }
  395. return nil
  396. }
  397. // Encode a slice of strings ([]string).
  398. func (o *Buffer) enc_slice_string(p *Properties, base uintptr) os.Error {
  399. ss := *(*[]string)(unsafe.Pointer(base + p.offset))
  400. l := len(ss)
  401. for i := 0; i < l; i++ {
  402. o.buf = bytes.Add(o.buf, p.tagcode)
  403. s := ss[i]
  404. o.EncodeStringBytes(s)
  405. }
  406. return nil
  407. }
  408. // Encode a slice of message structs ([]*struct).
  409. func (o *Buffer) enc_slice_struct_message(p *Properties, base uintptr) os.Error {
  410. s := *(*[]*struct{})(unsafe.Pointer(base + p.offset))
  411. l := len(s)
  412. typ := p.stype.Elem().(*reflect.StructType)
  413. for i := 0; i < l; i++ {
  414. v := s[i]
  415. if v == nil {
  416. return ErrRepeatedHasNil
  417. }
  418. // Can the object marshal itself?
  419. iv := unsafe.Unreflect(p.stype, unsafe.Pointer(&s[i]))
  420. if m, ok := iv.(Marshaler); ok {
  421. data, err := m.Marshal()
  422. if err != nil {
  423. return err
  424. }
  425. o.buf = bytes.Add(o.buf, p.tagcode)
  426. o.EncodeRawBytes(data)
  427. continue
  428. }
  429. obuf := o.buf
  430. o.buf = o.bufalloc()
  431. b := uintptr(unsafe.Pointer(v))
  432. err := o.enc_struct(typ, b)
  433. nbuf := o.buf
  434. o.buf = obuf
  435. if err != nil {
  436. o.buffree(nbuf)
  437. if err == ErrNil {
  438. return ErrRepeatedHasNil
  439. }
  440. return err
  441. }
  442. o.buf = bytes.Add(o.buf, p.tagcode)
  443. o.EncodeRawBytes(nbuf)
  444. o.buffree(nbuf)
  445. }
  446. return nil
  447. }
  448. // Encode a slice of group structs ([]*struct).
  449. func (o *Buffer) enc_slice_struct_group(p *Properties, base uintptr) os.Error {
  450. s := *(*[]*struct{})(unsafe.Pointer(base + p.offset))
  451. l := len(s)
  452. typ := p.stype.Elem().(*reflect.StructType)
  453. for i := 0; i < l; i++ {
  454. v := s[i]
  455. if v == nil {
  456. return ErrRepeatedHasNil
  457. }
  458. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  459. b := uintptr(unsafe.Pointer(v))
  460. err := o.enc_struct(typ, b)
  461. if err != nil {
  462. if err == ErrNil {
  463. return ErrRepeatedHasNil
  464. }
  465. return err
  466. }
  467. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  468. }
  469. return nil
  470. }
  471. // Encode an extension map.
  472. func (o *Buffer) enc_map(p *Properties, base uintptr) os.Error {
  473. v := *(*map[int32][]byte)(unsafe.Pointer(base + p.offset))
  474. for _, b := range v {
  475. o.buf = bytes.Add(o.buf, b)
  476. }
  477. return nil
  478. }
  479. // Encode a struct.
  480. func (o *Buffer) enc_struct(t *reflect.StructType, base uintptr) os.Error {
  481. prop := GetProperties(t)
  482. required := prop.reqCount
  483. for _, p := range prop.Prop {
  484. if p.enc != nil {
  485. err := p.enc(o, p, base)
  486. if err != nil {
  487. if err != ErrNil {
  488. return err
  489. }
  490. } else if p.Required {
  491. required--
  492. }
  493. }
  494. }
  495. // See if we encoded all required fields.
  496. if required > 0 {
  497. return ErrRequiredNotSet
  498. }
  499. return nil
  500. }