-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresourcefilenames.cc
100 lines (75 loc) · 3.06 KB
/
resourcefilenames.cc
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* =====================================================================================
*
* Filename: resourcefilenames.cc
*
* Description: Get filenames for resources.
*
* Version: 1.0
* Created: 10/05/2008 06:48:10 PM
* Revision: none
* Compiler: gcc
*
* Author: Bryce Allen (bda), bryce@bda.ath.cx
* Company:
*
* =====================================================================================
*/
#include <gmp.h>
#include <stdio.h>
#include <string.h>
#include "include/types.h"
#include "include/elgamal.h"
#include "resourcefilenames.h"
static size_t cryptosystemFileNameLength (char *label, ElgamalCryptosystem *e) {
// label_XXXX_XXXX.elg
return strlen(label) + 5 + 5 + 4 + 1;
}
// copies cryptosystemFileNameLength chars to target, or cryptosystemFileNameLength - 4 if omitExt == true
static void strcpyCryptosystemFileName (char *target, char *label, ElgamalCryptosystem *e, bool omitExt = false) {
//size_t len = cryptosystemFileNameLength (label, e);
char *p = target;
strcpy (p, label);
p += strlen (label);
*p = '_'; p++;
snprintf (p, 5, "%04zu", mpz_sizeinbase (e->prime, 2));
p += 4; // overwrite null byte
*p = '_'; p++;
snprintf (p, 5, "%04zu", mpz_sizeinbase (e->baseOrder, 2));
if (!omitExt) {
p += 4; // overwrite null byte
strcpy (p, ".elg"); // includes the null byte
}
}
char *getCryptosystemFileName (char *label, ElgamalCryptosystem *e) {
// label_XXXX_XXXX.elg
size_t len = cryptosystemFileNameLength (label, e);
char *fileName = (char *) malloc (len * sizeof (*fileName));
strcpyCryptosystemFileName (fileName, label, e);
return fileName;
}
char *getCryptosystemFilePath (char *label, ElgamalCryptosystem *e) {
size_t len = cryptosystemFileNameLength (label, e) + strlen(CRYPTOSYSTEM_DIR) + 1;
char *filePath = (char *) malloc (len * sizeof (*filePath));
strncpy (filePath, CRYPTOSYSTEM_DIR, strlen (CRYPTOSYSTEM_DIR)); // don't copy null byte
strcpyCryptosystemFileName (filePath + strlen(CRYPTOSYSTEM_DIR), label, e);
return filePath;
}
char *getCryptosystemDirPath (char *label, ElgamalCryptosystem *e) {
size_t len = cryptosystemFileNameLength (label, e) + strlen(CRYPTOSYSTEM_DIR) + 1 - 4;
char *dirPath = (char *) malloc (len * sizeof (*dirPath));
strncpy (dirPath, CRYPTOSYSTEM_DIR, strlen (CRYPTOSYSTEM_DIR)); // don't copy null byte
strcpyCryptosystemFileName (dirPath + strlen(CRYPTOSYSTEM_DIR), label, e, true);
//*(dirPath + len - 1) = '\0';
//free (fileName);
return dirPath;
}
// TODO: finish
char *getCipherTextFilePath (char *label, ElgamalCryptosystem *e, ElgamalCipherText ct) {
char *path = getCryptosystemDirPath (label, e);
size_t len = strlen (path) + 10;
path = (char *) realloc (path, len * sizeof (*path));
return path;
}
//char *getAttackTableFilePath (ElgamalCryptosystem *e, char *attackName, int bits1);
//char *getAttackResultsFilePath (ElgamalCryptosystem *e, char *attackName, int bits1, ElgamalCipherText ct);