Path: blob/master/thirdparty/brotli/common/constants.h
9906 views
/* Copyright 2016 Google Inc. All Rights Reserved.12Distributed under MIT license.3See file LICENSE for detail or copy at https://opensource.org/licenses/MIT4*/56/**7* @file8* Common constants used in decoder and encoder API.9*/1011#ifndef BROTLI_COMMON_CONSTANTS_H_12#define BROTLI_COMMON_CONSTANTS_H_1314#include <brotli/port.h>15#include <brotli/types.h>1617#include "platform.h"1819/* Specification: 7.3. Encoding of the context map */20#define BROTLI_CONTEXT_MAP_MAX_RLE 162122/* Specification: 2. Compressed representation overview */23#define BROTLI_MAX_NUMBER_OF_BLOCK_TYPES 2562425/* Specification: 3.3. Alphabet sizes: insert-and-copy length */26#define BROTLI_NUM_LITERAL_SYMBOLS 25627#define BROTLI_NUM_COMMAND_SYMBOLS 70428#define BROTLI_NUM_BLOCK_LEN_SYMBOLS 2629#define BROTLI_MAX_CONTEXT_MAP_SYMBOLS (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + \30BROTLI_CONTEXT_MAP_MAX_RLE)31#define BROTLI_MAX_BLOCK_TYPE_SYMBOLS (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + 2)3233/* Specification: 3.5. Complex prefix codes */34#define BROTLI_REPEAT_PREVIOUS_CODE_LENGTH 1635#define BROTLI_REPEAT_ZERO_CODE_LENGTH 1736#define BROTLI_CODE_LENGTH_CODES (BROTLI_REPEAT_ZERO_CODE_LENGTH + 1)37/* "code length of 8 is repeated" */38#define BROTLI_INITIAL_REPEATED_CODE_LENGTH 83940/* "Large Window Brotli" */4142/**43* The theoretical maximum number of distance bits specified for large window44* brotli, for 64-bit encoders and decoders. Even when in practice 32-bit45* encoders and decoders only support up to 30 max distance bits, the value is46* set to 62 because it affects the large window brotli file format.47* Specifically, it affects the encoding of simple huffman tree for distances,48* see Specification RFC 7932 chapter 3.4.49*/50#define BROTLI_LARGE_MAX_DISTANCE_BITS 62U51#define BROTLI_LARGE_MIN_WBITS 1052/**53* The maximum supported large brotli window bits by the encoder and decoder.54* Large window brotli allows up to 62 bits, however the current encoder and55* decoder, designed for 32-bit integers, only support up to 30 bits maximum.56*/57#define BROTLI_LARGE_MAX_WBITS 305859/* Specification: 4. Encoding of distances */60#define BROTLI_NUM_DISTANCE_SHORT_CODES 1661/**62* Maximal number of "postfix" bits.63*64* Number of "postfix" bits is stored as 2 bits in meta-block header.65*/66#define BROTLI_MAX_NPOSTFIX 367#define BROTLI_MAX_NDIRECT 12068#define BROTLI_MAX_DISTANCE_BITS 24U69#define BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX, NDIRECT, MAXNBITS) ( \70BROTLI_NUM_DISTANCE_SHORT_CODES + (NDIRECT) + \71((MAXNBITS) << ((NPOSTFIX) + 1)))72/* BROTLI_NUM_DISTANCE_SYMBOLS == 1128 */73#define BROTLI_NUM_DISTANCE_SYMBOLS \74BROTLI_DISTANCE_ALPHABET_SIZE( \75BROTLI_MAX_NDIRECT, BROTLI_MAX_NPOSTFIX, BROTLI_LARGE_MAX_DISTANCE_BITS)7677/* ((1 << 26) - 4) is the maximal distance that can be expressed in RFC 793278brotli stream using NPOSTFIX = 0 and NDIRECT = 0. With other NPOSTFIX and79NDIRECT values distances up to ((1 << 29) + 88) could be expressed. */80#define BROTLI_MAX_DISTANCE 0x3FFFFFC8182/* ((1 << 31) - 4) is the safe distance limit. Using this number as a limit83allows safe distance calculation without overflows, given the distance84alphabet size is limited to corresponding size85(see kLargeWindowDistanceCodeLimits). */86#define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC878889/* Specification: 4. Encoding of Literal Insertion Lengths and Copy Lengths */90#define BROTLI_NUM_INS_COPY_CODES 249192/* 7.1. Context modes and context ID lookup for literals */93/* "context IDs for literals are in the range of 0..63" */94#define BROTLI_LITERAL_CONTEXT_BITS 69596/* 7.2. Context ID for distances */97#define BROTLI_DISTANCE_CONTEXT_BITS 29899/* 9.1. Format of the Stream Header */100/* Number of slack bytes for window size. Don't confuse101with BROTLI_NUM_DISTANCE_SHORT_CODES. */102#define BROTLI_WINDOW_GAP 16103#define BROTLI_MAX_BACKWARD_LIMIT(W) (((size_t)1 << (W)) - BROTLI_WINDOW_GAP)104105typedef struct BrotliDistanceCodeLimit {106uint32_t max_alphabet_size;107uint32_t max_distance;108} BrotliDistanceCodeLimit;109110/* This function calculates maximal size of distance alphabet, such that the111distances greater than the given values can not be represented.112113This limits are designed to support fast and safe 32-bit decoders.114"32-bit" means that signed integer values up to ((1 << 31) - 1) could be115safely expressed.116117Brotli distance alphabet symbols do not represent consecutive distance118ranges. Each distance alphabet symbol (excluding direct distances and short119codes), represent interleaved (for NPOSTFIX > 0) range of distances.120A "group" of consecutive (1 << NPOSTFIX) symbols represent non-interleaved121range. Two consecutive groups require the same amount of "extra bits".122123It is important that distance alphabet represents complete "groups".124To avoid complex logic on encoder side about interleaved ranges125it was decided to restrict both sides to complete distance code "groups".126*/127BROTLI_UNUSED_FUNCTION BrotliDistanceCodeLimit BrotliCalculateDistanceCodeLimit(128uint32_t max_distance, uint32_t npostfix, uint32_t ndirect) {129BrotliDistanceCodeLimit result;130/* Marking this function as unused, because not all files131including "constants.h" use it -> compiler warns about that. */132BROTLI_UNUSED(&BrotliCalculateDistanceCodeLimit);133if (max_distance <= ndirect) {134/* This case never happens / exists only for the sake of completeness. */135result.max_alphabet_size = max_distance + BROTLI_NUM_DISTANCE_SHORT_CODES;136result.max_distance = max_distance;137return result;138} else {139/* The first prohibited value. */140uint32_t forbidden_distance = max_distance + 1;141/* Subtract "directly" encoded region. */142uint32_t offset = forbidden_distance - ndirect - 1;143uint32_t ndistbits = 0;144uint32_t tmp;145uint32_t half;146uint32_t group;147/* Postfix for the last dcode in the group. */148uint32_t postfix = (1u << npostfix) - 1;149uint32_t extra;150uint32_t start;151/* Remove postfix and "head-start". */152offset = (offset >> npostfix) + 4;153/* Calculate the number of distance bits. */154tmp = offset / 2;155/* Poor-man's log2floor, to avoid extra dependencies. */156while (tmp != 0) {ndistbits++; tmp = tmp >> 1;}157/* One bit is covered with subrange addressing ("half"). */158ndistbits--;159/* Find subrange. */160half = (offset >> ndistbits) & 1;161/* Calculate the "group" part of dcode. */162group = ((ndistbits - 1) << 1) | half;163/* Calculated "group" covers the prohibited distance value. */164if (group == 0) {165/* This case is added for correctness; does not occur for limit > 128. */166result.max_alphabet_size = ndirect + BROTLI_NUM_DISTANCE_SHORT_CODES;167result.max_distance = ndirect;168return result;169}170/* Decrement "group", so it is the last permitted "group". */171group--;172/* After group was decremented, ndistbits and half must be recalculated. */173ndistbits = (group >> 1) + 1;174/* The last available distance in the subrange has all extra bits set. */175extra = (1u << ndistbits) - 1;176/* Calculate region start. NB: ndistbits >= 1. */177start = (1u << (ndistbits + 1)) - 4;178/* Move to subregion. */179start += (group & 1) << ndistbits;180/* Calculate the alphabet size. */181result.max_alphabet_size = ((group << npostfix) | postfix) + ndirect +182BROTLI_NUM_DISTANCE_SHORT_CODES + 1;183/* Calculate the maximal distance representable by alphabet. */184result.max_distance = ((start + extra) << npostfix) + postfix + ndirect + 1;185return result;186}187}188189/* Represents the range of values belonging to a prefix code:190[offset, offset + 2^nbits) */191typedef struct {192uint16_t offset;193uint8_t nbits;194} BrotliPrefixCodeRange;195196/* "Soft-private", it is exported, but not "advertised" as API. */197BROTLI_COMMON_API extern const BrotliPrefixCodeRange198_kBrotliPrefixCodeRanges[BROTLI_NUM_BLOCK_LEN_SYMBOLS];199200#endif /* BROTLI_COMMON_CONSTANTS_H_ */201202203