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
|
//! OpenPGP packet parser.
//!
//! Wraps [`sequoia-openpgp::parse::PacketParser`] and [related
//! functionality].
//!
//! [`sequoia-openpgp::parse::PacketParser`]: ../../../sequoia_openpgp/parse/struct.PacketParser.html
//! [related functionality]: ../../../sequoia_openpgp/parse/index.html
use std::mem::forget;
use std::ptr;
use std::slice;
use libc::{c_char, c_int, size_t};
extern crate sequoia_openpgp as openpgp;
use super::packet::{
Packet,
};
use self::openpgp::parse::{
Parse,
PacketParserResult,
PacketParser,
PacketParserEOF,
};
use super::io::Reader;
use crate::error::Status;
use crate::MoveIntoRaw;
use crate::RefMutRaw;
pub mod stream;
/// Starts parsing OpenPGP packets stored in a `pgp_reader_t`
/// object.
///
/// This function returns a `PacketParser` for the first packet in
/// the stream.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_from_reader<'a>
(errp: Option<&mut *mut crate::error::Error>, reader: *mut Reader)
-> *mut PacketParserResult<'a> {
ffi_make_fry_from_errp!(errp);
let reader = reader.ref_mut_raw();
ffi_try_box!(PacketParser::from_reader(reader))
}
/// Starts parsing OpenPGP packets stored in a file named `path`.
///
/// This function returns a `PacketParser` for the first packet in
/// the stream.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_from_file
(errp: Option<&mut *mut crate::error::Error>, filename: *const c_char)
-> *mut PacketParserResult<'static> {
ffi_make_fry_from_errp!(errp);
let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
ffi_try_box!(PacketParser::from_file(&filename))
}
/// Starts parsing OpenPGP packets stored in a buffer.
///
/// This function returns a `PacketParser` for the first packet in
/// the stream.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_from_bytes
(errp: Option<&mut *mut crate::error::Error>, b: *const u8, len: size_t)
-> *mut PacketParserResult<'static> {
ffi_make_fry_from_errp!(errp);
assert!(!b.is_null());
let buf = unsafe {
slice::from_raw_parts(b, len as usize)
};
ffi_try_box!(PacketParser::from_bytes(buf))
}
/// Frees the packet parser result
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_result_free(
ppr: Option<&mut PacketParserResult>)
{
ffi_free!(ppr)
}
/// Frees the packet parser.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_free(pp: Option<&mut PacketParser>) {
ffi_free!(pp)
}
/// Frees the packet parser EOF object.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_eof_is_message(
errp: Option<&mut *mut crate::error::Error>,
eof: *const PacketParserEOF) -> bool
{
ffi_make_fry_from_errp!(errp);
let eof = ffi_param_ref!(eof);
ffi_try_or!(eof.is_message(), false);
true
}
/// Frees the packet parser EOF object.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_eof_free
(eof: Option<&mut PacketParserEOF>)
{
ffi_free!(eof)
}
/// Returns a reference to the packet that is being parsed.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_packet
(pp: *const PacketParser)
-> *const Packet {
let pp = ffi_param_ref!(pp);
(&pp.packet).move_into_raw()
}
/// Returns the current packet's recursion depth.
///
/// A top-level packet has a recursion depth of 0. Packets in a
/// top-level container have a recursion depth of 1, etc.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_recursion_depth
(pp: *const PacketParser)
-> u8 {
let pp = ffi_param_ref!(pp);
pp.recursion_depth() as u8
}
/// Finishes parsing the current packet and starts parsing the
/// following one.
///
/// This function finishes parsing the current packet. By
/// default, any unread content is dropped. (See
/// [`PacketParsererBuilder`] for how to configure this.) It then
/// creates a new packet parser for the following packet. If the
/// current packet is a container, this function does *not*
/// recurse into the container, but skips any packets it contains.
/// To recurse into the container, use the [`recurse()`] method.
///
/// [`PacketParsererBuilder`]: ../../../sequoia_openpgp/parse/struct.PacketParserBuilder.html
/// [`recurse()`]: fn.pgp_packet_parser_recurse.html
///
/// The return value is a tuple containing:
///
/// - A `Packet` holding the fully processed old packet;
///
/// - The old packet's recursion depth;
///
/// - A `PacketParser` holding the new packet;
///
/// - And, the recursion depth of the new packet.
///
/// A recursion depth of 0 means that the packet is a top-level
/// packet, a recursion depth of 1 means that the packet is an
/// immediate child of a top-level-packet, etc.
///
/// Since the packets are serialized in depth-first order and all
/// interior nodes are visited, we know that if the recursion
/// depth is the same, then the packets are siblings (they have a
/// common parent) and not, e.g., cousins (they have a common
/// grandparent). This is because, if we move up the tree, the
/// only way to move back down is to first visit a new container
/// (e.g., an aunt).
///
/// Using the two positions, we can compute the change in depth as
/// new_depth - old_depth. Thus, if the change in depth is 0, the
/// two packets are siblings. If the value is 1, the old packet
/// is a container, and the new packet is its first child. And,
/// if the value is -1, the new packet is contained in the old
/// packet's grandparent. The idea is illustrated below:
///
/// ```text
/// ancestor
/// | \
/// ... -n
/// |
/// grandparent
/// | \
/// parent -1
/// | \
/// packet 0
/// |
/// 1
/// ```
///
/// Note: since this function does not automatically recurse into
/// a container, the change in depth will always be non-positive.
/// If the current container is empty, this function DOES pop that
/// container off the container stack, and returns the following
/// packet in the parent container.
///
/// The items of the tuple are returned in out-parameters. If you do
/// not wish to receive the value, pass `NULL` as the parameter.
///
/// Consumes the given packet parser.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_next<'a>
(errp: Option<&mut *mut crate::error::Error>,
pp: *mut PacketParser<'a>,
old_packet: Option<&mut *mut Packet>,
ppr: Option<&mut *mut PacketParserResult<'a>>)
-> Status {
ffi_make_fry_from_errp!(errp);
let pp = ffi_param_move!(pp);
match pp.next() {
Ok((old_p, new_ppr)) => {
if let Some(p) = old_packet {
*p = old_p.move_into_raw();
}
if let Some(p) = ppr {
*p = box_raw!(new_ppr);
}
Status::Success
},
Err(e) => ffi_try_status!(Err::<(), anyhow::Error>(e)),
}
}
/// Finishes parsing the current packet and starts parsing the
/// next one, recursing if possible.
///
/// This method is similar to the [`next()`] method (see that
/// method for more details), but if the current packet is a
/// container (and we haven't reached the maximum recursion depth,
/// and the user hasn't started reading the packet's contents), we
/// recurse into the container, and return a `PacketParser` for
/// its first child. Otherwise, we return the next packet in the
/// packet stream. If this function recurses, then the new
/// packet's recursion depth will be `last_recursion_depth() + 1`;
/// because we always visit interior nodes, we can't recurse more
/// than one level at a time.
///
/// [`next()`]: fn.pgp_packet_parser_next.html
///
/// The items of the tuple are returned in out-parameters. If you do
/// not wish to receive the value, pass `NULL` as the parameter.
///
/// Consumes the given packet parser.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "C" fn pgp_packet_parser_recurse<'a>
(errp: Option<&mut *mut crate::error::Error>,
pp: *mut PacketParser<'a&
|