pith-digest 0.1.0

pith foundation: digest (zero-dep Rust)
Documentation
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//! The JNI surface of `pith-digest`: the
//! `Java_hash_pith_digest_PithDigest_*` exports the Java SDK
//! (`sdk/java`) binds its `native` methods through.
//!
//! The C ABI of [`crate::ffi`] is untouched: JNI requires exports
//! named `Java_<package>_<Class>_<method>`, so the Java-facing shims
//! live here and forward every call to the existing `pith_digest_*` C
//! exports — same status codes, same refusals, no second
//! implementation of any primitive. The module is compiled out of the
//! unit-test build (`#[cfg(all(not(test), feature = "std"))]` at the
//! registration site in `lib.rs`); the integration tests in
//! `tests/java_ffi.rs` exercise every export against a synthetic
//! environment so the coverage gate still sees the glue.
//!
//! JNI conventions of this module (the Java-side contract):
//!
//! * every export takes the JNI environment first and the receiving
//!   class second (the methods are static), then the Java arguments;
//! * the status code crosses back through a trailing one-element
//!   `int[]` — the same `PITH_OK` / `PITH_E_INVALID` /
//!   `PITH_E_REJECTED` values the C ABI returns;
//! * digests and base64 text cross back as a fresh `byte[]` — null
//!   unless the status is `PITH_OK`; base64 text is the RFC 4648
//!   ASCII bytes, so no UTF machinery is needed;
//! * scalar results (the u32 checksums, the u64 hashes) cross back as
//!   the plain `jint`/`jlong` return value, zero unless the status is
//!   `PITH_OK`;
//! * the splitmix64/xoshiro256** output sequences cross back as a
//!   fresh `long[]`; a null input array maps to `PITH_E_INVALID`,
//!   mirroring the C ABI's null-pointer rule; a null environment or
//!   status array short-circuits to a zero return without touching
//!   memory.
//!
//! The suite is zero-third-party (CI's `check-zero-deps.py` fails any
//! registry crate), so the JNI function table is hand-declared below:
//! every slot is pointer-sized and the positions are the fixed
//! `JNINativeInterface_` member order of `jni.h`. The slot indices
//! were parsed mechanically from the JDK 21 header and are validated
//! end-to-end against a live JVM every time the Java suite runs.

#![allow(unsafe_code)]
// The JNI typedefs keep the jni.h spelling (jint, jbyte, ...).
#![allow(non_camel_case_types)]

use core::ffi::c_void;

use crate::ffi::{
    PITH_E_INVALID, PITH_E_REJECTED, PITH_OK, pith_digest_adler32, pith_digest_base64_decode,
    pith_digest_base64_encode, pith_digest_crc32, pith_digest_crc32c, pith_digest_fnv1a64,
    pith_digest_hmac_sha1, pith_digest_hmac_sha256, pith_digest_hmac_sha512,
    pith_digest_murmur3_x64_128, pith_digest_sha1, pith_digest_sha256, pith_digest_sha512,
    pith_digest_splitmix64_fill, pith_digest_xoshiro256_fill, pith_digest_xxh64,
};

/// A JNI environment handle — C-mode `JNIEnv*`, a pointer to the
/// function table.
type JNIEnv = *const JniTable;

/// Any Java array reference; the glue only checks nullness before
/// handing arrays through the table.
type JArray = *mut c_void;

/// A Java `int[]` reference.
type JIntArray = *mut c_void;

/// A Java class object reference (static methods receive the class).
type JClass = *mut c_void;

/// `jbyte` per `jni.h`.
type jbyte = i8;
/// `jint`/`jsize` per `jni.h`.
type jint = i32;
/// `jlong` per `jni.h`.
type jlong = i64;

/// The JNI function-table slots this module calls.
///
/// Underscore-prefixed gap fields hold the slots between the used ones
/// (slot = field position; the four reserved pointers are part of the
/// prefix). Slot indices parsed from the JDK 21 `include/jni.h`:
/// `GetArrayLength` = 171, `NewByteArray` = 176, `NewLongArray` = 180,
/// `GetByteArrayRegion` = 200, `SetByteArrayRegion` = 208,
/// `SetIntArrayRegion` = 211, `SetLongArrayRegion` = 212.
#[repr(C)]
struct JniTable {
    /// Slots 0..=170: the four reserved pointers through
    /// `ReleaseStringUTFChars`.
    _prefix: [*mut c_void; 171],
    /// Slot 171.
    get_array_length: unsafe extern "system" fn(env: *mut JNIEnv, array: JArray) -> jint,
    /// Slots 172..=175.
    _gap_before_new_byte_array: [*mut c_void; 4],
    /// Slot 176.
    new_byte_array: unsafe extern "system" fn(env: *mut JNIEnv, len: jint) -> JArray,
    /// Slots 177..=179.
    _gap_before_new_long_array: [*mut c_void; 3],
    /// Slot 180.
    new_long_array: unsafe extern "system" fn(env: *mut JNIEnv, len: jint) -> JArray,
    /// Slots 181..=199.
    _gap_before_byte_region: [*mut c_void; 19],
    /// Slot 200.
    get_byte_array_region: unsafe extern "system" fn(
        env: *mut JNIEnv,
        array: JArray,
        start: jint,
        len: jint,
        buf: *mut jbyte,
    ),
    /// Slots 201..=207.
    _gap_before_set_byte_region: [*mut c_void; 7],
    /// Slot 208.
    set_byte_array_region: unsafe extern "system" fn(
        env: *mut JNIEnv,
        array: JArray,
        start: jint,
        len: jint,
        buf: *const jbyte,
    ),
    /// Slots 209..=210.
    _gap_before_set_int_region: [*mut c_void; 2],
    /// Slot 211.
    set_int_array_region: unsafe extern "system" fn(
        env: *mut JNIEnv,
        array: JIntArray,
        start: jint,
        len: jint,
        buf: *const jint,
    ),
    /// Slot 212.
    set_long_array_region: unsafe extern "system" fn(
        env: *mut JNIEnv,
        array: JArray,
        start: jint,
        len: jint,
        buf: *const jlong,
    ),
}

/// The function table behind an environment handle.
///
/// # Safety
///
/// `env` must be a live JNI environment pointer.
unsafe fn table<'a>(env: *mut JNIEnv) -> &'a JniTable {
    // `env` points at the function-table pointer (C-mode `JNIEnv*`):
    // deref twice to reach the table itself.
    unsafe { &**env }
}

/// Copies a Java `byte[]` through the environment into an owned
/// buffer.
///
/// # Safety
///
/// `env` must be a live JNI environment and `array` a live `byte[]`
/// reference for the duration of the call; a null array is
/// [`PITH_E_INVALID`], mirroring the C ABI's null-pointer rule.
unsafe fn java_bytes(env: *mut JNIEnv, array: JArray) -> Result<Vec<u8>, i32> {
    if array.is_null() {
        return Err(PITH_E_INVALID);
    }
    let functions = unsafe { table(env) };
    let len = unsafe { (functions.get_array_length)(env, array) };
    if len < 0 {
        return Err(PITH_E_INVALID);
    }
    let mut bytes = vec![0u8; len as usize];
    unsafe { (functions.get_byte_array_region)(env, array, 0, len, bytes.as_mut_ptr().cast()) };
    Ok(bytes)
}

/// Builds a fresh Java `byte[]` holding `bytes`, or `None` if the
/// environment refuses the allocation.
///
/// # Safety
///
/// `env` must be a live JNI environment.
unsafe fn new_java_bytes(env: *mut JNIEnv, bytes: &[u8]) -> Option<JArray> {
    let functions = unsafe { table(env) };
    let array = unsafe { (functions.new_byte_array)(env, bytes.len() as jint) };
    if array.is_null() {
        return None;
    }
    unsafe {
        (functions.set_byte_array_region)(env, array, 0, bytes.len() as jint, bytes.as_ptr().cast())
    };
    Some(array)
}

/// Builds a fresh Java `long[]` holding `values`, or `None` if the
/// environment refuses the allocation.
///
/// # Safety
///
/// `env` must be a live JNI environment.
unsafe fn new_java_longs(env: *mut JNIEnv, values: &[u64]) -> Option<JArray> {
    let functions = unsafe { table(env) };
    let array = unsafe { (functions.new_long_array)(env, values.len() as jint) };
    if array.is_null() {
        return None;
    }
    let longs: Vec<i64> = values.iter().map(|&value| value as i64).collect();
    unsafe {
        (functions.set_long_array_region)(env, array, 0, longs.len() as jint, longs.as_ptr())
    };
    Some(array)
}

/// Writes `value` into the one-element `int[]` status slot.
///
/// # Safety
///
/// `status` must be a live `int[]` of length ≥ 1 (checked by the
/// caller).
unsafe fn set_status(env: *mut JNIEnv, status: JIntArray, value: jint) {
    let functions = unsafe { table(env) };
    unsafe { (functions.set_int_array_region)(env, status, 0, 1, &value) };
}

/// Shared body of the fixed-width digest exports: copies `data`
/// through the environment once, calls `produce` with a stack
/// out-buffer of `LEN` bytes and hands the written bytes back as a
/// fresh `byte[]`.
///
/// # Safety
///
/// `env` must be a live JNI environment and `status` a live `int[]`.
unsafe fn digest_in<const LEN: usize>(
    env: *mut JNIEnv,
    status: JIntArray,
    data: JArray,
    produce: impl FnOnce(&[u8], *mut u8) -> i32,
) -> JArray {
    if env.is_null() || status.is_null() {
        return core::ptr::null_mut();
    }
    let bytes = match unsafe { java_bytes(env, data) } {
        Ok(bytes) => bytes,
        Err(status_code) => {
            unsafe { set_status(env, status, status_code) };
            return core::ptr::null_mut();
        }
    };
    let mut out = [0u8; LEN];
    let code = produce(&bytes, out.as_mut_ptr());
    if code != PITH_OK {
        unsafe { set_status(env, status, code) };
        return core::ptr::null_mut();
    }
    match unsafe { new_java_bytes(env, &out) } {
        Some(array) => {
            unsafe { set_status(env, status, PITH_OK) };
            array
        }
        None => {
            // The only failure left is the environment refusing the
            // array allocation; there is no dedicated code for it, so
            // it surfaces as a rejection, never as a panic.
            unsafe { set_status(env, status, PITH_E_REJECTED) };
            core::ptr::null_mut()
        }
    }
}

/// Shared body of the scalar exports: copies `data` through the
/// environment once and hands it to `produce`; the status crosses
/// through `status` and `PITH_E_INVALID` is returned when the
/// environment or status slot is null.
///
/// # Safety
///
/// `env` must be a live JNI environment and `status` a live `int[]`.
unsafe fn with_data(
    env: *mut JNIEnv,
    status: JIntArray,
    data: JArray,
    produce: impl FnOnce(&[u8]) -> i32,
) -> i32 {
    if env.is_null() || status.is_null() {
        return PITH_E_INVALID;
    }
    let bytes = match unsafe { java_bytes(env, data) } {
        Ok(bytes) => bytes,
        Err(status_code) => {
            unsafe { set_status(env, status, status_code) };
            return PITH_E_INVALID;
        }
    };
    produce(&bytes)
}

/// Shared body of the two seeded `long[]` fills.
///
/// # Safety
///
/// `env` must be a live JNI environment and `status` a live `int[]`.
unsafe fn longs_out(
    env: *mut JNIEnv,
    status: JIntArray,
    seed: jlong,
    count: jint,
    fill: unsafe extern "C" fn(u64, *mut u64, usize) -> i32,
) -> JArray {
    if env.is_null() || status.is_null() {
        return core::ptr::null_mut();
    }
    if count < 0 {
        unsafe { set_status(env, status, PITH_E_INVALID) };
        return core::ptr::null_mut();
    }
    let mut out = vec![0u64; count as usize];
    let code = unsafe { fill(seed as u64, out.as_mut_ptr(), out.len()) };
    if code != PITH_OK {
        unsafe { set_status(env, status, code) };
        return core::ptr::null_mut();
    }
    match unsafe { new_java_longs(env, &out) } {
        Some(array) => {
            unsafe { set_status(env, status, PITH_OK) };
            array
        }
        None => {
            unsafe { set_status(env, status, PITH_E_REJECTED) };
            core::ptr::null_mut()
        }
    }
}

/// The Java binding of [`pith_digest_sha256`]: the 32-byte digest.
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name; a public Rust
// signature over the private table type would trip
// `private_interfaces`.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_sha256Native(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    status: JIntArray,
) -> JArray {
    unsafe {
        digest_in::<32>(env, status, data, |bytes, out| {
            pith_digest_sha256(bytes.as_ptr(), bytes.len(), out)
        })
    }
}

/// The Java binding of [`pith_digest_sha1`]: the 20-byte digest.
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_sha1Native(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    status: JIntArray,
) -> JArray {
    unsafe {
        digest_in::<20>(env, status, data, |bytes, out| {
            pith_digest_sha1(bytes.as_ptr(), bytes.len(), out)
        })
    }
}

/// The Java binding of [`pith_digest_sha512`]: the 64-byte digest.
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_sha512Native(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    status: JIntArray,
) -> JArray {
    unsafe {
        digest_in::<64>(env, status, data, |bytes, out| {
            pith_digest_sha512(bytes.as_ptr(), bytes.len(), out)
        })
    }
}

/// Shared body of the three (key, data) HMAC exports.
///
/// # Safety
///
/// `env` must be a live JNI environment and `key`/`data`/`status`
/// live Java array references for the duration of the call.
unsafe fn hmac_out<const LEN: usize>(
    env: *mut JNIEnv,
    status: JIntArray,
    key: JArray,
    data: JArray,
    hmac: unsafe extern "C" fn(*const u8, usize, *const u8, usize, *mut u8) -> i32,
) -> JArray {
    if env.is_null() || status.is_null() {
        return core::ptr::null_mut();
    }
    let key_bytes = match unsafe { java_bytes(env, key) } {
        Ok(bytes) => bytes,
        Err(status_code) => {
            unsafe { set_status(env, status, status_code) };
            return core::ptr::null_mut();
        }
    };
    let data_bytes = match unsafe { java_bytes(env, data) } {
        Ok(bytes) => bytes,
        Err(status_code) => {
            unsafe { set_status(env, status, status_code) };
            return core::ptr::null_mut();
        }
    };
    let mut out = [0u8; LEN];
    let code = unsafe {
        hmac(
            key_bytes.as_ptr(),
            key_bytes.len(),
            data_bytes.as_ptr(),
            data_bytes.len(),
            out.as_mut_ptr(),
        )
    };
    if code != PITH_OK {
        unsafe { set_status(env, status, code) };
        return core::ptr::null_mut();
    }
    match unsafe { new_java_bytes(env, &out) } {
        Some(array) => {
            unsafe { set_status(env, status, PITH_OK) };
            array
        }
        None => {
            unsafe { set_status(env, status, PITH_E_REJECTED) };
            core::ptr::null_mut()
        }
    }
}

/// The Java binding of [`pith_digest_hmac_sha1`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `key`/`data`/`status`
/// live Java array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_hmacSha1Native(
    env: *mut JNIEnv,
    _class: JClass,
    key: JArray,
    data: JArray,
    status: JIntArray,
) -> JArray {
    unsafe { hmac_out::<20>(env, status, key, data, pith_digest_hmac_sha1) }
}

/// The Java binding of [`pith_digest_hmac_sha256`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `key`/`data`/`status`
/// live Java array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_hmacSha256Native(
    env: *mut JNIEnv,
    _class: JClass,
    key: JArray,
    data: JArray,
    status: JIntArray,
) -> JArray {
    unsafe { hmac_out::<32>(env, status, key, data, pith_digest_hmac_sha256) }
}

/// The Java binding of [`pith_digest_hmac_sha512`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `key`/`data`/`status`
/// live Java array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_hmacSha512Native(
    env: *mut JNIEnv,
    _class: JClass,
    key: JArray,
    data: JArray,
    status: JIntArray,
) -> JArray {
    unsafe { hmac_out::<64>(env, status, key, data, pith_digest_hmac_sha512) }
}

/// Shared body of the u32-checksum exports: the value crosses back as
/// the `jint` return, zero unless the status is `PITH_OK`.
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
unsafe fn checksum32_out(
    env: *mut JNIEnv,
    status: JIntArray,
    data: JArray,
    sum: unsafe extern "C" fn(*const u8, usize, *mut u32) -> i32,
) -> jint {
    let mut out: u32 = 0;
    if unsafe {
        with_data(env, status, data, |bytes| {
            sum(bytes.as_ptr(), bytes.len(), &mut out)
        })
    } != PITH_OK
    {
        return 0;
    }
    out as jint
}

/// The Java binding of [`pith_digest_crc32`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_crc32Native(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    status: JIntArray,
) -> jint {
    unsafe { checksum32_out(env, status, data, pith_digest_crc32) }
}

/// The Java binding of [`pith_digest_adler32`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_adler32Native(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    status: JIntArray,
) -> jint {
    unsafe { checksum32_out(env, status, data, pith_digest_adler32) }
}

/// The Java binding of [`pith_digest_crc32c`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_crc32cNative(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    status: JIntArray,
) -> jint {
    unsafe { checksum32_out(env, status, data, pith_digest_crc32c) }
}

/// Shared body of the u64-hash exports: the value crosses back as the
/// `jlong` return, zero unless the status is `PITH_OK`.
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
unsafe fn hash64_out(
    env: *mut JNIEnv,
    status: JIntArray,
    data: JArray,
    hash: unsafe extern "C" fn(*const u8, usize, *mut u64) -> i32,
) -> jlong {
    let mut out: u64 = 0;
    if unsafe {
        with_data(env, status, data, |bytes| {
            hash(bytes.as_ptr(), bytes.len(), &mut out)
        })
    } != PITH_OK
    {
        return 0;
    }
    out as jlong
}

/// The Java binding of [`pith_digest_fnv1a64`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_fnv1a64Native(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    status: JIntArray,
) -> jlong {
    unsafe { hash64_out(env, status, data, pith_digest_fnv1a64) }
}

/// The Java binding of [`pith_digest_xxh64`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_xxh64Native(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    seed: jlong,
    status: JIntArray,
) -> jlong {
    let mut out: u64 = 0;
    if unsafe {
        with_data(env, status, data, |bytes| {
            pith_digest_xxh64(bytes.as_ptr(), bytes.len(), seed as u64, &mut out)
        })
    } != PITH_OK
    {
        return 0;
    }
    out as jlong
}

/// The Java binding of [`pith_digest_murmur3_x64_128`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_murmur3X64128Native(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    seed: jint,
    status: JIntArray,
) -> JArray {
    unsafe {
        digest_in::<16>(env, status, data, |bytes, out| {
            pith_digest_murmur3_x64_128(bytes.as_ptr(), bytes.len(), seed as u32, out)
        })
    }
}

/// The Java binding of [`pith_digest_splitmix64_fill`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `status` a live `int[]`.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_splitmix64FillNative(
    env: *mut JNIEnv,
    _class: JClass,
    seed: jlong,
    count: jint,
    status: JIntArray,
) -> JArray {
    unsafe { longs_out(env, status, seed, count, pith_digest_splitmix64_fill) }
}

/// The Java binding of [`pith_digest_xoshiro256_fill`].
///
/// # Safety
///
/// `env` must be a live JNI environment and `status` a live `int[]`.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_xoshiro256FillNative(
    env: *mut JNIEnv,
    _class: JClass,
    seed: jlong,
    count: jint,
    status: JIntArray,
) -> JArray {
    unsafe { longs_out(env, status, seed, count, pith_digest_xoshiro256_fill) }
}

/// The Java binding of [`pith_digest_base64_encode`]: the encoded
/// text as its RFC 4648 ASCII bytes.
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_base64EncodeNative(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    status: JIntArray,
) -> JArray {
    if env.is_null() || status.is_null() {
        return core::ptr::null_mut();
    }
    let bytes = match unsafe { java_bytes(env, data) } {
        Ok(bytes) => bytes,
        Err(status_code) => {
            unsafe { set_status(env, status, status_code) };
            return core::ptr::null_mut();
        }
    };
    let mut out = vec![0u8; crate::base64::encoded_len(bytes.len())];
    let mut out_len: usize = 0;
    let code = unsafe {
        pith_digest_base64_encode(
            bytes.as_ptr(),
            bytes.len(),
            out.as_mut_ptr(),
            out.len(),
            &mut out_len,
        )
    };
    if code != PITH_OK {
        unsafe { set_status(env, status, code) };
        return core::ptr::null_mut();
    }
    match unsafe { new_java_bytes(env, &out[..out_len]) } {
        Some(array) => {
            unsafe { set_status(env, status, PITH_OK) };
            array
        }
        None => {
            unsafe { set_status(env, status, PITH_E_REJECTED) };
            core::ptr::null_mut()
        }
    }
}

/// The Java binding of [`pith_digest_base64_decode`]: `data` is the
/// canonical padded base64 text as its ASCII bytes.
///
/// # Safety
///
/// `env` must be a live JNI environment and `data`/`status` live Java
/// array references for the duration of the call.
//
// Private: the JVM links the export by symbol name.
#[unsafe(no_mangle)]
unsafe extern "system" fn Java_hash_pith_digest_PithDigest_base64DecodeNative(
    env: *mut JNIEnv,
    _class: JClass,
    data: JArray,
    status: JIntArray,
) -> JArray {
    if env.is_null() || status.is_null() {
        return core::ptr::null_mut();
    }
    let bytes = match unsafe { java_bytes(env, data) } {
        Ok(bytes) => bytes,
        Err(status_code) => {
            unsafe { set_status(env, status, status_code) };
            return core::ptr::null_mut();
        }
    };
    let mut out = vec![0u8; bytes.len() / 4 * 3];
    let mut out_len: usize = 0;
    let code = unsafe {
        pith_digest_base64_decode(
            bytes.as_ptr(),
            bytes.len(),
            out.as_mut_ptr(),
            out.len(),
            &mut out_len,
        )
    };
    if code != PITH_OK {
        unsafe { set_status(env, status, code) };
        return core::ptr::null_mut();
    }
    match unsafe { new_java_bytes(env, &out[..out_len]) } {
        Some(array) => {
            unsafe { set_status(env, status, PITH_OK) };
            array
        }
        None => {
            unsafe { set_status(env, status, PITH_E_REJECTED) };
            core::ptr::null_mut()
        }
    }
}