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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//! The CaRT file format is used to store/transfer malware and it's associated metadata.
//!
//! It neuters the malware so it cannot be executed and encrypts it so anti-virus software
//! cannot flag the CaRT file as malware.
//!
//! The functions, structs, and constants in the root of the package prefixed with `cart`
//! are all exported to build a c library.
//!
//! ```c
//! #include "cart.h"
//! #include <string.h>
//!
//! int main(char** argv, int argn) {
//!     // A file to encode
//!     char* input_file = "./cart.h";
//!     char* metadata_json = "{\"hello\": \"world\"}";
//!     char* carted_file = "./cart.h.cart";
//!     char* output_file = "./cart_copy.h";
//!
//!     // Encode file
//!     if(CART_NO_ERROR != cart_pack_file_default(input_file, carted_file, metadata_json)) {
//!         return 1;
//!     }
//!
//!     // Decode file
//!     CartUnpackResult result = cart_unpack_file(carted_file, output_file);
//!     if(result.error != CART_NO_ERROR) {
//!         return 2;
//!     }
//!
//!     cart_free_unpack_result(result);
//! }
//! ```
//!
//! An interfaces more suitable for calling from rust is in the [cart] module.
//! Note that the crate is named 'cart_container' but the library is exported as 'cart'.
//!
//! ```rust
//! use anyhow::Result;
//! use ::cart::cart::{pack_stream, JsonMap, unpack_stream};
//! use tempfile;
//!
//! fn main() -> Result<()> {
//!     // A file to encode
//!     let input_file = "./readme.md";
//!     let metadata_json: JsonMap = [("hello".to_owned(), serde_json::json!("world"))].into_iter().collect();
//!     let carted_file = tempfile::NamedTempFile::new()?;
//!     let output_file = tempfile::NamedTempFile::new()?;
//!
//!     // Encode file
//!     pack_stream(
//!         std::fs::File::open(input_file)?,
//!         carted_file.as_file(),
//!         Some(metadata_json.clone()),
//!         None,
//!         cart::digesters::default_digesters(),
//!         None
//!     )?;
//!
//!     // Decode file
//!     let (header, footer) = unpack_stream(
//!         carted_file.reopen()?,
//!         output_file.as_file(),
//!         None
//!     )?;
//! 
//!     let original_content = std::fs::read(input_file)?;
//!     // the content should be preserved
//!     assert_eq!(std::fs::read(output_file.path())?, original_content);
//!     // the header should be exactly the same
//!     assert_eq!(header.unwrap(), metadata_json);
//!     // the footer should contain all the digests. we used the default set which includes length
//!     assert_eq!(footer.unwrap().get("length"), Some(&serde_json::Value::from(original_content.len().to_string())));
//! 
//!     Ok(())
//! }
//! ```

use std::ffi::c_char;
use std::ptr::{null, null_mut};

use cart::{JsonMap, unpack_header};
use cart::{pack_stream, unpack_stream};
use cutil::{CFileReader, CFileWriter};
use digesters::default_digesters;

use crate::cart::unpack_required_header;

mod cipher;
mod cutil;
pub mod cart;
pub mod digesters;

/// Error code set when a call completes without errors
pub const CART_NO_ERROR: u32 = 0;
/// Error code when a string argument could not be parsed
pub const CART_ERROR_BAD_ARGUMENT_STR: u32 = 1;
/// Error code when an input file could not be opened
pub const CART_ERROR_OPEN_FILE_READ: u32 = 2;
/// Error code when an output file could not be opened
pub const CART_ERROR_OPEN_FILE_WRITE: u32 = 3;
/// Error code when input json could not be parsed
pub const CART_ERROR_BAD_JSON_ARGUMENT: u32 = 5;
/// Error code when an unexpected null argument was passed
pub const CART_ERROR_NULL_ARGUMENT: u32 = 7;
/// Error code when an error occurs processing the input data
pub const CART_ERROR_PROCESSING: u32 = 6;

/// Helper function to convert a c string with a path into a file object
fn _open(path: *const c_char, read: bool) -> Result<std::fs::File, u32> {
    // Check for null values
    if path == null() {
        return Err(CART_ERROR_BAD_ARGUMENT_STR)
    }

    // Wrap the c strings in something safer
    let path = unsafe { std::ffi::CStr::from_ptr(path) };

    // Make sure the input strings are valid utf-8
    let path = match path.to_str() {
        Ok(path) => path,
        Err(_) => return Err(CART_ERROR_BAD_ARGUMENT_STR),
    };

    if read {
        match std::fs::File::open(path) {
            Ok(file) => Ok(file),
            Err(_) => Err(CART_ERROR_OPEN_FILE_READ),
        }
    } else {
        match std::fs::OpenOptions::new().write(true).create(true).truncate(true).open(path) {
            Ok(file) => Ok(file),
            Err(_) => Err(CART_ERROR_OPEN_FILE_WRITE),
        }
    }
}

/// Helper function to load a c string into a json map
fn _ready_json(header_json: *const c_char) -> Result<Option<JsonMap>, u32> {
    if header_json == null() {
        Ok(None)
    }  else {
        // Build a length tracked string from a null terminated string
        let header_json = unsafe { std::ffi::CStr::from_ptr(header_json) };

        // Make sure the content of the string is utf-8
        match header_json.to_str() {
            Ok(header) => {
                // Parse json out of the string
                match serde_json::from_str(header){
                    Ok(header) => Ok(Some(header)),
                    Err(_) => return Err(CART_ERROR_BAD_JSON_ARGUMENT),
                }
            },
            Err(_) => return Err(CART_ERROR_BAD_ARGUMENT_STR),
        }
    }
}


/// Cart encode a file from disk into a new file.
///
/// Encode a file in the cart format using default parameters for all optional parameters.
/// The output file will be truncated if it already exists.
/// The header json should be a json encoded string with a mapping of key value pairs.
#[no_mangle]
pub extern "C" fn cart_pack_file_default(
    input_path: *const c_char,
    output_path: *const c_char,
    header_json: *const c_char,
) -> u32 {
    // Open input file
    let input_file = match _open(input_path, true) {
        Ok(file) => file,
        Err(err) => return err,
    };
    let input_file = std::io::BufReader::new(input_file);

    // Open output file
    let output_file = match _open(output_path, false) {
        Ok(file) => file,
        Err(err) => return err,
    };

    // Load in the header json if any is set.
    let header_json = match _ready_json(header_json) {
        Ok(header) => header,
        Err(err) => return err,
    };

    // Process stream
    let result = pack_stream(
        input_file,
        output_file,
        header_json,
        None,
        default_digesters(),
        None
    );

    match result {
        Ok(_) => CART_NO_ERROR,
        Err(_) => CART_ERROR_PROCESSING,
    }
}


/// Cart encode between open libc file handles.
///
/// Encode a file in the cart format using default parameters for all optional parameters.
/// The input handle must be open for reading, the output handle must be open for writing.
/// The header json should be a json encoded string with a mapping of key value pairs.
#[no_mangle]
pub extern "C" fn cart_pack_stream_default(
    input_stream: *mut libc::FILE,
    output_stream: *mut libc::FILE,
    header_json: *const c_char,
) -> u32 {
    // Open input file
    let input_file = match CFileReader::new(input_stream) {
        Ok(input) => input,
        Err(_) => return CART_ERROR_NULL_ARGUMENT,
    };
    let input_file = std::io::BufReader::new(input_file);

    // Open output file
    let output_file = match CFileWriter::new(output_stream) {
        Ok(output) => output,
        Err(_) => return CART_ERROR_NULL_ARGUMENT,
    };

    // Load in the header json if any is set.
    let header_json = match _ready_json(header_json) {
        Ok(header) => header,
        Err(err) => return err,
    };

    // Process stream
    let result = pack_stream(
        input_file,
        output_file,
        header_json,
        None,
        default_digesters(),
        None
    );

    match result {
        Ok(_) => CART_NO_ERROR,
        Err(_) => CART_ERROR_PROCESSING,
    }
}

/// A struct returned from encoding functions that may return a buffer.
///
/// The buffer `packed` should only be set if the `error` field is set to [CART_NO_ERROR].
/// Buffers behind this structure can be released using the [cart_free_pack_result] function.
#[repr(C)]
pub struct CartPackResult {
    error: u32,
    packed: *mut u8,
    packed_size: u64,
}

impl CartPackResult {
    fn new_err(error: u32) -> Self {
        Self {
            error,
            packed: null_mut(),
            packed_size: 0,
        }
    }

    fn new(data: Vec<u8>) -> Self {
        let mut data = data.into_boxed_slice();
        let out = Self {
            error: CART_NO_ERROR,
            packed: data.as_mut_ptr(),
            packed_size: data.len() as u64,
        };
        std::mem::forget(data);
        out
    }
}

/// Cart encode a buffer.
///
/// Encode a file in the cart format using default parameters for all optional parameters.
/// The header json should be a json encoded string with a mapping of key value pairs.
#[no_mangle]
pub extern "C" fn cart_pack_data_default(
    input_buffer: *const c_char,
    input_buffer_size: usize,
    header_json: *const c_char,
) -> CartPackResult {
    if input_buffer == null() || input_buffer_size == 0 {
        return CartPackResult::new_err(CART_ERROR_NULL_ARGUMENT)
    }

    // cast c pointer to rust slice
    let input_data = unsafe {
        let input_buffer = input_buffer as *const u8;
        std::slice::from_raw_parts(input_buffer, input_buffer_size)
    };

    // Load in the header json if any is set.
    let header_json = match _ready_json(header_json) {
        Ok(header) => header,
        Err(err) => return CartPackResult::new_err(err),
    };

    // capture output data in vector
    let mut output_buffer = vec![];

    // Process stream
    let result = pack_stream(
        input_data,
        &mut output_buffer,
        header_json,
        None,
        default_digesters(),
        None
    );

    match result {
        Ok(_) => CartPackResult::new(output_buffer),
        Err(_) => CartPackResult::new_err(CART_ERROR_PROCESSING),
    }
}

/// A struct returned from decoding functions that may return a buffer.
///
/// Which buffers have a value depends on the semantics of the function returning it.
/// Buffers should only be set if the `error` field is set to [CART_NO_ERROR].
/// Buffers behind this structure can be released using the [cart_free_unpack_result] function.
#[repr(C)]
pub struct CartUnpackResult {
    error: u32,
    body: *mut u8,
    body_size: u64,
    header_json: *mut u8,
    header_json_size: u64,
    footer_json: *mut u8,
    footer_json_size: u64,
}

impl CartUnpackResult {
    fn new_err(error: u32) -> Self {
        Self {
            error,
            body: std::ptr::null_mut(),
            body_size: 0,
            header_json: std::ptr::null_mut(),
            header_json_size: 0,
            footer_json: std::ptr::null_mut(),
            footer_json_size: 0,
        }
    }

    fn str_to_ptr(mut data: Vec<u8>) -> (*mut u8, u64) {
        if !data.is_empty() {
            data.push(0);
        }
        Self::data_to_ptr(data)
    }

    fn data_to_ptr(data: Vec<u8>) -> (*mut u8, u64) {
        if data.is_empty() {
            (null_mut(), 0)
        } else {
            let mut data = data.into_boxed_slice();
            let ptr = data.as_mut_ptr();
            let len = data.len() as u64;
            std::mem::forget(data);
            (ptr, len)
        }
    }

    fn new(body: Vec<u8>, header: Option<JsonMap>, footer: Option<JsonMap>) -> Self {
        let mut out = Self::new_meta(header, footer);

        let (ptr, len) = Self::data_to_ptr(body);

        out.body = ptr;
        out.body_size = len;

        return out;
    }

    fn new_meta(header: Option<JsonMap>, footer: Option<JsonMap>) -> Self {
        let header_data = match header {
            Some(header) => serde_json::to_vec(&header).unwrap_or_default(),
            None => Default::default(),
        };
        let footer_data = match footer {
            Some(footer) => serde_json::to_vec(&footer).unwrap_or_default(),
            None => Default::default(),
        };

        let (header_json, header_json_size) = Self::str_to_ptr(header_data);
        let (footer_json, footer_json_size) = Self::str_to_ptr(footer_data);

        Self {
            error: CART_NO_ERROR,
            body: std::ptr::null_mut(),
            body_size: 0,
            header_json,
            header_json_size,
            footer_json,
            footer_json_size,
        }
    }
}

/// Decode a cart encoded file into a new file.
///
/// The decoded file body is written to the output file and is not set the returned struct.
/// The output file will be truncated if it already exists.
#[no_mangle]
pub extern "C" fn cart_unpack_file(
    input_path: *const c_char,
    output_path: *const c_char,
) -> CartUnpackResult {
    // Open input file
    let input_file = match _open(input_path, true) {
        Ok(file) => file,
        Err(err) => return CartUnpackResult::new_err(err),
    };
    let input_file = std::io::BufReader::new(input_file);

    // Open output file
    let output_file = match _open(output_path, false) {
        Ok(file) => file,
        Err(err) => return CartUnpackResult::new_err(err),
    };

    // Process stream
    let result = unpack_stream(
        input_file,
        output_file,
        None
    );

    match result {
        Ok((header, footer)) => {
            CartUnpackResult::new_meta(header, footer)
        },
        Err(_) => CartUnpackResult::new_err(CART_ERROR_PROCESSING),
    }
}

/// Decode cart data from an open libc file into another.
///
/// The decoded file body is written to the output and is not set the returned struct.
/// The input handle must be open for reading, the output handle must be open for writing.
#[no_mangle]
pub extern "C" fn cart_unpack_stream(
    input_stream: *mut libc::FILE,
    output_stream: *mut libc::FILE,
) -> CartUnpackResult {
    // Wrap the input file object
    let input_stream = match CFileReader::new(input_stream) {
        Ok(input) => input,
        Err(_) => return CartUnpackResult::new_err(CART_ERROR_NULL_ARGUMENT),
    };
    let input_file = std::io::BufReader::new(input_stream);
    let output_file = match CFileWriter::new(output_stream) {
        Ok(out) => out,
        Err(_) => return CartUnpackResult::new_err(CART_ERROR_NULL_ARGUMENT),
    };

    // Process stream
    let result = unpack_stream(
        input_file,
        output_file,
        None
    );

    match result {
        Ok((header, footer)) => {
            CartUnpackResult::new_meta(header, footer)
        },
        Err(_) => CartUnpackResult::new_err(CART_ERROR_PROCESSING),
    }
}

/// Decode cart data from a buffer.
#[no_mangle]
pub extern "C" fn cart_unpack_data (
    input_buffer: *const c_char,
    input_buffer_size: usize
) -> CartUnpackResult {
    if input_buffer == null() || input_buffer_size == 0 {
        return CartUnpackResult::new_err(CART_ERROR_NULL_ARGUMENT)
    }

    // cast c pointer to rust slice
    let input_data = unsafe {
        let input_buffer = input_buffer as *const u8;
        std::slice::from_raw_parts(input_buffer, input_buffer_size)
    };

    // Capture output in buffer
    let mut output = vec![];

    // Process stream
    let result = unpack_stream(
        input_data,
        &mut output,
        None
    );

    match result {
        Ok((header, footer)) => {
            CartUnpackResult::new(output, header, footer)
        },
        Err(_) => CartUnpackResult::new_err(CART_ERROR_PROCESSING),
    }
}

/// Test if the file at a given path contains cart data.
#[no_mangle]
pub extern "C" fn cart_is_file_cart (
    input_path: *const c_char,
) -> bool {
    // Open input file
    let input_file = match _open(input_path, true) {
        Ok(file) => file,
        Err(_) => return false,
    };

    unpack_required_header(input_file, None).is_ok()
}

/// Test if the given file object contains cart data.
///
/// The file handle is read from and is not reset to its original location.
#[no_mangle]
pub extern "C" fn cart_is_stream_cart (
    stream: *mut libc::FILE,
) -> bool {
    // Open input file
    let input_file = match CFileReader::new(stream) {
        Ok(file) => file,
        Err(_) => return false,
    };
    unpack_required_header(input_file, None).is_ok()
}

/// Test if the given buffer contains cart data.
#[no_mangle]
pub extern "C" fn cart_is_data_cart (
    data: *const c_char,
    data_size: usize,
) -> bool {
    // Refuse empty input
    if data == null() || data_size == 0 {
        return false
    }

    // cast c pointer to rust slice
    let input_data = unsafe {
        let input_buffer = data as *const u8;
        std::slice::from_raw_parts(input_buffer, data_size)
    };
    unpack_required_header(input_data, None).is_ok()
}

/// Open the cart file at the given path and read out its metadata.
///
/// In the returned struct only the header buffer will contain data.
#[no_mangle]
pub extern "C" fn cart_get_file_metadata_only(
    input_path: *const c_char
) -> CartUnpackResult {
    // Open input file
    let input_file = match _open(input_path, true) {
        Ok(file) => file,
        Err(err) => return CartUnpackResult::new_err(err),
    };

    match unpack_header(input_file, None) {
        Ok((_, header, _)) => CartUnpackResult::new_meta(header, None),
        Err(_) => CartUnpackResult::new_err(CART_ERROR_PROCESSING),
    }
}

/// Read header metadata only from a cart file object.
///
/// In the returned struct only the header buffer will contain data.
#[no_mangle]
pub extern "C" fn cart_get_stream_metadata_only(
    stream: *mut libc::FILE
) -> CartUnpackResult {
    let input_file = match CFileReader::new(stream) {
        Ok(file) => file,
        Err(_) => return CartUnpackResult::new_err(CART_ERROR_NULL_ARGUMENT),
    };

    match unpack_header(input_file, None) {
        Ok((_, header, _)) => CartUnpackResult::new_meta(header, None),
        Err(_) => CartUnpackResult::new_err(CART_ERROR_PROCESSING),
    }
}

/// Read header metadata only from a buffer of cart data.
///
/// In the returned struct only the header buffer will contain data.
#[no_mangle]
pub extern "C" fn cart_get_data_metadata_only(
    data: *const c_char,
    data_size: usize
) -> CartUnpackResult {
    if data == null() || data_size == 0 {
        return CartUnpackResult::new_err(CART_ERROR_NULL_ARGUMENT)
    }

    let input_data = unsafe {
        let input_buffer = data as *const u8;
        std::slice::from_raw_parts(input_buffer, data_size)
    };
    match unpack_header(input_data, None) {
        Ok((_, header, _)) => CartUnpackResult::new_meta(header, None),
        Err(_) => CartUnpackResult::new_err(CART_ERROR_PROCESSING),
    }
}


/// Release any resources behind a [CartUnpackResult] struct.
///
/// This function should be safe to call even if the struct has no data.
/// This function should be safe to call repeatedly on the same struct.
#[no_mangle]
pub extern "C" fn cart_free_unpack_result(mut buf: CartUnpackResult) {
    unsafe {
        if buf.body != null_mut() {
            let s = std::slice::from_raw_parts_mut(buf.body, buf.body_size as usize);
            let s = s.as_mut_ptr();
            drop(Box::from_raw(s));
            buf.body = null_mut();
            buf.body_size = 0;
        }
        if buf.header_json != null_mut() {
            let s = std::slice::from_raw_parts_mut(buf.header_json, buf.header_json_size as usize);
            let s = s.as_mut_ptr();
            drop(Box::from_raw(s));
            buf.header_json = null_mut();
            buf.header_json_size = 0;
        }
        if buf.footer_json != null_mut() {
            let s = std::slice::from_raw_parts_mut(buf.footer_json, buf.footer_json_size as usize);
            let s = s.as_mut_ptr();
            drop(Box::from_raw(s));
            buf.footer_json = null_mut();
            buf.footer_json_size = 0;
        }
    }
}

/// Release any resources behind a [CartPackResult] struct.
///
/// This function should be safe to call even if the struct has no data.
/// This function should be safe to call repeatedly on the same struct.
#[no_mangle]
pub extern "C" fn cart_free_pack_result(mut buf: CartPackResult) {
    unsafe {
        if buf.packed != null_mut() {
            let s = std::slice::from_raw_parts_mut(buf.packed, buf.packed_size as usize);
            let s = s.as_mut_ptr();
            drop(Box::from_raw(s));
            buf.packed = null_mut();
            buf.packed_size = 0;
        }
    }
}


#[cfg(test)]
mod tests {
    use std::ffi::CString;
    use std::io::{Write, Read};
    use std::ptr::{null, null_mut};

    #[cfg(unix)]
    use libc::fopen;

    #[cfg(unix)]
    use crate::cart_unpack_stream;

    use crate::{cart_pack_file_default, CART_NO_ERROR, cart_unpack_file, cart_free_unpack_result, cart_is_file_cart, cart_is_stream_cart, cart_is_data_cart, cart_unpack_data, cart_get_file_metadata_only, cart_get_stream_metadata_only, cart_get_data_metadata_only, cart_pack_stream_default, cart_pack_data_default, cart_free_pack_result};


    #[test]
    fn round_trip_file() {
        // Prepare input json
        let mut input_meta = serde_json::Map::new();
        input_meta.insert("cat".to_owned(), serde_json::to_value("dog").unwrap());
        let input_json = serde_json::to_string(&input_meta).unwrap();
        let input_json = CString::new(input_json).unwrap();

        // prepare an input
        let raw_data = std::include_bytes!("cart.rs");
        let mut input = tempfile::NamedTempFile::new().unwrap();
        input.write_all(raw_data).unwrap();
        let input_path = CString::new(input.path().to_str().unwrap()).unwrap();

        // Encode the data with cart
        let buffer = tempfile::NamedTempFile::new().unwrap();
        let buffer_path = CString::new(buffer.path().to_str().unwrap()).unwrap();
        assert_eq!(cart_pack_file_default(input_path.as_ptr(), buffer_path.as_ptr(), input_json.as_ptr()), CART_NO_ERROR);
        assert!(cart_is_file_cart(buffer_path.as_ptr()));

        // Decode the cart data
        let mut output = tempfile::NamedTempFile::new().unwrap();
        let output_path = CString::new(output.path().to_str().unwrap()).unwrap();
        let out = cart_unpack_file(buffer_path.as_ptr(), output_path.as_ptr());
        assert_eq!(out.error, CART_NO_ERROR);
        assert_eq!(out.body, null_mut());
        assert_eq!(out.body_size, 0);
        assert!(out.footer_json != null_mut());
        assert!(out.footer_json_size > 0);

        // Check the header metadata
        let output_json = unsafe { std::slice::from_raw_parts(out.header_json, out.header_json_size as usize - 1) };
        let output_meta: serde_json::Map<String, serde_json::Value> = serde_json::from_slice(output_json).unwrap();
        assert_eq!(output_meta, input_meta);

        // Check the output is decoded right
        let mut output_data = vec![];
        let bytes = output.as_file_mut().read_to_end(&mut output_data).unwrap();
        assert_eq!(bytes, raw_data.len());
        assert_eq!(output_data, raw_data);

        // Release resources
        cart_free_unpack_result(out);
    }

    #[cfg(unix)]
    #[test]
    fn round_trip_stream() {
        // prepare an input
        let raw_data = std::include_bytes!("cart.rs");
        let mut input = tempfile::NamedTempFile::new().unwrap();
        input.write_all(raw_data).unwrap();
        let input_path = CString::new(input.path().to_str().unwrap()).unwrap();
        let mode_r = CString::new("rb").unwrap();
        let input_file = unsafe {fopen(input_path.as_ptr(), mode_r.as_ptr())};

        // Encode the data with cart
        let buffer = tempfile::NamedTempFile::new().unwrap();
        let buffer_path = CString::new(buffer.path().to_str().unwrap()).unwrap();
        let mode_rw = CString::new("rwb+").unwrap();
        let buffer_file = unsafe {fopen(buffer_path.as_ptr(), mode_rw.as_ptr())};
        assert_eq!(cart_pack_stream_default(input_file, buffer_file, null()), CART_NO_ERROR);
        let buffer_file = unsafe {fopen(buffer_path.as_ptr(), mode_rw.as_ptr())};
        assert!(cart_is_stream_cart(buffer_file));

        // Decode the cart data
        let buffer_file = unsafe {fopen(buffer_path.as_ptr(), mode_rw.as_ptr())};
        let mut output = tempfile::NamedTempFile::new().unwrap();
        let output_path = CString::new(output.path().to_str().unwrap()).unwrap();
        let output_file = unsafe {fopen(output_path.as_ptr(), mode_rw.as_ptr())};
        let out = cart_unpack_stream(buffer_file, output_file);
        assert_eq!(out.error, CART_NO_ERROR);
        assert_eq!(out.body, null_mut());
        assert_eq!(out.body_size, 0);
        assert_eq!(out.header_json, null_mut());
        assert_eq!(out.header_json_size, 0);
        assert!(out.footer_json != null_mut());
        assert!(out.footer_json_size > 0);

        // Check the output is decoded right
        let mut output_data = vec![];
        let bytes = output.as_file_mut().read_to_end(&mut output_data).unwrap();
        assert_eq!(bytes, raw_data.len());
        assert_eq!(output_data, raw_data);

        // Release resources
        cart_free_unpack_result(out);
    }

    #[test]
    fn round_trip_buffer() {
        // prepare an input
        let raw_data = std::include_bytes!("cart.rs");

        // Encode the data with cart
        let packed = cart_pack_data_default(raw_data.as_ptr() as *const i8, raw_data.len(), null());
        assert_eq!(packed.error, CART_NO_ERROR);
        assert!(cart_is_data_cart(packed.packed as *const i8, packed.packed_size as usize));

        // Decode the cart data
        let out = cart_unpack_data(packed.packed as *const i8, packed.packed_size as usize);
        assert_eq!(out.error, CART_NO_ERROR);
        assert_eq!(out.header_json, null_mut());
        assert_eq!(out.header_json_size, 0);
        assert!(out.footer_json != null_mut());
        assert!(out.footer_json_size > 0);

        // Check the output is decoded right
        let output_data = unsafe { std::slice::from_raw_parts(out.body, out.body_size as usize)};
        assert_eq!(output_data, raw_data);

        // Release resources
        cart_free_pack_result(packed);
        cart_free_unpack_result(out);
    }

    #[test]
    fn bad_input_buffer() {
        // prepare an input
        let raw_data = std::include_bytes!("cart.rs");
        let bad_metadata = r#"{"name": "snake}"#;
        let bad_metadata = CString::new(bad_metadata).unwrap();

        // Encode with bad metadata json
        let packed = cart_pack_data_default(raw_data.as_ptr() as *const i8, raw_data.len(), bad_metadata.as_ptr());
        assert_ne!(packed.error, CART_NO_ERROR);

        // Decode with bad data
        assert!(!cart_is_data_cart(raw_data.as_ptr() as *const i8, raw_data.len()));
        let packed = cart_unpack_data(raw_data.as_ptr() as *const i8, raw_data.len());
        assert_ne!(packed.error, CART_NO_ERROR);
    }


    #[test]
    fn null_is_cart_calls() {
        // All functions exported should be "safe" to call with null values in any field that
        // take a pointer, it should never result in crashes, only error codes
        let test_string = CString::new("test string").unwrap();

        assert!(!cart_is_file_cart(null()));
        assert!(!cart_is_stream_cart(null_mut()));
        assert!(!cart_is_data_cart(null(), 0));
        assert!(!cart_is_data_cart(null(), 1000000));
        assert!(!cart_is_data_cart(test_string.as_ptr(), 0));
    }

    #[cfg(unix)]
    #[test]
    fn null_unpack_calls() {
        // All functions exported should be "safe" to call with null values in any field that
        // take a pointer, it should never result in crashes, only error codes
        let input = tempfile::NamedTempFile::new().unwrap();
        let test_string = CString::new(input.path().to_str().unwrap()).unwrap();
        let mode = CString::new("rw").unwrap();
        let test_file = unsafe {fopen(test_string.as_ptr(), mode.as_ptr()) };

        cart_unpack_file(null(), null());
        cart_unpack_file(test_string.as_ptr(), null());
        cart_unpack_file(null(), test_string.as_ptr());
        cart_unpack_stream(null_mut(), null_mut());
        cart_unpack_stream(test_file, null_mut());
        cart_unpack_stream(null_mut(), test_file);
        cart_unpack_data(null(), 0);
        cart_unpack_data(null(), 10000);
        cart_unpack_data(test_string.as_ptr(), 0);
    }

    #[test]
    fn null_get_metadata_calls() {
        // All functions exported should be "safe" to call with null values in any field that
        // take a pointer, it should never result in crashes, only error codes
        let input = tempfile::NamedTempFile::new().unwrap();
        let test_string = CString::new(input.path().to_str().unwrap()).unwrap();

        cart_get_file_metadata_only(null());
        cart_get_stream_metadata_only(null_mut());
        cart_get_data_metadata_only(null(), 0);
        cart_get_data_metadata_only(null(), 10000);
        cart_get_data_metadata_only(test_string.as_ptr(), 0);
    }

    #[test]
    fn null_pack_file_calls() {
        // All functions exported should be "safe" to call with null values in any field that
        // take a pointer, it should never result in crashes, only error codes
        let input = tempfile::NamedTempFile::new().unwrap();
        let test_string = CString::new(input.path().to_str().unwrap()).unwrap();

        cart_pack_file_default(null(), null(), null());
        cart_pack_file_default(test_string.as_ptr(), null(), null());
        cart_pack_file_default(null(), test_string.as_ptr(), null());
    }

    #[test]
    fn null_pack_stream_calls_0() {
        cart_pack_stream_default(null_mut(), null_mut(), null());
    }

    #[cfg(unix)]
    #[test]
    fn null_pack_stream_calls_1() {
        let input = tempfile::NamedTempFile::new().unwrap();
        let test_string = CString::new(input.path().to_str().unwrap()).unwrap();
        let mode = CString::new("rwb").unwrap();
        let test_file = unsafe {fopen(test_string.as_ptr(), mode.as_ptr()) };

        cart_pack_stream_default(test_file, null_mut(), null());
    }

    #[cfg(unix)]
    #[test]
    fn null_pack_stream_calls_2() {
        let input = tempfile::NamedTempFile::new().unwrap();
        let test_string = CString::new(input.path().to_str().unwrap()).unwrap();
        let mode = CString::new("rwb").unwrap();
        let test_file = unsafe {fopen(test_string.as_ptr(), mode.as_ptr()) };

        cart_pack_stream_default(null_mut(), test_file, null());
    }

    #[test]
    fn null_pack_data_calls() {
        // All functions exported should be "safe" to call with null values in any field that
        // take a pointer, it should never result in crashes, only error codes
        let input = tempfile::NamedTempFile::new().unwrap();
        let test_string = CString::new(input.path().to_str().unwrap()).unwrap();

        cart_pack_data_default(null(), 0, null());
        cart_pack_data_default(null(), 119990, null());
        cart_pack_data_default(test_string.as_ptr(), 0, null());
    }

}