-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.cpp
More file actions
1540 lines (1481 loc) · 53 KB
/
parse.cpp
File metadata and controls
1540 lines (1481 loc) · 53 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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****
* Part of LnkDump2000
* Licence: GPL, version 3 or later (see COPYING file or https://www.gnu.org/licenses/gpl-3.0.txt)
*****/
#include "encoding.h"
#include "parse.h"
#include <array>
#include <list>
#include <string>
#include <vector>
#include <fstream>
#include <cstdarg>
#include <limits>
#include <iostream>
namespace LnkParser {
const size_t MAX_FILE_SIZE = 1024L * 1024;
Error
Error::format(const char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
char msg[1024] = "";
vsnprintf(msg, sizeof(msg), fmt, argp);
Error rv(msg);
va_end(argp);
return rv;
}
//! check if (a+b) will give a different result than if both args were converted to a wider int
template <typename T, typename U>
static bool add_overflows(T a, U b)
{
decltype(a+b) xa(a);
decltype(a+b) xb(b);
if (a > 0 && b > 0) {
// signedness does not matter here, xa == a && xb == b, but (a+b) can be of wider type
// result of addition will be > 0, so check if result does not overflow type of (a+b)
return (xb > std::numeric_limits<decltype(a+b)>::max() - xa);
} else if (a < 0 && b < 0) {
// both integers are signed and type of (a+b) is also signed
// result of addition will be < 0, so check if result does not underflow type of (a+b)
return (xb < std::numeric_limits<decltype(a+b)>::min() - xa);
} else if constexpr (std::is_integral_v<decltype(a)>) {
if (a < 0 && xa > 0 && b >= 0) {
// signed a is being converted to unsigned, because unsigned b is wider
// check that (a+b) does not underflow at 0
decltype(a) ya = -a;
return (decltype(a+b))b < ya;
}
} else if constexpr (std::is_integral_v<decltype(b)>) {
if (b < 0 && xb > 0 && a >= 0) {
// same thing, other way around
decltype(b) yb = -b;
return (decltype(a+b))a < yb;
}
}
return false;
}
//! how many bytes in a unicode string if it NUL-terminated
static size_t u16s0_nbytes(const std::u16string& s)
{
return (s.size() + 1) * 2;
}
//! how many unicode chars in bytes, including numeric_limits
static size_t u16_nchars(size_t bytes)
{
return bytes / 2;
}
// the file is comprised of little endian numeric fields and strings
// read the whole file and implement a basic API for reading relevant types
class FileStream
{
private:
std::vector<char> m_buffer;
size_t m_pos;
private:
void int_overflow()
{
throw Error::format("Integer overflow while reading stream.");
}
public:
FileStream (const std::string& filename)
{
std::ifstream in;
in.open(filename, std::ios::in | std::ios::binary);
in.exceptions(std::ifstream::failbit | std::ifstream::badbit);
auto begin = std::istreambuf_iterator<char>(in);
auto end = std::istreambuf_iterator<char>();
for (size_t counter = 0; begin != end && counter < MAX_FILE_SIZE; begin++, counter++) {
m_buffer.push_back(*begin);
}
in.close();
m_pos = 0;
}
bool is_eof() const
{
return m_buffer.size() <= 0 || m_pos >= m_buffer.size() - 1;
}
char getc()
{
if (add_overflows(m_pos, 1)) {
int_overflow();
}
return m_buffer.at(m_pos++);
}
char peek() const
{
return m_buffer.at(m_pos);
}
void ignore(size_t len)
{
if (add_overflows(m_pos, len)) {
int_overflow();
}
m_pos += len;
}
void seekg(size_t n)
{
m_pos = n;
}
size_t tellg() const
{
return m_pos;
}
//! always gets n bytes, or crashes
void read(char* buf, size_t n)
{
for (size_t i = 0; i < n; i++) {
buf[i] = getc();
}
}
void operator >>(uint8_t & i)
{
i = getc();
}
void operator >>(uint16_t& i)
{
unsigned char tmp[2];
read((char*)tmp, sizeof(tmp));
i = uint16_t(tmp[0]) | uint16_t(tmp[1]) << 8;
}
void operator >>(uint32_t& i)
{
unsigned char tmp[4];
read((char*)tmp, sizeof(tmp));
i = uint32_t(tmp[0]) | uint32_t(tmp[1]) << 8
| uint32_t(tmp[2]) << 16 | uint32_t(tmp[3]) << 24;
}
void operator >>(uint64_t& i)
{
uint32_t tmp[2];
this->operator>>(tmp[0]);
this->operator>>(tmp[1]);
i = uint64_t(tmp[0]) | uint64_t(tmp[1]) << 32;
}
void operator >>(int16_t & i)
{
uint16_t tmp;
this->operator>>(tmp);
i = (tmp < 0x8000) ? ((int16_t)tmp) : -((int16_t)((~tmp)+1));
}
void operator >>(LnkStruct::Guid &i)
{
uint8_t tmp[16];
read((char*)tmp, sizeof(tmp));
i = LnkStruct::Guid(std::to_array(tmp));
}
std::string read_ansi(size_t max)
{
// read NUL-terminated string
std::string r;
r.reserve(16);
for (size_t i = 0; i < max; i++) {
char c = getc();
if (c == 0) {
return r;
}
r.push_back(c);
}
return r;
}
//! reads at most 'max' number of 16bit characters, including NUL
std::u16string read_unicode(size_t max)
{
// .lnk uses UTF16 for unicode
std::u16string r;
for (size_t i = 0; i < max; i++) {
uint16_t c;
this->operator>>(c);
if (c == 0) {
return r;
}
r.push_back(c);
}
return r;
}
//! reads exactly 'len' number of bytes
std::string read_exact(size_t len)
{
// strings that are exact number of bytes in the format, but cut off on \0
size_t pos = tellg();
std::string r = read_ansi(len);
seekg(pos);
ignore(len);
return r;
}
//! reads exactly 'len' number of bytes
std::u16string read_exact_unicode(size_t len)
{
// strings that are exact number of bytes in the format, but cut off on \0
size_t pos = tellg();
std::u16string r = read_unicode(u16_nchars(len));
seekg(pos);
ignore(len);
return r;
}
std::vector<uint8_t> read_binary(size_t len)
{
std::vector<uint8_t> tmp(len);
read((char*)tmp.data(), len);
return tmp;
}
};
// sections start {{{
template <class T>
class Section
{
protected:
T m_data;
std::vector<Error> m_warnings;
LnkOutput::StreamPtr m_out;
template <class... Args>
void warn(const char *fmt, Args... args)
{
auto w = Error::format(fmt, args...);
m_warnings.push_back(w);
}
public:
Section(): m_out(LnkOutput::Stream::make()) { }
T& data() { return m_data; }
operator T& () { return m_data; }
std::vector<Error>& warnings() { return m_warnings; }
LnkOutput::StreamPtr output() { return std::move(m_out); }
};
class BoundsChecker
{
protected:
size_t m_struct_start;
size_t m_struct_end; // this should be offset 1 byte beyond struct
public:
size_t
struct_start(size_t start)
{
m_struct_start = start;
return m_struct_start;
}
size_t
struct_start() const
{
return m_struct_start;
}
size_t
struct_end() const
{
return m_struct_end;
}
//! set struct_end(). struct_start needs to be initalized before.
void
struct_len(size_t len, const char* name)
{
if (add_overflows(m_struct_start, len)) {
throw Error::format("Field '%s' has bad length %llu: integer overflow", name, len);
}
m_struct_end = m_struct_start + len;
}
//! set struct_end(). struct_start needs to be initalized before.
bool
struct_len_nothrow(size_t len)
{
if (add_overflows(m_struct_start, len)) {
return false;
}
m_struct_end = m_struct_start + len;
return true;
}
//! move start point by a number of bytes
bool
struct_pop_nothrow(size_t len)
{
if (add_overflows(m_struct_start, len) || m_struct_start + len > m_struct_end) {
return false;
} else {
m_struct_start += len;
return true;
}
}
//! check if at least 1 byte can be read from given offset
void
check_offsets(size_t off1, size_t off2, const char *field_name) const
{
if (add_overflows(m_struct_start, off1) || add_overflows(m_struct_start + off1, off2)) {
throw Error::format("Field '%s' has bad offset %llu+%llu+%llu: integer overflow",
field_name, uint64_t(m_struct_start), uint64_t(off1),
uint64_t(off2));
}
if (m_struct_start + off1 + off2 >= m_struct_end) {
throw Error::format("Field '%s' offset beyond end of structure %llu+%llu+%llu>%llu",
field_name, uint64_t(m_struct_start), uint64_t(off1),
uint64_t(off2), uint64_t(m_struct_end));
}
}
//! check if at least 1 byte can be read from given offset
bool
check_offsets_nothrow(size_t off1, size_t off2) const
{
if (add_overflows(m_struct_start, off1) || add_overflows(m_struct_start + off1, off2)) {
return false;
}
if (m_struct_start + off1 + off2 >= m_struct_end) {
return false;
}
return true;
}
//! maximum length of string from given offset to end
size_t
maxlen(size_t off1 = 0, size_t off2 = 0) const
{
if (add_overflows(m_struct_start, off1) || add_overflows(m_struct_start + off1, off2) ||
m_struct_start + off1 + off2 > m_struct_end)
{
return 0;
}
else
{
return m_struct_end - off2 - off1 - m_struct_start;
}
}
//! check if number of bytes can be read from given offset
bool
check_read_nothrow(size_t off, size_t bytes) const
{
return maxlen(off) >= bytes;
}
BoundsChecker(): m_struct_start(0), m_struct_end(0) { }
};
// section 2.1
class Header: public Section<LnkStruct::ShellLinkHeader>
{
public:
Header(FileStream &in)
{
LnkStruct::ShellLinkHeader r;
in >> r.HeaderSize;
if (r.HeaderSize != 0x4C) {
throw Error::format("Wrong header size, should be 0x4C, got %#X", r.HeaderSize);
}
// magic number
LnkStruct::Guid guid;
static const char *magic = "00021401-0000-0000-C000-000000000046";
in >> guid;
if (guid != magic) {
throw Error::format("Wrong magic number, expected %s, got %s",
magic, guid.string().c_str());
}
in >> r.LinkFlags;
if (!r.LinkFlags.verify()) {
// Invalid link flags fatal, because these define further structure of the file
throw Error::format("Link flags are not valid: %#X, invalid bits are %#X",
r.LinkFlags.value(), r.LinkFlags.get_invalid_bits());
}
m_out->put("LinkFlags", r.LinkFlags);
in >> r.FileAttributes;
m_out->put("FileAttributes", r.FileAttributes);
in >> r.CreationTime;
m_out->put("CreationTime", r.CreationTime);
in >> r.AccessTime;
m_out->put("AccessTime", r.AccessTime);
in >> r.WriteTime;
m_out->put("WriteTime", r.WriteTime);
in >> r.FileSize;
m_out->put("FileSize", r.FileSize, LnkOutput::IntegerValue::FileSize);
in >> r.IconIndex;
m_out->put_debug("IconIndex", r.IconIndex);
in >> r.ShowCommand;
m_out->put_debug("ShowCommand", r.ShowCommand);
in >> r.HotKeyLow;
m_out->put_debug("HotKeyLow", r.HotKeyLow);
in >> r.HotKeyHigh;
m_out->put_debug("HotKeyHigh", r.HotKeyHigh);
in >> r.Reversed1;
in >> r.Reserved2;
in >> r.Reserved3;
m_data = r;
}
};
// section 2.2
class LinkTargetIdList: public Section<LnkStruct::LinkTargetIdList>, protected BoundsChecker
{
private:
FileStream &m_in;
bool
ext_BEEF0004(BoundsChecker& b, LnkOutput::StreamPtr& o, LnkStruct::ShellId_BeefBase z)
{
LnkStruct::ShellId_Beef0004 e;
if (!b.struct_pop_nothrow(sizeof(e.CreationTime)+sizeof(e.AccessTime)+
sizeof(e.WindowsVersion)))
{
return false;
}
m_in >> e.CreationTime;
m_in >> e.AccessTime;
m_in >> e.WindowsVersion;
o->put("CreationTime", e.CreationTime);
o->put("AccessTime", e.AccessTime);
o->put_debug("WindowsVersion", e.WindowsVersion);
if (z.Version >= 7) {
if (!b.struct_pop_nothrow(sizeof(e.Unknown1)+sizeof(e.FileReference)+
sizeof(e.Unknown2)))
{
return false;
}
m_in >> e.Unknown1;
m_in >> e.FileReference;
o->put_debug("MFTEntryIndex", get_bits<0, 47>(e.FileReference));
o->put_debug("Sequence", get_bits<48, 63>(e.FileReference));
m_in >> e.Unknown2;
}
if (z.Version >= 3) {
if (!b.struct_pop_nothrow(sizeof(e.LongStringSize))) {
return false;
}
m_in >> e.LongStringSize;
}
if (z.Version >= 9) {
if (!b.struct_pop_nothrow(sizeof(e.Unknown3))) {
return false;
}
m_in >> e.Unknown3;
}
if (z.Version >= 8) {
if (!b.struct_pop_nothrow(sizeof(e.Unknown4))) {
return false;
}
m_in >> e.Unknown4;
}
if (z.Version >= 3) {
std::u16string s = m_in.read_unicode(u16_nchars(b.maxlen()));
if (!b.struct_pop_nothrow(u16s0_nbytes(s))) {
return false;
}
e.LongName = utf16le_to_utf8(s);
o->put("LongName", e.LongName, true);
}
if (z.Version >= 3 && e.LongStringSize > 0) {
std::string s = m_in.read_ansi(b.maxlen());
if (!b.struct_pop_nothrow(s.size()+1)) {
return false;
}
o->put("LocalizedName", e.LocalizedName, false);
}
if (z.Version >= 7 && e.LongStringSize > 0) {
std::u16string s = m_in.read_unicode(b.maxlen());
if (!b.struct_pop_nothrow(u16s0_nbytes(s))) {
return false;
}
e.LocalizedName = utf16le_to_utf8(s);
o->put("LocalizedNameU", e.LocalizedName, true);
}
return true;
}
LnkOutput::StreamPtr
x1f_root_folder(BoundsChecker b)
{
auto o = LnkOutput::Stream::make();
LnkStruct::ShellId_x1F_SortIndex_t sort_idx;
LnkStruct::Guid folder;
if (!b.struct_pop_nothrow(sizeof(sort_idx) + sizeof(folder))) {
return o;
}
m_in >> sort_idx;
o->put_debug("SortIndex", sort_idx);
m_in >> folder;
const char* desc = LnkStruct::shell_folder_guid_describe(folder.string().c_str());
if (desc) {
o->put("ShellFolder", desc, true);
o->put_debug("ShellFolderGuid", folder);
} else {
o->put("ShellFolderGuid", folder);
}
return o;
}
LnkOutput::StreamPtr
x20_volume(const LnkStruct::LinkTargetIdList::ID& id)
{
// found no documentation on this
auto o = LnkOutput::Stream::make();
uint8_t flags = (id.Data.data()[0] & (~0x70));
o->put("Flags", flags, LnkOutput::IntegerValue::Hex);
return o;
}
LnkOutput::StreamPtr
x30_file(const LnkStruct::LinkTargetIdList::ID& id, BoundsChecker b)
{
auto o = LnkOutput::Stream::make();
LnkStruct::ShellId_x30_Struct f;
f.Flags = (uint8_t)(id.Data.data()[0] & (~0x70));
o->put_debug("Flags", f.Flags);
size_t saved_itemid_offset = b.struct_start() - 1; // for pre-xp / post-xp heuristic
if (!b.struct_pop_nothrow(sizeof(f.Unknown1)+sizeof(f.FileSize)+sizeof(f.ModifiedTime)+
sizeof(f.Attributes)))
{
return o;
}
m_in >> f.Unknown1;
m_in >> f.FileSize;
o->put("FileSize", f.FileSize, LnkOutput::IntegerValue::FileSize);
m_in >> f.ModifiedTime;
o->put("ModifiedTime", f.ModifiedTime);
m_in >> f.Attributes;
LnkStruct::FileAttributes_t a{f.Attributes}; // it's only 16 bits in this struct
o->put("Attributes", a);
if (b.maxlen() <= 0) {
return o;
}
if (f.is_unicode()) {
std::u16string u = m_in.read_unicode(u16_nchars(b.maxlen()));
if (!b.struct_pop_nothrow(u16s0_nbytes(u))) {
return o;
}
f.Name = utf16le_to_utf8(u);
o->put("Name", f.Name, true);
} else {
std::string a = m_in.read_ansi(b.maxlen());
if (!b.struct_pop_nothrow(a.size()+1)) {
return o;
}
f.Name = a;
o->put("Name", f.Name, false);
}
// check for alignment byte
if (b.maxlen() <= 0) {
return o;
}
//std::cout << "offset at end of name " << b.struct_start() << std::endl;// TODO
if (m_in.peek() == 0) {
m_in.ignore(sizeof(char));
b.struct_pop_nothrow(sizeof(char));
}
if (b.maxlen() < sizeof(uint16_t)) {
return o;
}
// try to detect pre-xp / post-xp
uint16_t maybe_size;
uint16_t maybe_offset;
m_in >> maybe_size;
size_t version_offset = m_in.tellg();
//std::cout << "version offset = " << version_offset << ", b.offset = " << b.struct_start() << std::endl; TODO
m_in.seekg(b.struct_end() - sizeof(maybe_offset));
m_in >> maybe_offset;
// std::cout << "maybe_size=" << maybe_size << " maybe_offset=" << maybe_offset << // TODO
// " struct_offset=" << version_offset - saved_itemid_offset << std::endl;
if (b.maxlen() >= maybe_size && // extension size includes itself
maybe_offset == version_offset - saved_itemid_offset) // should point back here
{
//std::cout << "post-xp" << std::endl;
// post-xp
LnkStruct::ShellId_BeefBase z;
if (!b.struct_pop_nothrow(sizeof(z.Size))) {
return o;
}
m_in.seekg(b.struct_start());
if (!b.struct_pop_nothrow(sizeof(z.Version)+sizeof(z.Signature))) {
return o;
}
z.Size = maybe_size;
m_in >> z.Version;
o->put_debug("Version", z.Version);
m_in >> z.Signature;
o->put_debug("Signature", z.Signature, LnkOutput::IntegerValue::Hex);
if (z.Signature == LnkStruct::ShellId_Beef0004::Signature) {
ext_BEEF0004(b, o, z);
}
}
else
{
// pre-xp
m_in.seekg(b.struct_start()); // after string / align byte
if (f.is_unicode()) {
std::u16string u = m_in.read_unicode(u16_nchars(b.maxlen()));
if (!b.struct_pop_nothrow(u16s0_nbytes(u))) {
return o;
}
f.SecondaryName = utf16le_to_utf8(u);
o->put("SecondaryName", f.SecondaryName, true);
} else {
std::string a = m_in.read_ansi(b.maxlen());
if (!b.struct_pop_nothrow(a.size()+1)) {
return o;
}
f.SecondaryName = a;
o->put("SecondaryName", f.SecondaryName, false);
}
}
return o;
}
LnkOutput::StreamPtr
x40_network(const LnkStruct::LinkTargetIdList::ID& id, BoundsChecker b)
{
auto o = LnkOutput::Stream::make();
LnkStruct::ShellId_x40_Struct f;
f.Type = id.Data.data()[0] & (~0x70);
o->put("Type", f.Type);
if (!b.struct_pop_nothrow(sizeof(f.Unknown1)+sizeof(f.Flags))) {
return o;
}
m_in >> f.Unknown1;
m_in >> f.Flags;
o->put_debug("Flags", f.Flags);
if (b.maxlen() <= 0) {
return o;
}
f.Location = m_in.read_ansi(b.maxlen());
if (!b.struct_pop_nothrow(f.Location.size()+1) || b.maxlen() <= 0) {
return o;
}
o->put("Location", f.Location, false);
if (f.has_description()) {
f.Description = m_in.read_ansi(b.maxlen());
if (!b.struct_pop_nothrow(f.Description.size()+1) || b.maxlen() <= 0) {
return o;
}
o->put("Description", f.Description, false);
}
if (f.has_comments()) {
f.Comments = m_in.read_ansi(b.maxlen());
o->put("Comments", f.Comments, false);
}
return o;
}
LnkOutput::StreamPtr
x50_zip_folder(BoundsChecker b)
{
auto o = LnkOutput::Stream::make();
LnkStruct::ShellId_x50_Struct f;
if (!b.struct_pop_nothrow(sizeof(f.Unknown1)+sizeof(f.Unknown2)+sizeof(f.Unknown3)+
sizeof(f.Unknown4)+sizeof(f.Unknown5)+sizeof(f.Unknown6)+
sizeof(f.Timestamp)+sizeof(f.Unknown7)+sizeof(f.Timestamp2)))
{
return o;
}
m_in >> f.Unknown1;
m_in >> f.Unknown2;
m_in >> f.Unknown3;
m_in >> f.Unknown4;
m_in >> f.Unknown5;
m_in >> f.Unknown6;
m_in >> f.Timestamp;
o->put("Timestamp", f.Timestamp);
m_in >> f.Unknown7;
m_in >> f.Timestamp2;
if (f.Timestamp2 != 0) {
o->put("Timestamp2", f.Timestamp2);
}
if (!b.struct_pop_nothrow(sizeof(f.FullPathSize))) {
return o;
}
m_in >> f.FullPathSize; // ignore
if (b.maxlen() <= 0) {
return o;
}
std::u16string tmp = m_in.read_unicode(u16_nchars(b.maxlen()));
if (!b.struct_pop_nothrow(u16s0_nbytes(tmp))) {
return o;
}
f.FullPath = utf16le_to_utf8(tmp);
o->put("FullPath", f.FullPath, true);
return o;
}
LnkOutput::StreamPtr
x60_uri(const LnkStruct::LinkTargetIdList::ID& id, BoundsChecker b)
{
auto o = LnkOutput::Stream::make();
LnkStruct::ShellId_x60_Struct f;
if (!b.struct_pop_nothrow(sizeof(f.Flags))) {
return o;
}
m_in >> f.Flags;
o->put_debug("Flags", f.Flags);
if ((id.Data.data()[0] & (~0x70)) == 0x01 && (f.Flags & (~0x80)) == 0x00) {
// seems to only contain 1 byte flags, 4 bytes zero and a string
if (!b.struct_pop_nothrow(sizeof(f.Unknown1))) {
return o;
}
m_in >> f.Unknown1;
if (f.is_unicode()) {
auto u = m_in.read_unicode(u16_nchars(b.maxlen()));
f.URI = utf16le_to_utf8(u);
if (f.URI.size() > 0) {
o->put("URI", f.URI, true);
}
} else {
f.URI = m_in.read_ansi(b.maxlen());
if (f.URI.size() > 0) {
o->put("URI", f.URI, false);
}
}
return o;
}
if (!b.struct_pop_nothrow(sizeof(f.DataSize))) {
return o;
}
m_in >> f.DataSize;
if (f.DataSize > 0) {
if (!b.struct_pop_nothrow(sizeof(f.Unknown1)+sizeof(f.Unknown2)+sizeof(f.Timestamp)+
sizeof(f.Unknown4)+sizeof(f.Unknown5)+sizeof(f.Unknown6)+
sizeof(f.Unknown7)+sizeof(f.Unknown8)+sizeof(f.String1Bytes)))
{
return o;
}
m_in >> f.Unknown1;
m_in >> f.Unknown2;
m_in >> f.Timestamp;
o->put("Timestamp", f.Timestamp);
m_in >> f.Unknown4;
m_in >> f.Unknown5;
m_in >> f.Unknown6;
m_in >> f.Unknown7;
m_in >> f.Unknown8;
m_in >> f.String1Bytes;
if (!b.struct_pop_nothrow(f.String1Bytes)) {
return o;
}
if (f.is_unicode()) {
std::u16string u = m_in.read_exact_unicode(f.String1Bytes);
f.FTPHostname = utf16le_to_utf8(u);
if (f.FTPHostname.size() > 0) {
o->put("FTPHostName", f.FTPHostname, true);
}
} else {
f.FTPHostname = m_in.read_exact(f.String1Bytes);
if (f.FTPHostname.size() > 0) {
o->put("FTPHostName", f.FTPHostname, false);
}
}
if (!b.struct_pop_nothrow(sizeof(f.String2Bytes))) {
return o;
}
m_in >> f.String2Bytes;
if (!b.struct_pop_nothrow(f.String2Bytes)) {
return o;
}
if (f.is_unicode()) {
std::u16string u = m_in.read_exact_unicode(f.String2Bytes);
f.FTPUser = utf16le_to_utf8(u);
if (f.FTPUser.size() > 0) {
o->put("FTPUser", f.FTPUser, true);
}
} else {
f.FTPUser = m_in.read_exact(f.String2Bytes);
if (f.FTPUser.size() > 0) {
o->put("FTPUser", f.FTPUser, false);
}
}
if (!b.struct_pop_nothrow(sizeof(f.String3Bytes))) {
return o;
}
m_in >> f.String3Bytes;
if (!b.struct_pop_nothrow(f.String3Bytes)) {
return o;
}
if (f.is_unicode()) {
std::u16string u = m_in.read_exact_unicode(f.String3Bytes);
f.FTPPassword = utf16le_to_utf8(u);
if (f.FTPUser.size() > 0) {
o->put("FTPPassword", f.FTPPassword, true);
}
} else {
f.FTPPassword = m_in.read_exact(f.String3Bytes);
if (f.FTPUser.size() > 0) {
o->put("FTPPassword", f.FTPPassword, false);
}
}
}
if (b.maxlen() <= 0) {
return o;
}
if (f.is_unicode()) {
std::u16string u = m_in.read_unicode(u16_nchars(b.maxlen()));
f.URI = utf16le_to_utf8(u);
if (f.URI.size() > 0) {
o->put("URI", f.URI, true);
}
} else {
f.URI = m_in.read_ansi(b.maxlen());
if (f.URI.size() > 0) {
o->put("URI", f.URI, false);
}
}
// there are more data following, including maybe block BEEF0014
return o;
}
LnkOutput::StreamPtr
x70_control_panel(BoundsChecker b)
{
auto o = LnkOutput::Stream::make();
LnkStruct::ShellId_x70_Struct f;
if (!b.struct_pop_nothrow(sizeof(f.SortOrder)+sizeof(f.Unknown1)+sizeof(f.Unknown2)+
sizeof(f.Unknown3)+sizeof(f.GUID)))
{
return o;
}
m_in >> f.SortOrder;
o->put_debug("SortOrder", f.SortOrder, LnkOutput::IntegerValue::Hex);
m_in >> f.Unknown1;
m_in >> f.Unknown2;
m_in >> f.Unknown3;
m_in >> f.GUID;
const char* desc = LnkStruct::control_panel_guid_describe(f.GUID.string().c_str());
if (desc != nullptr) {
o->put("Category", desc, true);
}
o->put("GUID", f.GUID);
return o;
}
LnkOutput::StreamPtr
x74_user_folder_delegate(BoundsChecker b)
{
auto o = LnkOutput::Stream::make();
LnkStruct::ShellId_x74_Struct f;
BoundsChecker outer = b;
if (!b.struct_pop_nothrow(sizeof(f.Unknown1)+sizeof(f.DelegateOffset)+
sizeof(f.SubShellItemSignature)+sizeof(f.SubShellItemSize)))
{
return o;
}
BoundsChecker inner = b;
m_in >> f.Unknown1;
m_in >> f.DelegateOffset;
// offset+3, to exclude size of Unknown1 and DelegateOffset itself
if (!outer.check_offsets_nothrow(3, f.DelegateOffset)) {
return o;
}
m_in >> f.SubShellItemSignature;
m_in >> f.SubShellItemSize;
if (f.SubShellItemSignature != f.Signature ||
!b.struct_pop_nothrow(f.SubShellItemSize))
{
return o;
}
{
// inner item
auto& s = f.SubShellItem;
inner.struct_len(f.SubShellItemSize, "SubShellItem");
if (inner.struct_end() > outer.struct_end() ||
inner.struct_end() > outer.struct_start() + f.DelegateOffset + 3 ||
!inner.struct_pop_nothrow(sizeof(s.ClsType)+sizeof(s.Unknown1)+
sizeof(s.ModifiedTime)+sizeof(s.FileAttributes)))
{
return o;
}
m_in >> s.ClsType;
if (s.ClsType != 0x31) {
return o;
}
m_in >> s.Unknown1;
m_in >> s.FileSize;
o->put("FileSize", s.FileSize, LnkOutput::IntegerValue::FileSize);
m_in >> s.ModifiedTime;
o->put("ModifiedTime", s.ModifiedTime);
m_in >> s.FileAttributes;
LnkStruct::FileAttributes_t a{s.FileAttributes};
o->put("FileAttributes", a);
s.PrimaryName = m_in.read_ansi(inner.maxlen());
o->put("PrimaryName", s.PrimaryName, false);
}
m_in.seekg(outer.struct_start() + 3 + f.DelegateOffset);
// delegate item
if (!b.struct_pop_nothrow(sizeof(f.DelegateGuid)+sizeof(f.DelegateClass))) {
return o;
}
m_in >> f.DelegateGuid;
o->put_debug("DelegateGuid", f.DelegateGuid);
m_in >> f.DelegateClass;
const char* desc = LnkStruct::shell_folder_guid_describe(f.DelegateClass.string().c_str());
if (desc != nullptr) {
o->put_debug("DelegateClass", desc, true);
}
o->put_debug("DelegateClassGuid", f.DelegateClass);
// extension block BEEF0004 follows
LnkStruct::ShellId_BeefBase z;
if (!b.struct_pop_nothrow(sizeof(z.Size)+sizeof(z.Version)+sizeof(z.Signature))) {
return o;
}
m_in >> z.Size;
m_in >> z.Version;
m_in >> z.Signature;
if (z.Signature == LnkStruct::ShellId_Beef0004::Signature) {
ext_BEEF0004(b, o, z);
}
return o;
}
void
unknown_shellid(const LnkStruct::LinkTargetIdList::ID& id)
{
auto o = LnkOutput::Stream::make();
o->put("Bytes", id.Data);
m_out->put_debug("UnknownShellId", std::move(o));
}
public:
LinkTargetIdList(FileStream &in):
m_in(in)
{
// the problem with this struct is that it is so poorly documented.
// check bounds on each read and return if it would go past the end of the struct.
// avoid throwing errors, just ignore them in this case.
m_in >> m_data.IdListSize;
struct_start(m_in.tellg()); // IdListSize does not include size of itself
struct_len(m_data.IdListSize, "LinkTargetIdList");
while (true) {
LnkStruct::LinkTargetIdList::ID id;
BoundsChecker item_bounds;
item_bounds.struct_start(struct_start());
if (!check_read_nothrow(0, sizeof(id.ItemIdSize))) {
m_in.seekg(struct_end());
return;
}
m_in >> id.ItemIdSize; // ItemIdSize does include size of itself
if (id.ItemIdSize == 0) {
// terminal item
break;
}
if (!check_read_nothrow(0, id.ItemIdSize) ||
!item_bounds.struct_len_nothrow(id.ItemIdSize) ||
!item_bounds.struct_pop_nothrow(sizeof(id.ItemIdSize)))
{
//std::cerr << "size " << id.ItemIdSize << " too big, seek to structend " << struct_end() << std::endl; // TODO
m_in.seekg(struct_end());
return;
}
if (!check_read_nothrow(0, id.ItemIdSize - sizeof(id.ItemIdSize))) {
m_in.seekg(struct_end());
return;
}
id.Data = in.read_binary(id.ItemIdSize - sizeof(id.ItemIdSize));
m_in.seekg(item_bounds.struct_start()); // after ItemIdSize
uint8_t clstype;
if (!item_bounds.struct_pop_nothrow(sizeof(clstype))) {
m_in.seekg(struct_end());
return;
}
m_in >> clstype;
if (clstype == 0x1F) {
auto o = x1f_root_folder(item_bounds);
m_out->put("FolderShellId", std::move(o));
} else if ((clstype & 0x70) == 0x20) {
auto o = x20_volume(id);
o->put_debug("Bytes", id.Data);
m_out->put("VolumeShellId", std::move(o));
} else if ((clstype & 0x70) == 0x30) {
auto o = x30_file(id, item_bounds);
o->put_debug("Bytes", id.Data);
m_out->put("FileShellId", std::move(o));