/
TIVarFile.cpp
400 lines (347 loc) · 13.5 KB
/
TIVarFile.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Part of tivars_lib_cpp
* (C) 2015 Adrien 'Adriweb' Bertrand
* https://github.com/adriweb/tivars_lib_cpp
* License: MIT
*/
#include "TIVarFile.h"
#include "utils.h"
#include "TIModels.h"
#include "TypeHandlers/TypeHandlers.h"
#include <regex>
#include <numeric>
using namespace std;
namespace tivars
{
/*** Constructors ***/
/**
* Internal constructor, called from loadFromFile and createNew.
* @param string filePath
* @throws \Exception
*/
TIVarFile::TIVarFile(const string filePath) : BinaryFile(filePath)
{
if (filePath != "")
{
this->isFromFile = true;
if (this->fileSize < 76) // bare minimum for header + a var entry
{
throw runtime_error("This file is not a valid TI-[e]z80 variable file");
}
this->makeHeaderFromFile();
this->makeVarEntryFromFile();
this->computedChecksum = this->computeChecksumFromFileData();
this->inFileChecksum = this->getChecksumValueFromFile();
this->type = TIVarType::createFromID(this->varEntry.typeID);
} else {
this->isFromFile = false;
}
}
TIVarFile TIVarFile::loadFromFile(const string filePath)
{
if (filePath != "")
{
TIVarFile varFile(filePath);
return varFile;
} else {
throw runtime_error("No file path given");
}
}
TIVarFile TIVarFile::createNew(const TIVarType& type, const string name, const TIModel& model)
{
TIVarFile varFile;
varFile.type = type;
varFile.calcModel = model;
string newName = varFile.fixVarName(name);
if (!varFile.calcModel.supportsType(varFile.type))
{
throw runtime_error("This calculator model (" + varFile.calcModel.getName() + ") does not support the type " + varFile.type.getName());
}
string signature = varFile.calcModel.getSig();
std::copy(signature.begin(), signature.end(), varFile.header.signature);
uchar sig_extra[3] = {0x1A, 0x0A, 0x00};
std::copy(sig_extra, sig_extra + 3, varFile.header.sig_extra);
string comment = str_pad("Created by tivars_lib_cpp", 42, "\0");
std::copy(comment.begin(), comment.end(), varFile.header.comment);
varFile.header.entries_len = 0; // will have to be overwritten later
uint calcFlags = varFile.calcModel.getFlags();
uchar constBytes[2] = {0x0D, 0x00};
std::copy(constBytes, constBytes + 2, varFile.varEntry.constBytes);
varFile.varEntry.data_length = 0; // will have to be overwritten later
varFile.varEntry.typeID = (uchar) type.getId();
string varname = str_pad(newName, 8, "\0");
std::copy(varname.begin(), varname.begin() + 7, varFile.varEntry.varname);
varFile.varEntry.version = (calcFlags >= TIFeatureFlags::hasFlash) ? (uchar)0 : (uchar)-1;
varFile.varEntry.archivedFlag = (calcFlags >= TIFeatureFlags::hasFlash) ? (uchar)0 : (uchar)-1; // TODO: check when that needs to be 1.
varFile.varEntry.data_length2 = 0; // will have to be overwritten later
return varFile;
}
TIVarFile TIVarFile::createNew(const TIVarType& type, const string name)
{
return createNew(type, name, TIModel::createFromName("84+"));
}
TIVarFile TIVarFile::createNew(const TIVarType& type)
{
return createNew(type, "");
}
/*** Makers ***/
void TIVarFile::makeHeaderFromFile()
{
rewind(this->file);
string signature = this->get_string_bytes(8);
std::copy(signature.begin(), signature.end(), this->header.signature);
auto sig_extra = this->get_raw_bytes(3);
std::copy(sig_extra.begin(), sig_extra.end(), this->header.sig_extra);
string comment = this->get_string_bytes(42);
std::copy(comment.begin(), comment.end(), this->header.comment);
this->header.entries_len = (uint16_t)(this->get_raw_bytes(1)[0] & 0xFF);
this->header.entries_len += (uint16_t)(this->get_raw_bytes(1)[0] << 8);
this->calcModel = TIModel::createFromSignature(signature);
}
void TIVarFile::makeVarEntryFromFile()
{
uint calcFlags = this->calcModel.getFlags();
long dataSectionOffset = (8+3+42+2); // after header
fseek(this->file, dataSectionOffset, SEEK_SET);
auto constBytes = this->get_raw_bytes(2);
std::copy(constBytes.begin(), constBytes.end(), this->varEntry.constBytes);
this->varEntry.data_length = this->get_raw_bytes(1)[0] + (this->get_raw_bytes(1)[0] << 8);
this->varEntry.typeID = this->get_raw_bytes(1)[0];
string varname = this->get_string_bytes(8);
std::copy(varname.begin(), varname.begin() + 7, this->varEntry.varname);
this->varEntry.version = (calcFlags >= TIFeatureFlags::hasFlash) ? this->get_raw_bytes(1)[0] : (uchar)-1;
this->varEntry.archivedFlag = (calcFlags >= TIFeatureFlags::hasFlash) ? this->get_raw_bytes(1)[0] : (uchar)-1;
this->varEntry.data_length2 = this->get_raw_bytes(1)[0] + (this->get_raw_bytes(1)[0] << 8);
this->varEntry.data = this->get_raw_bytes(this->varEntry.data_length);
}
/*** Utils. ***/
bool TIVarFile::isValid()
{
return (this->isFromFile) ? (this->computedChecksum == this->inFileChecksum)
: (this->computedChecksum != 0);
}
/*** Private actions ***/
uint16_t TIVarFile::computeChecksumFromFileData()
{
if (this->isFromFile)
{
long dataSectionOffset = (8 + 3 + 42 + 2); // after header
fseek(this->file, dataSectionOffset, SEEK_SET);
uint16_t sum = 0;
for (long i = dataSectionOffset; i < this->fileSize - 2; i++)
{
sum += this->get_raw_bytes(1)[0];
}
return (uint16_t) (sum & 0xFFFF);
} else {
throw runtime_error("[Error] No file loaded");
}
}
uint16_t TIVarFile::computeChecksumFromInstanceData()
{
uint16_t sum = 0;
sum += std::accumulate(this->varEntry.constBytes, this->varEntry.constBytes + 2, 0);
sum += 2 * ((this->varEntry.data_length & 0xFF) + ((this->varEntry.data_length >> 8) & 0xFF));
sum += this->varEntry.typeID + (int)this->varEntry.version + (int)this->varEntry.archivedFlag;
sum += std::accumulate(this->varEntry.varname, this->varEntry.varname + 8, 0);
for (uint16_t i=0; i<this->varEntry.data_length; i++)
{
sum += this->varEntry.data[i];
}
return (uint16_t) (sum & 0xFFFF);
}
uint16_t TIVarFile::getChecksumValueFromFile()
{
if (this->isFromFile)
{
fseek(this->file, this->fileSize - 2, SEEK_SET);
return this->get_raw_bytes(1)[0] + (this->get_raw_bytes(1)[0] << 8);
} else {
throw runtime_error("[Error] No file loaded");
}
}
/**
* Updates the length fields in both the header and the var entry, as well as the checksum
*/
void TIVarFile::refreshMetadataFields()
{
this->varEntry.data_length = this->varEntry.data_length2 = (uint16_t) this->varEntry.data.size();
this->header.entries_len = (uint16_t) (this->varEntry.data_length + 17); // 17 == sum of the individual sizes.
this->computedChecksum = this->computeChecksumFromInstanceData();
}
string TIVarFile::fixVarName(const string& name)
{
string newName(name);
if (newName == "")
{
newName = "FILE" + ((type.getExts().size() > 0) ? type.getExts()[0] : "");
}
newName = regex_replace(newName, regex("[^a-zA-Z0-9]"), "");
if (newName.length() > 8 || newName == "" || is_numeric(newName.substr(0, 1)))
{
throw invalid_argument("Invalid name given. 8 chars (A-Z, 0-9) max, starting by a letter");
}
for (auto & c: newName) c = (char) toupper(c);
newName = str_pad(newName, 8, "\0");
return newName;
}
/*** Public actions **/
/**
* @param array data The array of bytes
*/
void TIVarFile::setContentFromData(const data_t data)
{
if (data.size() > 0)
{
this->varEntry.data = data;
this->refreshMetadataFields();
} else {
throw runtime_error("[Error] No data given");
}
}
void TIVarFile::setContentFromString(const string str, const options_t options)
{
this->varEntry.data = (this->type.getHandlers().first)(str, options);
this->refreshMetadataFields();
}
void TIVarFile::setCalcModel(const TIModel model)
{
this->calcModel = model;
string signature = model.getSig();
std::copy(signature.begin(), signature.end(), this->header.signature);
}
void TIVarFile::setVarName(const std::string name)
{
string varname = TIVarFile::fixVarName(name);
std::copy(varname.begin(), varname.begin() + 7, this->varEntry.varname);
this->refreshMetadataFields();
}
data_t TIVarFile::getRawContent()
{
return this->varEntry.data;
}
string TIVarFile::getReadableContent(const options_t options)
{
return (this->type.getHandlers().second)(this->varEntry.data, options);
}
void TIVarFile::fixChecksumInFile()
{
if (this->isFromFile)
{
if (!this->isValid())
{
fseek(this->file, this->fileSize - 2, SEEK_SET);
char buf[2] = {(char) (this->computedChecksum & 0xFF), (char) ((this->computedChecksum >> 8) & 0xFF)};
fwrite(buf, sizeof(char), sizeof(buf), this->file);
this->inFileChecksum = this->getChecksumValueFromFile();
}
} else {
throw runtime_error("[Error] No file loaded");
}
}
data_t TIVarFile::bindata_maker()
{
data_t bin_data;
// Header
{
for (uint i = 0; i < 8; i++)
{
bin_data.push_back(this->header.signature[i]);
}
for (uint i = 0; i < 3; i++)
{
bin_data.push_back(this->header.sig_extra[i]);
}
for (uint i = 0; i < 42; i++)
{
bin_data.push_back(this->header.comment[i]);
}
bin_data.push_back((uchar) (this->header.entries_len & 0xFF));
bin_data.push_back((uchar) ((this->header.entries_len >> 8) & 0xFF));
}
// Var entry
{
for (uint i = 0; i < 2; i++)
{
bin_data.push_back(this->varEntry.constBytes[i]);
}
bin_data.push_back((uchar) (this->varEntry.data_length & 0xFF));
bin_data.push_back((uchar) ((this->varEntry.data_length >> 8) & 0xFF));
bin_data.push_back(this->varEntry.typeID);
for (uint i = 0; i < 8; i++)
{
bin_data.push_back(this->varEntry.varname[i]);
}
if (this->varEntry.version != (uchar)-1)
{
bin_data.push_back(this->varEntry.version);
}
if (this->varEntry.archivedFlag != (uchar)-1)
{
bin_data.push_back(this->varEntry.archivedFlag);
}
bin_data.push_back((uchar) (this->varEntry.data_length2 & 0xFF));
bin_data.push_back((uchar) ((this->varEntry.data_length2 >> 8) & 0xFF));
for (uint i = 0; i < this->varEntry.data_length; i++)
{
bin_data.push_back(this->varEntry.data[i]);
}
}
return bin_data;
}
/**
* Writes a variable to an actual file on the FS
* If the variable was already loaded from a file, it will be used and overwritten,
* except if a specific directory and name are provided.
*
* @param string directory Directory to save the file to
* @param string name Name of the file, without the extension
*/
void TIVarFile::saveVarToFile(string directory, string name)
{
FILE* handle;
if (this->isFromFile && directory == "")
{
this->close();
handle = fopen(this->filePath.c_str(), "wb");
} else {
if (name == "")
{
string tmp("");
for (uint i=0; i<8; i++)
{
if (this->varEntry.varname[i])
{
tmp += this->varEntry.varname[i];
} else {
break;
}
}
name = tmp;
}
int extIndex = this->calcModel.getOrderId();
if (extIndex < 0)
{
extIndex = 0;
}
string fileName = name + "." + this->getType().getExts()[extIndex];
if (directory == "")
{
directory = ".";
}
string fullPath = directory + "/" + fileName;
handle = fopen(fullPath.c_str(), "wb");
}
this->refreshMetadataFields();
data_t bin_data = bindata_maker();
uchar* buf_bin = &bin_data[0];
fwrite(buf_bin, bin_data.size(), sizeof(uchar), handle);
char buf[2] = {(char) (this->computedChecksum & 0xFF), (char) ((this->computedChecksum >> 8) & 0xFF)};
fwrite(buf, sizeof(char), sizeof(buf), handle);
fclose(handle);
}
void TIVarFile::saveVarToFile()
{
saveVarToFile("", "");
}
}