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
// Copyright 2020 the Tectonic Project
// Licensed under the MIT License.

#![deny(missing_docs)]

//! This crate provides a few extern "C" functions that expose the functionality
//! of the flate2 crate in a C API that can be consumed by other C/C++ code in
//! the Tectonic codebase.
//!
//! If you change the interfaces here, rerun cbindgen as described in the README!

use flate2::{Compress, Compression, Decompress, FlushCompress, FlushDecompress, Status};
use std::{
    io::{Error, ErrorKind},
    slice,
};

/// Re-export of the flate2 crate linked by this crate.
pub use flate2;

/// Outcomes of (de)flate operations.
#[repr(C)]
pub enum FlateResult {
    /// The operation succeeded.
    Success = 0,

    /// The operation succeeded and encountered the end of the input.
    StreamEnd = 1,

    /// The operation failed because a buffer was not big enough or full enough.
    BufError = -1,

    /// The operation failed due to an error other than the ones enumerated
    /// here.
    OtherError = -2,
}

impl From<Status> for FlateResult {
    fn from(r: Status) -> Self {
        match r {
            Status::BufError => FlateResult::BufError,
            Status::Ok => FlateResult::Success,
            Status::StreamEnd => FlateResult::StreamEnd,
        }
    }
}

/// Compress a block of data. This function maps fairly directly onto the
/// `Compress::compress` function provided by `flate2`.
///
/// Returns nonzero on error.
///
/// # Safety
///
/// This is a C API function, so it is unsafe.
#[no_mangle]
pub unsafe extern "C" fn tectonic_flate_compress(
    output_ptr: *mut u8,
    output_len: *mut u64,
    input_ptr: *const u8,
    input_len: u64,
    compression_level: u32,
) -> FlateResult {
    let mut c = Compress::new(Compression::new(compression_level), true);

    let input = slice::from_raw_parts(input_ptr, input_len as usize);
    let output = slice::from_raw_parts_mut(output_ptr, *output_len as usize);

    let (size, result) = if c.compress(input, output, FlushCompress::Finish).is_err() {
        (0, FlateResult::OtherError)
    } else {
        (c.total_out(), FlateResult::Success)
    };

    *output_len = size;
    result
}

/// Deompress a block of data. This function maps fairly directly onto the
/// `Decompress::decompress` function provided by `flate2`.
///
/// Returns nonzero on error.
///
/// # Safety
///
/// This is a C API function, so it is unsafe.
#[no_mangle]
pub unsafe extern "C" fn tectonic_flate_decompress(
    output_ptr: *mut u8,
    output_len: *mut u64,
    input_ptr: *const u8,
    input_len: u64,
) -> FlateResult {
    let mut dc = Decompress::new(true);

    let input = slice::from_raw_parts(input_ptr, input_len as usize);
    let output = slice::from_raw_parts_mut(output_ptr, *output_len as usize);

    let (size, result) = match dc.decompress(input, output, FlushDecompress::Finish) {
        Ok(status) => (dc.total_out(), status.into()),
        Err(_) => (0, FlateResult::OtherError),
    };

    *output_len = size;
    result
}

/// A progressive decompressor of a complete input stream.
struct Decompressor<'a> {
    deflate: Decompress,
    input_data: &'a [u8],
    amount_written: u64,
    done: bool,
}

impl<'a> Decompressor<'a> {
    fn decompress_chunk(&mut self, output: &mut [u8]) -> Result<usize, Error> {
        if self.done {
            return Ok(0);
        }

        let inslice = &self.input_data[self.deflate.total_in() as usize..];
        let status = self
            .deflate
            .decompress(inslice, output, FlushDecompress::None)?;
        let delta = self.deflate.total_out() - self.amount_written;
        self.amount_written = self.deflate.total_out();

        match status {
            Status::Ok => {}
            Status::StreamEnd => {
                self.done = true;
            }
            Status::BufError => {
                return Err(Error::new(
                    ErrorKind::Other,
                    "incomplete input or too-small output buffer",
                ));
            }
        }

        Ok(delta as usize)
    }
}

/// Allocate a new DEFLATE decompressor.
///
/// # Safety
///
/// This is a C API function, so it is unsafe.
#[no_mangle]
pub unsafe extern "C" fn tectonic_flate_new_decompressor(
    input_ptr: *const u8,
    input_len: u64,
) -> *mut libc::c_void {
    let input = slice::from_raw_parts(input_ptr, input_len as usize);

    let dc = Decompressor {
        deflate: Decompress::new(true),
        input_data: input,
        amount_written: 0,
        done: false,
    };

    Box::leak(Box::new(dc)) as *mut Decompressor as *mut _
}

/// Decompress some DEFLATEd data.
///
/// After calling this function, the `input_len` parameter is rewritten with the
/// total number of bytes of compressed data that have been read. The
/// `output_len` parameter is rewritten with the total number of bytes of
/// decompressed data that have been written.
///
/// Returns nonzero on error.
///
/// # Safety
///
/// This is a C API function, so it is unsafe.
#[no_mangle]
pub unsafe extern "C" fn tectonic_flate_decompress_chunk(
    handle: *mut libc::c_void,
    output_ptr: *mut u8,
    output_len: *mut u64,
) -> libc::c_int {
    let mut dc = Box::from_raw(handle as *mut Decompressor);
    let output = slice::from_raw_parts_mut(output_ptr, *output_len as usize);

    let (amount, flag) = match dc.decompress_chunk(output) {
        Ok(n) => (n, 0),
        Err(_) => (0, 1),
    };

    *output_len = amount as u64;
    Box::leak(dc);
    flag
}

/// Deallocate a DEFLATE decompressor.
///
/// # Safety
///
/// This is a C API function, so it is unsafe.
#[no_mangle]
pub unsafe extern "C" fn tectonic_flate_free_decompressor(handle: *mut libc::c_void) {
    let _dc = Box::from_raw(handle as *mut Decompressor);
    // The box will be freed as we exit.
}