encode.h(3)
โจ NAME โจ
okay so this is encode.h โ the API for Brotli compression. You're reading encode.h(3), the library functions manual, and it's giving main character energy. ๐
No cap, this is the bestie guide to Brotli encoding.
๐ฆ SYNOPSIS ๐ฆ
Macros
#define BROTLI_DEFAULT_MODE BROTLI_MODE_GENERICโ the default vibe for theBROTLI_PARAM_MODEparameter. โจ#define BROTLI_DEFAULT_QUALITY 11โ the default value for theBROTLI_PARAM_QUALITYparameter. ๐#define BROTLI_DEFAULT_WINDOW 22โ the default value for theBROTLI_PARAM_LGWINparameter. ๐ธ#define BROTLI_LARGE_MAX_WINDOW_BITS 30โ the maximal value for theBROTLI_PARAM_LGWINparameter in 'Large Window Brotli' (32-bit). ๐#define BROTLI_MAX_INPUT_BLOCK_BITS 24โ the maximal value for theBROTLI_PARAM_LGBLOCKparameter. ๐ฅ#define BROTLI_MAX_QUALITY 11โ the maximal value for theBROTLI_PARAM_QUALITYparameter. ๐ฏ#define BROTLI_MAX_WINDOW_BITS 24โ the maximal value for theBROTLI_PARAM_LGWINparameter. ๐ฆ#define BROTLI_MIN_INPUT_BLOCK_BITS 16โ the minimal value for theBROTLI_PARAM_LGBLOCKparameter. ๐ฅบ#define BROTLI_MIN_QUALITY 0โ the minimal value for theBROTLI_PARAM_QUALITYparameter. ๐#define BROTLI_MIN_WINDOW_BITS 10โ the minimal value for theBROTLI_PARAM_LGWINparameter. ๐
Typedefs
typedef enum BrotliEncoderMode BrotliEncoderModeโ the options for theBROTLI_PARAM_MODEparameter. ๐typedef enum BrotliEncoderOperation BrotliEncoderOperationโ the operations that can be performed by the streaming encoder. ๐typedef enum BrotliEncoderParameter BrotliEncoderParameterโ the options to be used withBrotliEncoderSetParameter. โญtypedef struct BrotliEncoderStateStruct BrotliEncoderStateโ an opaque structure that holds the encoder state. ๐
Enumerations
(no enumerators listed here, bestie โ just vibes)
Functions
BROTLI_BOOL BrotliEncoderAttachPreparedDictionary (BrotliEncoderState *state, const BrotliEncoderPreparedDictionary *dictionary)โ attaches a prepared dictionary of any type to the encoder. ๐ซถBROTLI_BOOL BrotliEncoderCompress (int quality, int lgwin, BrotliEncoderMode mode, size_t input_size, const uint8_t input_buffer[input_size], size_t *encoded_size, uint8_t encoded_buffer[*encoded_size])โ performs one-shot memory-to-memory compression. ๐งBROTLI_BOOL BrotliEncoderCompressStream (BrotliEncoderState *state, BrotliEncoderOperation op, size_t *available_in, const uint8_t **next_in, size_t *available_out, uint8_t **next_out, size_t *total_out)โ compresses an input stream to an output stream. ๐BrotliEncoderState * BrotliEncoderCreateInstance (brotli_alloc_func alloc_func, brotli_free_func free_func, void *opaque)โ creates an instance ofBrotliEncoderStateand initializes it. โจvoid BrotliEncoderDestroyInstance (BrotliEncoderState *state)โ deinitializes and frees aBrotliEncoderStateinstance. ๐BROTLI_BOOL BrotliEncoderHasMoreOutput (BrotliEncoderState *state)โ checks if the encoder has more output. ๐BROTLI_BOOL BrotliEncoderIsFinished (BrotliEncoderState *state)โ checks if the encoder instance reached the final state. ๐size_t BrotliEncoderMaxCompressedSize (size_t input_size)โ calculates the output size bound for the giveninput_size. ๐BrotliEncoderPreparedDictionary * BrotliEncoderPrepareDictionary (BrotliSharedDictionaryType type, size_t data_size, const uint8_t data[data_size], int quality, brotli_alloc_func alloc_func, brotli_free_func free_func, void *opaque)โ prepares a shared dictionary from the given file format for the encoder. ๐BROTLI_BOOL BrotliEncoderSetParameter (BrotliEncoderState *state, BrotliEncoderParameter param, uint32_t value)โ sets the specified parameter to the given encoder instance. ๐๏ธconst uint8_t * BrotliEncoderTakeOutput (BrotliEncoderState *state, size_t *size)โ acquires a pointer to the internal output buffer. ๐คuint32_t BrotliEncoderVersion (void)โ gets an encoder library version. ๐ข
๐ Detailed Description ๐
API for Brotli compression. That's the whole vibe, bestie. ๐
๐ง Macro Definition Documentation ๐ง
#define BROTLI_DEFAULT_MODE BROTLI_MODE_GENERIC
This is the default value for the BROTLI_PARAM_MODE parameter. ๐
#define BROTLI_DEFAULT_QUALITY 11
This is the default value for the BROTLI_PARAM_QUALITY parameter. ๐
#define BROTLI_DEFAULT_WINDOW 22
This is the default value for the BROTLI_PARAM_LGWIN parameter. ๐ธ
#define BROTLI_MAX_INPUT_BLOCK_BITS 24
This is the maximal value for the BROTLI_PARAM_LGBLOCK parameter. ๐ฅ
#define BROTLI_MAX_QUALITY 11
This is the maximal value for the BROTLI_PARAM_QUALITY parameter. ๐ฏ
#define BROTLI_MAX_WINDOW_BITS 24
This is the maximal value for the BROTLI_PARAM_LGWIN parameter. ๐ฆ
Note: equal to the BROTLI_MAX_DISTANCE_BITS constant.
#define BROTLI_MIN_INPUT_BLOCK_BITS 16
This is the minimal value for the BROTLI_PARAM_LGBLOCK parameter. ๐ฅบ
#define BROTLI_MIN_QUALITY 0
This is the minimal value for the BROTLI_PARAM_QUALITY parameter. ๐
#define BROTLI_MIN_WINDOW_BITS 10
This is the minimal value for the BROTLI_PARAM_LGWIN parameter. ๐
๐งฌ Typedef Documentation ๐งฌ
typedef enum BrotliEncoderMode BrotliEncoderMode
These are the options for the BROTLI_PARAM_MODE parameter. ๐
typedef enum BrotliEncoderOperation BrotliEncoderOperation
These are the operations that can be performed by the streaming encoder. ๐
typedef enum BrotliEncoderParameter BrotliEncoderParameter
These are the options to be used with BrotliEncoderSetParameter. โญ
typedef struct BrotliEncoderStateStruct BrotliEncoderState
An opaque structure that holds the encoder state. It's allocated and initialized with BrotliEncoderCreateInstance. It's cleaned up and deallocated with BrotliEncoderDestroyInstance. ๐
๐ Enumeration Type Documentation ๐
enum BrotliEncoderMode
Options for the BROTLI_PARAM_MODE parameter. ๐
Enumerator
BROTLI_MODE_GENERICโ the default compression mode. In this mode, the compressor does not know anything in advance about the properties of the input. ๐คทโโ๏ธBROTLI_MODE_TEXTโ the compression mode for UTF-8 formatted text input. ๐BROTLI_MODE_FONTโ the compression mode used in WOFF 2.0. ๐ค
enum BrotliEncoderOperation
Operations that can be performed by the streaming encoder. ๐
Enumerator
BROTLI_OPERATION_PROCESSโ process input. The encoder may postpone producing output until it has processed enough input. โณBROTLI_OPERATION_FLUSHโ produce output for all processed input. The actual flush is performed when the input stream is depleted and there is enough space in the output stream. This means that the client should repeat theBROTLI_OPERATION_FLUSHoperation untilavailable_inbecomes0, andBrotliEncoderHasMoreOutputreturnsBROTLI_FALSE. If output is acquired viaBrotliEncoderTakeOutput, then the operation should be repeated after the output buffer is drained. ๐ฝ
Warning: Until flush is complete, the client SHOULD NOT swap, reduce or extend the input stream. ๐
When flush is complete, the output data will be sufficient for the decoder to reproduce all the given input. โจ
BROTLI_OPERATION_FINISHโ finalize the stream. The actual finalization is performed when the input stream is depleted and there is enough space in the output stream. This means that the client should repeat theBROTLI_OPERATION_FINISHoperation untilavailable_inbecomes0, andBrotliEncoderHasMoreOutputreturnsBROTLI_FALSE. If output is acquired viaBrotliEncoderTakeOutput, then the operation should be repeated after the output buffer is drained. ๐
Warning: Until finalization is complete, the client SHOULD NOT swap, reduce or extend the input stream. ๐
The helper function BrotliEncoderIsFinished checks if the stream is finalized and the output is fully dumped. ๐
Adding more input data to a finalized stream is impossible. ๐ซ
BROTLI_OPERATION_EMIT_METADATAโ emit a metadata block to the stream. Metadata is opaque to Brotli: neither the encoder nor the decoder processes this data or relies on it. It may be used to pass some extra information from the encoder client to the decoder client without interfering with the main data stream. ๐ฆ
Note: The encoder may emit empty metadata blocks internally, to pad the encoded stream to a byte boundary. ๐
Warning: Until emitting metadata is complete, the client SHOULD NOT swap, reduce or extend the input stream. ๐ The whole content of the input buffer is considered to be the content of the metadata block. Do NOT append metadata to the input stream before it is depleted with other operations. ๐ซ
The stream is soft-flushed before the metadata block is emitted. The metadata block MUST be no longer than than 16MiB. ๐ฆ
enum BrotliEncoderParameter
Options to be used with BrotliEncoderSetParameter. โญ
Enumerator
BROTLI_PARAM_MODEโ tune the encoder for a specific input.BrotliEncoderModeenumerates all available values. ๐๏ธBROTLI_PARAM_QUALITYโ the main compression speed-density lever. The higher the quality, the slower the compression. The range is fromBROTLI_MIN_QUALITYtoBROTLI_MAX_QUALITY. โ๏ธBROTLI_PARAM_LGWINโ recommended sliding LZ77 window size. The encoder may reduce this value, e.g. if the input is much smaller than the window size. ๐ช
Window size is (1 << value) - 16.
The range is from BROTLI_MIN_WINDOW_BITS to BROTLI_MAX_WINDOW_BITS. ๐
BROTLI_PARAM_LGBLOCKโ recommended input block size. The encoder may reduce this value, e.g. if the input is much smaller than the input block size. ๐งฑ
The range is from BROTLI_MIN_INPUT_BLOCK_BITS to BROTLI_MAX_INPUT_BLOCK_BITS. ๐
Note: A bigger input block size allows better compression, but consumes more memory. ๐พ The rough formula of memory used for temporary input storage is 3 << lgBlock. ๐งฎ
BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELINGโ a flag that affects usage of the 'literal context modeling' format feature. This flag is a 'decoding-speed vs compression ratio' trade-off. โกBROTLI_PARAM_SIZE_HINTโ estimated total input size for allBrotliEncoderCompressStreamcalls. The default value is0, which means that the total input size is unknown. ๐คทโโ๏ธBROTLI_PARAM_LARGE_WINDOWโ a flag that determines if 'Large Window Brotli' is used. ๐ชBROTLI_PARAM_NPOSTFIXโ recommended number of postfix bits (NPOSTFIX). The encoder may change this value. ๐ข
The range is from 0 to BROTLI_MAX_NPOSTFIX. ๐
BROTLI_PARAM_NDIRECTโ recommended number of direct distance codes (NDIRECT). The encoder may change this value. ๐
The range is from 0 to (15 << NPOSTFIX) in steps of (1 << NPOSTFIX). ๐ช
BROTLI_PARAM_STREAM_OFFSETโ number of bytes of input stream already processed by a different instance. ๐
Note: It is important to configure all the encoder instances with the same parameters (except this one) in order to allow all the encoded parts to obey the same restrictions implied by the header. ๐
If the offset is not 0, then the stream header is omitted. In any case the output start is byte aligned, so for proper streams stitching the 'predecessor' stream must be flushed. ๐งต
The range is not artificially limited, but all the values greater or equal to the maximal window size have the same effect. Values greater than 2**30 are not allowed. ๐ซ
๐ ๏ธ FUNCTION DOCUMENTATION ๐ ๏ธ
okay bestie, this is the part where we meet the actual functions, and honestly? iconic behavior all around. let's go one by one, no skipping, no cap ๐
๐ BROTLI_BOOL BrotliEncoderAttachPreparedDictionary (BrotliEncoderState * state, const BrotliEncoderPreparedDictionary * dictionary)
BROTLI_BOOL BrotliEncoderAttachPreparedDictionary (BrotliEncoderState * state,
const BrotliEncoderPreparedDictionary * dictionary)
Attaches a prepared dictionary of any type to the encoder, fr fr. And yes you can call this multiple times to attach multiple dictionaries โ it's giving collect-them-all energy. The dictionary type was determined by BrotliEncoderPrepareDictionary. You can attach multiple raw prefix dictionaries and/or max 1 serialized dictionary with custom words. ๐
Returns:
BROTLI_FALSEin case of errorBROTLI_TRUEotherwise
๐ BROTLI_BOOL BrotliEncoderCompress (int quality, int lgwin, BrotliEncoderMode mode, size_t input_size, const uint8_t input_buffer[input_size], size_t * encoded_size, uint8_t encoded_buffer[*encoded_size])
BROTLI_BOOL BrotliEncoderCompress (int quality, int lgwin, BrotliEncoderMode mode,
size_t input_size, const uint8_t input_buffer[input_size], size_t * encoded_size,
uint8_t encoded_buffer[*encoded_size])
Performs one-shot memory-to-memory compression โ the "just do it all in one go" queen ๐. It compresses the data in input_buffer into encoded_buffer, and sets *encoded_size to the compressed length.
Note: if BrotliEncoderMaxCompressedSize(input_size) returns a non-zero value, then output is guaranteed to be no longer than that. โจ
Note: iflgwinis greater thanBROTLI_MAX_WINDOW_BITSthen the resulting stream might be incompatible with RFC 7932 ๐ญ โ to decode such streams, the decoder should be configured withBROTLI_DECODER_PARAM_LARGE_WINDOW = 1.
Parameters:
qualityโ quality parameter value, e.g.BROTLI_DEFAULT_QUALITYlgwinโ lgwin parameter value, e.g.BROTLI_DEFAULT_WINDOWmodeโ mode parameter value, e.g.BROTLI_DEFAULT_MODEinput_sizeโ size ofinput_bufferinput_bufferโ input data buffer with at leastinput_sizeaddressable bytesencoded_sizeโ in: size ofencoded_buffer; out: length of compressed data written toencoded_buffer, or 0 if compression failsencoded_bufferโ compressed data destination buffer
Returns:
BROTLI_FALSEin case of compression errorBROTLI_FALSEif output buffer is too smallBROTLI_TRUEotherwise
๐ BROTLI_BOOL BrotliEncoderCompressStream (BrotliEncoderState * state, BrotliEncoderOperation op, size_t * available_in, const uint8_t ** next_in, size_t * available_out, uint8_t ** next_out, size_t * total_out)
BROTLI_BOOL BrotliEncoderCompressStream (BrotliEncoderState * state,
BrotliEncoderOperation op, size_t * available_in, const uint8_t ** next_in,
size_t * available_out, uint8_t ** next_out, size_t * total_out)
Compresses input stream to output stream โ the streaming era main character ๐. The values *available_in and *available_out must specify the number of bytes addressable at *next_in and *next_out respectively. When *available_out is 0, next_out is allowed to be NULL.
After each call, *available_in will be decremented by the amount of input bytes consumed, and the *next_in pointer will be incremented by that amount. Similarly, *available_out will be decremented by the amount of output bytes written, and the *next_out pointer will be incremented by that amount. Math is mathing ๐
total_out, if it is not a null-pointer, will be set to the number of bytes compressed since the last state initialization.
Internally the workflow consists of 3 tasks:
- (optionally) copy input data to internal buffer
- actually compress data and (optionally) store it to internal buffer
- (optionally) copy compressed bytes from internal buffer to output stream
Whenever all 3 tasks can't move forward anymore, or error occurs, this method returns the control flow to caller.
op is used to perform flush, finish the stream, or inject metadata block. See BrotliEncoderOperation for more information.
Flushing the stream means forcing encoding of all input passed to encoder and completing the current output block, so it could be fully decoded by stream decoder. To perform flush set op to BROTLI_OPERATION_FLUSH. Under some circumstances (e.g. lack of output stream capacity) this operation would require several calls to BrotliEncoderCompressStream. The method must be called again until both input stream is depleted and encoder has no more output (see BrotliEncoderHasMoreOutput) after the method is called.
Finishing the stream means encoding of all input passed to encoder and adding specific 'final' marks, so stream decoder could determine that stream is complete. To perform finish set op to BROTLI_OPERATION_FINISH. Under some circumstances (e.g. lack of output stream capacity) this operation would require several calls to BrotliEncoderCompressStream. The method must be called again until both input stream is depleted and encoder has no more output (see BrotliEncoderHasMoreOutput) after the method is called.
Warning: when flushing and finishing, op should not change until the operation is complete; input stream should not be swapped, reduced or extended as well. ๐จ do not touch, bestie, it's mid-transformation.
Parameters:
stateโ encoder instanceopโ requested operationavailable_inโ in: amount of available input; out: amount of unused inputnext_inโ pointer to the next input byteavailable_outโ in: length of output buffer; out: remaining size of output buffernext_outโ compressed output buffer cursor; can beNULLifavailable_outis 0total_outโ number of bytes produced so far; can beNULL
Returns:
BROTLI_FALSEif there was an errorBROTLI_TRUEotherwise
๐ BrotliEncoderState* BrotliEncoderCreateInstance (brotli_alloc_func alloc_func, brotli_free_func free_func, void * opaque)
BrotliEncoderState* BrotliEncoderCreateInstance (brotli_alloc_func alloc_func,
brotli_free_func free_func, void * opaque)
Creates an instance of BrotliEncoderState and initializes it โ the birth of a legend, honestly ๐ฆ. alloc_func and free_func MUST be both zero or both non-zero. In the case they are both zero, default memory allocators are used. opaque is passed to alloc_func and free_func when they are called. free_func has to return without doing anything when asked to free a NULL pointer.
Parameters:
alloc_funcโ custom memory allocation functionfree_funcโ custom memory free functionopaqueโ custom memory manager handle
Returns:
- 0 if instance can not be allocated or initialized
- pointer to initialized
BrotliEncoderStateotherwise
๐ void BrotliEncoderDestroyInstance (BrotliEncoderState * state)
void BrotliEncoderDestroyInstance (BrotliEncoderState * state)
Deinitializes and frees BrotliEncoderState instance. it's giving closure, and we love that for it ๐โจ
Parameters:
stateโ decoder instance to be cleaned up and deallocated
๐ BROTLI_BOOL BrotliEncoderHasMoreOutput (BrotliEncoderState * state)
BROTLI_BOOL BrotliEncoderHasMoreOutput (BrotliEncoderState * state)
Checks if encoder has more output. the vibe check function ๐
Parameters:
stateโ encoder instance
Returns:
BROTLI_TRUE, if encoder has some unconsumed outputBROTLI_FALSEotherwise
๐ BROTLI_BOOL BrotliEncoderIsFinished (BrotliEncoderState * state)
BROTLI_BOOL BrotliEncoderIsFinished (BrotliEncoderState * state)
Checks if encoder instance reached the final state. are we done yet or nah ๐ฅ
Parameters:
stateโ encoder instance
Returns:
BROTLI_TRUEif encoder is in a state where it reached the end of the input and produced all of the outputBROTLI_FALSEotherwise
๐ size_t BrotliEncoderMaxCompressedSize (size_t input_size)
size_t BrotliEncoderMaxCompressedSize (size_t input_size)
Calculates the output size bound for the given input_size. math girlies rise ๐
Warning: result is only valid ifqualityis at least 2 and, in caseBrotliEncoderCompressStreamwas used, no flushes (BROTLI_OPERATION_FLUSH) were performed. so like, don't come for this number if you broke the rules ๐
Parameters:
input_sizeโ size of projected input
Returns:
- 0 if result does not fit
size_t
๐ BrotliEncoderPreparedDictionary* BrotliEncoderPrepareDictionary (BrotliSharedDictionaryType type, size_t data_size, const uint8_t data[data_size], int quality, brotli_alloc_func alloc_func, brotli_free_func free_func, void * opaque)
BrotliEncoderPreparedDictionary* BrotliEncoderPrepareDictionary
(BrotliSharedDictionaryType type, size_t data_size, const uint8_t data[data_size],
int quality, brotli_alloc_func alloc_func, brotli_free_func free_func, void * opaque)
Prepares a shared dictionary from the given file format for the encoder. alloc_func and free_func MUST be both zero or both non-zero. In the case they are both zero, default memory allocators are used. opaque is passed to alloc_func and free_func when they are called. free_func has to return without doing anything when asked to free a NULL pointer.
Warning: created instance is 'lean'; it does not contain a copy ofdata, rather it contains only a pointer to it; therefore,dataMUST outlive the created instance. ๐จ it's borrowing, not owning โ do NOT let that buffer ghost you, bestie.
Parameters:
typeโ type of dictionary stored indatadata_sizeโ size ofdatabufferdataโ pointer to the dictionary dataqualityโ the maximum Brotli quality to prepare the dictionary for, useBROTLI_MAX_QUALITYby defaultalloc_funcโ custom memory allocation functionfree_funcโ custom memory free functionopaqueโ custom memory manager handle
๐ BROTLI_BOOL BrotliEncoderSetParameter (BrotliEncoderState * state, BrotliEncoderParameter param, uint32_t value)
BROTLI_BOOL BrotliEncoderSetParameter (BrotliEncoderState * state,
BrotliEncoderParameter param, uint32_t value)
Sets the specified parameter to the given encoder instance. customization era ๐
Parameters:
stateโ encoder instanceparamโ parameter to setvalueโ new parameter value
Returns:
BROTLI_FALSEif parameter is unrecognized, or value is invalidBROTLI_FALSEif value of parameter can not be changed at current encoder state (e.g. when encoding is started, window size might be already encoded and therefore it is impossible to change it)BROTLI_TRUEif value is accepted
Warning: invalid values might be accepted in case they would not break the encoding process. so likeโฆ it's not always gonna catch you ๐ญ
๐ const uint8_t* BrotliEncoderTakeOutput (BrotliEncoderState * state, size_t * size)
const uint8_t* BrotliEncoderTakeOutput (BrotliEncoderState * state, size_t * size)
Acquires pointer to internal output buffer. This method is used to make language bindings easier and more efficient:
- push data to
BrotliEncoderCompressStream, untilBrotliEncoderHasMoreOutputreturnsBROTLI_TRUE - use
BrotliEncoderTakeOutputto peek bytes and copy to language-specific entity
Also this could be useful if there is an output stream that is able to consume all the provided data (e.g. when data is saved to file system). ๐ธ
Attention: after every call toBrotliEncoderTakeOutput*sizebytes of output are considered consumed for all consecutive calls to the instance methods; returned pointer becomes invalidated as well. so peek fast, because that pointer expires like a story ๐ซถ
Note: encoder output is not guaranteed to be contiguous. This means that after the size-unrestricted call toBrotliEncoderTakeOutput, the immediate next call toBrotliEncoderTakeOutputmay return more data.
Parameters:
stateโ encoder instancesizeโ in: number of bytes caller is ready to take, 0 if any amount could be handled; out: amount of data pointed by returned pointer and considered consumed; out value is never greater than in value, unless it is 0
Returns:
- pointer to output data
๐ uint32_t BrotliEncoderVersion (void)
uint32_t BrotliEncoderVersion (void)
Gets an encoder library version. Look at BROTLI_MAKE_HEX_VERSION for more information. knowing exactly which version you're dealing with? highkey essential โญ
๐ AUTHOR ๐
Generated automatically by Doxygen for Brotli from the source code. yeah, the robots wrote this bit, not me โ respect to the automation โจ
and that's a wrap on encode.h(3), bestie!! ๐ this lil page is from Brotli and it's dated August 2021 โ go forth and compress things beautifully, you absolute legend ๐ฆ๐๐
๐ฆ where this page comes from
- alpine 3.23: brotli-doc 1.2.1-r0 ยท MIT
- alpine 3.24: brotli-doc 1.2.1-r0 ยท MIT
- arch rolling: brotli 1.2.0-1
- arch rolling: brotli 1.2.1-1 ยท MIT
- nixos 26.05: brotli 1.2.1
- nixpkgs unstable: brotli 1.2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms