grpc 1.23.0 → 1.24.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (245) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +765 -485
  3. data/include/grpc/grpc.h +3 -1
  4. data/include/grpc/grpc_security.h +20 -4
  5. data/include/grpc/impl/codegen/grpc_types.h +6 -5
  6. data/include/grpc/impl/codegen/port_platform.h +25 -0
  7. data/src/core/ext/filters/client_channel/backend_metric.cc +78 -0
  8. data/src/core/ext/filters/client_channel/backend_metric.h +36 -0
  9. data/src/core/ext/filters/client_channel/channel_connectivity.cc +16 -2
  10. data/src/core/ext/filters/client_channel/client_channel.cc +325 -267
  11. data/src/core/ext/filters/client_channel/client_channel_factory.h +0 -4
  12. data/src/core/ext/filters/client_channel/health/health_check_client.cc +23 -32
  13. data/src/core/ext/filters/client_channel/http_proxy.cc +7 -3
  14. data/src/core/ext/filters/client_channel/lb_policy.cc +1 -1
  15. data/src/core/ext/filters/client_channel/lb_policy.h +58 -34
  16. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +46 -50
  17. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel.h +9 -2
  18. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +35 -17
  19. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +130 -215
  20. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +34 -21
  21. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +1120 -802
  22. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel.h +8 -2
  23. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_channel_secure.cc +33 -12
  24. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_client_stats.cc +151 -40
  25. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_client_stats.h +184 -26
  26. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.cc +389 -245
  27. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.h +98 -60
  28. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +6 -1
  29. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +89 -0
  30. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +3 -8
  31. data/src/core/ext/filters/client_channel/server_address.cc +1 -3
  32. data/src/core/ext/filters/client_channel/server_address.h +1 -1
  33. data/src/core/ext/filters/client_channel/subchannel.h +2 -1
  34. data/src/core/ext/filters/client_idle/client_idle_filter.cc +207 -29
  35. data/src/core/ext/filters/http/client/http_client_filter.cc +10 -8
  36. data/src/core/ext/filters/http/client_authority_filter.cc +1 -1
  37. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +10 -7
  38. data/src/core/ext/filters/http/server/http_server_filter.cc +52 -26
  39. data/src/core/ext/transport/chttp2/client/insecure/channel_create.cc +23 -20
  40. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.cc +24 -21
  41. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +1 -1
  42. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +37 -24
  43. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +1 -0
  44. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +237 -191
  45. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +29 -27
  46. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +1 -1
  47. data/src/core/ext/transport/chttp2/transport/hpack_table.cc +19 -4
  48. data/src/core/ext/transport/chttp2/transport/hpack_table.h +13 -4
  49. data/src/core/ext/transport/chttp2/transport/incoming_metadata.cc +2 -1
  50. data/src/core/ext/transport/chttp2/transport/internal.h +0 -2
  51. data/src/core/ext/transport/chttp2/transport/parsing.cc +99 -71
  52. data/src/core/ext/upb-generated/envoy/api/v2/auth/cert.upb.c +222 -0
  53. data/src/core/ext/upb-generated/envoy/api/v2/auth/cert.upb.h +818 -0
  54. data/src/core/ext/upb-generated/envoy/api/v2/cds.upb.c +314 -0
  55. data/src/core/ext/upb-generated/envoy/api/v2/cds.upb.h +1142 -0
  56. data/src/core/ext/upb-generated/envoy/api/v2/cluster/circuit_breaker.upb.c +53 -0
  57. data/src/core/ext/upb-generated/envoy/api/v2/cluster/circuit_breaker.upb.h +158 -0
  58. data/src/core/ext/upb-generated/envoy/api/v2/cluster/filter.upb.c +34 -0
  59. data/src/core/ext/upb-generated/envoy/api/v2/cluster/filter.upb.h +69 -0
  60. data/src/core/ext/upb-generated/envoy/api/v2/cluster/outlier_detection.upb.c +49 -0
  61. data/src/core/ext/upb-generated/envoy/api/v2/cluster/outlier_detection.upb.h +240 -0
  62. data/src/core/ext/upb-generated/envoy/api/v2/core/address.upb.c +110 -0
  63. data/src/core/ext/upb-generated/envoy/api/v2/core/address.upb.h +324 -0
  64. data/src/core/ext/upb-generated/envoy/api/v2/core/base.upb.c +235 -0
  65. data/src/core/ext/upb-generated/envoy/api/v2/core/base.upb.h +661 -0
  66. data/src/core/ext/upb-generated/envoy/api/v2/core/config_source.upb.c +84 -0
  67. data/src/core/ext/upb-generated/envoy/api/v2/core/config_source.upb.h +274 -0
  68. data/src/core/ext/upb-generated/envoy/api/v2/core/grpc_service.upb.c +175 -0
  69. data/src/core/ext/upb-generated/envoy/api/v2/core/grpc_service.upb.h +572 -0
  70. data/src/core/ext/upb-generated/envoy/api/v2/core/health_check.upb.c +150 -0
  71. data/src/core/ext/upb-generated/envoy/api/v2/core/health_check.upb.h +596 -0
  72. data/src/core/ext/upb-generated/envoy/api/v2/core/http_uri.upb.c +35 -0
  73. data/src/core/ext/upb-generated/envoy/api/v2/core/http_uri.upb.h +80 -0
  74. data/src/core/ext/upb-generated/envoy/api/v2/core/protocol.upb.c +95 -0
  75. data/src/core/ext/upb-generated/envoy/api/v2/core/protocol.upb.h +308 -0
  76. data/src/core/ext/upb-generated/envoy/api/v2/discovery.upb.c +128 -0
  77. data/src/core/ext/upb-generated/envoy/api/v2/discovery.upb.h +392 -0
  78. data/src/core/ext/upb-generated/envoy/api/v2/eds.upb.c +91 -0
  79. data/src/core/ext/upb-generated/envoy/api/v2/eds.upb.h +236 -0
  80. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/endpoint.upb.c +88 -0
  81. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/endpoint.upb.h +258 -0
  82. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/load_report.upb.c +111 -0
  83. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/load_report.upb.h +324 -0
  84. data/src/core/ext/upb-generated/envoy/service/discovery/v2/ads.upb.c +23 -0
  85. data/src/core/ext/upb-generated/envoy/service/discovery/v2/ads.upb.h +50 -0
  86. data/src/core/ext/upb-generated/envoy/service/load_stats/v2/lrs.upb.c +52 -0
  87. data/src/core/ext/upb-generated/envoy/service/load_stats/v2/lrs.upb.h +130 -0
  88. data/src/core/ext/upb-generated/envoy/type/percent.upb.c +39 -0
  89. data/src/core/ext/upb-generated/envoy/type/percent.upb.h +87 -0
  90. data/src/core/ext/upb-generated/envoy/type/range.upb.c +39 -0
  91. data/src/core/ext/upb-generated/envoy/type/range.upb.h +85 -0
  92. data/src/core/ext/upb-generated/gogoproto/gogo.upb.c +17 -0
  93. data/src/core/ext/upb-generated/gogoproto/gogo.upb.h +30 -0
  94. data/src/core/ext/upb-generated/google/api/annotations.upb.c +18 -0
  95. data/src/core/ext/upb-generated/google/api/annotations.upb.h +30 -0
  96. data/src/core/ext/upb-generated/google/api/http.upb.c +66 -0
  97. data/src/core/ext/upb-generated/google/api/http.upb.h +190 -0
  98. data/src/core/ext/upb-generated/google/protobuf/any.upb.c +27 -0
  99. data/src/core/ext/upb-generated/google/protobuf/any.upb.h +58 -0
  100. data/src/core/ext/upb-generated/google/protobuf/descriptor.upb.c +485 -0
  101. data/src/core/ext/upb-generated/google/protobuf/descriptor.upb.h +1690 -0
  102. data/src/core/ext/upb-generated/google/protobuf/duration.upb.c +27 -0
  103. data/src/core/ext/upb-generated/google/protobuf/duration.upb.h +58 -0
  104. data/src/core/ext/upb-generated/google/protobuf/empty.upb.c +22 -0
  105. data/src/core/ext/upb-generated/google/protobuf/empty.upb.h +50 -0
  106. data/src/core/ext/upb-generated/google/protobuf/struct.upb.c +79 -0
  107. data/src/core/ext/upb-generated/google/protobuf/struct.upb.h +215 -0
  108. data/src/core/ext/upb-generated/google/protobuf/timestamp.upb.c +27 -0
  109. data/src/core/ext/upb-generated/google/protobuf/timestamp.upb.h +58 -0
  110. data/src/core/ext/upb-generated/google/protobuf/wrappers.upb.c +106 -0
  111. data/src/core/ext/upb-generated/google/protobuf/wrappers.upb.h +238 -0
  112. data/src/core/ext/upb-generated/google/rpc/status.upb.c +33 -0
  113. data/src/core/ext/upb-generated/google/rpc/status.upb.h +74 -0
  114. data/src/core/ext/upb-generated/src/proto/grpc/gcp/altscontext.upb.c +49 -0
  115. data/src/core/ext/upb-generated/src/proto/grpc/gcp/altscontext.upb.h +126 -0
  116. data/src/core/ext/upb-generated/src/proto/grpc/gcp/handshaker.upb.c +209 -0
  117. data/src/core/ext/upb-generated/src/proto/grpc/gcp/handshaker.upb.h +681 -0
  118. data/src/core/ext/upb-generated/src/proto/grpc/gcp/transport_security_common.upb.c +42 -0
  119. data/src/core/ext/upb-generated/src/proto/grpc/gcp/transport_security_common.upb.h +109 -0
  120. data/src/core/ext/upb-generated/src/proto/grpc/health/v1/health.upb.c +36 -0
  121. data/src/core/ext/upb-generated/src/proto/grpc/health/v1/health.upb.h +84 -0
  122. data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.c +133 -0
  123. data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.h +359 -0
  124. data/src/core/ext/upb-generated/udpa/data/orca/v1/orca_load_report.upb.c +58 -0
  125. data/src/core/ext/upb-generated/udpa/data/orca/v1/orca_load_report.upb.h +144 -0
  126. data/src/core/ext/upb-generated/validate/validate.upb.c +443 -0
  127. data/src/core/ext/upb-generated/validate/validate.upb.h +2037 -0
  128. data/src/core/lib/channel/channel_args.cc +21 -0
  129. data/src/core/lib/channel/channel_args.h +16 -2
  130. data/src/core/lib/channel/channel_stack.h +2 -1
  131. data/src/core/lib/channel/channelz.cc +54 -56
  132. data/src/core/lib/channel/channelz.h +29 -12
  133. data/src/core/lib/compression/compression.cc +2 -1
  134. data/src/core/lib/compression/compression_internal.h +8 -0
  135. data/src/core/lib/gpr/log_linux.cc +2 -2
  136. data/src/core/lib/gpr/log_posix.cc +2 -2
  137. data/src/core/lib/gpr/time_precise.cc +123 -36
  138. data/src/core/lib/gpr/time_precise.h +37 -0
  139. data/src/core/lib/gprpp/abstract.h +10 -0
  140. data/src/core/lib/gprpp/atomic.h +4 -0
  141. data/src/core/lib/gprpp/inlined_vector.h +20 -4
  142. data/src/core/lib/gprpp/map.h +109 -6
  143. data/src/core/lib/gprpp/memory.h +6 -0
  144. data/src/core/lib/gprpp/ref_counted_ptr.h +2 -0
  145. data/src/core/lib/iomgr/ev_epollex_linux.cc +29 -54
  146. data/src/core/lib/iomgr/exec_ctx.cc +27 -17
  147. data/src/core/lib/iomgr/exec_ctx.h +3 -0
  148. data/src/core/lib/iomgr/sockaddr_utils.cc +1 -3
  149. data/src/core/lib/iomgr/tcp_posix.cc +16 -25
  150. data/src/core/lib/iomgr/tcp_server_custom.cc +1 -1
  151. data/src/core/lib/iomgr/timer_manager.cc +8 -1
  152. data/src/core/lib/iomgr/timer_manager.h +2 -0
  153. data/src/core/lib/security/credentials/credentials.h +8 -2
  154. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.cc +23 -0
  155. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +3 -0
  156. data/src/core/lib/security/credentials/tls/spiffe_credentials.cc +3 -3
  157. data/src/core/lib/security/security_connector/ssl_utils.cc +1 -12
  158. data/src/core/lib/security/security_connector/ssl_utils.h +10 -6
  159. data/src/core/lib/security/security_connector/ssl_utils_config.cc +32 -0
  160. data/src/core/lib/security/security_connector/ssl_utils_config.h +30 -0
  161. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.cc +161 -49
  162. data/src/core/lib/security/security_connector/tls/spiffe_security_connector.h +34 -1
  163. data/src/core/lib/slice/slice_intern.cc +17 -9
  164. data/src/core/lib/slice/slice_internal.h +34 -7
  165. data/src/core/lib/slice/slice_utils.h +7 -3
  166. data/src/core/lib/surface/call.cc +97 -57
  167. data/src/core/lib/surface/channel.cc +2 -2
  168. data/src/core/lib/surface/completion_queue.cc +10 -16
  169. data/src/core/lib/surface/init.cc +3 -0
  170. data/src/core/lib/surface/server.cc +11 -14
  171. data/src/core/lib/surface/validate_metadata.cc +4 -0
  172. data/src/core/lib/surface/version.cc +2 -2
  173. data/src/core/lib/transport/metadata.cc +4 -4
  174. data/src/core/lib/transport/metadata_batch.cc +72 -16
  175. data/src/core/lib/transport/metadata_batch.h +38 -0
  176. data/src/core/lib/transport/static_metadata.cc +814 -1023
  177. data/src/core/lib/transport/static_metadata.h +271 -213
  178. data/src/core/lib/transport/transport.h +12 -0
  179. data/src/core/plugin_registry/grpc_plugin_registry.cc +4 -0
  180. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +104 -76
  181. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +34 -16
  182. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.h +2 -2
  183. data/src/core/tsi/alts/handshaker/alts_tsi_utils.cc +10 -6
  184. data/src/core/tsi/alts/handshaker/alts_tsi_utils.h +4 -3
  185. data/src/core/tsi/alts/handshaker/transport_security_common_api.cc +74 -48
  186. data/src/core/tsi/alts/handshaker/transport_security_common_api.h +34 -26
  187. data/src/core/tsi/ssl_transport_security.cc +14 -6
  188. data/src/core/tsi/ssl_transport_security.h +4 -0
  189. data/src/ruby/ext/grpc/ext-export.clang +1 -0
  190. data/src/ruby/ext/grpc/ext-export.gcc +6 -0
  191. data/src/ruby/ext/grpc/extconf.rb +5 -0
  192. data/src/ruby/ext/grpc/rb_enable_cpp.cc +22 -0
  193. data/src/ruby/ext/grpc/rb_grpc.c +1 -42
  194. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +4 -0
  195. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +6 -0
  196. data/src/ruby/lib/grpc.rb +2 -0
  197. data/src/ruby/lib/grpc/core/status_codes.rb +135 -0
  198. data/src/ruby/lib/grpc/errors.rb +4 -7
  199. data/src/ruby/lib/grpc/google_rpc_status_utils.rb +9 -4
  200. data/src/ruby/lib/grpc/structs.rb +15 -0
  201. data/src/ruby/lib/grpc/version.rb +1 -1
  202. data/src/ruby/spec/errors_spec.rb +1 -0
  203. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_import.proto +22 -0
  204. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_ruby_style.proto +34 -0
  205. data/src/ruby/spec/pb/codegen/package_option_spec.rb +53 -29
  206. data/third_party/upb/upb/decode.c +604 -0
  207. data/third_party/upb/upb/decode.h +21 -0
  208. data/third_party/upb/upb/encode.c +378 -0
  209. data/third_party/upb/upb/encode.h +21 -0
  210. data/third_party/upb/upb/generated_util.h +105 -0
  211. data/third_party/upb/upb/msg.c +111 -0
  212. data/third_party/upb/upb/msg.h +69 -0
  213. data/third_party/upb/upb/port.c +27 -0
  214. data/third_party/upb/upb/port_def.inc +152 -0
  215. data/third_party/upb/upb/port_undef.inc +21 -0
  216. data/third_party/upb/upb/table.c +911 -0
  217. data/third_party/upb/upb/table.int.h +507 -0
  218. data/third_party/upb/upb/upb.c +261 -0
  219. data/third_party/upb/upb/upb.h +364 -0
  220. metadata +129 -50
  221. data/src/core/ext/filters/client_channel/health/health.pb.c +0 -23
  222. data/src/core/ext/filters/client_channel/health/health.pb.h +0 -73
  223. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.c +0 -19
  224. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/duration.pb.h +0 -54
  225. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.c +0 -19
  226. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/google/protobuf/timestamp.pb.h +0 -54
  227. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.c +0 -89
  228. data/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +0 -164
  229. data/src/core/tsi/alts/handshaker/alts_handshaker_service_api.cc +0 -520
  230. data/src/core/tsi/alts/handshaker/alts_handshaker_service_api.h +0 -323
  231. data/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.cc +0 -145
  232. data/src/core/tsi/alts/handshaker/alts_handshaker_service_api_util.h +0 -149
  233. data/src/core/tsi/alts/handshaker/altscontext.pb.c +0 -47
  234. data/src/core/tsi/alts/handshaker/altscontext.pb.h +0 -63
  235. data/src/core/tsi/alts/handshaker/handshaker.pb.c +0 -122
  236. data/src/core/tsi/alts/handshaker/handshaker.pb.h +0 -254
  237. data/src/core/tsi/alts/handshaker/transport_security_common.pb.c +0 -49
  238. data/src/core/tsi/alts/handshaker/transport_security_common.pb.h +0 -78
  239. data/third_party/nanopb/pb.h +0 -579
  240. data/third_party/nanopb/pb_common.c +0 -97
  241. data/third_party/nanopb/pb_common.h +0 -42
  242. data/third_party/nanopb/pb_decode.c +0 -1347
  243. data/third_party/nanopb/pb_decode.h +0 -149
  244. data/third_party/nanopb/pb_encode.c +0 -696
  245. data/third_party/nanopb/pb_encode.h +0 -154
@@ -1,149 +0,0 @@
1
- /* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c.
2
- * The main function is pb_decode. You also need an input stream, and the
3
- * field descriptions created by nanopb_generator.py.
4
- */
5
-
6
- #ifndef PB_DECODE_H_INCLUDED
7
- #define PB_DECODE_H_INCLUDED
8
-
9
- #include "pb.h"
10
-
11
- #ifdef __cplusplus
12
- extern "C" {
13
- #endif
14
-
15
- /* Structure for defining custom input streams. You will need to provide
16
- * a callback function to read the bytes from your storage, which can be
17
- * for example a file or a network socket.
18
- *
19
- * The callback must conform to these rules:
20
- *
21
- * 1) Return false on IO errors. This will cause decoding to abort.
22
- * 2) You can use state to store your own data (e.g. buffer pointer),
23
- * and rely on pb_read to verify that no-body reads past bytes_left.
24
- * 3) Your callback may be used with substreams, in which case bytes_left
25
- * is different than from the main stream. Don't use bytes_left to compute
26
- * any pointers.
27
- */
28
- struct pb_istream_s
29
- {
30
- #ifdef PB_BUFFER_ONLY
31
- /* Callback pointer is not used in buffer-only configuration.
32
- * Having an int pointer here allows binary compatibility but
33
- * gives an error if someone tries to assign callback function.
34
- */
35
- int *callback;
36
- #else
37
- bool (*callback)(pb_istream_t *stream, pb_byte_t *buf, size_t count);
38
- #endif
39
-
40
- void *state; /* Free field for use by callback implementation */
41
- size_t bytes_left;
42
-
43
- #ifndef PB_NO_ERRMSG
44
- const char *errmsg;
45
- #endif
46
- };
47
-
48
- /***************************
49
- * Main decoding functions *
50
- ***************************/
51
-
52
- /* Decode a single protocol buffers message from input stream into a C structure.
53
- * Returns true on success, false on any failure.
54
- * The actual struct pointed to by dest must match the description in fields.
55
- * Callback fields of the destination structure must be initialized by caller.
56
- * All other fields will be initialized by this function.
57
- *
58
- * Example usage:
59
- * MyMessage msg = {};
60
- * uint8_t buffer[64];
61
- * pb_istream_t stream;
62
- *
63
- * // ... read some data into buffer ...
64
- *
65
- * stream = pb_istream_from_buffer(buffer, count);
66
- * pb_decode(&stream, MyMessage_fields, &msg);
67
- */
68
- bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
69
-
70
- /* Same as pb_decode, except does not initialize the destination structure
71
- * to default values. This is slightly faster if you need no default values
72
- * and just do memset(struct, 0, sizeof(struct)) yourself.
73
- *
74
- * This can also be used for 'merging' two messages, i.e. update only the
75
- * fields that exist in the new message.
76
- *
77
- * Note: If this function returns with an error, it will not release any
78
- * dynamically allocated fields. You will need to call pb_release() yourself.
79
- */
80
- bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
81
-
82
- /* Same as pb_decode, except expects the stream to start with the message size
83
- * encoded as varint. Corresponds to parseDelimitedFrom() in Google's
84
- * protobuf API.
85
- */
86
- bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
87
-
88
- #ifdef PB_ENABLE_MALLOC
89
- /* Release any allocated pointer fields. If you use dynamic allocation, you should
90
- * call this for any successfully decoded message when you are done with it. If
91
- * pb_decode() returns with an error, the message is already released.
92
- */
93
- void pb_release(const pb_field_t fields[], void *dest_struct);
94
- #endif
95
-
96
-
97
- /**************************************
98
- * Functions for manipulating streams *
99
- **************************************/
100
-
101
- /* Create an input stream for reading from a memory buffer.
102
- *
103
- * Alternatively, you can use a custom stream that reads directly from e.g.
104
- * a file or a network socket.
105
- */
106
- pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize);
107
-
108
- /* Function to read from a pb_istream_t. You can use this if you need to
109
- * read some custom header data, or to read data in field callbacks.
110
- */
111
- bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
112
-
113
-
114
- /************************************************
115
- * Helper functions for writing field callbacks *
116
- ************************************************/
117
-
118
- /* Decode the tag for the next field in the stream. Gives the wire type and
119
- * field tag. At end of the message, returns false and sets eof to true. */
120
- bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof);
121
-
122
- /* Skip the field payload data, given the wire type. */
123
- bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type);
124
-
125
- /* Decode an integer in the varint format. This works for bool, enum, int32,
126
- * int64, uint32 and uint64 field types. */
127
- bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest);
128
-
129
- /* Decode an integer in the zig-zagged svarint format. This works for sint32
130
- * and sint64. */
131
- bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest);
132
-
133
- /* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to
134
- * a 4-byte wide C variable. */
135
- bool pb_decode_fixed32(pb_istream_t *stream, void *dest);
136
-
137
- /* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to
138
- * a 8-byte wide C variable. */
139
- bool pb_decode_fixed64(pb_istream_t *stream, void *dest);
140
-
141
- /* Make a limited-length substream for reading a PB_WT_STRING field. */
142
- bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream);
143
- void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream);
144
-
145
- #ifdef __cplusplus
146
- } /* extern "C" */
147
- #endif
148
-
149
- #endif
@@ -1,696 +0,0 @@
1
- /* pb_encode.c -- encode a protobuf using minimal resources
2
- *
3
- * 2011 Petteri Aimonen <jpa@kapsi.fi>
4
- */
5
-
6
- #include "pb.h"
7
- #include "pb_encode.h"
8
- #include "pb_common.h"
9
-
10
- /* Use the GCC warn_unused_result attribute to check that all return values
11
- * are propagated correctly. On other compilers and gcc before 3.4.0 just
12
- * ignore the annotation.
13
- */
14
- #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
15
- #define checkreturn
16
- #else
17
- #define checkreturn __attribute__((warn_unused_result))
18
- #endif
19
-
20
- /**************************************
21
- * Declarations internal to this file *
22
- **************************************/
23
- typedef bool (*pb_encoder_t)(pb_ostream_t *stream, const pb_field_t *field, const void *src) checkreturn;
24
-
25
- static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
26
- static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field, const void *pData, size_t count, pb_encoder_t func);
27
- static bool checkreturn encode_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData);
28
- static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension);
29
- static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData);
30
- static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
31
- static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
32
- static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
33
- static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src);
34
- static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src);
35
- static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src);
36
- static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src);
37
- static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src);
38
-
39
- /* --- Function pointers to field encoders ---
40
- * Order in the array must match pb_action_t LTYPE numbering.
41
- */
42
- static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = {
43
- &pb_enc_varint,
44
- &pb_enc_uvarint,
45
- &pb_enc_svarint,
46
- &pb_enc_fixed32,
47
- &pb_enc_fixed64,
48
-
49
- &pb_enc_bytes,
50
- &pb_enc_string,
51
- &pb_enc_submessage,
52
- NULL, /* extensions */
53
- &pb_enc_bytes /* PB_LTYPE_FIXED_LENGTH_BYTES */
54
- };
55
-
56
- /*******************************
57
- * pb_ostream_t implementation *
58
- *******************************/
59
-
60
- static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
61
- {
62
- pb_byte_t *dest = (pb_byte_t*)stream->state;
63
- stream->state = dest + count;
64
-
65
- while (count--)
66
- *dest++ = *buf++;
67
-
68
- return true;
69
- }
70
-
71
- pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
72
- {
73
- pb_ostream_t stream;
74
- #ifdef PB_BUFFER_ONLY
75
- stream.callback = (void*)1; /* Just a marker value */
76
- #else
77
- stream.callback = &buf_write;
78
- #endif
79
- stream.state = buf;
80
- stream.max_size = bufsize;
81
- stream.bytes_written = 0;
82
- #ifndef PB_NO_ERRMSG
83
- stream.errmsg = NULL;
84
- #endif
85
- return stream;
86
- }
87
-
88
- bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
89
- {
90
- if (stream->callback != NULL)
91
- {
92
- if (stream->bytes_written + count > stream->max_size)
93
- PB_RETURN_ERROR(stream, "stream full");
94
-
95
- #ifdef PB_BUFFER_ONLY
96
- if (!buf_write(stream, buf, count))
97
- PB_RETURN_ERROR(stream, "io error");
98
- #else
99
- if (!stream->callback(stream, buf, count))
100
- PB_RETURN_ERROR(stream, "io error");
101
- #endif
102
- }
103
-
104
- stream->bytes_written += count;
105
- return true;
106
- }
107
-
108
- /*************************
109
- * Encode a single field *
110
- *************************/
111
-
112
- /* Encode a static array. Handles the size calculations and possible packing. */
113
- static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field,
114
- const void *pData, size_t count, pb_encoder_t func)
115
- {
116
- size_t i;
117
- const void *p;
118
- size_t size;
119
-
120
- if (count == 0)
121
- return true;
122
-
123
- if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size)
124
- PB_RETURN_ERROR(stream, "array max size exceeded");
125
-
126
- /* We always pack arrays if the datatype allows it. */
127
- if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
128
- {
129
- if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
130
- return false;
131
-
132
- /* Determine the total size of packed array. */
133
- if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
134
- {
135
- size = 4 * count;
136
- }
137
- else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
138
- {
139
- size = 8 * count;
140
- }
141
- else
142
- {
143
- pb_ostream_t sizestream = PB_OSTREAM_SIZING;
144
- p = pData;
145
- for (i = 0; i < count; i++)
146
- {
147
- if (!func(&sizestream, field, p))
148
- return false;
149
- p = (const char*)p + field->data_size;
150
- }
151
- size = sizestream.bytes_written;
152
- }
153
-
154
- if (!pb_encode_varint(stream, (uint64_t)size))
155
- return false;
156
-
157
- if (stream->callback == NULL)
158
- return pb_write(stream, NULL, size); /* Just sizing.. */
159
-
160
- /* Write the data */
161
- p = pData;
162
- for (i = 0; i < count; i++)
163
- {
164
- if (!func(stream, field, p))
165
- return false;
166
- p = (const char*)p + field->data_size;
167
- }
168
- }
169
- else
170
- {
171
- p = pData;
172
- for (i = 0; i < count; i++)
173
- {
174
- if (!pb_encode_tag_for_field(stream, field))
175
- return false;
176
-
177
- /* Normally the data is stored directly in the array entries, but
178
- * for pointer-type string and bytes fields, the array entries are
179
- * actually pointers themselves also. So we have to dereference once
180
- * more to get to the actual data. */
181
- if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
182
- (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
183
- PB_LTYPE(field->type) == PB_LTYPE_BYTES))
184
- {
185
- if (!func(stream, field, *(const void* const*)p))
186
- return false;
187
- }
188
- else
189
- {
190
- if (!func(stream, field, p))
191
- return false;
192
- }
193
- p = (const char*)p + field->data_size;
194
- }
195
- }
196
-
197
- return true;
198
- }
199
-
200
- /* Encode a field with static or pointer allocation, i.e. one whose data
201
- * is available to the encoder directly. */
202
- static bool checkreturn encode_basic_field(pb_ostream_t *stream,
203
- const pb_field_t *field, const void *pData)
204
- {
205
- pb_encoder_t func;
206
- const void *pSize;
207
- bool implicit_has = true;
208
-
209
- func = PB_ENCODERS[PB_LTYPE(field->type)];
210
-
211
- if (field->size_offset)
212
- pSize = (const char*)pData + field->size_offset;
213
- else
214
- pSize = &implicit_has;
215
-
216
- if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
217
- {
218
- /* pData is a pointer to the field, which contains pointer to
219
- * the data. If the 2nd pointer is NULL, it is interpreted as if
220
- * the has_field was false.
221
- */
222
-
223
- pData = *(const void* const*)pData;
224
- implicit_has = (pData != NULL);
225
- }
226
-
227
- switch (PB_HTYPE(field->type))
228
- {
229
- case PB_HTYPE_REQUIRED:
230
- if (!pData)
231
- PB_RETURN_ERROR(stream, "missing required field");
232
- if (!pb_encode_tag_for_field(stream, field))
233
- return false;
234
- if (!func(stream, field, pData))
235
- return false;
236
- break;
237
-
238
- case PB_HTYPE_OPTIONAL:
239
- if (*(const bool*)pSize)
240
- {
241
- if (!pb_encode_tag_for_field(stream, field))
242
- return false;
243
-
244
- if (!func(stream, field, pData))
245
- return false;
246
- }
247
- break;
248
-
249
- case PB_HTYPE_REPEATED:
250
- if (!encode_array(stream, field, pData, *(const pb_size_t*)pSize, func))
251
- return false;
252
- break;
253
-
254
- case PB_HTYPE_ONEOF:
255
- if (*(const pb_size_t*)pSize == field->tag)
256
- {
257
- if (!pb_encode_tag_for_field(stream, field))
258
- return false;
259
-
260
- if (!func(stream, field, pData))
261
- return false;
262
- }
263
- break;
264
-
265
- default:
266
- PB_RETURN_ERROR(stream, "invalid field type");
267
- }
268
-
269
- return true;
270
- }
271
-
272
- /* Encode a field with callback semantics. This means that a user function is
273
- * called to provide and encode the actual data. */
274
- static bool checkreturn encode_callback_field(pb_ostream_t *stream,
275
- const pb_field_t *field, const void *pData)
276
- {
277
- const pb_callback_t *callback = (const pb_callback_t*)pData;
278
-
279
- #ifdef PB_OLD_CALLBACK_STYLE
280
- const void *arg = callback->arg;
281
- #else
282
- void * const *arg = &(callback->arg);
283
- #endif
284
-
285
- if (callback->funcs.encode != NULL)
286
- {
287
- if (!callback->funcs.encode(stream, field, arg))
288
- PB_RETURN_ERROR(stream, "callback error");
289
- }
290
- return true;
291
- }
292
-
293
- /* Encode a single field of any callback or static type. */
294
- static bool checkreturn encode_field(pb_ostream_t *stream,
295
- const pb_field_t *field, const void *pData)
296
- {
297
- switch (PB_ATYPE(field->type))
298
- {
299
- case PB_ATYPE_STATIC:
300
- case PB_ATYPE_POINTER:
301
- return encode_basic_field(stream, field, pData);
302
-
303
- case PB_ATYPE_CALLBACK:
304
- return encode_callback_field(stream, field, pData);
305
-
306
- default:
307
- PB_RETURN_ERROR(stream, "invalid field type");
308
- }
309
- }
310
-
311
- /* Default handler for extension fields. Expects to have a pb_field_t
312
- * pointer in the extension->type->arg field. */
313
- static bool checkreturn default_extension_encoder(pb_ostream_t *stream,
314
- const pb_extension_t *extension)
315
- {
316
- const pb_field_t *field = (const pb_field_t*)extension->type->arg;
317
-
318
- if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
319
- {
320
- /* For pointer extensions, the pointer is stored directly
321
- * in the extension structure. This avoids having an extra
322
- * indirection. */
323
- return encode_field(stream, field, &extension->dest);
324
- }
325
- else
326
- {
327
- return encode_field(stream, field, extension->dest);
328
- }
329
- }
330
-
331
- /* Walk through all the registered extensions and give them a chance
332
- * to encode themselves. */
333
- static bool checkreturn encode_extension_field(pb_ostream_t *stream,
334
- const pb_field_t *field, const void *pData)
335
- {
336
- const pb_extension_t *extension = *(const pb_extension_t* const *)pData;
337
- PB_UNUSED(field);
338
-
339
- while (extension)
340
- {
341
- bool status;
342
- if (extension->type->encode)
343
- status = extension->type->encode(stream, extension);
344
- else
345
- status = default_extension_encoder(stream, extension);
346
-
347
- if (!status)
348
- return false;
349
-
350
- extension = extension->next;
351
- }
352
-
353
- return true;
354
- }
355
-
356
- /*********************
357
- * Encode all fields *
358
- *********************/
359
-
360
- static void *remove_const(const void *p)
361
- {
362
- /* Note: this casts away const, in order to use the common field iterator
363
- * logic for both encoding and decoding. */
364
- union {
365
- void *p1;
366
- const void *p2;
367
- } t;
368
- t.p2 = p;
369
- return t.p1;
370
- }
371
-
372
- bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
373
- {
374
- pb_field_iter_t iter;
375
- if (!pb_field_iter_begin(&iter, fields, remove_const(src_struct)))
376
- return true; /* Empty message type */
377
-
378
- do {
379
- if (PB_LTYPE(iter.pos->type) == PB_LTYPE_EXTENSION)
380
- {
381
- /* Special case for the extension field placeholder */
382
- if (!encode_extension_field(stream, iter.pos, iter.pData))
383
- return false;
384
- }
385
- else
386
- {
387
- /* Regular field */
388
- if (!encode_field(stream, iter.pos, iter.pData))
389
- return false;
390
- }
391
- } while (pb_field_iter_next(&iter));
392
-
393
- return true;
394
- }
395
-
396
- bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
397
- {
398
- return pb_encode_submessage(stream, fields, src_struct);
399
- }
400
-
401
- bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct)
402
- {
403
- pb_ostream_t stream = PB_OSTREAM_SIZING;
404
-
405
- if (!pb_encode(&stream, fields, src_struct))
406
- return false;
407
-
408
- *size = stream.bytes_written;
409
- return true;
410
- }
411
-
412
- /********************
413
- * Helper functions *
414
- ********************/
415
- bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value)
416
- {
417
- pb_byte_t buffer[10];
418
- size_t i = 0;
419
-
420
- if (value <= 0x7F)
421
- {
422
- pb_byte_t v = (pb_byte_t)value;
423
- return pb_write(stream, &v, 1);
424
- }
425
-
426
- while (value)
427
- {
428
- buffer[i] = (pb_byte_t)((value & 0x7F) | 0x80);
429
- value >>= 7;
430
- i++;
431
- }
432
- buffer[i-1] &= 0x7F; /* Unset top bit on last byte */
433
-
434
- return pb_write(stream, buffer, i);
435
- }
436
-
437
- bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value)
438
- {
439
- uint64_t zigzagged;
440
- if (value < 0)
441
- zigzagged = ~((uint64_t)value << 1);
442
- else
443
- zigzagged = (uint64_t)value << 1;
444
-
445
- return pb_encode_varint(stream, zigzagged);
446
- }
447
-
448
- bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
449
- {
450
- uint32_t val = *(const uint32_t*)value;
451
- pb_byte_t bytes[4];
452
- bytes[0] = (pb_byte_t)(val & 0xFF);
453
- bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
454
- bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
455
- bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
456
- return pb_write(stream, bytes, 4);
457
- }
458
-
459
- bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
460
- {
461
- uint64_t val = *(const uint64_t*)value;
462
- pb_byte_t bytes[8];
463
- bytes[0] = (pb_byte_t)(val & 0xFF);
464
- bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
465
- bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
466
- bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
467
- bytes[4] = (pb_byte_t)((val >> 32) & 0xFF);
468
- bytes[5] = (pb_byte_t)((val >> 40) & 0xFF);
469
- bytes[6] = (pb_byte_t)((val >> 48) & 0xFF);
470
- bytes[7] = (pb_byte_t)((val >> 56) & 0xFF);
471
- return pb_write(stream, bytes, 8);
472
- }
473
-
474
- bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
475
- {
476
- uint64_t tag = ((uint64_t)field_number << 3) | wiretype;
477
- return pb_encode_varint(stream, tag);
478
- }
479
-
480
- bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field)
481
- {
482
- pb_wire_type_t wiretype;
483
- switch (PB_LTYPE(field->type))
484
- {
485
- case PB_LTYPE_VARINT:
486
- case PB_LTYPE_UVARINT:
487
- case PB_LTYPE_SVARINT:
488
- wiretype = PB_WT_VARINT;
489
- break;
490
-
491
- case PB_LTYPE_FIXED32:
492
- wiretype = PB_WT_32BIT;
493
- break;
494
-
495
- case PB_LTYPE_FIXED64:
496
- wiretype = PB_WT_64BIT;
497
- break;
498
-
499
- case PB_LTYPE_BYTES:
500
- case PB_LTYPE_STRING:
501
- case PB_LTYPE_SUBMESSAGE:
502
- case PB_LTYPE_FIXED_LENGTH_BYTES:
503
- wiretype = PB_WT_STRING;
504
- break;
505
-
506
- default:
507
- PB_RETURN_ERROR(stream, "invalid field type");
508
- }
509
-
510
- return pb_encode_tag(stream, wiretype, field->tag);
511
- }
512
-
513
- bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
514
- {
515
- if (!pb_encode_varint(stream, (uint64_t)size))
516
- return false;
517
-
518
- return pb_write(stream, buffer, size);
519
- }
520
-
521
- bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
522
- {
523
- /* First calculate the message size using a non-writing substream. */
524
- pb_ostream_t substream = PB_OSTREAM_SIZING;
525
- size_t size;
526
- bool status;
527
-
528
- if (!pb_encode(&substream, fields, src_struct))
529
- {
530
- #ifndef PB_NO_ERRMSG
531
- stream->errmsg = substream.errmsg;
532
- #endif
533
- return false;
534
- }
535
-
536
- size = substream.bytes_written;
537
-
538
- if (!pb_encode_varint(stream, (uint64_t)size))
539
- return false;
540
-
541
- if (stream->callback == NULL)
542
- return pb_write(stream, NULL, size); /* Just sizing */
543
-
544
- if (stream->bytes_written + size > stream->max_size)
545
- PB_RETURN_ERROR(stream, "stream full");
546
-
547
- /* Use a substream to verify that a callback doesn't write more than
548
- * what it did the first time. */
549
- substream.callback = stream->callback;
550
- substream.state = stream->state;
551
- substream.max_size = size;
552
- substream.bytes_written = 0;
553
- #ifndef PB_NO_ERRMSG
554
- substream.errmsg = NULL;
555
- #endif
556
-
557
- status = pb_encode(&substream, fields, src_struct);
558
-
559
- stream->bytes_written += substream.bytes_written;
560
- stream->state = substream.state;
561
- #ifndef PB_NO_ERRMSG
562
- stream->errmsg = substream.errmsg;
563
- #endif
564
-
565
- if (substream.bytes_written != size)
566
- PB_RETURN_ERROR(stream, "submsg size changed");
567
-
568
- return status;
569
- }
570
-
571
- /* Field encoders */
572
-
573
- static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
574
- {
575
- int64_t value = 0;
576
-
577
- if (field->data_size == sizeof(int_least8_t))
578
- value = *(const int_least8_t*)src;
579
- else if (field->data_size == sizeof(int_least16_t))
580
- value = *(const int_least16_t*)src;
581
- else if (field->data_size == sizeof(int32_t))
582
- value = *(const int32_t*)src;
583
- else if (field->data_size == sizeof(int64_t))
584
- value = *(const int64_t*)src;
585
- else
586
- PB_RETURN_ERROR(stream, "invalid data_size");
587
-
588
- return pb_encode_varint(stream, (uint64_t)value);
589
- }
590
-
591
- static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
592
- {
593
- uint64_t value = 0;
594
-
595
- if (field->data_size == sizeof(uint_least8_t))
596
- value = *(const uint_least8_t*)src;
597
- else if (field->data_size == sizeof(uint_least16_t))
598
- value = *(const uint_least16_t*)src;
599
- else if (field->data_size == sizeof(uint32_t))
600
- value = *(const uint32_t*)src;
601
- else if (field->data_size == sizeof(uint64_t))
602
- value = *(const uint64_t*)src;
603
- else
604
- PB_RETURN_ERROR(stream, "invalid data_size");
605
-
606
- return pb_encode_varint(stream, value);
607
- }
608
-
609
- static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src)
610
- {
611
- int64_t value = 0;
612
-
613
- if (field->data_size == sizeof(int_least8_t))
614
- value = *(const int_least8_t*)src;
615
- else if (field->data_size == sizeof(int_least16_t))
616
- value = *(const int_least16_t*)src;
617
- else if (field->data_size == sizeof(int32_t))
618
- value = *(const int32_t*)src;
619
- else if (field->data_size == sizeof(int64_t))
620
- value = *(const int64_t*)src;
621
- else
622
- PB_RETURN_ERROR(stream, "invalid data_size");
623
-
624
- return pb_encode_svarint(stream, value);
625
- }
626
-
627
- static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src)
628
- {
629
- PB_UNUSED(field);
630
- return pb_encode_fixed64(stream, src);
631
- }
632
-
633
- static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src)
634
- {
635
- PB_UNUSED(field);
636
- return pb_encode_fixed32(stream, src);
637
- }
638
-
639
- static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
640
- {
641
- const pb_bytes_array_t *bytes = NULL;
642
-
643
- if (PB_LTYPE(field->type) == PB_LTYPE_FIXED_LENGTH_BYTES)
644
- return pb_encode_string(stream, (const pb_byte_t*)src, field->data_size);
645
-
646
- bytes = (const pb_bytes_array_t*)src;
647
-
648
- if (src == NULL)
649
- {
650
- /* Treat null pointer as an empty bytes field */
651
- return pb_encode_string(stream, NULL, 0);
652
- }
653
-
654
- if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
655
- PB_BYTES_ARRAY_T_ALLOCSIZE(bytes->size) > field->data_size)
656
- {
657
- PB_RETURN_ERROR(stream, "bytes size exceeded");
658
- }
659
-
660
- return pb_encode_string(stream, bytes->bytes, bytes->size);
661
- }
662
-
663
- static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
664
- {
665
- size_t size = 0;
666
- size_t max_size = field->data_size;
667
- const char *p = (const char*)src;
668
-
669
- if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
670
- max_size = (size_t)-1;
671
-
672
- if (src == NULL)
673
- {
674
- size = 0; /* Treat null pointer as an empty string */
675
- }
676
- else
677
- {
678
- /* strnlen() is not always available, so just use a loop */
679
- while (size < max_size && *p != '\0')
680
- {
681
- size++;
682
- p++;
683
- }
684
- }
685
-
686
- return pb_encode_string(stream, (const pb_byte_t*)src, size);
687
- }
688
-
689
- static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src)
690
- {
691
- if (field->ptr == NULL)
692
- PB_RETURN_ERROR(stream, "invalid field descriptor");
693
-
694
- return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src);
695
- }
696
-