/* Copyright 2013 Google Inc. All Rights Reserved.12Distributed under MIT license.3See file LICENSE for detail or copy at https://opensource.org/licenses/MIT4*/56/* Lookup table to map the previous two bytes to a context id.78There are four different context modeling modes defined here:9CONTEXT_LSB6: context id is the least significant 6 bits of the last byte,10CONTEXT_MSB6: context id is the most significant 6 bits of the last byte,11CONTEXT_UTF8: second-order context model tuned for UTF8-encoded text,12CONTEXT_SIGNED: second-order context model tuned for signed integers.1314If |p1| and |p2| are the previous two bytes, and |mode| is current context15mode, we calculate the context as:1617context = ContextLut(mode)[p1] | ContextLut(mode)[p2 + 256].1819For CONTEXT_UTF8 mode, if the previous two bytes are ASCII characters20(i.e. < 128), this will be equivalent to2122context = 4 * context1(p1) + context2(p2),2324where context1 is based on the previous byte in the following way:25260 : non-ASCII control271 : \t, \n, \r282 : space293 : other punctuation304 : " '315 : %326 : ( < [ {337 : ) > ] }348 : , ; :359 : .3610 : =3711 : number3812 : upper-case vowel3913 : upper-case consonant4014 : lower-case vowel4115 : lower-case consonant4243and context2 is based on the second last byte:44450 : control, space461 : punctuation472 : upper-case letter, number483 : lower-case letter4950If the last byte is ASCII, and the second last byte is not (in a valid UTF851stream it will be a continuation byte, value between 128 and 191), the52context is the same as if the second last byte was an ASCII control or space.5354If the last byte is a UTF8 lead byte (value >= 192), then the next byte will55be a continuation byte and the context id is 2 or 3 depending on the LSB of56the last byte and to a lesser extent on the second last byte if it is ASCII.5758If the last byte is a UTF8 continuation byte, the second last byte can be:59- continuation byte: the next byte is probably ASCII or lead byte (assuming604-byte UTF8 characters are rare) and the context id is 0 or 1.61- lead byte (192 - 207): next byte is ASCII or lead byte, context is 0 or 162- lead byte (208 - 255): next byte is continuation byte, context is 2 or 36364The possible value combinations of the previous two bytes, the range of65context ids and the type of the next byte is summarized in the table below:6667|--------\-----------------------------------------------------------------|68| \ Last byte |69| Second \---------------------------------------------------------------|70| last byte \ ASCII | cont. byte | lead byte |71| \ (0-127) | (128-191) | (192-) |72|=============|===================|=====================|==================|73| ASCII | next: ASCII/lead | not valid | next: cont. |74| (0-127) | context: 4 - 63 | | context: 2 - 3 |75|-------------|-------------------|---------------------|------------------|76| cont. byte | next: ASCII/lead | next: ASCII/lead | next: cont. |77| (128-191) | context: 4 - 63 | context: 0 - 1 | context: 2 - 3 |78|-------------|-------------------|---------------------|------------------|79| lead byte | not valid | next: ASCII/lead | not valid |80| (192-207) | | context: 0 - 1 | |81|-------------|-------------------|---------------------|------------------|82| lead byte | not valid | next: cont. | not valid |83| (208-) | | context: 2 - 3 | |84|-------------|-------------------|---------------------|------------------|85*/8687#ifndef BROTLI_COMMON_CONTEXT_H_88#define BROTLI_COMMON_CONTEXT_H_8990#include <brotli/port.h>91#include <brotli/types.h>9293typedef enum ContextType {94CONTEXT_LSB6 = 0,95CONTEXT_MSB6 = 1,96CONTEXT_UTF8 = 2,97CONTEXT_SIGNED = 398} ContextType;99100/* "Soft-private", it is exported, but not "advertised" as API. */101/* Common context lookup table for all context modes. */102BROTLI_COMMON_API extern const uint8_t _kBrotliContextLookupTable[2048];103104typedef const uint8_t* ContextLut;105106/* typeof(MODE) == ContextType; returns ContextLut */107#define BROTLI_CONTEXT_LUT(MODE) (&_kBrotliContextLookupTable[(MODE) << 9])108109/* typeof(LUT) == ContextLut */110#define BROTLI_CONTEXT(P1, P2, LUT) ((LUT)[P1] | ((LUT) + 256)[P2])111112#endif /* BROTLI_COMMON_CONTEXT_H_ */113114115