Skip to content

Commit cf5e53f

Browse files
authored
Merge pull request #4227 from facebook/doc_skippableFrame
clarify documentation for Frame-level methods invoked on a skippable frame
2 parents 788926f + f8a2b35 commit cf5e53f

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

lib/zstd.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
178178
* - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
179179
* - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small)
180180
* note 1 : a 0 return value means the frame is valid but "empty".
181+
* When invoking this method on a skippable frame, it will return 0.
181182
* note 2 : decompressed size is an optional field, it may not be present (typically in streaming mode).
182183
* When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
183184
* In which case, it's necessary to use streaming mode to decompress data.
@@ -197,22 +198,26 @@ ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
197198
#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
198199
ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);
199200

200-
/*! ZSTD_getDecompressedSize() :
201-
* NOTE: This function is now obsolete, in favor of ZSTD_getFrameContentSize().
201+
/*! ZSTD_getDecompressedSize() (obsolete):
202+
* This function is now obsolete, in favor of ZSTD_getFrameContentSize().
202203
* Both functions work the same way, but ZSTD_getDecompressedSize() blends
203204
* "empty", "unknown" and "error" results to the same return value (0),
204205
* while ZSTD_getFrameContentSize() gives them separate return values.
205206
* @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */
206207
ZSTD_DEPRECATED("Replaced by ZSTD_getFrameContentSize")
207-
ZSTDLIB_API
208-
unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
208+
ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
209209

210210
/*! ZSTD_findFrameCompressedSize() : Requires v1.4.0+
211211
* `src` should point to the start of a ZSTD frame or skippable frame.
212212
* `srcSize` must be >= first frame size
213213
* @return : the compressed size of the first frame starting at `src`,
214214
* suitable to pass as `srcSize` to `ZSTD_decompress` or similar,
215-
* or an error code if input is invalid */
215+
* or an error code if input is invalid
216+
* Note 1: this method is called _find*() because it's not enough to read the header,
217+
* it may have to scan through the frame's content, to reach its end.
218+
* Note 2: this method also works with Skippable Frames. In which case,
219+
* it returns the size of the complete skippable frame,
220+
* which is always equal to its content size + 8 bytes for headers. */
216221
ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
217222

218223

@@ -1507,7 +1512,7 @@ typedef struct {
15071512
/*! ZSTD_getFrameHeader() :
15081513
* decode Frame Header, or requires larger `srcSize`.
15091514
* @return : 0, `zfhPtr` is correctly filled,
1510-
* >0, `srcSize` is too small, value is wanted `srcSize` amount,
1515+
* >0, `srcSize` is too small, @return value is the wanted `srcSize` amount,
15111516
* or an error code, which can be tested using ZSTD_isError() */
15121517
ZSTDLIB_STATIC_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */
15131518
/*! ZSTD_getFrameHeader_advanced() :
@@ -1695,35 +1700,37 @@ ZSTD_compressSequencesAndLiterals(ZSTD_CCtx* cctx,
16951700
*
16961701
* Skippable frames begin with a 4-byte magic number. There are 16 possible choices of magic number,
16971702
* ranging from ZSTD_MAGIC_SKIPPABLE_START to ZSTD_MAGIC_SKIPPABLE_START+15.
1698-
* As such, the parameter magicVariant controls the exact skippable frame magic number variant used, so
1699-
* the magic number used will be ZSTD_MAGIC_SKIPPABLE_START + magicVariant.
1703+
* As such, the parameter magicVariant controls the exact skippable frame magic number variant used,
1704+
* so the magic number used will be ZSTD_MAGIC_SKIPPABLE_START + magicVariant.
17001705
*
17011706
* Returns an error if destination buffer is not large enough, if the source size is not representable
17021707
* with a 4-byte unsigned int, or if the parameter magicVariant is greater than 15 (and therefore invalid).
17031708
*
17041709
* @return : number of bytes written or a ZSTD error.
17051710
*/
17061711
ZSTDLIB_STATIC_API size_t ZSTD_writeSkippableFrame(void* dst, size_t dstCapacity,
1707-
const void* src, size_t srcSize, unsigned magicVariant);
1712+
const void* src, size_t srcSize,
1713+
unsigned magicVariant);
17081714

17091715
/*! ZSTD_readSkippableFrame() :
1710-
* Retrieves a zstd skippable frame containing data given by src, and writes it to dst buffer.
1716+
* Retrieves the content of a zstd skippable frame starting at @src, and writes it to @dst buffer.
17111717
*
1712-
* The parameter magicVariant will receive the magicVariant that was supplied when the frame was written,
1713-
* i.e. magicNumber - ZSTD_MAGIC_SKIPPABLE_START. This can be NULL if the caller is not interested
1714-
* in the magicVariant.
1718+
* The parameter @magicVariant will receive the magicVariant that was supplied when the frame was written,
1719+
* i.e. magicNumber - ZSTD_MAGIC_SKIPPABLE_START.
1720+
* This can be NULL if the caller is not interested in the magicVariant.
17151721
*
17161722
* Returns an error if destination buffer is not large enough, or if the frame is not skippable.
17171723
*
17181724
* @return : number of bytes written or a ZSTD error.
17191725
*/
1720-
ZSTDLIB_API size_t ZSTD_readSkippableFrame(void* dst, size_t dstCapacity, unsigned* magicVariant,
1721-
const void* src, size_t srcSize);
1726+
ZSTDLIB_STATIC_API size_t ZSTD_readSkippableFrame(void* dst, size_t dstCapacity,
1727+
unsigned* magicVariant,
1728+
const void* src, size_t srcSize);
17221729

17231730
/*! ZSTD_isSkippableFrame() :
17241731
* Tells if the content of `buffer` starts with a valid Frame Identifier for a skippable frame.
17251732
*/
1726-
ZSTDLIB_API unsigned ZSTD_isSkippableFrame(const void* buffer, size_t size);
1733+
ZSTDLIB_STATIC_API unsigned ZSTD_isSkippableFrame(const void* buffer, size_t size);
17271734

17281735

17291736

tests/fuzzer.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4110,12 +4110,18 @@ static int basicUnitTests(U32 const seed, double compressibility)
41104110
DISPLAYLEVEL(3, "OK \n");
41114111

41124112

4113-
/* findFrameCompressedSize on skippable frames */
4114-
DISPLAYLEVEL(3, "test%3i : frame compressed size of skippable frame : ", testNb++);
4115-
{ const char* frame = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde";
4116-
size_t const frameSrcSize = 13;
4117-
if (ZSTD_findFrameCompressedSize(frame, frameSrcSize) != frameSrcSize) goto _output_error; }
4118-
DISPLAYLEVEL(3, "OK \n");
4113+
/* frame operations on skippable frames */
4114+
{ const char skippableFrame[] = "\x50\x2a\x4d\x18\x05\x0\x0\0abcde";
4115+
size_t const skippableFrameSize = sizeof(skippableFrame) - 1 /* remove final /0 */;
4116+
4117+
DISPLAYLEVEL(3, "test%3i : ZSTD_findFrameCompressedSize on skippable frame : ", testNb++);
4118+
if (ZSTD_findFrameCompressedSize(skippableFrame, skippableFrameSize) != skippableFrameSize) goto _output_error;
4119+
DISPLAYLEVEL(3, "OK \n");
4120+
4121+
DISPLAYLEVEL(3, "test%3i : ZSTD_getFrameContentSize on skippable frame : ", testNb++);
4122+
if (ZSTD_getFrameContentSize(skippableFrame, skippableFrameSize) != 0) goto _output_error;
4123+
DISPLAYLEVEL(3, "OK \n");
4124+
}
41194125

41204126
/* error string tests */
41214127
DISPLAYLEVEL(3, "test%3i : testing ZSTD error code strings : ", testNb++);

0 commit comments

Comments
 (0)