-
Notifications
You must be signed in to change notification settings - Fork 10
/
list_exist.c
executable file
·135 lines (126 loc) · 2.67 KB
/
list_exist.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include "list_exist.h"
#include "vecteur.h"
#include "cons.h"
#include "erreurs.h"
#include "exit_if.h"
list_exist create()
{
srand(time(NULL));
return vecteur_faire();
}
static cons * is_in(list_exist list, char * key, int n)
{
int i = 0;
for(i = 0; i < vecteur_nombre_elements(list); i++)
{
cons * tmp = vecteur_lire(list, i);
switch(n)
{
case 0:
{
if(!strcmp(tmp->first, key))
return tmp;
break;
}
case 1:
{
if(!strcmp(tmp->second, key))
return tmp;
break;
}
}
}
return NULL;
}
// On suppose a<b
int rand_a_b(int a, int b){
return rand()%(b-a) +a;
}
static cons * generate_cons(list_exist list, char * s)
{
int sizeOfS = strlen(s);
char * next = malloc(sizeof(char)*(sizeOfS + 1));
cons * res = malloc(sizeof(*res));
EXIT_IF(next == NULL, err_malloc);
EXIT_IF(res == NULL, err_malloc);
memset(next, 0, sizeof(char)*(sizeOfS + 1));
int i = 0;
do
{
for(i = 0; i < sizeOfS; i++)
{
next[i] = (char) rand_a_b('A', 'Z');
}
}
while(is_in(list, next, 1) != NULL);
res->first = s;
res->second = next;
res->toSave = 1;
return res;
}
cons * get_from_key(list_exist list, char * key, int isAuto, int byPass)
{
cons * res = is_in(list, key, 0);
if(res == NULL)
{
char question = 'y';
if(!isAuto && strlen(key) > 3)
{
printf("Do you want to obfuscate %s?(y/n)\n", key);
question = getchar();
while(getchar() != '\n');
}
if(question == 'n' || strlen(key) < 3)
{
char *sec = malloc(sizeof(*sec)*(strlen(key)+1));
EXIT_IF(sec == NULL, err_malloc);
memcpy(sec, key, sizeof(*sec)*(strlen(key)+1));
res = malloc(sizeof(*res));
res->first = key;
res->second = sec;
byPass = 1;
}
else
{
res = generate_cons(list, key);
}
res->toSave = !byPass;
vecteur_ecrire(list, vecteur_nombre_elements(list), res);
}
else if(byPass)
{
res->toSave = !byPass;
}
return res;
}
void delete(list_exist list)
{
int i = 0;
for(i = 0; i < vecteur_nombre_elements(list); i++)
{
cons * tmp = vecteur_lire(list, i);
free(tmp->first);
free(tmp->second);
free(tmp);
}
vecteur_defaire(list);
}
void saveToFile(list_exist list, char * filename)
{
FILE * file = fopen(filename, "w");
int i = 0;
int fileSize = vecteur_nombre_elements(list);
fprintf(file, "#ifndef H_CONVERT\n#define H_CONVERT\n\n");
for(i = 0; i < fileSize; i++)
{
cons * tmp = vecteur_lire(list, i);
if(tmp->toSave)
fprintf(file, "#define %s %s \n", tmp->second, tmp->first);
}
fprintf(file, "#endif\n");
fclose(file);
}