修改pods

This commit is contained in:
2025-09-20 17:13:38 +08:00
parent 7787b3ee30
commit 28ff2b0264
5251 changed files with 345029 additions and 285168 deletions

View File

@@ -64,7 +64,34 @@ typedef enum {
extern "C" {
#endif
UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType type) {
// Convert from upb_FieldType to upb_CType
UPB_INLINE upb_CType upb_FieldType_CType(upb_FieldType field_type) {
static const upb_CType c_type[] = {
kUpb_CType_Double, // kUpb_FieldType_Double
kUpb_CType_Float, // kUpb_FieldType_Float
kUpb_CType_Int64, // kUpb_FieldType_Int64
kUpb_CType_UInt64, // kUpb_FieldType_UInt64
kUpb_CType_Int32, // kUpb_FieldType_Int32
kUpb_CType_UInt64, // kUpb_FieldType_Fixed64
kUpb_CType_UInt32, // kUpb_FieldType_Fixed32
kUpb_CType_Bool, // kUpb_FieldType_Bool
kUpb_CType_String, // kUpb_FieldType_String
kUpb_CType_Message, // kUpb_FieldType_Group
kUpb_CType_Message, // kUpb_FieldType_Message
kUpb_CType_Bytes, // kUpb_FieldType_Bytes
kUpb_CType_UInt32, // kUpb_FieldType_UInt32
kUpb_CType_Enum, // kUpb_FieldType_Enum
kUpb_CType_Int32, // kUpb_FieldType_SFixed32
kUpb_CType_Int64, // kUpb_FieldType_SFixed64
kUpb_CType_Int32, // kUpb_FieldType_SInt32
kUpb_CType_Int64, // kUpb_FieldType_SInt64
};
// -1 here because the enum is one-based but the table is zero-based.
return c_type[field_type - 1];
}
UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType field_type) {
// clang-format off
const unsigned kUnpackableTypes =
(1 << kUpb_FieldType_String) |
@@ -72,7 +99,7 @@ UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType type) {
(1 << kUpb_FieldType_Message) |
(1 << kUpb_FieldType_Group);
// clang-format on
return (1 << type) & ~kUnpackableTypes;
return (1 << field_type) & ~kUnpackableTypes;
}
#ifdef __cplusplus

View File

@@ -5,8 +5,8 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_WIRE_INTERNAL_SWAP_H_
#define UPB_WIRE_INTERNAL_SWAP_H_
#ifndef UPB_BASE_INTERNAL_ENDIAN_H_
#define UPB_BASE_INTERNAL_ENDIAN_H_
#include <stdint.h>
@@ -17,23 +17,24 @@
extern "C" {
#endif
UPB_INLINE bool _upb_IsLittleEndian(void) {
int x = 1;
UPB_INLINE bool upb_IsLittleEndian(void) {
const int x = 1;
return *(char*)&x == 1;
}
UPB_INLINE uint32_t _upb_BigEndian_Swap32(uint32_t val) {
if (_upb_IsLittleEndian()) return val;
UPB_INLINE uint32_t upb_BigEndian32(uint32_t val) {
if (upb_IsLittleEndian()) return val;
return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
}
UPB_INLINE uint64_t _upb_BigEndian_Swap64(uint64_t val) {
if (_upb_IsLittleEndian()) return val;
UPB_INLINE uint64_t upb_BigEndian64(uint64_t val) {
if (upb_IsLittleEndian()) return val;
return ((uint64_t)_upb_BigEndian_Swap32((uint32_t)val) << 32) |
_upb_BigEndian_Swap32((uint32_t)(val >> 32));
const uint64_t hi = ((uint64_t)upb_BigEndian32((uint32_t)val)) << 32;
const uint64_t lo = upb_BigEndian32((uint32_t)(val >> 32));
return hi | lo;
}
#ifdef __cplusplus
@@ -42,4 +43,4 @@ UPB_INLINE uint64_t _upb_BigEndian_Swap64(uint64_t val) {
#include "upb/port/undef.inc"
#endif /* UPB_WIRE_INTERNAL_SWAP_H_ */
#endif /* UPB_BASE_INTERNAL_ENDIAN_H_ */

View File

@@ -13,7 +13,7 @@
// Must be last.
#include "upb/port/def.inc"
#define _kUpb_Status_MaxMessage 127
#define _kUpb_Status_MaxMessage 511
typedef struct {
bool ok;

View File

@@ -8,11 +8,13 @@
#ifndef UPB_BASE_STATUS_HPP_
#define UPB_BASE_STATUS_HPP_
#ifdef __cplusplus
#include "upb/base/status.h"
namespace upb {
class Status {
class Status final {
public:
Status() { upb_Status_Clear(&status_); }
@@ -47,4 +49,6 @@ class Status {
} // namespace upb
#endif // __cplusplus
#endif // UPB_BASE_STATUS_HPP_

View File

@@ -24,10 +24,6 @@ typedef struct {
const char* data;
size_t size;
} upb_StringView;
// LINT.ThenChange(
// GoogleInternalName0,
// //depot/google3/third_party/upb/bits/golang/accessor.go:map_go_string
// )
#ifdef __cplusplus
extern "C" {
@@ -46,9 +42,15 @@ UPB_INLINE upb_StringView upb_StringView_FromString(const char* data) {
}
UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b) {
return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
return (a.size == b.size) && (!a.size || !memcmp(a.data, b.data, a.size));
}
// LINT.ThenChange(
// GoogleInternalName1,
// //depot/google3/third_party/upb/bits/golang/accessor.go:map_go_string,
// //depot/google3/third_party/upb/bits/typescript/string_view.ts
// )
#ifdef __cplusplus
} /* extern "C" */
#endif

29
Pods/gRPC-Core/third_party/upb/upb/base/upcast.h generated vendored Normal file
View File

@@ -0,0 +1,29 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_BASE_UPCAST_H_
#define UPB_BASE_UPCAST_H_
// Must be last.
#include "upb/port/def.inc"
// This macro provides a way to upcast message pointers in a way that is
// somewhat more bulletproof than blindly casting a pointer. Example:
//
// typedef struct {
// upb_Message UPB_PRIVATE(base);
// } pkg_FooMessage;
//
// void f(pkg_FooMessage* msg) {
// upb_Decode(UPB_UPCAST(msg), ...);
// }
#define UPB_UPCAST(x) (&(x)->base##_dont_copy_me__upb_internal_use_only)
#include "upb/port/undef.inc"
#endif /* UPB_BASE_UPCAST_H_ */

View File

@@ -9,6 +9,7 @@
#define UPB_GENERATED_CODE_SUPPORT_H_
// IWYU pragma: begin_exports
#include "upb/base/upcast.h"
#include "upb/message/accessors.h"
#include "upb/message/array.h"
#include "upb/message/internal/accessors.h"
@@ -26,8 +27,8 @@
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
#include "upb/wire/decode.h"
#include "upb/wire/decode_fast.h"
#include "upb/wire/encode.h"
#include "upb/wire/internal/decode_fast.h"
// IWYU pragma: end_exports
#endif // UPB_GENERATED_CODE_SUPPORT_H_

View File

@@ -1,32 +1,9 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/json/decode.h"
@@ -35,12 +12,24 @@
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/status.h"
#include "upb/base/string_view.h"
#include "upb/lex/atoi.h"
#include "upb/lex/unicode.h"
#include "upb/mem/arena.h"
#include "upb/message/array.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/mini_table/message.h"
#include "upb/reflection/def.h"
#include "upb/reflection/message.h"
#include "upb/wire/encode.h"
@@ -52,6 +41,7 @@ typedef struct {
upb_Arena* arena; /* TODO: should we have a tmp arena for tmp data? */
const upb_DefPool* symtab;
int depth;
int result;
upb_Status* status;
jmp_buf err;
int line;
@@ -61,12 +51,17 @@ typedef struct {
const upb_FieldDef* debug_field;
} jsondec;
typedef struct {
upb_MessageValue value;
bool ignore;
} upb_JsonMessageValue;
enum { JD_OBJECT, JD_ARRAY, JD_STRING, JD_NUMBER, JD_TRUE, JD_FALSE, JD_NULL };
/* Forward declarations of mutually-recursive functions. */
static void jsondec_wellknown(jsondec* d, upb_Message* msg,
const upb_MessageDef* m);
static upb_MessageValue jsondec_value(jsondec* d, const upb_FieldDef* f);
static upb_JsonMessageValue jsondec_value(jsondec* d, const upb_FieldDef* f);
static void jsondec_wellknownvalue(jsondec* d, upb_Message* msg,
const upb_MessageDef* m);
static void jsondec_object(jsondec* d, upb_Message* msg,
@@ -89,9 +84,13 @@ static bool jsondec_isvalue(const upb_FieldDef* f) {
jsondec_isnullvalue(f);
}
UPB_NORETURN static void jsondec_err(jsondec* d, const char* msg) {
static void jsondec_seterrmsg(jsondec* d, const char* msg) {
upb_Status_SetErrorFormat(d->status, "Error parsing JSON @%d:%d: %s", d->line,
(int)(d->ptr - d->line_begin), msg);
}
UPB_NORETURN static void jsondec_err(jsondec* d, const char* msg) {
jsondec_seterrmsg(d, msg);
UPB_LONGJMP(d->err, 1);
}
@@ -106,7 +105,9 @@ UPB_NORETURN static void jsondec_errf(jsondec* d, const char* fmt, ...) {
UPB_LONGJMP(d->err, 1);
}
static void jsondec_skipws(jsondec* d) {
// Advances d->ptr until the next non-whitespace character or to the end of
// the buffer.
static void jsondec_consumews(jsondec* d) {
while (d->ptr != d->end) {
switch (*d->ptr) {
case '\n':
@@ -122,7 +123,16 @@ static void jsondec_skipws(jsondec* d) {
return;
}
}
jsondec_err(d, "Unexpected EOF");
}
// Advances d->ptr until the next non-whitespace character. Postcondition that
// d->ptr is pointing at a valid non-whitespace character (will err if end of
// buffer is reached).
static void jsondec_skipws(jsondec* d) {
jsondec_consumews(d);
if (d->ptr == d->end) {
jsondec_err(d, "Unexpected EOF");
}
}
static bool jsondec_tryparsech(jsondec* d, char ch) {
@@ -157,6 +167,10 @@ static void jsondec_entrysep(jsondec* d) {
}
static int jsondec_rawpeek(jsondec* d) {
if (d->ptr == d->end) {
jsondec_err(d, "Unexpected EOF");
}
switch (*d->ptr) {
case '{':
return JD_OBJECT;
@@ -272,7 +286,7 @@ static void jsondec_skipdigits(jsondec* d) {
static double jsondec_number(jsondec* d) {
const char* start = d->ptr;
assert(jsondec_rawpeek(d) == JD_NUMBER);
UPB_ASSERT(jsondec_rawpeek(d) == JD_NUMBER);
/* Skip over the syntax of a number, as specified by JSON. */
if (*d->ptr == '-') d->ptr++;
@@ -307,9 +321,19 @@ parse:
* (strtod() accepts a superset of JSON syntax). */
errno = 0;
{
// Copy the number into a null-terminated scratch buffer since strtod
// expects a null-terminated string.
char nullz[64];
ptrdiff_t len = d->ptr - start;
if (len > (ptrdiff_t)(sizeof(nullz) - 1)) {
jsondec_err(d, "excessively long number");
}
memcpy(nullz, start, len);
nullz[len] = '\0';
char* end;
double val = strtod(start, &end);
assert(end == d->ptr);
double val = strtod(nullz, &end);
UPB_ASSERT(end - nullz == len);
/* Currently the min/max-val conformance tests fail if we check this. Does
* this mean the conformance tests are wrong or strtod() is wrong, or
@@ -450,7 +474,7 @@ static upb_StringView jsondec_string(jsondec* d) {
}
break;
default:
if ((unsigned char)*d->ptr < 0x20) {
if ((unsigned char)ch < 0x20) {
jsondec_err(d, "Invalid char in JSON string");
}
*end++ = ch;
@@ -647,6 +671,16 @@ static int64_t jsondec_strtoint64(jsondec* d, upb_StringView str) {
return ret;
}
static void jsondec_checkempty(jsondec* d, upb_StringView str,
const upb_FieldDef* f) {
if (str.size != 0) return;
d->result = kUpb_JsonDecodeResult_OkWithEmptyStringNumerics;
upb_Status_SetErrorFormat(d->status,
"Empty string is not a valid number (field: %s). "
"This will be an error in a future version.",
upb_FieldDef_FullName(f));
}
/* Primitive value types ******************************************************/
/* Parse INT32 or INT64 value. */
@@ -668,6 +702,7 @@ static upb_MessageValue jsondec_int(jsondec* d, const upb_FieldDef* f) {
}
case JD_STRING: {
upb_StringView str = jsondec_string(d);
jsondec_checkempty(d, str, f);
val.int64_val = jsondec_strtoint64(d, str);
break;
}
@@ -688,7 +723,7 @@ static upb_MessageValue jsondec_int(jsondec* d, const upb_FieldDef* f) {
/* Parse UINT32 or UINT64 value. */
static upb_MessageValue jsondec_uint(jsondec* d, const upb_FieldDef* f) {
upb_MessageValue val = {0};
upb_MessageValue val;
switch (jsondec_peek(d)) {
case JD_NUMBER: {
@@ -705,6 +740,7 @@ static upb_MessageValue jsondec_uint(jsondec* d, const upb_FieldDef* f) {
}
case JD_STRING: {
upb_StringView str = jsondec_string(d);
jsondec_checkempty(d, str, f);
val.uint64_val = jsondec_strtouint64(d, str);
break;
}
@@ -725,7 +761,7 @@ static upb_MessageValue jsondec_uint(jsondec* d, const upb_FieldDef* f) {
/* Parse DOUBLE or FLOAT value. */
static upb_MessageValue jsondec_double(jsondec* d, const upb_FieldDef* f) {
upb_StringView str;
upb_MessageValue val = {0};
upb_MessageValue val;
switch (jsondec_peek(d)) {
case JD_NUMBER:
@@ -733,14 +769,26 @@ static upb_MessageValue jsondec_double(jsondec* d, const upb_FieldDef* f) {
break;
case JD_STRING:
str = jsondec_string(d);
if (jsondec_streql(str, "NaN")) {
if (str.size == 0) {
jsondec_checkempty(d, str, f);
val.double_val = 0.0;
} else if (jsondec_streql(str, "NaN")) {
val.double_val = NAN;
} else if (jsondec_streql(str, "Infinity")) {
val.double_val = INFINITY;
} else if (jsondec_streql(str, "-Infinity")) {
val.double_val = -INFINITY;
} else {
val.double_val = strtod(str.data, NULL);
char* end;
val.double_val = strtod(str.data, &end);
if (end != str.data + str.size) {
d->result = kUpb_JsonDecodeResult_OkWithEmptyStringNumerics;
upb_Status_SetErrorFormat(
d->status,
"Non-number characters in quoted number (field: %s). "
"This will be an error in a future version.",
upb_FieldDef_FullName(f));
}
}
break;
default:
@@ -768,19 +816,19 @@ static upb_MessageValue jsondec_strfield(jsondec* d, const upb_FieldDef* f) {
return val;
}
static upb_MessageValue jsondec_enum(jsondec* d, const upb_FieldDef* f) {
static upb_JsonMessageValue jsondec_enum(jsondec* d, const upb_FieldDef* f) {
switch (jsondec_peek(d)) {
case JD_STRING: {
upb_StringView str = jsondec_string(d);
const upb_EnumDef* e = upb_FieldDef_EnumSubDef(f);
const upb_EnumValueDef* ev =
upb_EnumDef_FindValueByNameWithSize(e, str.data, str.size);
upb_MessageValue val;
upb_JsonMessageValue val = {.ignore = false};
if (ev) {
val.int32_val = upb_EnumValueDef_Number(ev);
val.value.int32_val = upb_EnumValueDef_Number(ev);
} else {
if (d->options & upb_JsonDecode_IgnoreUnknown) {
val.int32_val = 0;
val.ignore = true;
} else {
jsondec_errf(d, "Unknown enumerator: '" UPB_STRINGVIEW_FORMAT "'",
UPB_STRINGVIEW_ARGS(str));
@@ -790,15 +838,16 @@ static upb_MessageValue jsondec_enum(jsondec* d, const upb_FieldDef* f) {
}
case JD_NULL: {
if (jsondec_isnullvalue(f)) {
upb_MessageValue val;
upb_JsonMessageValue val = {.ignore = false};
jsondec_null(d);
val.int32_val = 0;
val.value.int32_val = 0;
return val;
}
}
/* Fallthrough. */
default:
return jsondec_int(d, f);
return (upb_JsonMessageValue){.value = jsondec_int(d, f),
.ignore = false};
}
}
@@ -837,17 +886,21 @@ static upb_MessageValue jsondec_bool(jsondec* d, const upb_FieldDef* f) {
/* Composite types (array/message/map) ****************************************/
static void jsondec_array(jsondec* d, upb_Message* msg, const upb_FieldDef* f) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Array* arr = upb_Message_Mutable(msg, f, d->arena).array;
jsondec_arrstart(d);
while (jsondec_arrnext(d)) {
upb_MessageValue elem = jsondec_value(d, f);
upb_Array_Append(arr, elem, d->arena);
upb_JsonMessageValue elem = jsondec_value(d, f);
if (!elem.ignore) {
upb_Array_Append(arr, elem.value, d->arena);
}
}
jsondec_arrend(d);
}
static void jsondec_map(jsondec* d, upb_Message* msg, const upb_FieldDef* f) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Map* map = upb_Message_Mutable(msg, f, d->arena).map;
const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(entry, 1);
@@ -855,17 +908,21 @@ static void jsondec_map(jsondec* d, upb_Message* msg, const upb_FieldDef* f) {
jsondec_objstart(d);
while (jsondec_objnext(d)) {
upb_MessageValue key, val;
upb_JsonMessageValue key, val;
key = jsondec_value(d, key_f);
UPB_ASSUME(!key.ignore); // Map key cannot be enum.
jsondec_entrysep(d);
val = jsondec_value(d, val_f);
upb_Map_Set(map, key, val, d->arena);
if (!val.ignore) {
upb_Map_Set(map, key.value, val.value, d->arena);
}
}
jsondec_objend(d);
}
static void jsondec_tomsg(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
if (upb_MessageDef_WellKnownType(m) == kUpb_WellKnown_Unspecified) {
jsondec_object(d, msg, m);
} else {
@@ -886,6 +943,7 @@ static upb_MessageValue jsondec_msg(jsondec* d, const upb_FieldDef* f) {
static void jsondec_field(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_StringView name;
const upb_FieldDef* f;
const upb_FieldDef* preserved;
@@ -924,7 +982,7 @@ static void jsondec_field(jsondec* d, upb_Message* msg,
}
if (upb_FieldDef_RealContainingOneof(f) &&
upb_Message_WhichOneof(msg, upb_FieldDef_ContainingOneof(f))) {
upb_Message_WhichOneofByDef(msg, upb_FieldDef_ContainingOneof(f))) {
jsondec_err(d, "More than one field for this oneof.");
}
@@ -940,8 +998,10 @@ static void jsondec_field(jsondec* d, upb_Message* msg,
const upb_MessageDef* subm = upb_FieldDef_MessageSubDef(f);
jsondec_tomsg(d, submsg, subm);
} else {
upb_MessageValue val = jsondec_value(d, f);
upb_Message_SetFieldByDef(msg, f, val, d->arena);
upb_JsonMessageValue val = jsondec_value(d, f);
if (!val.ignore) {
upb_Message_SetFieldByDef(msg, f, val.value, d->arena);
}
}
d->debug_field = preserved;
@@ -949,6 +1009,7 @@ static void jsondec_field(jsondec* d, upb_Message* msg,
static void jsondec_object(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
jsondec_objstart(d);
while (jsondec_objnext(d)) {
jsondec_field(d, msg, m);
@@ -956,7 +1017,7 @@ static void jsondec_object(jsondec* d, upb_Message* msg,
jsondec_objend(d);
}
static upb_MessageValue jsondec_value(jsondec* d, const upb_FieldDef* f) {
static upb_MessageValue jsondec_nonenum(jsondec* d, const upb_FieldDef* f) {
switch (upb_FieldDef_CType(f)) {
case kUpb_CType_Bool:
return jsondec_bool(d, f);
@@ -972,15 +1033,23 @@ static upb_MessageValue jsondec_value(jsondec* d, const upb_FieldDef* f) {
case kUpb_CType_String:
case kUpb_CType_Bytes:
return jsondec_strfield(d, f);
case kUpb_CType_Enum:
return jsondec_enum(d, f);
case kUpb_CType_Message:
return jsondec_msg(d, f);
case kUpb_CType_Enum:
default:
UPB_UNREACHABLE();
}
}
static upb_JsonMessageValue jsondec_value(jsondec* d, const upb_FieldDef* f) {
if (upb_FieldDef_CType(f) == kUpb_CType_Enum) {
return jsondec_enum(d, f);
} else {
return (upb_JsonMessageValue){.value = jsondec_nonenum(d, f),
.ignore = false};
}
}
/* Well-known types ***********************************************************/
static int jsondec_tsdigits(jsondec* d, const char** ptr, size_t digits,
@@ -1041,6 +1110,7 @@ static int64_t jsondec_unixtime(int y, int m, int d, int h, int min, int s) {
static void jsondec_timestamp(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_MessageValue seconds;
upb_MessageValue nanos;
upb_StringView str = jsondec_string(d);
@@ -1106,6 +1176,7 @@ malformed:
static void jsondec_duration(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_MessageValue seconds;
upb_MessageValue nanos;
upb_StringView str = jsondec_string(d);
@@ -1138,6 +1209,7 @@ static void jsondec_duration(jsondec* d, upb_Message* msg,
static void jsondec_listvalue(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_FieldDef* values_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_MessageDef* value_m = upb_FieldDef_MessageSubDef(values_f);
const upb_MiniTable* value_layout = upb_MessageDef_MiniTable(value_m);
@@ -1156,6 +1228,7 @@ static void jsondec_listvalue(jsondec* d, upb_Message* msg,
static void jsondec_struct(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_FieldDef* fields_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(fields_f);
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(entry_m, 2);
@@ -1178,6 +1251,7 @@ static void jsondec_struct(jsondec* d, upb_Message* msg,
static void jsondec_wellknownvalue(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_MessageValue val;
const upb_FieldDef* f;
upb_Message* submsg;
@@ -1266,6 +1340,7 @@ static upb_StringView jsondec_mask(jsondec* d, const char* buf,
static void jsondec_fieldmask(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
/* repeated string paths = 1; */
const upb_FieldDef* paths_f = upb_MessageDef_FindFieldByNumber(m, 1);
upb_Array* arr = upb_Message_Mutable(msg, paths_f, d->arena).array;
@@ -1289,6 +1364,7 @@ static void jsondec_fieldmask(jsondec* d, upb_Message* msg,
static void jsondec_anyfield(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
if (upb_MessageDef_WellKnownType(m) == kUpb_WellKnown_Unspecified) {
/* For regular types: {"@type": "[user type]", "f1": <V1>, "f2": <V2>}
* where f1, f2, etc. are the normal fields of this type. */
@@ -1307,6 +1383,7 @@ static void jsondec_anyfield(jsondec* d, upb_Message* msg,
static const upb_MessageDef* jsondec_typeurl(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_FieldDef* type_url_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_MessageDef* type_m;
upb_StringView type_url = jsondec_string(d);
@@ -1336,6 +1413,7 @@ static const upb_MessageDef* jsondec_typeurl(jsondec* d, upb_Message* msg,
}
static void jsondec_any(jsondec* d, upb_Message* msg, const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
/* string type_url = 1;
* bytes value = 2; */
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(m, 2);
@@ -1404,13 +1482,16 @@ static void jsondec_any(jsondec* d, upb_Message* msg, const upb_MessageDef* m) {
static void jsondec_wrapper(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(m, 1);
upb_MessageValue val = jsondec_value(d, value_f);
upb_Message_SetFieldByDef(msg, value_f, val, d->arena);
upb_JsonMessageValue val = jsondec_value(d, value_f);
UPB_ASSUME(val.ignore == false); // Wrapper cannot be an enum.
upb_Message_SetFieldByDef(msg, value_f, val.value, d->arena);
}
static void jsondec_wellknown(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
switch (upb_MessageDef_WellKnownType(m)) {
case kUpb_WellKnown_Any:
jsondec_any(d, msg, m);
@@ -1449,17 +1530,32 @@ static void jsondec_wellknown(jsondec* d, upb_Message* msg,
}
}
static bool upb_JsonDecoder_Decode(jsondec* const d, upb_Message* const msg,
const upb_MessageDef* const m) {
if (UPB_SETJMP(d->err)) return false;
static int upb_JsonDecoder_Decode(jsondec* const d, upb_Message* const msg,
const upb_MessageDef* const m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
if (UPB_SETJMP(d->err)) return kUpb_JsonDecodeResult_Error;
jsondec_tomsg(d, msg, m);
return true;
// Consume any trailing whitespace before checking if we read the entire
// input.
jsondec_consumews(d);
if (d->ptr == d->end) {
return d->result;
} else {
jsondec_seterrmsg(d, "unexpected trailing characters");
return kUpb_JsonDecodeResult_Error;
}
}
bool upb_JsonDecode(const char* buf, size_t size, upb_Message* msg,
const upb_MessageDef* m, const upb_DefPool* symtab,
int options, upb_Arena* arena, upb_Status* status) {
int upb_JsonDecodeDetectingNonconformance(const char* buf, size_t size,
upb_Message* msg,
const upb_MessageDef* m,
const upb_DefPool* symtab,
int options, upb_Arena* arena,
upb_Status* status) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
jsondec d;
if (size == 0) return true;
@@ -1471,6 +1567,7 @@ bool upb_JsonDecode(const char* buf, size_t size, upb_Message* msg,
d.status = status;
d.options = options;
d.depth = 64;
d.result = kUpb_JsonDecodeResult_Ok;
d.line = 1;
d.line_begin = d.ptr;
d.debug_field = NULL;

View File

@@ -1,36 +1,18 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_JSON_DECODE_H_
#define UPB_JSON_DECODE_H_
#include <stddef.h>
#include "upb/base/status.h"
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/reflection/def.h"
// Must be last.
@@ -42,9 +24,27 @@ extern "C" {
enum { upb_JsonDecode_IgnoreUnknown = 1 };
UPB_API bool upb_JsonDecode(const char* buf, size_t size, upb_Message* msg,
const upb_MessageDef* m, const upb_DefPool* symtab,
int options, upb_Arena* arena, upb_Status* status);
enum {
kUpb_JsonDecodeResult_Ok = 0,
kUpb_JsonDecodeResult_OkWithEmptyStringNumerics = 1,
kUpb_JsonDecodeResult_Error = 2,
};
UPB_API int upb_JsonDecodeDetectingNonconformance(const char* buf, size_t size,
upb_Message* msg,
const upb_MessageDef* m,
const upb_DefPool* symtab,
int options, upb_Arena* arena,
upb_Status* status);
UPB_API_INLINE bool upb_JsonDecode(const char* buf, size_t size,
upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* symtab, int options,
upb_Arena* arena, upb_Status* status) {
return upb_JsonDecodeDetectingNonconformance(buf, size, msg, m, symtab,
options, arena, status) ==
kUpb_JsonDecodeResult_Ok;
}
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -1,32 +1,9 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/json/encode.h"
@@ -70,7 +47,7 @@ static void jsonenc_value(jsonenc* e, const upb_Message* msg,
UPB_NORETURN static void jsonenc_err(jsonenc* e, const char* msg) {
upb_Status_SetErrorMessage(e->status, msg);
longjmp(e->err, 1);
UPB_LONGJMP(e->err, 1);
}
UPB_PRINTF(2, 3)
@@ -79,7 +56,7 @@ UPB_NORETURN static void jsonenc_errf(jsonenc* e, const char* fmt, ...) {
va_start(argp, fmt);
upb_Status_VSetErrorFormat(e->status, fmt, argp);
va_end(argp);
longjmp(e->err, 1);
UPB_LONGJMP(e->err, 1);
}
static upb_Arena* jsonenc_arena(jsonenc* e) {

View File

@@ -1,32 +1,9 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_JSON_ENCODE_H_
#define UPB_JSON_ENCODE_H_

View File

@@ -8,6 +8,8 @@
#include "upb/lex/round_trip.h"
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// Must be last.
@@ -28,6 +30,10 @@ static void upb_FixLocale(char* p) {
void _upb_EncodeRoundTripDouble(double val, char* buf, size_t size) {
assert(size >= kUpb_RoundTripBufferSize);
if (isnan(val)) {
snprintf(buf, size, "%s", "nan");
return;
}
snprintf(buf, size, "%.*g", DBL_DIG, val);
if (strtod(buf, NULL) != val) {
snprintf(buf, size, "%.*g", DBL_DIG + 2, val);
@@ -38,6 +44,10 @@ void _upb_EncodeRoundTripDouble(double val, char* buf, size_t size) {
void _upb_EncodeRoundTripFloat(float val, char* buf, size_t size) {
assert(size >= kUpb_RoundTripBufferSize);
if (isnan(val)) {
snprintf(buf, size, "%s", "nan");
return;
}
snprintf(buf, size, "%.*g", FLT_DIG, val);
if (strtof(buf, NULL) != val) {
snprintf(buf, size, "%.*g", FLT_DIG + 3, val);

View File

@@ -5,33 +5,164 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/mem/internal/arena.h"
#include "upb/mem/arena.h"
#ifdef UPB_TRACING_ENABLED
#include <stdatomic.h>
#endif
#include <stddef.h>
#include <stdint.h>
#include "upb/mem/alloc.h"
#include "upb/mem/internal/arena.h"
#include "upb/port/atomic.h"
// Must be last.
#include "upb/port/def.inc"
struct _upb_MemBlock {
static UPB_ATOMIC(size_t) max_block_size = 32 << 10;
void upb_Arena_SetMaxBlockSize(size_t max) { max_block_size = max; }
typedef struct upb_MemBlock {
// Atomic only for the benefit of SpaceAllocated().
UPB_ATOMIC(_upb_MemBlock*) next;
UPB_ATOMIC(struct upb_MemBlock*) next;
uint32_t size;
// Data follows.
};
} upb_MemBlock;
static const size_t memblock_reserve =
UPB_ALIGN_UP(sizeof(_upb_MemBlock), UPB_MALLOC_ALIGN);
typedef struct upb_ArenaInternal {
// upb_alloc* together with a low bit which signals if there is an initial
// block.
uintptr_t block_alloc;
typedef struct _upb_ArenaRoot {
upb_Arena* root;
// 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.
// The low bit is tagged:
// 0: pointer to parent
// 1: count, left shifted by one
UPB_ATOMIC(uintptr_t) parent_or_count;
// All nodes that are fused together are in a singly-linked list.
// == NULL at end of list.
UPB_ATOMIC(struct upb_ArenaInternal*) next;
// The last element of the linked list. This is present only as an
// optimization, so that we do not have to iterate over all members for every
// fuse. Only significant for an arena root. In other cases it is ignored.
// == self when no other list members.
UPB_ATOMIC(struct upb_ArenaInternal*) tail;
// Linked list of blocks to free/cleanup. Atomic only for the benefit of
// upb_Arena_SpaceAllocated().
UPB_ATOMIC(upb_MemBlock*) blocks;
} upb_ArenaInternal;
// All public + private state for an arena.
typedef struct {
upb_Arena head;
upb_ArenaInternal body;
} upb_ArenaState;
typedef struct {
upb_ArenaInternal* root;
uintptr_t tagged_count;
} _upb_ArenaRoot;
} upb_ArenaRoot;
static _upb_ArenaRoot _upb_Arena_FindRoot(upb_Arena* a) {
uintptr_t poc = upb_Atomic_Load(&a->parent_or_count, memory_order_acquire);
static const size_t kUpb_MemblockReserve =
UPB_ALIGN_UP(sizeof(upb_MemBlock), UPB_MALLOC_ALIGN);
// Extracts the (upb_ArenaInternal*) from a (upb_Arena*)
static upb_ArenaInternal* upb_Arena_Internal(const upb_Arena* a) {
return &((upb_ArenaState*)a)->body;
}
static bool _upb_Arena_IsTaggedRefcount(uintptr_t parent_or_count) {
return (parent_or_count & 1) == 1;
}
static bool _upb_Arena_IsTaggedPointer(uintptr_t parent_or_count) {
return (parent_or_count & 1) == 0;
}
static uintptr_t _upb_Arena_RefCountFromTagged(uintptr_t parent_or_count) {
UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count));
return parent_or_count >> 1;
}
static uintptr_t _upb_Arena_TaggedFromRefcount(uintptr_t refcount) {
uintptr_t parent_or_count = (refcount << 1) | 1;
UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count));
return parent_or_count;
}
static upb_ArenaInternal* _upb_Arena_PointerFromTagged(
uintptr_t parent_or_count) {
UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count));
return (upb_ArenaInternal*)parent_or_count;
}
static uintptr_t _upb_Arena_TaggedFromPointer(upb_ArenaInternal* ai) {
uintptr_t parent_or_count = (uintptr_t)ai;
UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count));
return parent_or_count;
}
static upb_alloc* _upb_ArenaInternal_BlockAlloc(upb_ArenaInternal* ai) {
return (upb_alloc*)(ai->block_alloc & ~0x1);
}
static uintptr_t _upb_Arena_MakeBlockAlloc(upb_alloc* alloc, bool has_initial) {
uintptr_t alloc_uint = (uintptr_t)alloc;
UPB_ASSERT((alloc_uint & 1) == 0);
return alloc_uint | (has_initial ? 1 : 0);
}
static bool _upb_ArenaInternal_HasInitialBlock(upb_ArenaInternal* ai) {
return ai->block_alloc & 0x1;
}
#ifdef UPB_TRACING_ENABLED
static void (*_init_arena_trace_handler)(const upb_Arena*, size_t size) = NULL;
static void (*_fuse_arena_trace_handler)(const upb_Arena*,
const upb_Arena*) = NULL;
static void (*_free_arena_trace_handler)(const upb_Arena*) = NULL;
void upb_Arena_SetTraceHandler(
void (*initArenaTraceHandler)(const upb_Arena*, size_t size),
void (*fuseArenaTraceHandler)(const upb_Arena*, const upb_Arena*),
void (*freeArenaTraceHandler)(const upb_Arena*)) {
_init_arena_trace_handler = initArenaTraceHandler;
_fuse_arena_trace_handler = fuseArenaTraceHandler;
_free_arena_trace_handler = freeArenaTraceHandler;
}
void upb_Arena_LogInit(const upb_Arena* arena, size_t size) {
if (_init_arena_trace_handler) {
_init_arena_trace_handler(arena, size);
}
}
void upb_Arena_LogFuse(const upb_Arena* arena1, const upb_Arena* arena2) {
if (_fuse_arena_trace_handler) {
_fuse_arena_trace_handler(arena1, arena2);
}
}
void upb_Arena_LogFree(const upb_Arena* arena) {
if (_free_arena_trace_handler) {
_free_arena_trace_handler(arena);
}
}
#endif // UPB_TRACING_ENABLED
static upb_ArenaRoot _upb_Arena_FindRoot(upb_Arena* a) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
uintptr_t poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire);
while (_upb_Arena_IsTaggedPointer(poc)) {
upb_Arena* next = _upb_Arena_PointerFromTagged(poc);
UPB_ASSERT(a != next);
upb_ArenaInternal* next = _upb_Arena_PointerFromTagged(poc);
UPB_ASSERT(ai != next);
uintptr_t next_poc =
upb_Atomic_Load(&next->parent_or_count, memory_order_acquire);
@@ -55,104 +186,133 @@ static _upb_ArenaRoot _upb_Arena_FindRoot(upb_Arena* a) {
// further away over time, but the path towards that root will continue to
// be valid and the creation of the path carries all the memory orderings
// required.
UPB_ASSERT(a != _upb_Arena_PointerFromTagged(next_poc));
upb_Atomic_Store(&a->parent_or_count, next_poc, memory_order_relaxed);
UPB_ASSERT(ai != _upb_Arena_PointerFromTagged(next_poc));
upb_Atomic_Store(&ai->parent_or_count, next_poc, memory_order_relaxed);
}
a = next;
ai = next;
poc = next_poc;
}
return (_upb_ArenaRoot){.root = a, .tagged_count = poc};
return (upb_ArenaRoot){.root = ai, .tagged_count = poc};
}
size_t upb_Arena_SpaceAllocated(upb_Arena* arena) {
arena = _upb_Arena_FindRoot(arena).root;
size_t upb_Arena_SpaceAllocated(upb_Arena* arena, size_t* fused_count) {
upb_ArenaInternal* ai = _upb_Arena_FindRoot(arena).root;
size_t memsize = 0;
size_t local_fused_count = 0;
while (arena != NULL) {
_upb_MemBlock* block =
upb_Atomic_Load(&arena->blocks, memory_order_relaxed);
while (ai != NULL) {
upb_MemBlock* block = upb_Atomic_Load(&ai->blocks, memory_order_relaxed);
while (block != NULL) {
memsize += sizeof(_upb_MemBlock) + block->size;
memsize += sizeof(upb_MemBlock) + block->size;
block = upb_Atomic_Load(&block->next, memory_order_relaxed);
}
arena = upb_Atomic_Load(&arena->next, memory_order_relaxed);
ai = upb_Atomic_Load(&ai->next, memory_order_relaxed);
local_fused_count++;
}
if (fused_count) *fused_count = local_fused_count;
return memsize;
}
bool UPB_PRIVATE(_upb_Arena_Contains)(const upb_Arena* a, void* ptr) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
UPB_ASSERT(ai);
upb_MemBlock* block = upb_Atomic_Load(&ai->blocks, memory_order_relaxed);
while (block) {
uintptr_t beg = (uintptr_t)block;
uintptr_t end = beg + block->size;
if ((uintptr_t)ptr >= beg && (uintptr_t)ptr < end) return true;
block = upb_Atomic_Load(&block->next, memory_order_relaxed);
}
return false;
}
uint32_t upb_Arena_DebugRefCount(upb_Arena* a) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
// These loads could probably be relaxed, but given that this is debug-only,
// it's not worth introducing a new variant for it.
uintptr_t poc = upb_Atomic_Load(&a->parent_or_count, memory_order_acquire);
uintptr_t poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire);
while (_upb_Arena_IsTaggedPointer(poc)) {
a = _upb_Arena_PointerFromTagged(poc);
poc = upb_Atomic_Load(&a->parent_or_count, memory_order_acquire);
ai = _upb_Arena_PointerFromTagged(poc);
poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire);
}
return _upb_Arena_RefCountFromTagged(poc);
}
static void upb_Arena_AddBlock(upb_Arena* a, void* ptr, size_t size) {
_upb_MemBlock* block = ptr;
static void _upb_Arena_AddBlock(upb_Arena* a, void* ptr, size_t size) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
upb_MemBlock* block = ptr;
// Insert into linked list.
block->size = (uint32_t)size;
upb_Atomic_Init(&block->next, a->blocks);
upb_Atomic_Store(&a->blocks, block, memory_order_release);
upb_Atomic_Init(&block->next, ai->blocks);
upb_Atomic_Store(&ai->blocks, block, memory_order_release);
a->head.ptr = UPB_PTR_AT(block, memblock_reserve, char);
a->head.end = UPB_PTR_AT(block, size, char);
a->UPB_PRIVATE(ptr) = UPB_PTR_AT(block, kUpb_MemblockReserve, char);
a->UPB_PRIVATE(end) = UPB_PTR_AT(block, size, char);
UPB_POISON_MEMORY_REGION(a->head.ptr, a->head.end - a->head.ptr);
UPB_POISON_MEMORY_REGION(a->UPB_PRIVATE(ptr),
a->UPB_PRIVATE(end) - a->UPB_PRIVATE(ptr));
}
static bool upb_Arena_AllocBlock(upb_Arena* a, size_t size) {
if (!a->block_alloc) return false;
_upb_MemBlock* last_block = upb_Atomic_Load(&a->blocks, memory_order_acquire);
static bool _upb_Arena_AllocBlock(upb_Arena* a, size_t size) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
if (!ai->block_alloc) return false;
upb_MemBlock* last_block = upb_Atomic_Load(&ai->blocks, memory_order_acquire);
size_t last_size = last_block != NULL ? last_block->size : 128;
size_t block_size = UPB_MAX(size, last_size * 2) + memblock_reserve;
_upb_MemBlock* block = upb_malloc(upb_Arena_BlockAlloc(a), block_size);
// Don't naturally grow beyond the max block size.
size_t clamped_size = UPB_MIN(last_size * 2, max_block_size);
// We may need to exceed the max block size if the user requested a large
// allocation.
size_t block_size = UPB_MAX(size, clamped_size) + kUpb_MemblockReserve;
upb_MemBlock* block =
upb_malloc(_upb_ArenaInternal_BlockAlloc(ai), block_size);
if (!block) return false;
upb_Arena_AddBlock(a, block, block_size);
_upb_Arena_AddBlock(a, block, block_size);
UPB_ASSERT(UPB_PRIVATE(_upb_ArenaHas)(a) >= size);
return true;
}
void* _upb_Arena_SlowMalloc(upb_Arena* a, size_t size) {
if (!upb_Arena_AllocBlock(a, size)) return NULL; /* Out of memory. */
UPB_ASSERT(_upb_ArenaHas(a) >= size);
return upb_Arena_Malloc(a, size);
void* UPB_PRIVATE(_upb_Arena_SlowMalloc)(upb_Arena* a, size_t size) {
if (!_upb_Arena_AllocBlock(a, size)) return NULL; // OOM
return upb_Arena_Malloc(a, size - UPB_ASAN_GUARD_SIZE);
}
/* Public Arena API ***********************************************************/
static upb_Arena* _upb_Arena_InitSlow(upb_alloc* alloc) {
const size_t first_block_overhead =
sizeof(upb_ArenaState) + kUpb_MemblockReserve;
upb_ArenaState* a;
static upb_Arena* upb_Arena_InitSlow(upb_alloc* alloc) {
const size_t first_block_overhead = sizeof(upb_Arena) + memblock_reserve;
upb_Arena* a;
/* We need to malloc the initial block. */
// We need to malloc the initial block.
char* mem;
size_t 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 = UPB_PTR_AT(mem, n - sizeof(upb_ArenaState), upb_ArenaState);
n -= sizeof(upb_ArenaState);
a->block_alloc = upb_Arena_MakeBlockAlloc(alloc, 0);
upb_Atomic_Init(&a->parent_or_count, _upb_Arena_TaggedFromRefcount(1));
upb_Atomic_Init(&a->next, NULL);
upb_Atomic_Init(&a->tail, a);
upb_Atomic_Init(&a->blocks, NULL);
a->body.block_alloc = _upb_Arena_MakeBlockAlloc(alloc, 0);
upb_Atomic_Init(&a->body.parent_or_count, _upb_Arena_TaggedFromRefcount(1));
upb_Atomic_Init(&a->body.next, NULL);
upb_Atomic_Init(&a->body.tail, &a->body);
upb_Atomic_Init(&a->body.blocks, NULL);
upb_Arena_AddBlock(a, mem, n);
_upb_Arena_AddBlock(&a->head, mem, n);
return a;
return &a->head;
}
upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc) {
upb_Arena* a;
UPB_ASSERT(sizeof(void*) * UPB_ARENA_SIZE_HACK >= sizeof(upb_ArenaState));
upb_ArenaState* a;
if (n) {
/* Align initial pointer up so that we return properly-aligned pointers. */
@@ -164,63 +324,75 @@ upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc) {
/* 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));
n = UPB_ALIGN_DOWN(n, UPB_ALIGN_OF(upb_ArenaState));
if (UPB_UNLIKELY(n < sizeof(upb_Arena))) {
return upb_Arena_InitSlow(alloc);
if (UPB_UNLIKELY(n < sizeof(upb_ArenaState))) {
#ifdef UPB_TRACING_ENABLED
upb_Arena* ret = _upb_Arena_InitSlow(alloc);
upb_Arena_LogInit(ret, n);
return ret;
#else
return _upb_Arena_InitSlow(alloc);
#endif
}
a = UPB_PTR_AT(mem, n - sizeof(*a), upb_Arena);
a = UPB_PTR_AT(mem, n - sizeof(upb_ArenaState), upb_ArenaState);
upb_Atomic_Init(&a->parent_or_count, _upb_Arena_TaggedFromRefcount(1));
upb_Atomic_Init(&a->next, NULL);
upb_Atomic_Init(&a->tail, a);
upb_Atomic_Init(&a->blocks, NULL);
a->block_alloc = upb_Arena_MakeBlockAlloc(alloc, 1);
a->head.ptr = mem;
a->head.end = UPB_PTR_AT(mem, n - sizeof(*a), char);
upb_Atomic_Init(&a->body.parent_or_count, _upb_Arena_TaggedFromRefcount(1));
upb_Atomic_Init(&a->body.next, NULL);
upb_Atomic_Init(&a->body.tail, &a->body);
upb_Atomic_Init(&a->body.blocks, NULL);
return a;
a->body.block_alloc = _upb_Arena_MakeBlockAlloc(alloc, 1);
a->head.UPB_PRIVATE(ptr) = mem;
a->head.UPB_PRIVATE(end) = UPB_PTR_AT(mem, n - sizeof(upb_ArenaState), char);
#ifdef UPB_TRACING_ENABLED
upb_Arena_LogInit(&a->head, n);
#endif
return &a->head;
}
static void arena_dofree(upb_Arena* a) {
UPB_ASSERT(_upb_Arena_RefCountFromTagged(a->parent_or_count) == 1);
while (a != NULL) {
static void _upb_Arena_DoFree(upb_ArenaInternal* ai) {
UPB_ASSERT(_upb_Arena_RefCountFromTagged(ai->parent_or_count) == 1);
while (ai != NULL) {
// Load first since arena itself is likely from one of its blocks.
upb_Arena* next_arena =
(upb_Arena*)upb_Atomic_Load(&a->next, memory_order_acquire);
upb_alloc* block_alloc = upb_Arena_BlockAlloc(a);
_upb_MemBlock* block = upb_Atomic_Load(&a->blocks, memory_order_acquire);
upb_ArenaInternal* next_arena =
(upb_ArenaInternal*)upb_Atomic_Load(&ai->next, memory_order_acquire);
upb_alloc* block_alloc = _upb_ArenaInternal_BlockAlloc(ai);
upb_MemBlock* block = upb_Atomic_Load(&ai->blocks, memory_order_acquire);
while (block != NULL) {
// Load first since we are deleting block.
_upb_MemBlock* next_block =
upb_MemBlock* next_block =
upb_Atomic_Load(&block->next, memory_order_acquire);
upb_free(block_alloc, block);
block = next_block;
}
a = next_arena;
ai = next_arena;
}
}
void upb_Arena_Free(upb_Arena* a) {
uintptr_t poc = upb_Atomic_Load(&a->parent_or_count, memory_order_acquire);
upb_ArenaInternal* ai = upb_Arena_Internal(a);
uintptr_t poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire);
retry:
while (_upb_Arena_IsTaggedPointer(poc)) {
a = _upb_Arena_PointerFromTagged(poc);
poc = upb_Atomic_Load(&a->parent_or_count, memory_order_acquire);
ai = _upb_Arena_PointerFromTagged(poc);
poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire);
}
// compare_exchange or fetch_sub are RMW operations, which are more
// expensive then direct loads. As an optimization, we only do RMW ops
// when we need to update things for other threads to see.
if (poc == _upb_Arena_TaggedFromRefcount(1)) {
arena_dofree(a);
#ifdef UPB_TRACING_ENABLED
upb_Arena_LogFree(a);
#endif
_upb_Arena_DoFree(ai);
return;
}
if (upb_Atomic_CompareExchangeWeak(
&a->parent_or_count, &poc,
&ai->parent_or_count, &poc,
_upb_Arena_TaggedFromRefcount(_upb_Arena_RefCountFromTagged(poc) - 1),
memory_order_release, memory_order_acquire)) {
// We were >1 and we decremented it successfully, so we are done.
@@ -232,12 +404,14 @@ retry:
goto retry;
}
static void _upb_Arena_DoFuseArenaLists(upb_Arena* const parent,
upb_Arena* child) {
upb_Arena* parent_tail = upb_Atomic_Load(&parent->tail, memory_order_relaxed);
static void _upb_Arena_DoFuseArenaLists(upb_ArenaInternal* const parent,
upb_ArenaInternal* child) {
upb_ArenaInternal* parent_tail =
upb_Atomic_Load(&parent->tail, memory_order_relaxed);
do {
// Our tail might be stale, but it will always converge to the true tail.
upb_Arena* parent_tail_next =
upb_ArenaInternal* parent_tail_next =
upb_Atomic_Load(&parent_tail->next, memory_order_relaxed);
while (parent_tail_next != NULL) {
parent_tail = parent_tail_next;
@@ -245,7 +419,7 @@ static void _upb_Arena_DoFuseArenaLists(upb_Arena* const parent,
upb_Atomic_Load(&parent_tail->next, memory_order_relaxed);
}
upb_Arena* displaced =
upb_ArenaInternal* displaced =
upb_Atomic_Exchange(&parent_tail->next, child, memory_order_relaxed);
parent_tail = upb_Atomic_Load(&child->tail, memory_order_relaxed);
@@ -257,23 +431,23 @@ static void _upb_Arena_DoFuseArenaLists(upb_Arena* const parent,
upb_Atomic_Store(&parent->tail, parent_tail, memory_order_relaxed);
}
static upb_Arena* _upb_Arena_DoFuse(upb_Arena* a1, upb_Arena* a2,
uintptr_t* ref_delta) {
// `parent_or_count` has two disctint modes
static upb_ArenaInternal* _upb_Arena_DoFuse(upb_Arena* a1, upb_Arena* a2,
uintptr_t* ref_delta) {
// `parent_or_count` has two distinct modes
// - parent pointer mode
// - refcount mode
//
// In parent pointer mode, it may change what pointer it refers to in the
// tree, but it will always approach a root. Any operation that walks the
// tree to the root may collapse levels of the tree concurrently.
_upb_ArenaRoot r1 = _upb_Arena_FindRoot(a1);
_upb_ArenaRoot r2 = _upb_Arena_FindRoot(a2);
upb_ArenaRoot r1 = _upb_Arena_FindRoot(a1);
upb_ArenaRoot r2 = _upb_Arena_FindRoot(a2);
if (r1.root == r2.root) return r1.root; // Already fused.
// Avoid cycles by always fusing into the root with the lower address.
if ((uintptr_t)r1.root > (uintptr_t)r2.root) {
_upb_ArenaRoot tmp = r1;
upb_ArenaRoot tmp = r1;
r1 = r2;
r2 = tmp;
}
@@ -316,7 +490,8 @@ static upb_Arena* _upb_Arena_DoFuse(upb_Arena* a1, upb_Arena* a2,
return r1.root;
}
static bool _upb_Arena_FixupRefs(upb_Arena* new_root, uintptr_t ref_delta) {
static bool _upb_Arena_FixupRefs(upb_ArenaInternal* new_root,
uintptr_t ref_delta) {
if (ref_delta == 0) return true; // No fixup required.
uintptr_t poc =
upb_Atomic_Load(&new_root->parent_or_count, memory_order_relaxed);
@@ -331,38 +506,66 @@ static bool _upb_Arena_FixupRefs(upb_Arena* new_root, uintptr_t ref_delta) {
bool upb_Arena_Fuse(upb_Arena* a1, upb_Arena* a2) {
if (a1 == a2) return true; // trivial fuse
#ifdef UPB_TRACING_ENABLED
upb_Arena_LogFuse(a1, a2);
#endif
upb_ArenaInternal* ai1 = upb_Arena_Internal(a1);
upb_ArenaInternal* ai2 = upb_Arena_Internal(a2);
// Do not fuse initial blocks since we cannot lifetime extend them.
// Any other fuse scenario is allowed.
if (upb_Arena_HasInitialBlock(a1) || upb_Arena_HasInitialBlock(a2)) {
if (_upb_ArenaInternal_HasInitialBlock(ai1) ||
_upb_ArenaInternal_HasInitialBlock(ai2)) {
return false;
}
// The number of refs we ultimately need to transfer to the new root.
uintptr_t ref_delta = 0;
while (true) {
upb_Arena* new_root = _upb_Arena_DoFuse(a1, a2, &ref_delta);
upb_ArenaInternal* new_root = _upb_Arena_DoFuse(a1, a2, &ref_delta);
if (new_root != NULL && _upb_Arena_FixupRefs(new_root, ref_delta)) {
return true;
}
}
}
void upb_Arena_IncRefFor(upb_Arena* arena, const void* owner) {
_upb_ArenaRoot r;
bool upb_Arena_IncRefFor(upb_Arena* a, const void* owner) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
if (_upb_ArenaInternal_HasInitialBlock(ai)) return false;
upb_ArenaRoot r;
retry:
r = _upb_Arena_FindRoot(arena);
r = _upb_Arena_FindRoot(a);
if (upb_Atomic_CompareExchangeWeak(
&r.root->parent_or_count, &r.tagged_count,
_upb_Arena_TaggedFromRefcount(
_upb_Arena_RefCountFromTagged(r.tagged_count) + 1),
memory_order_release, memory_order_acquire)) {
// We incremented it successfully, so we are done.
return;
return true;
}
// We failed update due to parent switching on the arena.
goto retry;
}
void upb_Arena_DecRefFor(upb_Arena* arena, const void* owner) {
upb_Arena_Free(arena);
void upb_Arena_DecRefFor(upb_Arena* a, const void* owner) { upb_Arena_Free(a); }
void UPB_PRIVATE(_upb_Arena_SwapIn)(upb_Arena* des, const upb_Arena* src) {
upb_ArenaInternal* desi = upb_Arena_Internal(des);
upb_ArenaInternal* srci = upb_Arena_Internal(src);
*des = *src;
desi->block_alloc = srci->block_alloc;
upb_MemBlock* blocks = upb_Atomic_Load(&srci->blocks, memory_order_relaxed);
upb_Atomic_Init(&desi->blocks, blocks);
}
void UPB_PRIVATE(_upb_Arena_SwapOut)(upb_Arena* des, const upb_Arena* src) {
upb_ArenaInternal* desi = upb_Arena_Internal(des);
upb_ArenaInternal* srci = upb_Arena_Internal(src);
*des = *src;
upb_MemBlock* blocks = upb_Atomic_Load(&srci->blocks, memory_order_relaxed);
upb_Atomic_Store(&desi->blocks, blocks, memory_order_relaxed);
}

View File

@@ -22,19 +22,15 @@
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/mem/alloc.h"
#include "upb/mem/internal/arena.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_Arena upb_Arena;
typedef struct {
char *ptr, *end;
} _upb_ArenaHead;
#ifdef __cplusplus
extern "C" {
#endif
@@ -47,81 +43,44 @@ UPB_API upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc);
UPB_API void upb_Arena_Free(upb_Arena* a);
UPB_API bool upb_Arena_Fuse(upb_Arena* a, upb_Arena* b);
void upb_Arena_IncRefFor(upb_Arena* arena, const void* owner);
void upb_Arena_DecRefFor(upb_Arena* arena, const void* owner);
bool upb_Arena_IncRefFor(upb_Arena* a, const void* owner);
void upb_Arena_DecRefFor(upb_Arena* a, const void* owner);
void* _upb_Arena_SlowMalloc(upb_Arena* a, size_t size);
size_t upb_Arena_SpaceAllocated(upb_Arena* arena);
uint32_t upb_Arena_DebugRefCount(upb_Arena* arena);
size_t upb_Arena_SpaceAllocated(upb_Arena* a, size_t* fused_count);
uint32_t upb_Arena_DebugRefCount(upb_Arena* a);
UPB_INLINE size_t _upb_ArenaHas(upb_Arena* a) {
_upb_ArenaHead* h = (_upb_ArenaHead*)a;
return (size_t)(h->end - h->ptr);
UPB_API_INLINE upb_Arena* upb_Arena_New(void) {
return upb_Arena_Init(NULL, 0, &upb_alloc_global);
}
UPB_API_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) {
size = UPB_ALIGN_MALLOC(size);
size_t span = size + UPB_ASAN_GUARD_SIZE;
if (UPB_UNLIKELY(_upb_ArenaHas(a) < span)) {
return _upb_Arena_SlowMalloc(a, size);
}
UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size);
// We have enough space to do a fast malloc.
_upb_ArenaHead* h = (_upb_ArenaHead*)a;
void* ret = h->ptr;
UPB_ASSERT(UPB_ALIGN_MALLOC((uintptr_t)ret) == (uintptr_t)ret);
UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size);
UPB_UNPOISON_MEMORY_REGION(ret, size);
UPB_API_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize,
size_t size);
h->ptr += span;
return ret;
}
// Sets the maximum block size for all arenas. This is a global configuration
// setting that will affect all existing and future arenas. If
// upb_Arena_Malloc() is called with a size larger than this, we will exceed
// this size and allocate a larger block.
//
// This API is meant for experimentation only. It will likely be removed in
// the future.
void upb_Arena_SetMaxBlockSize(size_t max);
// Shrinks the last alloc from arena.
// REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena.
// We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if
// this was not the last alloc.
UPB_API_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr,
size_t oldsize, size_t size) {
_upb_ArenaHead* h = (_upb_ArenaHead*)a;
oldsize = UPB_ALIGN_MALLOC(oldsize);
size = UPB_ALIGN_MALLOC(size);
// Must be the last alloc.
UPB_ASSERT((char*)ptr + oldsize == h->ptr - UPB_ASAN_GUARD_SIZE);
UPB_ASSERT(size <= oldsize);
h->ptr = (char*)ptr + size;
}
size_t oldsize, size_t size);
UPB_API_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize,
size_t size) {
_upb_ArenaHead* h = (_upb_ArenaHead*)a;
oldsize = UPB_ALIGN_MALLOC(oldsize);
size = UPB_ALIGN_MALLOC(size);
bool is_most_recent_alloc = (uintptr_t)ptr + oldsize == (uintptr_t)h->ptr;
if (is_most_recent_alloc) {
ptrdiff_t diff = size - oldsize;
if ((ptrdiff_t)_upb_ArenaHas(a) >= diff) {
h->ptr += diff;
return ptr;
}
} else if (size <= oldsize) {
return ptr;
}
void* ret = upb_Arena_Malloc(a, size);
if (ret && oldsize > 0) {
memcpy(ret, ptr, UPB_MIN(oldsize, size));
}
return ret;
}
UPB_API_INLINE upb_Arena* upb_Arena_New(void) {
return upb_Arena_Init(NULL, 0, &upb_alloc_global);
}
#ifdef UPB_TRACING_ENABLED
void upb_Arena_SetTraceHandler(void (*initArenaTraceHandler)(const upb_Arena*,
size_t size),
void (*fuseArenaTraceHandler)(const upb_Arena*,
const upb_Arena*),
void (*freeArenaTraceHandler)(const upb_Arena*));
#endif
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -8,6 +8,8 @@
#ifndef UPB_MEM_ARENA_HPP_
#define UPB_MEM_ARENA_HPP_
#ifdef __cplusplus
#include <memory>
#include "upb/mem/arena.h"
@@ -24,14 +26,18 @@ class Arena {
upb_Arena* ptr() const { return ptr_.get(); }
void Fuse(Arena& other) { upb_Arena_Fuse(ptr(), other.ptr()); }
// Fuses the arenas together.
// This operation can only be performed on arenas with no initial blocks. Will
// return false if the fuse failed due to either arena having an initial
// block.
bool Fuse(Arena& other) { return upb_Arena_Fuse(ptr(), other.ptr()); }
protected:
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.
// 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:
@@ -43,12 +49,14 @@ class InlinedArena : public Arena {
}
private:
InlinedArena(const InlinedArena*) = delete;
InlinedArena& operator=(const InlinedArena*) = delete;
InlinedArena(const InlinedArena&) = delete;
InlinedArena& operator=(const InlinedArena&) = delete;
char initial_block_[N];
};
} // namespace upb
#endif // __cplusplus
#endif // UPB_MEM_ARENA_HPP_

View File

@@ -8,86 +8,107 @@
#ifndef UPB_MEM_INTERNAL_ARENA_H_
#define UPB_MEM_INTERNAL_ARENA_H_
#include "upb/mem/arena.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Must be last.
#include "upb/port/def.inc"
typedef struct _upb_MemBlock _upb_MemBlock;
// This is QUITE an ugly hack, which specifies the number of pointers needed
// to equal (or exceed) the storage required for one upb_Arena.
//
// We need this because the decoder inlines a upb_Arena for performance but
// the full struct is not visible outside of arena.c. Yes, I know, it's awful.
#define UPB_ARENA_SIZE_HACK 7
// LINT.IfChange(upb_Arena)
struct upb_Arena {
_upb_ArenaHead head;
// upb_alloc* together with a low bit which signals if there is an initial
// block.
uintptr_t block_alloc;
// 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.
// The low bit is tagged:
// 0: pointer to parent
// 1: count, left shifted by one
UPB_ATOMIC(uintptr_t) parent_or_count;
// All nodes that are fused together are in a singly-linked list.
UPB_ATOMIC(upb_Arena*) next; // NULL at end of list.
// The last element of the linked list. This is present only as an
// optimization, so that we do not have to iterate over all members for every
// fuse. Only significant for an arena root. In other cases it is ignored.
UPB_ATOMIC(upb_Arena*) tail; // == self when no other list members.
// Linked list of blocks to free/cleanup. Atomic only for the benefit of
// upb_Arena_SpaceAllocated().
UPB_ATOMIC(_upb_MemBlock*) blocks;
char* UPB_ONLYBITS(ptr);
char* UPB_ONLYBITS(end);
};
UPB_INLINE bool _upb_Arena_IsTaggedRefcount(uintptr_t parent_or_count) {
return (parent_or_count & 1) == 1;
// LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/arena.ts:upb_Arena)
#ifdef __cplusplus
extern "C" {
#endif
void UPB_PRIVATE(_upb_Arena_SwapIn)(struct upb_Arena* des,
const struct upb_Arena* src);
void UPB_PRIVATE(_upb_Arena_SwapOut)(struct upb_Arena* des,
const struct upb_Arena* src);
// Returns whether |ptr| was allocated directly by |a| (so care must be used
// with fused arenas).
UPB_API bool UPB_ONLYBITS(_upb_Arena_Contains)(const struct upb_Arena* a,
void* ptr);
UPB_INLINE size_t UPB_PRIVATE(_upb_ArenaHas)(const struct upb_Arena* a) {
return (size_t)(a->UPB_ONLYBITS(end) - a->UPB_ONLYBITS(ptr));
}
UPB_INLINE bool _upb_Arena_IsTaggedPointer(uintptr_t parent_or_count) {
return (parent_or_count & 1) == 0;
UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size) {
void* UPB_PRIVATE(_upb_Arena_SlowMalloc)(struct upb_Arena * a, size_t size);
size = UPB_ALIGN_MALLOC(size);
const size_t span = size + UPB_ASAN_GUARD_SIZE;
if (UPB_UNLIKELY(UPB_PRIVATE(_upb_ArenaHas)(a) < span)) {
return UPB_PRIVATE(_upb_Arena_SlowMalloc)(a, span);
}
// We have enough space to do a fast malloc.
void* ret = a->UPB_ONLYBITS(ptr);
UPB_ASSERT(UPB_ALIGN_MALLOC((uintptr_t)ret) == (uintptr_t)ret);
UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size);
UPB_UNPOISON_MEMORY_REGION(ret, size);
a->UPB_ONLYBITS(ptr) += span;
return ret;
}
UPB_INLINE uintptr_t _upb_Arena_RefCountFromTagged(uintptr_t parent_or_count) {
UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count));
return parent_or_count >> 1;
UPB_API_INLINE void* upb_Arena_Realloc(struct upb_Arena* a, void* ptr,
size_t oldsize, size_t size) {
oldsize = UPB_ALIGN_MALLOC(oldsize);
size = UPB_ALIGN_MALLOC(size);
bool is_most_recent_alloc =
(uintptr_t)ptr + oldsize == (uintptr_t)a->UPB_ONLYBITS(ptr);
if (is_most_recent_alloc) {
ptrdiff_t diff = size - oldsize;
if ((ptrdiff_t)UPB_PRIVATE(_upb_ArenaHas)(a) >= diff) {
a->UPB_ONLYBITS(ptr) += diff;
return ptr;
}
} else if (size <= oldsize) {
return ptr;
}
void* ret = upb_Arena_Malloc(a, size);
if (ret && oldsize > 0) {
memcpy(ret, ptr, UPB_MIN(oldsize, size));
}
return ret;
}
UPB_INLINE uintptr_t _upb_Arena_TaggedFromRefcount(uintptr_t refcount) {
uintptr_t parent_or_count = (refcount << 1) | 1;
UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count));
return parent_or_count;
UPB_API_INLINE void upb_Arena_ShrinkLast(struct upb_Arena* a, void* ptr,
size_t oldsize, size_t size) {
oldsize = UPB_ALIGN_MALLOC(oldsize);
size = UPB_ALIGN_MALLOC(size);
// Must be the last alloc.
UPB_ASSERT((char*)ptr + oldsize ==
a->UPB_ONLYBITS(ptr) - UPB_ASAN_GUARD_SIZE);
UPB_ASSERT(size <= oldsize);
a->UPB_ONLYBITS(ptr) = (char*)ptr + size;
}
UPB_INLINE upb_Arena* _upb_Arena_PointerFromTagged(uintptr_t parent_or_count) {
UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count));
return (upb_Arena*)parent_or_count;
}
UPB_INLINE uintptr_t _upb_Arena_TaggedFromPointer(upb_Arena* a) {
uintptr_t parent_or_count = (uintptr_t)a;
UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count));
return parent_or_count;
}
UPB_INLINE upb_alloc* upb_Arena_BlockAlloc(upb_Arena* arena) {
return (upb_alloc*)(arena->block_alloc & ~0x1);
}
UPB_INLINE uintptr_t upb_Arena_MakeBlockAlloc(upb_alloc* alloc,
bool has_initial) {
uintptr_t alloc_uint = (uintptr_t)alloc;
UPB_ASSERT((alloc_uint & 1) == 0);
return alloc_uint | (has_initial ? 1 : 0);
}
UPB_INLINE bool upb_Arena_HasInitialBlock(upb_Arena* arena) {
return arena->block_alloc & 0x1;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"

View File

@@ -7,65 +7,37 @@
#include "upb/message/accessors.h"
#include <string.h>
#include "upb/mem/arena.h"
#include "upb/message/array.h"
#include "upb/message/internal/array.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/mini_table/field.h"
#include "upb/wire/decode.h"
#include "upb/wire/encode.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/reader.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
// Must be last.
#include "upb/port/def.inc"
upb_MapInsertStatus upb_Message_InsertMapEntry(upb_Map* map,
const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* map_entry_message,
upb_Arena* arena) {
bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* m,
const upb_MiniTableField* f,
upb_Message* map_entry_message, upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(map_entry_message));
const upb_MiniTable* map_entry_mini_table =
mini_table->subs[field->UPB_PRIVATE(submsg_index)].submsg;
upb_MiniTable_MapEntrySubMessage(m, f);
UPB_ASSERT(map_entry_mini_table);
UPB_ASSERT(map_entry_mini_table->field_count == 2);
const upb_MiniTableField* map_entry_key_field =
&map_entry_mini_table->fields[0];
upb_MiniTable_MapKey(map_entry_mini_table);
const upb_MiniTableField* map_entry_value_field =
&map_entry_mini_table->fields[1];
upb_MiniTable_MapValue(map_entry_mini_table);
// Map key/value cannot have explicit defaults,
// hence assuming a zero default is valid.
upb_MessageValue default_val;
memset(&default_val, 0, sizeof(upb_MessageValue));
upb_MessageValue map_entry_key;
upb_MessageValue map_entry_value;
_upb_Message_GetField(map_entry_message, map_entry_key_field, &default_val,
&map_entry_key);
_upb_Message_GetField(map_entry_message, map_entry_value_field, &default_val,
&map_entry_value);
return upb_Map_Insert(map, map_entry_key, map_entry_value, arena);
}
bool upb_Message_IsExactlyEqual(const upb_Message* m1, const upb_Message* m2,
const upb_MiniTable* layout) {
if (m1 == m2) return true;
int opts = kUpb_EncodeOption_SkipUnknown | kUpb_EncodeOption_Deterministic;
upb_Arena* a = upb_Arena_New();
// Compare deterministically serialized payloads with no unknown fields.
size_t size1, size2;
char *data1, *data2;
upb_EncodeStatus status1 = upb_Encode(m1, layout, opts, a, &data1, &size1);
upb_EncodeStatus status2 = upb_Encode(m2, layout, opts, a, &data2, &size2);
if (status1 != kUpb_EncodeStatus_Ok || status2 != kUpb_EncodeStatus_Ok) {
// TODO: How should we fail here? (In Ruby we throw an exception.)
upb_Arena_Free(a);
return false;
}
const bool ret = (size1 == size2) && (memcmp(data1, data2, size1) == 0);
upb_Arena_Free(a);
return ret;
upb_MessageValue map_entry_key =
upb_Message_GetField(map_entry_message, map_entry_key_field, default_val);
upb_MessageValue map_entry_value = upb_Message_GetField(
map_entry_message, map_entry_value_field, default_val);
return upb_Map_Set(map, map_entry_key, map_entry_value, arena);
}

View File

@@ -8,15 +8,19 @@
#ifndef UPB_MESSAGE_ACCESSORS_H_
#define UPB_MESSAGE_ACCESSORS_H_
#include "upb/base/descriptor_constants.h"
#include <stdint.h>
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/array.h"
#include "upb/message/internal/accessors.h"
#include "upb/message/internal/array.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/message.h"
#include "upb/message/map.h"
#include "upb/mini_table/enum.h"
#include "upb/message/message.h"
#include "upb/message/tagged_ptr.h"
#include "upb/message/value.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
@@ -25,351 +29,242 @@
extern "C" {
#endif
UPB_API_INLINE void upb_Message_ClearField(upb_Message* msg,
const upb_MiniTableField* field) {
if (upb_MiniTableField_IsExtension(field)) {
const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)field;
_upb_Message_ClearExtensionField(msg, ext);
} else {
_upb_Message_ClearNonExtensionField(msg, field);
}
}
// Functions ending in BaseField() take a (upb_MiniTableField*) argument
// and work only on non-extension fields.
//
// Functions ending in Extension() take a (upb_MiniTableExtension*) argument
// and work only on extensions.
UPB_API_INLINE void upb_Message_Clear(upb_Message* msg,
const upb_MiniTable* l) {
// Note: Can't use UPB_PTR_AT() here because we are doing pointer subtraction.
char* mem = (char*)msg - sizeof(upb_Message_Internal);
memset(mem, 0, upb_msg_sizeof(l));
}
UPB_API_INLINE void upb_Message_Clear(upb_Message* msg, const upb_MiniTable* m);
UPB_API_INLINE bool upb_Message_HasField(const upb_Message* msg,
const upb_MiniTableField* field) {
if (upb_MiniTableField_IsExtension(field)) {
const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)field;
return _upb_Message_HasExtensionField(msg, ext);
} else {
return _upb_Message_HasNonExtensionField(msg, field);
}
}
UPB_API_INLINE void upb_Message_ClearBaseField(upb_Message* msg,
const upb_MiniTableField* f);
UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber(
const upb_Message* message, const upb_MiniTableField* oneof_field) {
UPB_ASSUME(_upb_MiniTableField_InOneOf(oneof_field));
return _upb_getoneofcase_field(message, oneof_field);
}
UPB_API_INLINE void upb_Message_ClearExtension(upb_Message* msg,
const upb_MiniTableExtension* e);
UPB_API_INLINE bool upb_Message_GetBool(const upb_Message* msg,
const upb_MiniTableField* field,
bool default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Bool);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
bool ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE void upb_Message_ClearOneof(upb_Message* msg,
const upb_MiniTable* m,
const upb_MiniTableField* f);
UPB_API_INLINE bool upb_Message_SetBool(upb_Message* msg,
const upb_MiniTableField* field,
bool value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Bool);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE bool upb_Message_HasBaseField(const upb_Message* msg,
const upb_MiniTableField* f);
UPB_API_INLINE int32_t upb_Message_GetInt32(const upb_Message* msg,
const upb_MiniTableField* field,
int32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(field) == kUpb_CType_Enum);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
int32_t ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_HasExtension(const upb_Message* msg,
const upb_MiniTableExtension* e);
UPB_API_INLINE bool upb_Message_SetInt32(upb_Message* msg,
const upb_MiniTableField* field,
int32_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(field) == kUpb_CType_Enum);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE uint32_t upb_Message_GetUInt32(const upb_Message* msg,
const upb_MiniTableField* field,
uint32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt32);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
uint32_t ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetUInt32(upb_Message* msg,
const upb_MiniTableField* field,
uint32_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt32);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE void upb_Message_SetClosedEnum(
upb_Message* msg, const upb_MiniTable* msg_mini_table,
const upb_MiniTableField* field, int32_t value) {
UPB_ASSERT(upb_MiniTableField_IsClosedEnum(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSERT(upb_MiniTableEnum_CheckValue(
upb_MiniTable_GetSubEnumTable(msg_mini_table, field), value));
_upb_Message_SetNonExtensionField(msg, field, &value);
}
UPB_API_INLINE int64_t upb_Message_GetInt64(const upb_Message* msg,
const upb_MiniTableField* field,
uint64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int64);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
int64_t ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetInt64(upb_Message* msg,
const upb_MiniTableField* field,
int64_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int64);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE uint64_t upb_Message_GetUInt64(const upb_Message* msg,
const upb_MiniTableField* field,
uint64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt64);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
uint64_t ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetUInt64(upb_Message* msg,
const upb_MiniTableField* field,
uint64_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt64);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE float upb_Message_GetFloat(const upb_Message* msg,
const upb_MiniTableField* field,
float default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Float);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
float ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetFloat(upb_Message* msg,
const upb_MiniTableField* field,
float value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Float);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE double upb_Message_GetDouble(const upb_Message* msg,
const upb_MiniTableField* field,
double default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Double);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
double ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetDouble(upb_Message* msg,
const upb_MiniTableField* field,
double value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Double);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE upb_StringView
upb_Message_GetString(const upb_Message* msg, const upb_MiniTableField* field,
upb_StringView def_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_String ||
upb_MiniTableField_CType(field) == kUpb_CType_Bytes);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_StringView);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
upb_StringView ret;
_upb_Message_GetField(msg, field, &def_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetString(upb_Message* msg,
const upb_MiniTableField* field,
upb_StringView value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_String ||
upb_MiniTableField_CType(field) == kUpb_CType_Bytes);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_StringView);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE upb_MessageValue
upb_Message_GetField(const upb_Message* msg, const upb_MiniTableField* f,
upb_MessageValue default_val);
UPB_API_INLINE upb_TaggedMessagePtr upb_Message_GetTaggedMessagePtr(
const upb_Message* msg, const upb_MiniTableField* field,
upb_Message* default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
upb_TaggedMessagePtr tagged;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &tagged);
return tagged;
}
UPB_API_INLINE const upb_Message* upb_Message_GetMessage(
const upb_Message* msg, const upb_MiniTableField* field,
upb_Message* default_val) {
upb_TaggedMessagePtr tagged =
upb_Message_GetTaggedMessagePtr(msg, field, default_val);
return upb_TaggedMessagePtr_GetNonEmptyMessage(tagged);
}
// For internal use only; users cannot set tagged messages because only the
// parser and the message copier are allowed to directly create an empty
// message.
UPB_API_INLINE void _upb_Message_SetTaggedMessagePtr(
upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* field, upb_TaggedMessagePtr sub_message) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSERT(mini_table->subs[field->UPB_PRIVATE(submsg_index)].submsg);
_upb_Message_SetNonExtensionField(msg, field, &sub_message);
}
UPB_API_INLINE void upb_Message_SetMessage(upb_Message* msg,
const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* sub_message) {
_upb_Message_SetTaggedMessagePtr(
msg, mini_table, field, _upb_TaggedMessagePtr_Pack(sub_message, false));
}
UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage(
upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* field, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
upb_Message* sub_message = *UPB_PTR_AT(msg, field->offset, upb_Message*);
if (!sub_message) {
const upb_MiniTable* sub_mini_table =
mini_table->subs[field->UPB_PRIVATE(submsg_index)].submsg;
UPB_ASSERT(sub_mini_table);
sub_message = _upb_Message_New(sub_mini_table, arena);
*UPB_PTR_AT(msg, field->offset, upb_Message*) = sub_message;
_upb_Message_SetPresence(msg, field);
}
return sub_message;
}
upb_Message* default_val);
UPB_API_INLINE const upb_Array* upb_Message_GetArray(
const upb_Message* msg, const upb_MiniTableField* field) {
_upb_MiniTableField_CheckIsArray(field);
upb_Array* ret;
const upb_Array* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
return ret;
}
const upb_Message* msg, const upb_MiniTableField* f);
UPB_API_INLINE bool upb_Message_GetBool(const upb_Message* msg,
const upb_MiniTableField* f,
bool default_val);
UPB_API_INLINE double upb_Message_GetDouble(const upb_Message* msg,
const upb_MiniTableField* field,
double default_val);
UPB_API_INLINE float upb_Message_GetFloat(const upb_Message* msg,
const upb_MiniTableField* f,
float default_val);
UPB_API_INLINE int32_t upb_Message_GetInt32(const upb_Message* msg,
const upb_MiniTableField* f,
int32_t default_val);
UPB_API_INLINE int64_t upb_Message_GetInt64(const upb_Message* msg,
const upb_MiniTableField* f,
int64_t default_val);
UPB_API_INLINE const upb_Map* upb_Message_GetMap(const upb_Message* msg,
const upb_MiniTableField* f);
UPB_API_INLINE const upb_Message* upb_Message_GetMessage(
const upb_Message* msg, const upb_MiniTableField* f);
UPB_API_INLINE upb_Array* upb_Message_GetMutableArray(
upb_Message* msg, const upb_MiniTableField* field) {
_upb_MiniTableField_CheckIsArray(field);
return (upb_Array*)upb_Message_GetArray(msg, field);
}
upb_Message* msg, const upb_MiniTableField* f);
UPB_API_INLINE upb_Map* upb_Message_GetMutableMap(upb_Message* msg,
const upb_MiniTableField* f);
UPB_API_INLINE upb_Message* upb_Message_GetMutableMessage(
upb_Message* msg, const upb_MiniTableField* f);
UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray(
upb_Message* msg, const upb_MiniTableField* field, upb_Arena* arena) {
UPB_ASSERT(arena);
_upb_MiniTableField_CheckIsArray(field);
upb_Array* array = upb_Message_GetMutableArray(msg, field);
if (!array) {
array = _upb_Array_New(arena, 4, _upb_MiniTable_ElementSizeLg2(field));
// Check again due to: https://godbolt.org/z/7WfaoKG1r
_upb_MiniTableField_CheckIsArray(field);
_upb_Message_SetField(msg, field, &array, arena);
}
return array;
}
UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized(
upb_Message* msg, const upb_MiniTableField* field, size_t size,
upb_Arena* arena) {
_upb_MiniTableField_CheckIsArray(field);
upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, field, arena);
if (!arr || !_upb_Array_ResizeUninitialized(arr, size, arena)) return NULL;
return _upb_array_ptr(arr);
}
UPB_API_INLINE const upb_Map* upb_Message_GetMap(
const upb_Message* msg, const upb_MiniTableField* field) {
_upb_MiniTableField_CheckIsMap(field);
_upb_Message_AssertMapIsUntagged(msg, field);
upb_Map* ret;
const upb_Map* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE upb_Map* upb_Message_GetMutableMap(
upb_Message* msg, const upb_MiniTableField* field) {
return (upb_Map*)upb_Message_GetMap(msg, field);
}
upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena);
UPB_API_INLINE upb_Map* upb_Message_GetOrCreateMutableMap(
upb_Message* msg, const upb_MiniTable* map_entry_mini_table,
const upb_MiniTableField* field, upb_Arena* arena) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
const upb_MiniTableField* map_entry_key_field =
&map_entry_mini_table->fields[0];
const upb_MiniTableField* map_entry_value_field =
&map_entry_mini_table->fields[1];
return _upb_Message_GetOrCreateMutableMap(
msg, field,
_upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_key_field)),
_upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_value_field)),
arena);
}
const upb_MiniTableField* f, upb_Arena* arena);
UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage(
upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* f, upb_Arena* arena);
UPB_API_INLINE upb_StringView
upb_Message_GetString(const upb_Message* msg, const upb_MiniTableField* field,
upb_StringView default_val);
UPB_API_INLINE uint32_t upb_Message_GetUInt32(const upb_Message* msg,
const upb_MiniTableField* f,
uint32_t default_val);
UPB_API_INLINE uint64_t upb_Message_GetUInt64(const upb_Message* msg,
const upb_MiniTableField* f,
uint64_t default_val);
UPB_API_INLINE void upb_Message_SetClosedEnum(
upb_Message* msg, const upb_MiniTable* msg_mini_table,
const upb_MiniTableField* f, int32_t value);
// BaseField Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE void upb_Message_SetBaseField(upb_Message* msg,
const upb_MiniTableField* f,
const void* val);
UPB_API_INLINE void upb_Message_SetBaseFieldBool(struct upb_Message* msg,
const upb_MiniTableField* f,
bool value);
UPB_API_INLINE void upb_Message_SetBaseFieldDouble(struct upb_Message* msg,
const upb_MiniTableField* f,
double value);
UPB_API_INLINE void upb_Message_SetBaseFieldFloat(struct upb_Message* msg,
const upb_MiniTableField* f,
float value);
UPB_API_INLINE void upb_Message_SetBaseFieldInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t value);
UPB_API_INLINE void upb_Message_SetBaseFieldInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t value);
UPB_API_INLINE void upb_Message_SetBaseFieldMessage(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_Message* value);
UPB_API_INLINE void upb_Message_SetBaseFieldString(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value);
UPB_API_INLINE void upb_Message_SetBaseFieldUInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value);
UPB_API_INLINE void upb_Message_SetBaseFieldUInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value);
// Extension Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE bool upb_Message_SetExtension(upb_Message* msg,
const upb_MiniTableExtension* e,
const void* value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionBool(
struct upb_Message* msg, const upb_MiniTableExtension* e, bool value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionDouble(
struct upb_Message* msg, const upb_MiniTableExtension* e, double value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionFloat(
struct upb_Message* msg, const upb_MiniTableExtension* e, float value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionInt32(
struct upb_Message* msg, const upb_MiniTableExtension* e, int32_t value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionInt64(
struct upb_Message* msg, const upb_MiniTableExtension* e, int64_t value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionString(
struct upb_Message* msg, const upb_MiniTableExtension* e,
upb_StringView value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionUInt32(
struct upb_Message* msg, const upb_MiniTableExtension* e, uint32_t value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionUInt64(
struct upb_Message* msg, const upb_MiniTableExtension* e, uint64_t value,
upb_Arena* a);
// Universal Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE bool upb_Message_SetBool(upb_Message* msg,
const upb_MiniTableField* f, bool value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetDouble(upb_Message* msg,
const upb_MiniTableField* f,
double value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetFloat(upb_Message* msg,
const upb_MiniTableField* f,
float value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetInt32(upb_Message* msg,
const upb_MiniTableField* f,
int32_t value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetInt64(upb_Message* msg,
const upb_MiniTableField* f,
int64_t value, upb_Arena* a);
// Unlike the other similarly-named setters, this function can only be
// called on base fields. Prefer upb_Message_SetBaseFieldMessage().
UPB_API_INLINE void upb_Message_SetMessage(upb_Message* msg,
const upb_MiniTableField* f,
upb_Message* value);
UPB_API_INLINE bool upb_Message_SetString(upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetUInt32(upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetUInt64(upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value, upb_Arena* a);
////////////////////////////////////////////////////////////////////////////////
UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized(
upb_Message* msg, const upb_MiniTableField* f, size_t size,
upb_Arena* arena);
UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber(
const upb_Message* message, const upb_MiniTableField* oneof_field);
// For a field `f` which is in a oneof, return the field of that
// oneof that is actually set (or NULL if none).
UPB_API_INLINE const upb_MiniTableField* upb_Message_WhichOneof(
const upb_Message* msg, const upb_MiniTable* m,
const upb_MiniTableField* f);
// Updates a map entry given an entry message.
upb_MapInsertStatus upb_Message_InsertMapEntry(upb_Map* map,
const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* map_entry_message,
upb_Arena* arena);
// Compares two messages by serializing them and calling memcmp().
bool upb_Message_IsExactlyEqual(const upb_Message* m1, const upb_Message* m2,
const upb_MiniTable* layout);
bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* map_entry_message, upb_Arena* arena);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -1,105 +1,85 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/message/internal/array.h"
#include "upb/message/array.h"
#include <stdint.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/array.h"
#include "upb/message/message.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/size_log2.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
const char _upb_Array_CTypeSizeLg2Table[] = {
[kUpb_CType_Bool] = 0,
[kUpb_CType_Float] = 2,
[kUpb_CType_Int32] = 2,
[kUpb_CType_UInt32] = 2,
[kUpb_CType_Enum] = 2,
[kUpb_CType_Message] = UPB_SIZE(2, 3),
[kUpb_CType_Double] = 3,
[kUpb_CType_Int64] = 3,
[kUpb_CType_UInt64] = 3,
[kUpb_CType_String] = UPB_SIZE(3, 4),
[kUpb_CType_Bytes] = UPB_SIZE(3, 4),
};
upb_Array* upb_Array_New(upb_Arena* a, upb_CType type) {
return _upb_Array_New(a, 4, _upb_Array_CTypeSizeLg2(type));
const int lg2 = UPB_PRIVATE(_upb_CType_SizeLg2)(type);
return UPB_PRIVATE(_upb_Array_New)(a, 4, lg2);
}
const void* upb_Array_DataPtr(const upb_Array* arr) {
return _upb_array_ptr((upb_Array*)arr);
}
void* upb_Array_MutableDataPtr(upb_Array* arr) { return _upb_array_ptr(arr); }
size_t upb_Array_Size(const upb_Array* arr) { return arr->size; }
upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i) {
UPB_ASSERT(i < upb_Array_Size(arr));
upb_MessageValue ret;
const char* data = _upb_array_constptr(arr);
int lg2 = arr->data & 7;
UPB_ASSERT(i < arr->size);
const char* data = upb_Array_DataPtr(arr);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
memcpy(&ret, data + (i << lg2), 1 << lg2);
return ret;
}
upb_MutableMessageValue upb_Array_GetMutable(upb_Array* arr, size_t i) {
UPB_ASSERT(i < upb_Array_Size(arr));
upb_MutableMessageValue ret;
char* data = upb_Array_MutableDataPtr(arr);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
memcpy(&ret, data + (i << lg2), 1 << lg2);
return ret;
}
void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val) {
char* data = _upb_array_ptr(arr);
int lg2 = arr->data & 7;
UPB_ASSERT(i < arr->size);
UPB_ASSERT(!upb_Array_IsFrozen(arr));
UPB_ASSERT(i < upb_Array_Size(arr));
char* data = upb_Array_MutableDataPtr(arr);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
memcpy(data + (i << lg2), &val, 1 << lg2);
}
bool upb_Array_Append(upb_Array* arr, upb_MessageValue val, upb_Arena* arena) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
UPB_ASSERT(arena);
if (!upb_Array_Resize(arr, arr->size + 1, arena)) {
if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(
arr, arr->UPB_PRIVATE(size) + 1, arena)) {
return false;
}
upb_Array_Set(arr, arr->size - 1, val);
upb_Array_Set(arr, arr->UPB_PRIVATE(size) - 1, val);
return true;
}
void upb_Array_Move(upb_Array* arr, size_t dst_idx, size_t src_idx,
size_t count) {
const int lg2 = arr->data & 7;
char* data = _upb_array_ptr(arr);
UPB_ASSERT(!upb_Array_IsFrozen(arr));
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
char* data = upb_Array_MutableDataPtr(arr);
memmove(&data[dst_idx << lg2], &data[src_idx << lg2], count << lg2);
}
bool upb_Array_Insert(upb_Array* arr, size_t i, size_t count,
upb_Arena* arena) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
UPB_ASSERT(arena);
UPB_ASSERT(i <= arr->size);
UPB_ASSERT(count + arr->size >= count);
const size_t oldsize = arr->size;
if (!upb_Array_Resize(arr, arr->size + count, arena)) {
UPB_ASSERT(i <= arr->UPB_PRIVATE(size));
UPB_ASSERT(count + arr->UPB_PRIVATE(size) >= count);
const size_t oldsize = arr->UPB_PRIVATE(size);
if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(
arr, arr->UPB_PRIVATE(size) + count, arena)) {
return false;
}
upb_Array_Move(arr, i + count, i, oldsize - i);
@@ -111,44 +91,59 @@ bool upb_Array_Insert(upb_Array* arr, size_t i, size_t count,
* |------------|XXXXXXXX|--------|
*/
void upb_Array_Delete(upb_Array* arr, size_t i, size_t count) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
const size_t end = i + count;
UPB_ASSERT(i <= end);
UPB_ASSERT(end <= arr->size);
upb_Array_Move(arr, i, end, arr->size - end);
arr->size -= count;
UPB_ASSERT(end <= arr->UPB_PRIVATE(size));
upb_Array_Move(arr, i, end, arr->UPB_PRIVATE(size) - end);
arr->UPB_PRIVATE(size) -= count;
}
bool upb_Array_Resize(upb_Array* arr, size_t size, upb_Arena* arena) {
const size_t oldsize = arr->size;
if (UPB_UNLIKELY(!_upb_Array_ResizeUninitialized(arr, size, arena))) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
const size_t oldsize = arr->UPB_PRIVATE(size);
if (UPB_UNLIKELY(
!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, size, arena))) {
return false;
}
const size_t newsize = arr->size;
const size_t newsize = arr->UPB_PRIVATE(size);
if (newsize > oldsize) {
const int lg2 = arr->data & 7;
char* data = _upb_array_ptr(arr);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
char* data = upb_Array_MutableDataPtr(arr);
memset(data + (oldsize << lg2), 0, (newsize - oldsize) << lg2);
}
return true;
}
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
bool _upb_array_realloc(upb_Array* arr, size_t min_capacity, upb_Arena* arena) {
size_t new_capacity = UPB_MAX(arr->capacity, 4);
int elem_size_lg2 = arr->data & 7;
size_t old_bytes = arr->capacity << elem_size_lg2;
size_t new_bytes;
void* ptr = _upb_array_ptr(arr);
bool UPB_PRIVATE(_upb_Array_Realloc)(upb_Array* array, size_t min_capacity,
upb_Arena* arena) {
size_t new_capacity = UPB_MAX(array->UPB_PRIVATE(capacity), 4);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array);
size_t old_bytes = array->UPB_PRIVATE(capacity) << lg2;
void* ptr = upb_Array_MutableDataPtr(array);
// Log2 ceiling of size.
while (new_capacity < min_capacity) new_capacity *= 2;
new_bytes = new_capacity << elem_size_lg2;
const size_t new_bytes = new_capacity << lg2;
ptr = upb_Arena_Realloc(arena, ptr, old_bytes, new_bytes);
if (!ptr) return false;
arr->data = _upb_tag_arrptr(ptr, elem_size_lg2);
arr->capacity = new_capacity;
UPB_PRIVATE(_upb_Array_SetTaggedPtr)(array, ptr, lg2);
array->UPB_PRIVATE(capacity) = new_capacity;
return true;
}
void upb_Array_Freeze(upb_Array* arr, const upb_MiniTable* m) {
if (upb_Array_IsFrozen(arr)) return;
UPB_PRIVATE(_upb_Array_ShallowFreeze)(arr);
if (m) {
const size_t size = upb_Array_Size(arr);
for (size_t i = 0; i < size; i++) {
upb_MessageValue val = upb_Array_Get(arr, i);
upb_Message_Freeze((upb_Message*)val.msg_val, m);
}
}
}

View File

@@ -1,32 +1,9 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_ARRAY_H_
#define UPB_MESSAGE_ARRAY_H_
@@ -35,7 +12,10 @@
#include "upb/base/descriptor_constants.h"
#include "upb/mem/arena.h"
#include "upb/message/value.h" // IWYU pragma: export
#include "upb/message/internal/array.h"
#include "upb/message/value.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
@@ -50,11 +30,15 @@ extern "C" {
UPB_API upb_Array* upb_Array_New(upb_Arena* a, upb_CType type);
// Returns the number of elements in the array.
UPB_API size_t upb_Array_Size(const upb_Array* arr);
UPB_API_INLINE size_t upb_Array_Size(const upb_Array* arr);
// Returns the given element, which must be within the array's current size.
UPB_API upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i);
// Returns a mutating pointer to the given element, which must be within the
// array's current size.
UPB_API upb_MutableMessageValue upb_Array_GetMutable(upb_Array* arr, size_t i);
// Sets the given element, which must be within the array's current size.
UPB_API void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val);
@@ -79,15 +63,27 @@ UPB_API bool upb_Array_Insert(upb_Array* array, size_t i, size_t count,
// REQUIRES: `i + count <= upb_Array_Size(arr)`
UPB_API void upb_Array_Delete(upb_Array* array, size_t i, size_t count);
// Reserves |size| elements of storage for the array.
UPB_API_INLINE bool upb_Array_Reserve(struct upb_Array* array, size_t size,
upb_Arena* arena);
// Changes the size of a vector. New elements are initialized to NULL/0.
// Returns false on allocation failure.
UPB_API bool upb_Array_Resize(upb_Array* array, size_t size, upb_Arena* arena);
// Returns pointer to array data.
UPB_API const void* upb_Array_DataPtr(const upb_Array* arr);
UPB_API_INLINE const void* upb_Array_DataPtr(const upb_Array* arr);
// Returns mutable pointer to array data.
UPB_API void* upb_Array_MutableDataPtr(upb_Array* arr);
UPB_API_INLINE void* upb_Array_MutableDataPtr(upb_Array* arr);
// Mark an array and all of its descendents as frozen/immutable.
// If the array elements are messages then |m| must point to the minitable for
// those messages. Otherwise |m| must be NULL.
UPB_API void upb_Array_Freeze(upb_Array* arr, const upb_MiniTable* m);
// Returns whether an array has been frozen.
UPB_API_INLINE bool upb_Array_IsFrozen(const upb_Array* arr);
#ifdef __cplusplus
} /* extern "C" */

39
Pods/gRPC-Core/third_party/upb/upb/message/compat.c generated vendored Normal file
View File

@@ -0,0 +1,39 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/message/compat.h"
#include <stddef.h>
#include <stdint.h>
#include "upb/message/internal/extension.h"
#include "upb/message/message.h"
#include "upb/mini_table/extension.h"
// Must be last.
#include "upb/port/def.inc"
const upb_MiniTableExtension* upb_Message_ExtensionByIndex(
const upb_Message* msg, size_t index) {
size_t count;
const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getexts)(msg, &count);
UPB_ASSERT(index < count);
return ext[index].ext;
}
const upb_MiniTableExtension* upb_Message_FindExtensionByNumber(
const upb_Message* msg, uint32_t field_number) {
size_t count;
const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getexts)(msg, &count);
for (; count--; ext++) {
const upb_MiniTableExtension* e = ext->ext;
if (upb_MiniTableExtension_Number(e) == field_number) return e;
}
return NULL;
}

41
Pods/gRPC-Core/third_party/upb/upb/message/compat.h generated vendored Normal file
View File

@@ -0,0 +1,41 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_COMPAT_H_
#define UPB_MESSAGE_COMPAT_H_
#include <stdint.h>
#include "upb/message/message.h"
#include "upb/mini_table/extension.h"
// Must be last.
#include "upb/port/def.inc"
// upb does not support mixing minitables from different sources but these
// functions are still used by some existing users so for now we make them
// available here. This may or may not change in the future so do not add
// them to new code.
#ifdef __cplusplus
extern "C" {
#endif
const upb_MiniTableExtension* upb_Message_ExtensionByIndex(
const upb_Message* msg, size_t index);
// Returns the minitable with the given field number, or NULL on failure.
const upb_MiniTableExtension* upb_Message_FindExtensionByNumber(
const upb_Message* msg, uint32_t field_number);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_COMPAT_H_ */

325
Pods/gRPC-Core/third_party/upb/upb/message/copy.c generated vendored Normal file
View File

@@ -0,0 +1,325 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/message/copy.h"
#include <stdbool.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/accessors.h"
#include "upb/message/array.h"
#include "upb/message/internal/accessors.h"
#include "upb/message/internal/array.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/message/tagged_ptr.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/size_log2.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
// Must be last.
#include "upb/port/def.inc"
static upb_StringView upb_Clone_StringView(upb_StringView str,
upb_Arena* arena) {
if (str.size == 0) {
return upb_StringView_FromDataAndSize(NULL, 0);
}
void* cloned_data = upb_Arena_Malloc(arena, str.size);
upb_StringView cloned_str =
upb_StringView_FromDataAndSize(cloned_data, str.size);
memcpy(cloned_data, str.data, str.size);
return cloned_str;
}
static bool upb_Clone_MessageValue(void* value, upb_CType value_type,
const upb_MiniTable* sub, upb_Arena* arena) {
switch (value_type) {
case kUpb_CType_Bool:
case kUpb_CType_Float:
case kUpb_CType_Int32:
case kUpb_CType_UInt32:
case kUpb_CType_Enum:
case kUpb_CType_Double:
case kUpb_CType_Int64:
case kUpb_CType_UInt64:
return true;
case kUpb_CType_String:
case kUpb_CType_Bytes: {
upb_StringView source = *(upb_StringView*)value;
int size = source.size;
void* cloned_data = upb_Arena_Malloc(arena, size);
if (cloned_data == NULL) {
return false;
}
*(upb_StringView*)value =
upb_StringView_FromDataAndSize(cloned_data, size);
memcpy(cloned_data, source.data, size);
return true;
} break;
case kUpb_CType_Message: {
const upb_TaggedMessagePtr source = *(upb_TaggedMessagePtr*)value;
bool is_empty = upb_TaggedMessagePtr_IsEmpty(source);
if (is_empty) sub = UPB_PRIVATE(_upb_MiniTable_Empty)();
UPB_ASSERT(source);
upb_Message* clone = upb_Message_DeepClone(
UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(source), sub, arena);
*(upb_TaggedMessagePtr*)value =
UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(clone, is_empty);
return clone != NULL;
} break;
}
UPB_UNREACHABLE();
}
upb_Map* upb_Map_DeepClone(const upb_Map* map, upb_CType key_type,
upb_CType value_type,
const upb_MiniTable* map_entry_table,
upb_Arena* arena) {
upb_Map* cloned_map = _upb_Map_New(arena, map->key_size, map->val_size);
if (cloned_map == NULL) {
return NULL;
}
upb_MessageValue key, val;
size_t iter = kUpb_Map_Begin;
while (upb_Map_Next(map, &key, &val, &iter)) {
const upb_MiniTableField* value_field =
upb_MiniTable_MapValue(map_entry_table);
const upb_MiniTable* value_sub =
upb_MiniTableField_CType(value_field) == kUpb_CType_Message
? upb_MiniTable_GetSubMessageTable(map_entry_table, value_field)
: NULL;
upb_CType value_field_type = upb_MiniTableField_CType(value_field);
if (!upb_Clone_MessageValue(&val, value_field_type, value_sub, arena)) {
return NULL;
}
if (!upb_Map_Set(cloned_map, key, val, arena)) {
return NULL;
}
}
return cloned_map;
}
static upb_Map* upb_Message_Map_DeepClone(const upb_Map* map,
const upb_MiniTable* mini_table,
const upb_MiniTableField* f,
upb_Message* clone,
upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(clone));
const upb_MiniTable* map_entry_table =
upb_MiniTable_MapEntrySubMessage(mini_table, f);
UPB_ASSERT(map_entry_table);
const upb_MiniTableField* key_field = upb_MiniTable_MapKey(map_entry_table);
const upb_MiniTableField* value_field =
upb_MiniTable_MapValue(map_entry_table);
upb_Map* cloned_map = upb_Map_DeepClone(
map, upb_MiniTableField_CType(key_field),
upb_MiniTableField_CType(value_field), map_entry_table, arena);
if (!cloned_map) {
return NULL;
}
upb_Message_SetBaseField(clone, f, &cloned_map);
return cloned_map;
}
upb_Array* upb_Array_DeepClone(const upb_Array* array, upb_CType value_type,
const upb_MiniTable* sub, upb_Arena* arena) {
const size_t size = upb_Array_Size(array);
const int lg2 = UPB_PRIVATE(_upb_CType_SizeLg2)(value_type);
upb_Array* cloned_array = UPB_PRIVATE(_upb_Array_New)(arena, size, lg2);
if (!cloned_array) {
return NULL;
}
if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(cloned_array, size, arena)) {
return NULL;
}
for (size_t i = 0; i < size; ++i) {
upb_MessageValue val = upb_Array_Get(array, i);
if (!upb_Clone_MessageValue(&val, value_type, sub, arena)) {
return false;
}
upb_Array_Set(cloned_array, i, val);
}
return cloned_array;
}
static bool upb_Message_Array_DeepClone(const upb_Array* array,
const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* clone, upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(clone));
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(field);
upb_Array* cloned_array = upb_Array_DeepClone(
array, upb_MiniTableField_CType(field),
upb_MiniTableField_CType(field) == kUpb_CType_Message
? upb_MiniTable_GetSubMessageTable(mini_table, field)
: NULL,
arena);
// Clear out upb_Array* due to parent memcpy.
upb_Message_SetBaseField(clone, field, &cloned_array);
return true;
}
static bool upb_Clone_ExtensionValue(
const upb_MiniTableExtension* mini_table_ext, const upb_Extension* source,
upb_Extension* dest, upb_Arena* arena) {
dest->data = source->data;
return upb_Clone_MessageValue(
&dest->data, upb_MiniTableExtension_CType(mini_table_ext),
upb_MiniTableExtension_GetSubMessage(mini_table_ext), arena);
}
upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* mini_table,
upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(dst));
upb_StringView empty_string = upb_StringView_FromDataAndSize(NULL, 0);
// Only copy message area skipping upb_Message_Internal.
memcpy(dst + 1, src + 1, mini_table->UPB_PRIVATE(size) - sizeof(upb_Message));
for (int i = 0; i < upb_MiniTable_FieldCount(mini_table); ++i) {
const upb_MiniTableField* field =
upb_MiniTable_GetFieldByIndex(mini_table, i);
if (upb_MiniTableField_IsScalar(field)) {
switch (upb_MiniTableField_CType(field)) {
case kUpb_CType_Message: {
upb_TaggedMessagePtr tagged =
upb_Message_GetTaggedMessagePtr(src, field, NULL);
const upb_Message* sub_message =
UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(tagged);
if (sub_message != NULL) {
// If the message is currently in an unlinked, "empty" state we keep
// it that way, because we don't want to deal with decode options,
// decode status, or possible parse failure here.
bool is_empty = upb_TaggedMessagePtr_IsEmpty(tagged);
const upb_MiniTable* sub_message_table =
is_empty ? UPB_PRIVATE(_upb_MiniTable_Empty)()
: upb_MiniTable_GetSubMessageTable(mini_table, field);
upb_Message* dst_sub_message =
upb_Message_DeepClone(sub_message, sub_message_table, arena);
if (dst_sub_message == NULL) {
return NULL;
}
UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)
(dst, field,
UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(dst_sub_message,
is_empty));
}
} break;
case kUpb_CType_String:
case kUpb_CType_Bytes: {
upb_StringView str = upb_Message_GetString(src, field, empty_string);
if (str.size != 0) {
if (!upb_Message_SetString(
dst, field, upb_Clone_StringView(str, arena), arena)) {
return NULL;
}
}
} break;
default:
// Scalar, already copied.
break;
}
} else {
if (upb_MiniTableField_IsMap(field)) {
const upb_Map* map = upb_Message_GetMap(src, field);
if (map != NULL) {
if (!upb_Message_Map_DeepClone(map, mini_table, field, dst, arena)) {
return NULL;
}
}
} else {
const upb_Array* array = upb_Message_GetArray(src, field);
if (array != NULL) {
if (!upb_Message_Array_DeepClone(array, mini_table, field, dst,
arena)) {
return NULL;
}
}
}
}
}
// Clone extensions.
size_t ext_count;
const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getexts)(src, &ext_count);
for (size_t i = 0; i < ext_count; ++i) {
const upb_Extension* msg_ext = &ext[i];
const upb_MiniTableField* field = &msg_ext->ext->UPB_PRIVATE(field);
upb_Extension* dst_ext = UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
dst, msg_ext->ext, arena);
if (!dst_ext) return NULL;
if (upb_MiniTableField_IsScalar(field)) {
if (!upb_Clone_ExtensionValue(msg_ext->ext, msg_ext, dst_ext, arena)) {
return NULL;
}
} else {
upb_Array* msg_array = (upb_Array*)msg_ext->data.array_val;
UPB_ASSERT(msg_array);
upb_Array* cloned_array = upb_Array_DeepClone(
msg_array, upb_MiniTableField_CType(field),
upb_MiniTableExtension_GetSubMessage(msg_ext->ext), arena);
if (!cloned_array) {
return NULL;
}
dst_ext->data.array_val = cloned_array;
}
}
// Clone unknowns.
size_t unknown_size = 0;
const char* ptr = upb_Message_GetUnknown(src, &unknown_size);
if (unknown_size != 0) {
UPB_ASSERT(ptr);
// Make a copy into destination arena.
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(dst, ptr, unknown_size, arena)) {
return NULL;
}
}
return dst;
}
bool upb_Message_DeepCopy(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* mini_table, upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(dst));
upb_Message_Clear(dst, mini_table);
return _upb_Message_Copy(dst, src, mini_table, arena) != NULL;
}
// Deep clones a message using the provided target arena.
//
// Returns NULL on failure.
upb_Message* upb_Message_DeepClone(const upb_Message* msg,
const upb_MiniTable* m, upb_Arena* arena) {
upb_Message* clone = upb_Message_New(m, arena);
return _upb_Message_Copy(clone, msg, m, arena);
}
// Performs a shallow copy. TODO: Extend to handle unknown fields.
void upb_Message_ShallowCopy(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* m) {
UPB_ASSERT(!upb_Message_IsFrozen(dst));
memcpy(dst, src, m->UPB_PRIVATE(size));
}
// Performs a shallow clone. Ignores unknown fields.
upb_Message* upb_Message_ShallowClone(const upb_Message* msg,
const upb_MiniTable* m,
upb_Arena* arena) {
upb_Message* clone = upb_Message_New(m, arena);
upb_Message_ShallowCopy(clone, msg, m);
return clone;
}

56
Pods/gRPC-Core/third_party/upb/upb/message/copy.h generated vendored Normal file
View File

@@ -0,0 +1,56 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_COPY_H_
#define UPB_MESSAGE_COPY_H_
#include "upb/mem/arena.h"
#include "upb/message/array.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// Deep clones a message using the provided target arena.
upb_Message* upb_Message_DeepClone(const upb_Message* msg,
const upb_MiniTable* m, upb_Arena* arena);
// Shallow clones a message using the provided target arena.
upb_Message* upb_Message_ShallowClone(const upb_Message* msg,
const upb_MiniTable* m, upb_Arena* arena);
// Deep clones array contents.
upb_Array* upb_Array_DeepClone(const upb_Array* array, upb_CType value_type,
const upb_MiniTable* sub, upb_Arena* arena);
// Deep clones map contents.
upb_Map* upb_Map_DeepClone(const upb_Map* map, upb_CType key_type,
upb_CType value_type,
const upb_MiniTable* map_entry_table,
upb_Arena* arena);
// Deep copies the message from src to dst.
bool upb_Message_DeepCopy(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* m, upb_Arena* arena);
// Shallow copies the message from src to dst.
void upb_Message_ShallowCopy(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* m);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif // UPB_MESSAGE_COPY_H_

View File

@@ -8,10 +8,26 @@
#ifndef UPB_MESSAGE_INTERNAL_ACCESSORS_H_
#define UPB_MESSAGE_INTERNAL_ACCESSORS_H_
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/endian.h"
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/array.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/tagged_ptr.h"
#include "upb/message/internal/types.h"
#include "upb/message/value.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
@@ -42,101 +58,114 @@ extern "C" {
// Hasbit access ///////////////////////////////////////////////////////////////
UPB_INLINE size_t _upb_hasbit_ofs(size_t idx) { return idx / 8; }
UPB_INLINE bool UPB_PRIVATE(_upb_Message_GetHasbit)(
const struct upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
UPB_INLINE char _upb_hasbit_mask(size_t idx) { return 1 << (idx % 8); }
UPB_INLINE bool _upb_hasbit(const upb_Message* msg, size_t idx) {
return (*UPB_PTR_AT(msg, _upb_hasbit_ofs(idx), const char) &
_upb_hasbit_mask(idx)) != 0;
return (*UPB_PTR_AT(msg, offset, const char) & mask) != 0;
}
UPB_INLINE void _upb_sethas(const upb_Message* msg, size_t idx) {
(*UPB_PTR_AT(msg, _upb_hasbit_ofs(idx), char)) |= _upb_hasbit_mask(idx);
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetHasbit)(
const struct upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
(*UPB_PTR_AT(msg, offset, char)) |= mask;
}
UPB_INLINE void _upb_clearhas(const upb_Message* msg, size_t idx) {
(*UPB_PTR_AT(msg, _upb_hasbit_ofs(idx), char)) &= ~_upb_hasbit_mask(idx);
}
UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearHasbit)(
const struct upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
UPB_INLINE size_t _upb_Message_Hasidx(const upb_MiniTableField* f) {
UPB_ASSERT(f->presence > 0);
return f->presence;
}
UPB_INLINE bool _upb_hasbit_field(const upb_Message* msg,
const upb_MiniTableField* f) {
return _upb_hasbit(msg, _upb_Message_Hasidx(f));
}
UPB_INLINE void _upb_sethas_field(const upb_Message* msg,
const upb_MiniTableField* f) {
_upb_sethas(msg, _upb_Message_Hasidx(f));
(*UPB_PTR_AT(msg, offset, char)) &= ~mask;
}
// Oneof case access ///////////////////////////////////////////////////////////
UPB_INLINE size_t _upb_oneofcase_ofs(const upb_MiniTableField* f) {
UPB_ASSERT(f->presence < 0);
return ~(ptrdiff_t)f->presence;
UPB_INLINE uint32_t* UPB_PRIVATE(_upb_Message_OneofCasePtr)(
struct upb_Message* msg, const upb_MiniTableField* f) {
return UPB_PTR_AT(msg, UPB_PRIVATE(_upb_MiniTableField_OneofOffset)(f),
uint32_t);
}
UPB_INLINE uint32_t* _upb_oneofcase_field(upb_Message* msg,
const upb_MiniTableField* f) {
return UPB_PTR_AT(msg, _upb_oneofcase_ofs(f), uint32_t);
UPB_INLINE uint32_t UPB_PRIVATE(_upb_Message_GetOneofCase)(
const struct upb_Message* msg, const upb_MiniTableField* f) {
const uint32_t* ptr =
UPB_PRIVATE(_upb_Message_OneofCasePtr)((struct upb_Message*)msg, f);
return *ptr;
}
UPB_INLINE uint32_t _upb_getoneofcase_field(const upb_Message* msg,
const upb_MiniTableField* f) {
return *_upb_oneofcase_field((upb_Message*)msg, f);
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetOneofCase)(
struct upb_Message* msg, const upb_MiniTableField* f) {
uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f);
*ptr = upb_MiniTableField_Number(f);
}
// Returns true if the given field is the current oneof case.
// Does nothing if it is not the current oneof case.
UPB_INLINE bool UPB_PRIVATE(_upb_Message_ClearOneofCase)(
struct upb_Message* msg, const upb_MiniTableField* f) {
uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f);
if (*ptr != upb_MiniTableField_Number(f)) return false;
*ptr = 0;
return true;
}
UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber(
const struct upb_Message* message, const upb_MiniTableField* oneof_field) {
UPB_ASSUME(upb_MiniTableField_IsInOneof(oneof_field));
return UPB_PRIVATE(_upb_Message_GetOneofCase)(message, oneof_field);
}
UPB_API_INLINE const upb_MiniTableField* upb_Message_WhichOneof(
const struct upb_Message* msg, const upb_MiniTable* m,
const upb_MiniTableField* f) {
uint32_t field_number = upb_Message_WhichOneofFieldNumber(msg, f);
if (field_number == 0) {
// No field in the oneof is set.
return NULL;
}
return upb_MiniTable_FindFieldByNumber(m, field_number);
}
// LINT.ThenChange(GoogleInternalName2)
UPB_INLINE bool _upb_MiniTableField_InOneOf(const upb_MiniTableField* field) {
return field->presence < 0;
// Returns false if the message is missing any of its required fields.
UPB_INLINE bool UPB_PRIVATE(_upb_Message_IsInitializedShallow)(
const struct upb_Message* msg, const upb_MiniTable* m) {
uint64_t bits;
memcpy(&bits, msg + 1, sizeof(bits));
bits = upb_BigEndian64(bits);
return (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~bits) == 0;
}
UPB_INLINE void* _upb_MiniTableField_GetPtr(upb_Message* msg,
const upb_MiniTableField* field) {
return (char*)msg + field->offset;
UPB_INLINE void* UPB_PRIVATE(_upb_Message_MutableDataPtr)(
struct upb_Message* msg, const upb_MiniTableField* f) {
return (char*)msg + f->UPB_ONLYBITS(offset);
}
UPB_INLINE const void* _upb_MiniTableField_GetConstPtr(
const upb_Message* msg, const upb_MiniTableField* field) {
return (char*)msg + field->offset;
UPB_INLINE const void* UPB_PRIVATE(_upb_Message_DataPtr)(
const struct upb_Message* msg, const upb_MiniTableField* f) {
return (const char*)msg + f->UPB_ONLYBITS(offset);
}
UPB_INLINE void _upb_Message_SetPresence(upb_Message* msg,
const upb_MiniTableField* field) {
if (field->presence > 0) {
_upb_sethas_field(msg, field);
} else if (_upb_MiniTableField_InOneOf(field)) {
*_upb_oneofcase_field(msg, field) = field->number;
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetPresence)(
struct upb_Message* msg, const upb_MiniTableField* f) {
if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
UPB_PRIVATE(_upb_Message_SetHasbit)(msg, f);
} else if (upb_MiniTableField_IsInOneof(f)) {
UPB_PRIVATE(_upb_Message_SetOneofCase)(msg, f);
}
}
UPB_INLINE bool _upb_MiniTable_ValueIsNonZero(const void* default_val,
const upb_MiniTableField* field) {
char zero[16] = {0};
switch (_upb_MiniTableField_GetRep(field)) {
case kUpb_FieldRep_1Byte:
return memcmp(&zero, default_val, 1) != 0;
case kUpb_FieldRep_4Byte:
return memcmp(&zero, default_val, 4) != 0;
case kUpb_FieldRep_8Byte:
return memcmp(&zero, default_val, 8) != 0;
case kUpb_FieldRep_StringView: {
const upb_StringView* sv = (const upb_StringView*)default_val;
return sv->size != 0;
}
}
UPB_UNREACHABLE();
}
UPB_INLINE void _upb_MiniTable_CopyFieldData(void* to, const void* from,
const upb_MiniTableField* field) {
switch (_upb_MiniTableField_GetRep(field)) {
UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_DataCopy)(
const upb_MiniTableField* f, void* to, const void* from) {
switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
case kUpb_FieldRep_1Byte:
memcpy(to, from, 1);
return;
@@ -154,30 +183,34 @@ UPB_INLINE void _upb_MiniTable_CopyFieldData(void* to, const void* from,
UPB_UNREACHABLE();
}
UPB_INLINE size_t
_upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) {
const unsigned char table[] = {
0,
3, // kUpb_FieldType_Double = 1,
2, // kUpb_FieldType_Float = 2,
3, // kUpb_FieldType_Int64 = 3,
3, // kUpb_FieldType_UInt64 = 4,
2, // kUpb_FieldType_Int32 = 5,
3, // kUpb_FieldType_Fixed64 = 6,
2, // kUpb_FieldType_Fixed32 = 7,
0, // kUpb_FieldType_Bool = 8,
UPB_SIZE(3, 4), // kUpb_FieldType_String = 9,
UPB_SIZE(2, 3), // kUpb_FieldType_Group = 10,
UPB_SIZE(2, 3), // kUpb_FieldType_Message = 11,
UPB_SIZE(3, 4), // kUpb_FieldType_Bytes = 12,
2, // kUpb_FieldType_UInt32 = 13,
2, // kUpb_FieldType_Enum = 14,
2, // kUpb_FieldType_SFixed32 = 15,
3, // kUpb_FieldType_SFixed64 = 16,
2, // kUpb_FieldType_SInt32 = 17,
3, // kUpb_FieldType_SInt64 = 18,
};
return table[field->UPB_PRIVATE(descriptortype)];
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_DataEquals)(
const upb_MiniTableField* f, const void* a, const void* b) {
switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
case kUpb_FieldRep_1Byte:
return memcmp(a, b, 1) == 0;
case kUpb_FieldRep_4Byte:
return memcmp(a, b, 4) == 0;
case kUpb_FieldRep_8Byte:
return memcmp(a, b, 8) == 0;
case kUpb_FieldRep_StringView: {
const upb_StringView sa = *(const upb_StringView*)a;
const upb_StringView sb = *(const upb_StringView*)b;
return upb_StringView_IsEqual(sa, sb);
}
}
UPB_UNREACHABLE();
}
UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_DataClear)(
const upb_MiniTableField* f, void* val) {
const char zero[16] = {0};
UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, zero);
}
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(
const upb_MiniTableField* f, const void* val) {
const char zero[16] = {0};
return UPB_PRIVATE(_upb_MiniTableField_DataEquals)(f, val, zero);
}
// Here we define universal getter/setter functions for message fields.
@@ -190,7 +223,7 @@ _upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) {
// bool FooMessage_set_bool_field(const upb_Message* msg, bool val) {
// const upb_MiniTableField field = {1, 0, 0, /* etc... */};
// // All value in "field" are compile-time known.
// _upb_Message_SetNonExtensionField(msg, &field, &value);
// upb_Message_SetBaseField(msg, &field, &value);
// }
//
// // Via UPB_ASSUME().
@@ -198,9 +231,9 @@ _upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) {
// const upb_MiniTableField* field,
// bool value, upb_Arena* a) {
// UPB_ASSUME(field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bool);
// UPB_ASSUME(!upb_IsRepeatedOrMap(field));
// UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte);
// _upb_Message_SetField(msg, field, &value, a);
// UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
// kUpb_FieldRep_1Byte);
// upb_Message_SetField(msg, field, &value, a);
// }
//
// As a result, we can use these universal getters/setters for *all* message
@@ -212,148 +245,677 @@ _upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) {
// of a setter is known to be a non-extension, the arena may be NULL and the
// returned bool value may be ignored since it will always succeed.
UPB_INLINE bool _upb_Message_HasExtensionField(
const upb_Message* msg, const upb_MiniTableExtension* ext) {
UPB_ASSERT(upb_MiniTableField_HasPresence(&ext->field));
return _upb_Message_Getext(msg, ext) != NULL;
}
UPB_INLINE bool _upb_Message_HasNonExtensionField(
const upb_Message* msg, const upb_MiniTableField* field) {
UPB_API_INLINE bool upb_Message_HasBaseField(const struct upb_Message* msg,
const upb_MiniTableField* field) {
UPB_ASSERT(upb_MiniTableField_HasPresence(field));
UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
if (_upb_MiniTableField_InOneOf(field)) {
return _upb_getoneofcase_field(msg, field) == field->number;
if (upb_MiniTableField_IsInOneof(field)) {
return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, field) ==
upb_MiniTableField_Number(field);
} else {
return _upb_hasbit_field(msg, field);
return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, field);
}
}
static UPB_FORCEINLINE void _upb_Message_GetNonExtensionField(
const upb_Message* msg, const upb_MiniTableField* field,
UPB_API_INLINE bool upb_Message_HasExtension(const struct upb_Message* msg,
const upb_MiniTableExtension* e) {
UPB_ASSERT(upb_MiniTableField_HasPresence(&e->UPB_PRIVATE(field)));
return UPB_PRIVATE(_upb_Message_Getext)(msg, e) != NULL;
}
UPB_FORCEINLINE void _upb_Message_GetNonExtensionField(
const struct upb_Message* msg, const upb_MiniTableField* field,
const void* default_val, void* val) {
UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
if ((_upb_MiniTableField_InOneOf(field) ||
_upb_MiniTable_ValueIsNonZero(default_val, field)) &&
!_upb_Message_HasNonExtensionField(msg, field)) {
_upb_MiniTable_CopyFieldData(val, default_val, field);
if ((upb_MiniTableField_IsInOneof(field) ||
!UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(field, default_val)) &&
!upb_Message_HasBaseField(msg, field)) {
UPB_PRIVATE(_upb_MiniTableField_DataCopy)(field, val, default_val);
return;
}
_upb_MiniTable_CopyFieldData(val, _upb_MiniTableField_GetConstPtr(msg, field),
field);
UPB_PRIVATE(_upb_MiniTableField_DataCopy)
(field, val, UPB_PRIVATE(_upb_Message_DataPtr)(msg, field));
}
UPB_INLINE void _upb_Message_GetExtensionField(
const upb_Message* msg, const upb_MiniTableExtension* mt_ext,
const struct upb_Message* msg, const upb_MiniTableExtension* mt_ext,
const void* default_val, void* val) {
UPB_ASSUME(upb_MiniTableField_IsExtension(&mt_ext->field));
const upb_Message_Extension* ext = _upb_Message_Getext(msg, mt_ext);
const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, mt_ext);
const upb_MiniTableField* f = &mt_ext->UPB_PRIVATE(field);
UPB_ASSUME(upb_MiniTableField_IsExtension(f));
if (ext) {
_upb_MiniTable_CopyFieldData(val, &ext->data, &mt_ext->field);
UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, &ext->data);
} else {
_upb_MiniTable_CopyFieldData(val, default_val, &mt_ext->field);
UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, default_val);
}
}
UPB_INLINE void _upb_Message_GetField(const upb_Message* msg,
const upb_MiniTableField* field,
const void* default_val, void* val) {
// NOTE: The default_val is only used for fields that support presence.
// For repeated/map fields, the resulting upb_Array*/upb_Map* can be NULL if a
// upb_Array/upb_Map has not been allocated yet. Array/map fields do not have
// presence, so this is semantically identical to a pointer to an empty
// array/map, and must be treated the same for all semantic purposes.
UPB_API_INLINE upb_MessageValue upb_Message_GetField(
const struct upb_Message* msg, const upb_MiniTableField* field,
upb_MessageValue default_val) {
upb_MessageValue ret;
if (upb_MiniTableField_IsExtension(field)) {
_upb_Message_GetExtensionField(msg, (upb_MiniTableExtension*)field,
default_val, val);
&default_val, &ret);
} else {
_upb_Message_GetNonExtensionField(msg, field, default_val, val);
_upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
}
return ret;
}
UPB_INLINE void _upb_Message_SetNonExtensionField(
upb_Message* msg, const upb_MiniTableField* field, const void* val) {
UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
_upb_Message_SetPresence(msg, field);
_upb_MiniTable_CopyFieldData(_upb_MiniTableField_GetPtr(msg, field), val,
field);
UPB_API_INLINE void upb_Message_SetBaseField(struct upb_Message* msg,
const upb_MiniTableField* f,
const void* val) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
UPB_ASSUME(!upb_MiniTableField_IsExtension(f));
UPB_PRIVATE(_upb_Message_SetPresence)(msg, f);
UPB_PRIVATE(_upb_MiniTableField_DataCopy)
(f, UPB_PRIVATE(_upb_Message_MutableDataPtr)(msg, f), val);
}
UPB_INLINE bool _upb_Message_SetExtensionField(
upb_Message* msg, const upb_MiniTableExtension* mt_ext, const void* val,
upb_Arena* a) {
UPB_API_INLINE bool upb_Message_SetExtension(struct upb_Message* msg,
const upb_MiniTableExtension* e,
const void* val, upb_Arena* a) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
UPB_ASSERT(a);
upb_Message_Extension* ext =
_upb_Message_GetOrCreateExtension(msg, mt_ext, a);
upb_Extension* ext =
UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(msg, e, a);
if (!ext) return false;
_upb_MiniTable_CopyFieldData(&ext->data, val, &mt_ext->field);
UPB_PRIVATE(_upb_MiniTableField_DataCopy)
(&e->UPB_PRIVATE(field), &ext->data, val);
return true;
}
UPB_INLINE bool _upb_Message_SetField(upb_Message* msg,
const upb_MiniTableField* field,
const void* val, upb_Arena* a) {
if (upb_MiniTableField_IsExtension(field)) {
const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)field;
return _upb_Message_SetExtensionField(msg, ext, val, a);
// Sets the value of the given field in the given msg. The return value is true
// if the operation completed successfully, or false if memory allocation
// failed.
UPB_INLINE bool UPB_PRIVATE(_upb_Message_SetField)(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_MessageValue val,
upb_Arena* a) {
if (upb_MiniTableField_IsExtension(f)) {
const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)f;
return upb_Message_SetExtension(msg, ext, &val, a);
} else {
_upb_Message_SetNonExtensionField(msg, field, val);
upb_Message_SetBaseField(msg, f, &val);
return true;
}
}
UPB_INLINE void _upb_Message_ClearExtensionField(
upb_Message* msg, const upb_MiniTableExtension* ext_l) {
upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (!in->internal) return;
const upb_Message_Extension* base =
UPB_PTR_AT(in->internal, in->internal->ext_begin, upb_Message_Extension);
upb_Message_Extension* ext =
(upb_Message_Extension*)_upb_Message_Getext(msg, ext_l);
if (ext) {
*ext = *base;
in->internal->ext_begin += sizeof(upb_Message_Extension);
}
UPB_API_INLINE const upb_Array* upb_Message_GetArray(
const struct upb_Message* msg, const upb_MiniTableField* f) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_Array* ret;
const upb_Array* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, f, &default_val, &ret);
return ret;
}
UPB_INLINE void _upb_Message_ClearNonExtensionField(
upb_Message* msg, const upb_MiniTableField* field) {
if (field->presence > 0) {
_upb_clearhas(msg, _upb_Message_Hasidx(field));
} else if (_upb_MiniTableField_InOneOf(field)) {
uint32_t* oneof_case = _upb_oneofcase_field(msg, field);
if (*oneof_case != field->number) return;
*oneof_case = 0;
}
const char zeros[16] = {0};
_upb_MiniTable_CopyFieldData(_upb_MiniTableField_GetPtr(msg, field), zeros,
field);
UPB_API_INLINE bool upb_Message_GetBool(const struct upb_Message* msg,
const upb_MiniTableField* f,
bool default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
upb_MessageValue def;
def.bool_val = default_val;
return upb_Message_GetField(msg, f, def).bool_val;
}
UPB_INLINE void _upb_Message_AssertMapIsUntagged(
const upb_Message* msg, const upb_MiniTableField* field) {
UPB_API_INLINE double upb_Message_GetDouble(const struct upb_Message* msg,
const upb_MiniTableField* f,
double default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_MessageValue def;
def.double_val = default_val;
return upb_Message_GetField(msg, f, def).double_val;
}
UPB_API_INLINE float upb_Message_GetFloat(const struct upb_Message* msg,
const upb_MiniTableField* f,
float default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_MessageValue def;
def.float_val = default_val;
return upb_Message_GetField(msg, f, def).float_val;
}
UPB_API_INLINE int32_t upb_Message_GetInt32(const struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(f) == kUpb_CType_Enum);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_MessageValue def;
def.int32_val = default_val;
return upb_Message_GetField(msg, f, def).int32_val;
}
UPB_API_INLINE int64_t upb_Message_GetInt64(const struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_MessageValue def;
def.int64_val = default_val;
return upb_Message_GetField(msg, f, def).int64_val;
}
UPB_INLINE void UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(
const struct upb_Message* msg, const upb_MiniTableField* field) {
UPB_UNUSED(msg);
_upb_MiniTableField_CheckIsMap(field);
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
#ifndef NDEBUG
upb_TaggedMessagePtr default_val = 0;
upb_TaggedMessagePtr tagged;
uintptr_t default_val = 0;
uintptr_t tagged;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &tagged);
UPB_ASSERT(!upb_TaggedMessagePtr_IsEmpty(tagged));
#endif
}
UPB_INLINE upb_Map* _upb_Message_GetOrCreateMutableMap(
upb_Message* msg, const upb_MiniTableField* field, size_t key_size,
UPB_API_INLINE const struct upb_Map* upb_Message_GetMap(
const struct upb_Message* msg, const upb_MiniTableField* f) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(f);
UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(msg, f);
struct upb_Map* ret;
const struct upb_Map* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, f, &default_val, &ret);
return ret;
}
UPB_API_INLINE uintptr_t upb_Message_GetTaggedMessagePtr(
const struct upb_Message* msg, const upb_MiniTableField* f,
struct upb_Message* default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
uintptr_t tagged;
_upb_Message_GetNonExtensionField(msg, f, &default_val, &tagged);
return tagged;
}
// For internal use only; users cannot set tagged messages because only the
// parser and the message copier are allowed to directly create an empty
// message.
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)(
struct upb_Message* msg, const upb_MiniTableField* f,
uintptr_t sub_message) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_Message_SetBaseField(msg, f, &sub_message);
}
UPB_API_INLINE const struct upb_Message* upb_Message_GetMessage(
const struct upb_Message* msg, const upb_MiniTableField* f) {
uintptr_t tagged = upb_Message_GetTaggedMessagePtr(msg, f, NULL);
return upb_TaggedMessagePtr_GetNonEmptyMessage(tagged);
}
UPB_API_INLINE upb_Array* upb_Message_GetMutableArray(
struct upb_Message* msg, const upb_MiniTableField* f) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
return (upb_Array*)upb_Message_GetArray(msg, f);
}
UPB_API_INLINE struct upb_Map* upb_Message_GetMutableMap(
struct upb_Message* msg, const upb_MiniTableField* f) {
return (struct upb_Map*)upb_Message_GetMap(msg, f);
}
UPB_API_INLINE struct upb_Message* upb_Message_GetMutableMessage(
struct upb_Message* msg, const upb_MiniTableField* f) {
return (struct upb_Message*)upb_Message_GetMessage(msg, f);
}
UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray(
struct upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_Array* array = upb_Message_GetMutableArray(msg, f);
if (!array) {
array = UPB_PRIVATE(_upb_Array_New)(
arena, 4, UPB_PRIVATE(_upb_MiniTableField_ElemSizeLg2)(f));
// Check again due to: https://godbolt.org/z/7WfaoKG1r
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_MessageValue val;
val.array_val = array;
UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, arena);
}
return array;
}
UPB_INLINE struct upb_Map* _upb_Message_GetOrCreateMutableMap(
struct upb_Message* msg, const upb_MiniTableField* field, size_t key_size,
size_t val_size, upb_Arena* arena) {
_upb_MiniTableField_CheckIsMap(field);
_upb_Message_AssertMapIsUntagged(msg, field);
upb_Map* map = NULL;
upb_Map* default_map_value = NULL;
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(msg, field);
struct upb_Map* map = NULL;
struct upb_Map* default_map_value = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_map_value, &map);
if (!map) {
map = _upb_Map_New(arena, key_size, val_size);
// Check again due to: https://godbolt.org/z/7WfaoKG1r
_upb_MiniTableField_CheckIsMap(field);
_upb_Message_SetNonExtensionField(msg, field, &map);
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
upb_Message_SetBaseField(msg, field, &map);
}
return map;
}
UPB_API_INLINE struct upb_Map* upb_Message_GetOrCreateMutableMap(
struct upb_Message* msg, const upb_MiniTable* map_entry_mini_table,
const upb_MiniTableField* f, upb_Arena* arena) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
const upb_MiniTableField* map_entry_key_field =
&map_entry_mini_table->UPB_ONLYBITS(fields)[0];
const upb_MiniTableField* map_entry_value_field =
&map_entry_mini_table->UPB_ONLYBITS(fields)[1];
return _upb_Message_GetOrCreateMutableMap(
msg, f, _upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_key_field)),
_upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_value_field)),
arena);
}
UPB_API_INLINE struct upb_Message* upb_Message_GetOrCreateMutableMessage(
struct upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* f, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
UPB_ASSUME(!upb_MiniTableField_IsExtension(f));
struct upb_Message* sub_message =
*UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*);
if (!sub_message) {
const upb_MiniTable* sub_mini_table =
upb_MiniTable_SubMessage(mini_table, f);
UPB_ASSERT(sub_mini_table);
sub_message = _upb_Message_New(sub_mini_table, arena);
*UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*) =
sub_message;
UPB_PRIVATE(_upb_Message_SetPresence)(msg, f);
}
return sub_message;
}
UPB_API_INLINE upb_StringView
upb_Message_GetString(const struct upb_Message* msg,
const upb_MiniTableField* f, upb_StringView default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_StringView);
upb_MessageValue def;
def.str_val = default_val;
return upb_Message_GetField(msg, f, def).str_val;
}
UPB_API_INLINE uint32_t upb_Message_GetUInt32(const struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_MessageValue def;
def.uint32_val = default_val;
return upb_Message_GetField(msg, f, def).uint32_val;
}
UPB_API_INLINE uint64_t upb_Message_GetUInt64(const struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_MessageValue def;
def.uint64_val = default_val;
return upb_Message_GetField(msg, f, def).uint64_val;
}
// BaseField Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE void upb_Message_SetBaseFieldBool(struct upb_Message* msg,
const upb_MiniTableField* f,
bool value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldDouble(struct upb_Message* msg,
const upb_MiniTableField* f,
double value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldFloat(struct upb_Message* msg,
const upb_MiniTableField* f,
float value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(f) == kUpb_CType_Enum);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldMessage(struct upb_Message* msg,
const upb_MiniTableField* f,
struct upb_Message* value) {
UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)
(msg, f, UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(value, false));
}
UPB_API_INLINE void upb_Message_SetBaseFieldString(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_StringView);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldUInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldUInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetClosedEnum(struct upb_Message* msg,
const upb_MiniTable* m,
const upb_MiniTableField* f,
int32_t value) {
UPB_ASSERT(upb_MiniTableField_IsClosedEnum(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSERT(
upb_MiniTableEnum_CheckValue(upb_MiniTable_GetSubEnumTable(m, f), value));
upb_Message_SetBaseField(msg, f, &value);
}
// Extension Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE bool upb_Message_SetExtensionBool(
struct upb_Message* msg, const upb_MiniTableExtension* e, bool value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Bool);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_1Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionDouble(
struct upb_Message* msg, const upb_MiniTableExtension* e, double value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Double);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_8Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionFloat(
struct upb_Message* msg, const upb_MiniTableExtension* e, float value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Float);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_4Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionInt32(
struct upb_Message* msg, const upb_MiniTableExtension* e, int32_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int32 ||
upb_MiniTableExtension_CType(e) == kUpb_CType_Enum);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_4Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionInt64(
struct upb_Message* msg, const upb_MiniTableExtension* e, int64_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_8Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionString(
struct upb_Message* msg, const upb_MiniTableExtension* e,
upb_StringView value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_String ||
upb_MiniTableExtension_CType(e) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_StringView);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionUInt32(
struct upb_Message* msg, const upb_MiniTableExtension* e, uint32_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt32);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_4Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionUInt64(
struct upb_Message* msg, const upb_MiniTableExtension* e, uint64_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_8Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
// Universal Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE bool upb_Message_SetBool(struct upb_Message* msg,
const upb_MiniTableField* f, bool value,
upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionBool(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldBool(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetDouble(struct upb_Message* msg,
const upb_MiniTableField* f,
double value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionDouble(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldDouble(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetFloat(struct upb_Message* msg,
const upb_MiniTableField* f,
float value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionFloat(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldFloat(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionInt32(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldInt32(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionInt64(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldInt64(msg, f, value), true);
}
// Sets the value of a message-typed field. The mini_tables of `msg` and
// `value` must have been linked for this to work correctly.
UPB_API_INLINE void upb_Message_SetMessage(struct upb_Message* msg,
const upb_MiniTableField* f,
struct upb_Message* value) {
UPB_ASSERT(!upb_MiniTableField_IsExtension(f));
upb_Message_SetBaseFieldMessage(msg, f, value);
}
// Sets the value of a `string` or `bytes` field. The bytes of the value are not
// copied, so it is the caller's responsibility to ensure that they remain valid
// for the lifetime of `msg`. That might be done by copying them into the given
// arena, or by fusing that arena with the arena the bytes live in, for example.
UPB_API_INLINE bool upb_Message_SetString(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionString(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldString(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetUInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionUInt32(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldUInt32(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetUInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionUInt64(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldUInt64(msg, f, value), true);
}
UPB_API_INLINE void upb_Message_Clear(struct upb_Message* msg,
const upb_MiniTable* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
memset(msg, 0, m->UPB_PRIVATE(size));
if (in) {
// Reset the internal buffer to empty.
in->unknown_end = sizeof(upb_Message_Internal);
in->ext_begin = in->size;
UPB_PRIVATE(_upb_Message_SetInternal)(msg, in);
}
}
UPB_API_INLINE void upb_Message_ClearBaseField(struct upb_Message* msg,
const upb_MiniTableField* f) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
UPB_PRIVATE(_upb_Message_ClearHasbit)(msg, f);
} else if (upb_MiniTableField_IsInOneof(f)) {
uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f);
if (*ptr != upb_MiniTableField_Number(f)) return;
*ptr = 0;
}
const char zeros[16] = {0};
UPB_PRIVATE(_upb_MiniTableField_DataCopy)
(f, UPB_PRIVATE(_upb_Message_MutableDataPtr)(msg, f), zeros);
}
UPB_API_INLINE void upb_Message_ClearExtension(
struct upb_Message* msg, const upb_MiniTableExtension* e) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (!in) return;
const upb_Extension* base = UPB_PTR_AT(in, in->ext_begin, upb_Extension);
upb_Extension* ext = (upb_Extension*)UPB_PRIVATE(_upb_Message_Getext)(msg, e);
if (ext) {
*ext = *base;
in->ext_begin += sizeof(upb_Extension);
}
}
UPB_API_INLINE void upb_Message_ClearOneof(struct upb_Message* msg,
const upb_MiniTable* m,
const upb_MiniTableField* f) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
uint32_t field_number = upb_Message_WhichOneofFieldNumber(msg, f);
if (field_number == 0) {
// No field in the oneof is set.
return;
}
const upb_MiniTableField* field =
upb_MiniTable_FindFieldByNumber(m, field_number);
upb_Message_ClearBaseField(msg, field);
}
UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized(
struct upb_Message* msg, const upb_MiniTableField* f, size_t size,
upb_Arena* arena) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, f, arena);
if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, size, arena)) {
return NULL;
}
return upb_Array_MutableDataPtr(arr);
}
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -1,138 +1,145 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_INTERNAL_ARRAY_H_
#define UPB_MESSAGE_INTERNAL_ARRAY_H_
#include <stdint.h>
#include <string.h>
#include "upb/message/array.h"
#include "upb/mem/arena.h"
// Must be last.
#include "upb/port/def.inc"
#define _UPB_ARRAY_MASK_IMM 0x4 // Frozen/immutable bit.
#define _UPB_ARRAY_MASK_LG2 0x3 // Encoded elem size.
#define _UPB_ARRAY_MASK_ALL (_UPB_ARRAY_MASK_IMM | _UPB_ARRAY_MASK_LG2)
#ifdef __cplusplus
extern "C" {
#endif
// LINT.IfChange(struct_definition)
// LINT.IfChange(upb_Array)
// Our internal representation for repeated fields.
struct upb_Array {
uintptr_t data; /* Tagged ptr: low 3 bits of ptr are lg2(elem size). */
size_t size; /* The number of elements in the array. */
size_t capacity; /* Allocated storage. Measured in elements. */
// This is a tagged pointer. Bits #0 and #1 encode the elem size as follows:
// 0 maps to elem size 1
// 1 maps to elem size 4
// 2 maps to elem size 8
// 3 maps to elem size 16
//
// Bit #2 contains the frozen/immutable flag.
uintptr_t UPB_ONLYBITS(data);
size_t UPB_ONLYBITS(size); // The number of elements in the array.
size_t UPB_PRIVATE(capacity); // Allocated storage. Measured in elements.
};
// LINT.ThenChange(GoogleInternalName1)
UPB_INLINE size_t _upb_Array_ElementSizeLg2(const upb_Array* arr) {
size_t ret = arr->data & 7;
UPB_ASSERT(ret <= 4);
return ret;
UPB_INLINE void UPB_PRIVATE(_upb_Array_ShallowFreeze)(struct upb_Array* arr) {
arr->UPB_ONLYBITS(data) |= _UPB_ARRAY_MASK_IMM;
}
UPB_INLINE const void* _upb_array_constptr(const upb_Array* arr) {
_upb_Array_ElementSizeLg2(arr); // Check assertion.
return (void*)(arr->data & ~(uintptr_t)7);
UPB_API_INLINE bool upb_Array_IsFrozen(const struct upb_Array* arr) {
return (arr->UPB_ONLYBITS(data) & _UPB_ARRAY_MASK_IMM) != 0;
}
UPB_INLINE uintptr_t _upb_array_tagptr(void* ptr, int elem_size_lg2) {
UPB_INLINE void UPB_PRIVATE(_upb_Array_SetTaggedPtr)(struct upb_Array* array,
void* data, size_t lg2) {
UPB_ASSERT(lg2 != 1);
UPB_ASSERT(lg2 <= 4);
const size_t bits = lg2 - (lg2 != 0);
array->UPB_ONLYBITS(data) = (uintptr_t)data | bits;
}
UPB_INLINE size_t
UPB_PRIVATE(_upb_Array_ElemSizeLg2)(const struct upb_Array* array) {
const size_t bits = array->UPB_ONLYBITS(data) & _UPB_ARRAY_MASK_LG2;
const size_t lg2 = bits + (bits != 0);
return lg2;
}
UPB_API_INLINE const void* upb_Array_DataPtr(const struct upb_Array* array) {
UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array); // Check assertions.
return (void*)(array->UPB_ONLYBITS(data) & ~(uintptr_t)_UPB_ARRAY_MASK_ALL);
}
UPB_API_INLINE void* upb_Array_MutableDataPtr(struct upb_Array* array) {
return (void*)upb_Array_DataPtr(array);
}
UPB_INLINE struct upb_Array* UPB_PRIVATE(_upb_Array_New)(upb_Arena* arena,
size_t init_capacity,
int elem_size_lg2) {
UPB_ASSERT(elem_size_lg2 != 1);
UPB_ASSERT(elem_size_lg2 <= 4);
return (uintptr_t)ptr | elem_size_lg2;
}
UPB_INLINE void* _upb_array_ptr(upb_Array* arr) {
return (void*)_upb_array_constptr(arr);
}
UPB_INLINE uintptr_t _upb_tag_arrptr(void* ptr, int elem_size_lg2) {
UPB_ASSERT(elem_size_lg2 <= 4);
UPB_ASSERT(((uintptr_t)ptr & 7) == 0);
return (uintptr_t)ptr | (unsigned)elem_size_lg2;
}
extern const char _upb_Array_CTypeSizeLg2Table[];
UPB_INLINE size_t _upb_Array_CTypeSizeLg2(upb_CType ctype) {
return _upb_Array_CTypeSizeLg2Table[ctype];
}
UPB_INLINE upb_Array* _upb_Array_New(upb_Arena* a, size_t init_capacity,
int elem_size_lg2) {
UPB_ASSERT(elem_size_lg2 <= 4);
const size_t arr_size = UPB_ALIGN_UP(sizeof(upb_Array), UPB_MALLOC_ALIGN);
const size_t bytes = arr_size + (init_capacity << elem_size_lg2);
upb_Array* arr = (upb_Array*)upb_Arena_Malloc(a, bytes);
if (!arr) return NULL;
arr->data = _upb_tag_arrptr(UPB_PTR_AT(arr, arr_size, void), elem_size_lg2);
arr->size = 0;
arr->capacity = init_capacity;
return arr;
const size_t array_size =
UPB_ALIGN_UP(sizeof(struct upb_Array), UPB_MALLOC_ALIGN);
const size_t bytes = array_size + (init_capacity << elem_size_lg2);
struct upb_Array* array = (struct upb_Array*)upb_Arena_Malloc(arena, bytes);
if (!array) return NULL;
UPB_PRIVATE(_upb_Array_SetTaggedPtr)
(array, UPB_PTR_AT(array, array_size, void), elem_size_lg2);
array->UPB_ONLYBITS(size) = 0;
array->UPB_PRIVATE(capacity) = init_capacity;
return array;
}
// 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);
bool UPB_PRIVATE(_upb_Array_Realloc)(struct upb_Array* array, size_t min_size,
upb_Arena* arena);
UPB_INLINE bool _upb_array_reserve(upb_Array* arr, size_t size,
upb_Arena* arena) {
if (arr->capacity < size) return _upb_array_realloc(arr, size, arena);
UPB_API_INLINE bool upb_Array_Reserve(struct upb_Array* array, size_t size,
upb_Arena* arena) {
UPB_ASSERT(!upb_Array_IsFrozen(array));
if (array->UPB_PRIVATE(capacity) < size)
return UPB_PRIVATE(_upb_Array_Realloc)(array, size, arena);
return true;
}
// Resize without initializing new elements.
UPB_INLINE bool _upb_Array_ResizeUninitialized(upb_Array* arr, size_t size,
upb_Arena* arena) {
UPB_ASSERT(size <= arr->size || arena); // Allow NULL arena when shrinking.
if (!_upb_array_reserve(arr, size, arena)) return false;
arr->size = size;
UPB_INLINE bool UPB_PRIVATE(_upb_Array_ResizeUninitialized)(
struct upb_Array* array, size_t size, upb_Arena* arena) {
UPB_ASSERT(!upb_Array_IsFrozen(array));
UPB_ASSERT(size <= array->UPB_ONLYBITS(size) ||
arena); // Allow NULL arena when shrinking.
if (!upb_Array_Reserve(array, size, arena)) return false;
array->UPB_ONLYBITS(size) = size;
return true;
}
// This function is intended for situations where elem_size is compile-time
// constant or a known expression of the form (1 << lg2), so that the expression
// i*elem_size does not result in an actual multiplication.
UPB_INLINE void _upb_Array_Set(upb_Array* arr, size_t i, const void* data,
size_t elem_size) {
UPB_ASSERT(i < arr->size);
UPB_ASSERT(elem_size == 1U << _upb_Array_ElementSizeLg2(arr));
char* arr_data = (char*)_upb_array_ptr(arr);
UPB_INLINE void UPB_PRIVATE(_upb_Array_Set)(struct upb_Array* array, size_t i,
const void* data,
size_t elem_size) {
UPB_ASSERT(!upb_Array_IsFrozen(array));
UPB_ASSERT(i < array->UPB_ONLYBITS(size));
UPB_ASSERT(elem_size == 1U << UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array));
char* arr_data = (char*)upb_Array_MutableDataPtr(array);
memcpy(arr_data + (i * elem_size), data, elem_size);
}
UPB_INLINE void _upb_array_detach(const void* msg, size_t ofs) {
*UPB_PTR_AT(msg, ofs, upb_Array*) = NULL;
UPB_API_INLINE size_t upb_Array_Size(const struct upb_Array* arr) {
return arr->UPB_ONLYBITS(size);
}
// LINT.ThenChange(GoogleInternalName0)
#ifdef __cplusplus
} /* extern "C" */
#endif
#undef _UPB_ARRAY_MASK_IMM
#undef _UPB_ARRAY_MASK_LG2
#undef _UPB_ARRAY_MASK_ALL
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_INTERNAL_ARRAY_H_ */

View File

@@ -0,0 +1,289 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/message/internal/compare_unknown.h"
#include <stdlib.h>
#include "upb/base/string_view.h"
#include "upb/mem/alloc.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/reader.h"
#include "upb/wire/types.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_UnknownFields upb_UnknownFields;
typedef struct {
uint32_t tag;
union {
uint64_t varint;
uint64_t uint64;
uint32_t uint32;
upb_StringView delimited;
upb_UnknownFields* group;
} data;
} upb_UnknownField;
struct upb_UnknownFields {
size_t size;
size_t capacity;
upb_UnknownField* fields;
};
typedef struct {
upb_EpsCopyInputStream stream;
upb_Arena* arena;
upb_UnknownField* tmp;
size_t tmp_size;
int depth;
upb_UnknownCompareResult status;
jmp_buf err;
} upb_UnknownField_Context;
UPB_NORETURN static void upb_UnknownFields_OutOfMemory(
upb_UnknownField_Context* ctx) {
ctx->status = kUpb_UnknownCompareResult_OutOfMemory;
UPB_LONGJMP(ctx->err, 1);
}
static void upb_UnknownFields_Grow(upb_UnknownField_Context* ctx,
upb_UnknownField** base,
upb_UnknownField** ptr,
upb_UnknownField** end) {
size_t old = (*ptr - *base);
size_t new = UPB_MAX(4, old * 2);
*base = upb_Arena_Realloc(ctx->arena, *base, old * sizeof(**base),
new * sizeof(**base));
if (!*base) upb_UnknownFields_OutOfMemory(ctx);
*ptr = *base + old;
*end = *base + new;
}
// We have to implement our own sort here, since qsort() is not an in-order
// sort. Here we use merge sort, the simplest in-order sort.
static void upb_UnknownFields_Merge(upb_UnknownField* arr, size_t start,
size_t mid, size_t end,
upb_UnknownField* tmp) {
memcpy(tmp, &arr[start], (end - start) * sizeof(*tmp));
upb_UnknownField* ptr1 = tmp;
upb_UnknownField* end1 = &tmp[mid - start];
upb_UnknownField* ptr2 = &tmp[mid - start];
upb_UnknownField* end2 = &tmp[end - start];
upb_UnknownField* out = &arr[start];
while (ptr1 < end1 && ptr2 < end2) {
if (ptr1->tag <= ptr2->tag) {
*out++ = *ptr1++;
} else {
*out++ = *ptr2++;
}
}
if (ptr1 < end1) {
memcpy(out, ptr1, (end1 - ptr1) * sizeof(*out));
} else if (ptr2 < end2) {
memcpy(out, ptr1, (end2 - ptr2) * sizeof(*out));
}
}
static void upb_UnknownFields_SortRecursive(upb_UnknownField* arr, size_t start,
size_t end, upb_UnknownField* tmp) {
if (end - start > 1) {
size_t mid = start + ((end - start) / 2);
upb_UnknownFields_SortRecursive(arr, start, mid, tmp);
upb_UnknownFields_SortRecursive(arr, mid, end, tmp);
upb_UnknownFields_Merge(arr, start, mid, end, tmp);
}
}
static void upb_UnknownFields_Sort(upb_UnknownField_Context* ctx,
upb_UnknownFields* fields) {
if (ctx->tmp_size < fields->size) {
const int oldsize = ctx->tmp_size * sizeof(*ctx->tmp);
ctx->tmp_size = UPB_MAX(8, ctx->tmp_size);
while (ctx->tmp_size < fields->size) ctx->tmp_size *= 2;
const int newsize = ctx->tmp_size * sizeof(*ctx->tmp);
ctx->tmp = upb_grealloc(ctx->tmp, oldsize, newsize);
}
upb_UnknownFields_SortRecursive(fields->fields, 0, fields->size, ctx->tmp);
}
static upb_UnknownFields* upb_UnknownFields_DoBuild(
upb_UnknownField_Context* ctx, const char** buf) {
upb_UnknownField* arr_base = NULL;
upb_UnknownField* arr_ptr = NULL;
upb_UnknownField* arr_end = NULL;
const char* ptr = *buf;
uint32_t last_tag = 0;
bool sorted = true;
while (!upb_EpsCopyInputStream_IsDone(&ctx->stream, &ptr)) {
uint32_t tag;
ptr = upb_WireReader_ReadTag(ptr, &tag);
UPB_ASSERT(tag <= UINT32_MAX);
int wire_type = upb_WireReader_GetWireType(tag);
if (wire_type == kUpb_WireType_EndGroup) break;
if (tag < last_tag) sorted = false;
last_tag = tag;
if (arr_ptr == arr_end) {
upb_UnknownFields_Grow(ctx, &arr_base, &arr_ptr, &arr_end);
}
upb_UnknownField* field = arr_ptr;
field->tag = tag;
arr_ptr++;
switch (wire_type) {
case kUpb_WireType_Varint:
ptr = upb_WireReader_ReadVarint(ptr, &field->data.varint);
break;
case kUpb_WireType_64Bit:
ptr = upb_WireReader_ReadFixed64(ptr, &field->data.uint64);
break;
case kUpb_WireType_32Bit:
ptr = upb_WireReader_ReadFixed32(ptr, &field->data.uint32);
break;
case kUpb_WireType_Delimited: {
int size;
ptr = upb_WireReader_ReadSize(ptr, &size);
const char* s_ptr = ptr;
ptr = upb_EpsCopyInputStream_ReadStringAliased(&ctx->stream, &s_ptr,
size);
field->data.delimited.data = s_ptr;
field->data.delimited.size = size;
break;
}
case kUpb_WireType_StartGroup:
if (--ctx->depth == 0) {
ctx->status = kUpb_UnknownCompareResult_MaxDepthExceeded;
UPB_LONGJMP(ctx->err, 1);
}
field->data.group = upb_UnknownFields_DoBuild(ctx, &ptr);
ctx->depth++;
break;
default:
UPB_UNREACHABLE();
}
}
*buf = ptr;
upb_UnknownFields* ret = upb_Arena_Malloc(ctx->arena, sizeof(*ret));
if (!ret) upb_UnknownFields_OutOfMemory(ctx);
ret->fields = arr_base;
ret->size = arr_ptr - arr_base;
ret->capacity = arr_end - arr_base;
if (!sorted) {
upb_UnknownFields_Sort(ctx, ret);
}
return ret;
}
// Builds a upb_UnknownFields data structure from the binary data in buf.
static upb_UnknownFields* upb_UnknownFields_Build(upb_UnknownField_Context* ctx,
const char* ptr,
size_t size) {
upb_EpsCopyInputStream_Init(&ctx->stream, &ptr, size, true);
upb_UnknownFields* fields = upb_UnknownFields_DoBuild(ctx, &ptr);
UPB_ASSERT(upb_EpsCopyInputStream_IsDone(&ctx->stream, &ptr) &&
!upb_EpsCopyInputStream_IsError(&ctx->stream));
return fields;
}
// Compares two sorted upb_UnknownFields structures for equality.
static bool upb_UnknownFields_IsEqual(const upb_UnknownFields* uf1,
const upb_UnknownFields* uf2) {
if (uf1->size != uf2->size) return false;
for (size_t i = 0, n = uf1->size; i < n; i++) {
upb_UnknownField* f1 = &uf1->fields[i];
upb_UnknownField* f2 = &uf2->fields[i];
if (f1->tag != f2->tag) return false;
int wire_type = f1->tag & 7;
switch (wire_type) {
case kUpb_WireType_Varint:
if (f1->data.varint != f2->data.varint) return false;
break;
case kUpb_WireType_64Bit:
if (f1->data.uint64 != f2->data.uint64) return false;
break;
case kUpb_WireType_32Bit:
if (f1->data.uint32 != f2->data.uint32) return false;
break;
case kUpb_WireType_Delimited:
if (!upb_StringView_IsEqual(f1->data.delimited, f2->data.delimited)) {
return false;
}
break;
case kUpb_WireType_StartGroup:
if (!upb_UnknownFields_IsEqual(f1->data.group, f2->data.group)) {
return false;
}
break;
default:
UPB_UNREACHABLE();
}
}
return true;
}
static upb_UnknownCompareResult upb_UnknownField_DoCompare(
upb_UnknownField_Context* ctx, const char* buf1, size_t size1,
const char* buf2, size_t size2) {
upb_UnknownCompareResult ret;
// First build both unknown fields into a sorted data structure (similar
// to the UnknownFieldSet in C++).
upb_UnknownFields* uf1 = upb_UnknownFields_Build(ctx, buf1, size1);
upb_UnknownFields* uf2 = upb_UnknownFields_Build(ctx, buf2, size2);
// Now perform the equality check on the sorted structures.
if (upb_UnknownFields_IsEqual(uf1, uf2)) {
ret = kUpb_UnknownCompareResult_Equal;
} else {
ret = kUpb_UnknownCompareResult_NotEqual;
}
return ret;
}
static upb_UnknownCompareResult upb_UnknownField_Compare(
upb_UnknownField_Context* const ctx, const char* const buf1,
const size_t size1, const char* const buf2, const size_t size2) {
upb_UnknownCompareResult ret;
if (UPB_SETJMP(ctx->err) == 0) {
ret = upb_UnknownField_DoCompare(ctx, buf1, size1, buf2, size2);
} else {
ret = ctx->status;
UPB_ASSERT(ret != kUpb_UnknownCompareResult_Equal);
}
upb_Arena_Free(ctx->arena);
upb_gfree(ctx->tmp);
return ret;
}
upb_UnknownCompareResult UPB_PRIVATE(_upb_Message_UnknownFieldsAreEqual)(
const char* buf1, size_t size1, const char* buf2, size_t size2,
int max_depth) {
if (size1 == 0 && size2 == 0) return kUpb_UnknownCompareResult_Equal;
if (size1 == 0 || size2 == 0) return kUpb_UnknownCompareResult_NotEqual;
if (memcmp(buf1, buf2, size1) == 0) return kUpb_UnknownCompareResult_Equal;
upb_UnknownField_Context ctx = {
.arena = upb_Arena_New(),
.depth = max_depth,
.tmp = NULL,
.tmp_size = 0,
.status = kUpb_UnknownCompareResult_Equal,
};
if (!ctx.arena) return kUpb_UnknownCompareResult_OutOfMemory;
return upb_UnknownField_Compare(&ctx, buf1, size1, buf2, size2);
}

View File

@@ -0,0 +1,49 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_
#define UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_
#include <stddef.h>
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// Returns true if unknown fields from the two messages are equal when sorted
// and varints are made canonical.
//
// This function is discouraged, as the comparison is inherently lossy without
// schema data:
//
// 1. We don't know whether delimited fields are sub-messages. Unknown
// sub-messages will therefore not have their fields sorted and varints
// canonicalized.
// 2. We don't know about oneof/non-repeated fields, which should semantically
// discard every value except the last.
typedef enum {
kUpb_UnknownCompareResult_Equal = 0,
kUpb_UnknownCompareResult_NotEqual = 1,
kUpb_UnknownCompareResult_OutOfMemory = 2,
kUpb_UnknownCompareResult_MaxDepthExceeded = 3,
} upb_UnknownCompareResult;
upb_UnknownCompareResult UPB_PRIVATE(_upb_Message_UnknownFieldsAreEqual)(
const char* buf1, size_t size1, const char* buf2, size_t size2,
int max_depth);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_ */

View File

@@ -0,0 +1,63 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/message/internal/extension.h"
#include <string.h>
#include "upb/mem/arena.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/types.h"
#include "upb/mini_table/extension.h"
// Must be last.
#include "upb/port/def.inc"
const upb_Extension* UPB_PRIVATE(_upb_Message_Getext)(
const struct upb_Message* msg, const upb_MiniTableExtension* e) {
size_t n;
const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getexts)(msg, &n);
// For now we use linear search exclusively to find extensions.
// If this becomes an issue due to messages with lots of extensions,
// we can introduce a table of some sort.
for (size_t i = 0; i < n; i++) {
if (ext[i].ext == e) {
return &ext[i];
}
}
return NULL;
}
const upb_Extension* UPB_PRIVATE(_upb_Message_Getexts)(
const struct upb_Message* msg, size_t* count) {
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (in) {
*count = (in->size - in->ext_begin) / sizeof(upb_Extension);
return UPB_PTR_AT(in, in->ext_begin, void);
} else {
*count = 0;
return NULL;
}
}
upb_Extension* UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
struct upb_Message* msg, const upb_MiniTableExtension* e, upb_Arena* a) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Extension* ext = (upb_Extension*)UPB_PRIVATE(_upb_Message_Getext)(msg, e);
if (ext) return ext;
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, sizeof(upb_Extension), a))
return NULL;
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
in->ext_begin -= sizeof(upb_Extension);
ext = UPB_PTR_AT(in, in->ext_begin, void);
memset(ext, 0, sizeof(upb_Extension));
ext->ext = e;
return ext;
}

View File

@@ -8,10 +8,10 @@
#ifndef UPB_MESSAGE_INTERNAL_EXTENSION_H_
#define UPB_MESSAGE_INTERNAL_EXTENSION_H_
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include <stddef.h>
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/message/value.h"
#include "upb/mini_table/extension.h"
// Must be last.
@@ -27,12 +27,8 @@
// the most common extension type.
typedef struct {
const upb_MiniTableExtension* ext;
union {
upb_StringView str;
void* ptr;
char scalar_data[8];
} data;
} upb_Message_Extension;
upb_MessageValue data;
} upb_Extension;
#ifdef __cplusplus
extern "C" {
@@ -41,18 +37,19 @@ extern "C" {
// Adds the given extension data to the given message.
// |ext| is copied into the message instance.
// This logically replaces any previously-added extension with this number.
upb_Message_Extension* _upb_Message_GetOrCreateExtension(
upb_Message* msg, const upb_MiniTableExtension* ext, upb_Arena* arena);
upb_Extension* UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
struct upb_Message* msg, const upb_MiniTableExtension* ext,
upb_Arena* arena);
// Returns an array of extensions for this message.
// Note: the array is ordered in reverse relative to the order of creation.
const upb_Message_Extension* _upb_Message_Getexts(const upb_Message* msg,
size_t* count);
const upb_Extension* UPB_PRIVATE(_upb_Message_Getexts)(
const struct upb_Message* msg, size_t* count);
// Returns an extension for the given field number, or NULL if no extension
// exists for this field number.
const upb_Message_Extension* _upb_Message_Getext(
const upb_Message* msg, const upb_MiniTableExtension* ext);
// Returns an extension for a message with a given mini table,
// or NULL if no extension exists with this mini table.
const upb_Extension* UPB_PRIVATE(_upb_Message_Getext)(
const struct upb_Message* msg, const upb_MiniTableExtension* ext);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -1,51 +1,38 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
#ifndef UPB_MESSAGE_INTERNAL_MAP_H_
#define UPB_MESSAGE_INTERNAL_MAP_H_
#ifndef UPB_COLLECTIONS_INTERNAL_MAP_H_
#define UPB_COLLECTIONS_INTERNAL_MAP_H_
#include <stddef.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/hash/str_table.h"
#include "upb/mem/arena.h"
#include "upb/message/map.h"
// Must be last.
#include "upb/port/def.inc"
typedef enum {
kUpb_MapInsertStatus_Inserted = 0,
kUpb_MapInsertStatus_Replaced = 1,
kUpb_MapInsertStatus_OutOfMemory = 2,
} upb_MapInsertStatus;
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
struct upb_Map {
// 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;
bool UPB_PRIVATE(is_frozen);
upb_strtable table;
};
@@ -54,6 +41,14 @@ struct upb_Map {
extern "C" {
#endif
UPB_INLINE void UPB_PRIVATE(_upb_Map_ShallowFreeze)(struct upb_Map* map) {
map->UPB_PRIVATE(is_frozen) = true;
}
UPB_API_INLINE bool upb_Map_IsFrozen(const struct upb_Map* map) {
return map->UPB_PRIVATE(is_frozen);
}
// Converting between internal table representation and user values.
//
// _upb_map_tokey() and _upb_map_fromkey() are inverses.
@@ -100,7 +95,7 @@ UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size) {
}
}
UPB_INLINE void* _upb_map_next(const upb_Map* map, size_t* iter) {
UPB_INLINE void* _upb_map_next(const struct upb_Map* map, size_t* iter) {
upb_strtable_iter it;
it.t = &map->table;
it.index = *iter;
@@ -110,17 +105,21 @@ UPB_INLINE void* _upb_map_next(const upb_Map* map, size_t* iter) {
return (void*)str_tabent(&it);
}
UPB_INLINE void _upb_Map_Clear(upb_Map* map) {
UPB_INLINE void _upb_Map_Clear(struct upb_Map* map) {
UPB_ASSERT(!upb_Map_IsFrozen(map));
upb_strtable_clear(&map->table);
}
UPB_INLINE bool _upb_Map_Delete(upb_Map* map, const void* key, size_t key_size,
upb_value* val) {
UPB_INLINE bool _upb_Map_Delete(struct upb_Map* map, const void* key,
size_t key_size, upb_value* val) {
UPB_ASSERT(!upb_Map_IsFrozen(map));
upb_StringView k = _upb_map_tokey(key, key_size);
return upb_strtable_remove2(&map->table, k.data, k.size, val);
}
UPB_INLINE bool _upb_Map_Get(const upb_Map* map, const void* key,
UPB_INLINE bool _upb_Map_Get(const struct upb_Map* map, const void* key,
size_t key_size, void* val, size_t val_size) {
upb_value tabval;
upb_StringView k = _upb_map_tokey(key, key_size);
@@ -131,9 +130,12 @@ UPB_INLINE bool _upb_Map_Get(const upb_Map* map, const void* key,
return ret;
}
UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(upb_Map* map, const void* key,
size_t key_size, void* val,
size_t val_size, upb_Arena* a) {
UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(struct upb_Map* map,
const void* key, size_t key_size,
void* val, size_t val_size,
upb_Arena* a) {
UPB_ASSERT(!upb_Map_IsFrozen(map));
upb_StringView strkey = _upb_map_tokey(key, key_size);
upb_value tabval = {0};
if (!_upb_map_tovalue(val, val_size, &tabval, a)) {
@@ -150,7 +152,7 @@ UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(upb_Map* map, const void* key,
: kUpb_MapInsertStatus_Inserted;
}
UPB_INLINE size_t _upb_Map_Size(const upb_Map* map) {
UPB_INLINE size_t _upb_Map_Size(const struct upb_Map* map) {
return map->table.t.count;
}
@@ -162,7 +164,7 @@ UPB_INLINE size_t _upb_Map_CTypeSize(upb_CType ctype) {
}
// 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);
struct upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size);
#ifdef __cplusplus
} /* extern "C" */
@@ -170,4 +172,4 @@ upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size);
#include "upb/port/undef.inc"
#endif /* UPB_COLLECTIONS_INTERNAL_MAP_H_ */
#endif /* UPB_MESSAGE_INTERNAL_MAP_H_ */

View File

@@ -1,66 +1,41 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_COLLECTIONS_INTERNAL_MAP_ENTRY_H_
#define UPB_COLLECTIONS_INTERNAL_MAP_ENTRY_H_
#ifndef UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_
#define UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_
#include <stdint.h>
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/message/internal/types.h"
// Map entries aren't actually stored for map fields, they are only used during
// parsing. For parsing, it helps a lot if all map entry messages have the same
// layout. The layout code in mini_table/decode.c will ensure that all map
// entries have this layout.
// parsing. (It helps a lot if all map entry messages have the same layout.)
// The mini_table layout code will ensure that all map entries have this layout.
//
// Note that users can and do create map entries directly, which will also use
// this layout.
//
// NOTE: sync with wire/decode.c.
typedef struct {
struct upb_Message message;
// We only need 2 hasbits max, but due to alignment we'll use 8 bytes here,
// and the uint64_t helps make this clear.
uint64_t hasbits;
union {
upb_StringView str; // For str/bytes.
upb_value val; // For all other types.
double d[2]; // Padding for 32-bit builds.
} k;
union {
upb_StringView str; // For str/bytes.
upb_value val; // For all other types.
double d[2]; // Padding for 32-bit builds.
} v;
} upb_MapEntryData;
typedef struct {
upb_Message_Internal internal;
upb_MapEntryData data;
} upb_MapEntry;
#endif // UPB_COLLECTIONS_INTERNAL_MAP_ENTRY_H_
#endif // UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_

View File

@@ -1,40 +1,20 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
#ifndef UPB_COLLECTIONS_INTERNAL_MAP_SORTER_H_
#define UPB_COLLECTIONS_INTERNAL_MAP_SORTER_H_
#ifndef UPB_MESSAGE_INTERNAL_MAP_SORTER_H_
#define UPB_MESSAGE_INTERNAL_MAP_SORTER_H_
#include <stdlib.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/mem/alloc.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/map_entry.h"
@@ -69,25 +49,26 @@ UPB_INLINE void _upb_mapsorter_init(_upb_mapsorter* s) {
}
UPB_INLINE void _upb_mapsorter_destroy(_upb_mapsorter* s) {
if (s->entries) free(s->entries);
if (s->entries) upb_gfree(s->entries);
}
UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s, const upb_Map* map,
UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s,
const struct upb_Map* map,
_upb_sortedmap* sorted, upb_MapEntry* ent) {
if (sorted->pos == sorted->end) return false;
const upb_tabent* tabent = (const upb_tabent*)s->entries[sorted->pos++];
upb_StringView key = upb_tabstrview(tabent->key);
_upb_map_fromkey(key, &ent->data.k, map->key_size);
_upb_map_fromkey(key, &ent->k, map->key_size);
upb_value val = {tabent->val.val};
_upb_map_fromvalue(val, &ent->data.v, map->val_size);
_upb_map_fromvalue(val, &ent->v, map->val_size);
return true;
}
UPB_INLINE bool _upb_sortedmap_nextext(_upb_mapsorter* s,
_upb_sortedmap* sorted,
const upb_Message_Extension** ext) {
const upb_Extension** ext) {
if (sorted->pos == sorted->end) return false;
*ext = (const upb_Message_Extension*)s->entries[sorted->pos++];
*ext = (const upb_Extension*)s->entries[sorted->pos++];
return true;
}
@@ -97,11 +78,10 @@ UPB_INLINE void _upb_mapsorter_popmap(_upb_mapsorter* s,
}
bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
const upb_Map* map, _upb_sortedmap* sorted);
const struct upb_Map* map, _upb_sortedmap* sorted);
bool _upb_mapsorter_pushexts(_upb_mapsorter* s,
const upb_Message_Extension* exts, size_t count,
_upb_sortedmap* sorted);
bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Extension* exts,
size_t count, _upb_sortedmap* sorted);
#ifdef __cplusplus
} /* extern "C" */
@@ -109,4 +89,4 @@ bool _upb_mapsorter_pushexts(_upb_mapsorter* s,
#include "upb/port/undef.inc"
#endif /* UPB_COLLECTIONS_INTERNAL_MAP_SORTER_H_ */
#endif /* UPB_MESSAGE_INTERNAL_MAP_SORTER_H_ */

View File

@@ -0,0 +1,75 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/message/internal/message.h"
#include <math.h>
#include <string.h>
#include "upb/base/internal/log2.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/types.h"
// Must be last.
#include "upb/port/def.inc"
const float kUpb_FltInfinity = (float)(1.0 / 0.0);
const double kUpb_Infinity = 1.0 / 0.0;
const double kUpb_NaN = 0.0 / 0.0;
bool UPB_PRIVATE(_upb_Message_Realloc)(struct upb_Message* msg, size_t need,
upb_Arena* a) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const size_t overhead = sizeof(upb_Message_Internal);
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (!in) {
// No internal data, allocate from scratch.
size_t size = UPB_MAX(128, upb_Log2CeilingSize(need + overhead));
in = upb_Arena_Malloc(a, size);
if (!in) return false;
in->size = size;
in->unknown_end = overhead;
in->ext_begin = size;
UPB_PRIVATE(_upb_Message_SetInternal)(msg, in);
} else if (in->ext_begin - in->unknown_end < need) {
// Internal data is too small, reallocate.
size_t new_size = upb_Log2CeilingSize(in->size + need);
size_t ext_bytes = in->size - in->ext_begin;
size_t new_ext_begin = new_size - ext_bytes;
in = upb_Arena_Realloc(a, in, in->size, new_size);
if (!in) return false;
if (ext_bytes) {
// Need to move extension data to the end.
char* ptr = (char*)in;
memmove(ptr + new_ext_begin, ptr + in->ext_begin, ext_bytes);
}
in->ext_begin = new_ext_begin;
in->size = new_size;
UPB_PRIVATE(_upb_Message_SetInternal)(msg, in);
}
UPB_ASSERT(in->ext_begin - in->unknown_end >= need);
return true;
}
#if UPB_TRACING_ENABLED
static void (*_message_trace_handler)(const upb_MiniTable*, const upb_Arena*);
void upb_Message_LogNewMessage(const upb_MiniTable* m, const upb_Arena* arena) {
if (_message_trace_handler) {
_message_trace_handler(m, arena);
}
}
void upb_Message_SetNewMessageTraceHandler(void (*handler)(const upb_MiniTable*,
const upb_Arena*)) {
_message_trace_handler = handler;
}
#endif // UPB_TRACING_ENABLED

View File

@@ -12,17 +12,14 @@
** The definitions in this file are internal to upb.
**/
#ifndef UPB_MESSAGE_INTERNAL_H_
#define UPB_MESSAGE_INTERNAL_H_
#ifndef UPB_MESSAGE_INTERNAL_MESSAGE_H_
#define UPB_MESSAGE_INTERNAL_MESSAGE_H_
#include <stdlib.h>
#include <string.h>
#include "upb/mem/arena.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/types.h"
#include "upb/message/message.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/extension_registry.h"
#include "upb/mini_table/message.h"
// Must be last.
@@ -36,14 +33,12 @@ extern const float kUpb_FltInfinity;
extern const double kUpb_Infinity;
extern const double kUpb_NaN;
/* Internal members of a upb_Message that track unknown fields and/or
* extensions. We can change this without breaking binary compatibility. We put
* these before the user's data. The user's upb_Message* points after the
* upb_Message_Internal. */
// Internal members of a upb_Message that track unknown fields and/or
// extensions. We can change this without breaking binary compatibility.
struct upb_Message_InternalData {
/* Total size of this structure, including the data that follows.
* Must be aligned to 8, which is alignof(upb_Message_Extension) */
typedef struct upb_Message_Internal {
// Total size of this structure, including the data that follows.
// Must be aligned to 8, which is alignof(upb_Extension)
uint32_t size;
/* Offsets relative to the beginning of this structure.
@@ -53,49 +48,49 @@ struct upb_Message_InternalData {
* When the two meet, we're out of data and have to realloc.
*
* If we imagine that the final member of this struct is:
* char data[size - overhead]; // overhead =
* sizeof(upb_Message_InternalData)
* char data[size - overhead]; // overhead = sizeof(upb_Message_Internal)
*
* Then we have:
* unknown data: data[0 .. (unknown_end - overhead)]
* extensions data: data[(ext_begin - overhead) .. (size - overhead)] */
uint32_t unknown_end;
uint32_t ext_begin;
/* Data follows, as if there were an array:
* char data[size - sizeof(upb_Message_InternalData)]; */
};
// Data follows, as if there were an array:
// char data[size - sizeof(upb_Message_Internal)];
} upb_Message_Internal;
/* Maps upb_CType -> memory size. */
extern char _upb_CTypeo_size[12];
UPB_INLINE size_t upb_msg_sizeof(const upb_MiniTable* t) {
return t->size + sizeof(upb_Message_Internal);
}
#ifdef UPB_TRACING_ENABLED
UPB_API void upb_Message_LogNewMessage(const upb_MiniTable* m,
const upb_Arena* arena);
UPB_API void upb_Message_SetNewMessageTraceHandler(
void (*handler)(const upb_MiniTable*, const upb_Arena*));
#endif // UPB_TRACING_ENABLED
// Inline version upb_Message_New(), for internal use.
UPB_INLINE upb_Message* _upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena) {
size_t size = upb_msg_sizeof(mini_table);
void* mem = upb_Arena_Malloc(arena, size + sizeof(upb_Message_Internal));
if (UPB_UNLIKELY(!mem)) return NULL;
upb_Message* msg = UPB_PTR_AT(mem, sizeof(upb_Message_Internal), upb_Message);
memset(mem, 0, size);
UPB_INLINE struct upb_Message* _upb_Message_New(const upb_MiniTable* m,
upb_Arena* a) {
#ifdef UPB_TRACING_ENABLED
upb_Message_LogNewMessage(m, a);
#endif // UPB_TRACING_ENABLED
const int size = m->UPB_PRIVATE(size);
struct upb_Message* msg = (struct upb_Message*)upb_Arena_Malloc(a, size);
if (UPB_UNLIKELY(!msg)) return NULL;
memset(msg, 0, size);
return msg;
}
UPB_INLINE upb_Message_Internal* upb_Message_Getinternal(
const upb_Message* msg) {
ptrdiff_t size = sizeof(upb_Message_Internal);
return (upb_Message_Internal*)((char*)msg - size);
}
// Discards the unknown fields for this message only.
void _upb_Message_DiscardUnknown_shallow(upb_Message* msg);
void _upb_Message_DiscardUnknown_shallow(struct upb_Message* msg);
// Adds unknown data (serialized protobuf data) to the given message.
// The data is copied into the message instance.
bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena);
bool UPB_PRIVATE(_upb_Message_AddUnknown)(struct upb_Message* msg,
const char* data, size_t len,
upb_Arena* arena);
bool UPB_PRIVATE(_upb_Message_Realloc)(struct upb_Message* msg, size_t need,
upb_Arena* arena);
#ifdef __cplusplus
} /* extern "C" */
@@ -103,4 +98,4 @@ bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_INTERNAL_H_ */
#endif /* UPB_MESSAGE_INTERNAL_MESSAGE_H_ */

View File

@@ -0,0 +1,56 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_TABLE_INTERNAL_TAGGED_PTR_H_
#define UPB_MINI_TABLE_INTERNAL_TAGGED_PTR_H_
#include <stdint.h>
#include "upb/message/internal/message.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// Internal-only because empty messages cannot be created by the user.
UPB_INLINE uintptr_t
UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(struct upb_Message* ptr, bool empty) {
UPB_ASSERT(((uintptr_t)ptr & 1) == 0);
return (uintptr_t)ptr | (empty ? 1 : 0);
}
UPB_API_INLINE bool upb_TaggedMessagePtr_IsEmpty(uintptr_t ptr) {
return ptr & 1;
}
UPB_INLINE struct upb_Message* UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(
uintptr_t ptr) {
return (struct upb_Message*)(ptr & ~(uintptr_t)1);
}
UPB_API_INLINE struct upb_Message* upb_TaggedMessagePtr_GetNonEmptyMessage(
uintptr_t ptr) {
UPB_ASSERT(!upb_TaggedMessagePtr_IsEmpty(ptr));
return UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(ptr);
}
UPB_INLINE struct upb_Message* UPB_PRIVATE(
_upb_TaggedMessagePtr_GetEmptyMessage)(uintptr_t ptr) {
UPB_ASSERT(upb_TaggedMessagePtr_IsEmpty(ptr));
return UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(ptr);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_INTERNAL_TAGGED_PTR_H_ */

View File

@@ -5,19 +5,54 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_TABLE_INTERNAL_TYPES_H_
#define UPB_MINI_TABLE_INTERNAL_TYPES_H_
#ifndef UPB_MESSAGE_INTERNAL_TYPES_H_
#define UPB_MESSAGE_INTERNAL_TYPES_H_
typedef struct upb_Message_InternalData upb_Message_InternalData;
#include <stdint.h>
typedef struct {
// Must be last.
#include "upb/port/def.inc"
#define UPB_OPAQUE(x) x##_opaque
struct upb_Message {
union {
upb_Message_InternalData* internal;
// Force 8-byte alignment, since the data members may contain members that
// require 8-byte alignment.
double d;
uintptr_t UPB_OPAQUE(internal); // tagged pointer, low bit == frozen
double d; // Forces same size for 32-bit/64-bit builds
};
} upb_Message_Internal;
};
#endif // UPB_MINI_TABLE_INTERNAL_TYPES_H_
#ifdef __cplusplus
extern "C" {
#endif
UPB_INLINE void UPB_PRIVATE(_upb_Message_ShallowFreeze)(
struct upb_Message* msg) {
msg->UPB_OPAQUE(internal) |= 1ULL;
}
UPB_API_INLINE bool upb_Message_IsFrozen(const struct upb_Message* msg) {
return (msg->UPB_OPAQUE(internal) & 1ULL) != 0;
}
UPB_INLINE struct upb_Message_Internal* UPB_PRIVATE(_upb_Message_GetInternal)(
const struct upb_Message* msg) {
const uintptr_t tmp = msg->UPB_OPAQUE(internal) & ~1ULL;
return (struct upb_Message_Internal*)tmp;
}
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetInternal)(
struct upb_Message* msg, struct upb_Message_Internal* internal) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
msg->UPB_OPAQUE(internal) = (uintptr_t)internal;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#undef UPB_OPAQUE
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_INTERNAL_TYPES_H_ */

View File

@@ -1,39 +1,26 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/message/map.h"
#include <stdint.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/hash/str_table.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/map.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/message/value.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
@@ -131,6 +118,20 @@ upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter) {
return ret;
}
void upb_Map_Freeze(upb_Map* map, const upb_MiniTable* m) {
if (upb_Map_IsFrozen(map)) return;
UPB_PRIVATE(_upb_Map_ShallowFreeze)(map);
if (m) {
size_t iter = kUpb_Map_Begin;
upb_MessageValue key, val;
while (upb_Map_Next(map, &key, &val, &iter)) {
upb_Message_Freeze((upb_Message*)val.msg_val, m);
}
}
}
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size) {
@@ -140,6 +141,7 @@ upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size) {
upb_strtable_init(&map->table, 4, a);
map->key_size = key_size;
map->val_size = value_size;
map->UPB_PRIVATE(is_frozen) = false;
return map;
}

View File

@@ -1,32 +1,9 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_MAP_H_
#define UPB_MESSAGE_MAP_H_
@@ -35,7 +12,10 @@
#include "upb/base/descriptor_constants.h"
#include "upb/mem/arena.h"
#include "upb/message/value.h" // IWYU pragma: export
#include "upb/message/internal/map.h"
#include "upb/message/value.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
@@ -62,12 +42,6 @@ UPB_API bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
// Removes all entries in the map.
UPB_API void upb_Map_Clear(upb_Map* map);
typedef enum {
kUpb_MapInsertStatus_Inserted = 0,
kUpb_MapInsertStatus_Replaced = 1,
kUpb_MapInsertStatus_OutOfMemory = 2,
} upb_MapInsertStatus;
// Sets the given key to the given value, returning whether the key was inserted
// or replaced. If the key was inserted, then any existing iterators will be
// invalidated.
@@ -89,12 +63,6 @@ UPB_API_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key,
UPB_API bool upb_Map_Delete(upb_Map* map, upb_MessageValue key,
upb_MessageValue* val);
// (DEPRECATED and going away soon. Do not use.)
UPB_INLINE bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key,
upb_MessageValue* val) {
return upb_Map_Delete(map, key, val);
}
// Map iteration:
//
// size_t iter = kUpb_Map_Begin;
@@ -103,7 +71,7 @@ UPB_INLINE bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key,
// ...
// }
#define kUpb_Map_Begin ((size_t)-1)
#define kUpb_Map_Begin ((size_t) - 1)
// Advances to the next entry. Returns false if no more entries are present.
// Otherwise returns true and populates both *key and *value.
@@ -138,6 +106,14 @@ UPB_API bool upb_MapIterator_Done(const upb_Map* map, size_t iter);
UPB_API upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
UPB_API upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter);
// Mark a map and all of its descendents as frozen/immutable.
// If the map values are messages then |m| must point to the minitable for
// those messages. Otherwise |m| must be NULL.
UPB_API void upb_Map_Freeze(upb_Map* map, const upb_MiniTable* m);
// Returns whether a map has been frozen.
UPB_API_INLINE bool upb_Map_IsFrozen(const upb_Map* map);
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -1,32 +1,9 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// These functions are only used by generated code.

View File

@@ -1,36 +1,22 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. 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 LLC 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.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/message/internal/map_sorter.h"
#include <stdint.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/log2.h"
#include "upb/base/string_view.h"
#include "upb/mem/alloc.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/mini_table/extension.h"
// Must be last.
#include "upb/port/def.inc"
@@ -113,8 +99,10 @@ static bool _upb_mapsorter_resize(_upb_mapsorter* s, _upb_sortedmap* sorted,
sorted->end = sorted->start + size;
if (sorted->end > s->cap) {
const int oldsize = s->cap * sizeof(*s->entries);
s->cap = upb_Log2CeilingSize(sorted->end);
s->entries = realloc(s->entries, s->cap * sizeof(*s->entries));
const int newsize = s->cap * sizeof(*s->entries);
s->entries = upb_grealloc(s->entries, oldsize, newsize);
if (!s->entries) return false;
}
@@ -125,6 +113,7 @@ static bool _upb_mapsorter_resize(_upb_mapsorter* s, _upb_sortedmap* sorted,
bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
const upb_Map* map, _upb_sortedmap* sorted) {
int map_size = _upb_Map_Size(map);
UPB_ASSERT(map_size);
if (!_upb_mapsorter_resize(s, sorted, map_size)) return false;
@@ -147,17 +136,16 @@ bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
}
static int _upb_mapsorter_cmpext(const void* _a, const void* _b) {
const upb_Message_Extension* const* a = _a;
const upb_Message_Extension* const* b = _b;
uint32_t a_num = (*a)->ext->field.number;
uint32_t b_num = (*b)->ext->field.number;
const upb_Extension* const* a = _a;
const upb_Extension* const* b = _b;
uint32_t a_num = upb_MiniTableExtension_Number((*a)->ext);
uint32_t b_num = upb_MiniTableExtension_Number((*b)->ext);
assert(a_num != b_num);
return a_num < b_num ? -1 : 1;
}
bool _upb_mapsorter_pushexts(_upb_mapsorter* s,
const upb_Message_Extension* exts, size_t count,
_upb_sortedmap* sorted) {
bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Extension* exts,
size_t count, _upb_sortedmap* sorted) {
if (!_upb_mapsorter_resize(s, sorted, count)) return false;
for (size_t i = 0; i < count; i++) {

38
Pods/gRPC-Core/third_party/upb/upb/message/merge.c generated vendored Normal file
View File

@@ -0,0 +1,38 @@
#include "upb/message/merge.h"
#include "stddef.h"
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/mini_table/extension_registry.h"
#include "upb/mini_table/message.h"
#include "upb/wire/decode.h"
#include "upb/wire/encode.h"
// Must be last.
#include "upb/port/def.inc"
bool upb_Message_MergeFrom(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* mt,
const upb_ExtensionRegistry* extreg,
upb_Arena* arena) {
char* buf = NULL;
size_t size = 0;
// This tmp arena is used to hold the bytes for `src` serialized. This bends
// the typical "no hidden allocations" design of upb, but under a properly
// optimized implementation this extra allocation would not be necessary and
// so we don't want to unnecessarily have the bad API or bloat the passed-in
// arena with this very-short-term allocation.
upb_Arena* encode_arena = upb_Arena_New();
upb_EncodeStatus e_status = upb_Encode(src, mt, 0, encode_arena, &buf, &size);
if (e_status != kUpb_EncodeStatus_Ok) {
upb_Arena_Free(encode_arena);
return false;
}
upb_DecodeStatus d_status = upb_Decode(buf, size, dst, mt, extreg, 0, arena);
if (d_status != kUpb_DecodeStatus_Ok) {
upb_Arena_Free(encode_arena);
return false;
}
upb_Arena_Free(encode_arena);
return true;
}

26
Pods/gRPC-Core/third_party/upb/upb/message/merge.h generated vendored Normal file
View File

@@ -0,0 +1,26 @@
#ifndef THIRD_PARTY_UPB_UPB_MESSAGE_MERGE_H_
#define THIRD_PARTY_UPB_UPB_MESSAGE_MERGE_H_
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/mini_table/extension_registry.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
UPB_API bool upb_Message_MergeFrom(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* mt,
const upb_ExtensionRegistry* extreg,
upb_Arena* arena);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif // THIRD_PARTY_UPB_UPB_MESSAGE_MERGE_H_

View File

@@ -7,78 +7,56 @@
#include "upb/message/message.h"
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/base/internal/log2.h"
#include "upb/mem/arena.h"
#include "upb/message/accessors.h"
#include "upb/message/array.h"
#include "upb/message/internal/accessors.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/types.h"
#include "upb/message/map.h"
#include "upb/message/value.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
const float kUpb_FltInfinity = INFINITY;
const double kUpb_Infinity = INFINITY;
const double kUpb_NaN = NAN;
static const size_t message_overhead = sizeof(upb_Message_Internal);
static const size_t overhead = sizeof(upb_Message_InternalData);
upb_Message* upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena) {
return _upb_Message_New(mini_table, arena);
upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* a) {
return _upb_Message_New(m, a);
}
static bool realloc_internal(upb_Message* msg, size_t need, upb_Arena* arena) {
upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (!in->internal) {
/* No internal data, allocate from scratch. */
size_t size = UPB_MAX(128, upb_Log2CeilingSize(need + overhead));
upb_Message_InternalData* internal = upb_Arena_Malloc(arena, size);
if (!internal) return false;
internal->size = size;
internal->unknown_end = overhead;
internal->ext_begin = size;
in->internal = internal;
} else if (in->internal->ext_begin - in->internal->unknown_end < need) {
/* Internal data is too small, reallocate. */
size_t new_size = upb_Log2CeilingSize(in->internal->size + need);
size_t ext_bytes = in->internal->size - in->internal->ext_begin;
size_t new_ext_begin = new_size - ext_bytes;
upb_Message_InternalData* internal =
upb_Arena_Realloc(arena, in->internal, in->internal->size, new_size);
if (!internal) return false;
if (ext_bytes) {
/* Need to move extension data to the end. */
char* ptr = (char*)internal;
memmove(ptr + new_ext_begin, ptr + internal->ext_begin, ext_bytes);
}
internal->ext_begin = new_ext_begin;
internal->size = new_size;
in->internal = internal;
}
UPB_ASSERT(in->internal->ext_begin - in->internal->unknown_end >= need);
return true;
}
bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena) {
if (!realloc_internal(msg, len, arena)) return false;
upb_Message_Internal* in = upb_Message_Getinternal(msg);
memcpy(UPB_PTR_AT(in->internal, in->internal->unknown_end, char), data, len);
in->internal->unknown_end += len;
bool UPB_PRIVATE(_upb_Message_AddUnknown)(upb_Message* msg, const char* data,
size_t len, upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, len, arena)) return false;
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
memcpy(UPB_PTR_AT(in, in->unknown_end, char), data, len);
in->unknown_end += len;
return true;
}
void _upb_Message_DiscardUnknown_shallow(upb_Message* msg) {
upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (in->internal) {
in->internal->unknown_end = overhead;
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (in) {
in->unknown_end = message_overhead;
}
}
const char* upb_Message_GetUnknown(const upb_Message* msg, size_t* len) {
const upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (in->internal) {
*len = in->internal->unknown_end - overhead;
return (char*)(in->internal + 1);
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (in) {
*len = in->unknown_end - message_overhead;
return (char*)(in + 1);
} else {
*len = 0;
return NULL;
@@ -86,9 +64,10 @@ const char* upb_Message_GetUnknown(const upb_Message* msg, size_t* len) {
}
void upb_Message_DeleteUnknown(upb_Message* msg, const char* data, size_t len) {
upb_Message_Internal* in = upb_Message_Getinternal(msg);
const char* internal_unknown_end =
UPB_PTR_AT(in->internal, in->internal->unknown_end, char);
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
const char* internal_unknown_end = UPB_PTR_AT(in, in->unknown_end, char);
#ifndef NDEBUG
size_t full_unknown_size;
const char* full_unknown = upb_Message_GetUnknown(msg, &full_unknown_size);
@@ -97,58 +76,82 @@ void upb_Message_DeleteUnknown(upb_Message* msg, const char* data, size_t len) {
UPB_ASSERT((uintptr_t)(data + len) > (uintptr_t)data);
UPB_ASSERT((uintptr_t)(data + len) <= (uintptr_t)internal_unknown_end);
#endif
if ((data + len) != internal_unknown_end) {
memmove((char*)data, data + len, internal_unknown_end - data - len);
}
in->internal->unknown_end -= len;
}
const upb_Message_Extension* _upb_Message_Getexts(const upb_Message* msg,
size_t* count) {
const upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (in->internal) {
*count = (in->internal->size - in->internal->ext_begin) /
sizeof(upb_Message_Extension);
return UPB_PTR_AT(in->internal, in->internal->ext_begin, void);
} else {
*count = 0;
return NULL;
}
}
const upb_Message_Extension* _upb_Message_Getext(
const upb_Message* msg, const upb_MiniTableExtension* e) {
size_t n;
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &n);
/* For now we use linear search exclusively to find extensions. If this
* becomes an issue due to messages with lots of extensions, we can introduce
* a table of some sort. */
for (size_t i = 0; i < n; i++) {
if (ext[i].ext == e) {
return &ext[i];
}
}
return NULL;
}
upb_Message_Extension* _upb_Message_GetOrCreateExtension(
upb_Message* msg, const upb_MiniTableExtension* e, upb_Arena* arena) {
upb_Message_Extension* ext =
(upb_Message_Extension*)_upb_Message_Getext(msg, e);
if (ext) return ext;
if (!realloc_internal(msg, sizeof(upb_Message_Extension), arena)) return NULL;
upb_Message_Internal* in = upb_Message_Getinternal(msg);
in->internal->ext_begin -= sizeof(upb_Message_Extension);
ext = UPB_PTR_AT(in->internal, in->internal->ext_begin, void);
memset(ext, 0, sizeof(upb_Message_Extension));
ext->ext = e;
return ext;
in->unknown_end -= len;
}
size_t upb_Message_ExtensionCount(const upb_Message* msg) {
size_t count;
_upb_Message_Getexts(msg, &count);
UPB_PRIVATE(_upb_Message_Getexts)(msg, &count);
return count;
}
void upb_Message_Freeze(upb_Message* msg, const upb_MiniTable* m) {
if (upb_Message_IsFrozen(msg)) return;
UPB_PRIVATE(_upb_Message_ShallowFreeze)(msg);
// Base Fields.
const size_t field_count = upb_MiniTable_FieldCount(m);
for (size_t i = 0; i < field_count; i++) {
const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i);
const upb_MiniTable* m2 = upb_MiniTable_SubMessage(m, f);
switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f)) {
case kUpb_FieldMode_Array: {
upb_Array* arr = upb_Message_GetMutableArray(msg, f);
if (arr) upb_Array_Freeze(arr, m2);
break;
}
case kUpb_FieldMode_Map: {
upb_Map* map = upb_Message_GetMutableMap(msg, f);
if (map) {
const upb_MiniTableField* f2 = upb_MiniTable_MapValue(m2);
const upb_MiniTable* m3 = upb_MiniTable_SubMessage(m2, f2);
upb_Map_Freeze(map, m3);
}
break;
}
case kUpb_FieldMode_Scalar: {
if (m2) {
upb_Message* msg2 = upb_Message_GetMutableMessage(msg, f);
if (msg2) upb_Message_Freeze(msg2, m2);
}
break;
}
}
}
// Extensions.
size_t ext_count;
const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getexts)(msg, &ext_count);
for (size_t i = 0; i < ext_count; i++) {
const upb_MiniTableExtension* e = ext[i].ext;
const upb_MiniTableField* f = &e->UPB_PRIVATE(field);
const upb_MiniTable* m2 = upb_MiniTableExtension_GetSubMessage(e);
upb_MessageValue val;
memcpy(&val, &ext[i].data, sizeof(upb_MessageValue));
switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f)) {
case kUpb_FieldMode_Array: {
upb_Array* arr = (upb_Array*)val.array_val;
if (arr) upb_Array_Freeze(arr, m2);
break;
}
case kUpb_FieldMode_Map:
UPB_UNREACHABLE(); // Maps cannot be extensions.
break;
case kUpb_FieldMode_Scalar:
if (upb_MiniTableField_IsSubMessage(f)) {
upb_Message* msg2 = (upb_Message*)val.msg_val;
if (msg2) upb_Message_Freeze(msg2, m2);
}
break;
}
}
}

View File

@@ -15,24 +15,21 @@
#include <stddef.h>
#include "upb/mem/arena.h"
#include "upb/message/types.h" // IWYU pragma: export
#include "upb/message/internal/message.h"
#include "upb/message/internal/types.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_Message upb_Message;
#ifdef __cplusplus
extern "C" {
#endif
// Creates a new message with the given mini_table on the given arena.
UPB_API upb_Message* upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena);
// Adds unknown data (serialized protobuf data) to the given message.
// The data is copied into the message instance.
void upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena);
UPB_API upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* arena);
// Returns a reference to the message's unknown data.
const char* upb_Message_GetUnknown(const upb_Message* msg, size_t* len);
@@ -43,6 +40,20 @@ void upb_Message_DeleteUnknown(upb_Message* msg, const char* data, size_t len);
// Returns the number of extensions present in this message.
size_t upb_Message_ExtensionCount(const upb_Message* msg);
// Mark a message and all of its descendents as frozen/immutable.
UPB_API void upb_Message_Freeze(upb_Message* msg, const upb_MiniTable* m);
// Returns whether a message has been frozen.
UPB_API_INLINE bool upb_Message_IsFrozen(const upb_Message* msg);
#ifdef UPB_TRACING_ENABLED
UPB_API void upb_Message_LogNewMessage(const upb_MiniTable* m,
const upb_Arena* arena);
UPB_API void upb_Message_SetNewMessageTraceHandler(
void (*handler)(const upb_MiniTable* m, const upb_Arena* arena));
#endif // UPB_TRACING_ENABLED
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -5,60 +5,39 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_TABLE_TYPES_H_
#define UPB_MINI_TABLE_TYPES_H_
#ifndef UPB_MINI_TABLE_TAGGED_PTR_H_
#define UPB_MINI_TABLE_TAGGED_PTR_H_
#include <stdint.h>
#include "upb/message/types.h" // IWYU pragma: export
#include "upb/message/internal/tagged_ptr.h"
#include "upb/message/message.h"
// Must be last.
#include "upb/port/def.inc"
// When a upb_Message* is stored in a message, array, or map, it is stored in a
// tagged form. If the tag bit is set, the referenced upb_Message is of type
// _kUpb_MiniTable_Empty (a sentinel message type with no fields) instead of
// that field's true message type. This forms the basis of what we call
// "dynamic tree shaking."
//
// See the documentation for kUpb_DecodeOption_ExperimentalAllowUnlinked for
// more information.
typedef uintptr_t upb_TaggedMessagePtr;
#ifdef __cplusplus
extern "C" {
#endif
// When a upb_Message* is stored in a message, array, or map, it is stored in a
// tagged form. If the tag bit is set, the referenced upb_Message is of type
// _kUpb_MiniTable_Empty (a sentinel message type with no fields) instead of
// that field's true message type. This forms the basis of what we call
// "dynamic tree shaking."
//
// See the documentation for kUpb_DecodeOption_ExperimentalAllowUnlinked for
// more information.
typedef uintptr_t upb_TaggedMessagePtr;
// Internal-only because empty messages cannot be created by the user.
UPB_INLINE upb_TaggedMessagePtr _upb_TaggedMessagePtr_Pack(upb_Message* ptr,
bool empty) {
UPB_ASSERT(((uintptr_t)ptr & 1) == 0);
return (uintptr_t)ptr | (empty ? 1 : 0);
}
// Users who enable unlinked sub-messages must use this to test whether a
// message is empty before accessing it. If a message is empty, it must be
// message is empty before accessing it. If a message is empty, it must be
// first promoted using the interfaces in message/promote.h.
UPB_INLINE bool upb_TaggedMessagePtr_IsEmpty(upb_TaggedMessagePtr ptr) {
return ptr & 1;
}
UPB_API_INLINE bool upb_TaggedMessagePtr_IsEmpty(upb_TaggedMessagePtr ptr);
UPB_INLINE upb_Message* _upb_TaggedMessagePtr_GetMessage(
upb_TaggedMessagePtr ptr) {
return (upb_Message*)(ptr & ~(uintptr_t)1);
}
UPB_INLINE upb_Message* upb_TaggedMessagePtr_GetNonEmptyMessage(
upb_TaggedMessagePtr ptr) {
UPB_ASSERT(!upb_TaggedMessagePtr_IsEmpty(ptr));
return _upb_TaggedMessagePtr_GetMessage(ptr);
}
UPB_INLINE upb_Message* _upb_TaggedMessagePtr_GetEmptyMessage(
upb_TaggedMessagePtr ptr) {
UPB_ASSERT(upb_TaggedMessagePtr_IsEmpty(ptr));
return _upb_TaggedMessagePtr_GetMessage(ptr);
}
UPB_API_INLINE upb_Message* upb_TaggedMessagePtr_GetNonEmptyMessage(
upb_TaggedMessagePtr ptr);
#ifdef __cplusplus
} /* extern "C" */
@@ -66,4 +45,4 @@ UPB_INLINE upb_Message* _upb_TaggedMessagePtr_GetEmptyMessage(
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_TYPES_H_ */
#endif /* UPB_MINI_TABLE_TAGGED_PTR_H_ */

View File

@@ -1,15 +0,0 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_TYPES_H_
#define UPB_MESSAGE_TYPES_H_
// This typedef is in a leaf header to resolve a circular dependency between
// messages and mini tables.
typedef void upb_Message;
#endif /* UPB_MESSAGE_TYPES_H_ */

View File

@@ -12,10 +12,16 @@
#define UPB_MESSAGE_VALUE_H_
#include <stdint.h>
#include <string.h>
#include "upb/base/string_view.h"
#include "upb/message/tagged_ptr.h"
#include "upb/message/types.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
typedef union {
bool bool_val;
@@ -27,20 +33,38 @@ typedef union {
uint64_t uint64_val;
const struct upb_Array* array_val;
const struct upb_Map* map_val;
const upb_Message* msg_val;
const struct upb_Message* msg_val;
upb_StringView str_val;
// EXPERIMENTAL: A tagged upb_Message*. Users must use this instead of
// msg_val if unlinked sub-messages may possibly be in use. See the
// documentation in kUpb_DecodeOption_ExperimentalAllowUnlinked for more
// information.
upb_TaggedMessagePtr tagged_msg_val;
uintptr_t tagged_msg_val; // upb_TaggedMessagePtr
} upb_MessageValue;
UPB_API_INLINE upb_MessageValue upb_MessageValue_Zero(void) {
upb_MessageValue zero;
memset(&zero, 0, sizeof(zero));
return zero;
}
typedef union {
struct upb_Array* array;
struct upb_Map* map;
upb_Message* msg;
struct upb_Message* msg;
} upb_MutableMessageValue;
UPB_API_INLINE upb_MutableMessageValue upb_MutableMessageValue_Zero(void) {
upb_MutableMessageValue zero;
memset(&zero, 0, sizeof(zero));
return zero;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_VALUE_H_ */

View File

@@ -1,86 +0,0 @@
load(
"//bazel:build_defs.bzl",
"UPB_DEFAULT_COPTS",
"UPB_DEFAULT_CPPOPTS",
)
cc_library(
name = "mini_descriptor",
srcs = [
"build_enum.c",
"decode.c",
"link.c",
],
hdrs = [
"build_enum.h",
"decode.h",
"link.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":internal",
"//upb:base",
"//upb:mem",
"//upb:mini_table",
"//upb:mini_table_internal",
"//upb:port",
],
)
cc_library(
name = "internal",
srcs = [
"internal/base92.c",
"internal/encode.c",
],
hdrs = [
"internal/base92.h",
"internal/decoder.h",
"internal/encode.h",
"internal/encode.hpp",
"internal/modifiers.h",
"internal/wire_constants.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
"//upb:base",
"//upb:base_internal",
"//upb:port",
],
)
cc_test(
name = "encode_test",
srcs = ["internal/encode_test.cc"],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":internal",
":mini_descriptor",
"//:protobuf",
"@com_google_googletest//:gtest_main",
"//upb:base",
"//upb:mem",
"//upb:message_accessors_internal",
"//upb:mini_table",
"//upb:wire",
"@com_google_absl//absl/container:flat_hash_set",
],
)
# begin:github_only
filegroup(
name = "source_files",
srcs = glob(
[
"**/*.c",
"**/*.h",
],
),
visibility = [
"//upb/cmake:__pkg__",
"//python/dist:__pkg__",
]
)
# end:github_only

View File

@@ -7,8 +7,15 @@
#include "upb/mini_descriptor/build_enum.h"
#include <stddef.h>
#include <stdint.h>
#include "upb/base/status.h"
#include "upb/mem/arena.h"
#include "upb/mini_descriptor/internal/base92.h"
#include "upb/mini_descriptor/internal/decoder.h"
#include "upb/mini_descriptor/internal/wire_constants.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/internal/enum.h"
// Must be last.
@@ -36,26 +43,27 @@ static upb_MiniTableEnum* _upb_MiniTable_AddEnumDataMember(upb_MdEnumDecoder* d,
d->enum_table = upb_Arena_Realloc(d->arena, d->enum_table, old_sz, new_sz);
upb_MdDecoder_CheckOutOfMemory(&d->base, d->enum_table);
}
d->enum_table->data[d->enum_data_count++] = val;
d->enum_table->UPB_PRIVATE(data)[d->enum_data_count++] = val;
return d->enum_table;
}
static void upb_MiniTableEnum_BuildValue(upb_MdEnumDecoder* d, uint32_t val) {
upb_MiniTableEnum* table = d->enum_table;
d->enum_value_count++;
if (table->value_count || (val > 512 && d->enum_value_count < val / 32)) {
if (table->value_count == 0) {
assert(d->enum_data_count == table->mask_limit / 32);
if (table->UPB_PRIVATE(value_count) ||
(val > 512 && d->enum_value_count < val / 32)) {
if (table->UPB_PRIVATE(value_count) == 0) {
UPB_ASSERT(d->enum_data_count == table->UPB_PRIVATE(mask_limit) / 32);
}
table = _upb_MiniTable_AddEnumDataMember(d, val);
table->value_count++;
table->UPB_PRIVATE(value_count)++;
} else {
uint32_t new_mask_limit = ((val / 32) + 1) * 32;
while (table->mask_limit < new_mask_limit) {
while (table->UPB_PRIVATE(mask_limit) < new_mask_limit) {
table = _upb_MiniTable_AddEnumDataMember(d, 0);
table->mask_limit += 32;
table->UPB_PRIVATE(mask_limit) += 32;
}
table->data[val / 32] |= 1ULL << (val % 32);
table->UPB_PRIVATE(data)[val / 32] |= 1ULL << (val % 32);
}
}
@@ -73,11 +81,11 @@ static upb_MiniTableEnum* upb_MtDecoder_DoBuildMiniTableEnum(
upb_MdDecoder_CheckOutOfMemory(&d->base, d->enum_table);
// Guarantee at least 64 bits of mask without checking mask size.
d->enum_table->mask_limit = 64;
d->enum_table->UPB_PRIVATE(mask_limit) = 64;
d->enum_table = _upb_MiniTable_AddEnumDataMember(d, 0);
d->enum_table = _upb_MiniTable_AddEnumDataMember(d, 0);
d->enum_table->value_count = 0;
d->enum_table->UPB_PRIVATE(value_count) = 0;
const char* ptr = data;
uint32_t base = 0;
@@ -105,14 +113,15 @@ static upb_MiniTableEnum* upb_MtDecoder_DoBuildMiniTableEnum(
}
static upb_MiniTableEnum* upb_MtDecoder_BuildMiniTableEnum(
upb_MdEnumDecoder* const decoder, const char* const data, size_t const len) {
upb_MdEnumDecoder* const decoder, const char* const data,
size_t const len) {
if (UPB_SETJMP(decoder->base.err) != 0) return NULL;
return upb_MtDecoder_DoBuildMiniTableEnum(decoder, data, len);
}
upb_MiniTableEnum* upb_MiniDescriptor_BuildEnum(const char* data, size_t len,
upb_Arena* arena,
upb_Status* status) {
upb_MiniTableEnum* upb_MiniTableEnum_Build(const char* data, size_t len,
upb_Arena* arena,
upb_Status* status) {
upb_MdEnumDecoder decoder = {
.base =
{

View File

@@ -4,6 +4,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_DESCRIPTOR_BUILD_ENUM_H_
#define UPB_MINI_DESCRIPTOR_BUILD_ENUM_H_
@@ -18,20 +19,11 @@
extern "C" {
#endif
// Builds a upb_MiniTableEnum from an enum MiniDescriptor. The MiniDescriptor
// must be for an enum, not a message.
UPB_API upb_MiniTableEnum* upb_MiniDescriptor_BuildEnum(const char* data,
size_t len,
upb_Arena* arena,
upb_Status* status);
// TODO: Deprecated name; update callers.
UPB_API_INLINE upb_MiniTableEnum* upb_MiniTableEnum_Build(const char* data,
size_t len,
upb_Arena* arena,
upb_Status* status) {
return upb_MiniDescriptor_BuildEnum(data, len, arena, status);
}
// Builds a upb_MiniTableEnum from an enum mini descriptor.
// The mini descriptor must be for an enum, not a message.
UPB_API upb_MiniTableEnum* upb_MiniTableEnum_Build(const char* data, size_t len,
upb_Arena* arena,
upb_Status* status);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -8,20 +8,38 @@
#include "upb/mini_descriptor/decode.h"
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/log2.h"
#include "upb/base/status.h"
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/map_entry.h"
#include "upb/message/internal/types.h"
#include "upb/mini_descriptor/internal/base92.h"
#include "upb/mini_descriptor/internal/decoder.h"
#include "upb/mini_descriptor/internal/modifiers.h"
#include "upb/mini_descriptor/internal/wire_constants.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/message.h"
#include "upb/mini_table/internal/sub.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
// Must be last.
#include "upb/port/def.inc"
// We reserve unused hasbits to make room for upb_Message fields.
#define kUpb_Reserved_Hasbytes sizeof(struct upb_Message)
// 64 is the first hasbit that we currently use.
#define kUpb_Reserved_Hasbits (kUpb_Reserved_Hasbytes * 8)
// Note: we sort by this number when calculating layout order.
typedef enum {
kUpb_LayoutItemType_OneofCase, // Oneof case.
@@ -31,7 +49,7 @@ typedef enum {
kUpb_LayoutItemType_Max = kUpb_LayoutItemType_Field,
} upb_LayoutItemType;
#define kUpb_LayoutItem_IndexSentinel ((uint16_t)-1)
#define kUpb_LayoutItem_IndexSentinel ((uint16_t) - 1)
typedef struct {
// Index of the corresponding field. When this is a oneof field, the field's
@@ -69,7 +87,7 @@ enum PresenceClass {
};
static bool upb_MtDecoder_FieldIsPackable(upb_MiniTableField* field) {
return (field->mode & kUpb_FieldMode_Array) &&
return (field->UPB_PRIVATE(mode) & kUpb_FieldMode_Array) &&
upb_FieldType_IsPackable(field->UPB_PRIVATE(descriptortype));
}
@@ -86,18 +104,18 @@ static void upb_MiniTable_SetTypeAndSub(upb_MiniTableField* field,
if (is_proto3_enum) {
UPB_ASSERT(type == kUpb_FieldType_Enum);
type = kUpb_FieldType_Int32;
field->mode |= kUpb_LabelFlags_IsAlternate;
field->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsAlternate;
} else if (type == kUpb_FieldType_String &&
!(msg_modifiers & kUpb_MessageModifier_ValidateUtf8)) {
type = kUpb_FieldType_Bytes;
field->mode |= kUpb_LabelFlags_IsAlternate;
field->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsAlternate;
}
field->UPB_PRIVATE(descriptortype) = type;
if (upb_MtDecoder_FieldIsPackable(field) &&
(msg_modifiers & kUpb_MessageModifier_DefaultIsPacked)) {
field->mode |= kUpb_LabelFlags_IsPacked;
field->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsPacked;
}
if (type == kUpb_FieldType_Message || type == kUpb_FieldType_Group) {
@@ -164,18 +182,19 @@ static void upb_MiniTable_SetField(upb_MtDecoder* d, uint8_t ch,
int8_t type = _upb_FromBase92(ch);
if (ch >= _upb_ToBase92(kUpb_EncodedType_RepeatedBase)) {
type -= kUpb_EncodedType_RepeatedBase;
field->mode = kUpb_FieldMode_Array;
field->mode |= pointer_rep << kUpb_FieldRep_Shift;
field->offset = kNoPresence;
field->UPB_PRIVATE(mode) = kUpb_FieldMode_Array;
field->UPB_PRIVATE(mode) |= pointer_rep << kUpb_FieldRep_Shift;
field->UPB_PRIVATE(offset) = kNoPresence;
} else {
field->mode = kUpb_FieldMode_Scalar;
field->offset = kHasbitPresence;
field->UPB_PRIVATE(mode) = kUpb_FieldMode_Scalar;
field->UPB_PRIVATE(offset) = kHasbitPresence;
if (type == kUpb_EncodedType_Group || type == kUpb_EncodedType_Message) {
field->mode |= pointer_rep << kUpb_FieldRep_Shift;
field->UPB_PRIVATE(mode) |= pointer_rep << kUpb_FieldRep_Shift;
} else if ((unsigned long)type >= sizeof(kUpb_EncodedToFieldRep)) {
upb_MdDecoder_ErrorJmp(&d->base, "Invalid field type: %d", (int)type);
} else {
field->mode |= kUpb_EncodedToFieldRep[type] << kUpb_FieldRep_Shift;
field->UPB_PRIVATE(mode) |= kUpb_EncodedToFieldRep[type]
<< kUpb_FieldRep_Shift;
}
}
if ((unsigned long)type >= sizeof(kUpb_EncodedToType)) {
@@ -193,42 +212,49 @@ static void upb_MtDecoder_ModifyField(upb_MtDecoder* d,
if (!upb_MtDecoder_FieldIsPackable(field)) {
upb_MdDecoder_ErrorJmp(&d->base,
"Cannot flip packed on unpackable field %" PRIu32,
field->number);
upb_MiniTableField_Number(field));
}
field->mode ^= kUpb_LabelFlags_IsPacked;
field->UPB_PRIVATE(mode) ^= kUpb_LabelFlags_IsPacked;
}
if (field_modifiers & kUpb_EncodedFieldModifier_FlipValidateUtf8) {
if (field->UPB_PRIVATE(descriptortype) != kUpb_FieldType_Bytes ||
!(field->mode & kUpb_LabelFlags_IsAlternate)) {
upb_MdDecoder_ErrorJmp(
&d->base,
"Cannot flip ValidateUtf8 on field %" PRIu32 ", type=%d, mode=%d",
field->number, (int)field->UPB_PRIVATE(descriptortype),
(int)field->mode);
!(field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsAlternate)) {
upb_MdDecoder_ErrorJmp(&d->base,
"Cannot flip ValidateUtf8 on field %" PRIu32
", type=%d, mode=%d",
upb_MiniTableField_Number(field),
(int)field->UPB_PRIVATE(descriptortype),
(int)field->UPB_PRIVATE(mode));
}
field->UPB_PRIVATE(descriptortype) = kUpb_FieldType_String;
field->mode &= ~kUpb_LabelFlags_IsAlternate;
field->UPB_PRIVATE(mode) &= ~kUpb_LabelFlags_IsAlternate;
}
bool singular = field_modifiers & kUpb_EncodedFieldModifier_IsProto3Singular;
bool required = field_modifiers & kUpb_EncodedFieldModifier_IsRequired;
// Validate.
if ((singular || required) && field->offset != kHasbitPresence) {
if ((singular || required) && field->UPB_PRIVATE(offset) != kHasbitPresence) {
upb_MdDecoder_ErrorJmp(&d->base,
"Invalid modifier(s) for repeated field %" PRIu32,
field->number);
upb_MiniTableField_Number(field));
}
if (singular && required) {
upb_MdDecoder_ErrorJmp(
&d->base, "Field %" PRIu32 " cannot be both singular and required",
field->number);
upb_MiniTableField_Number(field));
}
if (singular) field->offset = kNoPresence;
if (singular && upb_MiniTableField_IsSubMessage(field)) {
upb_MdDecoder_ErrorJmp(&d->base,
"Field %" PRIu32 " cannot be a singular submessage",
upb_MiniTableField_Number(field));
}
if (singular) field->UPB_PRIVATE(offset) = kNoPresence;
if (required) {
field->offset = kRequiredPresence;
field->UPB_PRIVATE(offset) = kRequiredPresence;
}
}
@@ -258,8 +284,8 @@ static void upb_MtDecoder_PushOneof(upb_MtDecoder* d, upb_LayoutItem item) {
upb_MtDecoder_PushItem(d, item);
}
size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep,
upb_MiniTablePlatform platform) {
static size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep,
upb_MiniTablePlatform platform) {
static const uint8_t kRepToSize32[] = {
[kUpb_FieldRep_1Byte] = 1,
[kUpb_FieldRep_4Byte] = 4,
@@ -278,8 +304,8 @@ size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep,
: kRepToSize64[rep];
}
size_t upb_MtDecoder_AlignOfRep(upb_FieldRep rep,
upb_MiniTablePlatform platform) {
static size_t upb_MtDecoder_AlignOfRep(upb_FieldRep rep,
upb_MiniTablePlatform platform) {
static const uint8_t kRepToAlign32[] = {
[kUpb_FieldRep_1Byte] = 1,
[kUpb_FieldRep_4Byte] = 4,
@@ -315,7 +341,7 @@ static const char* upb_MtDecoder_DecodeOneofField(upb_MtDecoder* d,
" to oneof, no such field number.",
field_num);
}
if (f->offset != kHasbitPresence) {
if (f->UPB_PRIVATE(offset) != kHasbitPresence) {
upb_MdDecoder_ErrorJmp(
&d->base,
"Cannot add repeated, required, or singular field %" PRIu32
@@ -324,13 +350,13 @@ static const char* upb_MtDecoder_DecodeOneofField(upb_MtDecoder* d,
}
// Oneof storage must be large enough to accommodate the largest member.
int rep = f->mode >> kUpb_FieldRep_Shift;
int rep = f->UPB_PRIVATE(mode) >> kUpb_FieldRep_Shift;
if (upb_MtDecoder_SizeOfRep(rep, d->platform) >
upb_MtDecoder_SizeOfRep(item->rep, d->platform)) {
item->rep = rep;
}
// Prepend this field to the linked list.
f->offset = item->field_index;
f->UPB_PRIVATE(offset) = item->field_index;
item->field_index = (f - d->fields) + kOneofBase;
return ptr;
}
@@ -381,26 +407,30 @@ static const char* upb_MtDecoder_ParseModifier(upb_MtDecoder* d,
static void upb_MtDecoder_AllocateSubs(upb_MtDecoder* d,
upb_SubCounts sub_counts) {
uint32_t total_count = sub_counts.submsg_count + sub_counts.subenum_count;
size_t subs_bytes = sizeof(*d->table->subs) * total_count;
upb_MiniTableSub* subs = upb_Arena_Malloc(d->arena, subs_bytes);
size_t subs_bytes = sizeof(*d->table->UPB_PRIVATE(subs)) * total_count;
size_t ptrs_bytes = sizeof(upb_MiniTable*) * sub_counts.submsg_count;
upb_MiniTableSubInternal* subs = upb_Arena_Malloc(d->arena, subs_bytes);
const upb_MiniTable** subs_ptrs = upb_Arena_Malloc(d->arena, ptrs_bytes);
upb_MdDecoder_CheckOutOfMemory(&d->base, subs);
upb_MdDecoder_CheckOutOfMemory(&d->base, subs_ptrs);
uint32_t i = 0;
for (; i < sub_counts.submsg_count; i++) {
subs[i].submsg = &_kUpb_MiniTable_Empty;
subs_ptrs[i] = UPB_PRIVATE(_upb_MiniTable_Empty)();
subs[i].UPB_PRIVATE(submsg) = &subs_ptrs[i];
}
if (sub_counts.subenum_count) {
upb_MiniTableField* f = d->fields;
upb_MiniTableField* end_f = f + d->table->field_count;
upb_MiniTableField* end_f = f + d->table->UPB_PRIVATE(field_count);
for (; f < end_f; f++) {
if (f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum) {
f->UPB_PRIVATE(submsg_index) += sub_counts.submsg_count;
}
}
for (; i < sub_counts.submsg_count + sub_counts.subenum_count; i++) {
subs[i].subenum = NULL;
subs[i].UPB_PRIVATE(subenum) = NULL;
}
}
d->table->subs = subs;
d->table->UPB_PRIVATE(subs) = subs;
}
static const char* upb_MtDecoder_Parse(upb_MtDecoder* d, const char* ptr,
@@ -424,14 +454,14 @@ static const char* upb_MtDecoder_Parse(upb_MtDecoder* d, const char* ptr,
upb_MiniTableField* field = fields;
*field_count += 1;
fields = (char*)fields + field_size;
field->number = ++last_field_number;
field->UPB_PRIVATE(number) = ++last_field_number;
last_field = field;
upb_MiniTable_SetField(d, ch, field, msg_modifiers, sub_counts);
} else if (kUpb_EncodedValue_MinModifier <= ch &&
ch <= kUpb_EncodedValue_MaxModifier) {
ptr = upb_MtDecoder_ParseModifier(d, ptr, ch, last_field, &msg_modifiers);
if (msg_modifiers & kUpb_MessageModifier_IsExtendable) {
d->table->ext |= kUpb_ExtMode_Extendable;
d->table->UPB_PRIVATE(ext) |= kUpb_ExtMode_Extendable;
}
} else if (ch == kUpb_EncodedValue_End) {
if (!d->table) {
@@ -441,7 +471,7 @@ static const char* upb_MtDecoder_Parse(upb_MtDecoder* d, const char* ptr,
} else if (kUpb_EncodedValue_MinSkip <= ch &&
ch <= kUpb_EncodedValue_MaxSkip) {
if (need_dense_below) {
d->table->dense_below = d->table->field_count;
d->table->UPB_PRIVATE(dense_below) = d->table->UPB_PRIVATE(field_count);
need_dense_below = false;
}
uint32_t skip;
@@ -456,7 +486,7 @@ static const char* upb_MtDecoder_Parse(upb_MtDecoder* d, const char* ptr,
}
if (need_dense_below) {
d->table->dense_below = d->table->field_count;
d->table->UPB_PRIVATE(dense_below) = d->table->UPB_PRIVATE(field_count);
}
return ptr;
@@ -470,18 +500,18 @@ static void upb_MtDecoder_ParseMessage(upb_MtDecoder* d, const char* data,
upb_MdDecoder_CheckOutOfMemory(&d->base, d->fields);
upb_SubCounts sub_counts = {0, 0};
d->table->field_count = 0;
d->table->fields = d->fields;
d->table->UPB_PRIVATE(field_count) = 0;
d->table->UPB_PRIVATE(fields) = d->fields;
upb_MtDecoder_Parse(d, data, len, d->fields, sizeof(*d->fields),
&d->table->field_count, &sub_counts);
&d->table->UPB_PRIVATE(field_count), &sub_counts);
upb_Arena_ShrinkLast(d->arena, d->fields, sizeof(*d->fields) * len,
sizeof(*d->fields) * d->table->field_count);
d->table->fields = d->fields;
sizeof(*d->fields) * d->table->UPB_PRIVATE(field_count));
d->table->UPB_PRIVATE(fields) = d->fields;
upb_MtDecoder_AllocateSubs(d, sub_counts);
}
int upb_MtDecoder_CompareFields(const void* _a, const void* _b) {
static int upb_MtDecoder_CompareFields(const void* _a, const void* _b) {
const upb_LayoutItem* a = _a;
const upb_LayoutItem* b = _b;
// Currently we just sort by:
@@ -497,19 +527,19 @@ int upb_MtDecoder_CompareFields(const void* _a, const void* _b) {
#define UPB_COMBINE(rep, ty, idx) (((rep << type_bits) | ty) << idx_bits) | idx
uint32_t a_packed = UPB_COMBINE(a->rep, a->type, a->field_index);
uint32_t b_packed = UPB_COMBINE(b->rep, b->type, b->field_index);
assert(a_packed != b_packed);
UPB_ASSERT(a_packed != b_packed);
#undef UPB_COMBINE
return a_packed < b_packed ? -1 : 1;
}
static bool upb_MtDecoder_SortLayoutItems(upb_MtDecoder* d) {
// Add items for all non-oneof fields (oneofs were already added).
int n = d->table->field_count;
int n = d->table->UPB_PRIVATE(field_count);
for (int i = 0; i < n; i++) {
upb_MiniTableField* f = &d->fields[i];
if (f->offset >= kOneofBase) continue;
if (f->UPB_PRIVATE(offset) >= kOneofBase) continue;
upb_LayoutItem item = {.field_index = i,
.rep = f->mode >> kUpb_FieldRep_Shift,
.rep = f->UPB_PRIVATE(mode) >> kUpb_FieldRep_Shift,
.type = kUpb_LayoutItemType_Field};
upb_MtDecoder_PushItem(d, item);
}
@@ -528,46 +558,49 @@ static size_t upb_MiniTable_DivideRoundUp(size_t n, size_t d) {
static void upb_MtDecoder_AssignHasbits(upb_MtDecoder* d) {
upb_MiniTable* ret = d->table;
int n = ret->field_count;
int last_hasbit = 0; // 0 cannot be used.
int n = ret->UPB_PRIVATE(field_count);
size_t last_hasbit = kUpb_Reserved_Hasbits - 1;
// First assign required fields, which must have the lowest hasbits.
for (int i = 0; i < n; i++) {
upb_MiniTableField* field = (upb_MiniTableField*)&ret->fields[i];
if (field->offset == kRequiredPresence) {
upb_MiniTableField* field =
(upb_MiniTableField*)&ret->UPB_PRIVATE(fields)[i];
if (field->UPB_PRIVATE(offset) == kRequiredPresence) {
field->presence = ++last_hasbit;
} else if (field->offset == kNoPresence) {
} else if (field->UPB_PRIVATE(offset) == kNoPresence) {
field->presence = 0;
}
}
ret->required_count = last_hasbit;
if (ret->required_count > 63) {
if (last_hasbit > kUpb_Reserved_Hasbits + 63) {
upb_MdDecoder_ErrorJmp(&d->base, "Too many required fields");
}
ret->UPB_PRIVATE(required_count) = last_hasbit - (kUpb_Reserved_Hasbits - 1);
// Next assign non-required hasbit fields.
for (int i = 0; i < n; i++) {
upb_MiniTableField* field = (upb_MiniTableField*)&ret->fields[i];
if (field->offset == kHasbitPresence) {
upb_MiniTableField* field =
(upb_MiniTableField*)&ret->UPB_PRIVATE(fields)[i];
if (field->UPB_PRIVATE(offset) == kHasbitPresence) {
field->presence = ++last_hasbit;
}
}
ret->size = last_hasbit ? upb_MiniTable_DivideRoundUp(last_hasbit + 1, 8) : 0;
ret->UPB_PRIVATE(size) =
last_hasbit ? upb_MiniTable_DivideRoundUp(last_hasbit + 1, 8) : 0;
}
size_t upb_MtDecoder_Place(upb_MtDecoder* d, upb_FieldRep rep) {
static size_t upb_MtDecoder_Place(upb_MtDecoder* d, upb_FieldRep rep) {
size_t size = upb_MtDecoder_SizeOfRep(rep, d->platform);
size_t align = upb_MtDecoder_AlignOfRep(rep, d->platform);
size_t ret = UPB_ALIGN_UP(d->table->size, align);
size_t ret = UPB_ALIGN_UP(d->table->UPB_PRIVATE(size), align);
static const size_t max = UINT16_MAX;
size_t new_size = ret + size;
if (new_size > max) {
upb_MdDecoder_ErrorJmp(
&d->base, "Message size exceeded maximum size of %zu bytes", max);
}
d->table->size = new_size;
d->table->UPB_PRIVATE(size) = new_size;
return ret;
}
@@ -586,9 +619,10 @@ static void upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) {
upb_MiniTableField* f = &d->fields[item->field_index];
while (true) {
f->presence = ~item->offset;
if (f->offset == kUpb_LayoutItem_IndexSentinel) break;
UPB_ASSERT(f->offset - kOneofBase < d->table->field_count);
f = &d->fields[f->offset - kOneofBase];
if (f->UPB_PRIVATE(offset) == kUpb_LayoutItem_IndexSentinel) break;
UPB_ASSERT(f->UPB_PRIVATE(offset) - kOneofBase <
d->table->UPB_PRIVATE(field_count));
f = &d->fields[f->UPB_PRIVATE(offset) - kOneofBase];
}
}
@@ -598,14 +632,14 @@ static void upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) {
switch (item->type) {
case kUpb_LayoutItemType_OneofField:
while (true) {
uint16_t next_offset = f->offset;
f->offset = item->offset;
uint16_t next_offset = f->UPB_PRIVATE(offset);
f->UPB_PRIVATE(offset) = item->offset;
if (next_offset == kUpb_LayoutItem_IndexSentinel) break;
f = &d->fields[next_offset - kOneofBase];
}
break;
case kUpb_LayoutItemType_Field:
f->offset = item->offset;
f->UPB_PRIVATE(offset) = item->offset;
break;
default:
break;
@@ -617,20 +651,21 @@ static void upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) {
//
// On 32-bit we could potentially make this smaller, but there is no
// compelling reason to optimize this right now.
d->table->size = UPB_ALIGN_UP(d->table->size, 8);
d->table->UPB_PRIVATE(size) = UPB_ALIGN_UP(d->table->UPB_PRIVATE(size), 8);
}
static void upb_MtDecoder_ValidateEntryField(upb_MtDecoder* d,
const upb_MiniTableField* f,
uint32_t expected_num) {
const char* name = expected_num == 1 ? "key" : "val";
if (f->number != expected_num) {
const uint32_t f_number = upb_MiniTableField_Number(f);
if (f_number != expected_num) {
upb_MdDecoder_ErrorJmp(&d->base,
"map %s did not have expected number (%d vs %d)",
name, expected_num, (int)f->number);
name, expected_num, f_number);
}
if (upb_IsRepeatedOrMap(f)) {
if (!upb_MiniTableField_IsScalar(f)) {
upb_MdDecoder_ErrorJmp(
&d->base, "map %s cannot be repeated or map, or be in oneof", name);
}
@@ -655,9 +690,9 @@ static void upb_MtDecoder_ParseMap(upb_MtDecoder* d, const char* data,
upb_MtDecoder_ParseMessage(d, data, len);
upb_MtDecoder_AssignHasbits(d);
if (UPB_UNLIKELY(d->table->field_count != 2)) {
if (UPB_UNLIKELY(d->table->UPB_PRIVATE(field_count) != 2)) {
upb_MdDecoder_ErrorJmp(&d->base, "%hu fields in map",
d->table->field_count);
d->table->UPB_PRIVATE(field_count));
UPB_UNREACHABLE();
}
@@ -668,20 +703,16 @@ static void upb_MtDecoder_ParseMap(upb_MtDecoder* d, const char* data,
}
}
upb_MtDecoder_ValidateEntryField(d, &d->table->fields[0], 1);
upb_MtDecoder_ValidateEntryField(d, &d->table->fields[1], 2);
upb_MtDecoder_ValidateEntryField(d, &d->table->UPB_PRIVATE(fields)[0], 1);
upb_MtDecoder_ValidateEntryField(d, &d->table->UPB_PRIVATE(fields)[1], 2);
// Map entries have a pre-determined layout, regardless of types.
// NOTE: sync with mini_table/message_internal.h.
const size_t kv_size = d->platform == kUpb_MiniTablePlatform_32Bit ? 8 : 16;
const size_t hasbit_size = 8;
d->fields[0].offset = hasbit_size;
d->fields[1].offset = hasbit_size + kv_size;
d->table->size = UPB_ALIGN_UP(hasbit_size + kv_size + kv_size, 8);
d->fields[0].UPB_PRIVATE(offset) = offsetof(upb_MapEntry, k);
d->fields[1].UPB_PRIVATE(offset) = offsetof(upb_MapEntry, v);
d->table->UPB_PRIVATE(size) = sizeof(upb_MapEntry);
// Map entries have a special bit set to signal it's a map entry, used in
// upb_MiniTable_SetSubMessage() below.
d->table->ext |= kUpb_ExtMode_IsMapEntry;
d->table->UPB_PRIVATE(ext) |= kUpb_ExtMode_IsMapEntry;
}
static void upb_MtDecoder_ParseMessageSet(upb_MtDecoder* d, const char* data,
@@ -692,12 +723,12 @@ static void upb_MtDecoder_ParseMessageSet(upb_MtDecoder* d, const char* data,
}
upb_MiniTable* ret = d->table;
ret->size = 0;
ret->field_count = 0;
ret->ext = kUpb_ExtMode_IsMessageSet;
ret->dense_below = 0;
ret->table_mask = -1;
ret->required_count = 0;
ret->UPB_PRIVATE(size) = kUpb_Reserved_Hasbytes;
ret->UPB_PRIVATE(field_count) = 0;
ret->UPB_PRIVATE(ext) = kUpb_ExtMode_IsMessageSet;
ret->UPB_PRIVATE(dense_below) = 0;
ret->UPB_PRIVATE(table_mask) = -1;
ret->UPB_PRIVATE(required_count) = 0;
}
static upb_MiniTable* upb_MtDecoder_DoBuildMiniTableWithBuf(
@@ -705,12 +736,17 @@ static upb_MiniTable* upb_MtDecoder_DoBuildMiniTableWithBuf(
size_t* buf_size) {
upb_MdDecoder_CheckOutOfMemory(&decoder->base, decoder->table);
decoder->table->size = 0;
decoder->table->field_count = 0;
decoder->table->ext = kUpb_ExtMode_NonExtendable;
decoder->table->dense_below = 0;
decoder->table->table_mask = -1;
decoder->table->required_count = 0;
decoder->table->UPB_PRIVATE(size) = kUpb_Reserved_Hasbytes;
decoder->table->UPB_PRIVATE(field_count) = 0;
decoder->table->UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable;
decoder->table->UPB_PRIVATE(dense_below) = 0;
decoder->table->UPB_PRIVATE(table_mask) = -1;
decoder->table->UPB_PRIVATE(required_count) = 0;
#if UPB_TRACING_ENABLED
// MiniTables built from MiniDescriptors will not be able to vend the message
// name unless it is explicitly set with upb_MiniTable_SetFullName().
decoder->table->UPB_PRIVATE(full_name) = 0;
#endif
// Strip off and verify the version tag.
if (!len--) goto done;
@@ -797,22 +833,22 @@ static const char* upb_MtDecoder_DoBuildMiniTableExtension(
&count, &sub_counts);
if (!ret || count != 1) return NULL;
upb_MiniTableField* f = &ext->field;
upb_MiniTableField* f = &ext->UPB_PRIVATE(field);
f->mode |= kUpb_LabelFlags_IsExtension;
f->offset = 0;
f->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsExtension;
f->UPB_PRIVATE(offset) = 0;
f->presence = 0;
if (extendee->ext & kUpb_ExtMode_IsMessageSet) {
if (extendee->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMessageSet) {
// Extensions of MessageSet must be messages.
if (!upb_IsSubMessage(f)) return NULL;
if (!upb_MiniTableField_IsSubMessage(f)) return NULL;
// Extensions of MessageSet must be non-repeating.
if ((f->mode & kUpb_FieldMode_Mask) == kUpb_FieldMode_Array) return NULL;
if (upb_MiniTableField_IsArray(f)) return NULL;
}
ext->extendee = extendee;
ext->sub = sub;
ext->UPB_PRIVATE(extendee) = extendee;
ext->UPB_PRIVATE(sub) = sub;
return ret;
}

View File

@@ -11,8 +11,6 @@
#include "upb/base/status.h"
#include "upb/mem/arena.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
// Export the newer headers, for legacy users. New users should include the
@@ -76,8 +74,7 @@ UPB_API upb_MiniTableExtension* _upb_MiniTableExtension_Build(
UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_Build(
const char* data, size_t len, const upb_MiniTable* extendee,
upb_Arena* arena, upb_Status* status) {
upb_MiniTableSub sub;
sub.submsg = NULL;
upb_MiniTableSub sub = upb_MiniTableSub_FromMessage(NULL);
return _upb_MiniTableExtension_Build(
data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status);
}
@@ -85,8 +82,7 @@ UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_Build(
UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildMessage(
const char* data, size_t len, const upb_MiniTable* extendee,
upb_MiniTable* submsg, upb_Arena* arena, upb_Status* status) {
upb_MiniTableSub sub;
sub.submsg = submsg;
upb_MiniTableSub sub = upb_MiniTableSub_FromMessage(submsg);
return _upb_MiniTableExtension_Build(
data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status);
}
@@ -94,8 +90,7 @@ UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildMessage(
UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildEnum(
const char* data, size_t len, const upb_MiniTable* extendee,
upb_MiniTableEnum* subenum, upb_Arena* arena, upb_Status* status) {
upb_MiniTableSub sub;
sub.subenum = subenum;
upb_MiniTableSub sub = upb_MiniTableSub_FromEnum(subenum);
return _upb_MiniTableExtension_Build(
data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status);
}

View File

@@ -7,26 +7,42 @@
#include "upb/mini_descriptor/link.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/message.h"
#include "upb/mini_table/internal/sub.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
// Must be last.
#include "upb/port/def.inc"
bool upb_MiniTable_SetSubMessage(upb_MiniTable* table,
upb_MiniTableField* field,
const upb_MiniTable* sub) {
UPB_ASSERT((uintptr_t)table->fields <= (uintptr_t)field &&
(uintptr_t)field <
(uintptr_t)(table->fields + table->field_count));
UPB_ASSERT((uintptr_t)table->UPB_PRIVATE(fields) <= (uintptr_t)field &&
(uintptr_t)field < (uintptr_t)(table->UPB_PRIVATE(fields) +
table->UPB_PRIVATE(field_count)));
UPB_ASSERT(sub);
const bool sub_is_map = sub->ext & kUpb_ExtMode_IsMapEntry;
const bool sub_is_map = sub->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry;
switch (field->UPB_PRIVATE(descriptortype)) {
case kUpb_FieldType_Message:
if (sub_is_map) {
const bool table_is_map = table->ext & kUpb_ExtMode_IsMapEntry;
const bool table_is_map =
table->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry;
if (UPB_UNLIKELY(table_is_map)) return false;
field->mode = (field->mode & ~kUpb_FieldMode_Mask) | kUpb_FieldMode_Map;
field->UPB_PRIVATE(mode) =
(field->UPB_PRIVATE(mode) & ~kUpb_FieldMode_Mask) |
kUpb_FieldMode_Map;
}
break;
@@ -38,35 +54,35 @@ bool upb_MiniTable_SetSubMessage(upb_MiniTable* table,
return false;
}
upb_MiniTableSub* table_sub =
(void*)&table->subs[field->UPB_PRIVATE(submsg_index)];
int idx = field->UPB_PRIVATE(submsg_index);
upb_MiniTableSubInternal* table_subs = (void*)table->UPB_PRIVATE(subs);
// TODO: Add this assert back once YouTube is updated to not call
// this function repeatedly.
// UPB_ASSERT(table_sub->submsg == &_kUpb_MiniTable_Empty);
table_sub->submsg = sub;
// UPB_ASSERT(UPB_PRIVATE(_upb_MiniTable_IsEmpty)(table_sub->submsg));
memcpy((void*)table_subs[idx].UPB_PRIVATE(submsg), &sub, sizeof(void*));
return true;
}
bool upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTableField* field,
const upb_MiniTableEnum* sub) {
UPB_ASSERT((uintptr_t)table->fields <= (uintptr_t)field &&
(uintptr_t)field <
(uintptr_t)(table->fields + table->field_count));
UPB_ASSERT((uintptr_t)table->UPB_PRIVATE(fields) <= (uintptr_t)field &&
(uintptr_t)field < (uintptr_t)(table->UPB_PRIVATE(fields) +
table->UPB_PRIVATE(field_count)));
UPB_ASSERT(sub);
upb_MiniTableSub* table_sub =
(void*)&table->subs[field->UPB_PRIVATE(submsg_index)];
table_sub->subenum = sub;
(void*)&table->UPB_PRIVATE(subs)[field->UPB_PRIVATE(submsg_index)];
*table_sub = upb_MiniTableSub_FromEnum(sub);
return true;
}
uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* mt,
uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* m,
const upb_MiniTableField** subs) {
uint32_t msg_count = 0;
uint32_t enum_count = 0;
for (int i = 0; i < mt->field_count; i++) {
const upb_MiniTableField* f = &mt->fields[i];
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_CType(f) == kUpb_CType_Message) {
*subs = f;
++subs;
@@ -74,9 +90,9 @@ uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* mt,
}
}
for (int i = 0; i < mt->field_count; i++) {
const upb_MiniTableField* f = &mt->fields[i];
if (upb_MiniTableField_CType(f) == kUpb_CType_Enum) {
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_IsClosedEnum(f)) {
*subs = f;
++subs;
enum_count++;
@@ -89,34 +105,32 @@ uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* mt,
// The list of sub_tables and sub_enums must exactly match the number and order
// of sub-message fields and sub-enum fields given by upb_MiniTable_GetSubList()
// above.
bool upb_MiniTable_Link(upb_MiniTable* mt, const upb_MiniTable** sub_tables,
bool upb_MiniTable_Link(upb_MiniTable* m, const upb_MiniTable** sub_tables,
size_t sub_table_count,
const upb_MiniTableEnum** sub_enums,
size_t sub_enum_count) {
uint32_t msg_count = 0;
uint32_t enum_count = 0;
for (int i = 0; i < mt->field_count; i++) {
upb_MiniTableField* f = (upb_MiniTableField*)&mt->fields[i];
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
upb_MiniTableField* f =
(upb_MiniTableField*)upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_CType(f) == kUpb_CType_Message) {
const upb_MiniTable* sub = sub_tables[msg_count++];
if (msg_count > sub_table_count) return false;
if (sub != NULL) {
if (!upb_MiniTable_SetSubMessage(mt, f, sub)) return false;
}
if (sub && !upb_MiniTable_SetSubMessage(m, f, sub)) return false;
}
}
for (int i = 0; i < mt->field_count; i++) {
upb_MiniTableField* f = (upb_MiniTableField*)&mt->fields[i];
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
upb_MiniTableField* f =
(upb_MiniTableField*)upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_IsClosedEnum(f)) {
const upb_MiniTableEnum* sub = sub_enums[enum_count++];
if (enum_count > sub_enum_count) return false;
if (sub != NULL) {
if (!upb_MiniTable_SetSubEnum(mt, f, sub)) return false;
}
if (sub && !upb_MiniTable_SetSubEnum(m, f, sub)) return false;
}
}
return true;
return (msg_count == sub_table_count) && (enum_count == sub_enum_count);
}

View File

@@ -18,7 +18,7 @@
#include "upb/base/status.h"
#include "upb/mem/arena.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"

View File

@@ -8,6 +8,8 @@
#ifndef UPB_MINI_TABLE_ENUM_H_
#define UPB_MINI_TABLE_ENUM_H_
#include <stdint.h>
#include "upb/mini_table/internal/enum.h"
// Must be last
@@ -20,14 +22,8 @@ extern "C" {
#endif
// Validates enum value against range defined by enum mini table.
UPB_INLINE bool upb_MiniTableEnum_CheckValue(const struct upb_MiniTableEnum* e,
uint32_t val) {
_kUpb_FastEnumCheck_Status status = _upb_MiniTable_CheckEnumValueFast(e, val);
if (UPB_UNLIKELY(status == _kUpb_FastEnumCheck_CannotCheckFast)) {
return _upb_MiniTable_CheckEnumValueSlow(e, val);
}
return status == _kUpb_FastEnumCheck_ValueIsInEnum ? true : false;
}
UPB_API_INLINE bool upb_MiniTableEnum_CheckValue(const upb_MiniTableEnum* e,
uint32_t val);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -8,8 +8,37 @@
#ifndef UPB_MINI_TABLE_EXTENSION_H_
#define UPB_MINI_TABLE_EXTENSION_H_
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/extension.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_MiniTableExtension upb_MiniTableExtension;
#ifdef __cplusplus
extern "C" {
#endif
UPB_API_INLINE upb_CType
upb_MiniTableExtension_CType(const upb_MiniTableExtension* e);
UPB_API_INLINE uint32_t
upb_MiniTableExtension_Number(const upb_MiniTableExtension* e);
UPB_API_INLINE const upb_MiniTable* upb_MiniTableExtension_GetSubMessage(
const upb_MiniTableExtension* e);
UPB_API_INLINE void upb_MiniTableExtension_SetSubMessage(
upb_MiniTableExtension* e, const upb_MiniTable* m);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_EXTENSION_H_ */

View File

@@ -7,8 +7,14 @@
#include "upb/mini_table/extension_registry.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/hash/str_table.h"
#include "upb/mem/arena.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
@@ -36,7 +42,7 @@ upb_ExtensionRegistry* upb_ExtensionRegistry_New(upb_Arena* arena) {
UPB_API bool upb_ExtensionRegistry_Add(upb_ExtensionRegistry* r,
const upb_MiniTableExtension* e) {
char buf[EXTREG_KEY_SIZE];
extreg_key(buf, e->extendee, e->field.number);
extreg_key(buf, e->UPB_PRIVATE(extendee), upb_MiniTableExtension_Number(e));
if (upb_strtable_lookup2(&r->exts, buf, EXTREG_KEY_SIZE, NULL)) return false;
return upb_strtable_insert(&r->exts, buf, EXTREG_KEY_SIZE,
upb_value_constptr(e), r->arena);
@@ -57,12 +63,31 @@ failure:
for (end = e, e = start; e < end; e++) {
const upb_MiniTableExtension* ext = *e;
char buf[EXTREG_KEY_SIZE];
extreg_key(buf, ext->extendee, ext->field.number);
extreg_key(buf, ext->UPB_PRIVATE(extendee),
upb_MiniTableExtension_Number(ext));
upb_strtable_remove2(&r->exts, buf, EXTREG_KEY_SIZE, NULL);
}
return false;
}
#ifdef UPB_LINKARR_DECLARE
UPB_LINKARR_DECLARE(upb_AllExts, upb_MiniTableExtension);
bool upb_ExtensionRegistry_AddAllLinkedExtensions(upb_ExtensionRegistry* r) {
const upb_MiniTableExtension* start = UPB_LINKARR_START(upb_AllExts);
const upb_MiniTableExtension* stop = UPB_LINKARR_STOP(upb_AllExts);
for (const upb_MiniTableExtension* p = start; p < stop; p++) {
// Windows can introduce zero padding, so we have to skip zeroes.
if (upb_MiniTableExtension_Number(p) != 0) {
if (!upb_ExtensionRegistry_Add(r, p)) return false;
}
}
return true;
}
#endif // UPB_LINKARR_DECLARE
const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup(
const upb_ExtensionRegistry* r, const upb_MiniTable* t, uint32_t num) {
char buf[EXTREG_KEY_SIZE];

View File

@@ -71,6 +71,23 @@ bool upb_ExtensionRegistry_AddArray(upb_ExtensionRegistry* r,
const upb_MiniTableExtension** e,
size_t count);
#ifdef UPB_LINKARR_DECLARE
// Adds all extensions linked into the binary into the registry. The set of
// linked extensions is assembled by the linker using linker arrays. This
// will likely not work properly if the extensions are split across multiple
// shared libraries.
//
// Returns true if all extensions were added successfully, false on out of
// memory or if any extensions were already present.
//
// This API is currently not available on MSVC (though it *is* available on
// Windows using clang-cl).
UPB_API bool upb_ExtensionRegistry_AddAllLinkedExtensions(
upb_ExtensionRegistry* r);
#endif // UPB_LINKARR_DECLARE
// Looks up the extension (if any) defined for message type |t| and field
// number |num|. Returns the extension if found, otherwise NULL.
UPB_API const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup(

View File

@@ -8,86 +8,46 @@
#ifndef UPB_MINI_TABLE_FIELD_H_
#define UPB_MINI_TABLE_FIELD_H_
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/message.h"
#include "upb/mini_table/internal/sub.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_MiniTableField upb_MiniTableField;
#ifdef __cplusplus
extern "C" {
#endif
typedef struct upb_MiniTableField upb_MiniTableField;
UPB_API_INLINE upb_CType upb_MiniTableField_CType(const upb_MiniTableField* f);
UPB_API_INLINE upb_FieldType
upb_MiniTableField_Type(const upb_MiniTableField* field) {
if (field->mode & kUpb_LabelFlags_IsAlternate) {
if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Int32) {
return kUpb_FieldType_Enum;
} else if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bytes) {
return kUpb_FieldType_String;
} else {
UPB_ASSERT(false);
}
}
return (upb_FieldType)field->UPB_PRIVATE(descriptortype);
}
UPB_API_INLINE bool upb_MiniTableField_HasPresence(const upb_MiniTableField* f);
UPB_API_INLINE upb_CType upb_MiniTableField_CType(const upb_MiniTableField* f) {
switch (upb_MiniTableField_Type(f)) {
case kUpb_FieldType_Double:
return kUpb_CType_Double;
case kUpb_FieldType_Float:
return kUpb_CType_Float;
case kUpb_FieldType_Int64:
case kUpb_FieldType_SInt64:
case kUpb_FieldType_SFixed64:
return kUpb_CType_Int64;
case kUpb_FieldType_Int32:
case kUpb_FieldType_SFixed32:
case kUpb_FieldType_SInt32:
return kUpb_CType_Int32;
case kUpb_FieldType_UInt64:
case kUpb_FieldType_Fixed64:
return kUpb_CType_UInt64;
case kUpb_FieldType_UInt32:
case kUpb_FieldType_Fixed32:
return kUpb_CType_UInt32;
case kUpb_FieldType_Enum:
return kUpb_CType_Enum;
case kUpb_FieldType_Bool:
return kUpb_CType_Bool;
case kUpb_FieldType_String:
return kUpb_CType_String;
case kUpb_FieldType_Bytes:
return kUpb_CType_Bytes;
case kUpb_FieldType_Group:
case kUpb_FieldType_Message:
return kUpb_CType_Message;
}
UPB_UNREACHABLE();
}
UPB_API_INLINE bool upb_MiniTableField_IsExtension(
const upb_MiniTableField* field) {
return field->mode & kUpb_LabelFlags_IsExtension;
}
UPB_API_INLINE bool upb_MiniTableField_IsArray(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsClosedEnum(
const upb_MiniTableField* field) {
return field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum;
}
const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_HasPresence(
const upb_MiniTableField* field) {
if (upb_MiniTableField_IsExtension(field)) {
return !upb_IsRepeatedOrMap(field);
} else {
return field->presence != 0;
}
}
UPB_API_INLINE bool upb_MiniTableField_IsExtension(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsInOneof(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsMap(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsPacked(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsScalar(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsSubMessage(
const upb_MiniTableField* f);
UPB_API_INLINE uint32_t upb_MiniTableField_Number(const upb_MiniTableField* f);
UPB_API_INLINE upb_FieldType
upb_MiniTableField_Type(const upb_MiniTableField* f);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -8,8 +8,39 @@
#ifndef UPB_MINI_TABLE_FILE_H_
#define UPB_MINI_TABLE_FILE_H_
#include "upb/mini_table/enum.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/internal/file.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_MiniTableFile upb_MiniTableFile;
#ifdef __cplusplus
extern "C" {
#endif
UPB_API_INLINE const upb_MiniTableEnum* upb_MiniTableFile_Enum(
const upb_MiniTableFile* f, int i);
UPB_API_INLINE int upb_MiniTableFile_EnumCount(const upb_MiniTableFile* f);
UPB_API_INLINE const upb_MiniTableExtension* upb_MiniTableFile_Extension(
const upb_MiniTableFile* f, int i);
UPB_API_INLINE int upb_MiniTableFile_ExtensionCount(const upb_MiniTableFile* f);
UPB_API_INLINE const upb_MiniTable* upb_MiniTableFile_Message(
const upb_MiniTableFile* f, int i);
UPB_API_INLINE int upb_MiniTableFile_MessageCount(const upb_MiniTableFile* f);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_FILE_H_ */

View File

@@ -14,35 +14,34 @@
#include "upb/port/def.inc"
struct upb_MiniTableEnum {
uint32_t mask_limit; // Limit enum value that can be tested with mask.
uint32_t value_count; // Number of values after the bitfield.
uint32_t data[]; // Bitmask + enumerated values follow.
uint32_t UPB_PRIVATE(mask_limit); // Highest that can be tested with mask.
uint32_t UPB_PRIVATE(value_count); // Number of values after the bitfield.
uint32_t UPB_PRIVATE(data)[]; // Bitmask + enumerated values follow.
};
typedef enum {
_kUpb_FastEnumCheck_ValueIsInEnum = 0,
_kUpb_FastEnumCheck_ValueIsNotInEnum = 1,
_kUpb_FastEnumCheck_CannotCheckFast = 2,
} _kUpb_FastEnumCheck_Status;
#ifdef __cplusplus
extern "C" {
#endif
UPB_INLINE _kUpb_FastEnumCheck_Status _upb_MiniTable_CheckEnumValueFast(
UPB_API_INLINE bool upb_MiniTableEnum_CheckValue(
const struct upb_MiniTableEnum* e, uint32_t val) {
if (UPB_UNLIKELY(val >= 64)) return _kUpb_FastEnumCheck_CannotCheckFast;
uint64_t mask = e->data[0] | ((uint64_t)e->data[1] << 32);
return (mask & (1ULL << val)) ? _kUpb_FastEnumCheck_ValueIsInEnum
: _kUpb_FastEnumCheck_ValueIsNotInEnum;
}
if (UPB_LIKELY(val < 64)) {
const uint64_t mask =
e->UPB_PRIVATE(data)[0] | ((uint64_t)e->UPB_PRIVATE(data)[1] << 32);
const uint64_t bit = 1ULL << val;
return (mask & bit) != 0;
}
if (UPB_LIKELY(val < e->UPB_PRIVATE(mask_limit))) {
const uint32_t mask = e->UPB_PRIVATE(data)[val / 32];
const uint32_t bit = 1ULL << (val % 32);
return (mask & bit) != 0;
}
UPB_INLINE bool _upb_MiniTable_CheckEnumValueSlow(
const struct upb_MiniTableEnum* e, uint32_t val) {
if (val < e->mask_limit) return e->data[val / 32] & (1ULL << (val % 32));
// OPT: binary search long lists?
const uint32_t* start = &e->data[e->mask_limit / 32];
const uint32_t* limit = &e->data[(e->mask_limit / 32) + e->value_count];
const uint32_t* start =
&e->UPB_PRIVATE(data)[e->UPB_PRIVATE(mask_limit) / 32];
const uint32_t* limit = &e->UPB_PRIVATE(
data)[e->UPB_PRIVATE(mask_limit) / 32 + e->UPB_PRIVATE(value_count)];
for (const uint32_t* p = start; p < limit; p++) {
if (*p == val) return true;
}

View File

@@ -8,6 +8,10 @@
#ifndef UPB_MINI_TABLE_INTERNAL_EXTENSION_H_
#define UPB_MINI_TABLE_INTERNAL_EXTENSION_H_
#include <stddef.h>
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/sub.h"
@@ -16,12 +20,48 @@
struct upb_MiniTableExtension {
// Do not move this field. We need to be able to alias pointers.
struct upb_MiniTableField field;
struct upb_MiniTableField UPB_PRIVATE(field);
const struct upb_MiniTable* extendee;
union upb_MiniTableSub sub; // NULL unless submessage or proto2 enum
const struct upb_MiniTable* UPB_PRIVATE(extendee);
union upb_MiniTableSub UPB_PRIVATE(sub); // NULL unless submsg or proto2 enum
};
#ifdef __cplusplus
extern "C" {
#endif
UPB_API_INLINE upb_CType
upb_MiniTableExtension_CType(const struct upb_MiniTableExtension* e) {
return upb_MiniTableField_CType(&e->UPB_PRIVATE(field));
}
UPB_API_INLINE uint32_t
upb_MiniTableExtension_Number(const struct upb_MiniTableExtension* e) {
return e->UPB_PRIVATE(field).UPB_ONLYBITS(number);
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTableExtension_GetSubMessage(
const struct upb_MiniTableExtension* e) {
if (upb_MiniTableExtension_CType(e) != kUpb_CType_Message) {
return NULL;
}
return upb_MiniTableSub_Message(e->UPB_PRIVATE(sub));
}
UPB_API_INLINE void upb_MiniTableExtension_SetSubMessage(
struct upb_MiniTableExtension* e, const struct upb_MiniTable* m) {
e->UPB_PRIVATE(sub).UPB_PRIVATE(submsg) = m;
}
UPB_INLINE upb_FieldRep UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(
const struct upb_MiniTableExtension* e) {
return UPB_PRIVATE(_upb_MiniTableField_GetRep)(&e->UPB_PRIVATE(field));
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_INTERNAL_EXTENSION_H_ */

View File

@@ -8,17 +8,20 @@
#ifndef UPB_MINI_TABLE_INTERNAL_FIELD_H_
#define UPB_MINI_TABLE_INTERNAL_FIELD_H_
#include <stddef.h>
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/size_log2.h"
// Must be last.
#include "upb/port/def.inc"
// LINT.IfChange(struct_definition)
struct upb_MiniTableField {
uint32_t number;
uint16_t offset;
int16_t presence; // If >0, hasbit_index. If <0, ~oneof_index
uint32_t UPB_ONLYBITS(number);
uint16_t UPB_ONLYBITS(offset);
int16_t presence; // If >0, hasbit_index. If <0, ~oneof_index
// Indexes into `upb_MiniTable.subs`
// Will be set to `kUpb_NoSub` if `descriptortype` != MESSAGE/GROUP/ENUM
@@ -27,10 +30,10 @@ struct upb_MiniTableField {
uint8_t UPB_PRIVATE(descriptortype);
// upb_FieldMode | upb_LabelFlags | (upb_FieldRep << kUpb_FieldRep_Shift)
uint8_t mode;
uint8_t UPB_ONLYBITS(mode);
};
#define kUpb_NoSub ((uint16_t)-1)
#define kUpb_NoSub ((uint16_t) - 1)
typedef enum {
kUpb_FieldMode_Map = 0,
@@ -67,44 +70,150 @@ typedef enum {
#define kUpb_FieldRep_Shift 6
UPB_INLINE upb_FieldRep
_upb_MiniTableField_GetRep(const struct upb_MiniTableField* field) {
return (upb_FieldRep)(field->mode >> kUpb_FieldRep_Shift);
}
#ifdef __cplusplus
extern "C" {
#endif
UPB_INLINE upb_FieldMode
upb_FieldMode_Get(const struct upb_MiniTableField* field) {
return (upb_FieldMode)(field->mode & 3);
UPB_PRIVATE(_upb_MiniTableField_Mode)(const struct upb_MiniTableField* f) {
return (upb_FieldMode)(f->UPB_ONLYBITS(mode) & kUpb_FieldMode_Mask);
}
UPB_INLINE void _upb_MiniTableField_CheckIsArray(
const struct upb_MiniTableField* field) {
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_NativePointer);
UPB_ASSUME(upb_FieldMode_Get(field) == kUpb_FieldMode_Array);
UPB_ASSUME(field->presence == 0);
UPB_INLINE upb_FieldRep
UPB_PRIVATE(_upb_MiniTableField_GetRep)(const struct upb_MiniTableField* f) {
return (upb_FieldRep)(f->UPB_ONLYBITS(mode) >> kUpb_FieldRep_Shift);
}
UPB_INLINE void _upb_MiniTableField_CheckIsMap(
const struct upb_MiniTableField* field) {
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_NativePointer);
UPB_ASSUME(upb_FieldMode_Get(field) == kUpb_FieldMode_Map);
UPB_ASSUME(field->presence == 0);
UPB_API_INLINE bool upb_MiniTableField_IsArray(
const struct upb_MiniTableField* f) {
return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Array;
}
UPB_INLINE bool upb_IsRepeatedOrMap(const struct upb_MiniTableField* field) {
// This works because upb_FieldMode has no value 3.
return !(field->mode & kUpb_FieldMode_Scalar);
UPB_API_INLINE bool upb_MiniTableField_IsMap(
const struct upb_MiniTableField* f) {
return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Map;
}
UPB_INLINE bool upb_IsSubMessage(const struct upb_MiniTableField* field) {
return field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Message ||
field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group;
UPB_API_INLINE bool upb_MiniTableField_IsScalar(
const struct upb_MiniTableField* f) {
return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Scalar;
}
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_IsAlternate)(
const struct upb_MiniTableField* f) {
return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsAlternate) != 0;
}
UPB_API_INLINE bool upb_MiniTableField_IsExtension(
const struct upb_MiniTableField* f) {
return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsExtension) != 0;
}
UPB_API_INLINE bool upb_MiniTableField_IsPacked(
const struct upb_MiniTableField* f) {
return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsPacked) != 0;
}
UPB_API_INLINE upb_FieldType
upb_MiniTableField_Type(const struct upb_MiniTableField* f) {
const upb_FieldType type = (upb_FieldType)f->UPB_PRIVATE(descriptortype);
if (UPB_PRIVATE(_upb_MiniTableField_IsAlternate)(f)) {
if (type == kUpb_FieldType_Int32) return kUpb_FieldType_Enum;
if (type == kUpb_FieldType_Bytes) return kUpb_FieldType_String;
UPB_ASSERT(false);
}
return type;
}
UPB_API_INLINE
upb_CType upb_MiniTableField_CType(const struct upb_MiniTableField* f) {
return upb_FieldType_CType(upb_MiniTableField_Type(f));
}
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(
const struct upb_MiniTableField* f) {
return f->presence > 0;
}
UPB_INLINE char UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(
const struct upb_MiniTableField* f) {
UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f));
const size_t index = f->presence;
return 1 << (index % 8);
}
UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(
const struct upb_MiniTableField* f) {
UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f));
const size_t index = f->presence;
return index / 8;
}
UPB_API_INLINE bool upb_MiniTableField_IsClosedEnum(
const struct upb_MiniTableField* f) {
return f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum;
}
UPB_API_INLINE bool upb_MiniTableField_IsInOneof(
const struct upb_MiniTableField* f) {
return f->presence < 0;
}
UPB_API_INLINE bool upb_MiniTableField_IsSubMessage(
const struct upb_MiniTableField* f) {
return f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Message ||
f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group;
}
UPB_API_INLINE bool upb_MiniTableField_HasPresence(
const struct upb_MiniTableField* f) {
if (upb_MiniTableField_IsExtension(f)) {
return upb_MiniTableField_IsScalar(f);
} else {
return f->presence != 0;
}
}
UPB_API_INLINE uint32_t
upb_MiniTableField_Number(const struct upb_MiniTableField* f) {
return f->UPB_ONLYBITS(number);
}
UPB_INLINE uint16_t
UPB_PRIVATE(_upb_MiniTableField_Offset)(const struct upb_MiniTableField* f) {
return f->UPB_ONLYBITS(offset);
}
UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_OneofOffset)(
const struct upb_MiniTableField* f) {
UPB_ASSERT(upb_MiniTableField_IsInOneof(f));
return ~(ptrdiff_t)f->presence;
}
UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(
const struct upb_MiniTableField* f) {
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_NativePointer);
UPB_ASSUME(upb_MiniTableField_IsArray(f));
UPB_ASSUME(f->presence == 0);
}
UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(
const struct upb_MiniTableField* f) {
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_NativePointer);
UPB_ASSUME(upb_MiniTableField_IsMap(f));
UPB_ASSUME(f->presence == 0);
}
UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_ElemSizeLg2)(
const struct upb_MiniTableField* f) {
const upb_FieldType field_type = upb_MiniTableField_Type(f);
return UPB_PRIVATE(_upb_FieldType_SizeLg2)(field_type);
}
// LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/mini_table_field.ts)
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -8,22 +8,59 @@
#ifndef UPB_MINI_TABLE_INTERNAL_FILE_H_
#define UPB_MINI_TABLE_INTERNAL_FILE_H_
#include "upb/mini_table/internal/enum.h"
#include "upb/mini_table/internal/extension.h"
#include "upb/mini_table/internal/message.h"
// Must be last.
#include "upb/port/def.inc"
struct upb_MiniTableFile {
const struct upb_MiniTable** msgs;
const struct upb_MiniTableEnum** enums;
const struct upb_MiniTableExtension** exts;
int msg_count;
int enum_count;
int ext_count;
const struct upb_MiniTable** UPB_PRIVATE(msgs);
const struct upb_MiniTableEnum** UPB_PRIVATE(enums);
const struct upb_MiniTableExtension** UPB_PRIVATE(exts);
int UPB_PRIVATE(msg_count);
int UPB_PRIVATE(enum_count);
int UPB_PRIVATE(ext_count);
};
#ifdef __cplusplus
extern "C" {
#endif
UPB_API_INLINE int upb_MiniTableFile_EnumCount(
const struct upb_MiniTableFile* f) {
return f->UPB_PRIVATE(enum_count);
}
UPB_API_INLINE int upb_MiniTableFile_ExtensionCount(
const struct upb_MiniTableFile* f) {
return f->UPB_PRIVATE(ext_count);
}
UPB_API_INLINE int upb_MiniTableFile_MessageCount(
const struct upb_MiniTableFile* f) {
return f->UPB_PRIVATE(msg_count);
}
UPB_API_INLINE const struct upb_MiniTableEnum* upb_MiniTableFile_Enum(
const struct upb_MiniTableFile* f, int i) {
UPB_ASSERT(i < f->UPB_PRIVATE(enum_count));
return f->UPB_PRIVATE(enums)[i];
}
UPB_API_INLINE const struct upb_MiniTableExtension* upb_MiniTableFile_Extension(
const struct upb_MiniTableFile* f, int i) {
UPB_ASSERT(i < f->UPB_PRIVATE(ext_count));
return f->UPB_PRIVATE(exts)[i];
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTableFile_Message(
const struct upb_MiniTableFile* f, int i) {
UPB_ASSERT(i < f->UPB_PRIVATE(msg_count));
return f->UPB_PRIVATE(msgs)[i];
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_INTERNAL_FILE_H_ */

View File

@@ -7,13 +7,41 @@
#include "upb/mini_table/internal/message.h"
const struct upb_MiniTable _kUpb_MiniTable_Empty = {
.subs = NULL,
.fields = NULL,
.size = 0,
.field_count = 0,
.ext = kUpb_ExtMode_NonExtendable,
.dense_below = 0,
.table_mask = -1,
.required_count = 0,
#include <stddef.h>
#include "upb/message/internal/types.h"
// Must be last.
#include "upb/port/def.inc"
// A MiniTable for an empty message, used for unlinked sub-messages that are
// built via MiniDescriptors. Messages that use this MiniTable may possibly
// be linked later, in which case this MiniTable will be replaced with a real
// one. This pattern is known as "dynamic tree shaking", and it introduces
// complication because sub-messages may either be the "empty" type or the
// "real" type. A tagged bit indicates the difference.
const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_Empty) = {
.UPB_PRIVATE(subs) = NULL,
.UPB_PRIVATE(fields) = NULL,
.UPB_PRIVATE(size) = sizeof(struct upb_Message),
.UPB_PRIVATE(field_count) = 0,
.UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable,
.UPB_PRIVATE(dense_below) = 0,
.UPB_PRIVATE(table_mask) = -1,
.UPB_PRIVATE(required_count) = 0,
};
// A MiniTable for a statically tree shaken message. Messages that use this
// MiniTable are guaranteed to remain unlinked; unlike the empty message, this
// MiniTable is never replaced, which greatly simplifies everything, because the
// type of a sub-message is always known, without consulting a tagged bit.
const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_StaticallyTreeShaken) = {
.UPB_PRIVATE(subs) = NULL,
.UPB_PRIVATE(fields) = NULL,
.UPB_PRIVATE(size) = sizeof(struct upb_Message),
.UPB_PRIVATE(field_count) = 0,
.UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable,
.UPB_PRIVATE(dense_below) = 0,
.UPB_PRIVATE(table_mask) = -1,
.UPB_PRIVATE(required_count) = 0,
};

View File

@@ -8,15 +8,20 @@
#ifndef UPB_MINI_TABLE_INTERNAL_MESSAGE_H_
#define UPB_MINI_TABLE_INTERNAL_MESSAGE_H_
#include "upb/message/types.h"
#include <stddef.h>
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/sub.h"
// Must be last.
#include "upb/port/def.inc"
struct upb_Decoder;
struct upb_Message;
typedef const char* _upb_FieldParser(struct upb_Decoder* d, const char* ptr,
upb_Message* msg, intptr_t table,
struct upb_Message* msg, intptr_t table,
uint64_t hasbits, uint64_t data);
typedef struct {
uint64_t field_data;
@@ -35,50 +40,160 @@ typedef enum {
kUpb_ExtMode_IsMapEntry = 4,
} upb_ExtMode;
union upb_MiniTableSub;
// upb_MiniTable represents the memory layout of a given upb_MessageDef.
// The members are public so generated code can initialize them,
// but users MUST NOT directly read or write any of its members.
// LINT.IfChange(minitable_struct_definition)
struct upb_MiniTable {
const union upb_MiniTableSub* subs;
const struct upb_MiniTableField* fields;
const upb_MiniTableSubInternal* UPB_PRIVATE(subs);
const struct upb_MiniTableField* UPB_ONLYBITS(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 UPB_PRIVATE(size);
uint16_t field_count;
uint8_t ext; // upb_ExtMode, declared as uint8_t so sizeof(ext) == 1
uint8_t dense_below;
uint8_t table_mask;
uint8_t required_count; // Required fields have the lowest hasbits.
uint16_t UPB_ONLYBITS(field_count);
uint8_t UPB_PRIVATE(ext); // upb_ExtMode, uint8_t here so sizeof(ext) == 1
uint8_t UPB_PRIVATE(dense_below);
uint8_t UPB_PRIVATE(table_mask);
uint8_t UPB_PRIVATE(required_count); // Required fields have the low hasbits.
#ifdef UPB_TRACING_ENABLED
const char* UPB_PRIVATE(full_name);
#endif
#ifdef UPB_FASTTABLE_ENABLED
// To statically initialize the tables of variable length, we need a flexible
// array member, and we need to compile in gnu99 mode (constant initialization
// of flexible array members is a GNU extension, not in C99 unfortunately.
_upb_FastTable_Entry fasttable[];
_upb_FastTable_Entry UPB_PRIVATE(fasttable)[];
#endif
};
// LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/mini_table.ts)
#ifdef __cplusplus
extern "C" {
#endif
// A MiniTable for an empty message, used for unlinked sub-messages.
extern const struct upb_MiniTable _kUpb_MiniTable_Empty;
UPB_INLINE const struct upb_MiniTable* UPB_PRIVATE(
_upb_MiniTable_StrongReference)(const struct upb_MiniTable* mt) {
#if defined(__GNUC__)
__asm__("" : : "r"(mt));
#else
const struct upb_MiniTable* volatile unused = mt;
(void)&unused; // Use address to avoid an extra load of "unused".
#endif
return mt;
}
// Computes a bitmask in which the |l->required_count| lowest bits are set,
// except that we skip the lowest bit (because upb never uses hasbit 0).
UPB_INLINE const struct upb_MiniTable* UPB_PRIVATE(_upb_MiniTable_Empty)(void) {
extern const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_Empty);
return &UPB_PRIVATE(_kUpb_MiniTable_Empty);
}
UPB_API_INLINE int upb_MiniTable_FieldCount(const struct upb_MiniTable* m) {
return m->UPB_ONLYBITS(field_count);
}
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTable_IsEmpty)(
const struct upb_MiniTable* m) {
extern const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_Empty);
return m == &UPB_PRIVATE(_kUpb_MiniTable_Empty);
}
UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_GetFieldByIndex(
const struct upb_MiniTable* m, uint32_t i) {
return &m->UPB_ONLYBITS(fields)[i];
}
UPB_INLINE const struct upb_MiniTable* UPB_PRIVATE(
_upb_MiniTable_GetSubTableByIndex)(const struct upb_MiniTable* m,
uint32_t i) {
return *m->UPB_PRIVATE(subs)[i].UPB_PRIVATE(submsg);
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_SubMessage(
const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
if (upb_MiniTableField_CType(f) != kUpb_CType_Message) {
return NULL;
}
return UPB_PRIVATE(_upb_MiniTable_GetSubTableByIndex)(
m, f->UPB_PRIVATE(submsg_index));
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_GetSubMessageTable(
const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
const struct upb_MiniTable* ret = upb_MiniTable_SubMessage(m, f);
UPB_ASSUME(ret);
return UPB_PRIVATE(_upb_MiniTable_IsEmpty)(ret) ? NULL : ret;
}
UPB_API_INLINE bool upb_MiniTable_FieldIsLinked(
const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
return upb_MiniTable_GetSubMessageTable(m, f) != NULL;
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_MapEntrySubMessage(
const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
UPB_ASSERT(upb_MiniTable_FieldIsLinked(m, f)); // Map entries must be linked.
UPB_ASSERT(upb_MiniTableField_IsMap(f)); // Function precondition.
return upb_MiniTable_SubMessage(m, f);
}
UPB_API_INLINE const struct upb_MiniTableEnum* upb_MiniTable_GetSubEnumTable(
const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
UPB_ASSERT(upb_MiniTableField_CType(f) == kUpb_CType_Enum);
return m->UPB_PRIVATE(subs)[f->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(
subenum);
}
UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_MapKey(
const struct upb_MiniTable* m) {
UPB_ASSERT(upb_MiniTable_FieldCount(m) == 2);
const struct upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, 0);
UPB_ASSERT(upb_MiniTableField_Number(f) == 1);
return f;
}
UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_MapValue(
const struct upb_MiniTable* m) {
UPB_ASSERT(upb_MiniTable_FieldCount(m) == 2);
const struct upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, 1);
UPB_ASSERT(upb_MiniTableField_Number(f) == 2);
return f;
}
// Computes a bitmask in which the |m->required_count| lowest bits are set.
//
// Sample output:
// requiredmask(1) => 0b10 (0x2)
// requiredmask(5) => 0b111110 (0x3e)
UPB_INLINE uint64_t upb_MiniTable_requiredmask(const struct upb_MiniTable* l) {
int n = l->required_count;
assert(0 < n && n <= 63);
return ((1ULL << n) - 1) << 1;
// RequiredMask(1) => 0b1 (0x1)
// RequiredMask(5) => 0b11111 (0x1f)
UPB_INLINE uint64_t
UPB_PRIVATE(_upb_MiniTable_RequiredMask)(const struct upb_MiniTable* m) {
int n = m->UPB_PRIVATE(required_count);
UPB_ASSERT(0 < n && n <= 64);
return (1ULL << n) - 1;
}
#ifdef UPB_TRACING_ENABLED
UPB_INLINE const char* upb_MiniTable_FullName(
const struct upb_MiniTable* mini_table) {
return mini_table->UPB_PRIVATE(full_name);
}
// Initializes tracing proto name from language runtimes that construct
// mini tables dynamically at runtime. The runtime is responsible for passing
// controlling lifetime of name such as storing in same arena as mini_table.
UPB_INLINE void upb_MiniTable_SetFullName(struct upb_MiniTable* mini_table,
const char* full_name) {
mini_table->UPB_PRIVATE(full_name) = full_name;
}
#endif
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -0,0 +1,77 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
///
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_
#define UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_
#include <stddef.h>
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// Return the log2 of the storage size in bytes for a upb_CType
UPB_INLINE int UPB_PRIVATE(_upb_CType_SizeLg2)(upb_CType c_type) {
static const int8_t size[] = {
0, // kUpb_CType_Bool
2, // kUpb_CType_Float
2, // kUpb_CType_Int32
2, // kUpb_CType_UInt32
2, // kUpb_CType_Enum
UPB_SIZE(2, 3), // kUpb_CType_Message
3, // kUpb_CType_Double
3, // kUpb_CType_Int64
3, // kUpb_CType_UInt64
UPB_SIZE(3, 4), // kUpb_CType_String
UPB_SIZE(3, 4), // kUpb_CType_Bytes
};
// -1 here because the enum is one-based but the table is zero-based.
return size[c_type - 1];
}
// Return the log2 of the storage size in bytes for a upb_FieldType
UPB_INLINE int UPB_PRIVATE(_upb_FieldType_SizeLg2)(upb_FieldType field_type) {
static const int8_t size[] = {
3, // kUpb_FieldType_Double
2, // kUpb_FieldType_Float
3, // kUpb_FieldType_Int64
3, // kUpb_FieldType_UInt64
2, // kUpb_FieldType_Int32
3, // kUpb_FieldType_Fixed64
2, // kUpb_FieldType_Fixed32
0, // kUpb_FieldType_Bool
UPB_SIZE(3, 4), // kUpb_FieldType_String
UPB_SIZE(2, 3), // kUpb_FieldType_Group
UPB_SIZE(2, 3), // kUpb_FieldType_Message
UPB_SIZE(3, 4), // kUpb_FieldType_Bytes
2, // kUpb_FieldType_UInt32
2, // kUpb_FieldType_Enum
2, // kUpb_FieldType_SFixed32
3, // kUpb_FieldType_SFixed64
2, // kUpb_FieldType_SInt32
3, // kUpb_FieldType_SInt64
};
// -1 here because the enum is one-based but the table is zero-based.
return size[field_type - 1];
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_ */

View File

@@ -4,15 +4,55 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_TABLE_INTERNAL_SUB_H_
#define UPB_MINI_TABLE_INTERNAL_SUB_H_
#include "upb/mini_table/internal/enum.h"
#include "upb/mini_table/internal/message.h"
// Must be last.
#include "upb/port/def.inc"
typedef union {
const struct upb_MiniTable* const* UPB_PRIVATE(submsg);
const struct upb_MiniTableEnum* UPB_PRIVATE(subenum);
} upb_MiniTableSubInternal;
union upb_MiniTableSub {
const struct upb_MiniTable* submsg;
const struct upb_MiniTableEnum* subenum;
const struct upb_MiniTable* UPB_PRIVATE(submsg);
const struct upb_MiniTableEnum* UPB_PRIVATE(subenum);
};
#ifdef __cplusplus
extern "C" {
#endif
UPB_API_INLINE union upb_MiniTableSub upb_MiniTableSub_FromEnum(
const struct upb_MiniTableEnum* subenum) {
union upb_MiniTableSub out;
out.UPB_PRIVATE(subenum) = subenum;
return out;
}
UPB_API_INLINE union upb_MiniTableSub upb_MiniTableSub_FromMessage(
const struct upb_MiniTable* submsg) {
union upb_MiniTableSub out;
out.UPB_PRIVATE(submsg) = submsg;
return out;
}
UPB_API_INLINE const struct upb_MiniTableEnum* upb_MiniTableSub_Enum(
const union upb_MiniTableSub sub) {
return sub.UPB_PRIVATE(subenum);
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTableSub_Message(
const union upb_MiniTableSub sub) {
return sub.UPB_PRIVATE(submsg);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_INTERNAL_SUB_H_ */

View File

@@ -8,29 +8,30 @@
#include "upb/mini_table/message.h"
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include "upb/mem/arena.h"
#include "upb/mini_table/internal/message.h"
#include "upb/mini_table/field.h"
// Must be last.
#include "upb/port/def.inc"
const upb_MiniTableField* upb_MiniTable_FindFieldByNumber(
const upb_MiniTable* t, uint32_t number) {
const upb_MiniTable* m, uint32_t number) {
const size_t i = ((size_t)number) - 1; // 0 wraps to SIZE_MAX
// Ideal case: index into dense fields
if (i < t->dense_below) {
UPB_ASSERT(t->fields[i].number == number);
return &t->fields[i];
if (i < m->UPB_PRIVATE(dense_below)) {
UPB_ASSERT(m->UPB_PRIVATE(fields)[i].UPB_PRIVATE(number) == number);
return &m->UPB_PRIVATE(fields)[i];
}
// Slow case: binary search
int lo = t->dense_below;
int hi = t->field_count - 1;
int lo = m->UPB_PRIVATE(dense_below);
int hi = m->UPB_PRIVATE(field_count) - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
uint32_t num = t->fields[mid].number;
uint32_t num = m->UPB_PRIVATE(fields)[mid].UPB_PRIVATE(number);
if (num < number) {
lo = mid + 1;
continue;
@@ -39,23 +40,20 @@ const upb_MiniTableField* upb_MiniTable_FindFieldByNumber(
hi = mid - 1;
continue;
}
return &t->fields[mid];
return &m->UPB_PRIVATE(fields)[mid];
}
return NULL;
}
static bool upb_MiniTable_Is_Oneof(const upb_MiniTableField* f) {
return f->presence < 0;
}
const upb_MiniTableField* upb_MiniTable_GetOneof(const upb_MiniTable* m,
const upb_MiniTableField* f) {
if (UPB_UNLIKELY(!upb_MiniTable_Is_Oneof(f))) {
if (UPB_UNLIKELY(!upb_MiniTableField_IsInOneof(f))) {
return NULL;
}
const upb_MiniTableField* ptr = &m->fields[0];
const upb_MiniTableField* end = &m->fields[m->field_count];
while (++ptr < end) {
const upb_MiniTableField* ptr = &m->UPB_PRIVATE(fields)[0];
const upb_MiniTableField* end =
&m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)];
for (; ptr < end; ptr++) {
if (ptr->presence == (*f).presence) {
return ptr;
}
@@ -66,7 +64,8 @@ const upb_MiniTableField* upb_MiniTable_GetOneof(const upb_MiniTable* m,
bool upb_MiniTable_NextOneofField(const upb_MiniTable* m,
const upb_MiniTableField** f) {
const upb_MiniTableField* ptr = *f;
const upb_MiniTableField* end = &m->fields[m->field_count];
const upb_MiniTableField* end =
&m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)];
while (++ptr < end) {
if (ptr->presence == (*f)->presence) {
*f = ptr;

View File

@@ -15,45 +15,55 @@
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_MiniTable upb_MiniTable;
#ifdef __cplusplus
extern "C" {
#endif
typedef struct upb_MiniTable upb_MiniTable;
UPB_API const upb_MiniTableField* upb_MiniTable_FindFieldByNumber(
const upb_MiniTable* table, uint32_t number);
const upb_MiniTable* m, uint32_t number);
UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_GetFieldByIndex(
const upb_MiniTable* t, uint32_t index) {
return &t->fields[index];
}
const upb_MiniTable* m, uint32_t index);
// Returns the MiniTable for this message field. If the field is unlinked,
// returns NULL.
UPB_API_INLINE int upb_MiniTable_FieldCount(const upb_MiniTable* m);
// DEPRECATED: use upb_MiniTable_SubMessage() instead
// Returns the MiniTable for a message field, NULL if the field is unlinked.
UPB_API_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable(
const upb_MiniTable* mini_table, const upb_MiniTableField* field) {
UPB_ASSERT(upb_MiniTableField_CType(field) == kUpb_CType_Message);
const upb_MiniTable* ret =
mini_table->subs[field->UPB_PRIVATE(submsg_index)].submsg;
UPB_ASSUME(ret);
return ret == &_kUpb_MiniTable_Empty ? NULL : ret;
}
const upb_MiniTable* m, const upb_MiniTableField* f);
// Returns the MiniTableEnum for this enum field. If the field is unlinked,
// Returns the MiniTable for a message field if it is a submessage, otherwise
// returns NULL.
//
// WARNING: if dynamic tree shaking is in use, the return value may be the
// "empty", zero-field placeholder message instead of the real message type.
// If the message is later linked, this function will begin returning the real
// message type.
UPB_API_INLINE const upb_MiniTable* upb_MiniTable_SubMessage(
const upb_MiniTable* m, const upb_MiniTableField* f);
// Returns the MiniTable for a map field. The given field must refer to a map.
UPB_API_INLINE const upb_MiniTable* upb_MiniTable_MapEntrySubMessage(
const upb_MiniTable* m, const upb_MiniTableField* f);
// Returns the MiniTableEnum for a message field, NULL if the field is unlinked.
UPB_API_INLINE const upb_MiniTableEnum* upb_MiniTable_GetSubEnumTable(
const upb_MiniTable* mini_table, const upb_MiniTableField* field) {
UPB_ASSERT(upb_MiniTableField_CType(field) == kUpb_CType_Enum);
return mini_table->subs[field->UPB_PRIVATE(submsg_index)].subenum;
}
const upb_MiniTable* m, const upb_MiniTableField* f);
// Returns the MiniTableField for the key of a map.
UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_MapKey(
const upb_MiniTable* m);
// Returns the MiniTableField for the value of a map.
UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_MapValue(
const upb_MiniTable* m);
// Returns true if this MiniTable field is linked to a MiniTable for the
// sub-message.
UPB_API_INLINE bool upb_MiniTable_MessageFieldIsLinked(
const upb_MiniTable* mini_table, const upb_MiniTableField* field) {
return upb_MiniTable_GetSubMessageTable(mini_table, field) != NULL;
}
UPB_API_INLINE bool upb_MiniTable_FieldIsLinked(const upb_MiniTable* m,
const upb_MiniTableField* f);
// If this field is in a oneof, returns the first field in the oneof.
//

View File

@@ -8,8 +8,39 @@
#ifndef UPB_MINI_TABLE_SUB_H_
#define UPB_MINI_TABLE_SUB_H_
#include "upb/mini_table/enum.h"
#include "upb/mini_table/internal/sub.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
typedef union upb_MiniTableSub upb_MiniTableSub;
#endif /* UPB_MINI_TABLE_INTERNAL_SUB_H_ */
#ifdef __cplusplus
extern "C" {
#endif
// Constructors
UPB_API_INLINE upb_MiniTableSub
upb_MiniTableSub_FromEnum(const upb_MiniTableEnum* subenum);
UPB_API_INLINE upb_MiniTableSub
upb_MiniTableSub_FromMessage(const upb_MiniTable* submsg);
// Getters
UPB_API_INLINE const upb_MiniTableEnum* upb_MiniTableSub_Enum(
upb_MiniTableSub sub);
UPB_API_INLINE const upb_MiniTable* upb_MiniTableSub_Message(
upb_MiniTableSub sub);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_SUB_H_ */

View File

@@ -47,6 +47,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef UINTPTR_MAX
Error, UINTPTR_MAX is undefined
@@ -89,6 +90,12 @@ Error, UINTPTR_MAX is undefined
#define UPB_API_INLINE UPB_INLINE
#endif
#ifdef EXPORT_UPBC
#define UPBC_API UPB_EXPORT
#else
#define UPBC_API
#endif
#define UPB_MALLOC_ALIGN 8
#define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align))
#define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align))
@@ -99,6 +106,13 @@ Error, UINTPTR_MAX is undefined
#define UPB_ALIGN_OF(type) offsetof (struct { char c; type member; }, member)
#endif
#ifdef _MSC_VER
// Some versions of our Windows compiler don't support the C11 syntax.
#define UPB_ALIGN_AS(x) __declspec(align(x))
#else
#define UPB_ALIGN_AS(x) _Alignas(x)
#endif
// Hints to the compiler about likely/unlikely branches.
#if defined (__GNUC__) || defined(__clang__)
#define UPB_LIKELY(x) __builtin_expect((bool)(x), 1)
@@ -110,17 +124,17 @@ Error, UINTPTR_MAX is undefined
// Macros for function attributes on compilers that support them.
#ifdef __GNUC__
#define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
#define UPB_FORCEINLINE __inline__ __attribute__((always_inline)) static
#define UPB_NOINLINE __attribute__((noinline))
#define UPB_NORETURN __attribute__((__noreturn__))
#define UPB_PRINTF(str, first_vararg) __attribute__((format (printf, str, first_vararg)))
#elif defined(_MSC_VER)
#define UPB_NOINLINE
#define UPB_FORCEINLINE
#define UPB_FORCEINLINE static
#define UPB_NORETURN __declspec(noreturn)
#define UPB_PRINTF(str, first_vararg)
#else /* !defined(__GNUC__) */
#define UPB_FORCEINLINE
#define UPB_FORCEINLINE static
#define UPB_NOINLINE
#define UPB_NORETURN
#define UPB_PRINTF(str, first_vararg)
@@ -168,6 +182,9 @@ Error, UINTPTR_MAX is undefined
#ifdef __APPLE__
#define UPB_SETJMP(buf) _setjmp(buf)
#define UPB_LONGJMP(buf, val) _longjmp(buf, val)
#elif defined(WASM_WAMR)
#define UPB_SETJMP(buf) 0
#define UPB_LONGJMP(buf, val) abort()
#else
#define UPB_SETJMP(buf) setjmp(buf)
#define UPB_LONGJMP(buf, val) longjmp(buf, val)
@@ -185,6 +202,12 @@ Error, UINTPTR_MAX is undefined
#define UPB_PRIVATE(x) x##_dont_copy_me__upb_internal_use_only
#ifdef UPB_ALLOW_PRIVATE_ACCESS__FOR_BITS_ONLY
#define UPB_ONLYBITS(x) x
#else
#define UPB_ONLYBITS(x) UPB_PRIVATE(x)
#endif
/* Configure whether fasttable is switched on or not. *************************/
#ifdef __has_attribute
@@ -296,10 +319,10 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
/* Disable proto2 arena behavior (TEMPORARY) **********************************/
#ifdef UPB_DISABLE_PROTO2_ENUM_CHECKING
#define UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3 1
#ifdef UPB_DISABLE_CLOSED_ENUM_CHECKING
#define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 1
#else
#define UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3 0
#define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 0
#endif
#if defined(__cplusplus)
@@ -317,12 +340,103 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
#define UPB_DEPRECATED
#endif
// begin:google_only
// #define UPB_IS_GOOGLE3
// end:google_only
#if defined(UPB_IS_GOOGLE3) && !defined(UPB_BOOTSTRAP_STAGE0)
#if defined(UPB_IS_GOOGLE3) && \
(!defined(UPB_BOOTSTRAP_STAGE) || UPB_BOOTSTRAP_STAGE != 0)
#define UPB_DESC(sym) proto2_##sym
#define UPB_DESC_MINITABLE(sym) &proto2__##sym##_msg_init
#elif defined(UPB_BOOTSTRAP_STAGE) && UPB_BOOTSTRAP_STAGE == 0
#define UPB_DESC(sym) google_protobuf_##sym
#define UPB_DESC_MINITABLE(sym) google__protobuf__##sym##_msg_init()
#else
#define UPB_DESC(sym) google_protobuf_##sym
#define UPB_DESC_MINITABLE(sym) &google__protobuf__##sym##_msg_init
#endif
#undef UPB_IS_GOOGLE3
// Linker arrays combine elements from multiple translation units into a single
// array that can be iterated over at runtime.
//
// It is an alternative to pre-main "registration" functions.
//
// Usage:
//
// // In N translation units.
// UPB_LINKARR_APPEND(foo_array) static int elems[3] = {1, 2, 3};
//
// // At runtime:
// UPB_LINKARR_DECLARE(foo_array, int);
//
// void f() {
// const int* start = UPB_LINKARR_START(foo_array);
// const int* stop = UPB_LINKARR_STOP(foo_array);
// for (const int* p = start; p < stop; p++) {
// // Windows can introduce zero padding, so we have to skip zeroes.
// if (*p != 0) {
// vec.push_back(*p);
// }
// }
// }
#if defined(__ELF__) || defined(__wasm__)
#define UPB_LINKARR_APPEND(name) \
__attribute__((retain, used, section("linkarr_" #name)))
#define UPB_LINKARR_DECLARE(name, type) \
extern type const __start_linkarr_##name; \
extern type const __stop_linkarr_##name; \
UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1]
#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
#elif defined(__MACH__)
/* As described in: https://stackoverflow.com/a/22366882 */
#define UPB_LINKARR_APPEND(name) \
__attribute__((retain, used, section("__DATA,__la_" #name)))
#define UPB_LINKARR_DECLARE(name, type) \
extern type const __start_linkarr_##name __asm( \
"section$start$__DATA$__la_" #name); \
extern type const __stop_linkarr_##name __asm( \
"section$end$__DATA$" \
"__la_" #name); \
UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1]
#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
#elif defined(_MSC_VER) && defined(__clang__)
/* See:
* https://devblogs.microsoft.com/oldnewthing/20181107-00/?p=100155
* https://devblogs.microsoft.com/oldnewthing/20181108-00/?p=100165
* https://devblogs.microsoft.com/oldnewthing/20181109-00/?p=100175 */
// Usage of __attribute__ here probably means this is Clang-specific, and would
// not work on MSVC.
#define UPB_LINKARR_APPEND(name) \
__declspec(allocate("la_" #name "$j")) __attribute__((retain, used))
#define UPB_LINKARR_DECLARE(name, type) \
__declspec(allocate("la_" #name "$a")) type __start_linkarr_##name; \
__declspec(allocate("la_" #name "$z")) type __stop_linkarr_##name; \
UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1] = {0}
#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
#else
// Linker arrays are not supported on this platform. Make appends a no-op but
// don't define the other macros.
#define UPB_LINKARR_APPEND(name)
#endif
// Future versions of upb will include breaking changes to some APIs.
// This macro can be set to enable these API changes ahead of time, so that
// user code can be updated before upgrading versions of protobuf.
#ifdef UPB_FUTURE_BREAKING_CHANGES
// Properly enforce closed enums in python.
// Owner: mkruskal@
#define UPB_FUTURE_PYTHON_CLOSED_ENUM_ENFORCEMENT 1
#endif

View File

@@ -13,11 +13,13 @@
#undef UPB_EXPORT
#undef UPB_INLINE
#undef UPB_API
#undef UPBC_API
#undef UPB_API_INLINE
#undef UPB_ALIGN_UP
#undef UPB_ALIGN_DOWN
#undef UPB_ALIGN_MALLOC
#undef UPB_ALIGN_OF
#undef UPB_ALIGN_AS
#undef UPB_MALLOC_ALIGN
#undef UPB_LIKELY
#undef UPB_UNLIKELY
@@ -44,12 +46,20 @@
#undef UPB_ASAN
#undef UPB_ASAN_GUARD_SIZE
#undef UPB_CLANG_ASAN
#undef UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3
#undef UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN
#undef UPB_DEPRECATED
#undef UPB_GNUC_MIN
#undef UPB_DESCRIPTOR_UPB_H_FILENAME
#undef UPB_DESC
#undef UPB_DESC_MINITABLE
#undef UPB_IS_GOOGLE3
#undef UPB_ATOMIC
#undef UPB_USE_C11_ATOMICS
#undef UPB_PRIVATE
#undef UPB_ONLYBITS
#undef UPB_LINKARR_DECLARE
#undef UPB_LINKARR_APPEND
#undef UPB_LINKARR_START
#undef UPB_LINKARR_STOP
#undef UPB_FUTURE_BREAKING_CHANGES
#undef UPB_FUTURE_PYTHON_CLOSED_ENUM_ENFORCEMENT

View File

@@ -12,17 +12,7 @@
#ifndef UPB_REFLECTION_COMMON_H_
#define UPB_REFLECTION_COMMON_H_
// begin:google_only
// #ifndef UPB_BOOTSTRAP_STAGE0
// #include "net/proto2/proto/descriptor.upb.h"
// #else
// #include "google/protobuf/descriptor.upb.h"
// #endif
// end:google_only
// begin:github_only
#include "google/protobuf/descriptor.upb.h"
// end:github_only
#include "upb/reflection/descriptor_bootstrap.h" // IWYU pragma: export
typedef enum {
kUpb_Syntax_Proto2 = 2,

View File

@@ -8,13 +8,21 @@
#ifndef UPB_REFLECTION_DEF_HPP_
#define UPB_REFLECTION_DEF_HPP_
#include <stdint.h>
#include <cstring>
#include <memory>
#include <string>
#include <vector>
#include "upb/base/descriptor_constants.h"
#include "upb/base/status.hpp"
#include "upb/base/string_view.h"
#include "upb/mem/arena.hpp"
#include "upb/message/value.h"
#include "upb/mini_descriptor/decode.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
#include "upb/reflection/def.h"
#include "upb/reflection/internal/def_pool.h"
#include "upb/reflection/internal/enum_def.h"
@@ -53,6 +61,13 @@ class FieldDefPtr {
return upb_FieldDef_MiniTable(ptr_);
}
std::string MiniDescriptorEncode() const {
upb::Arena arena;
upb_StringView md;
upb_FieldDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md);
return std::string(md.data, md.size);
}
const UPB_DESC(FieldOptions) * options() const {
return upb_FieldDef_Options(ptr_);
}
@@ -80,6 +95,9 @@ class FieldDefPtr {
// been finalized.
uint32_t index() const { return upb_FieldDef_Index(ptr_); }
// Index into msgdef->layout->fields or file->exts
uint32_t layout_index() const { return upb_FieldDef_LayoutIndex(ptr_); }
// The MessageDef to which this field belongs (for extensions, the extended
// message).
MessageDefPtr containing_type() const;
@@ -94,6 +112,7 @@ class FieldDefPtr {
OneofDefPtr real_containing_oneof() const;
// Convenient field type tests.
bool IsEnum() const { return upb_FieldDef_IsEnum(ptr_); }
bool IsSubMessage() const { return upb_FieldDef_IsSubMessage(ptr_); }
bool IsString() const { return upb_FieldDef_IsString(ptr_); }
bool IsSequence() const { return upb_FieldDef_IsRepeated(ptr_); }
@@ -194,6 +213,9 @@ class MessageDefPtr {
const char* full_name() const { return upb_MessageDef_FullName(ptr_); }
const char* name() const { return upb_MessageDef_Name(ptr_); }
// Returns the MessageDef that contains this MessageDef (or null).
MessageDefPtr containing_type() const;
const upb_MiniTable* mini_table() const {
return upb_MessageDef_MiniTable(ptr_);
}
@@ -405,10 +427,14 @@ class EnumDefPtr {
const upb_EnumDef* ptr() const { return ptr_; }
explicit operator bool() const { return ptr_ != nullptr; }
FileDefPtr file() const;
const char* full_name() const { return upb_EnumDef_FullName(ptr_); }
const char* name() const { return upb_EnumDef_Name(ptr_); }
bool is_closed() const { return upb_EnumDef_IsClosed(ptr_); }
// Returns the MessageDef that contains this EnumDef (or null).
MessageDefPtr containing_type() const;
// The value that is used as the default when no field default is specified.
// If not set explicitly, the first value that was added will be used.
// The default value must be a member of the enum.
@@ -497,6 +523,10 @@ class FileDefPtr {
return FieldDefPtr(upb_FileDef_TopLevelExtension(ptr_, index));
}
bool resolves(const char* path) const {
return upb_FileDef_Resolves(ptr_, path);
}
explicit operator bool() const { return ptr_ != nullptr; }
friend bool operator==(FileDefPtr lhs, FileDefPtr rhs) {
@@ -555,8 +585,9 @@ class DefPool {
std::unique_ptr<upb_DefPool, decltype(&upb_DefPool_Free)> ptr_;
};
// TODO: This typedef is deprecated. Delete it.
using SymbolTable = DefPool;
inline FileDefPtr EnumDefPtr::file() const {
return FileDefPtr(upb_EnumDef_File(ptr_));
}
inline FileDefPtr FieldDefPtr::file() const {
return FileDefPtr(upb_FieldDef_File(ptr_));
@@ -566,6 +597,14 @@ inline FileDefPtr MessageDefPtr::file() const {
return FileDefPtr(upb_MessageDef_File(ptr_));
}
inline MessageDefPtr MessageDefPtr::containing_type() const {
return MessageDefPtr(upb_MessageDef_ContainingType(ptr_));
}
inline MessageDefPtr EnumDefPtr::containing_type() const {
return MessageDefPtr(upb_EnumDef_ContainingType(ptr_));
}
inline EnumDefPtr MessageDefPtr::enum_type(int i) const {
return EnumDefPtr(upb_MessageDef_NestedEnum(ptr_, i));
}

View File

@@ -7,9 +7,13 @@
#include "upb/reflection/internal/def_pool.h"
#include "upb/base/status.h"
#include "upb/hash/int_table.h"
#include "upb/hash/str_table.h"
#include "upb/mem/alloc.h"
#include "upb/mem/arena.h"
#include "upb/reflection/def_type.h"
#include "upb/reflection/file_def.h"
#include "upb/reflection/internal/def_builder.h"
#include "upb/reflection/internal/enum_def.h"
#include "upb/reflection/internal/enum_value_def.h"
@@ -17,6 +21,7 @@
#include "upb/reflection/internal/file_def.h"
#include "upb/reflection/internal/message_def.h"
#include "upb/reflection/internal/service_def.h"
#include "upb/reflection/internal/upb_edition_defaults.h"
// Must be last.
#include "upb/port/def.inc"
@@ -27,6 +32,7 @@ struct upb_DefPool {
upb_strtable files; // file_name -> (upb_FileDef*)
upb_inttable exts; // (upb_MiniTableExtension*) -> (upb_FieldDef*)
upb_ExtensionRegistry* extreg;
const UPB_DESC(FeatureSetDefaults) * feature_set_defaults;
upb_MiniTablePlatform platform;
void* scratch_data;
size_t scratch_size;
@@ -39,6 +45,8 @@ void upb_DefPool_Free(upb_DefPool* s) {
upb_gfree(s);
}
static const char serialized_defaults[] = UPB_INTERNAL_UPB_EDITION_DEFAULTS;
upb_DefPool* upb_DefPool_New(void) {
upb_DefPool* s = upb_gmalloc(sizeof(*s));
if (!s) return NULL;
@@ -59,6 +67,14 @@ upb_DefPool* upb_DefPool_New(void) {
s->platform = kUpb_MiniTablePlatform_Native;
upb_Status status;
if (!upb_DefPool_SetFeatureSetDefaults(
s, serialized_defaults, sizeof(serialized_defaults) - 1, &status)) {
goto err;
}
if (!s->feature_set_defaults) goto err;
return s;
err:
@@ -66,6 +82,63 @@ err:
return NULL;
}
const UPB_DESC(FeatureSetDefaults) *
upb_DefPool_FeatureSetDefaults(const upb_DefPool* s) {
return s->feature_set_defaults;
}
bool upb_DefPool_SetFeatureSetDefaults(upb_DefPool* s,
const char* serialized_defaults,
size_t serialized_len,
upb_Status* status) {
const UPB_DESC(FeatureSetDefaults)* defaults = UPB_DESC(
FeatureSetDefaults_parse)(serialized_defaults, serialized_len, s->arena);
if (!defaults) {
upb_Status_SetErrorFormat(status, "Failed to parse defaults");
return false;
}
if (upb_strtable_count(&s->files) > 0) {
upb_Status_SetErrorFormat(status,
"Feature set defaults can't be changed once the "
"pool has started building");
return false;
}
int min_edition = UPB_DESC(FeatureSetDefaults_minimum_edition(defaults));
int max_edition = UPB_DESC(FeatureSetDefaults_maximum_edition(defaults));
if (min_edition > max_edition) {
upb_Status_SetErrorFormat(status, "Invalid edition range %s to %s",
upb_FileDef_EditionName(min_edition),
upb_FileDef_EditionName(max_edition));
return false;
}
size_t size;
const UPB_DESC(
FeatureSetDefaults_FeatureSetEditionDefault)* const* default_list =
UPB_DESC(FeatureSetDefaults_defaults(defaults, &size));
int prev_edition = UPB_DESC(EDITION_UNKNOWN);
for (size_t i = 0; i < size; ++i) {
int edition = UPB_DESC(
FeatureSetDefaults_FeatureSetEditionDefault_edition(default_list[i]));
if (edition == UPB_DESC(EDITION_UNKNOWN)) {
upb_Status_SetErrorFormat(status, "Invalid edition UNKNOWN specified");
return false;
}
if (edition <= prev_edition) {
upb_Status_SetErrorFormat(status,
"Feature set defaults are not strictly "
"increasing, %s is greater than or equal to %s",
upb_FileDef_EditionName(prev_edition),
upb_FileDef_EditionName(edition));
return false;
}
prev_edition = edition;
}
// Copy the defaults into the pool.
s->feature_set_defaults = defaults;
return true;
}
bool _upb_DefPool_InsertExt(upb_DefPool* s, const upb_MiniTableExtension* ext,
const upb_FieldDef* f) {
return upb_inttable_insert(&s->exts, (uintptr_t)ext, upb_value_constptr(f),
@@ -279,7 +352,11 @@ static const upb_FileDef* upb_DefBuilder_AddFileToPool(
remove_filedef(s, builder->file);
builder->file = NULL;
}
} else if (!builder->arena || !builder->tmp_arena) {
} else if (!builder->arena || !builder->tmp_arena ||
!upb_strtable_init(&builder->feature_cache, 16,
builder->tmp_arena) ||
!(builder->legacy_features =
UPB_DESC(FeatureSet_new)(builder->tmp_arena))) {
_upb_DefBuilder_OomErr(builder);
} else {
_upb_FileDef_Create(builder, file_proto);
@@ -312,6 +389,8 @@ static const upb_FileDef* _upb_DefPool_AddFile(
upb_DefBuilder ctx = {
.symtab = s,
.tmp_buf = NULL,
.tmp_buf_size = 0,
.layout = layout,
.platform = s->platform,
.msg_count = 0,
@@ -427,7 +506,7 @@ const upb_FieldDef** upb_DefPool_GetAllExtensions(const upb_DefPool* s,
const upb_FieldDef* f = upb_value_getconstptr(val);
if (upb_FieldDef_ContainingType(f) == m) n++;
}
const upb_FieldDef** exts = malloc(n * sizeof(*exts));
const upb_FieldDef** exts = upb_gmalloc(n * sizeof(*exts));
iter = UPB_INTTABLE_BEGIN;
size_t i = 0;
while (upb_inttable_next(&s->exts, &key, &val, &iter)) {

View File

@@ -26,6 +26,14 @@ UPB_API void upb_DefPool_Free(upb_DefPool* s);
UPB_API upb_DefPool* upb_DefPool_New(void);
UPB_API const UPB_DESC(FeatureSetDefaults) *
upb_DefPool_FeatureSetDefaults(const upb_DefPool* s);
UPB_API bool upb_DefPool_SetFeatureSetDefaults(upb_DefPool* s,
const char* serialized_defaults,
size_t serialized_len,
upb_Status* status);
UPB_API const upb_MessageDef* upb_DefPool_FindMessageByName(
const upb_DefPool* s, const char* sym);
@@ -58,8 +66,8 @@ const upb_FieldDef* upb_DefPool_FindExtensionByNumber(const upb_DefPool* s,
const upb_MessageDef* m,
int32_t fieldnum);
const upb_ServiceDef* upb_DefPool_FindServiceByName(const upb_DefPool* s,
const char* name);
UPB_API const upb_ServiceDef* upb_DefPool_FindServiceByName(
const upb_DefPool* s, const char* name);
const upb_ServiceDef* upb_DefPool_FindServiceByNameWithSize(
const upb_DefPool* s, const char* name, size_t size);

View File

@@ -0,0 +1,19 @@
#ifndef THIRD_PARTY_UPB_UPB_REFLECTION_DESCRIPTOR_BOOTSTRAP_H_
#define THIRD_PARTY_UPB_UPB_REFLECTION_DESCRIPTOR_BOOTSTRAP_H_
// IWYU pragma: begin_exports
#if defined(UPB_BOOTSTRAP_STAGE) && UPB_BOOTSTRAP_STAGE == 0
// This header is checked in.
#include "upb/reflection/stage0/google/protobuf/descriptor.upb.h"
#elif UPB_BOOTSTRAP_STAGE == 1
// This header is generated at build time by the bootstrapping process.
#include "upb/reflection/stage1/google/protobuf/descriptor.upb.h"
#else
// This is the normal header, generated by upb_c_proto_library().
#include "google/protobuf/descriptor.upb.h"
#endif
// IWYU pragma: end_exports
#endif // THIRD_PARTY_UPB_UPB_REFLECTION_DESCRIPTOR_BOOTSTRAP_H_

View File

@@ -7,23 +7,35 @@
#include "upb/reflection/internal/enum_def.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/base/status.h"
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/hash/int_table.h"
#include "upb/hash/str_table.h"
#include "upb/mem/arena.h"
#include "upb/mini_descriptor/decode.h"
#include "upb/mini_descriptor/internal/encode.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/file.h"
#include "upb/reflection/def.h"
#include "upb/reflection/def_type.h"
#include "upb/reflection/internal/def_builder.h"
#include "upb/reflection/internal/desc_state.h"
#include "upb/reflection/internal/enum_reserved_range.h"
#include "upb/reflection/internal/enum_value_def.h"
#include "upb/reflection/internal/file_def.h"
#include "upb/reflection/internal/message_def.h"
#include "upb/reflection/internal/strdup2.h"
// Must be last.
#include "upb/port/def.inc"
struct upb_EnumDef {
const UPB_DESC(EnumOptions) * opts;
const UPB_DESC(EnumOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_MiniTableEnum* layout; // Only for proto2.
const upb_FileDef* file;
const upb_MessageDef* containing_type; // Could be merged with "file".
@@ -37,8 +49,10 @@ struct upb_EnumDef {
int res_range_count;
int res_name_count;
int32_t defaultval;
bool is_closed;
bool is_sorted; // Whether all of the values are defined in ascending order.
#if UINTPTR_MAX == 0xffffffff
uint32_t padding; // Increase size to a multiple of 8.
#endif
};
upb_EnumDef* _upb_EnumDef_At(const upb_EnumDef* e, int i) {
@@ -71,6 +85,11 @@ bool upb_EnumDef_HasOptions(const upb_EnumDef* e) {
return e->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_EnumDef_ResolvedFeatures(const upb_EnumDef* e) {
return e->resolved_features;
}
const char* upb_EnumDef_FullName(const upb_EnumDef* e) { return e->full_name; }
const char* upb_EnumDef_Name(const upb_EnumDef* e) {
@@ -140,7 +159,15 @@ const upb_EnumValueDef* upb_EnumDef_Value(const upb_EnumDef* e, int i) {
return _upb_EnumValueDef_At(e->values, i);
}
bool upb_EnumDef_IsClosed(const upb_EnumDef* e) { return e->is_closed; }
bool upb_EnumDef_IsClosed(const upb_EnumDef* e) {
if (UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN) return false;
return upb_EnumDef_IsSpecifiedAsClosed(e);
}
bool upb_EnumDef_IsSpecifiedAsClosed(const upb_EnumDef* e) {
return UPB_DESC(FeatureSet_enum_type)(e->resolved_features) ==
UPB_DESC(FeatureSet_CLOSED);
}
bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a,
upb_StringView* out) {
@@ -209,6 +236,7 @@ static upb_StringView* _upb_EnumReservedNames_New(
static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(EnumDescriptorProto) * enum_proto,
const UPB_DESC(FeatureSet*) parent_features,
upb_EnumDef* e) {
const UPB_DESC(EnumValueDescriptorProto)* const* values;
const UPB_DESC(EnumDescriptorProto_EnumReservedRange)* const* res_ranges;
@@ -216,6 +244,10 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
upb_StringView name;
size_t n_value, n_res_range, n_res_name;
UPB_DEF_SET_OPTIONS(e->opts, EnumDescriptorProto, EnumOptions, enum_proto);
e->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(EnumOptions_features)(e->opts));
// Must happen before _upb_DefBuilder_Add()
e->file = _upb_DefBuilder_File(ctx);
@@ -225,9 +257,6 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
_upb_DefBuilder_Add(ctx, e->full_name,
_upb_DefType_Pack(e, UPB_DEFTYPE_ENUM));
e->is_closed = (!UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3) &&
(upb_FileDef_Syntax(e->file) == kUpb_Syntax_Proto2);
values = UPB_DESC(EnumDescriptorProto_value)(enum_proto, &n_value);
bool ok = upb_strtable_init(&e->ntoi, n_value, ctx->arena);
@@ -238,8 +267,8 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
e->defaultval = 0;
e->value_count = n_value;
e->values =
_upb_EnumValueDefs_New(ctx, prefix, n_value, values, e, &e->is_sorted);
e->values = _upb_EnumValueDefs_New(ctx, prefix, n_value, values,
e->resolved_features, e, &e->is_sorted);
if (n_value == 0) {
_upb_DefBuilder_Errf(ctx, "enums must contain at least one value (%s)",
@@ -256,14 +285,11 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
e->res_name_count = n_res_name;
e->res_names = _upb_EnumReservedNames_New(ctx, n_res_name, res_names);
UPB_DEF_SET_OPTIONS(e->opts, EnumDescriptorProto, EnumOptions, enum_proto);
upb_inttable_compact(&e->iton, ctx->arena);
if (e->is_closed) {
if (upb_EnumDef_IsClosed(e)) {
if (ctx->layout) {
UPB_ASSERT(ctx->enum_count < ctx->layout->enum_count);
e->layout = ctx->layout->enums[ctx->enum_count++];
e->layout = upb_MiniTableFile_Enum(ctx->layout, ctx->enum_count++);
} else {
e->layout = create_enumlayout(ctx, e);
}
@@ -272,10 +298,11 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
}
}
upb_EnumDef* _upb_EnumDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto) * const* protos,
const upb_MessageDef* containing_type) {
upb_EnumDef* _upb_EnumDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const upb_MessageDef* containing_type) {
_upb_DefType_CheckPadding(sizeof(upb_EnumDef));
// If a containing type is defined then get the full name from that.
@@ -285,7 +312,7 @@ upb_EnumDef* _upb_EnumDefs_New(
upb_EnumDef* e = _upb_DefBuilder_Alloc(ctx, sizeof(upb_EnumDef) * n);
for (int i = 0; i < n; i++) {
create_enumdef(ctx, name, protos[i], &e[i]);
create_enumdef(ctx, name, protos[i], parent_features, &e[i]);
e[i].containing_type = containing_type;
}
return e;

View File

@@ -33,6 +33,7 @@ UPB_API const upb_EnumValueDef* upb_EnumDef_FindValueByNumber(
UPB_API const char* upb_EnumDef_FullName(const upb_EnumDef* e);
bool upb_EnumDef_HasOptions(const upb_EnumDef* e);
bool upb_EnumDef_IsClosed(const upb_EnumDef* e);
bool upb_EnumDef_IsSpecifiedAsClosed(const upb_EnumDef* e);
// Creates a mini descriptor string for an enum, returns true on success.
bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a,
@@ -40,6 +41,7 @@ bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a,
const char* upb_EnumDef_Name(const upb_EnumDef* e);
const UPB_DESC(EnumOptions) * upb_EnumDef_Options(const upb_EnumDef* e);
const UPB_DESC(FeatureSet) * upb_EnumDef_ResolvedFeatures(const upb_EnumDef* e);
upb_StringView upb_EnumDef_ReservedName(const upb_EnumDef* e, int i);
int upb_EnumDef_ReservedNameCount(const upb_EnumDef* e);

View File

@@ -7,19 +7,27 @@
#include "upb/reflection/internal/enum_value_def.h"
#include <stdint.h>
#include "upb/reflection/def_type.h"
#include "upb/reflection/enum_def.h"
#include "upb/reflection/enum_value_def.h"
#include "upb/reflection/file_def.h"
#include "upb/reflection/internal/def_builder.h"
#include "upb/reflection/internal/enum_def.h"
#include "upb/reflection/internal/file_def.h"
// Must be last.
#include "upb/port/def.inc"
struct upb_EnumValueDef {
const UPB_DESC(EnumValueOptions) * opts;
const UPB_DESC(EnumValueOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_EnumDef* parent;
const char* full_name;
int32_t number;
#if UINTPTR_MAX == 0xffffffff
uint32_t padding; // Increase size to a multiple of 8.
#endif
};
upb_EnumValueDef* _upb_EnumValueDef_At(const upb_EnumValueDef* v, int i) {
@@ -56,6 +64,11 @@ bool upb_EnumValueDef_HasOptions(const upb_EnumValueDef* v) {
return v->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_EnumValueDef_ResolvedFeatures(const upb_EnumValueDef* e) {
return e->resolved_features;
}
const upb_EnumDef* upb_EnumValueDef_Enum(const upb_EnumValueDef* v) {
return v->parent;
}
@@ -76,9 +89,15 @@ uint32_t upb_EnumValueDef_Index(const upb_EnumValueDef* v) {
}
static void create_enumvaldef(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(EnumValueDescriptorProto) *
const UPB_DESC(EnumValueDescriptorProto*)
val_proto,
const UPB_DESC(FeatureSet*) parent_features,
upb_EnumDef* e, upb_EnumValueDef* v) {
UPB_DEF_SET_OPTIONS(v->opts, EnumValueDescriptorProto, EnumValueOptions,
val_proto);
v->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(EnumValueOptions_features)(v->opts));
upb_StringView name = UPB_DESC(EnumValueDescriptorProto_name)(val_proto);
v->parent = e; // Must happen prior to _upb_DefBuilder_Add()
@@ -87,17 +106,27 @@ static void create_enumvaldef(upb_DefBuilder* ctx, const char* prefix,
_upb_DefBuilder_Add(ctx, v->full_name,
_upb_DefType_Pack(v, UPB_DEFTYPE_ENUMVAL));
UPB_DEF_SET_OPTIONS(v->opts, EnumValueDescriptorProto, EnumValueOptions,
val_proto);
bool ok = _upb_EnumDef_Insert(e, v, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
}
static void _upb_EnumValueDef_CheckZeroValue(upb_DefBuilder* ctx,
const upb_EnumDef* e,
const upb_EnumValueDef* v, int n) {
// When the special UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN is enabled, we have to
// exempt closed enums from this check, even when we are treating them as
// open.
if (upb_EnumDef_IsSpecifiedAsClosed(e) || n == 0 || v[0].number == 0) return;
_upb_DefBuilder_Errf(ctx, "for open enums, the first value must be zero (%s)",
upb_EnumDef_FullName(e));
}
// Allocate and initialize an array of |n| enum value defs owned by |e|.
upb_EnumValueDef* _upb_EnumValueDefs_New(
upb_DefBuilder* ctx, const char* prefix, int n,
const UPB_DESC(EnumValueDescriptorProto) * const* protos, upb_EnumDef* e,
const UPB_DESC(EnumValueDescriptorProto*) const* protos,
const UPB_DESC(FeatureSet*) parent_features, upb_EnumDef* e,
bool* is_sorted) {
_upb_DefType_CheckPadding(sizeof(upb_EnumValueDef));
@@ -107,19 +136,14 @@ upb_EnumValueDef* _upb_EnumValueDefs_New(
*is_sorted = true;
uint32_t previous = 0;
for (int i = 0; i < n; i++) {
create_enumvaldef(ctx, prefix, protos[i], e, &v[i]);
create_enumvaldef(ctx, prefix, protos[i], parent_features, e, &v[i]);
const uint32_t current = v[i].number;
if (previous > current) *is_sorted = false;
previous = current;
}
if (upb_FileDef_Syntax(ctx->file) == kUpb_Syntax_Proto3 && n > 0 &&
v[0].number != 0) {
_upb_DefBuilder_Errf(ctx,
"for proto3, the first enum value must be zero (%s)",
upb_EnumDef_FullName(e));
}
_upb_EnumValueDef_CheckZeroValue(ctx, e, v, n);
return v;
}

View File

@@ -27,6 +27,8 @@ UPB_API const char* upb_EnumValueDef_Name(const upb_EnumValueDef* v);
UPB_API int32_t upb_EnumValueDef_Number(const upb_EnumValueDef* v);
const UPB_DESC(EnumValueOptions) *
upb_EnumValueDef_Options(const upb_EnumValueDef* v);
const UPB_DESC(FeatureSet) *
upb_EnumValueDef_ResolvedFeatures(const upb_EnumValueDef* e);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -7,6 +7,9 @@
#include "upb/reflection/internal/extension_range.h"
#include <stdint.h>
#include "upb/reflection/extension_range.h"
#include "upb/reflection/field_def.h"
#include "upb/reflection/internal/def_builder.h"
#include "upb/reflection/message_def.h"
@@ -15,7 +18,8 @@
#include "upb/port/def.inc"
struct upb_ExtensionRange {
const UPB_DESC(ExtensionRangeOptions) * opts;
const UPB_DESC(ExtensionRangeOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
int32_t start;
int32_t end;
};
@@ -41,12 +45,18 @@ int32_t upb_ExtensionRange_End(const upb_ExtensionRange* r) { return r->end; }
upb_ExtensionRange* _upb_ExtensionRanges_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(DescriptorProto_ExtensionRange) * const* protos,
const upb_MessageDef* m) {
const UPB_DESC(DescriptorProto_ExtensionRange*) const* protos,
const UPB_DESC(FeatureSet*) parent_features, const upb_MessageDef* m) {
upb_ExtensionRange* r =
_upb_DefBuilder_Alloc(ctx, sizeof(upb_ExtensionRange) * n);
for (int i = 0; i < n; i++) {
UPB_DEF_SET_OPTIONS(r[i].opts, DescriptorProto_ExtensionRange,
ExtensionRangeOptions, protos[i]);
r[i].resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features,
UPB_DESC(ExtensionRangeOptions_features)(r[i].opts));
const int32_t start =
UPB_DESC(DescriptorProto_ExtensionRange_start)(protos[i]);
const int32_t end = UPB_DESC(DescriptorProto_ExtensionRange_end)(protos[i]);
@@ -66,8 +76,6 @@ upb_ExtensionRange* _upb_ExtensionRanges_New(
r[i].start = start;
r[i].end = end;
UPB_DEF_SET_OPTIONS(r[i].opts, DescriptorProto_ExtensionRange,
ExtensionRangeOptions, protos[i]);
}
return r;

View File

@@ -25,6 +25,8 @@ int32_t upb_ExtensionRange_End(const upb_ExtensionRange* r);
bool upb_ExtensionRange_HasOptions(const upb_ExtensionRange* r);
const UPB_DESC(ExtensionRangeOptions) *
upb_ExtensionRange_Options(const upb_ExtensionRange* r);
const UPB_DESC(FeatureSet) *
upb_ExtensionRange_ResolvedFeatures(const upb_ExtensionRange* e);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -10,13 +10,27 @@
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/base/upcast.h"
#include "upb/mem/arena.h"
#include "upb/message/accessors.h"
#include "upb/mini_descriptor/decode.h"
#include "upb/mini_descriptor/internal/encode.h"
#include "upb/mini_descriptor/internal/modifiers.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
#include "upb/reflection/def.h"
#include "upb/reflection/def_type.h"
#include "upb/reflection/internal/def_builder.h"
#include "upb/reflection/internal/def_pool.h"
#include "upb/reflection/internal/desc_state.h"
#include "upb/reflection/internal/enum_def.h"
#include "upb/reflection/internal/file_def.h"
@@ -35,7 +49,8 @@ typedef struct {
} str_t;
struct upb_FieldDef {
const UPB_DESC(FieldOptions) * opts;
const UPB_DESC(FieldOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_FileDef* file;
const upb_MessageDef* msgdef;
const char* full_name;
@@ -65,13 +80,9 @@ struct upb_FieldDef {
bool has_json_name;
bool has_presence;
bool is_extension;
bool is_packed;
bool is_proto3_optional;
upb_FieldType type_;
upb_Label label_;
#if UINTPTR_MAX == 0xffffffff
uint32_t padding; // Increase size to a multiple of 8.
#endif
};
upb_FieldDef* _upb_FieldDef_At(const upb_FieldDef* f, int i) {
@@ -86,49 +97,27 @@ bool upb_FieldDef_HasOptions(const upb_FieldDef* f) {
return f->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_FieldDef_ResolvedFeatures(const upb_FieldDef* f) {
return f->resolved_features;
}
const char* upb_FieldDef_FullName(const upb_FieldDef* f) {
return f->full_name;
}
upb_CType upb_FieldDef_CType(const upb_FieldDef* f) {
switch (f->type_) {
case kUpb_FieldType_Double:
return kUpb_CType_Double;
case kUpb_FieldType_Float:
return kUpb_CType_Float;
case kUpb_FieldType_Int64:
case kUpb_FieldType_SInt64:
case kUpb_FieldType_SFixed64:
return kUpb_CType_Int64;
case kUpb_FieldType_Int32:
case kUpb_FieldType_SFixed32:
case kUpb_FieldType_SInt32:
return kUpb_CType_Int32;
case kUpb_FieldType_UInt64:
case kUpb_FieldType_Fixed64:
return kUpb_CType_UInt64;
case kUpb_FieldType_UInt32:
case kUpb_FieldType_Fixed32:
return kUpb_CType_UInt32;
case kUpb_FieldType_Enum:
return kUpb_CType_Enum;
case kUpb_FieldType_Bool:
return kUpb_CType_Bool;
case kUpb_FieldType_String:
return kUpb_CType_String;
case kUpb_FieldType_Bytes:
return kUpb_CType_Bytes;
case kUpb_FieldType_Group:
case kUpb_FieldType_Message:
return kUpb_CType_Message;
}
UPB_UNREACHABLE();
return upb_FieldType_CType(f->type_);
}
upb_FieldType upb_FieldDef_Type(const upb_FieldDef* f) { return f->type_; }
uint32_t upb_FieldDef_Index(const upb_FieldDef* f) { return f->index_; }
uint32_t upb_FieldDef_LayoutIndex(const upb_FieldDef* f) {
return f->layout_index;
}
upb_Label upb_FieldDef_Label(const upb_FieldDef* f) { return f->label_; }
uint32_t upb_FieldDef_Number(const upb_FieldDef* f) { return f->number_; }
@@ -140,7 +129,9 @@ bool _upb_FieldDef_IsPackable(const upb_FieldDef* f) {
}
bool upb_FieldDef_IsPacked(const upb_FieldDef* f) {
return _upb_FieldDef_IsPackable(f) && f->is_packed;
return _upb_FieldDef_IsPackable(f) &&
UPB_DESC(FeatureSet_repeated_field_encoding(f->resolved_features)) ==
UPB_DESC(FeatureSet_PACKED);
}
const char* upb_FieldDef_Name(const upb_FieldDef* f) {
@@ -217,11 +208,11 @@ upb_MessageValue upb_FieldDef_Default(const upb_FieldDef* f) {
}
const upb_MessageDef* upb_FieldDef_MessageSubDef(const upb_FieldDef* f) {
return upb_FieldDef_CType(f) == kUpb_CType_Message ? f->sub.msgdef : NULL;
return upb_FieldDef_IsSubMessage(f) ? f->sub.msgdef : NULL;
}
const upb_EnumDef* upb_FieldDef_EnumSubDef(const upb_FieldDef* f) {
return upb_FieldDef_CType(f) == kUpb_CType_Enum ? f->sub.enumdef : NULL;
return upb_FieldDef_IsEnum(f) ? f->sub.enumdef : NULL;
}
const upb_MiniTableField* upb_FieldDef_MiniTable(const upb_FieldDef* f) {
@@ -231,11 +222,11 @@ const upb_MiniTableField* upb_FieldDef_MiniTable(const upb_FieldDef* f) {
file, f->layout_index);
} else {
const upb_MiniTable* layout = upb_MessageDef_MiniTable(f->msgdef);
return &layout->fields[f->layout_index];
return &layout->UPB_PRIVATE(fields)[f->layout_index];
}
}
const upb_MiniTableExtension* _upb_FieldDef_ExtensionMiniTable(
const upb_MiniTableExtension* upb_FieldDef_MiniTableExtension(
const upb_FieldDef* f) {
UPB_ASSERT(upb_FieldDef_IsExtension(f));
const upb_FileDef* file = upb_FieldDef_File(f);
@@ -253,44 +244,54 @@ bool _upb_FieldDef_IsProto3Optional(const upb_FieldDef* f) {
int _upb_FieldDef_LayoutIndex(const upb_FieldDef* f) { return f->layout_index; }
// begin:google_only
// static bool _upb_FieldDef_EnforceUtf8Option(const upb_FieldDef* f) {
// #if defined(UPB_BOOTSTRAP_STAGE0)
// return true;
// #else
// return UPB_DESC(FieldOptions_enforce_utf8)(f->opts);
// #endif
// }
// end:google_only
// begin:github_only
static bool _upb_FieldDef_EnforceUtf8Option(const upb_FieldDef* f) {
return true;
}
// end:github_only
bool _upb_FieldDef_ValidateUtf8(const upb_FieldDef* f) {
if (upb_FieldDef_Type(f) != kUpb_FieldType_String) return false;
return upb_FileDef_Syntax(upb_FieldDef_File(f)) == kUpb_Syntax_Proto3
? _upb_FieldDef_EnforceUtf8Option(f)
: false;
return UPB_DESC(FeatureSet_utf8_validation(f->resolved_features)) ==
UPB_DESC(FeatureSet_VERIFY);
}
bool _upb_FieldDef_IsGroupLike(const upb_FieldDef* f) {
// Groups are always tag-delimited.
if (f->type_ != kUpb_FieldType_Group) {
return false;
}
const upb_MessageDef* msg = upb_FieldDef_MessageSubDef(f);
// Group fields always are always the lowercase type name.
const char* mname = upb_MessageDef_Name(msg);
const char* fname = upb_FieldDef_Name(f);
size_t name_size = strlen(fname);
if (name_size != strlen(mname)) return false;
for (size_t i = 0; i < name_size; ++i) {
if ((mname[i] | 0x20) != fname[i]) {
// Case-insensitive ascii comparison.
return false;
}
}
if (upb_MessageDef_File(msg) != upb_FieldDef_File(f)) {
return false;
}
// Group messages are always defined in the same scope as the field. File
// level extensions will compare NULL == NULL here, which is why the file
// comparison above is necessary to ensure both come from the same file.
return upb_FieldDef_IsExtension(f) ? upb_FieldDef_ExtensionScope(f) ==
upb_MessageDef_ContainingType(msg)
: upb_FieldDef_ContainingType(f) ==
upb_MessageDef_ContainingType(msg);
}
uint64_t _upb_FieldDef_Modifiers(const upb_FieldDef* f) {
uint64_t out = upb_FieldDef_IsPacked(f) ? kUpb_FieldModifier_IsPacked : 0;
switch (f->label_) {
case kUpb_Label_Optional:
if (!upb_FieldDef_HasPresence(f)) {
out |= kUpb_FieldModifier_IsProto3Singular;
}
break;
case kUpb_Label_Repeated:
out |= kUpb_FieldModifier_IsRepeated;
break;
case kUpb_Label_Required:
out |= kUpb_FieldModifier_IsRequired;
break;
if (upb_FieldDef_IsRepeated(f)) {
out |= kUpb_FieldModifier_IsRepeated;
} else if (upb_FieldDef_IsRequired(f)) {
out |= kUpb_FieldModifier_IsRequired;
} else if (!upb_FieldDef_HasPresence(f)) {
out |= kUpb_FieldModifier_IsProto3Singular;
}
if (_upb_FieldDef_IsClosedEnum(f)) {
@@ -308,8 +309,11 @@ bool upb_FieldDef_HasDefault(const upb_FieldDef* f) { return f->has_default; }
bool upb_FieldDef_HasPresence(const upb_FieldDef* f) { return f->has_presence; }
bool upb_FieldDef_HasSubDef(const upb_FieldDef* f) {
return upb_FieldDef_IsSubMessage(f) ||
upb_FieldDef_CType(f) == kUpb_CType_Enum;
return upb_FieldDef_IsSubMessage(f) || upb_FieldDef_IsEnum(f);
}
bool upb_FieldDef_IsEnum(const upb_FieldDef* f) {
return upb_FieldDef_CType(f) == kUpb_CType_Enum;
}
bool upb_FieldDef_IsMap(const upb_FieldDef* f) {
@@ -330,7 +334,8 @@ bool upb_FieldDef_IsRepeated(const upb_FieldDef* f) {
}
bool upb_FieldDef_IsRequired(const upb_FieldDef* f) {
return upb_FieldDef_Label(f) == kUpb_Label_Required;
return UPB_DESC(FeatureSet_field_presence)(f->resolved_features) ==
UPB_DESC(FeatureSet_LEGACY_REQUIRED);
}
bool upb_FieldDef_IsString(const upb_FieldDef* f) {
@@ -554,27 +559,107 @@ static void set_default_default(upb_DefBuilder* ctx, upb_FieldDef* f) {
}
}
static bool _upb_FieldDef_InferLegacyFeatures(
upb_DefBuilder* ctx, upb_FieldDef* f,
const UPB_DESC(FieldDescriptorProto*) proto,
const UPB_DESC(FieldOptions*) options, upb_Syntax syntax,
UPB_DESC(FeatureSet*) features) {
bool ret = false;
if (UPB_DESC(FieldDescriptorProto_label)(proto) == kUpb_Label_Required) {
if (syntax == kUpb_Syntax_Proto3) {
_upb_DefBuilder_Errf(ctx, "proto3 fields cannot be required (%s)",
f->full_name);
}
int val = UPB_DESC(FeatureSet_LEGACY_REQUIRED);
UPB_DESC(FeatureSet_set_field_presence(features, val));
ret = true;
}
if (UPB_DESC(FieldDescriptorProto_type)(proto) == kUpb_FieldType_Group) {
int val = UPB_DESC(FeatureSet_DELIMITED);
UPB_DESC(FeatureSet_set_message_encoding(features, val));
ret = true;
}
if (UPB_DESC(FieldOptions_has_packed)(options)) {
int val = UPB_DESC(FieldOptions_packed)(options)
? UPB_DESC(FeatureSet_PACKED)
: UPB_DESC(FeatureSet_EXPANDED);
UPB_DESC(FeatureSet_set_repeated_field_encoding(features, val));
ret = true;
}
return ret;
}
static void _upb_FieldDef_Create(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(FieldDescriptorProto) *
const UPB_DESC(FeatureSet*) parent_features,
const UPB_DESC(FieldDescriptorProto*)
field_proto,
upb_MessageDef* m, upb_FieldDef* f) {
// Must happen before _upb_DefBuilder_Add()
f->file = _upb_DefBuilder_File(ctx);
if (!UPB_DESC(FieldDescriptorProto_has_name)(field_proto)) {
_upb_DefBuilder_Errf(ctx, "field has no name");
}
const upb_StringView name = UPB_DESC(FieldDescriptorProto_name)(field_proto);
f->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
f->label_ = (int)UPB_DESC(FieldDescriptorProto_label)(field_proto);
f->number_ = UPB_DESC(FieldDescriptorProto_number)(field_proto);
f->is_proto3_optional =
UPB_DESC(FieldDescriptorProto_proto3_optional)(field_proto);
f->msgdef = m;
f->scope.oneof = NULL;
UPB_DEF_SET_OPTIONS(f->opts, FieldDescriptorProto, FieldOptions, field_proto);
upb_Syntax syntax = upb_FileDef_Syntax(f->file);
const UPB_DESC(FeatureSet*) unresolved_features =
UPB_DESC(FieldOptions_features)(f->opts);
bool implicit = false;
if (syntax != kUpb_Syntax_Editions) {
upb_Message_Clear(UPB_UPCAST(ctx->legacy_features),
UPB_DESC_MINITABLE(FeatureSet));
if (_upb_FieldDef_InferLegacyFeatures(ctx, f, field_proto, f->opts, syntax,
ctx->legacy_features)) {
implicit = true;
unresolved_features = ctx->legacy_features;
}
}
if (UPB_DESC(FieldDescriptorProto_has_oneof_index)(field_proto)) {
int oneof_index = UPB_DESC(FieldDescriptorProto_oneof_index)(field_proto);
if (!m) {
_upb_DefBuilder_Errf(ctx, "oneof field (%s) has no containing msg",
f->full_name);
}
if (oneof_index < 0 || oneof_index >= upb_MessageDef_OneofCount(m)) {
_upb_DefBuilder_Errf(ctx, "oneof_index out of range (%s)", f->full_name);
}
upb_OneofDef* oneof = (upb_OneofDef*)upb_MessageDef_Oneof(m, oneof_index);
f->scope.oneof = oneof;
parent_features = upb_OneofDef_ResolvedFeatures(oneof);
_upb_OneofDef_Insert(ctx, oneof, f, name.data, name.size);
}
f->resolved_features = _upb_DefBuilder_DoResolveFeatures(
ctx, parent_features, unresolved_features, implicit);
f->label_ = (int)UPB_DESC(FieldDescriptorProto_label)(field_proto);
if (f->label_ == kUpb_Label_Optional &&
// TODO: remove once we can deprecate kUpb_Label_Required.
UPB_DESC(FeatureSet_field_presence)(f->resolved_features) ==
UPB_DESC(FeatureSet_LEGACY_REQUIRED)) {
f->label_ = kUpb_Label_Required;
}
if (!UPB_DESC(FieldDescriptorProto_has_name)(field_proto)) {
_upb_DefBuilder_Errf(ctx, "field has no name");
}
f->has_json_name = UPB_DESC(FieldDescriptorProto_has_json_name)(field_proto);
if (f->has_json_name) {
const upb_StringView sv =
@@ -610,7 +695,7 @@ static void _upb_FieldDef_Create(upb_DefBuilder* ctx, const char* prefix,
}
}
if (!has_type && has_type_name) {
if ((!has_type && has_type_name) || f->type_ == kUpb_FieldType_Message) {
f->type_ =
UPB_FIELD_TYPE_UNSPECIFIED; // We'll assign this in resolve_subdef()
} else {
@@ -630,56 +715,29 @@ static void _upb_FieldDef_Create(upb_DefBuilder* ctx, const char* prefix,
* to the field_proto until later when we can properly resolve it. */
f->sub.unresolved = field_proto;
if (f->label_ == kUpb_Label_Required &&
upb_FileDef_Syntax(f->file) == kUpb_Syntax_Proto3) {
_upb_DefBuilder_Errf(ctx, "proto3 fields cannot be required (%s)",
f->full_name);
}
if (UPB_DESC(FieldDescriptorProto_has_oneof_index)(field_proto)) {
int oneof_index = UPB_DESC(FieldDescriptorProto_oneof_index)(field_proto);
if (upb_FieldDef_Label(f) != kUpb_Label_Optional) {
_upb_DefBuilder_Errf(ctx, "fields in oneof must have OPTIONAL label (%s)",
f->full_name);
}
if (!m) {
_upb_DefBuilder_Errf(ctx, "oneof field (%s) has no containing msg",
f->full_name);
}
if (oneof_index >= upb_MessageDef_OneofCount(m)) {
_upb_DefBuilder_Errf(ctx, "oneof_index out of range (%s)", f->full_name);
}
upb_OneofDef* oneof = (upb_OneofDef*)upb_MessageDef_Oneof(m, oneof_index);
f->scope.oneof = oneof;
_upb_OneofDef_Insert(ctx, oneof, f, name.data, name.size);
}
UPB_DEF_SET_OPTIONS(f->opts, FieldDescriptorProto, FieldOptions, field_proto);
if (UPB_DESC(FieldOptions_has_packed)(f->opts)) {
f->is_packed = UPB_DESC(FieldOptions_packed)(f->opts);
} else {
f->is_packed = upb_FileDef_Syntax(f->file) == kUpb_Syntax_Proto3;
}
f->has_presence =
(!upb_FieldDef_IsRepeated(f)) &&
(f->type_ == kUpb_FieldType_Message || f->type_ == kUpb_FieldType_Group ||
upb_FieldDef_ContainingOneof(f) ||
(upb_FileDef_Syntax(f->file) == kUpb_Syntax_Proto2));
(f->is_extension ||
(f->type_ == kUpb_FieldType_Message ||
f->type_ == kUpb_FieldType_Group || upb_FieldDef_ContainingOneof(f) ||
UPB_DESC(FeatureSet_field_presence)(f->resolved_features) !=
UPB_DESC(FeatureSet_IMPLICIT)));
}
static void _upb_FieldDef_CreateExt(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(FieldDescriptorProto) *
const UPB_DESC(FeatureSet*) parent_features,
const UPB_DESC(FieldDescriptorProto*)
field_proto,
upb_MessageDef* m, upb_FieldDef* f) {
f->is_extension = true;
_upb_FieldDef_Create(ctx, prefix, field_proto, m, f);
_upb_FieldDef_Create(ctx, prefix, parent_features, field_proto, m, f);
if (UPB_DESC(FieldDescriptorProto_has_oneof_index)(field_proto)) {
_upb_DefBuilder_Errf(ctx, "oneof_index provided for extension field (%s)",
@@ -691,16 +749,19 @@ static void _upb_FieldDef_CreateExt(upb_DefBuilder* ctx, const char* prefix,
f->layout_index = ctx->ext_count++;
if (ctx->layout) {
UPB_ASSERT(_upb_FieldDef_ExtensionMiniTable(f)->field.number == f->number_);
UPB_ASSERT(upb_MiniTableExtension_Number(
upb_FieldDef_MiniTableExtension(f)) == f->number_);
}
}
static void _upb_FieldDef_CreateNotExt(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(FieldDescriptorProto) *
const UPB_DESC(FeatureSet*)
parent_features,
const UPB_DESC(FieldDescriptorProto*)
field_proto,
upb_MessageDef* m, upb_FieldDef* f) {
f->is_extension = false;
_upb_FieldDef_Create(ctx, prefix, field_proto, m, f);
_upb_FieldDef_Create(ctx, prefix, parent_features, field_proto, m, f);
if (!UPB_DESC(FieldDescriptorProto_has_oneof_index)(field_proto)) {
if (f->is_proto3_optional) {
@@ -714,10 +775,11 @@ static void _upb_FieldDef_CreateNotExt(upb_DefBuilder* ctx, const char* prefix,
_upb_MessageDef_InsertField(ctx, m, f);
}
upb_FieldDef* _upb_Extensions_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto) * const* protos, const char* prefix,
upb_MessageDef* m) {
upb_FieldDef* _upb_Extensions_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const char* prefix, upb_MessageDef* m) {
_upb_DefType_CheckPadding(sizeof(upb_FieldDef));
upb_FieldDef* defs =
(upb_FieldDef*)_upb_DefBuilder_Alloc(ctx, sizeof(upb_FieldDef) * n);
@@ -725,17 +787,19 @@ upb_FieldDef* _upb_Extensions_New(
for (int i = 0; i < n; i++) {
upb_FieldDef* f = &defs[i];
_upb_FieldDef_CreateExt(ctx, prefix, protos[i], m, f);
_upb_FieldDef_CreateExt(ctx, prefix, parent_features, protos[i], m, f);
f->index_ = i;
}
return defs;
}
upb_FieldDef* _upb_FieldDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto) * const* protos, const char* prefix,
upb_MessageDef* m, bool* is_sorted) {
upb_FieldDef* _upb_FieldDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const char* prefix, upb_MessageDef* m,
bool* is_sorted) {
_upb_DefType_CheckPadding(sizeof(upb_FieldDef));
upb_FieldDef* defs =
(upb_FieldDef*)_upb_DefBuilder_Alloc(ctx, sizeof(upb_FieldDef) * n);
@@ -744,7 +808,7 @@ upb_FieldDef* _upb_FieldDefs_New(
for (int i = 0; i < n; i++) {
upb_FieldDef* f = &defs[i];
_upb_FieldDef_CreateNotExt(ctx, prefix, protos[i], m, f);
_upb_FieldDef_CreateNotExt(ctx, prefix, parent_features, protos[i], m, f);
f->index_ = i;
if (!ctx->layout) {
// Speculate that the def fields are sorted. We will always sort the
@@ -781,8 +845,15 @@ static void resolve_subdef(upb_DefBuilder* ctx, const char* prefix,
break;
case UPB_DEFTYPE_MSG:
f->sub.msgdef = def;
f->type_ = kUpb_FieldType_Message; // It appears there is no way of
// this being a group.
f->type_ = kUpb_FieldType_Message;
// TODO: remove once we can deprecate
// kUpb_FieldType_Group.
if (UPB_DESC(FeatureSet_message_encoding)(f->resolved_features) ==
UPB_DESC(FeatureSet_DELIMITED) &&
!upb_MessageDef_IsMapEntry(def) &&
!(f->msgdef && upb_MessageDef_IsMapEntry(f->msgdef))) {
f->type_ = kUpb_FieldType_Group;
}
f->has_presence = !upb_FieldDef_IsRepeated(f);
break;
default:
@@ -878,10 +949,10 @@ static void resolve_extension(upb_DefBuilder* ctx, const char* prefix,
void _upb_FieldDef_BuildMiniTableExtension(upb_DefBuilder* ctx,
const upb_FieldDef* f) {
const upb_MiniTableExtension* ext = _upb_FieldDef_ExtensionMiniTable(f);
const upb_MiniTableExtension* ext = upb_FieldDef_MiniTableExtension(f);
if (ctx->layout) {
UPB_ASSERT(upb_FieldDef_Number(f) == ext->field.number);
UPB_ASSERT(upb_FieldDef_Number(f) == upb_MiniTableExtension_Number(ext));
} else {
upb_StringView desc;
if (!upb_FieldDef_MiniDescriptorEncode(f, ctx->tmp_arena, &desc)) {
@@ -891,13 +962,15 @@ void _upb_FieldDef_BuildMiniTableExtension(upb_DefBuilder* ctx,
upb_MiniTableExtension* mut_ext = (upb_MiniTableExtension*)ext;
upb_MiniTableSub sub = {NULL};
if (upb_FieldDef_IsSubMessage(f)) {
sub.submsg = upb_MessageDef_MiniTable(f->sub.msgdef);
const upb_MiniTable* submsg = upb_MessageDef_MiniTable(f->sub.msgdef);
sub = upb_MiniTableSub_FromMessage(submsg);
} else if (_upb_FieldDef_IsClosedEnum(f)) {
sub.subenum = _upb_EnumDef_MiniTable(f->sub.enumdef);
const upb_MiniTableEnum* subenum = _upb_EnumDef_MiniTable(f->sub.enumdef);
sub = upb_MiniTableSub_FromEnum(subenum);
}
bool ok2 = upb_MiniTableExtension_Init(desc.data, desc.size, mut_ext,
upb_MessageDef_MiniTable(f->msgdef),
sub, ctx->status);
bool ok2 = _upb_MiniTableExtension_Init(desc.data, desc.size, mut_ext,
upb_MessageDef_MiniTable(f->msgdef),
sub, ctx->platform, ctx->status);
if (!ok2) _upb_DefBuilder_Errf(ctx, "Could not build extension mini table");
}

View File

@@ -10,7 +10,13 @@
#ifndef UPB_REFLECTION_FIELD_DEF_H_
#define UPB_REFLECTION_FIELD_DEF_H_
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/message/value.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/reflection/common.h"
// Must be last.
@@ -39,10 +45,11 @@ bool upb_FieldDef_HasOptions(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_HasPresence(const upb_FieldDef* f);
bool upb_FieldDef_HasSubDef(const upb_FieldDef* f);
uint32_t upb_FieldDef_Index(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_IsEnum(const upb_FieldDef* f);
bool upb_FieldDef_IsExtension(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_IsMap(const upb_FieldDef* f);
bool upb_FieldDef_IsOptional(const upb_FieldDef* f);
bool upb_FieldDef_IsPacked(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_IsPacked(const upb_FieldDef* f);
bool upb_FieldDef_IsPrimitive(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_IsRepeated(const upb_FieldDef* f);
bool upb_FieldDef_IsRequired(const upb_FieldDef* f);
@@ -50,17 +57,23 @@ bool upb_FieldDef_IsString(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_IsSubMessage(const upb_FieldDef* f);
UPB_API const char* upb_FieldDef_JsonName(const upb_FieldDef* f);
UPB_API upb_Label upb_FieldDef_Label(const upb_FieldDef* f);
uint32_t upb_FieldDef_LayoutIndex(const upb_FieldDef* f);
UPB_API const upb_MessageDef* upb_FieldDef_MessageSubDef(const upb_FieldDef* f);
bool _upb_FieldDef_ValidateUtf8(const upb_FieldDef* f);
bool _upb_FieldDef_IsGroupLike(const upb_FieldDef* f);
// Creates a mini descriptor string for a field, returns true on success.
bool upb_FieldDef_MiniDescriptorEncode(const upb_FieldDef* f, upb_Arena* a,
upb_StringView* out);
const upb_MiniTableField* upb_FieldDef_MiniTable(const upb_FieldDef* f);
const upb_MiniTableExtension* upb_FieldDef_MiniTableExtension(
const upb_FieldDef* f);
UPB_API const char* upb_FieldDef_Name(const upb_FieldDef* f);
UPB_API uint32_t upb_FieldDef_Number(const upb_FieldDef* f);
const UPB_DESC(FieldOptions) * upb_FieldDef_Options(const upb_FieldDef* f);
const UPB_DESC(FeatureSet) *
upb_FieldDef_ResolvedFeatures(const upb_FieldDef* f);
UPB_API const upb_OneofDef* upb_FieldDef_RealContainingOneof(
const upb_FieldDef* f);
UPB_API upb_FieldType upb_FieldDef_Type(const upb_FieldDef* f);

View File

@@ -7,8 +7,17 @@
#include "upb/reflection/internal/file_def.h"
#include "upb/reflection/def_pool.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/base/string_view.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/extension_registry.h"
#include "upb/mini_table/file.h"
#include "upb/reflection/def.h"
#include "upb/reflection/internal/def_builder.h"
#include "upb/reflection/internal/def_pool.h"
#include "upb/reflection/internal/enum_def.h"
#include "upb/reflection/internal/field_def.h"
#include "upb/reflection/internal/message_def.h"
@@ -19,7 +28,8 @@
#include "upb/port/def.inc"
struct upb_FileDef {
const UPB_DESC(FileOptions) * opts;
const UPB_DESC(FileOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const char* name;
const char* package;
UPB_DESC(Edition) edition;
@@ -45,10 +55,29 @@ struct upb_FileDef {
upb_Syntax syntax;
};
UPB_API const char* upb_FileDef_EditionName(int edition) {
// TODO Synchronize this with descriptor.proto better.
switch (edition) {
case UPB_DESC(EDITION_PROTO2):
return "PROTO2";
case UPB_DESC(EDITION_PROTO3):
return "PROTO3";
case UPB_DESC(EDITION_2023):
return "2023";
default:
return "UNKNOWN";
}
}
const UPB_DESC(FileOptions) * upb_FileDef_Options(const upb_FileDef* f) {
return f->opts;
}
const UPB_DESC(FeatureSet) *
upb_FileDef_ResolvedFeatures(const upb_FileDef* f) {
return f->resolved_features;
}
bool upb_FileDef_HasOptions(const upb_FileDef* f) {
return f->opts != (void*)kUpbDefOptDefault;
}
@@ -141,6 +170,17 @@ const upb_MiniTableExtension* _upb_FileDef_ExtensionMiniTable(
return f->ext_layouts[i];
}
// Note: Import cycles are not allowed so this will terminate.
bool upb_FileDef_Resolves(const upb_FileDef* f, const char* path) {
if (!strcmp(f->name, path)) return true;
for (int i = 0; i < upb_FileDef_PublicDependencyCount(f); i++) {
const upb_FileDef* dep = upb_FileDef_PublicDependency(f, i);
if (upb_FileDef_Resolves(dep, path)) return true;
}
return false;
}
static char* strviewdup(upb_DefBuilder* ctx, upb_StringView view) {
char* ret = upb_strdup2(view.data, view.size, _upb_DefBuilder_Arena(ctx));
if (!ret) _upb_DefBuilder_OomErr(ctx);
@@ -165,6 +205,64 @@ static int count_exts_in_msg(const UPB_DESC(DescriptorProto) * msg_proto) {
return ext_count;
}
const UPB_DESC(FeatureSet*)
_upb_FileDef_FindEdition(upb_DefBuilder* ctx, int edition) {
const UPB_DESC(FeatureSetDefaults)* defaults =
upb_DefPool_FeatureSetDefaults(ctx->symtab);
int min = UPB_DESC(FeatureSetDefaults_minimum_edition)(defaults);
int max = UPB_DESC(FeatureSetDefaults_maximum_edition)(defaults);
if (edition < min) {
_upb_DefBuilder_Errf(ctx,
"Edition %s is earlier than the minimum edition %s "
"given in the defaults",
upb_FileDef_EditionName(edition),
upb_FileDef_EditionName(min));
return NULL;
}
if (edition > max) {
_upb_DefBuilder_Errf(ctx,
"Edition %s is later than the maximum edition %s "
"given in the defaults",
upb_FileDef_EditionName(edition),
upb_FileDef_EditionName(max));
return NULL;
}
size_t n;
const UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault)* const* d =
UPB_DESC(FeatureSetDefaults_defaults)(defaults, &n);
const UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault)* result = NULL;
for (size_t i = 0; i < n; i++) {
if (UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault_edition)(d[i]) >
edition) {
break;
}
result = d[i];
}
if (result == NULL) {
_upb_DefBuilder_Errf(ctx, "No valid default found for edition %s",
upb_FileDef_EditionName(edition));
return NULL;
}
// Merge the fixed and overridable features to get the edition's default
// feature set.
const UPB_DESC(FeatureSet)* fixed = UPB_DESC(
FeatureSetDefaults_FeatureSetEditionDefault_fixed_features)(result);
const UPB_DESC(FeatureSet)* overridable = UPB_DESC(
FeatureSetDefaults_FeatureSetEditionDefault_overridable_features)(result);
if (!fixed && !overridable) {
_upb_DefBuilder_Errf(ctx, "No valid default found for edition %s",
upb_FileDef_EditionName(edition));
return NULL;
} else if (!fixed) {
return overridable;
}
return _upb_DefBuilder_DoResolveFeatures(ctx, fixed, overridable,
/*is_implicit=*/true);
}
// Allocate and initialize one file def, and add it to the context object.
void _upb_FileDef_Create(upb_DefBuilder* ctx,
const UPB_DESC(FileDescriptorProto) * file_proto) {
@@ -193,11 +291,12 @@ void _upb_FileDef_Create(upb_DefBuilder* ctx,
if (ctx->layout) {
// We are using the ext layouts that were passed in.
file->ext_layouts = ctx->layout->exts;
if (ctx->layout->ext_count != file->ext_count) {
file->ext_layouts = ctx->layout->UPB_PRIVATE(exts);
const int mt_ext_count = upb_MiniTableFile_ExtensionCount(ctx->layout);
if (mt_ext_count != file->ext_count) {
_upb_DefBuilder_Errf(ctx,
"Extension count did not match layout (%d vs %d)",
ctx->layout->ext_count, file->ext_count);
mt_ext_count, file->ext_count);
}
} else {
// We are building ext layouts from scratch.
@@ -233,21 +332,33 @@ void _upb_FileDef_Create(upb_DefBuilder* ctx,
if (streql_view(syntax, "proto2")) {
file->syntax = kUpb_Syntax_Proto2;
file->edition = UPB_DESC(EDITION_PROTO2);
} else if (streql_view(syntax, "proto3")) {
file->syntax = kUpb_Syntax_Proto3;
file->edition = UPB_DESC(EDITION_PROTO3);
} else if (streql_view(syntax, "editions")) {
file->syntax = kUpb_Syntax_Editions;
file->edition = UPB_DESC(FileDescriptorProto_edition)(file_proto);
} else {
_upb_DefBuilder_Errf(ctx, "Invalid syntax '" UPB_STRINGVIEW_FORMAT "'",
UPB_STRINGVIEW_ARGS(syntax));
}
} else {
file->syntax = kUpb_Syntax_Proto2;
file->edition = UPB_DESC(EDITION_PROTO2);
}
// Read options.
UPB_DEF_SET_OPTIONS(file->opts, FileDescriptorProto, FileOptions, file_proto);
// Resolve features.
const UPB_DESC(FeatureSet*) edition_defaults =
_upb_FileDef_FindEdition(ctx, file->edition);
const UPB_DESC(FeatureSet*) unresolved =
UPB_DESC(FileOptions_features)(file->opts);
file->resolved_features =
_upb_DefBuilder_ResolveFeatures(ctx, edition_defaults, unresolved);
// Verify dependencies.
strs = UPB_DESC(FileDescriptorProto_dependency)(file_proto, &n);
file->dep_count = n;
@@ -293,22 +404,26 @@ void _upb_FileDef_Create(upb_DefBuilder* ctx,
// Create enums.
enums = UPB_DESC(FileDescriptorProto_enum_type)(file_proto, &n);
file->top_lvl_enum_count = n;
file->top_lvl_enums = _upb_EnumDefs_New(ctx, n, enums, NULL);
file->top_lvl_enums =
_upb_EnumDefs_New(ctx, n, enums, file->resolved_features, NULL);
// Create extensions.
exts = UPB_DESC(FileDescriptorProto_extension)(file_proto, &n);
file->top_lvl_ext_count = n;
file->top_lvl_exts = _upb_Extensions_New(ctx, n, exts, file->package, NULL);
file->top_lvl_exts = _upb_Extensions_New(
ctx, n, exts, file->resolved_features, file->package, NULL);
// Create messages.
msgs = UPB_DESC(FileDescriptorProto_message_type)(file_proto, &n);
file->top_lvl_msg_count = n;
file->top_lvl_msgs = _upb_MessageDefs_New(ctx, n, msgs, NULL);
file->top_lvl_msgs =
_upb_MessageDefs_New(ctx, n, msgs, file->resolved_features, NULL);
// Create services.
services = UPB_DESC(FileDescriptorProto_service)(file_proto, &n);
file->service_count = n;
file->services = _upb_ServiceDefs_New(ctx, n, services);
file->services =
_upb_ServiceDefs_New(ctx, n, services, file->resolved_features);
// Now that all names are in the table, build layouts and resolve refs.

View File

@@ -19,11 +19,14 @@
extern "C" {
#endif
UPB_API const char* upb_FileDef_EditionName(int edition);
const upb_FileDef* upb_FileDef_Dependency(const upb_FileDef* f, int i);
int upb_FileDef_DependencyCount(const upb_FileDef* f);
bool upb_FileDef_HasOptions(const upb_FileDef* f);
UPB_API const char* upb_FileDef_Name(const upb_FileDef* f);
const UPB_DESC(FileOptions) * upb_FileDef_Options(const upb_FileDef* f);
const UPB_DESC(FeatureSet) * upb_FileDef_ResolvedFeatures(const upb_FileDef* f);
const char* upb_FileDef_Package(const upb_FileDef* f);
UPB_DESC(Edition) upb_FileDef_Edition(const upb_FileDef* f);
UPB_API const upb_DefPool* upb_FileDef_Pool(const upb_FileDef* f);
@@ -48,6 +51,9 @@ int upb_FileDef_TopLevelMessageCount(const upb_FileDef* f);
const upb_FileDef* upb_FileDef_WeakDependency(const upb_FileDef* f, int i);
int upb_FileDef_WeakDependencyCount(const upb_FileDef* f);
// Returns whether |symbol| is transitively included by |f|
bool upb_FileDef_Resolves(const upb_FileDef* f, const char* symbol);
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -9,10 +9,16 @@
#include <string.h>
#include "upb/base/internal/log2.h"
#include "upb/base/upcast.h"
#include "upb/mem/alloc.h"
#include "upb/message/copy.h"
#include "upb/reflection/def_pool.h"
#include "upb/reflection/def_type.h"
#include "upb/reflection/field_def.h"
#include "upb/reflection/file_def.h"
#include "upb/reflection/internal/strdup2.h"
#include "upb/wire/decode.h"
// Must be last.
#include "upb/port/def.inc"
@@ -23,7 +29,8 @@
* initialized to zeroes.
*
* We have to allocate an extra pointer for upb's internal metadata. */
static const char opt_default_buf[_UPB_MAXOPT_SIZE + sizeof(void*)] = {0};
static UPB_ALIGN_AS(8) const
char opt_default_buf[_UPB_MAXOPT_SIZE + sizeof(void*)] = {0};
const char* kUpbDefOptDefault = &opt_default_buf[sizeof(void*)];
const char* _upb_DefBuilder_FullToShort(const char* fullname) {
@@ -113,15 +120,15 @@ const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx,
if (sym.size == 0) goto notfound;
upb_value v;
if (sym.data[0] == '.') {
/* Symbols starting with '.' are absolute, so we do a single lookup.
* Slice to omit the leading '.' */
// Symbols starting with '.' are absolute, so we do a single lookup.
// Slice to omit the leading '.'
if (!_upb_DefPool_LookupSym(ctx->symtab, sym.data + 1, sym.size - 1, &v)) {
goto notfound;
}
} else {
/* Remove components from base until we find an entry or run out. */
// Remove components from base until we find an entry or run out.
size_t baselen = base ? strlen(base) : 0;
char* tmp = malloc(sym.size + baselen + 1);
char* tmp = upb_gmalloc(sym.size + baselen + 1);
while (1) {
char* p = tmp;
if (baselen) {
@@ -135,11 +142,11 @@ const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx,
break;
}
if (!remove_component(tmp, &baselen)) {
free(tmp);
upb_gfree(tmp);
goto notfound;
}
}
free(tmp);
upb_gfree(tmp);
}
*type = _upb_DefType_Type(v);
@@ -337,3 +344,74 @@ void _upb_DefBuilder_CheckIdentSlow(upb_DefBuilder* ctx, upb_StringView name,
// We should never reach this point.
UPB_ASSERT(false);
}
upb_StringView _upb_DefBuilder_MakeKey(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
upb_StringView key) {
size_t need = key.size + sizeof(void*);
if (ctx->tmp_buf_size < need) {
ctx->tmp_buf_size = UPB_MAX(64, upb_Log2Ceiling(need));
ctx->tmp_buf = upb_Arena_Malloc(ctx->tmp_arena, ctx->tmp_buf_size);
if (!ctx->tmp_buf) _upb_DefBuilder_OomErr(ctx);
}
memcpy(ctx->tmp_buf, &parent, sizeof(void*));
memcpy(ctx->tmp_buf + sizeof(void*), key.data, key.size);
return upb_StringView_FromDataAndSize(ctx->tmp_buf, need);
}
bool _upb_DefBuilder_GetOrCreateFeatureSet(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
upb_StringView key,
UPB_DESC(FeatureSet**) set) {
upb_StringView k = _upb_DefBuilder_MakeKey(ctx, parent, key);
upb_value v;
if (upb_strtable_lookup2(&ctx->feature_cache, k.data, k.size, &v)) {
*set = upb_value_getptr(v);
return false;
}
*set = (UPB_DESC(FeatureSet*))upb_Message_DeepClone(
UPB_UPCAST(parent), UPB_DESC_MINITABLE(FeatureSet), ctx->arena);
if (!*set) _upb_DefBuilder_OomErr(ctx);
v = upb_value_ptr(*set);
if (!upb_strtable_insert(&ctx->feature_cache, k.data, k.size, v,
ctx->tmp_arena)) {
_upb_DefBuilder_OomErr(ctx);
}
return true;
}
const UPB_DESC(FeatureSet*)
_upb_DefBuilder_DoResolveFeatures(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
const UPB_DESC(FeatureSet*) child,
bool is_implicit) {
assert(parent);
if (!child) return parent;
if (child && !is_implicit &&
upb_FileDef_Syntax(ctx->file) != kUpb_Syntax_Editions) {
_upb_DefBuilder_Errf(ctx, "Features can only be specified for editions");
}
UPB_DESC(FeatureSet*) resolved;
size_t child_size;
const char* child_bytes =
UPB_DESC(FeatureSet_serialize)(child, ctx->tmp_arena, &child_size);
if (!child_bytes) _upb_DefBuilder_OomErr(ctx);
upb_StringView key = upb_StringView_FromDataAndSize(child_bytes, child_size);
if (!_upb_DefBuilder_GetOrCreateFeatureSet(ctx, parent, key, &resolved)) {
return resolved;
}
upb_DecodeStatus dec_status =
upb_Decode(child_bytes, child_size, UPB_UPCAST(resolved),
UPB_DESC_MINITABLE(FeatureSet), NULL, 0, ctx->arena);
if (dec_status != kUpb_DecodeStatus_Ok) _upb_DefBuilder_OomErr(ctx);
return resolved;
}

View File

@@ -36,6 +36,10 @@ extern "C" {
struct upb_DefBuilder {
upb_DefPool* symtab;
upb_strtable feature_cache; // Caches features by identity.
UPB_DESC(FeatureSet*) legacy_features; // For computing legacy features.
char* tmp_buf; // Temporary buffer in tmp_arena.
size_t tmp_buf_size; // Size of temporary buffer.
upb_FileDef* file; // File we are building.
upb_Arena* arena; // Allocate defs here.
upb_Arena* tmp_arena; // For temporary allocations.
@@ -128,6 +132,25 @@ UPB_INLINE void _upb_DefBuilder_CheckIdentFull(upb_DefBuilder* ctx,
if (!good) _upb_DefBuilder_CheckIdentSlow(ctx, name, true);
}
// Returns true if the returned feature set is new and must be populated.
bool _upb_DefBuilder_GetOrCreateFeatureSet(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
upb_StringView key,
UPB_DESC(FeatureSet**) set);
const UPB_DESC(FeatureSet*)
_upb_DefBuilder_DoResolveFeatures(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
const UPB_DESC(FeatureSet*) child,
bool is_implicit);
UPB_INLINE const UPB_DESC(FeatureSet*)
_upb_DefBuilder_ResolveFeatures(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
const UPB_DESC(FeatureSet*) child) {
return _upb_DefBuilder_DoResolveFeatures(ctx, parent, child, false);
}
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -22,10 +22,11 @@ bool _upb_EnumDef_Insert(upb_EnumDef* e, upb_EnumValueDef* v, upb_Arena* a);
const upb_MiniTableEnum* _upb_EnumDef_MiniTable(const upb_EnumDef* e);
// Allocate and initialize an array of |n| enum defs.
upb_EnumDef* _upb_EnumDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto) * const* protos,
const upb_MessageDef* containing_type);
upb_EnumDef* _upb_EnumDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const upb_MessageDef* containing_type);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -23,7 +23,7 @@ upb_EnumReservedRange* _upb_EnumReservedRange_At(const upb_EnumReservedRange* r,
// Allocate and initialize an array of |n| reserved ranges owned by |e|.
upb_EnumReservedRange* _upb_EnumReservedRanges_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto_EnumReservedRange) * const* protos,
const UPB_DESC(EnumDescriptorProto_EnumReservedRange*) const* protos,
const upb_EnumDef* e);
#ifdef __cplusplus

View File

@@ -22,7 +22,8 @@ upb_EnumValueDef* _upb_EnumValueDef_At(const upb_EnumValueDef* v, int i);
// Allocate and initialize an array of |n| enum value defs owned by |e|.
upb_EnumValueDef* _upb_EnumValueDefs_New(
upb_DefBuilder* ctx, const char* prefix, int n,
const UPB_DESC(EnumValueDescriptorProto) * const* protos, upb_EnumDef* e,
const UPB_DESC(EnumValueDescriptorProto*) const* protos,
const UPB_DESC(FeatureSet*) parent_features, upb_EnumDef* e,
bool* is_sorted);
const upb_EnumValueDef** _upb_EnumValueDefs_Sorted(const upb_EnumValueDef* v,

View File

@@ -22,8 +22,8 @@ upb_ExtensionRange* _upb_ExtensionRange_At(const upb_ExtensionRange* r, int i);
// Allocate and initialize an array of |n| extension ranges owned by |m|.
upb_ExtensionRange* _upb_ExtensionRanges_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(DescriptorProto_ExtensionRange) * const* protos,
const upb_MessageDef* m);
const UPB_DESC(DescriptorProto_ExtensionRange*) const* protos,
const UPB_DESC(FeatureSet*) parent_features, const upb_MessageDef* m);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -19,8 +19,6 @@ extern "C" {
upb_FieldDef* _upb_FieldDef_At(const upb_FieldDef* f, int i);
const upb_MiniTableExtension* _upb_FieldDef_ExtensionMiniTable(
const upb_FieldDef* f);
bool _upb_FieldDef_IsClosedEnum(const upb_FieldDef* f);
bool _upb_FieldDef_IsProto3Optional(const upb_FieldDef* f);
int _upb_FieldDef_LayoutIndex(const upb_FieldDef* f);
@@ -31,16 +29,19 @@ void _upb_FieldDef_BuildMiniTableExtension(upb_DefBuilder* ctx,
const upb_FieldDef* f);
// Allocate and initialize an array of |n| extensions (field defs).
upb_FieldDef* _upb_Extensions_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto) * const* protos, const char* prefix,
upb_MessageDef* m);
upb_FieldDef* _upb_Extensions_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const char* prefix, upb_MessageDef* m);
// Allocate and initialize an array of |n| field defs.
upb_FieldDef* _upb_FieldDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto) * const* protos, const char* prefix,
upb_MessageDef* m, bool* is_sorted);
upb_FieldDef* _upb_FieldDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const char* prefix, upb_MessageDef* m,
bool* is_sorted);
// Allocate and return a list of pointers to the |n| field defs in |ff|,
// sorted by field number.

View File

@@ -30,9 +30,12 @@ void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx,
void _upb_MessageDef_Resolve(upb_DefBuilder* ctx, upb_MessageDef* m);
// Allocate and initialize an array of |n| message defs.
upb_MessageDef* _upb_MessageDefs_New(
upb_DefBuilder* ctx, int n, const UPB_DESC(DescriptorProto) * const* protos,
const upb_MessageDef* containing_type);
upb_MessageDef* _upb_MessageDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(DescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*)
parent_features,
const upb_MessageDef* containing_type);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -20,9 +20,11 @@ extern "C" {
upb_MethodDef* _upb_MethodDef_At(const upb_MethodDef* m, int i);
// Allocate and initialize an array of |n| method defs owned by |s|.
upb_MethodDef* _upb_MethodDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(MethodDescriptorProto) * const* protos, upb_ServiceDef* s);
upb_MethodDef* _upb_MethodDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(MethodDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
upb_ServiceDef* s);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -22,9 +22,11 @@ void _upb_OneofDef_Insert(upb_DefBuilder* ctx, upb_OneofDef* o,
const upb_FieldDef* f, const char* name, size_t size);
// Allocate and initialize an array of |n| oneof defs owned by |m|.
upb_OneofDef* _upb_OneofDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(OneofDescriptorProto) * const* protos, upb_MessageDef* m);
upb_OneofDef* _upb_OneofDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(OneofDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
upb_MessageDef* m);
size_t _upb_OneofDefs_Finalize(upb_DefBuilder* ctx, upb_MessageDef* m);

View File

@@ -20,9 +20,11 @@ extern "C" {
upb_ServiceDef* _upb_ServiceDef_At(const upb_ServiceDef* s, int i);
// Allocate and initialize an array of |n| service defs.
upb_ServiceDef* _upb_ServiceDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(ServiceDescriptorProto) * const* protos);
upb_ServiceDef* _upb_ServiceDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(ServiceDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*)
parent_features);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -0,0 +1,20 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_REFLECTION_UPB_EDITION_DEFAULTS_H_
#define UPB_REFLECTION_UPB_EDITION_DEFAULTS_H_
// This file contains the serialized FeatureSetDefaults object for
// language-independent features and (possibly at some point) for upb-specific
// features. This is used for feature resolution under Editions.
// NOLINTBEGIN
// clang-format off
#define UPB_INTERNAL_UPB_EDITION_DEFAULTS "\n\023\030\204\007\"\000*\014\010\001\020\002\030\002 \003(\0010\002\n\023\030\347\007\"\000*\014\010\002\020\001\030\001 \002(\0010\001\n\023\030\350\007\"\014\010\001\020\001\030\001 \002(\0010\001*\000 \346\007(\350\007"
// clang-format on
// NOLINTEND
#endif // UPB_REFLECTION_UPB_EDITION_DEFAULTS_H_

View File

@@ -7,17 +7,23 @@
#include "upb/reflection/message.h"
#include <stdint.h>
#include <string.h>
#include "upb/hash/common.h"
#include "upb/mem/arena.h"
#include "upb/message/accessors.h"
#include "upb/message/array.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/message.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/message.h"
#include "upb/mini_table/message.h"
#include "upb/reflection/def.h"
#include "upb/reflection/def_pool.h"
#include "upb/reflection/def_type.h"
#include "upb/reflection/internal/field_def.h"
#include "upb/reflection/message_def.h"
#include "upb/reflection/oneof_def.h"
@@ -25,12 +31,18 @@
#include "upb/port/def.inc"
bool upb_Message_HasFieldByDef(const upb_Message* msg, const upb_FieldDef* f) {
const upb_MiniTableField* m_f = upb_FieldDef_MiniTable(f);
UPB_ASSERT(upb_FieldDef_HasPresence(f));
return upb_Message_HasField(msg, upb_FieldDef_MiniTable(f));
if (upb_MiniTableField_IsExtension(m_f)) {
return upb_Message_HasExtension(msg, (const upb_MiniTableExtension*)m_f);
} else {
return upb_Message_HasBaseField(msg, m_f);
}
}
const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
const upb_OneofDef* o) {
const upb_FieldDef* upb_Message_WhichOneofByDef(const upb_Message* msg,
const upb_OneofDef* o) {
const upb_FieldDef* f = upb_OneofDef_Field(o, 0);
if (upb_OneofDef_IsSynthetic(o)) {
UPB_ASSERT(upb_OneofDef_FieldCount(o) == 1);
@@ -47,14 +59,13 @@ const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
upb_MessageValue upb_Message_GetFieldByDef(const upb_Message* msg,
const upb_FieldDef* f) {
upb_MessageValue default_val = upb_FieldDef_Default(f);
upb_MessageValue ret;
_upb_Message_GetField(msg, upb_FieldDef_MiniTable(f), &default_val, &ret);
return ret;
return upb_Message_GetField(msg, upb_FieldDef_MiniTable(f), default_val);
}
upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
const upb_FieldDef* f,
upb_Arena* a) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
UPB_ASSERT(upb_FieldDef_IsSubMessage(f) || upb_FieldDef_IsRepeated(f));
if (upb_FieldDef_HasPresence(f) && !upb_Message_HasFieldByDef(msg, f)) {
// We need to skip the upb_Message_GetFieldByDef() call in this case.
@@ -93,35 +104,53 @@ make:
bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f,
upb_MessageValue val, upb_Arena* a) {
return _upb_Message_SetField(msg, upb_FieldDef_MiniTable(f), &val, a);
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_MiniTableField* m_f = upb_FieldDef_MiniTable(f);
if (upb_MiniTableField_IsExtension(m_f)) {
return upb_Message_SetExtension(msg, (const upb_MiniTableExtension*)m_f,
&val, a);
} else {
upb_Message_SetBaseField(msg, m_f, &val);
return true;
}
}
void upb_Message_ClearFieldByDef(upb_Message* msg, const upb_FieldDef* f) {
upb_Message_ClearField(msg, upb_FieldDef_MiniTable(f));
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_MiniTableField* m_f = upb_FieldDef_MiniTable(f);
if (upb_MiniTableField_IsExtension(m_f)) {
upb_Message_ClearExtension(msg, (const upb_MiniTableExtension*)m_f);
} else {
upb_Message_ClearBaseField(msg, m_f);
}
}
void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Clear(msg, upb_MessageDef_MiniTable(m));
}
bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool, const upb_FieldDef** out_f,
upb_MessageValue* out_val, size_t* iter) {
const upb_MiniTable* mt = upb_MessageDef_MiniTable(m);
size_t i = *iter;
size_t n = upb_MessageDef_FieldCount(m);
size_t n = upb_MiniTable_FieldCount(mt);
upb_MessageValue zero = upb_MessageValue_Zero();
UPB_UNUSED(ext_pool);
// Iterate over normal fields, returning the first one that is set.
while (++i < n) {
const upb_FieldDef* f = upb_MessageDef_Field(m, i);
const upb_MiniTableField* field = upb_FieldDef_MiniTable(f);
upb_MessageValue val = upb_Message_GetFieldByDef(msg, f);
const upb_MiniTableField* field = upb_MiniTable_GetFieldByIndex(mt, i);
upb_MessageValue val = upb_Message_GetField(msg, field, zero);
// Skip field if unset or empty.
if (upb_MiniTableField_HasPresence(field)) {
if (!upb_Message_HasFieldByDef(msg, f)) continue;
if (!upb_Message_HasBaseField(msg, field)) continue;
} else {
switch (upb_FieldMode_Get(field)) {
switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(field)) {
case kUpb_FieldMode_Map:
if (!val.map_val || upb_Map_Size(val.map_val) == 0) continue;
break;
@@ -129,13 +158,15 @@ bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
if (!val.array_val || upb_Array_Size(val.array_val) == 0) continue;
break;
case kUpb_FieldMode_Scalar:
if (!_upb_MiniTable_ValueIsNonZero(&val, field)) continue;
if (UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(field, &val))
continue;
break;
}
}
*out_val = val;
*out_f = f;
*out_f =
upb_MessageDef_FindFieldByNumber(m, upb_MiniTableField_Number(field));
*iter = i;
return true;
}
@@ -143,7 +174,7 @@ bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
if (ext_pool) {
// Return any extensions that are set.
size_t count;
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &count);
const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getexts)(msg, &count);
if (i - n < count) {
ext += count - 1 - (i - n);
memcpy(out_val, &ext->data, sizeof(*out_val));
@@ -159,6 +190,7 @@ bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
bool _upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
int depth) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
size_t iter = kUpb_Message_Begin;
const upb_FieldDef* f;
upb_MessageValue val;

View File

@@ -12,8 +12,7 @@
#include "upb/mem/arena.h"
#include "upb/message/map.h"
#include "upb/message/types.h"
#include "upb/message/value.h" // IWYU pragma: export
#include "upb/message/message.h"
#include "upb/reflection/common.h"
// Must be last.
@@ -31,8 +30,8 @@ UPB_API upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
upb_Arena* a);
// Returns the field that is set in the oneof, or NULL if none are set.
UPB_API const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
const upb_OneofDef* o);
UPB_API const upb_FieldDef* upb_Message_WhichOneofByDef(const upb_Message* msg,
const upb_OneofDef* o);
// Clear all data and unknown fields.
void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m);
@@ -72,9 +71,10 @@ UPB_API bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f,
#define kUpb_Message_Begin -1
bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool, const upb_FieldDef** f,
upb_MessageValue* val, size_t* iter);
UPB_API bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool,
const upb_FieldDef** f, upb_MessageValue* val,
size_t* iter);
// Clears all unknown field data from this message and all submessages.
UPB_API bool upb_Message_DiscardUnknown(upb_Message* msg,

View File

@@ -7,13 +7,22 @@
#include "upb/reflection/internal/message_def.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/hash/int_table.h"
#include "upb/hash/str_table.h"
#include "upb/mem/arena.h"
#include "upb/mini_descriptor/decode.h"
#include "upb/mini_descriptor/internal/encode.h"
#include "upb/mini_descriptor/internal/modifiers.h"
#include "upb/mini_table/file.h"
#include "upb/mini_table/message.h"
#include "upb/reflection/def.h"
#include "upb/reflection/def_type.h"
#include "upb/reflection/internal/def_builder.h"
#include "upb/reflection/internal/desc_state.h"
#include "upb/reflection/internal/enum_def.h"
@@ -28,7 +37,8 @@
#include "upb/port/def.inc"
struct upb_MessageDef {
const UPB_DESC(MessageOptions) * opts;
const UPB_DESC(MessageOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_MiniTable* layout;
const upb_FileDef* file;
const upb_MessageDef* containing_type;
@@ -66,6 +76,9 @@ struct upb_MessageDef {
bool in_message_set;
bool is_sorted;
upb_WellKnown well_known_type;
#if UINTPTR_MAX == 0xffffffff
uint32_t padding; // Increase size to a multiple of 8.
#endif
};
static void assign_msg_wellknowntype(upb_MessageDef* m) {
@@ -134,6 +147,11 @@ bool upb_MessageDef_HasOptions(const upb_MessageDef* m) {
return m->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_MessageDef_ResolvedFeatures(const upb_MessageDef* m) {
return m->resolved_features;
}
const char* upb_MessageDef_FullName(const upb_MessageDef* m) {
return m->full_name;
}
@@ -397,10 +415,12 @@ void _upb_MessageDef_InsertField(upb_DefBuilder* ctx, upb_MessageDef* m,
_upb_MessageDef_Insert(m, shortname, shortnamelen, field_v, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
// TODO: Once editions is supported this should turn into a
// check on LEGACY_BEST_EFFORT
if (strcmp(shortname, json_name) != 0 &&
upb_FileDef_Syntax(m->file) == kUpb_Syntax_Proto3 &&
bool skip_json_conflicts =
UPB_DESC(MessageOptions_deprecated_legacy_json_field_conflicts)(
upb_MessageDef_Options(m));
if (!skip_json_conflicts && strcmp(shortname, json_name) != 0 &&
UPB_DESC(FeatureSet_json_format)(m->resolved_features) ==
UPB_DESC(FeatureSet_ALLOW) &&
upb_strtable_lookup(&m->ntof, json_name, &v)) {
_upb_DefBuilder_Errf(
ctx, "duplicate json_name for (%s) with original field name (%s)",
@@ -408,14 +428,16 @@ void _upb_MessageDef_InsertField(upb_DefBuilder* ctx, upb_MessageDef* m,
}
if (upb_strtable_lookup(&m->jtof, json_name, &v)) {
_upb_DefBuilder_Errf(ctx, "duplicate json_name (%s)", json_name);
if (!skip_json_conflicts) {
_upb_DefBuilder_Errf(ctx, "duplicate json_name (%s)", json_name);
}
} else {
const size_t json_size = strlen(json_name);
ok = upb_strtable_insert(&m->jtof, json_name, json_size,
upb_value_constptr(f), ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
}
const size_t json_size = strlen(json_name);
ok = upb_strtable_insert(&m->jtof, json_name, json_size,
upb_value_constptr(f), ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
if (upb_inttable_lookup(&m->itof, field_number, NULL)) {
_upb_DefBuilder_Errf(ctx, "duplicate field number (%u)", field_number);
}
@@ -428,9 +450,8 @@ void _upb_MessageDef_CreateMiniTable(upb_DefBuilder* ctx, upb_MessageDef* m) {
if (ctx->layout == NULL) {
m->layout = _upb_MessageDef_MakeMiniTable(ctx, m);
} else {
UPB_ASSERT(ctx->msg_count < ctx->layout->msg_count);
m->layout = ctx->layout->msgs[ctx->msg_count++];
UPB_ASSERT(m->field_count == m->layout->field_count);
m->layout = upb_MiniTableFile_Message(ctx->layout, ctx->msg_count++);
UPB_ASSERT(m->field_count == upb_MiniTable_FieldCount(m->layout));
// We don't need the result of this call, but it will assign layout_index
// for all the fields in O(n lg n) time.
@@ -466,9 +487,9 @@ void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx,
UPB_ASSERT(layout_index < m->field_count);
upb_MiniTableField* mt_f =
(upb_MiniTableField*)&m->layout->fields[layout_index];
(upb_MiniTableField*)&m->layout->UPB_PRIVATE(fields)[layout_index];
if (sub_m) {
if (!mt->subs) {
if (!mt->UPB_PRIVATE(subs)) {
_upb_DefBuilder_Errf(ctx, "unexpected submsg for (%s)", m->full_name);
}
UPB_ASSERT(mt_f);
@@ -488,8 +509,9 @@ void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx,
for (int i = 0; i < m->field_count; i++) {
const upb_FieldDef* f = upb_MessageDef_Field(m, i);
const int layout_index = _upb_FieldDef_LayoutIndex(f);
UPB_ASSERT(layout_index < m->layout->field_count);
const upb_MiniTableField* mt_f = &m->layout->fields[layout_index];
UPB_ASSERT(layout_index < upb_MiniTable_FieldCount(m->layout));
const upb_MiniTableField* mt_f =
&m->layout->UPB_PRIVATE(fields)[layout_index];
UPB_ASSERT(upb_FieldDef_Type(f) == upb_MiniTableField_Type(mt_f));
UPB_ASSERT(upb_FieldDef_CType(f) == upb_MiniTableField_CType(mt_f));
UPB_ASSERT(upb_FieldDef_HasPresence(f) ==
@@ -517,7 +539,8 @@ static bool _upb_MessageDef_ValidateUtf8(const upb_MessageDef* m) {
static uint64_t _upb_MessageDef_Modifiers(const upb_MessageDef* m) {
uint64_t out = 0;
if (upb_FileDef_Syntax(m->file) == kUpb_Syntax_Proto3) {
if (UPB_DESC(FeatureSet_repeated_field_encoding(m->resolved_features)) ==
UPB_DESC(FeatureSet_PACKED)) {
out |= kUpb_MessageModifier_DefaultIsPacked;
}
@@ -631,7 +654,8 @@ static upb_StringView* _upb_ReservedNames_New(upb_DefBuilder* ctx, int n,
}
static void create_msgdef(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(DescriptorProto) * msg_proto,
const UPB_DESC(DescriptorProto*) msg_proto,
const UPB_DESC(FeatureSet*) parent_features,
const upb_MessageDef* containing_type,
upb_MessageDef* m) {
const UPB_DESC(OneofDescriptorProto)* const* oneofs;
@@ -643,6 +667,10 @@ static void create_msgdef(upb_DefBuilder* ctx, const char* prefix,
size_t n_ext_range, n_res_range, n_res_name;
upb_StringView name;
UPB_DEF_SET_OPTIONS(m->opts, DescriptorProto, MessageOptions, msg_proto);
m->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(MessageOptions_features)(m->opts));
// Must happen before _upb_DefBuilder_Add()
m->file = _upb_DefBuilder_File(ctx);
@@ -671,14 +699,12 @@ static void create_msgdef(upb_DefBuilder* ctx, const char* prefix,
ok = upb_strtable_init(&m->jtof, n_field, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
UPB_DEF_SET_OPTIONS(m->opts, DescriptorProto, MessageOptions, msg_proto);
m->oneof_count = n_oneof;
m->oneofs = _upb_OneofDefs_New(ctx, n_oneof, oneofs, m);
m->oneofs = _upb_OneofDefs_New(ctx, n_oneof, oneofs, m->resolved_features, m);
m->field_count = n_field;
m->fields =
_upb_FieldDefs_New(ctx, n_field, fields, m->full_name, m, &m->is_sorted);
m->fields = _upb_FieldDefs_New(ctx, n_field, fields, m->resolved_features,
m->full_name, m, &m->is_sorted);
// Message Sets may not contain fields.
if (UPB_UNLIKELY(UPB_DESC(MessageOptions_message_set_wire_format)(m->opts))) {
@@ -688,7 +714,8 @@ static void create_msgdef(upb_DefBuilder* ctx, const char* prefix,
}
m->ext_range_count = n_ext_range;
m->ext_ranges = _upb_ExtensionRanges_New(ctx, n_ext_range, ext_ranges, m);
m->ext_ranges = _upb_ExtensionRanges_New(ctx, n_ext_range, ext_ranges,
m->resolved_features, m);
m->res_range_count = n_res_range;
m->res_ranges =
@@ -706,23 +733,29 @@ static void create_msgdef(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(EnumDescriptorProto)* const* enums =
UPB_DESC(DescriptorProto_enum_type)(msg_proto, &n_enum);
m->nested_enum_count = n_enum;
m->nested_enums = _upb_EnumDefs_New(ctx, n_enum, enums, m);
m->nested_enums =
_upb_EnumDefs_New(ctx, n_enum, enums, m->resolved_features, m);
const UPB_DESC(FieldDescriptorProto)* const* exts =
UPB_DESC(DescriptorProto_extension)(msg_proto, &n_ext);
m->nested_ext_count = n_ext;
m->nested_exts = _upb_Extensions_New(ctx, n_ext, exts, m->full_name, m);
m->nested_exts = _upb_Extensions_New(ctx, n_ext, exts, m->resolved_features,
m->full_name, m);
const UPB_DESC(DescriptorProto)* const* msgs =
UPB_DESC(DescriptorProto_nested_type)(msg_proto, &n_msg);
m->nested_msg_count = n_msg;
m->nested_msgs = _upb_MessageDefs_New(ctx, n_msg, msgs, m);
m->nested_msgs =
_upb_MessageDefs_New(ctx, n_msg, msgs, m->resolved_features, m);
}
// Allocate and initialize an array of |n| message defs.
upb_MessageDef* _upb_MessageDefs_New(
upb_DefBuilder* ctx, int n, const UPB_DESC(DescriptorProto) * const* protos,
const upb_MessageDef* containing_type) {
upb_MessageDef* _upb_MessageDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(DescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*)
parent_features,
const upb_MessageDef* containing_type) {
_upb_DefType_CheckPadding(sizeof(upb_MessageDef));
const char* name = containing_type ? containing_type->full_name
@@ -730,7 +763,8 @@ upb_MessageDef* _upb_MessageDefs_New(
upb_MessageDef* m = _upb_DefBuilder_Alloc(ctx, sizeof(upb_MessageDef) * n);
for (int i = 0; i < n; i++) {
create_msgdef(ctx, name, protos[i], containing_type, &m[i]);
create_msgdef(ctx, name, protos[i], parent_features, containing_type,
&m[i]);
}
return m;
}

View File

@@ -136,6 +136,8 @@ int upb_MessageDef_RealOneofCount(const upb_MessageDef* m);
const UPB_DESC(MessageOptions) *
upb_MessageDef_Options(const upb_MessageDef* m);
const UPB_DESC(FeatureSet) *
upb_MessageDef_ResolvedFeatures(const upb_MessageDef* m);
upb_StringView upb_MessageDef_ReservedName(const upb_MessageDef* m, int i);
int upb_MessageDef_ReservedNameCount(const upb_MessageDef* m);

View File

@@ -15,7 +15,8 @@
#include "upb/port/def.inc"
struct upb_MethodDef {
const UPB_DESC(MethodOptions) * opts;
const UPB_DESC(MethodOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
upb_ServiceDef* service;
const char* full_name;
const upb_MessageDef* input_type;
@@ -41,6 +42,11 @@ bool upb_MethodDef_HasOptions(const upb_MethodDef* m) {
return m->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_MethodDef_ResolvedFeatures(const upb_MethodDef* m) {
return m->resolved_features;
}
const char* upb_MethodDef_FullName(const upb_MethodDef* m) {
return m->full_name;
}
@@ -68,8 +74,14 @@ bool upb_MethodDef_ServerStreaming(const upb_MethodDef* m) {
}
static void create_method(upb_DefBuilder* ctx,
const UPB_DESC(MethodDescriptorProto) * method_proto,
const UPB_DESC(MethodDescriptorProto*) method_proto,
const UPB_DESC(FeatureSet*) parent_features,
upb_ServiceDef* s, upb_MethodDef* m) {
UPB_DEF_SET_OPTIONS(m->opts, MethodDescriptorProto, MethodOptions,
method_proto);
m->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(MethodOptions_features)(m->opts));
upb_StringView name = UPB_DESC(MethodDescriptorProto_name)(method_proto);
m->service = s;
@@ -87,18 +99,17 @@ static void create_method(upb_DefBuilder* ctx,
ctx, m->full_name, m->full_name,
UPB_DESC(MethodDescriptorProto_output_type)(method_proto),
UPB_DEFTYPE_MSG);
UPB_DEF_SET_OPTIONS(m->opts, MethodDescriptorProto, MethodOptions,
method_proto);
}
// Allocate and initialize an array of |n| method defs belonging to |s|.
upb_MethodDef* _upb_MethodDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(MethodDescriptorProto) * const* protos, upb_ServiceDef* s) {
upb_MethodDef* _upb_MethodDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(MethodDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
upb_ServiceDef* s) {
upb_MethodDef* m = _upb_DefBuilder_Alloc(ctx, sizeof(upb_MethodDef) * n);
for (int i = 0; i < n; i++) {
create_method(ctx, protos[i], s, &m[i]);
create_method(ctx, protos[i], parent_features, s, &m[i]);
m[i].index = i;
}
return m;

View File

@@ -19,16 +19,19 @@
extern "C" {
#endif
bool upb_MethodDef_ClientStreaming(const upb_MethodDef* m);
UPB_API bool upb_MethodDef_ClientStreaming(const upb_MethodDef* m);
const char* upb_MethodDef_FullName(const upb_MethodDef* m);
bool upb_MethodDef_HasOptions(const upb_MethodDef* m);
int upb_MethodDef_Index(const upb_MethodDef* m);
const upb_MessageDef* upb_MethodDef_InputType(const upb_MethodDef* m);
const char* upb_MethodDef_Name(const upb_MethodDef* m);
const UPB_DESC(MethodOptions) * upb_MethodDef_Options(const upb_MethodDef* m);
const upb_MessageDef* upb_MethodDef_OutputType(const upb_MethodDef* m);
bool upb_MethodDef_ServerStreaming(const upb_MethodDef* m);
const upb_ServiceDef* upb_MethodDef_Service(const upb_MethodDef* m);
UPB_API const upb_MessageDef* upb_MethodDef_InputType(const upb_MethodDef* m);
UPB_API const char* upb_MethodDef_Name(const upb_MethodDef* m);
UPB_API const UPB_DESC(MethodOptions) *
upb_MethodDef_Options(const upb_MethodDef* m);
const UPB_DESC(FeatureSet) *
upb_MethodDef_ResolvedFeatures(const upb_MethodDef* m);
UPB_API const upb_MessageDef* upb_MethodDef_OutputType(const upb_MethodDef* m);
UPB_API bool upb_MethodDef_ServerStreaming(const upb_MethodDef* m);
UPB_API const upb_ServiceDef* upb_MethodDef_Service(const upb_MethodDef* m);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -22,7 +22,8 @@
#include "upb/port/def.inc"
struct upb_OneofDef {
const UPB_DESC(OneofOptions) * opts;
const UPB_DESC(OneofOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_MessageDef* parent;
const char* full_name;
int field_count;
@@ -30,9 +31,6 @@ struct upb_OneofDef {
const upb_FieldDef** fields;
upb_strtable ntof; // lookup a field by name
upb_inttable itof; // lookup a field by number (index)
#if UINTPTR_MAX == 0xffffffff
uint32_t padding; // Increase size to a multiple of 8.
#endif
};
upb_OneofDef* _upb_OneofDef_At(const upb_OneofDef* o, int i) {
@@ -47,6 +45,11 @@ bool upb_OneofDef_HasOptions(const upb_OneofDef* o) {
return o->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_OneofDef_ResolvedFeatures(const upb_OneofDef* o) {
return o->resolved_features;
}
const char* upb_OneofDef_FullName(const upb_OneofDef* o) {
return o->full_name;
}
@@ -166,9 +169,15 @@ size_t _upb_OneofDefs_Finalize(upb_DefBuilder* ctx, upb_MessageDef* m) {
}
static void create_oneofdef(upb_DefBuilder* ctx, upb_MessageDef* m,
const UPB_DESC(OneofDescriptorProto) * oneof_proto,
const UPB_DESC(OneofDescriptorProto*) oneof_proto,
const UPB_DESC(FeatureSet*) parent_features,
const upb_OneofDef* _o) {
upb_OneofDef* o = (upb_OneofDef*)_o;
UPB_DEF_SET_OPTIONS(o->opts, OneofDescriptorProto, OneofOptions, oneof_proto);
o->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(OneofOptions_features)(o->opts));
upb_StringView name = UPB_DESC(OneofDescriptorProto_name)(oneof_proto);
o->parent = m;
@@ -177,8 +186,6 @@ static void create_oneofdef(upb_DefBuilder* ctx, upb_MessageDef* m,
o->field_count = 0;
o->synthetic = false;
UPB_DEF_SET_OPTIONS(o->opts, OneofDescriptorProto, OneofOptions, oneof_proto);
if (upb_MessageDef_FindByNameWithSize(m, name.data, name.size, NULL, NULL)) {
_upb_DefBuilder_Errf(ctx, "duplicate oneof name (%s)", o->full_name);
}
@@ -195,14 +202,16 @@ static void create_oneofdef(upb_DefBuilder* ctx, upb_MessageDef* m,
}
// Allocate and initialize an array of |n| oneof defs.
upb_OneofDef* _upb_OneofDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(OneofDescriptorProto) * const* protos, upb_MessageDef* m) {
upb_OneofDef* _upb_OneofDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(OneofDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
upb_MessageDef* m) {
_upb_DefType_CheckPadding(sizeof(upb_OneofDef));
upb_OneofDef* o = _upb_DefBuilder_Alloc(ctx, sizeof(upb_OneofDef) * n);
for (int i = 0; i < n; i++) {
create_oneofdef(ctx, m, protos[i], &o[i]);
create_oneofdef(ctx, m, protos[i], parent_features, &o[i]);
}
return o;
}

View File

@@ -36,7 +36,9 @@ const upb_FieldDef* upb_OneofDef_LookupNumber(const upb_OneofDef* o,
uint32_t num);
UPB_API const char* upb_OneofDef_Name(const upb_OneofDef* o);
int upb_OneofDef_numfields(const upb_OneofDef* o);
const UPB_DESC(OneofOptions) * upb_OneofDef_Options(const upb_OneofDef* o);
const UPB_DESC(OneofOptions*) upb_OneofDef_Options(const upb_OneofDef* o);
const UPB_DESC(FeatureSet*)
upb_OneofDef_ResolvedFeatures(const upb_OneofDef* o);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -16,12 +16,16 @@
#include "upb/port/def.inc"
struct upb_ServiceDef {
const UPB_DESC(ServiceOptions) * opts;
const UPB_DESC(ServiceOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_FileDef* file;
const char* full_name;
upb_MethodDef* methods;
int method_count;
int index;
#if UINTPTR_MAX == 0xffffffff
uint32_t padding; // Increase size to a multiple of 8.
#endif
};
upb_ServiceDef* _upb_ServiceDef_At(const upb_ServiceDef* s, int index) {
@@ -37,6 +41,11 @@ bool upb_ServiceDef_HasOptions(const upb_ServiceDef* s) {
return s->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_ServiceDef_ResolvedFeatures(const upb_ServiceDef* s) {
return s->resolved_features;
}
const char* upb_ServiceDef_FullName(const upb_ServiceDef* s) {
return s->full_name;
}
@@ -72,37 +81,40 @@ const upb_MethodDef* upb_ServiceDef_FindMethodByName(const upb_ServiceDef* s,
}
static void create_service(upb_DefBuilder* ctx,
const UPB_DESC(ServiceDescriptorProto) * svc_proto,
const UPB_DESC(ServiceDescriptorProto*) svc_proto,
const UPB_DESC(FeatureSet*) parent_features,
upb_ServiceDef* s) {
upb_StringView name;
size_t n;
UPB_DEF_SET_OPTIONS(s->opts, ServiceDescriptorProto, ServiceOptions,
svc_proto);
s->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(ServiceOptions_features)(s->opts));
// Must happen before _upb_DefBuilder_Add()
s->file = _upb_DefBuilder_File(ctx);
name = UPB_DESC(ServiceDescriptorProto_name)(svc_proto);
upb_StringView name = UPB_DESC(ServiceDescriptorProto_name)(svc_proto);
const char* package = _upb_FileDef_RawPackage(s->file);
s->full_name = _upb_DefBuilder_MakeFullName(ctx, package, name);
_upb_DefBuilder_Add(ctx, s->full_name,
_upb_DefType_Pack(s, UPB_DEFTYPE_SERVICE));
size_t n;
const UPB_DESC(MethodDescriptorProto)* const* methods =
UPB_DESC(ServiceDescriptorProto_method)(svc_proto, &n);
s->method_count = n;
s->methods = _upb_MethodDefs_New(ctx, n, methods, s);
UPB_DEF_SET_OPTIONS(s->opts, ServiceDescriptorProto, ServiceOptions,
svc_proto);
s->methods = _upb_MethodDefs_New(ctx, n, methods, s->resolved_features, s);
}
upb_ServiceDef* _upb_ServiceDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(ServiceDescriptorProto) * const* protos) {
upb_ServiceDef* _upb_ServiceDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(ServiceDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*)
parent_features) {
_upb_DefType_CheckPadding(sizeof(upb_ServiceDef));
upb_ServiceDef* s = _upb_DefBuilder_Alloc(ctx, sizeof(upb_ServiceDef) * n);
for (int i = 0; i < n; i++) {
create_service(ctx, protos[i], &s[i]);
create_service(ctx, protos[i], parent_features, &s[i]);
s[i].index = i;
}
return s;

View File

@@ -19,17 +19,20 @@
extern "C" {
#endif
const upb_FileDef* upb_ServiceDef_File(const upb_ServiceDef* s);
UPB_API const upb_FileDef* upb_ServiceDef_File(const upb_ServiceDef* s);
const upb_MethodDef* upb_ServiceDef_FindMethodByName(const upb_ServiceDef* s,
const char* name);
const char* upb_ServiceDef_FullName(const upb_ServiceDef* s);
UPB_API const char* upb_ServiceDef_FullName(const upb_ServiceDef* s);
bool upb_ServiceDef_HasOptions(const upb_ServiceDef* s);
int upb_ServiceDef_Index(const upb_ServiceDef* s);
const upb_MethodDef* upb_ServiceDef_Method(const upb_ServiceDef* s, int i);
int upb_ServiceDef_MethodCount(const upb_ServiceDef* s);
UPB_API const upb_MethodDef* upb_ServiceDef_Method(const upb_ServiceDef* s,
int i);
UPB_API int upb_ServiceDef_MethodCount(const upb_ServiceDef* s);
const char* upb_ServiceDef_Name(const upb_ServiceDef* s);
const UPB_DESC(ServiceOptions) *
UPB_API const UPB_DESC(ServiceOptions) *
upb_ServiceDef_Options(const upb_ServiceDef* s);
const UPB_DESC(FeatureSet) *
upb_ServiceDef_ResolvedFeatures(const upb_ServiceDef* s);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -7,210 +7,81 @@
#include "upb/text/encode.h"
#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/lex/round_trip.h"
#include "upb/message/array.h"
#include "upb/message/internal/map_entry.h"
#include "upb/message/internal/map_sorter.h"
#include "upb/message/map.h"
#include "upb/port/vsnprintf_compat.h"
#include "upb/message/message.h"
#include "upb/message/value.h"
#include "upb/reflection/def.h"
#include "upb/reflection/message.h"
#include "upb/text/internal/encode.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/reader.h"
#include "upb/wire/types.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct {
char *buf, *ptr, *end;
size_t overflow;
int indent_depth;
int options;
const upb_DefPool* ext_pool;
_upb_mapsorter sorter;
} txtenc;
static void _upb_TextEncode_Msg(txtenc* e, const upb_Message* msg,
const upb_MessageDef* m);
static void txtenc_msg(txtenc* e, const upb_Message* msg,
const upb_MessageDef* m);
static void txtenc_putbytes(txtenc* e, const void* data, size_t len) {
size_t have = e->end - e->ptr;
if (UPB_LIKELY(have >= len)) {
memcpy(e->ptr, data, len);
e->ptr += len;
} else {
if (have) {
memcpy(e->ptr, data, have);
e->ptr += have;
}
e->overflow += (len - have);
}
}
static void txtenc_putstr(txtenc* e, const char* str) {
txtenc_putbytes(e, str, strlen(str));
}
static void txtenc_printf(txtenc* e, const char* fmt, ...) {
size_t n;
size_t have = e->end - e->ptr;
va_list args;
va_start(args, fmt);
n = _upb_vsnprintf(e->ptr, have, fmt, args);
va_end(args);
if (UPB_LIKELY(have > n)) {
e->ptr += n;
} else {
e->ptr = UPB_PTRADD(e->ptr, have);
e->overflow += (n - have);
}
}
static void txtenc_indent(txtenc* e) {
if ((e->options & UPB_TXTENC_SINGLELINE) == 0) {
int i = e->indent_depth;
while (i-- > 0) {
txtenc_putstr(e, " ");
}
}
}
static void txtenc_endfield(txtenc* e) {
if (e->options & UPB_TXTENC_SINGLELINE) {
txtenc_putstr(e, " ");
} else {
txtenc_putstr(e, "\n");
}
}
static void txtenc_enum(int32_t val, const upb_FieldDef* f, txtenc* e) {
static void _upb_TextEncode_Enum(int32_t val, const upb_FieldDef* f,
txtenc* e) {
const upb_EnumDef* e_def = upb_FieldDef_EnumSubDef(f);
const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(e_def, val);
if (ev) {
txtenc_printf(e, "%s", upb_EnumValueDef_Name(ev));
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%s", upb_EnumValueDef_Name(ev));
} else {
txtenc_printf(e, "%" PRId32, val);
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRId32, val);
}
}
static void txtenc_string(txtenc* e, upb_StringView str, bool bytes) {
const char* ptr = str.data;
const char* end = ptr + str.size;
txtenc_putstr(e, "\"");
while (ptr < end) {
switch (*ptr) {
case '\n':
txtenc_putstr(e, "\\n");
break;
case '\r':
txtenc_putstr(e, "\\r");
break;
case '\t':
txtenc_putstr(e, "\\t");
break;
case '\"':
txtenc_putstr(e, "\\\"");
break;
case '\'':
txtenc_putstr(e, "\\'");
break;
case '\\':
txtenc_putstr(e, "\\\\");
break;
default:
if ((bytes || (uint8_t)*ptr < 0x80) && !isprint(*ptr)) {
txtenc_printf(e, "\\%03o", (int)(uint8_t)*ptr);
} else {
txtenc_putbytes(e, ptr, 1);
}
break;
}
ptr++;
}
txtenc_putstr(e, "\"");
}
static void txtenc_field(txtenc* e, upb_MessageValue val,
const upb_FieldDef* f) {
txtenc_indent(e);
const upb_CType type = upb_FieldDef_CType(f);
static void _upb_TextEncode_Field(txtenc* e, upb_MessageValue val,
const upb_FieldDef* f) {
UPB_PRIVATE(_upb_TextEncode_Indent)(e);
const upb_CType ctype = upb_FieldDef_CType(f);
const bool is_ext = upb_FieldDef_IsExtension(f);
const char* full = upb_FieldDef_FullName(f);
const char* name = upb_FieldDef_Name(f);
if (type == kUpb_CType_Message) {
if (ctype == kUpb_CType_Message) {
if (is_ext) {
txtenc_printf(e, "[%s] {", full);
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "[%s] {", full);
} else {
txtenc_printf(e, "%s {", name);
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%s {", name);
}
txtenc_endfield(e);
UPB_PRIVATE(_upb_TextEncode_EndField)(e);
e->indent_depth++;
txtenc_msg(e, val.msg_val, upb_FieldDef_MessageSubDef(f));
_upb_TextEncode_Msg(e, val.msg_val, upb_FieldDef_MessageSubDef(f));
e->indent_depth--;
txtenc_indent(e);
txtenc_putstr(e, "}");
txtenc_endfield(e);
UPB_PRIVATE(_upb_TextEncode_Indent)(e);
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "}");
UPB_PRIVATE(_upb_TextEncode_EndField)(e);
return;
}
if (is_ext) {
txtenc_printf(e, "[%s]: ", full);
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "[%s]: ", full);
} else {
txtenc_printf(e, "%s: ", name);
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%s: ", name);
}
switch (type) {
case kUpb_CType_Bool:
txtenc_putstr(e, val.bool_val ? "true" : "false");
break;
case kUpb_CType_Float: {
char buf[32];
_upb_EncodeRoundTripFloat(val.float_val, buf, sizeof(buf));
txtenc_putstr(e, buf);
break;
}
case kUpb_CType_Double: {
char buf[32];
_upb_EncodeRoundTripDouble(val.double_val, buf, sizeof(buf));
txtenc_putstr(e, buf);
break;
}
case kUpb_CType_Int32:
txtenc_printf(e, "%" PRId32, val.int32_val);
break;
case kUpb_CType_UInt32:
txtenc_printf(e, "%" PRIu32, val.uint32_val);
break;
case kUpb_CType_Int64:
txtenc_printf(e, "%" PRId64, val.int64_val);
break;
case kUpb_CType_UInt64:
txtenc_printf(e, "%" PRIu64, val.uint64_val);
break;
case kUpb_CType_String:
txtenc_string(e, val.str_val, false);
break;
case kUpb_CType_Bytes:
txtenc_string(e, val.str_val, true);
break;
case kUpb_CType_Enum:
txtenc_enum(val.int32_val, f, e);
break;
default:
UPB_UNREACHABLE();
if (ctype == kUpb_CType_Enum) {
_upb_TextEncode_Enum(val.int32_val, f, e);
} else {
UPB_PRIVATE(_upb_TextEncode_Scalar)(e, val, ctype);
}
txtenc_endfield(e);
UPB_PRIVATE(_upb_TextEncode_EndField)(e);
}
/*
@@ -220,33 +91,34 @@ static void txtenc_field(txtenc* e, upb_MessageValue val,
* foo_field: 2
* foo_field: 3
*/
static void txtenc_array(txtenc* e, const upb_Array* arr,
const upb_FieldDef* f) {
static void _upb_TextEncode_Array(txtenc* e, const upb_Array* arr,
const upb_FieldDef* f) {
size_t i;
size_t size = upb_Array_Size(arr);
for (i = 0; i < size; i++) {
txtenc_field(e, upb_Array_Get(arr, i), f);
_upb_TextEncode_Field(e, upb_Array_Get(arr, i), f);
}
}
static void txtenc_mapentry(txtenc* e, upb_MessageValue key,
upb_MessageValue val, const upb_FieldDef* f) {
static void _upb_TextEncode_MapEntry(txtenc* e, upb_MessageValue key,
upb_MessageValue val,
const upb_FieldDef* f) {
const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef* key_f = upb_MessageDef_Field(entry, 0);
const upb_FieldDef* val_f = upb_MessageDef_Field(entry, 1);
txtenc_indent(e);
txtenc_printf(e, "%s {", upb_FieldDef_Name(f));
txtenc_endfield(e);
UPB_PRIVATE(_upb_TextEncode_Indent)(e);
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%s {", upb_FieldDef_Name(f));
UPB_PRIVATE(_upb_TextEncode_EndField)(e);
e->indent_depth++;
txtenc_field(e, key, key_f);
txtenc_field(e, val, val_f);
_upb_TextEncode_Field(e, key, key_f);
_upb_TextEncode_Field(e, val, val_f);
e->indent_depth--;
txtenc_indent(e);
txtenc_putstr(e, "}");
txtenc_endfield(e);
UPB_PRIVATE(_upb_TextEncode_Indent)(e);
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "}");
UPB_PRIVATE(_upb_TextEncode_EndField)(e);
}
/*
@@ -261,14 +133,17 @@ static void txtenc_mapentry(txtenc* e, upb_MessageValue key,
* value: 456
* }
*/
static void txtenc_map(txtenc* e, const upb_Map* map, const upb_FieldDef* f) {
static void _upb_TextEncode_Map(txtenc* e, const upb_Map* map,
const upb_FieldDef* f) {
if (e->options & UPB_TXTENC_NOSORT) {
size_t iter = kUpb_Map_Begin;
upb_MessageValue key, val;
while (upb_Map_Next(map, &key, &val, &iter)) {
txtenc_mapentry(e, key, val, f);
_upb_TextEncode_MapEntry(e, key, val, f);
}
} else {
if (upb_Map_Size(map) == 0) return;
const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef* key_f = upb_MessageDef_Field(entry, 0);
_upb_sortedmap sorted;
@@ -277,137 +152,27 @@ static void txtenc_map(txtenc* e, const upb_Map* map, const upb_FieldDef* f) {
_upb_mapsorter_pushmap(&e->sorter, upb_FieldDef_Type(key_f), map, &sorted);
while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) {
upb_MessageValue key, val;
memcpy(&key, &ent.data.k, sizeof(key));
memcpy(&val, &ent.data.v, sizeof(val));
txtenc_mapentry(e, key, val, f);
memcpy(&key, &ent.k, sizeof(key));
memcpy(&val, &ent.v, sizeof(val));
_upb_TextEncode_MapEntry(e, key, val, f);
}
_upb_mapsorter_popmap(&e->sorter, &sorted);
}
}
#define CHK(x) \
do { \
if (!(x)) { \
return false; \
} \
} while (0)
/*
* Unknown fields are printed by number.
*
* 1001: 123
* 1002: "hello"
* 1006: 0xdeadbeef
* 1003: {
* 1: 111
* }
*/
static const char* txtenc_unknown(txtenc* e, const char* ptr,
upb_EpsCopyInputStream* stream,
int groupnum) {
// We are guaranteed that the unknown data is valid wire format, and will not
// contain tag zero.
uint32_t end_group = groupnum > 0
? ((groupnum << kUpb_WireReader_WireTypeBits) |
kUpb_WireType_EndGroup)
: 0;
while (!upb_EpsCopyInputStream_IsDone(stream, &ptr)) {
uint32_t tag;
CHK(ptr = upb_WireReader_ReadTag(ptr, &tag));
if (tag == end_group) return ptr;
txtenc_indent(e);
txtenc_printf(e, "%d: ", (int)upb_WireReader_GetFieldNumber(tag));
switch (upb_WireReader_GetWireType(tag)) {
case kUpb_WireType_Varint: {
uint64_t val;
CHK(ptr = upb_WireReader_ReadVarint(ptr, &val));
txtenc_printf(e, "%" PRIu64, val);
break;
}
case kUpb_WireType_32Bit: {
uint32_t val;
ptr = upb_WireReader_ReadFixed32(ptr, &val);
txtenc_printf(e, "0x%08" PRIu32, val);
break;
}
case kUpb_WireType_64Bit: {
uint64_t val;
ptr = upb_WireReader_ReadFixed64(ptr, &val);
txtenc_printf(e, "0x%016" PRIu64, val);
break;
}
case kUpb_WireType_Delimited: {
int size;
char* start = e->ptr;
size_t start_overflow = e->overflow;
CHK(ptr = upb_WireReader_ReadSize(ptr, &size));
CHK(upb_EpsCopyInputStream_CheckDataSizeAvailable(stream, ptr, size));
// Speculatively try to parse as message.
txtenc_putstr(e, "{");
txtenc_endfield(e);
// EpsCopyInputStream can't back up, so create a sub-stream for the
// speculative parse.
upb_EpsCopyInputStream sub_stream;
const char* sub_ptr = upb_EpsCopyInputStream_GetAliasedPtr(stream, ptr);
upb_EpsCopyInputStream_Init(&sub_stream, &sub_ptr, size, true);
e->indent_depth++;
if (txtenc_unknown(e, sub_ptr, &sub_stream, -1)) {
ptr = upb_EpsCopyInputStream_Skip(stream, ptr, size);
e->indent_depth--;
txtenc_indent(e);
txtenc_putstr(e, "}");
} else {
// Didn't work out, print as raw bytes.
e->indent_depth--;
e->ptr = start;
e->overflow = start_overflow;
const char* str = ptr;
ptr = upb_EpsCopyInputStream_ReadString(stream, &str, size, NULL);
assert(ptr);
txtenc_string(e, (upb_StringView){.data = str, .size = size}, true);
}
break;
}
case kUpb_WireType_StartGroup:
txtenc_putstr(e, "{");
txtenc_endfield(e);
e->indent_depth++;
CHK(ptr = txtenc_unknown(e, ptr, stream,
upb_WireReader_GetFieldNumber(tag)));
e->indent_depth--;
txtenc_indent(e);
txtenc_putstr(e, "}");
break;
default:
return NULL;
}
txtenc_endfield(e);
}
return end_group == 0 && !upb_EpsCopyInputStream_IsError(stream) ? ptr : NULL;
}
#undef CHK
static void txtenc_msg(txtenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
static void _upb_TextEncode_Msg(txtenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
size_t iter = kUpb_Message_Begin;
const upb_FieldDef* f;
upb_MessageValue val;
while (upb_Message_Next(msg, m, e->ext_pool, &f, &val, &iter)) {
if (upb_FieldDef_IsMap(f)) {
txtenc_map(e, val.map_val, f);
_upb_TextEncode_Map(e, val.map_val, f);
} else if (upb_FieldDef_IsRepeated(f)) {
txtenc_array(e, val.array_val, f);
_upb_TextEncode_Array(e, val.array_val, f);
} else {
txtenc_field(e, val, f);
_upb_TextEncode_Field(e, val, f);
}
}
@@ -418,7 +183,7 @@ static void txtenc_msg(txtenc* e, const upb_Message* msg,
char* start = e->ptr;
upb_EpsCopyInputStream stream;
upb_EpsCopyInputStream_Init(&stream, &ptr, size, true);
if (!txtenc_unknown(e, ptr, &stream, -1)) {
if (!UPB_PRIVATE(_upb_TextEncode_Unknown)(e, ptr, &stream, -1)) {
/* Unknown failed to parse, back up and don't print it at all. */
e->ptr = start;
}
@@ -426,17 +191,6 @@ static void txtenc_msg(txtenc* e, const upb_Message* msg,
}
}
size_t txtenc_nullz(txtenc* e, size_t size) {
size_t ret = e->ptr - e->buf + e->overflow;
if (size > 0) {
if (e->ptr == e->end) e->ptr--;
*e->ptr = '\0';
}
return ret;
}
size_t upb_TextEncode(const upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool, int options, char* buf,
size_t size) {
@@ -451,7 +205,7 @@ size_t upb_TextEncode(const upb_Message* msg, const upb_MessageDef* m,
e.ext_pool = ext_pool;
_upb_mapsorter_init(&e.sorter);
txtenc_msg(&e, msg, m);
_upb_TextEncode_Msg(&e, msg, m);
_upb_mapsorter_destroy(&e.sorter);
return txtenc_nullz(&e, size);
return UPB_PRIVATE(_upb_TextEncode_Nullz)(&e, size);
}

View File

@@ -9,6 +9,7 @@
#define UPB_TEXT_ENCODE_H_
#include "upb/reflection/def.h"
#include "upb/text/options.h" // IWYU pragma: export
// Must be last.
#include "upb/port/def.inc"
@@ -17,17 +18,6 @@
extern "C" {
#endif
enum {
// When set, prints everything on a single line.
UPB_TXTENC_SINGLELINE = 1,
// When set, unknown fields are not printed.
UPB_TXTENC_SKIPUNKNOWN = 2,
// When set, maps are *not* sorted (this avoids allocating tmp mem).
UPB_TXTENC_NOSORT = 4
};
/* Encodes the given |msg| to text format. The message's reflection is given in
* |m|. The symtab in |symtab| is used to find extensions (if NULL, extensions
* will not be printed).

View File

@@ -0,0 +1,180 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/text/internal/encode.h"
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/lex/round_trip.h"
#include "upb/message/array.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/reader.h"
#include "upb/wire/types.h"
// Must be last.
#include "upb/port/def.inc"
#define CHK(x) \
do { \
if (!(x)) { \
return false; \
} \
} while (0)
/*
* Unknown fields are printed by number.
*
* 1001: 123
* 1002: "hello"
* 1006: 0xdeadbeef
* 1003: {
* 1: 111
* }
*/
const char* UPB_PRIVATE(_upb_TextEncode_Unknown)(txtenc* e, const char* ptr,
upb_EpsCopyInputStream* stream,
int groupnum) {
// We are guaranteed that the unknown data is valid wire format, and will not
// contain tag zero.
uint32_t end_group = groupnum > 0
? ((groupnum << kUpb_WireReader_WireTypeBits) |
kUpb_WireType_EndGroup)
: 0;
while (!upb_EpsCopyInputStream_IsDone(stream, &ptr)) {
uint32_t tag;
CHK(ptr = upb_WireReader_ReadTag(ptr, &tag));
if (tag == end_group) return ptr;
UPB_PRIVATE(_upb_TextEncode_Indent)(e);
UPB_PRIVATE(_upb_TextEncode_Printf)
(e, "%d: ", (int)upb_WireReader_GetFieldNumber(tag));
switch (upb_WireReader_GetWireType(tag)) {
case kUpb_WireType_Varint: {
uint64_t val;
CHK(ptr = upb_WireReader_ReadVarint(ptr, &val));
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRIu64, val);
break;
}
case kUpb_WireType_32Bit: {
uint32_t val;
ptr = upb_WireReader_ReadFixed32(ptr, &val);
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "0x%08" PRIu32, val);
break;
}
case kUpb_WireType_64Bit: {
uint64_t val;
ptr = upb_WireReader_ReadFixed64(ptr, &val);
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "0x%016" PRIu64, val);
break;
}
case kUpb_WireType_Delimited: {
int size;
char* start = e->ptr;
size_t start_overflow = e->overflow;
CHK(ptr = upb_WireReader_ReadSize(ptr, &size));
CHK(upb_EpsCopyInputStream_CheckDataSizeAvailable(stream, ptr, size));
// Speculatively try to parse as message.
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "{");
UPB_PRIVATE(_upb_TextEncode_EndField)(e);
// EpsCopyInputStream can't back up, so create a sub-stream for the
// speculative parse.
upb_EpsCopyInputStream sub_stream;
const char* sub_ptr = upb_EpsCopyInputStream_GetAliasedPtr(stream, ptr);
upb_EpsCopyInputStream_Init(&sub_stream, &sub_ptr, size, true);
e->indent_depth++;
if (UPB_PRIVATE(_upb_TextEncode_Unknown)(e, sub_ptr, &sub_stream, -1)) {
ptr = upb_EpsCopyInputStream_Skip(stream, ptr, size);
e->indent_depth--;
UPB_PRIVATE(_upb_TextEncode_Indent)(e);
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "}");
} else {
// Didn't work out, print as raw bytes.
e->indent_depth--;
e->ptr = start;
e->overflow = start_overflow;
const char* str = ptr;
ptr = upb_EpsCopyInputStream_ReadString(stream, &str, size, NULL);
UPB_ASSERT(ptr);
UPB_PRIVATE(_upb_TextEncode_Bytes)
(e, (upb_StringView){.data = str, .size = size});
}
break;
}
case kUpb_WireType_StartGroup:
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "{");
UPB_PRIVATE(_upb_TextEncode_EndField)(e);
e->indent_depth++;
CHK(ptr = UPB_PRIVATE(_upb_TextEncode_Unknown)(
e, ptr, stream, upb_WireReader_GetFieldNumber(tag)));
e->indent_depth--;
UPB_PRIVATE(_upb_TextEncode_Indent)(e);
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "}");
break;
default:
return NULL;
}
UPB_PRIVATE(_upb_TextEncode_EndField)(e);
}
return end_group == 0 && !upb_EpsCopyInputStream_IsError(stream) ? ptr : NULL;
}
#undef CHK
void UPB_PRIVATE(_upb_TextEncode_Scalar)(txtenc* e, upb_MessageValue val,
upb_CType ctype) {
switch (ctype) {
case kUpb_CType_Bool:
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, val.bool_val ? "true" : "false");
break;
case kUpb_CType_Float: {
char buf[32];
_upb_EncodeRoundTripFloat(val.float_val, buf, sizeof(buf));
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, buf);
break;
}
case kUpb_CType_Double: {
char buf[32];
_upb_EncodeRoundTripDouble(val.double_val, buf, sizeof(buf));
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, buf);
break;
}
case kUpb_CType_Int32:
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRId32, val.int32_val);
break;
case kUpb_CType_UInt32:
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRIu32, val.uint32_val);
break;
case kUpb_CType_Int64:
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRId64, val.int64_val);
break;
case kUpb_CType_UInt64:
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRIu64, val.uint64_val);
break;
case kUpb_CType_String:
UPB_PRIVATE(_upb_HardenedPrintString)
(e, val.str_val.data, val.str_val.size);
break;
case kUpb_CType_Bytes:
UPB_PRIVATE(_upb_TextEncode_Bytes)(e, val.str_val);
break;
case kUpb_CType_Enum:
UPB_ASSERT(false); // handled separately in each encoder
break;
default:
UPB_UNREACHABLE();
}
}

View File

@@ -0,0 +1,240 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_TEXT_ENCODE_INTERNAL_H_
#define UPB_TEXT_ENCODE_INTERNAL_H_
#include <stdarg.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/message/array.h"
#include "upb/message/internal/map_sorter.h"
#include "upb/port/vsnprintf_compat.h"
#include "upb/text/options.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "utf8_range.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct {
char *buf, *ptr, *end;
size_t overflow;
int indent_depth;
int options;
const struct upb_DefPool* ext_pool;
_upb_mapsorter sorter;
} txtenc;
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_PutBytes)(txtenc* e,
const void* data,
size_t len) {
size_t have = e->end - e->ptr;
if (UPB_LIKELY(have >= len)) {
memcpy(e->ptr, data, len);
e->ptr += len;
} else {
if (have) {
memcpy(e->ptr, data, have);
e->ptr += have;
}
e->overflow += (len - have);
}
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_PutStr)(txtenc* e,
const char* str) {
UPB_PRIVATE(_upb_TextEncode_PutBytes)(e, str, strlen(str));
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Printf)(txtenc* e, const char* fmt,
...) {
size_t n;
size_t have = e->end - e->ptr;
va_list args;
va_start(args, fmt);
n = _upb_vsnprintf(e->ptr, have, fmt, args);
va_end(args);
if (UPB_LIKELY(have > n)) {
e->ptr += n;
} else {
e->ptr = UPB_PTRADD(e->ptr, have);
e->overflow += (n - have);
}
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Indent)(txtenc* e) {
if ((e->options & UPB_TXTENC_SINGLELINE) == 0) {
int i = e->indent_depth;
while (i-- > 0) {
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, " ");
}
}
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_EndField)(txtenc* e) {
if (e->options & UPB_TXTENC_SINGLELINE) {
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, " ");
} else {
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\n");
}
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Escaped)(txtenc* e,
unsigned char ch) {
switch (ch) {
case '\n':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\n");
break;
case '\r':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\r");
break;
case '\t':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\t");
break;
case '\"':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\\"");
break;
case '\'':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\'");
break;
case '\\':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\\\");
break;
default:
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "\\%03o", ch);
break;
}
}
// Returns true if `ch` needs to be escaped in TextFormat, independent of any
// UTF-8 validity issues.
UPB_INLINE bool UPB_PRIVATE(_upb_DefinitelyNeedsEscape)(unsigned char ch) {
if (ch < 32) return true;
switch (ch) {
case '\"':
case '\'':
case '\\':
case 127:
return true;
}
return false;
}
UPB_INLINE bool UPB_PRIVATE(_upb_AsciiIsPrint)(unsigned char ch) {
return ch >= 32 && ch < 127;
}
// Returns true if this is a high byte that requires UTF-8 validation. If the
// UTF-8 validation fails, we must escape the byte.
UPB_INLINE bool UPB_PRIVATE(_upb_NeedsUtf8Validation)(unsigned char ch) {
return ch > 127;
}
// Returns the number of bytes in the prefix of `val` that do not need escaping.
// This is like utf8_range::SpanStructurallyValid(), except that it also
// terminates at any ASCII char that needs to be escaped in TextFormat (any char
// that has `DefinitelyNeedsEscape(ch) == true`).
//
// If we could get a variant of utf8_range::SpanStructurallyValid() that could
// terminate on any of these chars, that might be more efficient, but it would
// be much more complicated to modify that heavily SIMD code.
UPB_INLINE size_t UPB_PRIVATE(_SkipPassthroughBytes)(const char* ptr,
size_t size) {
for (size_t i = 0; i < size; i++) {
unsigned char uc = ptr[i];
if (UPB_PRIVATE(_upb_DefinitelyNeedsEscape)(uc)) return i;
if (UPB_PRIVATE(_upb_NeedsUtf8Validation)(uc)) {
// Find the end of this region of consecutive high bytes, so that we only
// give high bytes to the UTF-8 checker. This avoids needing to perform
// a second scan of the ASCII characters looking for characters that
// need escaping.
//
// We assume that high bytes are less frequent than plain, printable ASCII
// bytes, so we accept the double-scan of high bytes.
size_t end = i + 1;
for (; end < size; end++) {
if (!UPB_PRIVATE(_upb_NeedsUtf8Validation)(ptr[end])) break;
}
size_t n = end - i;
size_t ok = utf8_range_ValidPrefix(ptr + i, n);
if (ok != n) return i + ok;
i += ok - 1;
}
}
return size;
}
UPB_INLINE void UPB_PRIVATE(_upb_HardenedPrintString)(txtenc* e,
const char* ptr,
size_t len) {
// Print as UTF-8, while guarding against any invalid UTF-8 in the string
// field.
//
// If in the future we have a guaranteed invariant that invalid UTF-8 will
// never be present, we could avoid the UTF-8 check here.
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\"");
const char* end = ptr + len;
while (ptr < end) {
size_t n = UPB_PRIVATE(_SkipPassthroughBytes)(ptr, end - ptr);
if (n != 0) {
UPB_PRIVATE(_upb_TextEncode_PutBytes)(e, ptr, n);
ptr += n;
if (ptr == end) break;
}
// If repeated calls to CEscape() and PrintString() are expensive, we could
// consider batching them, at the cost of some complexity.
UPB_PRIVATE(_upb_TextEncode_Escaped)(e, *ptr);
ptr++;
}
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\"");
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Bytes)(txtenc* e,
upb_StringView data) {
const char* ptr = data.data;
const char* end = ptr + data.size;
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\"");
for (; ptr < end; ptr++) {
unsigned char uc = *ptr;
if (UPB_PRIVATE(_upb_AsciiIsPrint)(uc)) {
UPB_PRIVATE(_upb_TextEncode_PutBytes)(e, ptr, 1);
} else {
UPB_PRIVATE(_upb_TextEncode_Escaped)(e, uc);
}
}
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\"");
}
UPB_INLINE size_t UPB_PRIVATE(_upb_TextEncode_Nullz)(txtenc* e, size_t size) {
size_t ret = e->ptr - e->buf + e->overflow;
if (size > 0) {
if (e->ptr == e->end) e->ptr--;
*e->ptr = '\0';
}
return ret;
}
const char* UPB_PRIVATE(_upb_TextEncode_Unknown)(txtenc* e, const char* ptr,
upb_EpsCopyInputStream* stream,
int groupnum);
// Must not be called for ctype = kUpb_CType_Enum, as they require different
// handling depending on whether or not we're doing reflection-based encoding.
void UPB_PRIVATE(_upb_TextEncode_Scalar)(txtenc* e, upb_MessageValue val,
upb_CType ctype);
#include "upb/port/undef.inc"
#endif // UPB_TEXT_ENCODE_INTERNAL_H_

22
Pods/gRPC-Core/third_party/upb/upb/text/options.h generated vendored Normal file
View File

@@ -0,0 +1,22 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_TEXT_OPTIONS_H_
#define UPB_TEXT_OPTIONS_H_
enum {
// When set, prints everything on a single line.
UPB_TXTENC_SINGLELINE = 1,
// When set, unknown fields are not printed.
UPB_TXTENC_SKIPUNKNOWN = 2,
// When set, maps are *not* sorted (this avoids allocating tmp mem).
UPB_TXTENC_NOSORT = 4
};
#endif // UPB_TEXT_OPTIONS_H_

View File

@@ -14,10 +14,10 @@
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/endian.h"
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/mem/arena.h"
#include "upb/mem/internal/arena.h"
#include "upb/message/array.h"
#include "upb/message/internal/accessors.h"
#include "upb/message/internal/array.h"
@@ -25,6 +25,7 @@
#include "upb/message/internal/map.h"
#include "upb/message/internal/map_entry.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/tagged_ptr.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/message/tagged_ptr.h"
@@ -32,17 +33,16 @@
#include "upb/mini_table/extension.h"
#include "upb/mini_table/extension_registry.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/enum.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/message.h"
#include "upb/mini_table/internal/sub.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
#include "upb/port/atomic.h"
#include "upb/wire/encode.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/internal/constants.h"
#include "upb/wire/internal/decode.h"
#include "upb/wire/internal/swap.h"
#include "upb/wire/internal/decoder.h"
#include "upb/wire/reader.h"
// Must be last.
@@ -87,6 +87,27 @@ typedef union {
uint32_t size;
} wireval;
// Ideally these two functions should take the owning MiniTable pointer as a
// first argument, then we could just put them in mini_table/message.h as nice
// clean getters. But we don't have that so instead we gotta write these
// Frankenfunctions that take an array of subtables.
// TODO: Move these to mini_table/ anyway since there are other places
// that could use them.
// Returns the MiniTable corresponding to a given MiniTableField
// from an array of MiniTableSubs.
static const upb_MiniTable* _upb_MiniTableSubs_MessageByField(
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field) {
return *subs[field->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(submsg);
}
// Returns the MiniTableEnum corresponding to a given MiniTableField
// from an array of MiniTableSub.
static const upb_MiniTableEnum* _upb_MiniTableSubs_EnumByField(
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field) {
return subs[field->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(subenum);
}
static const char* _upb_Decoder_DecodeMessage(upb_Decoder* d, const char* ptr,
upb_Message* msg,
const upb_MiniTable* layout);
@@ -112,8 +133,10 @@ static void _upb_Decoder_VerifyUtf8(upb_Decoder* d, const char* buf, int len) {
}
static bool _upb_Decoder_Reserve(upb_Decoder* d, upb_Array* arr, size_t elem) {
bool need_realloc = arr->capacity - arr->size < elem;
if (need_realloc && !_upb_array_realloc(arr, arr->size + elem, &d->arena)) {
bool need_realloc =
arr->UPB_PRIVATE(capacity) - arr->UPB_PRIVATE(size) < elem;
if (need_realloc && !UPB_PRIVATE(_upb_Array_Realloc)(
arr, arr->UPB_PRIVATE(size) + elem, &d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
return need_realloc;
@@ -129,8 +152,7 @@ static _upb_DecodeLongVarintReturn _upb_Decoder_DecodeLongVarint(
const char* ptr, uint64_t val) {
_upb_DecodeLongVarintReturn ret = {NULL, 0};
uint64_t byte;
int i;
for (i = 1; i < 10; i++) {
for (int i = 1; i < 10; i++) {
byte = (uint8_t)ptr[i];
val += (byte - 1) << (i * 7);
if (!(byte & 0x80)) {
@@ -143,8 +165,8 @@ static _upb_DecodeLongVarintReturn _upb_Decoder_DecodeLongVarint(
}
UPB_FORCEINLINE
static const char* _upb_Decoder_DecodeVarint(upb_Decoder* d, const char* ptr,
uint64_t* val) {
const char* _upb_Decoder_DecodeVarint(upb_Decoder* d, const char* ptr,
uint64_t* val) {
uint64_t byte = (uint8_t)*ptr;
if (UPB_LIKELY((byte & 0x80) == 0)) {
*val = byte;
@@ -158,8 +180,8 @@ static const char* _upb_Decoder_DecodeVarint(upb_Decoder* d, const char* ptr,
}
UPB_FORCEINLINE
static const char* _upb_Decoder_DecodeTag(upb_Decoder* d, const char* ptr,
uint32_t* val) {
const char* _upb_Decoder_DecodeTag(upb_Decoder* d, const char* ptr,
uint32_t* val) {
uint64_t byte = (uint8_t)*ptr;
if (UPB_LIKELY((byte & 0x80) == 0)) {
*val = byte;
@@ -176,8 +198,8 @@ static const char* _upb_Decoder_DecodeTag(upb_Decoder* d, const char* ptr,
}
UPB_FORCEINLINE
static const char* upb_Decoder_DecodeSize(upb_Decoder* d, const char* ptr,
uint32_t* size) {
const char* upb_Decoder_DecodeSize(upb_Decoder* d, const char* ptr,
uint32_t* size) {
uint64_t size64;
ptr = _upb_Decoder_DecodeVarint(d, ptr, &size64);
if (size64 >= INT32_MAX ||
@@ -189,7 +211,7 @@ static const char* upb_Decoder_DecodeSize(upb_Decoder* d, const char* ptr,
}
static void _upb_Decoder_MungeInt32(wireval* val) {
if (!_upb_IsLittleEndian()) {
if (!upb_IsLittleEndian()) {
/* The next stage will memcpy(dst, &val, 4) */
val->uint32_val = val->uint64_val;
}
@@ -218,44 +240,53 @@ static void _upb_Decoder_Munge(int type, wireval* val) {
}
}
static upb_Message* _upb_Decoder_NewSubMessage(upb_Decoder* d,
const upb_MiniTableSub* subs,
const upb_MiniTableField* field,
upb_TaggedMessagePtr* target) {
const upb_MiniTable* subl = subs[field->UPB_PRIVATE(submsg_index)].submsg;
static upb_Message* _upb_Decoder_NewSubMessage2(upb_Decoder* d,
const upb_MiniTable* subl,
const upb_MiniTableField* field,
upb_TaggedMessagePtr* target) {
UPB_ASSERT(subl);
upb_Message* msg = _upb_Message_New(subl, &d->arena);
if (!msg) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
// Extensions should not be unlinked. A message extension should not be
// Extensions should not be unlinked. A message extension should not be
// registered until its sub-message type is available to be linked.
bool is_empty = subl == &_kUpb_MiniTable_Empty;
bool is_extension = field->mode & kUpb_LabelFlags_IsExtension;
bool is_empty = UPB_PRIVATE(_upb_MiniTable_IsEmpty)(subl);
bool is_extension = field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsExtension;
UPB_ASSERT(!(is_empty && is_extension));
if (is_empty && !(d->options & kUpb_DecodeOption_ExperimentalAllowUnlinked)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_UnlinkedSubMessage);
}
upb_TaggedMessagePtr tagged = _upb_TaggedMessagePtr_Pack(msg, is_empty);
upb_TaggedMessagePtr tagged =
UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(msg, is_empty);
memcpy(target, &tagged, sizeof(tagged));
return msg;
}
static upb_Message* _upb_Decoder_NewSubMessage(
upb_Decoder* d, const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* field, upb_TaggedMessagePtr* target) {
const upb_MiniTable* subl = _upb_MiniTableSubs_MessageByField(subs, field);
return _upb_Decoder_NewSubMessage2(d, subl, field, target);
}
static upb_Message* _upb_Decoder_ReuseSubMessage(
upb_Decoder* d, const upb_MiniTableSub* subs,
upb_Decoder* d, const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* field, upb_TaggedMessagePtr* target) {
upb_TaggedMessagePtr tagged = *target;
const upb_MiniTable* subl = subs[field->UPB_PRIVATE(submsg_index)].submsg;
const upb_MiniTable* subl = _upb_MiniTableSubs_MessageByField(subs, field);
UPB_ASSERT(subl);
if (!upb_TaggedMessagePtr_IsEmpty(tagged) || subl == &_kUpb_MiniTable_Empty) {
return _upb_TaggedMessagePtr_GetMessage(tagged);
if (!upb_TaggedMessagePtr_IsEmpty(tagged) ||
UPB_PRIVATE(_upb_MiniTable_IsEmpty)(subl)) {
return UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(tagged);
}
// We found an empty message from a previous parse that was performed before
// this field was linked. But it is linked now, so we want to allocate a new
// message of the correct type and promote data into it before continuing.
upb_Message* existing = _upb_TaggedMessagePtr_GetEmptyMessage(tagged);
upb_Message* existing =
UPB_PRIVATE(_upb_TaggedMessagePtr_GetEmptyMessage)(tagged);
upb_Message* promoted = _upb_Decoder_NewSubMessage(d, subs, field, target);
size_t size;
const char* unknown = upb_Message_GetUnknown(existing, &size);
@@ -276,11 +307,10 @@ static const char* _upb_Decoder_ReadString(upb_Decoder* d, const char* ptr,
}
UPB_FORCEINLINE
static const char* _upb_Decoder_RecurseSubMessage(upb_Decoder* d,
const char* ptr,
upb_Message* submsg,
const upb_MiniTable* subl,
uint32_t expected_end_group) {
const char* _upb_Decoder_RecurseSubMessage(upb_Decoder* d, const char* ptr,
upb_Message* submsg,
const upb_MiniTable* subl,
uint32_t expected_end_group) {
if (--d->depth < 0) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_MaxDepthExceeded);
}
@@ -293,11 +323,13 @@ static const char* _upb_Decoder_RecurseSubMessage(upb_Decoder* d,
}
UPB_FORCEINLINE
static const char* _upb_Decoder_DecodeSubMessage(
upb_Decoder* d, const char* ptr, upb_Message* submsg,
const upb_MiniTableSub* subs, const upb_MiniTableField* field, int size) {
const char* _upb_Decoder_DecodeSubMessage(upb_Decoder* d, const char* ptr,
upb_Message* submsg,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* field,
int size) {
int saved_delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, size);
const upb_MiniTable* subl = subs[field->UPB_PRIVATE(submsg_index)].submsg;
const upb_MiniTable* subl = _upb_MiniTableSubs_MessageByField(subs, field);
UPB_ASSERT(subl);
ptr = _upb_Decoder_RecurseSubMessage(d, ptr, submsg, subl, DECODE_NOGROUP);
upb_EpsCopyInputStream_PopLimit(&d->input, ptr, saved_delta);
@@ -305,10 +337,10 @@ static const char* _upb_Decoder_DecodeSubMessage(
}
UPB_FORCEINLINE
static const char* _upb_Decoder_DecodeGroup(upb_Decoder* d, const char* ptr,
upb_Message* submsg,
const upb_MiniTable* subl,
uint32_t number) {
const char* _upb_Decoder_DecodeGroup(upb_Decoder* d, const char* ptr,
upb_Message* submsg,
const upb_MiniTable* subl,
uint32_t number) {
if (_upb_Decoder_IsDone(d, &ptr)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
}
@@ -318,19 +350,20 @@ static const char* _upb_Decoder_DecodeGroup(upb_Decoder* d, const char* ptr,
}
UPB_FORCEINLINE
static const char* _upb_Decoder_DecodeUnknownGroup(upb_Decoder* d,
const char* ptr,
uint32_t number) {
const char* _upb_Decoder_DecodeUnknownGroup(upb_Decoder* d, const char* ptr,
uint32_t number) {
return _upb_Decoder_DecodeGroup(d, ptr, NULL, NULL, number);
}
UPB_FORCEINLINE
static const char* _upb_Decoder_DecodeKnownGroup(
upb_Decoder* d, const char* ptr, upb_Message* submsg,
const upb_MiniTableSub* subs, const upb_MiniTableField* field) {
const upb_MiniTable* subl = subs[field->UPB_PRIVATE(submsg_index)].submsg;
const char* _upb_Decoder_DecodeKnownGroup(upb_Decoder* d, const char* ptr,
upb_Message* submsg,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* field) {
const upb_MiniTable* subl = _upb_MiniTableSubs_MessageByField(subs, field);
UPB_ASSERT(subl);
return _upb_Decoder_DecodeGroup(d, ptr, submsg, subl, field->number);
return _upb_Decoder_DecodeGroup(d, ptr, submsg, subl,
field->UPB_PRIVATE(number));
}
static char* upb_Decoder_EncodeVarint32(uint32_t val, char* ptr) {
@@ -350,60 +383,50 @@ static void _upb_Decoder_AddUnknownVarints(upb_Decoder* d, upb_Message* msg,
end = upb_Decoder_EncodeVarint32(val1, end);
end = upb_Decoder_EncodeVarint32(val2, end);
if (!_upb_Message_AddUnknown(msg, buf, end - buf, &d->arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, buf, end - buf, &d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
}
UPB_NOINLINE
static bool _upb_Decoder_CheckEnumSlow(upb_Decoder* d, const char* ptr,
upb_Message* msg,
const upb_MiniTableEnum* e,
const upb_MiniTableField* field,
uint32_t v) {
if (_upb_MiniTable_CheckEnumValueSlow(e, v)) return true;
UPB_FORCEINLINE
bool _upb_Decoder_CheckEnum(upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTableEnum* e,
const upb_MiniTableField* field, wireval* val) {
const uint32_t v = val->uint32_val;
if (UPB_LIKELY(upb_MiniTableEnum_CheckValue(e, v))) return true;
// Unrecognized enum goes into unknown fields.
// For packed fields the tag could be arbitrarily far in the past, so we
// just re-encode the tag and value here.
uint32_t tag = ((uint32_t)field->number << 3) | kUpb_WireType_Varint;
// For packed fields the tag could be arbitrarily far in the past,
// so we just re-encode the tag and value here.
const uint32_t tag =
((uint32_t)field->UPB_PRIVATE(number) << 3) | kUpb_WireType_Varint;
upb_Message* unknown_msg =
field->mode & kUpb_LabelFlags_IsExtension ? d->unknown_msg : msg;
field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsExtension ? d->unknown_msg
: msg;
_upb_Decoder_AddUnknownVarints(d, unknown_msg, tag, v);
return false;
}
UPB_FORCEINLINE
static bool _upb_Decoder_CheckEnum(upb_Decoder* d, const char* ptr,
upb_Message* msg, const upb_MiniTableEnum* e,
const upb_MiniTableField* field,
wireval* val) {
uint32_t v = val->uint32_val;
_kUpb_FastEnumCheck_Status status = _upb_MiniTable_CheckEnumValueFast(e, v);
if (UPB_LIKELY(status == _kUpb_FastEnumCheck_ValueIsInEnum)) return true;
return _upb_Decoder_CheckEnumSlow(d, ptr, msg, e, field, v);
}
UPB_NOINLINE
static const char* _upb_Decoder_DecodeEnumArray(upb_Decoder* d, const char* ptr,
upb_Message* msg,
upb_Array* arr,
const upb_MiniTableSub* subs,
const upb_MiniTableField* field,
wireval* val) {
const upb_MiniTableEnum* e = subs[field->UPB_PRIVATE(submsg_index)].subenum;
static const char* _upb_Decoder_DecodeEnumArray(
upb_Decoder* d, const char* ptr, upb_Message* msg, upb_Array* arr,
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field,
wireval* val) {
const upb_MiniTableEnum* e = _upb_MiniTableSubs_EnumByField(subs, field);
if (!_upb_Decoder_CheckEnum(d, ptr, msg, e, field, val)) return ptr;
void* mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->size * 4, void);
arr->size++;
void* mem = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) * 4, void);
arr->UPB_PRIVATE(size)++;
memcpy(mem, val, 4);
return ptr;
}
UPB_FORCEINLINE
static const char* _upb_Decoder_DecodeFixedPacked(
upb_Decoder* d, const char* ptr, upb_Array* arr, wireval* val,
const upb_MiniTableField* field, int lg2) {
const char* _upb_Decoder_DecodeFixedPacked(upb_Decoder* d, const char* ptr,
upb_Array* arr, wireval* val,
const upb_MiniTableField* field,
int lg2) {
int mask = (1 << lg2) - 1;
size_t count = val->size >> lg2;
if ((val->size & mask) != 0) {
@@ -411,11 +434,12 @@ static const char* _upb_Decoder_DecodeFixedPacked(
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
}
_upb_Decoder_Reserve(d, arr, count);
void* mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->size << lg2, void);
arr->size += count;
void* mem = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) << lg2, void);
arr->UPB_PRIVATE(size) += count;
// Note: if/when the decoder supports multi-buffer input, we will need to
// handle buffer seams here.
if (_upb_IsLittleEndian()) {
if (upb_IsLittleEndian()) {
ptr = upb_EpsCopyInputStream_Copy(&d->input, ptr, mem, val->size);
} else {
int delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size);
@@ -437,20 +461,23 @@ static const char* _upb_Decoder_DecodeFixedPacked(
}
UPB_FORCEINLINE
static const char* _upb_Decoder_DecodeVarintPacked(
upb_Decoder* d, const char* ptr, upb_Array* arr, wireval* val,
const upb_MiniTableField* field, int lg2) {
const char* _upb_Decoder_DecodeVarintPacked(upb_Decoder* d, const char* ptr,
upb_Array* arr, wireval* val,
const upb_MiniTableField* field,
int lg2) {
int scale = 1 << lg2;
int saved_limit = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size);
char* out = UPB_PTR_AT(_upb_array_ptr(arr), arr->size << lg2, void);
char* out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) << lg2, void);
while (!_upb_Decoder_IsDone(d, &ptr)) {
wireval elem;
ptr = _upb_Decoder_DecodeVarint(d, ptr, &elem.uint64_val);
_upb_Decoder_Munge(field->UPB_PRIVATE(descriptortype), &elem);
if (_upb_Decoder_Reserve(d, arr, 1)) {
out = UPB_PTR_AT(_upb_array_ptr(arr), arr->size << lg2, void);
out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) << lg2, void);
}
arr->size++;
arr->UPB_PRIVATE(size)++;
memcpy(out, &elem, scale);
out += scale;
}
@@ -461,11 +488,12 @@ static const char* _upb_Decoder_DecodeVarintPacked(
UPB_NOINLINE
static const char* _upb_Decoder_DecodeEnumPacked(
upb_Decoder* d, const char* ptr, upb_Message* msg, upb_Array* arr,
const upb_MiniTableSub* subs, const upb_MiniTableField* field,
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field,
wireval* val) {
const upb_MiniTableEnum* e = subs[field->UPB_PRIVATE(submsg_index)].subenum;
const upb_MiniTableEnum* e = _upb_MiniTableSubs_EnumByField(subs, field);
int saved_limit = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size);
char* out = UPB_PTR_AT(_upb_array_ptr(arr), arr->size * 4, void);
char* out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) * 4, void);
while (!_upb_Decoder_IsDone(d, &ptr)) {
wireval elem;
ptr = _upb_Decoder_DecodeVarint(d, ptr, &elem.uint64_val);
@@ -474,9 +502,10 @@ static const char* _upb_Decoder_DecodeEnumPacked(
continue;
}
if (_upb_Decoder_Reserve(d, arr, 1)) {
out = UPB_PTR_AT(_upb_array_ptr(arr), arr->size * 4, void);
out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) * 4, void);
}
arr->size++;
arr->UPB_PRIVATE(size)++;
memcpy(out, &elem, 4);
out += 4;
}
@@ -484,43 +513,20 @@ static const char* _upb_Decoder_DecodeEnumPacked(
return ptr;
}
upb_Array* _upb_Decoder_CreateArray(upb_Decoder* d,
const upb_MiniTableField* field) {
/* Maps descriptor type -> elem_size_lg2. */
static const uint8_t kElemSizeLg2[] = {
[0] = -1, // invalid descriptor type
[kUpb_FieldType_Double] = 3,
[kUpb_FieldType_Float] = 2,
[kUpb_FieldType_Int64] = 3,
[kUpb_FieldType_UInt64] = 3,
[kUpb_FieldType_Int32] = 2,
[kUpb_FieldType_Fixed64] = 3,
[kUpb_FieldType_Fixed32] = 2,
[kUpb_FieldType_Bool] = 0,
[kUpb_FieldType_String] = UPB_SIZE(3, 4),
[kUpb_FieldType_Group] = UPB_SIZE(2, 3),
[kUpb_FieldType_Message] = UPB_SIZE(2, 3),
[kUpb_FieldType_Bytes] = UPB_SIZE(3, 4),
[kUpb_FieldType_UInt32] = 2,
[kUpb_FieldType_Enum] = 2,
[kUpb_FieldType_SFixed32] = 2,
[kUpb_FieldType_SFixed64] = 3,
[kUpb_FieldType_SInt32] = 2,
[kUpb_FieldType_SInt64] = 3,
};
size_t lg2 = kElemSizeLg2[field->UPB_PRIVATE(descriptortype)];
upb_Array* ret = _upb_Array_New(&d->arena, 4, lg2);
static upb_Array* _upb_Decoder_CreateArray(upb_Decoder* d,
const upb_MiniTableField* field) {
const upb_FieldType field_type = field->UPB_PRIVATE(descriptortype);
const size_t lg2 = UPB_PRIVATE(_upb_FieldType_SizeLg2)(field_type);
upb_Array* ret = UPB_PRIVATE(_upb_Array_New)(&d->arena, 4, lg2);
if (!ret) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
return ret;
}
static const char* _upb_Decoder_DecodeToArray(upb_Decoder* d, const char* ptr,
upb_Message* msg,
const upb_MiniTableSub* subs,
const upb_MiniTableField* field,
wireval* val, int op) {
upb_Array** arrp = UPB_PTR_AT(msg, field->offset, void);
static const char* _upb_Decoder_DecodeToArray(
upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field,
wireval* val, int op) {
upb_Array** arrp = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void);
upb_Array* arr = *arrp;
void* mem;
@@ -536,8 +542,9 @@ static const char* _upb_Decoder_DecodeToArray(upb_Decoder* d, const char* ptr,
case kUpb_DecodeOp_Scalar4Byte:
case kUpb_DecodeOp_Scalar8Byte:
/* Append scalar value. */
mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->size << op, void);
arr->size++;
mem = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) << op, void);
arr->UPB_PRIVATE(size)++;
memcpy(mem, val, 1 << op);
return ptr;
case kUpb_DecodeOp_String:
@@ -545,16 +552,18 @@ static const char* _upb_Decoder_DecodeToArray(upb_Decoder* d, const char* ptr,
/* Fallthrough. */
case kUpb_DecodeOp_Bytes: {
/* Append bytes. */
upb_StringView* str = (upb_StringView*)_upb_array_ptr(arr) + arr->size;
arr->size++;
upb_StringView* str = (upb_StringView*)upb_Array_MutableDataPtr(arr) +
arr->UPB_PRIVATE(size);
arr->UPB_PRIVATE(size)++;
return _upb_Decoder_ReadString(d, ptr, val->size, str);
}
case kUpb_DecodeOp_SubMessage: {
/* Append submessage / group. */
upb_TaggedMessagePtr* target = UPB_PTR_AT(
_upb_array_ptr(arr), arr->size * sizeof(void*), upb_TaggedMessagePtr);
upb_Array_MutableDataPtr(arr), arr->UPB_PRIVATE(size) * sizeof(void*),
upb_TaggedMessagePtr);
upb_Message* submsg = _upb_Decoder_NewSubMessage(d, subs, field, target);
arr->size++;
arr->UPB_PRIVATE(size)++;
if (UPB_UNLIKELY(field->UPB_PRIVATE(descriptortype) ==
kUpb_FieldType_Group)) {
return _upb_Decoder_DecodeKnownGroup(d, ptr, submsg, subs, field);
@@ -581,10 +590,11 @@ static const char* _upb_Decoder_DecodeToArray(upb_Decoder* d, const char* ptr,
}
}
upb_Map* _upb_Decoder_CreateMap(upb_Decoder* d, const upb_MiniTable* entry) {
/* Maps descriptor type -> upb map size. */
static upb_Map* _upb_Decoder_CreateMap(upb_Decoder* d,
const upb_MiniTable* entry) {
// Maps descriptor type -> upb map size
static const uint8_t kSizeInMap[] = {
[0] = -1, // invalid descriptor type */
[0] = -1, // invalid descriptor type
[kUpb_FieldType_Double] = 8,
[kUpb_FieldType_Float] = 4,
[kUpb_FieldType_Int64] = 8,
@@ -605,32 +615,31 @@ upb_Map* _upb_Decoder_CreateMap(upb_Decoder* d, const upb_MiniTable* entry) {
[kUpb_FieldType_SInt64] = 8,
};
const upb_MiniTableField* key_field = &entry->fields[0];
const upb_MiniTableField* val_field = &entry->fields[1];
const upb_MiniTableField* key_field = &entry->UPB_PRIVATE(fields)[0];
const upb_MiniTableField* val_field = &entry->UPB_PRIVATE(fields)[1];
char key_size = kSizeInMap[key_field->UPB_PRIVATE(descriptortype)];
char val_size = kSizeInMap[val_field->UPB_PRIVATE(descriptortype)];
UPB_ASSERT(key_field->offset == offsetof(upb_MapEntryData, k));
UPB_ASSERT(val_field->offset == offsetof(upb_MapEntryData, v));
UPB_ASSERT(key_field->UPB_PRIVATE(offset) == offsetof(upb_MapEntry, k));
UPB_ASSERT(val_field->UPB_PRIVATE(offset) == offsetof(upb_MapEntry, v));
upb_Map* ret = _upb_Map_New(&d->arena, key_size, val_size);
if (!ret) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
return ret;
}
static const char* _upb_Decoder_DecodeToMap(upb_Decoder* d, const char* ptr,
upb_Message* msg,
const upb_MiniTableSub* subs,
const upb_MiniTableField* field,
wireval* val) {
upb_Map** map_p = UPB_PTR_AT(msg, field->offset, upb_Map*);
static const char* _upb_Decoder_DecodeToMap(
upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field,
wireval* val) {
upb_Map** map_p = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), upb_Map*);
upb_Map* map = *map_p;
upb_MapEntry ent;
UPB_ASSERT(upb_MiniTableField_Type(field) == kUpb_FieldType_Message);
const upb_MiniTable* entry = subs[field->UPB_PRIVATE(submsg_index)].submsg;
const upb_MiniTable* entry = _upb_MiniTableSubs_MessageByField(subs, field);
UPB_ASSERT(entry);
UPB_ASSERT(entry->field_count == 2);
UPB_ASSERT(!upb_IsRepeatedOrMap(&entry->fields[0]));
UPB_ASSERT(!upb_IsRepeatedOrMap(&entry->fields[1]));
UPB_ASSERT(entry->UPB_PRIVATE(field_count) == 2);
UPB_ASSERT(upb_MiniTableField_IsScalar(&entry->UPB_PRIVATE(fields)[0]));
UPB_ASSERT(upb_MiniTableField_IsScalar(&entry->UPB_PRIVATE(fields)[1]));
if (!map) {
map = _upb_Decoder_CreateMap(d, entry);
@@ -640,35 +649,38 @@ static const char* _upb_Decoder_DecodeToMap(upb_Decoder* d, const char* ptr,
// Parse map entry.
memset(&ent, 0, sizeof(ent));
if (entry->fields[1].UPB_PRIVATE(descriptortype) == kUpb_FieldType_Message ||
entry->fields[1].UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group) {
if (entry->UPB_PRIVATE(fields)[1].UPB_PRIVATE(descriptortype) ==
kUpb_FieldType_Message ||
entry->UPB_PRIVATE(fields)[1].UPB_PRIVATE(descriptortype) ==
kUpb_FieldType_Group) {
// Create proactively to handle the case where it doesn't appear.
upb_TaggedMessagePtr msg;
_upb_Decoder_NewSubMessage(d, entry->subs, &entry->fields[1], &msg);
ent.data.v.val = upb_value_uintptr(msg);
_upb_Decoder_NewSubMessage(d, entry->UPB_PRIVATE(subs),
&entry->UPB_PRIVATE(fields)[1], &msg);
ent.v.val = upb_value_uintptr(msg);
}
ptr =
_upb_Decoder_DecodeSubMessage(d, ptr, &ent.data, subs, field, val->size);
ptr = _upb_Decoder_DecodeSubMessage(d, ptr, &ent.message, subs, field,
val->size);
// check if ent had any unknown fields
size_t size;
upb_Message_GetUnknown(&ent.data, &size);
upb_Message_GetUnknown(&ent.message, &size);
if (size != 0) {
char* buf;
size_t size;
uint32_t tag = ((uint32_t)field->number << 3) | kUpb_WireType_Delimited;
uint32_t tag =
((uint32_t)field->UPB_PRIVATE(number) << 3) | kUpb_WireType_Delimited;
upb_EncodeStatus status =
upb_Encode(&ent.data, entry, 0, &d->arena, &buf, &size);
upb_Encode(&ent.message, entry, 0, &d->arena, &buf, &size);
if (status != kUpb_EncodeStatus_Ok) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
_upb_Decoder_AddUnknownVarints(d, msg, tag, size);
if (!_upb_Message_AddUnknown(msg, buf, size, &d->arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, buf, size, &d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
} else {
if (_upb_Map_Insert(map, &ent.data.k, map->key_size, &ent.data.v,
map->val_size,
if (_upb_Map_Insert(map, &ent.k, map->key_size, &ent.v, map->val_size,
&d->arena) == kUpb_MapInsertStatus_OutOfMemory) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
@@ -678,31 +690,32 @@ static const char* _upb_Decoder_DecodeToMap(upb_Decoder* d, const char* ptr,
static const char* _upb_Decoder_DecodeToSubMessage(
upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTableSub* subs, const upb_MiniTableField* field, wireval* val,
int op) {
void* mem = UPB_PTR_AT(msg, field->offset, void);
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field,
wireval* val, int op) {
void* mem = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void);
int type = field->UPB_PRIVATE(descriptortype);
if (UPB_UNLIKELY(op == kUpb_DecodeOp_Enum) &&
!_upb_Decoder_CheckEnum(d, ptr, msg,
subs[field->UPB_PRIVATE(submsg_index)].subenum,
_upb_MiniTableSubs_EnumByField(subs, field),
field, val)) {
return ptr;
}
/* Set presence if necessary. */
if (field->presence > 0) {
_upb_sethas_field(msg, field);
} else if (field->presence < 0) {
/* Oneof case */
uint32_t* oneof_case = _upb_oneofcase_field(msg, field);
if (op == kUpb_DecodeOp_SubMessage && *oneof_case != field->number) {
// Set presence if necessary.
if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(field)) {
UPB_PRIVATE(_upb_Message_SetHasbit)(msg, field);
} else if (upb_MiniTableField_IsInOneof(field)) {
// Oneof case
uint32_t* oneof_case = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, field);
if (op == kUpb_DecodeOp_SubMessage &&
*oneof_case != field->UPB_PRIVATE(number)) {
memset(mem, 0, sizeof(void*));
}
*oneof_case = field->number;
*oneof_case = field->UPB_PRIVATE(number);
}
/* Store into message. */
// Store into message.
switch (op) {
case kUpb_DecodeOp_SubMessage: {
upb_TaggedMessagePtr* submsgp = mem;
@@ -745,28 +758,22 @@ static const char* _upb_Decoder_DecodeToSubMessage(
UPB_NOINLINE
const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
const upb_Message* msg,
const upb_MiniTable* l) {
UPB_ASSERT(l->required_count);
if (UPB_LIKELY((d->options & kUpb_DecodeOption_CheckRequired) == 0)) {
return ptr;
}
uint64_t msg_head;
memcpy(&msg_head, msg, 8);
msg_head = _upb_BigEndian_Swap64(msg_head);
if (upb_MiniTable_requiredmask(l) & ~msg_head) {
d->missing_required = true;
const upb_MiniTable* m) {
UPB_ASSERT(m->UPB_PRIVATE(required_count));
if (UPB_UNLIKELY(d->options & kUpb_DecodeOption_CheckRequired)) {
d->missing_required =
!UPB_PRIVATE(_upb_Message_IsInitializedShallow)(msg, m);
}
return ptr;
}
UPB_FORCEINLINE
static bool _upb_Decoder_TryFastDispatch(upb_Decoder* d, const char** ptr,
upb_Message* msg,
const upb_MiniTable* layout) {
bool _upb_Decoder_TryFastDispatch(upb_Decoder* d, const char** ptr,
upb_Message* msg, const upb_MiniTable* m) {
#if UPB_FASTTABLE
if (layout && layout->table_mask != (unsigned char)-1) {
if (m && m->UPB_PRIVATE(table_mask) != (unsigned char)-1) {
uint16_t tag = _upb_FastDecoder_LoadTag(*ptr);
intptr_t table = decode_totable(layout);
intptr_t table = decode_totable(m);
*ptr = _upb_FastDecoder_TagDispatch(d, *ptr, msg, table, 0, tag);
return true;
}
@@ -809,15 +816,17 @@ enum {
static void upb_Decoder_AddKnownMessageSetItem(
upb_Decoder* d, upb_Message* msg, const upb_MiniTableExtension* item_mt,
const char* data, uint32_t size) {
upb_Message_Extension* ext =
_upb_Message_GetOrCreateExtension(msg, item_mt, &d->arena);
upb_Extension* ext =
UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(msg, item_mt, &d->arena);
if (UPB_UNLIKELY(!ext)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
upb_Message* submsg = _upb_Decoder_NewSubMessage(
d, &ext->ext->sub, &ext->ext->field, (upb_TaggedMessagePtr*)&ext->data);
upb_DecodeStatus status = upb_Decode(data, size, submsg, item_mt->sub.submsg,
d->extreg, d->options, &d->arena);
upb_Message* submsg = _upb_Decoder_NewSubMessage2(
d, ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(submsg),
&ext->ext->UPB_PRIVATE(field), (upb_TaggedMessagePtr*)&ext->data);
upb_DecodeStatus status = upb_Decode(
data, size, submsg, upb_MiniTableExtension_GetSubMessage(item_mt),
d->extreg, d->options, &d->arena);
if (status != kUpb_DecodeStatus_Ok) _upb_Decoder_ErrorJmp(d, status);
}
@@ -838,9 +847,11 @@ static void upb_Decoder_AddUnknownMessageSetItem(upb_Decoder* d,
ptr = upb_Decoder_EncodeVarint32(kEndItemTag, ptr);
char* end = ptr;
if (!_upb_Message_AddUnknown(msg, buf, split - buf, &d->arena) ||
!_upb_Message_AddUnknown(msg, message_data, message_size, &d->arena) ||
!_upb_Message_AddUnknown(msg, split, end - split, &d->arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, buf, split - buf, &d->arena) ||
!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, message_data, message_size,
&d->arena) ||
!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, split, end - split,
&d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
}
@@ -920,34 +931,34 @@ static const upb_MiniTableField* _upb_Decoder_FindField(upb_Decoder* d,
if (t == NULL) return &none;
size_t idx = ((size_t)field_number) - 1; // 0 wraps to SIZE_MAX
if (idx < t->dense_below) {
/* Fastest case: index into dense fields. */
if (idx < t->UPB_PRIVATE(dense_below)) {
// Fastest case: index into dense fields.
goto found;
}
if (t->dense_below < t->field_count) {
/* Linear search non-dense fields. Resume scanning from last_field_index
* since fields are usually in order. */
if (t->UPB_PRIVATE(dense_below) < t->UPB_PRIVATE(field_count)) {
// Linear search non-dense fields. Resume scanning from last_field_index
// since fields are usually in order.
size_t last = *last_field_index;
for (idx = last; idx < t->field_count; idx++) {
if (t->fields[idx].number == field_number) {
for (idx = last; idx < t->UPB_PRIVATE(field_count); idx++) {
if (t->UPB_PRIVATE(fields)[idx].UPB_PRIVATE(number) == field_number) {
goto found;
}
}
for (idx = t->dense_below; idx < last; idx++) {
if (t->fields[idx].number == field_number) {
for (idx = t->UPB_PRIVATE(dense_below); idx < last; idx++) {
if (t->UPB_PRIVATE(fields)[idx].UPB_PRIVATE(number) == field_number) {
goto found;
}
}
}
if (d->extreg) {
switch (t->ext) {
switch (t->UPB_PRIVATE(ext)) {
case kUpb_ExtMode_Extendable: {
const upb_MiniTableExtension* ext =
upb_ExtensionRegistry_Lookup(d->extreg, t, field_number);
if (ext) return &ext->field;
if (ext) return &ext->UPB_PRIVATE(field);
break;
}
case kUpb_ExtMode_IsMessageSet:
@@ -960,15 +971,15 @@ static const upb_MiniTableField* _upb_Decoder_FindField(upb_Decoder* d,
}
}
return &none; /* Unknown field. */
return &none; // Unknown field.
found:
UPB_ASSERT(t->fields[idx].number == field_number);
UPB_ASSERT(t->UPB_PRIVATE(fields)[idx].UPB_PRIVATE(number) == field_number);
*last_field_index = idx;
return &t->fields[idx];
return &t->UPB_PRIVATE(fields)[idx];
}
int _upb_Decoder_GetVarintOp(const upb_MiniTableField* field) {
static int _upb_Decoder_GetVarintOp(const upb_MiniTableField* field) {
static const int8_t kVarintOps[] = {
[kUpb_FakeFieldType_FieldNotFound] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Double] = kUpb_DecodeOp_UnknownField,
@@ -996,14 +1007,14 @@ int _upb_Decoder_GetVarintOp(const upb_MiniTableField* field) {
}
UPB_FORCEINLINE
static void _upb_Decoder_CheckUnlinked(upb_Decoder* d, const upb_MiniTable* mt,
const upb_MiniTableField* field,
int* op) {
void _upb_Decoder_CheckUnlinked(upb_Decoder* d, const upb_MiniTable* mt,
const upb_MiniTableField* field, int* op) {
// If sub-message is not linked, treat as unknown.
if (field->mode & kUpb_LabelFlags_IsExtension) return;
const upb_MiniTableSub* sub = &mt->subs[field->UPB_PRIVATE(submsg_index)];
if (field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsExtension) return;
const upb_MiniTable* mt_sub =
_upb_MiniTableSubs_MessageByField(mt->UPB_PRIVATE(subs), field);
if ((d->options & kUpb_DecodeOption_ExperimentalAllowUnlinked) ||
sub->submsg != &_kUpb_MiniTable_Empty) {
!UPB_PRIVATE(_upb_MiniTable_IsEmpty)(mt_sub)) {
return;
}
#ifndef NDEBUG
@@ -1013,8 +1024,9 @@ static void _upb_Decoder_CheckUnlinked(upb_Decoder* d, const upb_MiniTable* mt,
// unlinked.
do {
UPB_ASSERT(upb_MiniTableField_CType(oneof) == kUpb_CType_Message);
const upb_MiniTableSub* oneof_sub =
&mt->subs[oneof->UPB_PRIVATE(submsg_index)];
const upb_MiniTable* oneof_sub =
*mt->UPB_PRIVATE(subs)[oneof->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(
submsg);
UPB_ASSERT(!oneof_sub);
} while (upb_MiniTable_NextOneofField(mt, &oneof));
}
@@ -1022,12 +1034,20 @@ static void _upb_Decoder_CheckUnlinked(upb_Decoder* d, const upb_MiniTable* mt,
*op = kUpb_DecodeOp_UnknownField;
}
int _upb_Decoder_GetDelimitedOp(upb_Decoder* d, const upb_MiniTable* mt,
const upb_MiniTableField* field) {
UPB_FORCEINLINE
void _upb_Decoder_MaybeVerifyUtf8(upb_Decoder* d,
const upb_MiniTableField* field, int* op) {
if ((field->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsAlternate) &&
UPB_UNLIKELY(d->options & kUpb_DecodeOption_AlwaysValidateUtf8))
*op = kUpb_DecodeOp_String;
}
static int _upb_Decoder_GetDelimitedOp(upb_Decoder* d, const upb_MiniTable* mt,
const upb_MiniTableField* field) {
enum { kRepeatedBase = 19 };
static const int8_t kDelimitedOps[] = {
/* For non-repeated field type. */
// For non-repeated field type.
[kUpb_FakeFieldType_FieldNotFound] =
kUpb_DecodeOp_UnknownField, // Field not found.
[kUpb_FieldType_Double] = kUpb_DecodeOp_UnknownField,
@@ -1049,7 +1069,7 @@ int _upb_Decoder_GetDelimitedOp(upb_Decoder* d, const upb_MiniTable* mt,
[kUpb_FieldType_SInt32] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_SInt64] = kUpb_DecodeOp_UnknownField,
[kUpb_FakeFieldType_MessageSetItem] = kUpb_DecodeOp_UnknownField,
// For repeated field type. */
// For repeated field type.
[kRepeatedBase + kUpb_FieldType_Double] = OP_FIXPCK_LG2(3),
[kRepeatedBase + kUpb_FieldType_Float] = OP_FIXPCK_LG2(2),
[kRepeatedBase + kUpb_FieldType_Int64] = OP_VARPCK_LG2(3),
@@ -1073,22 +1093,23 @@ int _upb_Decoder_GetDelimitedOp(upb_Decoder* d, const upb_MiniTable* mt,
};
int ndx = field->UPB_PRIVATE(descriptortype);
if (upb_FieldMode_Get(field) == kUpb_FieldMode_Array) ndx += kRepeatedBase;
if (upb_MiniTableField_IsArray(field)) ndx += kRepeatedBase;
int op = kDelimitedOps[ndx];
if (op == kUpb_DecodeOp_SubMessage) {
_upb_Decoder_CheckUnlinked(d, mt, field, &op);
} else if (op == kUpb_DecodeOp_Bytes) {
_upb_Decoder_MaybeVerifyUtf8(d, field, &op);
}
return op;
}
UPB_FORCEINLINE
static const char* _upb_Decoder_DecodeWireValue(upb_Decoder* d, const char* ptr,
const upb_MiniTable* mt,
const upb_MiniTableField* field,
int wire_type, wireval* val,
int* op) {
const char* _upb_Decoder_DecodeWireValue(upb_Decoder* d, const char* ptr,
const upb_MiniTable* mt,
const upb_MiniTableField* field,
int wire_type, wireval* val, int* op) {
static const unsigned kFixed32OkMask = (1 << kUpb_FieldType_Float) |
(1 << kUpb_FieldType_Fixed32) |
(1 << kUpb_FieldType_SFixed32);
@@ -1120,7 +1141,7 @@ static const char* _upb_Decoder_DecodeWireValue(upb_Decoder* d, const char* ptr,
*op = _upb_Decoder_GetDelimitedOp(d, mt, field);
return ptr;
case kUpb_WireType_StartGroup:
val->uint32_val = field->number;
val->uint32_val = field->UPB_PRIVATE(number);
if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group) {
*op = kUpb_DecodeOp_SubMessage;
_upb_Decoder_CheckUnlinked(d, mt, field, op);
@@ -1138,24 +1159,33 @@ static const char* _upb_Decoder_DecodeWireValue(upb_Decoder* d, const char* ptr,
}
UPB_FORCEINLINE
static const char* _upb_Decoder_DecodeKnownField(
upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTable* layout, const upb_MiniTableField* field, int op,
wireval* val) {
const upb_MiniTableSub* subs = layout->subs;
uint8_t mode = field->mode;
const char* _upb_Decoder_DecodeKnownField(upb_Decoder* d, const char* ptr,
upb_Message* msg,
const upb_MiniTable* layout,
const upb_MiniTableField* field,
int op, wireval* val) {
const upb_MiniTableSubInternal* subs = layout->UPB_PRIVATE(subs);
uint8_t mode = field->UPB_PRIVATE(mode);
upb_MiniTableSubInternal ext_sub;
if (UPB_UNLIKELY(mode & kUpb_LabelFlags_IsExtension)) {
const upb_MiniTableExtension* ext_layout =
(const upb_MiniTableExtension*)field;
upb_Message_Extension* ext =
_upb_Message_GetOrCreateExtension(msg, ext_layout, &d->arena);
upb_Extension* ext = UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
msg, ext_layout, &d->arena);
if (UPB_UNLIKELY(!ext)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
d->unknown_msg = msg;
msg = &ext->data;
subs = &ext->ext->sub;
msg = (upb_Message*)&ext->data;
if (upb_MiniTableField_IsSubMessage(&ext->ext->UPB_PRIVATE(field))) {
ext_sub.UPB_PRIVATE(submsg) =
&ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(submsg);
} else {
ext_sub.UPB_PRIVATE(subenum) =
ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(subenum);
}
subs = &ext_sub;
}
switch (mode & kUpb_FieldMode_Mask) {
@@ -1224,7 +1254,8 @@ static const char* _upb_Decoder_DecodeUnknownField(upb_Decoder* d,
start = d->unknown;
d->unknown = NULL;
}
if (!_upb_Message_AddUnknown(msg, start, ptr - start, &d->arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, start, ptr - start,
&d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
} else if (wire_type == kUpb_WireType_StartGroup) {
@@ -1296,7 +1327,7 @@ static const char* _upb_Decoder_DecodeMessage(upb_Decoder* d, const char* ptr,
}
}
return UPB_UNLIKELY(layout && layout->required_count)
return UPB_UNLIKELY(layout && layout->UPB_PRIVATE(required_count))
? _upb_Decoder_CheckRequired(d, ptr, msg, layout)
: ptr;
}
@@ -1311,10 +1342,11 @@ const char* _upb_FastDecoder_DecodeGeneric(struct upb_Decoder* d,
}
static upb_DecodeStatus _upb_Decoder_DecodeTop(struct upb_Decoder* d,
const char* buf, void* msg,
const upb_MiniTable* l) {
if (!_upb_Decoder_TryFastDispatch(d, &buf, msg, l)) {
_upb_Decoder_DecodeMessage(d, buf, msg, l);
const char* buf,
upb_Message* msg,
const upb_MiniTable* m) {
if (!_upb_Decoder_TryFastDispatch(d, &buf, msg, m)) {
_upb_Decoder_DecodeMessage(d, buf, msg, m);
}
if (d->end_group != DECODE_NOGROUP) return kUpb_DecodeStatus_Malformed;
if (d->missing_required) return kUpb_DecodeStatus_MissingRequired;
@@ -1330,26 +1362,25 @@ const char* _upb_Decoder_IsDoneFallback(upb_EpsCopyInputStream* e,
static upb_DecodeStatus upb_Decoder_Decode(upb_Decoder* const decoder,
const char* const buf,
void* const msg,
const upb_MiniTable* const l,
upb_Message* const msg,
const upb_MiniTable* const m,
upb_Arena* const arena) {
if (UPB_SETJMP(decoder->err) == 0) {
decoder->status = _upb_Decoder_DecodeTop(decoder, buf, msg, l);
decoder->status = _upb_Decoder_DecodeTop(decoder, buf, msg, m);
} else {
UPB_ASSERT(decoder->status != kUpb_DecodeStatus_Ok);
}
_upb_MemBlock* blocks =
upb_Atomic_Load(&decoder->arena.blocks, memory_order_relaxed);
arena->head = decoder->arena.head;
upb_Atomic_Store(&arena->blocks, blocks, memory_order_relaxed);
UPB_PRIVATE(_upb_Arena_SwapOut)(arena, &decoder->arena);
return decoder->status;
}
upb_DecodeStatus upb_Decode(const char* buf, size_t size, void* msg,
const upb_MiniTable* l,
upb_DecodeStatus upb_Decode(const char* buf, size_t size, upb_Message* msg,
const upb_MiniTable* mt,
const upb_ExtensionRegistry* extreg, int options,
upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Decoder decoder;
unsigned depth = (unsigned)options >> 16;
@@ -1369,12 +1400,65 @@ upb_DecodeStatus upb_Decode(const char* buf, size_t size, void* msg,
// done. The temporary arena only needs to be able to handle allocation,
// not fuse or free, so it does not need many of the members to be initialized
// (particularly parent_or_count).
_upb_MemBlock* blocks = upb_Atomic_Load(&arena->blocks, memory_order_relaxed);
decoder.arena.head = arena->head;
decoder.arena.block_alloc = arena->block_alloc;
upb_Atomic_Init(&decoder.arena.blocks, blocks);
UPB_PRIVATE(_upb_Arena_SwapIn)(&decoder.arena, arena);
return upb_Decoder_Decode(&decoder, buf, msg, l, arena);
return upb_Decoder_Decode(&decoder, buf, msg, mt, arena);
}
upb_DecodeStatus upb_DecodeLengthPrefixed(const char* buf, size_t size,
upb_Message* msg,
size_t* num_bytes_read,
const upb_MiniTable* mt,
const upb_ExtensionRegistry* extreg,
int options, upb_Arena* arena) {
// To avoid needing to make a Decoder just to decode the initial length,
// hand-decode the leading varint for the message length here.
uint64_t msg_len = 0;
for (size_t i = 0;; ++i) {
if (i >= size || i > 9) {
return kUpb_DecodeStatus_Malformed;
}
uint64_t b = *buf;
buf++;
msg_len += (b & 0x7f) << (i * 7);
if ((b & 0x80) == 0) {
*num_bytes_read = i + 1 + msg_len;
break;
}
}
// If the total number of bytes we would read (= the bytes from the varint
// plus however many bytes that varint says we should read) is larger then the
// input buffer then error as malformed.
if (*num_bytes_read > size) {
return kUpb_DecodeStatus_Malformed;
}
if (msg_len > INT32_MAX) {
return kUpb_DecodeStatus_Malformed;
}
return upb_Decode(buf, msg_len, msg, mt, extreg, options, arena);
}
const char* upb_DecodeStatus_String(upb_DecodeStatus status) {
switch (status) {
case kUpb_DecodeStatus_Ok:
return "Ok";
case kUpb_DecodeStatus_Malformed:
return "Wire format was corrupt";
case kUpb_DecodeStatus_OutOfMemory:
return "Arena alloc failed";
case kUpb_DecodeStatus_BadUtf8:
return "String field had bad UTF-8";
case kUpb_DecodeStatus_MaxDepthExceeded:
return "Exceeded upb_DecodeOptions_MaxDepth";
case kUpb_DecodeStatus_MissingRequired:
return "Missing required field";
case kUpb_DecodeStatus_UnlinkedSubMessage:
return "Unlinked sub-message field was present";
default:
return "Unknown decode status";
}
}
#undef OP_FIXPCK_LG2

View File

@@ -45,7 +45,7 @@ enum {
* already has some sub-message fields present. If the sub-message does
* not occur in the binary payload, we will never visit it and discover the
* incomplete sub-message. For this reason, this check is only useful for
* implemting ParseFromString() semantics. For MergeFromString(), a
* implementing ParseFromString() semantics. For MergeFromString(), a
* post-parse validation step will always be necessary. */
kUpb_DecodeOption_CheckRequired = 2,
@@ -83,6 +83,16 @@ enum {
* be created by the parser or the message-copying logic in message/copy.h.
*/
kUpb_DecodeOption_ExperimentalAllowUnlinked = 4,
/* EXPERIMENTAL:
*
* If set, decoding will enforce UTF-8 validation for string fields, even for
* proto2 or fields with `features.utf8_validation = NONE`. Normally, only
* proto3 string fields will be validated for UTF-8. Decoding will return
* kUpb_DecodeStatus_BadUtf8 for non-UTF-8 strings, which is the same behavior
* as non-UTF-8 proto3 string fields.
*/
kUpb_DecodeOption_AlwaysValidateUtf8 = 8,
};
UPB_INLINE uint32_t upb_DecodeOptions_MaxDepth(uint16_t depth) {
@@ -100,6 +110,7 @@ UPB_INLINE int upb_Decode_LimitDepth(uint32_t decode_options, uint32_t limit) {
return upb_DecodeOptions_MaxDepth(max_depth) | (decode_options & 0xffff);
}
// LINT.IfChange
typedef enum {
kUpb_DecodeStatus_Ok = 0,
kUpb_DecodeStatus_Malformed = 1, // Wire format was corrupt
@@ -117,12 +128,24 @@ typedef enum {
// of options.
kUpb_DecodeStatus_UnlinkedSubMessage = 6,
} upb_DecodeStatus;
// LINT.ThenChange(//depot/google3/third_party/protobuf/rust/upb.rs:decode_status)
UPB_API upb_DecodeStatus upb_Decode(const char* buf, size_t size,
upb_Message* msg, const upb_MiniTable* l,
upb_Message* msg, const upb_MiniTable* mt,
const upb_ExtensionRegistry* extreg,
int options, upb_Arena* arena);
// Same as upb_Decode but with a varint-encoded length prepended.
// On success 'num_bytes_read' will be set to the how many bytes were read,
// on failure the contents of num_bytes_read is undefined.
UPB_API upb_DecodeStatus upb_DecodeLengthPrefixed(
const char* buf, size_t size, upb_Message* msg, size_t* num_bytes_read,
const upb_MiniTable* mt, const upb_ExtensionRegistry* extreg, int options,
upb_Arena* arena);
// Utility function for wrapper languages to get an error string from a
// upb_DecodeStatus.
UPB_API const char* upb_DecodeStatus_String(upb_DecodeStatus status);
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -15,6 +15,7 @@
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/endian.h"
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/hash/str_table.h"
@@ -26,20 +27,29 @@
#include "upb/message/internal/map.h"
#include "upb/message/internal/map_entry.h"
#include "upb/message/internal/map_sorter.h"
#include "upb/message/internal/tagged_ptr.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/message/tagged_ptr.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/message.h"
#include "upb/mini_table/internal/sub.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
#include "upb/wire/internal/constants.h"
#include "upb/wire/internal/swap.h"
#include "upb/wire/types.h"
// Must be last.
#include "upb/port/def.inc"
// Returns the MiniTable corresponding to a given MiniTableField
// from an array of MiniTableSubs.
static const upb_MiniTable* _upb_Encoder_GetSubMiniTable(
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field) {
return *subs[field->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(submsg);
}
#define UPB_PB_VARINT_MAX_LEN 10
UPB_NOINLINE
@@ -110,7 +120,7 @@ static void encode_growbuffer(upb_encstate* e, size_t bytes) {
/* Call to ensure that at least "bytes" bytes are available for writing at
* e->ptr. Returns false if the bytes could not be allocated. */
UPB_FORCEINLINE
static void encode_reserve(upb_encstate* e, size_t bytes) {
void encode_reserve(upb_encstate* e, size_t bytes) {
if ((size_t)(e->ptr - e->buf) < bytes) {
encode_growbuffer(e, bytes);
return;
@@ -127,12 +137,12 @@ static void encode_bytes(upb_encstate* e, const void* data, size_t len) {
}
static void encode_fixed64(upb_encstate* e, uint64_t val) {
val = _upb_BigEndian_Swap64(val);
val = upb_BigEndian64(val);
encode_bytes(e, &val, sizeof(uint64_t));
}
static void encode_fixed32(upb_encstate* e, uint32_t val) {
val = _upb_BigEndian_Swap32(val);
val = upb_BigEndian32(val);
encode_bytes(e, &val, sizeof(uint32_t));
}
@@ -149,7 +159,7 @@ static void encode_longvarint(upb_encstate* e, uint64_t val) {
}
UPB_FORCEINLINE
static void encode_varint(upb_encstate* e, uint64_t val) {
void encode_varint(upb_encstate* e, uint64_t val) {
if (val < 128 && e->ptr != e->buf) {
--e->ptr;
*e->ptr = val;
@@ -179,22 +189,22 @@ static void encode_tag(upb_encstate* e, uint32_t field_number,
static void encode_fixedarray(upb_encstate* e, const upb_Array* arr,
size_t elem_size, uint32_t tag) {
size_t bytes = arr->size * elem_size;
const char* data = _upb_array_constptr(arr);
size_t bytes = upb_Array_Size(arr) * elem_size;
const char* data = upb_Array_DataPtr(arr);
const char* ptr = data + bytes - elem_size;
if (tag || !_upb_IsLittleEndian()) {
if (tag || !upb_IsLittleEndian()) {
while (true) {
if (elem_size == 4) {
uint32_t val;
memcpy(&val, ptr, sizeof(val));
val = _upb_BigEndian_Swap32(val);
val = upb_BigEndian32(val);
encode_bytes(e, &val, elem_size);
} else {
UPB_ASSERT(elem_size == 8);
uint64_t val;
memcpy(&val, ptr, sizeof(val));
val = _upb_BigEndian_Swap64(val);
val = upb_BigEndian64(val);
encode_bytes(e, &val, elem_size);
}
@@ -214,13 +224,14 @@ static void encode_TaggedMessagePtr(upb_encstate* e,
upb_TaggedMessagePtr tagged,
const upb_MiniTable* m, size_t* size) {
if (upb_TaggedMessagePtr_IsEmpty(tagged)) {
m = &_kUpb_MiniTable_Empty;
m = UPB_PRIVATE(_upb_MiniTable_Empty)();
}
encode_message(e, _upb_TaggedMessagePtr_GetMessage(tagged), m, size);
encode_message(e, UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(tagged), m,
size);
}
static void encode_scalar(upb_encstate* e, const void* _field_mem,
const upb_MiniTableSub* subs,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* f) {
const char* field_mem = _field_mem;
int wire_type;
@@ -269,12 +280,12 @@ static void encode_scalar(upb_encstate* e, const void* _field_mem,
case kUpb_FieldType_Group: {
size_t size;
upb_TaggedMessagePtr submsg = *(upb_TaggedMessagePtr*)field_mem;
const upb_MiniTable* subm = subs[f->UPB_PRIVATE(submsg_index)].submsg;
const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
if (submsg == 0) {
return;
}
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
encode_tag(e, f->number, kUpb_WireType_EndGroup);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_EndGroup);
encode_TaggedMessagePtr(e, submsg, subm, &size);
wire_type = kUpb_WireType_StartGroup;
e->depth++;
@@ -283,7 +294,7 @@ static void encode_scalar(upb_encstate* e, const void* _field_mem,
case kUpb_FieldType_Message: {
size_t size;
upb_TaggedMessagePtr submsg = *(upb_TaggedMessagePtr*)field_mem;
const upb_MiniTable* subm = subs[f->UPB_PRIVATE(submsg_index)].submsg;
const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
if (submsg == 0) {
return;
}
@@ -299,34 +310,35 @@ static void encode_scalar(upb_encstate* e, const void* _field_mem,
}
#undef CASE
encode_tag(e, f->number, wire_type);
encode_tag(e, upb_MiniTableField_Number(f), wire_type);
}
static void encode_array(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableSub* subs,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* f) {
const upb_Array* arr = *UPB_PTR_AT(msg, f->offset, upb_Array*);
bool packed = f->mode & kUpb_LabelFlags_IsPacked;
const upb_Array* arr = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), upb_Array*);
bool packed = upb_MiniTableField_IsPacked(f);
size_t pre_len = e->limit - e->ptr;
if (arr == NULL || arr->size == 0) {
if (arr == NULL || upb_Array_Size(arr) == 0) {
return;
}
#define VARINT_CASE(ctype, encode) \
{ \
const ctype* start = _upb_array_constptr(arr); \
const ctype* ptr = start + arr->size; \
uint32_t tag = packed ? 0 : (f->number << 3) | kUpb_WireType_Varint; \
do { \
ptr--; \
encode_varint(e, encode); \
if (tag) encode_varint(e, tag); \
} while (ptr != start); \
} \
#define VARINT_CASE(ctype, encode) \
{ \
const ctype* start = upb_Array_DataPtr(arr); \
const ctype* ptr = start + upb_Array_Size(arr); \
uint32_t tag = \
packed ? 0 : (f->UPB_PRIVATE(number) << 3) | kUpb_WireType_Varint; \
do { \
ptr--; \
encode_varint(e, encode); \
if (tag) encode_varint(e, tag); \
} while (ptr != start); \
} \
break;
#define TAG(wire_type) (packed ? 0 : (f->number << 3 | wire_type))
#define TAG(wire_type) (packed ? 0 : (f->UPB_PRIVATE(number) << 3 | wire_type))
switch (f->UPB_PRIVATE(descriptortype)) {
case kUpb_FieldType_Double:
@@ -359,42 +371,42 @@ static void encode_array(upb_encstate* e, const upb_Message* msg,
VARINT_CASE(int64_t, encode_zz64(*ptr));
case kUpb_FieldType_String:
case kUpb_FieldType_Bytes: {
const upb_StringView* start = _upb_array_constptr(arr);
const upb_StringView* ptr = start + arr->size;
const upb_StringView* start = upb_Array_DataPtr(arr);
const upb_StringView* ptr = start + upb_Array_Size(arr);
do {
ptr--;
encode_bytes(e, ptr->data, ptr->size);
encode_varint(e, ptr->size);
encode_tag(e, f->number, kUpb_WireType_Delimited);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
} while (ptr != start);
return;
}
case kUpb_FieldType_Group: {
const upb_TaggedMessagePtr* start = _upb_array_constptr(arr);
const upb_TaggedMessagePtr* ptr = start + arr->size;
const upb_MiniTable* subm = subs[f->UPB_PRIVATE(submsg_index)].submsg;
const upb_TaggedMessagePtr* start = upb_Array_DataPtr(arr);
const upb_TaggedMessagePtr* ptr = start + upb_Array_Size(arr);
const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
do {
size_t size;
ptr--;
encode_tag(e, f->number, kUpb_WireType_EndGroup);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_EndGroup);
encode_TaggedMessagePtr(e, *ptr, subm, &size);
encode_tag(e, f->number, kUpb_WireType_StartGroup);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_StartGroup);
} while (ptr != start);
e->depth++;
return;
}
case kUpb_FieldType_Message: {
const upb_TaggedMessagePtr* start = _upb_array_constptr(arr);
const upb_TaggedMessagePtr* ptr = start + arr->size;
const upb_MiniTable* subm = subs[f->UPB_PRIVATE(submsg_index)].submsg;
const upb_TaggedMessagePtr* start = upb_Array_DataPtr(arr);
const upb_TaggedMessagePtr* ptr = start + upb_Array_Size(arr);
const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
do {
size_t size;
ptr--;
encode_TaggedMessagePtr(e, *ptr, subm, &size);
encode_varint(e, size);
encode_tag(e, f->number, kUpb_WireType_Delimited);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
} while (ptr != start);
e->depth++;
return;
@@ -404,41 +416,41 @@ static void encode_array(upb_encstate* e, const upb_Message* msg,
if (packed) {
encode_varint(e, e->limit - e->ptr - pre_len);
encode_tag(e, f->number, kUpb_WireType_Delimited);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
}
}
static void encode_mapentry(upb_encstate* e, uint32_t number,
const upb_MiniTable* layout,
const upb_MapEntry* ent) {
const upb_MiniTableField* key_field = &layout->fields[0];
const upb_MiniTableField* val_field = &layout->fields[1];
const upb_MiniTableField* key_field = upb_MiniTable_MapKey(layout);
const upb_MiniTableField* val_field = upb_MiniTable_MapValue(layout);
size_t pre_len = e->limit - e->ptr;
size_t size;
encode_scalar(e, &ent->data.v, layout->subs, val_field);
encode_scalar(e, &ent->data.k, layout->subs, key_field);
encode_scalar(e, &ent->v, layout->UPB_PRIVATE(subs), val_field);
encode_scalar(e, &ent->k, layout->UPB_PRIVATE(subs), key_field);
size = (e->limit - e->ptr) - pre_len;
encode_varint(e, size);
encode_tag(e, number, kUpb_WireType_Delimited);
}
static void encode_map(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableSub* subs,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* f) {
const upb_Map* map = *UPB_PTR_AT(msg, f->offset, const upb_Map*);
const upb_MiniTable* layout = subs[f->UPB_PRIVATE(submsg_index)].submsg;
UPB_ASSERT(layout->field_count == 2);
const upb_Map* map = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), const upb_Map*);
const upb_MiniTable* layout = _upb_Encoder_GetSubMiniTable(subs, f);
UPB_ASSERT(upb_MiniTable_FieldCount(layout) == 2);
if (map == NULL) return;
if (!map || !upb_Map_Size(map)) return;
if (e->options & kUpb_EncodeOption_Deterministic) {
_upb_sortedmap sorted;
_upb_mapsorter_pushmap(&e->sorter,
layout->fields[0].UPB_PRIVATE(descriptortype), map,
&sorted);
_upb_mapsorter_pushmap(
&e->sorter, layout->UPB_PRIVATE(fields)[0].UPB_PRIVATE(descriptortype),
map, &sorted);
upb_MapEntry ent;
while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) {
encode_mapentry(e, f->number, layout, &ent);
encode_mapentry(e, upb_MiniTableField_Number(f), layout, &ent);
}
_upb_mapsorter_popmap(&e->sorter, &sorted);
} else {
@@ -447,20 +459,19 @@ static void encode_map(upb_encstate* e, const upb_Message* msg,
upb_value val;
while (upb_strtable_next2(&map->table, &key, &val, &iter)) {
upb_MapEntry ent;
_upb_map_fromkey(key, &ent.data.k, map->key_size);
_upb_map_fromvalue(val, &ent.data.v, map->val_size);
encode_mapentry(e, f->number, layout, &ent);
_upb_map_fromkey(key, &ent.k, map->key_size);
_upb_map_fromvalue(val, &ent.v, map->val_size);
encode_mapentry(e, upb_MiniTableField_Number(f), layout, &ent);
}
}
}
static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableSub* subs,
const upb_MiniTableField* f) {
if (f->presence == 0) {
/* Proto3 presence or map/array. */
const void* mem = UPB_PTR_AT(msg, f->offset, void);
switch (_upb_MiniTableField_GetRep(f)) {
// Proto3 presence or map/array.
const void* mem = UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), void);
switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
case kUpb_FieldRep_1Byte: {
char ch;
memcpy(&ch, mem, 1);
@@ -483,19 +494,20 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg,
default:
UPB_UNREACHABLE();
}
} else if (f->presence > 0) {
/* Proto2 presence: hasbit. */
return _upb_hasbit_field(msg, f);
} else if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
// Proto2 presence: hasbit.
return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, f);
} else {
/* Field is in a oneof. */
return _upb_getoneofcase_field(msg, f) == f->number;
// Field is in a oneof.
return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, f) ==
upb_MiniTableField_Number(f);
}
}
static void encode_field(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableSub* subs,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* field) {
switch (upb_FieldMode_Get(field)) {
switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(field)) {
case kUpb_FieldMode_Array:
encode_array(e, msg, subs, field);
break;
@@ -503,31 +515,40 @@ static void encode_field(upb_encstate* e, const upb_Message* msg,
encode_map(e, msg, subs, field);
break;
case kUpb_FieldMode_Scalar:
encode_scalar(e, UPB_PTR_AT(msg, field->offset, void), subs, field);
encode_scalar(e, UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void), subs,
field);
break;
default:
UPB_UNREACHABLE();
}
}
static void encode_msgset_item(upb_encstate* e,
const upb_Message_Extension* ext) {
static void encode_msgset_item(upb_encstate* e, const upb_Extension* ext) {
size_t size;
encode_tag(e, kUpb_MsgSet_Item, kUpb_WireType_EndGroup);
encode_message(e, ext->data.ptr, ext->ext->sub.submsg, &size);
encode_message(e, ext->data.msg_val,
upb_MiniTableExtension_GetSubMessage(ext->ext), &size);
encode_varint(e, size);
encode_tag(e, kUpb_MsgSet_Message, kUpb_WireType_Delimited);
encode_varint(e, ext->ext->field.number);
encode_varint(e, upb_MiniTableExtension_Number(ext->ext));
encode_tag(e, kUpb_MsgSet_TypeId, kUpb_WireType_Varint);
encode_tag(e, kUpb_MsgSet_Item, kUpb_WireType_StartGroup);
}
static void encode_ext(upb_encstate* e, const upb_Message_Extension* ext,
static void encode_ext(upb_encstate* e, const upb_Extension* ext,
bool is_message_set) {
if (UPB_UNLIKELY(is_message_set)) {
encode_msgset_item(e, ext);
} else {
encode_field(e, &ext->data, &ext->ext->sub, &ext->ext->field);
upb_MiniTableSubInternal sub;
if (upb_MiniTableField_IsSubMessage(&ext->ext->UPB_PRIVATE(field))) {
sub.UPB_PRIVATE(submsg) = &ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(submsg);
} else {
sub.UPB_PRIVATE(subenum) =
ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(subenum);
}
encode_field(e, (upb_Message*)&ext->data, &sub,
&ext->ext->UPB_PRIVATE(field));
}
}
@@ -535,12 +556,11 @@ static void encode_message(upb_encstate* e, const upb_Message* msg,
const upb_MiniTable* m, size_t* size) {
size_t pre_len = e->limit - e->ptr;
if ((e->options & kUpb_EncodeOption_CheckRequired) && m->required_count) {
uint64_t msg_head;
memcpy(&msg_head, msg, 8);
msg_head = _upb_BigEndian_Swap64(msg_head);
if (upb_MiniTable_requiredmask(m) & ~msg_head) {
encode_err(e, kUpb_EncodeStatus_MissingRequired);
if (e->options & kUpb_EncodeOption_CheckRequired) {
if (m->UPB_PRIVATE(required_count)) {
if (!UPB_PRIVATE(_upb_Message_IsInitializedShallow)(msg, m)) {
encode_err(e, kUpb_EncodeStatus_MissingRequired);
}
}
}
@@ -553,36 +573,38 @@ static void encode_message(upb_encstate* e, const upb_Message* msg,
}
}
if (m->ext != kUpb_ExtMode_NonExtendable) {
if (m->UPB_PRIVATE(ext) != kUpb_ExtMode_NonExtendable) {
/* Encode all extensions together. Unlike C++, we do not attempt to keep
* these in field number order relative to normal fields or even to each
* other. */
size_t ext_count;
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &ext_count);
const upb_Extension* ext =
UPB_PRIVATE(_upb_Message_Getexts)(msg, &ext_count);
if (ext_count) {
if (e->options & kUpb_EncodeOption_Deterministic) {
_upb_sortedmap sorted;
_upb_mapsorter_pushexts(&e->sorter, ext, ext_count, &sorted);
while (_upb_sortedmap_nextext(&e->sorter, &sorted, &ext)) {
encode_ext(e, ext, m->ext == kUpb_ExtMode_IsMessageSet);
encode_ext(e, ext, m->UPB_PRIVATE(ext) == kUpb_ExtMode_IsMessageSet);
}
_upb_mapsorter_popmap(&e->sorter, &sorted);
} else {
const upb_Message_Extension* end = ext + ext_count;
const upb_Extension* end = ext + ext_count;
for (; ext != end; ext++) {
encode_ext(e, ext, m->ext == kUpb_ExtMode_IsMessageSet);
encode_ext(e, ext, m->UPB_PRIVATE(ext) == kUpb_ExtMode_IsMessageSet);
}
}
}
}
if (m->field_count) {
const upb_MiniTableField* f = &m->fields[m->field_count];
const upb_MiniTableField* first = &m->fields[0];
if (upb_MiniTable_FieldCount(m)) {
const upb_MiniTableField* f =
&m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)];
const upb_MiniTableField* first = &m->UPB_PRIVATE(fields)[0];
while (f != first) {
f--;
if (encode_shouldencode(e, msg, m->subs, f)) {
encode_field(e, msg, m->subs, f);
if (encode_shouldencode(e, msg, f)) {
encode_field(e, msg, m->UPB_PRIVATE(subs), f);
}
}
}
@@ -591,16 +613,20 @@ static void encode_message(upb_encstate* e, const upb_Message* msg,
}
static upb_EncodeStatus upb_Encoder_Encode(upb_encstate* const encoder,
const void* const msg,
const upb_Message* const msg,
const upb_MiniTable* const l,
char** const buf,
size_t* const size) {
char** const buf, size_t* const size,
bool prepend_len) {
// Unfortunately we must continue to perform hackery here because there are
// code paths which blindly copy the returned pointer without bothering to
// check for errors until much later (b/235839510). So we still set *buf to
// NULL on error and we still set it to non-NULL on a successful empty result.
if (UPB_SETJMP(encoder->err) == 0) {
encode_message(encoder, msg, l, size);
size_t encoded_msg_size;
encode_message(encoder, msg, l, &encoded_msg_size);
if (prepend_len) {
encode_varint(encoder, encoded_msg_size);
}
*size = encoder->limit - encoder->ptr;
if (*size == 0) {
static char ch;
@@ -619,9 +645,10 @@ static upb_EncodeStatus upb_Encoder_Encode(upb_encstate* const encoder,
return encoder->status;
}
upb_EncodeStatus upb_Encode(const void* msg, const upb_MiniTable* l,
int options, upb_Arena* arena, char** buf,
size_t* size) {
static upb_EncodeStatus _upb_Encode(const upb_Message* msg,
const upb_MiniTable* l, int options,
upb_Arena* arena, char** buf, size_t* size,
bool prepend_len) {
upb_encstate e;
unsigned depth = (unsigned)options >> 16;
@@ -634,5 +661,33 @@ upb_EncodeStatus upb_Encode(const void* msg, const upb_MiniTable* l,
e.options = options;
_upb_mapsorter_init(&e.sorter);
return upb_Encoder_Encode(&e, msg, l, buf, size);
return upb_Encoder_Encode(&e, msg, l, buf, size, prepend_len);
}
upb_EncodeStatus upb_Encode(const upb_Message* msg, const upb_MiniTable* l,
int options, upb_Arena* arena, char** buf,
size_t* size) {
return _upb_Encode(msg, l, options, arena, buf, size, false);
}
upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message* msg,
const upb_MiniTable* l, int options,
upb_Arena* arena, char** buf,
size_t* size) {
return _upb_Encode(msg, l, options, arena, buf, size, true);
}
const char* upb_EncodeStatus_String(upb_EncodeStatus status) {
switch (status) {
case kUpb_EncodeStatus_Ok:
return "Ok";
case kUpb_EncodeStatus_MissingRequired:
return "Missing required field";
case kUpb_EncodeStatus_MaxDepthExceeded:
return "Max depth exceeded";
case kUpb_EncodeStatus_OutOfMemory:
return "Arena alloc failed";
default:
return "Unknown encode status";
}
}

View File

@@ -14,6 +14,7 @@
#include <stdint.h>
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/mini_table/message.h"
// Must be last.
@@ -32,13 +33,14 @@ enum {
* memory during encode. */
kUpb_EncodeOption_Deterministic = 1,
// When set, unknown fields are not printed.
// When set, unknown fields are not encoded.
kUpb_EncodeOption_SkipUnknown = 2,
// When set, the encode will fail if any required fields are missing.
kUpb_EncodeOption_CheckRequired = 4,
};
// LINT.IfChange
typedef enum {
kUpb_EncodeStatus_Ok = 0,
kUpb_EncodeStatus_OutOfMemory = 1, // Arena alloc failed
@@ -47,6 +49,7 @@ typedef enum {
// kUpb_EncodeOption_CheckRequired failed but the parse otherwise succeeded.
kUpb_EncodeStatus_MissingRequired = 3,
} upb_EncodeStatus;
// LINT.ThenChange(//depot/google3/third_party/protobuf/rust/upb.rs:encode_status)
UPB_INLINE uint32_t upb_EncodeOptions_MaxDepth(uint16_t depth) {
return (uint32_t)depth << 16;
@@ -63,9 +66,18 @@ UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options, uint32_t limit) {
return upb_EncodeOptions_MaxDepth(max_depth) | (encode_options & 0xffff);
}
UPB_API upb_EncodeStatus upb_Encode(const void* msg, const upb_MiniTable* l,
int options, upb_Arena* arena, char** buf,
size_t* size);
UPB_API upb_EncodeStatus upb_Encode(const upb_Message* msg,
const upb_MiniTable* l, int options,
upb_Arena* arena, char** buf, size_t* size);
// Encodes the message prepended by a varint of the serialized length.
UPB_API upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message* msg,
const upb_MiniTable* l,
int options, upb_Arena* arena,
char** buf, size_t* size);
// Utility function for wrapper languages to get an error string from a
// upb_EncodeStatus.
UPB_API const char* upb_EncodeStatus_String(upb_EncodeStatus status);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -37,8 +37,8 @@ typedef struct {
const char* end; // Can read up to SlopBytes bytes beyond this.
const char* limit_ptr; // For bounds checks, = end + UPB_MIN(limit, 0)
uintptr_t aliasing;
int limit; // Submessage limit relative to end
bool error; // To distinguish between EOF and error.
int limit; // Submessage limit relative to end
bool error; // To distinguish between EOF and error.
char patch[kUpb_EpsCopyInputStream_SlopBytes * 2];
} upb_EpsCopyInputStream;
@@ -375,7 +375,7 @@ typedef const char* upb_EpsCopyInputStream_ParseDelimitedFunc(
// fits within this buffer, calls `func` with `ctx` as a parameter, where the
// pushing and popping of limits is handled automatically and with lower cost
// than the normal PushLimit()/PopLimit() sequence.
static UPB_FORCEINLINE bool upb_EpsCopyInputStream_TryParseDelimitedFast(
UPB_FORCEINLINE bool upb_EpsCopyInputStream_TryParseDelimitedFast(
upb_EpsCopyInputStream* e, const char** ptr, int len,
upb_EpsCopyInputStream_ParseDelimitedFunc* func, void* ctx) {
if (!upb_EpsCopyInputStream_CheckSubMessageSizeAvailable(e, *ptr, len)) {

View File

@@ -15,12 +15,12 @@
// field type (eg. oneof boolean field with a 1 byte tag) and then dispatch
// to the specialized function as quickly as possible.
#include "upb/wire/decode_fast.h"
#include "upb/wire/internal/decode_fast.h"
#include "upb/message/array.h"
#include "upb/message/internal/array.h"
#include "upb/message/internal/types.h"
#include "upb/wire/internal/decode.h"
#include "upb/mini_table/sub.h"
#include "upb/wire/internal/decoder.h"
// Must be last.
#include "upb/port/def.inc"
@@ -58,14 +58,14 @@ static const char* fastdecode_isdonefallback(UPB_PARSE_PARAMS) {
}
UPB_FORCEINLINE
static const char* fastdecode_dispatch(UPB_PARSE_PARAMS) {
const char* fastdecode_dispatch(UPB_PARSE_PARAMS) {
int overrun;
switch (upb_EpsCopyInputStream_IsDoneStatus(&d->input, ptr, &overrun)) {
case kUpb_IsDoneStatus_Done:
*(uint32_t*)msg |= hasbits; // Sync hasbits.
const upb_MiniTable* l = decode_totablep(table);
return UPB_UNLIKELY(l->required_count)
? _upb_Decoder_CheckRequired(d, ptr, msg, l)
((uint32_t*)msg)[2] |= hasbits; // Sync hasbits.
const upb_MiniTable* m = decode_totablep(table);
return UPB_UNLIKELY(m->UPB_PRIVATE(required_count))
? _upb_Decoder_CheckRequired(d, ptr, msg, m)
: ptr;
case kUpb_IsDoneStatus_NotDone:
break;
@@ -80,7 +80,7 @@ static const char* fastdecode_dispatch(UPB_PARSE_PARAMS) {
}
UPB_FORCEINLINE
static bool fastdecode_checktag(uint16_t data, int tagbytes) {
bool fastdecode_checktag(uint16_t data, int tagbytes) {
if (tagbytes == 1) {
return (data & 0xff) == 0;
} else {
@@ -89,7 +89,7 @@ static bool fastdecode_checktag(uint16_t data, int tagbytes) {
}
UPB_FORCEINLINE
static const char* fastdecode_longsize(const char* ptr, int* size) {
const char* fastdecode_longsize(const char* ptr, int* size) {
int i;
UPB_ASSERT(*size & 0x80);
*size &= 0xff;
@@ -109,7 +109,7 @@ static const char* fastdecode_longsize(const char* ptr, int* size) {
}
UPB_FORCEINLINE
static const char* fastdecode_delimited(
const char* fastdecode_delimited(
upb_Decoder* d, const char* ptr,
upb_EpsCopyInputStream_ParseDelimitedFunc* func, void* ctx) {
ptr++;
@@ -162,26 +162,26 @@ typedef struct {
} fastdecode_nextret;
UPB_FORCEINLINE
static void* fastdecode_resizearr(upb_Decoder* d, void* dst,
fastdecode_arr* farr, int valbytes) {
void* fastdecode_resizearr(upb_Decoder* d, void* dst, fastdecode_arr* farr,
int valbytes) {
if (UPB_UNLIKELY(dst == farr->end)) {
size_t old_size = farr->arr->capacity;
size_t old_bytes = old_size * valbytes;
size_t new_size = old_size * 2;
size_t new_bytes = new_size * valbytes;
char* old_ptr = _upb_array_ptr(farr->arr);
size_t old_capacity = farr->arr->UPB_PRIVATE(capacity);
size_t old_bytes = old_capacity * valbytes;
size_t new_capacity = old_capacity * 2;
size_t new_bytes = new_capacity * valbytes;
char* old_ptr = upb_Array_MutableDataPtr(farr->arr);
char* new_ptr = upb_Arena_Realloc(&d->arena, old_ptr, old_bytes, new_bytes);
uint8_t elem_size_lg2 = __builtin_ctz(valbytes);
farr->arr->capacity = new_size;
farr->arr->data = _upb_array_tagptr(new_ptr, elem_size_lg2);
dst = (void*)(new_ptr + (old_size * valbytes));
farr->end = (void*)(new_ptr + (new_size * valbytes));
UPB_PRIVATE(_upb_Array_SetTaggedPtr)(farr->arr, new_ptr, elem_size_lg2);
farr->arr->UPB_PRIVATE(capacity) = new_capacity;
dst = (void*)(new_ptr + (old_capacity * valbytes));
farr->end = (void*)(new_ptr + (new_capacity * valbytes));
}
return dst;
}
UPB_FORCEINLINE
static bool fastdecode_tagmatch(uint32_t tag, uint64_t data, int tagbytes) {
bool fastdecode_tagmatch(uint32_t tag, uint64_t data, int tagbytes) {
if (tagbytes == 1) {
return (uint8_t)tag == (uint8_t)data;
} else {
@@ -190,18 +190,17 @@ static bool fastdecode_tagmatch(uint32_t tag, uint64_t data, int tagbytes) {
}
UPB_FORCEINLINE
static void fastdecode_commitarr(void* dst, fastdecode_arr* farr,
int valbytes) {
farr->arr->size =
(size_t)((char*)dst - (char*)_upb_array_ptr(farr->arr)) / valbytes;
void fastdecode_commitarr(void* dst, fastdecode_arr* farr, int valbytes) {
farr->arr->UPB_PRIVATE(size) =
(size_t)((char*)dst - (char*)upb_Array_MutableDataPtr(farr->arr)) /
valbytes;
}
UPB_FORCEINLINE
static fastdecode_nextret fastdecode_nextrepeated(upb_Decoder* d, void* dst,
const char** ptr,
fastdecode_arr* farr,
uint64_t data, int tagbytes,
int valbytes) {
fastdecode_nextret fastdecode_nextrepeated(upb_Decoder* d, void* dst,
const char** ptr,
fastdecode_arr* farr, uint64_t data,
int tagbytes, int valbytes) {
fastdecode_nextret ret;
dst = (char*)dst + valbytes;
@@ -223,16 +222,16 @@ static fastdecode_nextret fastdecode_nextrepeated(upb_Decoder* d, void* dst,
}
UPB_FORCEINLINE
static void* fastdecode_fieldmem(upb_Message* msg, uint64_t data) {
void* fastdecode_fieldmem(upb_Message* msg, uint64_t data) {
size_t ofs = data >> 48;
return (char*)msg + ofs;
}
UPB_FORCEINLINE
static void* fastdecode_getfield(upb_Decoder* d, const char* ptr,
upb_Message* msg, uint64_t* data,
uint64_t* hasbits, fastdecode_arr* farr,
int valbytes, upb_card card) {
void* fastdecode_getfield(upb_Decoder* d, const char* ptr, upb_Message* msg,
uint64_t* data, uint64_t* hasbits,
fastdecode_arr* farr, int valbytes, upb_card card) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
switch (card) {
case CARD_s: {
uint8_t hasbit_index = *data >> 24;
@@ -252,18 +251,18 @@ static void* fastdecode_getfield(upb_Decoder* d, const char* ptr,
uint8_t elem_size_lg2 = __builtin_ctz(valbytes);
upb_Array** arr_p = fastdecode_fieldmem(msg, *data);
char* begin;
*(uint32_t*)msg |= *hasbits;
((uint32_t*)msg)[2] |= *hasbits;
*hasbits = 0;
if (UPB_LIKELY(!*arr_p)) {
farr->arr = _upb_Array_New(&d->arena, 8, elem_size_lg2);
farr->arr = UPB_PRIVATE(_upb_Array_New)(&d->arena, 8, elem_size_lg2);
*arr_p = farr->arr;
} else {
farr->arr = *arr_p;
}
begin = _upb_array_ptr(farr->arr);
farr->end = begin + (farr->arr->capacity * valbytes);
begin = upb_Array_MutableDataPtr(farr->arr);
farr->end = begin + (farr->arr->UPB_PRIVATE(capacity) * valbytes);
*data = _upb_FastDecoder_LoadTag(ptr);
return begin + (farr->arr->size * valbytes);
return begin + (farr->arr->UPB_PRIVATE(size) * valbytes);
}
default:
UPB_UNREACHABLE();
@@ -271,7 +270,7 @@ static void* fastdecode_getfield(upb_Decoder* d, const char* ptr,
}
UPB_FORCEINLINE
static bool fastdecode_flippacked(uint64_t* data, int tagbytes) {
bool fastdecode_flippacked(uint64_t* data, int tagbytes) {
*data ^= (0x2 ^ 0x0); // Patch data to match packed wiretype.
return fastdecode_checktag(*data, tagbytes);
}
@@ -287,7 +286,7 @@ static bool fastdecode_flippacked(uint64_t* data, int tagbytes) {
/* varint fields **************************************************************/
UPB_FORCEINLINE
static uint64_t fastdecode_munge(uint64_t val, int valbytes, bool zigzag) {
uint64_t fastdecode_munge(uint64_t val, int valbytes, bool zigzag) {
if (valbytes == 1) {
return val != 0;
} else if (zigzag) {
@@ -303,7 +302,7 @@ static uint64_t fastdecode_munge(uint64_t val, int valbytes, bool zigzag) {
}
UPB_FORCEINLINE
static const char* fastdecode_varint64(const char* ptr, uint64_t* val) {
const char* fastdecode_varint64(const char* ptr, uint64_t* val) {
ptr++;
*val = (uint8_t)ptr[-1];
if (UPB_UNLIKELY(*val & 0x80)) {
@@ -378,8 +377,8 @@ typedef struct {
} fastdecode_varintdata;
UPB_FORCEINLINE
static const char* fastdecode_topackedvarint(upb_EpsCopyInputStream* e,
const char* ptr, void* ctx) {
const char* fastdecode_topackedvarint(upb_EpsCopyInputStream* e,
const char* ptr, void* ctx) {
upb_Decoder* d = (upb_Decoder*)e;
fastdecode_varintdata* data = ctx;
void* dst = data->dst;
@@ -540,17 +539,18 @@ TAGBYTES(p)
int elems = size / valbytes; \
\
if (UPB_LIKELY(!arr)) { \
*arr_p = arr = _upb_Array_New(&d->arena, elems, elem_size_lg2); \
*arr_p = arr = \
UPB_PRIVATE(_upb_Array_New)(&d->arena, elems, elem_size_lg2); \
if (!arr) { \
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
} \
} else { \
_upb_Array_ResizeUninitialized(arr, elems, &d->arena); \
UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, elems, &d->arena); \
} \
\
char* dst = _upb_array_ptr(arr); \
char* dst = upb_Array_MutableDataPtr(arr); \
memcpy(dst, ptr, size); \
arr->size = elems; \
arr->UPB_PRIVATE(size) = elems; \
\
ptr += size; \
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
@@ -607,6 +607,7 @@ UPB_NOINLINE
static const char* fastdecode_verifyutf8(upb_Decoder* d, const char* ptr,
upb_Message* msg, intptr_t table,
uint64_t hasbits, uint64_t data) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_StringView* dst = (upb_StringView*)data;
if (!_upb_Decoder_VerifyUtf8Inline(dst->data, dst->size)) {
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_BadUtf8);
@@ -652,18 +653,20 @@ UPB_NOINLINE
static const char* fastdecode_longstring_noutf8(
struct upb_Decoder* d, const char* ptr, upb_Message* msg, intptr_t table,
uint64_t hasbits, uint64_t data) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_StringView* dst = (upb_StringView*)data;
FASTDECODE_LONGSTRING(d, ptr, msg, table, hasbits, dst, false);
}
UPB_FORCEINLINE
static void fastdecode_docopy(upb_Decoder* d, const char* ptr, uint32_t size,
int copy, char* data, upb_StringView* dst) {
d->arena.head.ptr += copy;
dst->data = data;
void fastdecode_docopy(upb_Decoder* d, const char* ptr, uint32_t size, int copy,
char* data, size_t data_offset, upb_StringView* dst) {
d->arena.UPB_PRIVATE(ptr) += copy;
dst->data = data + data_offset;
UPB_UNPOISON_MEMORY_REGION(data, copy);
memcpy(data, ptr, copy);
UPB_POISON_MEMORY_REGION(data + size, copy - size);
UPB_POISON_MEMORY_REGION(data + data_offset + size,
copy - data_offset - size);
}
#define FASTDECODE_COPYSTRING(d, ptr, msg, table, hasbits, data, tagbytes, \
@@ -690,25 +693,24 @@ static void fastdecode_docopy(upb_Decoder* d, const char* ptr, uint32_t size,
ptr += tagbytes + 1; \
dst->size = size; \
\
buf = d->arena.head.ptr; \
arena_has = _upb_ArenaHas(&d->arena); \
buf = d->arena.UPB_PRIVATE(ptr); \
arena_has = UPB_PRIVATE(_upb_ArenaHas)(&d->arena); \
common_has = UPB_MIN(arena_has, \
upb_EpsCopyInputStream_BytesAvailable(&d->input, ptr)); \
\
if (UPB_LIKELY(size <= 15 - tagbytes)) { \
if (arena_has < 16) goto longstr; \
d->arena.head.ptr += 16; \
memcpy(buf, ptr - tagbytes - 1, 16); \
dst->data = buf + tagbytes + 1; \
fastdecode_docopy(d, ptr - tagbytes - 1, size, 16, buf, tagbytes + 1, \
dst); \
} else if (UPB_LIKELY(size <= 32)) { \
if (UPB_UNLIKELY(common_has < 32)) goto longstr; \
fastdecode_docopy(d, ptr, size, 32, buf, dst); \
fastdecode_docopy(d, ptr, size, 32, buf, 0, dst); \
} else if (UPB_LIKELY(size <= 64)) { \
if (UPB_UNLIKELY(common_has < 64)) goto longstr; \
fastdecode_docopy(d, ptr, size, 64, buf, dst); \
fastdecode_docopy(d, ptr, size, 64, buf, 0, dst); \
} else if (UPB_LIKELY(size < 128)) { \
if (UPB_UNLIKELY(common_has < 128)) goto longstr; \
fastdecode_docopy(d, ptr, size, 128, buf, dst); \
fastdecode_docopy(d, ptr, size, 128, buf, 0, dst); \
} else { \
goto longstr; \
} \
@@ -864,15 +866,15 @@ TAGBYTES(r)
/* message fields *************************************************************/
UPB_INLINE
upb_Message* decode_newmsg_ceil(upb_Decoder* d, const upb_MiniTable* l,
upb_Message* decode_newmsg_ceil(upb_Decoder* d, const upb_MiniTable* m,
int msg_ceil_bytes) {
size_t size = l->size + sizeof(upb_Message_Internal);
size_t size = m->UPB_PRIVATE(size);
char* msg_data;
if (UPB_LIKELY(msg_ceil_bytes > 0 &&
_upb_ArenaHas(&d->arena) >= msg_ceil_bytes)) {
UPB_PRIVATE(_upb_ArenaHas)(&d->arena) >= msg_ceil_bytes)) {
UPB_ASSERT(size <= (size_t)msg_ceil_bytes);
msg_data = d->arena.head.ptr;
d->arena.head.ptr += size;
msg_data = d->arena.UPB_PRIVATE(ptr);
d->arena.UPB_PRIVATE(ptr) += size;
UPB_UNPOISON_MEMORY_REGION(msg_data, msg_ceil_bytes);
memset(msg_data, 0, msg_ceil_bytes);
UPB_POISON_MEMORY_REGION(msg_data + size, msg_ceil_bytes - size);
@@ -880,7 +882,7 @@ upb_Message* decode_newmsg_ceil(upb_Decoder* d, const upb_MiniTable* l,
msg_data = (char*)upb_Arena_Malloc(&d->arena, size);
memset(msg_data, 0, size);
}
return msg_data + sizeof(upb_Message_Internal);
return (upb_Message*)msg_data;
}
typedef struct {
@@ -889,8 +891,8 @@ typedef struct {
} fastdecode_submsgdata;
UPB_FORCEINLINE
static const char* fastdecode_tosubmsg(upb_EpsCopyInputStream* e,
const char* ptr, void* ctx) {
const char* fastdecode_tosubmsg(upb_EpsCopyInputStream* e, const char* ptr,
void* ctx) {
upb_Decoder* d = (upb_Decoder*)e;
fastdecode_submsgdata* submsg = ctx;
ptr = fastdecode_dispatch(d, ptr, submsg->msg, submsg->table, 0, 0);
@@ -912,11 +914,13 @@ static const char* fastdecode_tosubmsg(upb_EpsCopyInputStream* e,
upb_Message** dst; \
uint32_t submsg_idx = (data >> 16) & 0xff; \
const upb_MiniTable* tablep = decode_totablep(table); \
const upb_MiniTable* subtablep = tablep->subs[submsg_idx].submsg; \
const upb_MiniTable* subtablep = upb_MiniTableSub_Message( \
*UPB_PRIVATE(_upb_MiniTable_GetSubByIndex)(tablep, submsg_idx)); \
fastdecode_submsgdata submsg = {decode_totable(subtablep)}; \
fastdecode_arr farr; \
\
if (subtablep->table_mask == (uint8_t)-1) { \
if (subtablep->UPB_PRIVATE(table_mask) == (uint8_t)-1) { \
d->depth++; \
RETURN_GENERIC("submessage doesn't have fast tables."); \
} \
\
@@ -924,7 +928,7 @@ static const char* fastdecode_tosubmsg(upb_EpsCopyInputStream* e,
sizeof(upb_Message*), card); \
\
if (card == CARD_s) { \
*(uint32_t*)msg |= hasbits; \
((uint32_t*)msg)[2] |= hasbits; \
hasbits = 0; \
} \
\

View File

@@ -39,8 +39,8 @@
// - '1' for one-byte tags (field numbers 1-15)
// - '2' for two-byte tags (field numbers 16-2048)
#ifndef UPB_WIRE_DECODE_FAST_H_
#define UPB_WIRE_DECODE_FAST_H_
#ifndef UPB_WIRE_INTERNAL_DECODE_FAST_H_
#define UPB_WIRE_INTERNAL_DECODE_FAST_H_
#include "upb/message/message.h"
@@ -110,6 +110,7 @@ TAGBYTES(o)
TAGBYTES(r)
#undef F
#undef UTF8
#undef TAGBYTES
/* sub-message fields *********************************************************/
@@ -132,9 +133,9 @@ TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)
#undef TAGBYTES
#undef SIZES
#undef F
#undef SIZES
#undef TAGBYTES
#undef UPB_PARSE_PARAMS
@@ -144,4 +145,4 @@ TAGBYTES(r)
#include "upb/port/undef.inc"
#endif /* UPB_WIRE_DECODE_FAST_H_ */
#endif /* UPB_WIRE_INTERNAL_DECODE_FAST_H_ */

View File

@@ -10,8 +10,8 @@
* decode.c and decode_fast.c.
*/
#ifndef UPB_WIRE_INTERNAL_DECODE_H_
#define UPB_WIRE_INTERNAL_DECODE_H_
#ifndef UPB_WIRE_INTERNAL_DECODER_H_
#define UPB_WIRE_INTERNAL_DECODER_H_
#include "upb/mem/internal/arena.h"
#include "upb/message/internal/message.h"
@@ -33,7 +33,10 @@ typedef struct upb_Decoder {
uint32_t end_group; // field number of END_GROUP tag, else DECODE_NOGROUP.
uint16_t options;
bool missing_required;
upb_Arena arena;
union {
upb_Arena arena;
void* foo[UPB_ARENA_SIZE_HACK];
};
upb_DecodeStatus status;
jmp_buf err;
@@ -56,36 +59,17 @@ extern const uint8_t upb_utf8_offsets[];
UPB_INLINE
bool _upb_Decoder_VerifyUtf8Inline(const char* ptr, int len) {
const char* end = ptr + len;
// Check 8 bytes at a time for any non-ASCII char.
while (end - ptr >= 8) {
uint64_t data;
memcpy(&data, ptr, 8);
if (data & 0x8080808080808080) goto non_ascii;
ptr += 8;
}
// Check one byte at a time for non-ASCII.
while (ptr < end) {
if (*ptr & 0x80) goto non_ascii;
ptr++;
}
return true;
non_ascii:
return utf8_range2((const unsigned char*)ptr, end - ptr) == 0;
return utf8_range_IsValid(ptr, len);
}
const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
const upb_Message* msg,
const upb_MiniTable* l);
const upb_MiniTable* m);
/* x86-64 pointers always have the high 16 bits matching. So we can shift
* left 8 and right 8 without loss of information. */
UPB_INLINE intptr_t decode_totable(const upb_MiniTable* tablep) {
return ((intptr_t)tablep << 8) | tablep->table_mask;
return ((intptr_t)tablep << 8) | tablep->UPB_PRIVATE(table_mask);
}
UPB_INLINE const upb_MiniTable* decode_totablep(intptr_t table) {
@@ -106,8 +90,8 @@ UPB_INLINE const char* _upb_Decoder_BufferFlipCallback(
if (!old_end) _upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
if (d->unknown) {
if (!_upb_Message_AddUnknown(d->unknown_msg, d->unknown,
old_end - d->unknown, &d->arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(
d->unknown_msg, d->unknown, old_end - d->unknown, &d->arena)) {
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
d->unknown = new_start;
@@ -126,9 +110,9 @@ const char* _upb_FastDecoder_TagDispatch(upb_Decoder* d, const char* ptr,
size_t idx = tag & mask;
UPB_ASSUME((idx & 7) == 0);
idx >>= 3;
data = table_p->fasttable[idx].field_data ^ tag;
UPB_MUSTTAIL return table_p->fasttable[idx].field_parser(d, ptr, msg, table,
hasbits, data);
data = table_p->UPB_PRIVATE(fasttable)[idx].field_data ^ tag;
UPB_MUSTTAIL return table_p->UPB_PRIVATE(fasttable)[idx].field_parser(
d, ptr, msg, table, hasbits, data);
}
#endif
@@ -140,4 +124,4 @@ UPB_INLINE uint32_t _upb_FastDecoder_LoadTag(const char* ptr) {
#include "upb/port/undef.inc"
#endif /* UPB_WIRE_INTERNAL_DECODE_H_ */
#endif /* UPB_WIRE_INTERNAL_DECODER_H_ */

View File

@@ -0,0 +1,61 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_WIRE_INTERNAL_READER_H_
#define UPB_WIRE_INTERNAL_READER_H_
// Must be last.
#include "upb/port/def.inc"
#define kUpb_WireReader_WireTypeBits 3
#define kUpb_WireReader_WireTypeMask 7
typedef struct {
const char* ptr;
uint64_t val;
} UPB_PRIVATE(_upb_WireReader_LongVarint);
#ifdef __cplusplus
extern "C" {
#endif
UPB_PRIVATE(_upb_WireReader_LongVarint)
UPB_PRIVATE(_upb_WireReader_ReadLongVarint)(const char* ptr, uint64_t val);
UPB_FORCEINLINE const char* UPB_PRIVATE(_upb_WireReader_ReadVarint)(
const char* ptr, uint64_t* val, int maxlen, uint64_t maxval) {
uint64_t byte = (uint8_t)*ptr;
if (UPB_LIKELY((byte & 0x80) == 0)) {
*val = (uint32_t)byte;
return ptr + 1;
}
const char* start = ptr;
UPB_PRIVATE(_upb_WireReader_LongVarint)
res = UPB_PRIVATE(_upb_WireReader_ReadLongVarint)(ptr, byte);
if (!res.ptr || (maxlen < 10 && res.ptr - start > maxlen) ||
res.val > maxval) {
return NULL; // Malformed.
}
*val = res.val;
return res.ptr;
}
UPB_API_INLINE uint32_t upb_WireReader_GetFieldNumber(uint32_t tag) {
return tag >> kUpb_WireReader_WireTypeBits;
}
UPB_API_INLINE uint8_t upb_WireReader_GetWireType(uint32_t tag) {
return tag & kUpb_WireReader_WireTypeMask;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif // UPB_WIRE_INTERNAL_READER_H_

View File

@@ -7,18 +7,20 @@
#include "upb/wire/reader.h"
#include <stddef.h>
#include <stdint.h>
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/types.h"
// Must be last.
#include "upb/port/def.inc"
UPB_NOINLINE _upb_WireReader_ReadLongVarintRet
_upb_WireReader_ReadLongVarint(const char* ptr, uint64_t val) {
_upb_WireReader_ReadLongVarintRet ret = {NULL, 0};
UPB_NOINLINE UPB_PRIVATE(_upb_WireReader_LongVarint)
UPB_PRIVATE(_upb_WireReader_ReadLongVarint)(const char* ptr, uint64_t val) {
UPB_PRIVATE(_upb_WireReader_LongVarint) ret = {NULL, 0};
uint64_t byte;
int i;
for (i = 1; i < 10; i++) {
for (int i = 1; i < 10; i++) {
byte = (uint8_t)ptr[i];
val += (byte - 1) << (i * 7);
if (!(byte & 0x80)) {
@@ -30,9 +32,9 @@ _upb_WireReader_ReadLongVarint(const char* ptr, uint64_t val) {
return ret;
}
const char* _upb_WireReader_SkipGroup(const char* ptr, uint32_t tag,
int depth_limit,
upb_EpsCopyInputStream* stream) {
const char* UPB_PRIVATE(_upb_WireReader_SkipGroup)(
const char* ptr, uint32_t tag, int depth_limit,
upb_EpsCopyInputStream* stream) {
if (--depth_limit == 0) return NULL;
uint32_t end_group_tag = (tag & ~7ULL) | kUpb_WireType_EndGroup;
while (!upb_EpsCopyInputStream_IsDone(stream, &ptr)) {

View File

@@ -8,53 +8,23 @@
#ifndef UPB_WIRE_READER_H_
#define UPB_WIRE_READER_H_
#include "upb/base/internal/endian.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/internal/swap.h"
#include "upb/wire/internal/reader.h"
#include "upb/wire/types.h" // IWYU pragma: export
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// The upb_WireReader interface is suitable for general-purpose parsing of
// protobuf binary wire format. It is designed to be used along with
// protobuf binary wire format. It is designed to be used along with
// upb_EpsCopyInputStream for buffering, and all parsing routines in this file
// assume that at least kUpb_EpsCopyInputStream_SlopBytes worth of data is
// available to read without any bounds checks.
#define kUpb_WireReader_WireTypeMask 7
#define kUpb_WireReader_WireTypeBits 3
typedef struct {
const char* ptr;
uint64_t val;
} _upb_WireReader_ReadLongVarintRet;
_upb_WireReader_ReadLongVarintRet _upb_WireReader_ReadLongVarint(
const char* ptr, uint64_t val);
static UPB_FORCEINLINE const char* _upb_WireReader_ReadVarint(const char* ptr,
uint64_t* val,
int maxlen,
uint64_t maxval) {
uint64_t byte = (uint8_t)*ptr;
if (UPB_LIKELY((byte & 0x80) == 0)) {
*val = (uint32_t)byte;
return ptr + 1;
}
const char* start = ptr;
_upb_WireReader_ReadLongVarintRet res =
_upb_WireReader_ReadLongVarint(ptr, byte);
if (!res.ptr || (maxlen < 10 && res.ptr - start > maxlen) ||
res.val > maxval) {
return NULL; // Malformed.
}
*val = res.val;
return res.ptr;
}
#ifdef __cplusplus
extern "C" {
#endif
// Parses a tag into `tag`, and returns a pointer past the end of the tag, or
// NULL if there was an error in the tag data.
@@ -62,28 +32,24 @@ static UPB_FORCEINLINE const char* _upb_WireReader_ReadVarint(const char* ptr,
// REQUIRES: there must be at least 10 bytes of data available at `ptr`.
// Bounds checks must be performed before calling this function, preferably
// by calling upb_EpsCopyInputStream_IsDone().
static UPB_FORCEINLINE const char* upb_WireReader_ReadTag(const char* ptr,
uint32_t* tag) {
UPB_FORCEINLINE const char* upb_WireReader_ReadTag(const char* ptr,
uint32_t* tag) {
uint64_t val;
ptr = _upb_WireReader_ReadVarint(ptr, &val, 5, UINT32_MAX);
ptr = UPB_PRIVATE(_upb_WireReader_ReadVarint)(ptr, &val, 5, UINT32_MAX);
if (!ptr) return NULL;
*tag = val;
return ptr;
}
// Given a tag, returns the field number.
UPB_INLINE uint32_t upb_WireReader_GetFieldNumber(uint32_t tag) {
return tag >> kUpb_WireReader_WireTypeBits;
}
UPB_API_INLINE uint32_t upb_WireReader_GetFieldNumber(uint32_t tag);
// Given a tag, returns the wire type.
UPB_INLINE uint8_t upb_WireReader_GetWireType(uint32_t tag) {
return tag & kUpb_WireReader_WireTypeMask;
}
UPB_API_INLINE uint8_t upb_WireReader_GetWireType(uint32_t tag);
UPB_INLINE const char* upb_WireReader_ReadVarint(const char* ptr,
uint64_t* val) {
return _upb_WireReader_ReadVarint(ptr, val, 10, UINT64_MAX);
return UPB_PRIVATE(_upb_WireReader_ReadVarint)(ptr, val, 10, UINT64_MAX);
}
// Skips data for a varint, returning a pointer past the end of the varint, or
@@ -119,7 +85,7 @@ UPB_INLINE const char* upb_WireReader_ReadSize(const char* ptr, int* size) {
UPB_INLINE const char* upb_WireReader_ReadFixed32(const char* ptr, void* val) {
uint32_t uval;
memcpy(&uval, ptr, 4);
uval = _upb_BigEndian_Swap32(uval);
uval = upb_BigEndian32(uval);
memcpy(val, &uval, 4);
return ptr + 4;
}
@@ -132,14 +98,14 @@ UPB_INLINE const char* upb_WireReader_ReadFixed32(const char* ptr, void* val) {
UPB_INLINE const char* upb_WireReader_ReadFixed64(const char* ptr, void* val) {
uint64_t uval;
memcpy(&uval, ptr, 8);
uval = _upb_BigEndian_Swap64(uval);
uval = upb_BigEndian64(uval);
memcpy(val, &uval, 8);
return ptr + 8;
}
const char* _upb_WireReader_SkipGroup(const char* ptr, uint32_t tag,
int depth_limit,
upb_EpsCopyInputStream* stream);
const char* UPB_PRIVATE(_upb_WireReader_SkipGroup)(
const char* ptr, uint32_t tag, int depth_limit,
upb_EpsCopyInputStream* stream);
// Skips data for a group, returning a pointer past the end of the group, or
// NULL if there was an error parsing the group. The `tag` argument should be
@@ -152,7 +118,7 @@ const char* _upb_WireReader_SkipGroup(const char* ptr, uint32_t tag,
// control over this?
UPB_INLINE const char* upb_WireReader_SkipGroup(
const char* ptr, uint32_t tag, upb_EpsCopyInputStream* stream) {
return _upb_WireReader_SkipGroup(ptr, tag, 100, stream);
return UPB_PRIVATE(_upb_WireReader_SkipGroup)(ptr, tag, 100, stream);
}
UPB_INLINE const char* _upb_WireReader_SkipValue(
@@ -173,7 +139,8 @@ UPB_INLINE const char* _upb_WireReader_SkipValue(
return ptr;
}
case kUpb_WireType_StartGroup:
return _upb_WireReader_SkipGroup(ptr, tag, depth_limit, stream);
return UPB_PRIVATE(_upb_WireReader_SkipGroup)(ptr, tag, depth_limit,
stream);
case kUpb_WireType_EndGroup:
return NULL; // Should be handled before now.
default: