summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi/examples
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-03-19 13:18:38 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-03-19 14:26:12 +0100
commit2929eae208d3f98fc15bc917332f22efd7622f15 (patch)
tree03a4b832ff48b51f8b4fbd64231aaa1f01aad40c /openpgp-ffi/examples
parentacc97e4103d7f2b0bc0a7caa4e0575047b3cf8e1 (diff)
openpgp-ffi: Simplify examples.
Diffstat (limited to 'openpgp-ffi/examples')
-rw-r--r--openpgp-ffi/examples/encrypt-for.c33
-rw-r--r--openpgp-ffi/examples/example.c25
-rw-r--r--openpgp-ffi/examples/parser.c23
-rw-r--r--openpgp-ffi/examples/reader.c23
4 files changed, 8 insertions, 96 deletions
diff --git a/openpgp-ffi/examples/encrypt-for.c b/openpgp-ffi/examples/encrypt-for.c
index 32106319..4a92d023 100644
--- a/openpgp-ffi/examples/encrypt-for.c
+++ b/openpgp-ffi/examples/encrypt-for.c
@@ -4,51 +4,28 @@
#define _GNU_SOURCE
#include <error.h>
#include <errno.h>
-#include <fcntl.h>
#include <stdio.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <time.h>
#include <sequoia/openpgp.h>
int
main (int argc, char **argv)
{
- struct stat st;
- int fd;
- uint8_t *b;
pgp_status_t rc;
pgp_error_t err;
int use_armor = 1;
pgp_tpk_t tpk;
pgp_writer_t sink;
pgp_writer_stack_t writer = NULL;
- void *cipher = NULL;
- size_t cipher_bytes = 0;
if (argc != 2)
error (1, 0, "Usage: %s <keyfile> <plain >cipher", argv[0]);
- if (stat (argv[1], &st))
- error (1, errno, "%s", argv[1]);
-
- fd = open (argv[1], O_RDONLY);
- if (fd == -1)
- error (1, errno, "%s", argv[1]);
-
- b = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
- close (fd);
- if (b == MAP_FAILED)
- error (1, errno, "mmap");
-
- tpk = pgp_tpk_from_bytes (&err, b, st.st_size);
+ tpk = pgp_tpk_from_file (&err, argv[1]);
if (tpk == NULL)
- error (1, 0, "pgp_packet_parser_from_bytes: %s", pgp_error_to_string (err));
+ error (1, 0, "pgp_tpk_from_file: %s", pgp_error_to_string (err));
- sink = pgp_writer_alloc (&cipher, &cipher_bytes);
+ sink = pgp_writer_from_fd (STDOUT_FILENO);
if (use_armor)
sink = pgp_armor_writer_new (&err, sink, PGP_ARMOR_KIND_MESSAGE,
@@ -90,8 +67,6 @@ main (int argc, char **argv)
if (rc)
error (1, 0, "pgp_writer_stack_write: %s", pgp_error_to_string (err));
- fwrite (cipher, 1, cipher_bytes, stdout);
-
- munmap (b, st.st_size);
+ pgp_tpk_free (tpk);
return 0;
}
diff --git a/openpgp-ffi/examples/example.c b/openpgp-ffi/examples/example.c
index 5b114d8f..46a45529 100644
--- a/openpgp-ffi/examples/example.c
+++ b/openpgp-ffi/examples/example.c
@@ -1,49 +1,28 @@
#define _GNU_SOURCE
#include <errno.h>
#include <error.h>
-#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
#include <sequoia/openpgp.h>
int
main (int argc, char **argv)
{
- struct stat st;
- int fd;
- uint8_t *b;
pgp_error_t err;
pgp_tpk_t tpk;
if (argc != 2)
error (1, 0, "Usage: %s <file>", argv[0]);
- if (stat (argv[1], &st))
- error (1, errno, "%s", argv[1]);
-
- fd = open (argv[1], O_RDONLY);
- if (fd == -1)
- error (1, errno, "%s", argv[1]);
-
- b = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
- if (b == MAP_FAILED)
- error (1, errno, "mmap");
-
- tpk = pgp_tpk_from_bytes (&err, b, st.st_size);
+ tpk = pgp_tpk_from_file (&err, argv[1]);
if (tpk == NULL)
- error (1, 0, "pgp_tpk_from_bytes: %s", pgp_error_to_string (err));
+ error (1, 0, "pgp_tpk_from_file: %s", pgp_error_to_string (err));
char *debug = pgp_tpk_debug (tpk);
printf ("%s", debug);
free (debug);
pgp_tpk_free (tpk);
- munmap (b, st.st_size);
- close (fd);
return 0;
}
diff --git a/openpgp-ffi/examples/parser.c b/openpgp-ffi/examples/parser.c
index db6a95d7..5ded2de2 100644
--- a/openpgp-ffi/examples/parser.c
+++ b/openpgp-ffi/examples/parser.c
@@ -4,12 +4,7 @@
#define _GNU_SOURCE
#include <error.h>
#include <errno.h>
-#include <fcntl.h>
#include <stdio.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
#include <time.h>
#include <sequoia/openpgp.h>
@@ -17,9 +12,6 @@
int
main (int argc, char **argv)
{
- struct stat st;
- int fd;
- uint8_t *b;
pgp_status_t rc;
pgp_error_t err;
pgp_packet_parser_result_t ppr;
@@ -28,24 +20,12 @@ main (int argc, char **argv)
if (argc != 2)
error (1, 0, "Usage: %s <file>", argv[0]);
- if (stat (argv[1], &st))
- error (1, errno, "%s", argv[1]);
-
- fd = open (argv[1], O_RDONLY);
- if (fd == -1)
- error (1, errno, "%s", argv[1]);
-
- b = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
- close (fd);
- if (b == MAP_FAILED)
- error (1, errno, "mmap");
-
size_t n = 0;
time_t start = time (NULL);
time_t elapsed;
size_t tens_of_s = 0;
- ppr = pgp_packet_parser_from_bytes (&err, b, st.st_size);
+ ppr = pgp_packet_parser_from_file (&err, argv[1]);
while (ppr && (pp = pgp_packet_parser_result_packet_parser (ppr)))
{
// Get a reference to the packet that is currently being parsed.
@@ -88,6 +68,5 @@ main (int argc, char **argv)
n, elapsed, (double) n / (double) elapsed);
pgp_packet_parser_result_free (ppr);
- munmap (b, st.st_size);
return 0;
}
diff --git a/openpgp-ffi/examples/reader.c b/openpgp-ffi/examples/reader.c
index a293bd80..b0273ebb 100644
--- a/openpgp-ffi/examples/reader.c
+++ b/openpgp-ffi/examples/reader.c
@@ -1,22 +1,14 @@
#define _GNU_SOURCE
#include <error.h>
#include <errno.h>
-#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
#include <sequoia/openpgp.h>
int
main (int argc, char **argv)
{
- struct stat st;
- int fd;
- uint8_t *b;
pgp_error_t err;
pgp_reader_t reader;
pgp_tpk_t tpk;
@@ -24,18 +16,7 @@ main (int argc, char **argv)
if (argc != 2)
error (1, 0, "Usage: %s <file>", argv[0]);
- if (stat (argv[1], &st))
- error (1, errno, "%s", argv[1]);
-
- fd = open (argv[1], O_RDONLY);
- if (fd == -1)
- error (1, errno, "%s", argv[1]);
-
- b = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
- if (b == MAP_FAILED)
- error (1, errno, "mmap");
-
- reader = pgp_reader_from_bytes (b, st.st_size);
+ reader = pgp_reader_from_file (&err, argv[1]);
tpk = pgp_tpk_from_reader (&err, reader);
if (tpk == NULL)
error (1, 0, "pgp_tpk_from_reader: %s", pgp_error_to_string (err));
@@ -46,7 +27,5 @@ main (int argc, char **argv)
pgp_tpk_free (tpk);
pgp_reader_free (reader);
- munmap (b, st.st_size);
- close (fd);
return 0;
}