aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/grpc/third_party
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /contrib/libs/grpc/third_party
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'contrib/libs/grpc/third_party')
-rw-r--r--contrib/libs/grpc/third_party/README.md99
-rw-r--r--contrib/libs/grpc/third_party/address_sorting/.yandex_meta/licenses.list.txt54
-rw-r--r--contrib/libs/grpc/third_party/address_sorting/LICENSE26
-rw-r--r--contrib/libs/grpc/third_party/address_sorting/address_sorting.c375
-rw-r--r--contrib/libs/grpc/third_party/address_sorting/address_sorting_internal.h70
-rw-r--r--contrib/libs/grpc/third_party/address_sorting/address_sorting_posix.c97
-rw-r--r--contrib/libs/grpc/third_party/address_sorting/address_sorting_windows.c95
-rw-r--r--contrib/libs/grpc/third_party/address_sorting/include/address_sorting/address_sorting.h113
-rw-r--r--contrib/libs/grpc/third_party/address_sorting/ya.make33
-rw-r--r--contrib/libs/grpc/third_party/upb/.yandex_meta/licenses.list.txt31
-rw-r--r--contrib/libs/grpc/third_party/upb/CONTRIBUTING.md7
-rw-r--r--contrib/libs/grpc/third_party/upb/DESIGN.md72
-rw-r--r--contrib/libs/grpc/third_party/upb/LICENSE26
-rw-r--r--contrib/libs/grpc/third_party/upb/README.md134
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/decode.c621
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/decode.h21
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/encode.c420
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/encode.h21
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/msg.c177
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/msg.h473
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/port.c26
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/port_def.inc179
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/port_undef.inc28
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/table.c880
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/table.int.h466
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/upb.c287
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/upb.h308
-rw-r--r--contrib/libs/grpc/third_party/upb/upb/upb.hpp88
-rw-r--r--contrib/libs/grpc/third_party/upb/ya.make39
29 files changed, 5266 insertions, 0 deletions
diff --git a/contrib/libs/grpc/third_party/README.md b/contrib/libs/grpc/third_party/README.md
new file mode 100644
index 0000000000..9bf04831bc
--- /dev/null
+++ b/contrib/libs/grpc/third_party/README.md
@@ -0,0 +1,99 @@
+# Third-party libraries
+
+gRPC depends on several third-party libraries, their source code is available
+(usually as a git submodule) in this directory.
+
+## Guidelines on updating submodules
+
+- IMPORTANT: whenever possible, try to only update to a stable release of a library (= not to master / random commit). Depending on unreleased revisions
+ makes gRPC installation harder for users, as it forces them to always build the dependency from source and prevents them from using more
+ convenient installation channels (linux packages, package managers etc.)
+
+- bazel BUILD uses a different dependency model - whenever updating a submodule, also update the revision in `grpc_deps.bzl` so that bazel and
+ non-bazel builds stay in sync (this is actually enforced by a sanity check in some cases)
+
+## Considerations when adding a new third-party dependency
+
+- gRPC C++ needs to stay buildable/installable even if the submodules are not present (e.g. the tar.gz archive with gRPC doesn't contain the submodules),
+ assuming that the dependencies are already installed. This is a requirement for being able to provide a reasonable install process (e.g. using cmake)
+ and to support package managers for gRPC C++.
+
+- Adding a new dependency is a lot of work (both for us and for the users).
+ We currently support multiple build systems (BAZEL, cmake, make, ...) so adding a new dependency usually requires updates in multiple build systems
+ (often not trivial). The installation process also needs to continue to work (we do have distrib tests to test many of the possible installation scenarios,
+ but they are not perfect). Adding a new dependency also usually affects the installation instructions that need to be updated.
+ Also keep in mind that adding a new dependency can be quite disruptive
+ for the users and community - it means that all users will need to update their projects accordingly (for C++ projects often non-trivial) and
+ the community-provided C++ packages (e.g. vcpkg) will need to be updated as well.
+
+## Checklist for adding a new third-party dependency
+
+**READ THIS BEFORE YOU ADD A NEW DEPENDENCY**
+
+- [ ] Make sure you understand the hidden costs of adding a dependency (see section above) and that you understand the complexities of updating the build files. Maintenance of the build files isn't for free, so expect to be involved in maintenance tasks, cleanup and support (e.g resolving user bugs) of the build files in the future.
+
+- [ ] Once your change is ready, start an [adhoc run of artifact - packages - distribtests flow](https://fusion.corp.google.com/projectanalysis/summary/KOKORO/prod%3Agrpc%2Fcore%2Fexperimental%2Fgrpc_build_artifacts_multiplatform) and make sure everything passes (for technical reasons, not all the distribtests can run on each PR automatically).
+
+- [ ] Check the impact of the new dependency on the size of our distribution packages (compare BEFORE and AFTER) and post the comparison on your PR (it should not be approved without checking the impact sizes of packages first). The package sizes AFTER can be obtained from the adhoc package build from bullet point above.
+
+## Instructions for updating dependencies
+
+Usually the process is
+
+1. update the submodule to selected commit (see guidance above)
+2. update the dependency in `grpc_deps.bzl` to the same commit
+3. update `tools/run_tests/sanity/check_submodules.sh` to make the sanity test pass
+4. (when needed) run `tools/buildgen/generate_projects.sh` to regenerate the generated files
+5. populate the bazel download mirror by running `bazel/update_mirror.sh`
+
+Updating some dependencies requires extra care.
+
+### Updating third_party/boringssl-with-bazel
+
+- Update the `third_party/boringssl-with-bazel` submodule to the latest [`master-with-bazel`](https://github.com/google/boringssl/tree/master-with-bazel) branch
+```
+git submodule update --init # just to start in a clean state
+cd third_party/boringssl-with-bazel
+git fetch origin # fetch what's new in the boringssl repository
+git checkout origin/master-with-bazel # checkout the current state of master-with-bazel branch in the boringssl repo
+# Note the latest commit SHA on master-with-bazel-branch
+cd ../.. # go back to grpc repo root
+git status # will show that there are new commits in third_party/boringssl-with-bazel
+git add third_party/boringssl-with-bazel # we actually want to update the changes to the submodule
+git commit -m "update submodule boringssl-with-bazel with origin/master-with-bazel" # commit
+```
+
+- Update boringssl dependency in `bazel/grpc_deps.bzl` to the same commit SHA as master-with-bazel branch
+ - Update `http_archive(name = "boringssl",` section by updating the sha in `strip_prefix` and `urls` fields.
+ - Also, set `sha256` field to "" as the existing value is not valid. This will be added later once we know what that value is.
+
+- Update `tools/run_tests/sanity/check_submodules.sh` with the same commit
+
+- Commit these changes `git commit -m "update boringssl dependency to master-with-bazel commit SHA"`
+
+- Run `tools/buildgen/generate_projects.sh` to regenerate the generated files
+ - Because `sha256` in `bazel/grpc_deps.bzl` was left empty, you will get a DEBUG msg like this one:
+```
+Rule 'boringssl' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "SHA value"
+```
+ - Commit the regenrated files `git commit -m "regenerate files"`
+ - Update `bazel/grpc_deps.bzl` with the SHA value shown in the above debug msg. Commit again `git commit -m "Updated sha256"`
+
+- Run `tools/distrib/generate_boringssl_prefix_header.sh`
+ - Commit again `git commit -m "generate boringssl prefix headers"`
+
+- Increment the boringssl podspec version number in
+ `templates/src/objective-c/BoringSSL-GRPC.podspec.template` and `templates/gRPC-Core.podspec.template`.
+ [example](https://github.com/grpc/grpc/pull/21527/commits/9d4411842f02f167209887f1f3d2b9ab5d14931a)
+ - Commit again `git commit -m "Increment podspec version"`
+
+- Run `tools/buildgen/generate_projects.sh` (yes, again)
+ - Commit again `git commit -m "Second regeneration"`
+
+- Create a PR with all the above commits.
+
+- Run `bazel/update_mirror.sh` to update GCS mirror.
+
+### Updating third_party/protobuf
+
+See http://go/grpc-third-party-protobuf-update-instructions (internal only)
diff --git a/contrib/libs/grpc/third_party/address_sorting/.yandex_meta/licenses.list.txt b/contrib/libs/grpc/third_party/address_sorting/.yandex_meta/licenses.list.txt
new file mode 100644
index 0000000000..2b12fd7215
--- /dev/null
+++ b/contrib/libs/grpc/third_party/address_sorting/.yandex_meta/licenses.list.txt
@@ -0,0 +1,54 @@
+====================BSD-3-Clause====================
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+
+
+====================BSD-3-Clause====================
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the project nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+====================COPYRIGHT====================
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
diff --git a/contrib/libs/grpc/third_party/address_sorting/LICENSE b/contrib/libs/grpc/third_party/address_sorting/LICENSE
new file mode 100644
index 0000000000..824d542db9
--- /dev/null
+++ b/contrib/libs/grpc/third_party/address_sorting/LICENSE
@@ -0,0 +1,26 @@
+Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. Neither the name of the project nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
diff --git a/contrib/libs/grpc/third_party/address_sorting/address_sorting.c b/contrib/libs/grpc/third_party/address_sorting/address_sorting.c
new file mode 100644
index 0000000000..9aee0a5419
--- /dev/null
+++ b/contrib/libs/grpc/third_party/address_sorting/address_sorting.c
@@ -0,0 +1,375 @@
+/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
+/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+/*
+ * This is an adaptation of Android's implementation of RFC 6724
+ * (in Android's getaddrinfo.c). It has some cosmetic differences
+ * from Android's getaddrinfo.c, but Android's getaddrinfo.c was
+ * used as a guide or example of a way to implement the RFC 6724 spec when
+ * this was written.
+ */
+
+#include "address_sorting_internal.h"
+
+#include <errno.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+
+// Scope values increase with increase in scope.
+static const int kIPv6AddrScopeLinkLocal = 1;
+static const int kIPv6AddrScopeSiteLocal = 2;
+static const int kIPv6AddrScopeGlobal = 3;
+
+static address_sorting_source_addr_factory* g_current_source_addr_factory =
+ NULL;
+
+static bool address_sorting_get_source_addr(const address_sorting_address* dest,
+ address_sorting_address* source) {
+ return g_current_source_addr_factory->vtable->get_source_addr(
+ g_current_source_addr_factory, dest, source);
+}
+
+bool address_sorting_get_source_addr_for_testing(
+ const address_sorting_address* dest, address_sorting_address* source) {
+ return address_sorting_get_source_addr(dest, source);
+}
+
+static int ipv6_prefix_match_length(const struct sockaddr_in6* sa,
+ const struct sockaddr_in6* sb) {
+ unsigned char* a = (unsigned char*)&sa->sin6_addr;
+ unsigned char* b = (unsigned char*)&sb->sin6_addr;
+ int cur_bit = 0;
+ while (cur_bit < 128) {
+ int high_bit = 1 << (CHAR_BIT - 1);
+ int a_val = a[cur_bit / CHAR_BIT] & (high_bit >> (cur_bit % CHAR_BIT));
+ int b_val = b[cur_bit / CHAR_BIT] & (high_bit >> (cur_bit % CHAR_BIT));
+ if (a_val == b_val) {
+ cur_bit++;
+ } else {
+ break;
+ }
+ }
+ return cur_bit;
+}
+
+static int in6_is_addr_loopback(const struct in6_addr* ipv6_address) {
+ uint32_t* bits32 = (uint32_t*)ipv6_address;
+ return bits32[0] == 0 && bits32[1] == 0 && bits32[2] == 0 &&
+ bits32[3] == htonl(1);
+}
+
+static int in6_is_addr_v4mapped(const struct in6_addr* ipv6_address) {
+ uint32_t* bits32 = (uint32_t*)ipv6_address;
+ return bits32[0] == 0 && bits32[1] == 0 && bits32[2] == htonl(0x0000ffff);
+}
+
+static int in6_is_addr_v4compat(const struct in6_addr* ipv6_address) {
+ uint32_t* bits32 = (uint32_t*)ipv6_address;
+ return bits32[0] == 0 && bits32[1] == 0 && bits32[2] == 0 && bits32[3] != 0 &&
+ bits32[3] != htonl(1);
+}
+
+static int in6_is_addr_sitelocal(const struct in6_addr* ipv6_address) {
+ uint8_t* bytes = (uint8_t*)ipv6_address;
+ return bytes[0] == 0xfe && (bytes[1] & 0xc0) == 0xc0;
+}
+
+static int in6_is_addr_linklocal(const struct in6_addr* ipv6_address) {
+ uint8_t* bytes = (uint8_t*)ipv6_address;
+ return bytes[0] == 0xfe && (bytes[1] & 0xc0) == 0x80;
+}
+
+static int in6_is_addr_6to4(const struct in6_addr* ipv6_address) {
+ uint8_t* bytes = (uint8_t*)ipv6_address;
+ return bytes[0] == 0x20 && bytes[1] == 0x02;
+}
+
+static int in6_is_addr_ula(const struct in6_addr* ipv6_address) {
+ uint8_t* bytes = (uint8_t*)ipv6_address;
+ return (bytes[0] & 0xfe) == 0xfc;
+}
+
+static int in6_is_addr_teredo(const struct in6_addr* ipv6_address) {
+ uint8_t* bytes = (uint8_t*)ipv6_address;
+ return bytes[0] == 0x20 && bytes[1] == 0x01 && bytes[2] == 0x00 &&
+ bytes[3] == 0x00;
+}
+
+static int in6_is_addr_6bone(const struct in6_addr* ipv6_address) {
+ uint8_t* bytes = (uint8_t*)ipv6_address;
+ return bytes[0] == 0x3f && bytes[1] == 0xfe;
+}
+
+address_sorting_family address_sorting_abstract_get_family(
+ const address_sorting_address* address) {
+ switch (((struct sockaddr*)address)->sa_family) {
+ case AF_INET:
+ return ADDRESS_SORTING_AF_INET;
+ case AF_INET6:
+ return ADDRESS_SORTING_AF_INET6;
+ default:
+ return ADDRESS_SORTING_UNKNOWN_FAMILY;
+ }
+}
+
+static int get_label_value(const address_sorting_address* resolved_addr) {
+ if (address_sorting_abstract_get_family(resolved_addr) ==
+ ADDRESS_SORTING_AF_INET) {
+ return 4;
+ } else if (address_sorting_abstract_get_family(resolved_addr) !=
+ ADDRESS_SORTING_AF_INET6) {
+ return 1;
+ }
+ struct sockaddr_in6* ipv6_addr = (struct sockaddr_in6*)&resolved_addr->addr;
+ if (in6_is_addr_loopback(&ipv6_addr->sin6_addr)) {
+ return 0;
+ } else if (in6_is_addr_v4mapped(&ipv6_addr->sin6_addr)) {
+ return 4;
+ } else if (in6_is_addr_6to4(&ipv6_addr->sin6_addr)) {
+ return 2;
+ } else if (in6_is_addr_teredo(&ipv6_addr->sin6_addr)) {
+ return 5;
+ } else if (in6_is_addr_ula(&ipv6_addr->sin6_addr)) {
+ return 13;
+ } else if (in6_is_addr_v4compat(&ipv6_addr->sin6_addr)) {
+ return 3;
+ } else if (in6_is_addr_sitelocal(&ipv6_addr->sin6_addr)) {
+ return 11;
+ } else if (in6_is_addr_6bone(&ipv6_addr->sin6_addr)) {
+ return 12;
+ }
+ return 1;
+}
+
+static int get_precedence_value(const address_sorting_address* resolved_addr) {
+ if (address_sorting_abstract_get_family(resolved_addr) ==
+ ADDRESS_SORTING_AF_INET) {
+ return 35;
+ } else if (address_sorting_abstract_get_family(resolved_addr) !=
+ ADDRESS_SORTING_AF_INET6) {
+ return 1;
+ }
+ struct sockaddr_in6* ipv6_addr = (struct sockaddr_in6*)&resolved_addr->addr;
+ if (in6_is_addr_loopback(&ipv6_addr->sin6_addr)) {
+ return 50;
+ } else if (in6_is_addr_v4mapped(&ipv6_addr->sin6_addr)) {
+ return 35;
+ } else if (in6_is_addr_6to4(&ipv6_addr->sin6_addr)) {
+ return 30;
+ } else if (in6_is_addr_teredo(&ipv6_addr->sin6_addr)) {
+ return 5;
+ } else if (in6_is_addr_ula(&ipv6_addr->sin6_addr)) {
+ return 3;
+ } else if (in6_is_addr_v4compat(&ipv6_addr->sin6_addr) ||
+ in6_is_addr_sitelocal(&ipv6_addr->sin6_addr) ||
+ in6_is_addr_6bone(&ipv6_addr->sin6_addr)) {
+ return 1;
+ }
+ return 40;
+}
+
+static int sockaddr_get_scope(const address_sorting_address* resolved_addr) {
+ if (address_sorting_abstract_get_family(resolved_addr) ==
+ ADDRESS_SORTING_AF_INET) {
+ return kIPv6AddrScopeGlobal;
+ } else if (address_sorting_abstract_get_family(resolved_addr) ==
+ ADDRESS_SORTING_AF_INET6) {
+ struct sockaddr_in6* ipv6_addr = (struct sockaddr_in6*)&resolved_addr->addr;
+ if (in6_is_addr_loopback(&ipv6_addr->sin6_addr) ||
+ in6_is_addr_linklocal(&ipv6_addr->sin6_addr)) {
+ return kIPv6AddrScopeLinkLocal;
+ }
+ if (in6_is_addr_sitelocal(&ipv6_addr->sin6_addr)) {
+ return kIPv6AddrScopeSiteLocal;
+ }
+ return kIPv6AddrScopeGlobal;
+ }
+ return 0;
+}
+
+static int compare_source_addr_exists(const address_sorting_sortable* first,
+ const address_sorting_sortable* second) {
+ if (first->source_addr_exists != second->source_addr_exists) {
+ return first->source_addr_exists ? -1 : 1;
+ }
+ return 0;
+}
+
+static int compare_source_dest_scope_matches(
+ const address_sorting_sortable* first,
+ const address_sorting_sortable* second) {
+ bool first_src_dst_scope_matches = false;
+ if (sockaddr_get_scope(&first->dest_addr) ==
+ sockaddr_get_scope(&first->source_addr)) {
+ first_src_dst_scope_matches = true;
+ }
+ bool second_src_dst_scope_matches = false;
+ if (sockaddr_get_scope(&second->dest_addr) ==
+ sockaddr_get_scope(&second->source_addr)) {
+ second_src_dst_scope_matches = true;
+ }
+ if (first_src_dst_scope_matches != second_src_dst_scope_matches) {
+ return first_src_dst_scope_matches ? -1 : 1;
+ }
+ return 0;
+}
+
+static int compare_source_dest_labels_match(
+ const address_sorting_sortable* first,
+ const address_sorting_sortable* second) {
+ bool first_label_matches = false;
+ if (get_label_value(&first->dest_addr) ==
+ get_label_value(&first->source_addr)) {
+ first_label_matches = true;
+ }
+ bool second_label_matches = false;
+ if (get_label_value(&second->dest_addr) ==
+ get_label_value(&second->source_addr)) {
+ second_label_matches = true;
+ }
+ if (first_label_matches != second_label_matches) {
+ return first_label_matches ? -1 : 1;
+ }
+ return 0;
+}
+
+static int compare_dest_precedence(const address_sorting_sortable* first,
+ const address_sorting_sortable* second) {
+ return get_precedence_value(&second->dest_addr) -
+ get_precedence_value(&first->dest_addr);
+}
+
+static int compare_dest_scope(const address_sorting_sortable* first,
+ const address_sorting_sortable* second) {
+ return sockaddr_get_scope(&first->dest_addr) -
+ sockaddr_get_scope(&second->dest_addr);
+}
+
+static int compare_source_dest_prefix_match_lengths(
+ const address_sorting_sortable* first,
+ const address_sorting_sortable* second) {
+ if (first->source_addr_exists &&
+ address_sorting_abstract_get_family(&first->source_addr) ==
+ ADDRESS_SORTING_AF_INET6 &&
+ second->source_addr_exists &&
+ address_sorting_abstract_get_family(&second->source_addr) ==
+ ADDRESS_SORTING_AF_INET6) {
+ int first_match_length =
+ ipv6_prefix_match_length((struct sockaddr_in6*)&first->source_addr.addr,
+ (struct sockaddr_in6*)&first->dest_addr.addr);
+ int second_match_length = ipv6_prefix_match_length(
+ (struct sockaddr_in6*)&second->source_addr.addr,
+ (struct sockaddr_in6*)&second->dest_addr.addr);
+ return second_match_length - first_match_length;
+ }
+ return 0;
+}
+
+static int rfc_6724_compare(const void* a, const void* b) {
+ const address_sorting_sortable* first = (address_sorting_sortable*)a;
+ const address_sorting_sortable* second = (address_sorting_sortable*)b;
+ int out = 0;
+ if ((out = compare_source_addr_exists(first, second))) {
+ return out;
+ }
+ if ((out = compare_source_dest_scope_matches(first, second))) {
+ return out;
+ }
+ if ((out = compare_source_dest_labels_match(first, second))) {
+ return out;
+ }
+ // TODO: Implement rule 3; avoid deprecated addresses.
+ // TODO: Implement rule 4; avoid temporary addresses.
+ if ((out = compare_dest_precedence(first, second))) {
+ return out;
+ }
+ // TODO: Implement rule 7; prefer native transports.
+ if ((out = compare_dest_scope(first, second))) {
+ return out;
+ }
+ if ((out = compare_source_dest_prefix_match_lengths(first, second))) {
+ return out;
+ }
+ // Prefer that the sort be stable otherwise
+ return (int)(first->original_index - second->original_index);
+}
+
+void address_sorting_override_source_addr_factory_for_testing(
+ address_sorting_source_addr_factory* factory) {
+ if (g_current_source_addr_factory == NULL) {
+ abort();
+ }
+ g_current_source_addr_factory->vtable->destroy(g_current_source_addr_factory);
+ g_current_source_addr_factory = factory;
+}
+
+static void sanity_check_private_fields_are_unused(
+ const address_sorting_sortable* sortable) {
+ address_sorting_address expected_source_addr;
+ memset(&expected_source_addr, 0, sizeof(expected_source_addr));
+ if (memcmp(&expected_source_addr, &sortable->source_addr,
+ sizeof(address_sorting_address)) ||
+ sortable->original_index || sortable->source_addr_exists) {
+ abort();
+ }
+}
+
+void address_sorting_rfc_6724_sort(address_sorting_sortable* sortables,
+ size_t sortables_len) {
+ for (size_t i = 0; i < sortables_len; i++) {
+ sanity_check_private_fields_are_unused(&sortables[i]);
+ sortables[i].original_index = i;
+ sortables[i].source_addr_exists = address_sorting_get_source_addr(
+ &sortables[i].dest_addr, &sortables[i].source_addr);
+ }
+ qsort(sortables, sortables_len, sizeof(address_sorting_sortable),
+ rfc_6724_compare);
+}
+
+void address_sorting_init() {
+ if (g_current_source_addr_factory != NULL) {
+ abort();
+ }
+ g_current_source_addr_factory =
+ address_sorting_create_source_addr_factory_for_current_platform();
+}
+
+void address_sorting_shutdown() {
+ if (g_current_source_addr_factory == NULL) {
+ abort();
+ }
+ g_current_source_addr_factory->vtable->destroy(g_current_source_addr_factory);
+ g_current_source_addr_factory = NULL;
+}
diff --git a/contrib/libs/grpc/third_party/address_sorting/address_sorting_internal.h b/contrib/libs/grpc/third_party/address_sorting/address_sorting_internal.h
new file mode 100644
index 0000000000..be59d44fee
--- /dev/null
+++ b/contrib/libs/grpc/third_party/address_sorting/address_sorting_internal.h
@@ -0,0 +1,70 @@
+/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
+/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+/*
+ * This is an adaptation of Android's implementation of RFC 6724
+ * (in Android's getaddrinfo.c). It has some cosmetic differences
+ * from Android's getaddrinfo.c, but Android's getaddrinfo.c was
+ * used as a guide or example of a way to implement the RFC 6724 spec when
+ * this was written.
+ */
+
+#ifndef ADDRESS_SORTING_INTERNAL_H
+#define ADDRESS_SORTING_INTERNAL_H
+
+#if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
+#include <winsock2.h>
+#include <ws2tcpip.h>
+// comment to prevent formatter from moving mswock.h upwards
+#include <mswsock.h>
+#define ADDRESS_SORTING_WINDOWS 1
+#else
+/* Workaround for issue described in
+ *
+ * https://bugs.launchpad.net/ubuntu/+source/eglibc/+bug/1187301 */
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#define ADDRESS_SORTING_POSIX 1
+#endif
+
+#include <stdbool.h>
+
+#include <address_sorting/address_sorting.h>
+
+address_sorting_source_addr_factory*
+address_sorting_create_source_addr_factory_for_current_platform();
+
+#endif // ADDRESS_SORTING_INTERNAL_H
diff --git a/contrib/libs/grpc/third_party/address_sorting/address_sorting_posix.c b/contrib/libs/grpc/third_party/address_sorting/address_sorting_posix.c
new file mode 100644
index 0000000000..d0dfe12469
--- /dev/null
+++ b/contrib/libs/grpc/third_party/address_sorting/address_sorting_posix.c
@@ -0,0 +1,97 @@
+/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
+/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+/*
+ * This is an adaptation of Android's implementation of RFC 6724
+ * (in Android's getaddrinfo.c). It has some cosmetic differences
+ * from Android's getaddrinfo.c, but Android's getaddrinfo.c was
+ * used as a guide or example of a way to implement the RFC 6724 spec when
+ * this was written.
+ */
+
+#include "address_sorting_internal.h"
+
+#if defined(ADDRESS_SORTING_POSIX)
+
+#include <errno.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+static bool posix_source_addr_factory_get_source_addr(
+ address_sorting_source_addr_factory* factory,
+ const address_sorting_address* dest_addr,
+ address_sorting_address* source_addr) {
+ bool source_addr_exists = false;
+ // Android sets SOCK_CLOEXEC. Don't set this here for portability.
+ int s = socket(((struct sockaddr*)dest_addr)->sa_family, SOCK_DGRAM, 0);
+ if (s != -1) {
+ if (connect(s, (const struct sockaddr*)&dest_addr->addr,
+ (socklen_t)dest_addr->len) != -1) {
+ address_sorting_address found_source_addr;
+ memset(&found_source_addr, 0, sizeof(found_source_addr));
+ found_source_addr.len = sizeof(found_source_addr.addr);
+ if (getsockname(s, (struct sockaddr*)&found_source_addr.addr,
+ (socklen_t*)&found_source_addr.len) != -1) {
+ source_addr_exists = true;
+ *source_addr = found_source_addr;
+ }
+ }
+ }
+ close(s);
+ return source_addr_exists;
+}
+
+static void posix_source_addr_factory_destroy(
+ address_sorting_source_addr_factory* self) {
+ free(self);
+}
+
+static const address_sorting_source_addr_factory_vtable
+ posix_source_addr_factory_vtable = {
+ posix_source_addr_factory_get_source_addr,
+ posix_source_addr_factory_destroy,
+};
+
+address_sorting_source_addr_factory*
+address_sorting_create_source_addr_factory_for_current_platform() {
+ address_sorting_source_addr_factory* factory =
+ malloc(sizeof(address_sorting_source_addr_factory));
+ memset(factory, 0, sizeof(address_sorting_source_addr_factory));
+ factory->vtable = &posix_source_addr_factory_vtable;
+ return factory;
+}
+
+#endif // defined(ADDRESS_SORTING_POSIX)
diff --git a/contrib/libs/grpc/third_party/address_sorting/address_sorting_windows.c b/contrib/libs/grpc/third_party/address_sorting/address_sorting_windows.c
new file mode 100644
index 0000000000..662a88248e
--- /dev/null
+++ b/contrib/libs/grpc/third_party/address_sorting/address_sorting_windows.c
@@ -0,0 +1,95 @@
+/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
+/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+/*
+ * This is an adaptation of Android's implementation of RFC 6724
+ * (in Android's getaddrinfo.c). It has some cosmetic differences
+ * from Android's getaddrinfo.c, but Android's getaddrinfo.c was
+ * used as a guide or example of a way to implement the RFC 6724 spec when
+ * this was written.
+ */
+
+#include "address_sorting_internal.h"
+
+#if defined(ADDRESS_SORTING_WINDOWS)
+
+#include <errno.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+
+static bool windows_source_addr_factory_get_source_addr(
+ address_sorting_source_addr_factory* factory,
+ const address_sorting_address* dest_addr,
+ address_sorting_address* source_addr) {
+ bool source_addr_exists = false;
+ SOCKET s = socket(((struct sockaddr_in6*)dest_addr)->sin6_family, SOCK_DGRAM,
+ IPPROTO_UDP);
+ if (s != INVALID_SOCKET) {
+ if (connect(s, (struct sockaddr*)dest_addr, (int)dest_addr->len) == 0) {
+ address_sorting_address found_source_addr;
+ memset(&found_source_addr, 0, sizeof(found_source_addr));
+ found_source_addr.len = sizeof(found_source_addr.addr);
+ if (getsockname(s, (struct sockaddr*)&found_source_addr.addr,
+ (socklen_t*)&found_source_addr.len) == 0) {
+ source_addr_exists = true;
+ *source_addr = found_source_addr;
+ }
+ }
+ closesocket(s);
+ }
+ return source_addr_exists;
+}
+
+static void windows_source_addr_factory_destroy(
+ address_sorting_source_addr_factory* self) {
+ free(self);
+}
+
+static const address_sorting_source_addr_factory_vtable
+ windows_source_addr_factory_vtable = {
+ windows_source_addr_factory_get_source_addr,
+ windows_source_addr_factory_destroy,
+};
+
+address_sorting_source_addr_factory*
+address_sorting_create_source_addr_factory_for_current_platform() {
+ address_sorting_source_addr_factory* factory =
+ malloc(sizeof(address_sorting_source_addr_factory));
+ memset(factory, 0, sizeof(address_sorting_source_addr_factory));
+ factory->vtable = &windows_source_addr_factory_vtable;
+ return factory;
+}
+
+#endif // defined(ADDRESS_SORTING_WINDOWS)
diff --git a/contrib/libs/grpc/third_party/address_sorting/include/address_sorting/address_sorting.h b/contrib/libs/grpc/third_party/address_sorting/include/address_sorting/address_sorting.h
new file mode 100644
index 0000000000..c58fafe3f7
--- /dev/null
+++ b/contrib/libs/grpc/third_party/address_sorting/include/address_sorting/address_sorting.h
@@ -0,0 +1,113 @@
+/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
+/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+/*
+ * This is an adaptation of Android's implementation of RFC 6724
+ * (in Android's getaddrinfo.c). It has some cosmetic differences
+ * from Android's getaddrinfo.c, but Android's getaddrinfo.c was
+ * used as a guide or example of a way to implement the RFC 6724 spec when
+ * this was written.
+ */
+
+#ifndef ADDRESS_SORTING_H
+#define ADDRESS_SORTING_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct address_sorting_address {
+ char addr[128];
+ size_t len;
+} address_sorting_address;
+
+/* address_sorting_sortable represents one entry in a list of destination
+ * IP addresses to sort. It contains the destination IP address
+ * "sorting key", along with placeholder and scratch fields. */
+typedef struct address_sorting_sortable {
+ // input data; sorting key
+ address_sorting_address dest_addr;
+ // input data; optional value to attach to the sorting key
+ void* user_data;
+ // internal fields, these must be zero'd when passed to sort function
+ address_sorting_address source_addr;
+ bool source_addr_exists;
+ size_t original_index;
+} address_sorting_sortable;
+
+void address_sorting_rfc_6724_sort(address_sorting_sortable* sortables,
+ size_t sortables_len);
+
+void address_sorting_init();
+void address_sorting_shutdown();
+
+struct address_sorting_source_addr_factory;
+
+/* The interfaces below are exposed only for testing */
+typedef struct {
+ /* Gets the source address that would be used for the passed-in destination
+ * address, and fills in *source_addr* with it if one exists.
+ * Returns true if a source address exists for the destination address,
+ * and false otherwise. */
+ bool (*get_source_addr)(struct address_sorting_source_addr_factory* factory,
+ const address_sorting_address* dest_addr,
+ address_sorting_address* source_addr);
+ void (*destroy)(struct address_sorting_source_addr_factory* factory);
+} address_sorting_source_addr_factory_vtable;
+
+typedef struct address_sorting_source_addr_factory {
+ const address_sorting_source_addr_factory_vtable* vtable;
+} address_sorting_source_addr_factory;
+
+/* Platform-compatible address family types */
+typedef enum {
+ ADDRESS_SORTING_AF_INET,
+ ADDRESS_SORTING_AF_INET6,
+ ADDRESS_SORTING_UNKNOWN_FAMILY,
+} address_sorting_family;
+
+/* Indicates whether the address is AF_INET, AF_INET6, or another address
+ * family. */
+address_sorting_family address_sorting_abstract_get_family(
+ const address_sorting_address* address);
+
+void address_sorting_override_source_addr_factory_for_testing(
+ address_sorting_source_addr_factory* factory);
+
+bool address_sorting_get_source_addr_for_testing(
+ const address_sorting_address* dest, address_sorting_address* source);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // ADDRESS_SORTING_H
diff --git a/contrib/libs/grpc/third_party/address_sorting/ya.make b/contrib/libs/grpc/third_party/address_sorting/ya.make
new file mode 100644
index 0000000000..66919eb97a
--- /dev/null
+++ b/contrib/libs/grpc/third_party/address_sorting/ya.make
@@ -0,0 +1,33 @@
+# Generated by devtools/yamaker.
+
+LIBRARY()
+
+OWNER(g:cpp-contrib)
+
+LICENSE(BSD-3-Clause)
+
+LICENSE_TEXTS(.yandex_meta/licenses.list.txt)
+
+ADDINCL(
+ ${ARCADIA_BUILD_ROOT}/contrib/libs/grpc
+ contrib/libs/grpc
+ contrib/libs/grpc/third_party/address_sorting/include
+)
+
+NO_COMPILER_WARNINGS()
+
+NO_RUNTIME()
+
+IF (OS_LINUX OR OS_DARWIN)
+ CFLAGS(
+ -DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK=1
+ )
+ENDIF()
+
+SRCS(
+ address_sorting.c
+ address_sorting_posix.c
+ address_sorting_windows.c
+)
+
+END()
diff --git a/contrib/libs/grpc/third_party/upb/.yandex_meta/licenses.list.txt b/contrib/libs/grpc/third_party/upb/.yandex_meta/licenses.list.txt
new file mode 100644
index 0000000000..b1f339a11d
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/.yandex_meta/licenses.list.txt
@@ -0,0 +1,31 @@
+====================BSD-3-Clause====================
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of Google Inc. nor the names of any other
+ contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+EVENT SHALL GOOGLE INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
+====================COPYRIGHT====================
+Copyright (c) 2009-2011, Google Inc.
+All rights reserved.
+
+
+====================Public-Domain====================
+ * MurmurHash2, by Austin Appleby (released as public domain).
diff --git a/contrib/libs/grpc/third_party/upb/CONTRIBUTING.md b/contrib/libs/grpc/third_party/upb/CONTRIBUTING.md
new file mode 100644
index 0000000000..2f866b4e29
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/CONTRIBUTING.md
@@ -0,0 +1,7 @@
+## <a name="cla"></a> Signing the CLA
+
+Please sign the [Google Contributor License Agreement
+(CLA)](https://cla.developers.google.com/)
+before sending pull requests. For any code changes to be
+accepted, the CLA must be signed. It's a quick process, I
+promise!
diff --git a/contrib/libs/grpc/third_party/upb/DESIGN.md b/contrib/libs/grpc/third_party/upb/DESIGN.md
new file mode 100644
index 0000000000..a7a8a28451
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/DESIGN.md
@@ -0,0 +1,72 @@
+
+μpb Design
+----------
+
+μpb has the following design goals:
+
+- C89 compatible.
+- small code size (both for the core library and generated messages).
+- fast performance (hundreds of MB/s).
+- idiomatic for C programs.
+- easy to wrap in high-level languages (Python, Ruby, Lua, etc) with
+ good performance and all standard protobuf features.
+- hands-off about memory management, allowing for easy integration
+ with existing VMs and/or garbage collectors.
+- offers binary ABI compatibility between apps, generated messages, and
+ the core library (doesn't require re-generating messages or recompiling
+ your application when the core library changes).
+- provides all features that users expect from a protobuf library
+ (generated messages in C, reflection, text format, etc.).
+- layered, so the core is small and doesn't require descriptors.
+- tidy about symbol references, so that any messages or features that
+ aren't used by a C program can have their code GC'd by the linker.
+- possible to use protobuf binary format without leaking message/field
+ names into the binary.
+
+μpb accomplishes these goals by keeping a very small core that does not contain
+descriptors. We need some way of knowing what fields are in each message and
+where they live, but instead of descriptors, we keep a small/lightweight summary
+of the .proto file. We call this a `upb_msglayout`. It contains the bare
+minimum of what we need to know to parse and serialize protobuf binary format
+into our internal representation for messages, `upb_msg`.
+
+The core then contains functions to parse/serialize a message, given a `upb_msg*`
+and a `const upb_msglayout*`.
+
+This approach is similar to [nanopb](https://github.com/nanopb/nanopb) which
+also compiles message definitions to a compact, internal representation without
+names. However nanopb does not aim to be a fully-featured library, and has no
+support for text format, JSON, or descriptors. μpb is unique in that it has a
+small core similar to nanopb (though not quite as small), but also offers a
+full-featured protobuf library for applications that want reflection, text
+format, JSON format, etc.
+
+Without descriptors, the core doesn't have access to field names, so it cannot
+parse/serialize to protobuf text format or JSON. Instead this functionality
+lives in separate modules that depend on the module implementing descriptors.
+With the descriptor module we can parse/serialize binary descriptors and
+validate that they follow all the rules of protobuf schemas.
+
+To provide binary compatibility, we version the structs that generated messages
+use to create a `upb_msglayout*`. The current initializers are
+`upb_msglayout_msginit_v1`, `upb_msglayout_fieldinit_v1`, etc. Then
+`upb_msglayout*` uses these as its internal representation. If upb changes its
+internal representation for a `upb_msglayout*`, it will also include code to
+convert the old representation to the new representation. This will use some
+more memory/CPU at runtime to convert between the two, but apps that statically
+link μpb will never need to worry about this.
+
+TODO
+----
+
+1. revise our generated code until it is in a state where we feel comfortable
+ committing to API/ABI stability for it. In particular there is an open
+ question of whether non-ABI-compatible field accesses should have a
+ fastpath different from the ABI-compatible field access.
+1. Add missing features (maps, extensions, unknown fields).
+1. Flesh out C++ wrappers.
+1. *(lower-priority)*: revise all of the existing encoders/decoders and
+ handlers. We probably will want to keep handlers, since they let us decouple
+ encoders/decoders from `upb_msg`, but we need to simplify all of that a LOT.
+ Likely we will want to make handlers only per-message instead of per-field,
+ except for variable-length fields.
diff --git a/contrib/libs/grpc/third_party/upb/LICENSE b/contrib/libs/grpc/third_party/upb/LICENSE
new file mode 100644
index 0000000000..da939845db
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/LICENSE
@@ -0,0 +1,26 @@
+
+Copyright (c) 2009-2011, Google Inc.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of Google Inc. nor the names of any other
+ contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+EVENT SHALL GOOGLE INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
diff --git a/contrib/libs/grpc/third_party/upb/README.md b/contrib/libs/grpc/third_party/upb/README.md
new file mode 100644
index 0000000000..a08aa0e909
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/README.md
@@ -0,0 +1,134 @@
+
+# μpb - a small protobuf implementation in C
+
+|Platform|Build Status|
+|--------|------------|
+|macOS|[![Build Status](https://storage.googleapis.com/upb-kokoro-results/status-badge/macos.png)](https://fusion.corp.google.com/projectanalysis/summary/KOKORO/prod%3Aupb%2Fmacos%2Fcontinuous)|
+|ubuntu|[![Build Status](https://storage.googleapis.com/upb-kokoro-results/status-badge/ubuntu.png)](https://fusion.corp.google.com/projectanalysis/summary/KOKORO/prod%3Aupb%2Fubuntu%2Fcontinuous)|
+
+μpb (often written 'upb') is a small protobuf implementation written in C.
+
+upb generates a C API for creating, parsing, and serializing messages
+as declared in `.proto` files. upb is heavily arena-based: all
+messages always live in an arena (note: the arena can live in stack or
+static memory if desired). Here is a simple example:
+
+```c
+#include "conformance/conformance.upb.h"
+
+void foo(const char* data, size_t size) {
+ upb_arena *arena;
+
+ /* Generated message type. */
+ conformance_ConformanceRequest *request;
+ conformance_ConformanceResponse *response;
+
+ arena = upb_arena_new();
+ request = conformance_ConformanceRequest_parse(data, size, arena);
+ response = conformance_ConformanceResponse_new(arena);
+
+ switch (conformance_ConformanceRequest_payload_case(request)) {
+ case conformance_ConformanceRequest_payload_protobuf_payload: {
+ upb_strview payload = conformance_ConformanceRequest_protobuf_payload(request);
+ // ...
+ break;
+ }
+
+ case conformance_ConformanceRequest_payload_NOT_SET:
+ fprintf(stderr, "conformance_upb: Request didn't have payload.\n");
+ break;
+
+ default: {
+ static const char msg[] = "Unsupported input format.";
+ conformance_ConformanceResponse_set_skipped(
+ response, upb_strview_make(msg, sizeof(msg)));
+ break;
+ }
+ }
+
+ /* Frees all messages on the arena. */
+ upb_arena_free(arena);
+}
+```
+
+API and ABI are both subject to change! Please do not distribute
+as a shared library for this reason (for now at least).
+
+## Using upb in your project
+
+Currently only Bazel is supported (CMake support is partial and incomplete
+but full CMake support is an eventual goal).
+
+To use upb in your Bazel project, first add upb to your `WORKSPACE` file,
+either as a `git_repository()` or as a `new_local_repository()` with a
+Git Submodule. (For an example, see `examples/bazel/ in this repo).
+
+```python
+# Add this to your WORKSPACE file.
+load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
+
+git_repository(
+ name = "upb",
+ remote = "https://github.com/protocolbuffers/upb.git",
+ commit = "d16bf99ac4658793748cda3251226059892b3b7b",
+)
+
+load("@upb//bazel:workspace_deps.bzl", "upb_deps")
+
+upb_deps()
+```
+
+Then in your BUILD file you can add `upb_proto_library()` rules that
+generate code for a corresponding `proto_library()` rule. For
+example:
+
+```python
+# Add this to your BUILD file.
+load("@upb//bazel:upb_proto_library.bzl", "upb_proto_library")
+
+proto_library(
+ name = "foo_proto",
+ srcs = ["foo.proto"],
+)
+
+upb_proto_library(
+ name = "foo_upbproto",
+ deps = [":foo_proto"],
+)
+
+cc_binary(
+ name = "test_binary",
+ srcs = ["test_binary.c"],
+ deps = [":foo_upbproto"],
+)
+```
+
+Then in your `.c` file you can #include the generated header:
+
+```c
+#include "foo.upb.h"
+
+/* Insert code that uses generated types. */
+```
+
+## Old "handlers" interfaces
+
+This library contains several semi-deprecated interfaces (see BUILD
+file for more info about which interfaces are deprecated). These
+deprecated interfaces are still used in some significant projects,
+such as the Ruby and PHP C bindings for protobuf in the [main protobuf
+repo](https://github.com/protocolbuffers/protobuf). The goal is to
+migrate the Ruby/PHP bindings to use the newer, simpler interfaces
+instead. Please do not use the old interfaces in new code.
+
+## Lua bindings
+
+This repo has some Lua bindings for the core library. These are
+experimental and very incomplete. These are currently included in
+order to validate that the C API is suitable for wrapping. As the
+project matures these Lua bindings may become publicly available.
+
+## Contact
+
+Author: Josh Haberman ([jhaberman@gmail.com](mailto:jhaberman@gmail.com),
+[haberman@google.com](mailto:haberman@google.com))
diff --git a/contrib/libs/grpc/third_party/upb/upb/decode.c b/contrib/libs/grpc/third_party/upb/upb/decode.c
new file mode 100644
index 0000000000..a9f1cf548a
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/decode.c
@@ -0,0 +1,621 @@
+
+#include <setjmp.h>
+#include <string.h>
+
+#include "upb/decode.h"
+#include "upb/upb.h"
+
+#include "upb/port_def.inc"
+
+/* Maps descriptor type -> upb field type. */
+static const uint8_t desctype_to_fieldtype[] = {
+ -1, /* invalid descriptor type */
+ UPB_TYPE_DOUBLE, /* DOUBLE */
+ UPB_TYPE_FLOAT, /* FLOAT */
+ UPB_TYPE_INT64, /* INT64 */
+ UPB_TYPE_UINT64, /* UINT64 */
+ UPB_TYPE_INT32, /* INT32 */
+ UPB_TYPE_UINT64, /* FIXED64 */
+ UPB_TYPE_UINT32, /* FIXED32 */
+ UPB_TYPE_BOOL, /* BOOL */
+ UPB_TYPE_STRING, /* STRING */
+ UPB_TYPE_MESSAGE, /* GROUP */
+ UPB_TYPE_MESSAGE, /* MESSAGE */
+ UPB_TYPE_BYTES, /* BYTES */
+ UPB_TYPE_UINT32, /* UINT32 */
+ UPB_TYPE_ENUM, /* ENUM */
+ UPB_TYPE_INT32, /* SFIXED32 */
+ UPB_TYPE_INT64, /* SFIXED64 */
+ UPB_TYPE_INT32, /* SINT32 */
+ UPB_TYPE_INT64, /* SINT64 */
+};
+
+/* Maps descriptor type -> upb map size. */
+static const uint8_t desctype_to_mapsize[] = {
+ -1, /* invalid descriptor type */
+ 8, /* DOUBLE */
+ 4, /* FLOAT */
+ 8, /* INT64 */
+ 8, /* UINT64 */
+ 4, /* INT32 */
+ 8, /* FIXED64 */
+ 4, /* FIXED32 */
+ 1, /* BOOL */
+ UPB_MAPTYPE_STRING, /* STRING */
+ sizeof(void *), /* GROUP */
+ sizeof(void *), /* MESSAGE */
+ UPB_MAPTYPE_STRING, /* BYTES */
+ 4, /* UINT32 */
+ 4, /* ENUM */
+ 4, /* SFIXED32 */
+ 8, /* SFIXED64 */
+ 4, /* SINT32 */
+ 8, /* SINT64 */
+};
+
+static const unsigned fixed32_ok = (1 << UPB_DTYPE_FLOAT) |
+ (1 << UPB_DTYPE_FIXED32) |
+ (1 << UPB_DTYPE_SFIXED32);
+
+static const unsigned fixed64_ok = (1 << UPB_DTYPE_DOUBLE) |
+ (1 << UPB_DTYPE_FIXED64) |
+ (1 << UPB_DTYPE_SFIXED64);
+
+/* Op: an action to be performed for a wire-type/field-type combination. */
+#define OP_SCALAR_LG2(n) (n) /* n in [0, 2, 3] => op in [0, 2, 3] */
+#define OP_STRING 4
+#define OP_BYTES 5
+#define OP_SUBMSG 6
+/* Ops above are scalar-only. Repeated fields can use any op. */
+#define OP_FIXPCK_LG2(n) (n + 5) /* n in [2, 3] => op in [7, 8] */
+#define OP_VARPCK_LG2(n) (n + 9) /* n in [0, 2, 3] => op in [9, 11, 12] */
+
+static const int8_t varint_ops[19] = {
+ -1, /* field not found */
+ -1, /* DOUBLE */
+ -1, /* FLOAT */
+ OP_SCALAR_LG2(3), /* INT64 */
+ OP_SCALAR_LG2(3), /* UINT64 */
+ OP_SCALAR_LG2(2), /* INT32 */
+ -1, /* FIXED64 */
+ -1, /* FIXED32 */
+ OP_SCALAR_LG2(0), /* BOOL */
+ -1, /* STRING */
+ -1, /* GROUP */
+ -1, /* MESSAGE */
+ -1, /* BYTES */
+ OP_SCALAR_LG2(2), /* UINT32 */
+ OP_SCALAR_LG2(2), /* ENUM */
+ -1, /* SFIXED32 */
+ -1, /* SFIXED64 */
+ OP_SCALAR_LG2(2), /* SINT32 */
+ OP_SCALAR_LG2(3), /* SINT64 */
+};
+
+static const int8_t delim_ops[37] = {
+ /* For non-repeated field type. */
+ -1, /* field not found */
+ -1, /* DOUBLE */
+ -1, /* FLOAT */
+ -1, /* INT64 */
+ -1, /* UINT64 */
+ -1, /* INT32 */
+ -1, /* FIXED64 */
+ -1, /* FIXED32 */
+ -1, /* BOOL */
+ OP_STRING, /* STRING */
+ -1, /* GROUP */
+ OP_SUBMSG, /* MESSAGE */
+ OP_BYTES, /* BYTES */
+ -1, /* UINT32 */
+ -1, /* ENUM */
+ -1, /* SFIXED32 */
+ -1, /* SFIXED64 */
+ -1, /* SINT32 */
+ -1, /* SINT64 */
+ /* For repeated field type. */
+ OP_FIXPCK_LG2(3), /* REPEATED DOUBLE */
+ OP_FIXPCK_LG2(2), /* REPEATED FLOAT */
+ OP_VARPCK_LG2(3), /* REPEATED INT64 */
+ OP_VARPCK_LG2(3), /* REPEATED UINT64 */
+ OP_VARPCK_LG2(2), /* REPEATED INT32 */
+ OP_FIXPCK_LG2(3), /* REPEATED FIXED64 */
+ OP_FIXPCK_LG2(2), /* REPEATED FIXED32 */
+ OP_VARPCK_LG2(0), /* REPEATED BOOL */
+ OP_STRING, /* REPEATED STRING */
+ OP_SUBMSG, /* REPEATED GROUP */
+ OP_SUBMSG, /* REPEATED MESSAGE */
+ OP_BYTES, /* REPEATED BYTES */
+ OP_VARPCK_LG2(2), /* REPEATED UINT32 */
+ OP_VARPCK_LG2(2), /* REPEATED ENUM */
+ OP_FIXPCK_LG2(2), /* REPEATED SFIXED32 */
+ OP_FIXPCK_LG2(3), /* REPEATED SFIXED64 */
+ OP_VARPCK_LG2(2), /* REPEATED SINT32 */
+ OP_VARPCK_LG2(3), /* REPEATED SINT64 */
+};
+
+/* Data pertaining to the parse. */
+typedef struct {
+ const char *limit; /* End of delimited region or end of buffer. */
+ upb_arena *arena;
+ int depth;
+ uint32_t end_group; /* Set to field number of END_GROUP tag, if any. */
+ jmp_buf err;
+} upb_decstate;
+
+typedef union {
+ bool bool_val;
+ uint32_t uint32_val;
+ uint64_t uint64_val;
+ upb_strview str_val;
+} wireval;
+
+static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
+ const upb_msglayout *layout);
+
+UPB_NORETURN static void decode_err(upb_decstate *d) { longjmp(d->err, 1); }
+
+void decode_verifyutf8(upb_decstate *d, const char *buf, int len) {
+ static const uint8_t utf8_offset[] = {
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+ 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0,
+ };
+
+ int i, j;
+ uint8_t offset;
+
+ i = 0;
+ while (i < len) {
+ offset = utf8_offset[(uint8_t)buf[i]];
+ if (offset == 0 || i + offset > len) {
+ decode_err(d);
+ }
+ for (j = i + 1; j < i + offset; j++) {
+ if ((buf[j] & 0xc0) != 0x80) {
+ decode_err(d);
+ }
+ }
+ i += offset;
+ }
+ if (i != len) decode_err(d);
+}
+
+static bool decode_reserve(upb_decstate *d, upb_array *arr, size_t elem) {
+ bool need_realloc = arr->size - arr->len < elem;
+ if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, d->arena)) {
+ decode_err(d);
+ }
+ return need_realloc;
+}
+
+UPB_NOINLINE
+static const char *decode_longvarint64(upb_decstate *d, const char *ptr,
+ const char *limit, uint64_t *val) {
+ uint8_t byte;
+ int bitpos = 0;
+ uint64_t out = 0;
+
+ do {
+ if (bitpos >= 70 || ptr == limit) decode_err(d);
+ byte = *ptr;
+ out |= (uint64_t)(byte & 0x7F) << bitpos;
+ ptr++;
+ bitpos += 7;
+ } while (byte & 0x80);
+
+ *val = out;
+ return ptr;
+}
+
+UPB_FORCEINLINE
+static const char *decode_varint64(upb_decstate *d, const char *ptr,
+ const char *limit, uint64_t *val) {
+ if (UPB_LIKELY(ptr < limit && (*ptr & 0x80) == 0)) {
+ *val = (uint8_t)*ptr;
+ return ptr + 1;
+ } else {
+ return decode_longvarint64(d, ptr, limit, val);
+ }
+}
+
+static const char *decode_varint32(upb_decstate *d, const char *ptr,
+ const char *limit, uint32_t *val) {
+ uint64_t u64;
+ ptr = decode_varint64(d, ptr, limit, &u64);
+ if (u64 > UINT32_MAX) decode_err(d);
+ *val = (uint32_t)u64;
+ return ptr;
+}
+
+static void decode_munge(int type, wireval *val) {
+ switch (type) {
+ case UPB_DESCRIPTOR_TYPE_BOOL:
+ val->bool_val = val->uint64_val != 0;
+ break;
+ case UPB_DESCRIPTOR_TYPE_SINT32: {
+ uint32_t n = val->uint32_val;
+ val->uint32_val = (n >> 1) ^ -(int32_t)(n & 1);
+ break;
+ }
+ case UPB_DESCRIPTOR_TYPE_SINT64: {
+ uint64_t n = val->uint64_val;
+ val->uint64_val = (n >> 1) ^ -(int64_t)(n & 1);
+ break;
+ }
+ case UPB_DESCRIPTOR_TYPE_INT32:
+ case UPB_DESCRIPTOR_TYPE_UINT32:
+ if (!_upb_isle()) {
+ /* The next stage will memcpy(dst, &val, 4) */
+ val->uint32_val = val->uint64_val;
+ }
+ break;
+ }
+}
+
+static const upb_msglayout_field *upb_find_field(const upb_msglayout *l,
+ uint32_t field_number) {
+ static upb_msglayout_field none = {0, 0, 0, 0, 0, 0};
+
+ /* Lots of optimization opportunities here. */
+ int i;
+ if (l == NULL) return &none;
+ for (i = 0; i < l->field_count; i++) {
+ if (l->fields[i].number == field_number) {
+ return &l->fields[i];
+ }
+ }
+
+ return &none; /* Unknown field. */
+}
+
+static upb_msg *decode_newsubmsg(upb_decstate *d, const upb_msglayout *layout,
+ const upb_msglayout_field *field) {
+ const upb_msglayout *subl = layout->submsgs[field->submsg_index];
+ return _upb_msg_new(subl, d->arena);
+}
+
+static void decode_tosubmsg(upb_decstate *d, upb_msg *submsg,
+ const upb_msglayout *layout,
+ const upb_msglayout_field *field, upb_strview val) {
+ const upb_msglayout *subl = layout->submsgs[field->submsg_index];
+ const char *saved_limit = d->limit;
+ if (--d->depth < 0) decode_err(d);
+ d->limit = val.data + val.size;
+ decode_msg(d, val.data, submsg, subl);
+ d->limit = saved_limit;
+ if (d->end_group != 0) decode_err(d);
+ d->depth++;
+}
+
+static const char *decode_group(upb_decstate *d, const char *ptr,
+ upb_msg *submsg, const upb_msglayout *subl,
+ uint32_t number) {
+ if (--d->depth < 0) decode_err(d);
+ ptr = decode_msg(d, ptr, submsg, subl);
+ if (d->end_group != number) decode_err(d);
+ d->end_group = 0;
+ d->depth++;
+ return ptr;
+}
+
+static const char *decode_togroup(upb_decstate *d, const char *ptr,
+ upb_msg *submsg, const upb_msglayout *layout,
+ const upb_msglayout_field *field) {
+ const upb_msglayout *subl = layout->submsgs[field->submsg_index];
+ return decode_group(d, ptr, submsg, subl, field->number);
+}
+
+static const char *decode_toarray(upb_decstate *d, const char *ptr,
+ upb_msg *msg, const upb_msglayout *layout,
+ const upb_msglayout_field *field, wireval val,
+ int op) {
+ upb_array **arrp = UPB_PTR_AT(msg, field->offset, void);
+ upb_array *arr = *arrp;
+ void *mem;
+
+ if (!arr) {
+ upb_fieldtype_t type = desctype_to_fieldtype[field->descriptortype];
+ arr = _upb_array_new(d->arena, type);
+ if (!arr) decode_err(d);
+ *arrp = arr;
+ }
+
+ decode_reserve(d, arr, 1);
+
+ switch (op) {
+ case OP_SCALAR_LG2(0):
+ case OP_SCALAR_LG2(2):
+ case OP_SCALAR_LG2(3):
+ /* Append scalar value. */
+ mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << op, void);
+ arr->len++;
+ memcpy(mem, &val, 1 << op);
+ return ptr;
+ case OP_STRING:
+ decode_verifyutf8(d, val.str_val.data, val.str_val.size);
+ /* Fallthrough. */
+ case OP_BYTES:
+ /* Append bytes. */
+ mem =
+ UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(upb_strview), void);
+ arr->len++;
+ memcpy(mem, &val, sizeof(upb_strview));
+ return ptr;
+ case OP_SUBMSG: {
+ /* Append submessage / group. */
+ upb_msg *submsg = decode_newsubmsg(d, layout, field);
+ *UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(void *), upb_msg *) =
+ submsg;
+ arr->len++;
+ if (UPB_UNLIKELY(field->descriptortype == UPB_DTYPE_GROUP)) {
+ ptr = decode_togroup(d, ptr, submsg, layout, field);
+ } else {
+ decode_tosubmsg(d, submsg, layout, field, val.str_val);
+ }
+ return ptr;
+ }
+ case OP_FIXPCK_LG2(2):
+ case OP_FIXPCK_LG2(3): {
+ /* Fixed packed. */
+ int lg2 = op - OP_FIXPCK_LG2(0);
+ int mask = (1 << lg2) - 1;
+ size_t count = val.str_val.size >> lg2;
+ if ((val.str_val.size & mask) != 0) {
+ decode_err(d); /* Length isn't a round multiple of elem size. */
+ }
+ decode_reserve(d, arr, count);
+ mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
+ arr->len += count;
+ memcpy(mem, val.str_val.data, val.str_val.size);
+ return ptr;
+ }
+ case OP_VARPCK_LG2(0):
+ case OP_VARPCK_LG2(2):
+ case OP_VARPCK_LG2(3): {
+ /* Varint packed. */
+ int lg2 = op - OP_VARPCK_LG2(0);
+ int scale = 1 << lg2;
+ const char *ptr = val.str_val.data;
+ const char *end = ptr + val.str_val.size;
+ char *out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
+ while (ptr < end) {
+ wireval elem;
+ ptr = decode_varint64(d, ptr, end, &elem.uint64_val);
+ decode_munge(field->descriptortype, &elem);
+ if (decode_reserve(d, arr, 1)) {
+ out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
+ }
+ arr->len++;
+ memcpy(out, &elem, scale);
+ out += scale;
+ }
+ if (ptr != end) decode_err(d);
+ return ptr;
+ }
+ default:
+ UPB_UNREACHABLE();
+ }
+}
+
+static void decode_tomap(upb_decstate *d, upb_msg *msg,
+ const upb_msglayout *layout,
+ const upb_msglayout_field *field, wireval val) {
+ upb_map **map_p = UPB_PTR_AT(msg, field->offset, upb_map *);
+ upb_map *map = *map_p;
+ upb_map_entry ent;
+ const upb_msglayout *entry = layout->submsgs[field->submsg_index];
+
+ if (!map) {
+ /* Lazily create map. */
+ const upb_msglayout *entry = layout->submsgs[field->submsg_index];
+ const upb_msglayout_field *key_field = &entry->fields[0];
+ const upb_msglayout_field *val_field = &entry->fields[1];
+ char key_size = desctype_to_mapsize[key_field->descriptortype];
+ char val_size = desctype_to_mapsize[val_field->descriptortype];
+ UPB_ASSERT(key_field->offset == 0);
+ UPB_ASSERT(val_field->offset == sizeof(upb_strview));
+ map = _upb_map_new(d->arena, key_size, val_size);
+ *map_p = map;
+ }
+
+ /* Parse map entry. */
+ memset(&ent, 0, sizeof(ent));
+
+ if (entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
+ entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_GROUP) {
+ /* Create proactively to handle the case where it doesn't appear. */
+ ent.v.val = upb_value_ptr(_upb_msg_new(entry->submsgs[0], d->arena));
+ }
+
+ decode_tosubmsg(d, &ent.k, layout, field, val.str_val);
+
+ /* Insert into map. */
+ _upb_map_set(map, &ent.k, map->key_size, &ent.v, map->val_size, d->arena);
+}
+
+static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg,
+ const upb_msglayout *layout,
+ const upb_msglayout_field *field, wireval val,
+ int op) {
+ void *mem = UPB_PTR_AT(msg, field->offset, void);
+ int type = field->descriptortype;
+
+ /* Set presence if necessary. */
+ if (field->presence < 0) {
+ /* Oneof case */
+ uint32_t *oneof_case = _upb_oneofcase_field(msg, field);
+ if (op == OP_SUBMSG && *oneof_case != field->number) {
+ memset(mem, 0, sizeof(void*));
+ }
+ *oneof_case = field->number;
+ } else if (field->presence > 0) {
+ _upb_sethas_field(msg, field);
+ }
+
+ /* Store into message. */
+ switch (op) {
+ case OP_SUBMSG: {
+ upb_msg **submsgp = mem;
+ upb_msg *submsg = *submsgp;
+ if (!submsg) {
+ submsg = decode_newsubmsg(d, layout, field);
+ *submsgp = submsg;
+ }
+ if (UPB_UNLIKELY(type == UPB_DTYPE_GROUP)) {
+ ptr = decode_togroup(d, ptr, submsg, layout, field);
+ } else {
+ decode_tosubmsg(d, submsg, layout, field, val.str_val);
+ }
+ break;
+ }
+ case OP_STRING:
+ decode_verifyutf8(d, val.str_val.data, val.str_val.size);
+ /* Fallthrough. */
+ case OP_BYTES:
+ memcpy(mem, &val, sizeof(upb_strview));
+ break;
+ case OP_SCALAR_LG2(3):
+ memcpy(mem, &val, 8);
+ break;
+ case OP_SCALAR_LG2(2):
+ memcpy(mem, &val, 4);
+ break;
+ case OP_SCALAR_LG2(0):
+ memcpy(mem, &val, 1);
+ break;
+ default:
+ UPB_UNREACHABLE();
+ }
+
+ return ptr;
+}
+
+static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
+ const upb_msglayout *layout) {
+ while (ptr < d->limit) {
+ uint32_t tag;
+ const upb_msglayout_field *field;
+ int field_number;
+ int wire_type;
+ const char *field_start = ptr;
+ wireval val;
+ int op;
+
+ ptr = decode_varint32(d, ptr, d->limit, &tag);
+ field_number = tag >> 3;
+ wire_type = tag & 7;
+
+ field = upb_find_field(layout, field_number);
+
+ switch (wire_type) {
+ case UPB_WIRE_TYPE_VARINT:
+ ptr = decode_varint64(d, ptr, d->limit, &val.uint64_val);
+ op = varint_ops[field->descriptortype];
+ decode_munge(field->descriptortype, &val);
+ break;
+ case UPB_WIRE_TYPE_32BIT:
+ if (d->limit - ptr < 4) decode_err(d);
+ memcpy(&val.uint32_val, ptr, 4);
+ val.uint32_val = _upb_be_swap32(val.uint32_val);
+ ptr += 4;
+ op = OP_SCALAR_LG2(2);
+ if (((1 << field->descriptortype) & fixed32_ok) == 0) goto unknown;
+ break;
+ case UPB_WIRE_TYPE_64BIT:
+ if (d->limit - ptr < 8) decode_err(d);
+ memcpy(&val.uint64_val, ptr, 8);
+ val.uint64_val = _upb_be_swap64(val.uint64_val);
+ ptr += 8;
+ op = OP_SCALAR_LG2(3);
+ if (((1 << field->descriptortype) & fixed64_ok) == 0) goto unknown;
+ break;
+ case UPB_WIRE_TYPE_DELIMITED: {
+ uint32_t size;
+ int ndx = field->descriptortype;
+ if (_upb_isrepeated(field)) ndx += 18;
+ ptr = decode_varint32(d, ptr, d->limit, &size);
+ if (size >= INT32_MAX || (size_t)(d->limit - ptr) < size) {
+ decode_err(d); /* Length overflow. */
+ }
+ val.str_val.data = ptr;
+ val.str_val.size = size;
+ ptr += size;
+ op = delim_ops[ndx];
+ break;
+ }
+ case UPB_WIRE_TYPE_START_GROUP:
+ val.uint32_val = field_number;
+ op = OP_SUBMSG;
+ if (field->descriptortype != UPB_DTYPE_GROUP) goto unknown;
+ break;
+ case UPB_WIRE_TYPE_END_GROUP:
+ d->end_group = field_number;
+ return ptr;
+ default:
+ decode_err(d);
+ }
+
+ if (op >= 0) {
+ /* Parse, using op for dispatch. */
+ switch (field->label) {
+ case UPB_LABEL_REPEATED:
+ case _UPB_LABEL_PACKED:
+ ptr = decode_toarray(d, ptr, msg, layout, field, val, op);
+ break;
+ case _UPB_LABEL_MAP:
+ decode_tomap(d, msg, layout, field, val);
+ break;
+ default:
+ ptr = decode_tomsg(d, ptr, msg, layout, field, val, op);
+ break;
+ }
+ } else {
+ unknown:
+ /* Skip unknown field. */
+ if (field_number == 0) decode_err(d);
+ if (wire_type == UPB_WIRE_TYPE_START_GROUP) {
+ ptr = decode_group(d, ptr, NULL, NULL, field_number);
+ }
+ if (msg) {
+ if (!_upb_msg_addunknown(msg, field_start, ptr - field_start,
+ d->arena)) {
+ decode_err(d);
+ }
+ }
+ }
+ }
+
+ if (ptr != d->limit) decode_err(d);
+ return ptr;
+}
+
+bool upb_decode(const char *buf, size_t size, void *msg, const upb_msglayout *l,
+ upb_arena *arena) {
+ upb_decstate state;
+ state.limit = buf + size;
+ state.arena = arena;
+ state.depth = 64;
+ state.end_group = 0;
+
+ if (setjmp(state.err)) return false;
+
+ if (size == 0) return true;
+ decode_msg(&state, buf, msg, l);
+
+ return state.end_group == 0;
+}
+
+#undef OP_SCALAR_LG2
+#undef OP_FIXPCK_LG2
+#undef OP_VARPCK_LG2
+#undef OP_STRING
+#undef OP_SUBMSG
diff --git a/contrib/libs/grpc/third_party/upb/upb/decode.h b/contrib/libs/grpc/third_party/upb/upb/decode.h
new file mode 100644
index 0000000000..9de8638de5
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/decode.h
@@ -0,0 +1,21 @@
+/*
+** upb_decode: parsing into a upb_msg using a upb_msglayout.
+*/
+
+#ifndef UPB_DECODE_H_
+#define UPB_DECODE_H_
+
+#include "upb/msg.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+bool upb_decode(const char *buf, size_t size, upb_msg *msg,
+ const upb_msglayout *l, upb_arena *arena);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* UPB_DECODE_H_ */
diff --git a/contrib/libs/grpc/third_party/upb/upb/encode.c b/contrib/libs/grpc/third_party/upb/upb/encode.c
new file mode 100644
index 0000000000..a6ce62bfa5
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/encode.c
@@ -0,0 +1,420 @@
+/* We encode backwards, to avoid pre-computing lengths (one-pass encode). */
+
+#include "upb/encode.h"
+
+#include <string.h>
+
+#include "upb/msg.h"
+#include "upb/upb.h"
+
+#include "upb/port_def.inc"
+
+#define UPB_PB_VARINT_MAX_LEN 10
+#define CHK(x) do { if (!(x)) { return false; } } while(0)
+
+static size_t upb_encode_varint(uint64_t val, char *buf) {
+ size_t i;
+ if (val < 128) { buf[0] = val; return 1; }
+ i = 0;
+ while (val) {
+ uint8_t byte = val & 0x7fU;
+ val >>= 7;
+ if (val) byte |= 0x80U;
+ buf[i++] = byte;
+ }
+ return i;
+}
+
+static uint32_t upb_zzencode_32(int32_t n) { return ((uint32_t)n << 1) ^ (n >> 31); }
+static uint64_t upb_zzencode_64(int64_t n) { return ((uint64_t)n << 1) ^ (n >> 63); }
+
+typedef struct {
+ upb_alloc *alloc;
+ char *buf, *ptr, *limit;
+} upb_encstate;
+
+static size_t upb_roundup_pow2(size_t bytes) {
+ size_t ret = 128;
+ while (ret < bytes) {
+ ret *= 2;
+ }
+ return ret;
+}
+
+static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
+ size_t old_size = e->limit - e->buf;
+ size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
+ char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size);
+ CHK(new_buf);
+
+ /* We want previous data at the end, realloc() put it at the beginning. */
+ if (old_size > 0) {
+ memmove(new_buf + new_size - old_size, e->buf, old_size);
+ }
+
+ e->ptr = new_buf + new_size - (e->limit - e->ptr);
+ e->limit = new_buf + new_size;
+ e->buf = new_buf;
+ return true;
+}
+
+/* Call to ensure that at least "bytes" bytes are available for writing at
+ * e->ptr. Returns false if the bytes could not be allocated. */
+static bool upb_encode_reserve(upb_encstate *e, size_t bytes) {
+ CHK(UPB_LIKELY((size_t)(e->ptr - e->buf) >= bytes) ||
+ upb_encode_growbuffer(e, bytes));
+
+ e->ptr -= bytes;
+ return true;
+}
+
+/* Writes the given bytes to the buffer, handling reserve/advance. */
+static bool upb_put_bytes(upb_encstate *e, const void *data, size_t len) {
+ if (len == 0) return true;
+ CHK(upb_encode_reserve(e, len));
+ memcpy(e->ptr, data, len);
+ return true;
+}
+
+static bool upb_put_fixed64(upb_encstate *e, uint64_t val) {
+ val = _upb_be_swap64(val);
+ return upb_put_bytes(e, &val, sizeof(uint64_t));
+}
+
+static bool upb_put_fixed32(upb_encstate *e, uint32_t val) {
+ val = _upb_be_swap32(val);
+ return upb_put_bytes(e, &val, sizeof(uint32_t));
+}
+
+static bool upb_put_varint(upb_encstate *e, uint64_t val) {
+ size_t len;
+ char *start;
+ CHK(upb_encode_reserve(e, UPB_PB_VARINT_MAX_LEN));
+ len = upb_encode_varint(val, e->ptr);
+ start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
+ memmove(start, e->ptr, len);
+ e->ptr = start;
+ return true;
+}
+
+static bool upb_put_double(upb_encstate *e, double d) {
+ uint64_t u64;
+ UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
+ memcpy(&u64, &d, sizeof(uint64_t));
+ return upb_put_fixed64(e, u64);
+}
+
+static bool upb_put_float(upb_encstate *e, float d) {
+ uint32_t u32;
+ UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
+ memcpy(&u32, &d, sizeof(uint32_t));
+ return upb_put_fixed32(e, u32);
+}
+
+static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) {
+ return upb_put_varint(e, (field_number << 3) | wire_type);
+}
+
+static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr,
+ size_t elem_size, uint32_t tag) {
+ size_t bytes = arr->len * elem_size;
+ const char* data = _upb_array_constptr(arr);
+ const char* ptr = data + bytes - elem_size;
+ if (tag) {
+ while (true) {
+ CHK(upb_put_bytes(e, ptr, elem_size) && upb_put_varint(e, tag));
+ if (ptr == data) break;
+ ptr -= elem_size;
+ }
+ return true;
+ } else {
+ return upb_put_bytes(e, data, bytes) && upb_put_varint(e, bytes);
+ }
+}
+
+bool upb_encode_message(upb_encstate *e, const char *msg,
+ const upb_msglayout *m, size_t *size);
+
+static bool upb_encode_scalarfield(upb_encstate *e, const void *_field_mem,
+ const upb_msglayout *m,
+ const upb_msglayout_field *f,
+ bool skip_zero_value) {
+ const char *field_mem = _field_mem;
+#define CASE(ctype, type, wire_type, encodeval) do { \
+ ctype val = *(ctype*)field_mem; \
+ if (skip_zero_value && val == 0) { \
+ return true; \
+ } \
+ return upb_put_ ## type(e, encodeval) && \
+ upb_put_tag(e, f->number, wire_type); \
+} while(0)
+
+ switch (f->descriptortype) {
+ case UPB_DESCRIPTOR_TYPE_DOUBLE:
+ CASE(double, double, UPB_WIRE_TYPE_64BIT, val);
+ case UPB_DESCRIPTOR_TYPE_FLOAT:
+ CASE(float, float, UPB_WIRE_TYPE_32BIT, val);
+ case UPB_DESCRIPTOR_TYPE_INT64:
+ case UPB_DESCRIPTOR_TYPE_UINT64:
+ CASE(uint64_t, varint, UPB_WIRE_TYPE_VARINT, val);
+ case UPB_DESCRIPTOR_TYPE_UINT32:
+ CASE(uint32_t, varint, UPB_WIRE_TYPE_VARINT, val);
+ case UPB_DESCRIPTOR_TYPE_INT32:
+ case UPB_DESCRIPTOR_TYPE_ENUM:
+ CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, (int64_t)val);
+ case UPB_DESCRIPTOR_TYPE_SFIXED64:
+ case UPB_DESCRIPTOR_TYPE_FIXED64:
+ CASE(uint64_t, fixed64, UPB_WIRE_TYPE_64BIT, val);
+ case UPB_DESCRIPTOR_TYPE_FIXED32:
+ case UPB_DESCRIPTOR_TYPE_SFIXED32:
+ CASE(uint32_t, fixed32, UPB_WIRE_TYPE_32BIT, val);
+ case UPB_DESCRIPTOR_TYPE_BOOL:
+ CASE(bool, varint, UPB_WIRE_TYPE_VARINT, val);
+ case UPB_DESCRIPTOR_TYPE_SINT32:
+ CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_32(val));
+ case UPB_DESCRIPTOR_TYPE_SINT64:
+ CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
+ case UPB_DESCRIPTOR_TYPE_STRING:
+ case UPB_DESCRIPTOR_TYPE_BYTES: {
+ upb_strview view = *(upb_strview*)field_mem;
+ if (skip_zero_value && view.size == 0) {
+ return true;
+ }
+ return upb_put_bytes(e, view.data, view.size) &&
+ upb_put_varint(e, view.size) &&
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
+ }
+ case UPB_DESCRIPTOR_TYPE_GROUP: {
+ size_t size;
+ void *submsg = *(void **)field_mem;
+ const upb_msglayout *subm = m->submsgs[f->submsg_index];
+ if (submsg == NULL) {
+ return true;
+ }
+ return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
+ upb_encode_message(e, submsg, subm, &size) &&
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
+ }
+ case UPB_DESCRIPTOR_TYPE_MESSAGE: {
+ size_t size;
+ void *submsg = *(void **)field_mem;
+ const upb_msglayout *subm = m->submsgs[f->submsg_index];
+ if (submsg == NULL) {
+ return true;
+ }
+ return upb_encode_message(e, submsg, subm, &size) &&
+ upb_put_varint(e, size) &&
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
+ }
+ }
+#undef CASE
+ UPB_UNREACHABLE();
+}
+
+static bool upb_encode_array(upb_encstate *e, const char *field_mem,
+ const upb_msglayout *m,
+ const upb_msglayout_field *f) {
+ const upb_array *arr = *(const upb_array**)field_mem;
+ bool packed = f->label == _UPB_LABEL_PACKED;
+
+ if (arr == NULL || arr->len == 0) {
+ return true;
+ }
+
+#define VARINT_CASE(ctype, encode) \
+ { \
+ const ctype *start = _upb_array_constptr(arr); \
+ const ctype *ptr = start + arr->len; \
+ size_t pre_len = e->limit - e->ptr; \
+ uint32_t tag = packed ? 0 : (f->number << 3) | UPB_WIRE_TYPE_VARINT; \
+ do { \
+ ptr--; \
+ CHK(upb_put_varint(e, encode)); \
+ if (tag) CHK(upb_put_varint(e, tag)); \
+ } while (ptr != start); \
+ if (!tag) CHK(upb_put_varint(e, e->limit - e->ptr - pre_len)); \
+ } \
+ break; \
+ do { \
+ ; \
+ } while (0)
+
+#define TAG(wire_type) (packed ? 0 : (f->number << 3 | wire_type))
+
+ switch (f->descriptortype) {
+ case UPB_DESCRIPTOR_TYPE_DOUBLE:
+ CHK(upb_put_fixedarray(e, arr, sizeof(double), TAG(UPB_WIRE_TYPE_64BIT)));
+ break;
+ case UPB_DESCRIPTOR_TYPE_FLOAT:
+ CHK(upb_put_fixedarray(e, arr, sizeof(float), TAG(UPB_WIRE_TYPE_32BIT)));
+ break;
+ case UPB_DESCRIPTOR_TYPE_SFIXED64:
+ case UPB_DESCRIPTOR_TYPE_FIXED64:
+ CHK(upb_put_fixedarray(e, arr, sizeof(uint64_t), TAG(UPB_WIRE_TYPE_64BIT)));
+ break;
+ case UPB_DESCRIPTOR_TYPE_FIXED32:
+ case UPB_DESCRIPTOR_TYPE_SFIXED32:
+ CHK(upb_put_fixedarray(e, arr, sizeof(uint32_t), TAG(UPB_WIRE_TYPE_32BIT)));
+ break;
+ case UPB_DESCRIPTOR_TYPE_INT64:
+ case UPB_DESCRIPTOR_TYPE_UINT64:
+ VARINT_CASE(uint64_t, *ptr);
+ case UPB_DESCRIPTOR_TYPE_UINT32:
+ VARINT_CASE(uint32_t, *ptr);
+ case UPB_DESCRIPTOR_TYPE_INT32:
+ case UPB_DESCRIPTOR_TYPE_ENUM:
+ VARINT_CASE(int32_t, (int64_t)*ptr);
+ case UPB_DESCRIPTOR_TYPE_BOOL:
+ VARINT_CASE(bool, *ptr);
+ case UPB_DESCRIPTOR_TYPE_SINT32:
+ VARINT_CASE(int32_t, upb_zzencode_32(*ptr));
+ case UPB_DESCRIPTOR_TYPE_SINT64:
+ VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
+ case UPB_DESCRIPTOR_TYPE_STRING:
+ case UPB_DESCRIPTOR_TYPE_BYTES: {
+ const upb_strview *start = _upb_array_constptr(arr);
+ const upb_strview *ptr = start + arr->len;
+ do {
+ ptr--;
+ CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
+ upb_put_varint(e, ptr->size) &&
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
+ } while (ptr != start);
+ return true;
+ }
+ case UPB_DESCRIPTOR_TYPE_GROUP: {
+ const void *const*start = _upb_array_constptr(arr);
+ const void *const*ptr = start + arr->len;
+ const upb_msglayout *subm = m->submsgs[f->submsg_index];
+ do {
+ size_t size;
+ ptr--;
+ CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
+ upb_encode_message(e, *ptr, subm, &size) &&
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP));
+ } while (ptr != start);
+ return true;
+ }
+ case UPB_DESCRIPTOR_TYPE_MESSAGE: {
+ const void *const*start = _upb_array_constptr(arr);
+ const void *const*ptr = start + arr->len;
+ const upb_msglayout *subm = m->submsgs[f->submsg_index];
+ do {
+ size_t size;
+ ptr--;
+ CHK(upb_encode_message(e, *ptr, subm, &size) &&
+ upb_put_varint(e, size) &&
+ upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
+ } while (ptr != start);
+ return true;
+ }
+ }
+#undef VARINT_CASE
+
+ if (packed) {
+ CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
+ }
+ return true;
+}
+
+static bool upb_encode_map(upb_encstate *e, const char *field_mem,
+ const upb_msglayout *m,
+ const upb_msglayout_field *f) {
+ const upb_map *map = *(const upb_map**)field_mem;
+ const upb_msglayout *entry = m->submsgs[f->submsg_index];
+ const upb_msglayout_field *key_field = &entry->fields[0];
+ const upb_msglayout_field *val_field = &entry->fields[1];
+ upb_strtable_iter i;
+ if (map == NULL) {
+ return true;
+ }
+
+ upb_strtable_begin(&i, &map->table);
+ for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
+ size_t pre_len = e->limit - e->ptr;
+ size_t size;
+ upb_strview key = upb_strtable_iter_key(&i);
+ const upb_value val = upb_strtable_iter_value(&i);
+ upb_map_entry ent;
+ _upb_map_fromkey(key, &ent.k, map->key_size);
+ _upb_map_fromvalue(val, &ent.v, map->val_size);
+ CHK(upb_encode_scalarfield(e, &ent.v, entry, val_field, false));
+ CHK(upb_encode_scalarfield(e, &ent.k, entry, key_field, false));
+ size = (e->limit - e->ptr) - pre_len;
+ CHK(upb_put_varint(e, size));
+ CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
+ }
+
+ return true;
+}
+
+
+bool upb_encode_message(upb_encstate *e, const char *msg,
+ const upb_msglayout *m, size_t *size) {
+ int i;
+ size_t pre_len = e->limit - e->ptr;
+ const char *unknown;
+ size_t unknown_size;
+
+ unknown = upb_msg_getunknown(msg, &unknown_size);
+
+ if (unknown) {
+ upb_put_bytes(e, unknown, unknown_size);
+ }
+
+ for (i = m->field_count - 1; i >= 0; i--) {
+ const upb_msglayout_field *f = &m->fields[i];
+
+ if (_upb_isrepeated(f)) {
+ CHK(upb_encode_array(e, msg + f->offset, m, f));
+ } else if (f->label == _UPB_LABEL_MAP) {
+ CHK(upb_encode_map(e, msg + f->offset, m, f));
+ } else {
+ bool skip_empty = false;
+ if (f->presence == 0) {
+ /* Proto3 presence. */
+ skip_empty = true;
+ } else if (f->presence > 0) {
+ /* Proto2 presence: hasbit. */
+ if (!_upb_hasbit_field(msg, f)) {
+ continue;
+ }
+ } else {
+ /* Field is in a oneof. */
+ if (_upb_getoneofcase_field(msg, f) != f->number) {
+ continue;
+ }
+ }
+ CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty));
+ }
+ }
+
+ *size = (e->limit - e->ptr) - pre_len;
+ return true;
+}
+
+char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena,
+ size_t *size) {
+ upb_encstate e;
+ e.alloc = upb_arena_alloc(arena);
+ e.buf = NULL;
+ e.limit = NULL;
+ e.ptr = NULL;
+
+ if (!upb_encode_message(&e, msg, m, size)) {
+ *size = 0;
+ return NULL;
+ }
+
+ *size = e.limit - e.ptr;
+
+ if (*size == 0) {
+ static char ch;
+ return &ch;
+ } else {
+ UPB_ASSERT(e.ptr);
+ return e.ptr;
+ }
+}
+
+#undef CHK
diff --git a/contrib/libs/grpc/third_party/upb/upb/encode.h b/contrib/libs/grpc/third_party/upb/upb/encode.h
new file mode 100644
index 0000000000..6842777058
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/encode.h
@@ -0,0 +1,21 @@
+/*
+** upb_encode: parsing into a upb_msg using a upb_msglayout.
+*/
+
+#ifndef UPB_ENCODE_H_
+#define UPB_ENCODE_H_
+
+#include "upb/msg.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena,
+ size_t *size);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* UPB_ENCODE_H_ */
diff --git a/contrib/libs/grpc/third_party/upb/upb/msg.c b/contrib/libs/grpc/third_party/upb/upb/msg.c
new file mode 100644
index 0000000000..25747c8481
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/msg.c
@@ -0,0 +1,177 @@
+
+#include "upb/msg.h"
+
+#include "upb/table.int.h"
+
+#include "upb/port_def.inc"
+
+/** upb_msg *******************************************************************/
+
+static const char _upb_fieldtype_to_sizelg2[12] = {
+ 0,
+ 0, /* UPB_TYPE_BOOL */
+ 2, /* UPB_TYPE_FLOAT */
+ 2, /* UPB_TYPE_INT32 */
+ 2, /* UPB_TYPE_UINT32 */
+ 2, /* UPB_TYPE_ENUM */
+ UPB_SIZE(2, 3), /* UPB_TYPE_MESSAGE */
+ 3, /* UPB_TYPE_DOUBLE */
+ 3, /* UPB_TYPE_INT64 */
+ 3, /* UPB_TYPE_UINT64 */
+ UPB_SIZE(3, 4), /* UPB_TYPE_STRING */
+ UPB_SIZE(3, 4), /* UPB_TYPE_BYTES */
+};
+
+static uintptr_t tag_arrptr(void* ptr, int elem_size_lg2) {
+ UPB_ASSERT(elem_size_lg2 <= 4);
+ return (uintptr_t)ptr | elem_size_lg2;
+}
+
+static int upb_msg_internalsize(const upb_msglayout *l) {
+ return sizeof(upb_msg_internal) - l->extendable * sizeof(void *);
+}
+
+static size_t upb_msg_sizeof(const upb_msglayout *l) {
+ return l->size + upb_msg_internalsize(l);
+}
+
+static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) {
+ ptrdiff_t size = sizeof(upb_msg_internal);
+ return UPB_PTR_AT(msg, -size, upb_msg_internal);
+}
+
+static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) {
+ return (upb_msg_internal*)upb_msg_getinternal_const(msg);
+}
+
+void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l) {
+ ptrdiff_t internal = upb_msg_internalsize(l);
+ void *mem = UPB_PTR_AT(msg, -internal, char);
+ memset(mem, 0, l->size + internal);
+}
+
+upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a) {
+ void *mem = upb_arena_malloc(a, upb_msg_sizeof(l));
+ upb_msg *msg;
+
+ if (!mem) {
+ return NULL;
+ }
+
+ msg = UPB_PTR_AT(mem, upb_msg_internalsize(l), upb_msg);
+ _upb_msg_clear(msg, l);
+ return msg;
+}
+
+bool _upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
+ upb_arena *arena) {
+ upb_msg_internal *in = upb_msg_getinternal(msg);
+ if (len > in->unknown_size - in->unknown_len) {
+ upb_alloc *alloc = upb_arena_alloc(arena);
+ size_t need = in->unknown_size + len;
+ size_t newsize = UPB_MAX(in->unknown_size * 2, need);
+ void *mem = upb_realloc(alloc, in->unknown, in->unknown_size, newsize);
+ if (!mem) return false;
+ in->unknown = mem;
+ in->unknown_size = newsize;
+ }
+ memcpy(in->unknown + in->unknown_len, data, len);
+ in->unknown_len += len;
+ return true;
+}
+
+void _upb_msg_discardunknown_shallow(upb_msg *msg) {
+ upb_msg_internal *in = upb_msg_getinternal(msg);
+ in->unknown_len = 0;
+}
+
+const char *upb_msg_getunknown(const upb_msg *msg, size_t *len) {
+ const upb_msg_internal *in = upb_msg_getinternal_const(msg);
+ *len = in->unknown_len;
+ return in->unknown;
+}
+
+/** upb_array *****************************************************************/
+
+upb_array *_upb_array_new(upb_arena *a, upb_fieldtype_t type) {
+ upb_array *arr = upb_arena_malloc(a, sizeof(upb_array));
+
+ if (!arr) {
+ return NULL;
+ }
+
+ arr->data = tag_arrptr(NULL, _upb_fieldtype_to_sizelg2[type]);
+ arr->len = 0;
+ arr->size = 0;
+
+ return arr;
+}
+
+bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena) {
+ size_t new_size = UPB_MAX(arr->size, 4);
+ int elem_size_lg2 = arr->data & 7;
+ size_t old_bytes = arr->size << elem_size_lg2;
+ size_t new_bytes;
+ void* ptr = _upb_array_ptr(arr);
+
+ /* Log2 ceiling of size. */
+ while (new_size < min_size) new_size *= 2;
+
+ new_bytes = new_size << elem_size_lg2;
+ ptr = upb_arena_realloc(arena, ptr, old_bytes, new_bytes);
+
+ if (!ptr) {
+ return false;
+ }
+
+ arr->data = tag_arrptr(ptr, elem_size_lg2);
+ arr->size = new_size;
+ return true;
+}
+
+static upb_array *getorcreate_array(upb_array **arr_ptr, upb_fieldtype_t type,
+ upb_arena *arena) {
+ upb_array *arr = *arr_ptr;
+ if (!arr) {
+ arr = _upb_array_new(arena, type);
+ if (!arr) return NULL;
+ *arr_ptr = arr;
+ }
+ return arr;
+}
+
+void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
+ upb_fieldtype_t type, upb_arena *arena) {
+ upb_array *arr = getorcreate_array(arr_ptr, type, arena);
+ return arr && _upb_array_resize(arr, size, arena) ? _upb_array_ptr(arr) : NULL;
+}
+
+bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
+ upb_fieldtype_t type, upb_arena *arena) {
+ upb_array *arr = getorcreate_array(arr_ptr, type, arena);
+ size_t elem = arr->len;
+ int lg2 = _upb_fieldtype_to_sizelg2[type];
+ char *data;
+
+ if (!arr || !_upb_array_resize(arr, elem + 1, arena)) return false;
+
+ data = _upb_array_ptr(arr);
+ memcpy(data + (elem << lg2), value, 1 << lg2);
+ return true;
+}
+
+/** upb_map *******************************************************************/
+
+upb_map *_upb_map_new(upb_arena *a, size_t key_size, size_t value_size) {
+ upb_map *map = upb_arena_malloc(a, sizeof(upb_map));
+
+ if (!map) {
+ return NULL;
+ }
+
+ upb_strtable_init2(&map->table, UPB_CTYPE_INT32, upb_arena_alloc(a));
+ map->key_size = key_size;
+ map->val_size = value_size;
+
+ return map;
+}
diff --git a/contrib/libs/grpc/third_party/upb/upb/msg.h b/contrib/libs/grpc/third_party/upb/upb/msg.h
new file mode 100644
index 0000000000..695c278b21
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/msg.h
@@ -0,0 +1,473 @@
+/*
+** Our memory representation for parsing tables and messages themselves.
+** Functions in this file are used by generated code and possibly reflection.
+**
+** The definitions in this file are internal to upb.
+**/
+
+#ifndef UPB_MSG_H_
+#define UPB_MSG_H_
+
+#include <stdint.h>
+#include <string.h>
+
+#include "upb/table.int.h"
+#include "upb/upb.h"
+
+#include "upb/port_def.inc"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs)
+
+typedef void upb_msg;
+
+/** upb_msglayout *************************************************************/
+
+/* upb_msglayout represents the memory layout of a given upb_msgdef. The
+ * members are public so generated code can initialize them, but users MUST NOT
+ * read or write any of its members. */
+
+/* These aren't real labels according to descriptor.proto, but in the table we
+ * use these for map/packed fields instead of UPB_LABEL_REPEATED. */
+enum {
+ _UPB_LABEL_MAP = 4,
+ _UPB_LABEL_PACKED = 7 /* Low 3 bits are common with UPB_LABEL_REPEATED. */
+};
+
+typedef struct {
+ uint32_t number;
+ uint16_t offset;
+ int16_t presence; /* If >0, hasbit_index. If <0, ~oneof_index. */
+ uint16_t submsg_index; /* undefined if descriptortype != MESSAGE or GROUP. */
+ uint8_t descriptortype;
+ uint8_t label; /* google.protobuf.Label or _UPB_LABEL_* above. */
+} upb_msglayout_field;
+
+typedef struct upb_msglayout {
+ const struct upb_msglayout *const* submsgs;
+ const upb_msglayout_field *fields;
+ /* Must be aligned to sizeof(void*). Doesn't include internal members like
+ * unknown fields, extension dict, pointer to msglayout, etc. */
+ uint16_t size;
+ uint16_t field_count;
+ bool extendable;
+} upb_msglayout;
+
+/** upb_msg *******************************************************************/
+
+/* Internal members of a upb_msg. We can change this without breaking binary
+ * compatibility. We put these before the user's data. The user's upb_msg*
+ * points after the upb_msg_internal. */
+
+/* Used when a message is not extendable. */
+typedef struct {
+ char *unknown;
+ size_t unknown_len;
+ size_t unknown_size;
+} upb_msg_internal;
+
+/* Used when a message is extendable. */
+typedef struct {
+ upb_inttable *extdict;
+ upb_msg_internal base;
+} upb_msg_internal_withext;
+
+/* Maps upb_fieldtype_t -> memory size. */
+extern char _upb_fieldtype_to_size[12];
+
+/* Creates a new messages with the given layout on the given arena. */
+upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a);
+
+/* Clears the given message. */
+void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l);
+
+/* Discards the unknown fields for this message only. */
+void _upb_msg_discardunknown_shallow(upb_msg *msg);
+
+/* Adds unknown data (serialized protobuf data) to the given message. The data
+ * is copied into the message instance. */
+bool _upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
+ upb_arena *arena);
+
+/* Returns a reference to the message's unknown data. */
+const char *upb_msg_getunknown(const upb_msg *msg, size_t *len);
+
+/** Hasbit access *************************************************************/
+
+UPB_INLINE bool _upb_hasbit(const upb_msg *msg, size_t idx) {
+ return (*PTR_AT(msg, idx / 8, const char) & (1 << (idx % 8))) != 0;
+}
+
+UPB_INLINE void _upb_sethas(const upb_msg *msg, size_t idx) {
+ (*PTR_AT(msg, idx / 8, char)) |= (char)(1 << (idx % 8));
+}
+
+UPB_INLINE void _upb_clearhas(const upb_msg *msg, size_t idx) {
+ (*PTR_AT(msg, idx / 8, char)) &= (char)(~(1 << (idx % 8)));
+}
+
+UPB_INLINE size_t _upb_msg_hasidx(const upb_msglayout_field *f) {
+ UPB_ASSERT(f->presence > 0);
+ return f->presence;
+}
+
+UPB_INLINE bool _upb_hasbit_field(const upb_msg *msg,
+ const upb_msglayout_field *f) {
+ return _upb_hasbit(msg, _upb_msg_hasidx(f));
+}
+
+UPB_INLINE void _upb_sethas_field(const upb_msg *msg,
+ const upb_msglayout_field *f) {
+ _upb_sethas(msg, _upb_msg_hasidx(f));
+}
+
+UPB_INLINE void _upb_clearhas_field(const upb_msg *msg,
+ const upb_msglayout_field *f) {
+ _upb_clearhas(msg, _upb_msg_hasidx(f));
+}
+
+/** Oneof case access *********************************************************/
+
+UPB_INLINE uint32_t *_upb_oneofcase(upb_msg *msg, size_t case_ofs) {
+ return PTR_AT(msg, case_ofs, uint32_t);
+}
+
+UPB_INLINE uint32_t _upb_getoneofcase(const void *msg, size_t case_ofs) {
+ return *PTR_AT(msg, case_ofs, uint32_t);
+}
+
+UPB_INLINE size_t _upb_oneofcase_ofs(const upb_msglayout_field *f) {
+ UPB_ASSERT(f->presence < 0);
+ return ~(ptrdiff_t)f->presence;
+}
+
+UPB_INLINE uint32_t *_upb_oneofcase_field(upb_msg *msg,
+ const upb_msglayout_field *f) {
+ return _upb_oneofcase(msg, _upb_oneofcase_ofs(f));
+}
+
+UPB_INLINE uint32_t _upb_getoneofcase_field(const upb_msg *msg,
+ const upb_msglayout_field *f) {
+ return _upb_getoneofcase(msg, _upb_oneofcase_ofs(f));
+}
+
+UPB_INLINE bool _upb_has_submsg_nohasbit(const upb_msg *msg, size_t ofs) {
+ return *PTR_AT(msg, ofs, const upb_msg*) != NULL;
+}
+
+UPB_INLINE bool _upb_isrepeated(const upb_msglayout_field *field) {
+ return (field->label & 3) == UPB_LABEL_REPEATED;
+}
+
+UPB_INLINE bool _upb_repeated_or_map(const upb_msglayout_field *field) {
+ return field->label >= UPB_LABEL_REPEATED;
+}
+
+/** upb_array *****************************************************************/
+
+/* Our internal representation for repeated fields. */
+typedef struct {
+ uintptr_t data; /* Tagged ptr: low 3 bits of ptr are lg2(elem size). */
+ size_t len; /* Measured in elements. */
+ size_t size; /* Measured in elements. */
+} upb_array;
+
+UPB_INLINE const void *_upb_array_constptr(const upb_array *arr) {
+ return (void*)(arr->data & ~(uintptr_t)7);
+}
+
+UPB_INLINE void *_upb_array_ptr(upb_array *arr) {
+ return (void*)_upb_array_constptr(arr);
+}
+
+/* Creates a new array on the given arena. */
+upb_array *_upb_array_new(upb_arena *a, upb_fieldtype_t type);
+
+/* Resizes the capacity of the array to be at least min_size. */
+bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena);
+
+/* Fallback functions for when the accessors require a resize. */
+void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
+ upb_fieldtype_t type, upb_arena *arena);
+bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
+ upb_fieldtype_t type, upb_arena *arena);
+
+UPB_INLINE bool _upb_array_reserve(upb_array *arr, size_t size,
+ upb_arena *arena) {
+ if (arr->size < size) return _upb_array_realloc(arr, size, arena);
+ return true;
+}
+
+UPB_INLINE bool _upb_array_resize(upb_array *arr, size_t size,
+ upb_arena *arena) {
+ if (!_upb_array_reserve(arr, size, arena)) return false;
+ arr->len = size;
+ return true;
+}
+
+UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs,
+ size_t *size) {
+ const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*);
+ if (arr) {
+ if (size) *size = arr->len;
+ return _upb_array_constptr(arr);
+ } else {
+ if (size) *size = 0;
+ return NULL;
+ }
+}
+
+UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs,
+ size_t *size) {
+ upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
+ if (arr) {
+ if (size) *size = arr->len;
+ return _upb_array_ptr(arr);
+ } else {
+ if (size) *size = 0;
+ return NULL;
+ }
+}
+
+UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
+ upb_fieldtype_t type,
+ upb_arena *arena) {
+ upb_array **arr_ptr = PTR_AT(msg, ofs, upb_array*);
+ upb_array *arr = *arr_ptr;
+ if (!arr || arr->size < size) {
+ return _upb_array_resize_fallback(arr_ptr, size, type, arena);
+ }
+ arr->len = size;
+ return _upb_array_ptr(arr);
+}
+
+
+UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
+ size_t elem_size,
+ upb_fieldtype_t type,
+ const void *value,
+ upb_arena *arena) {
+ upb_array **arr_ptr = PTR_AT(msg, ofs, upb_array*);
+ upb_array *arr = *arr_ptr;
+ void* ptr;
+ if (!arr || arr->len == arr->size) {
+ return _upb_array_append_fallback(arr_ptr, value, type, arena);
+ }
+ ptr = _upb_array_ptr(arr);
+ memcpy(PTR_AT(ptr, arr->len * elem_size, char), value, elem_size);
+ arr->len++;
+ return true;
+}
+
+/** upb_map *******************************************************************/
+
+/* Right now we use strmaps for everything. We'll likely want to use
+ * integer-specific maps for integer-keyed maps.*/
+typedef struct {
+ /* Size of key and val, based on the map type. Strings are represented as '0'
+ * because they must be handled specially. */
+ char key_size;
+ char val_size;
+
+ upb_strtable table;
+} upb_map;
+
+/* Map entries aren't actually stored, they are only used during parsing. For
+ * parsing, it helps a lot if all map entry messages have the same layout.
+ * The compiler and def.c must ensure that all map entries have this layout. */
+typedef struct {
+ upb_msg_internal internal;
+ union {
+ upb_strview str; /* For str/bytes. */
+ upb_value val; /* For all other types. */
+ } k;
+ union {
+ upb_strview str; /* For str/bytes. */
+ upb_value val; /* For all other types. */
+ } v;
+} upb_map_entry;
+
+/* Creates a new map on the given arena with this key/value type. */
+upb_map *_upb_map_new(upb_arena *a, size_t key_size, size_t value_size);
+
+/* Converting between internal table representation and user values.
+ *
+ * _upb_map_tokey() and _upb_map_fromkey() are inverses.
+ * _upb_map_tovalue() and _upb_map_fromvalue() are inverses.
+ *
+ * These functions account for the fact that strings are treated differently
+ * from other types when stored in a map.
+ */
+
+UPB_INLINE upb_strview _upb_map_tokey(const void *key, size_t size) {
+ if (size == UPB_MAPTYPE_STRING) {
+ return *(upb_strview*)key;
+ } else {
+ return upb_strview_make((const char*)key, size);
+ }
+}
+
+UPB_INLINE void _upb_map_fromkey(upb_strview key, void* out, size_t size) {
+ if (size == UPB_MAPTYPE_STRING) {
+ memcpy(out, &key, sizeof(key));
+ } else {
+ memcpy(out, key.data, size);
+ }
+}
+
+UPB_INLINE upb_value _upb_map_tovalue(const void *val, size_t size,
+ upb_arena *a) {
+ upb_value ret = {0};
+ if (size == UPB_MAPTYPE_STRING) {
+ upb_strview *strp = (upb_strview*)upb_arena_malloc(a, sizeof(*strp));
+ *strp = *(upb_strview*)val;
+ ret = upb_value_ptr(strp);
+ } else {
+ memcpy(&ret, val, size);
+ }
+ return ret;
+}
+
+UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size) {
+ if (size == UPB_MAPTYPE_STRING) {
+ const upb_strview *strp = (const upb_strview*)upb_value_getptr(val);
+ memcpy(out, strp, sizeof(upb_strview));
+ } else {
+ memcpy(out, &val, size);
+ }
+}
+
+/* Map operations, shared by reflection and generated code. */
+
+UPB_INLINE size_t _upb_map_size(const upb_map *map) {
+ return map->table.t.count;
+}
+
+UPB_INLINE bool _upb_map_get(const upb_map *map, const void *key,
+ size_t key_size, void *val, size_t val_size) {
+ upb_value tabval;
+ upb_strview k = _upb_map_tokey(key, key_size);
+ bool ret = upb_strtable_lookup2(&map->table, k.data, k.size, &tabval);
+ if (ret && val) {
+ _upb_map_fromvalue(tabval, val, val_size);
+ }
+ return ret;
+}
+
+UPB_INLINE void* _upb_map_next(const upb_map *map, size_t *iter) {
+ upb_strtable_iter it;
+ it.t = &map->table;
+ it.index = *iter;
+ upb_strtable_next(&it);
+ *iter = it.index;
+ if (upb_strtable_done(&it)) return NULL;
+ return (void*)str_tabent(&it);
+}
+
+UPB_INLINE bool _upb_map_set(upb_map *map, const void *key, size_t key_size,
+ void *val, size_t val_size, upb_arena *arena) {
+ upb_strview strkey = _upb_map_tokey(key, key_size);
+ upb_value tabval = _upb_map_tovalue(val, val_size, arena);
+ upb_alloc *a = upb_arena_alloc(arena);
+
+ /* TODO(haberman): add overwrite operation to minimize number of lookups. */
+ upb_strtable_remove3(&map->table, strkey.data, strkey.size, NULL, a);
+ return upb_strtable_insert3(&map->table, strkey.data, strkey.size, tabval, a);
+}
+
+UPB_INLINE bool _upb_map_delete(upb_map *map, const void *key, size_t key_size) {
+ upb_strview k = _upb_map_tokey(key, key_size);
+ return upb_strtable_remove3(&map->table, k.data, k.size, NULL, NULL);
+}
+
+UPB_INLINE void _upb_map_clear(upb_map *map) {
+ upb_strtable_clear(&map->table);
+}
+
+/* Message map operations, these get the map from the message first. */
+
+UPB_INLINE size_t _upb_msg_map_size(const upb_msg *msg, size_t ofs) {
+ upb_map *map = *UPB_PTR_AT(msg, ofs, upb_map *);
+ return map ? _upb_map_size(map) : 0;
+}
+
+UPB_INLINE bool _upb_msg_map_get(const upb_msg *msg, size_t ofs,
+ const void *key, size_t key_size, void *val,
+ size_t val_size) {
+ upb_map *map = *UPB_PTR_AT(msg, ofs, upb_map *);
+ if (!map) return false;
+ return _upb_map_get(map, key, key_size, val, val_size);
+}
+
+UPB_INLINE void *_upb_msg_map_next(const upb_msg *msg, size_t ofs,
+ size_t *iter) {
+ upb_map *map = *UPB_PTR_AT(msg, ofs, upb_map *);
+ if (!map) return NULL;
+ return _upb_map_next(map, iter);
+}
+
+UPB_INLINE bool _upb_msg_map_set(upb_msg *msg, size_t ofs, const void *key,
+ size_t key_size, void *val, size_t val_size,
+ upb_arena *arena) {
+ upb_map **map = PTR_AT(msg, ofs, upb_map *);
+ if (!*map) {
+ *map = _upb_map_new(arena, key_size, val_size);
+ }
+ return _upb_map_set(*map, key, key_size, val, val_size, arena);
+}
+
+UPB_INLINE bool _upb_msg_map_delete(upb_msg *msg, size_t ofs, const void *key,
+ size_t key_size) {
+ upb_map *map = *UPB_PTR_AT(msg, ofs, upb_map *);
+ if (!map) return false;
+ return _upb_map_delete(map, key, key_size);
+}
+
+UPB_INLINE void _upb_msg_map_clear(upb_msg *msg, size_t ofs) {
+ upb_map *map = *UPB_PTR_AT(msg, ofs, upb_map *);
+ if (!map) return;
+ _upb_map_clear(map);
+}
+
+/* Accessing map key/value from a pointer, used by generated code only. */
+
+UPB_INLINE void _upb_msg_map_key(const void* msg, void* key, size_t size) {
+ const upb_tabent *ent = (const upb_tabent*)msg;
+ uint32_t u32len;
+ upb_strview k;
+ k.data = upb_tabstr(ent->key, &u32len);
+ k.size = u32len;
+ _upb_map_fromkey(k, key, size);
+}
+
+UPB_INLINE void _upb_msg_map_value(const void* msg, void* val, size_t size) {
+ const upb_tabent *ent = (const upb_tabent*)msg;
+ upb_value v;
+ _upb_value_setval(&v, ent->val.val);
+ _upb_map_fromvalue(v, val, size);
+}
+
+UPB_INLINE void _upb_msg_map_set_value(void* msg, const void* val, size_t size) {
+ upb_tabent *ent = (upb_tabent*)msg;
+ /* This is like _upb_map_tovalue() except the entry already exists so we can
+ * reuse the allocated upb_strview for string fields. */
+ if (size == UPB_MAPTYPE_STRING) {
+ upb_strview *strp = (upb_strview*)(uintptr_t)ent->val.val;
+ memcpy(strp, val, sizeof(*strp));
+ } else {
+ memcpy(&ent->val.val, val, size);
+ }
+}
+
+#undef PTR_AT
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#include "upb/port_undef.inc"
+
+#endif /* UPB_MSG_H_ */
diff --git a/contrib/libs/grpc/third_party/upb/upb/port.c b/contrib/libs/grpc/third_party/upb/upb/port.c
new file mode 100644
index 0000000000..9ecf135167
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/port.c
@@ -0,0 +1,26 @@
+
+#include "upb/port_def.inc"
+
+#ifdef UPB_MSVC_VSNPRINTF
+/* Visual C++ earlier than 2015 doesn't have standard C99 snprintf and
+ * vsnprintf. To support them, missing functions are manually implemented
+ * using the existing secure functions. */
+int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg) {
+ if (!s) {
+ return _vscprintf(format, arg);
+ }
+ int ret = _vsnprintf_s(s, n, _TRUNCATE, format, arg);
+ if (ret < 0) {
+ ret = _vscprintf(format, arg);
+ }
+ return ret;
+}
+
+int msvc_snprintf(char* s, size_t n, const char* format, ...) {
+ va_list arg;
+ va_start(arg, format);
+ int ret = msvc_vsnprintf(s, n, format, arg);
+ va_end(arg);
+ return ret;
+}
+#endif
diff --git a/contrib/libs/grpc/third_party/upb/upb/port_def.inc b/contrib/libs/grpc/third_party/upb/upb/port_def.inc
new file mode 100644
index 0000000000..2c144dc066
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/port_def.inc
@@ -0,0 +1,179 @@
+/*
+* This is where we define macros used across upb.
+*
+* All of these macros are undef'd in port_undef.inc to avoid leaking them to
+* users.
+*
+* The correct usage is:
+*
+* #include "upb/foobar.h"
+* #include "upb/baz.h"
+*
+* // MUST be last included header.
+* #include "upb/port_def.inc"
+*
+* // Code for this file.
+* // <...>
+*
+* // Can be omitted for .c files, required for .h.
+* #include "upb/port_undef.inc"
+*
+* This file is private and must not be included by users!
+*/
+#include <stdint.h>
+#include <stddef.h>
+
+#if UINTPTR_MAX == 0xffffffff
+#define UPB_SIZE(size32, size64) size32
+#else
+#define UPB_SIZE(size32, size64) size64
+#endif
+
+/* If we always read/write as a consistent type to each address, this shouldn't
+ * violate aliasing.
+ */
+#define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs)))
+
+#define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
+ *UPB_PTR_AT(msg, case_offset, int) == case_val \
+ ? *UPB_PTR_AT(msg, offset, fieldtype) \
+ : default
+
+#define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
+ *UPB_PTR_AT(msg, case_offset, int) = case_val; \
+ *UPB_PTR_AT(msg, offset, fieldtype) = value;
+
+#define UPB_MAPTYPE_STRING 0
+
+/* UPB_INLINE: inline if possible, emit standalone code if required. */
+#ifdef __cplusplus
+#define UPB_INLINE inline
+#elif defined (__GNUC__) || defined(__clang__)
+#define UPB_INLINE static __inline__
+#else
+#define UPB_INLINE static
+#endif
+
+#define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align))
+#define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align))
+#define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, 16)
+#define UPB_ALIGN_OF(type) offsetof (struct { char c; type member; }, member)
+
+/* Hints to the compiler about likely/unlikely branches. */
+#if defined (__GNUC__) || defined(__clang__)
+#define UPB_LIKELY(x) __builtin_expect((x),1)
+#define UPB_UNLIKELY(x) __builtin_expect((x),0)
+#else
+#define UPB_LIKELY(x) (x)
+#define UPB_UNLIKELY(x) (x)
+#endif
+
+/* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
+ * doesn't provide these preprocessor symbols. */
+#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
+#define UPB_BIG_ENDIAN
+#endif
+
+/* Macros for function attributes on compilers that support them. */
+#ifdef __GNUC__
+#define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
+#define UPB_NOINLINE __attribute__((noinline))
+#define UPB_NORETURN __attribute__((__noreturn__))
+#else /* !defined(__GNUC__) */
+#define UPB_FORCEINLINE
+#define UPB_NOINLINE
+#define UPB_NORETURN
+#endif
+
+#if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
+/* C99/C++11 versions. */
+#include <stdio.h>
+#define _upb_snprintf snprintf
+#define _upb_vsnprintf vsnprintf
+#define _upb_va_copy(a, b) va_copy(a, b)
+#elif defined(_MSC_VER)
+/* Microsoft C/C++ versions. */
+#include <stdarg.h>
+#include <stdio.h>
+#if _MSC_VER < 1900
+int msvc_snprintf(char* s, size_t n, const char* format, ...);
+int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg);
+#define UPB_MSVC_VSNPRINTF
+#define _upb_snprintf msvc_snprintf
+#define _upb_vsnprintf msvc_vsnprintf
+#else
+#define _upb_snprintf snprintf
+#define _upb_vsnprintf vsnprintf
+#endif
+#define _upb_va_copy(a, b) va_copy(a, b)
+#elif defined __GNUC__
+/* A few hacky workarounds for functions not in C89.
+ * For internal use only!
+ * TODO(haberman): fix these by including our own implementations, or finding
+ * another workaround.
+ */
+#define _upb_snprintf __builtin_snprintf
+#define _upb_vsnprintf __builtin_vsnprintf
+#define _upb_va_copy(a, b) __va_copy(a, b)
+#else
+#error Need implementations of [v]snprintf and va_copy
+#endif
+
+#ifdef __cplusplus
+#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \
+ (defined(_MSC_VER) && _MSC_VER >= 1900)
+/* C++11 is present */
+#else
+#error upb requires C++11 for C++ support
+#endif
+#endif
+
+#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
+#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
+
+#define UPB_UNUSED(var) (void)var
+
+/* UPB_ASSUME(): in release mode, we tell the compiler to assume this is true.
+ */
+#ifdef NDEBUG
+#ifdef __GNUC__
+#define UPB_ASSUME(expr) if (!(expr)) __builtin_unreachable()
+#elif defined _MSC_VER
+#define UPB_ASSUME(expr) if (!(expr)) __assume(0)
+#else
+#define UPB_ASSUME(expr) do {} while (false && (expr))
+#endif
+#else
+#define UPB_ASSUME(expr) assert(expr)
+#endif
+
+/* UPB_ASSERT(): in release mode, we use the expression without letting it be
+ * evaluated. This prevents "unused variable" warnings. */
+#ifdef NDEBUG
+#define UPB_ASSERT(expr) do {} while (false && (expr))
+#else
+#define UPB_ASSERT(expr) assert(expr)
+#endif
+
+/* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only
+ * exist in debug mode. This turns into regular assert. */
+#define UPB_ASSERT_DEBUGVAR(expr) assert(expr)
+
+#if defined(__GNUC__) || defined(__clang__)
+#define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
+#else
+#define UPB_UNREACHABLE() do { assert(0); } while(0)
+#endif
+
+/* UPB_INFINITY representing floating-point positive infinity. */
+#include <math.h>
+#ifdef INFINITY
+#define UPB_INFINITY INFINITY
+#else
+#define UPB_INFINITY (1.0 / 0.0)
+#endif
+#ifdef NAN
+#define UPB_NAN NAN
+#else
+#define UPB_NAN (0.0 / 0.0)
+#endif
diff --git a/contrib/libs/grpc/third_party/upb/upb/port_undef.inc b/contrib/libs/grpc/third_party/upb/upb/port_undef.inc
new file mode 100644
index 0000000000..c2b5f4a322
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/port_undef.inc
@@ -0,0 +1,28 @@
+/* See port_def.inc. This should #undef all macros #defined there. */
+
+#undef UPB_MAPTYPE_STRING
+#undef UPB_SIZE
+#undef UPB_PTR_AT
+#undef UPB_READ_ONEOF
+#undef UPB_WRITE_ONEOF
+#undef UPB_INLINE
+#undef UPB_ALIGN_UP
+#undef UPB_ALIGN_DOWN
+#undef UPB_ALIGN_MALLOC
+#undef UPB_ALIGN_OF
+#undef UPB_FORCEINLINE
+#undef UPB_NOINLINE
+#undef UPB_NORETURN
+#undef UPB_MAX
+#undef UPB_MIN
+#undef UPB_UNUSED
+#undef UPB_ASSUME
+#undef UPB_ASSERT
+#undef UPB_ASSERT_DEBUGVAR
+#undef UPB_UNREACHABLE
+#undef UPB_INFINITY
+#undef UPB_NAN
+#undef UPB_MSVC_VSNPRINTF
+#undef _upb_snprintf
+#undef _upb_vsnprintf
+#undef _upb_va_copy
diff --git a/contrib/libs/grpc/third_party/upb/upb/table.c b/contrib/libs/grpc/third_party/upb/upb/table.c
new file mode 100644
index 0000000000..34a20530d8
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/table.c
@@ -0,0 +1,880 @@
+/*
+** upb_table Implementation
+**
+** Implementation is heavily inspired by Lua's ltable.c.
+*/
+
+#include "upb/table.int.h"
+
+#include <string.h>
+
+#include "upb/port_def.inc"
+
+#define UPB_MAXARRSIZE 16 /* 64k. */
+
+/* From Chromium. */
+#define ARRAY_SIZE(x) \
+ ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
+
+static const double MAX_LOAD = 0.85;
+
+/* The minimum utilization of the array part of a mixed hash/array table. This
+ * is a speed/memory-usage tradeoff (though it's not straightforward because of
+ * cache effects). The lower this is, the more memory we'll use. */
+static const double MIN_DENSITY = 0.1;
+
+bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
+
+int log2ceil(uint64_t v) {
+ int ret = 0;
+ bool pow2 = is_pow2(v);
+ while (v >>= 1) ret++;
+ ret = pow2 ? ret : ret + 1; /* Ceiling. */
+ return UPB_MIN(UPB_MAXARRSIZE, ret);
+}
+
+char *upb_strdup(const char *s, upb_alloc *a) {
+ return upb_strdup2(s, strlen(s), a);
+}
+
+char *upb_strdup2(const char *s, size_t len, upb_alloc *a) {
+ size_t n;
+ char *p;
+
+ /* Prevent overflow errors. */
+ if (len == SIZE_MAX) return NULL;
+ /* Always null-terminate, even if binary data; but don't rely on the input to
+ * have a null-terminating byte since it may be a raw binary buffer. */
+ n = len + 1;
+ p = upb_malloc(a, n);
+ if (p) {
+ memcpy(p, s, len);
+ p[len] = 0;
+ }
+ return p;
+}
+
+/* A type to represent the lookup key of either a strtable or an inttable. */
+typedef union {
+ uintptr_t num;
+ struct {
+ const char *str;
+ size_t len;
+ } str;
+} lookupkey_t;
+
+static lookupkey_t strkey2(const char *str, size_t len) {
+ lookupkey_t k;
+ k.str.str = str;
+ k.str.len = len;
+ return k;
+}
+
+static lookupkey_t intkey(uintptr_t key) {
+ lookupkey_t k;
+ k.num = key;
+ return k;
+}
+
+typedef uint32_t hashfunc_t(upb_tabkey key);
+typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2);
+
+/* Base table (shared code) ***************************************************/
+
+/* For when we need to cast away const. */
+static upb_tabent *mutable_entries(upb_table *t) {
+ return (upb_tabent*)t->entries;
+}
+
+static bool isfull(upb_table *t) {
+ if (upb_table_size(t) == 0) {
+ return true;
+ } else {
+ return ((double)(t->count + 1) / upb_table_size(t)) > MAX_LOAD;
+ }
+}
+
+static bool init(upb_table *t, uint8_t size_lg2, upb_alloc *a) {
+ size_t bytes;
+
+ t->count = 0;
+ t->size_lg2 = size_lg2;
+ t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
+ bytes = upb_table_size(t) * sizeof(upb_tabent);
+ if (bytes > 0) {
+ t->entries = upb_malloc(a, bytes);
+ if (!t->entries) return false;
+ memset(mutable_entries(t), 0, bytes);
+ } else {
+ t->entries = NULL;
+ }
+ return true;
+}
+
+static void uninit(upb_table *t, upb_alloc *a) {
+ upb_free(a, mutable_entries(t));
+}
+
+static upb_tabent *emptyent(upb_table *t) {
+ upb_tabent *e = mutable_entries(t) + upb_table_size(t);
+ while (1) { if (upb_tabent_isempty(--e)) return e; UPB_ASSERT(e > t->entries); }
+}
+
+static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) {
+ return (upb_tabent*)upb_getentry(t, hash);
+}
+
+static const upb_tabent *findentry(const upb_table *t, lookupkey_t key,
+ uint32_t hash, eqlfunc_t *eql) {
+ const upb_tabent *e;
+
+ if (t->size_lg2 == 0) return NULL;
+ e = upb_getentry(t, hash);
+ if (upb_tabent_isempty(e)) return NULL;
+ while (1) {
+ if (eql(e->key, key)) return e;
+ if ((e = e->next) == NULL) return NULL;
+ }
+}
+
+static upb_tabent *findentry_mutable(upb_table *t, lookupkey_t key,
+ uint32_t hash, eqlfunc_t *eql) {
+ return (upb_tabent*)findentry(t, key, hash, eql);
+}
+
+static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v,
+ uint32_t hash, eqlfunc_t *eql) {
+ const upb_tabent *e = findentry(t, key, hash, eql);
+ if (e) {
+ if (v) {
+ _upb_value_setval(v, e->val.val);
+ }
+ return true;
+ } else {
+ return false;
+ }
+}
+
+/* The given key must not already exist in the table. */
+static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey,
+ upb_value val, uint32_t hash,
+ hashfunc_t *hashfunc, eqlfunc_t *eql) {
+ upb_tabent *mainpos_e;
+ upb_tabent *our_e;
+
+ UPB_ASSERT(findentry(t, key, hash, eql) == NULL);
+
+ t->count++;
+ mainpos_e = getentry_mutable(t, hash);
+ our_e = mainpos_e;
+
+ if (upb_tabent_isempty(mainpos_e)) {
+ /* Our main position is empty; use it. */
+ our_e->next = NULL;
+ } else {
+ /* Collision. */
+ upb_tabent *new_e = emptyent(t);
+ /* Head of collider's chain. */
+ upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key));
+ if (chain == mainpos_e) {
+ /* Existing ent is in its main posisiton (it has the same hash as us, and
+ * is the head of our chain). Insert to new ent and append to this chain. */
+ new_e->next = mainpos_e->next;
+ mainpos_e->next = new_e;
+ our_e = new_e;
+ } else {
+ /* Existing ent is not in its main position (it is a node in some other
+ * chain). This implies that no existing ent in the table has our hash.
+ * Evict it (updating its chain) and use its ent for head of our chain. */
+ *new_e = *mainpos_e; /* copies next. */
+ while (chain->next != mainpos_e) {
+ chain = (upb_tabent*)chain->next;
+ UPB_ASSERT(chain);
+ }
+ chain->next = new_e;
+ our_e = mainpos_e;
+ our_e->next = NULL;
+ }
+ }
+ our_e->key = tabkey;
+ our_e->val.val = val.val;
+ UPB_ASSERT(findentry(t, key, hash, eql) == our_e);
+}
+
+static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
+ upb_tabkey *removed, uint32_t hash, eqlfunc_t *eql) {
+ upb_tabent *chain = getentry_mutable(t, hash);
+ if (upb_tabent_isempty(chain)) return false;
+ if (eql(chain->key, key)) {
+ /* Element to remove is at the head of its chain. */
+ t->count--;
+ if (val) _upb_value_setval(val, chain->val.val);
+ if (removed) *removed = chain->key;
+ if (chain->next) {
+ upb_tabent *move = (upb_tabent*)chain->next;
+ *chain = *move;
+ move->key = 0; /* Make the slot empty. */
+ } else {
+ chain->key = 0; /* Make the slot empty. */
+ }
+ return true;
+ } else {
+ /* Element to remove is either in a non-head position or not in the
+ * table. */
+ while (chain->next && !eql(chain->next->key, key)) {
+ chain = (upb_tabent*)chain->next;
+ }
+ if (chain->next) {
+ /* Found element to remove. */
+ upb_tabent *rm = (upb_tabent*)chain->next;
+ t->count--;
+ if (val) _upb_value_setval(val, chain->next->val.val);
+ if (removed) *removed = rm->key;
+ rm->key = 0; /* Make the slot empty. */
+ chain->next = rm->next;
+ return true;
+ } else {
+ /* Element to remove is not in the table. */
+ return false;
+ }
+ }
+}
+
+static size_t next(const upb_table *t, size_t i) {
+ do {
+ if (++i >= upb_table_size(t))
+ return SIZE_MAX - 1; /* Distinct from -1. */
+ } while(upb_tabent_isempty(&t->entries[i]));
+
+ return i;
+}
+
+static size_t begin(const upb_table *t) {
+ return next(t, -1);
+}
+
+
+/* upb_strtable ***************************************************************/
+
+/* A simple "subclass" of upb_table that only adds a hash function for strings. */
+
+static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) {
+ uint32_t len = (uint32_t) k2.str.len;
+ char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1);
+ if (str == NULL) return 0;
+ memcpy(str, &len, sizeof(uint32_t));
+ if (k2.str.len) memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len);
+ str[sizeof(uint32_t) + k2.str.len] = '\0';
+ return (uintptr_t)str;
+}
+
+static uint32_t strhash(upb_tabkey key) {
+ uint32_t len;
+ char *str = upb_tabstr(key, &len);
+ return upb_murmur_hash2(str, len, 0);
+}
+
+static bool streql(upb_tabkey k1, lookupkey_t k2) {
+ uint32_t len;
+ char *str = upb_tabstr(k1, &len);
+ return len == k2.str.len && (len == 0 || memcmp(str, k2.str.str, len) == 0);
+}
+
+bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) {
+ UPB_UNUSED(ctype); /* TODO(haberman): rm */
+ return init(&t->t, 2, a);
+}
+
+void upb_strtable_clear(upb_strtable *t) {
+ size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent);
+ t->t.count = 0;
+ memset((char*)t->t.entries, 0, bytes);
+}
+
+void upb_strtable_uninit2(upb_strtable *t, upb_alloc *a) {
+ size_t i;
+ for (i = 0; i < upb_table_size(&t->t); i++)
+ upb_free(a, (void*)t->t.entries[i].key);
+ uninit(&t->t, a);
+}
+
+bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a) {
+ upb_strtable new_table;
+ upb_strtable_iter i;
+
+ if (!init(&new_table.t, size_lg2, a))
+ return false;
+ upb_strtable_begin(&i, t);
+ for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) {
+ upb_strview key = upb_strtable_iter_key(&i);
+ upb_strtable_insert3(
+ &new_table, key.data, key.size,
+ upb_strtable_iter_value(&i), a);
+ }
+ upb_strtable_uninit2(t, a);
+ *t = new_table;
+ return true;
+}
+
+bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len,
+ upb_value v, upb_alloc *a) {
+ lookupkey_t key;
+ upb_tabkey tabkey;
+ uint32_t hash;
+
+ if (isfull(&t->t)) {
+ /* Need to resize. New table of double the size, add old elements to it. */
+ if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) {
+ return false;
+ }
+ }
+
+ key = strkey2(k, len);
+ tabkey = strcopy(key, a);
+ if (tabkey == 0) return false;
+
+ hash = upb_murmur_hash2(key.str.str, key.str.len, 0);
+ insert(&t->t, key, tabkey, v, hash, &strhash, &streql);
+ return true;
+}
+
+bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
+ upb_value *v) {
+ uint32_t hash = upb_murmur_hash2(key, len, 0);
+ return lookup(&t->t, strkey2(key, len), v, hash, &streql);
+}
+
+bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
+ upb_value *val, upb_alloc *alloc) {
+ uint32_t hash = upb_murmur_hash2(key, len, 0);
+ upb_tabkey tabkey;
+ if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) {
+ if (alloc) {
+ /* Arena-based allocs don't need to free and won't pass this. */
+ upb_free(alloc, (void*)tabkey);
+ }
+ return true;
+ } else {
+ return false;
+ }
+}
+
+/* Iteration */
+
+void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) {
+ i->t = t;
+ i->index = begin(&t->t);
+}
+
+void upb_strtable_next(upb_strtable_iter *i) {
+ i->index = next(&i->t->t, i->index);
+}
+
+bool upb_strtable_done(const upb_strtable_iter *i) {
+ if (!i->t) return true;
+ return i->index >= upb_table_size(&i->t->t) ||
+ upb_tabent_isempty(str_tabent(i));
+}
+
+upb_strview upb_strtable_iter_key(const upb_strtable_iter *i) {
+ upb_strview key;
+ uint32_t len;
+ UPB_ASSERT(!upb_strtable_done(i));
+ key.data = upb_tabstr(str_tabent(i)->key, &len);
+ key.size = len;
+ return key;
+}
+
+upb_value upb_strtable_iter_value(const upb_strtable_iter *i) {
+ UPB_ASSERT(!upb_strtable_done(i));
+ return _upb_value_val(str_tabent(i)->val.val);
+}
+
+void upb_strtable_iter_setdone(upb_strtable_iter *i) {
+ i->t = NULL;
+ i->index = SIZE_MAX;
+}
+
+bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
+ const upb_strtable_iter *i2) {
+ if (upb_strtable_done(i1) && upb_strtable_done(i2))
+ return true;
+ return i1->t == i2->t && i1->index == i2->index;
+}
+
+
+/* upb_inttable ***************************************************************/
+
+/* For inttables we use a hybrid structure where small keys are kept in an
+ * array and large keys are put in the hash table. */
+
+static uint32_t inthash(upb_tabkey key) { return upb_inthash(key); }
+
+static bool inteql(upb_tabkey k1, lookupkey_t k2) {
+ return k1 == k2.num;
+}
+
+static upb_tabval *mutable_array(upb_inttable *t) {
+ return (upb_tabval*)t->array;
+}
+
+static upb_tabval *inttable_val(upb_inttable *t, uintptr_t key) {
+ if (key < t->array_size) {
+ return upb_arrhas(t->array[key]) ? &(mutable_array(t)[key]) : NULL;
+ } else {
+ upb_tabent *e =
+ findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql);
+ return e ? &e->val : NULL;
+ }
+}
+
+static const upb_tabval *inttable_val_const(const upb_inttable *t,
+ uintptr_t key) {
+ return inttable_val((upb_inttable*)t, key);
+}
+
+size_t upb_inttable_count(const upb_inttable *t) {
+ return t->t.count + t->array_count;
+}
+
+static void check(upb_inttable *t) {
+ UPB_UNUSED(t);
+#if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG)
+ {
+ /* This check is very expensive (makes inserts/deletes O(N)). */
+ size_t count = 0;
+ upb_inttable_iter i;
+ upb_inttable_begin(&i, t);
+ for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) {
+ UPB_ASSERT(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL));
+ }
+ UPB_ASSERT(count == upb_inttable_count(t));
+ }
+#endif
+}
+
+bool upb_inttable_sizedinit(upb_inttable *t, size_t asize, int hsize_lg2,
+ upb_alloc *a) {
+ size_t array_bytes;
+
+ if (!init(&t->t, hsize_lg2, a)) return false;
+ /* Always make the array part at least 1 long, so that we know key 0
+ * won't be in the hash part, which simplifies things. */
+ t->array_size = UPB_MAX(1, asize);
+ t->array_count = 0;
+ array_bytes = t->array_size * sizeof(upb_value);
+ t->array = upb_malloc(a, array_bytes);
+ if (!t->array) {
+ uninit(&t->t, a);
+ return false;
+ }
+ memset(mutable_array(t), 0xff, array_bytes);
+ check(t);
+ return true;
+}
+
+bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) {
+ UPB_UNUSED(ctype); /* TODO(haberman): rm */
+ return upb_inttable_sizedinit(t, 0, 4, a);
+}
+
+void upb_inttable_uninit2(upb_inttable *t, upb_alloc *a) {
+ uninit(&t->t, a);
+ upb_free(a, mutable_array(t));
+}
+
+bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
+ upb_alloc *a) {
+ upb_tabval tabval;
+ tabval.val = val.val;
+ UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */
+
+ if (key < t->array_size) {
+ UPB_ASSERT(!upb_arrhas(t->array[key]));
+ t->array_count++;
+ mutable_array(t)[key].val = val.val;
+ } else {
+ if (isfull(&t->t)) {
+ /* Need to resize the hash part, but we re-use the array part. */
+ size_t i;
+ upb_table new_table;
+
+ if (!init(&new_table, t->t.size_lg2 + 1, a)) {
+ return false;
+ }
+
+ for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) {
+ const upb_tabent *e = &t->t.entries[i];
+ uint32_t hash;
+ upb_value v;
+
+ _upb_value_setval(&v, e->val.val);
+ hash = upb_inthash(e->key);
+ insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql);
+ }
+
+ UPB_ASSERT(t->t.count == new_table.count);
+
+ uninit(&t->t, a);
+ t->t = new_table;
+ }
+ insert(&t->t, intkey(key), key, val, upb_inthash(key), &inthash, &inteql);
+ }
+ check(t);
+ return true;
+}
+
+bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) {
+ const upb_tabval *table_v = inttable_val_const(t, key);
+ if (!table_v) return false;
+ if (v) _upb_value_setval(v, table_v->val);
+ return true;
+}
+
+bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) {
+ upb_tabval *table_v = inttable_val(t, key);
+ if (!table_v) return false;
+ table_v->val = val.val;
+ return true;
+}
+
+bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
+ bool success;
+ if (key < t->array_size) {
+ if (upb_arrhas(t->array[key])) {
+ upb_tabval empty = UPB_TABVALUE_EMPTY_INIT;
+ t->array_count--;
+ if (val) {
+ _upb_value_setval(val, t->array[key].val);
+ }
+ mutable_array(t)[key] = empty;
+ success = true;
+ } else {
+ success = false;
+ }
+ } else {
+ success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql);
+ }
+ check(t);
+ return success;
+}
+
+bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
+ upb_alloc *a) {
+ return upb_inttable_insert2(t, (uintptr_t)key, val, a);
+}
+
+bool upb_inttable_lookupptr(const upb_inttable *t, const void *key,
+ upb_value *v) {
+ return upb_inttable_lookup(t, (uintptr_t)key, v);
+}
+
+bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val) {
+ return upb_inttable_remove(t, (uintptr_t)key, val);
+}
+
+void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) {
+ /* A power-of-two histogram of the table keys. */
+ size_t counts[UPB_MAXARRSIZE + 1] = {0};
+
+ /* The max key in each bucket. */
+ uintptr_t max[UPB_MAXARRSIZE + 1] = {0};
+
+ upb_inttable_iter i;
+ size_t arr_count;
+ int size_lg2;
+ upb_inttable new_t;
+
+ upb_inttable_begin(&i, t);
+ for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
+ uintptr_t key = upb_inttable_iter_key(&i);
+ int bucket = log2ceil(key);
+ max[bucket] = UPB_MAX(max[bucket], key);
+ counts[bucket]++;
+ }
+
+ /* Find the largest power of two that satisfies the MIN_DENSITY
+ * definition (while actually having some keys). */
+ arr_count = upb_inttable_count(t);
+
+ for (size_lg2 = ARRAY_SIZE(counts) - 1; size_lg2 > 0; size_lg2--) {
+ if (counts[size_lg2] == 0) {
+ /* We can halve again without losing any entries. */
+ continue;
+ } else if (arr_count >= (1 << size_lg2) * MIN_DENSITY) {
+ break;
+ }
+
+ arr_count -= counts[size_lg2];
+ }
+
+ UPB_ASSERT(arr_count <= upb_inttable_count(t));
+
+ {
+ /* Insert all elements into new, perfectly-sized table. */
+ size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */
+ size_t hash_count = upb_inttable_count(t) - arr_count;
+ size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0;
+ int hashsize_lg2 = log2ceil(hash_size);
+
+ upb_inttable_sizedinit(&new_t, arr_size, hashsize_lg2, a);
+ upb_inttable_begin(&i, t);
+ for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
+ uintptr_t k = upb_inttable_iter_key(&i);
+ upb_inttable_insert2(&new_t, k, upb_inttable_iter_value(&i), a);
+ }
+ UPB_ASSERT(new_t.array_size == arr_size);
+ UPB_ASSERT(new_t.t.size_lg2 == hashsize_lg2);
+ }
+ upb_inttable_uninit2(t, a);
+ *t = new_t;
+}
+
+/* Iteration. */
+
+static const upb_tabent *int_tabent(const upb_inttable_iter *i) {
+ UPB_ASSERT(!i->array_part);
+ return &i->t->t.entries[i->index];
+}
+
+static upb_tabval int_arrent(const upb_inttable_iter *i) {
+ UPB_ASSERT(i->array_part);
+ return i->t->array[i->index];
+}
+
+void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t) {
+ i->t = t;
+ i->index = -1;
+ i->array_part = true;
+ upb_inttable_next(i);
+}
+
+void upb_inttable_next(upb_inttable_iter *iter) {
+ const upb_inttable *t = iter->t;
+ if (iter->array_part) {
+ while (++iter->index < t->array_size) {
+ if (upb_arrhas(int_arrent(iter))) {
+ return;
+ }
+ }
+ iter->array_part = false;
+ iter->index = begin(&t->t);
+ } else {
+ iter->index = next(&t->t, iter->index);
+ }
+}
+
+bool upb_inttable_done(const upb_inttable_iter *i) {
+ if (!i->t) return true;
+ if (i->array_part) {
+ return i->index >= i->t->array_size ||
+ !upb_arrhas(int_arrent(i));
+ } else {
+ return i->index >= upb_table_size(&i->t->t) ||
+ upb_tabent_isempty(int_tabent(i));
+ }
+}
+
+uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) {
+ UPB_ASSERT(!upb_inttable_done(i));
+ return i->array_part ? i->index : int_tabent(i)->key;
+}
+
+upb_value upb_inttable_iter_value(const upb_inttable_iter *i) {
+ UPB_ASSERT(!upb_inttable_done(i));
+ return _upb_value_val(
+ i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val);
+}
+
+void upb_inttable_iter_setdone(upb_inttable_iter *i) {
+ i->t = NULL;
+ i->index = SIZE_MAX;
+ i->array_part = false;
+}
+
+bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
+ const upb_inttable_iter *i2) {
+ if (upb_inttable_done(i1) && upb_inttable_done(i2))
+ return true;
+ return i1->t == i2->t && i1->index == i2->index &&
+ i1->array_part == i2->array_part;
+}
+
+#if defined(UPB_UNALIGNED_READS_OK) || defined(__s390x__)
+/* -----------------------------------------------------------------------------
+ * MurmurHash2, by Austin Appleby (released as public domain).
+ * Reformatted and C99-ified by Joshua Haberman.
+ * Note - This code makes a few assumptions about how your machine behaves -
+ * 1. We can read a 4-byte value from any address without crashing
+ * 2. sizeof(int) == 4 (in upb this limitation is removed by using uint32_t
+ * And it has a few limitations -
+ * 1. It will not work incrementally.
+ * 2. It will not produce the same results on little-endian and big-endian
+ * machines. */
+uint32_t upb_murmur_hash2(const void *key, size_t len, uint32_t seed) {
+ /* 'm' and 'r' are mixing constants generated offline.
+ * They're not really 'magic', they just happen to work well. */
+ const uint32_t m = 0x5bd1e995;
+ const int32_t r = 24;
+
+ /* Initialize the hash to a 'random' value */
+ uint32_t h = seed ^ len;
+
+ /* Mix 4 bytes at a time into the hash */
+ const uint8_t * data = (const uint8_t *)key;
+ while(len >= 4) {
+ uint32_t k;
+ memcpy(&k, data, sizeof(k));
+
+ k *= m;
+ k ^= k >> r;
+ k *= m;
+
+ h *= m;
+ h ^= k;
+
+ data += 4;
+ len -= 4;
+ }
+
+ /* Handle the last few bytes of the input array */
+ switch(len) {
+ case 3: h ^= data[2] << 16;
+ case 2: h ^= data[1] << 8;
+ case 1: h ^= data[0]; h *= m;
+ };
+
+ /* Do a few final mixes of the hash to ensure the last few
+ * bytes are well-incorporated. */
+ h ^= h >> 13;
+ h *= m;
+ h ^= h >> 15;
+
+ return h;
+}
+
+#else /* !UPB_UNALIGNED_READS_OK */
+
+/* -----------------------------------------------------------------------------
+ * MurmurHashAligned2, by Austin Appleby
+ * Same algorithm as MurmurHash2, but only does aligned reads - should be safer
+ * on certain platforms.
+ * Performance will be lower than MurmurHash2 */
+
+#define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
+
+uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) {
+ const uint32_t m = 0x5bd1e995;
+ const int32_t r = 24;
+ const uint8_t * data = (const uint8_t *)key;
+ uint32_t h = (uint32_t)(seed ^ len);
+ uint8_t align = (uintptr_t)data & 3;
+
+ if(align && (len >= 4)) {
+ /* Pre-load the temp registers */
+ uint32_t t = 0, d = 0;
+ int32_t sl;
+ int32_t sr;
+
+ switch(align) {
+ case 1: t |= data[2] << 16; /* fallthrough */
+ case 2: t |= data[1] << 8; /* fallthrough */
+ case 3: t |= data[0];
+ }
+
+ t <<= (8 * align);
+
+ data += 4-align;
+ len -= 4-align;
+
+ sl = 8 * (4-align);
+ sr = 8 * align;
+
+ /* Mix */
+
+ while(len >= 4) {
+ uint32_t k;
+
+ d = *(uint32_t *)data;
+ t = (t >> sr) | (d << sl);
+
+ k = t;
+
+ MIX(h,k,m);
+
+ t = d;
+
+ data += 4;
+ len -= 4;
+ }
+
+ /* Handle leftover data in temp registers */
+
+ d = 0;
+
+ if(len >= align) {
+ uint32_t k;
+
+ switch(align) {
+ case 3: d |= data[2] << 16; /* fallthrough */
+ case 2: d |= data[1] << 8; /* fallthrough */
+ case 1: d |= data[0]; /* fallthrough */
+ }
+
+ k = (t >> sr) | (d << sl);
+ MIX(h,k,m);
+
+ data += align;
+ len -= align;
+
+ /* ----------
+ * Handle tail bytes */
+
+ switch(len) {
+ case 3: h ^= data[2] << 16; /* fallthrough */
+ case 2: h ^= data[1] << 8; /* fallthrough */
+ case 1: h ^= data[0]; h *= m; /* fallthrough */
+ };
+ } else {
+ switch(len) {
+ case 3: d |= data[2] << 16; /* fallthrough */
+ case 2: d |= data[1] << 8; /* fallthrough */
+ case 1: d |= data[0]; /* fallthrough */
+ case 0: h ^= (t >> sr) | (d << sl); h *= m;
+ }
+ }
+
+ h ^= h >> 13;
+ h *= m;
+ h ^= h >> 15;
+
+ return h;
+ } else {
+ while(len >= 4) {
+ uint32_t k = *(uint32_t *)data;
+
+ MIX(h,k,m);
+
+ data += 4;
+ len -= 4;
+ }
+
+ /* ----------
+ * Handle tail bytes */
+
+ switch(len) {
+ case 3: h ^= data[2] << 16; /* fallthrough */
+ case 2: h ^= data[1] << 8; /* fallthrough */
+ case 1: h ^= data[0]; h *= m;
+ };
+
+ h ^= h >> 13;
+ h *= m;
+ h ^= h >> 15;
+
+ return h;
+ }
+}
+#undef MIX
+
+#endif /* UPB_UNALIGNED_READS_OK */
diff --git a/contrib/libs/grpc/third_party/upb/upb/table.int.h b/contrib/libs/grpc/third_party/upb/upb/table.int.h
new file mode 100644
index 0000000000..600637eef2
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/table.int.h
@@ -0,0 +1,466 @@
+/*
+** upb_table
+**
+** This header is INTERNAL-ONLY! Its interfaces are not public or stable!
+** This file defines very fast int->upb_value (inttable) and string->upb_value
+** (strtable) hash tables.
+**
+** The table uses chained scatter with Brent's variation (inspired by the Lua
+** implementation of hash tables). The hash function for strings is Austin
+** Appleby's "MurmurHash."
+**
+** The inttable uses uintptr_t as its key, which guarantees it can be used to
+** store pointers or integers of at least 32 bits (upb isn't really useful on
+** systems where sizeof(void*) < 4).
+**
+** The table must be homogenous (all values of the same type). In debug
+** mode, we check this on insert and lookup.
+*/
+
+#ifndef UPB_TABLE_H_
+#define UPB_TABLE_H_
+
+#include <stdint.h>
+#include <string.h>
+#include "upb/upb.h"
+
+#include "upb/port_def.inc"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* upb_value ******************************************************************/
+
+/* A tagged union (stored untagged inside the table) so that we can check that
+ * clients calling table accessors are correctly typed without having to have
+ * an explosion of accessors. */
+typedef enum {
+ UPB_CTYPE_INT32 = 1,
+ UPB_CTYPE_INT64 = 2,
+ UPB_CTYPE_UINT32 = 3,
+ UPB_CTYPE_UINT64 = 4,
+ UPB_CTYPE_BOOL = 5,
+ UPB_CTYPE_CSTR = 6,
+ UPB_CTYPE_PTR = 7,
+ UPB_CTYPE_CONSTPTR = 8,
+ UPB_CTYPE_FPTR = 9,
+ UPB_CTYPE_FLOAT = 10,
+ UPB_CTYPE_DOUBLE = 11
+} upb_ctype_t;
+
+typedef struct {
+ uint64_t val;
+} upb_value;
+
+/* Like strdup(), which isn't always available since it's not ANSI C. */
+char *upb_strdup(const char *s, upb_alloc *a);
+/* Variant that works with a length-delimited rather than NULL-delimited string,
+ * as supported by strtable. */
+char *upb_strdup2(const char *s, size_t len, upb_alloc *a);
+
+UPB_INLINE char *upb_gstrdup(const char *s) {
+ return upb_strdup(s, &upb_alloc_global);
+}
+
+UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val) {
+ v->val = val;
+}
+
+UPB_INLINE upb_value _upb_value_val(uint64_t val) {
+ upb_value ret;
+ _upb_value_setval(&ret, val);
+ return ret;
+}
+
+/* For each value ctype, define the following set of functions:
+ *
+ * // Get/set an int32 from a upb_value.
+ * int32_t upb_value_getint32(upb_value val);
+ * void upb_value_setint32(upb_value *val, int32_t cval);
+ *
+ * // Construct a new upb_value from an int32.
+ * upb_value upb_value_int32(int32_t val); */
+#define FUNCS(name, membername, type_t, converter, proto_type) \
+ UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
+ val->val = (converter)cval; \
+ } \
+ UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
+ upb_value ret; \
+ upb_value_set ## name(&ret, val); \
+ return ret; \
+ } \
+ UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
+ return (type_t)(converter)val.val; \
+ }
+
+FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32)
+FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64)
+FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32)
+FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64)
+FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL)
+FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR)
+FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR)
+FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR)
+FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR)
+
+#undef FUNCS
+
+UPB_INLINE void upb_value_setfloat(upb_value *val, float cval) {
+ memcpy(&val->val, &cval, sizeof(cval));
+}
+
+UPB_INLINE void upb_value_setdouble(upb_value *val, double cval) {
+ memcpy(&val->val, &cval, sizeof(cval));
+}
+
+UPB_INLINE upb_value upb_value_float(float cval) {
+ upb_value ret;
+ upb_value_setfloat(&ret, cval);
+ return ret;
+}
+
+UPB_INLINE upb_value upb_value_double(double cval) {
+ upb_value ret;
+ upb_value_setdouble(&ret, cval);
+ return ret;
+}
+
+#undef SET_TYPE
+
+
+/* upb_tabkey *****************************************************************/
+
+/* Either:
+ * 1. an actual integer key, or
+ * 2. a pointer to a string prefixed by its uint32_t length, owned by us.
+ *
+ * ...depending on whether this is a string table or an int table. We would
+ * make this a union of those two types, but C89 doesn't support statically
+ * initializing a non-first union member. */
+typedef uintptr_t upb_tabkey;
+
+UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) {
+ char* mem = (char*)key;
+ if (len) memcpy(len, mem, sizeof(*len));
+ return mem + sizeof(*len);
+}
+
+
+/* upb_tabval *****************************************************************/
+
+typedef struct {
+ uint64_t val;
+} upb_tabval;
+
+#define UPB_TABVALUE_EMPTY_INIT {-1}
+
+/* upb_table ******************************************************************/
+
+typedef struct _upb_tabent {
+ upb_tabkey key;
+ upb_tabval val;
+
+ /* Internal chaining. This is const so we can create static initializers for
+ * tables. We cast away const sometimes, but *only* when the containing
+ * upb_table is known to be non-const. This requires a bit of care, but
+ * the subtlety is confined to table.c. */
+ const struct _upb_tabent *next;
+} upb_tabent;
+
+typedef struct {
+ size_t count; /* Number of entries in the hash part. */
+ size_t mask; /* Mask to turn hash value -> bucket. */
+ uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
+
+ /* Hash table entries.
+ * Making this const isn't entirely accurate; what we really want is for it to
+ * have the same const-ness as the table it's inside. But there's no way to
+ * declare that in C. So we have to make it const so that we can statically
+ * initialize const hash tables. Then we cast away const when we have to.
+ */
+ const upb_tabent *entries;
+} upb_table;
+
+typedef struct {
+ upb_table t;
+} upb_strtable;
+
+typedef struct {
+ upb_table t; /* For entries that don't fit in the array part. */
+ const upb_tabval *array; /* Array part of the table. See const note above. */
+ size_t array_size; /* Array part size. */
+ size_t array_count; /* Array part number of elements. */
+} upb_inttable;
+
+#define UPB_ARRAY_EMPTYENT -1
+
+UPB_INLINE size_t upb_table_size(const upb_table *t) {
+ if (t->size_lg2 == 0)
+ return 0;
+ else
+ return 1 << t->size_lg2;
+}
+
+/* Internal-only functions, in .h file only out of necessity. */
+UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) {
+ return e->key == 0;
+}
+
+/* Used by some of the unit tests for generic hashing functionality. */
+uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed);
+
+UPB_INLINE uintptr_t upb_intkey(uintptr_t key) {
+ return key;
+}
+
+UPB_INLINE uint32_t upb_inthash(uintptr_t key) {
+ return (uint32_t)key;
+}
+
+static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
+ return t->entries + (hash & t->mask);
+}
+
+UPB_INLINE bool upb_arrhas(upb_tabval key) {
+ return key.val != (uint64_t)-1;
+}
+
+/* Initialize and uninitialize a table, respectively. If memory allocation
+ * failed, false is returned that the table is uninitialized. */
+bool upb_inttable_init2(upb_inttable *table, upb_ctype_t ctype, upb_alloc *a);
+bool upb_strtable_init2(upb_strtable *table, upb_ctype_t ctype, upb_alloc *a);
+void upb_inttable_uninit2(upb_inttable *table, upb_alloc *a);
+void upb_strtable_uninit2(upb_strtable *table, upb_alloc *a);
+
+UPB_INLINE bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype) {
+ return upb_inttable_init2(table, ctype, &upb_alloc_global);
+}
+
+UPB_INLINE bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype) {
+ return upb_strtable_init2(table, ctype, &upb_alloc_global);
+}
+
+UPB_INLINE void upb_inttable_uninit(upb_inttable *table) {
+ upb_inttable_uninit2(table, &upb_alloc_global);
+}
+
+UPB_INLINE void upb_strtable_uninit(upb_strtable *table) {
+ upb_strtable_uninit2(table, &upb_alloc_global);
+}
+
+/* Returns the number of values in the table. */
+size_t upb_inttable_count(const upb_inttable *t);
+UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) {
+ return t->t.count;
+}
+
+void upb_inttable_packedsize(const upb_inttable *t, size_t *size);
+void upb_strtable_packedsize(const upb_strtable *t, size_t *size);
+upb_inttable *upb_inttable_pack(const upb_inttable *t, void *p, size_t *ofs,
+ size_t size);
+upb_strtable *upb_strtable_pack(const upb_strtable *t, void *p, size_t *ofs,
+ size_t size);
+void upb_strtable_clear(upb_strtable *t);
+
+/* Inserts the given key into the hashtable with the given value. The key must
+ * not already exist in the hash table. For string tables, the key must be
+ * NULL-terminated, and the table will make an internal copy of the key.
+ * Inttables must not insert a value of UINTPTR_MAX.
+ *
+ * If a table resize was required but memory allocation failed, false is
+ * returned and the table is unchanged. */
+bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
+ upb_alloc *a);
+bool upb_strtable_insert3(upb_strtable *t, const char *key, size_t len,
+ upb_value val, upb_alloc *a);
+
+UPB_INLINE bool upb_inttable_insert(upb_inttable *t, uintptr_t key,
+ upb_value val) {
+ return upb_inttable_insert2(t, key, val, &upb_alloc_global);
+}
+
+UPB_INLINE bool upb_strtable_insert2(upb_strtable *t, const char *key,
+ size_t len, upb_value val) {
+ return upb_strtable_insert3(t, key, len, val, &upb_alloc_global);
+}
+
+/* For NULL-terminated strings. */
+UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key,
+ upb_value val) {
+ return upb_strtable_insert2(t, key, strlen(key), val);
+}
+
+/* Looks up key in this table, returning "true" if the key was found.
+ * If v is non-NULL, copies the value for this key into *v. */
+bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v);
+bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
+ upb_value *v);
+
+/* For NULL-terminated strings. */
+UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key,
+ upb_value *v) {
+ return upb_strtable_lookup2(t, key, strlen(key), v);
+}
+
+/* Removes an item from the table. Returns true if the remove was successful,
+ * and stores the removed item in *val if non-NULL. */
+bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val);
+bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
+ upb_value *val, upb_alloc *alloc);
+
+UPB_INLINE bool upb_strtable_remove2(upb_strtable *t, const char *key,
+ size_t len, upb_value *val) {
+ return upb_strtable_remove3(t, key, len, val, &upb_alloc_global);
+}
+
+/* For NULL-terminated strings. */
+UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key,
+ upb_value *v) {
+ return upb_strtable_remove2(t, key, strlen(key), v);
+}
+
+/* Updates an existing entry in an inttable. If the entry does not exist,
+ * returns false and does nothing. Unlike insert/remove, this does not
+ * invalidate iterators. */
+bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val);
+
+/* Convenience routines for inttables with pointer keys. */
+bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
+ upb_alloc *a);
+bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val);
+bool upb_inttable_lookupptr(
+ const upb_inttable *t, const void *key, upb_value *val);
+
+UPB_INLINE bool upb_inttable_insertptr(upb_inttable *t, const void *key,
+ upb_value val) {
+ return upb_inttable_insertptr2(t, key, val, &upb_alloc_global);
+}
+
+/* Optimizes the table for the current set of entries, for both memory use and
+ * lookup time. Client should call this after all entries have been inserted;
+ * inserting more entries is legal, but will likely require a table resize. */
+void upb_inttable_compact2(upb_inttable *t, upb_alloc *a);
+
+UPB_INLINE void upb_inttable_compact(upb_inttable *t) {
+ upb_inttable_compact2(t, &upb_alloc_global);
+}
+
+/* A special-case inlinable version of the lookup routine for 32-bit
+ * integers. */
+UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key,
+ upb_value *v) {
+ *v = upb_value_int32(0); /* Silence compiler warnings. */
+ if (key < t->array_size) {
+ upb_tabval arrval = t->array[key];
+ if (upb_arrhas(arrval)) {
+ _upb_value_setval(v, arrval.val);
+ return true;
+ } else {
+ return false;
+ }
+ } else {
+ const upb_tabent *e;
+ if (t->t.entries == NULL) return false;
+ for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) {
+ if ((uint32_t)e->key == key) {
+ _upb_value_setval(v, e->val.val);
+ return true;
+ }
+ if (e->next == NULL) return false;
+ }
+ }
+}
+
+/* Exposed for testing only. */
+bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a);
+
+/* Iterators ******************************************************************/
+
+/* Iterators for int and string tables. We are subject to some kind of unusual
+ * design constraints:
+ *
+ * For high-level languages:
+ * - we must be able to guarantee that we don't crash or corrupt memory even if
+ * the program accesses an invalidated iterator.
+ *
+ * For C++11 range-based for:
+ * - iterators must be copyable
+ * - iterators must be comparable
+ * - it must be possible to construct an "end" value.
+ *
+ * Iteration order is undefined.
+ *
+ * Modifying the table invalidates iterators. upb_{str,int}table_done() is
+ * guaranteed to work even on an invalidated iterator, as long as the table it
+ * is iterating over has not been freed. Calling next() or accessing data from
+ * an invalidated iterator yields unspecified elements from the table, but it is
+ * guaranteed not to crash and to return real table elements (except when done()
+ * is true). */
+
+
+/* upb_strtable_iter **********************************************************/
+
+/* upb_strtable_iter i;
+ * upb_strtable_begin(&i, t);
+ * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
+ * const char *key = upb_strtable_iter_key(&i);
+ * const upb_value val = upb_strtable_iter_value(&i);
+ * // ...
+ * }
+ */
+
+typedef struct {
+ const upb_strtable *t;
+ size_t index;
+} upb_strtable_iter;
+
+void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t);
+void upb_strtable_next(upb_strtable_iter *i);
+bool upb_strtable_done(const upb_strtable_iter *i);
+upb_strview upb_strtable_iter_key(const upb_strtable_iter *i);
+upb_value upb_strtable_iter_value(const upb_strtable_iter *i);
+void upb_strtable_iter_setdone(upb_strtable_iter *i);
+bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
+ const upb_strtable_iter *i2);
+
+
+/* upb_inttable_iter **********************************************************/
+
+/* upb_inttable_iter i;
+ * upb_inttable_begin(&i, t);
+ * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
+ * uintptr_t key = upb_inttable_iter_key(&i);
+ * upb_value val = upb_inttable_iter_value(&i);
+ * // ...
+ * }
+ */
+
+typedef struct {
+ const upb_inttable *t;
+ size_t index;
+ bool array_part;
+} upb_inttable_iter;
+
+UPB_INLINE const upb_tabent *str_tabent(const upb_strtable_iter *i) {
+ return &i->t->t.entries[i->index];
+}
+
+void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t);
+void upb_inttable_next(upb_inttable_iter *i);
+bool upb_inttable_done(const upb_inttable_iter *i);
+uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i);
+upb_value upb_inttable_iter_value(const upb_inttable_iter *i);
+void upb_inttable_iter_setdone(upb_inttable_iter *i);
+bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
+ const upb_inttable_iter *i2);
+
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#include "upb/port_undef.inc"
+
+#endif /* UPB_TABLE_H_ */
diff --git a/contrib/libs/grpc/third_party/upb/upb/upb.c b/contrib/libs/grpc/third_party/upb/upb/upb.c
new file mode 100644
index 0000000000..3089c059e1
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/upb.c
@@ -0,0 +1,287 @@
+
+#include "upb/upb.h"
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "upb/port_def.inc"
+
+/* upb_status *****************************************************************/
+
+void upb_status_clear(upb_status *status) {
+ if (!status) return;
+ status->ok = true;
+ status->msg[0] = '\0';
+}
+
+bool upb_ok(const upb_status *status) { return status->ok; }
+
+const char *upb_status_errmsg(const upb_status *status) { return status->msg; }
+
+void upb_status_seterrmsg(upb_status *status, const char *msg) {
+ if (!status) return;
+ status->ok = false;
+ strncpy(status->msg, msg, UPB_STATUS_MAX_MESSAGE - 1);
+ status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
+}
+
+void upb_status_seterrf(upb_status *status, const char *fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ upb_status_vseterrf(status, fmt, args);
+ va_end(args);
+}
+
+void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) {
+ if (!status) return;
+ status->ok = false;
+ _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args);
+ status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
+}
+
+void upb_status_vappenderrf(upb_status *status, const char *fmt, va_list args) {
+ size_t len;
+ if (!status) return;
+ status->ok = false;
+ len = strlen(status->msg);
+ _upb_vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args);
+ status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
+}
+
+/* upb_alloc ******************************************************************/
+
+static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize,
+ size_t size) {
+ UPB_UNUSED(alloc);
+ UPB_UNUSED(oldsize);
+ if (size == 0) {
+ free(ptr);
+ return NULL;
+ } else {
+ return realloc(ptr, size);
+ }
+}
+
+upb_alloc upb_alloc_global = {&upb_global_allocfunc};
+
+/* upb_arena ******************************************************************/
+
+/* Be conservative and choose 16 in case anyone is using SSE. */
+
+typedef struct mem_block {
+ struct mem_block *next;
+ uint32_t size;
+ uint32_t cleanups;
+ /* Data follows. */
+} mem_block;
+
+typedef struct cleanup_ent {
+ upb_cleanup_func *cleanup;
+ void *ud;
+} cleanup_ent;
+
+struct upb_arena {
+ _upb_arena_head head;
+ uint32_t *cleanups;
+
+ /* Allocator to allocate arena blocks. We are responsible for freeing these
+ * when we are destroyed. */
+ upb_alloc *block_alloc;
+ uint32_t last_size;
+
+ /* When multiple arenas are fused together, each arena points to a parent
+ * arena (root points to itself). The root tracks how many live arenas
+ * reference it. */
+ uint32_t refcount; /* Only used when a->parent == a */
+ struct upb_arena *parent;
+
+ /* Linked list of blocks to free/cleanup. */
+ mem_block *freelist, *freelist_tail;
+};
+
+static const size_t memblock_reserve = UPB_ALIGN_UP(sizeof(mem_block), 16);
+
+static upb_arena *arena_findroot(upb_arena *a) {
+ /* Path splitting keeps time complexity down, see:
+ * https://en.wikipedia.org/wiki/Disjoint-set_data_structure */
+ while (a->parent != a) {
+ upb_arena *next = a->parent;
+ a->parent = next->parent;
+ a = next;
+ }
+ return a;
+}
+
+static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size) {
+ mem_block *block = ptr;
+ upb_arena *root = arena_findroot(a);
+
+ /* The block is for arena |a|, but should appear in the freelist of |root|. */
+ block->next = root->freelist;
+ block->size = (uint32_t)size;
+ block->cleanups = 0;
+ root->freelist = block;
+ a->last_size = block->size;
+ if (!root->freelist_tail) root->freelist_tail = block;
+
+ a->head.ptr = UPB_PTR_AT(block, memblock_reserve, char);
+ a->head.end = UPB_PTR_AT(block, size, char);
+ a->cleanups = &block->cleanups;
+
+ /* TODO(haberman): ASAN poison. */
+}
+
+static bool upb_arena_allocblock(upb_arena *a, size_t size) {
+ size_t block_size = UPB_MAX(size, a->last_size * 2) + memblock_reserve;
+ mem_block *block = upb_malloc(a->block_alloc, block_size);
+
+ if (!block) return false;
+ upb_arena_addblock(a, block, block_size);
+ return true;
+}
+
+static bool arena_has(upb_arena *a, size_t size) {
+ _upb_arena_head *h = (_upb_arena_head*)a;
+ return (size_t)(h->end - h->ptr) >= size;
+}
+
+void *_upb_arena_slowmalloc(upb_arena *a, size_t size) {
+ if (!upb_arena_allocblock(a, size)) return NULL; /* Out of memory. */
+ UPB_ASSERT(arena_has(a, size));
+ return upb_arena_malloc(a, size);
+}
+
+static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize,
+ size_t size) {
+ upb_arena *a = (upb_arena*)alloc; /* upb_alloc is initial member. */
+ return upb_arena_realloc(a, ptr, oldsize, size);
+}
+
+/* Public Arena API ***********************************************************/
+
+upb_arena *arena_initslow(void *mem, size_t n, upb_alloc *alloc) {
+ const size_t first_block_overhead = sizeof(upb_arena) + memblock_reserve;
+ upb_arena *a;
+
+ /* We need to malloc the initial block. */
+ n = first_block_overhead + 256;
+ if (!alloc || !(mem = upb_malloc(alloc, n))) {
+ return NULL;
+ }
+
+ a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
+ n -= sizeof(*a);
+
+ a->head.alloc.func = &upb_arena_doalloc;
+ a->block_alloc = alloc;
+ a->parent = a;
+ a->refcount = 1;
+ a->freelist = NULL;
+ a->freelist_tail = NULL;
+
+ upb_arena_addblock(a, mem, n);
+
+ return a;
+}
+
+upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) {
+ upb_arena *a;
+
+ /* Round block size down to alignof(*a) since we will allocate the arena
+ * itself at the end. */
+ n = UPB_ALIGN_DOWN(n, UPB_ALIGN_OF(upb_arena));
+
+ if (UPB_UNLIKELY(n < sizeof(upb_arena))) {
+ return arena_initslow(mem, n, alloc);
+ }
+
+ a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
+ n -= sizeof(*a);
+
+ a->head.alloc.func = &upb_arena_doalloc;
+ a->block_alloc = alloc;
+ a->parent = a;
+ a->refcount = 1;
+ a->last_size = 128;
+ a->head.ptr = mem;
+ a->head.end = UPB_PTR_AT(mem, n, char);
+ a->freelist = NULL;
+ a->cleanups = NULL;
+
+ return a;
+}
+
+static void arena_dofree(upb_arena *a) {
+ mem_block *block = a->freelist;
+ UPB_ASSERT(a->parent == a);
+ UPB_ASSERT(a->refcount == 0);
+
+ while (block) {
+ /* Load first since we are deleting block. */
+ mem_block *next = block->next;
+
+ if (block->cleanups > 0) {
+ cleanup_ent *end = UPB_PTR_AT(block, block->size, void);
+ cleanup_ent *ptr = end - block->cleanups;
+
+ for (; ptr < end; ptr++) {
+ ptr->cleanup(ptr->ud);
+ }
+ }
+
+ upb_free(a->block_alloc, block);
+ block = next;
+ }
+}
+
+void upb_arena_free(upb_arena *a) {
+ a = arena_findroot(a);
+ if (--a->refcount == 0) arena_dofree(a);
+}
+
+bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func) {
+ cleanup_ent *ent;
+
+ if (!a->cleanups || !arena_has(a, sizeof(cleanup_ent))) {
+ if (!upb_arena_allocblock(a, 128)) return false; /* Out of memory. */
+ UPB_ASSERT(arena_has(a, sizeof(cleanup_ent)));
+ }
+
+ a->head.end -= sizeof(cleanup_ent);
+ ent = (cleanup_ent*)a->head.end;
+ (*a->cleanups)++;
+
+ ent->cleanup = func;
+ ent->ud = ud;
+
+ return true;
+}
+
+void upb_arena_fuse(upb_arena *a1, upb_arena *a2) {
+ upb_arena *r1 = arena_findroot(a1);
+ upb_arena *r2 = arena_findroot(a2);
+
+ if (r1 == r2) return; /* Already fused. */
+
+ /* We want to join the smaller tree to the larger tree.
+ * So swap first if they are backwards. */
+ if (r1->refcount < r2->refcount) {
+ upb_arena *tmp = r1;
+ r1 = r2;
+ r2 = tmp;
+ }
+
+ /* r1 takes over r2's freelist and refcount. */
+ r1->refcount += r2->refcount;
+ if (r2->freelist_tail) {
+ UPB_ASSERT(r2->freelist_tail->next == NULL);
+ r2->freelist_tail->next = r1->freelist;
+ r1->freelist = r2->freelist;
+ }
+ r2->parent = r1;
+}
diff --git a/contrib/libs/grpc/third_party/upb/upb/upb.h b/contrib/libs/grpc/third_party/upb/upb/upb.h
new file mode 100644
index 0000000000..e1d9d8cfd3
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/upb.h
@@ -0,0 +1,308 @@
+/*
+** This file contains shared definitions that are widely used across upb.
+*/
+
+#ifndef UPB_H_
+#define UPB_H_
+
+#include <assert.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "upb/port_def.inc"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* upb_status *****************************************************************/
+
+#define UPB_STATUS_MAX_MESSAGE 127
+
+typedef struct {
+ bool ok;
+ char msg[UPB_STATUS_MAX_MESSAGE]; /* Error message; NULL-terminated. */
+} upb_status;
+
+const char *upb_status_errmsg(const upb_status *status);
+bool upb_ok(const upb_status *status);
+
+/* These are no-op if |status| is NULL. */
+void upb_status_clear(upb_status *status);
+void upb_status_seterrmsg(upb_status *status, const char *msg);
+void upb_status_seterrf(upb_status *status, const char *fmt, ...);
+void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args);
+void upb_status_vappenderrf(upb_status *status, const char *fmt, va_list args);
+
+/** upb_strview ************************************************************/
+
+typedef struct {
+ const char *data;
+ size_t size;
+} upb_strview;
+
+UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) {
+ upb_strview ret;
+ ret.data = data;
+ ret.size = size;
+ return ret;
+}
+
+UPB_INLINE upb_strview upb_strview_makez(const char *data) {
+ return upb_strview_make(data, strlen(data));
+}
+
+UPB_INLINE bool upb_strview_eql(upb_strview a, upb_strview b) {
+ return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
+}
+
+#define UPB_STRVIEW_INIT(ptr, len) {ptr, len}
+
+#define UPB_STRVIEW_FORMAT "%.*s"
+#define UPB_STRVIEW_ARGS(view) (int)(view).size, (view).data
+
+/** upb_alloc *****************************************************************/
+
+/* A upb_alloc is a possibly-stateful allocator object.
+ *
+ * It could either be an arena allocator (which doesn't require individual
+ * free() calls) or a regular malloc() (which does). The client must therefore
+ * free memory unless it knows that the allocator is an arena allocator. */
+
+struct upb_alloc;
+typedef struct upb_alloc upb_alloc;
+
+/* A malloc()/free() function.
+ * If "size" is 0 then the function acts like free(), otherwise it acts like
+ * realloc(). Only "oldsize" bytes from a previous allocation are preserved. */
+typedef void *upb_alloc_func(upb_alloc *alloc, void *ptr, size_t oldsize,
+ size_t size);
+
+struct upb_alloc {
+ upb_alloc_func *func;
+};
+
+UPB_INLINE void *upb_malloc(upb_alloc *alloc, size_t size) {
+ UPB_ASSERT(alloc);
+ return alloc->func(alloc, NULL, 0, size);
+}
+
+UPB_INLINE void *upb_realloc(upb_alloc *alloc, void *ptr, size_t oldsize,
+ size_t size) {
+ UPB_ASSERT(alloc);
+ return alloc->func(alloc, ptr, oldsize, size);
+}
+
+UPB_INLINE void upb_free(upb_alloc *alloc, void *ptr) {
+ assert(alloc);
+ alloc->func(alloc, ptr, 0, 0);
+}
+
+/* The global allocator used by upb. Uses the standard malloc()/free(). */
+
+extern upb_alloc upb_alloc_global;
+
+/* Functions that hard-code the global malloc.
+ *
+ * We still get benefit because we can put custom logic into our global
+ * allocator, like injecting out-of-memory faults in debug/testing builds. */
+
+UPB_INLINE void *upb_gmalloc(size_t size) {
+ return upb_malloc(&upb_alloc_global, size);
+}
+
+UPB_INLINE void *upb_grealloc(void *ptr, size_t oldsize, size_t size) {
+ return upb_realloc(&upb_alloc_global, ptr, oldsize, size);
+}
+
+UPB_INLINE void upb_gfree(void *ptr) {
+ upb_free(&upb_alloc_global, ptr);
+}
+
+/* upb_arena ******************************************************************/
+
+/* upb_arena is a specific allocator implementation that uses arena allocation.
+ * The user provides an allocator that will be used to allocate the underlying
+ * arena blocks. Arenas by nature do not require the individual allocations
+ * to be freed. However the Arena does allow users to register cleanup
+ * functions that will run when the arena is destroyed.
+ *
+ * A upb_arena is *not* thread-safe.
+ *
+ * You could write a thread-safe arena allocator that satisfies the
+ * upb_alloc interface, but it would not be as efficient for the
+ * single-threaded case. */
+
+typedef void upb_cleanup_func(void *ud);
+
+struct upb_arena;
+typedef struct upb_arena upb_arena;
+
+typedef struct {
+ /* We implement the allocator interface.
+ * This must be the first member of upb_arena!
+ * TODO(haberman): remove once handlers are gone. */
+ upb_alloc alloc;
+
+ char *ptr, *end;
+} _upb_arena_head;
+
+/* Creates an arena from the given initial block (if any -- n may be 0).
+ * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this
+ * is a fixed-size arena and cannot grow. */
+upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc);
+void upb_arena_free(upb_arena *a);
+bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func);
+void upb_arena_fuse(upb_arena *a, upb_arena *b);
+void *_upb_arena_slowmalloc(upb_arena *a, size_t size);
+
+UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; }
+
+UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) {
+ _upb_arena_head *h = (_upb_arena_head*)a;
+ void* ret;
+ size = UPB_ALIGN_MALLOC(size);
+
+ if (UPB_UNLIKELY((size_t)(h->end - h->ptr) < size)) {
+ return _upb_arena_slowmalloc(a, size);
+ }
+
+ ret = h->ptr;
+ h->ptr += size;
+ return ret;
+}
+
+UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize,
+ size_t size) {
+ void *ret = upb_arena_malloc(a, size);
+
+ if (ret && oldsize > 0) {
+ memcpy(ret, ptr, oldsize);
+ }
+
+ return ret;
+}
+
+UPB_INLINE upb_arena *upb_arena_new(void) {
+ return upb_arena_init(NULL, 0, &upb_alloc_global);
+}
+
+/* Constants ******************************************************************/
+
+/* Generic function type. */
+typedef void upb_func(void);
+
+/* A list of types as they are encoded on-the-wire. */
+typedef enum {
+ UPB_WIRE_TYPE_VARINT = 0,
+ UPB_WIRE_TYPE_64BIT = 1,
+ UPB_WIRE_TYPE_DELIMITED = 2,
+ UPB_WIRE_TYPE_START_GROUP = 3,
+ UPB_WIRE_TYPE_END_GROUP = 4,
+ UPB_WIRE_TYPE_32BIT = 5
+} upb_wiretype_t;
+
+/* The types a field can have. Note that this list is not identical to the
+ * types defined in descriptor.proto, which gives INT32 and SINT32 separate
+ * types (we distinguish the two with the "integer encoding" enum below). */
+typedef enum {
+ UPB_TYPE_BOOL = 1,
+ UPB_TYPE_FLOAT = 2,
+ UPB_TYPE_INT32 = 3,
+ UPB_TYPE_UINT32 = 4,
+ UPB_TYPE_ENUM = 5, /* Enum values are int32. */
+ UPB_TYPE_MESSAGE = 6,
+ UPB_TYPE_DOUBLE = 7,
+ UPB_TYPE_INT64 = 8,
+ UPB_TYPE_UINT64 = 9,
+ UPB_TYPE_STRING = 10,
+ UPB_TYPE_BYTES = 11
+} upb_fieldtype_t;
+
+/* The repeated-ness of each field; this matches descriptor.proto. */
+typedef enum {
+ UPB_LABEL_OPTIONAL = 1,
+ UPB_LABEL_REQUIRED = 2,
+ UPB_LABEL_REPEATED = 3
+} upb_label_t;
+
+/* Descriptor types, as defined in descriptor.proto. */
+typedef enum {
+ /* Old (long) names. TODO(haberman): remove */
+ UPB_DESCRIPTOR_TYPE_DOUBLE = 1,
+ UPB_DESCRIPTOR_TYPE_FLOAT = 2,
+ UPB_DESCRIPTOR_TYPE_INT64 = 3,
+ UPB_DESCRIPTOR_TYPE_UINT64 = 4,
+ UPB_DESCRIPTOR_TYPE_INT32 = 5,
+ UPB_DESCRIPTOR_TYPE_FIXED64 = 6,
+ UPB_DESCRIPTOR_TYPE_FIXED32 = 7,
+ UPB_DESCRIPTOR_TYPE_BOOL = 8,
+ UPB_DESCRIPTOR_TYPE_STRING = 9,
+ UPB_DESCRIPTOR_TYPE_GROUP = 10,
+ UPB_DESCRIPTOR_TYPE_MESSAGE = 11,
+ UPB_DESCRIPTOR_TYPE_BYTES = 12,
+ UPB_DESCRIPTOR_TYPE_UINT32 = 13,
+ UPB_DESCRIPTOR_TYPE_ENUM = 14,
+ UPB_DESCRIPTOR_TYPE_SFIXED32 = 15,
+ UPB_DESCRIPTOR_TYPE_SFIXED64 = 16,
+ UPB_DESCRIPTOR_TYPE_SINT32 = 17,
+ UPB_DESCRIPTOR_TYPE_SINT64 = 18,
+
+ UPB_DTYPE_DOUBLE = 1,
+ UPB_DTYPE_FLOAT = 2,
+ UPB_DTYPE_INT64 = 3,
+ UPB_DTYPE_UINT64 = 4,
+ UPB_DTYPE_INT32 = 5,
+ UPB_DTYPE_FIXED64 = 6,
+ UPB_DTYPE_FIXED32 = 7,
+ UPB_DTYPE_BOOL = 8,
+ UPB_DTYPE_STRING = 9,
+ UPB_DTYPE_GROUP = 10,
+ UPB_DTYPE_MESSAGE = 11,
+ UPB_DTYPE_BYTES = 12,
+ UPB_DTYPE_UINT32 = 13,
+ UPB_DTYPE_ENUM = 14,
+ UPB_DTYPE_SFIXED32 = 15,
+ UPB_DTYPE_SFIXED64 = 16,
+ UPB_DTYPE_SINT32 = 17,
+ UPB_DTYPE_SINT64 = 18
+} upb_descriptortype_t;
+
+#define UPB_MAP_BEGIN ((size_t)-1)
+
+UPB_INLINE bool _upb_isle(void) {
+ int x = 1;
+ return *(char*)&x == 1;
+}
+
+UPB_INLINE uint32_t _upb_be_swap32(uint32_t val) {
+ if (_upb_isle()) {
+ return val;
+ } else {
+ return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
+ ((val & 0xff0000ULL) >> 8) | ((val & 0xff000000ULL) >> 24);
+ }
+}
+
+UPB_INLINE uint64_t _upb_be_swap64(uint64_t val) {
+ if (_upb_isle()) {
+ return val;
+ } else {
+ return ((val & 0xff) << 56) | ((val & 0xff00) << 40) |
+ ((val & 0xff0000) << 24) | ((val & 0xff000000) << 8) |
+ ((val & 0xff00000000ULL) >> 8) | ((val & 0xff0000000000ULL) >> 24) |
+ ((val & 0xff000000000000ULL) >> 40) |
+ ((val & 0xff00000000000000ULL) >> 56);
+ }
+}
+
+#include "upb/port_undef.inc"
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* UPB_H_ */
diff --git a/contrib/libs/grpc/third_party/upb/upb/upb.hpp b/contrib/libs/grpc/third_party/upb/upb/upb.hpp
new file mode 100644
index 0000000000..a3ec5faff8
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/upb/upb.hpp
@@ -0,0 +1,88 @@
+
+#ifndef UPB_HPP_
+#define UPB_HPP_
+
+#include <memory>
+
+#include "upb/upb.h"
+
+namespace upb {
+
+class Status {
+ public:
+ Status() { upb_status_clear(&status_); }
+
+ upb_status* ptr() { return &status_; }
+
+ // Returns true if there is no error.
+ bool ok() const { return upb_ok(&status_); }
+
+ // Guaranteed to be NULL-terminated.
+ const char *error_message() const { return upb_status_errmsg(&status_); }
+
+ // The error message will be truncated if it is longer than
+ // UPB_STATUS_MAX_MESSAGE-4.
+ void SetErrorMessage(const char *msg) { upb_status_seterrmsg(&status_, msg); }
+ void SetFormattedErrorMessage(const char *fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ upb_status_vseterrf(&status_, fmt, args);
+ va_end(args);
+ }
+
+ // Resets the status to a successful state with no message.
+ void Clear() { upb_status_clear(&status_); }
+
+ private:
+ upb_status status_;
+};
+
+class Arena {
+ public:
+ // A simple arena with no initial memory block and the default allocator.
+ Arena() : ptr_(upb_arena_new(), upb_arena_free) {}
+
+ upb_arena* ptr() { return ptr_.get(); }
+
+ // Allows this arena to be used as a generic allocator.
+ //
+ // The arena does not need free() calls so when using Arena as an allocator
+ // it is safe to skip them. However they are no-ops so there is no harm in
+ // calling free() either.
+ upb_alloc *allocator() { return upb_arena_alloc(ptr_.get()); }
+
+ // Add a cleanup function to run when the arena is destroyed.
+ // Returns false on out-of-memory.
+ template <class T>
+ bool Own(T *obj) {
+ return upb_arena_addcleanup(ptr_.get(), obj, [](void* obj) {
+ delete static_cast<T*>(obj);
+ });
+ }
+
+ void Fuse(Arena& other) { upb_arena_fuse(ptr(), other.ptr()); }
+
+ private:
+ std::unique_ptr<upb_arena, decltype(&upb_arena_free)> ptr_;
+};
+
+// InlinedArena seeds the arenas with a predefined amount of memory. No
+// heap memory will be allocated until the initial block is exceeded.
+template <int N>
+class InlinedArena : public Arena {
+ public:
+ InlinedArena() : ptr_(upb_arena_new(&initial_block_, N, &upb_alloc_global)) {}
+
+ upb_arena* ptr() { return ptr_.get(); }
+
+ private:
+ InlinedArena(const InlinedArena*) = delete;
+ InlinedArena& operator=(const InlinedArena*) = delete;
+
+ std::unique_ptr<upb_arena, decltype(&upb_arena_free)> ptr_;
+ char initial_block_[N];
+};
+
+} // namespace upb
+
+#endif // UPB_HPP_
diff --git a/contrib/libs/grpc/third_party/upb/ya.make b/contrib/libs/grpc/third_party/upb/ya.make
new file mode 100644
index 0000000000..1dc7487601
--- /dev/null
+++ b/contrib/libs/grpc/third_party/upb/ya.make
@@ -0,0 +1,39 @@
+# Generated by devtools/yamaker.
+
+LIBRARY()
+
+OWNER(g:cpp-contrib)
+
+LICENSE(
+ BSD-3-Clause AND
+ Public-Domain
+)
+
+LICENSE_TEXTS(.yandex_meta/licenses.list.txt)
+
+ADDINCL(
+ ${ARCADIA_BUILD_ROOT}/contrib/libs/grpc
+ contrib/libs/grpc
+ contrib/libs/grpc/third_party/upb
+)
+
+NO_COMPILER_WARNINGS()
+
+NO_RUNTIME()
+
+IF (OS_LINUX OR OS_DARWIN)
+ CFLAGS(
+ -DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK=1
+ )
+ENDIF()
+
+SRCS(
+ upb/decode.c
+ upb/encode.c
+ upb/msg.c
+ upb/port.c
+ upb/table.c
+ upb/upb.c
+)
+
+END()