-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipc.cpp
More file actions
1522 lines (1371 loc) · 43.9 KB
/
zipc.cpp
File metadata and controls
1522 lines (1371 loc) · 43.9 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 "zipc.h"
#include "zipc_utility.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <cstdint>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
extern "C" ssize_t copy_file_range(int fd_in, off_t* off_in, int fd_out, off_t* off_out, size_t len, unsigned int flags);
// Private definitions
enum zipc_mode
{
ZIPC_READ_ONLY,
ZIPC_WRITE_ONLY,
ZIPC_APPEND,
};
struct filenode
{
size_t size;
size_t data_offset;
uint32_t local_offset;
uint32_t crc;
};
struct zipc
{
std::string filename;
zipc_mode mode;
FILE* fp = nullptr;
std::unordered_map<std::string, filenode> files;
size_t central_dir_offset = 0;
bool central_dir_known = false;
bool map_write_active = false;
void* map_write_base = nullptr;
void* map_write_data = nullptr;
size_t map_write_length = 0;
size_t map_write_max = 0;
size_t map_write_data_offset = 0;
uint32_t map_write_local_offset = 0;
std::string map_write_path;
};
struct zipcstream
{
zipc* parent = nullptr;
FILE* stream = nullptr;
std::string path;
std::string temp_path;
size_t size = 0;
};
// Utility functions
static uint16_t read_le16(const unsigned char* ptr)
{
return (uint16_t)ptr[0] | ((uint16_t)ptr[1] << 8U);
}
static uint32_t read_le32(const unsigned char* ptr)
{
return (uint32_t)ptr[0] | ((uint32_t)ptr[1] << 8U) | ((uint32_t)ptr[2] << 16U) | ((uint32_t)ptr[3] << 24U);
}
static const uint32_t* crc32_table()
{
static uint32_t table[256];
static bool table_init = false;
if (!table_init)
{
for (uint32_t i = 0; i < 256; ++i)
{
uint32_t c = i;
for (int j = 0; j < 8; ++j)
{
if (c & 1U) c = 0xEDB88320U ^ (c >> 1U);
else c >>= 1U;
}
table[i] = c;
}
table_init = true;
}
return table;
}
static uint32_t crc32_update(uint32_t crc, const void* data, size_t size)
{
const uint32_t* table = crc32_table();
const unsigned char* p = static_cast<const unsigned char*>(data);
for (size_t i = 0; i < size; ++i)
{
crc = table[(crc ^ p[i]) & 0xFFU] ^ (crc >> 8U);
}
return crc;
}
static uint32_t crc32_calc(const void* data, size_t size)
{
uint32_t crc = 0xFFFFFFFFU;
crc = crc32_update(crc, data, size);
return crc ^ 0xFFFFFFFFU;
}
// Write all bytes even if fwrite returns short. Fail only on persistent error.
static bool write_all(FILE* fp, const void* buf, size_t size)
{
const unsigned char* p = static_cast<const unsigned char*>(buf);
size_t remaining = size;
while (remaining > 0)
{
size_t w = fwrite(p, 1, remaining, fp);
if (w == 0)
{
if (ferror(fp)) return false;
continue;
}
p += w;
remaining -= w;
}
return true;
}
// Read exactly size bytes; fail on EOF or error.
static bool read_fully(FILE* fp, void* buf, size_t size)
{
unsigned char* p = static_cast<unsigned char*>(buf);
size_t remaining = size;
while (remaining > 0)
{
size_t r = fread(p, 1, remaining, fp);
if (r == 0)
{
if (ferror(fp) || feof(fp)) return false;
continue;
}
p += r;
remaining -= r;
}
return true;
}
static bool write_empty_archive(FILE* fp)
{
static const unsigned char eocd[22] = {0x50, 0x4b, 0x05, 0x06,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
if (fseek(fp, 0, SEEK_SET) != 0) return false;
if (!write_all(fp, eocd, sizeof(eocd))) return false;
return fflush(fp) == 0;
}
static bool compute_data_offset(FILE* fp, uint32_t local_offset, size_t* data_offset)
{
if (fseek(fp, local_offset, SEEK_SET) != 0) return false;
unsigned char local[30];
if (!read_fully(fp, local, sizeof(local))) return false;
if (read_le32(local) != 0x04034b50) return false;
const uint16_t name_len = read_le16(local + 26);
const uint16_t extra_len = read_le16(local + 28);
*data_offset = static_cast<size_t>(local_offset) + 30U + name_len + extra_len;
return true;
}
static enum zipc_status write_central_directory(zipc* handle)
{
assert(handle);
if (!handle->fp) return ZIPC_IO_FAILURE;
std::vector<std::string> names;
names.reserve(handle->files.size());
for (const auto& kv : handle->files) names.push_back(kv.first);
std::sort(names.begin(), names.end());
const long cd_start_long = ftell(handle->fp);
if (cd_start_long < 0) return ZIPC_IO_FAILURE;
if (cd_start_long > 0xFFFFFFFFL) return ZIPC_UNSUPPORTED_FEATURE;
const uint32_t cd_start = (uint32_t)cd_start_long;
for (const auto& name : names)
{
const filenode& n = handle->files.at(name);
if (n.size > 0xFFFFFFFFULL) return ZIPC_UNSUPPORTED_FEATURE;
if (name.size() > 0xFFFF) return ZIPC_UNSUPPORTED_FEATURE;
unsigned char cd[46] = {0};
cd[0] = 0x50; cd[1] = 0x4b; cd[2] = 0x01; cd[3] = 0x02;
cd[4] = 20; cd[5] = 0; // version made by
cd[6] = 20; cd[7] = 0; // version needed to extract
// flags: 0
// compression: 0
// mod time/date: 0
cd[16] = (unsigned char)(n.crc & 0xFF);
cd[17] = (unsigned char)((n.crc >> 8) & 0xFF);
cd[18] = (unsigned char)((n.crc >> 16) & 0xFF);
cd[19] = (unsigned char)((n.crc >> 24) & 0xFF);
const uint32_t sz32 = (uint32_t)n.size;
cd[20] = (unsigned char)(sz32 & 0xFF);
cd[21] = (unsigned char)((sz32 >> 8) & 0xFF);
cd[22] = (unsigned char)((sz32 >> 16) & 0xFF);
cd[23] = (unsigned char)((sz32 >> 24) & 0xFF);
cd[24] = cd[20];
cd[25] = cd[21];
cd[26] = cd[22];
cd[27] = cd[23];
const uint16_t nlen = (uint16_t)name.size();
cd[28] = (unsigned char)(nlen & 0xFF);
cd[29] = (unsigned char)((nlen >> 8) & 0xFF);
// extra len 0, comment len 0, disk start 0, int/ext attrs 0
const uint32_t loc = n.local_offset;
cd[42] = (unsigned char)(loc & 0xFF);
cd[43] = (unsigned char)((loc >> 8) & 0xFF);
cd[44] = (unsigned char)((loc >> 16) & 0xFF);
cd[45] = (unsigned char)((loc >> 24) & 0xFF);
if (!write_all(handle->fp, cd, sizeof(cd))) return ZIPC_IO_FAILURE;
if (!write_all(handle->fp, name.data(), nlen)) return ZIPC_IO_FAILURE;
}
const long cd_end_long = ftell(handle->fp);
if (cd_end_long < 0) return ZIPC_IO_FAILURE;
const uint32_t cd_size = (uint32_t)(cd_end_long - (long)cd_start);
if (names.size() > 0xFFFF) return ZIPC_UNSUPPORTED_FEATURE;
const uint16_t entry_count = (uint16_t)names.size();
unsigned char eocd[22] = {0};
eocd[0] = 0x50; eocd[1] = 0x4b; eocd[2] = 0x05; eocd[3] = 0x06;
// disk numbers 0
eocd[8] = (unsigned char)(entry_count & 0xFF);
eocd[9] = (unsigned char)((entry_count >> 8) & 0xFF);
eocd[10] = eocd[8];
eocd[11] = eocd[9];
eocd[12] = (unsigned char)(cd_size & 0xFF);
eocd[13] = (unsigned char)((cd_size >> 8) & 0xFF);
eocd[14] = (unsigned char)((cd_size >> 16) & 0xFF);
eocd[15] = (unsigned char)((cd_size >> 24) & 0xFF);
eocd[16] = (unsigned char)(cd_start & 0xFF);
eocd[17] = (unsigned char)((cd_start >> 8) & 0xFF);
eocd[18] = (unsigned char)((cd_start >> 16) & 0xFF);
eocd[19] = (unsigned char)((cd_start >> 24) & 0xFF);
// comment length 0
if (!write_all(handle->fp, eocd, sizeof(eocd))) return ZIPC_IO_FAILURE;
if (fflush(handle->fp) != 0) return ZIPC_IO_FAILURE;
handle->central_dir_offset = cd_start;
handle->central_dir_known = true;
return ZIPC_SUCCESS;
}
static enum zipc_status load_existing_archive(zipc* z)
{
FILE* fp = z->fp;
if (fseek(fp, 0, SEEK_END) != 0) return ZIPC_IO_FAILURE;
const long file_size = ftell(fp);
if (file_size < 0) return ZIPC_IO_FAILURE;
if (file_size == 0) return ZIPC_SUCCESS;
if (file_size < 22) return ZIPC_CORRUPT_ARCHIVE;
const size_t max_search = 0xFFFF + 22;
size_t search = static_cast<size_t>(file_size);
if (search > max_search) search = max_search;
std::vector<unsigned char> tail(search);
if (fseek(fp, file_size - static_cast<long>(search), SEEK_SET) != 0) return ZIPC_IO_FAILURE;
if (!read_fully(fp, tail.data(), search)) return ZIPC_IO_FAILURE;
ssize_t eocd_idx = -1;
for (size_t i = search - 22;; --i)
{
if (read_le32(&tail[i]) == 0x06054b50)
{
eocd_idx = static_cast<ssize_t>(i);
break;
}
if (i == 0) break;
}
if (eocd_idx < 0) return ZIPC_CORRUPT_ARCHIVE;
const long eocd_pos = file_size - static_cast<long>(search) + eocd_idx;
const unsigned char* eocd = tail.data() + eocd_idx;
const uint16_t entry_count = read_le16(eocd + 10);
const uint32_t cd_size = read_le32(eocd + 12);
const uint32_t cd_offset = read_le32(eocd + 16);
if (cd_offset + cd_size > static_cast<uint32_t>(eocd_pos)) return ZIPC_CORRUPT_ARCHIVE;
if (fseek(fp, cd_offset, SEEK_SET) != 0) return ZIPC_IO_FAILURE;
std::vector<unsigned char> header(46);
for (uint16_t i = 0; i < entry_count; ++i)
{
if (!read_fully(fp, header.data(), header.size())) return ZIPC_CORRUPT_ARCHIVE;
if (read_le32(header.data()) != 0x02014b50) return ZIPC_CORRUPT_ARCHIVE;
const uint16_t flags = read_le16(header.data() + 8);
const uint16_t method = read_le16(header.data() + 10);
const uint32_t crc = read_le32(header.data() + 16);
const uint32_t compressed_size = read_le32(header.data() + 20);
const uint32_t uncompressed_size = read_le32(header.data() + 24);
const uint16_t name_len = read_le16(header.data() + 28);
const uint16_t extra_len = read_le16(header.data() + 30);
const uint16_t comment_len = read_le16(header.data() + 32);
const uint32_t local_offset = read_le32(header.data() + 42);
if (method != 0) return ZIPC_UNSUPPORTED_FEATURE;
if (flags & 0x08) return ZIPC_UNSUPPORTED_FEATURE;
if (compressed_size != uncompressed_size) return ZIPC_UNSUPPORTED_FEATURE;
std::string name(name_len, '\0');
if (!read_fully(fp, name.data(), name_len)) return ZIPC_CORRUPT_ARCHIVE;
if (fseek(fp, extra_len + comment_len, SEEK_CUR) != 0) return ZIPC_CORRUPT_ARCHIVE;
size_t data_offset = 0;
const long return_pos = ftell(fp);
if (!compute_data_offset(fp, local_offset, &data_offset)) return ZIPC_CORRUPT_ARCHIVE;
if (fseek(fp, return_pos, SEEK_SET) != 0) return ZIPC_IO_FAILURE;
if (z->files.count(name) == 0)
{
z->files.emplace(std::move(name), filenode{static_cast<size_t>(uncompressed_size), data_offset, local_offset, crc});
}
}
z->central_dir_offset = cd_offset;
z->central_dir_known = true;
return ZIPC_SUCCESS;
}
// Implementations
const char* zipc_strerror(zipc_status err)
{
switch (err)
{
case ZIPC_SUCCESS: return "Success";
case ZIPC_SYNTAX_ERROR: return "Syntax error";
case ZIPC_PERMISSION_FAILURE: return "Permission failure";
case ZIPC_PATH_ALREADY_EXISTS: return "Path already exists";
case ZIPC_IO_FAILURE: return "I/O failure";
case ZIPC_CORRUPT_ARCHIVE: return "Corrupt archive";
case ZIPC_UNSUPPORTED_FEATURE: return "Unsupported feature";
case ZIPC_PATH_NOT_FOUND: return "Path not found";
default: return "Unknown error";
}
}
zipc* zipc_open(const char* filename, const char* mode, enum zipc_status* err)
{
if (err) *err = ZIPC_SUCCESS;
if (!filename || !mode || strlen(mode) != 1)
{
if (err) *err = ZIPC_SYNTAX_ERROR;
return nullptr;
}
zipc* z = new zipc();
z->filename = filename;
if (mode[0] == 'r') z->mode = ZIPC_READ_ONLY;
else if (mode[0] == 'w') z->mode = ZIPC_WRITE_ONLY;
else if (mode[0] == 'a') z->mode = ZIPC_APPEND;
else
{
if (err) *err = ZIPC_SYNTAX_ERROR;
delete z;
return nullptr;
}
const char* fopen_mode = (z->mode == ZIPC_READ_ONLY) ? "rb" : (z->mode == ZIPC_WRITE_ONLY ? "wb+" : "rb+");
z->fp = fopen(filename, fopen_mode);
if (!z->fp && z->mode == ZIPC_APPEND)
{
z->fp = fopen(filename, "wb+");
if (z->fp && !write_empty_archive(z->fp))
{
fclose(z->fp);
z->fp = nullptr;
}
}
if (!z->fp)
{
if (err) *err = ZIPC_PERMISSION_FAILURE;
delete z;
return nullptr;
}
if (z->mode == ZIPC_WRITE_ONLY)
{
if (!write_empty_archive(z->fp))
{
if (err) *err = ZIPC_IO_FAILURE;
fclose(z->fp);
delete z;
return nullptr;
}
z->central_dir_offset = 0;
z->central_dir_known = true;
return z;
}
const enum zipc_status status = load_existing_archive(z);
if (status != ZIPC_SUCCESS)
{
if (err) *err = status;
fclose(z->fp);
delete z;
return nullptr;
}
if (z->mode == ZIPC_APPEND) fseek(z->fp, 0, SEEK_END);
return z;
}
enum zipc_status zipc_close(zipc* handle)
{
enum zipc_status r = ZIPC_SUCCESS;
if (!handle) return r;
if (handle->map_write_active) r = ZIPC_SYNTAX_ERROR;
if (handle->fp && fclose(handle->fp) != 0) r = ZIPC_IO_FAILURE;
delete handle;
return ZIPC_SUCCESS;
}
ssize_t zipc_filesize(zipc* handle, const char* path)
{
assert(handle);
assert(path);
if (handle->files.count(path) == 0) return -1;
const auto& v = handle->files.at(path);
return v.size;
}
zipc_mapping zipc_map_read(zipc* handle, const char* path, enum zipc_status* err)
{
assert(handle);
assert(path);
zipc_mapping m{};
if (!handle->fp)
{
if (err) *err = ZIPC_IO_FAILURE;
return m;
}
if (handle->mode == ZIPC_WRITE_ONLY)
{
if (err) *err = ZIPC_PERMISSION_FAILURE;
return m;
}
auto it = handle->files.find(path);
if (it == handle->files.end())
{
if (err) *err = ZIPC_PATH_NOT_FOUND;
return m;
}
const filenode& node = it->second;
if (node.size == 0)
{
static char empty = 0;
m.data = ∅
m.size = 0;
m.map_base = nullptr;
m.map_length = 0;
if (err) *err = ZIPC_SUCCESS;
return m;
}
const long pagesize_long = sysconf(_SC_PAGESIZE);
const size_t pagesize = pagesize_long > 0 ? (size_t)pagesize_long : 4096;
const size_t page_offset = node.data_offset % pagesize;
const size_t map_offset = node.data_offset - page_offset;
const size_t map_length = node.size + page_offset;
const int fd = fileno(handle->fp);
if (fd < 0)
{
if (err) *err = ZIPC_IO_FAILURE;
return m;
}
void* base = mmap(nullptr, map_length, PROT_READ, MAP_PRIVATE, fd, (off_t)map_offset);
if (base == MAP_FAILED)
{
if (err) *err = ZIPC_IO_FAILURE;
return m;
}
void* user_ptr = static_cast<unsigned char*>(base) + page_offset;
m.data = user_ptr;
m.size = node.size;
m.map_base = base;
m.map_length = map_length;
if (err) *err = ZIPC_SUCCESS;
return m;
}
zipc_mapping zipc_map_write(zipc* handle, const char* path, enum zipc_status* err, size_t max)
{
assert(handle);
assert(path);
if (err) *err = ZIPC_SUCCESS;
zipc_mapping mapping{};
if (!handle || !path)
{
if (err) *err = ZIPC_SYNTAX_ERROR;
return mapping;
}
if (handle->mode == ZIPC_READ_ONLY)
{
if (err) *err = ZIPC_PERMISSION_FAILURE;
return mapping;
}
if (!handle->fp)
{
if (err) *err = ZIPC_IO_FAILURE;
return mapping;
}
if (handle->map_write_active)
{
if (err) *err = ZIPC_PERMISSION_FAILURE;
return mapping;
}
if (handle->files.count(path) != 0)
{
if (err) *err = ZIPC_PATH_ALREADY_EXISTS;
return mapping;
}
const size_t name_len = strlen(path);
if (name_len > 0xFFFF)
{
if (err) *err = ZIPC_UNSUPPORTED_FEATURE;
return mapping;
}
if (max > 0xFFFFFFFFULL)
{
if (err) *err = ZIPC_UNSUPPORTED_FEATURE;
return mapping;
}
if (max == 0)
{
const enum zipc_status st = zipc_write(handle, path, 0, nullptr);
if (err) *err = st;
if (st != ZIPC_SUCCESS) return mapping;
static char empty = 0;
mapping.data = ∅
mapping.size = 0;
return mapping;
}
if (handle->central_dir_known)
{
if (fseek(handle->fp, (long)handle->central_dir_offset, SEEK_SET) != 0)
{
if (err) *err = ZIPC_IO_FAILURE;
return mapping;
}
const int fd = fileno(handle->fp);
if (fd >= 0 && ftruncate(fd, (off_t)handle->central_dir_offset) != 0)
{
if (err) *err = ZIPC_IO_FAILURE;
return mapping;
}
}
else if (fseek(handle->fp, 0, SEEK_END) != 0)
{
if (err) *err = ZIPC_IO_FAILURE;
return mapping;
}
const long local_offset_long = ftell(handle->fp);
if (local_offset_long < 0 || local_offset_long > 0xFFFFFFFFL)
{
if (err) *err = ZIPC_IO_FAILURE;
return mapping;
}
const uint32_t local_offset = (uint32_t)local_offset_long;
const uint32_t data_size32 = (uint32_t)max;
unsigned char local[30] = {0};
local[0] = 0x50; local[1] = 0x4b; local[2] = 0x03; local[3] = 0x04;
local[4] = 20; // version needed to extract
local[5] = 0;
// flags: 0
// compression: 0
// mod time/date: 0
// crc: 0 for now
local[18] = (unsigned char)(data_size32 & 0xFF);
local[19] = (unsigned char)((data_size32 >> 8) & 0xFF);
local[20] = (unsigned char)((data_size32 >> 16) & 0xFF);
local[21] = (unsigned char)((data_size32 >> 24) & 0xFF);
local[22] = local[18];
local[23] = local[19];
local[24] = local[20];
local[25] = local[21];
local[26] = (unsigned char)(name_len & 0xFF);
local[27] = (unsigned char)((name_len >> 8) & 0xFF);
// extra length 0
if (!write_all(handle->fp, local, sizeof(local)))
{
if (err) *err = ZIPC_IO_FAILURE;
return mapping;
}
if (!write_all(handle->fp, path, name_len))
{
if (err) *err = ZIPC_IO_FAILURE;
return mapping;
}
const size_t data_offset = (size_t)local_offset + 30U + name_len;
const size_t end_offset = data_offset + max;
const int fd = fileno(handle->fp);
if (fd < 0 || ftruncate(fd, (off_t)end_offset) != 0)
{
if (err) *err = ZIPC_IO_FAILURE;
return mapping;
}
const long pagesize_long = sysconf(_SC_PAGESIZE);
const size_t pagesize = pagesize_long > 0 ? (size_t)pagesize_long : 4096;
const size_t page_offset = data_offset % pagesize;
const size_t map_offset = data_offset - page_offset;
const size_t map_length = max + page_offset;
void* base = mmap(nullptr, map_length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t)map_offset);
if (base == MAP_FAILED)
{
if (ftruncate(fd, (off_t)local_offset) != 0)
{
// Preserve the original mmap failure status; this is only best-effort rollback.
}
if (err) *err = ZIPC_IO_FAILURE;
return mapping;
}
void* user_ptr = static_cast<unsigned char*>(base) + page_offset;
handle->map_write_active = true;
handle->map_write_base = base;
handle->map_write_data = user_ptr;
handle->map_write_length = map_length;
handle->map_write_max = max;
handle->map_write_data_offset = data_offset;
handle->map_write_local_offset = local_offset;
handle->map_write_path = path;
if (err) *err = ZIPC_SUCCESS;
mapping.data = user_ptr;
mapping.size = max;
mapping.map_base = base;
mapping.map_length = map_length;
return mapping;
}
void zipc_unmap_read(zipc* handle, zipc_mapping mapping)
{
assert(handle);
if (!mapping.map_base || mapping.map_length == 0) return;
munmap(const_cast<void*>(mapping.map_base), mapping.map_length);
}
enum zipc_status zipc_unmap_write(zipc* handle, zipc_mapping mapping, size_t size)
{
assert(handle);
if (!handle || !mapping.data) return ZIPC_SYNTAX_ERROR;
if (!handle->map_write_active) return ZIPC_SYNTAX_ERROR;
if (mapping.data != handle->map_write_data) return ZIPC_SYNTAX_ERROR;
if (size > handle->map_write_max) return ZIPC_SYNTAX_ERROR;
if (!handle->fp) return ZIPC_IO_FAILURE;
enum zipc_status status = ZIPC_SUCCESS;
const uint32_t crc = crc32_calc(mapping.data, size);
if (munmap(handle->map_write_base, handle->map_write_length) != 0) status = ZIPC_IO_FAILURE;
const size_t end_offset = handle->map_write_data_offset + size;
if (status == ZIPC_SUCCESS && size < handle->map_write_max)
{
const int fd = fileno(handle->fp);
if (fd >= 0 && ftruncate(fd, (off_t)end_offset) != 0) status = ZIPC_IO_FAILURE;
}
if (status == ZIPC_SUCCESS &&
fseek(handle->fp, (long)handle->map_write_local_offset + 14, SEEK_SET) != 0) status = ZIPC_IO_FAILURE;
unsigned char header[12];
if (status == ZIPC_SUCCESS)
{
header[0] = (unsigned char)(crc & 0xFF);
header[1] = (unsigned char)((crc >> 8) & 0xFF);
header[2] = (unsigned char)((crc >> 16) & 0xFF);
header[3] = (unsigned char)((crc >> 24) & 0xFF);
const uint32_t size32 = (uint32_t)size;
header[4] = (unsigned char)(size32 & 0xFF);
header[5] = (unsigned char)((size32 >> 8) & 0xFF);
header[6] = (unsigned char)((size32 >> 16) & 0xFF);
header[7] = (unsigned char)((size32 >> 24) & 0xFF);
header[8] = header[4];
header[9] = header[5];
header[10] = header[6];
header[11] = header[7];
if (!write_all(handle->fp, header, sizeof(header))) status = ZIPC_IO_FAILURE;
}
if (status == ZIPC_SUCCESS &&
fseek(handle->fp, (long)end_offset, SEEK_SET) != 0) status = ZIPC_IO_FAILURE;
if (status == ZIPC_SUCCESS)
{
filenode node;
node.size = size;
node.data_offset = handle->map_write_data_offset;
node.local_offset = handle->map_write_local_offset;
node.crc = crc;
handle->files.emplace(handle->map_write_path, node);
status = write_central_directory(handle);
if (status != ZIPC_SUCCESS) handle->files.erase(handle->map_write_path);
}
handle->map_write_active = false;
handle->map_write_base = nullptr;
handle->map_write_data = nullptr;
handle->map_write_length = 0;
handle->map_write_max = 0;
handle->map_write_data_offset = 0;
handle->map_write_local_offset = 0;
handle->map_write_path.clear();
return status;
}
enum zipc_status zipc_write(zipc* handle, const char* path, size_t size, const void* ptr)
{
assert(handle);
assert(path);
if (handle->mode == ZIPC_READ_ONLY) return ZIPC_PERMISSION_FAILURE;
if (!ptr && size > 0) return ZIPC_SYNTAX_ERROR;
if (handle->files.count(path) != 0) return ZIPC_PATH_ALREADY_EXISTS;
if (!handle->fp) return ZIPC_IO_FAILURE;
if (size > 0xFFFFFFFFULL) return ZIPC_UNSUPPORTED_FEATURE;
const size_t name_len = strlen(path);
if (name_len > 0xFFFF) return ZIPC_UNSUPPORTED_FEATURE;
if (handle->central_dir_known)
{
if (fseek(handle->fp, (long)handle->central_dir_offset, SEEK_SET) != 0) return ZIPC_IO_FAILURE;
const int fd = fileno(handle->fp);
if (fd >= 0 && ftruncate(fd, (off_t)handle->central_dir_offset) != 0) return ZIPC_IO_FAILURE;
}
else
{
if (fseek(handle->fp, 0, SEEK_END) != 0) return ZIPC_IO_FAILURE;
}
const long local_offset_long = ftell(handle->fp);
if (local_offset_long < 0 || local_offset_long > 0xFFFFFFFFL) return ZIPC_IO_FAILURE;
const uint32_t local_offset = (uint32_t)local_offset_long;
const uint32_t crc = crc32_calc(ptr, size);
const uint32_t data_size32 = (uint32_t)size;
unsigned char local[30] = {0};
local[0] = 0x50; local[1] = 0x4b; local[2] = 0x03; local[3] = 0x04;
local[4] = 20; // version needed to extract
local[5] = 0;
// flags: 0
// compression: 0
// mod time/date: 0
local[14] = (unsigned char)(crc & 0xFF);
local[15] = (unsigned char)((crc >> 8) & 0xFF);
local[16] = (unsigned char)((crc >> 16) & 0xFF);
local[17] = (unsigned char)((crc >> 24) & 0xFF);
local[18] = (unsigned char)(data_size32 & 0xFF);
local[19] = (unsigned char)((data_size32 >> 8) & 0xFF);
local[20] = (unsigned char)((data_size32 >> 16) & 0xFF);
local[21] = (unsigned char)((data_size32 >> 24) & 0xFF);
local[22] = local[18];
local[23] = local[19];
local[24] = local[20];
local[25] = local[21];
local[26] = (unsigned char)(name_len & 0xFF);
local[27] = (unsigned char)((name_len >> 8) & 0xFF);
// extra length 0
if (!write_all(handle->fp, local, sizeof(local))) return ZIPC_IO_FAILURE;
if (!write_all(handle->fp, path, name_len)) return ZIPC_IO_FAILURE;
if (size > 0 && !write_all(handle->fp, ptr, size)) return ZIPC_IO_FAILURE;
filenode node;
node.size = size;
node.data_offset = (size_t)local_offset + 30U + name_len;
node.local_offset = local_offset;
node.crc = crc;
handle->files.emplace(path, node);
return write_central_directory(handle);
}
enum zipc_status zipc_read(zipc* handle, const char* path, size_t size, void* ptr)
{
assert(handle);
assert(path);
if (!handle->fp) return ZIPC_IO_FAILURE;
if (handle->files.count(path) == 0) return ZIPC_PATH_NOT_FOUND;
if (handle->mode == ZIPC_WRITE_ONLY) return ZIPC_PERMISSION_FAILURE;
if (!ptr && size > 0) return ZIPC_SYNTAX_ERROR;
const filenode& node = handle->files.at(path);
if (size > node.size) return ZIPC_SYNTAX_ERROR;
if (fseek(handle->fp, (long)node.data_offset, SEEK_SET) != 0) return ZIPC_IO_FAILURE;
if (size > 0 && !read_fully(handle->fp, ptr, size)) return ZIPC_IO_FAILURE;
return ZIPC_SUCCESS;
}
zipcstream* zipc_stream_open(zipc* handle, const char* path, const char* mode, enum zipc_status* err)
{
assert(handle);
assert(path);
if (err) *err = ZIPC_SUCCESS;
if (!handle || !path || !mode)
{
if (err) *err = ZIPC_SYNTAX_ERROR;
return nullptr;
}
if (mode[0] != '\0')
{
if (err) *err = ZIPC_UNSUPPORTED_FEATURE;
return nullptr;
}
if (handle->mode == ZIPC_READ_ONLY)
{
if (err) *err = ZIPC_PERMISSION_FAILURE;
return nullptr;
}
if (!handle->fp)
{
if (err) *err = ZIPC_IO_FAILURE;
return nullptr;
}
if (handle->files.count(path) != 0)
{
if (err) *err = ZIPC_PATH_ALREADY_EXISTS;
return nullptr;
}
const std::string& filename = handle->filename;
const size_t slash = filename.find_last_of('/');
std::string dir;
if (slash == std::string::npos)
{
dir = ".";
}
else if (slash == 0)
{
dir = "/";
}
else
{
dir = filename.substr(0, slash);
}
std::string tmpl;
if (dir == "/") tmpl = "/.zipc_tmp_XXXXXX";
else tmpl = dir + "/.zipc_tmp_XXXXXX";
std::vector<char> buf(tmpl.begin(), tmpl.end());
buf.push_back('\0');
const int fd = mkstemp(buf.data());
if (fd < 0)
{
if (err) *err = ZIPC_IO_FAILURE;
return nullptr;
}
FILE* fp = fdopen(fd, "wb+");
if (!fp)
{
close(fd);
unlink(buf.data());
if (err) *err = ZIPC_IO_FAILURE;
return nullptr;
}
zipcstream* c = new zipcstream();
c->parent = handle;
c->stream = fp;
c->path = path;
c->temp_path = buf.data();
c->size = 0;
return c;
}
enum zipc_status zipc_stream_write(const zipc* handle, zipcstream* stream, size_t size, const void* ptr)
{
assert(handle);
assert(stream);
if (!handle || !stream) return ZIPC_SYNTAX_ERROR;
if (stream->parent != handle)
{
if (stream->stream) fclose(stream->stream);
if (!stream->temp_path.empty()) unlink(stream->temp_path.c_str());
delete stream;
return ZIPC_SYNTAX_ERROR;
}
if (handle->mode == ZIPC_READ_ONLY) return ZIPC_PERMISSION_FAILURE;
if (!stream->stream) return ZIPC_IO_FAILURE;
if (!ptr && size > 0) return ZIPC_SYNTAX_ERROR;
if (stream->path.empty()) return ZIPC_SYNTAX_ERROR;
if (size == 0) return ZIPC_SUCCESS;
if (!write_all(stream->stream, ptr, size)) return ZIPC_IO_FAILURE;
stream->size += size;
return ZIPC_SUCCESS;
}
enum zipc_status zipc_stream_close(zipc* handle, zipcstream* stream)
{
assert(handle);
assert(stream);
if (!handle || !stream) return ZIPC_SYNTAX_ERROR;
if (handle->mode == ZIPC_READ_ONLY)
{
if (stream->stream) fclose(stream->stream);
if (!stream->temp_path.empty()) unlink(stream->temp_path.c_str());
delete stream;
return ZIPC_PERMISSION_FAILURE;
}
if (!handle->fp || !stream->stream)
{
if (stream->stream) fclose(stream->stream);
if (!stream->temp_path.empty()) unlink(stream->temp_path.c_str());
delete stream;
return ZIPC_IO_FAILURE;
}
if (stream->path.empty())
{
fclose(stream->stream);
if (!stream->temp_path.empty()) unlink(stream->temp_path.c_str());
delete stream;
return ZIPC_SYNTAX_ERROR;
}
if (handle->files.count(stream->path) != 0)
{
fclose(stream->stream);
if (!stream->temp_path.empty()) unlink(stream->temp_path.c_str());
delete stream;
return ZIPC_PATH_ALREADY_EXISTS;
}
if (fflush(stream->stream) != 0)
{
fclose(stream->stream);
if (!stream->temp_path.empty()) unlink(stream->temp_path.c_str());
delete stream;
return ZIPC_IO_FAILURE;
}
if (fseek(stream->stream, 0, SEEK_END) != 0)
{
fclose(stream->stream);
if (!stream->temp_path.empty()) unlink(stream->temp_path.c_str());
delete stream;
return ZIPC_IO_FAILURE;
}
const long size_long = ftell(stream->stream);
if (size_long < 0)
{
fclose(stream->stream);
if (!stream->temp_path.empty()) unlink(stream->temp_path.c_str());
delete stream;
return ZIPC_IO_FAILURE;
}
const size_t data_size = (size_t)size_long;
if (stream->size != data_size)
{
fclose(stream->stream);
if (!stream->temp_path.empty()) unlink(stream->temp_path.c_str());
delete stream;
return ZIPC_IO_FAILURE;
}
if (data_size > 0xFFFFFFFFULL)
{
fclose(stream->stream);
if (!stream->temp_path.empty()) unlink(stream->temp_path.c_str());
delete stream;
return ZIPC_UNSUPPORTED_FEATURE;
}
const size_t name_len = stream->path.size();
if (name_len > 0xFFFF)
{