Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:09:01

0001 /* Copyright 2013 Google Inc. All Rights Reserved.
0002 
0003    Distributed under MIT license.
0004    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
0005 */
0006 
0007 /**
0008  * @file
0009  * API for Brotli compression.
0010  */
0011 
0012 #ifndef BROTLI_ENC_ENCODE_H_
0013 #define BROTLI_ENC_ENCODE_H_
0014 
0015 #include <brotli/port.h>
0016 #include <brotli/shared_dictionary.h>
0017 #include <brotli/types.h>  /* IWYU pragma: export */
0018 
0019 #if defined(__cplusplus) || defined(c_plusplus)
0020 extern "C" {
0021 #endif
0022 
0023 /** Minimal value for ::BROTLI_PARAM_LGWIN parameter. */
0024 #define BROTLI_MIN_WINDOW_BITS 10
0025 /**
0026  * Maximal value for ::BROTLI_PARAM_LGWIN parameter.
0027  *
0028  * @note equal to @c BROTLI_MAX_DISTANCE_BITS constant.
0029  */
0030 #define BROTLI_MAX_WINDOW_BITS 24
0031 /**
0032  * Maximal value for ::BROTLI_PARAM_LGWIN parameter
0033  * in "Large Window Brotli" (32-bit).
0034  */
0035 #define BROTLI_LARGE_MAX_WINDOW_BITS 30
0036 /** Minimal value for ::BROTLI_PARAM_LGBLOCK parameter. */
0037 #define BROTLI_MIN_INPUT_BLOCK_BITS 16
0038 /** Maximal value for ::BROTLI_PARAM_LGBLOCK parameter. */
0039 #define BROTLI_MAX_INPUT_BLOCK_BITS 24
0040 /** Minimal value for ::BROTLI_PARAM_QUALITY parameter. */
0041 #define BROTLI_MIN_QUALITY 0
0042 /** Maximal value for ::BROTLI_PARAM_QUALITY parameter. */
0043 #define BROTLI_MAX_QUALITY 11
0044 
0045 /** Options for ::BROTLI_PARAM_MODE parameter. */
0046 typedef enum BrotliEncoderMode {
0047   /**
0048    * Default compression mode.
0049    *
0050    * In this mode compressor does not know anything in advance about the
0051    * properties of the input.
0052    */
0053   BROTLI_MODE_GENERIC = 0,
0054   /** Compression mode for UTF-8 formatted text input. */
0055   BROTLI_MODE_TEXT = 1,
0056   /** Compression mode used in WOFF 2.0. */
0057   BROTLI_MODE_FONT = 2
0058 } BrotliEncoderMode;
0059 
0060 /** Default value for ::BROTLI_PARAM_QUALITY parameter. */
0061 #define BROTLI_DEFAULT_QUALITY 11
0062 /** Default value for ::BROTLI_PARAM_LGWIN parameter. */
0063 #define BROTLI_DEFAULT_WINDOW 22
0064 /** Default value for ::BROTLI_PARAM_MODE parameter. */
0065 #define BROTLI_DEFAULT_MODE BROTLI_MODE_GENERIC
0066 
0067 /** Operations that can be performed by streaming encoder. */
0068 typedef enum BrotliEncoderOperation {
0069   /**
0070    * Process input.
0071    *
0072    * Encoder may postpone producing output, until it has processed enough input.
0073    */
0074   BROTLI_OPERATION_PROCESS = 0,
0075   /**
0076    * Produce output for all processed input.
0077    *
0078    * Actual flush is performed when input stream is depleted and there is enough
0079    * space in output stream. This means that client should repeat
0080    * ::BROTLI_OPERATION_FLUSH operation until @p available_in becomes @c 0, and
0081    * ::BrotliEncoderHasMoreOutput returns ::BROTLI_FALSE. If output is acquired
0082    * via ::BrotliEncoderTakeOutput, then operation should be repeated after
0083    * output buffer is drained.
0084    *
0085    * @warning Until flush is complete, client @b SHOULD @b NOT swap,
0086    *          reduce or extend input stream.
0087    *
0088    * When flush is complete, output data will be sufficient for decoder to
0089    * reproduce all the given input.
0090    */
0091   BROTLI_OPERATION_FLUSH = 1,
0092   /**
0093    * Finalize the stream.
0094    *
0095    * Actual finalization is performed when input stream is depleted and there is
0096    * enough space in output stream. This means that client should repeat
0097    * ::BROTLI_OPERATION_FINISH operation until @p available_in becomes @c 0, and
0098    * ::BrotliEncoderHasMoreOutput returns ::BROTLI_FALSE. If output is acquired
0099    * via ::BrotliEncoderTakeOutput, then operation should be repeated after
0100    * output buffer is drained.
0101    *
0102    * @warning Until finalization is complete, client @b SHOULD @b NOT swap,
0103    *          reduce or extend input stream.
0104    *
0105    * Helper function ::BrotliEncoderIsFinished checks if stream is finalized and
0106    * output fully dumped.
0107    *
0108    * Adding more input data to finalized stream is impossible.
0109    */
0110   BROTLI_OPERATION_FINISH = 2,
0111   /**
0112    * Emit metadata block to stream.
0113    *
0114    * Metadata is opaque to Brotli: neither encoder, nor decoder processes this
0115    * data or relies on it. It may be used to pass some extra information from
0116    * encoder client to decoder client without interfering with main data stream.
0117    *
0118    * @note Encoder may emit empty metadata blocks internally, to pad encoded
0119    *       stream to byte boundary.
0120    *
0121    * @warning Until emitting metadata is complete client @b SHOULD @b NOT swap,
0122    *          reduce or extend input stream.
0123    *
0124    * @warning The whole content of input buffer is considered to be the content
0125    *          of metadata block. Do @b NOT @e append metadata to input stream,
0126    *          before it is depleted with other operations.
0127    *
0128    * Stream is soft-flushed before metadata block is emitted. Metadata block
0129    * @b MUST be no longer than than 16MiB.
0130    */
0131   BROTLI_OPERATION_EMIT_METADATA = 3
0132 } BrotliEncoderOperation;
0133 
0134 /** Options to be used with ::BrotliEncoderSetParameter. */
0135 typedef enum BrotliEncoderParameter {
0136   /**
0137    * Tune encoder for specific input.
0138    *
0139    * ::BrotliEncoderMode enumerates all available values.
0140    */
0141   BROTLI_PARAM_MODE = 0,
0142   /**
0143    * The main compression speed-density lever.
0144    *
0145    * The higher the quality, the slower the compression. Range is
0146    * from ::BROTLI_MIN_QUALITY to ::BROTLI_MAX_QUALITY.
0147    */
0148   BROTLI_PARAM_QUALITY = 1,
0149   /**
0150    * Recommended sliding LZ77 window size.
0151    *
0152    * Encoder may reduce this value, e.g. if input is much smaller than
0153    * window size.
0154    *
0155    * Window size is `(1 << value) - 16`.
0156    *
0157    * Range is from ::BROTLI_MIN_WINDOW_BITS to ::BROTLI_MAX_WINDOW_BITS.
0158    */
0159   BROTLI_PARAM_LGWIN = 2,
0160   /**
0161    * Recommended input block size.
0162    *
0163    * Encoder may reduce this value, e.g. if input is much smaller than input
0164    * block size.
0165    *
0166    * Range is from ::BROTLI_MIN_INPUT_BLOCK_BITS to
0167    * ::BROTLI_MAX_INPUT_BLOCK_BITS.
0168    *
0169    * @note Bigger input block size allows better compression, but consumes more
0170    *       memory. \n The rough formula of memory used for temporary input
0171    *       storage is `3 << lgBlock`.
0172    */
0173   BROTLI_PARAM_LGBLOCK = 3,
0174   /**
0175    * Flag that affects usage of "literal context modeling" format feature.
0176    *
0177    * This flag is a "decoding-speed vs compression ratio" trade-off.
0178    */
0179   BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING = 4,
0180   /**
0181    * Estimated total input size for all ::BrotliEncoderCompressStream calls.
0182    *
0183    * The default value is 0, which means that the total input size is unknown.
0184    */
0185   BROTLI_PARAM_SIZE_HINT = 5,
0186   /**
0187    * Flag that determines if "Large Window Brotli" is used.
0188    */
0189   BROTLI_PARAM_LARGE_WINDOW = 6,
0190   /**
0191    * Recommended number of postfix bits (NPOSTFIX).
0192    *
0193    * Encoder may change this value.
0194    *
0195    * Range is from 0 to ::BROTLI_MAX_NPOSTFIX.
0196    */
0197   BROTLI_PARAM_NPOSTFIX = 7,
0198   /**
0199    * Recommended number of direct distance codes (NDIRECT).
0200    *
0201    * Encoder may change this value.
0202    *
0203    * Range is from 0 to (15 << NPOSTFIX) in steps of (1 << NPOSTFIX).
0204    */
0205   BROTLI_PARAM_NDIRECT = 8,
0206   /**
0207    * Number of bytes of input stream already processed by a different instance.
0208    *
0209    * @note It is important to configure all the encoder instances with same
0210    *       parameters (except this one) in order to allow all the encoded parts
0211    *       obey the same restrictions implied by header.
0212    *
0213    * If offset is not 0, then stream header is omitted.
0214    * In any case output start is byte aligned, so for proper streams stitching
0215    * "predecessor" stream must be flushed.
0216    *
0217    * Range is not artificially limited, but all the values greater or equal to
0218    * maximal window size have the same effect. Values greater than 2**30 are not
0219    * allowed.
0220    */
0221   BROTLI_PARAM_STREAM_OFFSET = 9
0222 } BrotliEncoderParameter;
0223 
0224 /**
0225  * Opaque structure that holds encoder state.
0226  *
0227  * Allocated and initialized with ::BrotliEncoderCreateInstance.
0228  * Cleaned up and deallocated with ::BrotliEncoderDestroyInstance.
0229  */
0230 typedef struct BrotliEncoderStateStruct BrotliEncoderState;
0231 
0232 /**
0233  * Sets the specified parameter to the given encoder instance.
0234  *
0235  * @param state encoder instance
0236  * @param param parameter to set
0237  * @param value new parameter value
0238  * @returns ::BROTLI_FALSE if parameter is unrecognized, or value is invalid
0239  * @returns ::BROTLI_FALSE if value of parameter can not be changed at current
0240  *          encoder state (e.g. when encoding is started, window size might be
0241  *          already encoded and therefore it is impossible to change it)
0242  * @returns ::BROTLI_TRUE if value is accepted
0243  * @warning invalid values might be accepted in case they would not break
0244  *          encoding process.
0245  */
0246 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderSetParameter(
0247     BrotliEncoderState* state, BrotliEncoderParameter param, uint32_t value);
0248 
0249 /**
0250  * Creates an instance of ::BrotliEncoderState and initializes it.
0251  *
0252  * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the
0253  * case they are both zero, default memory allocators are used. @p opaque is
0254  * passed to @p alloc_func and @p free_func when they are called. @p free_func
0255  * has to return without doing anything when asked to free a NULL pointer.
0256  *
0257  * @param alloc_func custom memory allocation function
0258  * @param free_func custom memory free function
0259  * @param opaque custom memory manager handle
0260  * @returns @c 0 if instance can not be allocated or initialized
0261  * @returns pointer to initialized ::BrotliEncoderState otherwise
0262  */
0263 BROTLI_ENC_API BrotliEncoderState* BrotliEncoderCreateInstance(
0264     brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
0265 
0266 /**
0267  * Deinitializes and frees ::BrotliEncoderState instance.
0268  *
0269  * @param state decoder instance to be cleaned up and deallocated
0270  */
0271 BROTLI_ENC_API void BrotliEncoderDestroyInstance(BrotliEncoderState* state);
0272 
0273 /* Opaque type for pointer to different possible internal structures containing
0274    dictionary prepared for the encoder */
0275 typedef struct BrotliEncoderPreparedDictionaryStruct
0276     BrotliEncoderPreparedDictionary;
0277 
0278 /**
0279  * Prepares a shared dictionary from the given file format for the encoder.
0280  *
0281  * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the
0282  * case they are both zero, default memory allocators are used. @p opaque is
0283  * passed to @p alloc_func and @p free_func when they are called. @p free_func
0284  * has to return without doing anything when asked to free a NULL pointer.
0285  *
0286  * @warning Created instance is "lean"; it does not contain copy of @p data,
0287  *          rather it contains only pointer to it; therefore,
0288  *          @p data @b MUST outlive the created instance.
0289  *
0290  * @param type type of dictionary stored in data
0291  * @param data_size size of @p data buffer
0292  * @param data pointer to the dictionary data
0293  * @param quality the maximum Brotli quality to prepare the dictionary for,
0294  *        use BROTLI_MAX_QUALITY by default
0295  * @param alloc_func custom memory allocation function
0296  * @param free_func custom memory free function
0297  * @param opaque custom memory manager handle
0298  */
0299 BROTLI_ENC_API BrotliEncoderPreparedDictionary*
0300 BrotliEncoderPrepareDictionary(BrotliSharedDictionaryType type,
0301     size_t data_size, const uint8_t data[BROTLI_ARRAY_PARAM(data_size)],
0302     int quality,
0303     brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
0304 
0305 BROTLI_ENC_API void BrotliEncoderDestroyPreparedDictionary(
0306     BrotliEncoderPreparedDictionary* dictionary);
0307 
0308 /**
0309  * Attaches a prepared dictionary of any type to the encoder. Can be used
0310  * multiple times to attach multiple dictionaries. The dictionary type was
0311  * determined by BrotliEncoderPrepareDictionary. Multiple raw prefix
0312  * dictionaries and/or max 1 serialized dictionary with custom words can be
0313  * attached.
0314  *
0315  * @returns ::BROTLI_FALSE in case of error
0316  * @returns ::BROTLI_TRUE otherwise
0317  */
0318 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderAttachPreparedDictionary(
0319     BrotliEncoderState* state,
0320     const BrotliEncoderPreparedDictionary* dictionary);
0321 
0322 /**
0323  * Calculates the output size bound for the given @p input_size.
0324  *
0325  * @warning Result is only valid if quality is at least @c 2 and, in
0326  *          case ::BrotliEncoderCompressStream was used, no flushes
0327  *          (::BROTLI_OPERATION_FLUSH) were performed.
0328  *
0329  * @param input_size size of projected input
0330  * @returns @c 0 if result does not fit @c size_t
0331  */
0332 BROTLI_ENC_API size_t BrotliEncoderMaxCompressedSize(size_t input_size);
0333 
0334 /**
0335  * Performs one-shot memory-to-memory compression.
0336  *
0337  * Compresses the data in @p input_buffer into @p encoded_buffer, and sets
0338  * @p *encoded_size to the compressed length.
0339  *
0340  * @note If ::BrotliEncoderMaxCompressedSize(@p input_size) returns non-zero
0341  *       value, then output is guaranteed to be no longer than that.
0342  *
0343  * @note If @p lgwin is greater than ::BROTLI_MAX_WINDOW_BITS then resulting
0344  *       stream might be incompatible with RFC 7932; to decode such streams,
0345  *       decoder should be configured with
0346  *       ::BROTLI_DECODER_PARAM_LARGE_WINDOW = @c 1
0347  *
0348  * @param quality quality parameter value, e.g. ::BROTLI_DEFAULT_QUALITY
0349  * @param lgwin lgwin parameter value, e.g. ::BROTLI_DEFAULT_WINDOW
0350  * @param mode mode parameter value, e.g. ::BROTLI_DEFAULT_MODE
0351  * @param input_size size of @p input_buffer
0352  * @param input_buffer input data buffer with at least @p input_size
0353  *        addressable bytes
0354  * @param[in, out] encoded_size @b in: size of @p encoded_buffer; \n
0355  *                 @b out: length of compressed data written to
0356  *                 @p encoded_buffer, or @c 0 if compression fails
0357  * @param encoded_buffer compressed data destination buffer
0358  * @returns ::BROTLI_FALSE in case of compression error
0359  * @returns ::BROTLI_FALSE if output buffer is too small
0360  * @returns ::BROTLI_TRUE otherwise
0361  */
0362 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompress(
0363     int quality, int lgwin, BrotliEncoderMode mode, size_t input_size,
0364     const uint8_t input_buffer[BROTLI_ARRAY_PARAM(input_size)],
0365     size_t* encoded_size,
0366     uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(*encoded_size)]);
0367 
0368 /**
0369  * Compresses input stream to output stream.
0370  *
0371  * The values @p *available_in and @p *available_out must specify the number of
0372  * bytes addressable at @p *next_in and @p *next_out respectively.
0373  * When @p *available_out is @c 0, @p next_out is allowed to be @c NULL.
0374  *
0375  * After each call, @p *available_in will be decremented by the amount of input
0376  * bytes consumed, and the @p *next_in pointer will be incremented by that
0377  * amount. Similarly, @p *available_out will be decremented by the amount of
0378  * output bytes written, and the @p *next_out pointer will be incremented by
0379  * that amount.
0380  *
0381  * @p total_out, if it is not a null-pointer, will be set to the number
0382  * of bytes compressed since the last @p state initialization.
0383  *
0384  *
0385  *
0386  * Internally workflow consists of 3 tasks:
0387  *  -# (optionally) copy input data to internal buffer
0388  *  -# actually compress data and (optionally) store it to internal buffer
0389  *  -# (optionally) copy compressed bytes from internal buffer to output stream
0390  *
0391  * Whenever all 3 tasks can't move forward anymore, or error occurs, this
0392  * method returns the control flow to caller.
0393  *
0394  * @p op is used to perform flush, finish the stream, or inject metadata block.
0395  * See ::BrotliEncoderOperation for more information.
0396  *
0397  * Flushing the stream means forcing encoding of all input passed to encoder and
0398  * completing the current output block, so it could be fully decoded by stream
0399  * decoder. To perform flush set @p op to ::BROTLI_OPERATION_FLUSH.
0400  * Under some circumstances (e.g. lack of output stream capacity) this operation
0401  * would require several calls to ::BrotliEncoderCompressStream. The method must
0402  * be called again until both input stream is depleted and encoder has no more
0403  * output (see ::BrotliEncoderHasMoreOutput) after the method is called.
0404  *
0405  * Finishing the stream means encoding of all input passed to encoder and
0406  * adding specific "final" marks, so stream decoder could determine that stream
0407  * is complete. To perform finish set @p op to ::BROTLI_OPERATION_FINISH.
0408  * Under some circumstances (e.g. lack of output stream capacity) this operation
0409  * would require several calls to ::BrotliEncoderCompressStream. The method must
0410  * be called again until both input stream is depleted and encoder has no more
0411  * output (see ::BrotliEncoderHasMoreOutput) after the method is called.
0412  *
0413  * @warning When flushing and finishing, @p op should not change until operation
0414  *          is complete; input stream should not be swapped, reduced or
0415  *          extended as well.
0416  *
0417  * @param state encoder instance
0418  * @param op requested operation
0419  * @param[in, out] available_in @b in: amount of available input; \n
0420  *                 @b out: amount of unused input
0421  * @param[in, out] next_in pointer to the next input byte
0422  * @param[in, out] available_out @b in: length of output buffer; \n
0423  *                 @b out: remaining size of output buffer
0424  * @param[in, out] next_out compressed output buffer cursor;
0425  *                 can be @c NULL if @p available_out is @c 0
0426  * @param[out] total_out number of bytes produced so far; can be @c NULL
0427  * @returns ::BROTLI_FALSE if there was an error
0428  * @returns ::BROTLI_TRUE otherwise
0429  */
0430 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompressStream(
0431     BrotliEncoderState* state, BrotliEncoderOperation op, size_t* available_in,
0432     const uint8_t** next_in, size_t* available_out, uint8_t** next_out,
0433     size_t* total_out);
0434 
0435 /**
0436  * Checks if encoder instance reached the final state.
0437  *
0438  * @param state encoder instance
0439  * @returns ::BROTLI_TRUE if encoder is in a state where it reached the end of
0440  *          the input and produced all of the output
0441  * @returns ::BROTLI_FALSE otherwise
0442  */
0443 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderIsFinished(BrotliEncoderState* state);
0444 
0445 /**
0446  * Checks if encoder has more output.
0447  *
0448  * @param state encoder instance
0449  * @returns ::BROTLI_TRUE, if encoder has some unconsumed output
0450  * @returns ::BROTLI_FALSE otherwise
0451  */
0452 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderHasMoreOutput(
0453     BrotliEncoderState* state);
0454 
0455 /**
0456  * Acquires pointer to internal output buffer.
0457  *
0458  * This method is used to make language bindings easier and more efficient:
0459  *  -# push data to ::BrotliEncoderCompressStream,
0460  *     until ::BrotliEncoderHasMoreOutput returns BROTLI_TRUE
0461  *  -# use ::BrotliEncoderTakeOutput to peek bytes and copy to language-specific
0462  *     entity
0463  *
0464  * Also this could be useful if there is an output stream that is able to
0465  * consume all the provided data (e.g. when data is saved to file system).
0466  *
0467  * @attention After every call to ::BrotliEncoderTakeOutput @p *size bytes of
0468  *            output are considered consumed for all consecutive calls to the
0469  *            instance methods; returned pointer becomes invalidated as well.
0470  *
0471  * @note Encoder output is not guaranteed to be contiguous. This means that
0472  *       after the size-unrestricted call to ::BrotliEncoderTakeOutput,
0473  *       immediate next call to ::BrotliEncoderTakeOutput may return more data.
0474  *
0475  * @param state encoder instance
0476  * @param[in, out] size @b in: number of bytes caller is ready to take, @c 0 if
0477  *                 any amount could be handled; \n
0478  *                 @b out: amount of data pointed by returned pointer and
0479  *                 considered consumed; \n
0480  *                 out value is never greater than in value, unless it is @c 0
0481  * @returns pointer to output data
0482  */
0483 BROTLI_ENC_API const uint8_t* BrotliEncoderTakeOutput(
0484     BrotliEncoderState* state, size_t* size);
0485 
0486 /* Returns the estimated peak memory usage (in bytes) of the BrotliCompress()
0487    function, not counting the memory needed for the input and output. */
0488 BROTLI_ENC_EXTRA_API size_t BrotliEncoderEstimatePeakMemoryUsage(
0489     int quality, int lgwin, size_t input_size);
0490 /* Returns 0 if dictionary is not valid; otherwise returns allocation size. */
0491 BROTLI_ENC_EXTRA_API size_t BrotliEncoderGetPreparedDictionarySize(
0492     const BrotliEncoderPreparedDictionary* dictionary);
0493 
0494 /**
0495  * Gets an encoder library version.
0496  *
0497  * Look at BROTLI_MAKE_HEX_VERSION for more information.
0498  */
0499 BROTLI_ENC_API uint32_t BrotliEncoderVersion(void);
0500 
0501 #if defined(__cplusplus) || defined(c_plusplus)
0502 }  /* extern "C" */
0503 #endif
0504 
0505 #endif  /* BROTLI_ENC_ENCODE_H_ */