1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google Inc. 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 // -----------------------------------------------------------------------------
9 // Ruby Message functions. Strictly free of dependencies on
10 // Ruby interpreter internals.
11
12 #include "shared_message.h"
13
14 // Support function for Message_Hash. Returns a hash value for the given
15 // message.
shared_Message_Hash(const upb_Message * msg,const upb_MessageDef * m,uint64_t seed,upb_Status * status)16 uint64_t shared_Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
17 uint64_t seed, upb_Status* status) {
18 upb_Arena* arena = upb_Arena_New();
19 char* data;
20 size_t size;
21
22 // Hash a deterministically serialized payloads with no unknown fields.
23 upb_EncodeStatus encode_status = upb_Encode(
24 msg, upb_MessageDef_MiniTable(m),
25 kUpb_EncodeOption_SkipUnknown | kUpb_EncodeOption_Deterministic, arena,
26 &data, &size);
27
28 if (encode_status == kUpb_EncodeStatus_Ok) {
29 uint64_t ret = _upb_Hash(data, size, seed);
30 upb_Arena_Free(arena);
31 return ret;
32 }
33
34 upb_Arena_Free(arena);
35 upb_Status_SetErrorMessage(status, "Error calculating hash");
36 return 0;
37 }
38