grpc 1.30.0 → 1.31.1

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 (383) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +560 -619
  3. data/include/grpc/grpc_security.h +8 -0
  4. data/include/grpc/grpc_security_constants.h +3 -0
  5. data/include/grpc/impl/codegen/grpc_types.h +7 -5
  6. data/include/grpc/impl/codegen/port_platform.h +0 -32
  7. data/src/core/ext/filters/client_channel/backend_metric.cc +12 -9
  8. data/src/core/ext/filters/client_channel/client_channel.cc +406 -261
  9. data/src/core/ext/filters/client_channel/config_selector.cc +62 -0
  10. data/src/core/ext/filters/client_channel/config_selector.h +93 -0
  11. data/src/core/ext/filters/client_channel/global_subchannel_pool.cc +24 -2
  12. data/src/core/ext/filters/client_channel/health/health_check_client.cc +2 -0
  13. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +6 -5
  14. data/src/core/ext/filters/client_channel/http_proxy.cc +6 -4
  15. data/src/core/ext/filters/client_channel/lb_policy.h +2 -0
  16. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +39 -23
  17. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +4 -6
  18. data/src/core/ext/filters/client_channel/lb_policy/weighted_target/weighted_target.cc +3 -4
  19. data/src/core/ext/filters/client_channel/lb_policy/xds/xds_routing.cc +381 -72
  20. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +4 -5
  21. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +5 -2
  22. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.cc +6 -5
  23. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_libuv.cc +8 -6
  24. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +9 -7
  25. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_windows.cc +7 -5
  26. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +33 -48
  27. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +6 -2
  28. data/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +21 -18
  29. data/src/core/ext/filters/client_channel/resolver_registry.cc +13 -14
  30. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +6 -7
  31. data/src/core/ext/filters/client_channel/resolving_lb_policy.cc +33 -28
  32. data/src/core/ext/filters/client_channel/resolving_lb_policy.h +39 -20
  33. data/src/core/ext/filters/client_channel/service_config_channel_arg_filter.cc +142 -0
  34. data/src/core/ext/filters/client_channel/subchannel.cc +1 -1
  35. data/src/core/ext/filters/client_channel/xds/xds_api.cc +327 -123
  36. data/src/core/ext/filters/client_channel/xds/xds_api.h +72 -7
  37. data/src/core/ext/filters/client_channel/xds/xds_bootstrap.cc +12 -23
  38. data/src/core/ext/filters/client_channel/xds/xds_client.cc +112 -33
  39. data/src/core/ext/filters/client_channel/xds/xds_client_stats.h +10 -10
  40. data/src/core/ext/filters/http/client/http_client_filter.cc +5 -5
  41. data/src/core/ext/filters/http/http_filters_plugin.cc +2 -1
  42. data/src/core/ext/filters/http/message_compress/message_decompress_filter.cc +74 -33
  43. data/src/core/ext/filters/http/message_compress/message_decompress_filter.h +3 -1
  44. data/src/core/ext/filters/message_size/message_size_filter.cc +56 -80
  45. data/src/core/ext/filters/message_size/message_size_filter.h +6 -0
  46. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +383 -347
  47. data/src/core/ext/transport/chttp2/server/chttp2_server.h +6 -2
  48. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2.cc +1 -1
  49. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.cc +7 -13
  50. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.cc +7 -8
  51. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +19 -4
  52. data/src/core/ext/transport/chttp2/transport/flow_control.cc +22 -27
  53. data/src/core/ext/transport/chttp2/transport/flow_control.h +14 -16
  54. data/src/core/ext/transport/chttp2/transport/frame_data.cc +9 -12
  55. data/src/core/ext/transport/chttp2/transport/frame_goaway.cc +4 -6
  56. data/src/core/ext/transport/chttp2/transport/frame_ping.cc +5 -6
  57. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.cc +12 -13
  58. data/src/core/ext/transport/chttp2/transport/frame_settings.cc +6 -7
  59. data/src/core/ext/transport/chttp2/transport/frame_window_update.cc +9 -12
  60. data/src/core/ext/transport/chttp2/transport/hpack_parser.cc +25 -29
  61. data/src/core/ext/transport/chttp2/transport/hpack_table.cc +13 -17
  62. data/src/core/ext/transport/chttp2/transport/internal.h +13 -0
  63. data/src/core/ext/transport/chttp2/transport/parsing.cc +33 -43
  64. data/src/core/ext/transport/chttp2/transport/writing.cc +9 -14
  65. data/src/core/ext/transport/inproc/inproc_transport.cc +35 -15
  66. data/src/core/ext/upb-generated/envoy/annotations/deprecation.upb.h +0 -1
  67. data/src/core/ext/upb-generated/envoy/annotations/resource.upb.h +3 -4
  68. data/src/core/ext/upb-generated/envoy/api/v2/auth/cert.upb.h +0 -1
  69. data/src/core/ext/upb-generated/envoy/api/v2/auth/common.upb.h +80 -69
  70. data/src/core/ext/upb-generated/envoy/api/v2/auth/secret.upb.h +24 -23
  71. data/src/core/ext/upb-generated/envoy/api/v2/auth/tls.upb.h +66 -56
  72. data/src/core/ext/upb-generated/envoy/api/v2/cds.upb.h +1 -2
  73. data/src/core/ext/upb-generated/envoy/api/v2/cluster.upb.c +2 -2
  74. data/src/core/ext/upb-generated/envoy/api/v2/cluster.upb.h +317 -311
  75. data/src/core/ext/upb-generated/envoy/api/v2/cluster/circuit_breaker.upb.h +42 -34
  76. data/src/core/ext/upb-generated/envoy/api/v2/cluster/filter.upb.h +7 -7
  77. data/src/core/ext/upb-generated/envoy/api/v2/cluster/outlier_detection.upb.h +79 -61
  78. data/src/core/ext/upb-generated/envoy/api/v2/core/address.upb.h +55 -49
  79. data/src/core/ext/upb-generated/envoy/api/v2/core/backoff.upb.h +9 -8
  80. data/src/core/ext/upb-generated/envoy/api/v2/core/base.upb.c +1 -1
  81. data/src/core/ext/upb-generated/envoy/api/v2/core/base.upb.h +163 -169
  82. data/src/core/ext/upb-generated/envoy/api/v2/core/config_source.upb.h +51 -45
  83. data/src/core/ext/upb-generated/envoy/api/v2/core/event_service_config.upb.h +4 -5
  84. data/src/core/ext/upb-generated/envoy/api/v2/core/grpc_service.upb.h +107 -100
  85. data/src/core/ext/upb-generated/envoy/api/v2/core/health_check.upb.h +137 -117
  86. data/src/core/ext/upb-generated/envoy/api/v2/core/http_uri.upb.h +9 -9
  87. data/src/core/ext/upb-generated/envoy/api/v2/core/protocol.upb.h +105 -87
  88. data/src/core/ext/upb-generated/envoy/api/v2/core/socket_option.upb.h +12 -13
  89. data/src/core/ext/upb-generated/envoy/api/v2/discovery.upb.c +1 -1
  90. data/src/core/ext/upb-generated/envoy/api/v2/discovery.upb.h +95 -101
  91. data/src/core/ext/upb-generated/envoy/api/v2/eds.upb.h +1 -2
  92. data/src/core/ext/upb-generated/envoy/api/v2/endpoint.upb.c +1 -1
  93. data/src/core/ext/upb-generated/envoy/api/v2/endpoint.upb.h +49 -65
  94. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/endpoint.upb.h +0 -1
  95. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/endpoint_components.upb.h +49 -42
  96. data/src/core/ext/upb-generated/envoy/api/v2/endpoint/load_report.upb.h +70 -62
  97. data/src/core/ext/upb-generated/envoy/api/v2/lds.upb.h +1 -2
  98. data/src/core/ext/upb-generated/envoy/api/v2/listener.upb.h +81 -65
  99. data/src/core/ext/upb-generated/envoy/api/v2/listener/listener.upb.h +0 -1
  100. data/src/core/ext/upb-generated/envoy/api/v2/listener/listener_components.upb.c +1 -1
  101. data/src/core/ext/upb-generated/envoy/api/v2/listener/listener_components.upb.h +91 -80
  102. data/src/core/ext/upb-generated/envoy/api/v2/listener/udp_listener_config.upb.h +9 -10
  103. data/src/core/ext/upb-generated/envoy/api/v2/rds.upb.h +1 -2
  104. data/src/core/ext/upb-generated/envoy/api/v2/route.upb.h +36 -31
  105. data/src/core/ext/upb-generated/envoy/api/v2/route/route.upb.h +0 -1
  106. data/src/core/ext/upb-generated/envoy/api/v2/route/route_components.upb.c +7 -7
  107. data/src/core/ext/upb-generated/envoy/api/v2/route/route_components.upb.h +648 -696
  108. data/src/core/ext/upb-generated/envoy/api/v2/scoped_route.upb.h +16 -15
  109. data/src/core/ext/upb-generated/envoy/api/v2/srds.upb.h +1 -2
  110. data/src/core/ext/upb-generated/envoy/config/filter/accesslog/v2/accesslog.upb.c +1 -1
  111. data/src/core/ext/upb-generated/envoy/config/filter/accesslog/v2/accesslog.upb.h +95 -88
  112. data/src/core/ext/upb-generated/envoy/config/filter/network/http_connection_manager/v2/http_connection_manager.upb.h +234 -199
  113. data/src/core/ext/upb-generated/envoy/config/listener/v2/api_listener.upb.h +5 -5
  114. data/src/core/ext/upb-generated/envoy/config/trace/v2/http_tracer.upb.h +13 -13
  115. data/src/core/ext/upb-generated/envoy/service/discovery/v2/ads.upb.h +1 -2
  116. data/src/core/ext/upb-generated/envoy/service/load_stats/v2/lrs.upb.h +20 -18
  117. data/src/core/ext/upb-generated/envoy/type/http.upb.h +0 -1
  118. data/src/core/ext/upb-generated/envoy/type/matcher/regex.upb.h +18 -17
  119. data/src/core/ext/upb-generated/envoy/type/matcher/string.upb.h +14 -14
  120. data/src/core/ext/upb-generated/envoy/type/metadata/v2/metadata.upb.h +23 -23
  121. data/src/core/ext/upb-generated/envoy/type/percent.upb.h +8 -9
  122. data/src/core/ext/upb-generated/envoy/type/range.upb.h +15 -16
  123. data/src/core/ext/upb-generated/envoy/type/semantic_version.upb.h +7 -8
  124. data/src/core/ext/upb-generated/envoy/type/tracing/v2/custom_tag.upb.h +36 -35
  125. data/src/core/ext/upb-generated/gogoproto/gogo.upb.h +0 -1
  126. data/src/core/ext/upb-generated/google/api/annotations.upb.h +0 -1
  127. data/src/core/ext/upb-generated/google/api/http.upb.h +29 -28
  128. data/src/core/ext/upb-generated/google/protobuf/any.upb.h +5 -6
  129. data/src/core/ext/upb-generated/google/protobuf/descriptor.upb.c +3 -3
  130. data/src/core/ext/upb-generated/google/protobuf/descriptor.upb.h +412 -386
  131. data/src/core/ext/upb-generated/google/protobuf/duration.upb.h +5 -6
  132. data/src/core/ext/upb-generated/google/protobuf/empty.upb.h +1 -2
  133. data/src/core/ext/upb-generated/google/protobuf/struct.upb.c +1 -1
  134. data/src/core/ext/upb-generated/google/protobuf/struct.upb.h +33 -54
  135. data/src/core/ext/upb-generated/google/protobuf/timestamp.upb.h +5 -6
  136. data/src/core/ext/upb-generated/google/protobuf/wrappers.upb.h +27 -28
  137. data/src/core/ext/upb-generated/google/rpc/status.upb.h +8 -8
  138. data/src/core/ext/upb-generated/src/proto/grpc/gcp/altscontext.upb.c +1 -1
  139. data/src/core/ext/upb-generated/src/proto/grpc/gcp/altscontext.upb.h +32 -45
  140. data/src/core/ext/upb-generated/src/proto/grpc/gcp/handshaker.upb.c +4 -4
  141. data/src/core/ext/upb-generated/src/proto/grpc/gcp/handshaker.upb.h +157 -178
  142. data/src/core/ext/upb-generated/src/proto/grpc/gcp/transport_security_common.upb.h +14 -13
  143. data/src/core/ext/upb-generated/src/proto/grpc/health/v1/health.upb.h +6 -7
  144. data/src/core/ext/upb-generated/src/proto/grpc/lb/v1/load_balancer.upb.h +59 -56
  145. data/src/core/ext/upb-generated/udpa/annotations/migrate.upb.h +11 -12
  146. data/src/core/ext/upb-generated/udpa/annotations/sensitive.upb.h +0 -1
  147. data/src/core/ext/upb-generated/udpa/annotations/status.upb.h +5 -6
  148. data/src/core/ext/upb-generated/udpa/data/orca/v1/orca_load_report.upb.c +6 -6
  149. data/src/core/ext/upb-generated/udpa/data/orca/v1/orca_load_report.upb.h +41 -68
  150. data/src/core/ext/upb-generated/validate/validate.upb.h +536 -535
  151. data/src/core/lib/channel/channel_trace.cc +2 -6
  152. data/src/core/lib/channel/channelz.cc +5 -15
  153. data/src/core/lib/gpr/log_linux.cc +6 -8
  154. data/src/core/lib/gpr/log_posix.cc +6 -8
  155. data/src/core/lib/gpr/string.cc +10 -9
  156. data/src/core/lib/gpr/string.h +4 -2
  157. data/src/core/lib/gprpp/global_config_env.cc +8 -6
  158. data/src/core/lib/http/httpcli.cc +13 -10
  159. data/src/core/lib/http/httpcli_security_connector.cc +5 -5
  160. data/src/core/lib/iomgr/cfstream_handle.cc +1 -0
  161. data/src/core/lib/iomgr/endpoint_pair_posix.cc +10 -10
  162. data/src/core/lib/iomgr/error_cfstream.cc +9 -8
  163. data/src/core/lib/iomgr/ev_epoll1_linux.cc +5 -6
  164. data/src/core/lib/iomgr/ev_epollex_linux.cc +15 -21
  165. data/src/core/lib/iomgr/ev_poll_posix.cc +6 -5
  166. data/src/core/lib/iomgr/ev_posix.cc +2 -0
  167. data/src/core/lib/iomgr/iomgr.cc +10 -0
  168. data/src/core/lib/iomgr/iomgr.h +10 -0
  169. data/src/core/lib/iomgr/is_epollexclusive_available.cc +14 -0
  170. data/src/core/lib/iomgr/port.h +1 -21
  171. data/src/core/lib/iomgr/resolve_address_custom.cc +13 -18
  172. data/src/core/lib/iomgr/resolve_address_windows.cc +8 -8
  173. data/src/core/lib/iomgr/resource_quota.cc +34 -31
  174. data/src/core/lib/iomgr/sockaddr_utils.cc +7 -5
  175. data/src/core/lib/iomgr/sockaddr_utils.h +1 -1
  176. data/src/core/lib/iomgr/socket_utils_common_posix.cc +95 -55
  177. data/src/core/lib/iomgr/socket_windows.cc +4 -5
  178. data/src/core/lib/iomgr/tcp_client_cfstream.cc +9 -11
  179. data/src/core/lib/iomgr/tcp_client_custom.cc +6 -9
  180. data/src/core/lib/iomgr/tcp_client_posix.cc +27 -36
  181. data/src/core/lib/iomgr/tcp_client_windows.cc +9 -9
  182. data/src/core/lib/iomgr/tcp_custom.cc +1 -1
  183. data/src/core/lib/iomgr/tcp_custom.h +1 -1
  184. data/src/core/lib/iomgr/tcp_server.cc +3 -4
  185. data/src/core/lib/iomgr/tcp_server.h +7 -5
  186. data/src/core/lib/iomgr/tcp_server_custom.cc +6 -14
  187. data/src/core/lib/iomgr/tcp_server_posix.cc +34 -41
  188. data/src/core/lib/iomgr/tcp_server_utils_posix.h +3 -4
  189. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +5 -7
  190. data/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc +4 -9
  191. data/src/core/lib/iomgr/tcp_server_windows.cc +16 -16
  192. data/src/core/lib/iomgr/timer_generic.cc +13 -12
  193. data/src/core/lib/iomgr/udp_server.cc +24 -23
  194. data/src/core/lib/iomgr/udp_server.h +5 -2
  195. data/src/core/lib/iomgr/unix_sockets_posix.cc +9 -14
  196. data/src/core/lib/iomgr/unix_sockets_posix.h +3 -1
  197. data/src/core/lib/iomgr/unix_sockets_posix_noop.cc +5 -2
  198. data/src/core/lib/json/json_reader.cc +20 -21
  199. data/src/core/lib/security/credentials/credentials.h +5 -3
  200. data/src/core/lib/security/credentials/google_default/credentials_generic.cc +8 -6
  201. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +12 -9
  202. data/src/core/lib/security/credentials/jwt/jwt_credentials.cc +7 -4
  203. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +19 -28
  204. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +6 -6
  205. data/src/core/lib/security/credentials/ssl/ssl_credentials.cc +20 -0
  206. data/src/core/lib/security/credentials/ssl/ssl_credentials.h +10 -0
  207. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +10 -0
  208. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +10 -10
  209. data/src/core/lib/security/security_connector/security_connector.cc +2 -0
  210. data/src/core/lib/security/security_connector/security_connector.h +1 -1
  211. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +18 -11
  212. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.h +5 -0
  213. data/src/core/lib/security/security_connector/ssl_utils.cc +44 -23
  214. data/src/core/lib/security/security_connector/ssl_utils.h +6 -2
  215. data/src/core/lib/security/security_connector/tls/tls_security_connector.cc +27 -24
  216. data/src/core/lib/security/transport/auth_filters.h +0 -5
  217. data/src/core/lib/security/transport/client_auth_filter.cc +10 -9
  218. data/src/core/lib/security/util/json_util.cc +12 -13
  219. data/src/core/lib/slice/slice.cc +38 -1
  220. data/src/core/lib/slice/slice_internal.h +1 -0
  221. data/src/core/lib/surface/call.cc +40 -41
  222. data/src/core/lib/surface/completion_queue.cc +271 -14
  223. data/src/core/lib/surface/completion_queue.h +8 -0
  224. data/src/core/lib/surface/init.cc +2 -0
  225. data/src/core/lib/surface/server.cc +565 -632
  226. data/src/core/lib/surface/server.h +34 -12
  227. data/src/core/lib/surface/version.cc +2 -2
  228. data/src/core/lib/transport/transport.h +6 -0
  229. data/src/core/lib/uri/uri_parser.cc +8 -15
  230. data/src/core/plugin_registry/grpc_plugin_registry.cc +4 -0
  231. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +23 -13
  232. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +2 -0
  233. data/src/core/tsi/alts/handshaker/transport_security_common_api.cc +2 -0
  234. data/src/core/tsi/ssl_transport_security.cc +108 -11
  235. data/src/core/tsi/ssl_transport_security.h +14 -2
  236. data/src/core/tsi/transport_security_interface.h +5 -0
  237. data/src/ruby/bin/math_services_pb.rb +4 -4
  238. data/src/ruby/ext/grpc/extconf.rb +5 -2
  239. data/src/ruby/ext/grpc/rb_call.c +3 -2
  240. data/src/ruby/ext/grpc/rb_call.h +4 -0
  241. data/src/ruby/ext/grpc/rb_call_credentials.c +57 -12
  242. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +4 -0
  243. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +6 -0
  244. data/src/ruby/lib/grpc/generic/client_stub.rb +1 -1
  245. data/src/ruby/lib/grpc/generic/interceptors.rb +1 -1
  246. data/src/ruby/lib/grpc/version.rb +1 -1
  247. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +2 -2
  248. data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +5 -0
  249. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +28 -12
  250. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_import2.proto +23 -0
  251. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_ruby_style.proto +2 -0
  252. data/src/ruby/spec/pb/codegen/grpc/testing/same_package_service_name.proto +27 -0
  253. data/src/ruby/spec/pb/codegen/grpc/testing/same_ruby_package_service_name.proto +29 -0
  254. data/src/ruby/spec/pb/codegen/package_option_spec.rb +25 -1
  255. data/src/ruby/spec/support/services.rb +10 -4
  256. data/src/ruby/spec/user_agent_spec.rb +74 -0
  257. data/third_party/boringssl-with-bazel/err_data.c +89 -83
  258. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_bitstr.c +3 -3
  259. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_enum.c +3 -3
  260. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_int.c +1 -1
  261. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_object.c +3 -3
  262. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_time.c +2 -2
  263. data/third_party/boringssl-with-bazel/src/crypto/asn1/a_type.c +1 -1
  264. data/third_party/boringssl-with-bazel/src/crypto/asn1/asn1_lib.c +1 -1
  265. data/third_party/boringssl-with-bazel/src/crypto/asn1/asn_pack.c +1 -1
  266. data/third_party/boringssl-with-bazel/src/crypto/asn1/f_enum.c +1 -1
  267. data/third_party/boringssl-with-bazel/src/crypto/asn1/f_int.c +1 -1
  268. data/third_party/boringssl-with-bazel/src/crypto/asn1/f_string.c +1 -1
  269. data/third_party/boringssl-with-bazel/src/crypto/cipher_extra/tls_cbc.c +1 -0
  270. data/third_party/boringssl-with-bazel/src/crypto/ec_extra/hash_to_curve.c +12 -52
  271. data/third_party/boringssl-with-bazel/src/crypto/ec_extra/internal.h +0 -22
  272. data/third_party/boringssl-with-bazel/src/crypto/evp/evp_asn1.c +143 -0
  273. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/aes/mode_wrappers.c +17 -1
  274. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/bn/internal.h +11 -1
  275. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h +2 -1
  276. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/p224-64.c +13 -11
  277. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/p256-x86_64.c +24 -23
  278. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/p256.c +20 -16
  279. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/simple_mul.c +2 -2
  280. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/util.c +3 -3
  281. data/third_party/boringssl-with-bazel/src/crypto/fipsmodule/self_check/self_check.c +62 -0
  282. data/third_party/boringssl-with-bazel/src/crypto/mem.c +29 -15
  283. data/third_party/boringssl-with-bazel/src/crypto/pkcs8/internal.h +7 -0
  284. data/third_party/boringssl-with-bazel/src/crypto/pkcs8/pkcs8_x509.c +36 -5
  285. data/third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h +0 -29
  286. data/third_party/boringssl-with-bazel/src/crypto/trust_token/pmbtoken.c +116 -363
  287. data/third_party/boringssl-with-bazel/src/crypto/trust_token/trust_token.c +7 -45
  288. data/third_party/boringssl-with-bazel/src/crypto/x509/a_strex.c +4 -4
  289. data/third_party/boringssl-with-bazel/src/crypto/x509/algorithm.c +8 -0
  290. data/third_party/boringssl-with-bazel/src/crypto/x509/asn1_gen.c +4 -4
  291. data/third_party/boringssl-with-bazel/src/crypto/x509/x509.c +0 -67
  292. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_cmp.c +13 -6
  293. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_req.c +10 -0
  294. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_set.c +41 -0
  295. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_trs.c +4 -1
  296. data/third_party/boringssl-with-bazel/src/crypto/x509/x509_vfy.c +28 -9
  297. data/third_party/boringssl-with-bazel/src/crypto/x509/x509cset.c +25 -0
  298. data/third_party/boringssl-with-bazel/src/crypto/x509/x_crl.c +35 -13
  299. data/third_party/boringssl-with-bazel/src/crypto/x509/x_pubkey.c +0 -154
  300. data/third_party/boringssl-with-bazel/src/crypto/x509/x_x509.c +28 -6
  301. data/third_party/boringssl-with-bazel/src/crypto/x509v3/internal.h +5 -0
  302. data/third_party/boringssl-with-bazel/src/crypto/x509v3/v3_purp.c +74 -35
  303. data/third_party/boringssl-with-bazel/src/include/openssl/aes.h +16 -4
  304. data/third_party/boringssl-with-bazel/src/include/openssl/asn1.h +22 -22
  305. data/third_party/boringssl-with-bazel/src/include/openssl/base.h +1 -1
  306. data/third_party/boringssl-with-bazel/src/include/openssl/evp.h +69 -0
  307. data/third_party/boringssl-with-bazel/src/include/openssl/ssl.h +33 -16
  308. data/third_party/boringssl-with-bazel/src/include/openssl/trust_token.h +1 -10
  309. data/third_party/boringssl-with-bazel/src/include/openssl/x509.h +789 -715
  310. data/third_party/boringssl-with-bazel/src/ssl/handoff.cc +3 -3
  311. data/third_party/boringssl-with-bazel/src/ssl/handshake.cc +9 -2
  312. data/third_party/boringssl-with-bazel/src/ssl/handshake_client.cc +2 -2
  313. data/third_party/boringssl-with-bazel/src/ssl/handshake_server.cc +9 -0
  314. data/third_party/boringssl-with-bazel/src/ssl/internal.h +17 -14
  315. data/third_party/boringssl-with-bazel/src/ssl/ssl_asn1.cc +7 -7
  316. data/third_party/boringssl-with-bazel/src/ssl/ssl_lib.cc +28 -0
  317. data/third_party/boringssl-with-bazel/src/ssl/ssl_session.cc +4 -24
  318. data/third_party/boringssl-with-bazel/src/ssl/ssl_versions.cc +5 -5
  319. data/third_party/boringssl-with-bazel/src/ssl/t1_enc.cc +45 -24
  320. data/third_party/boringssl-with-bazel/src/ssl/tls13_client.cc +31 -21
  321. data/third_party/boringssl-with-bazel/src/ssl/tls13_server.cc +12 -9
  322. data/third_party/re2/re2/bitmap256.h +117 -0
  323. data/third_party/re2/re2/bitstate.cc +385 -0
  324. data/third_party/re2/re2/compile.cc +1279 -0
  325. data/third_party/re2/re2/dfa.cc +2130 -0
  326. data/third_party/re2/re2/filtered_re2.cc +121 -0
  327. data/third_party/re2/re2/filtered_re2.h +109 -0
  328. data/third_party/re2/re2/mimics_pcre.cc +197 -0
  329. data/third_party/re2/re2/nfa.cc +713 -0
  330. data/third_party/re2/re2/onepass.cc +623 -0
  331. data/third_party/re2/re2/parse.cc +2464 -0
  332. data/third_party/re2/re2/perl_groups.cc +119 -0
  333. data/third_party/re2/re2/pod_array.h +55 -0
  334. data/third_party/re2/re2/prefilter.cc +710 -0
  335. data/third_party/re2/re2/prefilter.h +108 -0
  336. data/third_party/re2/re2/prefilter_tree.cc +407 -0
  337. data/third_party/re2/re2/prefilter_tree.h +139 -0
  338. data/third_party/re2/re2/prog.cc +988 -0
  339. data/third_party/re2/re2/prog.h +436 -0
  340. data/third_party/re2/re2/re2.cc +1362 -0
  341. data/third_party/re2/re2/re2.h +1002 -0
  342. data/third_party/re2/re2/regexp.cc +980 -0
  343. data/third_party/re2/re2/regexp.h +659 -0
  344. data/third_party/re2/re2/set.cc +154 -0
  345. data/third_party/re2/re2/set.h +80 -0
  346. data/third_party/re2/re2/simplify.cc +657 -0
  347. data/third_party/re2/re2/sparse_array.h +392 -0
  348. data/third_party/re2/re2/sparse_set.h +264 -0
  349. data/third_party/re2/re2/stringpiece.cc +65 -0
  350. data/third_party/re2/re2/stringpiece.h +210 -0
  351. data/third_party/re2/re2/tostring.cc +351 -0
  352. data/third_party/re2/re2/unicode_casefold.cc +582 -0
  353. data/third_party/re2/re2/unicode_casefold.h +78 -0
  354. data/third_party/re2/re2/unicode_groups.cc +6269 -0
  355. data/third_party/re2/re2/unicode_groups.h +67 -0
  356. data/third_party/re2/re2/walker-inl.h +246 -0
  357. data/third_party/re2/util/benchmark.h +156 -0
  358. data/third_party/re2/util/flags.h +26 -0
  359. data/third_party/re2/util/logging.h +109 -0
  360. data/third_party/re2/util/malloc_counter.h +19 -0
  361. data/third_party/re2/util/mix.h +41 -0
  362. data/third_party/re2/util/mutex.h +148 -0
  363. data/third_party/re2/util/pcre.cc +1025 -0
  364. data/third_party/re2/util/pcre.h +681 -0
  365. data/third_party/re2/util/rune.cc +260 -0
  366. data/third_party/re2/util/strutil.cc +149 -0
  367. data/third_party/re2/util/strutil.h +21 -0
  368. data/third_party/re2/util/test.h +50 -0
  369. data/third_party/re2/util/utf.h +44 -0
  370. data/third_party/re2/util/util.h +42 -0
  371. data/third_party/upb/upb/decode.c +467 -504
  372. data/third_party/upb/upb/encode.c +163 -121
  373. data/third_party/upb/upb/msg.c +130 -64
  374. data/third_party/upb/upb/msg.h +418 -14
  375. data/third_party/upb/upb/port_def.inc +35 -6
  376. data/third_party/upb/upb/port_undef.inc +8 -1
  377. data/third_party/upb/upb/table.c +53 -75
  378. data/third_party/upb/upb/table.int.h +11 -43
  379. data/third_party/upb/upb/upb.c +148 -124
  380. data/third_party/upb/upb/upb.h +65 -147
  381. data/third_party/upb/upb/upb.hpp +86 -0
  382. metadata +90 -30
  383. data/third_party/upb/upb/generated_util.h +0 -105
@@ -0,0 +1,1279 @@
1
+ // Copyright 2007 The RE2 Authors. All Rights Reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ // Compile regular expression to Prog.
6
+ //
7
+ // Prog and Inst are defined in prog.h.
8
+ // This file's external interface is just Regexp::CompileToProg.
9
+ // The Compiler class defined in this file is private.
10
+
11
+ #include <stdint.h>
12
+ #include <string.h>
13
+ #include <unordered_map>
14
+ #include <utility>
15
+
16
+ #include "util/logging.h"
17
+ #include "util/utf.h"
18
+ #include "re2/pod_array.h"
19
+ #include "re2/prog.h"
20
+ #include "re2/re2.h"
21
+ #include "re2/regexp.h"
22
+ #include "re2/walker-inl.h"
23
+
24
+ namespace re2 {
25
+
26
+ // List of pointers to Inst* that need to be filled in (patched).
27
+ // Because the Inst* haven't been filled in yet,
28
+ // we can use the Inst* word to hold the list's "next" pointer.
29
+ // It's kind of sleazy, but it works well in practice.
30
+ // See http://swtch.com/~rsc/regexp/regexp1.html for inspiration.
31
+ //
32
+ // Because the out and out1 fields in Inst are no longer pointers,
33
+ // we can't use pointers directly here either. Instead, p refers
34
+ // to inst_[p>>1].out (p&1 == 0) or inst_[p>>1].out1 (p&1 == 1).
35
+ // p == 0 represents the NULL list. This is okay because instruction #0
36
+ // is always the fail instruction, which never appears on a list.
37
+
38
+ struct PatchList {
39
+ uint32_t p;
40
+
41
+ // Returns patch list containing just p.
42
+ static PatchList Mk(uint32_t p);
43
+
44
+ // Patches all the entries on l to have value v.
45
+ // Caller must not ever use patch list again.
46
+ static void Patch(Prog::Inst *inst0, PatchList l, uint32_t v);
47
+
48
+ // Deref returns the next pointer pointed at by p.
49
+ static PatchList Deref(Prog::Inst *inst0, PatchList l);
50
+
51
+ // Appends two patch lists and returns result.
52
+ static PatchList Append(Prog::Inst *inst0, PatchList l1, PatchList l2);
53
+ };
54
+
55
+ static PatchList nullPatchList = { 0 };
56
+
57
+ // Returns patch list containing just p.
58
+ PatchList PatchList::Mk(uint32_t p) {
59
+ PatchList l;
60
+ l.p = p;
61
+ return l;
62
+ }
63
+
64
+ // Returns the next pointer pointed at by l.
65
+ PatchList PatchList::Deref(Prog::Inst* inst0, PatchList l) {
66
+ Prog::Inst* ip = &inst0[l.p>>1];
67
+ if (l.p&1)
68
+ l.p = ip->out1();
69
+ else
70
+ l.p = ip->out();
71
+ return l;
72
+ }
73
+
74
+ // Patches all the entries on l to have value v.
75
+ void PatchList::Patch(Prog::Inst *inst0, PatchList l, uint32_t val) {
76
+ while (l.p != 0) {
77
+ Prog::Inst* ip = &inst0[l.p>>1];
78
+ if (l.p&1) {
79
+ l.p = ip->out1();
80
+ ip->out1_ = val;
81
+ } else {
82
+ l.p = ip->out();
83
+ ip->set_out(val);
84
+ }
85
+ }
86
+ }
87
+
88
+ // Appends two patch lists and returns result.
89
+ PatchList PatchList::Append(Prog::Inst* inst0, PatchList l1, PatchList l2) {
90
+ if (l1.p == 0)
91
+ return l2;
92
+ if (l2.p == 0)
93
+ return l1;
94
+
95
+ PatchList l = l1;
96
+ for (;;) {
97
+ PatchList next = PatchList::Deref(inst0, l);
98
+ if (next.p == 0)
99
+ break;
100
+ l = next;
101
+ }
102
+
103
+ Prog::Inst* ip = &inst0[l.p>>1];
104
+ if (l.p&1)
105
+ ip->out1_ = l2.p;
106
+ else
107
+ ip->set_out(l2.p);
108
+
109
+ return l1;
110
+ }
111
+
112
+ // Compiled program fragment.
113
+ struct Frag {
114
+ uint32_t begin;
115
+ PatchList end;
116
+
117
+ Frag() : begin(0) { end.p = 0; } // needed so Frag can go in vector
118
+ Frag(uint32_t begin, PatchList end) : begin(begin), end(end) {}
119
+ };
120
+
121
+ // Input encodings.
122
+ enum Encoding {
123
+ kEncodingUTF8 = 1, // UTF-8 (0-10FFFF)
124
+ kEncodingLatin1, // Latin-1 (0-FF)
125
+ };
126
+
127
+ class Compiler : public Regexp::Walker<Frag> {
128
+ public:
129
+ explicit Compiler();
130
+ ~Compiler();
131
+
132
+ // Compiles Regexp to a new Prog.
133
+ // Caller is responsible for deleting Prog when finished with it.
134
+ // If reversed is true, compiles for walking over the input
135
+ // string backward (reverses all concatenations).
136
+ static Prog *Compile(Regexp* re, bool reversed, int64_t max_mem);
137
+
138
+ // Compiles alternation of all the re to a new Prog.
139
+ // Each re has a match with an id equal to its index in the vector.
140
+ static Prog* CompileSet(Regexp* re, RE2::Anchor anchor, int64_t max_mem);
141
+
142
+ // Interface for Regexp::Walker, which helps traverse the Regexp.
143
+ // The walk is purely post-recursive: given the machines for the
144
+ // children, PostVisit combines them to create the machine for
145
+ // the current node. The child_args are Frags.
146
+ // The Compiler traverses the Regexp parse tree, visiting
147
+ // each node in depth-first order. It invokes PreVisit before
148
+ // visiting the node's children and PostVisit after visiting
149
+ // the children.
150
+ Frag PreVisit(Regexp* re, Frag parent_arg, bool* stop);
151
+ Frag PostVisit(Regexp* re, Frag parent_arg, Frag pre_arg, Frag* child_args,
152
+ int nchild_args);
153
+ Frag ShortVisit(Regexp* re, Frag parent_arg);
154
+ Frag Copy(Frag arg);
155
+
156
+ // Given fragment a, returns a+ or a+?; a* or a*?; a? or a??
157
+ Frag Plus(Frag a, bool nongreedy);
158
+ Frag Star(Frag a, bool nongreedy);
159
+ Frag Quest(Frag a, bool nongreedy);
160
+
161
+ // Given fragment a, returns (a) capturing as \n.
162
+ Frag Capture(Frag a, int n);
163
+
164
+ // Given fragments a and b, returns ab; a|b
165
+ Frag Cat(Frag a, Frag b);
166
+ Frag Alt(Frag a, Frag b);
167
+
168
+ // Returns a fragment that can't match anything.
169
+ Frag NoMatch();
170
+
171
+ // Returns a fragment that matches the empty string.
172
+ Frag Match(int32_t id);
173
+
174
+ // Returns a no-op fragment.
175
+ Frag Nop();
176
+
177
+ // Returns a fragment matching the byte range lo-hi.
178
+ Frag ByteRange(int lo, int hi, bool foldcase);
179
+
180
+ // Returns a fragment matching an empty-width special op.
181
+ Frag EmptyWidth(EmptyOp op);
182
+
183
+ // Adds n instructions to the program.
184
+ // Returns the index of the first one.
185
+ // Returns -1 if no more instructions are available.
186
+ int AllocInst(int n);
187
+
188
+ // Rune range compiler.
189
+
190
+ // Begins a new alternation.
191
+ void BeginRange();
192
+
193
+ // Adds a fragment matching the rune range lo-hi.
194
+ void AddRuneRange(Rune lo, Rune hi, bool foldcase);
195
+ void AddRuneRangeLatin1(Rune lo, Rune hi, bool foldcase);
196
+ void AddRuneRangeUTF8(Rune lo, Rune hi, bool foldcase);
197
+ void Add_80_10ffff();
198
+
199
+ // New suffix that matches the byte range lo-hi, then goes to next.
200
+ int UncachedRuneByteSuffix(uint8_t lo, uint8_t hi, bool foldcase, int next);
201
+ int CachedRuneByteSuffix(uint8_t lo, uint8_t hi, bool foldcase, int next);
202
+
203
+ // Returns true iff the suffix is cached.
204
+ bool IsCachedRuneByteSuffix(int id);
205
+
206
+ // Adds a suffix to alternation.
207
+ void AddSuffix(int id);
208
+
209
+ // Adds a suffix to the trie starting from the given root node.
210
+ // Returns zero iff allocating an instruction fails. Otherwise, returns
211
+ // the current root node, which might be different from what was given.
212
+ int AddSuffixRecursive(int root, int id);
213
+
214
+ // Finds the trie node for the given suffix. Returns a Frag in order to
215
+ // distinguish between pointing at the root node directly (end.p == 0)
216
+ // and pointing at an Alt's out1 or out (end.p&1 == 1 or 0, respectively).
217
+ Frag FindByteRange(int root, int id);
218
+
219
+ // Compares two ByteRanges and returns true iff they are equal.
220
+ bool ByteRangeEqual(int id1, int id2);
221
+
222
+ // Returns the alternation of all the added suffixes.
223
+ Frag EndRange();
224
+
225
+ // Single rune.
226
+ Frag Literal(Rune r, bool foldcase);
227
+
228
+ void Setup(Regexp::ParseFlags flags, int64_t max_mem, RE2::Anchor anchor);
229
+ Prog* Finish(Regexp* re);
230
+
231
+ // Returns .* where dot = any byte
232
+ Frag DotStar();
233
+
234
+ private:
235
+ Prog* prog_; // Program being built.
236
+ bool failed_; // Did we give up compiling?
237
+ Encoding encoding_; // Input encoding
238
+ bool reversed_; // Should program run backward over text?
239
+
240
+ PODArray<Prog::Inst> inst_;
241
+ int ninst_; // Number of instructions used.
242
+ int max_ninst_; // Maximum number of instructions.
243
+
244
+ int64_t max_mem_; // Total memory budget.
245
+
246
+ std::unordered_map<uint64_t, int> rune_cache_;
247
+ Frag rune_range_;
248
+
249
+ RE2::Anchor anchor_; // anchor mode for RE2::Set
250
+
251
+ Compiler(const Compiler&) = delete;
252
+ Compiler& operator=(const Compiler&) = delete;
253
+ };
254
+
255
+ Compiler::Compiler() {
256
+ prog_ = new Prog();
257
+ failed_ = false;
258
+ encoding_ = kEncodingUTF8;
259
+ reversed_ = false;
260
+ ninst_ = 0;
261
+ max_ninst_ = 1; // make AllocInst for fail instruction okay
262
+ max_mem_ = 0;
263
+ int fail = AllocInst(1);
264
+ inst_[fail].InitFail();
265
+ max_ninst_ = 0; // Caller must change
266
+ }
267
+
268
+ Compiler::~Compiler() {
269
+ delete prog_;
270
+ }
271
+
272
+ int Compiler::AllocInst(int n) {
273
+ if (failed_ || ninst_ + n > max_ninst_) {
274
+ failed_ = true;
275
+ return -1;
276
+ }
277
+
278
+ if (ninst_ + n > inst_.size()) {
279
+ int cap = inst_.size();
280
+ if (cap == 0)
281
+ cap = 8;
282
+ while (ninst_ + n > cap)
283
+ cap *= 2;
284
+ PODArray<Prog::Inst> inst(cap);
285
+ if (inst_.data() != NULL)
286
+ memmove(inst.data(), inst_.data(), ninst_*sizeof inst_[0]);
287
+ memset(inst.data() + ninst_, 0, (cap - ninst_)*sizeof inst_[0]);
288
+ inst_ = std::move(inst);
289
+ }
290
+ int id = ninst_;
291
+ ninst_ += n;
292
+ return id;
293
+ }
294
+
295
+ // These routines are somewhat hard to visualize in text --
296
+ // see http://swtch.com/~rsc/regexp/regexp1.html for
297
+ // pictures explaining what is going on here.
298
+
299
+ // Returns an unmatchable fragment.
300
+ Frag Compiler::NoMatch() {
301
+ return Frag(0, nullPatchList);
302
+ }
303
+
304
+ // Is a an unmatchable fragment?
305
+ static bool IsNoMatch(Frag a) {
306
+ return a.begin == 0;
307
+ }
308
+
309
+ // Given fragments a and b, returns fragment for ab.
310
+ Frag Compiler::Cat(Frag a, Frag b) {
311
+ if (IsNoMatch(a) || IsNoMatch(b))
312
+ return NoMatch();
313
+
314
+ // Elide no-op.
315
+ Prog::Inst* begin = &inst_[a.begin];
316
+ if (begin->opcode() == kInstNop &&
317
+ a.end.p == (a.begin << 1) &&
318
+ begin->out() == 0) {
319
+ // in case refs to a somewhere
320
+ PatchList::Patch(inst_.data(), a.end, b.begin);
321
+ return b;
322
+ }
323
+
324
+ // To run backward over string, reverse all concatenations.
325
+ if (reversed_) {
326
+ PatchList::Patch(inst_.data(), b.end, a.begin);
327
+ return Frag(b.begin, a.end);
328
+ }
329
+
330
+ PatchList::Patch(inst_.data(), a.end, b.begin);
331
+ return Frag(a.begin, b.end);
332
+ }
333
+
334
+ // Given fragments for a and b, returns fragment for a|b.
335
+ Frag Compiler::Alt(Frag a, Frag b) {
336
+ // Special case for convenience in loops.
337
+ if (IsNoMatch(a))
338
+ return b;
339
+ if (IsNoMatch(b))
340
+ return a;
341
+
342
+ int id = AllocInst(1);
343
+ if (id < 0)
344
+ return NoMatch();
345
+
346
+ inst_[id].InitAlt(a.begin, b.begin);
347
+ return Frag(id, PatchList::Append(inst_.data(), a.end, b.end));
348
+ }
349
+
350
+ // When capturing submatches in like-Perl mode, a kOpAlt Inst
351
+ // treats out_ as the first choice, out1_ as the second.
352
+ //
353
+ // For *, +, and ?, if out_ causes another repetition,
354
+ // then the operator is greedy. If out1_ is the repetition
355
+ // (and out_ moves forward), then the operator is non-greedy.
356
+
357
+ // Given a fragment a, returns a fragment for a* or a*? (if nongreedy)
358
+ Frag Compiler::Star(Frag a, bool nongreedy) {
359
+ int id = AllocInst(1);
360
+ if (id < 0)
361
+ return NoMatch();
362
+ inst_[id].InitAlt(0, 0);
363
+ PatchList::Patch(inst_.data(), a.end, id);
364
+ if (nongreedy) {
365
+ inst_[id].out1_ = a.begin;
366
+ return Frag(id, PatchList::Mk(id << 1));
367
+ } else {
368
+ inst_[id].set_out(a.begin);
369
+ return Frag(id, PatchList::Mk((id << 1) | 1));
370
+ }
371
+ }
372
+
373
+ // Given a fragment for a, returns a fragment for a+ or a+? (if nongreedy)
374
+ Frag Compiler::Plus(Frag a, bool nongreedy) {
375
+ // a+ is just a* with a different entry point.
376
+ Frag f = Star(a, nongreedy);
377
+ return Frag(a.begin, f.end);
378
+ }
379
+
380
+ // Given a fragment for a, returns a fragment for a? or a?? (if nongreedy)
381
+ Frag Compiler::Quest(Frag a, bool nongreedy) {
382
+ if (IsNoMatch(a))
383
+ return Nop();
384
+ int id = AllocInst(1);
385
+ if (id < 0)
386
+ return NoMatch();
387
+ PatchList pl;
388
+ if (nongreedy) {
389
+ inst_[id].InitAlt(0, a.begin);
390
+ pl = PatchList::Mk(id << 1);
391
+ } else {
392
+ inst_[id].InitAlt(a.begin, 0);
393
+ pl = PatchList::Mk((id << 1) | 1);
394
+ }
395
+ return Frag(id, PatchList::Append(inst_.data(), pl, a.end));
396
+ }
397
+
398
+ // Returns a fragment for the byte range lo-hi.
399
+ Frag Compiler::ByteRange(int lo, int hi, bool foldcase) {
400
+ int id = AllocInst(1);
401
+ if (id < 0)
402
+ return NoMatch();
403
+ inst_[id].InitByteRange(lo, hi, foldcase, 0);
404
+ return Frag(id, PatchList::Mk(id << 1));
405
+ }
406
+
407
+ // Returns a no-op fragment. Sometimes unavoidable.
408
+ Frag Compiler::Nop() {
409
+ int id = AllocInst(1);
410
+ if (id < 0)
411
+ return NoMatch();
412
+ inst_[id].InitNop(0);
413
+ return Frag(id, PatchList::Mk(id << 1));
414
+ }
415
+
416
+ // Returns a fragment that signals a match.
417
+ Frag Compiler::Match(int32_t match_id) {
418
+ int id = AllocInst(1);
419
+ if (id < 0)
420
+ return NoMatch();
421
+ inst_[id].InitMatch(match_id);
422
+ return Frag(id, nullPatchList);
423
+ }
424
+
425
+ // Returns a fragment matching a particular empty-width op (like ^ or $)
426
+ Frag Compiler::EmptyWidth(EmptyOp empty) {
427
+ int id = AllocInst(1);
428
+ if (id < 0)
429
+ return NoMatch();
430
+ inst_[id].InitEmptyWidth(empty, 0);
431
+ return Frag(id, PatchList::Mk(id << 1));
432
+ }
433
+
434
+ // Given a fragment a, returns a fragment with capturing parens around a.
435
+ Frag Compiler::Capture(Frag a, int n) {
436
+ if (IsNoMatch(a))
437
+ return NoMatch();
438
+ int id = AllocInst(2);
439
+ if (id < 0)
440
+ return NoMatch();
441
+ inst_[id].InitCapture(2*n, a.begin);
442
+ inst_[id+1].InitCapture(2*n+1, 0);
443
+ PatchList::Patch(inst_.data(), a.end, id+1);
444
+
445
+ return Frag(id, PatchList::Mk((id+1) << 1));
446
+ }
447
+
448
+ // A Rune is a name for a Unicode code point.
449
+ // Returns maximum rune encoded by UTF-8 sequence of length len.
450
+ static int MaxRune(int len) {
451
+ int b; // number of Rune bits in len-byte UTF-8 sequence (len < UTFmax)
452
+ if (len == 1)
453
+ b = 7;
454
+ else
455
+ b = 8-(len+1) + 6*(len-1);
456
+ return (1<<b) - 1; // maximum Rune for b bits.
457
+ }
458
+
459
+ // The rune range compiler caches common suffix fragments,
460
+ // which are very common in UTF-8 (e.g., [80-bf]).
461
+ // The fragment suffixes are identified by their start
462
+ // instructions. NULL denotes the eventual end match.
463
+ // The Frag accumulates in rune_range_. Caching common
464
+ // suffixes reduces the UTF-8 "." from 32 to 24 instructions,
465
+ // and it reduces the corresponding one-pass NFA from 16 nodes to 8.
466
+
467
+ void Compiler::BeginRange() {
468
+ rune_cache_.clear();
469
+ rune_range_.begin = 0;
470
+ rune_range_.end = nullPatchList;
471
+ }
472
+
473
+ int Compiler::UncachedRuneByteSuffix(uint8_t lo, uint8_t hi, bool foldcase,
474
+ int next) {
475
+ Frag f = ByteRange(lo, hi, foldcase);
476
+ if (next != 0) {
477
+ PatchList::Patch(inst_.data(), f.end, next);
478
+ } else {
479
+ rune_range_.end = PatchList::Append(inst_.data(), rune_range_.end, f.end);
480
+ }
481
+ return f.begin;
482
+ }
483
+
484
+ static uint64_t MakeRuneCacheKey(uint8_t lo, uint8_t hi, bool foldcase,
485
+ int next) {
486
+ return (uint64_t)next << 17 |
487
+ (uint64_t)lo << 9 |
488
+ (uint64_t)hi << 1 |
489
+ (uint64_t)foldcase;
490
+ }
491
+
492
+ int Compiler::CachedRuneByteSuffix(uint8_t lo, uint8_t hi, bool foldcase,
493
+ int next) {
494
+ uint64_t key = MakeRuneCacheKey(lo, hi, foldcase, next);
495
+ std::unordered_map<uint64_t, int>::const_iterator it = rune_cache_.find(key);
496
+ if (it != rune_cache_.end())
497
+ return it->second;
498
+ int id = UncachedRuneByteSuffix(lo, hi, foldcase, next);
499
+ rune_cache_[key] = id;
500
+ return id;
501
+ }
502
+
503
+ bool Compiler::IsCachedRuneByteSuffix(int id) {
504
+ uint8_t lo = inst_[id].lo_;
505
+ uint8_t hi = inst_[id].hi_;
506
+ bool foldcase = inst_[id].foldcase() != 0;
507
+ int next = inst_[id].out();
508
+
509
+ uint64_t key = MakeRuneCacheKey(lo, hi, foldcase, next);
510
+ return rune_cache_.find(key) != rune_cache_.end();
511
+ }
512
+
513
+ void Compiler::AddSuffix(int id) {
514
+ if (failed_)
515
+ return;
516
+
517
+ if (rune_range_.begin == 0) {
518
+ rune_range_.begin = id;
519
+ return;
520
+ }
521
+
522
+ if (encoding_ == kEncodingUTF8) {
523
+ // Build a trie in order to reduce fanout.
524
+ rune_range_.begin = AddSuffixRecursive(rune_range_.begin, id);
525
+ return;
526
+ }
527
+
528
+ int alt = AllocInst(1);
529
+ if (alt < 0) {
530
+ rune_range_.begin = 0;
531
+ return;
532
+ }
533
+ inst_[alt].InitAlt(rune_range_.begin, id);
534
+ rune_range_.begin = alt;
535
+ }
536
+
537
+ int Compiler::AddSuffixRecursive(int root, int id) {
538
+ DCHECK(inst_[root].opcode() == kInstAlt ||
539
+ inst_[root].opcode() == kInstByteRange);
540
+
541
+ Frag f = FindByteRange(root, id);
542
+ if (IsNoMatch(f)) {
543
+ int alt = AllocInst(1);
544
+ if (alt < 0)
545
+ return 0;
546
+ inst_[alt].InitAlt(root, id);
547
+ return alt;
548
+ }
549
+
550
+ int br;
551
+ if (f.end.p == 0)
552
+ br = root;
553
+ else if (f.end.p&1)
554
+ br = inst_[f.begin].out1();
555
+ else
556
+ br = inst_[f.begin].out();
557
+
558
+ if (IsCachedRuneByteSuffix(br)) {
559
+ // We can't fiddle with cached suffixes, so make a clone of the head.
560
+ int byterange = AllocInst(1);
561
+ if (byterange < 0)
562
+ return 0;
563
+ inst_[byterange].InitByteRange(inst_[br].lo(), inst_[br].hi(),
564
+ inst_[br].foldcase(), inst_[br].out());
565
+
566
+ // Ensure that the parent points to the clone, not to the original.
567
+ // Note that this could leave the head unreachable except via the cache.
568
+ br = byterange;
569
+ if (f.end.p == 0)
570
+ root = br;
571
+ else if (f.end.p&1)
572
+ inst_[f.begin].out1_ = br;
573
+ else
574
+ inst_[f.begin].set_out(br);
575
+ }
576
+
577
+ int out = inst_[id].out();
578
+ if (!IsCachedRuneByteSuffix(id)) {
579
+ // The head should be the instruction most recently allocated, so free it
580
+ // instead of leaving it unreachable.
581
+ DCHECK_EQ(id, ninst_-1);
582
+ inst_[id].out_opcode_ = 0;
583
+ inst_[id].out1_ = 0;
584
+ ninst_--;
585
+ }
586
+
587
+ out = AddSuffixRecursive(inst_[br].out(), out);
588
+ if (out == 0)
589
+ return 0;
590
+
591
+ inst_[br].set_out(out);
592
+ return root;
593
+ }
594
+
595
+ bool Compiler::ByteRangeEqual(int id1, int id2) {
596
+ return inst_[id1].lo() == inst_[id2].lo() &&
597
+ inst_[id1].hi() == inst_[id2].hi() &&
598
+ inst_[id1].foldcase() == inst_[id2].foldcase();
599
+ }
600
+
601
+ Frag Compiler::FindByteRange(int root, int id) {
602
+ if (inst_[root].opcode() == kInstByteRange) {
603
+ if (ByteRangeEqual(root, id))
604
+ return Frag(root, nullPatchList);
605
+ else
606
+ return NoMatch();
607
+ }
608
+
609
+ while (inst_[root].opcode() == kInstAlt) {
610
+ int out1 = inst_[root].out1();
611
+ if (ByteRangeEqual(out1, id))
612
+ return Frag(root, PatchList::Mk((root << 1) | 1));
613
+
614
+ // CharClass is a sorted list of ranges, so if out1 of the root Alt wasn't
615
+ // what we're looking for, then we can stop immediately. Unfortunately, we
616
+ // can't short-circuit the search in reverse mode.
617
+ if (!reversed_)
618
+ return NoMatch();
619
+
620
+ int out = inst_[root].out();
621
+ if (inst_[out].opcode() == kInstAlt)
622
+ root = out;
623
+ else if (ByteRangeEqual(out, id))
624
+ return Frag(root, PatchList::Mk(root << 1));
625
+ else
626
+ return NoMatch();
627
+ }
628
+
629
+ LOG(DFATAL) << "should never happen";
630
+ return NoMatch();
631
+ }
632
+
633
+ Frag Compiler::EndRange() {
634
+ return rune_range_;
635
+ }
636
+
637
+ // Converts rune range lo-hi into a fragment that recognizes
638
+ // the bytes that would make up those runes in the current
639
+ // encoding (Latin 1 or UTF-8).
640
+ // This lets the machine work byte-by-byte even when
641
+ // using multibyte encodings.
642
+
643
+ void Compiler::AddRuneRange(Rune lo, Rune hi, bool foldcase) {
644
+ switch (encoding_) {
645
+ default:
646
+ case kEncodingUTF8:
647
+ AddRuneRangeUTF8(lo, hi, foldcase);
648
+ break;
649
+ case kEncodingLatin1:
650
+ AddRuneRangeLatin1(lo, hi, foldcase);
651
+ break;
652
+ }
653
+ }
654
+
655
+ void Compiler::AddRuneRangeLatin1(Rune lo, Rune hi, bool foldcase) {
656
+ // Latin-1 is easy: runes *are* bytes.
657
+ if (lo > hi || lo > 0xFF)
658
+ return;
659
+ if (hi > 0xFF)
660
+ hi = 0xFF;
661
+ AddSuffix(UncachedRuneByteSuffix(static_cast<uint8_t>(lo),
662
+ static_cast<uint8_t>(hi), foldcase, 0));
663
+ }
664
+
665
+ void Compiler::Add_80_10ffff() {
666
+ // The 80-10FFFF (Runeself-Runemax) rune range occurs frequently enough
667
+ // (for example, for /./ and /[^a-z]/) that it is worth simplifying: by
668
+ // permitting overlong encodings in E0 and F0 sequences and code points
669
+ // over 10FFFF in F4 sequences, the size of the bytecode and the number
670
+ // of equivalence classes are reduced significantly.
671
+ int id;
672
+ if (reversed_) {
673
+ // Prefix factoring matters, but we don't have to handle it here
674
+ // because the rune range trie logic takes care of that already.
675
+ id = UncachedRuneByteSuffix(0xC2, 0xDF, false, 0);
676
+ id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
677
+ AddSuffix(id);
678
+
679
+ id = UncachedRuneByteSuffix(0xE0, 0xEF, false, 0);
680
+ id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
681
+ id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
682
+ AddSuffix(id);
683
+
684
+ id = UncachedRuneByteSuffix(0xF0, 0xF4, false, 0);
685
+ id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
686
+ id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
687
+ id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
688
+ AddSuffix(id);
689
+ } else {
690
+ // Suffix factoring matters - and we do have to handle it here.
691
+ int cont1 = UncachedRuneByteSuffix(0x80, 0xBF, false, 0);
692
+ id = UncachedRuneByteSuffix(0xC2, 0xDF, false, cont1);
693
+ AddSuffix(id);
694
+
695
+ int cont2 = UncachedRuneByteSuffix(0x80, 0xBF, false, cont1);
696
+ id = UncachedRuneByteSuffix(0xE0, 0xEF, false, cont2);
697
+ AddSuffix(id);
698
+
699
+ int cont3 = UncachedRuneByteSuffix(0x80, 0xBF, false, cont2);
700
+ id = UncachedRuneByteSuffix(0xF0, 0xF4, false, cont3);
701
+ AddSuffix(id);
702
+ }
703
+ }
704
+
705
+ void Compiler::AddRuneRangeUTF8(Rune lo, Rune hi, bool foldcase) {
706
+ if (lo > hi)
707
+ return;
708
+
709
+ // Pick off 80-10FFFF as a common special case.
710
+ if (lo == 0x80 && hi == 0x10ffff) {
711
+ Add_80_10ffff();
712
+ return;
713
+ }
714
+
715
+ // Split range into same-length sized ranges.
716
+ for (int i = 1; i < UTFmax; i++) {
717
+ Rune max = MaxRune(i);
718
+ if (lo <= max && max < hi) {
719
+ AddRuneRangeUTF8(lo, max, foldcase);
720
+ AddRuneRangeUTF8(max+1, hi, foldcase);
721
+ return;
722
+ }
723
+ }
724
+
725
+ // ASCII range is always a special case.
726
+ if (hi < Runeself) {
727
+ AddSuffix(UncachedRuneByteSuffix(static_cast<uint8_t>(lo),
728
+ static_cast<uint8_t>(hi), foldcase, 0));
729
+ return;
730
+ }
731
+
732
+ // Split range into sections that agree on leading bytes.
733
+ for (int i = 1; i < UTFmax; i++) {
734
+ uint32_t m = (1<<(6*i)) - 1; // last i bytes of a UTF-8 sequence
735
+ if ((lo & ~m) != (hi & ~m)) {
736
+ if ((lo & m) != 0) {
737
+ AddRuneRangeUTF8(lo, lo|m, foldcase);
738
+ AddRuneRangeUTF8((lo|m)+1, hi, foldcase);
739
+ return;
740
+ }
741
+ if ((hi & m) != m) {
742
+ AddRuneRangeUTF8(lo, (hi&~m)-1, foldcase);
743
+ AddRuneRangeUTF8(hi&~m, hi, foldcase);
744
+ return;
745
+ }
746
+ }
747
+ }
748
+
749
+ // Finally. Generate byte matching equivalent for lo-hi.
750
+ uint8_t ulo[UTFmax], uhi[UTFmax];
751
+ int n = runetochar(reinterpret_cast<char*>(ulo), &lo);
752
+ int m = runetochar(reinterpret_cast<char*>(uhi), &hi);
753
+ (void)m; // USED(m)
754
+ DCHECK_EQ(n, m);
755
+
756
+ // The logic below encodes this thinking:
757
+ //
758
+ // 1. When we have built the whole suffix, we know that it cannot
759
+ // possibly be a suffix of anything longer: in forward mode, nothing
760
+ // else can occur before the leading byte; in reverse mode, nothing
761
+ // else can occur after the last continuation byte or else the leading
762
+ // byte would have to change. Thus, there is no benefit to caching
763
+ // the first byte of the suffix whereas there is a cost involved in
764
+ // cloning it if it begins a common prefix, which is fairly likely.
765
+ //
766
+ // 2. Conversely, the last byte of the suffix cannot possibly be a
767
+ // prefix of anything because next == 0, so we will never want to
768
+ // clone it, but it is fairly likely to be a common suffix. Perhaps
769
+ // more so in reverse mode than in forward mode because the former is
770
+ // "converging" towards lower entropy, but caching is still worthwhile
771
+ // for the latter in cases such as 80-BF.
772
+ //
773
+ // 3. Handling the bytes between the first and the last is less
774
+ // straightforward and, again, the approach depends on whether we are
775
+ // "converging" towards lower entropy: in forward mode, a single byte
776
+ // is unlikely to be part of a common suffix whereas a byte range
777
+ // is more likely so; in reverse mode, a byte range is unlikely to
778
+ // be part of a common suffix whereas a single byte is more likely
779
+ // so. The same benefit versus cost argument applies here.
780
+ int id = 0;
781
+ if (reversed_) {
782
+ for (int i = 0; i < n; i++) {
783
+ // In reverse UTF-8 mode: cache the leading byte; don't cache the last
784
+ // continuation byte; cache anything else iff it's a single byte (XX-XX).
785
+ if (i == 0 || (ulo[i] == uhi[i] && i != n-1))
786
+ id = CachedRuneByteSuffix(ulo[i], uhi[i], false, id);
787
+ else
788
+ id = UncachedRuneByteSuffix(ulo[i], uhi[i], false, id);
789
+ }
790
+ } else {
791
+ for (int i = n-1; i >= 0; i--) {
792
+ // In forward UTF-8 mode: don't cache the leading byte; cache the last
793
+ // continuation byte; cache anything else iff it's a byte range (XX-YY).
794
+ if (i == n-1 || (ulo[i] < uhi[i] && i != 0))
795
+ id = CachedRuneByteSuffix(ulo[i], uhi[i], false, id);
796
+ else
797
+ id = UncachedRuneByteSuffix(ulo[i], uhi[i], false, id);
798
+ }
799
+ }
800
+ AddSuffix(id);
801
+ }
802
+
803
+ // Should not be called.
804
+ Frag Compiler::Copy(Frag arg) {
805
+ // We're using WalkExponential; there should be no copying.
806
+ LOG(DFATAL) << "Compiler::Copy called!";
807
+ failed_ = true;
808
+ return NoMatch();
809
+ }
810
+
811
+ // Visits a node quickly; called once WalkExponential has
812
+ // decided to cut this walk short.
813
+ Frag Compiler::ShortVisit(Regexp* re, Frag) {
814
+ failed_ = true;
815
+ return NoMatch();
816
+ }
817
+
818
+ // Called before traversing a node's children during the walk.
819
+ Frag Compiler::PreVisit(Regexp* re, Frag, bool* stop) {
820
+ // Cut off walk if we've already failed.
821
+ if (failed_)
822
+ *stop = true;
823
+
824
+ return Frag(); // not used by caller
825
+ }
826
+
827
+ Frag Compiler::Literal(Rune r, bool foldcase) {
828
+ switch (encoding_) {
829
+ default:
830
+ return Frag();
831
+
832
+ case kEncodingLatin1:
833
+ return ByteRange(r, r, foldcase);
834
+
835
+ case kEncodingUTF8: {
836
+ if (r < Runeself) // Make common case fast.
837
+ return ByteRange(r, r, foldcase);
838
+ uint8_t buf[UTFmax];
839
+ int n = runetochar(reinterpret_cast<char*>(buf), &r);
840
+ Frag f = ByteRange((uint8_t)buf[0], buf[0], false);
841
+ for (int i = 1; i < n; i++)
842
+ f = Cat(f, ByteRange((uint8_t)buf[i], buf[i], false));
843
+ return f;
844
+ }
845
+ }
846
+ }
847
+
848
+ // Called after traversing the node's children during the walk.
849
+ // Given their frags, build and return the frag for this re.
850
+ Frag Compiler::PostVisit(Regexp* re, Frag, Frag, Frag* child_frags,
851
+ int nchild_frags) {
852
+ // If a child failed, don't bother going forward, especially
853
+ // since the child_frags might contain Frags with NULLs in them.
854
+ if (failed_)
855
+ return NoMatch();
856
+
857
+ // Given the child fragments, return the fragment for this node.
858
+ switch (re->op()) {
859
+ case kRegexpRepeat:
860
+ // Should not see; code at bottom of function will print error
861
+ break;
862
+
863
+ case kRegexpNoMatch:
864
+ return NoMatch();
865
+
866
+ case kRegexpEmptyMatch:
867
+ return Nop();
868
+
869
+ case kRegexpHaveMatch: {
870
+ Frag f = Match(re->match_id());
871
+ if (anchor_ == RE2::ANCHOR_BOTH) {
872
+ // Append \z or else the subexpression will effectively be unanchored.
873
+ // Complemented by the UNANCHORED case in CompileSet().
874
+ f = Cat(EmptyWidth(kEmptyEndText), f);
875
+ }
876
+ return f;
877
+ }
878
+
879
+ case kRegexpConcat: {
880
+ Frag f = child_frags[0];
881
+ for (int i = 1; i < nchild_frags; i++)
882
+ f = Cat(f, child_frags[i]);
883
+ return f;
884
+ }
885
+
886
+ case kRegexpAlternate: {
887
+ Frag f = child_frags[0];
888
+ for (int i = 1; i < nchild_frags; i++)
889
+ f = Alt(f, child_frags[i]);
890
+ return f;
891
+ }
892
+
893
+ case kRegexpStar:
894
+ return Star(child_frags[0], (re->parse_flags()&Regexp::NonGreedy) != 0);
895
+
896
+ case kRegexpPlus:
897
+ return Plus(child_frags[0], (re->parse_flags()&Regexp::NonGreedy) != 0);
898
+
899
+ case kRegexpQuest:
900
+ return Quest(child_frags[0], (re->parse_flags()&Regexp::NonGreedy) != 0);
901
+
902
+ case kRegexpLiteral:
903
+ return Literal(re->rune(), (re->parse_flags()&Regexp::FoldCase) != 0);
904
+
905
+ case kRegexpLiteralString: {
906
+ // Concatenation of literals.
907
+ if (re->nrunes() == 0)
908
+ return Nop();
909
+ Frag f;
910
+ for (int i = 0; i < re->nrunes(); i++) {
911
+ Frag f1 = Literal(re->runes()[i],
912
+ (re->parse_flags()&Regexp::FoldCase) != 0);
913
+ if (i == 0)
914
+ f = f1;
915
+ else
916
+ f = Cat(f, f1);
917
+ }
918
+ return f;
919
+ }
920
+
921
+ case kRegexpAnyChar:
922
+ BeginRange();
923
+ AddRuneRange(0, Runemax, false);
924
+ return EndRange();
925
+
926
+ case kRegexpAnyByte:
927
+ return ByteRange(0x00, 0xFF, false);
928
+
929
+ case kRegexpCharClass: {
930
+ CharClass* cc = re->cc();
931
+ if (cc->empty()) {
932
+ // This can't happen.
933
+ LOG(DFATAL) << "No ranges in char class";
934
+ failed_ = true;
935
+ return NoMatch();
936
+ }
937
+
938
+ // ASCII case-folding optimization: if the char class
939
+ // behaves the same on A-Z as it does on a-z,
940
+ // discard any ranges wholly contained in A-Z
941
+ // and mark the other ranges as foldascii.
942
+ // This reduces the size of a program for
943
+ // (?i)abc from 3 insts per letter to 1 per letter.
944
+ bool foldascii = cc->FoldsASCII();
945
+
946
+ // Character class is just a big OR of the different
947
+ // character ranges in the class.
948
+ BeginRange();
949
+ for (CharClass::iterator i = cc->begin(); i != cc->end(); ++i) {
950
+ // ASCII case-folding optimization (see above).
951
+ if (foldascii && 'A' <= i->lo && i->hi <= 'Z')
952
+ continue;
953
+
954
+ // If this range contains all of A-Za-z or none of it,
955
+ // the fold flag is unnecessary; don't bother.
956
+ bool fold = foldascii;
957
+ if ((i->lo <= 'A' && 'z' <= i->hi) || i->hi < 'A' || 'z' < i->lo ||
958
+ ('Z' < i->lo && i->hi < 'a'))
959
+ fold = false;
960
+
961
+ AddRuneRange(i->lo, i->hi, fold);
962
+ }
963
+ return EndRange();
964
+ }
965
+
966
+ case kRegexpCapture:
967
+ // If this is a non-capturing parenthesis -- (?:foo) --
968
+ // just use the inner expression.
969
+ if (re->cap() < 0)
970
+ return child_frags[0];
971
+ return Capture(child_frags[0], re->cap());
972
+
973
+ case kRegexpBeginLine:
974
+ return EmptyWidth(reversed_ ? kEmptyEndLine : kEmptyBeginLine);
975
+
976
+ case kRegexpEndLine:
977
+ return EmptyWidth(reversed_ ? kEmptyBeginLine : kEmptyEndLine);
978
+
979
+ case kRegexpBeginText:
980
+ return EmptyWidth(reversed_ ? kEmptyEndText : kEmptyBeginText);
981
+
982
+ case kRegexpEndText:
983
+ return EmptyWidth(reversed_ ? kEmptyBeginText : kEmptyEndText);
984
+
985
+ case kRegexpWordBoundary:
986
+ return EmptyWidth(kEmptyWordBoundary);
987
+
988
+ case kRegexpNoWordBoundary:
989
+ return EmptyWidth(kEmptyNonWordBoundary);
990
+ }
991
+ LOG(DFATAL) << "Missing case in Compiler: " << re->op();
992
+ failed_ = true;
993
+ return NoMatch();
994
+ }
995
+
996
+ // Is this regexp required to start at the beginning of the text?
997
+ // Only approximate; can return false for complicated regexps like (\Aa|\Ab),
998
+ // but handles (\A(a|b)). Could use the Walker to write a more exact one.
999
+ static bool IsAnchorStart(Regexp** pre, int depth) {
1000
+ Regexp* re = *pre;
1001
+ Regexp* sub;
1002
+ // The depth limit makes sure that we don't overflow
1003
+ // the stack on a deeply nested regexp. As the comment
1004
+ // above says, IsAnchorStart is conservative, so returning
1005
+ // a false negative is okay. The exact limit is somewhat arbitrary.
1006
+ if (re == NULL || depth >= 4)
1007
+ return false;
1008
+ switch (re->op()) {
1009
+ default:
1010
+ break;
1011
+ case kRegexpConcat:
1012
+ if (re->nsub() > 0) {
1013
+ sub = re->sub()[0]->Incref();
1014
+ if (IsAnchorStart(&sub, depth+1)) {
1015
+ PODArray<Regexp*> subcopy(re->nsub());
1016
+ subcopy[0] = sub; // already have reference
1017
+ for (int i = 1; i < re->nsub(); i++)
1018
+ subcopy[i] = re->sub()[i]->Incref();
1019
+ *pre = Regexp::Concat(subcopy.data(), re->nsub(), re->parse_flags());
1020
+ re->Decref();
1021
+ return true;
1022
+ }
1023
+ sub->Decref();
1024
+ }
1025
+ break;
1026
+ case kRegexpCapture:
1027
+ sub = re->sub()[0]->Incref();
1028
+ if (IsAnchorStart(&sub, depth+1)) {
1029
+ *pre = Regexp::Capture(sub, re->parse_flags(), re->cap());
1030
+ re->Decref();
1031
+ return true;
1032
+ }
1033
+ sub->Decref();
1034
+ break;
1035
+ case kRegexpBeginText:
1036
+ *pre = Regexp::LiteralString(NULL, 0, re->parse_flags());
1037
+ re->Decref();
1038
+ return true;
1039
+ }
1040
+ return false;
1041
+ }
1042
+
1043
+ // Is this regexp required to start at the end of the text?
1044
+ // Only approximate; can return false for complicated regexps like (a\z|b\z),
1045
+ // but handles ((a|b)\z). Could use the Walker to write a more exact one.
1046
+ static bool IsAnchorEnd(Regexp** pre, int depth) {
1047
+ Regexp* re = *pre;
1048
+ Regexp* sub;
1049
+ // The depth limit makes sure that we don't overflow
1050
+ // the stack on a deeply nested regexp. As the comment
1051
+ // above says, IsAnchorEnd is conservative, so returning
1052
+ // a false negative is okay. The exact limit is somewhat arbitrary.
1053
+ if (re == NULL || depth >= 4)
1054
+ return false;
1055
+ switch (re->op()) {
1056
+ default:
1057
+ break;
1058
+ case kRegexpConcat:
1059
+ if (re->nsub() > 0) {
1060
+ sub = re->sub()[re->nsub() - 1]->Incref();
1061
+ if (IsAnchorEnd(&sub, depth+1)) {
1062
+ PODArray<Regexp*> subcopy(re->nsub());
1063
+ subcopy[re->nsub() - 1] = sub; // already have reference
1064
+ for (int i = 0; i < re->nsub() - 1; i++)
1065
+ subcopy[i] = re->sub()[i]->Incref();
1066
+ *pre = Regexp::Concat(subcopy.data(), re->nsub(), re->parse_flags());
1067
+ re->Decref();
1068
+ return true;
1069
+ }
1070
+ sub->Decref();
1071
+ }
1072
+ break;
1073
+ case kRegexpCapture:
1074
+ sub = re->sub()[0]->Incref();
1075
+ if (IsAnchorEnd(&sub, depth+1)) {
1076
+ *pre = Regexp::Capture(sub, re->parse_flags(), re->cap());
1077
+ re->Decref();
1078
+ return true;
1079
+ }
1080
+ sub->Decref();
1081
+ break;
1082
+ case kRegexpEndText:
1083
+ *pre = Regexp::LiteralString(NULL, 0, re->parse_flags());
1084
+ re->Decref();
1085
+ return true;
1086
+ }
1087
+ return false;
1088
+ }
1089
+
1090
+ void Compiler::Setup(Regexp::ParseFlags flags, int64_t max_mem,
1091
+ RE2::Anchor anchor) {
1092
+ if (flags & Regexp::Latin1)
1093
+ encoding_ = kEncodingLatin1;
1094
+ max_mem_ = max_mem;
1095
+ if (max_mem <= 0) {
1096
+ max_ninst_ = 100000; // more than enough
1097
+ } else if (static_cast<size_t>(max_mem) <= sizeof(Prog)) {
1098
+ // No room for anything.
1099
+ max_ninst_ = 0;
1100
+ } else {
1101
+ int64_t m = (max_mem - sizeof(Prog)) / sizeof(Prog::Inst);
1102
+ // Limit instruction count so that inst->id() fits nicely in an int.
1103
+ // SparseArray also assumes that the indices (inst->id()) are ints.
1104
+ // The call to WalkExponential uses 2*max_ninst_ below,
1105
+ // and other places in the code use 2 or 3 * prog->size().
1106
+ // Limiting to 2^24 should avoid overflow in those places.
1107
+ // (The point of allowing more than 32 bits of memory is to
1108
+ // have plenty of room for the DFA states, not to use it up
1109
+ // on the program.)
1110
+ if (m >= 1<<24)
1111
+ m = 1<<24;
1112
+ // Inst imposes its own limit (currently bigger than 2^24 but be safe).
1113
+ if (m > Prog::Inst::kMaxInst)
1114
+ m = Prog::Inst::kMaxInst;
1115
+ max_ninst_ = static_cast<int>(m);
1116
+ }
1117
+ anchor_ = anchor;
1118
+ }
1119
+
1120
+ // Compiles re, returning program.
1121
+ // Caller is responsible for deleting prog_.
1122
+ // If reversed is true, compiles a program that expects
1123
+ // to run over the input string backward (reverses all concatenations).
1124
+ // The reversed flag is also recorded in the returned program.
1125
+ Prog* Compiler::Compile(Regexp* re, bool reversed, int64_t max_mem) {
1126
+ Compiler c;
1127
+ c.Setup(re->parse_flags(), max_mem, RE2::UNANCHORED /* unused */);
1128
+ c.reversed_ = reversed;
1129
+
1130
+ // Simplify to remove things like counted repetitions
1131
+ // and character classes like \d.
1132
+ Regexp* sre = re->Simplify();
1133
+ if (sre == NULL)
1134
+ return NULL;
1135
+
1136
+ // Record whether prog is anchored, removing the anchors.
1137
+ // (They get in the way of other optimizations.)
1138
+ bool is_anchor_start = IsAnchorStart(&sre, 0);
1139
+ bool is_anchor_end = IsAnchorEnd(&sre, 0);
1140
+
1141
+ // Generate fragment for entire regexp.
1142
+ Frag all = c.WalkExponential(sre, Frag(), 2*c.max_ninst_);
1143
+ sre->Decref();
1144
+ if (c.failed_)
1145
+ return NULL;
1146
+
1147
+ // Success! Finish by putting Match node at end, and record start.
1148
+ // Turn off c.reversed_ (if it is set) to force the remaining concatenations
1149
+ // to behave normally.
1150
+ c.reversed_ = false;
1151
+ all = c.Cat(all, c.Match(0));
1152
+
1153
+ c.prog_->set_reversed(reversed);
1154
+ if (c.prog_->reversed()) {
1155
+ c.prog_->set_anchor_start(is_anchor_end);
1156
+ c.prog_->set_anchor_end(is_anchor_start);
1157
+ } else {
1158
+ c.prog_->set_anchor_start(is_anchor_start);
1159
+ c.prog_->set_anchor_end(is_anchor_end);
1160
+ }
1161
+
1162
+ c.prog_->set_start(all.begin);
1163
+ if (!c.prog_->anchor_start()) {
1164
+ // Also create unanchored version, which starts with a .*? loop.
1165
+ all = c.Cat(c.DotStar(), all);
1166
+ }
1167
+ c.prog_->set_start_unanchored(all.begin);
1168
+
1169
+ // Hand ownership of prog_ to caller.
1170
+ return c.Finish(re);
1171
+ }
1172
+
1173
+ Prog* Compiler::Finish(Regexp* re) {
1174
+ if (failed_)
1175
+ return NULL;
1176
+
1177
+ if (prog_->start() == 0 && prog_->start_unanchored() == 0) {
1178
+ // No possible matches; keep Fail instruction only.
1179
+ ninst_ = 1;
1180
+ }
1181
+
1182
+ // Hand off the array to Prog.
1183
+ prog_->inst_ = std::move(inst_);
1184
+ prog_->size_ = ninst_;
1185
+
1186
+ prog_->Optimize();
1187
+ prog_->Flatten();
1188
+ prog_->ComputeByteMap();
1189
+
1190
+ if (!prog_->reversed()) {
1191
+ std::string prefix;
1192
+ bool prefix_foldcase;
1193
+ if (re->RequiredPrefixForAccel(&prefix, &prefix_foldcase) &&
1194
+ !prefix_foldcase) {
1195
+ prog_->prefix_size_ = prefix.size();
1196
+ prog_->prefix_front_ = prefix.front();
1197
+ prog_->prefix_back_ = prefix.back();
1198
+ }
1199
+ }
1200
+
1201
+ // Record remaining memory for DFA.
1202
+ if (max_mem_ <= 0) {
1203
+ prog_->set_dfa_mem(1<<20);
1204
+ } else {
1205
+ int64_t m = max_mem_ - sizeof(Prog);
1206
+ m -= prog_->size_*sizeof(Prog::Inst); // account for inst_
1207
+ if (prog_->CanBitState())
1208
+ m -= prog_->size_*sizeof(uint16_t); // account for list_heads_
1209
+ if (m < 0)
1210
+ m = 0;
1211
+ prog_->set_dfa_mem(m);
1212
+ }
1213
+
1214
+ Prog* p = prog_;
1215
+ prog_ = NULL;
1216
+ return p;
1217
+ }
1218
+
1219
+ // Converts Regexp to Prog.
1220
+ Prog* Regexp::CompileToProg(int64_t max_mem) {
1221
+ return Compiler::Compile(this, false, max_mem);
1222
+ }
1223
+
1224
+ Prog* Regexp::CompileToReverseProg(int64_t max_mem) {
1225
+ return Compiler::Compile(this, true, max_mem);
1226
+ }
1227
+
1228
+ Frag Compiler::DotStar() {
1229
+ return Star(ByteRange(0x00, 0xff, false), true);
1230
+ }
1231
+
1232
+ // Compiles RE set to Prog.
1233
+ Prog* Compiler::CompileSet(Regexp* re, RE2::Anchor anchor, int64_t max_mem) {
1234
+ Compiler c;
1235
+ c.Setup(re->parse_flags(), max_mem, anchor);
1236
+
1237
+ Regexp* sre = re->Simplify();
1238
+ if (sre == NULL)
1239
+ return NULL;
1240
+
1241
+ Frag all = c.WalkExponential(sre, Frag(), 2*c.max_ninst_);
1242
+ sre->Decref();
1243
+ if (c.failed_)
1244
+ return NULL;
1245
+
1246
+ c.prog_->set_anchor_start(true);
1247
+ c.prog_->set_anchor_end(true);
1248
+
1249
+ if (anchor == RE2::UNANCHORED) {
1250
+ // Prepend .* or else the expression will effectively be anchored.
1251
+ // Complemented by the ANCHOR_BOTH case in PostVisit().
1252
+ all = c.Cat(c.DotStar(), all);
1253
+ }
1254
+ c.prog_->set_start(all.begin);
1255
+ c.prog_->set_start_unanchored(all.begin);
1256
+
1257
+ Prog* prog = c.Finish(re);
1258
+ if (prog == NULL)
1259
+ return NULL;
1260
+
1261
+ // Make sure DFA has enough memory to operate,
1262
+ // since we're not going to fall back to the NFA.
1263
+ bool dfa_failed = false;
1264
+ StringPiece sp = "hello, world";
1265
+ prog->SearchDFA(sp, sp, Prog::kAnchored, Prog::kManyMatch,
1266
+ NULL, &dfa_failed, NULL);
1267
+ if (dfa_failed) {
1268
+ delete prog;
1269
+ return NULL;
1270
+ }
1271
+
1272
+ return prog;
1273
+ }
1274
+
1275
+ Prog* Prog::CompileSet(Regexp* re, RE2::Anchor anchor, int64_t max_mem) {
1276
+ return Compiler::CompileSet(re, anchor, max_mem);
1277
+ }
1278
+
1279
+ } // namespace re2