-
Notifications
You must be signed in to change notification settings - Fork 4
/
Stream.cpp
233 lines (180 loc) · 4.6 KB
/
Stream.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "Stream.h"
#ifndef NO_CRYPTO
#include "crypto/sha1.h"
#endif
#define BUFFER_SIZE (16*1024*1024)
bool Stream::ReadCString(std::string &str)
{
int8_t ch;
str.clear();
while (Read8(&ch))
{
if (ch == 0)
return true;
str.push_back(ch);
}
return false;
}
bool Stream::Printf(const char *fmt, ...)
{
size_t alloc_size = strlen(fmt) + 256;
char *buf = new char[alloc_size];
va_list ap;
va_start(ap, fmt);
int len = vsnprintf(buf, alloc_size, fmt, ap);
va_end(ap);
bool ret;
if (len > 0)
{
ret = Write(buf, (size_t)len);
}
else
{
ret = false;
}
delete[] buf;
return ret;
}
bool Stream::Copy(Stream *other, size_t size)
{
uint8_t *copy_buf;
size_t remaining;
if (size <BUFFER_SIZE)
{
copy_buf = new uint8_t[size];
}
else
{
copy_buf = new uint8_t[BUFFER_SIZE];
}
remaining = size;
while (remaining > 0)
{
size_t r = (remaining > BUFFER_SIZE) ? BUFFER_SIZE: remaining;
if (!other->Read(copy_buf, r))
{
delete[] copy_buf;
return false;
}
if (!Write(copy_buf, r))
{
delete[] copy_buf;
return false;
}
remaining -= r;
}
delete[] copy_buf;
return true;
}
#ifdef NO_CRYPTO
bool Stream::CopyEx(Stream *, size_t, Hash, uint8_t *, Cipher, const uint8_t *, int, Cipher, const uint8_t *, int)
{
DPRINTF("%s: Crypto is not enabled.\n", FUNCNAME);
return false;
}
#else
bool Stream::CopyEx(Stream *other, size_t size, Hash hash_mode, uint8_t *hash, Cipher decrypt_mode, const uint8_t *decrypt_key, int decrypt_key_size, Cipher encrypt_mode, const uint8_t *encrypt_key, int encrypt_key_size)
{
static const size_t block_size = 16;
uint8_t *copy_buf;
size_t remaining;
SHA1_CTX ctx;
bool decrypt, encrypt, do_hash;
assert((BUFFER_SIZE % block_size) == 0);
do_hash = (hash_mode == Hash::SHA1);
decrypt = (decrypt_mode == Cipher::AES_PLAIN);
encrypt = (encrypt_mode == Cipher::AES_PLAIN);
if (size < BUFFER_SIZE)
{
size_t buf_size = size;
if (decrypt || encrypt)
{
if ((buf_size % block_size) != 0)
{
buf_size += (block_size - (buf_size % block_size));
}
}
copy_buf = new uint8_t[buf_size];
}
else
{
copy_buf = new uint8_t[BUFFER_SIZE];
}
if (do_hash)
__SHA1_Init(&ctx);
remaining = size;
while (remaining > 0)
{
size_t r = (remaining > BUFFER_SIZE) ? BUFFER_SIZE: remaining;
size_t read_size = r;
size_t write_size = r;
bool eof = false;
if (decrypt)
{
if ((read_size % block_size) != 0)
{
read_size += (block_size - (r % block_size));
eof = true;
}
}
if (!other->Read(copy_buf, read_size))
{
delete[] copy_buf;
return false;
}
if (do_hash)
__SHA1_Update(&ctx, copy_buf, (uint32_t)r);
if (decrypt)
{
Utils::AesEcbDecrypt(copy_buf, read_size, decrypt_key, decrypt_key_size);
}
if (encrypt)
{
if ((write_size % block_size) != 0)
{
size_t new_write_size = write_size + (block_size - (r % block_size));
memset(copy_buf+write_size, 0, new_write_size-write_size);
write_size = new_write_size;
}
Utils::AesEcbEncrypt(copy_buf, write_size, encrypt_key, encrypt_key_size);
}
if (!Write(copy_buf, write_size))
{
delete[] copy_buf;
return false;
}
if (eof)
break;
remaining -= r;
}
if (do_hash)
__SHA1_Final(&ctx, hash);
delete[] copy_buf;
return true;
}
#endif // NO_CRYPTO
bool Stream::Align(unsigned int alignment)
{
unsigned int write_size = 0;
uint64_t file_pos = Tell();
if ((file_pos % alignment) != 0)
write_size = (alignment - (file_pos % alignment));
for (unsigned int i = 0; i < write_size; i++)
{
if (!Write8(0))
return false;
}
return true;
}
bool Stream::SkipToAlignment(unsigned int alignment)
{
unsigned int seek_size = 0;
uint64_t file_pos = Tell();
if ((file_pos % alignment) != 0)
seek_size = (alignment - (file_pos % alignment));
else
return true;
if (!Seek(seek_size, SEEK_CUR))
return false;
return true;
}