-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.c
158 lines (133 loc) · 4.82 KB
/
manager.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "manager.h"
#include "hashcol.h"
#include <stdio.h>
#include <ctype.h>
/*Compute Collatz sequence : return 1 at the end of seq */
static inline unsigned collatz(unsigned value)
{
if(value > 1)
return (value & 1) ? ((value << 1) + (value | 1)) : (value >> 1);
else
return 1;
}
/* when the sequence key get value MIN_SEQ, we restart the sequence with initial key value.*/
static inline unsigned key_setter(Collatz *col)
{
return (col->key <= col->minseq) ? col->startval : col->key;
}
#if 0
/*
* New key setter methode : here we change completely the key under certain condition :
* When the value of : col->minseq is reached we change the col->key to (col->key << 1); and restart the loop.
* when the sequence key get value MIN_SEQ (see the deader file for MIN_SEQ), we restart the sequence with initial key value.*/
static inline unsigned collatzKey_set(Collatz *col, unsigned swap)
{
/*DEBUG_("new key is : %d\n", col->key) ;*/
return (col->key <= col->minseq) ? ((col->startval <<= swap)) : col->key;
}
#endif
/*This function shift the wchar_t c with an offset value, defined with the collatz sequence (the value is next "key").*/
static inline wchar_t _crypt(unsigned offset, wchar_t c)
{
return ((FIRST_RASCII + (c + offset) % LAST_RASCII) - FIRST_RASCII);
}
/*Find the original value of the modulo operation, with the limit : val = LAST_RASCII*/
static inline wchar_t abswchar_t(int c, unsigned val)
{
while(c < 0)
c += val;
return c;
}
static inline wchar_t _decrypt(unsigned offset, int c)
{
/*The inverse operation of the encryption */
c = c + FIRST_RASCII - (offset + FIRST_RASCII) % LAST_RASCII;
/*We return from the negative result to the original result, if there's some negatives shit of course.*/
c = abswchar_t(c, LAST_RASCII);
return c;
}
/* tell if the character is between : LAST_RASCII 'wchar_t' and
* FIRST_RASCII 'wchar_t ', or NOT. If not, nothing will be encrypted and the return is NULL.
* For the ifdef commande : choose to crypt the carriage return '\n' of new line or not.
*
* */
static inline short isColAscii(wchar_t c)
{
#ifdef ENTER
return (((c >= FIRST_RASCII) && (c <= LAST_RASCII)) || (c == '\n'));
#elif defined ALLCHARIOT
return (((c >= FIRST_RASCII) && (c <= LAST_RASCII)) || (c == '\n')
|| (c == '\f') || (c == '\v') || (c == '\r') || (c == '\t'));
#endif
return ((c >= FIRST_RASCII) && (c <= LAST_RASCII));
}
/*init data coll : Dont require a strong password in param's
* because the hash function will generate anyway a big unsigned */
extern void coll_Init(Collatz *col, const char * const password , unsigned minseq)
{
col->key = hashcode(password);
col->minseq = minseq;
col->startval = col->key;
col->cp_print = 0;
col->codedMsg = NULL;
col->clearMsg = NULL;
}
/* ENC DATA with coll
return NULL if the code message is an empty string or if isColAscii() return is false.
If the password string is 'NULL' there will be no encryption.
*/
extern wchar_t * coll_Encrypt(Collatz *col, const wchar_t * const data)
{
size_t i;
if(!data) return NULL;
col->len = wcslen(data);
assert(col->codedMsg = (wchar_t*)malloc((col->len + 1) * sizeof(wchar_t)));
wmemset(col->codedMsg, 0, (col->len + 1)) ;
/*test here only for coll ascii limit from FIRST_RASCII to LAST_RASCII : see the file "manager.h"*/
for(i = 0; i < col->len; i++)
{
if(isColAscii(data[i]))
{
col->key = collatz(col->key);
col->key = key_setter(col);
col->codedMsg[i] = _crypt(col->key, data[i]);
/* count the non-printable characters */
if (!isprint(col->codedMsg[i]))
col->cp_print++;
}
else
{
free(col->codedMsg), col->codedMsg = NULL;
return NULL;
}
}
col->codedMsg[i] = L'\0';
return col->codedMsg;
}
/*
DEC DATA with coll : return NULL if there's nothing to decrypt.*/
extern wchar_t * coll_Decrypt(Collatz *col)
{
if(!col->codedMsg) return NULL;
size_t i;
assert(col->clearMsg = malloc((col->len + 1) * sizeof(wchar_t)));
wmemset(col->clearMsg, 0, (col->len + 1));
/* reset the key */
col->key = col->startval;
for(i = 0; i < col->len; i++)
{
col->key = collatz(col->key);
col->key = key_setter(col);
col->clearMsg[i] = _decrypt(col->key, col->codedMsg[i]);
}
col->clearMsg[i] = L'\0';
return col->clearMsg;
}
/*free some ram space and shit you know */
extern void coll_dispose(Collatz *col)
{
if(col->codedMsg)
free(col->codedMsg), col->codedMsg = NULL;
if(col->clearMsg)
free(col->clearMsg), col->clearMsg = NULL;
}