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 #ifndef UPB_MINI_DESCRIPTOR_INTERNAL_DECODER_H_
9 #define UPB_MINI_DESCRIPTOR_INTERNAL_DECODER_H_
10
11 #include "upb/base/status.h"
12 #include "upb/mini_descriptor/internal/base92.h"
13
14 // Must be last.
15 #include "upb/port/def.inc"
16
17 // upb_MdDecoder: used internally for decoding MiniDescriptors for messages,
18 // extensions, and enums.
19 typedef struct {
20 const char* end;
21 upb_Status* status;
22 jmp_buf err;
23 } upb_MdDecoder;
24
25 UPB_PRINTF(2, 3)
upb_MdDecoder_ErrorJmp(upb_MdDecoder * d,const char * fmt,...)26 UPB_NORETURN UPB_INLINE void upb_MdDecoder_ErrorJmp(upb_MdDecoder* d,
27 const char* fmt, ...) {
28 if (d->status) {
29 va_list argp;
30 upb_Status_SetErrorMessage(d->status, "Error building mini table: ");
31 va_start(argp, fmt);
32 upb_Status_VAppendErrorFormat(d->status, fmt, argp);
33 va_end(argp);
34 }
35 UPB_LONGJMP(d->err, 1);
36 }
37
upb_MdDecoder_CheckOutOfMemory(upb_MdDecoder * d,const void * ptr)38 UPB_INLINE void upb_MdDecoder_CheckOutOfMemory(upb_MdDecoder* d,
39 const void* ptr) {
40 if (!ptr) upb_MdDecoder_ErrorJmp(d, "Out of memory");
41 }
42
upb_MdDecoder_DecodeBase92Varint(upb_MdDecoder * d,const char * ptr,char first_ch,uint8_t min,uint8_t max,uint32_t * out_val)43 UPB_INLINE const char* upb_MdDecoder_DecodeBase92Varint(
44 upb_MdDecoder* d, const char* ptr, char first_ch, uint8_t min, uint8_t max,
45 uint32_t* out_val) {
46 ptr = _upb_Base92_DecodeVarint(ptr, d->end, first_ch, min, max, out_val);
47 if (!ptr) upb_MdDecoder_ErrorJmp(d, "Overlong varint");
48 return ptr;
49 }
50
51 #include "upb/port/undef.inc"
52
53 #endif // UPB_MINI_DESCRIPTOR_INTERNAL_DECODER_H_
54