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
extern crate libc;

use libc::{c_int, c_uint, wchar_t, c_long, c_char, c_void, c_uchar};

// ----------------- CONSTANTS ----------------- //

pub const ERAR_SUCCESS: c_int = 0;
pub const ERAR_END_ARCHIVE: c_int = 10;
pub const ERAR_NO_MEMORY: c_int = 11;
pub const ERAR_BAD_DATA: c_int = 12;
pub const ERAR_BAD_ARCHIVE: c_int = 13;
pub const ERAR_UNKNOWN_FORMAT: c_int = 14;
pub const ERAR_EOPEN: c_int = 15;
pub const ERAR_ECREATE: c_int = 16;
pub const ERAR_ECLOSE: c_int = 17;
pub const ERAR_EREAD: c_int = 18;
pub const ERAR_EWRITE: c_int = 19;
pub const ERAR_SMALL_BUF: c_int = 20;
pub const ERAR_UNKNOWN: c_int = 21;
pub const ERAR_MISSING_PASSWORD: c_int = 22;
pub const ERAR_EREFERENCE: c_int = 23;
pub const ERAR_BAD_PASSWORD: c_int = 24;

pub const RAR_OM_LIST: c_uint = 0;
pub const RAR_OM_EXTRACT: c_uint = 1;
pub const RAR_OM_LIST_INCSPLIT: c_uint = 2;

pub const RAR_SKIP: c_int = 0;
pub const RAR_TEST: c_int = 1;
pub const RAR_EXTRACT: c_int = 2;

pub const RAR_VOL_ASK: c_long = 0;
pub const RAR_VOL_NOTIFY: c_long = 1;

pub const RAR_HASH_NONE: c_uint = 0;
pub const RAR_HASH_CRC32: c_uint = 1;
pub const RAR_HASH_BLAKE2: c_uint = 2;

pub const RHDF_SPLITBEFORE: c_uint = 1 << 0; // 1, 0x1
pub const RHDF_SPLITAFTER: c_uint = 1 << 1; // 2, 0x2
pub const RHDF_ENCRYPTED: c_uint = 1 << 2; // 4, 0x4
// pub const RHDF_RESERVED: c_uint = 1 << 3; // 8, 0x8
pub const RHDF_SOLID: c_uint = 1 << 4; // 16, 0x10
pub const RHDF_DIRECTORY: c_uint = 1 << 5; // 32, 0x20

pub const UCM_CHANGEVOLUME: c_uint = 0;
pub const UCM_PROCESSDATA: c_uint = 1;
pub const UCM_NEEDPASSWORD: c_uint = 2;
pub const UCM_CHANGEVOLUMEW: c_uint = 3;
pub const UCM_NEEDPASSWORDW: c_uint = 4;

pub type ChangeVolProc = extern "C" fn(*mut c_char, c_int) -> c_int;
pub type ProcessDataProc = extern "C" fn(*mut c_uchar, c_int) -> c_int;
pub type Callback = extern "C" fn(
    c_uint,
    c_long,
    c_long,
    c_long
) -> c_int;

pub type Handle = *const c_void;

// ----------------- STRUCTS ----------------- //

#[repr(C)]
pub struct HeaderData {
    pub archive_name: [c_char; 260],
    pub filename: [c_char; 260],
    pub flags: c_uint,
    pub pack_size: c_uint,
    pub unp_size: c_uint,
    pub host_os: c_uint,
    pub file_crc: c_uint,
    pub file_time: c_uint,
    pub unp_ver: c_uint,
    pub method: c_uint,
    pub file_attr: c_uint,
    pub comment_buffer: *mut c_char,
    pub comment_buffer_size: c_uint,
    pub comment_size: c_uint,
    pub comment_state: c_uint
}

#[repr(C)]
pub struct HeaderDataEx {
    pub archive_name: [c_char; 1024],
    pub archive_name_w: [wchar_t; 1024],
    pub filename: [c_char; 1024],
    pub filename_w: [wchar_t; 1024],
    pub flags: c_uint,
    pub pack_size: c_uint,
    pub pack_size_high: c_uint,
    pub unp_size: c_uint,
    pub unp_size_high: c_uint,
    pub host_os: c_uint,
    pub file_crc: c_uint,
    pub file_time: c_uint,
    pub unp_ver: c_uint,
    pub method: c_uint,
    pub file_attr: c_uint,
    pub comment_buffer: *mut c_char,
    pub comment_buffer_size: c_uint,
    pub comment_size: c_uint,
    pub comment_state: c_uint,
    pub dict_size: c_uint,
    pub hash_type: c_uint,
    pub hash: [c_char; 32],
    pub reserved: [c_uint; 1014]
}

#[repr(C)]
pub struct OpenArchiveData {
    pub archive_name: *const c_char,
    pub open_mode: c_uint,
    pub open_result: c_uint,
    pub comment_buffer: *mut c_char,
    pub comment_buffer_size: c_uint,
    pub comment_size: c_uint,
    pub comment_state: c_uint,
}

#[repr(C)]
pub struct OpenArchiveDataEx {
    pub archive_name: *const c_char,
    pub archive_name_w: *const wchar_t,
    pub open_mode: c_uint,
    pub open_result: c_uint,
    pub comment_buffer: *mut c_char,
    pub comment_buffer_size: c_uint,
    pub comment_size: c_uint,
    pub comment_state: c_uint,
    pub flags: c_uint,
    pub callback: Option<Callback>,
    pub user_data: c_long,
    pub reserved: [c_uint; 28]
}

// ----------------- BINDINGS ----------------- //

#[link(name = "unrar", kind = "static")]
extern "C" {
    pub fn RAROpenArchive(data: *mut OpenArchiveData) -> Handle;

    pub fn RAROpenArchiveEx(data: *mut OpenArchiveDataEx) -> Handle;

    pub fn RARCloseArchive(handle: Handle) -> c_int;

    pub fn RARReadHeader(
        handle: Handle,
        header_data: *mut HeaderData
    ) -> c_int;

    pub fn RARReadHeaderEx(
        handle: Handle,
        header_data: *mut HeaderDataEx
    ) -> c_int;

    pub fn RARProcessFile(
        handle: Handle,
        operation: c_int,
        dest_path: *const c_char,
        dest_name: *const c_char
    ) -> c_int;

    pub fn RARProcessFileW(
        handle: Handle,
        operation: c_int,
        dest_path: *const wchar_t,
        dest_name: *const wchar_t
    ) -> c_int;

    pub fn RARSetCallback(
        handle: Handle,
        callback: Callback,
        user_data: c_long
    );

    pub fn RARSetChangeVolProc(
        handle: Handle,
        change_vol_proc: ChangeVolProc
    );

    pub fn RARSetProcessDataProc(
        handle: Handle,
        process_data_proc: ProcessDataProc
    );

    pub fn RARSetPassword(
        handle: Handle,
        password: *const c_char
    );

    pub fn RARGetDllVersion() -> c_int;
}

// ----------------- MINIMAL ABSTRACTIONS ----------------- //

impl Default for HeaderData {
    fn default() -> Self {
        HeaderData {
            archive_name: [0; 260],
            filename: [0; 260],
            flags: 0,
            pack_size: 0,
            unp_size: 0,
            host_os: 0,
            file_crc: 0,
            file_time: 0,
            unp_ver: 0,
            method: 0,
            file_attr: 0,
            comment_buffer: 0 as *mut _,
            comment_buffer_size: 0,
            comment_size: 0,
            comment_state: 0
        }
    }
}

impl Default for HeaderDataEx {
    fn default() -> Self {
        HeaderDataEx {
            archive_name: [0; 1024],
            archive_name_w: [0; 1024],
            filename: [0; 1024],
            filename_w: [0; 1024],
            flags: 0,
            pack_size: 0,
            pack_size_high: 0,
            unp_size: 0,
            unp_size_high: 0,
            host_os: 0,
            file_crc: 0,
            file_time: 0,
            unp_ver: 0,
            method: 0,
            file_attr: 0,
            comment_buffer: 0 as *mut _,
            comment_buffer_size: 0,
            comment_size: 0,
            comment_state: 0,
            dict_size: 0,
            hash_type: 0,
            hash: [0; 32],
            reserved: [0; 1014]
        }
    }
}

impl OpenArchiveData {
    pub fn new(archive: *const c_char, mode: c_uint) -> Self {
        Self::with_comment_buffer(archive, mode, 0 as *mut _, 0)
    }

    pub fn with_comment_buffer(
        archive_name: *const c_char,
        open_mode: c_uint,
        buffer: *mut c_char,
        buffer_size: c_uint
    ) -> Self {
        OpenArchiveData {
            archive_name: archive_name,
            open_mode: open_mode,
            comment_buffer: buffer,
            comment_buffer_size: buffer_size,
            // set by library:
            open_result: 0,
            comment_size: 0,
            comment_state: 0
        }
    }
}

impl Default for OpenArchiveDataEx {
    fn default() -> Self {
        OpenArchiveDataEx {
            archive_name: 0 as *const _,
            archive_name_w: 0 as *const _,
            open_mode: 0,
            open_result: 0,
            comment_buffer: 0 as *mut _,
            comment_buffer_size: 0,
            comment_size: 0,
            comment_state: 0,
            flags: 0,
            callback: None,
            user_data: 0,
            reserved: [0; 28]
        }
    }
}

// ----------------- TESTS ----------------- //

#[cfg(test)]
mod tests {
    #[test]
    fn test_version() {
        assert_eq!(unsafe{super::RARGetDllVersion()}, 7);
    }
}