-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt.c
57 lines (45 loc) · 975 Bytes
/
decrypt.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
#include "include/fjk.h"
/*
* input: 87531642
*
* data: |8|7|5|3|1|6|4|2|
* +-+-+-+-+-+-+-+-+
* tail: | | | | | | |7|8|
* body 1: |1|2| | | | |7|8|
* body 2: |1|2|3|4| | |7|8|
* body 3: |1|2|3|4|5|6|7|8|
*
* output: 12345678
*/
char* fjk_decrypt(const char *encoded, size_t size)
{
char *result;
size_t current, enc_offset, dec_offset;
struct tail t;
struct chunk c;
c.idx = CHUNK_SZ - 1;
c.offset = 0;
result = malloc(size);
t.size = size % CHUNK_SZ;
t.idx = t.size - 1;
current = t.size;
c.count = size / CHUNK_SZ;
// Write tail
while (t.idx >= 0) {
dec_offset = size - t.idx - 1;
enc_offset = t.idx--;
*(result + dec_offset) = *(encoded + enc_offset);
}
// Write data
while (current < size) {
dec_offset = c.offset + c.idx * c.count;
enc_offset = current;
*(result + dec_offset) = *(encoded + enc_offset);
current += 1;
if (!c.idx--) {
c.idx = CHUNK_SZ - 1;
c.offset += 1;
}
}
return result;
}