diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-01-08 12:35:22 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-01-08 14:24:43 +0100 |
commit | 32e06d22c906f66f6cb94c942da0ab06bce53f14 (patch) | |
tree | 33c7b952b655fe27f70436bdc6d2c080e667a177 | |
parent | 6edda878c730fd7f675c356f44ff1ce14e650b73 (diff) |
ffi: Add armor example.
-rw-r--r-- | ffi/examples/.gitignore | 1 | ||||
-rw-r--r-- | ffi/examples/Makefile | 2 | ||||
-rw-r--r-- | ffi/examples/armor.c | 72 |
3 files changed, 74 insertions, 1 deletions
diff --git a/ffi/examples/.gitignore b/ffi/examples/.gitignore index 396865df..17a9b656 100644 --- a/ffi/examples/.gitignore +++ b/ffi/examples/.gitignore @@ -4,3 +4,4 @@ keyserver reader parser encrypt-for +armor diff --git a/ffi/examples/Makefile b/ffi/examples/Makefile index d27526ad..4ca01078 100644 --- a/ffi/examples/Makefile +++ b/ffi/examples/Makefile @@ -5,7 +5,7 @@ CARGO_TARGET_DIR ?= $(shell pwd)/../../target # We currently only support absolute paths. CARGO_TARGET_DIR := $(abspath $(CARGO_TARGET_DIR)) -TARGETS = example keyserver configure reader parser encrypt-for +TARGETS = example keyserver configure reader parser encrypt-for armor CFLAGS = -I../include -O0 -g -Wall -Werror LDFLAGS = -L$(CARGO_TARGET_DIR)/debug -lsequoia_ffi diff --git a/ffi/examples/armor.c b/ffi/examples/armor.c new file mode 100644 index 00000000..a2643667 --- /dev/null +++ b/ffi/examples/armor.c @@ -0,0 +1,72 @@ +#define _GNU_SOURCE +#include <assert.h> +#include <error.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <sequoia.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"; + +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; + char message[12]; + 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); + if (header == NULL) + error (1, 0, "Getting headers failed: %s", + sq_error_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); + + 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)); + } + + assert (memcmp (message, "Hello world!", 12) == 0); + + sq_reader_free (armor); + sq_reader_free (bytes); + sq_context_free (ctx); + return 0; +} |