Upgrade nanopb to 0.3.9.2.

Perfomed by merging version 0.3.9.2 from upstream.

Retained local change defining PB_FIELD2 to PB_FIELD to preserve
compatibility with autogenerated nanopb headers committed to Git
in third party vendor code.

Bug: 203713560
Test: build, presubmit
Change-Id: Id4ea86c30c6a3d7e0217dfdfb3eef8c215aa2a62
diff --git a/Android.bp b/Android.bp
new file mode 100644
index 0000000..bd07587
--- /dev/null
+++ b/Android.bp
@@ -0,0 +1,112 @@
+// Copyright (C) 2014 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//
+
+package {
+    default_applicable_licenses: ["external_nanopb-c_license"],
+}
+
+// Added automatically by a large-scale-change that took the approach of
+// 'apply every license found to every target'. While this makes sure we respect
+// every license restriction, it may not be entirely correct.
+//
+// e.g. GPL in an MIT project might only apply to the contrib/ directory.
+//
+// Please consider splitting the single license below into multiple licenses,
+// taking care not to lose any license_kind information, and overriding the
+// default license using the 'licenses: [...]' property on targets as needed.
+//
+// For unused files, consider creating a 'fileGroup' with "//visibility:private"
+// to attach the license to, and including a comment whether the files may be
+// used in the current project.
+//
+// large-scale-change included anything that looked like it might be a license
+// text as a license_text. e.g. LICENSE, NOTICE, COPYING etc.
+//
+// Please consider removing redundant or irrelevant files from 'license_text:'.
+// See: http://go/android-license-faq
+license {
+    name: "external_nanopb-c_license",
+    visibility: [":__subpackages__"],
+    license_kinds: [
+        "SPDX-license-identifier-BSD",
+        "SPDX-license-identifier-MIT",
+        "SPDX-license-identifier-Zlib",
+        "legacy_unencumbered",
+    ],
+    license_text: [
+        "LICENSE.txt",
+        "NOTICE",
+    ],
+}
+
+cc_defaults {
+    name: "libprotobuf-c-nano-defaults",
+    cflags: [
+        "-Wall",
+        "-Werror",
+        "-Wno-sign-compare",
+        "-Wno-unused-parameter",
+    ],
+    srcs: [
+        "pb_common.c",
+        "pb_decode.c",
+        "pb_encode.c",
+    ],
+    sdk_version: "19",
+    export_include_dirs: ["."],
+    vendor_available: true,
+    host_supported: true,
+}
+
+cc_library_static {
+    name: "libprotobuf-c-nano",
+    defaults: ["libprotobuf-c-nano-defaults"],
+}
+
+cc_library_static {
+    name: "libprotobuf-c-nano-enable_malloc",
+    defaults: ["libprotobuf-c-nano-defaults"],
+
+    cflags: ["-DPB_ENABLE_MALLOC"],
+}
+
+cc_library_static {
+    name: "libprotobuf-c-nano-16bit",
+    defaults: ["libprotobuf-c-nano-defaults"],
+
+    cflags: ["-DPB_FIELD_16BIT"],
+}
+
+cc_library_static {
+    name: "libprotobuf-c-nano-enable_malloc-16bit",
+    defaults: ["libprotobuf-c-nano-defaults"],
+
+    cflags: ["-DPB_ENABLE_MALLOC", "-DPB_FIELD_16BIT"],
+}
+
+cc_library_static {
+    name: "libprotobuf-c-nano-32bit",
+    defaults: ["libprotobuf-c-nano-defaults"],
+
+    cflags: ["-DPB_FIELD_32BIT"],
+}
+
+cc_library_static {
+    name: "libprotobuf-c-nano-enable_malloc-32bit",
+    defaults: ["libprotobuf-c-nano-defaults"],
+
+    cflags: ["-DPB_ENABLE_MALLOC", "-DPB_FIELD_32BIT"],
+}
diff --git a/CleanSpec.mk b/CleanSpec.mk
new file mode 100644
index 0000000..e287fe0
--- /dev/null
+++ b/CleanSpec.mk
@@ -0,0 +1,54 @@
+# Copyright (C) 2014 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# If you don't need to do a full clean build but would like to touch
+# a file or delete some intermediate files, add a clean step to the end
+# of the list.  These steps will only be run once, if they haven't been
+# run before.
+#
+# E.g.:
+#     $(call add-clean-step, touch -c external/sqlite/sqlite3.h)
+#     $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libz_intermediates)
+#
+# Always use "touch -c" and "rm -f" or "rm -rf" to gracefully deal with
+# files that are missing or have been moved.
+#
+# Use $(PRODUCT_OUT) to get to the "out/target/product/blah/" directory.
+# Use $(OUT_DIR) to refer to the "out" directory.
+#
+# If you need to re-do something that's already mentioned, just copy
+# the command and add it to the bottom of the list.  E.g., if a change
+# that you made last week required touching a file and a change you
+# made today requires touching the same file, just copy the old
+# touch step and add it to the end of the list.
+#
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
+
+# For example:
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/AndroidTests_intermediates)
+#$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/core_intermediates)
+#$(call add-clean-step, find $(OUT_DIR) -type f -name "IGTalkSession*" -print0 | xargs -0 rm -f)
+#$(call add-clean-step, rm -rf $(PRODUCT_OUT)/data/*)
+
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libnanopb-c-2.8.0*)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libnanopb-c-2.8.0*)
+
+# ************************************************
+# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+# ************************************************
+
+
diff --git a/METADATA b/METADATA
new file mode 100644
index 0000000..6e439ec
--- /dev/null
+++ b/METADATA
@@ -0,0 +1,12 @@
+name: "nanopb"
+description: "Nanopb - Protocol Buffers for Embedded Systems"
+
+third_party {
+  url {
+    type: GIT
+    value: "https://github.com/nanopb/nanopb"
+  }
+  version: "0.3.9.2"
+  last_upgrade_date { year: 2021 month: 10 day: 27 }
+  license_type: NOTICE
+}
diff --git a/MODULE_LICENSE_BSD_LIKE b/MODULE_LICENSE_BSD_LIKE
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/MODULE_LICENSE_BSD_LIKE
diff --git a/NOTICE b/NOTICE
new file mode 100644
index 0000000..d11c9af
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Petteri Aimonen <jpa at nanopb.mail.kapsi.fi>
+
+This software is provided 'as-is', without any express or 
+implied warranty. In no event will the authors be held liable 
+for any damages arising from the use of this software.
+
+Permission is granted to anyone to use this software for any 
+purpose, including commercial applications, and to alter it and 
+redistribute it freely, subject to the following restrictions:
+
+1. The origin of this software must not be misrepresented; you 
+   must not claim that you wrote the original software. If you use 
+   this software in a product, an acknowledgment in the product 
+   documentation would be appreciated but is not required.
+
+2. Altered source versions must be plainly marked as such, and 
+   must not be misrepresented as being the original software.
+
+3. This notice may not be removed or altered from any source 
+   distribution.
diff --git a/OWNERS b/OWNERS
new file mode 100644
index 0000000..400f949
--- /dev/null
+++ b/OWNERS
@@ -0,0 +1,4 @@
+shanyu@google.com
+jeffbailey@google.com
+rtenneti@google.com
+krzysio@google.com
diff --git a/generator/Android.bp b/generator/Android.bp
new file mode 100644
index 0000000..cd7516c
--- /dev/null
+++ b/generator/Android.bp
@@ -0,0 +1,33 @@
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "external_nanopb-c_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-BSD
+    //   SPDX-license-identifier-MIT
+    default_applicable_licenses: ["external_nanopb-c_license"],
+}
+
+python_binary_host {
+   name: "protoc-gen-nanopb",
+   main: "nanopb_generator.py",
+   srcs: [
+       "nanopb_generator.py",
+       "proto/nanopb.proto",
+       "proto/plugin.proto",
+   ],
+   proto: {
+       canonical_path_from_root: false,
+       include_dirs: ["external/protobuf/src"],
+   },
+   libs: ["libprotobuf-python"],
+   version: {
+       py2: {
+           enabled: true,
+           embedded_launcher: true,
+       },
+       py3: {
+           enabled: false,
+       },
+   },
+}
diff --git a/generator/google/protobuf/__init__.py b/generator/google/protobuf/__init__.py
new file mode 100644
index 0000000..6210a40
--- /dev/null
+++ b/generator/google/protobuf/__init__.py
@@ -0,0 +1,39 @@
+# Protocol Buffers - Google's data interchange format
+# Copyright 2008 Google Inc.  All rights reserved.
+# https://developers.google.com/protocol-buffers/
+#
+# 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 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 COPYRIGHT HOLDERS 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 COPYRIGHT
+# OWNER 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 2007 Google Inc. All Rights Reserved.
+
+__version__ = '3.0.0'
+
+if __name__ != '__main__':
+  try:
+    __import__('pkg_resources').declare_namespace(__name__)
+  except ImportError:
+    __path__ = __import__('pkgutil').extend_path(__path__, __name__)
diff --git a/patches/pb.h.patch b/patches/pb.h.patch
new file mode 100644
index 0000000..f269b7d
--- /dev/null
+++ b/patches/pb.h.patch
@@ -0,0 +1,27 @@
+diff --git a/pb.h b/pb.h
+index 174a84b..52d24de 100644
+--- a/pb.h
++++ b/pb.h
+@@ -122,6 +122,11 @@
+ #define PB_STATIC_ASSERT_MSG(MSG, LINE, COUNTER) PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
+ #define PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) pb_static_assertion_##MSG##LINE##COUNTER
+ #endif
++#ifndef STATIC_ASSERT
++#define STATIC_ASSERT PB_STATIC_ASSERT
++#define STATIC_ASSERT_MSG PB_STATIC_ASSERT_MSG
++#define STATIC_ASSERT_MSG_ PB_STATIC_ASSERT_MSG_
++#endif
+ #else
+ #define PB_STATIC_ASSERT(COND,MSG)
+ #endif
+@@ -526,6 +531,7 @@ struct pb_extension_s {
+         PB_ ## rules ## _ ## allocation(tag, message, field, \
+         PB_DATAOFFSET_ ## placement(message, field, prevfield), \
+         PB_LTYPE_MAP_ ## type, ptr)
++#define PB_FIELD2 PB_FIELD
+ 
+ /* Field description for repeated static fixed count fields.*/
+ #define PB_REPEATED_FIXED_COUNT(tag, type, placement, message, field, prevfield, ptr) \
+-- 
+2.18.0.345.g5c9ce644c3-goog
+
diff --git a/pb.h b/pb.h
index a10f012..41c8cdc 100644
--- a/pb.h
+++ b/pb.h
@@ -189,7 +189,7 @@
 #define PB_HTYPE_MASK     0x30
 
 /**** Field allocation types ****/
- 
+
 #define PB_ATYPE_STATIC   0x00
 #define PB_ATYPE_POINTER  0x80
 #define PB_ATYPE_CALLBACK 0x40
@@ -237,7 +237,7 @@
     pb_ssize_t size_offset; /* Offset of array size or has-boolean, relative to data */
     pb_size_t data_size; /* Data size in bytes for a single item */
     pb_size_t array_size; /* Maximum number of entries in array */
-    
+
     /* Field definitions for submessage
      * OR default value for all other non-array, non-callback types
      * If null, then field will zeroed. */
@@ -303,8 +303,8 @@
         bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
         bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
     } funcs;
-#endif    
-    
+#endif
+
     /* Free arg for use by callback */
     void *arg;
 };
@@ -334,7 +334,7 @@
      */
     bool (*decode)(pb_istream_t *stream, pb_extension_t *extension,
                    uint32_t tag, pb_wire_type_t wire_type);
-    
+
     /* Called once after all regular fields have been encoded.
      * If you have something to write, do so and return true.
      * If you do not have anything to write, just return true.
@@ -342,7 +342,7 @@
      * Set to NULL for default handler.
      */
     bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension);
-    
+
     /* Free field for use by the callback. */
     const void *arg;
 };
@@ -351,11 +351,11 @@
     /* Type describing the extension field. Usually you'll initialize
      * this to a pointer to the automatically generated structure. */
     const pb_extension_type_t *type;
-    
+
     /* Destination for the decoded data. This must match the datatype
      * of the extension field. */
     void *dest;
-    
+
     /* Pointer to the next extension handler, or NULL.
      * If this extension does not match a field, the next handler is
      * automatically called. */
@@ -462,7 +462,7 @@
 #define PB_SINGULAR_CALLBACK(tag, st, m, fd, ltype, ptr) \
     {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
     fd, 0, pb_membersize(st, m), 0, ptr}
-    
+
 #define PB_REPEATED_CALLBACK(tag, st, m, fd, ltype, ptr) \
     {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype, \
     fd, 0, pb_membersize(st, m), 0, ptr}
@@ -526,6 +526,7 @@
         PB_ ## rules ## _ ## allocation(tag, message, field, \
         PB_DATAOFFSET_ ## placement(message, field, prevfield), \
         PB_LTYPE_MAP_ ## type, ptr)
+#define PB_FIELD2 PB_FIELD
 
 /* Field description for repeated static fixed count fields.*/
 #define PB_REPEATED_FIXED_COUNT(tag, type, placement, message, field, prevfield, ptr) \