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 <string.h>
11
12 #include "upb/mem/arena.h"
13 #include "upb/message/message.h"
14 #include "upb/mini_table/message.h"
15 #include "upb/wire/encode.h"
16
17 // Must be last.
18 #include "upb/port/def.inc"
19
upb_Message_IsExactlyEqual(const upb_Message * msg1,const upb_Message * msg2,const upb_MiniTable * m)20 bool upb_Message_IsExactlyEqual(const upb_Message* msg1,
21 const upb_Message* msg2,
22 const upb_MiniTable* m) {
23 if (msg1 == msg2) return true;
24
25 int opts = kUpb_EncodeOption_SkipUnknown | kUpb_EncodeOption_Deterministic;
26 upb_Arena* a = upb_Arena_New();
27
28 // Compare deterministically serialized payloads with no unknown fields.
29 size_t size1, size2;
30 char *data1, *data2;
31 upb_EncodeStatus status1 = upb_Encode(msg1, m, opts, a, &data1, &size1);
32 upb_EncodeStatus status2 = upb_Encode(msg2, m, opts, a, &data2, &size2);
33
34 if (status1 != kUpb_EncodeStatus_Ok || status2 != kUpb_EncodeStatus_Ok) {
35 // TODO: How should we fail here? (In Ruby we throw an exception.)
36 upb_Arena_Free(a);
37 return false;
38 }
39
40 const bool ret = (size1 == size2) && (memcmp(data1, data2, size1) == 0);
41 upb_Arena_Free(a);
42 return ret;
43 }
44