summaryrefslogtreecommitdiffstats
path: root/ffi/examples
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2018-02-28 16:05:52 +0100
committerJustus Winter <justus@sequoia-pgp.org>2018-02-28 18:27:14 +0100
commit70918970bb7ef7436cc1386a88e9e41a07cc1b44 (patch)
tree221a6edd68ddbaa6044e3d3402fbc3a27ce7553d /ffi/examples
parent7df799b20555785c6591e824c967898f4683a644 (diff)
ffi: Bind {reader,writer}-related functions in the openpgp module.
- Bind the armor filters, as well as the reader-based constructor of TPK.
Diffstat (limited to 'ffi/examples')
-rw-r--r--ffi/examples/Makefile2
-rw-r--r--ffi/examples/reader.c56
2 files changed, 57 insertions, 1 deletions
diff --git a/ffi/examples/Makefile b/ffi/examples/Makefile
index aa1da727..d3fff15e 100644
--- a/ffi/examples/Makefile
+++ b/ffi/examples/Makefile
@@ -1,6 +1,6 @@
# Makefile for examples written in C.
-TARGETS = example keyserver configure
+TARGETS = example keyserver configure reader
CFLAGS = -I../include -O0 -g -Wall
LDFLAGS = -L../../target/debug -lsequoia_ffi
diff --git a/ffi/examples/reader.c b/ffi/examples/reader.c
new file mode 100644
index 00000000..dde98016
--- /dev/null
+++ b/ffi/examples/reader.c
@@ -0,0 +1,56 @@
+#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 <sequoia.h>
+
+int
+main (int argc, char **argv)
+{
+ struct stat st;
+ int fd;
+ uint8_t *b;
+ 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");
+ if (ctx == NULL)
+ error (1, 0, "Initializing sequoia failed.");
+
+ 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 = sq_reader_from_bytes (b, st.st_size);
+ if (reader == NULL)
+ error (1, 0, "sq_reader_from_bytes: %s", sq_last_strerror (ctx));
+
+ tpk = sq_tpk_from_reader (ctx, reader);
+ if (tpk == NULL)
+ error (1, 0, "sq_tpk_from_reader: %s", sq_last_strerror (ctx));
+
+ 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;
+}