encode.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proto
  5. /*
  6. * Routines for encoding data into the wire format for protocol buffers.
  7. */
  8. import (
  9. "errors"
  10. "reflect"
  11. )
  12. var (
  13. // errRepeatedHasNil is the error returned if Marshal is called with
  14. // a struct with a repeated field containing a nil element.
  15. errRepeatedHasNil = errors.New("proto: repeated field has nil element")
  16. // errOneofHasNil is the error returned if Marshal is called with
  17. // a struct with a oneof field containing a nil element.
  18. errOneofHasNil = errors.New("proto: oneof field has nil value")
  19. // ErrNil is the error returned if Marshal is called with nil.
  20. ErrNil = errors.New("proto: Marshal called with nil")
  21. // ErrTooLarge is the error returned if Marshal is called with a
  22. // message that encodes to >2GB.
  23. ErrTooLarge = errors.New("proto: message encodes to over 2 GB")
  24. )
  25. // The fundamental encoders that put bytes on the wire.
  26. // Those that take integer types all accept uint64 and are
  27. // therefore of type valueEncoder.
  28. const maxVarintBytes = 10 // maximum length of a varint
  29. // EncodeVarint returns the varint encoding of x.
  30. // This is the format for the
  31. // int32, int64, uint32, uint64, bool, and enum
  32. // protocol buffer types.
  33. // Not used by the package itself, but helpful to clients
  34. // wishing to use the same encoding.
  35. func EncodeVarint(x uint64) []byte {
  36. var buf [maxVarintBytes]byte
  37. var n int
  38. for n = 0; x > 127; n++ {
  39. buf[n] = 0x80 | uint8(x&0x7F)
  40. x >>= 7
  41. }
  42. buf[n] = uint8(x)
  43. n++
  44. return buf[0:n]
  45. }
  46. // EncodeVarint writes a varint-encoded integer to the Buffer.
  47. // This is the format for the
  48. // int32, int64, uint32, uint64, bool, and enum
  49. // protocol buffer types.
  50. func (p *Buffer) EncodeVarint(x uint64) error {
  51. for x >= 1<<7 {
  52. p.buf = append(p.buf, uint8(x&0x7f|0x80))
  53. x >>= 7
  54. }
  55. p.buf = append(p.buf, uint8(x))
  56. return nil
  57. }
  58. // SizeVarint returns the varint encoding size of an integer.
  59. func SizeVarint(x uint64) int {
  60. switch {
  61. case x < 1<<7:
  62. return 1
  63. case x < 1<<14:
  64. return 2
  65. case x < 1<<21:
  66. return 3
  67. case x < 1<<28:
  68. return 4
  69. case x < 1<<35:
  70. return 5
  71. case x < 1<<42:
  72. return 6
  73. case x < 1<<49:
  74. return 7
  75. case x < 1<<56:
  76. return 8
  77. case x < 1<<63:
  78. return 9
  79. }
  80. return 10
  81. }
  82. // EncodeFixed64 writes a 64-bit integer to the Buffer.
  83. // This is the format for the
  84. // fixed64, sfixed64, and double protocol buffer types.
  85. func (p *Buffer) EncodeFixed64(x uint64) error {
  86. p.buf = append(p.buf,
  87. uint8(x),
  88. uint8(x>>8),
  89. uint8(x>>16),
  90. uint8(x>>24),
  91. uint8(x>>32),
  92. uint8(x>>40),
  93. uint8(x>>48),
  94. uint8(x>>56))
  95. return nil
  96. }
  97. // EncodeFixed32 writes a 32-bit integer to the Buffer.
  98. // This is the format for the
  99. // fixed32, sfixed32, and float protocol buffer types.
  100. func (p *Buffer) EncodeFixed32(x uint64) error {
  101. p.buf = append(p.buf,
  102. uint8(x),
  103. uint8(x>>8),
  104. uint8(x>>16),
  105. uint8(x>>24))
  106. return nil
  107. }
  108. // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
  109. // to the Buffer.
  110. // This is the format used for the sint64 protocol buffer type.
  111. func (p *Buffer) EncodeZigzag64(x uint64) error {
  112. // use signed number to get arithmetic right shift.
  113. return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  114. }
  115. // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
  116. // to the Buffer.
  117. // This is the format used for the sint32 protocol buffer type.
  118. func (p *Buffer) EncodeZigzag32(x uint64) error {
  119. // use signed number to get arithmetic right shift.
  120. return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  121. }
  122. // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
  123. // This is the format used for the bytes protocol buffer
  124. // type and for embedded messages.
  125. func (p *Buffer) EncodeRawBytes(b []byte) error {
  126. p.EncodeVarint(uint64(len(b)))
  127. p.buf = append(p.buf, b...)
  128. return nil
  129. }
  130. // EncodeStringBytes writes an encoded string to the Buffer.
  131. // This is the format used for the proto2 string type.
  132. func (p *Buffer) EncodeStringBytes(s string) error {
  133. p.EncodeVarint(uint64(len(s)))
  134. p.buf = append(p.buf, s...)
  135. return nil
  136. }
  137. // Marshaler is the interface representing objects that can marshal themselves.
  138. type Marshaler interface {
  139. Marshal() ([]byte, error)
  140. }
  141. // EncodeMessage writes the protocol buffer to the Buffer,
  142. // prefixed by a varint-encoded length.
  143. func (p *Buffer) EncodeMessage(pb Message) error {
  144. siz := Size(pb)
  145. p.EncodeVarint(uint64(siz))
  146. return p.Marshal(pb)
  147. }
  148. // All protocol buffer fields are nillable, but be careful.
  149. func isNil(v reflect.Value) bool {
  150. switch v.Kind() {
  151. case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  152. return v.IsNil()
  153. }
  154. return false
  155. }