另客网go项目公用的代码库

encode.go 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  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. "errors"
  37. "reflect"
  38. )
  39. var (
  40. // errRepeatedHasNil is the error returned if Marshal is called with
  41. // a struct with a repeated field containing a nil element.
  42. errRepeatedHasNil = errors.New("proto: repeated field has nil element")
  43. // errOneofHasNil is the error returned if Marshal is called with
  44. // a struct with a oneof field containing a nil element.
  45. errOneofHasNil = errors.New("proto: oneof field has nil value")
  46. // ErrNil is the error returned if Marshal is called with nil.
  47. ErrNil = errors.New("proto: Marshal called with nil")
  48. // ErrTooLarge is the error returned if Marshal is called with a
  49. // message that encodes to >2GB.
  50. ErrTooLarge = errors.New("proto: message encodes to over 2 GB")
  51. )
  52. // The fundamental encoders that put bytes on the wire.
  53. // Those that take integer types all accept uint64 and are
  54. // therefore of type valueEncoder.
  55. const maxVarintBytes = 10 // maximum length of a varint
  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 [maxVarintBytes]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) error {
  78. for x >= 1<<7 {
  79. p.buf = append(p.buf, uint8(x&0x7f|0x80))
  80. x >>= 7
  81. }
  82. p.buf = append(p.buf, uint8(x))
  83. return nil
  84. }
  85. // SizeVarint returns the varint encoding size of an integer.
  86. func SizeVarint(x uint64) int {
  87. switch {
  88. case x < 1<<7:
  89. return 1
  90. case x < 1<<14:
  91. return 2
  92. case x < 1<<21:
  93. return 3
  94. case x < 1<<28:
  95. return 4
  96. case x < 1<<35:
  97. return 5
  98. case x < 1<<42:
  99. return 6
  100. case x < 1<<49:
  101. return 7
  102. case x < 1<<56:
  103. return 8
  104. case x < 1<<63:
  105. return 9
  106. }
  107. return 10
  108. }
  109. // EncodeFixed64 writes a 64-bit integer to the Buffer.
  110. // This is the format for the
  111. // fixed64, sfixed64, and double protocol buffer types.
  112. func (p *Buffer) EncodeFixed64(x uint64) error {
  113. p.buf = append(p.buf,
  114. uint8(x),
  115. uint8(x>>8),
  116. uint8(x>>16),
  117. uint8(x>>24),
  118. uint8(x>>32),
  119. uint8(x>>40),
  120. uint8(x>>48),
  121. uint8(x>>56))
  122. return nil
  123. }
  124. // EncodeFixed32 writes a 32-bit integer to the Buffer.
  125. // This is the format for the
  126. // fixed32, sfixed32, and float protocol buffer types.
  127. func (p *Buffer) EncodeFixed32(x uint64) error {
  128. p.buf = append(p.buf,
  129. uint8(x),
  130. uint8(x>>8),
  131. uint8(x>>16),
  132. uint8(x>>24))
  133. return nil
  134. }
  135. // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
  136. // to the Buffer.
  137. // This is the format used for the sint64 protocol buffer type.
  138. func (p *Buffer) EncodeZigzag64(x uint64) error {
  139. // use signed number to get arithmetic right shift.
  140. return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  141. }
  142. // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
  143. // to the Buffer.
  144. // This is the format used for the sint32 protocol buffer type.
  145. func (p *Buffer) EncodeZigzag32(x uint64) error {
  146. // use signed number to get arithmetic right shift.
  147. return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  148. }
  149. // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
  150. // This is the format used for the bytes protocol buffer
  151. // type and for embedded messages.
  152. func (p *Buffer) EncodeRawBytes(b []byte) error {
  153. p.EncodeVarint(uint64(len(b)))
  154. p.buf = append(p.buf, b...)
  155. return nil
  156. }
  157. // EncodeStringBytes writes an encoded string to the Buffer.
  158. // This is the format used for the proto2 string type.
  159. func (p *Buffer) EncodeStringBytes(s string) error {
  160. p.EncodeVarint(uint64(len(s)))
  161. p.buf = append(p.buf, s...)
  162. return nil
  163. }
  164. // Marshaler is the interface representing objects that can marshal themselves.
  165. type Marshaler interface {
  166. Marshal() ([]byte, error)
  167. }
  168. // EncodeMessage writes the protocol buffer to the Buffer,
  169. // prefixed by a varint-encoded length.
  170. func (p *Buffer) EncodeMessage(pb Message) error {
  171. siz := Size(pb)
  172. p.EncodeVarint(uint64(siz))
  173. return p.Marshal(pb)
  174. }
  175. // All protocol buffer fields are nillable, but be careful.
  176. func isNil(v reflect.Value) bool {
  177. switch v.Kind() {
  178. case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  179. return v.IsNil()
  180. }
  181. return false
  182. }