|
| 1 | +// Protocol Buffers - Google's data interchange format |
| 2 | +// Copyright 2025 Google LLC. All rights reserved. |
| 3 | +// |
| 4 | +// Use of this source code is governed by a BSD-style |
| 5 | +// license that can be found in the LICENSE file or at |
| 6 | +// https://developers.google.com/open-source/licenses/bsd |
| 7 | + |
| 8 | +#include <stdint.h> |
| 9 | + |
| 10 | +#include "upb/base/string_view.h" |
| 11 | +#include "upb/message/internal/message.h" |
| 12 | +#include "upb/message/message.h" |
| 13 | +#include "upb/wire/decode.h" |
| 14 | +#include "upb/wire/decode_fast/cardinality.h" |
| 15 | +#include "upb/wire/decode_fast/field_parsers.h" |
| 16 | +#include "upb/wire/eps_copy_input_stream.h" |
| 17 | +#include "upb/wire/internal/decoder.h" |
| 18 | +#include "upb/wire/reader.h" |
| 19 | + |
| 20 | +// Must be last. |
| 21 | +#include "upb/port/def.inc" |
| 22 | + |
| 23 | +UPB_FORCEINLINE bool _upb_FastDecoder_DoDecodeUnknown( |
| 24 | + struct upb_Decoder* d, const char** ptr, upb_Message* msg, intptr_t table, |
| 25 | + uint64_t hasbits, uint64_t data, upb_DecodeFastNext* ret) { |
| 26 | + const char* start = *ptr; |
| 27 | + |
| 28 | + // Important: if the branch is correctly predicted, the ptr incremen is |
| 29 | + // treated as constant and subsequent loads will not have a data dependency on |
| 30 | + // the branch. |
| 31 | + if (UPB_LIKELY((data & 0x80) == 0)) { |
| 32 | + *ptr += 1; |
| 33 | + // Ensure the field number is not 0. |
| 34 | + // Use bitwise op to only examine first byte minus additional tag data. |
| 35 | + if (UPB_UNLIKELY((data & 0xF8) == 0)) { |
| 36 | + return UPB_DECODEFAST_ERROR(d, kUpb_DecodeStatus_Malformed, ret); |
| 37 | + } |
| 38 | + } else if ((data & 0x8000) == 0) { |
| 39 | + *ptr += 2; |
| 40 | + // Ensure the field number is not 0. |
| 41 | + // Use bitwise op to limit to first two bytes, and ignore continuation bit & |
| 42 | + // additional tag data. |
| 43 | + if (UPB_UNLIKELY((data & 0x7f78) == 0)) { |
| 44 | + return UPB_DECODEFAST_ERROR(d, kUpb_DecodeStatus_Malformed, ret); |
| 45 | + } |
| 46 | + } else { |
| 47 | + // Tag >=2048 |
| 48 | + return UPB_DECODEFAST_EXIT(kUpb_DecodeFastNext_FallbackToMiniTable, ret); |
| 49 | + } |
| 50 | + |
| 51 | + uint32_t wire_type = data & 0x07; |
| 52 | + |
| 53 | + if (UPB_UNLIKELY(wire_type == kUpb_WireType_EndGroup || |
| 54 | + wire_type == kUpb_WireType_StartGroup)) { |
| 55 | + // FastDecoder doesn't handle group fields, but it can be used to decode a |
| 56 | + // message that is itself a group. When decoding a group, the end of the |
| 57 | + // message is marked by an EndGroup tag. Since EndGroup tags are not in |
| 58 | + // the MiniTable, they are routed to the unknown field handler. We must |
| 59 | + // intercept them here to properly terminate the message. |
| 60 | + return UPB_DECODEFAST_EXIT(kUpb_DecodeFastNext_FallbackToMiniTable, ret); |
| 61 | + } |
| 62 | + |
| 63 | + upb_EpsCopyInputStream_StartCapture(&d->input, start); |
| 64 | + |
| 65 | + switch (wire_type) { |
| 66 | + case kUpb_WireType_Varint: |
| 67 | + *ptr = upb_WireReader_SkipVarint(*ptr, &d->input); |
| 68 | + if (UPB_UNLIKELY(!ptr)) { |
| 69 | + return UPB_DECODEFAST_ERROR(d, kUpb_DecodeStatus_Malformed, ret); |
| 70 | + } |
| 71 | + break; |
| 72 | + case kUpb_WireType_32Bit: |
| 73 | + UPB_PRIVATE(upb_EpsCopyInputStream_ConsumeBytes)(&d->input, 4); |
| 74 | + *ptr += 4; |
| 75 | + break; |
| 76 | + case kUpb_WireType_64Bit: |
| 77 | + UPB_PRIVATE(upb_EpsCopyInputStream_ConsumeBytes)(&d->input, 8); |
| 78 | + *ptr += 8; |
| 79 | + break; |
| 80 | + case kUpb_WireType_Delimited: { |
| 81 | + int size; |
| 82 | + *ptr = upb_WireReader_ReadSize(*ptr, &size, &d->input); |
| 83 | + if (UPB_UNLIKELY(!ptr || !upb_EpsCopyInputStream_CheckSize(&d->input, |
| 84 | + *ptr, size))) { |
| 85 | + return UPB_DECODEFAST_ERROR(d, kUpb_DecodeStatus_Malformed, ret); |
| 86 | + } |
| 87 | + *ptr += size; |
| 88 | + break; |
| 89 | + } |
| 90 | + default: |
| 91 | + return UPB_DECODEFAST_ERROR(d, kUpb_DecodeStatus_Malformed, ret); |
| 92 | + } |
| 93 | + |
| 94 | + upb_StringView sv; |
| 95 | + if (UPB_UNLIKELY(!upb_EpsCopyInputStream_EndCapture(&d->input, *ptr, &sv))) { |
| 96 | + return UPB_DECODEFAST_ERROR(d, kUpb_DecodeStatus_Malformed, ret); |
| 97 | + } |
| 98 | + |
| 99 | + upb_AddUnknownMode mode = kUpb_AddUnknown_Copy; |
| 100 | + if (d->options & kUpb_DecodeOption_AliasString) { |
| 101 | + if (sv.data != d->input.buffer_start) { |
| 102 | + mode = kUpb_AddUnknown_AliasAllowMerge; |
| 103 | + } else { |
| 104 | + mode = kUpb_AddUnknown_Alias; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, sv.data, sv.size, &d->arena, |
| 109 | + mode)) { |
| 110 | + return UPB_DECODEFAST_ERROR(d, kUpb_DecodeStatus_OutOfMemory, ret); |
| 111 | + } |
| 112 | + |
| 113 | + data = 0; |
| 114 | + return true; |
| 115 | +} |
| 116 | + |
| 117 | +UPB_PRESERVE_NONE const char* _upb_FastDecoder_DecodeUnknown( |
| 118 | + struct upb_Decoder* d, const char* ptr, upb_Message* msg, intptr_t table, |
| 119 | + uint64_t hasbits, uint64_t data) { |
| 120 | + upb_DecodeFastNext ret = kUpb_DecodeFastNext_Dispatch; |
| 121 | + _upb_FastDecoder_DoDecodeUnknown(d, &ptr, msg, table, hasbits, data, &ret); |
| 122 | + UPB_DECODEFAST_NEXT(ret); |
| 123 | +} |
0 commit comments