-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwiftSearch.cpp
More file actions
7751 lines (7491 loc) · 298 KB
/
Copy pathSwiftSearch.cpp
File metadata and controls
7751 lines (7491 loc) · 298 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
#include "targetver.h"
#include <fcntl.h>
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wignored-attributes"
#endif
#include <mmintrin.h>
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#include <io.h>
#include <process.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/stat.h>
#include <tchar.h>
#include <time.h>
#include <wchar.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#include <algorithm>
#if defined(_MSC_VER) && _MSC_VER >= 1800 // We don't check _CPPLIB_VER here, because we want to use the <atomic> that came with the compiler, even if we use a different STL
#define is_trivially_copyable_v sizeof is_trivially_copyable
#include <atomic>
#undef is_trivially_copyable_v
#endif
#if defined(_CPPLIB_VER) && _CPPLIB_VER >= 610
#include <mutex>
#endif
#ifndef assert
#include <cassert>
#endif
#include <map>
#include <fstream>
#include <iterator>
#include <string>
#include <utility>
#include <vector>
namespace WTL { using std::min; using std::max; }
#include <Windows.h>
#include <Dbt.h>
#include <muiload.h>
#include <ProvExce.h>
#include <ShlObj.h>
#ifndef ILIsEmpty
inline BOOL ILIsEmpty(LPCITEMIDLIST pidl) { return ((pidl == NULL) || (pidl->mkid.cb==0)); }
#endif
#include <WinNLS.h>
#include <atlbase.h>
#include <atlapp.h>
#include <atlcrack.h>
#include <atlmisc.h>
extern WTL::CAppModule _Module;
#include <atlwin.h>
#include <atldlgs.h>
#include <atlframe.h>
#include <atlctrls.h>
#include <atlctrlx.h>
#include <atltheme.h>
#include "nformat.hpp"
#include "path.hpp"
#include "BackgroundWorker.hpp"
#include "ShellItemIDList.hpp"
#include "CModifiedDialogImpl.hpp"
#include "NtUserCallHook.hpp"
#include "string_matcher.hpp"
#include "resource.h"
#ifdef __clang__
#ifndef _CPPLIB_VER
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wextra-qualification" // for _Fpz
namespace std { fpos_t const std::_Fpz = { 0 }; }
#pragma clang diagnostic pop
#endif
EXTERN_C const GUID DECLSPEC_SELECTANY IID_IShellFolder = { 0x000214E6L, 0, 0, { 0xC0, 0, 0, 0, 0, 0, 0, 0x46 } };
#endif
template<class T, class Traits = std::char_traits<T>, class Ax = std::allocator<T> >
class basic_vector_based_string : public
#ifdef _DEBUG
std::basic_string<T, Traits, Ax>
#else
std::vector<T, Ax>
#endif
{
typedef basic_vector_based_string this_type;
typedef std::basic_string<T, Traits, Ax> string_type;
typedef
#ifdef _DEBUG
string_type
#else
std::vector<T, Ax>
#endif
base_type;
public:
typedef typename base_type::size_type size_type;
typedef typename base_type::allocator_type allocator_type;
typedef typename base_type::value_type const *const_pointer;
typedef typename base_type::value_type value_type;
static size_type const npos = ~size_type();
basic_vector_based_string() : base_type() { }
explicit basic_vector_based_string(const_pointer const value, size_t const n = npos) : base_type(value, value + static_cast<ptrdiff_t>(n == npos ? Traits::length(value) : n)) { }
explicit basic_vector_based_string(size_type const n) : base_type(n) { }
explicit basic_vector_based_string(size_type const n, value_type const &value) : base_type(n, value) { }
explicit basic_vector_based_string(allocator_type const &ax) : base_type(ax) { }
explicit basic_vector_based_string(size_type const n, allocator_type const &ax) : base_type(n, ax) { }
explicit basic_vector_based_string(size_type const n, value_type const &value, allocator_type const &ax) : base_type(n, value, ax) { }
using base_type::insert;
using base_type::operator =;
#ifndef _DEBUG
typedef Traits traits_type;
typedef typename base_type::difference_type difference_type;
typedef typename base_type::iterator iterator;
typedef typename base_type::const_iterator const_iterator;
using base_type::erase;
void append(size_t const n, value_type const &value)
{
if (!n) { return; }
#if defined(_MSC_VER) && !defined(_CPPLIB_VER)
if (this->_End - this->_Last < static_cast<ptrdiff_t>(n))
{
this->reserve(this->size() + n);
for (size_t i = 0; i != n; ++i)
{
this->push_back(value);
}
}
else
{
std::uninitialized_fill(this->_Last, this->_Last + static_cast<ptrdiff_t>(n), value);
this->_Last += n;
}
#else
this->reserve(this->size() + n);
for (size_t i = 0; i != n; ++i)
{
this->push_back(value);
}
#endif
}
void append(const_pointer const begin, const_pointer const end)
{
#if defined(_MSC_VER) && !defined(_CPPLIB_VER)
ptrdiff_t const n = end - begin;
if (this->_End - this->_Last < n)
{
this->insert(this->end(), begin, end);
}
else
{
std::uninitialized_copy(begin, end, this->_Last);
this->_Last += n;
}
#else
this->insert(this->end(), begin, end);
#endif
}
void append(const_pointer const value, size_type n = npos) { return this->append(value, value + static_cast<ptrdiff_t>(n == npos ? Traits::length(value) : n)); }
size_type find(value_type const &value, size_type const offset = 0) const { const_iterator begin = this->begin() + static_cast<difference_type>(offset), end = this->end(); size_type result = static_cast<size_type>(std::find(begin, end, value) - begin); if (result >= static_cast<size_type>(end - begin)) { result = npos; } else { result += offset; } return result; }
const_pointer c_str() { const_pointer p; size_t const n = this->size(); if (n == 0 || this->capacity() <= n || *(&*this->begin() + static_cast<ptrdiff_t>(n)) != value_type()) { this->push_back(value_type()); p = &*this->begin(); this->pop_back(); } else { p = &*this->begin(); } return p; }
const_pointer data() const { return this->empty() ? NULL :&*this->begin(); }
iterator erase(size_t const pos, size_type const n = npos)
{ return this->erase(this->begin() + static_cast<difference_type>(pos), this->begin() + static_cast<difference_type>(pos) + (n == npos ? this->size() - pos : n)); }
iterator insert(size_t const pos, const_pointer const value, size_type const n = npos)
{ return this->insert(this->begin() + static_cast<difference_type>(pos), value, n); }
this_type operator +(base_type const &other) const { this_type result; result.reserve(this->size() + other.size()); result += *this; result += other; return result; }
#if defined(_MSC_VER) && !defined(_CPPLIB_VER)
void push_back(value_type const &value)
{
if (this->_Last != this->_End)
{
this->allocator.construct(this->_Last, value);
++this->_Last;
}
else
{
this->base_type::push_back(value);
}
}
#endif
friend this_type operator +(const_pointer const left, base_type const &right) { size_t const nleft = Traits::length(left); this_type result; result.reserve(nleft + right.size()); result.append(left, nleft); result += right; return result; }
this_type &operator+=(base_type const &value) { if (!value.empty()) { this->append(&*value.begin(), &*(value.end() - 1) + 1); } return *this; }
#else
using base_type::operator+=;
this_type &operator+=(this_type const &value) { if (!value.empty()) { this->append(&*value.begin(), &*(value.end() - 1) + 1); } return *this; }
#endif
template<class Ax2>
friend std::basic_string<T, Traits, Ax2> &operator+=(std::basic_string<T, Traits, Ax2> &out, this_type const &me) { out.append(me.begin(), me.end()); return out; }
iterator insert(iterator const i, const_pointer const value, size_type const n = npos)
{ size_type const pos = static_cast<size_type>(i - this->begin()); this->insert(i, value, value + static_cast<ptrdiff_t>(n == npos ? Traits::length(value) : n)); return this->begin() + static_cast<difference_type>(pos); }
this_type &operator =(const_pointer const value) { this->clear(); this->append(value); return *this; }
};
template<class It, class Less>
bool is_sorted_ex(It begin, It const end, Less less, bool const reversed = false)
{
if (begin != end)
{
It i(begin);
It const &left = reversed ? i : begin, &right = reversed ? begin : i;
++i;
while (i != end)
{
if (less(*right, *left))
{
return false;
}
begin = i;
++i;
}
}
return true;
}
template<class ValueType, class KeyComp>
struct stable_sort_by_key_comparator : KeyComp
{
explicit stable_sort_by_key_comparator(KeyComp const &comp = KeyComp()) : KeyComp(comp) { }
typedef ValueType value_type;
bool operator()(value_type const &a, value_type const &b) const
{
return this->KeyComp::operator()(a.first, b.first) || (!this->KeyComp::operator()(b.first, a.first) && (a.second < b.second));
}
};
template<class It, class Key, class Swapper>
void stable_sort_by_key(It begin, It end, Key key, Swapper swapper)
{
typedef typename std::iterator_traits<It>::difference_type Diff;
typedef std::less<typename Key::result_type> KeyComp;
typedef std::vector<std::pair<typename Key::result_type, Diff> > Keys;
Keys keys;
Diff const n = std::distance(begin, end);
keys.reserve(static_cast<typename Keys::size_type>(n));
{
Diff j = 0;
for (It i = begin; i != end; ++i)
{ keys.push_back(typename Keys::value_type(key(*i), j++)); }
}
std::stable_sort(keys.begin(), keys.end(), stable_sort_by_key_comparator<typename Keys::value_type, KeyComp>());
for (Diff i = 0; i != n; ++i)
{
for (Diff j = i; ; )
{
using std::swap;
swap(j, keys[j].second);
swapper(*(begin + j), *(begin + j));
if (j == i) { break; }
using std::iter_swap;
swapper(*(begin + j), *(begin + keys[j].second));
}
}
}
class DynamicAllocator
{
protected:
~DynamicAllocator() { }
public:
typedef void *pointer;
typedef void const *const_pointer;
virtual void deallocate(pointer const p, size_t const n) = 0;
virtual pointer allocate(size_t const n, pointer const hint = NULL) = 0;
virtual pointer reallocate(pointer const p, size_t const n, bool const allow_movement) = 0;
};
template<class T>
class dynamic_allocator : private std::allocator<T>
{
typedef dynamic_allocator this_type;
typedef std::allocator<T> base_type;
typedef DynamicAllocator dynamic_allocator_type;
dynamic_allocator_type *_alloc; /* Keep this IMMUTABLE, since otherwise we need to share state via heap allocation */
public:
typedef typename base_type::value_type value_type;
typedef typename base_type::const_pointer const_pointer;
typedef typename base_type::const_reference const_reference;
typedef typename base_type::difference_type difference_type;
typedef typename base_type::pointer pointer;
typedef typename base_type::reference reference;
typedef typename base_type::size_type size_type;
dynamic_allocator() : base_type(), _alloc() { }
explicit dynamic_allocator(dynamic_allocator_type *const alloc) : base_type(), _alloc(alloc) { }
template<class U>
dynamic_allocator(dynamic_allocator<U> const &other) : base_type(other.base()), _alloc(other.dynamic_alloc()) { }
template<class U>
struct rebind { typedef dynamic_allocator<U> other; };
dynamic_allocator_type *dynamic_alloc() const { return this->_alloc; }
base_type &base() { return static_cast<base_type &>(*this); }
base_type const &base() const { return static_cast<base_type const &>(*this); }
pointer allocate(size_type const n, void *const p = NULL) { pointer r; if (this->_alloc) { r = static_cast<pointer>(this->_alloc->allocate(n * sizeof(value_type), static_cast<typename dynamic_allocator_type::pointer>(p))); } else { r = this->base_type::allocate(n, p); } return r; }
void deallocate(pointer const p, size_type const n) { if (this->_alloc) { this->_alloc->deallocate(p, n * sizeof(value_type)); } else { this->base_type::deallocate(p, n); } }
pointer reallocate(pointer const p, size_t const n, bool const allow_movement) { if (this->_alloc) { return static_cast<pointer>(this->_alloc->reallocate(p, n * sizeof(value_type), allow_movement)); } else { return NULL; } }
bool operator==(this_type const &other) const { return static_cast<base_type const &>(*this) == static_cast<base_type const &>(other); }
bool operator!=(this_type const &other) const { return static_cast<base_type const &>(*this) != static_cast<base_type const &>(other); }
template<class U>
this_type &operator =(dynamic_allocator<U> const &other) { return static_cast<this_type &>(this->base_type::operator =(other.base())); }
using base_type::construct;
using base_type::destroy;
using base_type::address;
using base_type::max_size;
};
class SingleMovableGlobalAllocator : public DynamicAllocator
{
SingleMovableGlobalAllocator(SingleMovableGlobalAllocator const &);
SingleMovableGlobalAllocator &operator =(SingleMovableGlobalAllocator const &);
HGLOBAL _recycled;
public:
~SingleMovableGlobalAllocator() { if (this->_recycled) { GlobalFree(this->_recycled); this->_recycled = NULL; } }
SingleMovableGlobalAllocator() : _recycled() { }
bool disown(HGLOBAL const h) { bool const same = h == this->_recycled; if (same) { this->_recycled = NULL; } return same; }
void deallocate(pointer const p, size_t const n)
{
if (n)
{
if (HGLOBAL const h = GlobalHandle(p))
{
GlobalUnlock(h);
if (!this->_recycled)
{
this->_recycled = h;
}
else
{
GlobalFree(h);
}
}
}
}
pointer allocate(size_t const n, pointer const hint = NULL)
{
HGLOBAL mem = NULL;
if (n)
{
if (this->_recycled)
{
mem = GlobalReAlloc(this->_recycled, n, GMEM_MOVEABLE);
if (mem) { this->_recycled = NULL; }
}
if (!mem) { mem = GlobalAlloc(GMEM_MOVEABLE, n); }
}
pointer p = NULL;
if (mem)
{
p = GlobalLock(mem);
}
return p;
}
pointer reallocate(pointer const p, size_t const n, bool const allow_movement)
{
pointer result = NULL;
if (HGLOBAL h = GlobalHandle(p))
{
if (allow_movement || GlobalUnlock(h))
{
if (HGLOBAL const r = GlobalReAlloc(h, n, GMEM_MOVEABLE))
{
result = GlobalLock(r);
}
}
}
return result;
}
};
namespace std { typedef basic_string<TCHAR> tstring; typedef basic_vector_based_string<TCHAR, std::char_traits<TCHAR>, dynamic_allocator<TCHAR> > tvstring; }
namespace std
{
template<class> struct is_scalar;
#if defined(_CPPLIB_VER) && 600 <= _CPPLIB_VER
#ifdef _XMEMORY_
template<class T1, class T2> struct is_scalar<std::pair<T1, T2> > : integral_constant<bool, is_pod<T1>::value && is_pod<T2>::value>{};
template<class T1, class T2, class _Diff, class _Valty>
inline void _Uninit_def_fill_n(std::pair<T1, T2> *_First, _Diff _Count, _Wrap_alloc<allocator<std::pair<T1, T2> > >&, _Valty *,
#if defined(_CPPLIB_VER) && _CPPLIB_VER >= 650
_Trivially_copyable_ptr_iterator_tag
#else
_Scalar_ptr_iterator_tag
#endif
)
{ _Fill_n(_First, _Count, _Valty()); }
#endif
#else
template<class T> struct is_pod { static bool const value = __is_pod(T); };
#endif
template<class It, class T, class Traits, class Ax>
back_insert_iterator<basic_vector_based_string<T, Traits, Ax> > copy(It begin, It end, back_insert_iterator<basic_vector_based_string<T, Traits, Ax> > out)
{
typedef back_insert_iterator<basic_vector_based_string<T, Traits, Ax> > Base;
struct Derived : Base { using Base::container; };
typename Base::container_type &container = *static_cast<Derived &>(out).container;
container.append(begin, end);
return out;
}
}
namespace stdext
{
template<class T> struct remove_const { typedef T type; };
template<class T> struct remove_const<const T> { typedef T type; };
template<class T> struct remove_volatile { typedef T type; };
template<class T> struct remove_volatile<volatile T> { typedef T type; };
template<class T>
struct remove_cv { typedef typename remove_volatile<typename remove_const<T>::type>::type type; };
}
struct File
{
typedef int handle_type;
handle_type f;
~File() { if (f) { _close(f); } }
operator handle_type &() { return this->f; }
operator handle_type () const { return this->f; }
handle_type *operator &() { return &this->f; }
};
#define X(Class) (GetProcAddress(GetModuleHandle(TEXT("win32u.dll")), HOOK_TYPE(Class)::static_name()))
HOOK_DEFINE_DEFAULT(HANDLE __stdcall, NtUserGetProp, (HWND hWnd, ATOM PropId), X);
HOOK_DEFINE_DEFAULT(BOOL __stdcall, NtUserSetProp, (HWND hWnd, ATOM PropId, HANDLE value), X);
HOOK_DEFINE_DEFAULT(LRESULT __stdcall, NtUserMessageCall, (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, ULONG_PTR xParam, DWORD xpfnProc, BOOL bAnsi), X);
HOOK_DEFINE_DEFAULT(BOOL __stdcall, NtUserRedrawWindow, (HWND hWnd, CONST RECT *lprcUpdate, HRGN hrgnUpdate, UINT flags), X);
#undef X
namespace atomic_namespace
{
#ifdef _ATOMIC_
using namespace std;
#else
extern "C" { void _ReadWriteBarrier(void); }
#pragma intrinsic(_ReadWriteBarrier)
enum memory_order
{
memory_order_relaxed,
memory_order_consume,
memory_order_acquire,
memory_order_release,
memory_order_acq_rel,
memory_order_seq_cst
};
struct atomic_flag
{
long _value;
bool test_and_set(memory_order const order = memory_order_seq_cst) volatile { (void) order; return !!_interlockedbittestandset(&this->_value, 0); }
void clear(memory_order const order = memory_order_seq_cst) volatile { (void) order; _interlockedbittestandreset(&this->_value, 0); }
};
template<class T>
class atomic;
template<>
class atomic<bool>
{
typedef char storage_type;
typedef bool value_type;
storage_type value;
public:
atomic() { }
explicit atomic(value_type const value) : value(value) { }
operator value_type() const volatile { return this->load(); }
atomic volatile &operator =(value_type const &value) volatile { this->store(value); return *this; }
value_type exchange(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void) order; return !!_InterlockedExchange8(const_cast<storage_type *>(&this->value), static_cast<storage_type>(value)); }
value_type load(memory_order const order = memory_order_seq_cst) const volatile { (void) order; return !!_InterlockedOr8(const_cast<storage_type *>(&this->value), storage_type()); }
value_type store(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void) order; return !!_InterlockedExchange8(const_cast<storage_type *>(&this->value), static_cast<storage_type>(value)); }
};
template<>
class atomic<unsigned int>
{
typedef long storage_type;
typedef unsigned int value_type;
storage_type value;
public:
atomic() { }
explicit atomic(value_type const value) : value(value) { }
operator value_type() const volatile { return this->load(); }
atomic volatile &operator =(value_type const &value) volatile { this->store(value); return *this; }
value_type exchange(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void) order; return static_cast<value_type>(_InterlockedExchange(&this->value, static_cast<storage_type>(value))); }
value_type load(memory_order const order = memory_order_seq_cst) const volatile { (void) order; return static_cast<value_type>(_InterlockedOr(const_cast<storage_type volatile *>(&this->value), storage_type())); }
value_type store(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void) order; return static_cast<value_type>(_InterlockedExchange(&this->value, static_cast<storage_type>(value))); }
value_type fetch_add(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void) order; return static_cast<value_type>(_InterlockedExchangeAdd(&this->value, static_cast<storage_type>(value))); }
value_type fetch_sub(value_type const value, memory_order const order = memory_order_seq_cst) volatile { return this->fetch_add(value_type() - value, order); }
};
template<>
class atomic<long long>
{
typedef long long storage_type;
typedef long long value_type;
storage_type value;
#ifdef _M_IX86
static storage_type _InterlockedOr64(storage_type volatile *const result, storage_type const value) { storage_type prev, curr; _ReadWriteBarrier(); do { prev = *result; curr = prev | value; } while (prev != _InterlockedCompareExchange64(result, curr, prev)); _ReadWriteBarrier(); return prev; }
static storage_type _InterlockedExchange64(storage_type volatile *const result, storage_type const value) { storage_type prev; _ReadWriteBarrier(); do { prev = *result; } while (prev != _InterlockedCompareExchange64(result, value, prev)); _ReadWriteBarrier(); return (prev); }
static storage_type _InterlockedExchangeAdd64(storage_type volatile *const result, storage_type const value) { storage_type prev, curr; _ReadWriteBarrier(); do { prev = *result; curr = prev + value; } while (prev != _InterlockedCompareExchange64(result, curr, prev)); _ReadWriteBarrier(); return prev; }
#endif
public:
atomic() { }
explicit atomic(value_type const value) : value(value) { }
operator value_type() const volatile { return this->load(); }
atomic volatile &operator =(value_type const &value) volatile { this->store(value); return *this; }
value_type exchange(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void)order; return static_cast<value_type>(_InterlockedExchange64(&this->value, static_cast<storage_type>(value))); }
value_type load(memory_order const order = memory_order_seq_cst) const volatile { (void) order; return static_cast<value_type>(_InterlockedOr64(const_cast<storage_type volatile *>(&this->value), storage_type())); }
value_type store(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void) order; return static_cast<value_type>(_InterlockedExchange64(&this->value, static_cast<storage_type>(value))); }
value_type fetch_add(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void) order; return static_cast<value_type>(_InterlockedExchangeAdd64(&this->value, static_cast<storage_type>(value))); }
value_type fetch_sub(value_type const value, memory_order const order = memory_order_seq_cst) volatile { return this->fetch_add(value_type() - value, order); }
};
template<>
class atomic<unsigned long long>
{
typedef long long storage_type;
typedef unsigned long long value_type;
storage_type value;
#ifdef _M_IX86
static storage_type _InterlockedOr64(storage_type volatile *const result, storage_type const value) { storage_type prev, curr; _ReadWriteBarrier(); do { prev = *result; curr = prev | value; } while (prev != _InterlockedCompareExchange64(result, curr, prev)); _ReadWriteBarrier(); return prev; }
static storage_type _InterlockedExchange64(storage_type volatile *const result, storage_type const value) { storage_type prev; _ReadWriteBarrier(); do { prev = *result; } while (prev != _InterlockedCompareExchange64(result, value, prev)); _ReadWriteBarrier(); return (prev); }
static storage_type _InterlockedExchangeAdd64(storage_type volatile *const result, storage_type const value) { storage_type prev, curr; _ReadWriteBarrier(); do { prev = *result; curr = prev + value; } while (prev != _InterlockedCompareExchange64(result, curr, prev)); _ReadWriteBarrier(); return prev; }
#endif
public:
atomic() { }
explicit atomic(value_type const value) : value(value) { }
operator value_type() const volatile { return this->load(); }
atomic volatile &operator =(value_type const &value) volatile { this->store(value); return *this; }
value_type exchange(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void)order; return static_cast<value_type>(_InterlockedExchange64(&this->value, static_cast<storage_type>(value))); }
value_type load(memory_order const order = memory_order_seq_cst) const volatile { (void) order; return static_cast<value_type>(_InterlockedOr64(const_cast<storage_type volatile *>(&this->value), storage_type())); }
value_type store(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void) order; return static_cast<value_type>(_InterlockedExchange64(&this->value, static_cast<storage_type>(value))); }
value_type fetch_add(value_type const value, memory_order const order = memory_order_seq_cst) volatile { (void) order; return static_cast<value_type>(_InterlockedExchangeAdd64(&this->value, static_cast<storage_type>(value))); }
value_type fetch_sub(value_type const value, memory_order const order = memory_order_seq_cst) volatile { return this->fetch_add(value_type() - value, order); }
};
#endif
#ifndef _MUTEX_
template<class Mutex>
class unique_lock
{
Mutex *p;
public:
typedef Mutex mutex_type;
~unique_lock() { if (p) { p->unlock(); } }
unique_lock() : p() { }
explicit unique_lock(mutex_type *const mutex, bool const already_locked = false) : p(mutex) { if (p && !already_locked) { p->lock(); } }
explicit unique_lock(mutex_type &mutex, bool const already_locked = false) : p(&mutex) { if (!already_locked) { p->lock(); } }
unique_lock(unique_lock const &other) : p(other.p) { if (p) { p->lock(); } }
unique_lock &operator =(unique_lock other) { return this->swap(other), *this; }
void swap(unique_lock &other) { using std::swap; swap(this->p, other.p); }
friend void swap(unique_lock &a, unique_lock &b) { return a.swap(b); }
mutex_type *mutex() const { return this->p; }
};
class recursive_mutex
{
void init()
{
#if defined(_WIN32)
InitializeCriticalSection(&p);
#elif defined(_OPENMP) || defined(_OMP_LOCK_T)
omp_init_lock(&p);
#endif
}
void term()
{
#if defined(_WIN32)
DeleteCriticalSection(&p);
#elif defined(_OPENMP) || defined(_OMP_LOCK_T)
omp_destroy_lock(&p);
#endif
}
public:
recursive_mutex &operator =(recursive_mutex const &) { return *this; }
#if defined(_WIN32)
CRITICAL_SECTION p;
#elif defined(_OPENMP) || defined(_OMP_LOCK_T)
omp_lock_t p;
#elif defined(BOOST_THREAD_MUTEX_HPP)
boost::recursive_mutex m;
#else
std::recursive_mutex m;
#endif
recursive_mutex(recursive_mutex const &) { this->init(); }
recursive_mutex() { this->init(); }
~recursive_mutex() { this->term(); }
void lock()
{
#if defined(_WIN32)
EnterCriticalSection(&p);
#elif defined(_OPENMP) || defined(_OMP_LOCK_T)
omp_set_lock(&p);
#else
return m.lock();
#endif
}
void unlock()
{
#if defined(_WIN32)
LeaveCriticalSection(&p);
#elif defined(_OPENMP) || defined(_OMP_LOCK_T)
omp_unset_lock(&p);
#else
return m.unlock();
#endif
}
bool try_lock()
{
#if defined(_WIN32)
return !!TryEnterCriticalSection(&p);
#elif defined(_OPENMP) || defined(_OMP_LOCK_T)
return !!omp_test_lock(&p);
#else
return m.try_lock();
#endif
}
};
#endif
}
template<size_t N>
int safe_stprintf(TCHAR (&s)[N], TCHAR const *const format, ...)
{
int result;
va_list args;
va_start(args, format);
result = _vsntprintf(s, N - 1, format, args);
va_end(args);
if (result < 0) { s[0] = _T('\0'); }
else if (result == N - 1) { s[result] = _T('\0'); }
return result;
}
ATL::CWindow topmostWindow;
atomic_namespace::recursive_mutex global_exception_mutex;
long global_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
{
long result;
if (ExceptionInfo->ExceptionRecord->ExceptionCode == 0x40010006 /*DBG_PRINTEXCEPTION_C*/ ||
ExceptionInfo->ExceptionRecord->ExceptionCode == 0x4001000A /*DBG_PRINTEXCEPTION_WIDE_C*/ ||
ExceptionInfo->ExceptionRecord->ExceptionCode == 0xE06D7363 /*C++ exception*/ ||
ExceptionInfo->ExceptionRecord->ExceptionCode == RPC_S_SERVER_UNAVAILABLE)
{
result = EXCEPTION_CONTINUE_SEARCH;
}
else
{
atomic_namespace::unique_lock<atomic_namespace::recursive_mutex> const guard(global_exception_mutex);
TCHAR buf[512];
safe_stprintf(buf, _T("Apologies, but this program has encountered error 0x%lX.\r\n\r\nPLEASE tell me about this so I can try to fix it for you!\r\n\r\nIf you see OK, press OK.\r\nOtherwise:\r\n- Press Retry to attempt to handle the error (recommended)\r\n- Press Abort to quit\r\n- Press Ignore to continue (NOT recommended)"), ExceptionInfo->ExceptionRecord->ExceptionCode);
int const r = MessageBox(topmostWindow.m_hWnd, buf, _T("Fatal Error"), MB_ICONERROR | ((ExceptionInfo->ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE) ? MB_OK : MB_ABORTRETRYIGNORE) | MB_TASKMODAL);
if (r == IDABORT) { _exit(ExceptionInfo->ExceptionRecord->ExceptionCode); }
result = r == IDIGNORE ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
}
return result;
}
bool is_ascii(wchar_t const *const s, size_t const n)
{
bool result = true;
for (size_t i = 0; i != n; ++i)
{
wchar_t const ch = s[i];
result &= (SCHAR_MIN <= static_cast<long long>(ch)) & (ch <= SCHAR_MAX);
}
return result;
}
static void append_directional(std::tvstring &str, TCHAR const sz[], size_t const cch, int const ascii_mode /* -1 = decompress ASCII, 0 = no change, +1 = compress ASCII */, bool const reverse = false)
{
typedef std::tvstring Str;
size_t const cch_in_str = ascii_mode > 0 ? (cch + 1) / 2 : cch;
size_t const n = str.size();
#if defined(_MSC_VER) && !defined(_CPPLIB_VER)
struct Derived : Str { using Str::_First; using Str::_Last; using Str::_End; using Str::allocator; };
Derived &derived = static_cast<Derived &>(str);
#endif
if (n + cch_in_str > str.capacity())
{
#if defined(_MSC_VER) && !defined(_CPPLIB_VER)
dynamic_allocator<TCHAR> &alloc /* ensure correct type of allocator! */ = derived.allocator;
size_t const min_capacity = str.capacity() + str.capacity() / 2;
size_t new_capacity = n + cch_in_str;
if (new_capacity < min_capacity) { new_capacity = min_capacity; }
if (TCHAR *const p = alloc.reallocate(derived._First, new_capacity, true /* TCHAR is movable */))
{
derived._First = p;
derived._Last = derived._First + static_cast<ptrdiff_t>(n);
derived._End = derived._First + static_cast<ptrdiff_t>(new_capacity);
}
#endif
str.reserve(n + n / 2 + cch_in_str * 2);
}
#if defined(_MSC_VER) && !defined(_CPPLIB_VER)
// we have enough capacity, so just extend
derived._Last += static_cast<ptrdiff_t>(cch_in_str);
#else
str.resize(n + cch_in_str);
#endif
if (cch)
{
TCHAR *const o = &str[n];
if (reverse)
{
if (ascii_mode < 0)
{ std::reverse_copy(static_cast<char const *>(static_cast<void const *>(sz)), static_cast<char const *>(static_cast<void const *>(sz)) + static_cast<ptrdiff_t>(cch), o); }
else if (ascii_mode > 0)
{
for (size_t i = 0; i != cch; ++i)
{ static_cast<char *>(static_cast<void *>(o))[i] = static_cast<char>(sz[cch - 1 - i]); }
}
else
{ std::reverse_copy(sz, sz + static_cast<ptrdiff_t>(cch), o); }
}
else
{
if (ascii_mode < 0)
{ std::copy(static_cast<char const *>(static_cast<void const *>(sz)), static_cast<char const *>(static_cast<void const *>(sz)) + static_cast<ptrdiff_t>(cch), o); }
else if (ascii_mode > 0)
{
for (size_t i = 0; i != cch; ++i)
{ static_cast<char *>(static_cast<void *>(o))[i] = static_cast<char>(sz[i]); }
}
else
{ std::copy(sz, sz + static_cast<ptrdiff_t>(cch), o); }
}
}
}
void CppRaiseException(unsigned long const error)
{
struct _EXCEPTION_POINTERS *pExPtrs = NULL;
bool thrown = false;
int exCode = 0;
struct CppExceptionThrower { void operator()(int exCode, struct _EXCEPTION_POINTERS *pExPtrs) { throw CStructured_Exception(exCode, pExPtrs); } static bool assign(struct _EXCEPTION_POINTERS **to, struct _EXCEPTION_POINTERS *from) { *to = from; return true; } };
__try
{ ATL::_AtlRaiseException(error, 0); }
__except (CppExceptionThrower::assign(&pExPtrs, static_cast<struct _EXCEPTION_POINTERS *>(GetExceptionInformation())) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{ exCode = GetExceptionCode(); thrown = true; }
if (thrown) { CppExceptionThrower()(exCode, pExPtrs); }
}
void CheckAndThrow(int const success) { if (!success) { CppRaiseException(GetLastError()); } }
LPTSTR GetAnyErrorText(DWORD errorCode, va_list* pArgList = NULL)
{
static TCHAR buffer[1 << 15];
ZeroMemory(buffer, sizeof(buffer));
if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | (pArgList == NULL ? FORMAT_MESSAGE_IGNORE_INSERTS : 0), NULL, errorCode, 0, buffer, sizeof(buffer) / sizeof(*buffer), pArgList))
{
if (!FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | (pArgList == NULL ? FORMAT_MESSAGE_IGNORE_INSERTS : 0), GetModuleHandle(_T("NTDLL.dll")), errorCode, 0, buffer, sizeof(buffer) / sizeof(*buffer), pArgList))
{ safe_stprintf(buffer, _T("%#lx"), errorCode); }
}
return buffer;
}
struct Wow64
{
static HMODULE GetKernel32()
{
HMODULE kernel32 = NULL;
return GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast<LPCTSTR>(&GetSystemInfo), &kernel32) ? kernel32 : NULL;
}
typedef BOOL WINAPI IsWow64Process_t(IN HANDLE hProcess, OUT PBOOL Wow64Process); static IsWow64Process_t *IsWow64Process;
static bool is_wow64()
{
bool result = false;
#ifdef _M_IX86
BOOL isWOW64 = FALSE;
if (!IsWow64Process)
{ IsWow64Process = reinterpret_cast<IsWow64Process_t *>(GetProcAddress(GetKernel32(), _CRT_STRINGIZE(IsWow64Process))); }
result = IsWow64Process && IsWow64Process(GetCurrentProcess(), &isWOW64) && isWOW64;
#endif
return result;
}
typedef BOOL WINAPI Wow64DisableWow64FsRedirection_t(PVOID *OldValue); static Wow64DisableWow64FsRedirection_t *Wow64DisableWow64FsRedirection;
static void *disable()
{
void *old = NULL;
#ifdef _M_IX86
if (!Wow64DisableWow64FsRedirection)
{ Wow64DisableWow64FsRedirection = reinterpret_cast<Wow64DisableWow64FsRedirection_t *>(GetProcAddress(GetKernel32(), _CRT_STRINGIZE(Wow64DisableWow64FsRedirection))); }
if (Wow64DisableWow64FsRedirection && !Wow64DisableWow64FsRedirection(&old))
{ old = NULL; }
#endif
return old;
}
typedef BOOL WINAPI Wow64RevertWow64FsRedirection_t(PVOID OlValue); static Wow64RevertWow64FsRedirection_t *Wow64RevertWow64FsRedirection;
static bool revert(PVOID old)
{
bool result = false;
#ifdef _M_IX86
if (!Wow64RevertWow64FsRedirection)
{ Wow64RevertWow64FsRedirection = reinterpret_cast<Wow64RevertWow64FsRedirection_t *>(GetProcAddress(GetKernel32(), _CRT_STRINGIZE(Wow64RevertWow64FsRedirection))); }
result = Wow64RevertWow64FsRedirection && Wow64RevertWow64FsRedirection(old);
#endif
return result;
}
Wow64() { }
};
#ifdef _M_IX86
Wow64 const init_wow64;
Wow64::IsWow64Process_t *Wow64::IsWow64Process = NULL;
Wow64::Wow64DisableWow64FsRedirection_t *Wow64::Wow64DisableWow64FsRedirection = NULL;
Wow64::Wow64RevertWow64FsRedirection_t *Wow64::Wow64RevertWow64FsRedirection = NULL;
#endif
struct Wow64Disable
{
bool disable;
void *cookie;
Wow64Disable(bool disable) : disable(disable) { if (this->disable) { this->cookie = Wow64::disable(); } }
~Wow64Disable() { if (this->disable) { Wow64::revert(this->cookie); } }
};
namespace winnt
{
struct IO_STATUS_BLOCK { union { long Status; void *Pointer; }; uintptr_t Information; };
enum IO_PRIORITY_HINT { IoPriorityVeryLow = 0, IoPriorityLow, IoPriorityNormal, IoPriorityHigh, IoPriorityCritical, MaxIoPriorityTypes };
struct FILE_FS_SIZE_INFORMATION { long long TotalAllocationUnits, ActualAvailableAllocationUnits; unsigned long SectorsPerAllocationUnit, BytesPerSector; };
struct FILE_FS_ATTRIBUTE_INFORMATION { unsigned long FileSystemAttributes; unsigned long MaximumComponentNameLength; unsigned long FileSystemNameLength; wchar_t FileSystemName[1]; };
struct FILE_FS_DEVICE_INFORMATION { unsigned long DeviceType, Characteristics; } fsinfo;
union FILE_IO_PRIORITY_HINT_INFORMATION { IO_PRIORITY_HINT PriorityHint; unsigned long long _alignment; };
struct TIME_FIELDS { short Year; short Month; short Day; short Hour; short Minute; short Second; short Milliseconds; short Weekday; };
template<class T> struct identity { typedef T type; };
typedef long NTSTATUS;
#define X(F, T) identity<T>::type &F = *reinterpret_cast<identity<T>::type *const &>(static_cast<FARPROC const &>(GetProcAddress(GetModuleHandle(_T("ntdll.dll")), #F)))
X(NtQueryVolumeInformationFile, NTSTATUS NTAPI(HANDLE FileHandle, IO_STATUS_BLOCK *IoStatusBlock, PVOID FsInformation, unsigned long Length, unsigned long FsInformationClass));
X(NtQueryInformationFile, NTSTATUS NTAPI(IN HANDLE FileHandle, OUT IO_STATUS_BLOCK *IoStatusBlock, OUT PVOID FileInformation, IN ULONG Length, IN unsigned long FileInformationClass));
X(NtSetInformationFile, NTSTATUS NTAPI(IN HANDLE FileHandle, OUT IO_STATUS_BLOCK *IoStatusBlock, IN PVOID FileInformation, IN ULONG Length, IN unsigned long FileInformationClass));
X(RtlNtStatusToDosError, unsigned long NTAPI(IN NTSTATUS NtStatus));
X(RtlTimeToTimeFields, VOID NTAPI(LARGE_INTEGER *Time, TIME_FIELDS *TimeFields));
#undef X
}
bool isdevnull(int fd)
{
winnt::IO_STATUS_BLOCK iosb;
winnt::FILE_FS_DEVICE_INFORMATION fsinfo;
return winnt::NtQueryVolumeInformationFile((HANDLE)_get_osfhandle(fd), &iosb, &fsinfo, sizeof(fsinfo), 4) == 0 && fsinfo.DeviceType == 0x00000015;
}
bool isdevnull(FILE *f)
{
return isdevnull(
#ifdef _O_BINARY
_fileno(f)
#else
fileno(f)
#endif
);
}
namespace ntfs
{
#pragma pack(push, 1)
struct NTFS_BOOT_SECTOR
{
unsigned char Jump[3];
unsigned char Oem[8];
unsigned short BytesPerSector;
unsigned char SectorsPerCluster;
unsigned short ReservedSectors;
unsigned char Padding1[3];
unsigned short Unused1;
unsigned char MediaDescriptor;
unsigned short Padding2;
unsigned short SectorsPerTrack;
unsigned short NumberOfHeads;
unsigned long HiddenSectors;
unsigned long Unused2;
unsigned long Unused3;
long long TotalSectors;
long long MftStartLcn;
long long Mft2StartLcn;
signed char ClustersPerFileRecordSegment;
unsigned char Padding3[3];
unsigned long ClustersPerIndexBlock;
long long VolumeSerialNumber;
unsigned long Checksum;
unsigned char BootStrap[0x200 - 0x54];
unsigned int file_record_size() const { return this->ClustersPerFileRecordSegment >= 0 ? this->ClustersPerFileRecordSegment * this->SectorsPerCluster * this->BytesPerSector : 1U << static_cast<int>(-this->ClustersPerFileRecordSegment); }
unsigned int cluster_size() const { return this->SectorsPerCluster * this->BytesPerSector; }
};
#pragma pack(pop)
struct MULTI_SECTOR_HEADER
{
unsigned long Magic;
unsigned short USAOffset;
unsigned short USACount;
bool unfixup(size_t max_size)
{
unsigned short *usa = reinterpret_cast<unsigned short *>(&reinterpret_cast<unsigned char *>(this)[this->USAOffset]);
unsigned short const usa0 = usa[0];
bool result = true;
for (unsigned short i = 1; i < this->USACount; i++)
{
const size_t offset = i * 512 - sizeof(unsigned short);
unsigned short *const check = (unsigned short *) ((unsigned char*)this + offset);
if (offset < max_size)
{
result &= *check == usa0;
*check = usa[i];
}
else { break; }
}
return result;
}
};
enum AttributeTypeCode
{
AttributeStandardInformation = 0x10,
AttributeAttributeList = 0x20,
AttributeFileName = 0x30,
AttributeObjectId = 0x40,
AttributeSecurityDescriptor = 0x50,
AttributeVolumeName = 0x60,
AttributeVolumeInformation = 0x70,
AttributeData = 0x80,
AttributeIndexRoot = 0x90,
AttributeIndexAllocation = 0xA0,
AttributeBitmap = 0xB0,
AttributeReparsePoint = 0xC0,
AttributeEAInformation = 0xD0,
AttributeEA = 0xE0,
AttributePropertySet = 0xF0,
AttributeLoggedUtilityStream = 0x100,
AttributeEnd = -1,
};
struct ATTRIBUTE_RECORD_HEADER
{
AttributeTypeCode Type;
unsigned long Length;
unsigned char IsNonResident;
unsigned char NameLength;
unsigned short NameOffset;
unsigned short Flags; // 0x0001 = Compressed, 0x4000 = Encrypted, 0x8000 = Sparse
unsigned short Instance;
union
{
struct RESIDENT
{
unsigned long ValueLength;
unsigned short ValueOffset;
unsigned short Flags;
inline void *GetValue() { return reinterpret_cast<void *>(reinterpret_cast<char *>(CONTAINING_RECORD(this, ATTRIBUTE_RECORD_HEADER, Resident)) + this->ValueOffset); }
inline void const *GetValue() const { return reinterpret_cast<const void *>(reinterpret_cast<const char *>(CONTAINING_RECORD(this, ATTRIBUTE_RECORD_HEADER, Resident)) + this->ValueOffset); }
} Resident;
struct NONRESIDENT
{
long long LowestVCN;
long long HighestVCN;
unsigned short MappingPairsOffset;
unsigned char CompressionUnit;
unsigned char Reserved[5];
long long AllocatedSize;
long long DataSize;
long long InitializedSize;
long long CompressedSize;
} NonResident;
};
ATTRIBUTE_RECORD_HEADER *next() { return reinterpret_cast<ATTRIBUTE_RECORD_HEADER *>(reinterpret_cast<unsigned char *>(this) + this->Length); }
ATTRIBUTE_RECORD_HEADER const *next() const { return reinterpret_cast<ATTRIBUTE_RECORD_HEADER const *>(reinterpret_cast<unsigned char const *>(this) + this->Length); }
wchar_t *name() { return reinterpret_cast<wchar_t *>(reinterpret_cast<unsigned char *>(this) + this->NameOffset); }
wchar_t const *name() const { return reinterpret_cast<wchar_t const *>(reinterpret_cast<unsigned char const *>(this) + this->NameOffset); }
};
enum FILE_RECORD_HEADER_FLAGS
{
FRH_IN_USE = 0x0001, /* Record is in use */
FRH_DIRECTORY = 0x0002, /* Record is a directory */
};
struct FILE_RECORD_SEGMENT_HEADER
{
MULTI_SECTOR_HEADER MultiSectorHeader;
unsigned long long LogFileSequenceNumber;
unsigned short SequenceNumber;
unsigned short LinkCount;
unsigned short FirstAttributeOffset;
unsigned short Flags /* FILE_RECORD_HEADER_FLAGS */;
unsigned long BytesInUse;
unsigned long BytesAllocated;
unsigned long long BaseFileRecordSegment;
unsigned short NextAttributeNumber;
//http://blogs.technet.com/b/joscon/archive/2011/01/06/how-hard-links-work.aspx
unsigned short SegmentNumberUpper_or_USA_or_UnknownReserved; // WARNING: This does NOT seem to be the actual "upper" segment number of anything! I found it to be 0x26e on one of my drives... and checkdisk didn't say anything about it
unsigned long SegmentNumberLower;
ATTRIBUTE_RECORD_HEADER *begin() { return reinterpret_cast<ATTRIBUTE_RECORD_HEADER *>(reinterpret_cast<unsigned char *>(this) + this->FirstAttributeOffset); }
ATTRIBUTE_RECORD_HEADER const *begin() const { return reinterpret_cast<ATTRIBUTE_RECORD_HEADER const *>(reinterpret_cast<unsigned char const *>(this) + this->FirstAttributeOffset); }
void *end(size_t const max_buffer_size = ~size_t()) { return reinterpret_cast<unsigned char *>(this) + (max_buffer_size < this->BytesInUse ? max_buffer_size : this->BytesInUse); }
void const *end(size_t const max_buffer_size = ~size_t()) const { return reinterpret_cast<unsigned char const *>(this) + (max_buffer_size < this->BytesInUse ? max_buffer_size : this->BytesInUse); }
};
struct FILENAME_INFORMATION