-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
libplzma.hpp
1596 lines (1188 loc) · 72 KB
/
libplzma.hpp
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
//
// By using this Software, you are accepting original [LZMA SDK] and MIT license below:
//
// The MIT License (MIT)
//
// Copyright (c) 2015 - 2024 Oleh Kulykov <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#ifndef __LIBPLZMA_HPP__
#define __LIBPLZMA_HPP__ 1
#include <cstddef>
#include <cstdarg>
#include <cstdlib>
#include "libplzma.h"
/// @file libplzma.hpp
/// @brief Single public header of the Core C++ library part.
///
/// This header file uses types, preprocessor definitions and common C functions presented in a single public header of the C library(libplzma.h).
/// Everything what you need to use this library in C++ | Objective-C++ | Node.js env. is here.
namespace plzma {
/// @brief The presentation of the allocated raw heap memory.
///
/// Uses \a plzma_malloc, \a plzma_realloc and \a plzma_free functions for handling the raw heap memory.
/// Following the \a Resource \a Acquisition \a Is \a Initialization or \a RAII technique.
/// The allocated memory will be automatically deallocated via \a plzma_free function.
struct LIBPLZMA_CPP_CLASS_API RawHeapMemory final {
protected:
#if defined(DEBUG)
union {
void * LIBPLZMA_NULLABLE _memory = nullptr;
char * LIBPLZMA_NULLABLE _cstring;
wchar_t * LIBPLZMA_NULLABLE _wstring;
};
#else
void * LIBPLZMA_NULLABLE _memory = nullptr;
#endif
RawHeapMemory & operator = (const RawHeapMemory &) = delete;
RawHeapMemory(const RawHeapMemory &) = delete;
public:
/// @brief Moves allocated memory from \a memory instance.
///
/// The previously allocated memory will be released via \a plzma_free function.
RawHeapMemory & operator = (RawHeapMemory && memory) noexcept;
/// @brief Checks the memory is allocated.
/// @return \a true if memory allocated and exists, otherwise \a false.
inline operator bool () const noexcept { return _memory != nullptr; }
/// @return The memory pointer casted to a specific type via \a static_cast function.
/// @tparam T The type of memory.
template<typename T>
inline operator T () noexcept { return static_cast<T>(_memory); }
/// @return The memory pointer casted to a specific type via \a static_cast function.
/// @tparam T The type of memory.
template<typename T>
inline operator T () const noexcept { return static_cast<T>(_memory); }
/// @brief Returns raw pointer to the memory and nullifies the internal storage.
void * LIBPLZMA_NULLABLE take() noexcept;
/// @brief Changes the size of the raw heap memory.
///
/// Uses \a plzma_realloc to change the size of existed memory or \a plzma_malloc to allocate new one.
/// @param size The new size in bytes of memory. In case if \a size is zero, the allocated memory will be released via \a plzma_free function.
/// @exception The \a Exception with \a plzma_error_code_not_enough_memory code in case if required amount of memory can't be allocated.
void resize(const size_t size);
/// @brief Erases the allocated memory.
/// @param eraseType The type of the erasing of the memory. See \a plzma_erase enum.
/// @param size The number bytes to erase.
void erase(const plzma_erase eraseType, const size_t size) noexcept;
/// @brief Erases and releases the allocated memory.
/// @param eraseType The type of the erasing of the memory. See \a plzma_erase enum.
/// @param size The number bytes to erase.
void clear(const plzma_erase eraseType = plzma_erase_none, const size_t size = 0) noexcept;
/// @brief Contructs the \a RawHeapMemory instance and allocates the memory if needed.
///
/// Uses \a plzma_malloc function to allocate the memory.
/// @param size The size in bytes of memory to allocate.
/// @param eraseType The type of the erasing of newly allocated memory. See \a plzma_erase enum.
/// @exception The \a Exception with \a plzma_error_code_not_enough_memory code in case if required amount of memory can't be allocated.
RawHeapMemory(const size_t size, const plzma_erase eraseType = plzma_erase_none);
/// @brief Contructs the \a RawHeapMemory instance and moves previously allocated memory
/// from another \a RawHeapMemory instance.
RawHeapMemory(RawHeapMemory && memory) noexcept;
/// @brief Contructs empty \a RawHeapMemory instance without allocating memory.
RawHeapMemory() noexcept = default;
/// @brief Destroys the \a RawHeapMemory instance and releases the allocating memory via \a plzma_free function.
~RawHeapMemory() noexcept;
};
template<bool B, typename T = void>
struct enableIf { };
template <typename T>
struct enableIf<true, T> { typedef T type; };
template <bool B, class T = void>
using enableIfT = typename enableIf<B, T>::type;
/// @brief The struct template that provides a way to store 2 instances/objects as a single unit.
/// Same as \a std::pair template.
/// @tparam FIRST The type of the \a first instance/object.
/// @tparam SECOND The type of the \a second instance/object.
template<typename FIRST, typename SECOND, bool FIRST_COPYABLE = true>
struct Pair final {
FIRST first;
SECOND second;
Pair<FIRST, SECOND, FIRST_COPYABLE> & operator = (const Pair<FIRST, SECOND, FIRST_COPYABLE> &) = default;
Pair<FIRST, SECOND, FIRST_COPYABLE> & operator = (Pair<FIRST, SECOND, FIRST_COPYABLE> && pair) noexcept {
first = static_cast<FIRST &&>(pair.first);
second = static_cast<SECOND &&>(pair.second);
return *this;
}
template <typename T = void, typename = enableIfT<FIRST_COPYABLE, T> >
Pair(const FIRST & f, const SECOND & s) : first(f), second(s) { }
Pair(FIRST && f, SECOND && s) : first(static_cast<FIRST &&>(f)), second(static_cast<SECOND &&>(s)) { }
Pair(Pair<FIRST, SECOND, FIRST_COPYABLE> && pair) noexcept : first(static_cast<FIRST &&>(pair.first)), second(static_cast<SECOND &&>(pair.second)) { }
Pair(const Pair<FIRST, SECOND, FIRST_COPYABLE> &) = default;
Pair() noexcept = default;
};
/// @brief The struct template that provides a way to store 3 instances/objects as a single unit.
/// @tparam FIRST The type of the \a first instance/object.
/// @tparam SECOND The type of the \a second instance/object.
/// @tparam THIRD The type of the \a third instance/object.
template<typename FIRST, typename SECOND, typename THIRD>
struct Trio final {
FIRST first;
SECOND second;
THIRD third;
Trio<FIRST, SECOND, THIRD> & operator = (const Trio<FIRST, SECOND, THIRD> &) = default;
Trio<FIRST, SECOND, THIRD> & operator = (Trio<FIRST, SECOND, THIRD> && trio) noexcept {
first = static_cast<FIRST &&>(trio.first);
second = static_cast<SECOND &&>(trio.second);
third = static_cast<THIRD &&>(trio.third);
return *this;
}
Trio(const FIRST & f, const SECOND & s, const THIRD & t) : first(f), second(s), third(t) { }
Trio(FIRST && f, SECOND && s, THIRD && t) noexcept : first(static_cast<FIRST &&>(f)), second(static_cast<SECOND &&>(s)), third(static_cast<THIRD &&>(t)) { }
Trio(Trio<FIRST, SECOND, THIRD> && trio) noexcept : first(static_cast<FIRST &&>(trio.first)), second(static_cast<SECOND &&>(trio.second)), third(static_cast<THIRD &&>(trio.third)) { }
Trio(const Trio<FIRST, SECOND, THIRD> &) = default;
Trio() noexcept = default;
};
/// @brief Shared pointer for \a Automatic \a Reference \a Counting \a (ARC) classes.
/// Similar to the \a std::shared_ptr template, but not compatible due to underlying code-base.
/// @tparam T The Class type which supports ARC, i.e. class which contains \a retain and \a release methods.
template<class T>
struct SharedPtr final {
private:
T * LIBPLZMA_NULLABLE _ptr = nullptr;
public:
/// @brief Simply casts the reference of \a T type to an another type. Uses \a static_cast function.
/// @return The new shared pointer of \a R type which refers to the same +retained reference.
template<class R>
SharedPtr<R> cast() const {
return SharedPtr<R>(static_cast<R *>(_ptr));
}
/// @return The const reference to the ARC class.
const T * LIBPLZMA_NULLABLE get() const noexcept {
return _ptr;
}
/// @return The reference to the ARC class.
T * LIBPLZMA_NULLABLE get() noexcept {
return _ptr;
}
/// @brief Takes the reference without changing the ownership and nullifies internal pointer.
/// @return The reference to the ARC class.
/// @note Retain counter keeps unchanged.
T * LIBPLZMA_NULLABLE take() noexcept {
T * ptr = _ptr;
_ptr = nullptr;
return ptr;
}
/// @brief Assigns the new reference without changing the ownership.
/// The previous reference will be released.
/// @note Retain counter of the argument \a ptr unchanged.
void assign(T * LIBPLZMA_NULLABLE ptr) {
if (_ptr != ptr && _ptr) {
_ptr->release();
}
_ptr = ptr;
}
/// @brief Releases the reference and nullifies the internal storage.
void clear() {
if (_ptr) {
_ptr->release();
_ptr = nullptr;
}
}
/// @return The reference to the ARC class.
inline T * LIBPLZMA_NULLABLE operator -> () noexcept { return _ptr; }
/// @return The const reference to the ARC class.
inline const T * LIBPLZMA_NULLABLE operator -> () const noexcept { return _ptr; }
/// @brief Checks the reference exists.
inline operator bool () const noexcept { return _ptr != nullptr; }
/// @brief Checks the reference does not exists.
inline bool operator ! () const noexcept { return _ptr == nullptr; }
/// @brief Moves the reference from an another shared pointer without changing the ownership.
/// The previuos reference will be released.
SharedPtr & operator = (SharedPtr && ptr) {
T * prev = _ptr;
_ptr = ptr._ptr;
ptr._ptr = nullptr;
if (_ptr != prev && prev) {
prev->release();
}
return *this;
}
/// @brief Copies the reference from an another shared pointer with changing the ownership.
/// The new reference will be retained.
/// The previuos reference will be released.
SharedPtr & operator = (const SharedPtr & ptr) {
T * prev = _ptr;
if ( (_ptr = ptr._ptr) ) {
_ptr->retain();
}
if (prev) {
prev->release();
}
return *this;
}
/// @brief Constructs the \a SharedPtr instance with another shared pointer instance.
/// The ownership is moved, transferred from the \a ptr shared instance.
SharedPtr(SharedPtr && ptr) noexcept : _ptr(ptr._ptr) {
ptr._ptr = nullptr;
}
/// @brief Constructs the \a SharedPtr instance with another shared pointer instance.
/// The ownership is shared between 2 instances, i.e. the existed reference is retained.
SharedPtr(const SharedPtr & ptr) : _ptr(ptr._ptr) {
if (_ptr) {
_ptr->retain();
}
}
/// @brief Constructs the \a SharedPtr instance with the reference to the ARC class.
/// The reference is retained.
SharedPtr(T * LIBPLZMA_NULLABLE ptr) : _ptr(ptr) {
if (_ptr) {
_ptr->retain();
}
}
/// @brief Constructs the empty \a SharedPtr instance without any reference to the ARC class.
SharedPtr() noexcept = default;
/// @brief Destroys the \a SharedPtr instance and releases the existed reference to the ARC class.
~SharedPtr() {
if (_ptr) {
_ptr->release();
}
}
};
/// @brief Constructs a \a SharedPtr instance with movable arguments.
/// Similar to the \a std::make_shared function.
/// @tparam T The Class type which supports ARC
/// @tparam ARGS The movable arguments for a contructor of the \a T ARC class.
template<class T, class ... ARGS>
SharedPtr<T> makeShared(ARGS && ... args) {
return SharedPtr<T>(new T(static_cast<ARGS &&>(args)...));
}
/// @brief The exception class for all cases in the library.
/// Similar to the \a std::exception, plus contains extended information.
class LIBPLZMA_CPP_CLASS_API Exception final {
private:
mutable char * LIBPLZMA_NULLABLE _what = nullptr;
mutable char * LIBPLZMA_NULLABLE _file = nullptr;
mutable char * LIBPLZMA_NULLABLE _reason = nullptr;
mutable plzma_error_code _code = plzma_error_code_unknown;
mutable int _line = 0; // `int` cause of compiller's preprocessor definition type.
Exception & operator = (const Exception &) = delete;
Exception & operator = (Exception &&) = delete;
Exception(const Exception &) = delete;
public:
/// @return Returns an explanatory string.
const char * LIBPLZMA_NULLABLE what() const noexcept;
/// @return Returns the file name in which the exception was thrown.
const char * LIBPLZMA_NULLABLE file() const noexcept;
/// @return Returns the reason why the exception was thrown.
const char * LIBPLZMA_NULLABLE reason() const noexcept;
/// @return Returns predefined code, type of the exception. See \a plzma_error_code enum.
plzma_error_code code() const noexcept;
/// @return Returns the line number of the file where exception was thrown.
int line() const noexcept;
/// @brief Changes the \a what property of the exception by concatinating all provided strings.
LIBPLZMA_REQUIRES_LAST_NULL_ARG void setWhat(const char * LIBPLZMA_NULLABLE what = nullptr, ...) noexcept;
/// @brief Changes the \a reason property of the exception by concatinating all provided strings.
LIBPLZMA_REQUIRES_LAST_NULL_ARG void setReason(const char * LIBPLZMA_NULLABLE reason = nullptr, ...) noexcept;
/// @brief Contructs the \a Exception instance with information from another one.
Exception(Exception && exception) noexcept;
/// @brief Contructs the \a Exception instance with \a code, \a what, \a file and \a line information.
Exception(const plzma_error_code code,
const char * LIBPLZMA_NULLABLE what,
const char * LIBPLZMA_NULLABLE file,
const int line) noexcept;
/// @brief Contructs empty \a Exception instance.
Exception() noexcept = default;
~Exception() noexcept;
/// @brief Creates new heap copy of the exception and moves all info from a source instance.
/// The operator \a new will be used.
/// @return The new heap copy or \a nullptr if the exception instance can't be created.
Exception * LIBPLZMA_NULLABLE moveToHeapCopy() const noexcept;
/// @brief Creates new heap copy of the exception with \a code, \a what, \a file and \a line information.
/// The operator \a new will be used.
/// @return The new heap copy or \a nullptr if the exception instance can't be created.
static Exception * LIBPLZMA_NULLABLE create(const plzma_error_code code,
const char * LIBPLZMA_NULLABLE what,
const char * LIBPLZMA_NULLABLE file,
const int line) noexcept;
};
template struct LIBPLZMA_CPP_CLASS_API Pair<size_t, size_t>;
/// @brief The class \a String stores and manipulates sequences of the UTF-8 and wide character strings.
///
/// During the lifetime, the string might be updated with the UTF-8 or wide character string arguments,
/// but internally stores only one type of characters, i.e. UTF-8 or wide.
/// That's why, the conversion between UTF-8 <-> wide(16|32 bit) might take place.
struct LIBPLZMA_CPP_CLASS_API String {
protected:
mutable RawHeapMemory _cs;
mutable RawHeapMemory _ws;
mutable plzma_size_t _cslen = 0;
plzma_size_t _size = 0;
void syncWide() const;
void syncUtf8() const;
void moveFrom(String && str, const plzma_erase eraseType = plzma_erase_none) noexcept;
void copyFrom(const String & str, const plzma_erase eraseType = plzma_erase_none);
void copyFrom(const wchar_t * LIBPLZMA_NULLABLE str, const plzma_erase eraseType = plzma_erase_none);
void copyFrom(const char * LIBPLZMA_NULLABLE str, const plzma_erase eraseType = plzma_erase_none);
void append(const wchar_t * LIBPLZMA_NONNULL * LIBPLZMA_NONNULL stringsList,
const Pair<size_t, size_t> * LIBPLZMA_NONNULL sizesList,
const size_t count,
const plzma_erase eraseType = plzma_erase_none);
void append(const char * LIBPLZMA_NONNULL * LIBPLZMA_NONNULL stringsList,
const Pair<size_t, size_t> * LIBPLZMA_NONNULL sizesList,
const size_t count,
const plzma_erase eraseType = plzma_erase_none);
public:
/// @return The UTF-8, null-terminated charater string.
/// @note If the internal string presentation is wide, then the wide chars will be converted to UTF-8 and returned.
/// If the \a count of characters is zero, then the \a plzma_empty_cstring will be returned.
/// @exception The \a Exception with \a plzma_error_code_internal code in case of conversion error or any nested.
const char * LIBPLZMA_NONNULL utf8() const;
/// @return The wide, null-terminated charater string.
/// @note If the internal string presentation is UTF-8, then the UTF-8 chars will be converted to wide and returned.
/// If the \a count of characters is zero, then the \a plzma_empty_wstring will be returned.
/// @exception The \a Exception with \a plzma_error_code_internal code in case of conversion error or any nested.
const wchar_t * LIBPLZMA_NONNULL wide() const;
/// @return The number of unicode characters in a string.
plzma_size_t count() const noexcept;
/// @brief Clears the string. Optionaly, the string's container might be erased.
/// @param eraseType The type of the erasing the string's content before releasing. See \a plzma_erase enum.
void clear(const plzma_erase eraseType = plzma_erase_none) noexcept;
/// @brief Copies the content of another string instance.
virtual void set(const String & str);
/// @brief Copies the content of the wide, null-terminated character \a str string reference.
virtual void set(const wchar_t * LIBPLZMA_NULLABLE str);
/// @brief Copies the content of the UTF-8, null-terminated character \a str string reference.
virtual void set(const char * LIBPLZMA_NULLABLE str);
/// @brief Appends string with the content of the wide, null-terminated character \a str string reference.
virtual void append(const wchar_t * LIBPLZMA_NULLABLE str);
/// @brief Appends string with the content of the UTF-8, null-terminated character \a str string reference.
virtual void append(const char * LIBPLZMA_NULLABLE str);
/// @brief Moves the content of the \a str string.
String & operator = (String && str) noexcept;
/// @brief Copies the content of another string instance.
String & operator = (const String & str);
/// @brief Constructs the \a String instance and moves the content from the \a str string.
String(String && str) noexcept;
/// @brief Constructs the \a String instance and copies the content of the \a str string.
String(const String & str);
/// @brief Constructs the \a String instance and copies the content of the \a str wide, null-terminated character string.
String(const wchar_t * LIBPLZMA_NULLABLE str);
/// @brief Constructs the \a String instance and copies the content of the \a str UTF-8, null-terminated character string.
String(const char * LIBPLZMA_NULLABLE str);
/// @brief Constructs the empty \a String instance.
String() noexcept = default;
virtual ~String() noexcept = default;
/// @return The number of bytes/characters after the control character.
/// @link https://tools.ietf.org/html/rfc3629#section-3
static size_t utf8TrailingBytes(const uint8_t c) noexcept;
/// @brief Calculates the length and number of unicode characters of the UTF-8 string limited to the maximum number of unicode characters.
/// @param str The UTF-8, null-terminated character string.
/// @param maxSize The maximum number of unicode characters.
/// @return The pair: <length, number of unicode characters>
static Pair<size_t, size_t> lengthMaxCount(const char * LIBPLZMA_NULLABLE str, const size_t maxSize) noexcept;
/// @brief Calculates the length and number of unicode characters of the UTF-8 string limited to the maximum C-length.
/// @param str The UTF-8, null-terminated character string.
/// @param maxLength The maximum length of the C-string.
/// @return The pair: <length, number of unicode characters>
static Pair<size_t, size_t> lengthMaxLength(const char * LIBPLZMA_NULLABLE str, const size_t maxLength) noexcept;
};
/// @brief The \a Path class stores path's string presentation.
///
/// During the manipulation of the path's string presentation:
/// @li 1. The path separator will be automatically replaced with the platform specific one.
/// @li 2. The previous content will be erased with \a plzma_erase_zero, i.e. zero-filled.
struct LIBPLZMA_CPP_CLASS_API Path final : public String {
public:
class Iterator;
virtual void set(const String & str) override final;
virtual void set(const wchar_t * LIBPLZMA_NULLABLE str) override final;
virtual void set(const char * LIBPLZMA_NULLABLE str) override final;
virtual void append(const wchar_t * LIBPLZMA_NULLABLE str) override final;
virtual void append(const char * LIBPLZMA_NULLABLE str) override final;
void append(const Path & path);
Path appending(const wchar_t * LIBPLZMA_NULLABLE str) const;
Path appending(const char * LIBPLZMA_NULLABLE str) const;
Path appending(const Path & path) const;
/// @brief Appends the random component to the path.
///
/// If path successfully updated, then the updated path doesn't exists in a root directory.
/// The component consists of ASCII characters in range ['a'; 'z'].
/// @exception The \a Exception with \a plzma_error_code_internal code in case if path can't be appended.
void appendRandomComponent();
/// @return New path with new random component.
/// @see \a appendRandomComponent method.
Path appendingRandomComponent() const;
/// @brief Receives the last component of the path.
Path lastComponent() const;
/// @brief Removes the last component from the path.
void removeLastComponent();
/// @return New path by without last component.
/// @see \a removeLastComponent method.
Path removingLastComponent() const;
/// @brief Checks the path exists with optional checking for a directory.
/// @param isDir The optional pointer to a boolean variable to store the result of checking for a directory.
/// @return \a true if path exists, otherwise \a false.
bool exists(bool * LIBPLZMA_NULLABLE isDir = nullptr) const;
/// @brief Checks the path exists and has read permissions.
/// @return \a true if path exists and readable, otherwise \a false.
bool readable() const;
/// @brief Checks the path exists and has write permissions.
/// @return \a true if path exists and writable, otherwise \a false.
bool writable() const;
/// @brief Checks the path exists and has read-write permissions.
/// @return \a true if path exists, readable and writable, otherwise \a false.
bool readableAndWritable() const;
/// @brief Provides the stat info of the path.
/// @return The stat info of the path or empty/zero-filled struct if operation was failed.
plzma_path_stat stat() const;
/// @brief Physically removes the directory with all content or file associated with the path.
/// @param skipErrors Skips errors and continuing removing or stop on first error.
/// @return The result of removing directory or file.
bool remove(const bool skipErrors = false) const;
/// @brief Creates the directory at specific path.
/// @param withIntermediates Create intermediate directories for each component or not.
/// @return The creation result of the directory or if directory already exists.
bool createDir(const bool withIntermediates) const;
/// @brief Set creation, last access and modification unix timestamp of the file path.
/// @param timestamp The unix timestamps.
/// @return \a true if timestamp is set, otherwise \a false.
/// @note No checks for a file path existence, path type or any.
bool applyFileTimestamp(const plzma_path_timestamp timestamp);
/// @brief Opens a file associated with path.
/// @param mode The open file mode string. For Windows, it's possible to provide encoded character set to use(one of UTF-8, UTF-16LE, or UNICODE).
/// @return The file reference or nullptr.
/// @see \a fopen C function.
FILE * LIBPLZMA_NULLABLE openFile(const char * LIBPLZMA_NONNULL mode) const;
/// @brief Opens a directory associated with path for iterating the content.
/// @param mode The open directory mode.
/// @return The \a SharedPtr of the path iterator.
/// @exception The \a Exception with \a plzma_error_code_io code in case if a directory can't be opened.
SharedPtr<Iterator> openDir(const plzma_open_dir_mode_t mode = 0) const;
bool operator == (const Path & path) const;
Path & operator = (Path && path) noexcept;
Path(Path && path) noexcept;
Path & operator = (const Path & path);
Path(const Path & path);
Path(const wchar_t * LIBPLZMA_NULLABLE path);
Path(const char * LIBPLZMA_NULLABLE path);
Path() noexcept : String() { }
/// @note For a security reasons, the content will be erased with zeros, i.e. '.erase(plzma_erase_zero)'.
virtual ~Path() noexcept;
/// @brief Provides the path with the platform specific temporary directory for the library.
/// The provided directory path, if such exists, has a read-write permissions.
/// @return The path with existed temporary directory or empty path.
static Path tmpPath();
};
/// @brief Interface to a platform specific directory path iterator.
class LIBPLZMA_CPP_CLASS_API Path::Iterator {
private:
friend struct SharedPtr<Path::Iterator>;
virtual void retain() noexcept = 0;
virtual void release() noexcept = 0;
protected:
Path _root;
Path _path;
Path _component;
uint16_t _flags = 0;
plzma_open_dir_mode_t _mode = 0;
void clearPaths() noexcept;
Iterator() = delete;
Iterator(const Path & root, const plzma_open_dir_mode_t mode);
virtual ~Iterator() noexcept = default;
public:
/// @brief Recevies the current file or directory component.
const Path & component() const noexcept;
/// @brief Recevies the current file or directory path.
Path path() const;
/// @brief Recevies the current file or directory full path, prefixed with root path.
Path fullPath() const;
/// @brief Checks the current iterator's path is directory.
/// @return \a true the iterator's path is directory.
bool isDir() const noexcept;
/// @brief Continue iteration.
/// @return \a true The next file or directory located, otherwise iteration is finished.
virtual bool next() = 0;
/// @brief Closes iteration and all open directory descriptors/handlers.
virtual void close() noexcept = 0;
};
template struct LIBPLZMA_CPP_CLASS_API SharedPtr<Path::Iterator>;
/// @brief The archive item.
class LIBPLZMA_CPP_CLASS_API Item final {
private:
friend struct SharedPtr<Item>;
Path _path;
uint64_t _size = 0;
uint64_t _packSize = 0;
plzma_path_timestamp _timestamp{0, 0, 0};
uint32_t _crc32 = 0;
plzma_size_t _index = 0;
plzma_size_t _referenceCounter = 0;
bool _encrypted = false;
bool _isDir = false;
void retain() noexcept;
void release() noexcept;
Item(Item &&) = delete;
Item & operator = (Item &&) = delete;
Item & operator = (const Item &) = delete;
Item(const Item &) = delete;
Item() = delete;
public:
/// @return Receives the item's path inside the archive.
const Path & path() const noexcept;
/// @return Receives the item's index inside the archive.
plzma_size_t index() const noexcept;
/// @return Receives the size in bytes of the item.
uint64_t size() const noexcept;
/// @return Receives the packed size in bytes of the item.
uint64_t packSize() const noexcept;
/// @return Receives the CRC-32 checksum of the item's content.
uint32_t crc32() const noexcept;
/// @return The creation time of the item. Unix timestamp.
time_t creationTime() const noexcept;
/// @return The last access time of the item. Unix timestamp.
time_t accessTime() const noexcept;
/// @return The last modification time of the item. Unix timestamp.
time_t modificationTime() const noexcept;
/// @return The creation, last access and last modification unix timestamps of the item.
plzma_path_timestamp timestamp() const noexcept;
/// @return Checks the item is encrypted.
bool encrypted() const noexcept;
/// @return Checks the item is directory or file.
bool isDir() const noexcept;
/// @brief Updates the size of the item.
/// @param size The size in bytes.
void setSize(const uint64_t size) noexcept;
/// @brief Updates the packed size of the item.
/// @param size The size in bytes.
void setPackSize(const uint64_t size) noexcept;
/// @brief Updates the CRC-32 checksum of the item.
/// @param crc The CRC-32 checksum value.
void setCrc32(const uint32_t crc) noexcept;
/// @brief Updates creation time of the item.
/// @param time The unix timestamp.
void setCreationTime(const time_t time) noexcept;
/// @brief Updates last access time of the item.
/// @param time The unix timestamp.
void setAccessTime(const time_t time) noexcept;
/// @brief Updates modification time of the item.
/// @param time The unix timestamp.
void setModificationTime(const time_t time) noexcept;
/// @brief Set creation, last access and last modification unix timestamps of the item.
void setTimestamp(const plzma_path_timestamp timestamp) noexcept;
/// @brief Marks the item is encrypted.
void setEncrypted(const bool encrypted) noexcept;
/// @brief Marks the item is directory.
void setIsDir(const bool dir) noexcept;
/// @brief Constructs the \a Item instance with path and index in the archive.
/// @param path The associated item's path.
/// @param index The index of the item in the archive.
Item(const Path & path, const plzma_size_t index);
/// @brief Constructs the \a Item instance with movable path and index in the archive.
/// @param path The associated item's path. After the successfull creation of the item, the path is empty.
/// @param index The index of the item in the archive.
Item(Path && path, const plzma_size_t index) noexcept;
};
template struct LIBPLZMA_CPP_CLASS_API SharedPtr<Item>;
template struct LIBPLZMA_CPP_CLASS_API Pair<void *, size_t>;
/// @brief Interface to the input file stream.
class InStream {
private:
friend struct SharedPtr<InStream>;
virtual void retain() = 0;
virtual void release() = 0;
protected:
virtual void * LIBPLZMA_NONNULL base() noexcept = 0;
virtual ~InStream() noexcept = default;
public:
/// @brief Checks the input file stream is opened.
/// @note Thread-safe.
virtual bool opened() const = 0;
/// @brief Erases and removes the content of the stream.
/// @param eraseType The type of erasing the content.
/// @return The erasing result.
/// @note Thread-safe.
virtual bool erase(const plzma_erase eraseType = plzma_erase_none) = 0;
};
template struct LIBPLZMA_CPP_CLASS_API SharedPtr<InStream>;
/// @brief Creates the input file stream with path.
/// @param path The non-empty input file path.
/// @return The shared pointer with input file stream.
/// @exception The \a Exception with \a plzma_error_code_invalid_arguments code in case if path is empty.
LIBPLZMA_CPP_API(SharedPtr<InStream>) makeSharedInStream(const Path & path);
/// @brief Creates the input file stream with path.
/// @param path The movable non-empty input file path.
/// @return The shared pointer with input file stream.
/// @exception The \a Exception with \a plzma_error_code_invalid_arguments code in case if path is empty.
LIBPLZMA_CPP_API(SharedPtr<InStream>) makeSharedInStream(Path && path);
/// @brief Creates the input file stream with the file memory content.
/// During the creation, the memory will copyed.
/// @param memory The file memory content.
/// @param size The memory size in bytes.
/// @return The shared pointer with input file stream.
/// @exception The \a Exception with \a plzma_error_code_not_enough_memory code in case if can't allocate required size of memory.
/// @exception The \a Exception with \a plzma_error_code_invalid_arguments code in case if memory/size is empty.
LIBPLZMA_CPP_API(SharedPtr<InStream>) makeSharedInStream(const void * LIBPLZMA_NONNULL memory, const size_t size);
/// @brief Creates the input file stream with the file memory content.
/// During the creation, the memory will not be copyed.
/// @param memory The file memory content.
/// @param size The memory size in bytes.
/// @param freeCallback The callback, which will be triggered with provided \a memory pointer at the end of stream's lifetime.
/// @return The shared pointer with input file stream.
/// @exception The \a Exception with \a plzma_error_code_invalid_arguments code in case if memory/size/freeCallback is empty.
LIBPLZMA_CPP_API(SharedPtr<InStream>) makeSharedInStream(void * LIBPLZMA_NONNULL memory, const size_t size, plzma_free_callback LIBPLZMA_NONNULL freeCallback);
/// @brief Creates the input file stream with user defined callbacks.
/// @param openCallback Opens the file stream for reading. Similar to \a fopen C function.
/// @param closeCallback Closes the file stream. Similar to \a fclose C function.
/// @param seekCallback Sets the read offset of the stream. Similar to \a fseek C function.
/// @param readCallback Reads the number of bytes into provided byffer. Similar to \a fread C function.
/// @param context The user defined context provided to all callbacks.
/// @return The shared pointer with input file stream.
/// @exception The \a Exception with \a plzma_error_code_invalid_arguments code in case if not all callbacks are provided.
LIBPLZMA_CPP_API(SharedPtr<InStream>) makeSharedInStream(plzma_in_stream_open_callback LIBPLZMA_NONNULL openCallback,
plzma_in_stream_close_callback LIBPLZMA_NONNULL closeCallback,
plzma_in_stream_seek_callback LIBPLZMA_NONNULL seekCallback,
plzma_in_stream_read_callback LIBPLZMA_NONNULL readCallback,
const plzma_context context = plzma_context{nullptr, nullptr}); // C2059 = { .context = nullptr, .deinitializer = nullptr }
template<typename T>
class Vector;
typedef Vector<SharedPtr<InStream> > InStreamArray;
/// @brief Creates the multi input stream with a list of input streams.
/// The list should not be empty. The order: file.001, file.002, ..., file.XXX
/// @param streams The non-empty list of input streams. Each stream inside the list should also exist.
/// @return The shared pointer with input file stream.
/// @exception The \a Exception with \a plzma_error_code_invalid_arguments code in case if streams list is empty or contains empty stream.
LIBPLZMA_CPP_API(SharedPtr<InStream>) makeSharedInStream(InStreamArray && streams);
template struct LIBPLZMA_CPP_CLASS_API Pair<RawHeapMemory, size_t, false>;
typedef Pair<RawHeapMemory, size_t, false> RawHeapMemorySize;
/// @brief Interface to the output file or memory stream.
class OutStream {
protected:
friend struct SharedPtr<OutStream>;
virtual void retain() = 0;
virtual void release() = 0;
virtual void * LIBPLZMA_NONNULL base() noexcept = 0;
virtual ~OutStream() noexcept = default;
public:
/// @return Checks the output file stream is opened.
/// @note Thread-safe.
virtual bool opened() const = 0;
/// @brief Erases and removes the content of the stream.
/// @param eraseType The type of erasing the content.
/// @return The erasing result.
/// @note Thread-safe.
virtual bool erase(const plzma_erase eraseType = plzma_erase_none) = 0;
/// @brief Copies the content of the stream to a heap memory.
///
/// The stream must be closed.
/// @return The pair with newly allocated heap memory with the stream's content.
/// @exception The \a Exception with \a plzma_error_code_not_enough_memory code in case if required amount of memory can't be allocated.
/// @note Thread-safe.
virtual RawHeapMemorySize copyContent() const = 0;
};
template struct LIBPLZMA_CPP_CLASS_API SharedPtr<OutStream>;
/// @brief Creates the output file stream with path.
/// @param path The non-empty output file path.
/// @return The output file stream.
/// @exception The \a Exception with \a plzma_error_code_invalid_arguments code in case if path is empty.
LIBPLZMA_CPP_API(SharedPtr<OutStream>) makeSharedOutStream(const Path & path);
/// @brief Creates the output file stream object with movable path.
/// @param path The non-empty output file path. After the successfull creation of the stream, the path is empty.
/// @return The output file stream.
/// @exception The \a Exception with \a plzma_error_code_invalid_arguments code in case if path is empty.
LIBPLZMA_CPP_API(SharedPtr<OutStream>) makeSharedOutStream(Path && path);
/// @brief Creates the output file stream object for writing to memory.
/// @return The output file stream.
LIBPLZMA_CPP_API(SharedPtr<OutStream>) makeSharedOutStream(void);
typedef Vector<SharedPtr<OutStream> > OutStreamArray;
/// @brief Interface to the output multi volume/part stream.
class OutMultiStream : public OutStream {
private:
friend struct SharedPtr<OutMultiStream>;
protected:
virtual ~OutMultiStream() noexcept = default;
public:
/// @return The list of created sub-streams. The stream must be closed.
/// If stream is opened, then the list is empty.
virtual OutStreamArray streams() const = 0;
};
template struct LIBPLZMA_CPP_CLASS_API SharedPtr<OutMultiStream>;
/// @brief Creates the output multi stream with directory path, part name, extension, format and part size.
/// All sub-streams are file streams.
/// @param dirPath The non-empty output directory path.
/// @param partName The non-empty output file name.
/// @param partExtension Optional extension.