xref: /aosp_15_r20/external/grpc-grpc/third_party/upb/upb/mini_descriptor/internal/base92.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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_BASE92_H_
9 #define UPB_MINI_DESCRIPTOR_INTERNAL_BASE92_H_
10 
11 #include <stdint.h>
12 
13 #include "upb/base/internal/log2.h"
14 
15 // Must be last.
16 #include "upb/port/def.inc"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
_upb_ToBase92(int8_t ch)22 UPB_INLINE char _upb_ToBase92(int8_t ch) {
23   extern const char _kUpb_ToBase92[];
24   UPB_ASSERT(0 <= ch && ch < 92);
25   return _kUpb_ToBase92[ch];
26 }
27 
_upb_FromBase92(uint8_t ch)28 UPB_INLINE char _upb_FromBase92(uint8_t ch) {
29   extern const int8_t _kUpb_FromBase92[];
30   if (' ' > ch || ch > '~') return -1;
31   return _kUpb_FromBase92[ch - ' '];
32 }
33 
_upb_Base92_DecodeVarint(const char * ptr,const char * end,char first_ch,uint8_t min,uint8_t max,uint32_t * out_val)34 UPB_INLINE const char* _upb_Base92_DecodeVarint(const char* ptr,
35                                                 const char* end, char first_ch,
36                                                 uint8_t min, uint8_t max,
37                                                 uint32_t* out_val) {
38   uint32_t val = 0;
39   uint32_t shift = 0;
40   const int bits_per_char =
41       upb_Log2Ceiling(_upb_FromBase92(max) - _upb_FromBase92(min));
42   char ch = first_ch;
43   while (1) {
44     uint32_t bits = _upb_FromBase92(ch) - _upb_FromBase92(min);
45     val |= bits << shift;
46     if (ptr == end || *ptr < min || max < *ptr) {
47       *out_val = val;
48       UPB_ASSUME(ptr != NULL);
49       return ptr;
50     }
51     ch = *ptr++;
52     shift += bits_per_char;
53     if (shift >= 32) return NULL;
54   }
55 }
56 
57 #ifdef __cplusplus
58 } /* extern "C" */
59 #endif
60 
61 #include "upb/port/undef.inc"
62 
63 #endif  // UPB_MINI_DESCRIPTOR_INTERNAL_BASE92_H_
64