split apart mini_table.c into a new subdir

PiperOrigin-RevId: 484352293
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 6c4dc6f9e8
commit c033eff26f
  1. 31
      BUILD
  2. 173
      upb/mini_table.h
  3. 5
      upb/mini_table.hpp
  4. 11
      upb/mini_table/accessors.c
  5. 18
      upb/mini_table/accessors.h
  6. 8
      upb/mini_table/accessors_test.cc
  7. 79
      upb/mini_table/common.c
  8. 40
      upb/mini_table/common.h
  9. 123
      upb/mini_table/common_internal.h
  10. 406
      upb/mini_table/decode.c
  11. 97
      upb/mini_table/decode.h
  12. 301
      upb/mini_table/encode.c
  13. 109
      upb/mini_table/encode.h
  14. 11
      upb/mini_table/encode_test.cc
  15. 36
      upb/mini_table_accessors_internal.h
  16. 2
      upb/reflection/desc_state_internal.h
  17. 3
      upb/reflection/enum_def.c
  18. 3
      upb/reflection/field_def.c
  19. 2
      upb/reflection/message_def.c
  20. 1
      upb/reflection/oneof_def.c
  21. 2
      upbc/code_generator_request.c

31
BUILD

@ -174,9 +174,13 @@ cc_library(
cc_library(
name = "mini_table_internal",
hdrs = [
srcs = [
"upb/mini_table/common.h",
"upb/msg_internal.h",
],
hdrs = [
"upb/mini_table/common_internal.h",
],
visibility = ["//:__subpackages__"],
deps = [
":extension_registry",
@ -189,11 +193,18 @@ cc_library(
cc_library(
name = "mini_table",
srcs = [
"upb/mini_table.c",
"upb/mini_table/common.c",
"upb/mini_table/common.h",
"upb/mini_table/common_internal.h",
"upb/mini_table/decode.c",
"upb/mini_table/encode.c",
"upb/msg_internal.h",
],
hdrs = [
"upb/mini_table.h",
"upb/mini_table.hpp",
"upb/mini_table/decode.h",
"upb/mini_table/encode.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
@ -201,6 +212,7 @@ cc_library(
":extension_registry",
":mini_table_internal",
":port",
":table_internal",
":upb",
],
)
@ -208,11 +220,12 @@ cc_library(
cc_library(
name = "mini_table_accessors",
srcs = [
"upb/internal/mini_table_accessors.h",
"upb/mini_table_accessors.c",
"upb/mini_table/accessors.c",
"upb/mini_table/common.h",
"upb/msg_internal.h",
],
hdrs = [
"upb/mini_table_accessors.h",
"upb/mini_table/accessors.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
@ -222,15 +235,16 @@ cc_library(
":mini_table",
":mini_table_internal",
":port",
":table_internal",
":upb",
],
)
cc_test(
name = "mini_table_test",
name = "mini_table_encode_test",
srcs = [
"upb/internal/table.h",
"upb/mini_table_test.cc",
"upb/mini_table/encode_test.cc",
],
deps = [
":extension_registry",
@ -246,7 +260,7 @@ cc_test(
cc_test(
name = "mini_table_accessors_test",
srcs = ["upb/mini_table_accessors_test.cc"],
srcs = ["upb/mini_table/accessors_test.cc"],
deps = [
":collections",
":mini_table",
@ -920,6 +934,7 @@ cc_library(
hdrs = ["upb/fuzz_test_util.h"],
deps = [
":mini_table",
":mini_table_internal",
":upb",
],
)

@ -25,177 +25,12 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This header is deprecated, use the header(s) below instead.
#ifndef UPB_MINI_TABLE_H_
#define UPB_MINI_TABLE_H_
#include "upb/msg_internal.h"
// Must be last.
#include "upb/port_def.inc"
#ifdef __cplusplus
extern "C" {
#endif
const upb_MiniTable_Field* upb_MiniTable_FindFieldByNumber(
const upb_MiniTable* table, uint32_t number);
upb_FieldType upb_MiniTableField_Type(const upb_MiniTable_Field* field);
UPB_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable(
const upb_MiniTable* mini_table, const upb_MiniTable_Field* field) {
return mini_table->subs[field->submsg_index].submsg;
}
UPB_INLINE const upb_MiniTable_Enum* upb_MiniTable_GetSubEnumTable(
const upb_MiniTable* mini_table, const upb_MiniTable_Field* field) {
return mini_table->subs[field->submsg_index].subenum;
}
/** upb_MtDataEncoder *********************************************************/
// Functions to encode a string in a format that can be loaded by
// upb_MiniTable_Build().
typedef enum {
kUpb_MessageModifier_ValidateUtf8 = 1 << 0,
kUpb_MessageModifier_DefaultIsPacked = 1 << 1,
kUpb_MessageModifier_IsExtendable = 1 << 2,
} kUpb_MessageModifier;
typedef enum {
kUpb_FieldModifier_IsRepeated = 1 << 0,
kUpb_FieldModifier_IsPacked = 1 << 1,
kUpb_FieldModifier_IsClosedEnum = 1 << 2,
kUpb_FieldModifier_IsProto3Singular = 1 << 3,
kUpb_FieldModifier_IsRequired = 1 << 4,
} kUpb_FieldModifier;
typedef struct {
char* end; // Limit of the buffer passed as a parameter.
// Aliased to internal-only members in .cc.
char internal[32];
} upb_MtDataEncoder;
// If the input buffer has at least this many bytes available, the encoder call
// is guaranteed to succeed (as long as field number order is maintained).
#define kUpb_MtDataEncoder_MinSize 16
// Encodes field/oneof information for a given message. The sequence of calls
// should look like:
//
// upb_MtDataEncoder e;
// char buf[256];
// char* ptr = buf;
// e.end = ptr + sizeof(buf);
// unit64_t msg_mod = ...; // bitwise & of kUpb_MessageModifiers or zero
// ptr = upb_MtDataEncoder_StartMessage(&e, ptr, msg_mod);
// // Fields *must* be in field number order.
// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
//
// // If oneofs are present. Oneofs must be encoded after regular fields.
// ptr = upb_MiniTable_StartOneof(&e, ptr)
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
//
// ptr = upb_MiniTable_StartOneof(&e, ptr);
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
//
// Oneofs must be encoded after all regular fields.
char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr,
uint64_t msg_mod);
char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod);
char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr);
char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr,
uint32_t field_num);
// Encodes the set of values for a given enum. The values must be given in
// order (after casting to uint32_t), and repeats are not allowed.
char* upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e, char* ptr);
char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr,
uint32_t val);
char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr);
// Encodes an entire mini descriptor for an extension.
char* upb_MtDataEncoder_EncodeExtension(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod);
// Encodes an entire mini descriptor for a map.
char* upb_MtDataEncoder_EncodeMap(upb_MtDataEncoder* e, char* ptr,
upb_FieldType key_type,
upb_FieldType value_type, uint64_t value_mod);
// Encodes an entire mini descriptor for a message set.
char* upb_MtDataEncoder_EncodeMessageSet(upb_MtDataEncoder* e, char* ptr);
/** upb_MiniTable *************************************************************/
typedef enum {
kUpb_MiniTablePlatform_32Bit,
kUpb_MiniTablePlatform_64Bit,
kUpb_MiniTablePlatform_Native =
UPB_SIZE(kUpb_MiniTablePlatform_32Bit, kUpb_MiniTablePlatform_64Bit),
} upb_MiniTablePlatform;
// Builds a mini table from the data encoded in the buffer [data, len]. If any
// errors occur, returns NULL and sets a status message. In the success case,
// the caller must call upb_MiniTable_SetSub*() for all message or proto2 enum
// fields to link the table to the appropriate sub-tables.
upb_MiniTable* upb_MiniTable_Build(const char* data, size_t len,
upb_MiniTablePlatform platform,
upb_Arena* arena, upb_Status* status);
// Links a sub-message field to a MiniTable for that sub-message. If a
// sub-message field is not linked, it will be treated as an unknown field
// during parsing, and setting the field will not be allowed. It is possible
// to link the message field later, at which point it will no longer be treated
// as unknown. However there is no synchronization for this operation, which
// means parallel mutation requires external synchronization.
void upb_MiniTable_SetSubMessage(upb_MiniTable* table,
upb_MiniTable_Field* field,
const upb_MiniTable* sub);
// Links an enum field to a MiniTable for that enum. All enum fields must
// be linked prior to parsing.
void upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTable_Field* field,
const upb_MiniTable_Enum* sub);
const char* upb_MiniTable_BuildExtension(const char* data, size_t len,
upb_MiniTable_Extension* ext,
const upb_MiniTable* extendee,
upb_MiniTable_Sub sub,
upb_Status* status);
upb_MiniTable_Enum* upb_MiniTable_BuildEnum(const char* data, size_t len,
upb_Arena* arena,
upb_Status* status);
// Like upb_MiniTable_Build(), but the user provides a buffer of layout data so
// it can be reused from call to call, avoiding repeated realloc()/free().
//
// The caller owns `*buf` both before and after the call, and must free() it
// when it is no longer in use. The function will realloc() `*buf` as
// necessary, updating `*size` accordingly.
upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len,
upb_MiniTablePlatform platform,
upb_Arena* arena, void** buf,
size_t* buf_size, upb_Status* status);
// For testing only.
char upb_ToBase92(int8_t ch);
char upb_FromBase92(uint8_t ch);
bool upb_IsTypePackable(upb_FieldType type);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/encode.h"
#endif /* UPB_MINI_TABLE_H_ */

@ -28,11 +28,10 @@
#ifndef UPB_MINI_TABLE_HPP_
#define UPB_MINI_TABLE_HPP_
#include <assert.h>
#include <string>
#include "upb/mini_table.h"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/encode.h"
namespace upb {

@ -25,18 +25,17 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "upb/mini_table_accessors.h"
#include "upb/mini_table/accessors.h"
#include "upb/decode.h"
#include "upb/encode.h"
#include "upb/internal/array.h"
#include "upb/mini_table.h"
// Must be last.
#include "upb/msg.h"
#include "upb/port_def.inc"
size_t upb_MiniTable_Field_GetSize(const upb_MiniTable_Field* f) {
static size_t _upb_MiniTable_Field_GetSize(const upb_MiniTable_Field* f) {
static unsigned char sizes[] = {
0, /* 0 */
8, /* kUpb_FieldType_Double */
@ -62,7 +61,7 @@ size_t upb_MiniTable_Field_GetSize(const upb_MiniTable_Field* f) {
}
// Maps descriptor type to elem_size_lg2.
int upb_MiniTable_Field_CTypeLg2Size(const upb_MiniTable_Field* f) {
static int _upb_MiniTable_Field_CTypeLg2Size(const upb_MiniTable_Field* f) {
static const uint8_t sizes[] = {
-1, /* invalid descriptor type */
3, /* DOUBLE */
@ -110,14 +109,14 @@ void upb_MiniTable_ClearField(upb_Message* msg,
if (*oneof_case != field->number) return;
*oneof_case = 0;
}
memset(mem, 0, upb_MiniTable_Field_GetSize(field));
memset(mem, 0, _upb_MiniTable_Field_GetSize(field));
}
void* upb_MiniTable_ResizeArray(upb_Message* msg,
const upb_MiniTable_Field* field, size_t len,
upb_Arena* arena) {
return _upb_Array_Resize_accessor2(
msg, field->offset, len, upb_MiniTable_Field_CTypeLg2Size(field), arena);
msg, field->offset, len, _upb_MiniTable_Field_CTypeLg2Size(field), arena);
}
typedef struct {

@ -29,8 +29,7 @@
#define UPB_MINI_TABLE_ACCESSORS_H_
#include "upb/array.h"
#include "upb/internal/mini_table_accessors.h"
#include "upb/mini_table.h"
#include "upb/mini_table/common.h"
// Must be last.
#include "upb/port_def.inc"
@ -39,6 +38,21 @@
extern "C" {
#endif
UPB_INLINE bool _upb_MiniTable_Field_InOneOf(const upb_MiniTable_Field* field) {
return field->presence < 0;
}
UPB_INLINE void _upb_MiniTable_SetPresence(upb_Message* msg,
const upb_MiniTable_Field* field) {
if (field->presence > 0) {
_upb_sethas_field(msg, field);
} else if (_upb_MiniTable_Field_InOneOf(field)) {
*_upb_oneofcase_field(msg, field) = field->number;
}
}
// EVERYTHING ABOVE THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
bool upb_MiniTable_HasField(const upb_Message* msg,
const upb_MiniTable_Field* field);

@ -31,15 +31,15 @@
* accessed through reflective APIs exposed through mini table accessors.
*/
#include "upb/mini_table_accessors.h"
#include "upb/mini_table/accessors.h"
#include "gtest/gtest.h"
#include "google/protobuf/test_messages_proto2.upb.h"
#include "google/protobuf/test_messages_proto3.upb.h"
#include "upb/array.h"
#include "upb/decode.h"
#include "upb/mini_table.h"
#include "upb/msg_internal.h"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/encode.h"
#include "upb/test.upb.h"
#include "upb/upb.h"
@ -522,7 +522,7 @@ TEST(GeneratedCode, PromoteUnknownMessage) {
&serialized_size);
upb_MiniTable* mini_table = CreateMiniTableWithEmptySubTables(arena);
upb_Message* msg = upb_Message_New(mini_table, arena);
upb_Message* msg = _upb_Message_New(mini_table, arena);
upb_DecodeStatus decode_status = upb_Decode(serialized, serialized_size, msg,
mini_table, nullptr, 0, arena);
EXPECT_EQ(decode_status, kUpb_DecodeStatus_Ok);

@ -0,0 +1,79 @@
/*
* Copyright (c) 2009-2021, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google 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 Google LLC 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.
*/
#include "upb/mini_table/common.h"
#include <inttypes.h>
#include "upb/arena.h"
#include "upb/mini_table/common_internal.h"
#include "upb/upb.h"
// Must be last.
#include "upb/port_def.inc"
const char _kUpb_ToBase92[] = {
' ', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=',
'>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', '[', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '{', '|', '}', '~',
};
const int8_t _kUpb_FromBase92[] = {
0, 1, -1, 2, 3, 4, 5, -1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, -1, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
};
const upb_MiniTable_Field* upb_MiniTable_FindFieldByNumber(
const upb_MiniTable* table, uint32_t number) {
int n = table->field_count;
for (int i = 0; i < n; i++) {
if (table->fields[i].number == number) {
return &table->fields[i];
}
}
return NULL;
}
upb_FieldType upb_MiniTableField_Type(const upb_MiniTable_Field* field) {
if (field->mode & kUpb_LabelFlags_IsAlternate) {
if (field->descriptortype == kUpb_FieldType_Int32) {
return kUpb_FieldType_Enum;
} else if (field->descriptortype == kUpb_FieldType_Bytes) {
return kUpb_FieldType_String;
} else {
UPB_ASSERT(false);
}
}
return field->descriptortype;
}

@ -25,29 +25,45 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UPB_INTERNAL_MINI_TABLE_ACCESSORS_H_
#define UPB_INTERNAL_MINI_TABLE_ACCESSORS_H_
#ifndef UPB_MINI_TABLE_COMMON_H_
#define UPB_MINI_TABLE_COMMON_H_
#include "upb/msg_internal.h"
// Must be last.
#include "upb/port_def.inc"
typedef enum {
kUpb_FieldModifier_IsRepeated = 1 << 0,
kUpb_FieldModifier_IsPacked = 1 << 1,
kUpb_FieldModifier_IsClosedEnum = 1 << 2,
kUpb_FieldModifier_IsProto3Singular = 1 << 3,
kUpb_FieldModifier_IsRequired = 1 << 4,
} kUpb_FieldModifier;
typedef enum {
kUpb_MessageModifier_ValidateUtf8 = 1 << 0,
kUpb_MessageModifier_DefaultIsPacked = 1 << 1,
kUpb_MessageModifier_IsExtendable = 1 << 2,
} kUpb_MessageModifier;
#ifdef __cplusplus
extern "C" {
#endif
UPB_INLINE bool _upb_MiniTable_Field_InOneOf(const upb_MiniTable_Field* field) {
return field->presence < 0;
const upb_MiniTable_Field* upb_MiniTable_FindFieldByNumber(
const upb_MiniTable* table, uint32_t number);
upb_FieldType upb_MiniTableField_Type(const upb_MiniTable_Field* field);
UPB_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable(
const upb_MiniTable* mini_table, const upb_MiniTable_Field* field) {
return mini_table->subs[field->submsg_index].submsg;
}
UPB_INLINE void _upb_MiniTable_SetPresence(upb_Message* msg,
const upb_MiniTable_Field* field) {
if (field->presence > 0) {
_upb_sethas_field(msg, field);
} else if (_upb_MiniTable_Field_InOneOf(field)) {
*_upb_oneofcase_field(msg, field) = field->number;
}
UPB_INLINE const upb_MiniTable_Enum* upb_MiniTable_GetSubEnumTable(
const upb_MiniTable* mini_table, const upb_MiniTable_Field* field) {
return mini_table->subs[field->submsg_index].subenum;
}
#ifdef __cplusplus
@ -56,4 +72,4 @@ UPB_INLINE void _upb_MiniTable_SetPresence(upb_Message* msg,
#include "upb/port_undef.inc"
#endif // UPB_INTERNAL_MINI_TABLE_ACCESSORS_H_
#endif /* UPB_MINI_TABLE_COMMON_H_ */

@ -0,0 +1,123 @@
/*
* Copyright (c) 2009-2022, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google 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 Google LLC 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.
*/
#ifndef UPB_MINI_TABLE_COMMON_INTERNAL_H_
#define UPB_MINI_TABLE_COMMON_INTERNAL_H_
#include "upb/upb.h"
// Must be last.
#include "upb/port_def.inc"
typedef enum {
kUpb_EncodedType_Double = 0,
kUpb_EncodedType_Float = 1,
kUpb_EncodedType_Fixed32 = 2,
kUpb_EncodedType_Fixed64 = 3,
kUpb_EncodedType_SFixed32 = 4,
kUpb_EncodedType_SFixed64 = 5,
kUpb_EncodedType_Int32 = 6,
kUpb_EncodedType_UInt32 = 7,
kUpb_EncodedType_SInt32 = 8,
kUpb_EncodedType_Int64 = 9,
kUpb_EncodedType_UInt64 = 10,
kUpb_EncodedType_SInt64 = 11,
kUpb_EncodedType_OpenEnum = 12,
kUpb_EncodedType_Bool = 13,
kUpb_EncodedType_Bytes = 14,
kUpb_EncodedType_String = 15,
kUpb_EncodedType_Group = 16,
kUpb_EncodedType_Message = 17,
kUpb_EncodedType_ClosedEnum = 18,
kUpb_EncodedType_RepeatedBase = 20,
} upb_EncodedType;
typedef enum {
kUpb_EncodedFieldModifier_FlipPacked = 1 << 0,
// upb only.
kUpb_EncodedFieldModifier_IsProto3Singular = 1 << 2,
kUpb_EncodedFieldModifier_IsRequired = 1 << 3,
} upb_EncodedFieldModifier;
enum {
kUpb_EncodedValue_MinField = ' ',
kUpb_EncodedValue_MaxField = 'I',
kUpb_EncodedValue_MinModifier = 'L',
kUpb_EncodedValue_MaxModifier = '[',
kUpb_EncodedValue_End = '^',
kUpb_EncodedValue_MinSkip = '_',
kUpb_EncodedValue_MaxSkip = '~',
kUpb_EncodedValue_OneofSeparator = '~',
kUpb_EncodedValue_FieldSeparator = '|',
kUpb_EncodedValue_MinOneofField = ' ',
kUpb_EncodedValue_MaxOneofField = 'b',
kUpb_EncodedValue_MaxEnumMask = 'A',
};
enum {
kUpb_EncodedVersion_EnumV1 = '!',
kUpb_EncodedVersion_ExtensionV1 = '#',
kUpb_EncodedVersion_MapV1 = '%',
kUpb_EncodedVersion_MessageV1 = '$',
kUpb_EncodedVersion_MessageSetV1 = '&',
};
#ifdef __cplusplus
extern "C" {
#endif
UPB_INLINE char _upb_ToBase92(int8_t ch) {
extern const char _kUpb_ToBase92[];
UPB_ASSERT(0 <= ch && ch < 92);
return _kUpb_ToBase92[ch];
}
UPB_INLINE char _upb_FromBase92(uint8_t ch) {
extern const int8_t _kUpb_FromBase92[];
if (' ' > ch || ch > '~') return -1;
return _kUpb_FromBase92[ch - ' '];
}
UPB_INLINE bool _upb_FieldType_IsPackable(upb_FieldType type) {
// clang-format off
const unsigned kUnpackableTypes =
(1 << kUpb_FieldType_String) |
(1 << kUpb_FieldType_Bytes) |
(1 << kUpb_FieldType_Message) |
(1 << kUpb_FieldType_Group);
// clang-format on
return (1 << type) & ~kUnpackableTypes;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#endif /* UPB_MINI_TABLE_COMMON_INTERNAL_H_ */

@ -25,402 +25,18 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "upb/mini_table.h"
#include "upb/mini_table/decode.h"
#include <inttypes.h>
#include "upb/arena.h"
#include "upb/msg_internal.h"
#include "upb/mini_table/common.h"
#include "upb/mini_table/common_internal.h"
#include "upb/upb.h"
// Must be last.
#include "upb/port_def.inc"
typedef enum {
kUpb_EncodedType_Double = 0,
kUpb_EncodedType_Float = 1,
kUpb_EncodedType_Fixed32 = 2,
kUpb_EncodedType_Fixed64 = 3,
kUpb_EncodedType_SFixed32 = 4,
kUpb_EncodedType_SFixed64 = 5,
kUpb_EncodedType_Int32 = 6,
kUpb_EncodedType_UInt32 = 7,
kUpb_EncodedType_SInt32 = 8,
kUpb_EncodedType_Int64 = 9,
kUpb_EncodedType_UInt64 = 10,
kUpb_EncodedType_SInt64 = 11,
kUpb_EncodedType_OpenEnum = 12,
kUpb_EncodedType_Bool = 13,
kUpb_EncodedType_Bytes = 14,
kUpb_EncodedType_String = 15,
kUpb_EncodedType_Group = 16,
kUpb_EncodedType_Message = 17,
kUpb_EncodedType_ClosedEnum = 18,
kUpb_EncodedType_RepeatedBase = 20,
} upb_EncodedType;
typedef enum {
kUpb_EncodedFieldModifier_FlipPacked = 1 << 0,
// upb only.
kUpb_EncodedFieldModifier_IsProto3Singular = 1 << 2,
kUpb_EncodedFieldModifier_IsRequired = 1 << 3,
} upb_EncodedFieldModifier;
enum {
kUpb_EncodedValue_MinField = ' ',
kUpb_EncodedValue_MaxField = 'I',
kUpb_EncodedValue_MinModifier = 'L',
kUpb_EncodedValue_MaxModifier = '[',
kUpb_EncodedValue_End = '^',
kUpb_EncodedValue_MinSkip = '_',
kUpb_EncodedValue_MaxSkip = '~',
kUpb_EncodedValue_OneofSeparator = '~',
kUpb_EncodedValue_FieldSeparator = '|',
kUpb_EncodedValue_MinOneofField = ' ',
kUpb_EncodedValue_MaxOneofField = 'b',
kUpb_EncodedValue_MaxEnumMask = 'A',
};
enum {
kUpb_EncodedVersion_EnumV1 = '!',
kUpb_EncodedVersion_ExtensionV1 = '#',
kUpb_EncodedVersion_MapV1 = '%',
kUpb_EncodedVersion_MessageV1 = '$',
kUpb_EncodedVersion_MessageSetV1 = '&',
};
char upb_ToBase92(int8_t ch) {
static const char kUpb_ToBase92[] = {
' ', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=',
'>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', '[', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '{', '|', '}', '~',
};
UPB_ASSERT(0 <= ch && ch < 92);
return kUpb_ToBase92[ch];
}
char upb_FromBase92(uint8_t ch) {
static const int8_t kUpb_FromBase92[] = {
0, 1, -1, 2, 3, 4, 5, -1, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
};
if (' ' > ch || ch > '~') return -1;
return kUpb_FromBase92[ch - ' '];
}
bool upb_IsTypePackable(upb_FieldType type) {
// clang-format off
static const unsigned kUnpackableTypes =
(1 << kUpb_FieldType_String) |
(1 << kUpb_FieldType_Bytes) |
(1 << kUpb_FieldType_Message) |
(1 << kUpb_FieldType_Group);
// clang-format on
return (1 << type) & ~kUnpackableTypes;
}
/** upb_MtDataEncoder *********************************************************/
typedef struct {
uint64_t present_values_mask;
uint32_t last_written_value;
} upb_MtDataEncoderInternal_EnumState;
typedef struct {
uint64_t msg_modifiers;
uint32_t last_field_num;
enum {
kUpb_OneofState_NotStarted,
kUpb_OneofState_StartedOneof,
kUpb_OneofState_EmittedOneofField,
} oneof_state;
} upb_MtDataEncoderInternal_MsgState;
typedef struct {
char* buf_start; // Only for checking kUpb_MtDataEncoder_MinSize.
union {
upb_MtDataEncoderInternal_EnumState enum_state;
upb_MtDataEncoderInternal_MsgState msg_state;
} state;
} upb_MtDataEncoderInternal;
static upb_MtDataEncoderInternal* upb_MtDataEncoder_GetInternal(
upb_MtDataEncoder* e, char* buf_start) {
UPB_ASSERT(sizeof(upb_MtDataEncoderInternal) <= sizeof(e->internal));
upb_MtDataEncoderInternal* ret = (upb_MtDataEncoderInternal*)e->internal;
ret->buf_start = buf_start;
return ret;
}
static char* upb_MtDataEncoder_PutRaw(upb_MtDataEncoder* e, char* ptr,
char ch) {
upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal;
UPB_ASSERT(ptr - in->buf_start < kUpb_MtDataEncoder_MinSize);
if (ptr == e->end) return NULL;
*ptr++ = ch;
return ptr;
}
static char* upb_MtDataEncoder_Put(upb_MtDataEncoder* e, char* ptr, char ch) {
return upb_MtDataEncoder_PutRaw(e, ptr, upb_ToBase92(ch));
}
static char* upb_MtDataEncoder_PutBase92Varint(upb_MtDataEncoder* e, char* ptr,
uint32_t val, int min, int max) {
int shift = _upb_Log2Ceiling(upb_FromBase92(max) - upb_FromBase92(min) + 1);
UPB_ASSERT(shift <= 6);
uint32_t mask = (1 << shift) - 1;
do {
uint32_t bits = val & mask;
ptr = upb_MtDataEncoder_Put(e, ptr, bits + upb_FromBase92(min));
if (!ptr) return NULL;
val >>= shift;
} while (val);
return ptr;
}
char* upb_MtDataEncoder_PutModifier(upb_MtDataEncoder* e, char* ptr,
uint64_t mod) {
if (mod) {
ptr = upb_MtDataEncoder_PutBase92Varint(e, ptr, mod,
kUpb_EncodedValue_MinModifier,
kUpb_EncodedValue_MaxModifier);
}
return ptr;
}
char* upb_MtDataEncoder_EncodeExtension(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.msg_state.msg_modifiers = 0;
in->state.msg_state.last_field_num = 0;
in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted;
ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_ExtensionV1);
if (!ptr) return NULL;
return upb_MtDataEncoder_PutField(e, ptr, type, field_num, field_mod);
}
char* upb_MtDataEncoder_EncodeMap(upb_MtDataEncoder* e, char* ptr,
upb_FieldType key_type,
upb_FieldType value_type,
uint64_t value_mod) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.msg_state.msg_modifiers = 0;
in->state.msg_state.last_field_num = 0;
in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted;
ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MapV1);
if (!ptr) return NULL;
ptr = upb_MtDataEncoder_PutField(e, ptr, key_type, 1, 0);
if (!ptr) return NULL;
return upb_MtDataEncoder_PutField(e, ptr, value_type, 2, value_mod);
}
char* upb_MtDataEncoder_EncodeMessageSet(upb_MtDataEncoder* e, char* ptr) {
(void)upb_MtDataEncoder_GetInternal(e, ptr);
return upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MessageSetV1);
}
char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr,
uint64_t msg_mod) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.msg_state.msg_modifiers = msg_mod;
in->state.msg_state.last_field_num = 0;
in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted;
ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MessageV1);
if (!ptr) return NULL;
return upb_MtDataEncoder_PutModifier(e, ptr, msg_mod);
}
char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod) {
static const char kUpb_TypeToEncoded[] = {
[kUpb_FieldType_Double] = kUpb_EncodedType_Double,
[kUpb_FieldType_Float] = kUpb_EncodedType_Float,
[kUpb_FieldType_Int64] = kUpb_EncodedType_Int64,
[kUpb_FieldType_UInt64] = kUpb_EncodedType_UInt64,
[kUpb_FieldType_Int32] = kUpb_EncodedType_Int32,
[kUpb_FieldType_Fixed64] = kUpb_EncodedType_Fixed64,
[kUpb_FieldType_Fixed32] = kUpb_EncodedType_Fixed32,
[kUpb_FieldType_Bool] = kUpb_EncodedType_Bool,
[kUpb_FieldType_String] = kUpb_EncodedType_String,
[kUpb_FieldType_Group] = kUpb_EncodedType_Group,
[kUpb_FieldType_Message] = kUpb_EncodedType_Message,
[kUpb_FieldType_Bytes] = kUpb_EncodedType_Bytes,
[kUpb_FieldType_UInt32] = kUpb_EncodedType_UInt32,
[kUpb_FieldType_Enum] = kUpb_EncodedType_OpenEnum,
[kUpb_FieldType_SFixed32] = kUpb_EncodedType_SFixed32,
[kUpb_FieldType_SFixed64] = kUpb_EncodedType_SFixed64,
[kUpb_FieldType_SInt32] = kUpb_EncodedType_SInt32,
[kUpb_FieldType_SInt64] = kUpb_EncodedType_SInt64,
};
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (field_num <= in->state.msg_state.last_field_num) return NULL;
if (in->state.msg_state.last_field_num + 1 != field_num) {
// Put skip.
UPB_ASSERT(field_num > in->state.msg_state.last_field_num);
uint32_t skip = field_num - in->state.msg_state.last_field_num;
ptr = upb_MtDataEncoder_PutBase92Varint(
e, ptr, skip, kUpb_EncodedValue_MinSkip, kUpb_EncodedValue_MaxSkip);
if (!ptr) return NULL;
}
in->state.msg_state.last_field_num = field_num;
uint32_t encoded_modifiers = 0;
// Put field type.
int encoded_type = kUpb_TypeToEncoded[type];
if (field_mod & kUpb_FieldModifier_IsClosedEnum) {
UPB_ASSERT(type == kUpb_FieldType_Enum);
encoded_type = kUpb_EncodedType_ClosedEnum;
}
if (field_mod & kUpb_FieldModifier_IsRepeated) {
// Repeated fields shift the type number up (unlike other modifiers which
// are bit flags).
encoded_type += kUpb_EncodedType_RepeatedBase;
if (upb_IsTypePackable(type)) {
bool field_is_packed = field_mod & kUpb_FieldModifier_IsPacked;
bool default_is_packed = in->state.msg_state.msg_modifiers &
kUpb_MessageModifier_DefaultIsPacked;
if (field_is_packed != default_is_packed) {
encoded_modifiers |= kUpb_EncodedFieldModifier_FlipPacked;
}
}
}
ptr = upb_MtDataEncoder_Put(e, ptr, encoded_type);
if (!ptr) return NULL;
if (field_mod & kUpb_FieldModifier_IsProto3Singular) {
encoded_modifiers |= kUpb_EncodedFieldModifier_IsProto3Singular;
}
if (field_mod & kUpb_FieldModifier_IsRequired) {
encoded_modifiers |= kUpb_EncodedFieldModifier_IsRequired;
}
return upb_MtDataEncoder_PutModifier(e, ptr, encoded_modifiers);
}
char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (in->state.msg_state.oneof_state == kUpb_OneofState_NotStarted) {
ptr = upb_MtDataEncoder_Put(e, ptr, upb_FromBase92(kUpb_EncodedValue_End));
} else {
ptr = upb_MtDataEncoder_Put(
e, ptr, upb_FromBase92(kUpb_EncodedValue_OneofSeparator));
}
in->state.msg_state.oneof_state = kUpb_OneofState_StartedOneof;
return ptr;
}
char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr,
uint32_t field_num) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (in->state.msg_state.oneof_state == kUpb_OneofState_EmittedOneofField) {
ptr = upb_MtDataEncoder_Put(
e, ptr, upb_FromBase92(kUpb_EncodedValue_FieldSeparator));
if (!ptr) return NULL;
}
ptr = upb_MtDataEncoder_PutBase92Varint(e, ptr, field_num, upb_ToBase92(0),
upb_ToBase92(63));
in->state.msg_state.oneof_state = kUpb_OneofState_EmittedOneofField;
return ptr;
}
char* upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e, char* ptr) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.enum_state.present_values_mask = 0;
in->state.enum_state.last_written_value = 0;
return upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_EnumV1);
}
static char* upb_MtDataEncoder_FlushDenseEnumMask(upb_MtDataEncoder* e,
char* ptr) {
upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal;
ptr = upb_MtDataEncoder_Put(e, ptr, in->state.enum_state.present_values_mask);
in->state.enum_state.present_values_mask = 0;
in->state.enum_state.last_written_value += 5;
return ptr;
}
char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr,
uint32_t val) {
// TODO(b/229641772): optimize this encoding.
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
UPB_ASSERT(val >= in->state.enum_state.last_written_value);
uint32_t delta = val - in->state.enum_state.last_written_value;
if (delta >= 5 && in->state.enum_state.present_values_mask) {
ptr = upb_MtDataEncoder_FlushDenseEnumMask(e, ptr);
if (!ptr) {
return NULL;
}
delta -= 5;
}
if (delta >= 5) {
ptr = upb_MtDataEncoder_PutBase92Varint(
e, ptr, delta, kUpb_EncodedValue_MinSkip, kUpb_EncodedValue_MaxSkip);
in->state.enum_state.last_written_value += delta;
delta = 0;
}
UPB_ASSERT((in->state.enum_state.present_values_mask >> delta) == 0);
in->state.enum_state.present_values_mask |= 1ULL << delta;
return ptr;
}
char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (!in->state.enum_state.present_values_mask) return ptr;
return upb_MtDataEncoder_FlushDenseEnumMask(e, ptr);
}
const upb_MiniTable_Field* upb_MiniTable_FindFieldByNumber(
const upb_MiniTable* table, uint32_t number) {
int n = table->field_count;
for (int i = 0; i < n; i++) {
if (table->fields[i].number == number) {
return &table->fields[i];
}
}
return NULL;
}
upb_FieldType upb_MiniTableField_Type(const upb_MiniTable_Field* field) {
if (field->mode & kUpb_LabelFlags_IsAlternate) {
if (field->descriptortype == kUpb_FieldType_Int32) {
return kUpb_FieldType_Enum;
} else if (field->descriptortype == kUpb_FieldType_Bytes) {
return kUpb_FieldType_String;
} else {
UPB_ASSERT(false);
}
}
return field->descriptortype;
}
/** Data decoder **************************************************************/
// Note: we sort by this number when calculating layout order.
typedef enum {
kUpb_LayoutItemType_OneofCase, // Oneof case.
@ -499,10 +115,10 @@ static const char* upb_MiniTable_DecodeBase92Varint(upb_MtDecoder* d,
uint32_t val = 0;
uint32_t shift = 0;
const int bits_per_char =
_upb_Log2Ceiling(upb_FromBase92(max) - upb_FromBase92(min));
_upb_Log2Ceiling(_upb_FromBase92(max) - _upb_FromBase92(min));
char ch = first_ch;
while (1) {
uint32_t bits = upb_FromBase92(ch) - upb_FromBase92(min);
uint32_t bits = _upb_FromBase92(ch) - _upb_FromBase92(min);
val |= bits << shift;
if (ptr == d->end || *ptr < min || max < *ptr) {
*out_val = val;
@ -534,7 +150,7 @@ static bool upb_MiniTable_HasSub(upb_MiniTable_Field* field,
static bool upb_MtDecoder_FieldIsPackable(upb_MiniTable_Field* field) {
return (field->mode & kUpb_FieldMode_Array) &&
upb_IsTypePackable(field->descriptortype);
_upb_FieldType_IsPackable(field->descriptortype);
}
static void upb_MiniTable_SetTypeAndSub(upb_MiniTable_Field* field,
@ -611,8 +227,8 @@ static void upb_MiniTable_SetField(upb_MtDecoder* d, uint8_t ch,
? kUpb_FieldRep_4Byte
: kUpb_FieldRep_8Byte;
int8_t type = upb_FromBase92(ch);
if (ch >= upb_ToBase92(kUpb_EncodedType_RepeatedBase)) {
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;
@ -1100,8 +716,8 @@ static void upb_MtDecoder_ParseMap(upb_MtDecoder* d, const char* data,
upb_MtDecoder_ErrorFormat(d, "Invalid map encode length: %zu", len);
UPB_UNREACHABLE();
}
const upb_EncodedType e0 = upb_FromBase92(data[0]);
const upb_EncodedType e1 = upb_FromBase92(data[1]);
const upb_EncodedType e0 = _upb_FromBase92(data[0]);
const upb_EncodedType e1 = _upb_FromBase92(data[1]);
switch (e0) {
case kUpb_EncodedType_Fixed32:
case kUpb_EncodedType_Fixed64:
@ -1286,7 +902,7 @@ upb_MiniTable_Enum* upb_MiniTable_BuildEnum(const char* data, size_t len,
while (ptr < decoder.end) {
char ch = *ptr++;
if (ch <= kUpb_EncodedValue_MaxEnumMask) {
uint32_t mask = upb_FromBase92(ch);
uint32_t mask = _upb_FromBase92(ch);
for (int i = 0; i < 5; i++, base++, mask >>= 1) {
if (mask & 1) upb_MiniTable_BuildEnumValue(&decoder, base);
}

@ -0,0 +1,97 @@
/*
* Copyright (c) 2009-2022, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google 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 Google LLC 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.
*/
#ifndef UPB_MINI_TABLE_DECODE_H_
#define UPB_MINI_TABLE_DECODE_H_
#include "upb/mini_table/common.h"
// Must be last.
#include "upb/port_def.inc"
typedef enum {
kUpb_MiniTablePlatform_32Bit,
kUpb_MiniTablePlatform_64Bit,
kUpb_MiniTablePlatform_Native =
UPB_SIZE(kUpb_MiniTablePlatform_32Bit, kUpb_MiniTablePlatform_64Bit),
} upb_MiniTablePlatform;
#ifdef __cplusplus
extern "C" {
#endif
// Builds a mini table from the data encoded in the buffer [data, len]. If any
// errors occur, returns NULL and sets a status message. In the success case,
// the caller must call upb_MiniTable_SetSub*() for all message or proto2 enum
// fields to link the table to the appropriate sub-tables.
upb_MiniTable* upb_MiniTable_Build(const char* data, size_t len,
upb_MiniTablePlatform platform,
upb_Arena* arena, upb_Status* status);
// Links a sub-message field to a MiniTable for that sub-message. If a
// sub-message field is not linked, it will be treated as an unknown field
// during parsing, and setting the field will not be allowed. It is possible
// to link the message field later, at which point it will no longer be treated
// as unknown. However there is no synchronization for this operation, which
// means parallel mutation requires external synchronization.
void upb_MiniTable_SetSubMessage(upb_MiniTable* table,
upb_MiniTable_Field* field,
const upb_MiniTable* sub);
// Links an enum field to a MiniTable for that enum. All enum fields must
// be linked prior to parsing.
void upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTable_Field* field,
const upb_MiniTable_Enum* sub);
const char* upb_MiniTable_BuildExtension(const char* data, size_t len,
upb_MiniTable_Extension* ext,
const upb_MiniTable* extendee,
upb_MiniTable_Sub sub,
upb_Status* status);
upb_MiniTable_Enum* upb_MiniTable_BuildEnum(const char* data, size_t len,
upb_Arena* arena,
upb_Status* status);
// Like upb_MiniTable_Build(), but the user provides a buffer of layout data so
// it can be reused from call to call, avoiding repeated realloc()/free().
//
// The caller owns `*buf` both before and after the call, and must free() it
// when it is no longer in use. The function will realloc() `*buf` as
// necessary, updating `*size` accordingly.
upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len,
upb_MiniTablePlatform platform,
upb_Arena* arena, void** buf,
size_t* buf_size, upb_Status* status);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#endif /* UPB_MINI_TABLE_DECODE_H_ */

@ -0,0 +1,301 @@
/*
* Copyright (c) 2009-2021, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google 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 Google LLC 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.
*/
#include "upb/mini_table/encode.h"
#include <inttypes.h>
#include "upb/arena.h"
#include "upb/mini_table/common.h"
#include "upb/mini_table/common_internal.h"
#include "upb/upb.h"
// Must be last.
#include "upb/port_def.inc"
typedef struct {
uint64_t present_values_mask;
uint32_t last_written_value;
} upb_MtDataEncoderInternal_EnumState;
typedef struct {
uint64_t msg_modifiers;
uint32_t last_field_num;
enum {
kUpb_OneofState_NotStarted,
kUpb_OneofState_StartedOneof,
kUpb_OneofState_EmittedOneofField,
} oneof_state;
} upb_MtDataEncoderInternal_MsgState;
typedef struct {
char* buf_start; // Only for checking kUpb_MtDataEncoder_MinSize.
union {
upb_MtDataEncoderInternal_EnumState enum_state;
upb_MtDataEncoderInternal_MsgState msg_state;
} state;
} upb_MtDataEncoderInternal;
static upb_MtDataEncoderInternal* upb_MtDataEncoder_GetInternal(
upb_MtDataEncoder* e, char* buf_start) {
UPB_ASSERT(sizeof(upb_MtDataEncoderInternal) <= sizeof(e->internal));
upb_MtDataEncoderInternal* ret = (upb_MtDataEncoderInternal*)e->internal;
ret->buf_start = buf_start;
return ret;
}
static char* upb_MtDataEncoder_PutRaw(upb_MtDataEncoder* e, char* ptr,
char ch) {
upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal;
UPB_ASSERT(ptr - in->buf_start < kUpb_MtDataEncoder_MinSize);
if (ptr == e->end) return NULL;
*ptr++ = ch;
return ptr;
}
static char* upb_MtDataEncoder_Put(upb_MtDataEncoder* e, char* ptr, char ch) {
return upb_MtDataEncoder_PutRaw(e, ptr, _upb_ToBase92(ch));
}
static char* upb_MtDataEncoder_PutBase92Varint(upb_MtDataEncoder* e, char* ptr,
uint32_t val, int min, int max) {
int shift = _upb_Log2Ceiling(_upb_FromBase92(max) - _upb_FromBase92(min) + 1);
UPB_ASSERT(shift <= 6);
uint32_t mask = (1 << shift) - 1;
do {
uint32_t bits = val & mask;
ptr = upb_MtDataEncoder_Put(e, ptr, bits + _upb_FromBase92(min));
if (!ptr) return NULL;
val >>= shift;
} while (val);
return ptr;
}
char* upb_MtDataEncoder_PutModifier(upb_MtDataEncoder* e, char* ptr,
uint64_t mod) {
if (mod) {
ptr = upb_MtDataEncoder_PutBase92Varint(e, ptr, mod,
kUpb_EncodedValue_MinModifier,
kUpb_EncodedValue_MaxModifier);
}
return ptr;
}
char* upb_MtDataEncoder_EncodeExtension(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.msg_state.msg_modifiers = 0;
in->state.msg_state.last_field_num = 0;
in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted;
ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_ExtensionV1);
if (!ptr) return NULL;
return upb_MtDataEncoder_PutField(e, ptr, type, field_num, field_mod);
}
char* upb_MtDataEncoder_EncodeMap(upb_MtDataEncoder* e, char* ptr,
upb_FieldType key_type,
upb_FieldType value_type,
uint64_t value_mod) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.msg_state.msg_modifiers = 0;
in->state.msg_state.last_field_num = 0;
in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted;
ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MapV1);
if (!ptr) return NULL;
ptr = upb_MtDataEncoder_PutField(e, ptr, key_type, 1, 0);
if (!ptr) return NULL;
return upb_MtDataEncoder_PutField(e, ptr, value_type, 2, value_mod);
}
char* upb_MtDataEncoder_EncodeMessageSet(upb_MtDataEncoder* e, char* ptr) {
(void)upb_MtDataEncoder_GetInternal(e, ptr);
return upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MessageSetV1);
}
char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr,
uint64_t msg_mod) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.msg_state.msg_modifiers = msg_mod;
in->state.msg_state.last_field_num = 0;
in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted;
ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MessageV1);
if (!ptr) return NULL;
return upb_MtDataEncoder_PutModifier(e, ptr, msg_mod);
}
char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod) {
static const char kUpb_TypeToEncoded[] = {
[kUpb_FieldType_Double] = kUpb_EncodedType_Double,
[kUpb_FieldType_Float] = kUpb_EncodedType_Float,
[kUpb_FieldType_Int64] = kUpb_EncodedType_Int64,
[kUpb_FieldType_UInt64] = kUpb_EncodedType_UInt64,
[kUpb_FieldType_Int32] = kUpb_EncodedType_Int32,
[kUpb_FieldType_Fixed64] = kUpb_EncodedType_Fixed64,
[kUpb_FieldType_Fixed32] = kUpb_EncodedType_Fixed32,
[kUpb_FieldType_Bool] = kUpb_EncodedType_Bool,
[kUpb_FieldType_String] = kUpb_EncodedType_String,
[kUpb_FieldType_Group] = kUpb_EncodedType_Group,
[kUpb_FieldType_Message] = kUpb_EncodedType_Message,
[kUpb_FieldType_Bytes] = kUpb_EncodedType_Bytes,
[kUpb_FieldType_UInt32] = kUpb_EncodedType_UInt32,
[kUpb_FieldType_Enum] = kUpb_EncodedType_OpenEnum,
[kUpb_FieldType_SFixed32] = kUpb_EncodedType_SFixed32,
[kUpb_FieldType_SFixed64] = kUpb_EncodedType_SFixed64,
[kUpb_FieldType_SInt32] = kUpb_EncodedType_SInt32,
[kUpb_FieldType_SInt64] = kUpb_EncodedType_SInt64,
};
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (field_num <= in->state.msg_state.last_field_num) return NULL;
if (in->state.msg_state.last_field_num + 1 != field_num) {
// Put skip.
UPB_ASSERT(field_num > in->state.msg_state.last_field_num);
uint32_t skip = field_num - in->state.msg_state.last_field_num;
ptr = upb_MtDataEncoder_PutBase92Varint(
e, ptr, skip, kUpb_EncodedValue_MinSkip, kUpb_EncodedValue_MaxSkip);
if (!ptr) return NULL;
}
in->state.msg_state.last_field_num = field_num;
uint32_t encoded_modifiers = 0;
// Put field type.
int encoded_type = kUpb_TypeToEncoded[type];
if (field_mod & kUpb_FieldModifier_IsClosedEnum) {
UPB_ASSERT(type == kUpb_FieldType_Enum);
encoded_type = kUpb_EncodedType_ClosedEnum;
}
if (field_mod & kUpb_FieldModifier_IsRepeated) {
// Repeated fields shift the type number up (unlike other modifiers which
// are bit flags).
encoded_type += kUpb_EncodedType_RepeatedBase;
if (_upb_FieldType_IsPackable(type)) {
bool field_is_packed = field_mod & kUpb_FieldModifier_IsPacked;
bool default_is_packed = in->state.msg_state.msg_modifiers &
kUpb_MessageModifier_DefaultIsPacked;
if (field_is_packed != default_is_packed) {
encoded_modifiers |= kUpb_EncodedFieldModifier_FlipPacked;
}
}
}
ptr = upb_MtDataEncoder_Put(e, ptr, encoded_type);
if (!ptr) return NULL;
if (field_mod & kUpb_FieldModifier_IsProto3Singular) {
encoded_modifiers |= kUpb_EncodedFieldModifier_IsProto3Singular;
}
if (field_mod & kUpb_FieldModifier_IsRequired) {
encoded_modifiers |= kUpb_EncodedFieldModifier_IsRequired;
}
return upb_MtDataEncoder_PutModifier(e, ptr, encoded_modifiers);
}
char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (in->state.msg_state.oneof_state == kUpb_OneofState_NotStarted) {
ptr = upb_MtDataEncoder_Put(e, ptr, _upb_FromBase92(kUpb_EncodedValue_End));
} else {
ptr = upb_MtDataEncoder_Put(
e, ptr, _upb_FromBase92(kUpb_EncodedValue_OneofSeparator));
}
in->state.msg_state.oneof_state = kUpb_OneofState_StartedOneof;
return ptr;
}
char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr,
uint32_t field_num) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (in->state.msg_state.oneof_state == kUpb_OneofState_EmittedOneofField) {
ptr = upb_MtDataEncoder_Put(
e, ptr, _upb_FromBase92(kUpb_EncodedValue_FieldSeparator));
if (!ptr) return NULL;
}
ptr = upb_MtDataEncoder_PutBase92Varint(e, ptr, field_num, _upb_ToBase92(0),
_upb_ToBase92(63));
in->state.msg_state.oneof_state = kUpb_OneofState_EmittedOneofField;
return ptr;
}
char* upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e, char* ptr) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.enum_state.present_values_mask = 0;
in->state.enum_state.last_written_value = 0;
return upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_EnumV1);
}
static char* upb_MtDataEncoder_FlushDenseEnumMask(upb_MtDataEncoder* e,
char* ptr) {
upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal;
ptr = upb_MtDataEncoder_Put(e, ptr, in->state.enum_state.present_values_mask);
in->state.enum_state.present_values_mask = 0;
in->state.enum_state.last_written_value += 5;
return ptr;
}
char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr,
uint32_t val) {
// TODO(b/229641772): optimize this encoding.
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
UPB_ASSERT(val >= in->state.enum_state.last_written_value);
uint32_t delta = val - in->state.enum_state.last_written_value;
if (delta >= 5 && in->state.enum_state.present_values_mask) {
ptr = upb_MtDataEncoder_FlushDenseEnumMask(e, ptr);
if (!ptr) {
return NULL;
}
delta -= 5;
}
if (delta >= 5) {
ptr = upb_MtDataEncoder_PutBase92Varint(
e, ptr, delta, kUpb_EncodedValue_MinSkip, kUpb_EncodedValue_MaxSkip);
in->state.enum_state.last_written_value += delta;
delta = 0;
}
UPB_ASSERT((in->state.enum_state.present_values_mask >> delta) == 0);
in->state.enum_state.present_values_mask |= 1ULL << delta;
return ptr;
}
char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (!in->state.enum_state.present_values_mask) return ptr;
return upb_MtDataEncoder_FlushDenseEnumMask(e, ptr);
}

@ -0,0 +1,109 @@
/*
* Copyright (c) 2009-2022, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google 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 Google LLC 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.
*/
#ifndef UPB_MINI_TABLE_ENCODE_H_
#define UPB_MINI_TABLE_ENCODE_H_
#include "upb/mini_table/common.h"
// Must be last.
#include "upb/port_def.inc"
// If the input buffer has at least this many bytes available, the encoder call
// is guaranteed to succeed (as long as field number order is maintained).
#define kUpb_MtDataEncoder_MinSize 16
typedef struct {
char* end; // Limit of the buffer passed as a parameter.
// Aliased to internal-only members in .cc.
char internal[32];
} upb_MtDataEncoder;
#ifdef __cplusplus
extern "C" {
#endif
// Encodes field/oneof information for a given message. The sequence of calls
// should look like:
//
// upb_MtDataEncoder e;
// char buf[256];
// char* ptr = buf;
// e.end = ptr + sizeof(buf);
// unit64_t msg_mod = ...; // bitwise & of kUpb_MessageModifiers or zero
// ptr = upb_MtDataEncoder_StartMessage(&e, ptr, msg_mod);
// // Fields *must* be in field number order.
// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
// ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
//
// // If oneofs are present. Oneofs must be encoded after regular fields.
// ptr = upb_MiniTable_StartOneof(&e, ptr)
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
//
// ptr = upb_MiniTable_StartOneof(&e, ptr);
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
// ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
//
// Oneofs must be encoded after all regular fields.
char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr,
uint64_t msg_mod);
char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod);
char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr);
char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr,
uint32_t field_num);
// Encodes the set of values for a given enum. The values must be given in
// order (after casting to uint32_t), and repeats are not allowed.
char* upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e, char* ptr);
char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr,
uint32_t val);
char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr);
// Encodes an entire mini descriptor for an extension.
char* upb_MtDataEncoder_EncodeExtension(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod);
// Encodes an entire mini descriptor for a map.
char* upb_MtDataEncoder_EncodeMap(upb_MtDataEncoder* e, char* ptr,
upb_FieldType key_type,
upb_FieldType value_type, uint64_t value_mod);
// Encodes an entire mini descriptor for a message set.
char* upb_MtDataEncoder_EncodeMessageSet(upb_MtDataEncoder* e, char* ptr);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port_undef.inc"
#endif /* UPB_MINI_TABLE_ENCODE_H_ */

@ -25,15 +25,14 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "upb/mini_table.hpp"
#include "google/protobuf/descriptor.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/flat_hash_set.h"
#include "upb/decode.h"
#include "upb/mini_table.h"
#include "upb/msg_internal.h"
#include "upb/mini_table.hpp"
#include "upb/mini_table/common_internal.h"
#include "upb/mini_table/decode.h"
#include "upb/upb.h"
#include "upb/upb.hpp"
@ -202,13 +201,13 @@ INSTANTIATE_TEST_SUITE_P(Platforms, MiniTableTest,
TEST(MiniTablePlatformIndependentTest, Base92Roundtrip) {
for (char i = 0; i < 92; i++) {
EXPECT_EQ(i, upb_FromBase92(upb_ToBase92(i)));
EXPECT_EQ(i, _upb_FromBase92(_upb_ToBase92(i)));
}
}
TEST(MiniTablePlatformIndependentTest, IsTypePackable) {
for (int i = 1; i <= protobuf::FieldDescriptor::MAX_TYPE; i++) {
EXPECT_EQ(upb_IsTypePackable(static_cast<upb_FieldType>(i)),
EXPECT_EQ(_upb_FieldType_IsPackable(static_cast<upb_FieldType>(i)),
protobuf::FieldDescriptor::IsTypePackable(
static_cast<protobuf::FieldDescriptor::Type>(i)));
}

@ -1,36 +0,0 @@
/*
* Copyright (c) 2009-2022, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google 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 Google LLC 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.
*/
#ifndef UPB_MINI_TABLE_ACCESSORS_INTERNAL_H_
#define UPB_MINI_TABLE_ACCESSORS_INTERNAL_H_
// TODO(b/232091617): Delete this entire header which currently exists only for
// temporary backwards compatibility.
#include "upb/internal/mini_table_accessors.h"
#endif // UPB_MINI_TABLE_ACCESSORS_INTERNAL_H_

@ -28,7 +28,7 @@
#ifndef UPB_REFLECTION_DESC_STATE_INTERNAL_H_
#define UPB_REFLECTION_DESC_STATE_INTERNAL_H_
#include "upb/mini_table.h"
#include "upb/mini_table/encode.h"
// Must be last.
#include "upb/port_def.inc"

@ -27,7 +27,8 @@
#include <stdio.h>
#include "upb/mini_table.h"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/encode.h"
#include "upb/reflection/def_builder_internal.h"
#include "upb/reflection/def_type.h"
#include "upb/reflection/desc_state_internal.h"

@ -28,7 +28,8 @@
#include <ctype.h>
#include <errno.h>
#include "upb/mini_table.h"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/encode.h"
#include "upb/reflection/def_builder_internal.h"
#include "upb/reflection/def_pool.h"
#include "upb/reflection/def_type.h"

@ -25,7 +25,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "upb/mini_table.h"
#include "upb/mini_table/decode.h"
#include "upb/reflection/def_builder_internal.h"
#include "upb/reflection/def_type.h"
#include "upb/reflection/desc_state_internal.h"

@ -29,7 +29,6 @@
#include <stdlib.h>
#include <string.h>
#include "upb/mini_table.h"
#include "upb/reflection/def_builder_internal.h"
#include "upb/reflection/def_type.h"
#include "upb/reflection/field_def_internal.h"

@ -28,10 +28,8 @@
#include "upbc/code_generator_request.h"
#include <inttypes.h>
#include <stdio.h>
#include "google/protobuf/compiler/plugin.upb.h"
#include "upb/mini_table.h"
#include "upb/reflection/def.h"
// Must be last.

Loading…
Cancel
Save