Rizin
unix-like reverse engineering framework and cli tools
algo_fletcher.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_hash.h>
5 #include <rz_util/rz_assert.h>
6 
7 #include "../algorithms/fletcher/fletcher.h"
8 
9 #define rz_fletcher_common_plugin_context_new(bits) \
10  static void *plugin_fletcher##bits##_context_new() { \
11  return RZ_NEW0(RzFletcher##bits); \
12  }
13 
14 #define rz_fletcher_common_plugin_context_free(bits) \
15  static void plugin_fletcher##bits##_context_free(void *context) { \
16  free(context); \
17  }
18 
19 #define rz_fletcher_common_plugin_digest_size(bits) \
20  static RzHashSize plugin_fletcher##bits##_digest_size(void *context) { \
21  return RZ_HASH_FLETCHER##bits##_DIGEST_SIZE; \
22  }
23 
24 #define rz_fletcher_common_plugin_init(bits) \
25  static bool plugin_fletcher##bits##_init(void *context) { \
26  rz_return_val_if_fail(context, false); \
27  rz_fletcher##bits##_init((RzFletcher##bits *)context); \
28  return true; \
29  }
30 
31 #define rz_fletcher_common_plugin_update(bits) \
32  static bool plugin_fletcher##bits##_update(void *context, const ut8 *data, ut64 size) { \
33  rz_return_val_if_fail(context &&data, false); \
34  rz_fletcher##bits##_update((RzFletcher##bits *)context, data, size); \
35  return true; \
36  }
37 
38 #define rz_fletcher_common_plugin_final(bits) \
39  static bool plugin_fletcher##bits##_final(void *context, ut8 *digest) { \
40  rz_return_val_if_fail(context &&digest, false); \
41  rz_fletcher##bits##_final(digest, (RzFletcher##bits *)context); \
42  return true; \
43  }
44 
45 #define rz_fletcher_common_plugin_small_block(bits) \
46  static bool plugin_fletcher##bits##_small_block(const ut8 *data, ut64 size, ut8 **digest, RzHashSize *digest_size) { \
47  rz_return_val_if_fail(data &&digest, false); \
48  ut8 *dgst = malloc(RZ_HASH_FLETCHER##bits##_DIGEST_SIZE); \
49  if (!dgst) { \
50  return false; \
51  } \
52  RzFletcher##bits ctx; \
53  rz_fletcher##bits##_init(&ctx); \
54  rz_fletcher##bits##_update(&ctx, data, size); \
55  rz_fletcher##bits##_final(dgst, &ctx); \
56  *digest = dgst; \
57  if (digest_size) { \
58  *digest_size = RZ_HASH_FLETCHER##bits##_DIGEST_SIZE; \
59  } \
60  return true; \
61  }
62 
63 #ifndef RZ_PLUGIN_INCORE
64 #define rz_lib_fletcher_common(bits) \
65  RZ_API RzLibStruct rizin_plugin = { \
66  .type = RZ_LIB_TYPE_HASH, \
67  .data = &rz_hash_plugin_fletcher##bits, \
68  .version = RZ_VERSION \
69  }
70 #else
71 #define rz_lib_fletcher_common(bits)
72 #endif
73 
74 #define rz_fletcher_common_plugin_define_hash_cfg(bits) \
75  rz_fletcher_common_plugin_context_new(bits); \
76  rz_fletcher_common_plugin_context_free(bits); \
77  rz_fletcher_common_plugin_digest_size(bits); \
78  rz_fletcher_common_plugin_init(bits); \
79  rz_fletcher_common_plugin_update(bits); \
80  rz_fletcher_common_plugin_final(bits); \
81  rz_fletcher_common_plugin_small_block(bits); \
82  RzHashPlugin rz_hash_plugin_fletcher##bits = { \
83  .name = "fletcher" #bits, \
84  .license = "LGPL3", \
85  .author = "deroad", \
86  .support_hmac = false, \
87  .context_new = plugin_fletcher##bits##_context_new, \
88  .context_free = plugin_fletcher##bits##_context_free, \
89  .digest_size = plugin_fletcher##bits##_digest_size, \
90  .block_size = plugin_fletcher_block_size, \
91  .init = plugin_fletcher##bits##_init, \
92  .update = plugin_fletcher##bits##_update, \
93  .final = plugin_fletcher##bits##_final, \
94  .small_block = plugin_fletcher##bits##_small_block, \
95  }; \
96  rz_lib_fletcher_common(bits)
97 
100 }
101 
#define rz_fletcher_common_plugin_define_hash_cfg(bits)
Definition: algo_fletcher.c:74
static RzHashSize plugin_fletcher_block_size(void *context)
Definition: algo_fletcher.c:98
#define RZ_HASH_FLETCHER_BLOCK_LENGTH
Definition: fletcher.h:10
ut32 RzHashSize
Definition: rz_hash.h:24