summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi/src/packet_pile.rs
blob: e1aa97fe839096d7e9d3f53a831ad6fd35a00262 (plain)
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
//! `PacketPile`s, deserialized sequences of OpenPGP messages.
//!
//!
//! Wraps [`sequoia-openpgp::PacketPile`].
//!
//! [`sequoia-openpgp::PacketPile`]: ../../sequoia_openpgp/struct.PacketPile.html

use std::slice;
use std::io::{Read, Write};
use libc::{uint8_t, c_char, size_t};

extern crate sequoia_openpgp as openpgp;
use self::openpgp::{
    parse::Parse,
    serialize::Serialize,
};

use Maybe;
use ::error::Status;

/// A `PacketPile` holds a deserialized sequence of OpenPGP messages.
///
/// Wraps [`sequoia-openpgp::PacketPile`].
///
/// [`sequoia-openpgp::PacketPile`]: ../../sequoia_openpgp/struct.PacketPile.html
#[::ffi_wrapper_type(prefix = "pgp_",
                     derive = "Clone, Debug, PartialEq, Serialize")]
pub struct PacketPile(openpgp::PacketPile);

/// Deserializes the OpenPGP message stored in a `std::io::Read`
/// object.
///
/// Although this method is easier to use to parse an OpenPGP
/// message than a `PacketParser` or a `PacketPileParser`, this
/// interface buffers the whole message in memory.  Thus, the
/// caller must be certain that the *deserialized* message is not
/// too large.
///
/// Note: this interface *does* buffer the contents of packets.
#[::ffi_catch_abort] #[no_mangle] pub extern "system"
fn pgp_packet_pile_from_reader(errp: Option<&mut *mut ::error::Error>,
                               reader: *mut Box<Read>)
                               -> Maybe<PacketPile> {
    let reader = ffi_param_ref_mut!(reader);
    openpgp::PacketPile::from_reader(reader).move_into_raw(errp)
}

/// Deserializes the OpenPGP message stored in the file named by
/// `filename`.
///
/// See `pgp_packet_pile_from_reader` for more details and caveats.
#[::ffi_catch_abort] #[no_mangle] pub extern "system"
fn pgp_packet_pile_from_file(errp: Option<&mut *mut ::error::Error>,
                             filename: *const c_char)
                             -> Maybe<PacketPile> {
    let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
    openpgp::PacketPile::from_file(&filename).move_into_raw(errp)
}

/// Deserializes the OpenPGP message stored in the provided buffer.
///
/// See `pgp_packet_pile_from_reader` for more details and caveats.
#[::ffi_catch_abort] #[no_mangle] pub extern "system"
fn pgp_packet_pile_from_bytes(errp: Option<&mut *mut ::error::Error>,
                              b: *const uint8_t, len: size_t)
                              -> Maybe<PacketPile> {
    assert!(!b.is_null());
    let buf = unsafe {
        slice::from_raw_parts(b, len as usize)
    };

    openpgp::PacketPile::from_bytes(buf).move_into_raw(errp)
}