summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi/examples/immutable-reference-demo.c
blob: 47efbdd65c263b8778e45b603e6b79fd096e4faa (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
#define _GNU_SOURCE
#include <errno.h>
/* Roughly glibc compatible error reporting.  */
#define error(S, E, F, ...) do {                        \
  fprintf (stderr, (F), __VA_ARGS__);                   \
  int s = (S), e = (E);                                 \
  if (e) { fprintf (stderr, ": %s", strerror (e)); }    \
  fprintf (stderr, "\n");                               \
  fflush (stderr);                                      \
  if (s) { exit (s); }                                  \
  } while (0)
#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)
{
  /* Create a new TSK.  */
  pgp_tsk_t tsk;
  pgp_signature_t revocation;
  pgp_tsk_new (NULL, "", &tsk, &revocation);
  pgp_signature_free (revocation);

  /* Let's borrow a immutable reference from it.  */
  pgp_tpk_t tpk = pgp_tsk_tpk (tsk);

  /* Let's violate The Rules and forge a mutable reference from
   * the immutable one!  */
  pgp_tpk_t tpk_mut = (pgp_tpk_t) tpk;

  /* And try to convert the TPK to a TSK, moving the ownership!  */
  pgp_tpk_into_tsk (tpk_mut);

  /* Always clean up though.  */
  pgp_tsk_free (tsk);
  return 0;
}