Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/brotli/common/context.h
9906 views
1
/* Copyright 2013 Google Inc. All Rights Reserved.
2
3
Distributed under MIT license.
4
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
/* Lookup table to map the previous two bytes to a context id.
8
9
There are four different context modeling modes defined here:
10
CONTEXT_LSB6: context id is the least significant 6 bits of the last byte,
11
CONTEXT_MSB6: context id is the most significant 6 bits of the last byte,
12
CONTEXT_UTF8: second-order context model tuned for UTF8-encoded text,
13
CONTEXT_SIGNED: second-order context model tuned for signed integers.
14
15
If |p1| and |p2| are the previous two bytes, and |mode| is current context
16
mode, we calculate the context as:
17
18
context = ContextLut(mode)[p1] | ContextLut(mode)[p2 + 256].
19
20
For CONTEXT_UTF8 mode, if the previous two bytes are ASCII characters
21
(i.e. < 128), this will be equivalent to
22
23
context = 4 * context1(p1) + context2(p2),
24
25
where context1 is based on the previous byte in the following way:
26
27
0 : non-ASCII control
28
1 : \t, \n, \r
29
2 : space
30
3 : other punctuation
31
4 : " '
32
5 : %
33
6 : ( < [ {
34
7 : ) > ] }
35
8 : , ; :
36
9 : .
37
10 : =
38
11 : number
39
12 : upper-case vowel
40
13 : upper-case consonant
41
14 : lower-case vowel
42
15 : lower-case consonant
43
44
and context2 is based on the second last byte:
45
46
0 : control, space
47
1 : punctuation
48
2 : upper-case letter, number
49
3 : lower-case letter
50
51
If the last byte is ASCII, and the second last byte is not (in a valid UTF8
52
stream it will be a continuation byte, value between 128 and 191), the
53
context is the same as if the second last byte was an ASCII control or space.
54
55
If the last byte is a UTF8 lead byte (value >= 192), then the next byte will
56
be a continuation byte and the context id is 2 or 3 depending on the LSB of
57
the last byte and to a lesser extent on the second last byte if it is ASCII.
58
59
If the last byte is a UTF8 continuation byte, the second last byte can be:
60
- continuation byte: the next byte is probably ASCII or lead byte (assuming
61
4-byte UTF8 characters are rare) and the context id is 0 or 1.
62
- lead byte (192 - 207): next byte is ASCII or lead byte, context is 0 or 1
63
- lead byte (208 - 255): next byte is continuation byte, context is 2 or 3
64
65
The possible value combinations of the previous two bytes, the range of
66
context ids and the type of the next byte is summarized in the table below:
67
68
|--------\-----------------------------------------------------------------|
69
| \ Last byte |
70
| Second \---------------------------------------------------------------|
71
| last byte \ ASCII | cont. byte | lead byte |
72
| \ (0-127) | (128-191) | (192-) |
73
|=============|===================|=====================|==================|
74
| ASCII | next: ASCII/lead | not valid | next: cont. |
75
| (0-127) | context: 4 - 63 | | context: 2 - 3 |
76
|-------------|-------------------|---------------------|------------------|
77
| cont. byte | next: ASCII/lead | next: ASCII/lead | next: cont. |
78
| (128-191) | context: 4 - 63 | context: 0 - 1 | context: 2 - 3 |
79
|-------------|-------------------|---------------------|------------------|
80
| lead byte | not valid | next: ASCII/lead | not valid |
81
| (192-207) | | context: 0 - 1 | |
82
|-------------|-------------------|---------------------|------------------|
83
| lead byte | not valid | next: cont. | not valid |
84
| (208-) | | context: 2 - 3 | |
85
|-------------|-------------------|---------------------|------------------|
86
*/
87
88
#ifndef BROTLI_COMMON_CONTEXT_H_
89
#define BROTLI_COMMON_CONTEXT_H_
90
91
#include <brotli/port.h>
92
#include <brotli/types.h>
93
94
typedef enum ContextType {
95
CONTEXT_LSB6 = 0,
96
CONTEXT_MSB6 = 1,
97
CONTEXT_UTF8 = 2,
98
CONTEXT_SIGNED = 3
99
} ContextType;
100
101
/* "Soft-private", it is exported, but not "advertised" as API. */
102
/* Common context lookup table for all context modes. */
103
BROTLI_COMMON_API extern const uint8_t _kBrotliContextLookupTable[2048];
104
105
typedef const uint8_t* ContextLut;
106
107
/* typeof(MODE) == ContextType; returns ContextLut */
108
#define BROTLI_CONTEXT_LUT(MODE) (&_kBrotliContextLookupTable[(MODE) << 9])
109
110
/* typeof(LUT) == ContextLut */
111
#define BROTLI_CONTEXT(P1, P2, LUT) ((LUT)[P1] | ((LUT) + 256)[P2])
112
113
#endif /* BROTLI_COMMON_CONTEXT_H_ */
114
115