-
Notifications
You must be signed in to change notification settings - Fork 32
/
objecthash.c
296 lines (251 loc) · 7.05 KB
/
objecthash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <assert.h>
#include <json-c/json.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unicode/utypes.h"
#include "unicode/localpointer.h"
#include "unicode/uset.h"
#include "unicode/unorm2.h"
#include "unicode/ustring.h"
#include "objecthash.h"
bool object_hash(/*const*/ json_object *j, byte hash[HASH_SIZE]);
static void hash_update(hash_ctx * const c, const byte * const b,
const size_t l) {
SHA256_Update(c, b, l);
}
static void hash_init(hash_ctx * const c, const byte t) {
SHA256_Init(c);
byte b[1];
b[0] = t;
hash_update(c, b, 1);
}
static void hash_final(hash_ctx * const c, hash h) {
SHA256_Final(h, c);
}
static void hash_bytes(const byte t, const byte * const b, const size_t l,
hash hash) {
hash_ctx ctx;
hash_init(&ctx, t);
hash_update(&ctx, b, l);
hash_final(&ctx, hash);
}
static int dict_comp(const void *a, const void *b) {
// note that this should always terminate in < sizeof(hash) anyway
return memcmp(a, b, 2 * sizeof(hash));
}
static bool object_hash_str(const char *str, size_t len, byte hash[HASH_SIZE]) {
hash_bytes('u', (const byte *)str, len, hash);
return true;
}
static bool object_hash_bool(bool b, hash h) {
hash_bytes('b', (byte *)(b ? "1" : "0"), 1, h);
return true;
}
static bool object_hash_dict(/*const*/ json_object * const d, hash h) {
// FIXME: there may be a better way
size_t len = 0;
{
json_object_object_foreach(d, key, val) {
++len;
// key and val are unused, but we don't want the compiler to complain.
(void)key;
(void)val;
}
}
byte *hashes = malloc(2 * len * sizeof(hash));
size_t n = 0;
json_object_object_foreach(d, key, val) {
object_hash_str(key, strlen(key), &hashes[2 * n * sizeof(hash)]);
object_hash(val, &hashes[(2 * n + 1) * sizeof(hash)]);
++n;
}
qsort(hashes, len, 2 * sizeof(hash), dict_comp);
hash_bytes('d', hashes, 2 * len * sizeof(hash), h);
free(hashes);
return true;
}
static bool object_hash_int(int64_t i, hash h) {
char buf[100];
sprintf(buf, "%" PRId64, i);
hash_bytes('i', (byte *)buf, strlen(buf), h);
return true;
}
static void float_normalize(double f, char out[1000]) {
const char * const base = out;
// special case 0
// Note that if we allowed f to end up > .5 or == 0, we'd get the same thing
if (f == 0.0) {
strcpy(out, "+0:");
return;
}
// sign
*out = '+';
if (f < 0) {
*out = '-';
f = -f;
}
++out;
// exponent
int e = 0;
while (f > 1) {
f /= 2;
e += 1;
}
while (f <= .5) {
f *= 2;
e -= 1;
}
out += sprintf(out, "%d:", e);
// mantissa
assert(f <= 1);
assert(f > .5);
while (f != 0) {
if (f >= 1) {
*out++ = '1';
f -= 1;
} else {
*out++ = '0';
}
assert (f < 1);
assert (out - base < 1000);
f *= 2;
}
*out = '\0';
}
static bool object_hash_float(const double d, hash h) {
char buf[1000];
float_normalize(d, buf);
hash_bytes('f', (byte *)buf, strlen(buf), h);
return true;
}
static bool object_hash_list(json_object *l, hash h) {
hash_ctx ctx;
hash_init(&ctx, 'l');
int len = json_object_array_length(l);
for (int n = 0; n < len; ++n) {
byte ihash[HASH_SIZE];
if (!object_hash(json_object_array_get_idx(l, n), ihash))
return false;
hash_update(&ctx, ihash, sizeof ihash);
}
hash_final(&ctx, h);
return true;
}
bool object_hash(/*const*/ json_object *j, byte hash[HASH_SIZE]) {
enum json_type type;
type = json_object_get_type(j);
switch (type) {
case json_type_boolean:
return object_hash_bool(json_object_get_boolean(j), hash);
case json_type_double:
return object_hash_float(json_object_get_double(j), hash);
case json_type_int:
return object_hash_int(json_object_get_int64(j), hash);
case json_type_string: {
const char *s = json_object_get_string(j);
return object_hash_str(s, strlen(s), hash);
}
case json_type_object:
return object_hash_dict(j, hash);
case json_type_array:
return object_hash_list(j, hash);
case json_type_null:
hash_bytes('n', NULL, 0, hash);
return true;
default:
break;
}
printf("unknown type = %d\n", type);
assert(false);
return false;
}
bool python_json_hash(const char * const json, hash hash) {
json_object * const j = json_tokener_parse(json);
return object_hash(j, hash);
}
typedef json_object *leaf_fn(json_object *j);
static json_object *apply_to_leaves(json_object *j, leaf_fn *fn) {
enum json_type type;
type = json_object_get_type(j);
switch (type) {
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_null:
case json_type_string:
return fn(j);
case json_type_object: {
json_object *r = json_object_new_object();
json_object_object_foreach(j, key, val) {
json_object *k = json_object_new_string(key);
json_object *kk = fn(k);
json_object_put(k);
const char *kkk = json_object_get_string(kk);
json_object_object_add(r, kkk, apply_to_leaves(val, fn));
json_object_put(kk);
}
return r;
}
case json_type_array: {
json_object *r = json_object_new_array();
for (int n = 0; n < json_object_array_length(j); ++n) {
json_object_array_add(r, apply_to_leaves(json_object_array_get_idx(j, n),
fn));
}
return r;
}
}
printf("type = %d\n", type);
assert(false);
return NULL;
}
static json_object *commonize_int(json_object *j) {
if (json_object_get_type(j) == json_type_int)
return json_object_new_double(json_object_get_int64(j));
return json_object_get(j);
}
json_object *commonize(json_object *j) {
return apply_to_leaves(j, commonize_int);
}
bool common_json_hash(const char * const json, hash hash) {
json_object * const j = json_tokener_parse(json);
return object_hash(commonize(j), hash);
}
static json_object *unicode_normalize_string(json_object *j) {
if (json_object_get_type(j) == json_type_string) {
const char *s = json_object_get_string(j);
UErrorCode err;
const UNormalizer2 *un = unorm2_getNFCInstance(&err);
assert(un);
assert(!err);
int32_t length;
u_strFromUTF8(NULL, 0, &length, s, strlen(s), &err);
assert(!err);
UChar *us = malloc(length * sizeof(UChar));
int32_t uslength;
u_strFromUTF8(us, length, &uslength, s, strlen(s), &err);
assert(!err);
// FIXME: surely we can find out how long the result will be?
UChar *usn = malloc(uslength * 10 * sizeof(UChar));
int32_t usnlength = unorm2_normalize(un, us, uslength, usn, 10 * uslength,
&err);
assert(!err);
assert(usnlength <= uslength * 10);
int32_t n8length;
u_strToUTF8(NULL, 0, &n8length, usn, usnlength, &err);
assert(!err);
char *n8 = malloc(n8length);
u_strToUTF8(n8, n8length, &n8length, usn, usnlength, &err);
assert(!err);
free(n8);
free(usn);
free(us);
return json_object_new_string(n8);
}
return json_object_get(j);
}
json_object *unicode_normalize(json_object *j) {
return apply_to_leaves(j, unicode_normalize_string);
}