Pad loading into linked list functional.
[easyotp.git] / libotp.c
blob0cbe526e11fba7fcdcc13f1b9373b592ce54a1b8
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 void add_pad(PAD *new_pad)
37 printf("add %.8X\n", (unsigned int)new_pad);
38 if (!pads)
40 pads = new_pad;
42 else
44 PAD *p, *tail;
46 /* Find tail */
47 for (p = pads; p; p = p->next)
48 tail = p;
49 tail->next = new_pad;
53 /** Show a list of all loaded pads. */
54 void show_pads()
56 PAD *p;
58 for (p = pads; p; p = p->next)
60 printf("Pad: %s: %s (next=%.8x)\n", p->name, p->local_filename,
61 (unsigned int)p->next);
65 /** Load a pad file from disk, adding to 'pads' global. */
66 void load_pad(char *local_filename)
68 FILE *fp;
69 PAD *new_pad;
71 fp = fopen("/Volumes/Not Backed Up/otp/otp-dazzlement", "rb");
72 if (!fp)
74 perror("fopen");
75 exit(EXIT_FAILURE);
78 new_pad = malloc(sizeof(PAD));
79 if (!new_pad)
81 perror("malloc");
82 exit(EX_UNAVAILABLE);
85 new_pad->local_filename = strdup(local_filename);
86 new_pad->name = NULL;
87 /* TODO: new_pad->offset = */
88 new_pad->fp = fp;
89 new_pad->next = NULL;
91 add_pad(new_pad);
94 /** Close all pads and free allocated memory. */
95 void free_pads()
97 PAD *p, *next;
99 for (p = pads; p; p = next)
101 free(p->name);
102 free(p->local_filename);
103 fclose(p->fp);
104 next = p->next;
105 free(p);
109 int main()
111 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
112 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
113 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
114 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
115 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
117 show_pads();
118 free_pads();
120 return 0;