plist_ffi 0.1.6

C FFI for the amazing plist crate, compatible with libplist
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
// Jackson Coxson

use std::{
    ffi::{CStr, CString, c_char},
    ptr::null_mut,
};

use plist::Value;

use crate::{NodeType, PlistWrapper, plist_dict_iter, plist_err_t, plist_t};

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_get_size(node: plist_t) -> u32 {
    if let Value::Dictionary(d) = unsafe { &mut *node }.borrow_self() {
        d.len() as u32
    } else {
        0
    }
}

/// Since the free function accepts an iterator, the root objects
/// has to be iterator compatible.
/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_new_iter(_node: plist_t, iter: *mut plist_dict_iter) {
    let p = PlistWrapper::new_iterator(0).into_ptr();
    unsafe { *iter = p };
}

/// # Safety
/// Don't pass a bad plist >:(
/// Use the system allocator or else
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_next_item(
    node: plist_t,
    iter: plist_dict_iter,
    key: *mut *mut c_char,
    item: *mut plist_t,
) {
    let wrapper = unsafe { &mut *node };
    let node = wrapper.borrow_self();

    if let Value::Dictionary(d) = node {
        let iter = unsafe { &mut *iter }.iter_next();

        if iter as usize >= d.len() {
            unsafe { *item = null_mut() };
            return;
        }
        let (p_key, p) = d.iter_mut().nth(iter as usize).unwrap();
        let p_key = p_key.to_string();
        let pc_key = CString::new(p_key.as_str()).unwrap();
        let p = PlistWrapper {
            node: NodeType::Child {
                node: p as *mut Value,
                parent: node as *mut Value,
                index: u32::MAX,
                key: Some(p_key),
            },
            children_wrappers: Vec::new(),
        }
        .into_ptr();
        wrapper.children_wrappers.push(p);
        unsafe {
            *item = p;
            *key = pc_key.into_raw();
        };
    }
}

/// # Safety
/// Don't pass a bad plist >:(
/// Use the system allocator or else
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_get_item_key(node: plist_t, k: *mut *mut c_char) {
    let node = unsafe { &mut *node };
    match &node.node {
        NodeType::Node(_) => {}
        NodeType::Child { key, .. } => {
            if let Some(key) = key {
                let key = CString::new(key.as_str()).unwrap().into_raw();
                unsafe { *k = key };
            }
        }
        NodeType::Iterator(_) => panic!("you passed an iterator as a node"),
    };
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_get_item(node: plist_t, key: *const c_char) -> plist_t {
    let key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
    let wrapper = unsafe { &mut *node };
    let node = wrapper.borrow_self();
    if let Value::Dictionary(d) = node
        && let Some(v) = d.get_mut(key)
    {
        let p = PlistWrapper {
            node: NodeType::Child {
                node: v as *mut Value,
                parent: node as *mut Value,
                index: u32::MAX,
                key: Some(key.to_string()),
            },
            children_wrappers: Vec::new(),
        }
        .into_ptr();
        wrapper.children_wrappers.push(p);
        return p;
    }
    null_mut()
}

/// We don't have a key plist type in the plist crate
/// We'll just assume the caller knows what they're doing.
/// Blazing fast trust 🚀
/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_item_get_key(node: plist_t) -> plist_t {
    let node = unsafe { &mut *node };
    match &node.node {
        NodeType::Node(_) => null_mut(),
        NodeType::Child { key, .. } => {
            if let Some(key) = key {
                let p = Value::String(key.to_string());
                let p = PlistWrapper::new_node(p);
                p.into_ptr()
            } else {
                null_mut()
            }
        }
        NodeType::Iterator(_) => panic!("you passed an iterator as a node"),
    }
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_set_item(node: plist_t, key: *const c_char, item: plist_t) {
    let wrapper = unsafe { &mut *node };
    let key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
    let node = wrapper.borrow_self();
    if let Value::Dictionary(d) = node {
        let item = unsafe { *Box::from_raw(item) };
        d.insert(
            key.to_string(),
            item.consume().expect("you tried to steal a child"),
        );
    }
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_remove_item(node: plist_t, key: *const c_char) {
    let wrapper = unsafe { &mut *node };
    let key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
    let node = wrapper.borrow_self();
    if let Value::Dictionary(d) = node {
        d.remove(key).expect("item doesn't exist");
    }
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_merge(target: *mut plist_t, source: plist_t) {
    let target = unsafe { &mut **target };
    let source = unsafe { *Box::from_raw(source) }
        .consume()
        .expect("you tried to steal a child");
    let node = target.borrow_self();

    if let Value::Dictionary(d_target) = node
        && let Value::Dictionary(d_source) = source
    {
        d_target.extend(d_source);
    }

    // no need to change the pointer since we modified the target in-memory
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_get_bool(dict: plist_t, key: *const c_char) -> u8 {
    let node = unsafe { &mut *dict }.borrow_self();
    let key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();

    if let Value::Dictionary(d) = node {
        match internal_get_bool(d, key) {
            Some(true) => 1,
            Some(false) => 0,
            None => 0,
        }
    } else {
        0
    }
}

fn internal_get_bool(d: &mut plist::Dictionary, key: &str) -> Option<bool> {
    /* The value node can be of type #PLIST_BOOLEAN, but also
     * #PLIST_STRING (either 'true' or 'false'),
     * #PLIST_INT with a numerical value of 0 or >= 1,
     * or #PLIST_DATA with a single byte with a value of 0 or >= 1.
     */
    if let Some(d) = d.get(key) {
        match d {
            Value::Boolean(b) => Some(*b),
            Value::Data(d) => {
                if d.len() != 1 {
                    return None;
                }
                if d[0] < 1 { Some(false) } else { Some(true) }
            }
            Value::Integer(i) => {
                if let Some(i) = i.as_signed() {
                    if i < 1 { Some(false) } else { Some(true) }
                } else if let Some(i) = i.as_unsigned() {
                    if i < 1 { Some(false) } else { Some(true) }
                } else {
                    None
                }
            }
            Value::String(s) => match s.to_lowercase().as_str() {
                "true" => Some(true),
                "false" => Some(false),
                _ => None,
            },
            _ => None,
        }
    } else {
        None
    }
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_get_int(dict: plist_t, key: *const c_char) -> i64 {
    let node = unsafe { &mut *dict }.borrow_self();
    let key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();

    if let Value::Dictionary(d) = node {
        internal_get_i64(d, key).unwrap_or_default()
    } else {
        0
    }
}

fn internal_get_i64(d: &mut plist::Dictionary, key: &str) -> Option<i64> {
    // * The value node can be of type #PLIST_INT, but also
    // * #PLIST_STRING with a numerical value as string (decimal or hexadecimal),
    // * or #PLIST_DATA with a size of 1, 2, 4, or 8 bytes in little endian byte order.
    if let Some(d) = d.get(key) {
        match d {
            Value::Data(d) => {
                if d.len() == 1 {
                    Some(d[0] as i64)
                } else if d.len() == 2 {
                    Some(i16::from_le_bytes([d[0], d[1]]) as i64)
                } else if d.len() == 4 {
                    Some(i32::from_le_bytes([d[0], d[1], d[2], d[3]]) as i64)
                } else if d.len() == 8 {
                    Some(i64::from_le_bytes([
                        d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7],
                    ]))
                } else {
                    None
                }
            }
            Value::Integer(i) => {
                if let Some(i) = i.as_signed() {
                    Some(i)
                } else if let Some(i) = i.as_unsigned() {
                    i64::try_from(i).ok()
                } else {
                    None
                }
            }
            Value::String(s) => {
                if let Ok(s) = s.parse() {
                    Some(s)
                } else {
                    i64::from_str_radix(s, 16).ok()
                }
            }
            _ => None,
        }
    } else {
        None
    }
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_get_uint(dict: plist_t, key: *const c_char) -> u64 {
    let node = unsafe { &mut *dict }.borrow_self();
    let key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();

    if let Value::Dictionary(d) = node {
        internal_get_u64(d, key).unwrap_or_default()
    } else {
        0
    }
}

fn internal_get_u64(d: &mut plist::Dictionary, key: &str) -> Option<u64> {
    // * The value node can be of type #PLIST_INT, but also
    // * #PLIST_STRING with a numerical value as string (decimal or hexadecimal),
    // * or #PLIST_DATA with a size of 1, 2, 4, or 8 bytes in little endian byte order.
    if let Some(d) = d.get(key) {
        match d {
            Value::Data(d) => {
                if d.len() == 1 {
                    Some(d[0] as u64)
                } else if d.len() == 2 {
                    Some(u16::from_le_bytes([d[0], d[1]]) as u64)
                } else if d.len() == 4 {
                    Some(u32::from_le_bytes([d[0], d[1], d[2], d[3]]) as u64)
                } else if d.len() == 8 {
                    Some(u64::from_le_bytes([
                        d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7],
                    ]))
                } else {
                    None
                }
            }
            Value::Integer(i) => {
                if let Some(i) = i.as_unsigned() {
                    Some(i)
                } else if let Some(i) = i.as_signed() {
                    u64::try_from(i).ok()
                } else {
                    None
                }
            }
            Value::String(s) => {
                if let Ok(s) = s.parse() {
                    Some(s)
                } else {
                    u64::from_str_radix(s, 16).ok()
                }
            }
            _ => None,
        }
    } else {
        None
    }
}

/**
 * Copy a node from *source_dict* to *target_dict*.
 * The node is looked up in *source_dict* with given *key*, unless *alt_source_key*
 * is non-NULL, in which case it is looked up with *alt_source_key*.
 * The entry in *target_dict* is **always** created with *key*.
 *
 * @param target_dict The target dictionary to copy to.
 * @param source_dict The source dictionary to copy from.
 * @param key The key for the node to copy.
 * @param alt_source_key The alternative source key for lookup in *source_dict* or NULL.
 *
 * @result PLIST_ERR_SUCCESS on success or PLIST_ERR_INVALID_ARG if the source dictionary does not contain
 *     any entry with given key or alt_source_key.
 */
/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_copy_item(
    target_plist: plist_t,
    source_plist: plist_t,
    key: *const c_char,
    alt_source_key: *const c_char,
) -> plist_err_t {
    let target_plist = unsafe { &mut *target_plist }.borrow_self();
    let source_plist = unsafe { &mut *source_plist }.borrow_self();
    let insert_key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
    let lookup_key = if alt_source_key.is_null() {
        insert_key
    } else {
        unsafe { CStr::from_ptr(alt_source_key) }.to_str().unwrap()
    };

    if let Value::Dictionary(d_target) = target_plist
        && let Value::Dictionary(d_source) = source_plist
    {
        if let Some(to_copy) = d_source.get(lookup_key) {
            let to_copy = to_copy.clone();
            d_target.insert(insert_key.to_string(), to_copy);
            plist_err_t::PLIST_ERR_SUCCESS
        } else {
            plist_err_t::PLIST_ERR_INVALID_ARG
        }
    } else {
        plist_err_t::PLIST_ERR_INVALID_ARG
    }
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_copy_bool(
    target_plist: plist_t,
    source_plist: plist_t,
    key: *const c_char,
    alt_source_key: *const c_char,
) -> plist_err_t {
    let lookup_key = if alt_source_key.is_null() {
        key
    } else {
        alt_source_key
    };
    let lookup_key = unsafe { CStr::from_ptr(lookup_key) }.to_str().unwrap();
    let insert_key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
    let target_plist = unsafe { &mut *target_plist }.borrow_self();
    let source_plist = unsafe { &mut *source_plist }.borrow_self();
    if let Value::Dictionary(d_target) = target_plist
        && let Value::Dictionary(d_source) = source_plist
    {
        match internal_get_bool(d_source, lookup_key) {
            Some(b) => {
                let p = Value::Boolean(b);
                d_target.insert(insert_key.to_string(), p);
                plist_err_t::PLIST_ERR_SUCCESS
            }
            None => plist_err_t::PLIST_ERR_INVALID_ARG,
        }
    } else {
        plist_err_t::PLIST_ERR_INVALID_ARG
    }
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_copy_int(
    target_plist: plist_t,
    source_plist: plist_t,
    key: *const c_char,
    alt_source_key: *const c_char,
) -> plist_err_t {
    let lookup_key = if alt_source_key.is_null() {
        key
    } else {
        alt_source_key
    };
    let lookup_key = unsafe { CStr::from_ptr(lookup_key) }.to_str().unwrap();
    let insert_key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
    let target_plist = unsafe { &mut *target_plist }.borrow_self();
    let source_plist = unsafe { &mut *source_plist }.borrow_self();
    if let Value::Dictionary(d_target) = target_plist
        && let Value::Dictionary(d_source) = source_plist
    {
        match internal_get_i64(d_source, lookup_key) {
            Some(i) => {
                let p = Value::Integer(i.into());
                d_target.insert(insert_key.to_string(), p);
                plist_err_t::PLIST_ERR_SUCCESS
            }
            None => plist_err_t::PLIST_ERR_INVALID_ARG,
        }
    } else {
        plist_err_t::PLIST_ERR_INVALID_ARG
    }
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_copy_uint(
    target_plist: plist_t,
    source_plist: plist_t,
    key: *const c_char,
    alt_source_key: *const c_char,
) -> plist_err_t {
    let lookup_key = if alt_source_key.is_null() {
        key
    } else {
        alt_source_key
    };
    let lookup_key = unsafe { CStr::from_ptr(lookup_key) }.to_str().unwrap();
    let insert_key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
    let target_plist = unsafe { &mut *target_plist }.borrow_self();
    let source_plist = unsafe { &mut *source_plist }.borrow_self();
    if let Value::Dictionary(d_target) = target_plist
        && let Value::Dictionary(d_source) = source_plist
    {
        match internal_get_u64(d_source, lookup_key) {
            Some(i) => {
                let p = Value::Integer(i.into());
                d_target.insert(insert_key.to_string(), p);
                plist_err_t::PLIST_ERR_SUCCESS
            }
            None => plist_err_t::PLIST_ERR_INVALID_ARG,
        }
    } else {
        plist_err_t::PLIST_ERR_INVALID_ARG
    }
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_copy_data(
    target_plist: plist_t,
    source_plist: plist_t,
    key: *const c_char,
    alt_source_key: *const c_char,
) -> plist_err_t {
    let lookup_key = if alt_source_key.is_null() {
        key
    } else {
        alt_source_key
    };
    let lookup_key = unsafe { CStr::from_ptr(lookup_key) }.to_str().unwrap();
    let insert_key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
    let target_plist = unsafe { &mut *target_plist }.borrow_self();
    let source_plist = unsafe { &mut *source_plist }.borrow_self();
    if let Value::Dictionary(d_target) = target_plist
        && let Value::Dictionary(d_source) = source_plist
    {
        if let Some(Value::Data(d)) = d_source.get(lookup_key) {
            let d = Value::Data(d.clone());
            d_target.insert(insert_key.to_string(), d);
            plist_err_t::PLIST_ERR_SUCCESS
        } else {
            plist_err_t::PLIST_ERR_INVALID_ARG
        }
    } else {
        plist_err_t::PLIST_ERR_INVALID_ARG
    }
}

/// # Safety
/// Don't pass a bad plist >:(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn plist_dict_copy_string(
    target_plist: plist_t,
    source_plist: plist_t,
    key: *const c_char,
    alt_source_key: *const c_char,
) -> plist_err_t {
    let lookup_key = if alt_source_key.is_null() {
        key
    } else {
        alt_source_key
    };
    let lookup_key = unsafe { CStr::from_ptr(lookup_key) }.to_str().unwrap();
    let insert_key = unsafe { CStr::from_ptr(key) }.to_str().unwrap();
    let target_plist = unsafe { &mut *target_plist }.borrow_self();
    let source_plist = unsafe { &mut *source_plist }.borrow_self();
    if let Value::Dictionary(d_target) = target_plist
        && let Value::Dictionary(d_source) = source_plist
    {
        if let Some(Value::String(s)) = d_source.get(lookup_key) {
            let d = Value::String(s.clone());
            d_target.insert(insert_key.to_string(), d);
            plist_err_t::PLIST_ERR_SUCCESS
        } else {
            plist_err_t::PLIST_ERR_INVALID_ARG
        }
    } else {
        plist_err_t::PLIST_ERR_INVALID_ARG
    }
}