summaryrefslogtreecommitdiffstats
path: root/ffi/examples/example.c
blob: 2b4d812679daf703f178db22c40feae8d8cbb027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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_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]);

  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 = sq_tpk_from_bytes (ctx, 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));
    }
  sq_tpk_dump (tpk);
  sq_tpk_free (tpk);
  sq_context_free (ctx);
  munmap (b, st.st_size);
  close (fd);
  return 0;
}