Path: blob/master/thirdparty/brotli/common/constants.h
21758 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 "platform.h"1516/* Specification: 7.3. Encoding of the context map */17#define BROTLI_CONTEXT_MAP_MAX_RLE 161819/* Specification: 2. Compressed representation overview */20#define BROTLI_MAX_NUMBER_OF_BLOCK_TYPES 2562122/* Specification: 3.3. Alphabet sizes: insert-and-copy length */23#define BROTLI_NUM_LITERAL_SYMBOLS 25624#define BROTLI_NUM_COMMAND_SYMBOLS 70425#define BROTLI_NUM_BLOCK_LEN_SYMBOLS 2626#define BROTLI_MAX_CONTEXT_MAP_SYMBOLS (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + \27BROTLI_CONTEXT_MAP_MAX_RLE)28#define BROTLI_MAX_BLOCK_TYPE_SYMBOLS (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + 2)2930/* Specification: 3.5. Complex prefix codes */31#define BROTLI_REPEAT_PREVIOUS_CODE_LENGTH 1632#define BROTLI_REPEAT_ZERO_CODE_LENGTH 1733#define BROTLI_CODE_LENGTH_CODES (BROTLI_REPEAT_ZERO_CODE_LENGTH + 1)34/* "code length of 8 is repeated" */35#define BROTLI_INITIAL_REPEATED_CODE_LENGTH 83637/* "Large Window Brotli" */3839/**40* The theoretical maximum number of distance bits specified for large window41* brotli, for 64-bit encoders and decoders. Even when in practice 32-bit42* encoders and decoders only support up to 30 max distance bits, the value is43* set to 62 because it affects the large window brotli file format.44* Specifically, it affects the encoding of simple huffman tree for distances,45* see Specification RFC 7932 chapter 3.4.46*/47#define BROTLI_LARGE_MAX_DISTANCE_BITS 62U48#define BROTLI_LARGE_MIN_WBITS 1049/**50* The maximum supported large brotli window bits by the encoder and decoder.51* Large window brotli allows up to 62 bits, however the current encoder and52* decoder, designed for 32-bit integers, only support up to 30 bits maximum.53*/54#define BROTLI_LARGE_MAX_WBITS 305556/* Specification: 4. Encoding of distances */57#define BROTLI_NUM_DISTANCE_SHORT_CODES 1658/**59* Maximal number of "postfix" bits.60*61* Number of "postfix" bits is stored as 2 bits in meta-block header.62*/63#define BROTLI_MAX_NPOSTFIX 364#define BROTLI_MAX_NDIRECT 12065#define BROTLI_MAX_DISTANCE_BITS 24U66#define BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX, NDIRECT, MAXNBITS) ( \67BROTLI_NUM_DISTANCE_SHORT_CODES + (NDIRECT) + \68((MAXNBITS) << ((NPOSTFIX) + 1)))69/* BROTLI_NUM_DISTANCE_SYMBOLS == 1128 */70#define BROTLI_NUM_DISTANCE_SYMBOLS \71BROTLI_DISTANCE_ALPHABET_SIZE( \72BROTLI_MAX_NDIRECT, BROTLI_MAX_NPOSTFIX, BROTLI_LARGE_MAX_DISTANCE_BITS)7374/* ((1 << 26) - 4) is the maximal distance that can be expressed in RFC 793275brotli stream using NPOSTFIX = 0 and NDIRECT = 0. With other NPOSTFIX and76NDIRECT values distances up to ((1 << 29) + 88) could be expressed. */77#define BROTLI_MAX_DISTANCE 0x3FFFFFC7879/* ((1 << 31) - 4) is the safe distance limit. Using this number as a limit80allows safe distance calculation without overflows, given the distance81alphabet size is limited to corresponding size82(see kLargeWindowDistanceCodeLimits). */83#define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC848586/* Specification: 4. Encoding of Literal Insertion Lengths and Copy Lengths */87#define BROTLI_NUM_INS_COPY_CODES 248889/* 7.1. Context modes and context ID lookup for literals */90/* "context IDs for literals are in the range of 0..63" */91#define BROTLI_LITERAL_CONTEXT_BITS 69293/* 7.2. Context ID for distances */94#define BROTLI_DISTANCE_CONTEXT_BITS 29596/* 9.1. Format of the Stream Header */97/* Number of slack bytes for window size. Don't confuse98with BROTLI_NUM_DISTANCE_SHORT_CODES. */99#define BROTLI_WINDOW_GAP 16100#define BROTLI_MAX_BACKWARD_LIMIT(W) (((size_t)1 << (W)) - BROTLI_WINDOW_GAP)101102typedef struct BrotliDistanceCodeLimit {103uint32_t max_alphabet_size;104uint32_t max_distance;105} BrotliDistanceCodeLimit;106107/* This function calculates maximal size of distance alphabet, such that the108distances greater than the given values can not be represented.109110This limits are designed to support fast and safe 32-bit decoders.111"32-bit" means that signed integer values up to ((1 << 31) - 1) could be112safely expressed.113114Brotli distance alphabet symbols do not represent consecutive distance115ranges. Each distance alphabet symbol (excluding direct distances and short116codes), represent interleaved (for NPOSTFIX > 0) range of distances.117A "group" of consecutive (1 << NPOSTFIX) symbols represent non-interleaved118range. Two consecutive groups require the same amount of "extra bits".119120It is important that distance alphabet represents complete "groups".121To avoid complex logic on encoder side about interleaved ranges122it was decided to restrict both sides to complete distance code "groups".123*/124BROTLI_UNUSED_FUNCTION BrotliDistanceCodeLimit BrotliCalculateDistanceCodeLimit(125uint32_t max_distance, uint32_t npostfix, uint32_t ndirect) {126BrotliDistanceCodeLimit result;127/* Marking this function as unused, because not all files128including "constants.h" use it -> compiler warns about that. */129BROTLI_UNUSED(&BrotliCalculateDistanceCodeLimit);130if (max_distance <= ndirect) {131/* This case never happens / exists only for the sake of completeness. */132result.max_alphabet_size = max_distance + BROTLI_NUM_DISTANCE_SHORT_CODES;133result.max_distance = max_distance;134return result;135} else {136/* The first prohibited value. */137uint32_t forbidden_distance = max_distance + 1;138/* Subtract "directly" encoded region. */139uint32_t offset = forbidden_distance - ndirect - 1;140uint32_t ndistbits = 0;141uint32_t tmp;142uint32_t half;143uint32_t group;144/* Postfix for the last dcode in the group. */145uint32_t postfix = (1u << npostfix) - 1;146uint32_t extra;147uint32_t start;148/* Remove postfix and "head-start". */149offset = (offset >> npostfix) + 4;150/* Calculate the number of distance bits. */151tmp = offset / 2;152/* Poor-man's log2floor, to avoid extra dependencies. */153while (tmp != 0) {ndistbits++; tmp = tmp >> 1;}154/* One bit is covered with subrange addressing ("half"). */155ndistbits--;156/* Find subrange. */157half = (offset >> ndistbits) & 1;158/* Calculate the "group" part of dcode. */159group = ((ndistbits - 1) << 1) | half;160/* Calculated "group" covers the prohibited distance value. */161if (group == 0) {162/* This case is added for correctness; does not occur for limit > 128. */163result.max_alphabet_size = ndirect + BROTLI_NUM_DISTANCE_SHORT_CODES;164result.max_distance = ndirect;165return result;166}167/* Decrement "group", so it is the last permitted "group". */168group--;169/* After group was decremented, ndistbits and half must be recalculated. */170ndistbits = (group >> 1) + 1;171/* The last available distance in the subrange has all extra bits set. */172extra = (1u << ndistbits) - 1;173/* Calculate region start. NB: ndistbits >= 1. */174start = (1u << (ndistbits + 1)) - 4;175/* Move to subregion. */176start += (group & 1) << ndistbits;177/* Calculate the alphabet size. */178result.max_alphabet_size = ((group << npostfix) | postfix) + ndirect +179BROTLI_NUM_DISTANCE_SHORT_CODES + 1;180/* Calculate the maximal distance representable by alphabet. */181result.max_distance = ((start + extra) << npostfix) + postfix + ndirect + 1;182return result;183}184}185186/* Represents the range of values belonging to a prefix code:187[offset, offset + 2^nbits) */188typedef struct {189uint16_t offset;190uint8_t nbits;191} BrotliPrefixCodeRange;192193/* "Soft-private", it is exported, but not "advertised" as API. */194BROTLI_COMMON_API extern const BROTLI_MODEL("small")195BrotliPrefixCodeRange _kBrotliPrefixCodeRanges[BROTLI_NUM_BLOCK_LEN_SYMBOLS];196197#endif /* BROTLI_COMMON_CONSTANTS_H_ */198199200