1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "upb/mini_table/common.h"
29 
30 #include <inttypes.h>
31 
32 #include "upb/mem/arena.h"
33 #include "upb/mini_table/common_internal.h"
34 
35 // Must be last.
36 #include "upb/port/def.inc"
37 
38 const char _kUpb_ToBase92[] = {
39     ' ', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/',
40     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=',
41     '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
42     'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
43     'Z', '[', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
44     'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
45     'w', 'x', 'y', 'z', '{', '|', '}', '~',
46 };
47 
48 const int8_t _kUpb_FromBase92[] = {
49     0,  1,  -1, 2,  3,  4,  5,  -1, 6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16,
50     17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
51     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
52     55, 56, 57, -1, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
53     73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
54 };
55 
upb_MiniTable_FindFieldByNumber(const upb_MiniTable * t,uint32_t number)56 const upb_MiniTableField* upb_MiniTable_FindFieldByNumber(
57     const upb_MiniTable* t, uint32_t number) {
58   const size_t i = ((size_t)number) - 1;  // 0 wraps to SIZE_MAX
59 
60   // Ideal case: index into dense fields
61   if (i < t->dense_below) {
62     UPB_ASSERT(t->fields[i].number == number);
63     return &t->fields[i];
64   }
65 
66   // Slow case: binary search
67   int lo = t->dense_below;
68   int hi = t->field_count - 1;
69   while (lo <= hi) {
70     int mid = (lo + hi) / 2;
71     uint32_t num = t->fields[mid].number;
72     if (num < number) {
73       lo = mid + 1;
74       continue;
75     }
76     if (num > number) {
77       hi = mid - 1;
78       continue;
79     }
80     return &t->fields[mid];
81   }
82   return NULL;
83 }
84 
upb_MiniTableField_Type(const upb_MiniTableField * field)85 upb_FieldType upb_MiniTableField_Type(const upb_MiniTableField* field) {
86   if (field->mode & kUpb_LabelFlags_IsAlternate) {
87     if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Int32) {
88       return kUpb_FieldType_Enum;
89     } else if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bytes) {
90       return kUpb_FieldType_String;
91     } else {
92       UPB_ASSERT(false);
93     }
94   }
95   return field->UPB_PRIVATE(descriptortype);
96 }
97 
upb_MiniTable_Is_Oneof(const upb_MiniTableField * f)98 static bool upb_MiniTable_Is_Oneof(const upb_MiniTableField* f) {
99   return f->presence < 0;
100 }
101 
upb_MiniTable_GetOneof(const upb_MiniTable * m,const upb_MiniTableField * f)102 const upb_MiniTableField* upb_MiniTable_GetOneof(const upb_MiniTable* m,
103                                                  const upb_MiniTableField* f) {
104   if (UPB_UNLIKELY(!upb_MiniTable_Is_Oneof(f))) {
105     return NULL;
106   }
107   const upb_MiniTableField* ptr = &m->fields[0];
108   const upb_MiniTableField* end = &m->fields[m->field_count];
109   while (++ptr < end) {
110     if (ptr->presence == (*f).presence) {
111       return ptr;
112     }
113   }
114   return NULL;
115 }
116 
upb_MiniTable_NextOneofField(const upb_MiniTable * m,const upb_MiniTableField ** f)117 bool upb_MiniTable_NextOneofField(const upb_MiniTable* m,
118                                   const upb_MiniTableField** f) {
119   const upb_MiniTableField* ptr = *f;
120   const upb_MiniTableField* end = &m->fields[m->field_count];
121   while (++ptr < end) {
122     if (ptr->presence == (*f)->presence) {
123       *f = ptr;
124       return true;
125     }
126   }
127   return false;
128 }
129