-
Notifications
You must be signed in to change notification settings - Fork 19
/
qs_common.h
1244 lines (1133 loc) · 45.8 KB
/
qs_common.h
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
/* qs - Quick Serialization of R Objects
Copyright (C) 2019-present Travers Ching
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
You can contact the author at:
https://github.com/traversc/qs
*/
#ifndef QS_COMMON_H
#define QS_COMMON_H
#include <Rcpp.h>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <algorithm>
#include <memory>
#include <array>
#include <cstddef>
#include <type_traits>
#include <utility>
#include <string>
#include <vector>
#include <climits>
#include <cstdint>
#include <unordered_map>
// platform specific headers
#ifdef _WIN32
#include <sys/stat.h> // _S_IWRITE
#include <Fileapi.h>
#include <WinDef.h>
#include <Winbase.h>
#include <Handleapi.h>
// TRUE and FALSE is defined in one of these headers as 1 and 0; conflicts with #define in R headers
#ifdef TRUE
#undef TRUE
#endif
#ifdef FALSE
#undef FALSE
#endif
#else
#include <sys/mman.h> // mmap
#endif
#include <atomic>
#include <thread>
#include <R.h>
#include <Rinternals.h>
#include <Rversion.h>
#include "RApiSerializeAPI.h"
#include "zstd.h"
#include "lz4.h"
#include "lz4hc.h"
#include "BLOSC/shuffle_routines.h"
#include "BLOSC/unshuffle_routines.h"
#include "xxhash/xxhash.c"
#include <R_ext/Rdynload.h>
// #include "expand_binding_value.h"
using namespace Rcpp;
#if R_VERSION >= R_Version(3, 5, 0)
#define USE_ALT_REP
#include "sf_external.h"
#endif
////////////////////////////////////////////////////////////////
// common utility functions and constants
////////////////////////////////////////////////////////////////
// endian function defined in qs_functions.cpp
bool is_big_endian();
// https://stackoverflow.com/a/36835959/2723734
inline constexpr unsigned char operator "" _u8(unsigned long long arg) noexcept {
return static_cast<uint8_t>(arg);
}
inline constexpr unsigned char operator "" _u16(unsigned long long arg) noexcept {
return static_cast<uint16_t>(arg);
}
static constexpr uint64_t BLOCKRESERVE = 64ULL;
static constexpr uint32_t NA_STRING_LENGTH = 4294967295UL; // 2^32-1 -- length used to signify NA value; note maximum string size is defined by `int` in mkCharLen, so this value is safe
static constexpr uint64_t MIN_SHUFFLE_ELEMENTS = 4ULL;
static constexpr uint64_t BLOCKSIZE = 524288ULL;
static constexpr uint64_t MAX_SAFE_INTEGER = 9007199254740991ULL; // 2^53-1 -- the largest integer that can be "safely" represented as a double ~ (about 9000 terabytes)
static const std::array<uint8_t,4> magic_bits = {0x0B,0x0E,0x0A,0x0C};
static const std::array<uint8_t,4> empty_bits = {0,0,0,0};
static constexpr uint8_t list_header_5 = 0x20_u8;
static constexpr uint8_t list_header_8 = 0x01_u8;
static constexpr uint8_t list_header_16 = 0x02_u8;
static constexpr uint8_t list_header_32 = 0x03_u8;
static constexpr uint8_t list_header_64 = 0x04_u8;
static constexpr uint8_t numeric_header_5 = 0x40_u8;
static constexpr uint8_t numeric_header_8 = 0x05_u8;
static constexpr uint8_t numeric_header_16 = 0x06_u8;
static constexpr uint8_t numeric_header_32 = 0x07_u8;
static constexpr uint8_t numeric_header_64 = 0x08_u8;
static constexpr uint8_t integer_header_5 = 0x60_u8;
static constexpr uint8_t integer_header_8 = 0x09_u8;
static constexpr uint8_t integer_header_16 = 0x0A_u8;
static constexpr uint8_t integer_header_32 = 0x0B_u8;
static constexpr uint8_t integer_header_64 = 0x0C_u8;
static constexpr uint8_t logical_header_5 = 0x80_u8;
static constexpr uint8_t logical_header_8 = 0x0D_u8;
static constexpr uint8_t logical_header_16 = 0x0E_u8;
static constexpr uint8_t logical_header_32 = 0x0F_u8;
static constexpr uint8_t logical_header_64 = 0x10_u8;
static constexpr uint8_t raw_header_32 = 0x17_u8;
static constexpr uint8_t raw_header_64 = 0x18_u8;
static constexpr uint8_t null_header = 0x00_u8;
static constexpr uint8_t sym_header = 0x1D_u8;
static constexpr uint8_t character_header_5 = 0xA0_u8;
static constexpr uint8_t character_header_8 = 0x11_u8;
static constexpr uint8_t character_header_16 = 0x12_u8;
static constexpr uint8_t character_header_32 = 0x13_u8;
static constexpr uint8_t character_header_64 = 0x14_u8;
static constexpr uint8_t string_header_NA = 0x0F_u8;
static constexpr uint8_t string_header_5 = 0x20_u8;
static constexpr uint8_t string_header_8 = 0x01_u8;
static constexpr uint8_t string_header_16 = 0x02_u8;
static constexpr uint8_t string_header_32 = 0x03_u8;
static constexpr uint8_t string_enc_native = 0x00_u8;
static constexpr uint8_t string_enc_utf8 = 0x40_u8;
static constexpr uint8_t string_enc_latin1 = 0x80_u8;
static constexpr uint8_t string_enc_bytes = 0xC0_u8;
static constexpr uint8_t complex_header_32 = 0x15_u8;
static constexpr uint8_t complex_header_64 = 0x16_u8;
static constexpr uint8_t attribute_header_5 = 0xE0_u8;
static constexpr uint8_t attribute_header_8 = 0x1E_u8;
static constexpr uint8_t attribute_header_32 = 0x1F_u8;
// static constexpr uint8_t unused2 = 0x1B_u8; // not in use
static constexpr uint8_t nstype_header_32 = 0x19_u8; // other (rare) types of objects are serialized via R_serialize
static constexpr uint8_t nstype_header_64 = 0x1A_u8;
// since we might run out of headers in 8 bits, use 16 bits
static constexpr uint8_t extension_header = 0x1C_u8;
// header combined with extension is useful for writing
static constexpr uint8_t s4_header = 0x01_u8;
static constexpr uint8_t s4flag_header = 0x02_u8;
static constexpr uint8_t pairlist_header = 0x03_u8;
static constexpr uint8_t lang_header = 0x04_u8;
static constexpr uint8_t clos_header = 0x05_u8;
static constexpr uint8_t prom_header = 0x06_u8;
static constexpr uint8_t dot_header = 0x07_u8;
// environment headers
static constexpr uint8_t unlocked_env_header = 0x08_u8; // deprecated but still supported
static constexpr uint8_t locked_env_header = 0x09_u8; // deprecated but still supported
static constexpr uint8_t reference_object_header = 0x10_u8;
// with flags
static constexpr uint8_t pairlist_wf_header = 0x11_u8;
static constexpr uint8_t lang_wf_header = 0x12_u8;
static constexpr uint8_t clos_wf_header = 0x13_u8;
static constexpr uint8_t prom_wf_header = 0x14_u8;
static constexpr uint8_t dot_wf_header = 0x15_u8;
// static constexpr std::array<uint8_t,2> s4_header_with_ext {{ extension_header, s4_header }};
// static constexpr std::array<uint8_t,2> s4flag_header_with_ext {{ extension_header, s4flag_header }};
// static constexpr std::array<uint8_t,2> pairlist_header_with_ext {{ extension_header, pairlist_header }};
// static constexpr std::array<uint8_t,2> lang_header_with_ext {{ extension_header, lang_header }};
// static constexpr std::array<uint8_t,2> clos_header_with_ext {{ extension_header, clos_header }};
// static constexpr std::array<uint8_t,2> prom_header_with_ext {{ extension_header, prom_header }};
// static constexpr std::array<uint8_t,2> dot_headerr_with_ext {{ extension_header, dot_header }};
// static constexpr std::array<uint8_t,2> unlocked_env_header_with_ext {{ extension_header, unlocked_env_header }};
// static constexpr std::array<uint8_t,2> locked_env_header_with_ext {{ extension_header, locked_env_header }};
// static constexpr std::array<uint8_t,2> reference_object_header_with_ext {{ extension_header, reference_object_header }};
// can package env or global env etc lock status be changed?
// Technically yes, but even R serialization doesn't capture that information
// They are captured by a special hook and contents are not serialized
// static constexpr uint8_t package_env_header_32 = 0x06_u8;
// static constexpr uint16_t package_env_header_with_ext_32 = 0x1C06_u16;
// static constexpr uint8_t global_env_header = 0x07_u8;
// static constexpr uint16_t global_env_header_with_ext = 0x1C07_u16;
// static constexpr uint8_t base_env_header = 0x08_u8;
// static constexpr uint16_t base_env_header_with_ext = 0x1C08_u16;
// static constexpr uint8_t empty_env_header = 0x09_u8;
// static constexpr uint16_t empty_env_header_with_ext = 0x1C09_u16;
enum class qstype {NUMERIC, INTEGER, LOGICAL, CHARACTER, NIL, LIST, COMPLEX, RAW, PAIRLIST, LANG, CLOS, PROM, DOT, SYM,
PAIRLIST_WF, LANG_WF, CLOS_WF, PROM_WF, DOT_WF, // with flags
S4, S4FLAG, LOCKED_ENV, UNLOCKED_ENV, REFERENCE,
ATTRIBUTE, RSERIALIZED};
///////////////////////////////////////////////////////
// There are three types of output input streams -- std::ifstream/ofstream, file descriptor, windows handle
// these methods are overloaded and normalized so we can use a common template interface
// to do: R connections
inline uint64_t read_check(std::ifstream & con, char * const ptr, const uint64_t count) {
con.read(ptr, count);
uint64_t return_value = con.gcount();
if(return_value != count) {
throw std::runtime_error("error reading from connection (not enough bytes read)");
}
return return_value;
}
inline uint64_t read_allow(std::ifstream & con, char * const ptr, const uint64_t count) {
con.read(ptr, count);
return con.gcount();
}
inline uint64_t write_check(std::ofstream & con, const char * const ptr, const uint64_t count) {
// (void)check_size; // avoid unused variable warning
con.write(ptr, count);
return count;
}
inline bool isSeekable(std::ifstream & myFile) {
return true;
}
///////////////////////////////////////////////////////
// helper functions for Rconnection
// #ifdef USE_R_CONNECTION
// // rconnection read and write with error checking
// inline uint64_t read_check(char * ptr, uint64_t count, Rconnection con, bool check_size=true) {
// uint64_t return_value = R_ReadConnection(con, ptr, count);
// if(check_size) {
// if(return_value != count) {
// throw std::runtime_error("error reading from connection (wrong size)");
// }
// }
// return return_value;
// }
// // rconnection read and write with error checking
// inline uint64_t write_check(char * ptr, uint64_t count, Rconnection con, bool check_size=true) {
// uint64_t return_value = R_WriteConnection(con, ptr, count);
// if(check_size) {
// if(return_value != count) {
// throw std::runtime_error("error writing to connection (wrong size)");
// }
// }
// return return_value;
// }
// inline void writeSizeToCon8(Rconnection con, uint64_t x) {
// uint64_t x_temp = static_cast<uint64_t>(x);
// write_check(reinterpret_cast<char*>(&x_temp),8, con);
// }
// inline void writeSizeToCon4(Rconnection con, uint64_t x) {
// uint32_t x_temp = static_cast<uint32_t>(x);
// write_check(reinterpret_cast<char*>(&x_temp),4, con);
// }
// inline uint32_t readSize4(Rconnection con) {
// std::array<char,4> a = {0,0,0,0};
// read_check(a.data(),4, con);
// return *reinterpret_cast<uint32_t*>(a.data());
// }
// inline uint64_t readSize8(Rconnection con) {
// std::array<char,8> a = {0,0,0,0,0,0,0,0};
// read_check(a.data(),8, con);
// return *reinterpret_cast<uint64_t*>(a.data());
// }
// #endif
///////////////////////////////////////////////////////
// helper functions for file descriptors
// low level interface
// no destructor -- left up to user
// whether fd is read or write is left up to user -- dont use both
// to do: evaluate whether wrapping in a FILE* or std::ostream is more efficient?
static constexpr uint64_t FD_BUFFER_SIZE = 524288; // 2^17
struct fd_wrapper {
int fd;
uint64_t bytes_processed;
uint64_t buffered_bytes;
uint64_t buffer_offset;
std::array<char, FD_BUFFER_SIZE> buffer;
fd_wrapper(int fd) : fd(fd), bytes_processed(0), buffered_bytes(0), buffer_offset(0) {
if(ferror()) throw std::runtime_error("file descriptor is not valid");
}
int ferror() {
#ifdef _WIN32
return errno == EBADF;
#else
return fcntl(fd, F_GETFD) == -1 || errno == EBADF;
#endif
}
inline uint64_t read(char * const ptr, const uint64_t count) {
uint64_t remaining_bytes = count;
while(remaining_bytes > buffered_bytes - buffer_offset) {
std::memcpy(ptr + count - remaining_bytes, buffer.data() + buffer_offset, buffered_bytes - buffer_offset);
remaining_bytes -= buffered_bytes - buffer_offset;
ssize_t temp = ::read(fd, buffer.data(), FD_BUFFER_SIZE);
if(temp < 0) throw std::runtime_error("error reading fd");
bytes_processed += temp;
buffered_bytes = temp;
buffer_offset = 0;
if(buffered_bytes == 0) {
return count - remaining_bytes; // if we reached this point, there wasn't enough data left in file
}
}
// remaining_bytes <= buffered_bytes - buffer_offset
std::memcpy(ptr + count - remaining_bytes, buffer.data() + buffer_offset, remaining_bytes);
buffer_offset += remaining_bytes;
// remaining_bytes = 0; -- no longer necessary to track
return count;
}
// buffer_offset is not uesd
inline uint64_t write(const char * const ptr, const uint64_t count) {
uint64_t remaining_bytes = count;
uint64_t ptr_offset = 0;
// std::cout << "ptr: " << static_cast<void*>(ptr) << ", count: " << count << std::endl;
while(remaining_bytes > 0) {
if(remaining_bytes >= FD_BUFFER_SIZE - buffered_bytes) {
// std::cout << "A: rb " << remaining_bytes << ", bb " << buffered_bytes << ", po " << ptr_offset << std::endl;
uint64_t bytes_to_write = FD_BUFFER_SIZE - buffered_bytes;
if(buffered_bytes == 0) {
// skip memcpy since nothing in buffer
ssize_t temp = ::write(fd, ptr + ptr_offset, FD_BUFFER_SIZE);
if(temp < 0) throw std::runtime_error("error writing to fd");
} else {
std::memcpy(buffer.data() + buffered_bytes, ptr + ptr_offset, bytes_to_write);
ssize_t temp = ::write(fd, buffer.data(), FD_BUFFER_SIZE);
if(temp < 0) throw std::runtime_error("error writing to fd");
}
remaining_bytes -= bytes_to_write;
buffered_bytes = 0;
ptr_offset += bytes_to_write;
} else { // remaining_bytes < FD_BUFFER_SIZE - buffered_bytes
// std::cout << "B: rb " << remaining_bytes << ", bb " << buffered_bytes << ", po " << ptr_offset << std::endl;
std::memcpy(buffer.data() + buffered_bytes, ptr + ptr_offset, remaining_bytes);
buffered_bytes += remaining_bytes;
remaining_bytes = 0;
// ptr_offset += remaining_bytes
}
}
bytes_processed += count;
return count;
}
inline void flush() {
ssize_t temp = ::write(fd, buffer.data(), buffered_bytes);
if(temp < 0) throw std::runtime_error("error writing to fd");
buffered_bytes = 0;
}
fd_wrapper * seekp(uint64_t pos) {
throw std::runtime_error("file descriptor is not seekable");
return nullptr;
}
fd_wrapper * seekg(uint64_t pos) {
throw std::runtime_error("file descriptor is not seekable");
return nullptr;
}
};
inline uint64_t read_check(fd_wrapper & con, char * const ptr, const uint64_t count) {
uint64_t return_value = con.read(ptr, count);
if (con.ferror()) {
throw std::runtime_error("error writing to connection");
}
if(return_value != count) {
throw std::runtime_error("error reading from connection (not enough bytes read)");
}
return return_value;
}
inline uint64_t read_allow(fd_wrapper & con, char * const ptr, const uint64_t count) {
uint64_t return_value = con.read(ptr, count);
if (con.ferror()) {
throw std::runtime_error("error writing to connection");
}
return return_value;
}
inline uint64_t write_check(fd_wrapper & con, const char * const ptr, const uint64_t count) {
uint64_t return_value = con.write(ptr, count);
if (con.ferror()) {
throw std::runtime_error("error writing to connection");
}
// if(check_size) {
// if(return_value != count) {
// throw std::runtime_error("error writing to connection (not enough bytes written)");
// }
// }
return return_value;
}
inline bool isSeekable(fd_wrapper & myFile) {
return false;
}
///////////////////////////////////////////////////////
// helper functions for reading/writing to memory
// Instance should only be used for reading or writing, not both
struct mem_wrapper {
char * start;
uint64_t available_bytes;
uint64_t bytes_processed;
mem_wrapper(void * s, uint64_t ab) : start(static_cast<char*>(s)), available_bytes(ab), bytes_processed(0) {}
inline uint64_t read(char * const ptr, uint64_t count) {
if(count + bytes_processed > available_bytes) {
count = available_bytes - bytes_processed;
}
std::memcpy(ptr, start + bytes_processed, count);
bytes_processed += count;
return count;
}
inline uint64_t write(const char * const ptr, uint64_t count) {
if(count + bytes_processed > available_bytes) {
count = available_bytes - bytes_processed;
}
std::memcpy(start + bytes_processed, ptr, count);
bytes_processed += count;
return count;
}
inline void writeDirect(const char * const ptr, uint64_t count, uint64_t offset) {
std::memcpy(start + offset, ptr, count);
}
mem_wrapper * seekp(uint64_t pos) {
throw std::runtime_error("not seekable");
return nullptr;
}
mem_wrapper * seekg(uint64_t pos) {
throw std::runtime_error("not seekable");
return nullptr;
}
};
inline uint64_t read_check(mem_wrapper & con, char * const ptr, const uint64_t count) {
uint64_t return_value = con.read(ptr, count);
if(return_value != count) {
throw std::runtime_error("error reading from connection (not enough bytes read)");
}
return return_value;
}
inline uint64_t read_allow(mem_wrapper & con, char * const ptr, const uint64_t count) {
uint64_t return_value = con.read(ptr, count);
return return_value;
}
inline uint64_t write_check(mem_wrapper & con, const char * const ptr, uint64_t const count) {
uint64_t return_value = con.write(ptr, count);
// if(check_size) {
// if(return_value != count) {
// throw std::runtime_error("error writing to connection (not enough bytes written)");
// }
// }
return return_value;
}
inline bool isSeekable(mem_wrapper & myFile) {
return false;
}
///////////////////////////////////////////////////////
// helper functions for writing to std::vector
// This is only used for writing
// since we don't know the serialized size ahead of time, we need a resizable memory buffer
// reading -- use mem_wrapper
struct vec_wrapper {
std::vector<char> buffer;
uint64_t bytes_processed = 0;
vec_wrapper() : buffer(std::vector<char>(BLOCKSIZE)) {}
inline uint64_t write(const char * const ptr, uint64_t count) {
if(count + bytes_processed > buffer.size()) {
uint64_t new_buffer_size = buffer.size() * 3/2;
while(new_buffer_size < count*3/2 + bytes_processed) {
new_buffer_size = new_buffer_size * 3/2;
}
buffer.resize(new_buffer_size);
}
std::memcpy(buffer.data() + bytes_processed, ptr, count);
bytes_processed += count;
// std::cout << static_cast<void*>(buffer.data()) << " " << count << std::endl;
return count;
}
inline void writeDirect(const char * const ptr, uint64_t count, uint64_t offset) {
std::memcpy(buffer.data() + offset, ptr, count);
}
vec_wrapper * seekp(uint64_t pos) {
throw std::runtime_error("not seekable");
return nullptr;
}
vec_wrapper * seekg(uint64_t pos) {
throw std::runtime_error("not seekable");
return nullptr;
}
void shrink() {
buffer.resize(bytes_processed);
}
};
inline uint64_t write_check(vec_wrapper & con, const char * const ptr, uint64_t count) {
uint64_t return_value = con.write(ptr, count);
// if(check_size) {
// if(return_value != count) {
// throw std::runtime_error("error writing to connection (not enough bytes written)");
// }
// }
return return_value;
}
inline bool isSeekable(vec_wrapper & myFile) {
return false;
}
///////////////////////////////////////////////////////
// windows handle wrapper
#ifdef _WIN32
struct handle_wrapper {
HANDLE h;
uint64_t bytes_processed;
handle_wrapper(HANDLE h) : h(h), bytes_processed(0) {}
inline DWORD read(char * const ptr, const uint64_t count) {
DWORD bytes_read;
bool ret = ReadFile(h, ptr, count, &bytes_read, NULL);
if(!ret) throw std::runtime_error("error reading from handle");
return bytes_read;
}
inline DWORD write(const char * const ptr, const uint64_t count) {
DWORD bytes_written;
bool ret = WriteFile(h, ptr, count, &bytes_written, NULL);
if(!ret) throw std::runtime_error("error writing to handle");
bytes_processed += bytes_written;
return bytes_written;
}
handle_wrapper * seekp(uint64_t pos) {
throw std::runtime_error("file descriptor is not seekable");
return nullptr;
}
handle_wrapper * seekg(uint64_t pos) {
throw std::runtime_error("file descriptor is not seekable");
return nullptr;
}
};
inline uint64_t read_check(handle_wrapper & con, char * const ptr, const uint64_t count) {
uint64_t return_value = con.read(ptr, count);
if(return_value != count) {
throw std::runtime_error("error writing to handle (not enough bytes read)");
}
return return_value;
}
inline uint64_t read_allow(handle_wrapper & con, char * const ptr, const uint64_t count) {
uint64_t return_value = con.read(ptr, count);
return return_value;
}
inline uint64_t write_check(handle_wrapper & con, const char * const ptr, const uint64_t count) {
uint64_t return_value = con.write(ptr, count);
// if(check_size) {
// if(return_value != count) {
// throw std::runtime_error("error writing to handle (not enough bytes written)");
// }
// }
return return_value;
}
inline bool isSeekable(handle_wrapper & myFile) {
return false;
}
#endif
///////////////////////////////////////////////////////
// templated classes for reading and writing integer sizes
template <class stream_writer>
inline void writeSize8(stream_writer & myFile, const uint64_t x) {
auto x_temp = static_cast<uint64_t>(x);
write_check(myFile, reinterpret_cast<char*>(&x_temp),8);
}
template <class stream_writer>
inline void writeSize4(stream_writer & myFile, const uint64_t x) {
auto x_temp = static_cast<uint32_t>(x);
write_check(myFile, reinterpret_cast<char*>(&x_temp),4);
}
template <class stream_writer>
inline uint32_t readSize4(stream_writer & myFile) {
std::array<char,4> a;
read_check(myFile, a.data(),4);
return *reinterpret_cast<uint32_t*>(a.data());
}
template <class stream_writer>
inline uint64_t readSize8(stream_writer & myFile) {
std::array<char,8> a;
read_check(myFile, a.data(),8);
return *reinterpret_cast<uint64_t*>(a.data());
}
///////////////////////////////////////////////////////
// unaligned cast to <POD>
template<typename POD>
inline POD unaligned_cast(const char * const data, const uint64_t offset) {
POD y;
std::memcpy(&y, data + offset, sizeof(y));
return y;
}
// maximum value is 7, reserve bit shared with shuffle bit
// if we need more slots we will have to use other reserve bits
enum class compalg : uint8_t {
zstd = 0, lz4 = 1, lz4hc = 2, zstd_stream = 3, uncompressed = 4
};
// qs reserve header details
// reserve[0] format version (start writing and checking in qs 0.20.1)
// reserve[1] (low byte) 1 = hash of serialized object written to last 4 bytes of file -- before 16.3, no hash check was performed
// reserve[1] (high byte) unused
// reserve[2] (low byte) shuffle control: 0x01 = logical shuffle, 0x02 = integer shuffle, 0x04 = double shuffle
// reserve[2] (high byte) algorithm: 0x01 = lz4, 0x00 = zstd, 0x02 = "lz4hc", 0x03 = zstd_stream
// reserve[3] endian: 1 = big endian, 0 = little endian
static constexpr int CURRENT_FORMAT_VER = 3;
struct QsMetadata {
uint64_t clength; // compressed length -- for comparing bytes_read / blocks_read with recorded # ..
bool check_hash;
uint8_t endian;
uint8_t compress_algorithm;
int compress_level;
int format_version;
bool lgl_shuffle;
bool int_shuffle;
bool real_shuffle;
bool cplx_shuffle;
//constructor from qsave
QsMetadata(const std::string & preset, const std::string & algorithm, const int compress_level, int shuffle_control, const bool check_hash) :
clength(0), check_hash(check_hash), endian(is_big_endian()) {
if(preset == "fast") {
compress_algorithm = static_cast<uint8_t>(compalg::lz4);
this->compress_level = 100;
shuffle_control = 0;
} else if(preset == "balanced") {
compress_algorithm = static_cast<uint8_t>(compalg::lz4);
this->compress_level = 1;
shuffle_control = 15;
} else if(preset == "high") {
compress_algorithm = static_cast<uint8_t>(compalg::zstd);
this->compress_level = 4;
shuffle_control = 15;
} else if(preset == "archive") {
compress_algorithm = static_cast<uint8_t>(compalg::zstd_stream);
this->compress_level = 14;
shuffle_control = 15;
} else if(preset == "uncompressed") {
compress_algorithm = static_cast<uint8_t>(compalg::uncompressed);
this->compress_level = 0;
shuffle_control = 0;
} else if(preset == "custom") {
if(algorithm == "zstd") {
compress_algorithm = static_cast<uint8_t>(compalg::zstd);
this->compress_level = compress_level;
if(compress_level > 22 || compress_level < -50) throw std::runtime_error("zstd compress_level must be an integer between -50 and 22");
} else if(algorithm == "zstd_stream") {
compress_algorithm = static_cast<uint8_t>(compalg::zstd_stream);
this->compress_level = compress_level;
if(compress_level > 22 || compress_level < -50) throw std::runtime_error("zstd compress_level must be an integer between -50 and 22");
} else if(algorithm == "lz4") {
compress_algorithm = static_cast<uint8_t>(compalg::lz4);
this->compress_level = compress_level;
if(compress_level < 1) throw std::runtime_error("lz4 compress_level must be an integer greater than 1");
} else if(algorithm == "lz4hc") {
compress_algorithm = static_cast<uint8_t>(compalg::lz4hc);
this->compress_level = compress_level;
if(compress_level < 1 || compress_level > 12) throw std::runtime_error("lz4hc compress_level must be an integer between 1 and 12");
} else if(algorithm == "uncompressed") {
compress_algorithm = static_cast<uint8_t>(compalg::uncompressed);
this->compress_level = 0;
} else {
throw std::runtime_error("algorithm must be one of zstd, lz4, lz4hc or zstd_stream");
}
} else {
throw std::runtime_error("preset must be one of fast, balanced (default), high, archive or custom");
}
if(shuffle_control < 0 || shuffle_control > 15) throw std::runtime_error("shuffle_control must be an integer between 0 and 15");
lgl_shuffle = shuffle_control & 0x01;
int_shuffle = shuffle_control & 0x02;
real_shuffle = shuffle_control & 0x04;
cplx_shuffle = shuffle_control & 0x08;
format_version = CURRENT_FORMAT_VER;
}
// 0x0B0E0A0C
static bool checkMagicNumber(const std::array<uint8_t, 4> & reserve_bits) {
if(reserve_bits[0] != magic_bits[0]) return false;
if(reserve_bits[1] != magic_bits[1]) return false;
if(reserve_bits[2] != magic_bits[2]) return false;
if(reserve_bits[3] != magic_bits[3]) return false;
return true;
}
QsMetadata(const uint64_t clength,
const bool check_hash,
const uint8_t endian,
const uint8_t compress_algorithm,
const int compress_level,
const int format_version,
const bool lgl_shuffle,
const bool int_shuffle,
const bool real_shuffle,
const bool cplx_shuffle) :
clength(clength), check_hash(check_hash), endian(endian), compress_algorithm(compress_algorithm),
compress_level(compress_level), format_version(format_version), lgl_shuffle(lgl_shuffle), int_shuffle(int_shuffle),
real_shuffle(real_shuffle), cplx_shuffle(cplx_shuffle) {}
// constructor from q_read
template <class stream_reader>
static QsMetadata create(stream_reader & myFile) {
std::array<uint8_t,4> reserve_bits;
read_check(myFile, reinterpret_cast<char*>(reserve_bits.data()),4);
// version 2
if(reserve_bits[0] != 0) {
std::array<uint8_t,4> reserve_bits2;
if(!checkMagicNumber(reserve_bits)) throw std::runtime_error("QS format not detected");
read_check(myFile, reinterpret_cast<char*>(reserve_bits2.data()),4); // empty reserve bits for now
read_check(myFile, reinterpret_cast<char*>(reserve_bits.data()),4);
}
uint8_t sys_endian = is_big_endian() ? 0x01 : 0x00;
if(reserve_bits[3] != sys_endian) throw std::runtime_error("Endian of system doesn't match file endian");
if(reserve_bits[0] > CURRENT_FORMAT_VER) Rcerr << "File format may be newer; please update qs to latest version";
uint8_t compress_algorithm = reserve_bits[2] >> 4;
int compress_level = 1;
bool lgl_shuffle = reserve_bits[2] & 0x01;
bool int_shuffle = reserve_bits[2] & 0x02;
bool real_shuffle = reserve_bits[2] & 0x04;
bool cplx_shuffle = reserve_bits[2] & 0x08;
bool check_hash = reserve_bits[1];
uint8_t endian = reserve_bits[3];
int format_version = reserve_bits[0];
uint64_t clength = readSize8(myFile);
return {clength,
check_hash,
endian,
compress_algorithm,
compress_level,
format_version,
lgl_shuffle,
int_shuffle,
real_shuffle,
cplx_shuffle};
}
// version 2
template <class stream_writer>
void writeToFile(stream_writer & myFile) {
write_check(myFile, reinterpret_cast<const char*>(magic_bits.data()), 4);
write_check(myFile, reinterpret_cast<const char*>(empty_bits.data()),4);
std::array<uint8_t,4> reserve_bits = {0,0,0,0};
reserve_bits[0] = static_cast<uint8_t>(format_version);
reserve_bits[1] = check_hash;
reserve_bits[2] += compress_algorithm << 4;
reserve_bits[3] = is_big_endian() ? 0x01 : 0x00;
reserve_bits[2] += (lgl_shuffle) + (int_shuffle << 1) + (real_shuffle << 2) + (cplx_shuffle << 3);
write_check(myFile, reinterpret_cast<char*>(reserve_bits.data()),4);
}
};
// Normalize lz4/zstd function arguments so we can use function types
using compress_fun = size_t (*)(void*, size_t, const void*, size_t, int);
using decompress_fun = size_t (*)(void*, size_t, const void*, size_t);
using cbound_fun = size_t (*)(size_t);
using iserror_fun = unsigned (*)(size_t);
size_t LZ4_compressBound_fun(size_t srcSize) {
return LZ4_compressBound(srcSize);
}
size_t LZ4_compress_fun( void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
int compressionLevel) {
return LZ4_compress_fast(reinterpret_cast<char*>(const_cast<void*>(src)),
reinterpret_cast<char*>(const_cast<void*>(dst)),
static_cast<int>(srcSize), static_cast<int>(dstCapacity), compressionLevel);
}
size_t LZ4_compress_HC_fun( void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
int compressionLevel) {
return LZ4_compress_HC(reinterpret_cast<char*>(const_cast<void*>(src)),
reinterpret_cast<char*>(const_cast<void*>(dst)),
static_cast<int>(srcSize), static_cast<int>(dstCapacity), compressionLevel);
}
size_t LZ4_decompress_fun( void* dst, size_t dstCapacity,
const void* src, size_t compressedSize) {
int ret = LZ4_decompress_safe(reinterpret_cast<char*>(const_cast<void*>(src)),
reinterpret_cast<char*>(const_cast<void*>(dst)),
static_cast<int>(compressedSize), static_cast<int>(dstCapacity));
if(ret < 0) {
return SIZE_MAX;
} else {
return ret;
}
}
unsigned LZ4_isError_fun(size_t retval) {
if(retval == SIZE_MAX) {
return 1;
} else {
return 0;
}
}
template <class stream_reader>
uint32_t validate_data(const QsMetadata & qm, stream_reader & myFile, const uint32_t recorded_hash,
const uint32_t computed_hash, const uint64_t computed_length, const bool strict) {
// destructively check EOF -- cannot putback data
std::array<char,4> temp;
uint64_t remaining_bytes = read_allow(myFile, temp.data(), 4);
if(remaining_bytes != 0) {
uint64_t remaining_bytes2 = read_allow(myFile, temp.data(), 4);
while(remaining_bytes2 != 0) {
remaining_bytes += remaining_bytes2;
remaining_bytes2 = read_allow(myFile, temp.data(), 4);
}
std::string msg = "end of file not reached, " + std::to_string(remaining_bytes) + " bytes remaining";
if(strict) {
throw std::runtime_error(msg.c_str());
} else {
Rcerr << "Warning: " << msg << std::endl;
}
}
if((qm.clength != 0) && (computed_length != 0) && (computed_length != qm.clength)) {
if(strict) {
throw std::runtime_error("computed object length does not match recorded object length");
} else {
Rcerr << "Warning: computed object length does not match recorded object length, data may be corrupted" << std::endl;
}
}
if(qm.check_hash) {
if(computed_hash != recorded_hash) {
if(strict) {
throw std::runtime_error("Warning: hash checksum does not match (Recorded, Computed) (" +
std::to_string(recorded_hash) + "," + std::to_string(computed_hash) + "), data may be corrupted");
} else {
Rcerr << "Warning: hash checksum does not match (Recorded, Computed) (" << recorded_hash << "," << computed_hash << "), data may be corrupted" << std::endl;
}
}
return recorded_hash;
}
return 0;
}
// R stack tracker using RAII
// protection handling using RAII should have no issues with longjmp
// ref: https://developer.r-project.org/Blog/public/2019/03/28/use-of-c---in-packages/
// "R restores the protection stack depth before taking a long jump,
// so if a C++ destructor includes say UNPROTECT(1) call to restore
// the protection stack depth, it does not matter it is not executed,
// because R will do that automatically."
//
// There is also a limit of 10,000 on the protection stack.
// Theoretically, we'd need to track it globally to be 100% error proof.
// Realistically, it should never occur in this package as you'd need a list with depth 10,000.
// Ref: https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Garbage-Collection
struct Protect_Tracker {
unsigned int n = 0;
Protect_Tracker() {}
~Protect_Tracker() {
UNPROTECT(n);
}
void operator++(int) {
n++;
}
};
////////////////////////////////////////////////////////////////
// Compress and decompress templates
////////////////////////////////////////////////////////////////
// Testing xxh3 algorithm
// There doesn't seem to be a significant enough improvement
// in speed to be worth the additional changes
// struct xxhash_env {
// XXH3_state_t* x;
// xxhash_env() : x(XXH3_createState()) {
// XXH_errorcode ret = XXH3_64bits_reset(x);
// if(ret == XXH_ERROR) throw std::runtime_error("error in hashing function");
// }
// ~xxhash_env() {
// XXH3_freeState(x);
// }
// void reset() {
// XXH_errorcode ret = XXH3_64bits_reset(x);
// if(ret == XXH_ERROR) throw std::runtime_error("error in hashing function");
// }
// void update(const void * const input, const uint64_t length) {
// XXH_errorcode ret = XXH3_64bits_update(x, input, length);
// if(ret == XXH_ERROR) throw std::runtime_error("error in hashing function");
// // std::cout << digest() << std::endl;
// }
// uint32_t digest() {
// return XXH3_64bits_digest(x) & 0xffffffff;
// }
// };
#define XXH_SEED 12345
struct xxhash_env {
XXH32_state_s* x;
xxhash_env() : x(XXH32_createState()) {
XXH_errorcode ret = XXH32_reset(x, XXH_SEED);
if(ret == XXH_ERROR) throw std::runtime_error("error in hashing function");
}
~xxhash_env() {
XXH32_freeState(x);
}
void reset() {
XXH_errorcode ret = XXH32_reset(x, XXH_SEED);
if(ret == XXH_ERROR) throw std::runtime_error("error in hashing function");
}
void update(const void * const input, const uint64_t length) {
XXH_errorcode ret = XXH32_update(x, input, length);
if(ret == XXH_ERROR) throw std::runtime_error("error in hashing function");
// std::cout << digest() << std::endl;
}
uint32_t digest() {
return XXH32_digest(x);
}
};
struct zstd_compress_env {
// ZSTD_CCtx* zcs;
// zstd_compress_env() : zcs(ZSTD_createCCtx()) {}
// ~zstd_compress_env() {
// ZSTD_freeCCtx(zcs);
// }
uint64_t compress( void * dst, size_t dstCapacity,
const void * src, size_t srcSize,
int compressionLevel) {
// return ZSTD_compressCCtx(zcs, dst, dstCapacity, src, srcSize, compressionLevel);
uint64_t return_value = ZSTD_compress(dst, dstCapacity, src, srcSize, compressionLevel);
if(ZSTD_isError(return_value)) throw std::runtime_error("zstd compression error");
return return_value;
}
uint64_t compressBound(uint64_t srcSize) {
return ZSTD_compressBound(srcSize);
}
};
struct lz4_compress_env {
// std::vector<char> zcs;
// char* state;
// lz4_compress_env() {
// zcs = std::vector<char>(LZ4_sizeofState());
// state = zcs.data();
// }
uint64_t compress( char * dst, int dstCapacity,
const char * src, int srcSize,
int compressionLevel) {
// return LZ4_compress_fast_extState(state, reinterpret_cast<char*>(const_cast<void*>(src)),
// reinterpret_cast<char*>(const_cast<void*>(dst)),
// static_cast<int>(srcSize), static_cast<int>(dstCapacity), compressionLevel);
int return_value = LZ4_compress_fast(src, dst, srcSize, dstCapacity, compressionLevel);
if(return_value == 0) throw std::runtime_error("lz4 compression error");
return return_value;
}
uint64_t compressBound(uint64_t srcSize) {
return LZ4_compressBound(srcSize);
}
};
struct lz4hc_compress_env {
// std::vector<char> zcs;
// char* state;
// lz4hc_compress_env() {
// zcs = std::vector<char>(LZ4_sizeofStateHC());