libhashkit/aes: simplify code
[awesomized/libmemcached] / src / libhashkit / hashkit.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libhashkit/common.h"
17
18 static inline void _hashkit_init(hashkit_st *self) {
19 self->base_hash.function = hashkit_one_at_a_time;
20 self->base_hash.context = NULL;
21
22 self->distribution_hash.function = hashkit_one_at_a_time;
23 self->distribution_hash.context = NULL;
24
25 self->flags.is_base_same_distributed = true;
26 self->_key = NULL;
27 }
28
29 static inline hashkit_st *_hashkit_create(hashkit_st *self) {
30 if (self) {
31 self->options.is_allocated = false;
32 } else {
33 self = (hashkit_st *) calloc(1, sizeof(hashkit_st));
34 if (self == NULL) {
35 return NULL;
36 }
37
38 self->options.is_allocated = true;
39 }
40
41 return self;
42 }
43
44 hashkit_st *hashkit_create(hashkit_st *self) {
45 self = _hashkit_create(self);
46 if (self == NULL) {
47 return NULL;
48 }
49
50 _hashkit_init(self);
51
52 return self;
53 }
54
55 void hashkit_free(hashkit_st *self) {
56 if (self and self->_key) {
57 aes_free_key((aes_key_t *) self->_key);
58 self->_key = NULL;
59 }
60
61 if (hashkit_is_allocated(self)) {
62 free(self);
63 }
64 }
65
66 hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *source) {
67 if (source == NULL) {
68 return hashkit_create(destination);
69 }
70
71 /* new_clone will be a pointer to destination */
72 destination = _hashkit_create(destination);
73
74 // Should only happen on allocation failure.
75 if (destination == NULL) {
76 return NULL;
77 }
78
79 destination->base_hash = source->base_hash;
80 destination->distribution_hash = source->distribution_hash;
81 destination->flags = source->flags;
82 destination->_key = aes_clone_key((aes_key_t *) source->_key);
83
84 return destination;
85 }
86
87 bool hashkit_compare(const hashkit_st *first, const hashkit_st *second) {
88 if (not first or not second)
89 return false;
90
91 if (first->base_hash.function == second->base_hash.function
92 and first->base_hash.context == second->base_hash.context
93 and first->distribution_hash.function == second->distribution_hash.function
94 and first->distribution_hash.context == second->distribution_hash.context
95 and first->flags.is_base_same_distributed == second->flags.is_base_same_distributed)
96 {
97 return true;
98 }
99
100 return false;
101 }