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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* Simplified version of output generated by rust-bindgen 0.57.0 */
#![allow(warnings)]

#[doc = " A single UMASH params struct stores the parameters for a pair of"]
#[doc = " independent `UMASH` functions."]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct umash_params {
    pub poly: [[u64; 2usize]; 2usize],
    pub oh: [u64; 34usize],
}
#[test]
fn bindgen_test_layout_umash_params() {
    assert_eq!(
        ::std::mem::size_of::<umash_params>(),
        304usize,
        concat!("Size of: ", stringify!(umash_params))
    );
    assert_eq!(
        ::std::mem::align_of::<umash_params>(),
        8usize,
        concat!("Alignment of ", stringify!(umash_params))
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_params>())).poly as *const _ as usize },
        0usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_params),
            "::",
            stringify!(poly)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_params>())).oh as *const _ as usize },
        32usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_params),
            "::",
            stringify!(oh)
        )
    );
}
#[doc = " A fingerprint consists of two independent `UMASH` hash values."]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct umash_fp {
    pub hash: [u64; 2usize],
}
#[test]
fn bindgen_test_layout_umash_fp() {
    assert_eq!(
        ::std::mem::size_of::<umash_fp>(),
        16usize,
        concat!("Size of: ", stringify!(umash_fp))
    );
    assert_eq!(
        ::std::mem::align_of::<umash_fp>(),
        8usize,
        concat!("Alignment of ", stringify!(umash_fp))
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_fp>())).hash as *const _ as usize },
        0usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_fp),
            "::",
            stringify!(hash)
        )
    );
}
#[doc = " This struct holds the state for incremental UMASH hashing or"]
#[doc = " fingerprinting."]
#[doc = ""]
#[doc = " A sink owns no allocation, and simply borrows a pointer to its"]
#[doc = " `umash_params`.  It can be byte-copied to snapshot its state."]
#[doc = ""]
#[doc = " The layout works best with alignment to 64 bytes, but does not"]
#[doc = " require it."]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct umash_sink {
    pub poly_state: [umash_sink__bindgen_ty_1; 2usize],
    pub buf: [::std::os::raw::c_char; 32usize],
    pub oh: *const u64,
    pub oh_iter: u32,
    pub bufsz: u8,
    pub block_size: u8,
    pub large_umash: bool,
    pub hash_wanted: u8,
    pub oh_acc: umash_sink_umash_oh,
    pub oh_twisted: umash_sink_umash_twisted_oh,
    pub seed: u64,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct umash_sink__bindgen_ty_1 {
    pub mul: [u64; 2usize],
    pub acc: u64,
}
#[test]
fn bindgen_test_layout_umash_sink__bindgen_ty_1() {
    assert_eq!(
        ::std::mem::size_of::<umash_sink__bindgen_ty_1>(),
        24usize,
        concat!("Size of: ", stringify!(umash_sink__bindgen_ty_1))
    );
    assert_eq!(
        ::std::mem::align_of::<umash_sink__bindgen_ty_1>(),
        8usize,
        concat!("Alignment of ", stringify!(umash_sink__bindgen_ty_1))
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink__bindgen_ty_1>())).mul as *const _ as usize },
        0usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink__bindgen_ty_1),
            "::",
            stringify!(mul)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink__bindgen_ty_1>())).acc as *const _ as usize },
        16usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink__bindgen_ty_1),
            "::",
            stringify!(acc)
        )
    );
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct umash_sink_umash_oh {
    pub bits: [u64; 2usize],
}
#[test]
fn bindgen_test_layout_umash_sink_umash_oh() {
    assert_eq!(
        ::std::mem::size_of::<umash_sink_umash_oh>(),
        16usize,
        concat!("Size of: ", stringify!(umash_sink_umash_oh))
    );
    assert_eq!(
        ::std::mem::align_of::<umash_sink_umash_oh>(),
        8usize,
        concat!("Alignment of ", stringify!(umash_sink_umash_oh))
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink_umash_oh>())).bits as *const _ as usize },
        0usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink_umash_oh),
            "::",
            stringify!(bits)
        )
    );
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct umash_sink_umash_twisted_oh {
    pub lrc: [u64; 2usize],
    pub prev: [u64; 2usize],
    pub acc: umash_sink_umash_oh,
}
#[test]
fn bindgen_test_layout_umash_sink_umash_twisted_oh() {
    assert_eq!(
        ::std::mem::size_of::<umash_sink_umash_twisted_oh>(),
        48usize,
        concat!("Size of: ", stringify!(umash_sink_umash_twisted_oh))
    );
    assert_eq!(
        ::std::mem::align_of::<umash_sink_umash_twisted_oh>(),
        8usize,
        concat!("Alignment of ", stringify!(umash_sink_umash_twisted_oh))
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink_umash_twisted_oh>())).lrc as *const _ as usize },
        0usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink_umash_twisted_oh),
            "::",
            stringify!(lrc)
        )
    );
    assert_eq!(
        unsafe {
            &(*(::std::ptr::null::<umash_sink_umash_twisted_oh>())).prev as *const _ as usize
        },
        16usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink_umash_twisted_oh),
            "::",
            stringify!(prev)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink_umash_twisted_oh>())).acc as *const _ as usize },
        32usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink_umash_twisted_oh),
            "::",
            stringify!(acc)
        )
    );
}
#[test]
fn bindgen_test_layout_umash_sink() {
    assert_eq!(
        ::std::mem::size_of::<umash_sink>(),
        168usize,
        concat!("Size of: ", stringify!(umash_sink))
    );
    assert_eq!(
        ::std::mem::align_of::<umash_sink>(),
        8usize,
        concat!("Alignment of ", stringify!(umash_sink))
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).poly_state as *const _ as usize },
        0usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(poly_state)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).buf as *const _ as usize },
        48usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(buf)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).oh as *const _ as usize },
        80usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(oh)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).oh_iter as *const _ as usize },
        88usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(oh_iter)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).bufsz as *const _ as usize },
        92usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(bufsz)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).block_size as *const _ as usize },
        93usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(block_size)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).large_umash as *const _ as usize },
        94usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(large_umash)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).hash_wanted as *const _ as usize },
        95usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(hash_wanted)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).oh_acc as *const _ as usize },
        96usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(oh_acc)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).oh_twisted as *const _ as usize },
        112usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(oh_twisted)
        )
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_sink>())).seed as *const _ as usize },
        160usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_sink),
            "::",
            stringify!(seed)
        )
    );
}
#[doc = " The `umash_state` struct wraps a sink in a type-safe interface: we"]
#[doc = " don't want to try and extract a fingerprint from a sink configured"]
#[doc = " for hashing."]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct umash_state {
    pub sink: umash_sink,
}
#[test]
fn bindgen_test_layout_umash_state() {
    assert_eq!(
        ::std::mem::size_of::<umash_state>(),
        168usize,
        concat!("Size of: ", stringify!(umash_state))
    );
    assert_eq!(
        ::std::mem::align_of::<umash_state>(),
        8usize,
        concat!("Alignment of ", stringify!(umash_state))
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_state>())).sink as *const _ as usize },
        0usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_state),
            "::",
            stringify!(sink)
        )
    );
}
#[doc = " Similarly, the `umash_fp_state` struct wraps a sink from which we"]
#[doc = " should extract a fingerprint."]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct umash_fp_state {
    pub sink: umash_sink,
}
#[test]
fn bindgen_test_layout_umash_fp_state() {
    assert_eq!(
        ::std::mem::size_of::<umash_fp_state>(),
        168usize,
        concat!("Size of: ", stringify!(umash_fp_state))
    );
    assert_eq!(
        ::std::mem::align_of::<umash_fp_state>(),
        8usize,
        concat!("Alignment of ", stringify!(umash_fp_state))
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<umash_fp_state>())).sink as *const _ as usize },
        0usize,
        concat!(
            "Offset of field: ",
            stringify!(umash_fp_state),
            "::",
            stringify!(sink)
        )
    );
}
extern "C" {
    #[doc = " Converts a `umash_params` struct filled with random values into"]
    #[doc = " something usable by the UMASH functions below."]
    #[doc = ""]
    #[doc = " When it succeeds, this function is idempotent.  Failure happens"]
    #[doc = " with probability < 2**-110 is `params` is filled with uniformly"]
    #[doc = " distributed random bits.  That's an astronomically unlikely event,"]
    #[doc = " and most likely signals an issue with the caller's (pseudo-)random"]
    #[doc = " number generator."]
    #[doc = ""]
    #[doc = " @return false on failure, probably because the input was not random."]
    pub fn umash_params_prepare(params: *mut umash_params) -> bool;
}
extern "C" {
    #[doc = " Deterministically derives a `umash_params` struct from `bits` and"]
    #[doc = " `key`.  The `bits` values do not have to be particularly well"]
    #[doc = " distributed, and can be generated sequentially."]
    #[doc = ""]
    #[doc = " @param key a pointer to exactly 32 secret bytes.  NULL will be"]
    #[doc = "   replaced with \"Do not use UMASH VS adversaries.\", the default"]
    #[doc = "   UMASH secret."]
    pub fn umash_params_derive(
        arg1: *mut umash_params,
        bits: u64,
        key: *const ::std::os::raw::c_void,
    );
}
extern "C" {
    #[doc = " Updates a `umash_sink` to take into account `data[0 ... n_bytes)`."]
    pub fn umash_sink_update(
        arg1: *mut umash_sink,
        data: *const ::std::os::raw::c_void,
        n_bytes: ::std::os::raw::c_ulong,
    );
}
extern "C" {
    #[doc = " Computes the UMASH hash of `data[0 ... n_bytes)`."]
    #[doc = ""]
    #[doc = " Randomly generated `param` lead to independent UMASH values and"]
    #[doc = " associated worst-case collision bounds; changing the `seed` comes"]
    #[doc = " with no guarantee."]
    #[doc = ""]
    #[doc = " @param which 0 to compute the first UMASH defined by `params`, 1"]
    #[doc = "   for the second."]
    pub fn umash_full(
        params: *const umash_params,
        seed: u64,
        which: ::std::os::raw::c_int,
        data: *const ::std::os::raw::c_void,
        n_bytes: ::std::os::raw::c_ulong,
    ) -> u64;
}
extern "C" {
    #[doc = " Computes the UMASH fingerprint of `data[0 ... n_bytes)`."]
    #[doc = ""]
    #[doc = " Randomly generated `param` lead to independent UMASH values and"]
    #[doc = " associated worst-case collision bounds; changing the `seed` comes"]
    #[doc = " with no guarantee."]
    pub fn umash_fprint(
        params: *const umash_params,
        seed: u64,
        data: *const ::std::os::raw::c_void,
        n_bytes: ::std::os::raw::c_ulong,
    ) -> umash_fp;
}
extern "C" {
    #[doc = " Prepares a `umash_state` for computing the `which`th UMASH function in"]
    #[doc = " `params`."]
    pub fn umash_init(
        arg1: *mut umash_state,
        params: *const umash_params,
        seed: u64,
        which: ::std::os::raw::c_int,
    );
}
extern "C" {
    #[doc = " Returns the UMASH value for the bytes that have been"]
    #[doc = " `umash_sink_update`d into the state."]
    pub fn umash_digest(arg1: *const umash_state) -> u64;
}
extern "C" {
    #[doc = " Prepares a `umash_fp_state` for computing the UMASH fingerprint in"]
    #[doc = " `params`."]
    pub fn umash_fp_init(arg1: *mut umash_fp_state, params: *const umash_params, seed: u64);
}
extern "C" {
    #[doc = " Returns the UMASH fingerprint for the bytes that have been"]
    #[doc = " `umash_sink_update`d into the state."]
    pub fn umash_fp_digest(arg1: *const umash_fp_state) -> umash_fp;
}

#[cfg(test)]
mod tests {
    use crate::{umash_fp, umash_fprint, umash_full, umash_params, umash_params_derive};
    use std::ffi::CString;
    use std::os::raw::c_ulong;
    use std::os::raw::c_void;

    #[test]
    fn test_example_case() {
        let mut key = CString::new("hello example.c").unwrap().into_bytes();
        key.resize(32, 0u8);

        let input = CString::new("the quick brown fox").unwrap();
        let seed = 42u64;

        let mut my_params: umash_params = unsafe { std::mem::zeroed() };
        unsafe { umash_params_derive(&mut my_params, 0, key.as_ptr() as *const c_void) };

        let fprint = unsafe {
            umash_fprint(
                &my_params,
                seed,
                input.as_bytes().as_ptr() as *const c_void,
                input.as_bytes().len() as c_ulong,
            )
        };
        assert_eq!(fprint.hash, [0x398c5bb5cc113d03, 0x3a52693519575aba]);

        let hash0 = unsafe {
            umash_full(
                &my_params,
                seed,
                0,
                input.as_bytes().as_ptr() as *const c_void,
                input.as_bytes().len() as c_ulong,
            )
        };
        assert_eq!(hash0, 0x398c5bb5cc113d03);

        let hash1 = unsafe {
            umash_full(
                &my_params,
                seed,
                1,
                input.as_bytes().as_ptr() as *const c_void,
                input.as_bytes().len() as c_ulong,
            )
        };
        assert_eq!(hash1, 0x3a52693519575aba);
    }
}