pw_protobuf: add max serialized size constants

Adds a new header (serialized_size.h) with max serialized byte size
constants to pw_protobuf.

Change-Id: Ib00734664fc81c77f00a7b1a6e0c453e6ad35091
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/18820
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: Ewout van Bekkum <ewout@google.com>
diff --git a/pw_protobuf/BUILD b/pw_protobuf/BUILD
index 9935c01..54b4d3a 100644
--- a/pw_protobuf/BUILD
+++ b/pw_protobuf/BUILD
@@ -34,6 +34,7 @@
         "public/pw_protobuf/decoder.h",
         "public/pw_protobuf/encoder.h",
         "public/pw_protobuf/find.h",
+        "public/pw_protobuf/serialized_size.h",
         "public/pw_protobuf/wire_format.h",
     ],
     includes = ["public"],
diff --git a/pw_protobuf/BUILD.gn b/pw_protobuf/BUILD.gn
index eab8eaa..b4638a7 100644
--- a/pw_protobuf/BUILD.gn
+++ b/pw_protobuf/BUILD.gn
@@ -36,6 +36,7 @@
     "public/pw_protobuf/decoder.h",
     "public/pw_protobuf/encoder.h",
     "public/pw_protobuf/find.h",
+    "public/pw_protobuf/serialized_size.h",
     "public/pw_protobuf/wire_format.h",
   ]
   sources = [
diff --git a/pw_protobuf/public/pw_protobuf/serialized_size.h b/pw_protobuf/public/pw_protobuf/serialized_size.h
new file mode 100644
index 0000000..aeb5a68
--- /dev/null
+++ b/pw_protobuf/public/pw_protobuf/serialized_size.h
@@ -0,0 +1,46 @@
+// Copyright 2020 The Pigweed Authors
+//
+// 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
+//
+//     https://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.
+#pragma once
+
+#include <cstdint>
+
+#include "pw_varint/varint.h"
+
+namespace pw::protobuf {
+
+// Field types that directly map to fixed wire types:
+inline constexpr size_t kMaxSizeBytesFixed32 = 4;
+inline constexpr size_t kMaxSizeBytesFixed64 = 8;
+inline constexpr size_t kMaxSizeBytesSfixed32 = 4;
+inline constexpr size_t kMaxSizeBytesSfixed64 = 8;
+inline constexpr size_t kMaxSizeBytesFloat = kMaxSizeBytesFixed32;
+inline constexpr size_t kMaxSizeBytesDouble = kMaxSizeBytesFixed64;
+
+// Field types that map to varint:
+inline constexpr size_t kMaxSizeBytesUint32 = varint::kMaxVarint32SizeBytes;
+inline constexpr size_t kMaxSizeBytesUint64 = varint::kMaxVarint64SizeBytes;
+inline constexpr size_t kMaxSizeBytesSint32 = varint::kMaxVarint32SizeBytes;
+inline constexpr size_t kMaxSizeBytesSint64 = varint::kMaxVarint64SizeBytes;
+// The int32 field type does not use zigzag encoding, ergo negative values
+// can result in the worst case varint size.
+inline constexpr size_t kMaxSizeBytesInt32 = varint::kMaxVarint64SizeBytes;
+inline constexpr size_t kMaxSizeBytesInt64 = varint::kMaxVarint64SizeBytes;
+// The bool field type is backed by a varint, but has a limited value range.
+inline constexpr size_t kMaxSizeBytesBool = 1;
+
+inline constexpr size_t kMaxSizeOfFieldKey = varint::kMaxVarint32SizeBytes;
+
+inline constexpr size_t kMaxSizeOfLength = varint::kMaxVarint32SizeBytes;
+
+}  // namespace pw::protobuf