| /* Amalgamated source file */ |
| #include "ruby-upb.h" |
| |
| /* |
| * This is where we define internal portability macros used across upb. |
| * |
| * All of these macros are undef'd in undef.inc to avoid leaking them to users. |
| * |
| * The correct usage is: |
| * |
| * #include "upb/foobar.h" |
| * #include "upb/baz.h" |
| * |
| * // MUST be last included header. |
| * #include "upb/port/def.inc" |
| * |
| * // Code for this file. |
| * // <...> |
| * |
| * // Can be omitted for .c files, required for .h. |
| * #include "upb/port/undef.inc" |
| * |
| * This file is private and must not be included by users! |
| */ |
| |
| #if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \ |
| (defined(__cplusplus) && __cplusplus >= 201402L) || \ |
| (defined(_MSC_VER) && _MSC_VER >= 1900)) |
| #error upb requires C99 or C++14 or MSVC >= 2015. |
| #endif |
| |
| // Portable check for GCC minimum version: |
| // https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html |
| #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) |
| #define UPB_GNUC_MIN(x, y) \ |
| (__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y)) |
| #else |
| #define UPB_GNUC_MIN(x, y) 0 |
| #endif |
| |
| #include <assert.h> |
| #include <setjmp.h> |
| #include <stdbool.h> |
| #include <stdint.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| |
| #ifndef UINTPTR_MAX |
| Error, UINTPTR_MAX is undefined |
| #endif |
| |
| #if UINTPTR_MAX == 0xffffffff |
| #define UPB_SIZE(size32, size64) size32 |
| #else |
| #define UPB_SIZE(size32, size64) size64 |
| #endif |
| |
| /* If we always read/write as a consistent type to each address, this shouldn't |
| * violate aliasing. |
| */ |
| #define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs))) |
| |
| #define UPB_MAPTYPE_STRING 0 |
| |
| // UPB_EXPORT: always generate a public symbol. |
| #if defined(__GNUC__) || defined(__clang__) |
| #define UPB_EXPORT __attribute__((visibility("default"))) __attribute__((used)) |
| #else |
| #define UPB_EXPORT |
| #endif |
| |
| // UPB_INLINE: inline if possible, emit standalone code if required. |
| #ifdef __cplusplus |
| #define UPB_INLINE inline |
| #elif defined (__GNUC__) || defined(__clang__) |
| #define UPB_INLINE static __inline__ |
| #else |
| #define UPB_INLINE static |
| #endif |
| |
| #ifdef UPB_BUILD_API |
| #define UPB_API UPB_EXPORT |
| #define UPB_API_INLINE UPB_EXPORT |
| #else |
| #define UPB_API |
| #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)) |
| #define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, UPB_MALLOC_ALIGN) |
| #ifdef __clang__ |
| #define UPB_ALIGN_OF(type) _Alignof(type) |
| #else |
| #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) |
| #define UPB_UNLIKELY(x) __builtin_expect((bool)(x), 0) |
| #else |
| #define UPB_LIKELY(x) (x) |
| #define UPB_UNLIKELY(x) (x) |
| #endif |
| |
| // Macros for function attributes on compilers that support them. |
| #ifdef __GNUC__ |
| #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 static |
| #define UPB_NORETURN __declspec(noreturn) |
| #define UPB_PRINTF(str, first_vararg) |
| #else /* !defined(__GNUC__) */ |
| #define UPB_FORCEINLINE static |
| #define UPB_NOINLINE |
| #define UPB_NORETURN |
| #define UPB_PRINTF(str, first_vararg) |
| #endif |
| |
| #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y)) |
| #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y)) |
| |
| #define UPB_UNUSED(var) (void)var |
| |
| // UPB_ASSUME(): in release mode, we tell the compiler to assume this is true. |
| #ifdef NDEBUG |
| #ifdef __GNUC__ |
| #define UPB_ASSUME(expr) if (!(expr)) __builtin_unreachable() |
| #elif defined _MSC_VER |
| #define UPB_ASSUME(expr) if (!(expr)) __assume(0) |
| #else |
| #define UPB_ASSUME(expr) do {} while (false && (expr)) |
| #endif |
| #else |
| #define UPB_ASSUME(expr) assert(expr) |
| #endif |
| |
| /* UPB_ASSERT(): in release mode, we use the expression without letting it be |
| * evaluated. This prevents "unused variable" warnings. */ |
| #ifdef NDEBUG |
| #define UPB_ASSERT(expr) do {} while (false && (expr)) |
| #else |
| #define UPB_ASSERT(expr) assert(expr) |
| #endif |
| |
| #if defined(__GNUC__) || defined(__clang__) |
| #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0) |
| #elif defined(_MSC_VER) |
| #define UPB_UNREACHABLE() \ |
| do { \ |
| assert(0); \ |
| __assume(0); \ |
| } while (0) |
| #else |
| #define UPB_UNREACHABLE() do { assert(0); } while(0) |
| #endif |
| |
| /* UPB_SETJMP() / UPB_LONGJMP(): avoid setting/restoring signal mask. */ |
| #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) |
| #endif |
| |
| #ifdef __GNUC__ |
| #define UPB_USE_C11_ATOMICS |
| #define UPB_ATOMIC(T) _Atomic(T) |
| #else |
| #define UPB_ATOMIC(T) T |
| #endif |
| |
| /* UPB_PTRADD(ptr, ofs): add pointer while avoiding "NULL + 0" UB */ |
| #define UPB_PTRADD(ptr, ofs) ((ofs) ? (ptr) + (ofs) : (ptr)) |
| |
| #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 |
| #define UPB_HAS_ATTRIBUTE(x) __has_attribute(x) |
| #else |
| #define UPB_HAS_ATTRIBUTE(x) 0 |
| #endif |
| |
| #if UPB_HAS_ATTRIBUTE(musttail) |
| #define UPB_MUSTTAIL __attribute__((musttail)) |
| #else |
| #define UPB_MUSTTAIL |
| #endif |
| |
| #undef UPB_HAS_ATTRIBUTE |
| |
| /* This check is not fully robust: it does not require that we have "musttail" |
| * support available. We need tail calls to avoid consuming arbitrary amounts |
| * of stack space. |
| * |
| * GCC/Clang can mostly be trusted to generate tail calls as long as |
| * optimization is enabled, but, debug builds will not generate tail calls |
| * unless "musttail" is available. |
| * |
| * We should probably either: |
| * 1. require that the compiler supports musttail. |
| * 2. add some fallback code for when musttail isn't available (ie. return |
| * instead of tail calling). This is safe and portable, but this comes at |
| * a CPU cost. |
| */ |
| #if (defined(__x86_64__) || defined(__aarch64__)) && defined(__GNUC__) |
| #define UPB_FASTTABLE_SUPPORTED 1 |
| #else |
| #define UPB_FASTTABLE_SUPPORTED 0 |
| #endif |
| |
| /* define UPB_ENABLE_FASTTABLE to force fast table support. |
| * This is useful when we want to ensure we are really getting fasttable, |
| * for example for testing or benchmarking. */ |
| #if defined(UPB_ENABLE_FASTTABLE) |
| #if !UPB_FASTTABLE_SUPPORTED |
| #error fasttable is x86-64/ARM64 only and requires GCC or Clang. |
| #endif |
| #define UPB_FASTTABLE 1 |
| /* Define UPB_TRY_ENABLE_FASTTABLE to use fasttable if possible. |
| * This is useful for releasing code that might be used on multiple platforms, |
| * for example the PHP or Ruby C extensions. */ |
| #elif defined(UPB_TRY_ENABLE_FASTTABLE) |
| #define UPB_FASTTABLE UPB_FASTTABLE_SUPPORTED |
| #else |
| #define UPB_FASTTABLE 0 |
| #endif |
| |
| /* UPB_FASTTABLE_INIT() allows protos compiled for fasttable to gracefully |
| * degrade to non-fasttable if the runtime or platform do not support it. */ |
| #if !UPB_FASTTABLE |
| #define UPB_FASTTABLE_INIT(...) |
| #define UPB_FASTTABLE_MASK(mask) -1 |
| #else |
| #define UPB_FASTTABLE_INIT(...) __VA_ARGS__ |
| #define UPB_FASTTABLE_MASK(mask) mask |
| #endif |
| |
| #undef UPB_FASTTABLE_SUPPORTED |
| |
| /* ASAN poisoning (for arena). |
| * If using UPB from an interpreted language like Ruby, a build of the |
| * interpreter compiled with ASAN enabled must be used in order to get sane and |
| * expected behavior. |
| */ |
| |
| /* Due to preprocessor limitations, the conditional logic for setting |
| * UPN_CLANG_ASAN below cannot be consolidated into a portable one-liner. |
| * See https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html. |
| */ |
| #if defined(__has_feature) |
| #if __has_feature(address_sanitizer) |
| #define UPB_CLANG_ASAN 1 |
| #else |
| #define UPB_CLANG_ASAN 0 |
| #endif |
| #else |
| #define UPB_CLANG_ASAN 0 |
| #endif |
| |
| #if defined(__SANITIZE_ADDRESS__) || UPB_CLANG_ASAN |
| #define UPB_ASAN 1 |
| #define UPB_ASAN_GUARD_SIZE 32 |
| #ifdef __cplusplus |
| extern "C" { |
| #endif |
| void __asan_poison_memory_region(void const volatile *addr, size_t size); |
| void __asan_unpoison_memory_region(void const volatile *addr, size_t size); |
| #ifdef __cplusplus |
| } /* extern "C" */ |
| #endif |
| #define UPB_POISON_MEMORY_REGION(addr, size) \ |
| __asan_poison_memory_region((addr), (size)) |
| #define UPB_UNPOISON_MEMORY_REGION(addr, size) \ |
| __asan_unpoison_memory_region((addr), (size)) |
| #else |
| #define UPB_ASAN 0 |
| #define UPB_ASAN_GUARD_SIZE 0 |
| #define UPB_POISON_MEMORY_REGION(addr, size) \ |
| ((void)(addr), (void)(size)) |
| #define UPB_UNPOISON_MEMORY_REGION(addr, size) \ |
| ((void)(addr), (void)(size)) |
| #endif |
| |
| /* Disable proto2 arena behavior (TEMPORARY) **********************************/ |
| |
| #ifdef UPB_DISABLE_CLOSED_ENUM_CHECKING |
| #define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 1 |
| #else |
| #define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 0 |
| #endif |
| |
| #if defined(__cplusplus) |
| #if defined(__clang__) || UPB_GNUC_MIN(6, 0) |
| // https://gcc.gnu.org/gcc-6/changes.html |
| #if __cplusplus >= 201402L |
| #define UPB_DEPRECATED [[deprecated]] |
| #else |
| #define UPB_DEPRECATED __attribute__((deprecated)) |
| #endif |
| #else |
| #define UPB_DEPRECATED |
| #endif |
| #else |
| #define UPB_DEPRECATED |
| #endif |
| |
| #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), \ |
| no_sanitize("address"))) |
| #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), \ |
| no_sanitize("address"))) |
| #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, no_sanitize("address"))) |
| #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 |
| |
| |
| #include <errno.h> |
| #include <float.h> |
| #include <stdarg.h> |
| #include <stdlib.h> |
| #include <string.h> |
| |
| // Must be last. |
| |
| void upb_Status_Clear(upb_Status* status) { |
| if (!status) return; |
| status->ok = true; |
| status->msg[0] = '\0'; |
| } |
| |
| bool upb_Status_IsOk(const upb_Status* status) { return status->ok; } |
| |
| const char* upb_Status_ErrorMessage(const upb_Status* status) { |
| return status->msg; |
| } |
| |
| void upb_Status_SetErrorMessage(upb_Status* status, const char* msg) { |
| if (!status) return; |
| status->ok = false; |
| strncpy(status->msg, msg, _kUpb_Status_MaxMessage - 1); |
| status->msg[_kUpb_Status_MaxMessage - 1] = '\0'; |
| } |
| |
| void upb_Status_SetErrorFormat(upb_Status* status, const char* fmt, ...) { |
| va_list args; |
| va_start(args, fmt); |
| upb_Status_VSetErrorFormat(status, fmt, args); |
| va_end(args); |
| } |
| |
| void upb_Status_VSetErrorFormat(upb_Status* status, const char* fmt, |
| va_list args) { |
| if (!status) return; |
| status->ok = false; |
| vsnprintf(status->msg, sizeof(status->msg), fmt, args); |
| status->msg[_kUpb_Status_MaxMessage - 1] = '\0'; |
| } |
| |
| void upb_Status_VAppendErrorFormat(upb_Status* status, const char* fmt, |
| va_list args) { |
| size_t len; |
| if (!status) return; |
| status->ok = false; |
| len = strlen(status->msg); |
| vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args); |
| status->msg[_kUpb_Status_MaxMessage - 1] = '\0'; |
| } |
| |
| |
| static const char* _upb_EpsCopyInputStream_NoOpCallback( |
| upb_EpsCopyInputStream* e, const char* old_end, const char* new_start) { |
| return new_start; |
| } |
| |
| const char* _upb_EpsCopyInputStream_IsDoneFallbackNoCallback( |
| upb_EpsCopyInputStream* e, const char* ptr, int overrun) { |
| return _upb_EpsCopyInputStream_IsDoneFallbackInline( |
| e, ptr, overrun, _upb_EpsCopyInputStream_NoOpCallback); |
| } |
| |
| |
| #include <errno.h> |
| #include <float.h> |
| #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> |
| |
| |
| // Must be last. |
| |
| typedef struct { |
| const char *ptr, *end; |
| upb_Arena* arena; /* TODO: should we have a tmp arena for tmp data? */ |
| const upb_DefPool* symtab; |
| int depth; |
| upb_Status* status; |
| jmp_buf err; |
| int line; |
| const char* line_begin; |
| bool is_first; |
| int options; |
| 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_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, |
| const upb_MessageDef* m); |
| |
| static bool jsondec_streql(upb_StringView str, const char* lit) { |
| return str.size == strlen(lit) && memcmp(str.data, lit, str.size) == 0; |
| } |
| |
| static bool jsondec_isnullvalue(const upb_FieldDef* f) { |
| return upb_FieldDef_CType(f) == kUpb_CType_Enum && |
| strcmp(upb_EnumDef_FullName(upb_FieldDef_EnumSubDef(f)), |
| "google.protobuf.NullValue") == 0; |
| } |
| |
| static bool jsondec_isvalue(const upb_FieldDef* f) { |
| return (upb_FieldDef_CType(f) == kUpb_CType_Message && |
| upb_MessageDef_WellKnownType(upb_FieldDef_MessageSubDef(f)) == |
| kUpb_WellKnown_Value) || |
| jsondec_isnullvalue(f); |
| } |
| |
| 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); |
| } |
| |
| UPB_PRINTF(2, 3) |
| UPB_NORETURN static void jsondec_errf(jsondec* d, const char* fmt, ...) { |
| va_list argp; |
| upb_Status_SetErrorFormat(d->status, "Error parsing JSON @%d:%d: ", d->line, |
| (int)(d->ptr - d->line_begin)); |
| va_start(argp, fmt); |
| upb_Status_VAppendErrorFormat(d->status, fmt, argp); |
| va_end(argp); |
| UPB_LONGJMP(d->err, 1); |
| } |
| |
| // 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': |
| d->line++; |
| d->line_begin = d->ptr; |
| /* Fallthrough. */ |
| case '\r': |
| case '\t': |
| case ' ': |
| d->ptr++; |
| break; |
| default: |
| return; |
| } |
| } |
| } |
| |
| // 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) { |
| if (d->ptr == d->end || *d->ptr != ch) return false; |
| d->ptr++; |
| return true; |
| } |
| |
| static void jsondec_parselit(jsondec* d, const char* lit) { |
| size_t avail = d->end - d->ptr; |
| size_t len = strlen(lit); |
| if (avail < len || memcmp(d->ptr, lit, len) != 0) { |
| jsondec_errf(d, "Expected: '%s'", lit); |
| } |
| d->ptr += len; |
| } |
| |
| static void jsondec_wsch(jsondec* d, char ch) { |
| jsondec_skipws(d); |
| if (!jsondec_tryparsech(d, ch)) { |
| jsondec_errf(d, "Expected: '%c'", ch); |
| } |
| } |
| |
| static void jsondec_true(jsondec* d) { jsondec_parselit(d, "true"); } |
| static void jsondec_false(jsondec* d) { jsondec_parselit(d, "false"); } |
| static void jsondec_null(jsondec* d) { jsondec_parselit(d, "null"); } |
| |
| static void jsondec_entrysep(jsondec* d) { |
| jsondec_skipws(d); |
| jsondec_parselit(d, ":"); |
| } |
| |
| static int jsondec_rawpeek(jsondec* d) { |
| if (d->ptr == d->end) { |
| jsondec_err(d, "Unexpected EOF"); |
| } |
| |
| switch (*d->ptr) { |
| case '{': |
| return JD_OBJECT; |
| case '[': |
| return JD_ARRAY; |
| case '"': |
| return JD_STRING; |
| case '-': |
| case '0': |
| case '1': |
| case '2': |
| case '3': |
| case '4': |
| case '5': |
| case '6': |
| case '7': |
| case '8': |
| case '9': |
| return JD_NUMBER; |
| case 't': |
| return JD_TRUE; |
| case 'f': |
| return JD_FALSE; |
| case 'n': |
| return JD_NULL; |
| default: |
| jsondec_errf(d, "Unexpected character: '%c'", *d->ptr); |
| } |
| } |
| |
| /* JSON object/array **********************************************************/ |
| |
| /* These are used like so: |
| * |
| * jsondec_objstart(d); |
| * while (jsondec_objnext(d)) { |
| * ... |
| * } |
| * jsondec_objend(d) */ |
| |
| static int jsondec_peek(jsondec* d) { |
| jsondec_skipws(d); |
| return jsondec_rawpeek(d); |
| } |
| |
| static void jsondec_push(jsondec* d) { |
| if (--d->depth < 0) { |
| jsondec_err(d, "Recursion limit exceeded"); |
| } |
| d->is_first = true; |
| } |
| |
| static bool jsondec_seqnext(jsondec* d, char end_ch) { |
| bool is_first = d->is_first; |
| d->is_first = false; |
| jsondec_skipws(d); |
| if (*d->ptr == end_ch) return false; |
| if (!is_first) jsondec_parselit(d, ","); |
| return true; |
| } |
| |
| static void jsondec_arrstart(jsondec* d) { |
| jsondec_push(d); |
| jsondec_wsch(d, '['); |
| } |
| |
| static void jsondec_arrend(jsondec* d) { |
| d->depth++; |
| jsondec_wsch(d, ']'); |
| } |
| |
| static bool jsondec_arrnext(jsondec* d) { return jsondec_seqnext(d, ']'); } |
| |
| static void jsondec_objstart(jsondec* d) { |
| jsondec_push(d); |
| jsondec_wsch(d, '{'); |
| } |
| |
| static void jsondec_objend(jsondec* d) { |
| d->depth++; |
| jsondec_wsch(d, '}'); |
| } |
| |
| static bool jsondec_objnext(jsondec* d) { |
| if (!jsondec_seqnext(d, '}')) return false; |
| if (jsondec_peek(d) != JD_STRING) { |
| jsondec_err(d, "Object must start with string"); |
| } |
| return true; |
| } |
| |
| /* JSON number ****************************************************************/ |
| |
| static bool jsondec_tryskipdigits(jsondec* d) { |
| const char* start = d->ptr; |
| |
| while (d->ptr < d->end) { |
| if (*d->ptr < '0' || *d->ptr > '9') { |
| break; |
| } |
| d->ptr++; |
| } |
| |
| return d->ptr != start; |
| } |
| |
| static void jsondec_skipdigits(jsondec* d) { |
| if (!jsondec_tryskipdigits(d)) { |
| jsondec_err(d, "Expected one or more digits"); |
| } |
| } |
| |
| static double jsondec_number(jsondec* d) { |
| const char* start = d->ptr; |
| |
| UPB_ASSERT(jsondec_rawpeek(d) == JD_NUMBER); |
| |
| /* Skip over the syntax of a number, as specified by JSON. */ |
| if (*d->ptr == '-') d->ptr++; |
| |
| if (jsondec_tryparsech(d, '0')) { |
| if (jsondec_tryskipdigits(d)) { |
| jsondec_err(d, "number cannot have leading zero"); |
| } |
| } else { |
| jsondec_skipdigits(d); |
| } |
| |
| if (d->ptr == d->end) goto parse; |
| if (jsondec_tryparsech(d, '.')) { |
| jsondec_skipdigits(d); |
| } |
| if (d->ptr == d->end) goto parse; |
| |
| if (*d->ptr == 'e' || *d->ptr == 'E') { |
| d->ptr++; |
| if (d->ptr == d->end) { |
| jsondec_err(d, "Unexpected EOF in number"); |
| } |
| if (*d->ptr == '+' || *d->ptr == '-') { |
| d->ptr++; |
| } |
| jsondec_skipdigits(d); |
| } |
| |
| parse: |
| /* Having verified the syntax of a JSON number, use strtod() to 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(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 |
| * something else? Investigate further. */ |
| /* |
| if (errno == ERANGE) { |
| jsondec_err(d, "Number out of range"); |
| } |
| */ |
| |
| if (val > DBL_MAX || val < -DBL_MAX) { |
| jsondec_err(d, "Number out of range"); |
| } |
| |
| return val; |
| } |
| } |
| |
| /* JSON string ****************************************************************/ |
| |
| static char jsondec_escape(jsondec* d) { |
| switch (*d->ptr++) { |
| case '"': |
| return '\"'; |
| case '\\': |
| return '\\'; |
| case '/': |
| return '/'; |
| case 'b': |
| return '\b'; |
| case 'f': |
| return '\f'; |
| case 'n': |
| return '\n'; |
| case 'r': |
| return '\r'; |
| case 't': |
| return '\t'; |
| default: |
| jsondec_err(d, "Invalid escape char"); |
| } |
| } |
| |
| static uint32_t jsondec_codepoint(jsondec* d) { |
| uint32_t cp = 0; |
| const char* end; |
| |
| if (d->end - d->ptr < 4) { |
| jsondec_err(d, "EOF inside string"); |
| } |
| |
| end = d->ptr + 4; |
| while (d->ptr < end) { |
| char ch = *d->ptr++; |
| if (ch >= '0' && ch <= '9') { |
| ch -= '0'; |
| } else if (ch >= 'a' && ch <= 'f') { |
| ch = ch - 'a' + 10; |
| } else if (ch >= 'A' && ch <= 'F') { |
| ch = ch - 'A' + 10; |
| } else { |
| jsondec_err(d, "Invalid hex digit"); |
| } |
| cp = (cp << 4) | ch; |
| } |
| |
| return cp; |
| } |
| |
| /* Parses a \uXXXX unicode escape (possibly a surrogate pair). */ |
| static size_t jsondec_unicode(jsondec* d, char* out) { |
| uint32_t cp = jsondec_codepoint(d); |
| if (upb_Unicode_IsHigh(cp)) { |
| /* Surrogate pair: two 16-bit codepoints become a 32-bit codepoint. */ |
| jsondec_parselit(d, "\\u"); |
| uint32_t low = jsondec_codepoint(d); |
| if (!upb_Unicode_IsLow(low)) jsondec_err(d, "Invalid low surrogate"); |
| cp = upb_Unicode_FromPair(cp, low); |
| } else if (upb_Unicode_IsLow(cp)) { |
| jsondec_err(d, "Unpaired low surrogate"); |
| } |
| |
| /* Write to UTF-8 */ |
| int bytes = upb_Unicode_ToUTF8(cp, out); |
| if (bytes == 0) jsondec_err(d, "Invalid codepoint"); |
| return bytes; |
| } |
| |
| static void jsondec_resize(jsondec* d, char** buf, char** end, char** buf_end) { |
| size_t oldsize = *buf_end - *buf; |
| size_t len = *end - *buf; |
| size_t size = UPB_MAX(8, 2 * oldsize); |
| |
| *buf = upb_Arena_Realloc(d->arena, *buf, len, size); |
| if (!*buf) jsondec_err(d, "Out of memory"); |
| |
| *end = *buf + len; |
| *buf_end = *buf + size; |
| } |
| |
| static upb_StringView jsondec_string(jsondec* d) { |
| char* buf = NULL; |
| char* end = NULL; |
| char* buf_end = NULL; |
| |
| jsondec_skipws(d); |
| |
| if (*d->ptr++ != '"') { |
| jsondec_err(d, "Expected string"); |
| } |
| |
| while (d->ptr < d->end) { |
| char ch = *d->ptr++; |
| |
| if (end == buf_end) { |
| jsondec_resize(d, &buf, &end, &buf_end); |
| } |
| |
| switch (ch) { |
| case '"': { |
| upb_StringView ret; |
| ret.data = buf; |
| ret.size = end - buf; |
| *end = '\0'; /* Needed for possible strtod(). */ |
| return ret; |
| } |
| case '\\': |
| if (d->ptr == d->end) goto eof; |
| if (*d->ptr == 'u') { |
| d->ptr++; |
| if (buf_end - end < 4) { |
| /* Allow space for maximum-sized codepoint (4 bytes). */ |
| jsondec_resize(d, &buf, &end, &buf_end); |
| } |
| end += jsondec_unicode(d, end); |
| } else { |
| *end++ = jsondec_escape(d); |
| } |
| break; |
| default: |
| if ((unsigned char)ch < 0x20) { |
| jsondec_err(d, "Invalid char in JSON string"); |
| } |
| *end++ = ch; |
| break; |
| } |
| } |
| |
| eof: |
| jsondec_err(d, "EOF inside string"); |
| } |
| |
| static void jsondec_skipval(jsondec* d) { |
| switch (jsondec_peek(d)) { |
| case JD_OBJECT: |
| jsondec_objstart(d); |
| while (jsondec_objnext(d)) { |
| jsondec_string(d); |
| jsondec_entrysep(d); |
| jsondec_skipval(d); |
| } |
| jsondec_objend(d); |
| break; |
| case JD_ARRAY: |
| jsondec_arrstart(d); |
| while (jsondec_arrnext(d)) { |
| jsondec_skipval(d); |
| } |
| jsondec_arrend(d); |
| break; |
| case JD_TRUE: |
| jsondec_true(d); |
| break; |
| case JD_FALSE: |
| jsondec_false(d); |
| break; |
| case JD_NULL: |
| jsondec_null(d); |
| break; |
| case JD_STRING: |
| jsondec_string(d); |
| break; |
| case JD_NUMBER: |
| jsondec_number(d); |
| break; |
| } |
| } |
| |
| /* Base64 decoding for bytes fields. ******************************************/ |
| |
| static unsigned int jsondec_base64_tablelookup(const char ch) { |
| /* Table includes the normal base64 chars plus the URL-safe variant. */ |
| const signed char table[256] = { |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, 62 /*+*/, -1, 62 /*-*/, -1, 63 /*/ */, 52 /*0*/, |
| 53 /*1*/, 54 /*2*/, 55 /*3*/, 56 /*4*/, 57 /*5*/, 58 /*6*/, 59 /*7*/, |
| 60 /*8*/, 61 /*9*/, -1, -1, -1, -1, -1, |
| -1, -1, 0 /*A*/, 1 /*B*/, 2 /*C*/, 3 /*D*/, 4 /*E*/, |
| 5 /*F*/, 6 /*G*/, 07 /*H*/, 8 /*I*/, 9 /*J*/, 10 /*K*/, 11 /*L*/, |
| 12 /*M*/, 13 /*N*/, 14 /*O*/, 15 /*P*/, 16 /*Q*/, 17 /*R*/, 18 /*S*/, |
| 19 /*T*/, 20 /*U*/, 21 /*V*/, 22 /*W*/, 23 /*X*/, 24 /*Y*/, 25 /*Z*/, |
| -1, -1, -1, -1, 63 /*_*/, -1, 26 /*a*/, |
| 27 /*b*/, 28 /*c*/, 29 /*d*/, 30 /*e*/, 31 /*f*/, 32 /*g*/, 33 /*h*/, |
| 34 /*i*/, 35 /*j*/, 36 /*k*/, 37 /*l*/, 38 /*m*/, 39 /*n*/, 40 /*o*/, |
| 41 /*p*/, 42 /*q*/, 43 /*r*/, 44 /*s*/, 45 /*t*/, 46 /*u*/, 47 /*v*/, |
| 48 /*w*/, 49 /*x*/, 50 /*y*/, 51 /*z*/, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1, -1, -1, -1, |
| -1, -1, -1, -1}; |
| |
| /* Sign-extend return value so high bit will be set on any unexpected char. */ |
| return table[(unsigned)ch]; |
| } |
| |
| static char* jsondec_partialbase64(jsondec* d, const char* ptr, const char* end, |
| char* out) { |
| int32_t val = -1; |
| |
| switch (end - ptr) { |
| case 2: |
| val = jsondec_base64_tablelookup(ptr[0]) << 18 | |
| jsondec_base64_tablelookup(ptr[1]) << 12; |
| out[0] = val >> 16; |
| out += 1; |
| break; |
| case 3: |
| val = jsondec_base64_tablelookup(ptr[0]) << 18 | |
| jsondec_base64_tablelookup(ptr[1]) << 12 | |
| jsondec_base64_tablelookup(ptr[2]) << 6; |
| out[0] = val >> 16; |
| out[1] = (val >> 8) & 0xff; |
| out += 2; |
| break; |
| } |
| |
| if (val < 0) { |
| jsondec_err(d, "Corrupt base64"); |
| } |
| |
| return out; |
| } |
| |
| static size_t jsondec_base64(jsondec* d, upb_StringView str) { |
| /* We decode in place. This is safe because this is a new buffer (not |
| * aliasing the input) and because base64 decoding shrinks 4 bytes into 3. */ |
| char* out = (char*)str.data; |
| const char* ptr = str.data; |
| const char* end = ptr + str.size; |
| const char* end4 = ptr + (str.size & -4); /* Round down to multiple of 4. */ |
| |
| for (; ptr < end4; ptr += 4, out += 3) { |
| int val = jsondec_base64_tablelookup(ptr[0]) << 18 | |
| jsondec_base64_tablelookup(ptr[1]) << 12 | |
| jsondec_base64_tablelookup(ptr[2]) << 6 | |
| jsondec_base64_tablelookup(ptr[3]) << 0; |
| |
| if (val < 0) { |
| /* Junk chars or padding. Remove trailing padding, if any. */ |
| if (end - ptr == 4 && ptr[3] == '=') { |
| if (ptr[2] == '=') { |
| end -= 2; |
| } else { |
| end -= 1; |
| } |
| } |
| break; |
| } |
| |
| out[0] = val >> 16; |
| out[1] = (val >> 8) & 0xff; |
| out[2] = val & 0xff; |
| } |
| |
| if (ptr < end) { |
| /* Process remaining chars. We do not require padding. */ |
| out = jsondec_partialbase64(d, ptr, end, out); |
| } |
| |
| return out - str.data; |
| } |
| |
| /* Low-level integer parsing **************************************************/ |
| |
| static const char* jsondec_buftouint64(jsondec* d, const char* ptr, |
| const char* end, uint64_t* val) { |
| const char* out = upb_BufToUint64(ptr, end, val); |
| if (!out) jsondec_err(d, "Integer overflow"); |
| return out; |
| } |
| |
| static const char* jsondec_buftoint64(jsondec* d, const char* ptr, |
| const char* end, int64_t* val, |
| bool* is_neg) { |
| const char* out = upb_BufToInt64(ptr, end, val, is_neg); |
| if (!out) jsondec_err(d, "Integer overflow"); |
| return out; |
| } |
| |
| static uint64_t jsondec_strtouint64(jsondec* d, upb_StringView str) { |
| const char* end = str.data + str.size; |
| uint64_t ret; |
| if (jsondec_buftouint64(d, str.data, end, &ret) != end) { |
| jsondec_err(d, "Non-number characters in quoted integer"); |
| } |
| return ret; |
| } |
| |
| static int64_t jsondec_strtoint64(jsondec* d, upb_StringView str) { |
| const char* end = str.data + str.size; |
| int64_t ret; |
| if (jsondec_buftoint64(d, str.data, end, &ret, NULL) != end) { |
| jsondec_err(d, "Non-number characters in quoted integer"); |
| } |
| return ret; |
| } |
| |
| /* Primitive value types ******************************************************/ |
| |
| /* Parse INT32 or INT64 value. */ |
| static upb_MessageValue jsondec_int(jsondec* d, const upb_FieldDef* f) { |
| upb_MessageValue val; |
| |
| switch (jsondec_peek(d)) { |
| case JD_NUMBER: { |
| double dbl = jsondec_number(d); |
| if (dbl > 9223372036854774784.0 || dbl < -9223372036854775808.0) { |
| jsondec_err(d, "JSON number is out of range."); |
| } |
| val.int64_val = dbl; /* must be guarded, overflow here is UB */ |
| if (val.int64_val != dbl) { |
| jsondec_errf(d, "JSON number was not integral (%f != %" PRId64 ")", dbl, |
| val.int64_val); |
| } |
| break; |
| } |
| case JD_STRING: { |
| upb_StringView str = jsondec_string(d); |
| val.int64_val = jsondec_strtoint64(d, str); |
| break; |
| } |
| default: |
| jsondec_err(d, "Expected number or string"); |
| } |
| |
| if (upb_FieldDef_CType(f) == kUpb_CType_Int32 || |
| upb_FieldDef_CType(f) == kUpb_CType_Enum) { |
| if (val.int64_val > INT32_MAX || val.int64_val < INT32_MIN) { |
| jsondec_err(d, "Integer out of range."); |
| } |
| val.int32_val = (int32_t)val.int64_val; |
| } |
| |
| return val; |
| } |
| |
| /* Parse UINT32 or UINT64 value. */ |
| static upb_MessageValue jsondec_uint(jsondec* d, const upb_FieldDef* f) { |
| upb_MessageValue val; |
| |
| switch (jsondec_peek(d)) { |
| case JD_NUMBER: { |
| double dbl = jsondec_number(d); |
| if (dbl > 18446744073709549568.0 || dbl < 0) { |
| jsondec_err(d, "JSON number is out of range."); |
| } |
| val.uint64_val = dbl; /* must be guarded, overflow here is UB */ |
| if (val.uint64_val != dbl) { |
| jsondec_errf(d, "JSON number was not integral (%f != %" PRIu64 ")", dbl, |
| val.uint64_val); |
| } |
| break; |
| } |
| case JD_STRING: { |
| upb_StringView str = jsondec_string(d); |
| val.uint64_val = jsondec_strtouint64(d, str); |
| break; |
| } |
| default: |
| jsondec_err(d, "Expected number or string"); |
| } |
| |
| if (upb_FieldDef_CType(f) == kUpb_CType_UInt32) { |
| if (val.uint64_val > UINT32_MAX) { |
| jsondec_err(d, "Integer out of range."); |
| } |
| val.uint32_val = (uint32_t)val.uint64_val; |
| } |
| |
| return val; |
| } |
| |
| /* Parse DOUBLE or FLOAT value. */ |
| static upb_MessageValue jsondec_double(jsondec* d, const upb_FieldDef* f) { |
| upb_StringView str; |
| upb_MessageValue val; |
| |
| switch (jsondec_peek(d)) { |
| case JD_NUMBER: |
| val.double_val = jsondec_number(d); |
| break; |
| case JD_STRING: |
| str = jsondec_string(d); |
| 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); |
| } |
| break; |
| default: |
| jsondec_err(d, "Expected number or string"); |
| } |
| |
| if (upb_FieldDef_CType(f) == kUpb_CType_Float) { |
| float f = val.double_val; |
| if (val.double_val != INFINITY && val.double_val != -INFINITY) { |
| if (f == INFINITY || f == -INFINITY) jsondec_err(d, "Float out of range"); |
| } |
| val.float_val = f; |
| } |
| |
| return val; |
| } |
| |
| /* Parse STRING or BYTES value. */ |
| static upb_MessageValue jsondec_strfield(jsondec* d, const upb_FieldDef* f) { |
| upb_MessageValue val; |
| val.str_val = jsondec_string(d); |
| if (upb_FieldDef_CType(f) == kUpb_CType_Bytes) { |
| val.str_val.size = jsondec_base64(d, val.str_val); |
| } |
| return val; |
| } |
| |
| 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_JsonMessageValue val = {.ignore = false}; |
| if (ev) { |
| val.value.int32_val = upb_EnumValueDef_Number(ev); |
| } else { |
| if (d->options & upb_JsonDecode_IgnoreUnknown) { |
| val.ignore = true; |
| } else { |
| jsondec_errf(d, "Unknown enumerator: '" UPB_STRINGVIEW_FORMAT "'", |
| UPB_STRINGVIEW_ARGS(str)); |
| } |
| } |
| return val; |
| } |
| case JD_NULL: { |
| if (jsondec_isnullvalue(f)) { |
| upb_JsonMessageValue val = {.ignore = false}; |
| jsondec_null(d); |
| val.value.int32_val = 0; |
| return val; |
| } |
| } |
| /* Fallthrough. */ |
| default: |
| return (upb_JsonMessageValue){.value = jsondec_int(d, f), |
| .ignore = false}; |
| } |
| } |
| |
| static upb_MessageValue jsondec_bool(jsondec* d, const upb_FieldDef* f) { |
| bool is_map_key = upb_FieldDef_Number(f) == 1 && |
| upb_MessageDef_IsMapEntry(upb_FieldDef_ContainingType(f)); |
| upb_MessageValue val; |
| |
| if (is_map_key) { |
| upb_StringView str = jsondec_string(d); |
| if (jsondec_streql(str, "true")) { |
| val.bool_val = true; |
| } else if (jsondec_streql(str, "false")) { |
| val.bool_val = false; |
| } else { |
| jsondec_err(d, "Invalid boolean map key"); |
| } |
| } else { |
| switch (jsondec_peek(d)) { |
| case JD_TRUE: |
| val.bool_val = true; |
| jsondec_true(d); |
| break; |
| case JD_FALSE: |
| val.bool_val = false; |
| jsondec_false(d); |
| break; |
| default: |
| jsondec_err(d, "Expected true or false"); |
| } |
| } |
| |
| return val; |
| } |
| |
| /* 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_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); |
| const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(entry, 2); |
| |
| jsondec_objstart(d); |
| while (jsondec_objnext(d)) { |
| 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); |
| 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 { |
| jsondec_wellknown(d, msg, m); |
| } |
| } |
| |
| static upb_MessageValue jsondec_msg(jsondec* d, const upb_FieldDef* f) { |
| const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f); |
| const upb_MiniTable* layout = upb_MessageDef_MiniTable(m); |
| upb_Message* msg = upb_Message_New(layout, d->arena); |
| upb_MessageValue val; |
| |
| jsondec_tomsg(d, msg, m); |
| val.msg_val = msg; |
| return val; |
| } |
| |
| 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; |
| |
| name = jsondec_string(d); |
| jsondec_entrysep(d); |
| |
| if (name.size >= 2 && name.data[0] == '[' && |
| name.data[name.size - 1] == ']') { |
| f = upb_DefPool_FindExtensionByNameWithSize(d->symtab, name.data + 1, |
| name.size - 2); |
| if (f && upb_FieldDef_ContainingType(f) != m) { |
| jsondec_errf( |
| d, "Extension %s extends message %s, but was seen in message %s", |
| upb_FieldDef_FullName(f), |
| upb_MessageDef_FullName(upb_FieldDef_ContainingType(f)), |
| upb_MessageDef_FullName(m)); |
| } |
| } else { |
| f = upb_MessageDef_FindByJsonNameWithSize(m, name.data, name.size); |
| } |
| |
| if (!f) { |
| if ((d->options & upb_JsonDecode_IgnoreUnknown) == 0) { |
| jsondec_errf(d, "No such field: " UPB_STRINGVIEW_FORMAT, |
| UPB_STRINGVIEW_ARGS(name)); |
| } |
| jsondec_skipval(d); |
| return; |
| } |
| |
| if (jsondec_peek(d) == JD_NULL && !jsondec_isvalue(f)) { |
| /* JSON "null" indicates a default value, so no need to set anything. */ |
| jsondec_null(d); |
| return; |
| } |
| |
| if (upb_FieldDef_RealContainingOneof(f) && |
| upb_Message_WhichOneofByDef(msg, upb_FieldDef_ContainingOneof(f))) { |
| jsondec_err(d, "More than one field for this oneof."); |
| } |
| |
| preserved = d->debug_field; |
| d->debug_field = f; |
| |
| if (upb_FieldDef_IsMap(f)) { |
| jsondec_map(d, msg, f); |
| } else if (upb_FieldDef_IsRepeated(f)) { |
| jsondec_array(d, msg, f); |
| } else if (upb_FieldDef_IsSubMessage(f)) { |
| upb_Message* submsg = upb_Message_Mutable(msg, f, d->arena).msg; |
| const upb_MessageDef* subm = upb_FieldDef_MessageSubDef(f); |
| jsondec_tomsg(d, submsg, subm); |
| } else { |
| upb_JsonMessageValue val = jsondec_value(d, f); |
| if (!val.ignore) { |
| upb_Message_SetFieldByDef(msg, f, val.value, d->arena); |
| } |
| } |
| |
| d->debug_field = preserved; |
| } |
| |
| 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); |
| } |
| jsondec_objend(d); |
| } |
| |
| 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); |
| case kUpb_CType_Float: |
| case kUpb_CType_Double: |
| return jsondec_double(d, f); |
| case kUpb_CType_UInt32: |
| case kUpb_CType_UInt64: |
| return jsondec_uint(d, f); |
| case kUpb_CType_Int32: |
| case kUpb_CType_Int64: |
| return jsondec_int(d, f); |
| case kUpb_CType_String: |
| case kUpb_CType_Bytes: |
| return jsondec_strfield(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, |
| const char* after) { |
| uint64_t val; |
| const char* p = *ptr; |
| const char* end = p + digits; |
| size_t after_len = after ? strlen(after) : 0; |
| |
| UPB_ASSERT(digits <= 9); /* int can't overflow. */ |
| |
| if (jsondec_buftouint64(d, p, end, &val) != end || |
| (after_len && memcmp(end, after, after_len) != 0)) { |
| jsondec_err(d, "Malformed timestamp"); |
| } |
| |
| UPB_ASSERT(val < INT_MAX); |
| |
| *ptr = end + after_len; |
| return (int)val; |
| } |
| |
| static int jsondec_nanos(jsondec* d, const char** ptr, const char* end) { |
| uint64_t nanos = 0; |
| const char* p = *ptr; |
| |
| if (p != end && *p == '.') { |
| const char* nano_end = jsondec_buftouint64(d, p + 1, end, &nanos); |
| int digits = (int)(nano_end - p - 1); |
| int exp_lg10 = 9 - digits; |
| if (digits > 9) { |
| jsondec_err(d, "Too many digits for partial seconds"); |
| } |
| while (exp_lg10--) nanos *= 10; |
| *ptr = nano_end; |
| } |
| |
| UPB_ASSERT(nanos < INT_MAX); |
| |
| return (int)nanos; |
| } |
| |
| /* jsondec_epochdays(1970, 1, 1) == 1970-01-01 == 0. */ |
| int jsondec_epochdays(int y, int m, int d) { |
| const uint32_t year_base = 4800; /* Before min year, multiple of 400. */ |
| const uint32_t m_adj = m - 3; /* March-based month. */ |
| const uint32_t carry = m_adj > (uint32_t)m ? 1 : 0; |
| const uint32_t adjust = carry ? 12 : 0; |
| const uint32_t y_adj = y + year_base - carry; |
| const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048; |
| const uint32_t leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400; |
| return y_adj * 365 + leap_days + month_days + (d - 1) - 2472632; |
| } |
| |
| static int64_t jsondec_unixtime(int y, int m, int d, int h, int min, int s) { |
| return (int64_t)jsondec_epochdays(y, m, d) * 86400 + h * 3600 + min * 60 + 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); |
| const char* ptr = str.data; |
| const char* end = ptr + str.size; |
| |
| if (str.size < 20) goto malformed; |
| |
| { |
| /* 1972-01-01T01:00:00 */ |
| int year = jsondec_tsdigits(d, &ptr, 4, "-"); |
| int mon = jsondec_tsdigits(d, &ptr, 2, "-"); |
| int day = jsondec_tsdigits(d, &ptr, 2, "T"); |
| int hour = jsondec_tsdigits(d, &ptr, 2, ":"); |
| int min = jsondec_tsdigits(d, &ptr, 2, ":"); |
| int sec = jsondec_tsdigits(d, &ptr, 2, NULL); |
| |
| seconds.int64_val = jsondec_unixtime(year, mon, day, hour, min, sec); |
| } |
| |
| nanos.int32_val = jsondec_nanos(d, &ptr, end); |
| |
| { |
| /* [+-]08:00 or Z */ |
| int ofs_hour = 0; |
| int ofs_min = 0; |
| bool neg = false; |
| |
| if (ptr == end) goto malformed; |
| |
| switch (*ptr++) { |
| case '-': |
| neg = true; |
| /* fallthrough */ |
| case '+': |
| if ((end - ptr) != 5) goto malformed; |
| ofs_hour = jsondec_tsdigits(d, &ptr, 2, ":"); |
| ofs_min = jsondec_tsdigits(d, &ptr, 2, NULL); |
| ofs_min = ((ofs_hour * 60) + ofs_min) * 60; |
| seconds.int64_val += (neg ? ofs_min : -ofs_min); |
| break; |
| case 'Z': |
| if (ptr != end) goto malformed; |
| break; |
| default: |
| goto malformed; |
| } |
| } |
| |
| if (seconds.int64_val < -62135596800) { |
| jsondec_err(d, "Timestamp out of range"); |
| } |
| |
| upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 1), |
| seconds, d->arena); |
| upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 2), nanos, |
| d->arena); |
| return; |
| |
| malformed: |
| jsondec_err(d, "Malformed timestamp"); |
| } |
| |
| 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); |
| const char* ptr = str.data; |
| const char* end = ptr + str.size; |
| const int64_t max = (uint64_t)3652500 * 86400; |
| bool neg = false; |
| |
| /* "3.000000001s", "3s", etc. */ |
| ptr = jsondec_buftoint64(d, ptr, end, &seconds.int64_val, &neg); |
| nanos.int32_val = jsondec_nanos(d, &ptr, end); |
| |
| if (end - ptr != 1 || *ptr != 's') { |
| jsondec_err(d, "Malformed duration"); |
| } |
| |
| if (seconds.int64_val < -max || seconds.int64_val > max) { |
| jsondec_err(d, "Duration out of range"); |
| } |
| |
| if (neg) { |
| nanos.int32_val = -nanos.int32_val; |
| } |
| |
| upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 1), |
| seconds, d->arena); |
| upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 2), nanos, |
| d->arena); |
| } |
| |
| 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); |
| upb_Array* values = upb_Message_Mutable(msg, values_f, d->arena).array; |
| |
| jsondec_arrstart(d); |
| while (jsondec_arrnext(d)) { |
| upb_Message* value_msg = upb_Message_New(value_layout, d->arena); |
| upb_MessageValue value; |
| value.msg_val = value_msg; |
| upb_Array_Append(values, value, d->arena); |
| jsondec_wellknownvalue(d, value_msg, value_m); |
| } |
| jsondec_arrend(d); |
| } |
| |
| 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); |
| const upb_MessageDef* value_m = upb_FieldDef_MessageSubDef(value_f); |
| const upb_MiniTable* value_layout = upb_MessageDef_MiniTable(value_m); |
| upb_Map* fields = upb_Message_Mutable(msg, fields_f, d->arena).map; |
| |
| jsondec_objstart(d); |
| while (jsondec_objnext(d)) { |
| upb_MessageValue key, value; |
| upb_Message* value_msg = upb_Message_New(value_layout, d->arena); |
| key.str_val = jsondec_string(d); |
| value.msg_val = value_msg; |
| upb_Map_Set(fields, key, value, d->arena); |
| jsondec_entrysep(d); |
| jsondec_wellknownvalue(d, value_msg, value_m); |
| } |
| jsondec_objend(d); |
| } |
| |
| 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; |
| |
| switch (jsondec_peek(d)) { |
| case JD_NUMBER: |
| /* double number_value = 2; */ |
| f = upb_MessageDef_FindFieldByNumber(m, 2); |
| val.double_val = jsondec_number(d); |
| break; |
| case JD_STRING: |
| /* string string_value = 3; */ |
| f = upb_MessageDef_FindFieldByNumber(m, 3); |
| val.str_val = jsondec_string(d); |
| break; |
| case JD_FALSE: |
| /* bool bool_value = 4; */ |
| f = upb_MessageDef_FindFieldByNumber(m, 4); |
| val.bool_val = false; |
| jsondec_false(d); |
| break; |
| case JD_TRUE: |
| /* bool bool_value = 4; */ |
| f = upb_MessageDef_FindFieldByNumber(m, 4); |
| val.bool_val = true; |
| jsondec_true(d); |
| break; |
| case JD_NULL: |
| /* NullValue null_value = 1; */ |
| f = upb_MessageDef_FindFieldByNumber(m, 1); |
| val.int32_val = 0; |
| jsondec_null(d); |
| break; |
| /* Note: these cases return, because upb_Message_Mutable() is enough. */ |
| case JD_OBJECT: |
| /* Struct struct_value = 5; */ |
| f = upb_MessageDef_FindFieldByNumber(m, 5); |
| submsg = upb_Message_Mutable(msg, f, d->arena).msg; |
| jsondec_struct(d, submsg, upb_FieldDef_MessageSubDef(f)); |
| return; |
| case JD_ARRAY: |
| /* ListValue list_value = 6; */ |
| f = upb_MessageDef_FindFieldByNumber(m, 6); |
| submsg = upb_Message_Mutable(msg, f, d->arena).msg; |
| jsondec_listvalue(d, submsg, upb_FieldDef_MessageSubDef(f)); |
| return; |
| default: |
| UPB_UNREACHABLE(); |
| } |
| |
| upb_Message_SetFieldByDef(msg, f, val, d->arena); |
| } |
| |
| static upb_StringView jsondec_mask(jsondec* d, const char* buf, |
| const char* end) { |
| /* FieldMask fields grow due to inserted '_' characters, so we can't do the |
| * transform in place. */ |
| const char* ptr = buf; |
| upb_StringView ret; |
| char* out; |
| |
| ret.size = end - ptr; |
| while (ptr < end) { |
| ret.size += (*ptr >= 'A' && *ptr <= 'Z'); |
| ptr++; |
| } |
| |
| out = upb_Arena_Malloc(d->arena, ret.size); |
| ptr = buf; |
| ret.data = out; |
| |
| while (ptr < end) { |
| char ch = *ptr++; |
| if (ch >= 'A' && ch <= 'Z') { |
| *out++ = '_'; |
| *out++ = ch + 32; |
| } else if (ch == '_') { |
| jsondec_err(d, "field mask may not contain '_'"); |
| } else { |
| *out++ = ch; |
| } |
| } |
| |
| return ret; |
| } |
| |
| 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; |
| upb_StringView str = jsondec_string(d); |
| const char* ptr = str.data; |
| const char* end = ptr + str.size; |
| upb_MessageValue val; |
| |
| while (ptr < end) { |
| const char* elem_end = memchr(ptr, ',', end - ptr); |
| if (elem_end) { |
| val.str_val = jsondec_mask(d, ptr, elem_end); |
| ptr = elem_end + 1; |
| } else { |
| val.str_val = jsondec_mask(d, ptr, end); |
| ptr = end; |
| } |
| upb_Array_Append(arr, val, d->arena); |
| } |
| } |
| |
| 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. */ |
| jsondec_field(d, msg, m); |
| } else { |
| /* For well-known types: {"@type": "[well-known type]", "value": <X>} |
| * where <X> is whatever encoding the WKT normally uses. */ |
| upb_StringView str = jsondec_string(d); |
| jsondec_entrysep(d); |
| if (!jsondec_streql(str, "value")) { |
| jsondec_err(d, "Key for well-known type must be 'value'"); |
| } |
| jsondec_wellknown(d, msg, m); |
| } |
| } |
| |
| 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); |
| const char* end = type_url.data + type_url.size; |
| const char* ptr = end; |
| upb_MessageValue val; |
| |
| val.str_val = type_url; |
| upb_Message_SetFieldByDef(msg, type_url_f, val, d->arena); |
| |
| /* Find message name after the last '/' */ |
| while (ptr > type_url.data && *--ptr != '/') { |
| } |
| |
| if (ptr == type_url.data || ptr == end) { |
| jsondec_err(d, "Type url must have at least one '/' and non-empty host"); |
| } |
| |
| ptr++; |
| type_m = upb_DefPool_FindMessageByNameWithSize(d->symtab, ptr, end - ptr); |
| |
| if (!type_m) { |
| jsondec_err(d, "Type was not found"); |
| } |
| |
| return type_m; |
| } |
| |
| 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); |
| upb_Message* any_msg; |
| const upb_MessageDef* any_m = NULL; |
| const char* pre_type_data = NULL; |
| const char* pre_type_end = NULL; |
| upb_MessageValue encoded; |
| |
| jsondec_objstart(d); |
| |
| /* Scan looking for "@type", which is not necessarily first. */ |
| while (!any_m && jsondec_objnext(d)) { |
| const char* start = d->ptr; |
| upb_StringView name = jsondec_string(d); |
| jsondec_entrysep(d); |
| if (jsondec_streql(name, "@type")) { |
| any_m = jsondec_typeurl(d, msg, m); |
| if (pre_type_data) { |
| pre_type_end = start; |
| while (*pre_type_end != ',') pre_type_end--; |
| } |
| } else { |
| if (!pre_type_data) pre_type_data = start; |
| jsondec_skipval(d); |
| } |
| } |
| |
| if (!any_m) { |
| jsondec_err(d, "Any object didn't contain a '@type' field"); |
| } |
| |
| const upb_MiniTable* any_layout = upb_MessageDef_MiniTable(any_m); |
| any_msg = upb_Message_New(any_layout, d->arena); |
| |
| if (pre_type_data) { |
| size_t len = pre_type_end - pre_type_data + 1; |
| char* tmp = upb_Arena_Malloc(d->arena, len); |
| const char* saved_ptr = d->ptr; |
| const char* saved_end = d->end; |
| memcpy(tmp, pre_type_data, len - 1); |
| tmp[len - 1] = '}'; |
| d->ptr = tmp; |
| d->end = tmp + len; |
| d->is_first = true; |
| while (jsondec_objnext(d)) { |
| jsondec_anyfield(d, any_msg, any_m); |
| } |
| d->ptr = saved_ptr; |
| d->end = saved_end; |
| } |
| |
| while (jsondec_objnext(d)) { |
| jsondec_anyfield(d, any_msg, any_m); |
| } |
| |
| jsondec_objend(d); |
| |
| upb_EncodeStatus status = |
| upb_Encode(any_msg, upb_MessageDef_MiniTable(any_m), 0, d->arena, |
| (char**)&encoded.str_val.data, &encoded.str_val.size); |
| // TODO: We should fail gracefully here on a bad return status. |
| UPB_ASSERT(status == kUpb_EncodeStatus_Ok); |
| upb_Message_SetFieldByDef(msg, value_f, encoded, d->arena); |
| } |
| |
| 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_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); |
| break; |
| case kUpb_WellKnown_FieldMask: |
| jsondec_fieldmask(d, msg, m); |
| break; |
| case kUpb_WellKnown_Duration: |
| jsondec_duration(d, msg, m); |
| break; |
| case kUpb_WellKnown_Timestamp: |
| jsondec_timestamp(d, msg, m); |
| break; |
| case kUpb_WellKnown_Value: |
| jsondec_wellknownvalue(d, msg, m); |
| break; |
| case kUpb_WellKnown_ListValue: |
| jsondec_listvalue(d, msg, m); |
| break; |
| case kUpb_WellKnown_Struct: |
| jsondec_struct(d, msg, m); |
| break; |
| case kUpb_WellKnown_DoubleValue: |
| case kUpb_WellKnown_FloatValue: |
| case kUpb_WellKnown_Int64Value: |
| case kUpb_WellKnown_UInt64Value: |
| case kUpb_WellKnown_Int32Value: |
| case kUpb_WellKnown_UInt32Value: |
| case kUpb_WellKnown_StringValue: |
| case kUpb_WellKnown_BytesValue: |
| case kUpb_WellKnown_BoolValue: |
| jsondec_wrapper(d, msg, m); |
| break; |
| default: |
| UPB_UNREACHABLE(); |
| } |
| } |
| |
| static bool 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 false; |
| |
| jsondec_tomsg(d, msg, m); |
| |
| // Consume any trailing whitespace before checking if we read the entire |
| // input. |
| jsondec_consumews(d); |
| |
| if (d->ptr == d->end) { |
| return true; |
| } else { |
| jsondec_seterrmsg(d, "unexpected trailing characters"); |
| return false; |
| } |
| } |
| |
| 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) { |
| UPB_ASSERT(!upb_Message_IsFrozen(msg)); |
| jsondec d; |
| |
| if (size == 0) return true; |
| |
| d.ptr = buf; |
| d.end = buf + size; |
| d.arena = arena; |
| d.symtab = symtab; |
| d.status = status; |
| d.options = options; |
| d.depth = 64; |
| d.line = 1; |
| d.line_begin = d.ptr; |
| d.debug_field = NULL; |
| d.is_first = false; |
| |
| return upb_JsonDecoder_Decode(&d, msg, m); |
| } |
| |
| |
| #include <ctype.h> |
| #include <float.h> |
| #include <inttypes.h> |
| #include <math.h> |
| #include <stdarg.h> |
| #include <string.h> |
| |
| |
| // Must be last. |
| |
| typedef struct { |
| char *buf, *ptr, *end; |
| size_t overflow; |
| int indent_depth; |
| int options; |
| const upb_DefPool* ext_pool; |
| jmp_buf err; |
| upb_Status* status; |
| upb_Arena* arena; |
| } jsonenc; |
| |
| static void jsonenc_msg(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m); |
| static void jsonenc_scalar(jsonenc* e, upb_MessageValue val, |
| const upb_FieldDef* f); |
| static void jsonenc_msgfield(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m); |
| static void jsonenc_msgfields(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m, bool first); |
| static void jsonenc_value(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m); |
| |
| UPB_NORETURN static void jsonenc_err(jsonenc* e, const char* msg) { |
| upb_Status_SetErrorMessage(e->status, msg); |
| UPB_LONGJMP(e->err, 1); |
| } |
| |
| UPB_PRINTF(2, 3) |
| UPB_NORETURN static void jsonenc_errf(jsonenc* e, const char* fmt, ...) { |
| va_list argp; |
| va_start(argp, fmt); |
| upb_Status_VSetErrorFormat(e->status, fmt, argp); |
| va_end(argp); |
| UPB_LONGJMP(e->err, 1); |
| } |
| |
| static upb_Arena* jsonenc_arena(jsonenc* e) { |
| /* Create lazily, since it's only needed for Any */ |
| if (!e->arena) { |
| e->arena = upb_Arena_New(); |
| } |
| return e->arena; |
| } |
| |
| static void jsonenc_putbytes(jsonenc* 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 jsonenc_putstr(jsonenc* e, const char* str) { |
| jsonenc_putbytes(e, str, strlen(str)); |
| } |
| |
| UPB_PRINTF(2, 3) |
| static void jsonenc_printf(jsonenc* 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 jsonenc_nanos(jsonenc* e, int32_t nanos) { |
| int digits = 9; |
| |
| if (nanos == 0) return; |
| if (nanos < 0 || nanos >= 1000000000) { |
| jsonenc_err(e, "error formatting timestamp as JSON: invalid nanos"); |
| } |
| |
| while (nanos % 1000 == 0) { |
| nanos /= 1000; |
| digits -= 3; |
| } |
| |
| jsonenc_printf(e, ".%.*" PRId32, digits, nanos); |
| } |
| |
| static void jsonenc_timestamp(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m) { |
| const upb_FieldDef* seconds_f = upb_MessageDef_FindFieldByNumber(m, 1); |
| const upb_FieldDef* nanos_f = upb_MessageDef_FindFieldByNumber(m, 2); |
| int64_t seconds = upb_Message_GetFieldByDef(msg, seconds_f).int64_val; |
| int32_t nanos = upb_Message_GetFieldByDef(msg, nanos_f).int32_val; |
| int L, N, I, J, K, hour, min, sec; |
| |
| if (seconds < -62135596800) { |
| jsonenc_err(e, |
| "error formatting timestamp as JSON: minimum acceptable value " |
| "is 0001-01-01T00:00:00Z"); |
| } else if (seconds > 253402300799) { |
| jsonenc_err(e, |
| "error formatting timestamp as JSON: maximum acceptable value " |
| "is 9999-12-31T23:59:59Z"); |
| } |
| |
| /* Julian Day -> Y/M/D, Algorithm from: |
| * Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for |
| * Processing Calendar Dates," Communications of the Association of |
| * Computing Machines, vol. 11 (1968), p. 657. */ |
| seconds += 62135596800; // Ensure seconds is positive. |
| L = (int)(seconds / 86400) - 719162 + 68569 + 2440588; |
| N = 4 * L / 146097; |
| L = L - (146097 * N + 3) / 4; |
| I = 4000 * (L + 1) / 1461001; |
| L = L - 1461 * I / 4 + 31; |
| J = 80 * L / 2447; |
| K = L - 2447 * J / 80; |
| L = J / 11; |
| J = J + 2 - 12 * L; |
| I = 100 * (N - 49) + I + L; |
| |
| sec = seconds % 60; |
| min = (seconds / 60) % 60; |
| hour = (seconds / 3600) % 24; |
| |
| jsonenc_printf(e, "\"%04d-%02d-%02dT%02d:%02d:%02d", I, J, K, hour, min, sec); |
| jsonenc_nanos(e, nanos); |
| jsonenc_putstr(e, "Z\""); |
| } |
| |
| static void jsonenc_duration(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m) { |
| const upb_FieldDef* seconds_f = upb_MessageDef_FindFieldByNumber(m, 1); |
| const upb_FieldDef* nanos_f = upb_MessageDef_FindFieldByNumber(m, 2); |
| int64_t seconds = upb_Message_GetFieldByDef(msg, seconds_f).int64_val; |
| int32_t nanos = upb_Message_GetFieldByDef(msg, nanos_f).int32_val; |
| bool negative = false; |
| |
| if (seconds > 315576000000 || seconds < -315576000000 || |
| (seconds != 0 && nanos != 0 && (seconds < 0) != (nanos < 0))) { |
| jsonenc_err(e, "bad duration"); |
| } |
| |
| if (seconds < 0) { |
| negative = true; |
| seconds = -seconds; |
| } |
| if (nanos < 0) { |
| negative = true; |
| nanos = -nanos; |
| } |
| |
| jsonenc_putstr(e, "\""); |
| if (negative) { |
| jsonenc_putstr(e, "-"); |
| } |
| jsonenc_printf(e, "%" PRId64, seconds); |
| jsonenc_nanos(e, nanos); |
| jsonenc_putstr(e, "s\""); |
| } |
| |
| static void jsonenc_enum(int32_t val, const upb_FieldDef* f, jsonenc* e) { |
| const upb_EnumDef* e_def = upb_FieldDef_EnumSubDef(f); |
| |
| if (strcmp(upb_EnumDef_FullName(e_def), "google.protobuf.NullValue") == 0) { |
| jsonenc_putstr(e, "null"); |
| } else { |
| const upb_EnumValueDef* ev = |
| (e->options & upb_JsonEncode_FormatEnumsAsIntegers) |
| ? NULL |
| : upb_EnumDef_FindValueByNumber(e_def, val); |
| |
| if (ev) { |
| jsonenc_printf(e, "\"%s\"", upb_EnumValueDef_Name(ev)); |
| } else { |
| jsonenc_printf(e, "%" PRId32, val); |
| } |
| } |
| } |
| |
| static void jsonenc_bytes(jsonenc* e, upb_StringView str) { |
| /* This is the regular base64, not the "web-safe" version. */ |
| static const char base64[] = |
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| const unsigned char* ptr = (unsigned char*)str.data; |
| const unsigned char* end = UPB_PTRADD(ptr, str.size); |
| char buf[4]; |
| |
| jsonenc_putstr(e, "\""); |
| |
| while (end - ptr >= 3) { |
| buf[0] = base64[ptr[0] >> 2]; |
| buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)]; |
| buf[2] = base64[((ptr[1] & 0xf) << 2) | (ptr[2] >> 6)]; |
| buf[3] = base64[ptr[2] & 0x3f]; |
| jsonenc_putbytes(e, buf, 4); |
| ptr += 3; |
| } |
| |
| switch (end - ptr) { |
| case 2: |
| buf[0] = base64[ptr[0] >> 2]; |
| buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)]; |
| buf[2] = base64[(ptr[1] & 0xf) << 2]; |
| buf[3] = '='; |
| jsonenc_putbytes(e, buf, 4); |
| break; |
| case 1: |
| buf[0] = base64[ptr[0] >> 2]; |
| buf[1] = base64[((ptr[0] & 0x3) << 4)]; |
| buf[2] = '='; |
| buf[3] = '='; |
| jsonenc_putbytes(e, buf, 4); |
| break; |
| } |
| |
| jsonenc_putstr(e, "\""); |
| } |
| |
| static void jsonenc_stringbody(jsonenc* e, upb_StringView str) { |
| const char* ptr = str.data; |
| const char* end = UPB_PTRADD(ptr, str.size); |
| |
| while (ptr < end) { |
| switch (*ptr) { |
| case '\n': |
| jsonenc_putstr(e, "\\n"); |
| break; |
| case '\r': |
| jsonenc_putstr(e, "\\r"); |
| break; |
| case '\t': |
| jsonenc_putstr(e, "\\t"); |
| break; |
| case '\"': |
| jsonenc_putstr(e, "\\\""); |
| break; |
| case '\f': |
| jsonenc_putstr(e, "\\f"); |
| break; |
| case '\b': |
| jsonenc_putstr(e, "\\b"); |
| break; |
| case '\\': |
| jsonenc_putstr(e, "\\\\"); |
| break; |
| default: |
| if ((uint8_t)*ptr < 0x20) { |
| jsonenc_printf(e, "\\u%04x", (int)(uint8_t)*ptr); |
| } else { |
| /* This could be a non-ASCII byte. We rely on the string being valid |
| * UTF-8. */ |
| jsonenc_putbytes(e, ptr, 1); |
| } |
| break; |
| } |
| ptr++; |
| } |
| } |
| |
| static void jsonenc_string(jsonenc* e, upb_StringView str) { |
| jsonenc_putstr(e, "\""); |
| jsonenc_stringbody(e, str); |
| jsonenc_putstr(e, "\""); |
| } |
| |
| static bool upb_JsonEncode_HandleSpecialDoubles(jsonenc* e, double val) { |
| if (val == INFINITY) { |
| jsonenc_putstr(e, "\"Infinity\""); |
| } else if (val == -INFINITY) { |
| jsonenc_putstr(e, "\"-Infinity\""); |
| } else if (val != val) { |
| jsonenc_putstr(e, "\"NaN\""); |
| } else { |
| return false; |
| } |
| return true; |
| } |
| |
| static void upb_JsonEncode_Double(jsonenc* e, double val) { |
| if (upb_JsonEncode_HandleSpecialDoubles(e, val)) return; |
| char buf[32]; |
| _upb_EncodeRoundTripDouble(val, buf, sizeof(buf)); |
| jsonenc_putstr(e, buf); |
| } |
| |
| static void upb_JsonEncode_Float(jsonenc* e, float val) { |
| if (upb_JsonEncode_HandleSpecialDoubles(e, val)) return; |
| char buf[32]; |
| _upb_EncodeRoundTripFloat(val, buf, sizeof(buf)); |
| jsonenc_putstr(e, buf); |
| } |
| |
| static void jsonenc_wrapper(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m) { |
| const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(m, 1); |
| upb_MessageValue val = upb_Message_GetFieldByDef(msg, val_f); |
| jsonenc_scalar(e, val, val_f); |
| } |
| |
| static const upb_MessageDef* jsonenc_getanymsg(jsonenc* e, |
| upb_StringView type_url) { |
| /* Find last '/', if any. */ |
| const char* end = type_url.data + type_url.size; |
| const char* ptr = end; |
| const upb_MessageDef* ret; |
| |
| if (!e->ext_pool) { |
| jsonenc_err(e, "Tried to encode Any, but no symtab was provided"); |
| } |
| |
| if (type_url.size == 0) goto badurl; |
| |
| while (true) { |
| if (--ptr == type_url.data) { |
| /* Type URL must contain at least one '/', with host before. */ |
| goto badurl; |
| } |
| if (*ptr == '/') { |
| ptr++; |
| break; |
| } |
| } |
| |
| ret = upb_DefPool_FindMessageByNameWithSize(e->ext_pool, ptr, end - ptr); |
| |
| if (!ret) { |
| jsonenc_errf(e, "Couldn't find Any type: %.*s", (int)(end - ptr), ptr); |
| } |
| |
| return ret; |
| |
| badurl: |
| jsonenc_errf(e, "Bad type URL: " UPB_STRINGVIEW_FORMAT, |
| UPB_STRINGVIEW_ARGS(type_url)); |
| } |
| |
| static void jsonenc_any(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m) { |
| const upb_FieldDef* type_url_f = upb_MessageDef_FindFieldByNumber(m, 1); |
| const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(m, 2); |
| upb_StringView type_url = upb_Message_GetFieldByDef(msg, type_url_f).str_val; |
| upb_StringView value = upb_Message_GetFieldByDef(msg, value_f).str_val; |
| const upb_MessageDef* any_m = jsonenc_getanymsg(e, type_url); |
| const upb_MiniTable* any_layout = upb_MessageDef_MiniTable(any_m); |
| upb_Arena* arena = jsonenc_arena(e); |
| upb_Message* any = upb_Message_New(any_layout, arena); |
| |
| if (upb_Decode(value.data, value.size, any, any_layout, NULL, 0, arena) != |
| kUpb_DecodeStatus_Ok) { |
| jsonenc_err(e, "Error decoding message in Any"); |
| } |
| |
| jsonenc_putstr(e, "{\"@type\":"); |
| jsonenc_string(e, type_url); |
| |
| if (upb_MessageDef_WellKnownType(any_m) == kUpb_WellKnown_Unspecified) { |
| /* Regular messages: {"@type": "...","foo": 1, "bar": 2} */ |
| jsonenc_msgfields(e, any, any_m, false); |
| } else { |
| /* Well-known type: {"@type": "...","value": <well-known encoding>} */ |
| jsonenc_putstr(e, ",\"value\":"); |
| jsonenc_msgfield(e, any, any_m); |
| } |
| |
| jsonenc_putstr(e, "}"); |
| } |
| |
| static void jsonenc_putsep(jsonenc* e, const char* str, bool* first) { |
| if (*first) { |
| *first = false; |
| } else { |
| jsonenc_putstr(e, str); |
| } |
| } |
| |
| static void jsonenc_fieldpath(jsonenc* e, upb_StringView path) { |
| const char* ptr = path.data; |
| const char* end = ptr + path.size; |
| |
| while (ptr < end) { |
| char ch = *ptr; |
| |
| if (ch >= 'A' && ch <= 'Z') { |
| jsonenc_err(e, "Field mask element may not have upper-case letter."); |
| } else if (ch == '_') { |
| if (ptr == end - 1 || *(ptr + 1) < 'a' || *(ptr + 1) > 'z') { |
| jsonenc_err(e, "Underscore must be followed by a lowercase letter."); |
| } |
| ch = *++ptr - 32; |
| } |
| |
| jsonenc_putbytes(e, &ch, 1); |
| ptr++; |
| } |
| } |
| |
| static void jsonenc_fieldmask(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m) { |
| const upb_FieldDef* paths_f = upb_MessageDef_FindFieldByNumber(m, 1); |
| const upb_Array* paths = upb_Message_GetFieldByDef(msg, paths_f).array_val; |
| bool first = true; |
| size_t i, n = 0; |
| |
| if (paths) n = upb_Array_Size(paths); |
| |
| jsonenc_putstr(e, "\""); |
| |
| for (i = 0; i < n; i++) { |
| jsonenc_putsep(e, ",", &first); |
| jsonenc_fieldpath(e, upb_Array_Get(paths, i).str_val); |
| } |
| |
| jsonenc_putstr(e, "\""); |
| } |
| |
| static void jsonenc_struct(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m) { |
| jsonenc_putstr(e, "{"); |
| |
| const upb_FieldDef* fields_f = upb_MessageDef_FindFieldByNumber(m, 1); |
| const upb_Map* fields = upb_Message_GetFieldByDef(msg, fields_f).map_val; |
| |
| if (fields) { |
| const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(fields_f); |
| const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(entry_m, 2); |
| |
| size_t iter = kUpb_Map_Begin; |
| bool first = true; |
| |
| upb_MessageValue key, val; |
| while (upb_Map_Next(fields, &key, &val, &iter)) { |
| jsonenc_putsep(e, ",", &first); |
| jsonenc_string(e, key.str_val); |
| jsonenc_putstr(e, ":"); |
| jsonenc_value(e, val.msg_val, upb_FieldDef_MessageSubDef(value_f)); |
| } |
| } |
| |
| jsonenc_putstr(e, "}"); |
| } |
| |
| static void jsonenc_listvalue(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m) { |
| const upb_FieldDef* values_f = upb_MessageDef_FindFieldByNumber(m, 1); |
| const upb_MessageDef* values_m = upb_FieldDef_MessageSubDef(values_f); |
| const upb_Array* values = upb_Message_GetFieldByDef(msg, values_f).array_val; |
| size_t i; |
| bool first = true; |
| |
| jsonenc_putstr(e, "["); |
| |
| if (values) { |
| const size_t size = upb_Array_Size(values); |
| for (i = 0; i < size; i++) { |
| upb_MessageValue elem = upb_Array_Get(values, i); |
| |
| jsonenc_putsep(e, ",", &first); |
| jsonenc_value(e, elem.msg_val, values_m); |
| } |
| } |
| |
| jsonenc_putstr(e, "]"); |
| } |
| |
| static void jsonenc_value(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m) { |
| /* TODO: do we want a reflection method to get oneof case? */ |
| size_t iter = kUpb_Message_Begin; |
| const upb_FieldDef* f; |
| upb_MessageValue val; |
| |
| if (!upb_Message_Next(msg, m, NULL, &f, &val, &iter)) { |
| jsonenc_err(e, "No value set in Value proto"); |
| } |
| |
| switch (upb_FieldDef_Number(f)) { |
| case 1: |
| jsonenc_putstr(e, "null"); |
| break; |
| case 2: |
| if (upb_JsonEncode_HandleSpecialDoubles(e, val.double_val)) { |
| jsonenc_err( |
| e, |
| "google.protobuf.Value cannot encode double values for " |
| "infinity or nan, because they would be parsed as a string"); |
| } |
| upb_JsonEncode_Double(e, val.double_val); |
| break; |
| case 3: |
| jsonenc_string(e, val.str_val); |
| break; |
| case 4: |
| jsonenc_putstr(e, val.bool_val ? "true" : "false"); |
| break; |
| case 5: |
| jsonenc_struct(e, val.msg_val, upb_FieldDef_MessageSubDef(f)); |
| break; |
| case 6: |
| jsonenc_listvalue(e, val.msg_val, upb_FieldDef_MessageSubDef(f)); |
| break; |
| } |
| } |
| |
| static void jsonenc_msgfield(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m) { |
| switch (upb_MessageDef_WellKnownType(m)) { |
| case kUpb_WellKnown_Unspecified: |
| jsonenc_msg(e, msg, m); |
| break; |
| case kUpb_WellKnown_Any: |
| jsonenc_any(e, msg, m); |
| break; |
| case kUpb_WellKnown_FieldMask: |
| jsonenc_fieldmask(e, msg, m); |
| break; |
| case kUpb_WellKnown_Duration: |
| jsonenc_duration(e, msg, m); |
| break; |
| case kUpb_WellKnown_Timestamp: |
| jsonenc_timestamp(e, msg, m); |
| break; |
| case kUpb_WellKnown_DoubleValue: |
| case kUpb_WellKnown_FloatValue: |
| case kUpb_WellKnown_Int64Value: |
| case kUpb_WellKnown_UInt64Value: |
| case kUpb_WellKnown_Int32Value: |
| case kUpb_WellKnown_UInt32Value: |
| case kUpb_WellKnown_StringValue: |
| case kUpb_WellKnown_BytesValue: |
| case kUpb_WellKnown_BoolValue: |
| jsonenc_wrapper(e, msg, m); |
| break; |
| case kUpb_WellKnown_Value: |
| jsonenc_value(e, msg, m); |
| break; |
| case kUpb_WellKnown_ListValue: |
| jsonenc_listvalue(e, msg, m); |
| break; |
| case kUpb_WellKnown_Struct: |
| jsonenc_struct(e, msg, m); |
| break; |
| } |
| } |
| |
| static void jsonenc_scalar(jsonenc* e, upb_MessageValue val, |
| const upb_FieldDef* f) { |
| switch (upb_FieldDef_CType(f)) { |
| case kUpb_CType_Bool: |
| jsonenc_putstr(e, val.bool_val ? "true" : "false"); |
| break; |
| case kUpb_CType_Float: |
| upb_JsonEncode_Float(e, val.float_val); |
| break; |
| case kUpb_CType_Double: |
| upb_JsonEncode_Double(e, val.double_val); |
| break; |
| case kUpb_CType_Int32: |
| jsonenc_printf(e, "%" PRId32, val.int32_val); |
| break; |
| case kUpb_CType_UInt32: |
| jsonenc_printf(e, "%" PRIu32, val.uint32_val); |
| break; |
| case kUpb_CType_Int64: |
| jsonenc_printf(e, "\"%" PRId64 "\"", val.int64_val); |
| break; |
| case kUpb_CType_UInt64: |
| jsonenc_printf(e, "\"%" PRIu64 "\"", val.uint64_val); |
| break; |
| case kUpb_CType_String: |
| jsonenc_string(e, val.str_val); |
| break; |
| case kUpb_CType_Bytes: |
| jsonenc_bytes(e, val.str_val); |
| break; |
| case kUpb_CType_Enum: |
| jsonenc_enum(val.int32_val, f, e); |
| break; |
| case kUpb_CType_Message: |
| jsonenc_msgfield(e, val.msg_val, upb_FieldDef_MessageSubDef(f)); |
| break; |
| } |
| } |
| |
| static void jsonenc_mapkey(jsonenc* e, upb_MessageValue val, |
| const upb_FieldDef* f) { |
| jsonenc_putstr(e, "\""); |
| |
| switch (upb_FieldDef_CType(f)) { |
| case kUpb_CType_Bool: |
| jsonenc_putstr(e, val.bool_val ? "true" : "false"); |
| break; |
| case kUpb_CType_Int32: |
| jsonenc_printf(e, "%" PRId32, val.int32_val); |
| break; |
| case kUpb_CType_UInt32: |
| jsonenc_printf(e, "%" PRIu32, val.uint32_val); |
| break; |
| case kUpb_CType_Int64: |
| jsonenc_printf(e, "%" PRId64, val.int64_val); |
| break; |
| case kUpb_CType_UInt64: |
| jsonenc_printf(e, "%" PRIu64, val.uint64_val); |
| break; |
| case kUpb_CType_String: |
| jsonenc_stringbody(e, val.str_val); |
| break; |
| default: |
| UPB_UNREACHABLE(); |
| } |
| |
| jsonenc_putstr(e, "\":"); |
| } |
| |
| static void jsonenc_array(jsonenc* e, const upb_Array* arr, |
| const upb_FieldDef* f) { |
| size_t i; |
| size_t size = arr ? upb_Array_Size(arr) : 0; |
| bool first = true; |
| |
| jsonenc_putstr(e, "["); |
| |
| for (i = 0; i < size; i++) { |
| jsonenc_putsep(e, ",", &first); |
| jsonenc_scalar(e, upb_Array_Get(arr, i), f); |
| } |
| |
| jsonenc_putstr(e, "]"); |
| } |
| |
| static void jsonenc_map(jsonenc* e, const upb_Map* map, const upb_FieldDef* f) { |
| jsonenc_putstr(e, "{"); |
| |
| const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f); |
| const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(entry, 1); |
| const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(entry, 2); |
| |
| if (map) { |
| size_t iter = kUpb_Map_Begin; |
| bool first = true; |
| |
| upb_MessageValue key, val; |
| while (upb_Map_Next(map, &key, &val, &iter)) { |
| jsonenc_putsep(e, ",", &first); |
| jsonenc_mapkey(e, key, key_f); |
| jsonenc_scalar(e, val, val_f); |
| } |
| } |
| |
| jsonenc_putstr(e, "}"); |
| } |
| |
| static void jsonenc_fieldval(jsonenc* e, const upb_FieldDef* f, |
| upb_MessageValue val, bool* first) { |
| const char* name; |
| |
| jsonenc_putsep(e, ",", first); |
| |
| if (upb_FieldDef_IsExtension(f)) { |
| // TODO: For MessageSet, I would have expected this to print the message |
| // name here, but Python doesn't appear to do this. We should do more |
| // research here about what various implementations do. |
| jsonenc_printf(e, "\"[%s]\":", upb_FieldDef_FullName(f)); |
| } else { |
| if (e->options & upb_JsonEncode_UseProtoNames) { |
| name = upb_FieldDef_Name(f); |
| } else { |
| name = upb_FieldDef_JsonName(f); |
| } |
| jsonenc_printf(e, "\"%s\":", name); |
| } |
| |
| if (upb_FieldDef_IsMap(f)) { |
| jsonenc_map(e, val.map_val, f); |
| } else if (upb_FieldDef_IsRepeated(f)) { |
| jsonenc_array(e, val.array_val, f); |
| } else { |
| jsonenc_scalar(e, val, f); |
| } |
| } |
| |
| static void jsonenc_msgfields(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m, bool first) { |
| upb_MessageValue val; |
| const upb_FieldDef* f; |
| |
| if (e->options & upb_JsonEncode_EmitDefaults) { |
| /* Iterate over all fields. */ |
| int i = 0; |
| int n = upb_MessageDef_FieldCount(m); |
| for (i = 0; i < n; i++) { |
| f = upb_MessageDef_Field(m, i); |
| if (!upb_FieldDef_HasPresence(f) || upb_Message_HasFieldByDef(msg, f)) { |
| jsonenc_fieldval(e, f, upb_Message_GetFieldByDef(msg, f), &first); |
| } |
| } |
| } else { |
| /* Iterate over non-empty fields. */ |
| size_t iter = kUpb_Message_Begin; |
| while (upb_Message_Next(msg, m, e->ext_pool, &f, &val, &iter)) { |
| jsonenc_fieldval(e, f, val, &first); |
| } |
| } |
| } |
| |
| static void jsonenc_msg(jsonenc* e, const upb_Message* msg, |
| const upb_MessageDef* m) { |
| jsonenc_putstr(e, "{"); |
| jsonenc_msgfields(e, msg, m, true); |
| jsonenc_putstr(e, "}"); |
| } |
| |
| static size_t jsonenc_nullz(jsonenc* 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; |
| } |
| |
| static size_t upb_JsonEncoder_Encode(jsonenc* const e, |
| const upb_Message* const msg, |
| const upb_MessageDef* const m, |
| const size_t size) { |
| if (UPB_SETJMP(e->err) != 0) return -1; |
| |
| jsonenc_msgfield(e, msg, m); |
| if (e->arena) upb_Arena_Free(e->arena); |
| return jsonenc_nullz(e, size); |
|