nutcracker 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. data/README.md +22 -0
  2. data/Rakefile +55 -0
  3. data/bin/nutcracker +2 -0
  4. data/ext/nutcracker/ChangeLog +66 -0
  5. data/ext/nutcracker/LICENSE +177 -0
  6. data/ext/nutcracker/Makefile.am +7 -0
  7. data/ext/nutcracker/Makefile.in +726 -0
  8. data/ext/nutcracker/NOTICE +124 -0
  9. data/ext/nutcracker/README.md +240 -0
  10. data/ext/nutcracker/aclocal.m4 +956 -0
  11. data/ext/nutcracker/conf/nutcracker.leaf.yml +10 -0
  12. data/ext/nutcracker/conf/nutcracker.root.yml +8 -0
  13. data/ext/nutcracker/conf/nutcracker.yml +67 -0
  14. data/ext/nutcracker/config.h.in +316 -0
  15. data/ext/nutcracker/config/config.guess +1561 -0
  16. data/ext/nutcracker/config/config.sub +1686 -0
  17. data/ext/nutcracker/config/depcomp +630 -0
  18. data/ext/nutcracker/config/install-sh +520 -0
  19. data/ext/nutcracker/config/ltmain.sh +8413 -0
  20. data/ext/nutcracker/config/missing +376 -0
  21. data/ext/nutcracker/configure +18862 -0
  22. data/ext/nutcracker/configure.ac +155 -0
  23. data/ext/nutcracker/contrib/Makefile.am +3 -0
  24. data/ext/nutcracker/contrib/Makefile.in +560 -0
  25. data/ext/nutcracker/contrib/yaml-0.1.4.tar.gz +0 -0
  26. data/ext/nutcracker/contrib/yaml-0.1.4/LICENSE +19 -0
  27. data/ext/nutcracker/contrib/yaml-0.1.4/Makefile.am +20 -0
  28. data/ext/nutcracker/contrib/yaml-0.1.4/Makefile.in +736 -0
  29. data/ext/nutcracker/contrib/yaml-0.1.4/README +27 -0
  30. data/ext/nutcracker/contrib/yaml-0.1.4/aclocal.m4 +956 -0
  31. data/ext/nutcracker/contrib/yaml-0.1.4/config.h.in +80 -0
  32. data/ext/nutcracker/contrib/yaml-0.1.4/config/config.guess +1561 -0
  33. data/ext/nutcracker/contrib/yaml-0.1.4/config/config.sub +1686 -0
  34. data/ext/nutcracker/contrib/yaml-0.1.4/config/depcomp +630 -0
  35. data/ext/nutcracker/contrib/yaml-0.1.4/config/install-sh +520 -0
  36. data/ext/nutcracker/contrib/yaml-0.1.4/config/ltmain.sh +8406 -0
  37. data/ext/nutcracker/contrib/yaml-0.1.4/config/missing +376 -0
  38. data/ext/nutcracker/contrib/yaml-0.1.4/configure +13085 -0
  39. data/ext/nutcracker/contrib/yaml-0.1.4/configure.ac +75 -0
  40. data/ext/nutcracker/contrib/yaml-0.1.4/doc/doxygen.cfg +222 -0
  41. data/ext/nutcracker/contrib/yaml-0.1.4/include/yaml.h +1971 -0
  42. data/ext/nutcracker/contrib/yaml-0.1.4/m4/libtool.m4 +7357 -0
  43. data/ext/nutcracker/contrib/yaml-0.1.4/m4/ltoptions.m4 +368 -0
  44. data/ext/nutcracker/contrib/yaml-0.1.4/m4/ltsugar.m4 +123 -0
  45. data/ext/nutcracker/contrib/yaml-0.1.4/m4/ltversion.m4 +23 -0
  46. data/ext/nutcracker/contrib/yaml-0.1.4/m4/lt~obsolete.m4 +92 -0
  47. data/ext/nutcracker/contrib/yaml-0.1.4/src/Makefile.am +4 -0
  48. data/ext/nutcracker/contrib/yaml-0.1.4/src/Makefile.in +484 -0
  49. data/ext/nutcracker/contrib/yaml-0.1.4/src/api.c +1392 -0
  50. data/ext/nutcracker/contrib/yaml-0.1.4/src/dumper.c +394 -0
  51. data/ext/nutcracker/contrib/yaml-0.1.4/src/emitter.c +2329 -0
  52. data/ext/nutcracker/contrib/yaml-0.1.4/src/loader.c +432 -0
  53. data/ext/nutcracker/contrib/yaml-0.1.4/src/parser.c +1374 -0
  54. data/ext/nutcracker/contrib/yaml-0.1.4/src/reader.c +465 -0
  55. data/ext/nutcracker/contrib/yaml-0.1.4/src/scanner.c +3570 -0
  56. data/ext/nutcracker/contrib/yaml-0.1.4/src/writer.c +141 -0
  57. data/ext/nutcracker/contrib/yaml-0.1.4/src/yaml_private.h +640 -0
  58. data/ext/nutcracker/contrib/yaml-0.1.4/tests/Makefile.am +8 -0
  59. data/ext/nutcracker/contrib/yaml-0.1.4/tests/Makefile.in +675 -0
  60. data/ext/nutcracker/contrib/yaml-0.1.4/tests/example-deconstructor-alt.c +800 -0
  61. data/ext/nutcracker/contrib/yaml-0.1.4/tests/example-deconstructor.c +1130 -0
  62. data/ext/nutcracker/contrib/yaml-0.1.4/tests/example-reformatter-alt.c +217 -0
  63. data/ext/nutcracker/contrib/yaml-0.1.4/tests/example-reformatter.c +202 -0
  64. data/ext/nutcracker/contrib/yaml-0.1.4/tests/run-dumper.c +311 -0
  65. data/ext/nutcracker/contrib/yaml-0.1.4/tests/run-emitter.c +327 -0
  66. data/ext/nutcracker/contrib/yaml-0.1.4/tests/run-loader.c +63 -0
  67. data/ext/nutcracker/contrib/yaml-0.1.4/tests/run-parser.c +63 -0
  68. data/ext/nutcracker/contrib/yaml-0.1.4/tests/run-scanner.c +63 -0
  69. data/ext/nutcracker/contrib/yaml-0.1.4/tests/test-reader.c +354 -0
  70. data/ext/nutcracker/contrib/yaml-0.1.4/tests/test-version.c +29 -0
  71. data/ext/nutcracker/extconf.rb +5 -0
  72. data/ext/nutcracker/m4/libtool.m4 +7376 -0
  73. data/ext/nutcracker/m4/ltoptions.m4 +368 -0
  74. data/ext/nutcracker/m4/ltsugar.m4 +123 -0
  75. data/ext/nutcracker/m4/ltversion.m4 +23 -0
  76. data/ext/nutcracker/m4/lt~obsolete.m4 +92 -0
  77. data/ext/nutcracker/notes/c-styleguide.txt +425 -0
  78. data/ext/nutcracker/notes/debug.txt +96 -0
  79. data/ext/nutcracker/notes/memcache.txt +123 -0
  80. data/ext/nutcracker/notes/recommendation.md +118 -0
  81. data/ext/nutcracker/notes/redis.md +415 -0
  82. data/ext/nutcracker/notes/socket.txt +131 -0
  83. data/ext/nutcracker/scripts/multi_get.sh +26 -0
  84. data/ext/nutcracker/scripts/nutcracker.init +73 -0
  85. data/ext/nutcracker/scripts/nutcracker.spec +52 -0
  86. data/ext/nutcracker/scripts/pipelined_read.sh +23 -0
  87. data/ext/nutcracker/scripts/pipelined_write.sh +29 -0
  88. data/ext/nutcracker/scripts/populate_memcached.sh +24 -0
  89. data/ext/nutcracker/scripts/redis-check.py +23 -0
  90. data/ext/nutcracker/scripts/redis-check.sh +564 -0
  91. data/ext/nutcracker/src/Makefile.am +46 -0
  92. data/ext/nutcracker/src/Makefile.in +726 -0
  93. data/ext/nutcracker/src/hashkit/Makefile.am +22 -0
  94. data/ext/nutcracker/src/hashkit/Makefile.in +501 -0
  95. data/ext/nutcracker/src/hashkit/nc_crc32.c +105 -0
  96. data/ext/nutcracker/src/hashkit/nc_fnv.c +82 -0
  97. data/ext/nutcracker/src/hashkit/nc_hashkit.h +74 -0
  98. data/ext/nutcracker/src/hashkit/nc_hsieh.c +93 -0
  99. data/ext/nutcracker/src/hashkit/nc_jenkins.c +230 -0
  100. data/ext/nutcracker/src/hashkit/nc_ketama.c +240 -0
  101. data/ext/nutcracker/src/hashkit/nc_md5.c +379 -0
  102. data/ext/nutcracker/src/hashkit/nc_modula.c +144 -0
  103. data/ext/nutcracker/src/hashkit/nc_murmur.c +99 -0
  104. data/ext/nutcracker/src/hashkit/nc_one_at_a_time.c +51 -0
  105. data/ext/nutcracker/src/hashkit/nc_random.c +146 -0
  106. data/ext/nutcracker/src/nc.c +573 -0
  107. data/ext/nutcracker/src/nc_array.c +204 -0
  108. data/ext/nutcracker/src/nc_array.h +73 -0
  109. data/ext/nutcracker/src/nc_client.c +189 -0
  110. data/ext/nutcracker/src/nc_client.h +28 -0
  111. data/ext/nutcracker/src/nc_conf.c +1766 -0
  112. data/ext/nutcracker/src/nc_conf.h +134 -0
  113. data/ext/nutcracker/src/nc_connection.c +392 -0
  114. data/ext/nutcracker/src/nc_connection.h +99 -0
  115. data/ext/nutcracker/src/nc_core.c +334 -0
  116. data/ext/nutcracker/src/nc_core.h +131 -0
  117. data/ext/nutcracker/src/nc_event.c +214 -0
  118. data/ext/nutcracker/src/nc_event.h +39 -0
  119. data/ext/nutcracker/src/nc_log.c +254 -0
  120. data/ext/nutcracker/src/nc_log.h +120 -0
  121. data/ext/nutcracker/src/nc_mbuf.c +285 -0
  122. data/ext/nutcracker/src/nc_mbuf.h +67 -0
  123. data/ext/nutcracker/src/nc_message.c +828 -0
  124. data/ext/nutcracker/src/nc_message.h +253 -0
  125. data/ext/nutcracker/src/nc_proxy.c +359 -0
  126. data/ext/nutcracker/src/nc_proxy.h +34 -0
  127. data/ext/nutcracker/src/nc_queue.h +788 -0
  128. data/ext/nutcracker/src/nc_rbtree.c +348 -0
  129. data/ext/nutcracker/src/nc_rbtree.h +47 -0
  130. data/ext/nutcracker/src/nc_request.c +588 -0
  131. data/ext/nutcracker/src/nc_response.c +332 -0
  132. data/ext/nutcracker/src/nc_server.c +841 -0
  133. data/ext/nutcracker/src/nc_server.h +143 -0
  134. data/ext/nutcracker/src/nc_signal.c +131 -0
  135. data/ext/nutcracker/src/nc_signal.h +34 -0
  136. data/ext/nutcracker/src/nc_stats.c +1188 -0
  137. data/ext/nutcracker/src/nc_stats.h +206 -0
  138. data/ext/nutcracker/src/nc_string.c +109 -0
  139. data/ext/nutcracker/src/nc_string.h +112 -0
  140. data/ext/nutcracker/src/nc_util.c +619 -0
  141. data/ext/nutcracker/src/nc_util.h +214 -0
  142. data/ext/nutcracker/src/proto/Makefile.am +14 -0
  143. data/ext/nutcracker/src/proto/Makefile.in +482 -0
  144. data/ext/nutcracker/src/proto/nc_memcache.c +1306 -0
  145. data/ext/nutcracker/src/proto/nc_proto.h +155 -0
  146. data/ext/nutcracker/src/proto/nc_redis.c +2102 -0
  147. data/lib/nutcracker.rb +7 -0
  148. data/lib/nutcracker/version.rb +3 -0
  149. metadata +194 -0
@@ -0,0 +1,240 @@
1
+ /*
2
+ * twemproxy - A fast and lightweight proxy for memcached protocol.
3
+ * Copyright (C) 2011 Twitter, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #include <stdio.h>
19
+ #include <stdlib.h>
20
+ #include <math.h>
21
+
22
+ #include <nc_core.h>
23
+ #include <nc_server.h>
24
+ #include <nc_hashkit.h>
25
+
26
+ #define KETAMA_CONTINUUM_ADDITION 10 /* # extra slots to build into continuum */
27
+ #define KETAMA_POINTS_PER_SERVER 160 /* 40 points per hash */
28
+ #define KETAMA_MAX_HOSTLEN 86
29
+
30
+ static uint32_t
31
+ ketama_hash(const char *key, size_t key_length, uint32_t alignment)
32
+ {
33
+ unsigned char results[16];
34
+
35
+ md5_signature((unsigned char*)key, key_length, results);
36
+
37
+ return ((uint32_t) (results[3 + alignment * 4] & 0xFF) << 24)
38
+ | ((uint32_t) (results[2 + alignment * 4] & 0xFF) << 16)
39
+ | ((uint32_t) (results[1 + alignment * 4] & 0xFF) << 8)
40
+ | (results[0 + alignment * 4] & 0xFF);
41
+ }
42
+
43
+ static int
44
+ ketama_item_cmp(const void *t1, const void *t2)
45
+ {
46
+ const struct continuum *ct1 = t1, *ct2 = t2;
47
+
48
+ if (ct1->value == ct2->value) {
49
+ return 0;
50
+ } else if (ct1->value > ct2->value) {
51
+ return 1;
52
+ } else {
53
+ return -1;
54
+ }
55
+ }
56
+
57
+ rstatus_t
58
+ ketama_update(struct server_pool *pool)
59
+ {
60
+ uint32_t nserver; /* # server - live and dead */
61
+ uint32_t nlive_server; /* # live server */
62
+ uint32_t pointer_per_server; /* pointers per server proportional to weight */
63
+ uint32_t pointer_per_hash; /* pointers per hash */
64
+ uint32_t pointer_counter; /* # pointers on continuum */
65
+ uint32_t pointer_index; /* pointer index */
66
+ uint32_t points_per_server; /* points per server */
67
+ uint32_t continuum_index; /* continuum index */
68
+ uint32_t continuum_addition; /* extra space in the continuum */
69
+ uint32_t server_index; /* server index */
70
+ uint32_t value; /* continuum value */
71
+ uint32_t total_weight; /* total live server weight */
72
+ int64_t now; /* current timestamp in usec */
73
+
74
+ ASSERT(array_n(&pool->server) > 0);
75
+
76
+ now = nc_usec_now();
77
+ if (now < 0) {
78
+ return NC_ERROR;
79
+ }
80
+
81
+ /*
82
+ * Count live servers and total weight, and also update the next time to
83
+ * rebuild the distribution
84
+ */
85
+ nserver = array_n(&pool->server);
86
+ nlive_server = 0;
87
+ total_weight = 0;
88
+ pool->next_rebuild = 0LL;
89
+ for (server_index = 0; server_index < nserver; server_index++) {
90
+ struct server *server = array_get(&pool->server, server_index);
91
+
92
+ if (pool->auto_eject_hosts) {
93
+ if (server->next_retry <= now) {
94
+ server->next_retry = 0LL;
95
+ nlive_server++;
96
+ } else if (pool->next_rebuild == 0LL ||
97
+ server->next_retry < pool->next_rebuild) {
98
+ pool->next_rebuild = server->next_retry;
99
+ }
100
+ } else {
101
+ nlive_server++;
102
+ }
103
+
104
+ ASSERT(server->weight > 0);
105
+
106
+ /* count weight only for live servers */
107
+ if (!pool->auto_eject_hosts || server->next_retry <= now) {
108
+ total_weight += server->weight;
109
+ }
110
+ }
111
+
112
+ pool->nlive_server = nlive_server;
113
+
114
+ if (nlive_server == 0) {
115
+ log_debug(LOG_DEBUG, "no live servers for pool %"PRIu32" '%.*s'",
116
+ pool->idx, pool->name.len, pool->name.data);
117
+
118
+ return NC_OK;
119
+ }
120
+ log_debug(LOG_DEBUG, "%"PRIu32" of %"PRIu32" servers are live for pool "
121
+ "%"PRIu32" '%.*s'", nlive_server, nserver, pool->idx,
122
+ pool->name.len, pool->name.data);
123
+
124
+ continuum_addition = KETAMA_CONTINUUM_ADDITION;
125
+ points_per_server = KETAMA_POINTS_PER_SERVER;
126
+ /*
127
+ * Allocate the continuum for the pool, the first time, and every time we
128
+ * add a new server to the pool
129
+ */
130
+ if (nlive_server > pool->nserver_continuum) {
131
+ struct continuum *continuum;
132
+ uint32_t nserver_continuum = nlive_server + continuum_addition;
133
+ uint32_t ncontinuum = nserver_continuum * points_per_server;
134
+
135
+ continuum = nc_realloc(pool->continuum, sizeof(*continuum) * ncontinuum);
136
+ if (continuum == NULL) {
137
+ return NC_ENOMEM;
138
+ }
139
+
140
+ pool->continuum = continuum;
141
+ pool->nserver_continuum = nserver_continuum;
142
+ /* pool->ncontinuum is initialized later as it could be <= ncontinuum */
143
+ }
144
+
145
+ /*
146
+ * Build a continuum with the servers that are live and points from
147
+ * these servers that are proportial to their weight
148
+ */
149
+ continuum_index = 0;
150
+ pointer_counter = 0;
151
+ for (server_index = 0; server_index < nserver; server_index++) {
152
+ struct server *server;
153
+ float pct;
154
+
155
+ server = array_get(&pool->server, server_index);
156
+
157
+ if (pool->auto_eject_hosts && server->next_retry > now) {
158
+ continue;
159
+ }
160
+
161
+ pct = (float)server->weight / (float)total_weight;
162
+ pointer_per_server = (uint32_t) ((floorf((float) (pct * KETAMA_POINTS_PER_SERVER / 4 * (float)nlive_server + 0.0000000001))) * 4);
163
+ pointer_per_hash = 4;
164
+
165
+ log_debug(LOG_VERB, "%.*s:%"PRIu16" weight %"PRIu32" of %"PRIu32" "
166
+ "pct %0.5f points per server %"PRIu32"",
167
+ server->name.len, server->name.data, server->port,
168
+ server->weight, total_weight, pct, pointer_per_server);
169
+
170
+ for (pointer_index = 1;
171
+ pointer_index <= pointer_per_server / pointer_per_hash;
172
+ pointer_index++) {
173
+
174
+ char host[KETAMA_MAX_HOSTLEN]= "";
175
+ size_t hostlen;
176
+ uint32_t x;
177
+
178
+ hostlen = snprintf(host, KETAMA_MAX_HOSTLEN, "%.*s-%u",
179
+ server->name.len, server->name.data,
180
+ pointer_index - 1);
181
+
182
+ for (x = 0; x < pointer_per_hash; x++) {
183
+ value = ketama_hash(host, hostlen, x);
184
+ pool->continuum[continuum_index].index = server_index;
185
+ pool->continuum[continuum_index++].value = value;
186
+ }
187
+ }
188
+ pointer_counter += pointer_per_server;
189
+ }
190
+
191
+ pool->ncontinuum = pointer_counter;
192
+ qsort(pool->continuum, pool->ncontinuum, sizeof(*pool->continuum),
193
+ ketama_item_cmp);
194
+
195
+ for (pointer_index = 0;
196
+ pointer_index < ((nlive_server * KETAMA_POINTS_PER_SERVER) - 1);
197
+ pointer_index++) {
198
+ if (pointer_index + 1 >= pointer_counter) {
199
+ break;
200
+ }
201
+ ASSERT(pool->continuum[pointer_index].value <=
202
+ pool->continuum[pointer_index + 1].value);
203
+ }
204
+
205
+ log_debug(LOG_VERB, "updated pool %"PRIu32" '%.*s' with %"PRIu32" of "
206
+ "%"PRIu32" servers live in %"PRIu32" slots and %"PRIu32" "
207
+ "active points in %"PRIu32" slots", pool->idx,
208
+ pool->name.len, pool->name.data, nlive_server, nserver,
209
+ pool->nserver_continuum, pool->ncontinuum,
210
+ (pool->nserver_continuum + continuum_addition) * points_per_server);
211
+
212
+ return NC_OK;
213
+ }
214
+
215
+ uint32_t
216
+ ketama_dispatch(struct continuum *continuum, uint32_t ncontinuum, uint32_t hash)
217
+ {
218
+ struct continuum *begin, *end, *left, *right, *middle;
219
+
220
+ ASSERT(continuum != NULL);
221
+ ASSERT(ncontinuum != 0);
222
+
223
+ begin = left = continuum;
224
+ end = right = continuum + ncontinuum;
225
+
226
+ while (left < right) {
227
+ middle = left + (right - left) / 2;
228
+ if (middle->value < hash) {
229
+ left = middle + 1;
230
+ } else {
231
+ right = middle;
232
+ }
233
+ }
234
+
235
+ if (right == end) {
236
+ right = begin;
237
+ }
238
+
239
+ return right->index;
240
+ }
@@ -0,0 +1,379 @@
1
+ /*
2
+ * twemproxy - A fast and lightweight proxy for memcached protocol.
3
+ * Copyright (C) 2011 Twitter, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ /*
19
+ * This Library has been modified from its original form by
20
+ * Brian Aker (brian@tangent.org)
21
+ *
22
+ * See below for original Copyright.
23
+ */
24
+ /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
25
+ *
26
+ * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
27
+ * rights reserved.
28
+ *
29
+ * License to copy and use this software is granted provided that it
30
+ * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
31
+ * Algorithm" in all material mentioning or referencing this software
32
+ * or this function.
33
+ *
34
+ * License is also granted to make and use derivative works provided
35
+ * that such works are identified as "derived from the RSA Data
36
+ * Security, Inc. MD5 Message-Digest Algorithm" in all material
37
+ * mentioning or referencing the derived work.
38
+ *
39
+ * RSA Data Security, Inc. makes no representations concerning either
40
+ * the merchantability of this software or the suitability of this
41
+ * software for any particular purpose. It is provided "as is"
42
+ * without express or implied warranty of any kind.
43
+ *
44
+ * These notices must be retained in any copies of any part of this
45
+ * documentation and/or software.
46
+ */
47
+
48
+ #include <nc_core.h>
49
+
50
+ /* POINTER defines a generic pointer type */
51
+ typedef unsigned char *POINTER;
52
+
53
+
54
+ /* UINT4 defines a four byte word */
55
+ typedef unsigned int UINT4;
56
+
57
+
58
+ /* MD5 context. */
59
+ typedef struct {
60
+ UINT4 state[4]; /* state (ABCD) */
61
+ UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
62
+ unsigned char buffer[64]; /* input buffer */
63
+ } MD5_CTX;
64
+
65
+ static void MD5Init (MD5_CTX *context); /* context */
66
+ static void MD5Update ( MD5_CTX *context, /* context */
67
+ const unsigned char *input, /* input block */
68
+ unsigned int inputLen); /* length of input block */
69
+ static void MD5Final ( unsigned char digest[16], /* message digest */
70
+ MD5_CTX *context); /* context */
71
+
72
+ /* Constants for MD5Transform routine. */
73
+
74
+ #define S11 7
75
+ #define S12 12
76
+ #define S13 17
77
+ #define S14 22
78
+ #define S21 5
79
+ #define S22 9
80
+ #define S23 14
81
+ #define S24 20
82
+ #define S31 4
83
+ #define S32 11
84
+ #define S33 16
85
+ #define S34 23
86
+ #define S41 6
87
+ #define S42 10
88
+ #define S43 15
89
+ #define S44 21
90
+
91
+
92
+ static void MD5Transform (UINT4 state[4],
93
+ unsigned char block[64]);
94
+ static void Encode (unsigned char *output,
95
+ UINT4 *input,
96
+ unsigned int len);
97
+ static void Decode(UINT4 *output, unsigned char *input, unsigned int len);
98
+
99
+ static unsigned char PADDING[64] = {
100
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
103
+ };
104
+
105
+ /* F, G, H and I are basic MD5 functions.
106
+ */
107
+ #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
108
+ #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
109
+ #define H(x, y, z) ((x) ^ (y) ^ (z))
110
+ #define I(x, y, z) ((y) ^ ((x) | (~z)))
111
+
112
+ /* ROTATE_LEFT rotates x left n bits.
113
+ */
114
+ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
115
+
116
+ /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
117
+ Rotation is separate from addition to prevent recomputation.
118
+ */
119
+ #define FF(a, b, c, d, x, s, ac) { \
120
+ (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
121
+ (a) = ROTATE_LEFT ((a), (s)); \
122
+ (a) += (b); \
123
+ }
124
+ #define GG(a, b, c, d, x, s, ac) { \
125
+ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
126
+ (a) = ROTATE_LEFT ((a), (s)); \
127
+ (a) += (b); \
128
+ }
129
+ #define HH(a, b, c, d, x, s, ac) { \
130
+ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
131
+ (a) = ROTATE_LEFT ((a), (s)); \
132
+ (a) += (b); \
133
+ }
134
+ #define II(a, b, c, d, x, s, ac) { \
135
+ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
136
+ (a) = ROTATE_LEFT ((a), (s)); \
137
+ (a) += (b); \
138
+ }
139
+
140
+
141
+ /*
142
+ Just a simple method for getting the signature
143
+ result must be == 16
144
+ */
145
+ void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result)
146
+ {
147
+ MD5_CTX my_md5;
148
+
149
+ MD5Init(&my_md5);
150
+ (void)MD5Update(&my_md5, key, length);
151
+ MD5Final(result, &my_md5);
152
+ }
153
+
154
+ /* MD5 initialization. Begins an MD5 operation, writing a new context.
155
+ */
156
+ static void MD5Init (MD5_CTX *context) /* context */
157
+ {
158
+ context->count[0] = context->count[1] = 0;
159
+ /* Load magic initialization constants.
160
+ */
161
+ context->state[0] = 0x67452301;
162
+ context->state[1] = 0xefcdab89;
163
+ context->state[2] = 0x98badcfe;
164
+ context->state[3] = 0x10325476;
165
+ }
166
+
167
+ /* MD5 block update operation. Continues an MD5 message-digest
168
+ operation, processing another message block, and updating the
169
+ context.
170
+ */
171
+
172
+ static void MD5Update (
173
+ MD5_CTX *context, /* context */
174
+ const unsigned char *input, /* input block */
175
+ unsigned int inputLen) /* length of input block */
176
+ {
177
+ unsigned int i, idx, partLen;
178
+
179
+ /* Compute number of bytes mod 64 */
180
+ idx = (unsigned int)((context->count[0] >> 3) & 0x3F);
181
+
182
+
183
+ /* Update number of bits */
184
+ if ((context->count[0] += ((UINT4)inputLen << 3))
185
+ < ((UINT4)inputLen << 3))
186
+ context->count[1]++;
187
+ context->count[1] += ((UINT4)inputLen >> 29);
188
+
189
+ partLen = 64 - idx;
190
+
191
+ /* Transform as many times as possible.
192
+ */
193
+ if (inputLen >= partLen) {
194
+ memcpy((POINTER)&context->buffer[idx], (POINTER)input, partLen);
195
+ MD5Transform(context->state, context->buffer);
196
+
197
+ for (i = partLen; i + 63 < inputLen; i += 64)
198
+ MD5Transform (context->state, (unsigned char *)&input[i]);
199
+
200
+ idx = 0;
201
+ }
202
+ else
203
+ i = 0;
204
+
205
+ /* Buffer remaining input */
206
+ memcpy((POINTER)&context->buffer[idx], (POINTER)&input[i],
207
+ inputLen-i);
208
+ }
209
+
210
+ /* MD5 finalization. Ends an MD5 message-digest operation, writing the
211
+ the message digest and zeroizing the context.
212
+ */
213
+
214
+ static void MD5Final (
215
+ unsigned char digest[16], /* message digest */
216
+ MD5_CTX *context) /* context */
217
+ {
218
+ unsigned char bits[8];
219
+ unsigned int idx, padLen;
220
+
221
+ /* Save number of bits */
222
+ Encode (bits, context->count, 8);
223
+
224
+ /* Pad out to 56 mod 64.
225
+ */
226
+ idx = (unsigned int)((context->count[0] >> 3) & 0x3f);
227
+ padLen = (idx < 56) ? (56 - idx) : (120 - idx);
228
+ MD5Update (context, PADDING, padLen);
229
+
230
+ /* Append length (before padding) */
231
+ MD5Update (context, bits, 8);
232
+
233
+ /* Store state in digest */
234
+ Encode (digest, context->state, 16);
235
+
236
+ /* Zeroize sensitive information.
237
+ */
238
+ memset((POINTER)context, 0, sizeof (*context));
239
+ }
240
+
241
+ /* MD5 basic transformation. Transforms state based on block.
242
+ */
243
+ static void MD5Transform (
244
+ UINT4 state[4],
245
+ unsigned char block[64])
246
+ {
247
+ UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
248
+
249
+ Decode (x, block, 64);
250
+
251
+ /* Round 1 */
252
+ FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
253
+ FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
254
+ FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
255
+ FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
256
+ FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
257
+ FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
258
+ FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
259
+ FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
260
+ FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
261
+ FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
262
+ FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
263
+ FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
264
+ FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
265
+ FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
266
+ FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
267
+ FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
268
+
269
+ /* Round 2 */
270
+ GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
271
+ GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
272
+ GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
273
+ GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
274
+ GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
275
+ GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
276
+ GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
277
+ GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
278
+ GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
279
+ GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
280
+ GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
281
+ GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
282
+ GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
283
+ GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
284
+ GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
285
+ GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
286
+
287
+ /* Round 3 */
288
+ HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
289
+ HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
290
+ HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
291
+ HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
292
+ HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
293
+ HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
294
+ HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
295
+ HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
296
+ HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
297
+ HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
298
+ HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
299
+ HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
300
+ HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
301
+ HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
302
+ HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
303
+ HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
304
+
305
+ /* Round 4 */
306
+ II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
307
+ II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
308
+ II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
309
+ II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
310
+ II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
311
+ II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
312
+ II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
313
+ II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
314
+ II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
315
+ II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
316
+ II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
317
+ II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
318
+ II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
319
+ II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
320
+ II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
321
+ II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
322
+
323
+
324
+ state[0] += a;
325
+ state[1] += b;
326
+ state[2] += c;
327
+ state[3] += d;
328
+
329
+ /* Zeroize sensitive information.
330
+ */
331
+ memset((POINTER)x, 0, sizeof (x));
332
+ }
333
+
334
+ /* Encodes input (UINT4) into output (unsigned char). Assumes len is
335
+ a multiple of 4.
336
+ */
337
+ static void Encode (
338
+ unsigned char *output,
339
+ UINT4 *input,
340
+ unsigned int len)
341
+ {
342
+ unsigned int i, j;
343
+
344
+ for (i = 0, j = 0; j < len; i++, j += 4) {
345
+ output[j] = (unsigned char)(input[i] & 0xff);
346
+ output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
347
+ output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
348
+ output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
349
+ }
350
+ }
351
+
352
+
353
+ /* Decodes input (unsigned char) into output (UINT4). Assumes len is
354
+ a multiple of 4.
355
+ */
356
+ static void Decode (
357
+ UINT4 *output,
358
+ unsigned char *input,
359
+ unsigned int len)
360
+ {
361
+ unsigned int i, j;
362
+
363
+ for (i = 0, j = 0; j < len; i++, j += 4)
364
+ output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
365
+ (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
366
+ }
367
+
368
+ uint32_t
369
+ hash_md5(const char *key, size_t key_length)
370
+ {
371
+ unsigned char results[16];
372
+
373
+ md5_signature((unsigned char*)key, (unsigned int)key_length, results);
374
+
375
+ return ((uint32_t) (results[3] & 0xFF) << 24)
376
+ | ((uint32_t) (results[2] & 0xFF) << 16)
377
+ | ((uint32_t) (results[1] & 0xFF) << 8)
378
+ | (results[0] & 0xFF);
379
+ }