forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectSerializer.cpp
599 lines (488 loc) · 16.1 KB
/
ProjectSerializer.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/**********************************************************************
Sneedacity: A Digital Audio Editor
Sneedacity(R) is copyright (c) 1999-2010 Sneedacity Team.
License: GPL v2. See License.txt.
ProjectSerializer.cpp
*******************************************************************//**
\class ProjectSerializer
\brief a class used to (de)serialize the project catalog
*//********************************************************************/
#include "ProjectSerializer.h"
#include <algorithm>
#include <cstdint>
#include <mutex>
#include <wx/ustring.h>
///
/// ProjectSerializer class
///
// Simple "binary xml" format used exclusively for project documents.
//
// It is not intended that the user view or modify the file.
//
// It IS intended that very little work be done during auto save, so numbers
// and strings are written in their native format. They will be converted
// during recovery.
//
// The file has 3 main sections:
//
// character size 1 (UTF-8), 2 (UTF-16) or 4 (UTF-32)
// name dictionary dictionary of all names used in the document
// data fields the "encoded" XML document
//
// If a subtree is added, it will be preceded with FT_Push to tell the decoder
// to preserve the active dictionary. The decoder will then restore the
// dictionary when an FT_Pop is encountered. Nesting is unlimited.
//
// To save space, each name (attribute or element) encountered is stored in
// the name dictionary and replaced with the assigned 2-byte identifier.
//
// All strings are in native unicode format, 2-byte or 4-byte.
//
// All name "lengths" are 2-byte signed, so are limited to 32767 bytes long.
// All string/data "lengths" are 4-byte signed.
enum FieldTypes
{
FT_CharSize, // type, ID, value
FT_StartTag, // type, ID
FT_EndTag, // type, ID
FT_String, // type, ID, string length, string
FT_Int, // type, ID, value
FT_Bool, // type, ID, value
FT_Long, // type, ID, value
FT_LongLong, // type, ID, value
FT_SizeT, // type, ID, value
FT_Float, // type, ID, value, digits
FT_Double, // type, ID, value, digits
FT_Data, // type, string length, string
FT_Raw, // type, string length, string
FT_Push, // type only
FT_Pop, // type only
FT_Name // type, ID, name length, name
};
// Static so that the dict can be reused each time.
//
// If entries get added later, like when an envelope node (for example)
// is written and then the envelope is later removed, the dict will still
// contain the envelope name, but that's not a problem.
NameMap ProjectSerializer::mNames;
wxMemoryBuffer ProjectSerializer::mDict;
TranslatableString ProjectSerializer::FailureMessage( const FilePath &/*filePath*/ )
{
return
XO("This recovery file was saved by Sneedacity 2.3.0 or before.\n"
"You need to run that version of Sneedacity to recover the project." );
}
namespace {
// Aliases for the FIXED-WIDTH integer types that are used in the file
// format.
// Chosen so that among the four build types (32 bit Windows, 64
// bit Windows, 64 bit Mac clang, Linux g++) presently done (3.0.0
// development), we use the narrowest width of the type on any of them, so
// that anything saved on one build will be read back identically on all
// builds. (Although this means that very large values on some systems might
// be saved and then read back with loss.)
// In fact the only types for which this matters are long (only 32 bits on
// 32 and 64 bit Windows) and size_t (only 32 bits on 32 bit Windows).
using UShort = std::uint16_t;
using Int = std::int32_t;
using Long = std::int32_t; // To save long values
using ULong = std::uint32_t; // To save size_t values
using LongLong = std::int64_t;
// Detect this computer's endianness
bool IsLittleEndian()
{
const std::uint32_t x = 1u;
return
static_cast<const unsigned char*>(static_cast<const void*>(&x))[0];
// We will assume the same for other widths!
}
// In C++20 this could be
// constexpr bool IsLittleEndian = (std::endian::native == std::endian::little);
// static_assert( IsLittleEndian || (std::endian::native == std::endian::big),
// "Oh no! I'm mixed-endian!" );
// Functions that can read and write native integer types to a canonicalized
// little-endian file format. (We don't bother to do the same for floating
// point numbers.)
// Write native little-endian to little-endian file format
template< typename Number >
void WriteLittleEndian( wxMemoryBuffer &out, Number value )
{
out.AppendData( &value, sizeof(value) );
}
// Write native big-endian to little-endian file format
template< typename Number >
void WriteBigEndian( wxMemoryBuffer &out, Number value )
{
auto begin = static_cast<unsigned char*>( static_cast<void*>( &value ) );
std::reverse( begin, begin + sizeof( value ) );
out.AppendData( &value, sizeof(value) );
}
// Read little-endian file format to native little-endian
template< typename Number >
Number ReadLittleEndian( wxMemoryInputStream &in )
{
Number result;
in.Read( &result, sizeof(result) );
return result;
}
// Read little-endian file format to native big-endian
template< typename Number >
Number ReadBigEndian( wxMemoryInputStream &in )
{
Number result;
in.Read( &result, sizeof(result) );
auto begin = static_cast<unsigned char*>( static_cast<void*>( &result ) );
std::reverse( begin, begin + sizeof( result ) );
return result;
}
// Choose between implementations!
static const auto WriteUShort = IsLittleEndian()
? &WriteLittleEndian<UShort> : &WriteBigEndian<UShort>;
static const auto WriteInt = IsLittleEndian()
? &WriteLittleEndian<Int> : &WriteBigEndian<Int>;
static const auto WriteLong = IsLittleEndian()
? &WriteLittleEndian<Long> : &WriteBigEndian<Long>;
static const auto WriteULong = IsLittleEndian()
? &WriteLittleEndian<ULong> : &WriteBigEndian<ULong>;
static const auto WriteLongLong = IsLittleEndian()
? &WriteLittleEndian<LongLong> : &WriteBigEndian<LongLong>;
static const auto ReadUShort = IsLittleEndian()
? &ReadLittleEndian<UShort> : &ReadBigEndian<UShort>;
static const auto ReadInt = IsLittleEndian()
? &ReadLittleEndian<Int> : &ReadBigEndian<Int>;
static const auto ReadLong = IsLittleEndian()
? &ReadLittleEndian<Long> : &ReadBigEndian<Long>;
static const auto ReadULong = IsLittleEndian()
? &ReadLittleEndian<ULong> : &ReadBigEndian<ULong>;
static const auto ReadLongLong = IsLittleEndian()
? &ReadLittleEndian<LongLong> : &ReadBigEndian<LongLong>;
// Functions to read and write certain lengths -- maybe we will change
// our choices for widths or signedness?
using Length = Int; // Instead, as wide as size_t?
static const auto WriteLength = WriteInt;
static const auto ReadLength = ReadInt;
using Digits = Int; // Instead, just an unsigned char?
static const auto WriteDigits = WriteInt;
static const auto ReadDigits = ReadInt;
}
ProjectSerializer::ProjectSerializer(size_t allocSize)
{
mDict.SetBufSize(allocSize);
mBuffer.SetBufSize(allocSize);
static std::once_flag flag;
std::call_once(flag, []{
// Just once per run, store header information in the unique static
// dictionary that will be written into each project that is saved.
// Store the size of "wxStringCharType" so we can convert during recovery
// in case the file is used on a system with a different character size.
char size = sizeof(wxStringCharType);
mDict.AppendByte(FT_CharSize);
mDict.AppendData(&size, 1);
});
mDictChanged = false;
}
ProjectSerializer::~ProjectSerializer()
{
}
void ProjectSerializer::StartTag(const wxString & name)
{
mBuffer.AppendByte(FT_StartTag);
WriteName(name);
}
void ProjectSerializer::EndTag(const wxString & name)
{
mBuffer.AppendByte(FT_EndTag);
WriteName(name);
}
void ProjectSerializer::WriteAttr(const wxString & name, const wxChar *value)
{
WriteAttr(name, wxString(value));
}
void ProjectSerializer::WriteAttr(const wxString & name, const wxString & value)
{
mBuffer.AppendByte(FT_String);
WriteName(name);
const Length len = value.length() * sizeof(wxStringCharType);
WriteLength( mBuffer, len );
mBuffer.AppendData(value.wx_str(), len);
}
void ProjectSerializer::WriteAttr(const wxString & name, int value)
{
mBuffer.AppendByte(FT_Int);
WriteName(name);
WriteInt( mBuffer, value );
}
void ProjectSerializer::WriteAttr(const wxString & name, bool value)
{
mBuffer.AppendByte(FT_Bool);
WriteName(name);
mBuffer.AppendByte(value);
}
void ProjectSerializer::WriteAttr(const wxString & name, long value)
{
mBuffer.AppendByte(FT_Long);
WriteName(name);
WriteLong( mBuffer, value );
}
void ProjectSerializer::WriteAttr(const wxString & name, long long value)
{
mBuffer.AppendByte(FT_LongLong);
WriteName(name);
WriteLongLong( mBuffer, value );
}
void ProjectSerializer::WriteAttr(const wxString & name, size_t value)
{
mBuffer.AppendByte(FT_SizeT);
WriteName(name);
WriteULong( mBuffer, value );
}
void ProjectSerializer::WriteAttr(const wxString & name, float value, int digits)
{
mBuffer.AppendByte(FT_Float);
WriteName(name);
mBuffer.AppendData(&value, sizeof(value));
WriteDigits( mBuffer, digits );
}
void ProjectSerializer::WriteAttr(const wxString & name, double value, int digits)
{
mBuffer.AppendByte(FT_Double);
WriteName(name);
mBuffer.AppendData(&value, sizeof(value));
WriteDigits( mBuffer, digits );
}
void ProjectSerializer::WriteData(const wxString & value)
{
mBuffer.AppendByte(FT_Data);
Length len = value.length() * sizeof(wxStringCharType);
WriteLength( mBuffer, len );
mBuffer.AppendData(value.wx_str(), len);
}
void ProjectSerializer::Write(const wxString & value)
{
mBuffer.AppendByte(FT_Raw);
Length len = value.length() * sizeof(wxStringCharType);
WriteLength( mBuffer, len );
mBuffer.AppendData(value.wx_str(), len);
}
void ProjectSerializer::WriteSubTree(const ProjectSerializer & value)
{
mBuffer.AppendByte(FT_Push);
mBuffer.AppendData(value.mDict.GetData(), value.mDict.GetDataLen());
mBuffer.AppendData(value.mBuffer.GetData(), value.mBuffer.GetDataLen());
mBuffer.AppendByte(FT_Pop);
}
void ProjectSerializer::WriteName(const wxString & name)
{
wxASSERT(name.length() * sizeof(wxStringCharType) <= SHRT_MAX);
UShort id;
auto nameiter = mNames.find(name);
if (nameiter != mNames.end())
{
id = nameiter->second;
}
else
{
// mNames is static. This appends each name to static mDict only once
// in each run.
UShort len = name.length() * sizeof(wxStringCharType);
id = mNames.size();
mNames[name] = id;
mDict.AppendByte(FT_Name);
WriteUShort( mDict, id );
WriteUShort( mDict, len );
mDict.AppendData(name.wx_str(), len);
mDictChanged = true;
}
WriteUShort( mBuffer, id );
}
const wxMemoryBuffer &ProjectSerializer::GetDict() const
{
return mDict;
}
const wxMemoryBuffer &ProjectSerializer::GetData() const
{
return mBuffer;
}
bool ProjectSerializer::IsEmpty() const
{
return mBuffer.GetDataLen() == 0;
}
bool ProjectSerializer::DictChanged() const
{
return mDictChanged;
}
// See ProjectFileIO::LoadProject() for explanation of the blockids arg
wxString ProjectSerializer::Decode(const wxMemoryBuffer &buffer)
{
wxMemoryInputStream in(buffer.GetData(), buffer.GetDataLen());
XMLStringWriter out;
std::vector<char> bytes;
IdMap mIds;
std::vector<IdMap> mIdStack;
char mCharSize = 0;
mIds.clear();
struct Error{}; // exception type for short-range try/catch
auto Lookup = [&mIds]( UShort id ) -> const wxString &
{
auto iter = mIds.find( id );
if (iter == mIds.end())
{
throw Error{};
}
return iter->second;
};
auto ReadString = [&mCharSize, &in, &bytes](int len) -> wxString
{
bytes.reserve( len + 4 );
auto data = bytes.data();
in.Read( data, len );
// Make a null terminator of the widest type
memset( data + len, '\0', 4 );
wxUString str;
switch (mCharSize)
{
case 1:
str.assignFromUTF8(data, len);
break;
case 2:
str.assignFromUTF16((wxChar16 *) data, len / 2);
break;
case 4:
str = wxU32CharBuffer::CreateNonOwned((wxChar32 *) data, len / 4);
break;
default:
wxASSERT_MSG(false, wxT("Characters size not 1, 2, or 4"));
break;
}
return str;
};
try
{
while (!in.Eof())
{
UShort id;
switch (in.GetC())
{
case FT_Push:
{
mIdStack.push_back(mIds);
mIds.clear();
}
break;
case FT_Pop:
{
mIds = mIdStack.back();
mIdStack.pop_back();
}
break;
case FT_Name:
{
id = ReadUShort( in );
auto len = ReadUShort( in );
mIds[id] = ReadString(len);
}
break;
case FT_StartTag:
{
id = ReadUShort( in );
out.StartTag(Lookup(id));
}
break;
case FT_EndTag:
{
id = ReadUShort( in );
out.EndTag(Lookup(id));
}
break;
case FT_String:
{
id = ReadUShort( in );
int len = ReadLength( in );
out.WriteAttr(Lookup(id), ReadString(len));
}
break;
case FT_Float:
{
float val;
id = ReadUShort( in );
in.Read(&val, sizeof(val));
int dig = ReadDigits( in );
out.WriteAttr(Lookup(id), val, dig);
}
break;
case FT_Double:
{
double val;
id = ReadUShort( in );
in.Read(&val, sizeof(val));
int dig = ReadDigits( in );
out.WriteAttr(Lookup(id), val, dig);
}
break;
case FT_Int:
{
id = ReadUShort( in );
int val = ReadInt( in );
out.WriteAttr(Lookup(id), val);
}
break;
case FT_Bool:
{
unsigned char val;
id = ReadUShort( in );
in.Read(&val, 1);
out.WriteAttr(Lookup(id), val);
}
break;
case FT_Long:
{
id = ReadUShort( in );
long val = ReadLong( in );
out.WriteAttr(Lookup(id), val);
}
break;
case FT_LongLong:
{
id = ReadUShort( in );
long long val = ReadLongLong( in );
out.WriteAttr(Lookup(id), val);
}
break;
case FT_SizeT:
{
id = ReadUShort( in );
size_t val = ReadULong( in );
out.WriteAttr(Lookup(id), val);
}
break;
case FT_Data:
{
int len = ReadLength( in );
out.WriteData(ReadString(len));
}
break;
case FT_Raw:
{
int len = ReadLength( in );
out.Write(ReadString(len));
}
break;
case FT_CharSize:
{
in.Read(&mCharSize, 1);
}
break;
default:
wxASSERT(true);
break;
}
}
}
catch( const Error& )
{
// Document was corrupt, or platform differences in size or endianness
// were not well canonicalized
return {};
}
return out;
}