]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/zstd/contrib/seekable_format/zstdseek_decompress.c
Update to Zstandard 1.3.7
[FreeBSD/FreeBSD.git] / sys / contrib / zstd / contrib / seekable_format / zstdseek_decompress.c
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10
11 /* *********************************************************
12 *  Turn on Large Files support (>4GB) for 32-bit Linux/Unix
13 ***********************************************************/
14 #if !defined(__64BIT__) || defined(__MINGW32__)       /* No point defining Large file for 64 bit but MinGW-w64 requires it */
15 #  if !defined(_FILE_OFFSET_BITS)
16 #    define _FILE_OFFSET_BITS 64                      /* turn off_t into a 64-bit type for ftello, fseeko */
17 #  endif
18 #  if !defined(_LARGEFILE_SOURCE)                     /* obsolete macro, replaced with _FILE_OFFSET_BITS */
19 #    define _LARGEFILE_SOURCE 1                       /* Large File Support extension (LFS) - fseeko, ftello */
20 #  endif
21 #  if defined(_AIX) || defined(__hpux)
22 #    define _LARGE_FILES                              /* Large file support on 32-bits AIX and HP-UX */
23 #  endif
24 #endif
25
26 /* ************************************************************
27 * Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
28 ***************************************************************/
29 #if defined(_MSC_VER) && _MSC_VER >= 1400
30 #   define LONG_SEEK _fseeki64
31 #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
32 #  define LONG_SEEK fseeko
33 #elif defined(__MINGW32__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS) && defined(__MSVCRT__)
34 #   define LONG_SEEK fseeko64
35 #elif defined(_WIN32) && !defined(__DJGPP__)
36 #   include <windows.h>
37     static int LONG_SEEK(FILE* file, __int64 offset, int origin) {
38         LARGE_INTEGER off;
39         DWORD method;
40         off.QuadPart = offset;
41         if (origin == SEEK_END)
42             method = FILE_END;
43         else if (origin == SEEK_CUR)
44             method = FILE_CURRENT;
45         else
46             method = FILE_BEGIN;
47
48         if (SetFilePointerEx((HANDLE) _get_osfhandle(_fileno(file)), off, NULL, method))
49             return 0;
50         else
51             return -1;
52     }
53 #else
54 #   define LONG_SEEK fseek
55 #endif
56
57 #include <stdlib.h> /* malloc, free */
58 #include <stdio.h>  /* FILE* */
59 #include <assert.h>
60
61 #define XXH_STATIC_LINKING_ONLY
62 #define XXH_NAMESPACE ZSTD_
63 #include "xxhash.h"
64
65 #define ZSTD_STATIC_LINKING_ONLY
66 #include "zstd.h"
67 #include "zstd_errors.h"
68 #include "mem.h"
69 #include "zstd_seekable.h"
70
71 #undef ERROR
72 #define ERROR(name) ((size_t)-ZSTD_error_##name)
73
74 #define CHECK_IO(f) { int const errcod = (f); if (errcod < 0) return ERROR(seekableIO); }
75
76 #undef MIN
77 #undef MAX
78 #define MIN(a, b) ((a) < (b) ? (a) : (b))
79 #define MAX(a, b) ((a) > (b) ? (a) : (b))
80
81 /* Special-case callbacks for FILE* and in-memory modes, so that we can treat
82  * them the same way as the advanced API */
83 static int ZSTD_seekable_read_FILE(void* opaque, void* buffer, size_t n)
84 {
85     size_t const result = fread(buffer, 1, n, (FILE*)opaque);
86     if (result != n) {
87         return -1;
88     }
89     return 0;
90 }
91
92 static int ZSTD_seekable_seek_FILE(void* opaque, long long offset, int origin)
93 {
94     int const ret = LONG_SEEK((FILE*)opaque, offset, origin);
95     if (ret) return ret;
96     return fflush((FILE*)opaque);
97 }
98
99 typedef struct {
100     const void *ptr;
101     size_t size;
102     size_t pos;
103 } buffWrapper_t;
104
105 static int ZSTD_seekable_read_buff(void* opaque, void* buffer, size_t n)
106 {
107     buffWrapper_t* buff = (buffWrapper_t*) opaque;
108     if (buff->size + n > buff->pos) return -1;
109     memcpy(buffer, (const BYTE*)buff->ptr + buff->pos, n);
110     buff->pos += n;
111     return 0;
112 }
113
114 static int ZSTD_seekable_seek_buff(void* opaque, long long offset, int origin)
115 {
116     buffWrapper_t* const buff = (buffWrapper_t*) opaque;
117     unsigned long long newOffset;
118     switch (origin) {
119     case SEEK_SET:
120         newOffset = offset;
121         break;
122     case SEEK_CUR:
123         newOffset = (unsigned long long)buff->pos + offset;
124         break;
125     case SEEK_END:
126         newOffset = (unsigned long long)buff->size - offset;
127         break;
128     default:
129         assert(0);  /* not possible */
130     }
131     if (newOffset > buff->size) {
132         return -1;
133     }
134     buff->pos = newOffset;
135     return 0;
136 }
137
138 typedef struct {
139     U64 cOffset;
140     U64 dOffset;
141     U32 checksum;
142 } seekEntry_t;
143
144 typedef struct {
145     seekEntry_t* entries;
146     size_t tableLen;
147
148     int checksumFlag;
149 } seekTable_t;
150
151 #define SEEKABLE_BUFF_SIZE ZSTD_BLOCKSIZE_MAX
152
153 struct ZSTD_seekable_s {
154     ZSTD_DStream* dstream;
155     seekTable_t seekTable;
156     ZSTD_seekable_customFile src;
157
158     U64 decompressedOffset;
159     U32 curFrame;
160
161     BYTE inBuff[SEEKABLE_BUFF_SIZE]; /* need to do our own input buffering */
162     BYTE outBuff[SEEKABLE_BUFF_SIZE]; /* so we can efficiently decompress the
163                                          starts of chunks before we get to the
164                                          desired section */
165     ZSTD_inBuffer in; /* maintain continuity across ZSTD_seekable_decompress operations */
166     buffWrapper_t buffWrapper; /* for `src.opaque` in in-memory mode */
167
168     XXH64_state_t xxhState;
169 };
170
171 ZSTD_seekable* ZSTD_seekable_create(void)
172 {
173     ZSTD_seekable* zs = malloc(sizeof(ZSTD_seekable));
174
175     if (zs == NULL) return NULL;
176
177     /* also initializes stage to zsds_init */
178     memset(zs, 0, sizeof(*zs));
179
180     zs->dstream = ZSTD_createDStream();
181     if (zs->dstream == NULL) {
182         free(zs);
183         return NULL;
184     }
185
186     return zs;
187 }
188
189 size_t ZSTD_seekable_free(ZSTD_seekable* zs)
190 {
191     if (zs == NULL) return 0; /* support free on null */
192     ZSTD_freeDStream(zs->dstream);
193     free(zs->seekTable.entries);
194     free(zs);
195
196     return 0;
197 }
198
199 /** ZSTD_seekable_offsetToFrameIndex() :
200  *  Performs a binary search to find the last frame with a decompressed offset
201  *  <= pos
202  *  @return : the frame's index */
203 U32 ZSTD_seekable_offsetToFrameIndex(ZSTD_seekable* const zs, unsigned long long pos)
204 {
205     U32 lo = 0;
206     U32 hi = zs->seekTable.tableLen;
207
208     if (pos >= zs->seekTable.entries[zs->seekTable.tableLen].dOffset) {
209         return zs->seekTable.tableLen;
210     }
211
212     while (lo + 1 < hi) {
213         U32 const mid = lo + ((hi - lo) >> 1);
214         if (zs->seekTable.entries[mid].dOffset <= pos) {
215             lo = mid;
216         } else {
217             hi = mid;
218         }
219     }
220     return lo;
221 }
222
223 U32 ZSTD_seekable_getNumFrames(ZSTD_seekable* const zs)
224 {
225     return zs->seekTable.tableLen;
226 }
227
228 unsigned long long ZSTD_seekable_getFrameCompressedOffset(ZSTD_seekable* const zs, U32 frameIndex)
229 {
230     if (frameIndex >= zs->seekTable.tableLen) return ZSTD_SEEKABLE_FRAMEINDEX_TOOLARGE;
231     return zs->seekTable.entries[frameIndex].cOffset;
232 }
233
234 unsigned long long ZSTD_seekable_getFrameDecompressedOffset(ZSTD_seekable* const zs, U32 frameIndex)
235 {
236     if (frameIndex >= zs->seekTable.tableLen) return ZSTD_SEEKABLE_FRAMEINDEX_TOOLARGE;
237     return zs->seekTable.entries[frameIndex].dOffset;
238 }
239
240 size_t ZSTD_seekable_getFrameCompressedSize(ZSTD_seekable* const zs, U32 frameIndex)
241 {
242     if (frameIndex >= zs->seekTable.tableLen) return ERROR(frameIndex_tooLarge);
243     return zs->seekTable.entries[frameIndex + 1].cOffset -
244            zs->seekTable.entries[frameIndex].cOffset;
245 }
246
247 size_t ZSTD_seekable_getFrameDecompressedSize(ZSTD_seekable* const zs, U32 frameIndex)
248 {
249     if (frameIndex > zs->seekTable.tableLen) return ERROR(frameIndex_tooLarge);
250     return zs->seekTable.entries[frameIndex + 1].dOffset -
251            zs->seekTable.entries[frameIndex].dOffset;
252 }
253
254 static size_t ZSTD_seekable_loadSeekTable(ZSTD_seekable* zs)
255 {
256     int checksumFlag;
257     ZSTD_seekable_customFile src = zs->src;
258     /* read the footer, fixed size */
259     CHECK_IO(src.seek(src.opaque, -(int)ZSTD_seekTableFooterSize, SEEK_END));
260     CHECK_IO(src.read(src.opaque, zs->inBuff, ZSTD_seekTableFooterSize));
261
262     if (MEM_readLE32(zs->inBuff + 5) != ZSTD_SEEKABLE_MAGICNUMBER) {
263         return ERROR(prefix_unknown);
264     }
265
266     {   BYTE const sfd = zs->inBuff[4];
267         checksumFlag = sfd >> 7;
268
269         /* check reserved bits */
270         if ((checksumFlag >> 2) & 0x1f) {
271             return ERROR(corruption_detected);
272         }
273     }
274
275     {   U32 const numFrames = MEM_readLE32(zs->inBuff);
276         U32 const sizePerEntry = 8 + (checksumFlag?4:0);
277         U32 const tableSize = sizePerEntry * numFrames;
278         U32 const frameSize = tableSize + ZSTD_seekTableFooterSize + ZSTD_skippableHeaderSize;
279
280         U32 remaining = frameSize - ZSTD_seekTableFooterSize; /* don't need to re-read footer */
281         {
282             U32 const toRead = MIN(remaining, SEEKABLE_BUFF_SIZE);
283
284             CHECK_IO(src.seek(src.opaque, -(S64)frameSize, SEEK_END));
285             CHECK_IO(src.read(src.opaque, zs->inBuff, toRead));
286
287             remaining -= toRead;
288         }
289
290         if (MEM_readLE32(zs->inBuff) != (ZSTD_MAGIC_SKIPPABLE_START | 0xE)) {
291             return ERROR(prefix_unknown);
292         }
293         if (MEM_readLE32(zs->inBuff+4) + ZSTD_skippableHeaderSize != frameSize) {
294             return ERROR(prefix_unknown);
295         }
296
297         {   /* Allocate an extra entry at the end so that we can do size
298              * computations on the last element without special case */
299             seekEntry_t* entries = (seekEntry_t*)malloc(sizeof(seekEntry_t) * (numFrames + 1));
300
301             U32 idx = 0;
302             U32 pos = 8;
303
304
305             U64 cOffset = 0;
306             U64 dOffset = 0;
307
308             if (!entries) {
309                 free(entries);
310                 return ERROR(memory_allocation);
311             }
312
313             /* compute cumulative positions */
314             for (; idx < numFrames; idx++) {
315                 if (pos + sizePerEntry > SEEKABLE_BUFF_SIZE) {
316                     U32 const offset = SEEKABLE_BUFF_SIZE - pos;
317                     U32 const toRead = MIN(remaining, SEEKABLE_BUFF_SIZE - offset);
318                     memmove(zs->inBuff, zs->inBuff + pos, offset); /* move any data we haven't read yet */
319                     CHECK_IO(src.read(src.opaque, zs->inBuff+offset, toRead));
320                     remaining -= toRead;
321                     pos = 0;
322                 }
323                 entries[idx].cOffset = cOffset;
324                 entries[idx].dOffset = dOffset;
325
326                 cOffset += MEM_readLE32(zs->inBuff + pos);
327                 pos += 4;
328                 dOffset += MEM_readLE32(zs->inBuff + pos);
329                 pos += 4;
330                 if (checksumFlag) {
331                     entries[idx].checksum = MEM_readLE32(zs->inBuff + pos);
332                     pos += 4;
333                 }
334             }
335             entries[numFrames].cOffset = cOffset;
336             entries[numFrames].dOffset = dOffset;
337
338             zs->seekTable.entries = entries;
339             zs->seekTable.tableLen = numFrames;
340             zs->seekTable.checksumFlag = checksumFlag;
341             return 0;
342         }
343     }
344 }
345
346 size_t ZSTD_seekable_initBuff(ZSTD_seekable* zs, const void* src, size_t srcSize)
347 {
348     zs->buffWrapper = (buffWrapper_t){src, srcSize, 0};
349     {   ZSTD_seekable_customFile srcFile = {&zs->buffWrapper,
350                                             &ZSTD_seekable_read_buff,
351                                             &ZSTD_seekable_seek_buff};
352         return ZSTD_seekable_initAdvanced(zs, srcFile); }
353 }
354
355 size_t ZSTD_seekable_initFile(ZSTD_seekable* zs, FILE* src)
356 {
357     ZSTD_seekable_customFile srcFile = {src, &ZSTD_seekable_read_FILE,
358                                         &ZSTD_seekable_seek_FILE};
359     return ZSTD_seekable_initAdvanced(zs, srcFile);
360 }
361
362 size_t ZSTD_seekable_initAdvanced(ZSTD_seekable* zs, ZSTD_seekable_customFile src)
363 {
364     zs->src = src;
365
366     {   const size_t seekTableInit = ZSTD_seekable_loadSeekTable(zs);
367         if (ZSTD_isError(seekTableInit)) return seekTableInit; }
368
369     zs->decompressedOffset = (U64)-1;
370     zs->curFrame = (U32)-1;
371
372     {   const size_t dstreamInit = ZSTD_initDStream(zs->dstream);
373         if (ZSTD_isError(dstreamInit)) return dstreamInit; }
374     return 0;
375 }
376
377 size_t ZSTD_seekable_decompress(ZSTD_seekable* zs, void* dst, size_t len, unsigned long long offset)
378 {
379     U32 targetFrame = ZSTD_seekable_offsetToFrameIndex(zs, offset);
380     do {
381         /* check if we can continue from a previous decompress job */
382         if (targetFrame != zs->curFrame || offset != zs->decompressedOffset) {
383             zs->decompressedOffset = zs->seekTable.entries[targetFrame].dOffset;
384             zs->curFrame = targetFrame;
385
386             CHECK_IO(zs->src.seek(zs->src.opaque,
387                                   zs->seekTable.entries[targetFrame].cOffset,
388                                   SEEK_SET));
389             zs->in = (ZSTD_inBuffer){zs->inBuff, 0, 0};
390             XXH64_reset(&zs->xxhState, 0);
391             ZSTD_resetDStream(zs->dstream);
392         }
393
394         while (zs->decompressedOffset < offset + len) {
395             size_t toRead;
396             ZSTD_outBuffer outTmp;
397             size_t prevOutPos;
398             if (zs->decompressedOffset < offset) {
399                 /* dummy decompressions until we get to the target offset */
400                 outTmp = (ZSTD_outBuffer){zs->outBuff, MIN(SEEKABLE_BUFF_SIZE, offset - zs->decompressedOffset), 0};
401             } else {
402                 outTmp = (ZSTD_outBuffer){dst, len, zs->decompressedOffset - offset};
403             }
404
405             prevOutPos = outTmp.pos;
406             toRead = ZSTD_decompressStream(zs->dstream, &outTmp, &zs->in);
407             if (ZSTD_isError(toRead)) {
408                 return toRead;
409             }
410
411             if (zs->seekTable.checksumFlag) {
412                 XXH64_update(&zs->xxhState, (BYTE*)outTmp.dst + prevOutPos,
413                              outTmp.pos - prevOutPos);
414             }
415             zs->decompressedOffset += outTmp.pos - prevOutPos;
416
417             if (toRead == 0) {
418                 /* frame complete */
419
420                 /* verify checksum */
421                 if (zs->seekTable.checksumFlag &&
422                     (XXH64_digest(&zs->xxhState) & 0xFFFFFFFFU) !=
423                             zs->seekTable.entries[targetFrame].checksum) {
424                     return ERROR(corruption_detected);
425                 }
426
427                 if (zs->decompressedOffset < offset + len) {
428                     /* go back to the start and force a reset of the stream */
429                     targetFrame = ZSTD_seekable_offsetToFrameIndex(zs, zs->decompressedOffset);
430                 }
431                 break;
432             }
433
434             /* read in more data if we're done with this buffer */
435             if (zs->in.pos == zs->in.size) {
436                 toRead = MIN(toRead, SEEKABLE_BUFF_SIZE);
437                 CHECK_IO(zs->src.read(zs->src.opaque, zs->inBuff, toRead));
438                 zs->in.size = toRead;
439                 zs->in.pos = 0;
440             }
441         }
442     } while (zs->decompressedOffset != offset + len);
443
444     return len;
445 }
446
447 size_t ZSTD_seekable_decompressFrame(ZSTD_seekable* zs, void* dst, size_t dstSize, U32 frameIndex)
448 {
449     if (frameIndex >= zs->seekTable.tableLen) {
450         return ERROR(frameIndex_tooLarge);
451     }
452
453     {
454         size_t const decompressedSize =
455                 zs->seekTable.entries[frameIndex + 1].dOffset -
456                 zs->seekTable.entries[frameIndex].dOffset;
457         if (dstSize < decompressedSize) {
458             return ERROR(dstSize_tooSmall);
459         }
460         return ZSTD_seekable_decompress(
461                 zs, dst, decompressedSize,
462                 zs->seekTable.entries[frameIndex].dOffset);
463     }
464 }