+++ /dev/null
-/* Copyright 2013 Google Inc. All Rights Reserved.\r
-\r
- Distributed under MIT license.\r
- See file LICENSE for detail or copy at https://opensource.org/licenses/MIT\r
-*/\r
-\r
-/* Functions to map previous bytes into a context id. */\r
-\r
-#ifndef BROTLI_ENC_CONTEXT_H_\r
-#define BROTLI_ENC_CONTEXT_H_\r
-\r
-#include "../common/types.h"\r
-#include "../common/port.h"\r
-\r
-#if defined(__cplusplus) || defined(c_plusplus)\r
-extern "C" {\r
-#endif\r
-\r
-/* Second-order context lookup table for UTF8 byte streams.\r
-\r
- If p1 and p2 are the previous two bytes, we calculate the context as\r
-\r
- context = kUTF8ContextLookup[p1] | kUTF8ContextLookup[p2 + 256].\r
-\r
- If the previous two bytes are ASCII characters (i.e. < 128), this will be\r
- equivalent to\r
-\r
- context = 4 * context1(p1) + context2(p2),\r
-\r
- where context1 is based on the previous byte in the following way:\r
-\r
- 0 : non-ASCII control\r
- 1 : \t, \n, \r\r
- 2 : space\r
- 3 : other punctuation\r
- 4 : " '\r
- 5 : %\r
- 6 : ( < [ {\r
- 7 : ) > ] }\r
- 8 : , ; :\r
- 9 : .\r
- 10 : =\r
- 11 : number\r
- 12 : upper-case vowel\r
- 13 : upper-case consonant\r
- 14 : lower-case vowel\r
- 15 : lower-case consonant\r
-\r
- and context2 is based on the second last byte:\r
-\r
- 0 : control, space\r
- 1 : punctuation\r
- 2 : upper-case letter, number\r
- 3 : lower-case letter\r
-\r
- If the last byte is ASCII, and the second last byte is not (in a valid UTF8\r
- stream it will be a continuation byte, value between 128 and 191), the\r
- context is the same as if the second last byte was an ASCII control or space.\r
-\r
- If the last byte is a UTF8 lead byte (value >= 192), then the next byte will\r
- be a continuation byte and the context id is 2 or 3 depending on the LSB of\r
- the last byte and to a lesser extent on the second last byte if it is ASCII.\r
-\r
- If the last byte is a UTF8 continuation byte, the second last byte can be:\r
- - continuation byte: the next byte is probably ASCII or lead byte (assuming\r
- 4-byte UTF8 characters are rare) and the context id is 0 or 1.\r
- - lead byte (192 - 207): next byte is ASCII or lead byte, context is 0 or 1\r
- - lead byte (208 - 255): next byte is continuation byte, context is 2 or 3\r
-\r
- The possible value combinations of the previous two bytes, the range of\r
- context ids and the type of the next byte is summarized in the table below:\r
-\r
- |--------\-----------------------------------------------------------------|\r
- | \ Last byte |\r
- | Second \---------------------------------------------------------------|\r
- | last byte \ ASCII | cont. byte | lead byte |\r
- | \ (0-127) | (128-191) | (192-) |\r
- |=============|===================|=====================|==================|\r
- | ASCII | next: ASCII/lead | not valid | next: cont. |\r
- | (0-127) | context: 4 - 63 | | context: 2 - 3 |\r
- |-------------|-------------------|---------------------|------------------|\r
- | cont. byte | next: ASCII/lead | next: ASCII/lead | next: cont. |\r
- | (128-191) | context: 4 - 63 | context: 0 - 1 | context: 2 - 3 |\r
- |-------------|-------------------|---------------------|------------------|\r
- | lead byte | not valid | next: ASCII/lead | not valid |\r
- | (192-207) | | context: 0 - 1 | |\r
- |-------------|-------------------|---------------------|------------------|\r
- | lead byte | not valid | next: cont. | not valid |\r
- | (208-) | | context: 2 - 3 | |\r
- |-------------|-------------------|---------------------|------------------|\r
-*/\r
-static const uint8_t kUTF8ContextLookup[512] = {\r
- /* Last byte. */\r
- /* */\r
- /* ASCII range. */\r
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 4, 0, 0,\r
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r
- 8, 12, 16, 12, 12, 20, 12, 16, 24, 28, 12, 12, 32, 12, 36, 12,\r
- 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 32, 32, 24, 40, 28, 12,\r
- 12, 48, 52, 52, 52, 48, 52, 52, 52, 48, 52, 52, 52, 52, 52, 48,\r
- 52, 52, 52, 52, 52, 48, 52, 52, 52, 52, 52, 24, 12, 28, 12, 12,\r
- 12, 56, 60, 60, 60, 56, 60, 60, 60, 56, 60, 60, 60, 60, 60, 56,\r
- 60, 60, 60, 60, 60, 56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 4, 4, 4, 4, 4, 4,\r
- 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,\r
- 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,\r
- 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,\r
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,\r
-};\r
-\r
-typedef enum ContextType {\r
- CONTEXT_LSB6 = 0,\r
- CONTEXT_MSB6 = 1,\r
- CONTEXT_UTF8 = 2,\r
- CONTEXT_SIGNED = 3\r
-} ContextType;\r
-\r
-static BROTLI_INLINE uint8_t Context(uint8_t p1, uint8_t p2, ContextType mode) {\r
- switch (mode) {\r
- case CONTEXT_LSB6:\r
- return p1 & 0x3f;\r
- case CONTEXT_MSB6:\r
- return (uint8_t)(p1 >> 2);\r
- case CONTEXT_UTF8:\r
- return kUTF8ContextLookup[p1] | kUTF8ContextLookup[p2 + 256];\r
- case CONTEXT_SIGNED:\r
- return (uint8_t)((kSigned3BitContextLookup[p1] << 3) +\r
- kSigned3BitContextLookup[p2]);\r
- default:\r
- return 0;\r
- }\r
-}\r
-\r
-#if defined(__cplusplus) || defined(c_plusplus)\r
-} /* extern "C" */\r
-#endif\r
-\r
-#endif /* BROTLI_ENC_CONTEXT_H_ */\r