1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 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 "upb/wire/reader.h"
9
10 #include <stddef.h>
11 #include <stdint.h>
12
13 #include "upb/wire/eps_copy_input_stream.h"
14 #include "upb/wire/types.h"
15
16 // Must be last.
17 #include "upb/port/def.inc"
18
UPB_PRIVATE(_upb_WireReader_LongVarint)19 UPB_NOINLINE UPB_PRIVATE(_upb_WireReader_LongVarint)
20 UPB_PRIVATE(_upb_WireReader_ReadLongVarint)(const char* ptr, uint64_t val) {
21 UPB_PRIVATE(_upb_WireReader_LongVarint) ret = {NULL, 0};
22 uint64_t byte;
23 for (int i = 1; i < 10; i++) {
24 byte = (uint8_t)ptr[i];
25 val += (byte - 1) << (i * 7);
26 if (!(byte & 0x80)) {
27 ret.ptr = ptr + i + 1;
28 ret.val = val;
29 return ret;
30 }
31 }
32 return ret;
33 }
34
UPB_PRIVATE(_upb_WireReader_SkipGroup)35 const char* UPB_PRIVATE(_upb_WireReader_SkipGroup)(
36 const char* ptr, uint32_t tag, int depth_limit,
37 upb_EpsCopyInputStream* stream) {
38 if (--depth_limit == 0) return NULL;
39 uint32_t end_group_tag = (tag & ~7ULL) | kUpb_WireType_EndGroup;
40 while (!upb_EpsCopyInputStream_IsDone(stream, &ptr)) {
41 uint32_t tag;
42 ptr = upb_WireReader_ReadTag(ptr, &tag);
43 if (!ptr) return NULL;
44 if (tag == end_group_tag) return ptr;
45 ptr = _upb_WireReader_SkipValue(ptr, tag, depth_limit, stream);
46 if (!ptr) return NULL;
47 }
48 return ptr;
49 }
50