Integrate add_pad() into load_pad(), since it won't ever be called otherwise.
[easyotp.git] / libotp.c
blob22bb56bfdbf24fc6f43e47eca041e709af9df9c1
1 /** Practical One-time Pad Library
3 * Created:20080514
4 * By Jeff Connelly
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sysexits.h>
10 #include <string.h>
12 #define MARKER_TO "to:"
13 #define MARKER_BEGIN "--EMOTP_BEGIN--"
14 #define MARKER_END "--EMOTP_END--"
16 /** Packaged up encrypted message, ready for transport. */
17 typedef struct _PACKAGE
19 unsigned long offset;
20 char *pad_name;
21 char *cipher_text;
22 } PACKAGE;
24 typedef struct _PAD
26 char *local_filename;
27 char *name;
28 unsigned long offset;
29 FILE *fp;
30 struct _PAD *next;
31 } PAD;
33 PAD *pads = NULL;
35 /** Show a list of all loaded pads. */
36 void show_pads()
38 PAD *p;
40 for (p = pads; p; p = p->next)
42 printf("Pad: %s: %s\n", p->name, p->local_filename);
46 /** Load a pad file from disk, adding to 'pads' global. */
47 void load_pad(char *local_filename)
49 FILE *fp;
50 PAD *new_pad;
52 fp = fopen("/Volumes/Not Backed Up/otp/otp-dazzlement", "rb");
53 if (!fp)
55 perror("fopen");
56 exit(EXIT_FAILURE);
59 new_pad = malloc(sizeof(PAD));
60 if (!new_pad)
62 perror("malloc");
63 exit(EX_UNAVAILABLE);
66 new_pad->local_filename = strdup(local_filename);
67 new_pad->name = strdup("dc"); /* TODO */
68 new_pad->offset = -1; /* TODO */
69 new_pad->fp = fp;
70 new_pad->next = NULL;
72 /* Add to linked list. */
73 if (!pads)
75 pads = new_pad;
77 else
79 PAD *p, *tail;
81 /* Find tail */
82 for (p = pads; p; p = p->next)
83 tail = p;
84 tail->next = new_pad;
88 /** Close all pads and free allocated memory. */
89 void free_pads()
91 PAD *p, *next;
93 for (p = pads; p; p = next)
95 free(p->name);
96 free(p->local_filename);
97 fclose(p->fp);
98 next = p->next;
99 free(p);
103 int main()
105 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
106 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
107 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
108 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
109 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
111 show_pads();
112 free_pads();
114 return 0;