forked from diasurgical/devilution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.cpp
More file actions
142 lines (128 loc) · 3.12 KB
/
codec.cpp
File metadata and controls
142 lines (128 loc) · 3.12 KB
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
/**
* @file codec.cpp
*
* Implementation of save game encryption algorithm.
*/
#include "all.h"
typedef struct CodecSignature {
DWORD checksum;
BYTE error;
BYTE last_chunk_size;
WORD unused;
} CodecSignature;
#define BLOCKSIZE 64
static void codec_init_key(int unused, const char *pszPassword)
{
int i, ch, n;
char key[136]; // last 64 bytes are the SHA1
char pw[64];
char digest[SHA1HashSize];
char *keyInit;
srand(0x7058);
keyInit = key;
for (i = 0; i < sizeof(key); i++) {
*keyInit = rand();
keyInit++;
}
ch = 0;
for (i = 0; i < 64; i++) {
if (!pszPassword[ch])
ch = 0;
pw[i] = pszPassword[ch];
ch++;
}
SHA1Reset(0);
SHA1Calculate(0, pw, digest);
SHA1Clear();
for (i = 0; i < sizeof(key); i++)
key[i] ^= digest[i % SHA1HashSize];
memset(pw, 0, sizeof(pw));
memset(digest, 0, sizeof(digest));
for (n = 0; n < 3; n++) {
SHA1Reset(n);
SHA1Calculate(n, &key[72], NULL);
}
memset(key, 0, sizeof(key));
}
int codec_decode(BYTE *pbSrcDst, DWORD size, const char *pszPassword)
{
char buf[128];
char dst[SHA1HashSize];
int i;
CodecSignature *sig;
codec_init_key(0, pszPassword);
if (size <= sizeof(CodecSignature))
return 0;
size -= sizeof(CodecSignature);
if (size % BLOCKSIZE != 0)
return 0;
for (i = size; i != 0; pbSrcDst += BLOCKSIZE, i -= BLOCKSIZE) {
memcpy(buf, pbSrcDst, BLOCKSIZE);
SHA1Result(0, dst);
for (int j = 0; j < BLOCKSIZE; j++) {
buf[j] ^= dst[j % SHA1HashSize];
}
SHA1Calculate(0, buf, NULL);
memset(dst, 0, sizeof(dst));
memcpy(pbSrcDst, buf, BLOCKSIZE);
}
memset(buf, 0, sizeof(buf));
sig = (CodecSignature *)pbSrcDst;
if (sig->error > 0) {
goto error;
}
SHA1Result(0, dst);
if (sig->checksum != *(DWORD *)dst) {
memset(dst, 0, sizeof(dst));
goto error;
}
size += sig->last_chunk_size - BLOCKSIZE;
SHA1Clear();
return size;
error:
SHA1Clear();
return 0;
}
DWORD codec_get_encoded_len(DWORD dwSrcBytes)
{
if (dwSrcBytes % BLOCKSIZE != 0)
dwSrcBytes += BLOCKSIZE - (dwSrcBytes % BLOCKSIZE);
return dwSrcBytes + sizeof(CodecSignature);
}
void codec_encode(BYTE *pbSrcDst, DWORD size, int size_64, const char *pszPassword)
{
char buf[128];
char tmp[SHA1HashSize];
char dst[SHA1HashSize];
DWORD chunk;
WORD last_chunk;
CodecSignature *sig;
if (size_64 != codec_get_encoded_len(size))
app_fatal("Invalid encode parameters");
codec_init_key(1, pszPassword);
last_chunk = 0;
while (size != 0) {
chunk = size < BLOCKSIZE ? size : BLOCKSIZE;
memcpy(buf, pbSrcDst, chunk);
if (chunk < BLOCKSIZE)
memset(buf + chunk, 0, BLOCKSIZE - chunk);
SHA1Result(0, dst);
SHA1Calculate(0, buf, NULL);
for (int j = 0; j < BLOCKSIZE; j++) {
buf[j] ^= dst[j % SHA1HashSize];
}
memset(dst, 0, sizeof(dst));
memcpy(pbSrcDst, buf, BLOCKSIZE);
last_chunk = chunk;
pbSrcDst += BLOCKSIZE;
size -= chunk;
}
memset(buf, 0, sizeof(buf));
SHA1Result(0, tmp);
sig = (CodecSignature *)pbSrcDst;
sig->error = 0;
sig->unused = 0;
sig->checksum = *(DWORD *)&tmp[0];
sig->last_chunk_size = last_chunk;
SHA1Clear();
}