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
///
/// 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.
///
/// An interfaces more suitable for calling from rust is in the [cart] module.
///

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

use cart::{JsonMap, _unpack_header};
use cart::{pack_stream, unpack_stream};
use digesters::default_digesters;
use libc::c_void;

use crate::cart::_unpack_required_header;

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 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,
    }
}


struct CFileReader {
    stream: *mut libc::FILE
}

impl std::io::Read for CFileReader {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        unsafe {
            let ptr = buf.as_mut_ptr() as *mut c_void;
            let size = libc::fread(ptr, 1, buf.len(), self.stream);
            if size == 0 {
                if libc::feof(self.stream) != 0 {
                    return Ok(size)
                } else {
                    return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, anyhow::anyhow!("Failed to read from raw file handle")))
                }
            } else {
                return Ok(size)
            }
        };
    }
}

impl CFileReader {
    fn new(stream: *mut libc::FILE) -> Self {
        Self{stream}
    }
}

struct CFileWriter {
    stream: *mut libc::FILE
}

impl std::io::Write for CFileWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        unsafe {
            let ptr = buf.as_ptr() as *const c_void;
            Ok(libc::fwrite(ptr, 1, buf.len(), self.stream))
        }
    }

    fn flush(&mut self) -> std::io::Result<()> {
        if unsafe {libc::fflush(self.stream)} == 0 {
            Ok(())
        } else {
            Err(std::io::Error::new(std::io::ErrorKind::InvalidData, anyhow::anyhow!("Failed to flush raw file handle")))
        }
    }
}

/// 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 = CFileReader{stream: input_stream};
    let input_file = std::io::BufReader::new(input_file);

    // 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,
        CFileWriter{stream: output_stream},
        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 {
    // 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 = CFileReader {stream: input_stream};
    let input_file = std::io::BufReader::new(input_stream);
    let output_file = CFileWriter { stream: output_stream };

    // 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 {
    // 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 = CFileReader::new(stream);
    _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 {
    // 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 = CFileReader::new(stream);

    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 {
    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;
        }
    }
}