• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "upb/message/compare.h"
9 
10 #include <stddef.h>
11 
12 #include "upb/base/descriptor_constants.h"
13 #include "upb/message/accessors.h"
14 #include "upb/message/array.h"
15 #include "upb/message/internal/accessors.h"
16 #include "upb/message/internal/compare_unknown.h"
17 #include "upb/message/internal/extension.h"
18 #include "upb/message/internal/iterator.h"
19 #include "upb/message/map.h"
20 #include "upb/message/message.h"
21 #include "upb/mini_table/extension.h"
22 #include "upb/mini_table/field.h"
23 #include "upb/mini_table/internal/field.h"
24 #include "upb/mini_table/message.h"
25 
26 // Must be last.
27 #include "upb/port/def.inc"
28 
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
upb_Message_IsEmpty(const upb_Message * msg,const upb_MiniTable * m)34 bool upb_Message_IsEmpty(const upb_Message* msg, const upb_MiniTable* m) {
35   if (upb_Message_ExtensionCount(msg)) return false;
36 
37   const upb_MiniTableField* f;
38   upb_MessageValue v;
39   size_t iter = kUpb_BaseField_Begin;
40   return !UPB_PRIVATE(_upb_Message_NextBaseField)(msg, m, &f, &v, &iter);
41 }
42 
_upb_Array_IsEqual(const upb_Array * arr1,const upb_Array * arr2,upb_CType ctype,const upb_MiniTable * m,int options)43 static bool _upb_Array_IsEqual(const upb_Array* arr1, const upb_Array* arr2,
44                                upb_CType ctype, const upb_MiniTable* m,
45                                int options) {
46   // Check for trivial equality.
47   if (arr1 == arr2) return true;
48 
49   // Must have identical element counts.
50   const size_t size1 = arr1 ? upb_Array_Size(arr1) : 0;
51   const size_t size2 = arr2 ? upb_Array_Size(arr2) : 0;
52   if (size1 != size2) return false;
53 
54   for (size_t i = 0; i < size1; i++) {
55     const upb_MessageValue val1 = upb_Array_Get(arr1, i);
56     const upb_MessageValue val2 = upb_Array_Get(arr2, i);
57 
58     if (!upb_MessageValue_IsEqual(val1, val2, ctype, m, options)) return false;
59   }
60 
61   return true;
62 }
63 
_upb_Map_IsEqual(const upb_Map * map1,const upb_Map * map2,const upb_MiniTable * m,int options)64 static bool _upb_Map_IsEqual(const upb_Map* map1, const upb_Map* map2,
65                              const upb_MiniTable* m, int options) {
66   // Check for trivial equality.
67   if (map1 == map2) return true;
68 
69   // Must have identical element counts.
70   size_t size1 = map1 ? upb_Map_Size(map1) : 0;
71   size_t size2 = map2 ? upb_Map_Size(map2) : 0;
72   if (size1 != size2) return false;
73 
74   const upb_MiniTableField* f = upb_MiniTable_MapValue(m);
75   const upb_MiniTable* m2_value = upb_MiniTable_SubMessage(m, f);
76   const upb_CType ctype = upb_MiniTableField_CType(f);
77 
78   upb_MessageValue key, val1, val2;
79   size_t iter = kUpb_Map_Begin;
80   while (upb_Map_Next(map1, &key, &val1, &iter)) {
81     if (!upb_Map_Get(map2, key, &val2)) return false;
82     if (!upb_MessageValue_IsEqual(val1, val2, ctype, m2_value, options))
83       return false;
84   }
85 
86   return true;
87 }
88 
_upb_Message_BaseFieldsAreEqual(const upb_Message * msg1,const upb_Message * msg2,const upb_MiniTable * m,int options)89 static bool _upb_Message_BaseFieldsAreEqual(const upb_Message* msg1,
90                                             const upb_Message* msg2,
91                                             const upb_MiniTable* m,
92                                             int options) {
93   // Iterate over all base fields for each message.
94   // The order will always match if the messages are equal.
95   size_t iter1 = kUpb_BaseField_Begin;
96   size_t iter2 = kUpb_BaseField_Begin;
97 
98   for (;;) {
99     const upb_MiniTableField *f1, *f2;
100     upb_MessageValue val1, val2;
101 
102     const bool got1 =
103         UPB_PRIVATE(_upb_Message_NextBaseField)(msg1, m, &f1, &val1, &iter1);
104     const bool got2 =
105         UPB_PRIVATE(_upb_Message_NextBaseField)(msg2, m, &f2, &val2, &iter2);
106 
107     if (got1 != got2) return false;  // Must have identical field counts.
108     if (!got1) return true;          // Loop termination condition.
109     if (f1 != f2) return false;      // Must have identical fields set.
110 
111     const upb_MiniTable* subm = upb_MiniTable_SubMessage(m, f1);
112     const upb_CType ctype = upb_MiniTableField_CType(f1);
113 
114     bool eq;
115     switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f1)) {
116       case kUpb_FieldMode_Array:
117         eq = _upb_Array_IsEqual(val1.array_val, val2.array_val, ctype, subm,
118                                 options);
119         break;
120       case kUpb_FieldMode_Map:
121         eq = _upb_Map_IsEqual(val1.map_val, val2.map_val, subm, options);
122         break;
123       case kUpb_FieldMode_Scalar:
124         eq = upb_MessageValue_IsEqual(val1, val2, ctype, subm, options);
125         break;
126     }
127     if (!eq) return false;
128   }
129 }
130 
_upb_Message_ExtensionsAreEqual(const upb_Message * msg1,const upb_Message * msg2,const upb_MiniTable * m,int options)131 static bool _upb_Message_ExtensionsAreEqual(const upb_Message* msg1,
132                                             const upb_Message* msg2,
133                                             const upb_MiniTable* m,
134                                             int options) {
135   // Must have identical extension counts.
136   if (upb_Message_ExtensionCount(msg1) != upb_Message_ExtensionCount(msg2)) {
137     return false;
138   }
139 
140   const upb_MiniTableExtension* e;
141   upb_MessageValue val1;
142 
143   // Iterate over all extensions for msg1, and search msg2 for each extension.
144   size_t iter1 = kUpb_Extension_Begin;
145   while (UPB_PRIVATE(_upb_Message_NextExtension)(msg1, m, &e, &val1, &iter1)) {
146     const upb_Extension* ext2 = UPB_PRIVATE(_upb_Message_Getext)(msg2, e);
147     if (!ext2) return false;
148 
149     const upb_MessageValue val2 = ext2->data;
150     const upb_MiniTableField* f = &e->UPB_PRIVATE(field);
151     const upb_MiniTable* subm = upb_MiniTableField_IsSubMessage(f)
152                                     ? upb_MiniTableExtension_GetSubMessage(e)
153                                     : NULL;
154     const upb_CType ctype = upb_MiniTableField_CType(f);
155 
156     bool eq;
157     switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f)) {
158       case kUpb_FieldMode_Array:
159         eq = _upb_Array_IsEqual(val1.array_val, val2.array_val, ctype, subm,
160                                 options);
161         break;
162       case kUpb_FieldMode_Map:
163         UPB_UNREACHABLE();  // Maps cannot be extensions.
164         break;
165       case kUpb_FieldMode_Scalar: {
166         eq = upb_MessageValue_IsEqual(val1, val2, ctype, subm, options);
167         break;
168       }
169     }
170     if (!eq) return false;
171   }
172   return true;
173 }
174 
upb_Message_IsEqual(const upb_Message * msg1,const upb_Message * msg2,const upb_MiniTable * m,int options)175 bool upb_Message_IsEqual(const upb_Message* msg1, const upb_Message* msg2,
176                          const upb_MiniTable* m, int options) {
177   if (UPB_UNLIKELY(msg1 == msg2)) return true;
178 
179   if (!_upb_Message_BaseFieldsAreEqual(msg1, msg2, m, options)) return false;
180   if (!_upb_Message_ExtensionsAreEqual(msg1, msg2, m, options)) return false;
181 
182   if (!(options & kUpb_CompareOption_IncludeUnknownFields)) return true;
183 
184   // Check the unknown fields.
185   size_t usize1, usize2;
186   const char* uf1 = upb_Message_GetUnknown(msg1, &usize1);
187   const char* uf2 = upb_Message_GetUnknown(msg2, &usize2);
188 
189   // The wire encoder enforces a maximum depth of 100 so we match that here.
190   return UPB_PRIVATE(_upb_Message_UnknownFieldsAreEqual)(
191              uf1, usize1, uf2, usize2, 100) == kUpb_UnknownCompareResult_Equal;
192 }
193