diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-01-16 17:59:26 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-01-17 10:28:07 +0100 |
commit | 739b528ec5b1c94ee3c5c1e37bb88c810c48a0a4 (patch) | |
tree | 4219032208db41803c0d85389610931b6fc7b9d8 /ffi/examples | |
parent | 2751c19a7aa0f70d76c0581f27fca9d15214e5ff (diff) |
ffi: Do not use a context where an errp suffices.
- This prepares us for the FFI crate split.
- Fixes #158.
Diffstat (limited to 'ffi/examples')
-rw-r--r-- | ffi/examples/armor.c | 20 | ||||
-rw-r--r-- | ffi/examples/encrypt-for.c | 45 | ||||
-rw-r--r-- | ffi/examples/example.c | 15 | ||||
-rw-r--r-- | ffi/examples/parser.c | 24 | ||||
-rw-r--r-- | ffi/examples/reader.c | 20 |
5 files changed, 26 insertions, 98 deletions
diff --git a/ffi/examples/armor.c b/ffi/examples/armor.c index a2643667..5a1b9339 100644 --- a/ffi/examples/armor.c +++ b/ffi/examples/armor.c @@ -20,7 +20,6 @@ int main (int argc, char **argv) { sq_error_t err; - sq_context_t ctx; sq_reader_t bytes; sq_reader_t armor; sq_armor_kind_t kind; @@ -28,18 +27,12 @@ main (int argc, char **argv) sq_armor_header_t *header; size_t header_len; - ctx = sq_context_new ("org.sequoia-pgp.example", &err); - if (ctx == NULL) - error (1, 0, "Initializing sequoia failed: %s", - sq_error_string (err)); - bytes = sq_reader_from_bytes ((uint8_t *) armored, strlen (armored)); armor = sq_armor_reader_new (bytes, SQ_ARMOR_KIND_ANY); - header = sq_armor_reader_headers (ctx, armor, &header_len); + header = sq_armor_reader_headers (&err, armor, &header_len); if (header == NULL) - error (1, 0, "Getting headers failed: %s", - sq_error_string (err)); + error (1, 0, "Getting headers failed: %s", sq_error_string (err)); assert (header_len == 2); assert (strcmp (header[0].key, "Key0") == 0 @@ -56,17 +49,12 @@ main (int argc, char **argv) kind = sq_armor_reader_kind (armor); assert (kind == SQ_ARMOR_KIND_FILE); - if (sq_reader_read (ctx, armor, (uint8_t *) message, 12) < 0) - { - err = sq_context_last_error (ctx); - error (1, 0, "Reading failed: %s", - sq_error_string (err)); - } + if (sq_reader_read (&err, armor, (uint8_t *) message, 12) < 0) + error (1, 0, "Reading failed: %s", sq_error_string (err)); assert (memcmp (message, "Hello world!", 12) == 0); sq_reader_free (armor); sq_reader_free (bytes); - sq_context_free (ctx); return 0; } diff --git a/ffi/examples/encrypt-for.c b/ffi/examples/encrypt-for.c index 15ff3ef6..f0c34449 100644 --- a/ffi/examples/encrypt-for.c +++ b/ffi/examples/encrypt-for.c @@ -22,7 +22,6 @@ main (int argc, char **argv) uint8_t *b; sq_status_t rc; sq_error_t err; - sq_context_t ctx; int use_armor = 1; sq_tpk_t tpk; sq_writer_t sink; @@ -33,11 +32,6 @@ main (int argc, char **argv) if (argc != 2) error (1, 0, "Usage: %s <keyfile> <plain >cipher", argv[0]); - ctx = sq_context_new ("org.sequoia-pgp.example", &err); - if (ctx == NULL) - error (1, 0, "Initializing sequoia failed: %s", - sq_error_string (err)); - if (stat (argv[1], &st)) error (1, errno, "%s", argv[1]); @@ -50,37 +44,28 @@ main (int argc, char **argv) if (b == MAP_FAILED) error (1, errno, "mmap"); - tpk = sq_tpk_from_bytes (ctx, b, st.st_size); + tpk = sq_tpk_from_bytes (&err, b, st.st_size); if (tpk == NULL) - { - err = sq_context_last_error (ctx); - error (1, 0, "sq_packet_parser_from_bytes: %s", sq_error_string (err)); - } + error (1, 0, "sq_packet_parser_from_bytes: %s", sq_error_string (err)); sink = sq_writer_alloc (&cipher, &cipher_bytes); if (use_armor) - sink = sq_armor_writer_new (ctx, sink, SQ_ARMOR_KIND_MESSAGE, + sink = sq_armor_writer_new (&err, sink, SQ_ARMOR_KIND_MESSAGE, NULL, 0); writer = sq_writer_stack_message (sink); - writer = sq_encryptor_new (ctx, + writer = sq_encryptor_new (&err, writer, NULL, 0, /* no passwords */ &tpk, 1, SQ_ENCRYPTION_MODE_FOR_TRANSPORT); if (writer == NULL) - { - err = sq_context_last_error (ctx); - error (1, 0, "sq_encryptor_new: %s", sq_error_string (err)); - } + error (1, 0, "sq_encryptor_new: %s", sq_error_string (err)); - writer = sq_literal_writer_new (ctx, writer); + writer = sq_literal_writer_new (&err, writer); if (writer == NULL) - { - err = sq_context_last_error (ctx); - error (1, 0, "sq_literal_writer_new: %s", sq_error_string (err)); - } + error (1, 0, "sq_literal_writer_new: %s", sq_error_string (err)); size_t nread; uint8_t buf[4096]; @@ -90,28 +75,22 @@ main (int argc, char **argv) while (nread) { ssize_t written; - written = sq_writer_stack_write (ctx, writer, b, nread); + written = sq_writer_stack_write (&err, writer, b, nread); if (written < 0) - { - err = sq_context_last_error (ctx); - error (1, 0, "sq_writer_stack_write: %s", sq_error_string (err)); - } + error (1, 0, "sq_writer_stack_write: %s", sq_error_string (err)); + b += written; nread -= written; } } - rc = sq_writer_stack_finalize (ctx, writer); + rc = sq_writer_stack_finalize (&err, writer); writer = NULL; if (rc) - { - err = sq_context_last_error (ctx); - error (1, 0, "sq_writer_stack_write: %s", sq_error_string (err)); - } + error (1, 0, "sq_writer_stack_write: %s", sq_error_string (err)); fwrite (cipher, 1, cipher_bytes, stdout); - sq_context_free (ctx); munmap (b, st.st_size); return 0; } diff --git a/ffi/examples/example.c b/ffi/examples/example.c index 2b4d8126..8636d011 100644 --- a/ffi/examples/example.c +++ b/ffi/examples/example.c @@ -17,17 +17,11 @@ main (int argc, char **argv) int fd; uint8_t *b; sq_error_t err; - sq_context_t ctx; sq_tpk_t tpk; if (argc != 2) error (1, 0, "Usage: %s <file>", argv[0]); - ctx = sq_context_new("org.sequoia-pgp.example", &err); - if (ctx == NULL) - error (1, 0, "Initializing sequoia failed: %s", - sq_error_string (err)); - if (stat (argv[1], &st)) error (1, errno, "%s", argv[1]); @@ -39,15 +33,12 @@ main (int argc, char **argv) if (b == MAP_FAILED) error (1, errno, "mmap"); - tpk = sq_tpk_from_bytes (ctx, b, st.st_size); + tpk = sq_tpk_from_bytes (&err, b, st.st_size); if (tpk == NULL) - { - sq_error_t err = sq_context_last_error (ctx); - error (1, 0, "sq_tpk_from_bytes: %s", sq_error_string (err)); - } + error (1, 0, "sq_tpk_from_bytes: %s", sq_error_string (err)); + sq_tpk_dump (tpk); sq_tpk_free (tpk); - sq_context_free (ctx); munmap (b, st.st_size); close (fd); return 0; diff --git a/ffi/examples/parser.c b/ffi/examples/parser.c index e26dd646..8d953422 100644 --- a/ffi/examples/parser.c +++ b/ffi/examples/parser.c @@ -22,18 +22,12 @@ main (int argc, char **argv) uint8_t *b; sq_status_t rc; sq_error_t err; - sq_context_t ctx; sq_packet_parser_result_t ppr; sq_packet_parser_t pp; if (argc != 2) error (1, 0, "Usage: %s <file>", argv[0]); - ctx = sq_context_new ("org.sequoia-pgp.example", &err); - if (ctx == NULL) - error (1, 0, "Initializing sequoia failed: %s", - sq_error_string (err)); - if (stat (argv[1], &st)) error (1, errno, "%s", argv[1]); @@ -51,7 +45,7 @@ main (int argc, char **argv) time_t elapsed; size_t tens_of_s = 0; - ppr = sq_packet_parser_from_bytes (ctx, b, st.st_size); + ppr = sq_packet_parser_from_bytes (&err, b, st.st_size); while (ppr && (pp = sq_packet_parser_result_packet_parser (ppr))) { // Get a reference to the packet that is currently being parsed. @@ -64,13 +58,10 @@ main (int argc, char **argv) // Finish parsing the current packet (returned in p), and read // the header of the next packet (returned in ppr). - rc = sq_packet_parser_next (ctx, pp, &p, &ppr); + rc = sq_packet_parser_next (&err, pp, &p, &ppr); if (rc) - { - err = sq_context_last_error (ctx); - error (1, 0, "sq_packet_parser_from_bytes: %s", - sq_error_string (err)); - } + error (1, 0, "sq_packet_parser_from_bytes: %s", + sq_error_string (err)); // We now own p. If we want, we can save it in some structure. // This would be useful when collecting PKESK packets. Either @@ -91,17 +82,12 @@ main (int argc, char **argv) } } if (ppr == NULL) - { - err = sq_context_last_error (ctx); - error (1, 0, "sq_packet_parser_from_bytes: %s", sq_error_string (err)); - } + error (1, 0, "sq_packet_parser_from_bytes: %s", sq_error_string (err)); fprintf (stderr, "Parsed %ld packets in %ld seconds, %.2f packets/s.\n", n, elapsed, (double) n / (double) elapsed); sq_packet_parser_result_free (ppr); - - sq_context_free (ctx); munmap (b, st.st_size); return 0; } diff --git a/ffi/examples/reader.c b/ffi/examples/reader.c index 1587979b..8e458620 100644 --- a/ffi/examples/reader.c +++ b/ffi/examples/reader.c @@ -17,18 +17,12 @@ main (int argc, char **argv) int fd; uint8_t *b; sq_error_t err; - sq_context_t ctx; sq_reader_t reader; sq_tpk_t tpk; if (argc != 2) error (1, 0, "Usage: %s <file>", argv[0]); - ctx = sq_context_new("org.sequoia-pgp.example", &err); - if (ctx == NULL) - error (1, 0, "Initializing sequoia failed: %s", - sq_error_string (err)); - if (stat (argv[1], &st)) error (1, errno, "%s", argv[1]); @@ -41,23 +35,13 @@ main (int argc, char **argv) error (1, errno, "mmap"); reader = sq_reader_from_bytes (b, st.st_size); - if (reader == NULL) - { - sq_error_t err = sq_context_last_error (ctx); - error (1, 0, "sq_reader_from_bytes: %s", sq_error_string (err)); - } - - tpk = sq_tpk_from_reader (ctx, reader); + tpk = sq_tpk_from_reader (&err, reader); if (tpk == NULL) - { - sq_error_t err = sq_context_last_error (ctx); - error (1, 0, "sq_tpk_from_reader: %s", sq_error_string (err)); - } + error (1, 0, "sq_tpk_from_reader: %s", sq_error_string (err)); sq_tpk_dump (tpk); sq_tpk_free (tpk); sq_reader_free (reader); - sq_context_free (ctx); munmap (b, st.st_size); close (fd); return 0; |