Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
10 KiB

3 years ago
/*
* 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/util/required_fields.h"
#include <inttypes.h>
3 years ago
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include "upb/reflection.h"
// Must be last.
#include "upb/port_def.inc"
////////////////////////////////////////////////////////////////////////////////
// upb_FieldPath_ToText()
////////////////////////////////////////////////////////////////////////////////
typedef struct {
char* buf;
char* ptr;
char* end;
size_t overflow;
} upb_PrintfAppender;
UPB_PRINTF(2, 3)
static void upb_FieldPath_Printf(upb_PrintfAppender* a, const char* fmt, ...) {
size_t n;
size_t have = a->end - a->ptr;
va_list args;
va_start(args, fmt);
n = vsnprintf(a->ptr, have, fmt, args);
va_end(args);
if (UPB_LIKELY(have > n)) {
a->ptr += n;
} else {
a->ptr = UPB_PTRADD(a->ptr, have);
a->overflow += (n - have);
}
}
static size_t upb_FieldPath_NullTerminate(upb_PrintfAppender *d, size_t size) {
size_t ret = d->ptr - d->buf + d->overflow;
if (size > 0) {
if (d->ptr == d->end) d->ptr--;
*d->ptr = '\0';
}
return ret;
}
static void upb_FieldPath_PutMapKey(upb_PrintfAppender *a, upb_msgval map_key,
const upb_fielddef *key_f) {
switch (upb_fielddef_type(key_f)) {
case UPB_TYPE_INT32:
upb_FieldPath_Printf(a, "[%" PRId32 "]", map_key.int32_val);
break;
case UPB_TYPE_INT64:
upb_FieldPath_Printf(a, "[%" PRId64 "]", map_key.int64_val);
break;
case UPB_TYPE_UINT32:
upb_FieldPath_Printf(a, "[%" PRIu32 "]", map_key.uint32_val);
break;
case UPB_TYPE_UINT64:
upb_FieldPath_Printf(a, "[%" PRIu64 "]", map_key.uint64_val);
break;
case UPB_TYPE_BOOL:
upb_FieldPath_Printf(a, "[%s]", map_key.bool_val ? "true" : "false");
break;
case UPB_TYPE_STRING:
upb_FieldPath_Printf(a, "[\"");
for (size_t i = 0; i < map_key.str_val.size; i++) {
char ch = map_key.str_val.data[i];
if (ch == '"') {
upb_FieldPath_Printf(a, "\\\"");
} else {
upb_FieldPath_Printf(a, "%c", ch);
}
}
upb_FieldPath_Printf(a, "\"]");
break;
default:
UPB_UNREACHABLE(); // Other types can't be map keys.
}
}
size_t upb_FieldPath_ToText(upb_FieldPathEntry** path, char* buf, size_t size) {
upb_FieldPathEntry *ptr = *path;
upb_PrintfAppender appender;
appender.buf = buf;
appender.ptr = buf;
appender.end = UPB_PTRADD(buf, size);
appender.overflow = 0;
bool first = true;
while (ptr->field) {
const upb_fielddef *f = ptr->field;
upb_FieldPath_Printf(&appender, first ? "%s" : ".%s", upb_fielddef_name(f));
first = false;
ptr++;
if (upb_fielddef_ismap(f)) {
const upb_fielddef *key_f = upb_msgdef_field(upb_fielddef_msgsubdef(f), 0);
upb_FieldPath_PutMapKey(&appender, ptr->map_key, key_f);
ptr++;
} else if (upb_fielddef_isseq(f)) {
upb_FieldPath_Printf(&appender, "[%zu]", ptr->array_index);
ptr++;
}
}
// Advance beyond terminating NULL.
ptr++;
*path = ptr;
return upb_FieldPath_NullTerminate(&appender, size);
}
////////////////////////////////////////////////////////////////////////////////
// upb_util_HasUnsetRequired()
////////////////////////////////////////////////////////////////////////////////
3 years ago
typedef struct {
upb_FieldPathEntry *path;
3 years ago
size_t size;
size_t cap;
} upb_FieldPathVector;
typedef struct {
upb_FieldPathVector stack;
upb_FieldPathVector out_fields;
3 years ago
const upb_symtab *ext_pool;
jmp_buf err;
bool has_unset_required;
bool save_paths;
} upb_FindContext;
static void upb_FieldPathVector_Init(upb_FieldPathVector *vec) {
vec->path = NULL;
vec->size = 0;
vec->cap = 0;
3 years ago
}
static void upb_FieldPathVector_Reserve(upb_FindContext *ctx,
upb_FieldPathVector *vec,
size_t elems) {
if (vec->cap - vec->size < elems) {
size_t need = vec->size + elems;
vec->cap = UPB_MAX(4, vec->cap);
while (vec->cap < need) vec->cap *= 2;
vec->path = realloc(vec->path, vec->cap * sizeof(*vec->path));
if (!vec->path) { UPB_LONGJMP(ctx->err, 1); }
3 years ago
}
}
static void upb_FindContext_Push(upb_FindContext* ctx, upb_FieldPathEntry ent) {
if (!ctx->save_paths) return;
upb_FieldPathVector_Reserve(ctx, &ctx->stack, 1);
ctx->stack.path[ctx->stack.size++] = ent;
}
3 years ago
static void upb_FindContext_Pop(upb_FindContext* ctx) {
if (!ctx->save_paths) return;
assert(ctx->stack.size != 0);
ctx->stack.size--;
3 years ago
}
static void upb_util_FindUnsetInMessage(upb_FindContext* ctx,
const upb_msg* msg,
const upb_msgdef* m) {
// Iterate over all fields to see if any required fields are missing.
3 years ago
for (int i = 0, n = upb_msgdef_fieldcount(m); i < n; i++) {
const upb_fielddef* f = upb_msgdef_field(m, i);
3 years ago
if (upb_fielddef_label(f) != UPB_LABEL_REQUIRED) continue;
3 years ago
if (!upb_msg_has(msg, f)) {
// A required field is missing.
ctx->has_unset_required = true;
if (ctx->save_paths) {
// Append the contents of the stack to the out array, then
// NULL-terminate.
upb_FieldPathVector_Reserve(ctx, &ctx->out_fields, ctx->stack.size + 2);
memcpy(&ctx->out_fields.path[ctx->out_fields.size], ctx->stack.path,
ctx->stack.size * sizeof(*ctx->stack.path));
ctx->out_fields.size += ctx->stack.size;
ctx->out_fields.path[ctx->out_fields.size++] =
(upb_FieldPathEntry){.field = f};
ctx->out_fields.path[ctx->out_fields.size++] =
(upb_FieldPathEntry){.field = NULL};
}
3 years ago
}
}
}
static void upb_util_FindUnsetRequiredInternal(upb_FindContext* ctx,
const upb_msg* msg,
const upb_msgdef* m) {
// OPT: add markers in the schema for where we can avoid iterating:
// 1. messages with no required fields.
// 2. messages that cannot possibly reach any required fields.
upb_util_FindUnsetInMessage(ctx, msg, m);
3 years ago
// Iterate over all present fields to find sub-messages that might be missing
// required fields. This may revisit some of the fields already inspected
// in the previous loop. We do this separately because this loop will also
// find present extensions, which the previous loop will not.
//
// TODO(haberman): consider changing upb_msg_next() to be capable of visiting
// extensions only, for example with a UPB_MSG_BEGINEXT constant.
3 years ago
size_t iter = UPB_MSG_BEGIN;
const upb_fielddef* f;
3 years ago
upb_msgval val;
while (upb_msg_next(msg, m, ctx->ext_pool, &f, &val, &iter)) {
// Skip non-submessage fields.
if (!upb_fielddef_issubmsg(f)) continue;
upb_FindContext_Push(ctx, (upb_FieldPathEntry){.field = f});
const upb_msgdef* sub_m = upb_fielddef_msgsubdef(f);
3 years ago
if (upb_fielddef_ismap(f)) {
// Map field.
const upb_fielddef* val_f = upb_msgdef_field(sub_m, 1);
const upb_msgdef* val_m = upb_fielddef_msgsubdef(val_f);
3 years ago
if (!val_m) continue;
const upb_map* map = val.map_val;
3 years ago
size_t iter = UPB_MAP_BEGIN;
while (upb_mapiter_next(map, &iter)) {
upb_msgval key = upb_mapiter_key(map, iter);
3 years ago
upb_msgval map_val = upb_mapiter_value(map, iter);
upb_FindContext_Push(ctx, (upb_FieldPathEntry){.map_key = key});
upb_util_FindUnsetRequiredInternal(ctx, map_val.msg_val, val_m);
upb_FindContext_Pop(ctx);
3 years ago
}
} else if (upb_fielddef_isseq(f)) {
// Repeated field.
const upb_array* arr = val.array_val;
3 years ago
for (size_t i = 0, n = upb_array_size(arr); i < n; i++) {
upb_msgval elem = upb_array_get(arr, i);
upb_FindContext_Push(ctx, (upb_FieldPathEntry){.array_index = i});
upb_util_FindUnsetRequiredInternal(ctx, elem.msg_val, sub_m);
upb_FindContext_Pop(ctx);
3 years ago
}
} else {
// Scalar sub-message field.
upb_util_FindUnsetRequiredInternal(ctx, val.msg_val, sub_m);
3 years ago
}
upb_FindContext_Pop(ctx);
}
3 years ago
}
bool upb_util_HasUnsetRequired(const upb_msg* msg, const upb_msgdef* m,
const upb_symtab* ext_pool,
upb_FieldPathEntry** fields) {
upb_FindContext ctx;
ctx.has_unset_required = false;
ctx.save_paths = fields != NULL;
ctx.ext_pool = ext_pool;
upb_FieldPathVector_Init(&ctx.stack);
upb_FieldPathVector_Init(&ctx.out_fields);
upb_util_FindUnsetRequiredInternal(&ctx, msg, m);
free(ctx.stack.path);
if (fields) {
upb_FieldPathVector_Reserve(&ctx, &ctx.out_fields, 1);
ctx.out_fields.path[ctx.out_fields.size] =
(upb_FieldPathEntry){.field = NULL};
*fields = ctx.out_fields.path;
}
return ctx.has_unset_required;
3 years ago
}