112
static void load_zindex(RAZF *rz, knetFile *fp){
110
114
static void load_zindex(RAZF *rz, int fd){
113
118
if(!rz->load_index) return;
114
119
if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
115
120
is_be = is_big_endian();
122
knet_read(fp, &rz->index->size, sizeof(int));
116
124
read(fd, &rz->index->size, sizeof(int));
117
126
if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
118
127
rz->index->cap = rz->index->size;
119
128
v32 = rz->index->size / RZ_BIN_SIZE + 1;
120
129
rz->index->bin_offsets = malloc(sizeof(int64_t) * v32);
131
knet_read(fp, rz->index->bin_offsets, sizeof(int64_t) * v32);
121
133
read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
122
135
rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size);
137
knet_read(fp, rz->index->cell_offsets, sizeof(int) * rz->index->size);
123
139
read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size);
125
142
for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
126
143
for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
142
159
rz = calloc(1, sizeof(RAZF));
144
164
rz->filedes = fd;
145
166
rz->stream = calloc(sizeof(z_stream), 1);
146
167
rz->inbuf = malloc(RZ_BUFFER_SIZE);
147
168
rz->outbuf = malloc(RZ_BUFFER_SIZE);
176
197
deflate(rz->stream, Z_NO_FLUSH);
177
198
rz->out += tout - rz->stream->avail_out;
178
199
if(rz->stream->avail_out) break;
201
write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
179
203
write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
180
205
rz->stream->avail_out = RZ_BUFFER_SIZE;
181
206
rz->stream->next_out = rz->outbuf;
182
207
if(rz->stream->avail_in == 0) break;
192
217
rz->buf_off = rz->buf_len = 0;
194
219
if(rz->stream->avail_out){
221
write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
195
223
write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
196
225
rz->stream->avail_out = RZ_BUFFER_SIZE;
197
226
rz->stream->next_out = rz->outbuf;
201
230
deflate(rz->stream, Z_FULL_FLUSH);
202
231
rz->out += tout - rz->stream->avail_out;
203
232
if(rz->stream->avail_out == 0){
234
write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
204
236
write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
205
238
rz->stream->avail_out = RZ_BUFFER_SIZE;
206
239
rz->stream->next_out = rz->outbuf;
221
254
deflate(rz->stream, Z_FINISH);
222
255
rz->out += tout - rz->stream->avail_out;
223
256
if(rz->stream->avail_out < RZ_BUFFER_SIZE){
258
write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
224
260
write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
225
262
rz->stream->avail_out = RZ_BUFFER_SIZE;
226
263
rz->stream->next_out = rz->outbuf;
349
static RAZF* razf_open_r(knetFile *fp, int _load_index){
311
351
static RAZF* razf_open_r(int fd, int _load_index){
313
354
int ext_off, ext_len;
314
355
int n, is_be, ret;
316
357
unsigned char c[] = "RAZF";
318
setmode(fd, O_BINARY);
320
358
rz = calloc(1, sizeof(RAZF));
364
setmode(fd, O_BINARY);
322
366
rz->filedes = fd;
323
368
rz->stream = calloc(sizeof(z_stream), 1);
324
369
rz->inbuf = malloc(RZ_BUFFER_SIZE);
325
370
rz->outbuf = malloc(RZ_BUFFER_SIZE);
326
371
rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
373
n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
327
375
n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
328
377
ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len);
356
405
rz->load_index = _load_index;
357
406
rz->file_type = FILE_TYPE_RZ;
408
if(knet_seek(fp, -16, SEEK_END) == -1){
358
410
if(lseek(fd, -16, SEEK_END) == -1){
360
413
rz->seekable = 0;
361
414
rz->index = NULL;
364
417
is_be = is_big_endian();
365
418
rz->seekable = 1;
420
knet_read(fp, &end, sizeof(int64_t));
366
422
read(fd, &end, sizeof(int64_t));
367
424
if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end);
368
425
else rz->src_end = end;
428
knet_read(fp, &end, sizeof(int64_t));
369
430
read(fd, &end, sizeof(int64_t));
370
432
if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end);
371
433
else rz->end = end;
376
438
if(rz->end > rz->src_end){
440
knet_seek(fp, rz->in, SEEK_SET);
377
442
lseek(fd, rz->in, SEEK_SET);
447
knet_seek(fp, rz->end, SEEK_SET);
448
if(knet_tell(fp) != rz->end){
449
knet_seek(fp, rz->in, SEEK_SET);
380
451
if(lseek(fd, rz->end, SEEK_SET) != rz->end){
381
452
lseek(fd, rz->in, SEEK_SET);
458
knet_seek(fp, n, SEEK_SET);
384
460
load_zindex(rz, fd);
385
461
lseek(fd, n, SEEK_SET);
468
RAZF* razf_dopen(int fd, const char *mode){
469
if (strstr(mode, "r")) fprintf(stderr,"[razf_dopen] implement me\n");
470
else if(strstr(mode, "w")) return razf_open_w(fd);
474
RAZF* razf_dopen2(int fd, const char *mode)
476
fprintf(stderr,"[razf_dopen2] implement me\n");
390
480
RAZF* razf_dopen(int fd, const char *mode){
391
481
if(strstr(mode, "r")) return razf_open_r(fd, 1);
392
482
else if(strstr(mode, "w")) return razf_open_w(fd);
399
489
else if(strstr(mode, "w")) return razf_open_w(fd);
400
490
else return NULL;
403
494
static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){
406
497
if(strstr(mode, "r")){
499
knetFile *fd = knet_open(filename, "r");
501
fprintf(stderr, "[_razf_open] fail to open %s\n", filename);
408
506
fd = open(filename, O_RDONLY | O_BINARY);
410
508
fd = open(filename, O_RDONLY);
511
if(fd < 0) return NULL;
412
512
rz = razf_open_r(fd, _load_index);
413
513
} else if(strstr(mode, "w")){
415
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
515
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
417
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
517
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
519
if(fd < 0) return NULL;
419
520
rz = razf_open_w(fd);
420
521
} else return NULL;
435
536
switch(rz->file_type){
436
537
case FILE_TYPE_PLAIN:
437
538
if(rz->end == 0x7fffffffffffffffLL){
540
if(knet_seek(rz->x.fpr, 0, SEEK_CUR) == -1) return 0;
541
n = knet_tell(rz->x.fpr);
542
knet_seek(rz->x.fpr, 0, SEEK_END);
543
rz->end = knet_tell(rz->x.fpr);
544
knet_seek(rz->x.fpr, n, SEEK_SET);
438
546
if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0;
439
547
rz->end = lseek(rz->filedes, 0, SEEK_END);
440
548
lseek(rz->filedes, n, SEEK_SET);
442
551
*u_size = *c_size = rz->end;
458
567
if(rz->z_eof || rz->z_err) return 0;
459
568
if (rz->file_type == FILE_TYPE_PLAIN) {
570
ret = knet_read(rz->x.fpr, data, size);
460
572
ret = read(rz->filedes, data, size);
461
574
if (ret == 0) rz->z_eof = 1;
467
580
if(rz->stream->avail_in == 0){
468
581
if(rz->in >= rz->end){ rz->z_eof = 1; break; }
469
582
if(rz->end - rz->in < RZ_BUFFER_SIZE){
584
rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, rz->end -rz->in);
470
586
rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in);
590
rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
472
592
rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE);
474
595
if(rz->stream->avail_in == 0){
481
602
ret = inflate(rz->stream, Z_BLOCK);
482
603
rz->in += tin - rz->stream->avail_in;
483
604
if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){
484
fprintf(stderr, "[_razf_read] inflate error: %d (at %s:%d)\n", ret, __FILE__, __LINE__);
605
fprintf(stderr, "[_razf_read] inflate error: %d %s (at %s:%d)\n", ret, rz->stream->msg ? rz->stream->msg : "", __FILE__, __LINE__);
567
688
if(rz->buf_flush) continue;
568
689
rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE);
690
if(rz->z_eof || rz->z_err) break;
571
692
rz->out += ori_size - size;
572
693
return ori_size - size;
575
696
static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){
698
knet_seek(rz->x.fpr, in, SEEK_SET);
576
700
lseek(rz->filedes, in, SEEK_SET);
579
704
rz->block_pos = in;
592
717
if(rz->file_type == FILE_TYPE_PLAIN){
593
718
rz->buf_off = rz->buf_len = 0;
594
719
pos = block_start + block_offset;
721
knet_seek(rz->x.fpr, pos, SEEK_SET);
722
pos = knet_tell(rz->x.fpr);
595
724
pos = lseek(rz->filedes, pos, SEEK_SET);
596
726
rz->out = rz->in = pos;
614
744
if (where == SEEK_CUR) pos += rz->out;
615
745
else if (where == SEEK_END) pos += rz->src_end;
616
746
if(rz->file_type == FILE_TYPE_PLAIN){
748
knet_seek(rz->x.fpr, pos, SEEK_SET);
749
seek_pos = knet_tell(rz->x.fpr);
617
751
seek_pos = lseek(rz->filedes, pos, SEEK_SET);
618
753
rz->buf_off = rz->buf_len = 0;
619
754
rz->out = rz->in = seek_pos;
663
798
#ifndef _RZ_READONLY
664
799
razf_end_flush(rz);
665
800
deflateEnd(rz->stream);
802
save_zindex(rz, rz->x.fpw);
804
write(rz->x.fpw, &rz->in, sizeof(int64_t));
805
write(rz->x.fpw, &rz->out, sizeof(int64_t));
807
uint64_t v64 = byte_swap_8((uint64_t)rz->in);
808
write(rz->x.fpw, &v64, sizeof(int64_t));
809
v64 = byte_swap_8((uint64_t)rz->out);
810
write(rz->x.fpw, &v64, sizeof(int64_t));
666
813
save_zindex(rz, rz->filedes);
667
814
if(is_big_endian()){
668
815
write(rz->filedes, &rz->in, sizeof(int64_t));