[][src]Function sequoia_openpgp_ffi::armor::pgp_armor_reader_from_bytes

#[no_mangle]
pub extern "C" fn pgp_armor_reader_from_bytes(
    b: *const u8,
    len: size_t,
    mode: c_int
) -> *mut Reader

Creates a Reader from a buffer.

C Declaration

pgp_reader_t
pgp_armor_reader_from_bytes (const uint8_t *b,
                             size_t len,
                             int mode);

Example

#include <assert.h>
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sequoia/openpgp.h>

const char *armored =
  "-----BEGIN PGP ARMORED FILE-----\n"
  "Key0: Value0\n"
  "Key1: Value1\n"
  "\n"
  "SGVsbG8gd29ybGQh\n"
  "=s4Gu\n"
  "-----END PGP ARMORED FILE-----\n";

pgp_reader_t armor =
    pgp_armor_reader_from_bytes ((uint8_t *) armored, strlen (armored),
    PGP_ARMOR_KIND_ANY);

pgp_error_t err;
pgp_armor_header_t header;
size_t header_len;
header = pgp_armor_reader_headers (&err, armor, &header_len);
if (header == NULL)
  error (1, 0, "Getting headers failed: %s", pgp_error_to_string (err));

assert (header_len == 2);
assert (strcmp (header[0].key, "Key0") == 0
        && strcmp (header[0].value, "Value0") == 0);
assert (strcmp (header[1].key, "Key1") == 0
        && strcmp (header[1].value, "Value1") == 0);
for (size_t i = 0; i < header_len; i++)
  {
    free (header[i].key);
    free (header[i].value);
  }
free (header);

char message[12];
if (pgp_reader_read (&err, armor, (uint8_t *) message, 12) < 0)
  error (1, 0, "Reading failed: %s", pgp_error_to_string (err));

assert (pgp_armor_reader_kind (armor) == PGP_ARMOR_KIND_FILE);
assert (memcmp (message, "Hello world!", 12) == 0);

pgp_reader_free (armor);