forked from sumatrapdfreader/sumatrapdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializeTxt.cpp
More file actions
741 lines (673 loc) · 20.3 KB
/
Copy pathSerializeTxt.cpp
File metadata and controls
741 lines (673 loc) · 20.3 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
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* Copyright 2015 the SumatraPDF project authors (see AUTHORS file).
License: Simplified BSD (see COPYING.BSD) */
#include "BaseUtil.h"
#include "StrSlice.h"
#include "SerializeTxt.h"
#include "TxtParser.h"
namespace sertxt {
#define NL "\r\n"
static const StructMetadata *GetStructDef(const FieldMetadata *fieldDef)
{
CrashIf(0 == fieldDef->defValOrDefinition);
return (const StructMetadata *)fieldDef->defValOrDefinition;
}
// the assumption here is that the data was either built by Deserialize()
// or was created by application code in a way that observes our rule: each
// struct and string was separately allocated with malloc()
void FreeStruct(uint8_t *structStart, const StructMetadata *def)
{
if (!structStart)
return;
Type type;
const FieldMetadata *fieldDef = nullptr;
for (int i = 0; i < def->nFields; i++) {
fieldDef = def->fields + i;
uint8_t *data = structStart + fieldDef->offset;
type = (Type)(fieldDef->type & TYPE_MASK);
if (TYPE_STRUCT_PTR == type) {
uint8_t **p = (uint8_t**)data;
FreeStruct(*p, GetStructDef(fieldDef));
*p = nullptr;
} else if (TYPE_ARRAY == type) {
Vec<uint8_t*> **vecPtr = (Vec<uint8_t*> **)data;
Vec<uint8_t*> *vec = *vecPtr;
CrashIf(!vec);
for (size_t j = 0; j < vec->Count(); j++) {
FreeStruct(vec->At(j), GetStructDef(fieldDef));
}
delete vec;
*vecPtr = nullptr;
} else if ((TYPE_STR == type) || (TYPE_WSTR == type)) {
char **sp = (char**)data;
char *s = *sp;
free(s);
*sp = nullptr;
}
}
free(structStart);
}
static bool IsSignedIntType(Type type)
{
return ((TYPE_I16 == type) ||
(TYPE_I32 == type));
}
static bool IsUnsignedIntType(Type type)
{
return ((TYPE_U16 == type) ||
(TYPE_U32 == type) ||
(TYPE_U64 == type));
}
static bool WriteStructInt(uint8_t *p, Type type, int64_t val)
{
if (TYPE_I16 == type) {
if (val > 0xffff)
return false;
int16_t v = (int16_t)val;
int16_t *vp = (int16_t*)p;
*vp = v;
return true;
}
if (TYPE_I32 == type) {
if (val > 0xffffffff)
return false;
int32_t v = (int32_t)val;
int32_t *vp = (int32_t*)p;
*vp = v;
return true;
}
CrashIf(true);
return false;
}
static void WriteStructBool(uint8_t *p, bool val)
{
bool *bp = (bool*)p;
if (val)
*bp = true;
else
*bp = false;
}
static bool WriteStructUInt(uint8_t *p, Type type, uint64_t val)
{
if (TYPE_U16 == type) {
if (val > 0xffff)
return false;
uint16_t v = (uint16_t)val;
uint16_t *vp = (uint16_t*)p;
*vp = v;
return true;
}
if ((TYPE_U32 == type) || (TYPE_COLOR == type)) {
if (val > 0xffffffff)
return false;
uint32_t v = (uint32_t)val;
uint32_t *vp = (uint32_t*)p;
*vp = v;
return true;
}
if (TYPE_U64 == type) {
uint64_t *vp = (uint64_t*)p;
*vp = val;
return true;
}
CrashIf(true);
return false;
}
static void WriteStructPtrVal(uint8_t *p, void *val)
{
void **pp = (void**)p;
*pp = val;
}
static void WriteStructStr(uint8_t *p, char *s)
{
char **sp = (char **)p;
*sp = s;
}
static void WriteStructWStr(uint8_t *p, WCHAR *s)
{
WCHAR **sp = (WCHAR **)p;
*sp = s;
}
static void WriteStructFloat(uint8_t *p, float f)
{
float *fp = (float*)p;
*fp = f;
}
static bool ReadStructBool(const uint8_t *p)
{
bool *bp = (bool*)p;
return *bp;
}
static int64_t ReadStructInt(const uint8_t *p, Type type)
{
if (TYPE_I16 == type) {
int16_t *vp = (int16_t*)p;
return (int64_t)*vp;
}
if (TYPE_I32 == type) {
int32_t *vp = (int32_t*)p;
return (int64_t)*vp;
}
CrashIf(true);
return 0;
}
static uint64_t ReadStructUInt(const uint8_t *p, Type type)
{
if (TYPE_U16 == type) {
uint16_t *vp = (uint16_t*)p;
return (uint64_t)*vp;
}
if ((TYPE_U32 == type) || (TYPE_COLOR == type)) {
uint32_t *vp = (uint32_t*)p;
return (uint64_t)*vp;
}
if (TYPE_U64 == type) {
uint64_t *vp = (uint64_t*)p;
return *vp;
}
CrashIf(true);
return 0;
}
static float ReadStructFloat(const uint8_t *p)
{
float *fp = (float*)p;
return *fp;
}
static void *ReadStructPtr(const uint8_t *p)
{
void **pp = (void**)p;
return *pp;
}
class DecodeState {
public:
// data being decoded
TxtParser parser;
DecodeState() {}
};
static bool ParseUInt(char *s, char *e, uint64_t *nOut)
{
str::TrimWsEnd(s, e);
int d;
uint64_t n = 0;
uint64_t prev = 0;
while (s < e) {
d = *s - '0';
if (d < 0 || d > 9)
return false;
n = n * 10 + d;
if (n < prev) {
// on overflow return 0
*nOut = 0;
return true;
}
prev = n;
++s;
}
*nOut = n;
return true;
}
static bool ParseInt(char *s, char *e, int64_t *iOut)
{
if (s >= e)
return false;
bool neg = false;
if ('-' == *s) {
neg = true;
s++;
}
uint64_t u;
if (!ParseUInt(s, e, &u))
return false;
#if 0 // TODO:: why is this missing?
if (u > MAXLONG64)
return false;
#endif
int64_t i = (int64_t)u;
if (neg)
i = -i;
*iOut = i;
return true;
}
static bool ParseColor(char *s, char *e, COLORREF *colOut)
{
str::TrimWsEnd(s, e);
*e = 0;
int a, r, g, b;
if (!str::Parse(s, "#%2x%2x%2x%2x", &a, &r, &g, &b)) {
a = 0;
if (!str::Parse(s, "#%2x%2x%2x", &r, &g, &b)) {
return false;
}
}
COLORREF col = RGB(r, g, b);
COLORREF alpha = (COLORREF)a;
alpha = alpha << 24;
col = col | alpha;
*colOut = col;
return true;
}
static bool ParseBool(char *s, char *e, bool *bOut)
{
str::TrimWsEnd(s, e);
size_t len = e - s;
if (4 == len && str::EqNI(s, "true", 4)) {
*bOut = true;
return true;
}
if (5 == len && str::EqNI(s, "false", 5)) {
*bOut = false;
return true;
}
int64_t i;
if (!ParseInt(s, e, &i))
return false;
if (0 == i) {
*bOut = false;
return true;
}
if (1 == i) {
*bOut = true;
return true;
}
return false;
}
static bool ParseFloat(char *s, char *e, float *f)
{
char *end = e;
*f = (float)strtod(s, &end);
return true;
}
static uint8_t* DeserializeRec(DecodeState& ds, TxtNode *firstNode, const StructMetadata *def);
static TxtNode *FindNode(TxtNode *curr, const char *name, size_t nameLen)
{
if (!curr)
return nullptr;
char *nodeName;
size_t nodeNameLen;
TxtNode *child;
TxtNode *found = nullptr;
for (size_t i = 0; i < curr->children->Count(); i++) {
child = curr->children->At(i);
if (TextNode == child->type || StructNode == child->type) {
nodeName = child->keyStart;
nodeNameLen = child->keyEnd - nodeName;
if (nameLen == nodeNameLen && str::EqNI(name, nodeName, nameLen))
return child;
}
if (TextNode == child->type)
continue;
found = FindNode(child, name, nameLen);
if (found)
return found;
}
return nullptr;
}
static void WriteDefaultValue(uint8_t *structDataPtr, Type type)
{
// all other types have default value of 0, which we get for
// free because the memory for struct is zero-allocated
if (TYPE_FLOAT == type) {
WriteStructFloat(structDataPtr, 0);
}
}
static void FreeTxtNode(TxtNode *node)
{
if (node->children) {
for (size_t i = 0; i < node->children->Count(); i++) {
TxtNode *child = node->children->At(i);
CrashIf(TextNode != child->type);
delete child;
}
}
delete node->children;
delete node;
}
static TxtNode *StructNodeFromTextNode(DecodeState& ds, TxtNode *txtNode, const StructMetadata *structDef)
{
UNUSED(ds);
CrashIf(TextNode != txtNode->type);
str::Slice slice(txtNode->valStart, txtNode->valEnd);
TxtNode *node = new TxtNode(StructNode);
node->children = new Vec<TxtNode*>();
uint16_t fieldNo = 0;
char *fieldName = (char*)structDef->fieldNames;
TxtNode *child;
for (;;) {
slice.SkipWsUntilNewline();
if (slice.Finished())
goto Error;
child = new TxtNode(TextNode);
child->valStart = slice.curr;
slice.SkipNonWs();
child->valEnd = slice.curr;
child->keyStart = fieldName;
child->keyEnd = fieldName + str::Len(fieldName);
node->children->Append(child);
++fieldNo;
if (fieldNo == structDef->nFields)
break;
seqstrings::SkipStr(fieldName);
}
return node;
Error:
FreeTxtNode(node);
return nullptr;
}
static uint8_t *DeserializeCompact(DecodeState& ds, TxtNode *node, const StructMetadata *structDef)
{
CrashIf(TextNode != node->type);
TxtNode *structNode = StructNodeFromTextNode(ds, node, structDef);
if (!structNode)
return nullptr;
uint8_t *res = DeserializeRec(ds, structNode, structDef);
FreeTxtNode(structNode);
return res;
}
static uint8_t *DecodeStruct(DecodeState& ds, const FieldMetadata *fieldDef, TxtNode *node, bool isCompact)
{
uint8_t *d = nullptr;
if (isCompact && (TextNode == node->type)) {
d = DeserializeCompact(ds, node, GetStructDef(fieldDef));
} else {
if (StructNode == node->type)
d = DeserializeRec(ds, node, GetStructDef(fieldDef));
}
return d;
}
static bool DecodeField(DecodeState& ds, TxtNode *firstNode, const char *fieldName, const FieldMetadata *fieldDef, uint8_t *structDataStart)
{
Type type = fieldDef->type;
uint8_t *structDataPtr = structDataStart + fieldDef->offset;
if ((type & TYPE_NO_STORE_MASK) != 0) {
WriteDefaultValue(structDataPtr, type);
return true;
}
bool isCompact = ((type & TYPE_STORE_COMPACT_MASK) != 0);
type = (Type)(type & TYPE_MASK);
size_t fieldNameLen = str::Len(fieldName);
// if the node doesn't exist in data, try to get it from default data
TxtNode *node = FindNode(firstNode, fieldName, fieldNameLen);
if (!node) {
WriteDefaultValue(structDataPtr, type);
return true;
}
bool ok;
if (TYPE_BOOL == type) {
bool bVal;
ok = ParseBool(node->valStart, node->valEnd, &bVal);
if (!ok)
return false;
WriteStructBool(structDataPtr, bVal);
} else if (TYPE_COLOR == type) {
COLORREF val;
ok = ParseColor(node->valStart, node->valEnd, &val);
if (ok)
WriteStructUInt(structDataPtr, TYPE_COLOR, val);
} else if (IsUnsignedIntType(type)) {
uint64_t n;
ok = ParseUInt(node->valStart, node->valEnd, &n);
if (ok)
ok = WriteStructUInt(structDataPtr, type, n);
} else if (IsSignedIntType(type)) {
int64_t n;
ok = ParseInt(node->valStart, node->valEnd, &n);
if (ok)
ok = WriteStructInt(structDataPtr, type, n);
} else if (TYPE_STRUCT_PTR == type) {
uint8_t *d = DecodeStruct(ds, fieldDef, node, isCompact);
if (d)
WriteStructPtrVal(structDataPtr, d);
} else if (TYPE_STR == type) {
char *s = node->valStart;
size_t sLen = node->valEnd - s;
if (s && (sLen > 0)) {
// note: we don't free s because it's remembered in structDataPtr
s = str::DupN(s, sLen);
WriteStructStr(structDataPtr, s);
}
} else if (TYPE_WSTR == type) {
char *s = node->valStart;
size_t sLen = node->valEnd - s;
if (s && (sLen > 0)) {
// note: we don't free ws because it's remembered in structDataPtr
WCHAR *ws = str::conv::FromUtf8(s);
WriteStructWStr(structDataPtr, ws);
}
} else if (TYPE_FLOAT == type) {
float f;
ok = ParseFloat(node->valStart, node->valEnd, &f);
if (ok)
WriteStructFloat(structDataPtr, f);
} else if (TYPE_ARRAY == type) {
if (StructNode != node->type)
return false;
Vec<uint8_t*> *vec = new Vec<uint8_t*>();
// we remember it right away, so that it gets freed in case of error
WriteStructPtrVal(structDataPtr, (void*)vec);
TxtNode *child;
for (size_t i = 0; i < node->children->Count(); i++) {
child = node->children->At(i);
uint8_t *d = DecodeStruct(ds, fieldDef, child, isCompact);
if (d)
vec->Append(d);
}
} else {
CrashIf(true);
return false;
}
return true;
}
static uint8_t* DeserializeRec(DecodeState& ds, TxtNode *firstNode, const StructMetadata *def)
{
bool ok = true;
if (!firstNode)
return nullptr;
uint8_t *res = AllocArray<uint8_t>(def->size);
const StructMetadata **defPtr = (const StructMetadata**)res;
*defPtr = def;
const char *fieldName = def->fieldNames;
for (int i = 0; i < def->nFields; i++) {
ok = DecodeField(ds, firstNode, fieldName, def->fields + i, res);
if (!ok)
goto Error;
seqstrings::SkipStr(fieldName);
}
return res;
Error:
FreeStruct(res, def);
return nullptr;
}
uint8_t* Deserialize(struct TxtNode *root, const StructMetadata *def)
{
DecodeState ds;
return DeserializeRec(ds, root, def);
}
uint8_t* Deserialize(char *data, size_t dataSize, const StructMetadata *def)
{
if (!data)
return nullptr;
DecodeState ds;
ds.parser.SetToParse(data, dataSize);
bool ok = ParseTxt(ds.parser);
if (!ok)
return nullptr;
return DeserializeRec(ds, ds.parser.nodes.At(0), def);
}
static void AppendNest(str::Str<char>& s, int nest)
{
while (nest > 0) {
s.Append(" ");
--nest;
}
}
static void AppendVal(const char *val, char escapeChar, bool compact, str::Str<char>& res)
{
const char *start = val;
const char *s = start;
char escaped = 0;
while (*s) {
char c = *s++;
if (escapeChar == c)
escaped = escapeChar;
else if (']' == c)
escaped = ']';
else if ('[' == c)
escaped = '[';
else if ('\n' == c)
escaped = 'n';
else if ('\r' == c)
escaped = 'r';
if (0 == escaped)
continue;
size_t len = s - start - 1;
res.Append(start, len);
res.Append(escapeChar);
res.Append(escaped);
start = s;
escaped = 0;
}
size_t len = s - start;
res.Append(start, len);
if (!compact)
res.Append(NL);
}
struct EncodeState {
str::Str<char> res;
char escapeChar;
// nesting level for the currently serialized value
int nest;
// is currently serialized structure in compact form
bool compact;
EncodeState() {
escapeChar = SERIALIZE_ESCAPE_CHAR;
nest = 0;
compact = false;
}
};
static void AppendKeyVal(EncodeState& es, const char *key, const char *val)
{
if (es.compact) {
es.res.Append(" ");
} else {
AppendNest(es.res, es.nest);
es.res.Append(key);
es.res.Append(": ");
}
AppendVal(val, es.escapeChar, es.compact, es.res);
}
void SerializeRec(EncodeState& es, const uint8_t *structStart, const StructMetadata *def);
static void SerializeField(EncodeState& es, const char *fieldName, const FieldMetadata *fieldDef, const uint8_t *structStart)
{
str::Str<char> val;
str::Str<char>& res = es.res;
Type type = fieldDef->type;
if ((type & TYPE_NO_STORE_MASK) != 0)
return;
if (!structStart)
return;
bool isCompact = ((type & TYPE_STORE_COMPACT_MASK) != 0);
type = (Type)(type & TYPE_MASK);
const uint8_t *data = structStart + fieldDef->offset;
if (TYPE_BOOL == type) {
bool b = ReadStructBool(data);
AppendKeyVal(es, fieldName, b ? "true" : "false");
} else if (TYPE_COLOR == type) {
uint64_t u = ReadStructUInt(data, type);
COLORREF c = (COLORREF)u;
int r = (int)((uint8_t)(c & 0xff));
int g = (int)((uint8_t)((c >> 8) & 0xff));
int b = (int)((uint8_t)((c >> 16) & 0xff));
int a = (int)((uint8_t)((c >> 24) & 0xff));
if (a > 0)
val.AppendFmt("#%02x%02x%02x%02x", a, r, g, b);
else
val.AppendFmt("#%02x%02x%02x", r, g, b);
AppendKeyVal(es, fieldName, val.Get());
} else if (IsUnsignedIntType(type)) {
uint64_t u = ReadStructUInt(data, type);
//val.AppendFmt("%" PRIu64, u);
val.AppendFmt("%I64u", u);
AppendKeyVal(es, fieldName, val.Get());
} else if (IsSignedIntType(type)) {
int64_t i = ReadStructInt(data, type);
//val.AppendFmt("%" PRIi64, u);
val.AppendFmt("%I64d", i);
AppendKeyVal(es, fieldName, val.Get());
} else if (TYPE_FLOAT == type) {
float f = ReadStructFloat(data);
val.AppendFmt("%g", f);
AppendKeyVal(es, fieldName, val.Get());
} else if (TYPE_STR == type) {
char *s = (char*)ReadStructPtr(data);
if (s)
AppendKeyVal(es, fieldName, s);
} else if (TYPE_WSTR == type) {
WCHAR *s = (WCHAR*)ReadStructPtr(data);
if (s) {
AutoFree val2(str::conv::ToUtf8(s));
AppendKeyVal(es, fieldName, val2);
}
} else if (TYPE_STRUCT_PTR == type) {
AppendNest(res, es.nest);
res.Append(fieldName);
if (isCompact)
res.Append(":");
else
res.Append(" [" NL);
const uint8_t *structStart2 = (const uint8_t *)ReadStructPtr(data);
++es.nest;
// compact status only lives for one structure, so this is enough
es.compact = isCompact;
SerializeRec(es, structStart2, GetStructDef(fieldDef));
--es.nest;
es.compact = false;
if (isCompact) {
res.Append(NL);
} else {
AppendNest(res, es.nest);
res.Append("]" NL);
}
} else if (TYPE_ARRAY == type) {
AppendNest(res, es.nest);
res.Append(fieldName);
res.Append(" [" NL);
Vec<const uint8_t*> *vec = (Vec<const uint8_t*> *)ReadStructPtr(data);
++es.nest;
for (size_t i = 0; vec && (i < vec->Count()); i++) {
AppendNest(res, es.nest);
res.Append("[" NL);
const uint8_t *elData = vec->At(i);
++es.nest;
SerializeRec(es, elData, GetStructDef(fieldDef));
--es.nest;
AppendNest(res, es.nest);
res.Append("]" NL);
}
--es.nest;
AppendNest(res, es.nest);
res.Append("]" NL);
} else {
CrashIf(true);
}
}
void SerializeRec(EncodeState& es, const uint8_t *structStart, const StructMetadata *def)
{
if (!structStart)
return;
const char *fieldName = def->fieldNames;
for (size_t i = 0; i < def->nFields; i++) {
const FieldMetadata *fieldDef = &def->fields[i];
SerializeField(es, fieldName, fieldDef, structStart);
seqstrings::SkipStr(fieldName);
}
}
uint8_t *Serialize(const uint8_t *rootStruct, const StructMetadata *def, size_t *sizeOut)
{
EncodeState es;
es.res.Append(UTF8_BOM "; see https://www.sumatrapdfreader.org/settings.html for documentation" NL);
es.nest = 0;
SerializeRec(es, rootStruct, def);
if (sizeOut)
*sizeOut = es.res.Size();
return (uint8_t *)es.res.StealData();
}
} // namespace sertxt