PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/github.com/golang/protobuf/proto/encode.go

https://gitlab.com/unofficial-mirrors/mattermost-platform
Go | 221 lines | 112 code | 23 blank | 86 comment | 4 complexity | 269a728f793ffda95ffa2069d6748597 MD5 | raw file
  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. "fmt"
  38. "reflect"
  39. )
  40. // RequiredNotSetError is the error returned if Marshal is called with
  41. // a protocol buffer struct whose required fields have not
  42. // all been initialized. It is also the error returned if Unmarshal is
  43. // called with an encoded protocol buffer that does not include all the
  44. // required fields.
  45. //
  46. // When printed, RequiredNotSetError reports the first unset required field in a
  47. // message. If the field cannot be precisely determined, it is reported as
  48. // "{Unknown}".
  49. type RequiredNotSetError struct {
  50. field string
  51. }
  52. func (e *RequiredNotSetError) Error() string {
  53. return fmt.Sprintf("proto: required field %q not set", e.field)
  54. }
  55. var (
  56. // errRepeatedHasNil is the error returned if Marshal is called with
  57. // a struct with a repeated field containing a nil element.
  58. errRepeatedHasNil = errors.New("proto: repeated field has nil element")
  59. // errOneofHasNil is the error returned if Marshal is called with
  60. // a struct with a oneof field containing a nil element.
  61. errOneofHasNil = errors.New("proto: oneof field has nil value")
  62. // ErrNil is the error returned if Marshal is called with nil.
  63. ErrNil = errors.New("proto: Marshal called with nil")
  64. // ErrTooLarge is the error returned if Marshal is called with a
  65. // message that encodes to >2GB.
  66. ErrTooLarge = errors.New("proto: message encodes to over 2 GB")
  67. )
  68. // The fundamental encoders that put bytes on the wire.
  69. // Those that take integer types all accept uint64 and are
  70. // therefore of type valueEncoder.
  71. const maxVarintBytes = 10 // maximum length of a varint
  72. // EncodeVarint returns the varint encoding of x.
  73. // This is the format for the
  74. // int32, int64, uint32, uint64, bool, and enum
  75. // protocol buffer types.
  76. // Not used by the package itself, but helpful to clients
  77. // wishing to use the same encoding.
  78. func EncodeVarint(x uint64) []byte {
  79. var buf [maxVarintBytes]byte
  80. var n int
  81. for n = 0; x > 127; n++ {
  82. buf[n] = 0x80 | uint8(x&0x7F)
  83. x >>= 7
  84. }
  85. buf[n] = uint8(x)
  86. n++
  87. return buf[0:n]
  88. }
  89. // EncodeVarint writes a varint-encoded integer to the Buffer.
  90. // This is the format for the
  91. // int32, int64, uint32, uint64, bool, and enum
  92. // protocol buffer types.
  93. func (p *Buffer) EncodeVarint(x uint64) error {
  94. for x >= 1<<7 {
  95. p.buf = append(p.buf, uint8(x&0x7f|0x80))
  96. x >>= 7
  97. }
  98. p.buf = append(p.buf, uint8(x))
  99. return nil
  100. }
  101. // SizeVarint returns the varint encoding size of an integer.
  102. func SizeVarint(x uint64) int {
  103. switch {
  104. case x < 1<<7:
  105. return 1
  106. case x < 1<<14:
  107. return 2
  108. case x < 1<<21:
  109. return 3
  110. case x < 1<<28:
  111. return 4
  112. case x < 1<<35:
  113. return 5
  114. case x < 1<<42:
  115. return 6
  116. case x < 1<<49:
  117. return 7
  118. case x < 1<<56:
  119. return 8
  120. case x < 1<<63:
  121. return 9
  122. }
  123. return 10
  124. }
  125. // EncodeFixed64 writes a 64-bit integer to the Buffer.
  126. // This is the format for the
  127. // fixed64, sfixed64, and double protocol buffer types.
  128. func (p *Buffer) EncodeFixed64(x uint64) error {
  129. p.buf = append(p.buf,
  130. uint8(x),
  131. uint8(x>>8),
  132. uint8(x>>16),
  133. uint8(x>>24),
  134. uint8(x>>32),
  135. uint8(x>>40),
  136. uint8(x>>48),
  137. uint8(x>>56))
  138. return nil
  139. }
  140. // EncodeFixed32 writes a 32-bit integer to the Buffer.
  141. // This is the format for the
  142. // fixed32, sfixed32, and float protocol buffer types.
  143. func (p *Buffer) EncodeFixed32(x uint64) error {
  144. p.buf = append(p.buf,
  145. uint8(x),
  146. uint8(x>>8),
  147. uint8(x>>16),
  148. uint8(x>>24))
  149. return nil
  150. }
  151. // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
  152. // to the Buffer.
  153. // This is the format used for the sint64 protocol buffer type.
  154. func (p *Buffer) EncodeZigzag64(x uint64) error {
  155. // use signed number to get arithmetic right shift.
  156. return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  157. }
  158. // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
  159. // to the Buffer.
  160. // This is the format used for the sint32 protocol buffer type.
  161. func (p *Buffer) EncodeZigzag32(x uint64) error {
  162. // use signed number to get arithmetic right shift.
  163. return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  164. }
  165. // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
  166. // This is the format used for the bytes protocol buffer
  167. // type and for embedded messages.
  168. func (p *Buffer) EncodeRawBytes(b []byte) error {
  169. p.EncodeVarint(uint64(len(b)))
  170. p.buf = append(p.buf, b...)
  171. return nil
  172. }
  173. // EncodeStringBytes writes an encoded string to the Buffer.
  174. // This is the format used for the proto2 string type.
  175. func (p *Buffer) EncodeStringBytes(s string) error {
  176. p.EncodeVarint(uint64(len(s)))
  177. p.buf = append(p.buf, s...)
  178. return nil
  179. }
  180. // Marshaler is the interface representing objects that can marshal themselves.
  181. type Marshaler interface {
  182. Marshal() ([]byte, error)
  183. }
  184. // EncodeMessage writes the protocol buffer to the Buffer,
  185. // prefixed by a varint-encoded length.
  186. func (p *Buffer) EncodeMessage(pb Message) error {
  187. siz := Size(pb)
  188. p.EncodeVarint(uint64(siz))
  189. return p.Marshal(pb)
  190. }
  191. // All protocol buffer fields are nillable, but be careful.
  192. func isNil(v reflect.Value) bool {
  193. switch v.Kind() {
  194. case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  195. return v.IsNil()
  196. }
  197. return false
  198. }