grpc 1.23.1 → 1.24.0.pre1

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 +757 -477
  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 +134 -55
  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
@@ -0,0 +1,21 @@
1
+ /*
2
+ ** upb_decode: parsing into a upb_msg using a upb_msglayout.
3
+ */
4
+
5
+ #ifndef UPB_DECODE_H_
6
+ #define UPB_DECODE_H_
7
+
8
+ #include "upb/msg.h"
9
+
10
+ #ifdef __cplusplus
11
+ extern "C" {
12
+ #endif
13
+
14
+ bool upb_decode(const char *buf, size_t size, upb_msg *msg,
15
+ const upb_msglayout *l, upb_arena *arena);
16
+
17
+ #ifdef __cplusplus
18
+ } /* extern "C" */
19
+ #endif
20
+
21
+ #endif /* UPB_DECODE_H_ */
@@ -0,0 +1,378 @@
1
+ /* We encode backwards, to avoid pre-computing lengths (one-pass encode). */
2
+
3
+ #include "upb/encode.h"
4
+
5
+ #include <string.h>
6
+
7
+ #include "upb/msg.h"
8
+ #include "upb/upb.h"
9
+
10
+ #include "upb/port_def.inc"
11
+
12
+ #define UPB_PB_VARINT_MAX_LEN 10
13
+ #define CHK(x) do { if (!(x)) { return false; } } while(0)
14
+
15
+ static size_t upb_encode_varint(uint64_t val, char *buf) {
16
+ size_t i;
17
+ if (val < 128) { buf[0] = val; return 1; }
18
+ i = 0;
19
+ while (val) {
20
+ uint8_t byte = val & 0x7fU;
21
+ val >>= 7;
22
+ if (val) byte |= 0x80U;
23
+ buf[i++] = byte;
24
+ }
25
+ return i;
26
+ }
27
+
28
+ static uint32_t upb_zzencode_32(int32_t n) { return ((uint32_t)n << 1) ^ (n >> 31); }
29
+ static uint64_t upb_zzencode_64(int64_t n) { return ((uint64_t)n << 1) ^ (n >> 63); }
30
+
31
+ typedef struct {
32
+ upb_alloc *alloc;
33
+ char *buf, *ptr, *limit;
34
+ } upb_encstate;
35
+
36
+ static size_t upb_roundup_pow2(size_t bytes) {
37
+ size_t ret = 128;
38
+ while (ret < bytes) {
39
+ ret *= 2;
40
+ }
41
+ return ret;
42
+ }
43
+
44
+ static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
45
+ size_t old_size = e->limit - e->buf;
46
+ size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
47
+ char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size);
48
+ CHK(new_buf);
49
+
50
+ /* We want previous data at the end, realloc() put it at the beginning. */
51
+ if (old_size > 0) {
52
+ memmove(new_buf + new_size - old_size, e->buf, old_size);
53
+ }
54
+
55
+ e->ptr = new_buf + new_size - (e->limit - e->ptr);
56
+ e->limit = new_buf + new_size;
57
+ e->buf = new_buf;
58
+ return true;
59
+ }
60
+
61
+ /* Call to ensure that at least "bytes" bytes are available for writing at
62
+ * e->ptr. Returns false if the bytes could not be allocated. */
63
+ static bool upb_encode_reserve(upb_encstate *e, size_t bytes) {
64
+ CHK(UPB_LIKELY((size_t)(e->ptr - e->buf) >= bytes) ||
65
+ upb_encode_growbuffer(e, bytes));
66
+
67
+ e->ptr -= bytes;
68
+ return true;
69
+ }
70
+
71
+ /* Writes the given bytes to the buffer, handling reserve/advance. */
72
+ static bool upb_put_bytes(upb_encstate *e, const void *data, size_t len) {
73
+ CHK(upb_encode_reserve(e, len));
74
+ memcpy(e->ptr, data, len);
75
+ return true;
76
+ }
77
+
78
+ static bool upb_put_fixed64(upb_encstate *e, uint64_t val) {
79
+ /* TODO(haberman): byte-swap for big endian. */
80
+ return upb_put_bytes(e, &val, sizeof(uint64_t));
81
+ }
82
+
83
+ static bool upb_put_fixed32(upb_encstate *e, uint32_t val) {
84
+ /* TODO(haberman): byte-swap for big endian. */
85
+ return upb_put_bytes(e, &val, sizeof(uint32_t));
86
+ }
87
+
88
+ static bool upb_put_varint(upb_encstate *e, uint64_t val) {
89
+ size_t len;
90
+ char *start;
91
+ CHK(upb_encode_reserve(e, UPB_PB_VARINT_MAX_LEN));
92
+ len = upb_encode_varint(val, e->ptr);
93
+ start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
94
+ memmove(start, e->ptr, len);
95
+ e->ptr = start;
96
+ return true;
97
+ }
98
+
99
+ static bool upb_put_double(upb_encstate *e, double d) {
100
+ uint64_t u64;
101
+ UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
102
+ memcpy(&u64, &d, sizeof(uint64_t));
103
+ return upb_put_fixed64(e, u64);
104
+ }
105
+
106
+ static bool upb_put_float(upb_encstate *e, float d) {
107
+ uint32_t u32;
108
+ UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
109
+ memcpy(&u32, &d, sizeof(uint32_t));
110
+ return upb_put_fixed32(e, u32);
111
+ }
112
+
113
+ static uint32_t upb_readcase(const char *msg, const upb_msglayout_field *f) {
114
+ uint32_t ret;
115
+ uint32_t offset = ~f->presence;
116
+ memcpy(&ret, msg + offset, sizeof(ret));
117
+ return ret;
118
+ }
119
+
120
+ static bool upb_readhasbit(const char *msg, const upb_msglayout_field *f) {
121
+ uint32_t hasbit = f->presence;
122
+ UPB_ASSERT(f->presence > 0);
123
+ return msg[hasbit / 8] & (1 << (hasbit % 8));
124
+ }
125
+
126
+ static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) {
127
+ return upb_put_varint(e, (field_number << 3) | wire_type);
128
+ }
129
+
130
+ static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr,
131
+ size_t size) {
132
+ size_t bytes = arr->len * size;
133
+ return upb_put_bytes(e, arr->data, bytes) && upb_put_varint(e, bytes);
134
+ }
135
+
136
+ bool upb_encode_message(upb_encstate *e, const char *msg,
137
+ const upb_msglayout *m, size_t *size);
138
+
139
+ static bool upb_encode_array(upb_encstate *e, const char *field_mem,
140
+ const upb_msglayout *m,
141
+ const upb_msglayout_field *f) {
142
+ const upb_array *arr = *(const upb_array**)field_mem;
143
+
144
+ if (arr == NULL || arr->len == 0) {
145
+ return true;
146
+ }
147
+
148
+ #define VARINT_CASE(ctype, encode) { \
149
+ ctype *start = arr->data; \
150
+ ctype *ptr = start + arr->len; \
151
+ size_t pre_len = e->limit - e->ptr; \
152
+ do { \
153
+ ptr--; \
154
+ CHK(upb_put_varint(e, encode)); \
155
+ } while (ptr != start); \
156
+ CHK(upb_put_varint(e, e->limit - e->ptr - pre_len)); \
157
+ } \
158
+ break; \
159
+ do { ; } while(0)
160
+
161
+ switch (f->descriptortype) {
162
+ case UPB_DESCRIPTOR_TYPE_DOUBLE:
163
+ CHK(upb_put_fixedarray(e, arr, sizeof(double)));
164
+ break;
165
+ case UPB_DESCRIPTOR_TYPE_FLOAT:
166
+ CHK(upb_put_fixedarray(e, arr, sizeof(float)));
167
+ break;
168
+ case UPB_DESCRIPTOR_TYPE_SFIXED64:
169
+ case UPB_DESCRIPTOR_TYPE_FIXED64:
170
+ CHK(upb_put_fixedarray(e, arr, sizeof(uint64_t)));
171
+ break;
172
+ case UPB_DESCRIPTOR_TYPE_FIXED32:
173
+ case UPB_DESCRIPTOR_TYPE_SFIXED32:
174
+ CHK(upb_put_fixedarray(e, arr, sizeof(uint32_t)));
175
+ break;
176
+ case UPB_DESCRIPTOR_TYPE_INT64:
177
+ case UPB_DESCRIPTOR_TYPE_UINT64:
178
+ VARINT_CASE(uint64_t, *ptr);
179
+ case UPB_DESCRIPTOR_TYPE_UINT32:
180
+ VARINT_CASE(uint32_t, *ptr);
181
+ case UPB_DESCRIPTOR_TYPE_INT32:
182
+ case UPB_DESCRIPTOR_TYPE_ENUM:
183
+ VARINT_CASE(int32_t, (int64_t)*ptr);
184
+ case UPB_DESCRIPTOR_TYPE_BOOL:
185
+ VARINT_CASE(bool, *ptr);
186
+ case UPB_DESCRIPTOR_TYPE_SINT32:
187
+ VARINT_CASE(int32_t, upb_zzencode_32(*ptr));
188
+ case UPB_DESCRIPTOR_TYPE_SINT64:
189
+ VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
190
+ case UPB_DESCRIPTOR_TYPE_STRING:
191
+ case UPB_DESCRIPTOR_TYPE_BYTES: {
192
+ upb_strview *start = arr->data;
193
+ upb_strview *ptr = start + arr->len;
194
+ do {
195
+ ptr--;
196
+ CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
197
+ upb_put_varint(e, ptr->size) &&
198
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
199
+ } while (ptr != start);
200
+ return true;
201
+ }
202
+ case UPB_DESCRIPTOR_TYPE_GROUP: {
203
+ void **start = arr->data;
204
+ void **ptr = start + arr->len;
205
+ const upb_msglayout *subm = m->submsgs[f->submsg_index];
206
+ do {
207
+ size_t size;
208
+ ptr--;
209
+ CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
210
+ upb_encode_message(e, *ptr, subm, &size) &&
211
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP));
212
+ } while (ptr != start);
213
+ return true;
214
+ }
215
+ case UPB_DESCRIPTOR_TYPE_MESSAGE: {
216
+ void **start = arr->data;
217
+ void **ptr = start + arr->len;
218
+ const upb_msglayout *subm = m->submsgs[f->submsg_index];
219
+ do {
220
+ size_t size;
221
+ ptr--;
222
+ CHK(upb_encode_message(e, *ptr, subm, &size) &&
223
+ upb_put_varint(e, size) &&
224
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
225
+ } while (ptr != start);
226
+ return true;
227
+ }
228
+ }
229
+ #undef VARINT_CASE
230
+
231
+ /* We encode all primitive arrays as packed, regardless of what was specified
232
+ * in the .proto file. Could special case 1-sized arrays. */
233
+ CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
234
+ return true;
235
+ }
236
+
237
+ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem,
238
+ const upb_msglayout *m,
239
+ const upb_msglayout_field *f,
240
+ bool skip_zero_value) {
241
+ #define CASE(ctype, type, wire_type, encodeval) do { \
242
+ ctype val = *(ctype*)field_mem; \
243
+ if (skip_zero_value && val == 0) { \
244
+ return true; \
245
+ } \
246
+ return upb_put_ ## type(e, encodeval) && \
247
+ upb_put_tag(e, f->number, wire_type); \
248
+ } while(0)
249
+
250
+ switch (f->descriptortype) {
251
+ case UPB_DESCRIPTOR_TYPE_DOUBLE:
252
+ CASE(double, double, UPB_WIRE_TYPE_64BIT, val);
253
+ case UPB_DESCRIPTOR_TYPE_FLOAT:
254
+ CASE(float, float, UPB_WIRE_TYPE_32BIT, val);
255
+ case UPB_DESCRIPTOR_TYPE_INT64:
256
+ case UPB_DESCRIPTOR_TYPE_UINT64:
257
+ CASE(uint64_t, varint, UPB_WIRE_TYPE_VARINT, val);
258
+ case UPB_DESCRIPTOR_TYPE_UINT32:
259
+ CASE(uint32_t, varint, UPB_WIRE_TYPE_VARINT, val);
260
+ case UPB_DESCRIPTOR_TYPE_INT32:
261
+ case UPB_DESCRIPTOR_TYPE_ENUM:
262
+ CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, (int64_t)val);
263
+ case UPB_DESCRIPTOR_TYPE_SFIXED64:
264
+ case UPB_DESCRIPTOR_TYPE_FIXED64:
265
+ CASE(uint64_t, fixed64, UPB_WIRE_TYPE_64BIT, val);
266
+ case UPB_DESCRIPTOR_TYPE_FIXED32:
267
+ case UPB_DESCRIPTOR_TYPE_SFIXED32:
268
+ CASE(uint32_t, fixed32, UPB_WIRE_TYPE_32BIT, val);
269
+ case UPB_DESCRIPTOR_TYPE_BOOL:
270
+ CASE(bool, varint, UPB_WIRE_TYPE_VARINT, val);
271
+ case UPB_DESCRIPTOR_TYPE_SINT32:
272
+ CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_32(val));
273
+ case UPB_DESCRIPTOR_TYPE_SINT64:
274
+ CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
275
+ case UPB_DESCRIPTOR_TYPE_STRING:
276
+ case UPB_DESCRIPTOR_TYPE_BYTES: {
277
+ upb_strview view = *(upb_strview*)field_mem;
278
+ if (skip_zero_value && view.size == 0) {
279
+ return true;
280
+ }
281
+ return upb_put_bytes(e, view.data, view.size) &&
282
+ upb_put_varint(e, view.size) &&
283
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
284
+ }
285
+ case UPB_DESCRIPTOR_TYPE_GROUP: {
286
+ size_t size;
287
+ void *submsg = *(void **)field_mem;
288
+ const upb_msglayout *subm = m->submsgs[f->submsg_index];
289
+ if (submsg == NULL) {
290
+ return true;
291
+ }
292
+ return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
293
+ upb_encode_message(e, submsg, subm, &size) &&
294
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
295
+ }
296
+ case UPB_DESCRIPTOR_TYPE_MESSAGE: {
297
+ size_t size;
298
+ void *submsg = *(void **)field_mem;
299
+ const upb_msglayout *subm = m->submsgs[f->submsg_index];
300
+ if (submsg == NULL) {
301
+ return true;
302
+ }
303
+ return upb_encode_message(e, submsg, subm, &size) &&
304
+ upb_put_varint(e, size) &&
305
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
306
+ }
307
+ }
308
+ #undef CASE
309
+ UPB_UNREACHABLE();
310
+ }
311
+
312
+ bool upb_encode_message(upb_encstate *e, const char *msg,
313
+ const upb_msglayout *m, size_t *size) {
314
+ int i;
315
+ size_t pre_len = e->limit - e->ptr;
316
+ const char *unknown;
317
+ size_t unknown_size;
318
+
319
+ for (i = m->field_count - 1; i >= 0; i--) {
320
+ const upb_msglayout_field *f = &m->fields[i];
321
+
322
+ if (f->label == UPB_LABEL_REPEATED) {
323
+ CHK(upb_encode_array(e, msg + f->offset, m, f));
324
+ } else {
325
+ bool skip_empty = false;
326
+ if (f->presence == 0) {
327
+ /* Proto3 presence. */
328
+ skip_empty = true;
329
+ } else if (f->presence > 0) {
330
+ /* Proto2 presence: hasbit. */
331
+ if (!upb_readhasbit(msg, f)) {
332
+ continue;
333
+ }
334
+ } else {
335
+ /* Field is in a oneof. */
336
+ if (upb_readcase(msg, f) != f->number) {
337
+ continue;
338
+ }
339
+ }
340
+ CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty));
341
+ }
342
+ }
343
+
344
+ unknown = upb_msg_getunknown(msg, &unknown_size);
345
+
346
+ if (unknown) {
347
+ upb_put_bytes(e, unknown, unknown_size);
348
+ }
349
+
350
+ *size = (e->limit - e->ptr) - pre_len;
351
+ return true;
352
+ }
353
+
354
+ char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena,
355
+ size_t *size) {
356
+ upb_encstate e;
357
+ e.alloc = upb_arena_alloc(arena);
358
+ e.buf = NULL;
359
+ e.limit = NULL;
360
+ e.ptr = NULL;
361
+
362
+ if (!upb_encode_message(&e, msg, m, size)) {
363
+ *size = 0;
364
+ return NULL;
365
+ }
366
+
367
+ *size = e.limit - e.ptr;
368
+
369
+ if (*size == 0) {
370
+ static char ch;
371
+ return &ch;
372
+ } else {
373
+ UPB_ASSERT(e.ptr);
374
+ return e.ptr;
375
+ }
376
+ }
377
+
378
+ #undef CHK
@@ -0,0 +1,21 @@
1
+ /*
2
+ ** upb_encode: parsing into a upb_msg using a upb_msglayout.
3
+ */
4
+
5
+ #ifndef UPB_ENCODE_H_
6
+ #define UPB_ENCODE_H_
7
+
8
+ #include "upb/msg.h"
9
+
10
+ #ifdef __cplusplus
11
+ extern "C" {
12
+ #endif
13
+
14
+ char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena,
15
+ size_t *size);
16
+
17
+ #ifdef __cplusplus
18
+ } /* extern "C" */
19
+ #endif
20
+
21
+ #endif /* UPB_ENCODE_H_ */
@@ -0,0 +1,105 @@
1
+ /*
2
+ ** Functions for use by generated code. These are not public and users must
3
+ ** not call them directly.
4
+ */
5
+
6
+ #ifndef UPB_GENERATED_UTIL_H_
7
+ #define UPB_GENERATED_UTIL_H_
8
+
9
+ #include <stdint.h>
10
+ #include "upb/msg.h"
11
+
12
+ #include "upb/port_def.inc"
13
+
14
+ #define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs)
15
+
16
+ UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs,
17
+ size_t *size) {
18
+ const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*);
19
+ if (arr) {
20
+ if (size) *size = arr->len;
21
+ return arr->data;
22
+ } else {
23
+ if (size) *size = 0;
24
+ return NULL;
25
+ }
26
+ }
27
+
28
+ UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs,
29
+ size_t *size) {
30
+ upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
31
+ if (arr) {
32
+ if (size) *size = arr->len;
33
+ return arr->data;
34
+ } else {
35
+ if (size) *size = 0;
36
+ return NULL;
37
+ }
38
+ }
39
+
40
+ /* TODO(haberman): this is a mess. It will improve when upb_array no longer
41
+ * carries reflective state (type, elem_size). */
42
+ UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
43
+ size_t elem_size,
44
+ upb_fieldtype_t type,
45
+ upb_arena *arena) {
46
+ upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
47
+
48
+ if (!arr) {
49
+ arr = upb_array_new(arena);
50
+ if (!arr) return NULL;
51
+ *PTR_AT(msg, ofs, upb_array*) = arr;
52
+ }
53
+
54
+ if (size > arr->size) {
55
+ size_t new_size = UPB_MAX(arr->size, 4);
56
+ size_t old_bytes = arr->size * elem_size;
57
+ size_t new_bytes;
58
+ while (new_size < size) new_size *= 2;
59
+ new_bytes = new_size * elem_size;
60
+ arr->data = upb_arena_realloc(arena, arr->data, old_bytes, new_bytes);
61
+ if (!arr->data) {
62
+ return NULL;
63
+ }
64
+ arr->size = new_size;
65
+ }
66
+
67
+ arr->len = size;
68
+ return arr->data;
69
+ }
70
+
71
+ UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
72
+ size_t elem_size,
73
+ upb_fieldtype_t type,
74
+ const void *value,
75
+ upb_arena *arena) {
76
+ upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
77
+ size_t i = arr ? arr->len : 0;
78
+ void *data =
79
+ _upb_array_resize_accessor(msg, ofs, i + 1, elem_size, type, arena);
80
+ if (!data) return false;
81
+ memcpy(PTR_AT(data, i * elem_size, char), value, elem_size);
82
+ return true;
83
+ }
84
+
85
+ UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) {
86
+ return (*PTR_AT(msg, idx / 8, const char) & (1 << (idx % 8))) != 0;
87
+ }
88
+
89
+ UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) {
90
+ return (*PTR_AT(msg, idx / 8, char)) |= (char)(1 << (idx % 8));
91
+ }
92
+
93
+ UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) {
94
+ return (*PTR_AT(msg, idx / 8, char)) &= (char)(~(1 << (idx % 8)));
95
+ }
96
+
97
+ UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) {
98
+ return *PTR_AT(msg, case_ofs, int32_t) == num;
99
+ }
100
+
101
+ #undef PTR_AT
102
+
103
+ #include "upb/port_undef.inc"
104
+
105
+ #endif /* UPB_GENERATED_UTIL_H_ */