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/enum_def.h"
30 #include "upb/reflection/enum_reserved_range_internal.h"
31 // #include "upb/reflection/extension_range_internal.h"
32 #include "upb/reflection/field_def.h"
33 // #include "upb/reflection/message_def.h"
34
35 // Must be last.
36 #include "upb/port/def.inc"
37
38 struct upb_EnumReservedRange {
39 int32_t start;
40 int32_t end;
41 };
42
_upb_EnumReservedRange_At(const upb_EnumReservedRange * r,int i)43 upb_EnumReservedRange* _upb_EnumReservedRange_At(const upb_EnumReservedRange* r,
44 int i) {
45 return (upb_EnumReservedRange*)&r[i];
46 }
47
upb_EnumReservedRange_Start(const upb_EnumReservedRange * r)48 int32_t upb_EnumReservedRange_Start(const upb_EnumReservedRange* r) {
49 return r->start;
50 }
upb_EnumReservedRange_End(const upb_EnumReservedRange * r)51 int32_t upb_EnumReservedRange_End(const upb_EnumReservedRange* r) {
52 return r->end;
53 }
54
_upb_EnumReservedRanges_New(upb_DefBuilder * ctx,int n,const UPB_DESC (EnumDescriptorProto_EnumReservedRange)* const * protos,const upb_EnumDef * e)55 upb_EnumReservedRange* _upb_EnumReservedRanges_New(
56 upb_DefBuilder* ctx, int n,
57 const UPB_DESC(EnumDescriptorProto_EnumReservedRange) * const* protos,
58 const upb_EnumDef* e) {
59 upb_EnumReservedRange* r =
60 _upb_DefBuilder_Alloc(ctx, sizeof(upb_EnumReservedRange) * n);
61
62 for (int i = 0; i < n; i++) {
63 const int32_t start =
64 UPB_DESC(EnumDescriptorProto_EnumReservedRange_start)(protos[i]);
65 const int32_t end =
66 UPB_DESC(EnumDescriptorProto_EnumReservedRange_end)(protos[i]);
67
68 // A full validation would also check that each range is disjoint, and that
69 // none of the fields overlap with the extension ranges, but we are just
70 // sanity checking here.
71
72 // Note: Not a typo! Unlike extension ranges and message reserved ranges,
73 // the end value of an enum reserved range is *inclusive*!
74 if (end < start) {
75 _upb_DefBuilder_Errf(ctx, "Reserved range (%d, %d) is invalid, enum=%s\n",
76 (int)start, (int)end, upb_EnumDef_FullName(e));
77 }
78
79 r[i].start = start;
80 r[i].end = end;
81 }
82
83 return r;
84 }
85