-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzstd-seek.c
655 lines (534 loc) · 21.4 KB
/
zstd-seek.c
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
/* ******************************************************************
* libzstd-seek
* Copyright (c) 2020, Martinelli Marco
*
* You can contact the author at :
* - Source repository : https://github.com/martinellimarco/libzstd-seek
*
* This source code is licensed under the GPLv3 (found in the LICENSE
* file in the root directory of this source tree).
****************************************************************** */
#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <string.h>
#include "zstd-seek.h"
#ifdef _WIN32
#include "windows-mmap.h"
#else
#include <unistd.h>
#include <sys/mman.h>
#endif
typedef struct {
size_t compressedOffset; //how may bytes to skip from the beginning of the compressed stream (skip to target frame)
size_t uncompressedOffset; //how many bytes skip from the beginning of the uncompressed frame (move inside target frame)
ZSTDSeek_JumpTableRecord jtr; //copy of the jump table record that was used to calculate this jump coordinate
} ZSTDSeek_JumpCoordinate;
struct ZSTDSeek_Context_s{
ZSTD_DCtx* dctx;
void *buff; //the start of the buffer with the zstd frame(s)
size_t size; //the length of buff
size_t lastFrameCompressedSize; //the size of the last frame processed by read
size_t currentUncompressedPos; //the current position in the uncompressed file, returned by tell
size_t currentCompressedPos; //the position in the compressed file, returned by compressedTell
ZSTDSeek_JumpTable* jt;
int jumpTableFullyInitialized;
ZSTDSeek_JumpCoordinate jc;
size_t tmpOutBuffSize;
uint8_t* tmpOutBuff;
size_t tmpOutBuffPos; //the position where we read so far in the tmpOutBuff. if tmpOutBuffPos < output.pos we have data left in this buffer to read before we move on to uncompress more data
int mmap_fd; //the file descriptor of the memory map, used only if the context is created with ZSTDSeek_createFromFile or ZSTDSeek_createFromFileWithoutJumpTable
int close_fd; //1 if we own the mmap_fd and we are responsible of closing it, 0 otherwise
uint8_t* inBuff; //it's a pointer to something inside buff
ZSTD_inBuffer input;
ZSTD_outBuffer output;
};
/* Jump Table API */
ZSTDSeek_JumpTable* ZSTDSeek_getJumpTableOfContext(ZSTDSeek_Context *sctx){
if(!sctx){
return NULL;
}
return sctx->jt;
}
ZSTDSeek_JumpTable* ZSTDSeek_newJumpTable(){
ZSTDSeek_JumpTable *jt = malloc(sizeof(ZSTDSeek_JumpTable));
jt->records = malloc(sizeof(ZSTDSeek_JumpTableRecord));
jt->length = 0;
jt->capacity = 1;
return jt;
}
void ZSTDSeek_freeJumpTable(ZSTDSeek_JumpTable* jt){
if(!jt){
DEBUG("Invalid argument");
return;
}
free(jt->records);
free(jt);
}
void ZSTDSeek_addJumpTableRecord(ZSTDSeek_JumpTable* jt, size_t compressedPos, size_t uncompressedPos){
if(!jt){
DEBUG("Invalid argument");
return;
}
if(jt->length == jt->capacity){
if(jt->capacity == UINT64_MAX){
DEBUG("Jump Table: Maximum capacity reached\n");
return;
}else if(jt->capacity < (UINT64_MAX>>1)){
jt->capacity *= 2;
}else{
jt->capacity = UINT64_MAX;
}
jt->records = realloc(jt->records, jt->capacity*sizeof(ZSTDSeek_JumpTableRecord));
}
jt->records[jt->length++] = (ZSTDSeek_JumpTableRecord){
compressedPos,
uncompressedPos
};
}
int ZSTDSeek_initializeJumpTable(ZSTDSeek_Context *sctx){
return ZSTDSeek_initializeJumpTableUpUntilPos(sctx, SIZE_MAX);
}
bool ZSTDSeek_isLittleEndian(){
volatile int x = 1;
return *(char*)(&x) == 1;
}
uint32_t ZSTDSeek_fromLE32(uint32_t data){
if(ZSTDSeek_isLittleEndian()){
return data;
}else{
uint32_t swap = ((data & 0xFF000000) >> 24) |
((data & 0x00FF0000) >> 8) |
((data & 0x0000FF00) << 8) |
((data & 0x000000FF) << 24);
return swap;
}
}
int ZSTDSeek_initializeJumpTableUpUntilPos(ZSTDSeek_Context *sctx, size_t upUntilPos){
if(!sctx){
DEBUG("ZSTDSeek_Context is NULL\n");
return -1;
}
void *buff = sctx->buff;
size_t size = sctx->size;
uint8_t *footer = (uint8_t *)buff + (size - ZSTD_SEEK_TABLE_FOOTER_SIZE);
uint32_t magicnumber = ZSTDSeek_fromLE32(*((uint32_t *)(footer + 5)));
if(magicnumber == ZSTD_SEEKABLE_MAGICNUMBER){
DEBUG("Seektable detected\n");
uint8_t sfd = *((uint8_t*)(footer + 4));
uint8_t checksumFlag = sfd >> 7;
/* check reserved bits */
if((sfd >> 2) & 0x1f){
DEBUG("Last frame checksumFlag= %x: Bits 3-7 should be zero. Ignoring malformed seektable.\n",(uint32_t)sfd);
}else{
uint32_t const numFrames = ZSTDSeek_fromLE32(*((uint32_t *)footer));
uint32_t const sizePerEntry = 8 + (checksumFlag ? 4 : 0);
uint32_t const tableSize = sizePerEntry * numFrames;
uint32_t const frameSize = tableSize + ZSTD_SEEK_TABLE_FOOTER_SIZE + ZSTD_SKIPPABLE_HEADER_SIZE;
uint8_t *frame = (uint8_t *)buff + (size - frameSize);
uint32_t skippableHeader = ZSTDSeek_fromLE32(*((uint32_t *)frame));
if(skippableHeader != (ZSTD_MAGIC_SKIPPABLE_START|0xE)){
DEBUG("Last frame Header = %u does not match magic number %u. Ignoring malformed seektable.\n", skippableHeader, (ZSTD_MAGIC_SKIPPABLE_START|0xE));
}else{
uint32_t _frameSize = ZSTDSeek_fromLE32(*((uint32_t *)(frame + 4)));
if(_frameSize + ZSTD_SKIPPABLE_HEADER_SIZE != frameSize){
DEBUG("Last frame size = %u does not match expected size = %u. Ignoring malformed seektable.\n", _frameSize + ZSTD_SKIPPABLE_HEADER_SIZE, frameSize);
}else{
uint8_t *table = frame + ZSTD_SKIPPABLE_HEADER_SIZE;
size_t cOffset = 0;
size_t dOffset = 0;
for(uint32_t i = 0; i < numFrames; i++){
ZSTDSeek_addJumpTableRecord(sctx->jt, cOffset, dOffset);
cOffset += ZSTDSeek_fromLE32(*((uint32_t *)(table + (i * sizePerEntry))));
dOffset += ZSTDSeek_fromLE32(*((uint32_t *)(table + (i * sizePerEntry) + 4)));
}
ZSTDSeek_addJumpTableRecord(sctx->jt, cOffset, dOffset);
sctx->jumpTableFullyInitialized = 1;
return 0;
}
}
}
}
size_t frameCompressedSize;
size_t compressedPos = 0;
size_t uncompressedPos = 0;
if(sctx->jt->length > 0){
compressedPos = sctx->jt->records[sctx->jt->length-1].compressedPos;
uncompressedPos = sctx->jt->records[sctx->jt->length-1].uncompressedPos;
}
buff = (uint8_t *)sctx->buff + compressedPos;
sctx->jumpTableFullyInitialized = 1;
while ((frameCompressedSize = ZSTD_findFrameCompressedSize(buff, size))>0 && !ZSTD_isError(frameCompressedSize)) {
uint32_t const magic = ZSTDSeek_fromLE32(*((uint32_t *)buff));
if((magic & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START){
compressedPos += frameCompressedSize;
buff = (uint8_t *)buff + frameCompressedSize;
continue;
}
if(sctx->jt->length == 0 || sctx->jt->records[sctx->jt->length-1].uncompressedPos < uncompressedPos){
ZSTDSeek_addJumpTableRecord(sctx->jt, compressedPos, uncompressedPos);
}
size_t frameContentSize = ZSTD_getFrameContentSize(buff, size);
if(ZSTD_isError(frameContentSize)){//true if the uncompressed size is not known
frameContentSize = 0;
ZSTD_DCtx *dctx = ZSTD_createDCtx();
size_t const buffOutSize = ZSTD_DStreamOutSize();
void* const buffOut = malloc(buffOutSize);
size_t lastRet = 0;
void *buffIn = buff;
ZSTD_inBuffer input = { buffIn, frameCompressedSize, 0 };
while (input.pos < input.size) {
ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
lastRet = ZSTD_decompressStream(dctx, &output , &input);
if(ZSTD_isError(lastRet)){
DEBUG("Error decompressing: %s\n", ZSTD_getErrorName(lastRet));
ZSTD_freeDCtx(dctx);
free(buffOut);
return -1;
}
frameContentSize += output.pos;
}
ZSTD_freeDCtx(dctx);
free(buffOut);
if (lastRet != 0) {
DEBUG("Unexpected EOF. Is the file truncated?\n");
return -1;
}
}
compressedPos += frameCompressedSize;
uncompressedPos += frameContentSize;
buff = (uint8_t *)buff + frameCompressedSize;
if(uncompressedPos >= upUntilPos){
sctx->jumpTableFullyInitialized = 0;
break;
}
}
if(sctx->jt->length > 0){
if(sctx->jt->records[sctx->jt->length-1].uncompressedPos < uncompressedPos){
ZSTDSeek_addJumpTableRecord(sctx->jt, compressedPos, uncompressedPos);
}
return 0;
}else{ //0 frames found
DEBUG("No frames\n");
return -1;
}
}
int ZSTDSeek_jumpTableIsInitialized(ZSTDSeek_Context *sctx){
return sctx->jumpTableFullyInitialized;
}
ZSTDSeek_JumpCoordinate ZSTDSeek_getJumpCoordinate(ZSTDSeek_Context *sctx, size_t uncompressedPos) {
if(!sctx->jumpTableFullyInitialized && (sctx->jt->length == 0 || sctx->jt->records[sctx->jt->length-1].uncompressedPos <= uncompressedPos)){
ZSTDSeek_initializeJumpTableUpUntilPos(sctx, uncompressedPos);
}
//search for the greater value of m where sctx->jt->records[m].uncompressedPos <= uncompressedPos
uint64_t l = 0;
uint64_t r = sctx->jt->length - 1;
while(l <= r){
uint64_t m = (l+r)/2;
if(sctx->jt->records[m].uncompressedPos > uncompressedPos){
r = m-1;
}else if((m+1) < sctx->jt->length && sctx->jt->records[m+1].uncompressedPos <= uncompressedPos){
l = m+1;
}else{
return (ZSTDSeek_JumpCoordinate){sctx->jt->records[m].compressedPos, uncompressedPos - sctx->jt->records[m].uncompressedPos, sctx->jt->records[m]};
}
}
/*
//old linear search
for(uint64_t i = sctx->jt->length - 1; i >= 0; i--){
if(sctx->jt->records[i].uncompressedPos <= uncompressedPos){
return (ZSTDSeek_JumpCoordinate){sctx->jt->records[i].compressedPos, uncompressedPos - sctx->jt->records[i].uncompressedPos, sctx->jt->records[i]};
}
}
*/
return (ZSTDSeek_JumpCoordinate){0, uncompressedPos, (ZSTDSeek_JumpTableRecord){0, 0}};
}
/* Seek API */
ZSTDSeek_Context* ZSTDSeek_createFromFileWithoutJumpTable(const char* file){
struct stat st;
stat(file, &st);
int fd = open(file, O_RDONLY, 0);
if(fd < 0){
DEBUG("Unable to open '%s'\n", file);
return NULL;
}
void* const buff = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if(buff == MAP_FAILED){
DEBUG("Unable to mmap '%s'\n", file);
return NULL;
}
ZSTDSeek_Context *sctx = ZSTDSeek_createWithoutJumpTable(buff, st.st_size);
if(sctx){
sctx->mmap_fd = fd;
sctx->close_fd = 1;
return sctx;
}else{
munmap(buff, st.st_size);
close(fd);
return NULL;
}
}
ZSTDSeek_Context* ZSTDSeek_createFromFile(const char* file){
ZSTDSeek_Context* sctx = ZSTDSeek_createFromFileWithoutJumpTable(file);
if(sctx && ZSTDSeek_initializeJumpTable(sctx)!=0){
DEBUG("Can't initialize the jump table\n");
ZSTDSeek_free(sctx);
return NULL;
}
return sctx;
}
ZSTDSeek_Context* ZSTDSeek_createFromFileDescriptorWithoutJumpTable(int fd){
size_t size = lseek(fd,0L,SEEK_END);
void* const buff = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
if(buff == MAP_FAILED){
DEBUG("Unable to mmap file descriptor %d\n", fd);
return NULL;
}
ZSTDSeek_Context *sctx = ZSTDSeek_createWithoutJumpTable(buff, size);
if(sctx){
sctx->mmap_fd = fd;
sctx->close_fd = 0;
return sctx;
}else{
munmap(buff, size);
close(fd);
return NULL;
}
}
ZSTDSeek_Context* ZSTDSeek_createFromFileDescriptor(int fd){
ZSTDSeek_Context* sctx = ZSTDSeek_createFromFileDescriptorWithoutJumpTable(fd);
if(sctx && ZSTDSeek_initializeJumpTable(sctx)!=0){
DEBUG("Can't initialize the jump table\n");
ZSTDSeek_free(sctx);
return NULL;
}
return sctx;
}
ZSTDSeek_Context* ZSTDSeek_createWithoutJumpTable(void *buff, size_t size){
ZSTD_DCtx *dctx = ZSTD_createDCtx();
ZSTDSeek_Context* sctx = malloc(sizeof(ZSTDSeek_Context));
sctx->dctx = dctx;
sctx->buff = buff;
sctx->size = size;
sctx->inBuff = (uint8_t*)buff;
sctx->currentUncompressedPos = 0;
sctx->currentCompressedPos = 0;
sctx->lastFrameCompressedSize = 0;
sctx->jc = (ZSTDSeek_JumpCoordinate){0,0};
sctx->tmpOutBuffSize = ZSTD_DStreamOutSize();
sctx->tmpOutBuff = (uint8_t*)malloc(sctx->tmpOutBuffSize);
sctx->tmpOutBuffPos = 0;
sctx->mmap_fd = -1;
sctx->close_fd = 0;
sctx->input = (ZSTD_inBuffer){sctx->inBuff, 0, 0};
sctx->output = (ZSTD_outBuffer){sctx->tmpOutBuff, 0, 0};
sctx->jt = ZSTDSeek_newJumpTable();
sctx->jumpTableFullyInitialized = 0;
//test if the buffer starts with a valid frame
if(ZSTD_isError(ZSTD_findFrameCompressedSize(sctx->buff, sctx->size))){
DEBUG("Invalid format\n");
ZSTDSeek_free(sctx);
return NULL;
}
return sctx;
}
ZSTDSeek_Context* ZSTDSeek_create(void *buff, size_t size){
ZSTDSeek_Context* sctx = ZSTDSeek_createWithoutJumpTable(buff, size);
if(sctx && ZSTDSeek_initializeJumpTable(sctx)!=0){
DEBUG("Can't initialize the jump table\n");
ZSTDSeek_free(sctx);
return NULL;
}
return sctx;
}
size_t ZSTDSeek_read(void *outBuff, size_t outBuffSize, ZSTDSeek_Context *sctx){
if(!sctx){
DEBUG("ZSTDSeek_Context is NULL\n");
return 0;
}
ZSTDSeek_JumpCoordinate localJc = ZSTDSeek_getJumpCoordinate(sctx, sctx->currentUncompressedPos); //trigger the generation of a jump table record, if needed
sctx->currentCompressedPos = localJc.jtr.compressedPos;
size_t maxReadable = ZSTDSeek_lastKnownUncompressedFileSize(sctx) - sctx->currentUncompressedPos;
size_t toRead = maxReadable < outBuffSize ? maxReadable : outBuffSize;
size_t shouldRead = toRead;
if(sctx->tmpOutBuffPos < sctx->output.pos){
if(sctx->jc.uncompressedOffset > sctx->output.pos){
sctx->jc.uncompressedOffset -= sctx->output.pos;
}else{
size_t maxCopy = (sctx->output.pos - sctx->tmpOutBuffPos) - sctx->jc.uncompressedOffset;
size_t toCopy = maxCopy < toRead ? maxCopy : toRead;
memcpy(outBuff, sctx->tmpOutBuff+sctx->tmpOutBuffPos+sctx->jc.uncompressedOffset, toCopy);
toRead -= toCopy;
outBuff = (uint8_t *)outBuff + toCopy;
sctx->currentUncompressedPos += toCopy;
sctx->tmpOutBuffPos += toCopy + sctx->jc.uncompressedOffset;
sctx->jc.uncompressedOffset = 0;
}
}
while (toRead > 0 && ((sctx->input.pos < sctx->input.size) || (sctx->lastFrameCompressedSize = ZSTD_findFrameCompressedSize(sctx->inBuff, sctx->size)) > 0)){
if(sctx->input.pos == sctx->input.size){
sctx->input = (ZSTD_inBuffer){sctx->inBuff, sctx->lastFrameCompressedSize, 0};
}
while (sctx->input.pos < sctx->input.size) {
sctx->output = (ZSTD_outBuffer){ sctx->tmpOutBuff, sctx->tmpOutBuffSize, 0 };
sctx->tmpOutBuffPos = 0;
size_t const ret = ZSTD_decompressStream(sctx->dctx, &sctx->output , &sctx->input);
if(ZSTD_isError(ret)){
DEBUG("Error decompressing: %s\n", ZSTD_getErrorName(ret));
return ZSTDSEEK_ERR_READ;
}
sctx->currentCompressedPos += sctx->input.pos;
if(sctx->jc.uncompressedOffset > sctx->output.pos){
sctx->jc.uncompressedOffset -= sctx->output.pos;
}else{
size_t maxCopy = (sctx->output.pos - sctx->tmpOutBuffPos) - sctx->jc.uncompressedOffset;
size_t toCopy = maxCopy < toRead ? maxCopy : toRead;
memcpy(outBuff, sctx->tmpOutBuff+sctx->tmpOutBuffPos+sctx->jc.uncompressedOffset, toCopy);
toRead -= toCopy;
outBuff = (uint8_t *)outBuff + toCopy;
sctx->currentUncompressedPos += toCopy;
sctx->tmpOutBuffPos += toCopy + sctx->jc.uncompressedOffset;
sctx->jc.uncompressedOffset = 0;
}
if(toRead == 0){
break;
}
}
if(sctx->input.pos == sctx->input.size){ //end of frame
sctx->inBuff+=sctx->lastFrameCompressedSize;
}
if(toRead == 0){
break;
}
}
return shouldRead - toRead;
}
int ZSTDSeek_seek(ZSTDSeek_Context *sctx, long offset, int origin){
if(!sctx){
DEBUG("ZSTDSeek_Context is NULL\n");
return -1;
}
if(origin == SEEK_CUR){
if(offset==0){
return 0;
}
offset = (long)sctx->currentUncompressedPos + offset;
origin = SEEK_SET;
}else if(origin == SEEK_END){
offset = (long)ZSTDSeek_uncompressedFileSize(sctx) + offset;
origin = SEEK_SET;
}
if(origin == SEEK_SET){
if(offset < 0){
DEBUG("Negative seek\n");
return ZSTDSEEK_ERR_NEGATIVE_SEEK;
}else if(offset > 0){
ZSTDSeek_getJumpCoordinate(sctx, sctx->currentUncompressedPos+offset); //trigger an update of the lastKnownUncompressedFileSize
if(offset > ZSTDSeek_lastKnownUncompressedFileSize(sctx)){
DEBUG("Seek to a frame beyond the buffer length\n");
return ZSTDSEEK_ERR_BEYOND_END_SEEK;
}
}
if(offset == sctx->currentUncompressedPos){ //we are already there, do nothing
return 0;
}
ZSTDSeek_JumpCoordinate new_jc = ZSTDSeek_getJumpCoordinate(sctx, offset);
if(sctx->jc.compressedOffset != new_jc.compressedOffset || offset < sctx->currentUncompressedPos){ //reset
ZSTD_DCtx_reset(sctx->dctx, ZSTD_reset_session_only);
sctx->jc = new_jc;
sctx->inBuff = (uint8_t *)sctx->buff + sctx->jc.compressedOffset; //jump to the beginning of the frame..
sctx->currentUncompressedPos = offset; //..and adjust the uncompressed position..
sctx->currentCompressedPos = sctx->jc.compressedOffset;
sctx->tmpOutBuffPos = 0; //..and reset the position in the tmp buffer
sctx->input = (ZSTD_inBuffer){sctx->inBuff, 0, 0};
sctx->output = (ZSTD_outBuffer){sctx->tmpOutBuff, 0, 0};
}else{ //move forward
size_t toSkipTotal = offset - sctx->currentUncompressedPos;
size_t const buffOutSize = ZSTD_DStreamOutSize();
void* const buffOut = malloc(buffOutSize);
while(toSkipTotal>0){
size_t toSkip = buffOutSize < toSkipTotal ? buffOutSize : toSkipTotal;
toSkipTotal -= ZSTDSeek_read(buffOut, toSkip, sctx);
}
free(buffOut);
}
}else{
DEBUG("Invalid origin\n");
return -1;
}
return 0;
}
long ZSTDSeek_tell(ZSTDSeek_Context *sctx){
if(!sctx){
DEBUG("ZSTDSeek_Context is NULL\n");
return -1;
}
return sctx->currentUncompressedPos;
}
long ZSTDSeek_compressedTell(ZSTDSeek_Context *sctx){
if(!sctx){
DEBUG("ZSTDSeek_Context is NULL\n");
return -1;
}
return sctx->currentCompressedPos;
}
size_t ZSTDSeek_uncompressedFileSize(ZSTDSeek_Context *sctx){
if(!sctx){
DEBUG("ZSTDSeek_Context is NULL\n");
return 0;
}
ZSTDSeek_initializeJumpTable(sctx);
return ZSTDSeek_lastKnownUncompressedFileSize(sctx);
}
size_t ZSTDSeek_lastKnownUncompressedFileSize(ZSTDSeek_Context *sctx){
if(!sctx){
DEBUG("ZSTDSeek_Context is NULL\n");
return 0;
}
return sctx->jt->length > 0 ? sctx->jt->records[sctx->jt->length-1].uncompressedPos : 0;
}
int ZSTDSeek_fileno(ZSTDSeek_Context *sctx){
return sctx->mmap_fd;
}
size_t ZSTDSeek_countFramesUpTo(ZSTDSeek_Context *sctx, size_t upTo){
if(!sctx){
DEBUG("ZSTDSeek_Context is NULL\n");
return 0;
}
size_t frameCompressedSize;
void *buff = sctx->buff;
size_t size = sctx->size;
size_t counter = 0;
while ((frameCompressedSize = ZSTD_findFrameCompressedSize(buff, size))>0 && !ZSTD_isError(frameCompressedSize)) {
counter++;
buff = (uint8_t *)buff + frameCompressedSize;
if(counter >= upTo){
return upTo;
}
}
return counter;
}
size_t ZSTDSeek_getNumberOfFrames(ZSTDSeek_Context *sctx){
return ZSTDSeek_countFramesUpTo(sctx, SIZE_MAX);
}
int ZSTDSeek_isMultiframe(ZSTDSeek_Context *sctx){
return ZSTDSeek_countFramesUpTo(sctx, 2) > 1;
}
void ZSTDSeek_free(ZSTDSeek_Context *sctx){
if(!sctx){
DEBUG("ZSTDSeek_Context is NULL\n");
return;
}
if(sctx->dctx){
ZSTD_freeDCtx(sctx->dctx);
}
ZSTDSeek_freeJumpTable(sctx->jt);
if(sctx->mmap_fd>=0 && sctx->close_fd){
munmap(sctx->buff, sctx->size);
close(sctx->mmap_fd);
}
free(sctx->tmpOutBuff);
free(sctx);
}