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,97 +0,0 @@
1
- /* pb_common.c: Common support functions for pb_encode.c and pb_decode.c.
2
- *
3
- * 2014 Petteri Aimonen <jpa@kapsi.fi>
4
- */
5
-
6
- #include "pb_common.h"
7
-
8
- bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_field_t *fields, void *dest_struct)
9
- {
10
- iter->start = fields;
11
- iter->pos = fields;
12
- iter->required_field_index = 0;
13
- iter->dest_struct = dest_struct;
14
- iter->pData = (char*)dest_struct + iter->pos->data_offset;
15
- iter->pSize = (char*)iter->pData + iter->pos->size_offset;
16
-
17
- return (iter->pos->tag != 0);
18
- }
19
-
20
- bool pb_field_iter_next(pb_field_iter_t *iter)
21
- {
22
- const pb_field_t *prev_field = iter->pos;
23
-
24
- if (prev_field->tag == 0)
25
- {
26
- /* Handle empty message types, where the first field is already the terminator.
27
- * In other cases, the iter->pos never points to the terminator. */
28
- return false;
29
- }
30
-
31
- iter->pos++;
32
-
33
- if (iter->pos->tag == 0)
34
- {
35
- /* Wrapped back to beginning, reinitialize */
36
- (void)pb_field_iter_begin(iter, iter->start, iter->dest_struct);
37
- return false;
38
- }
39
- else
40
- {
41
- /* Increment the pointers based on previous field size */
42
- size_t prev_size = prev_field->data_size;
43
-
44
- if (PB_HTYPE(prev_field->type) == PB_HTYPE_ONEOF &&
45
- PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
46
- {
47
- /* Don't advance pointers inside unions */
48
- prev_size = 0;
49
- iter->pData = (char*)iter->pData - prev_field->data_offset;
50
- }
51
- else if (PB_ATYPE(prev_field->type) == PB_ATYPE_STATIC &&
52
- PB_HTYPE(prev_field->type) == PB_HTYPE_REPEATED)
53
- {
54
- /* In static arrays, the data_size tells the size of a single entry and
55
- * array_size is the number of entries */
56
- prev_size *= prev_field->array_size;
57
- }
58
- else if (PB_ATYPE(prev_field->type) == PB_ATYPE_POINTER)
59
- {
60
- /* Pointer fields always have a constant size in the main structure.
61
- * The data_size only applies to the dynamically allocated area. */
62
- prev_size = sizeof(void*);
63
- }
64
-
65
- if (PB_HTYPE(prev_field->type) == PB_HTYPE_REQUIRED)
66
- {
67
- /* Count the required fields, in order to check their presence in the
68
- * decoder. */
69
- iter->required_field_index++;
70
- }
71
-
72
- iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset;
73
- iter->pSize = (char*)iter->pData + iter->pos->size_offset;
74
- return true;
75
- }
76
- }
77
-
78
- bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag)
79
- {
80
- const pb_field_t *start = iter->pos;
81
-
82
- do {
83
- if (iter->pos->tag == tag &&
84
- PB_LTYPE(iter->pos->type) != PB_LTYPE_EXTENSION)
85
- {
86
- /* Found the wanted field */
87
- return true;
88
- }
89
-
90
- (void)pb_field_iter_next(iter);
91
- } while (iter->pos != start);
92
-
93
- /* Searched all the way back to start, and found nothing. */
94
- return false;
95
- }
96
-
97
-
@@ -1,42 +0,0 @@
1
- /* pb_common.h: Common support functions for pb_encode.c and pb_decode.c.
2
- * These functions are rarely needed by applications directly.
3
- */
4
-
5
- #ifndef PB_COMMON_H_INCLUDED
6
- #define PB_COMMON_H_INCLUDED
7
-
8
- #include "pb.h"
9
-
10
- #ifdef __cplusplus
11
- extern "C" {
12
- #endif
13
-
14
- /* Iterator for pb_field_t list */
15
- struct pb_field_iter_s {
16
- const pb_field_t *start; /* Start of the pb_field_t array */
17
- const pb_field_t *pos; /* Current position of the iterator */
18
- unsigned required_field_index; /* Zero-based index that counts only the required fields */
19
- void *dest_struct; /* Pointer to start of the structure */
20
- void *pData; /* Pointer to current field value */
21
- void *pSize; /* Pointer to count/has field */
22
- };
23
- typedef struct pb_field_iter_s pb_field_iter_t;
24
-
25
- /* Initialize the field iterator structure to beginning.
26
- * Returns false if the message type is empty. */
27
- bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_field_t *fields, void *dest_struct);
28
-
29
- /* Advance the iterator to the next field.
30
- * Returns false when the iterator wraps back to the first field. */
31
- bool pb_field_iter_next(pb_field_iter_t *iter);
32
-
33
- /* Advance the iterator until it points at a field with the given tag.
34
- * Returns false if no such field exists. */
35
- bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag);
36
-
37
- #ifdef __cplusplus
38
- } /* extern "C" */
39
- #endif
40
-
41
- #endif
42
-
@@ -1,1347 +0,0 @@
1
- /* pb_decode.c -- decode a protobuf using minimal resources
2
- *
3
- * 2011 Petteri Aimonen <jpa@kapsi.fi>
4
- */
5
-
6
- /* Use the GCC warn_unused_result attribute to check that all return values
7
- * are propagated correctly. On other compilers and gcc before 3.4.0 just
8
- * ignore the annotation.
9
- */
10
- #if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
11
- #define checkreturn
12
- #else
13
- #define checkreturn __attribute__((warn_unused_result))
14
- #endif
15
-
16
- #include "pb.h"
17
- #include "pb_decode.h"
18
- #include "pb_common.h"
19
-
20
- /**************************************
21
- * Declarations internal to this file *
22
- **************************************/
23
-
24
- typedef bool (*pb_decoder_t)(pb_istream_t *stream, const pb_field_t *field, void *dest) checkreturn;
25
-
26
- static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
27
- static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
28
- static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size);
29
- static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
30
- static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
31
- static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter);
32
- static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension);
33
- static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
34
- static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter);
35
- static bool checkreturn find_extension_field(pb_field_iter_t *iter);
36
- static void pb_field_set_to_default(pb_field_iter_t *iter);
37
- static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct);
38
- static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
39
- static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
40
- static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
41
- static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
42
- static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
43
- static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
44
- static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
45
- static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
46
- static bool checkreturn pb_skip_varint(pb_istream_t *stream);
47
- static bool checkreturn pb_skip_string(pb_istream_t *stream);
48
-
49
- #ifdef PB_ENABLE_MALLOC
50
- static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
51
- static bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter);
52
- static void pb_release_single_field(const pb_field_iter_t *iter);
53
- #endif
54
-
55
- /* --- Function pointers to field decoders ---
56
- * Order in the array must match pb_action_t LTYPE numbering.
57
- */
58
- static const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
59
- &pb_dec_varint,
60
- &pb_dec_uvarint,
61
- &pb_dec_svarint,
62
- &pb_dec_fixed32,
63
- &pb_dec_fixed64,
64
-
65
- &pb_dec_bytes,
66
- &pb_dec_string,
67
- &pb_dec_submessage,
68
- NULL, /* extensions */
69
- &pb_dec_bytes /* PB_LTYPE_FIXED_LENGTH_BYTES */
70
- };
71
-
72
- /*******************************
73
- * pb_istream_t implementation *
74
- *******************************/
75
-
76
- static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
77
- {
78
- const pb_byte_t *source = (const pb_byte_t*)stream->state;
79
- stream->state = (pb_byte_t*)stream->state + count;
80
-
81
- if (buf != NULL)
82
- {
83
- while (count--)
84
- *buf++ = *source++;
85
- }
86
-
87
- return true;
88
- }
89
-
90
- bool checkreturn pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
91
- {
92
- #ifndef PB_BUFFER_ONLY
93
- if (buf == NULL && stream->callback != buf_read)
94
- {
95
- /* Skip input bytes */
96
- pb_byte_t tmp[16];
97
- while (count > 16)
98
- {
99
- if (!pb_read(stream, tmp, 16))
100
- return false;
101
-
102
- count -= 16;
103
- }
104
-
105
- return pb_read(stream, tmp, count);
106
- }
107
- #endif
108
-
109
- if (stream->bytes_left < count)
110
- PB_RETURN_ERROR(stream, "end-of-stream");
111
-
112
- #ifndef PB_BUFFER_ONLY
113
- if (!stream->callback(stream, buf, count))
114
- PB_RETURN_ERROR(stream, "io error");
115
- #else
116
- if (!buf_read(stream, buf, count))
117
- return false;
118
- #endif
119
-
120
- stream->bytes_left -= count;
121
- return true;
122
- }
123
-
124
- /* Read a single byte from input stream. buf may not be NULL.
125
- * This is an optimization for the varint decoding. */
126
- static bool checkreturn pb_readbyte(pb_istream_t *stream, pb_byte_t *buf)
127
- {
128
- if (stream->bytes_left == 0)
129
- PB_RETURN_ERROR(stream, "end-of-stream");
130
-
131
- #ifndef PB_BUFFER_ONLY
132
- if (!stream->callback(stream, buf, 1))
133
- PB_RETURN_ERROR(stream, "io error");
134
- #else
135
- *buf = *(const pb_byte_t*)stream->state;
136
- stream->state = (pb_byte_t*)stream->state + 1;
137
- #endif
138
-
139
- stream->bytes_left--;
140
-
141
- return true;
142
- }
143
-
144
- pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize)
145
- {
146
- pb_istream_t stream;
147
- /* Cast away the const from buf without a compiler error. We are
148
- * careful to use it only in a const manner in the callbacks.
149
- */
150
- union {
151
- void *state;
152
- const void *c_state;
153
- } state;
154
- #ifdef PB_BUFFER_ONLY
155
- stream.callback = NULL;
156
- #else
157
- stream.callback = &buf_read;
158
- #endif
159
- state.c_state = buf;
160
- stream.state = state.state;
161
- stream.bytes_left = bufsize;
162
- #ifndef PB_NO_ERRMSG
163
- stream.errmsg = NULL;
164
- #endif
165
- return stream;
166
- }
167
-
168
- /********************
169
- * Helper functions *
170
- ********************/
171
-
172
- static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
173
- {
174
- pb_byte_t byte;
175
- uint32_t result;
176
-
177
- if (!pb_readbyte(stream, &byte))
178
- return false;
179
-
180
- if ((byte & 0x80) == 0)
181
- {
182
- /* Quick case, 1 byte value */
183
- result = byte;
184
- }
185
- else
186
- {
187
- /* Multibyte case */
188
- uint_fast8_t bitpos = 7;
189
- result = byte & 0x7F;
190
-
191
- do
192
- {
193
- if (bitpos >= 32)
194
- PB_RETURN_ERROR(stream, "varint overflow");
195
-
196
- if (!pb_readbyte(stream, &byte))
197
- return false;
198
-
199
- result |= (uint32_t)(byte & 0x7F) << bitpos;
200
- bitpos = (uint_fast8_t)(bitpos + 7);
201
- } while (byte & 0x80);
202
- }
203
-
204
- *dest = result;
205
- return true;
206
- }
207
-
208
- bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
209
- {
210
- pb_byte_t byte;
211
- uint_fast8_t bitpos = 0;
212
- uint64_t result = 0;
213
-
214
- do
215
- {
216
- if (bitpos >= 64)
217
- PB_RETURN_ERROR(stream, "varint overflow");
218
-
219
- if (!pb_readbyte(stream, &byte))
220
- return false;
221
-
222
- result |= (uint64_t)(byte & 0x7F) << bitpos;
223
- bitpos = (uint_fast8_t)(bitpos + 7);
224
- } while (byte & 0x80);
225
-
226
- *dest = result;
227
- return true;
228
- }
229
-
230
- bool checkreturn pb_skip_varint(pb_istream_t *stream)
231
- {
232
- pb_byte_t byte;
233
- do
234
- {
235
- if (!pb_read(stream, &byte, 1))
236
- return false;
237
- } while (byte & 0x80);
238
- return true;
239
- }
240
-
241
- bool checkreturn pb_skip_string(pb_istream_t *stream)
242
- {
243
- uint32_t length;
244
- if (!pb_decode_varint32(stream, &length))
245
- return false;
246
-
247
- return pb_read(stream, NULL, length);
248
- }
249
-
250
- bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
251
- {
252
- uint32_t temp;
253
- *eof = false;
254
- *wire_type = (pb_wire_type_t) 0;
255
- *tag = 0;
256
-
257
- if (!pb_decode_varint32(stream, &temp))
258
- {
259
- if (stream->bytes_left == 0)
260
- *eof = true;
261
-
262
- return false;
263
- }
264
-
265
- if (temp == 0)
266
- {
267
- *eof = true; /* Special feature: allow 0-terminated messages. */
268
- return false;
269
- }
270
-
271
- *tag = temp >> 3;
272
- *wire_type = (pb_wire_type_t)(temp & 7);
273
- return true;
274
- }
275
-
276
- bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
277
- {
278
- switch (wire_type)
279
- {
280
- case PB_WT_VARINT: return pb_skip_varint(stream);
281
- case PB_WT_64BIT: return pb_read(stream, NULL, 8);
282
- case PB_WT_STRING: return pb_skip_string(stream);
283
- case PB_WT_32BIT: return pb_read(stream, NULL, 4);
284
- default: PB_RETURN_ERROR(stream, "invalid wire_type");
285
- }
286
- }
287
-
288
- /* Read a raw value to buffer, for the purpose of passing it to callback as
289
- * a substream. Size is maximum size on call, and actual size on return.
290
- */
291
- static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size)
292
- {
293
- size_t max_size = *size;
294
- switch (wire_type)
295
- {
296
- case PB_WT_VARINT:
297
- *size = 0;
298
- do
299
- {
300
- (*size)++;
301
- if (*size > max_size) return false;
302
- if (!pb_read(stream, buf, 1)) return false;
303
- } while (*buf++ & 0x80);
304
- return true;
305
-
306
- case PB_WT_64BIT:
307
- *size = 8;
308
- return pb_read(stream, buf, 8);
309
-
310
- case PB_WT_32BIT:
311
- *size = 4;
312
- return pb_read(stream, buf, 4);
313
-
314
- default: PB_RETURN_ERROR(stream, "invalid wire_type");
315
- }
316
- }
317
-
318
- /* Decode string length from stream and return a substream with limited length.
319
- * Remember to close the substream using pb_close_string_substream().
320
- */
321
- bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
322
- {
323
- uint32_t size;
324
- if (!pb_decode_varint32(stream, &size))
325
- return false;
326
-
327
- *substream = *stream;
328
- if (substream->bytes_left < size)
329
- PB_RETURN_ERROR(stream, "parent stream too short");
330
-
331
- substream->bytes_left = size;
332
- stream->bytes_left -= size;
333
- return true;
334
- }
335
-
336
- void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
337
- {
338
- stream->state = substream->state;
339
-
340
- #ifndef PB_NO_ERRMSG
341
- stream->errmsg = substream->errmsg;
342
- #endif
343
- }
344
-
345
- /*************************
346
- * Decode a single field *
347
- *************************/
348
-
349
- static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
350
- {
351
- pb_type_t type;
352
- pb_decoder_t func;
353
-
354
- type = iter->pos->type;
355
- func = PB_DECODERS[PB_LTYPE(type)];
356
-
357
- switch (PB_HTYPE(type))
358
- {
359
- case PB_HTYPE_REQUIRED:
360
- return func(stream, iter->pos, iter->pData);
361
-
362
- case PB_HTYPE_OPTIONAL:
363
- *(bool*)iter->pSize = true;
364
- return func(stream, iter->pos, iter->pData);
365
-
366
- case PB_HTYPE_REPEATED:
367
- if (wire_type == PB_WT_STRING
368
- && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
369
- {
370
- /* Packed array */
371
- bool status = true;
372
- pb_size_t *size = (pb_size_t*)iter->pSize;
373
- pb_istream_t substream;
374
- if (!pb_make_string_substream(stream, &substream))
375
- return false;
376
-
377
- while (substream.bytes_left > 0 && *size < iter->pos->array_size)
378
- {
379
- void *pItem = (char*)iter->pData + iter->pos->data_size * (*size);
380
- if (!func(&substream, iter->pos, pItem))
381
- {
382
- status = false;
383
- break;
384
- }
385
- (*size)++;
386
- }
387
- pb_close_string_substream(stream, &substream);
388
-
389
- if (substream.bytes_left != 0)
390
- PB_RETURN_ERROR(stream, "array overflow");
391
-
392
- return status;
393
- }
394
- else
395
- {
396
- /* Repeated field */
397
- pb_size_t *size = (pb_size_t*)iter->pSize;
398
- void *pItem = (char*)iter->pData + iter->pos->data_size * (*size);
399
- if (*size >= iter->pos->array_size)
400
- PB_RETURN_ERROR(stream, "array overflow");
401
-
402
- (*size)++;
403
- return func(stream, iter->pos, pItem);
404
- }
405
-
406
- case PB_HTYPE_ONEOF:
407
- *(pb_size_t*)iter->pSize = iter->pos->tag;
408
- if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
409
- {
410
- /* We memset to zero so that any callbacks are set to NULL.
411
- * Then set any default values. */
412
- memset(iter->pData, 0, iter->pos->data_size);
413
- pb_message_set_to_defaults((const pb_field_t*)iter->pos->ptr, iter->pData);
414
- }
415
- return func(stream, iter->pos, iter->pData);
416
-
417
- default:
418
- PB_RETURN_ERROR(stream, "invalid field type");
419
- }
420
- }
421
-
422
- #ifdef PB_ENABLE_MALLOC
423
- /* Allocate storage for the field and store the pointer at iter->pData.
424
- * array_size is the number of entries to reserve in an array.
425
- * Zero size is not allowed, use pb_free() for releasing.
426
- */
427
- static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
428
- {
429
- void *ptr = *(void**)pData;
430
-
431
- if (data_size == 0 || array_size == 0)
432
- PB_RETURN_ERROR(stream, "invalid size");
433
-
434
- /* Check for multiplication overflows.
435
- * This code avoids the costly division if the sizes are small enough.
436
- * Multiplication is safe as long as only half of bits are set
437
- * in either multiplicand.
438
- */
439
- {
440
- const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
441
- if (data_size >= check_limit || array_size >= check_limit)
442
- {
443
- const size_t size_max = (size_t)-1;
444
- if (size_max / array_size < data_size)
445
- {
446
- PB_RETURN_ERROR(stream, "size too large");
447
- }
448
- }
449
- }
450
-
451
- /* Allocate new or expand previous allocation */
452
- /* Note: on failure the old pointer will remain in the structure,
453
- * the message must be freed by caller also on error return. */
454
- ptr = pb_realloc(ptr, array_size * data_size);
455
- if (ptr == NULL)
456
- PB_RETURN_ERROR(stream, "realloc failed");
457
-
458
- *(void**)pData = ptr;
459
- return true;
460
- }
461
-
462
- /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
463
- static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
464
- {
465
- if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
466
- PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
467
- {
468
- *(void**)pItem = NULL;
469
- }
470
- else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
471
- {
472
- pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, pItem);
473
- }
474
- }
475
- #endif
476
-
477
- static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
478
- {
479
- #ifndef PB_ENABLE_MALLOC
480
- PB_UNUSED(wire_type);
481
- PB_UNUSED(iter);
482
- PB_RETURN_ERROR(stream, "no malloc support");
483
- #else
484
- pb_type_t type;
485
- pb_decoder_t func;
486
-
487
- type = iter->pos->type;
488
- func = PB_DECODERS[PB_LTYPE(type)];
489
-
490
- switch (PB_HTYPE(type))
491
- {
492
- case PB_HTYPE_REQUIRED:
493
- case PB_HTYPE_OPTIONAL:
494
- case PB_HTYPE_ONEOF:
495
- if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE &&
496
- *(void**)iter->pData != NULL)
497
- {
498
- /* Duplicate field, have to release the old allocation first. */
499
- pb_release_single_field(iter);
500
- }
501
-
502
- if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
503
- {
504
- *(pb_size_t*)iter->pSize = iter->pos->tag;
505
- }
506
-
507
- if (PB_LTYPE(type) == PB_LTYPE_STRING ||
508
- PB_LTYPE(type) == PB_LTYPE_BYTES)
509
- {
510
- return func(stream, iter->pos, iter->pData);
511
- }
512
- else
513
- {
514
- if (!allocate_field(stream, iter->pData, iter->pos->data_size, 1))
515
- return false;
516
-
517
- initialize_pointer_field(*(void**)iter->pData, iter);
518
- return func(stream, iter->pos, *(void**)iter->pData);
519
- }
520
-
521
- case PB_HTYPE_REPEATED:
522
- if (wire_type == PB_WT_STRING
523
- && PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
524
- {
525
- /* Packed array, multiple items come in at once. */
526
- bool status = true;
527
- pb_size_t *size = (pb_size_t*)iter->pSize;
528
- size_t allocated_size = *size;
529
- void *pItem;
530
- pb_istream_t substream;
531
-
532
- if (!pb_make_string_substream(stream, &substream))
533
- return false;
534
-
535
- while (substream.bytes_left)
536
- {
537
- if ((size_t)*size + 1 > allocated_size)
538
- {
539
- /* Allocate more storage. This tries to guess the
540
- * number of remaining entries. Round the division
541
- * upwards. */
542
- allocated_size += (substream.bytes_left - 1) / iter->pos->data_size + 1;
543
-
544
- if (!allocate_field(&substream, iter->pData, iter->pos->data_size, allocated_size))
545
- {
546
- status = false;
547
- break;
548
- }
549
- }
550
-
551
- /* Decode the array entry */
552
- pItem = *(char**)iter->pData + iter->pos->data_size * (*size);
553
- initialize_pointer_field(pItem, iter);
554
- if (!func(&substream, iter->pos, pItem))
555
- {
556
- status = false;
557
- break;
558
- }
559
-
560
- if (*size == PB_SIZE_MAX)
561
- {
562
- #ifndef PB_NO_ERRMSG
563
- stream->errmsg = "too many array entries";
564
- #endif
565
- status = false;
566
- break;
567
- }
568
-
569
- (*size)++;
570
- }
571
- pb_close_string_substream(stream, &substream);
572
-
573
- return status;
574
- }
575
- else
576
- {
577
- /* Normal repeated field, i.e. only one item at a time. */
578
- pb_size_t *size = (pb_size_t*)iter->pSize;
579
- void *pItem;
580
-
581
- if (*size == PB_SIZE_MAX)
582
- PB_RETURN_ERROR(stream, "too many array entries");
583
-
584
- (*size)++;
585
- if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
586
- return false;
587
-
588
- pItem = *(char**)iter->pData + iter->pos->data_size * (*size - 1);
589
- initialize_pointer_field(pItem, iter);
590
- return func(stream, iter->pos, pItem);
591
- }
592
-
593
- default:
594
- PB_RETURN_ERROR(stream, "invalid field type");
595
- }
596
- #endif
597
- }
598
-
599
- static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
600
- {
601
- pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
602
-
603
- #ifdef PB_OLD_CALLBACK_STYLE
604
- void *arg = pCallback->arg;
605
- #else
606
- void **arg = &(pCallback->arg);
607
- #endif
608
-
609
- if (pCallback->funcs.decode == NULL)
610
- return pb_skip_field(stream, wire_type);
611
-
612
- if (wire_type == PB_WT_STRING)
613
- {
614
- pb_istream_t substream;
615
-
616
- if (!pb_make_string_substream(stream, &substream))
617
- return false;
618
-
619
- do
620
- {
621
- if (!pCallback->funcs.decode(&substream, iter->pos, arg))
622
- PB_RETURN_ERROR(stream, "callback failed");
623
- } while (substream.bytes_left);
624
-
625
- pb_close_string_substream(stream, &substream);
626
- return true;
627
- }
628
- else
629
- {
630
- /* Copy the single scalar value to stack.
631
- * This is required so that we can limit the stream length,
632
- * which in turn allows to use same callback for packed and
633
- * not-packed fields. */
634
- pb_istream_t substream;
635
- pb_byte_t buffer[10];
636
- size_t size = sizeof(buffer);
637
-
638
- if (!read_raw_value(stream, wire_type, buffer, &size))
639
- return false;
640
- substream = pb_istream_from_buffer(buffer, size);
641
-
642
- return pCallback->funcs.decode(&substream, iter->pos, arg);
643
- }
644
- }
645
-
646
- static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
647
- {
648
- #ifdef PB_ENABLE_MALLOC
649
- /* When decoding an oneof field, check if there is old data that must be
650
- * released first. */
651
- if (PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF)
652
- {
653
- if (!pb_release_union_field(stream, iter))
654
- return false;
655
- }
656
- #endif
657
-
658
- switch (PB_ATYPE(iter->pos->type))
659
- {
660
- case PB_ATYPE_STATIC:
661
- return decode_static_field(stream, wire_type, iter);
662
-
663
- case PB_ATYPE_POINTER:
664
- return decode_pointer_field(stream, wire_type, iter);
665
-
666
- case PB_ATYPE_CALLBACK:
667
- return decode_callback_field(stream, wire_type, iter);
668
-
669
- default:
670
- PB_RETURN_ERROR(stream, "invalid field type");
671
- }
672
- }
673
-
674
- static void iter_from_extension(pb_field_iter_t *iter, pb_extension_t *extension)
675
- {
676
- /* Fake a field iterator for the extension field.
677
- * It is not actually safe to advance this iterator, but decode_field
678
- * will not even try to. */
679
- const pb_field_t *field = (const pb_field_t*)extension->type->arg;
680
- (void)pb_field_iter_begin(iter, field, extension->dest);
681
- iter->pData = extension->dest;
682
- iter->pSize = &extension->found;
683
-
684
- if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
685
- {
686
- /* For pointer extensions, the pointer is stored directly
687
- * in the extension structure. This avoids having an extra
688
- * indirection. */
689
- iter->pData = &extension->dest;
690
- }
691
- }
692
-
693
- /* Default handler for extension fields. Expects a pb_field_t structure
694
- * in extension->type->arg. */
695
- static bool checkreturn default_extension_decoder(pb_istream_t *stream,
696
- pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
697
- {
698
- const pb_field_t *field = (const pb_field_t*)extension->type->arg;
699
- pb_field_iter_t iter;
700
-
701
- if (field->tag != tag)
702
- return true;
703
-
704
- iter_from_extension(&iter, extension);
705
- extension->found = true;
706
- return decode_field(stream, wire_type, &iter);
707
- }
708
-
709
- /* Try to decode an unknown field as an extension field. Tries each extension
710
- * decoder in turn, until one of them handles the field or loop ends. */
711
- static bool checkreturn decode_extension(pb_istream_t *stream,
712
- uint32_t tag, pb_wire_type_t wire_type, pb_field_iter_t *iter)
713
- {
714
- pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
715
- size_t pos = stream->bytes_left;
716
-
717
- while (extension != NULL && pos == stream->bytes_left)
718
- {
719
- bool status;
720
- if (extension->type->decode)
721
- status = extension->type->decode(stream, extension, tag, wire_type);
722
- else
723
- status = default_extension_decoder(stream, extension, tag, wire_type);
724
-
725
- if (!status)
726
- return false;
727
-
728
- extension = extension->next;
729
- }
730
-
731
- return true;
732
- }
733
-
734
- /* Step through the iterator until an extension field is found or until all
735
- * entries have been checked. There can be only one extension field per
736
- * message. Returns false if no extension field is found. */
737
- static bool checkreturn find_extension_field(pb_field_iter_t *iter)
738
- {
739
- const pb_field_t *start = iter->pos;
740
-
741
- do {
742
- if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
743
- return true;
744
- (void)pb_field_iter_next(iter);
745
- } while (iter->pos != start);
746
-
747
- return false;
748
- }
749
-
750
- /* Initialize message fields to default values, recursively */
751
- static void pb_field_set_to_default(pb_field_iter_t *iter)
752
- {
753
- pb_type_t type;
754
- type = iter->pos->type;
755
-
756
- if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
757
- {
758
- pb_extension_t *ext = *(pb_extension_t* const *)iter->pData;
759
- while (ext != NULL)
760
- {
761
- pb_field_iter_t ext_iter;
762
- ext->found = false;
763
- iter_from_extension(&ext_iter, ext);
764
- pb_field_set_to_default(&ext_iter);
765
- ext = ext->next;
766
- }
767
- }
768
- else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
769
- {
770
- bool init_data = true;
771
- if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL)
772
- {
773
- /* Set has_field to false. Still initialize the optional field
774
- * itself also. */
775
- *(bool*)iter->pSize = false;
776
- }
777
- else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
778
- PB_HTYPE(type) == PB_HTYPE_ONEOF)
779
- {
780
- /* REPEATED: Set array count to 0, no need to initialize contents.
781
- ONEOF: Set which_field to 0. */
782
- *(pb_size_t*)iter->pSize = 0;
783
- init_data = false;
784
- }
785
-
786
- if (init_data)
787
- {
788
- if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
789
- {
790
- /* Initialize submessage to defaults */
791
- pb_message_set_to_defaults((const pb_field_t *) iter->pos->ptr, iter->pData);
792
- }
793
- else if (iter->pos->ptr != NULL)
794
- {
795
- /* Initialize to default value */
796
- memcpy(iter->pData, iter->pos->ptr, iter->pos->data_size);
797
- }
798
- else
799
- {
800
- /* Initialize to zeros */
801
- memset(iter->pData, 0, iter->pos->data_size);
802
- }
803
- }
804
- }
805
- else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
806
- {
807
- /* Initialize the pointer to NULL. */
808
- *(void**)iter->pData = NULL;
809
-
810
- /* Initialize array count to 0. */
811
- if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
812
- PB_HTYPE(type) == PB_HTYPE_ONEOF)
813
- {
814
- *(pb_size_t*)iter->pSize = 0;
815
- }
816
- }
817
- else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
818
- {
819
- /* Don't overwrite callback */
820
- }
821
- }
822
-
823
- static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
824
- {
825
- pb_field_iter_t iter;
826
-
827
- if (!pb_field_iter_begin(&iter, fields, dest_struct))
828
- return; /* Empty message type */
829
-
830
- do
831
- {
832
- pb_field_set_to_default(&iter);
833
- } while (pb_field_iter_next(&iter));
834
- }
835
-
836
- /*********************
837
- * Decode all fields *
838
- *********************/
839
-
840
- bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
841
- {
842
- uint32_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 31) / 32] = {0, 0};
843
- const uint32_t allbits = ~(uint32_t)0;
844
- uint32_t extension_range_start = 0;
845
- pb_field_iter_t iter;
846
-
847
- /* Return value ignored, as empty message types will be correctly handled by
848
- * pb_field_iter_find() anyway. */
849
- (void)pb_field_iter_begin(&iter, fields, dest_struct);
850
-
851
- while (stream->bytes_left)
852
- {
853
- uint32_t tag;
854
- pb_wire_type_t wire_type;
855
- bool eof;
856
-
857
- if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
858
- {
859
- if (eof)
860
- break;
861
- else
862
- return false;
863
- }
864
-
865
- if (!pb_field_iter_find(&iter, tag))
866
- {
867
- /* No match found, check if it matches an extension. */
868
- if (tag >= extension_range_start)
869
- {
870
- if (!find_extension_field(&iter))
871
- extension_range_start = (uint32_t)-1;
872
- else
873
- extension_range_start = iter.pos->tag;
874
-
875
- if (tag >= extension_range_start)
876
- {
877
- size_t pos = stream->bytes_left;
878
-
879
- if (!decode_extension(stream, tag, wire_type, &iter))
880
- return false;
881
-
882
- if (pos != stream->bytes_left)
883
- {
884
- /* The field was handled */
885
- continue;
886
- }
887
- }
888
- }
889
-
890
- /* No match found, skip data */
891
- if (!pb_skip_field(stream, wire_type))
892
- return false;
893
- continue;
894
- }
895
-
896
- if (PB_HTYPE(iter.pos->type) == PB_HTYPE_REQUIRED
897
- && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
898
- {
899
- uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
900
- fields_seen[iter.required_field_index >> 5] |= tmp;
901
- }
902
-
903
- if (!decode_field(stream, wire_type, &iter))
904
- return false;
905
- }
906
-
907
- /* Check that all required fields were present. */
908
- {
909
- /* First figure out the number of required fields by
910
- * seeking to the end of the field array. Usually we
911
- * are already close to end after decoding.
912
- */
913
- unsigned req_field_count;
914
- pb_type_t last_type;
915
- unsigned i;
916
- do {
917
- req_field_count = iter.required_field_index;
918
- last_type = iter.pos->type;
919
- } while (pb_field_iter_next(&iter));
920
-
921
- /* Fixup if last field was also required. */
922
- if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
923
- req_field_count++;
924
-
925
- if (req_field_count > 0)
926
- {
927
- /* Check the whole words */
928
- for (i = 0; i < (req_field_count >> 5); i++)
929
- {
930
- if (fields_seen[i] != allbits)
931
- PB_RETURN_ERROR(stream, "missing required field");
932
- }
933
-
934
- /* Check the remaining bits */
935
- if (fields_seen[req_field_count >> 5] != (allbits >> (32 - (req_field_count & 31))))
936
- PB_RETURN_ERROR(stream, "missing required field");
937
- }
938
- }
939
-
940
- return true;
941
- }
942
-
943
- bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
944
- {
945
- bool status;
946
- pb_message_set_to_defaults(fields, dest_struct);
947
- status = pb_decode_noinit(stream, fields, dest_struct);
948
-
949
- #ifdef PB_ENABLE_MALLOC
950
- if (!status)
951
- pb_release(fields, dest_struct);
952
- #endif
953
-
954
- return status;
955
- }
956
-
957
- bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
958
- {
959
- pb_istream_t substream;
960
- bool status;
961
-
962
- if (!pb_make_string_substream(stream, &substream))
963
- return false;
964
-
965
- status = pb_decode(&substream, fields, dest_struct);
966
- pb_close_string_substream(stream, &substream);
967
- return status;
968
- }
969
-
970
- #ifdef PB_ENABLE_MALLOC
971
- /* Given an oneof field, if there has already been a field inside this oneof,
972
- * release it before overwriting with a different one. */
973
- static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *iter)
974
- {
975
- pb_size_t old_tag = *(pb_size_t*)iter->pSize; /* Previous which_ value */
976
- pb_size_t new_tag = iter->pos->tag; /* New which_ value */
977
-
978
- if (old_tag == 0)
979
- return true; /* Ok, no old data in union */
980
-
981
- if (old_tag == new_tag)
982
- return true; /* Ok, old data is of same type => merge */
983
-
984
- /* Release old data. The find can fail if the message struct contains
985
- * invalid data. */
986
- if (!pb_field_iter_find(iter, old_tag))
987
- PB_RETURN_ERROR(stream, "invalid union tag");
988
-
989
- pb_release_single_field(iter);
990
-
991
- /* Restore iterator to where it should be.
992
- * This shouldn't fail unless the pb_field_t structure is corrupted. */
993
- if (!pb_field_iter_find(iter, new_tag))
994
- PB_RETURN_ERROR(stream, "iterator error");
995
-
996
- return true;
997
- }
998
-
999
- static void pb_release_single_field(const pb_field_iter_t *iter)
1000
- {
1001
- pb_type_t type;
1002
- type = iter->pos->type;
1003
-
1004
- if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
1005
- {
1006
- if (*(pb_size_t*)iter->pSize != iter->pos->tag)
1007
- return; /* This is not the current field in the union */
1008
- }
1009
-
1010
- /* Release anything contained inside an extension or submsg.
1011
- * This has to be done even if the submsg itself is statically
1012
- * allocated. */
1013
- if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
1014
- {
1015
- /* Release fields from all extensions in the linked list */
1016
- pb_extension_t *ext = *(pb_extension_t**)iter->pData;
1017
- while (ext != NULL)
1018
- {
1019
- pb_field_iter_t ext_iter;
1020
- iter_from_extension(&ext_iter, ext);
1021
- pb_release_single_field(&ext_iter);
1022
- ext = ext->next;
1023
- }
1024
- }
1025
- else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
1026
- {
1027
- /* Release fields in submessage or submsg array */
1028
- void *pItem = iter->pData;
1029
- pb_size_t count = 1;
1030
-
1031
- if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1032
- {
1033
- pItem = *(void**)iter->pData;
1034
- }
1035
-
1036
- if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1037
- {
1038
- count = *(pb_size_t*)iter->pSize;
1039
-
1040
- if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > iter->pos->array_size)
1041
- {
1042
- /* Protect against corrupted _count fields */
1043
- count = iter->pos->array_size;
1044
- }
1045
- }
1046
-
1047
- if (pItem)
1048
- {
1049
- while (count--)
1050
- {
1051
- pb_release((const pb_field_t*)iter->pos->ptr, pItem);
1052
- pItem = (char*)pItem + iter->pos->data_size;
1053
- }
1054
- }
1055
- }
1056
-
1057
- if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1058
- {
1059
- if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1060
- (PB_LTYPE(type) == PB_LTYPE_STRING ||
1061
- PB_LTYPE(type) == PB_LTYPE_BYTES))
1062
- {
1063
- /* Release entries in repeated string or bytes array */
1064
- void **pItem = *(void***)iter->pData;
1065
- pb_size_t count = *(pb_size_t*)iter->pSize;
1066
- while (count--)
1067
- {
1068
- pb_free(*pItem);
1069
- *pItem++ = NULL;
1070
- }
1071
- }
1072
-
1073
- if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1074
- {
1075
- /* We are going to release the array, so set the size to 0 */
1076
- *(pb_size_t*)iter->pSize = 0;
1077
- }
1078
-
1079
- /* Release main item */
1080
- pb_free(*(void**)iter->pData);
1081
- *(void**)iter->pData = NULL;
1082
- }
1083
- }
1084
-
1085
- void pb_release(const pb_field_t fields[], void *dest_struct)
1086
- {
1087
- pb_field_iter_t iter;
1088
-
1089
- if (!dest_struct)
1090
- return; /* Ignore NULL pointers, similar to free() */
1091
-
1092
- if (!pb_field_iter_begin(&iter, fields, dest_struct))
1093
- return; /* Empty message type */
1094
-
1095
- do
1096
- {
1097
- pb_release_single_field(&iter);
1098
- } while (pb_field_iter_next(&iter));
1099
- }
1100
- #endif
1101
-
1102
- /* Field decoders */
1103
-
1104
- bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
1105
- {
1106
- uint64_t value;
1107
- if (!pb_decode_varint(stream, &value))
1108
- return false;
1109
-
1110
- if (value & 1)
1111
- *dest = (int64_t)(~(value >> 1));
1112
- else
1113
- *dest = (int64_t)(value >> 1);
1114
-
1115
- return true;
1116
- }
1117
-
1118
- bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1119
- {
1120
- pb_byte_t bytes[4];
1121
-
1122
- if (!pb_read(stream, bytes, 4))
1123
- return false;
1124
-
1125
- *(uint32_t*)dest = ((uint32_t)bytes[0] << 0) |
1126
- ((uint32_t)bytes[1] << 8) |
1127
- ((uint32_t)bytes[2] << 16) |
1128
- ((uint32_t)bytes[3] << 24);
1129
- return true;
1130
- }
1131
-
1132
- bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1133
- {
1134
- pb_byte_t bytes[8];
1135
-
1136
- if (!pb_read(stream, bytes, 8))
1137
- return false;
1138
-
1139
- *(uint64_t*)dest = ((uint64_t)bytes[0] << 0) |
1140
- ((uint64_t)bytes[1] << 8) |
1141
- ((uint64_t)bytes[2] << 16) |
1142
- ((uint64_t)bytes[3] << 24) |
1143
- ((uint64_t)bytes[4] << 32) |
1144
- ((uint64_t)bytes[5] << 40) |
1145
- ((uint64_t)bytes[6] << 48) |
1146
- ((uint64_t)bytes[7] << 56);
1147
-
1148
- return true;
1149
- }
1150
-
1151
- static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1152
- {
1153
- uint64_t value;
1154
- int64_t svalue;
1155
- int64_t clamped;
1156
- if (!pb_decode_varint(stream, &value))
1157
- return false;
1158
-
1159
- /* See issue 97: Google's C++ protobuf allows negative varint values to
1160
- * be cast as int32_t, instead of the int64_t that should be used when
1161
- * encoding. Previous nanopb versions had a bug in encoding. In order to
1162
- * not break decoding of such messages, we cast <=32 bit fields to
1163
- * int32_t first to get the sign correct.
1164
- */
1165
- if (field->data_size == sizeof(int64_t))
1166
- svalue = (int64_t)value;
1167
- else
1168
- svalue = (int32_t)value;
1169
-
1170
- /* Cast to the proper field size, while checking for overflows */
1171
- if (field->data_size == sizeof(int64_t))
1172
- clamped = *(int64_t*)dest = svalue;
1173
- else if (field->data_size == sizeof(int32_t))
1174
- clamped = *(int32_t*)dest = (int32_t)svalue;
1175
- else if (field->data_size == sizeof(int_least16_t))
1176
- clamped = *(int_least16_t*)dest = (int_least16_t)svalue;
1177
- else if (field->data_size == sizeof(int_least8_t))
1178
- clamped = *(int_least8_t*)dest = (int_least8_t)svalue;
1179
- else
1180
- PB_RETURN_ERROR(stream, "invalid data_size");
1181
-
1182
- if (clamped != svalue)
1183
- PB_RETURN_ERROR(stream, "integer too large");
1184
-
1185
- return true;
1186
- }
1187
-
1188
- static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1189
- {
1190
- uint64_t value, clamped;
1191
- if (!pb_decode_varint(stream, &value))
1192
- return false;
1193
-
1194
- /* Cast to the proper field size, while checking for overflows */
1195
- if (field->data_size == sizeof(uint64_t))
1196
- clamped = *(uint64_t*)dest = value;
1197
- else if (field->data_size == sizeof(uint32_t))
1198
- clamped = *(uint32_t*)dest = (uint32_t)value;
1199
- else if (field->data_size == sizeof(uint_least16_t))
1200
- clamped = *(uint_least16_t*)dest = (uint_least16_t)value;
1201
- else if (field->data_size == sizeof(uint_least8_t))
1202
- clamped = *(uint_least8_t*)dest = (uint_least8_t)value;
1203
- else
1204
- PB_RETURN_ERROR(stream, "invalid data_size");
1205
-
1206
- if (clamped != value)
1207
- PB_RETURN_ERROR(stream, "integer too large");
1208
-
1209
- return true;
1210
- }
1211
-
1212
- static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
1213
- {
1214
- int64_t value, clamped;
1215
- if (!pb_decode_svarint(stream, &value))
1216
- return false;
1217
-
1218
- /* Cast to the proper field size, while checking for overflows */
1219
- if (field->data_size == sizeof(int64_t))
1220
- clamped = *(int64_t*)dest = value;
1221
- else if (field->data_size == sizeof(int32_t))
1222
- clamped = *(int32_t*)dest = (int32_t)value;
1223
- else if (field->data_size == sizeof(int_least16_t))
1224
- clamped = *(int_least16_t*)dest = (int_least16_t)value;
1225
- else if (field->data_size == sizeof(int_least8_t))
1226
- clamped = *(int_least8_t*)dest = (int_least8_t)value;
1227
- else
1228
- PB_RETURN_ERROR(stream, "invalid data_size");
1229
-
1230
- if (clamped != value)
1231
- PB_RETURN_ERROR(stream, "integer too large");
1232
-
1233
- return true;
1234
- }
1235
-
1236
- static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
1237
- {
1238
- PB_UNUSED(field);
1239
- return pb_decode_fixed32(stream, dest);
1240
- }
1241
-
1242
- static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
1243
- {
1244
- PB_UNUSED(field);
1245
- return pb_decode_fixed64(stream, dest);
1246
- }
1247
-
1248
- static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
1249
- {
1250
- uint32_t size;
1251
- size_t alloc_size;
1252
- pb_bytes_array_t *bdest;
1253
-
1254
- if (!pb_decode_varint32(stream, &size))
1255
- return false;
1256
-
1257
- if (size > PB_SIZE_MAX)
1258
- PB_RETURN_ERROR(stream, "bytes overflow");
1259
-
1260
- alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1261
- if (size > alloc_size)
1262
- PB_RETURN_ERROR(stream, "size too large");
1263
-
1264
- if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1265
- {
1266
- #ifndef PB_ENABLE_MALLOC
1267
- PB_RETURN_ERROR(stream, "no malloc support");
1268
- #else
1269
- if (!allocate_field(stream, dest, alloc_size, 1))
1270
- return false;
1271
- bdest = *(pb_bytes_array_t**)dest;
1272
- #endif
1273
- }
1274
- else
1275
- {
1276
- if (PB_LTYPE(field->type) == PB_LTYPE_FIXED_LENGTH_BYTES) {
1277
- if (size != field->data_size)
1278
- PB_RETURN_ERROR(stream, "incorrect inline bytes size");
1279
- return pb_read(stream, (pb_byte_t*)dest, field->data_size);
1280
- }
1281
-
1282
- if (alloc_size > field->data_size)
1283
- PB_RETURN_ERROR(stream, "bytes overflow");
1284
- bdest = (pb_bytes_array_t*)dest;
1285
- }
1286
-
1287
- bdest->size = (pb_size_t)size;
1288
- return pb_read(stream, bdest->bytes, size);
1289
- }
1290
-
1291
- static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
1292
- {
1293
- uint32_t size;
1294
- size_t alloc_size;
1295
- bool status;
1296
- if (!pb_decode_varint32(stream, &size))
1297
- return false;
1298
-
1299
- /* Space for null terminator */
1300
- alloc_size = size + 1;
1301
-
1302
- if (alloc_size < size)
1303
- PB_RETURN_ERROR(stream, "size too large");
1304
-
1305
- if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1306
- {
1307
- #ifndef PB_ENABLE_MALLOC
1308
- PB_RETURN_ERROR(stream, "no malloc support");
1309
- #else
1310
- if (!allocate_field(stream, dest, alloc_size, 1))
1311
- return false;
1312
- dest = *(void**)dest;
1313
- #endif
1314
- }
1315
- else
1316
- {
1317
- if (alloc_size > field->data_size)
1318
- PB_RETURN_ERROR(stream, "string overflow");
1319
- }
1320
-
1321
- status = pb_read(stream, (pb_byte_t*)dest, size);
1322
- *((pb_byte_t*)dest + size) = 0;
1323
- return status;
1324
- }
1325
-
1326
- static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
1327
- {
1328
- bool status;
1329
- pb_istream_t substream;
1330
- const pb_field_t* submsg_fields = (const pb_field_t*)field->ptr;
1331
-
1332
- if (!pb_make_string_substream(stream, &substream))
1333
- return false;
1334
-
1335
- if (field->ptr == NULL)
1336
- PB_RETURN_ERROR(stream, "invalid field descriptor");
1337
-
1338
- /* New array entries need to be initialized, while required and optional
1339
- * submessages have already been initialized in the top-level pb_decode. */
1340
- if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
1341
- status = pb_decode(&substream, submsg_fields, dest);
1342
- else
1343
- status = pb_decode_noinit(&substream, submsg_fields, dest);
1344
-
1345
- pb_close_string_substream(stream, &substream);
1346
- return status;
1347
- }