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/extension_registry.h"
29
30 #include "upb/hash/str_table.h"
31 #include "upb/mini_table/extension_internal.h"
32
33 // Must be last.
34 #include "upb/port/def.inc"
35
36 #define EXTREG_KEY_SIZE (sizeof(upb_MiniTable*) + sizeof(uint32_t))
37
38 struct upb_ExtensionRegistry {
39 upb_Arena* arena;
40 upb_strtable exts; // Key is upb_MiniTable* concatenated with fieldnum.
41 };
42
extreg_key(char * buf,const upb_MiniTable * l,uint32_t fieldnum)43 static void extreg_key(char* buf, const upb_MiniTable* l, uint32_t fieldnum) {
44 memcpy(buf, &l, sizeof(l));
45 memcpy(buf + sizeof(l), &fieldnum, sizeof(fieldnum));
46 }
47
upb_ExtensionRegistry_New(upb_Arena * arena)48 upb_ExtensionRegistry* upb_ExtensionRegistry_New(upb_Arena* arena) {
49 upb_ExtensionRegistry* r = upb_Arena_Malloc(arena, sizeof(*r));
50 if (!r) return NULL;
51 r->arena = arena;
52 if (!upb_strtable_init(&r->exts, 8, arena)) return NULL;
53 return r;
54 }
55
upb_ExtensionRegistry_Add(upb_ExtensionRegistry * r,const upb_MiniTableExtension * e)56 UPB_API bool upb_ExtensionRegistry_Add(upb_ExtensionRegistry* r,
57 const upb_MiniTableExtension* e) {
58 char buf[EXTREG_KEY_SIZE];
59 extreg_key(buf, e->extendee, e->field.number);
60 if (upb_strtable_lookup2(&r->exts, buf, EXTREG_KEY_SIZE, NULL)) return false;
61 return upb_strtable_insert(&r->exts, buf, EXTREG_KEY_SIZE,
62 upb_value_constptr(e), r->arena);
63 }
64
upb_ExtensionRegistry_AddArray(upb_ExtensionRegistry * r,const upb_MiniTableExtension ** e,size_t count)65 bool upb_ExtensionRegistry_AddArray(upb_ExtensionRegistry* r,
66 const upb_MiniTableExtension** e,
67 size_t count) {
68 const upb_MiniTableExtension** start = e;
69 const upb_MiniTableExtension** end = UPB_PTRADD(e, count);
70 for (; e < end; e++) {
71 if (!upb_ExtensionRegistry_Add(r, *e)) goto failure;
72 }
73 return true;
74
75 failure:
76 // Back out the entries previously added.
77 for (end = e, e = start; e < end; e++) {
78 const upb_MiniTableExtension* ext = *e;
79 char buf[EXTREG_KEY_SIZE];
80 extreg_key(buf, ext->extendee, ext->field.number);
81 upb_strtable_remove2(&r->exts, buf, EXTREG_KEY_SIZE, NULL);
82 }
83 return false;
84 }
85
upb_ExtensionRegistry_Lookup(const upb_ExtensionRegistry * r,const upb_MiniTable * t,uint32_t num)86 const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup(
87 const upb_ExtensionRegistry* r, const upb_MiniTable* t, uint32_t num) {
88 char buf[EXTREG_KEY_SIZE];
89 upb_value v;
90 extreg_key(buf, t, num);
91 if (upb_strtable_lookup2(&r->exts, buf, EXTREG_KEY_SIZE, &v)) {
92 return upb_value_getconstptr(v);
93 } else {
94 return NULL;
95 }
96 }
97