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/reflection/def_builder_internal.h"
29 #include "upb/reflection/extension_range_internal.h"
30 #include "upb/reflection/field_def.h"
31 #include "upb/reflection/message_def.h"
32
33 // Must be last.
34 #include "upb/port/def.inc"
35
36 struct upb_ExtensionRange {
37 const UPB_DESC(ExtensionRangeOptions) * opts;
38 int32_t start;
39 int32_t end;
40 };
41
_upb_ExtensionRange_At(const upb_ExtensionRange * r,int i)42 upb_ExtensionRange* _upb_ExtensionRange_At(const upb_ExtensionRange* r, int i) {
43 return (upb_ExtensionRange*)&r[i];
44 }
45
UPB_DESC(ExtensionRangeOptions)46 const UPB_DESC(ExtensionRangeOptions) *
47 upb_ExtensionRange_Options(const upb_ExtensionRange* r) {
48 return r->opts;
49 }
50
upb_ExtensionRange_HasOptions(const upb_ExtensionRange * r)51 bool upb_ExtensionRange_HasOptions(const upb_ExtensionRange* r) {
52 return r->opts != (void*)kUpbDefOptDefault;
53 }
54
upb_ExtensionRange_Start(const upb_ExtensionRange * r)55 int32_t upb_ExtensionRange_Start(const upb_ExtensionRange* r) {
56 return r->start;
57 }
58
upb_ExtensionRange_End(const upb_ExtensionRange * r)59 int32_t upb_ExtensionRange_End(const upb_ExtensionRange* r) { return r->end; }
60
_upb_ExtensionRanges_New(upb_DefBuilder * ctx,int n,const UPB_DESC (DescriptorProto_ExtensionRange)* const * protos,const upb_MessageDef * m)61 upb_ExtensionRange* _upb_ExtensionRanges_New(
62 upb_DefBuilder* ctx, int n,
63 const UPB_DESC(DescriptorProto_ExtensionRange) * const* protos,
64 const upb_MessageDef* m) {
65 upb_ExtensionRange* r =
66 _upb_DefBuilder_Alloc(ctx, sizeof(upb_ExtensionRange) * n);
67
68 for (int i = 0; i < n; i++) {
69 const int32_t start =
70 UPB_DESC(DescriptorProto_ExtensionRange_start)(protos[i]);
71 const int32_t end = UPB_DESC(DescriptorProto_ExtensionRange_end)(protos[i]);
72 const int32_t max = UPB_DESC(MessageOptions_message_set_wire_format)(
73 upb_MessageDef_Options(m))
74 ? INT32_MAX
75 : kUpb_MaxFieldNumber + 1;
76
77 // A full validation would also check that each range is disjoint, and that
78 // none of the fields overlap with the extension ranges, but we are just
79 // sanity checking here.
80 if (start < 1 || end <= start || end > max) {
81 _upb_DefBuilder_Errf(ctx,
82 "Extension range (%d, %d) is invalid, message=%s\n",
83 (int)start, (int)end, upb_MessageDef_FullName(m));
84 }
85
86 r[i].start = start;
87 r[i].end = end;
88 UPB_DEF_SET_OPTIONS(r[i].opts, DescriptorProto_ExtensionRange,
89 ExtensionRangeOptions, protos[i]);
90 }
91
92 return r;
93 }
94