micropdf 0.17.0

A pure Rust PDF library - A pure Rust PDF library with fz_/pdf_ API compatibility
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
//! FFI bindings for archive operations
//!
//! Provides C-compatible API for ZIP, TAR, and directory archives.

use std::ffi::CStr;
use std::os::raw::c_char;
use std::sync::LazyLock;

use super::{Handle, HandleStore};
use crate::fitz::archive::{Archive, ArchiveFormat};

/// Global storage for archives
pub static ARCHIVES: LazyLock<HandleStore<Archive>> = LazyLock::new(HandleStore::new);

/// Open an archive from a file path
///
/// # Arguments
/// * `path` - Path to archive file or directory (null-terminated C string)
///
/// # Returns
/// Handle to the archive, or 0 on error
#[unsafe(no_mangle)]
pub extern "C" fn fz_open_archive(_ctx: Handle, path: *const c_char) -> Handle {
    if path.is_null() {
        return 0;
    }

    unsafe {
        if let Ok(path_str) = CStr::from_ptr(path).to_str() {
            if let Ok(archive) = Archive::open(path_str) {
                return ARCHIVES.insert(archive);
            }
        }
    }
    0
}

/// Open an archive from a buffer
///
/// # Arguments
/// * `data` - Pointer to buffer data
/// * `size` - Size of buffer in bytes
///
/// # Returns
/// Handle to the archive, or 0 on error
#[unsafe(no_mangle)]
pub extern "C" fn fz_open_archive_with_buffer(
    _ctx: Handle,
    data: *const u8,
    size: usize,
) -> Handle {
    if data.is_null() || size == 0 {
        return 0;
    }

    unsafe {
        let data_slice = std::slice::from_raw_parts(data, size);
        let data_vec = data_slice.to_vec();

        if let Ok(archive) = Archive::from_buffer(data_vec) {
            return ARCHIVES.insert(archive);
        }
    }
    0
}

/// Keep an archive (increment ref count)
///
/// # Arguments
/// * `archive` - Handle to the archive
///
/// # Returns
/// The same handle
#[unsafe(no_mangle)]
pub extern "C" fn fz_keep_archive(_ctx: Handle, archive: Handle) -> Handle {
    archive
}

/// Drop an archive
///
/// # Arguments
/// * `archive` - Handle to the archive
#[unsafe(no_mangle)]
pub extern "C" fn fz_drop_archive(_ctx: Handle, archive: Handle) {
    ARCHIVES.remove(archive);
}

/// Get the number of entries in an archive
///
/// # Arguments
/// * `archive` - Handle to the archive
///
/// # Returns
/// Number of entries, or -1 on error
#[unsafe(no_mangle)]
pub extern "C" fn fz_count_archive_entries(_ctx: Handle, archive: Handle) -> i32 {
    if let Some(a) = ARCHIVES.get(archive) {
        if let Ok(guard) = a.lock() {
            if let Ok(count) = guard.count_entries() {
                return count as i32;
            }
        }
    }
    -1
}

/// List an archive entry by index
///
/// # Arguments
/// * `archive` - Handle to the archive
/// * `idx` - Entry index
/// * `buf` - Buffer to write entry name into
/// * `bufsize` - Size of buffer
///
/// # Returns
/// Number of bytes written (excluding null terminator), or -1 on error
#[unsafe(no_mangle)]
pub extern "C" fn fz_list_archive_entry(
    _ctx: Handle,
    archive: Handle,
    idx: i32,
    buf: *mut c_char,
    bufsize: i32,
) -> i32 {
    if buf.is_null() || bufsize <= 0 || idx < 0 {
        return -1;
    }

    if let Some(a) = ARCHIVES.get(archive) {
        if let Ok(guard) = a.lock() {
            if let Ok(name) = guard.list_entry(idx as usize) {
                let name_bytes = name.as_bytes();
                let copy_len = name_bytes.len().min((bufsize - 1) as usize);

                unsafe {
                    std::ptr::copy_nonoverlapping(name_bytes.as_ptr(), buf as *mut u8, copy_len);
                    *buf.add(copy_len) = 0; // Null terminate
                }

                return copy_len as i32;
            }
        }
    }
    -1
}

/// Check if an archive has a specific entry
///
/// # Arguments
/// * `archive` - Handle to the archive
/// * `name` - Entry name (null-terminated C string)
///
/// # Returns
/// 1 if entry exists, 0 otherwise
#[unsafe(no_mangle)]
pub extern "C" fn fz_has_archive_entry(_ctx: Handle, archive: Handle, name: *const c_char) -> i32 {
    if name.is_null() {
        return 0;
    }

    unsafe {
        if let Ok(name_str) = CStr::from_ptr(name).to_str() {
            if let Some(a) = ARCHIVES.get(archive) {
                if let Ok(guard) = a.lock() {
                    return if guard.has_entry(name_str) { 1 } else { 0 };
                }
            }
        }
    }
    0
}

/// Read an entry from an archive
///
/// # Arguments
/// * `archive` - Handle to the archive
/// * `name` - Entry name (null-terminated C string)
///
/// # Returns
/// Handle to a buffer containing the entry data, or 0 on error
#[unsafe(no_mangle)]
pub extern "C" fn fz_read_archive_entry(
    _ctx: Handle,
    archive: Handle,
    name: *const c_char,
) -> Handle {
    if name.is_null() {
        return 0;
    }

    unsafe {
        if let Ok(name_str) = CStr::from_ptr(name).to_str() {
            if let Some(a) = ARCHIVES.get(archive) {
                if let Ok(mut guard) = a.lock() {
                    if let Ok(data) = guard.read_entry(name_str) {
                        // Create FFI buffer from data
                        let buffer = super::buffer::Buffer::from_data(&data);
                        return super::BUFFERS.insert(buffer);
                    }
                }
            }
        }
    }
    0
}

/// Get the format of an archive
///
/// # Arguments
/// * `archive` - Handle to the archive
///
/// # Returns
/// Format code: 0=unknown, 1=zip, 2=tar, 3=directory
#[unsafe(no_mangle)]
pub extern "C" fn fz_archive_format(_ctx: Handle, archive: Handle) -> i32 {
    if let Some(a) = ARCHIVES.get(archive) {
        if let Ok(guard) = a.lock() {
            return match guard.format() {
                ArchiveFormat::Unknown => 0,
                ArchiveFormat::Zip => 1,
                ArchiveFormat::Tar => 2,
                ArchiveFormat::Directory => 3,
            };
        }
    }
    0
}

/// Get all entry names from an archive
///
/// # Arguments
/// * `archive` - Handle to the archive
/// * `buf` - Buffer to write entry names into (one per line, newline-separated)
/// * `bufsize` - Size of buffer
///
/// # Returns
/// Number of bytes written, or -1 on error
#[unsafe(no_mangle)]
pub extern "C" fn fz_archive_entry_names(
    _ctx: Handle,
    archive: Handle,
    buf: *mut c_char,
    bufsize: i32,
) -> i32 {
    if buf.is_null() || bufsize <= 0 {
        return -1;
    }

    if let Some(a) = ARCHIVES.get(archive) {
        if let Ok(guard) = a.lock() {
            let names = guard.entry_names();
            let all_names = names.join("\n");
            let name_bytes = all_names.as_bytes();
            let copy_len = name_bytes.len().min((bufsize - 1) as usize);

            unsafe {
                std::ptr::copy_nonoverlapping(name_bytes.as_ptr(), buf as *mut u8, copy_len);
                *buf.add(copy_len) = 0; // Null terminate
            }

            return copy_len as i32;
        }
    }
    -1
}

/// Check if an archive is valid
#[unsafe(no_mangle)]
pub extern "C" fn fz_archive_is_valid(_ctx: Handle, archive: Handle) -> i32 {
    if ARCHIVES.get(archive).is_some() {
        1
    } else {
        0
    }
}

/// Clone an archive (increase ref count)
#[unsafe(no_mangle)]
pub extern "C" fn fz_clone_archive(_ctx: Handle, archive: Handle) -> Handle {
    fz_keep_archive(_ctx, archive)
}

/// Get archive entry size
#[unsafe(no_mangle)]
pub extern "C" fn fz_archive_entry_size(_ctx: Handle, archive: Handle, name: *const c_char) -> i32 {
    if name.is_null() {
        return -1;
    }

    let c_name = unsafe {
        match CStr::from_ptr(name).to_str() {
            Ok(s) => s,
            Err(_) => return -1,
        }
    };

    if let Some(a) = ARCHIVES.get(archive) {
        if let Ok(mut guard) = a.lock() {
            if guard.has_entry(c_name) {
                if let Ok(data) = guard.read_entry(c_name) {
                    return data.len() as i32;
                }
            }
        }
    }
    -1
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::PathBuf;

    // Helper to create a test directory with unique name
    fn create_test_dir() -> PathBuf {
        use std::time::{SystemTime, UNIX_EPOCH};

        let base_dir = std::env::temp_dir();
        // Ensure temp dir exists
        let _ = fs::create_dir_all(&base_dir);

        // Use timestamp and thread ID for uniqueness
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let thread_id = std::thread::current().id();
        let dir_name = format!("micropdf_test_archive_{}_{:?}", timestamp, thread_id);
        let dir = base_dir.join(dir_name);

        fs::create_dir_all(&dir).unwrap();

        // Create test files
        fs::write(dir.join("file1.txt"), b"Hello, World!").unwrap();
        fs::write(dir.join("file2.txt"), b"Test data").unwrap();

        dir
    }

    fn cleanup_test_dir(dir: &PathBuf) {
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn test_open_archive_directory() {
        let test_dir = create_test_dir();
        let path_str = test_dir.to_str().unwrap();
        let path_cstr = std::ffi::CString::new(path_str).unwrap();

        let archive = fz_open_archive(0, path_cstr.as_ptr());
        assert_ne!(archive, 0);

        let format = fz_archive_format(0, archive);
        assert_eq!(format, 3); // Directory format

        fz_drop_archive(0, archive);
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_has_archive_entry() {
        let test_dir = create_test_dir();
        let path_str = test_dir.to_str().unwrap();
        let path_cstr = std::ffi::CString::new(path_str).unwrap();

        let archive = fz_open_archive(0, path_cstr.as_ptr());
        assert_ne!(archive, 0);

        // Check for existing file
        let file_name = std::ffi::CString::new("file1.txt").unwrap();
        let has_entry = fz_has_archive_entry(0, archive, file_name.as_ptr());
        assert_eq!(has_entry, 1);

        // Check for non-existing file
        let missing_file = std::ffi::CString::new("nonexistent.txt").unwrap();
        let has_missing = fz_has_archive_entry(0, archive, missing_file.as_ptr());
        assert_eq!(has_missing, 0);

        fz_drop_archive(0, archive);
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_read_archive_entry() {
        let test_dir = create_test_dir();
        let path_str = test_dir.to_str().unwrap();
        let path_cstr = std::ffi::CString::new(path_str).unwrap();

        let archive = fz_open_archive(0, path_cstr.as_ptr());
        assert_ne!(archive, 0);

        // Read file
        let file_name = std::ffi::CString::new("file1.txt").unwrap();
        let buffer_handle = fz_read_archive_entry(0, archive, file_name.as_ptr());
        assert_ne!(buffer_handle, 0);

        // Get buffer size
        let size = super::super::buffer::fz_buffer_storage(0, buffer_handle, std::ptr::null_mut());
        assert_eq!(size, 13); // "Hello, World!" length

        // Clean up
        super::super::buffer::fz_drop_buffer(0, buffer_handle);
        fz_drop_archive(0, archive);
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_open_archive_with_null_path() {
        let archive = fz_open_archive(0, std::ptr::null());
        assert_eq!(archive, 0);
    }

    #[test]
    fn test_open_archive_with_buffer() {
        // Create simple test data (not a valid archive, but tests the API)
        let data = [0u8; 100];
        let archive = fz_open_archive_with_buffer(0, data.as_ptr(), data.len());
        // Should return 0 since data is not a valid archive
        assert_eq!(archive, 0);
    }

    #[test]
    fn test_count_entries_invalid_archive() {
        let count = fz_count_archive_entries(0, 9999);
        assert_eq!(count, -1);
    }

    #[test]
    fn test_list_entry_invalid_archive() {
        let mut buf = [0i8; 256];
        let len = fz_list_archive_entry(0, 9999, 0, buf.as_mut_ptr(), 256);
        assert_eq!(len, -1);
    }

    #[test]
    fn test_archive_format_invalid() {
        let format = fz_archive_format(0, 9999);
        assert_eq!(format, 0); // Unknown
    }

    #[test]
    fn test_entry_names() {
        let test_dir = create_test_dir();
        let path_str = test_dir.to_str().unwrap();
        let path_cstr = std::ffi::CString::new(path_str).unwrap();

        let archive = fz_open_archive(0, path_cstr.as_ptr());
        assert_ne!(archive, 0);

        let mut buf = [0i8; 1024];
        let len = fz_archive_entry_names(0, archive, buf.as_mut_ptr(), 1024);
        assert!(len > 0);

        // Verify we got some names
        let names = unsafe { CStr::from_ptr(buf.as_ptr()) }.to_str().unwrap();
        assert!(names.contains("file1.txt") || names.contains("file2.txt"));

        fz_drop_archive(0, archive);
        cleanup_test_dir(&test_dir);
    }
}