Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54: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>
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  * @param type type of dictionary stored in data
0287  * @param data_size size of @p data buffer
0288  * @param data pointer to the dictionary data
0289  * @param quality the maximum Brotli quality to prepare the dictionary for,
0290  *        use BROTLI_MAX_QUALITY by default
0291  * @param alloc_func custom memory allocation function
0292  * @param free_func custom memory free function
0293  * @param opaque custom memory manager handle
0294  */
0295 BROTLI_ENC_API BrotliEncoderPreparedDictionary*
0296 BrotliEncoderPrepareDictionary(BrotliSharedDictionaryType type,
0297     size_t data_size, const uint8_t data[BROTLI_ARRAY_PARAM(data_size)],
0298     int quality,
0299     brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
0300 
0301 BROTLI_ENC_API void BrotliEncoderDestroyPreparedDictionary(
0302     BrotliEncoderPreparedDictionary* dictionary);
0303 
0304 /**
0305  * Attaches a prepared dictionary of any type to the encoder. Can be used
0306  * multiple times to attach multiple dictionaries. The dictionary type was
0307  * determined by BrotliEncoderPrepareDictionary. Multiple raw prefix
0308  * dictionaries and/or max 1 serialized dictionary with custom words can be
0309  * attached.
0310  *
0311  * @returns ::BROTLI_FALSE in case of error
0312  * @returns ::BROTLI_TRUE otherwise
0313  */
0314 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderAttachPreparedDictionary(
0315     BrotliEncoderState* state,
0316     const BrotliEncoderPreparedDictionary* dictionary);
0317 
0318 /**
0319  * Calculates the output size bound for the given @p input_size.
0320  *
0321  * @warning Result is only valid if quality is at least @c 2 and, in
0322  *          case ::BrotliEncoderCompressStream was used, no flushes
0323  *          (::BROTLI_OPERATION_FLUSH) were performed.
0324  *
0325  * @param input_size size of projected input
0326  * @returns @c 0 if result does not fit @c size_t
0327  */
0328 BROTLI_ENC_API size_t BrotliEncoderMaxCompressedSize(size_t input_size);
0329 
0330 /**
0331  * Performs one-shot memory-to-memory compression.
0332  *
0333  * Compresses the data in @p input_buffer into @p encoded_buffer, and sets
0334  * @p *encoded_size to the compressed length.
0335  *
0336  * @note If ::BrotliEncoderMaxCompressedSize(@p input_size) returns non-zero
0337  *       value, then output is guaranteed to be no longer than that.
0338  *
0339  * @note If @p lgwin is greater than ::BROTLI_MAX_WINDOW_BITS then resulting
0340  *       stream might be incompatible with RFC 7932; to decode such streams,
0341  *       decoder should be configured with
0342  *       ::BROTLI_DECODER_PARAM_LARGE_WINDOW = @c 1
0343  *
0344  * @param quality quality parameter value, e.g. ::BROTLI_DEFAULT_QUALITY
0345  * @param lgwin lgwin parameter value, e.g. ::BROTLI_DEFAULT_WINDOW
0346  * @param mode mode parameter value, e.g. ::BROTLI_DEFAULT_MODE
0347  * @param input_size size of @p input_buffer
0348  * @param input_buffer input data buffer with at least @p input_size
0349  *        addressable bytes
0350  * @param[in, out] encoded_size @b in: size of @p encoded_buffer; \n
0351  *                 @b out: length of compressed data written to
0352  *                 @p encoded_buffer, or @c 0 if compression fails
0353  * @param encoded_buffer compressed data destination buffer
0354  * @returns ::BROTLI_FALSE in case of compression error
0355  * @returns ::BROTLI_FALSE if output buffer is too small
0356  * @returns ::BROTLI_TRUE otherwise
0357  */
0358 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompress(
0359     int quality, int lgwin, BrotliEncoderMode mode, size_t input_size,
0360     const uint8_t input_buffer[BROTLI_ARRAY_PARAM(input_size)],
0361     size_t* encoded_size,
0362     uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(*encoded_size)]);
0363 
0364 /**
0365  * Compresses input stream to output stream.
0366  *
0367  * The values @p *available_in and @p *available_out must specify the number of
0368  * bytes addressable at @p *next_in and @p *next_out respectively.
0369  * When @p *available_out is @c 0, @p next_out is allowed to be @c NULL.
0370  *
0371  * After each call, @p *available_in will be decremented by the amount of input
0372  * bytes consumed, and the @p *next_in pointer will be incremented by that
0373  * amount. Similarly, @p *available_out will be decremented by the amount of
0374  * output bytes written, and the @p *next_out pointer will be incremented by
0375  * that amount.
0376  *
0377  * @p total_out, if it is not a null-pointer, will be set to the number
0378  * of bytes compressed since the last @p state initialization.
0379  *
0380  *
0381  *
0382  * Internally workflow consists of 3 tasks:
0383  *  -# (optionally) copy input data to internal buffer
0384  *  -# actually compress data and (optionally) store it to internal buffer
0385  *  -# (optionally) copy compressed bytes from internal buffer to output stream
0386  *
0387  * Whenever all 3 tasks can't move forward anymore, or error occurs, this
0388  * method returns the control flow to caller.
0389  *
0390  * @p op is used to perform flush, finish the stream, or inject metadata block.
0391  * See ::BrotliEncoderOperation for more information.
0392  *
0393  * Flushing the stream means forcing encoding of all input passed to encoder and
0394  * completing the current output block, so it could be fully decoded by stream
0395  * decoder. To perform flush set @p op to ::BROTLI_OPERATION_FLUSH.
0396  * Under some circumstances (e.g. lack of output stream capacity) this operation
0397  * would require several calls to ::BrotliEncoderCompressStream. The method must
0398  * be called again until both input stream is depleted and encoder has no more
0399  * output (see ::BrotliEncoderHasMoreOutput) after the method is called.
0400  *
0401  * Finishing the stream means encoding of all input passed to encoder and
0402  * adding specific "final" marks, so stream decoder could determine that stream
0403  * is complete. To perform finish set @p op to ::BROTLI_OPERATION_FINISH.
0404  * Under some circumstances (e.g. lack of output stream capacity) this operation
0405  * would require several calls to ::BrotliEncoderCompressStream. The method must
0406  * be called again until both input stream is depleted and encoder has no more
0407  * output (see ::BrotliEncoderHasMoreOutput) after the method is called.
0408  *
0409  * @warning When flushing and finishing, @p op should not change until operation
0410  *          is complete; input stream should not be swapped, reduced or
0411  *          extended as well.
0412  *
0413  * @param state encoder instance
0414  * @param op requested operation
0415  * @param[in, out] available_in @b in: amount of available input; \n
0416  *                 @b out: amount of unused input
0417  * @param[in, out] next_in pointer to the next input byte
0418  * @param[in, out] available_out @b in: length of output buffer; \n
0419  *                 @b out: remaining size of output buffer
0420  * @param[in, out] next_out compressed output buffer cursor;
0421  *                 can be @c NULL if @p available_out is @c 0
0422  * @param[out] total_out number of bytes produced so far; can be @c NULL
0423  * @returns ::BROTLI_FALSE if there was an error
0424  * @returns ::BROTLI_TRUE otherwise
0425  */
0426 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompressStream(
0427     BrotliEncoderState* state, BrotliEncoderOperation op, size_t* available_in,
0428     const uint8_t** next_in, size_t* available_out, uint8_t** next_out,
0429     size_t* total_out);
0430 
0431 /**
0432  * Checks if encoder instance reached the final state.
0433  *
0434  * @param state encoder instance
0435  * @returns ::BROTLI_TRUE if encoder is in a state where it reached the end of
0436  *          the input and produced all of the output
0437  * @returns ::BROTLI_FALSE otherwise
0438  */
0439 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderIsFinished(BrotliEncoderState* state);
0440 
0441 /**
0442  * Checks if encoder has more output.
0443  *
0444  * @param state encoder instance
0445  * @returns ::BROTLI_TRUE, if encoder has some unconsumed output
0446  * @returns ::BROTLI_FALSE otherwise
0447  */
0448 BROTLI_ENC_API BROTLI_BOOL BrotliEncoderHasMoreOutput(
0449     BrotliEncoderState* state);
0450 
0451 /**
0452  * Acquires pointer to internal output buffer.
0453  *
0454  * This method is used to make language bindings easier and more efficient:
0455  *  -# push data to ::BrotliEncoderCompressStream,
0456  *     until ::BrotliEncoderHasMoreOutput returns BROTLI_TRUE
0457  *  -# use ::BrotliEncoderTakeOutput to peek bytes and copy to language-specific
0458  *     entity
0459  *
0460  * Also this could be useful if there is an output stream that is able to
0461  * consume all the provided data (e.g. when data is saved to file system).
0462  *
0463  * @attention After every call to ::BrotliEncoderTakeOutput @p *size bytes of
0464  *            output are considered consumed for all consecutive calls to the
0465  *            instance methods; returned pointer becomes invalidated as well.
0466  *
0467  * @note Encoder output is not guaranteed to be contiguous. This means that
0468  *       after the size-unrestricted call to ::BrotliEncoderTakeOutput,
0469  *       immediate next call to ::BrotliEncoderTakeOutput may return more data.
0470  *
0471  * @param state encoder instance
0472  * @param[in, out] size @b in: number of bytes caller is ready to take, @c 0 if
0473  *                 any amount could be handled; \n
0474  *                 @b out: amount of data pointed by returned pointer and
0475  *                 considered consumed; \n
0476  *                 out value is never greater than in value, unless it is @c 0
0477  * @returns pointer to output data
0478  */
0479 BROTLI_ENC_API const uint8_t* BrotliEncoderTakeOutput(
0480     BrotliEncoderState* state, size_t* size);
0481 
0482 /* Returns the estimated peak memory usage (in bytes) of the BrotliCompress()
0483    function, not counting the memory needed for the input and output. */
0484 BROTLI_ENC_EXTRA_API size_t BrotliEncoderEstimatePeakMemoryUsage(
0485     int quality, int lgwin, size_t input_size);
0486 /* Returns 0 if dictionary is not valid; otherwise returns allocation size. */
0487 BROTLI_ENC_EXTRA_API size_t BrotliEncoderGetPreparedDictionarySize(
0488     const BrotliEncoderPreparedDictionary* dictionary);
0489 
0490 /**
0491  * Gets an encoder library version.
0492  *
0493  * Look at BROTLI_MAKE_HEX_VERSION for more information.
0494  */
0495 BROTLI_ENC_API uint32_t BrotliEncoderVersion(void);
0496 
0497 #if defined(__cplusplus) || defined(c_plusplus)
0498 }  /* extern "C" */
0499 #endif
0500 
0501 #endif  /* BROTLI_ENC_ENCODE_H_ */