[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-17。"],[],[],null,["# Compression\n===========\n\nAbout compression\n-----------------\n\nCompression is a key feature of Web Risk. Compression\nsignificantly reduces bandwidth requirements, which is particularly\nrelevant for mobile devices. The Web Risk server currently\nsupports Rice compression. More compression methods might be added in the\nfuture.\n\nCompression is set using the\n[`supportedCompressions`](/web-risk/docs/reference/rest/v1/threatLists/computeDiff#constraints)\nfield and\n[`CompressionType`](/web-risk/docs/reference/rest/v1/threatLists/computeDiff#compressiontype).\nClients should use the RICE and RAW compression types. Web Risk uses the\n`COMPRESSION_TYPE_UNSPECIFIED` type when the compression type is not set (RAW\ncompression will be substituted).\n\nThe Web Risk server will also use standard HTTP compression to further\ncompress responses, regardless of the compression type selected, as long as the\nclient sets the correct HTTP compression header. To learn more, see the\nWikipedia article on [HTTP compression](https://en.wikipedia.org/wiki/HTTP_compression).\n\nRice compression\n----------------\n\nAs noted, the Web Risk server currently supports Rice compression.\nFor more inforomation, see the Wikipedia article [Golomb\ncoding](https://en.wikipedia.org/wiki/Golomb_coding).\n\n### Compression/decompression\n\nThe\n[`RiceDeltaEncoding`](/web-risk/docs/reference/rest/v1/threatLists/computeDiff#ricedeltaencoding)\nobject represents the Rice-Golomb encoded data and is used to send compressed\nremoval indices or compressed 4-byte hash prefixes. Hash prefixes longer than\n4 bytes will not be compressed, and will be served in raw format instead.\n\nFor removal indices, the list of indices is sorted in ascending order and then\ndelta encoded using RICE encoding. For additions, the 4-byte hash prefixes are\nre-interpreted as little-endian uint32s, sorted in ascending order, and then\ndelta encoded using RICE encoding. Note the difference in hash format between\nRICE compression and RAW: raw hashes are lexicographically sorted bytes, whereas\nRICE hashes are uint32s sorted in acsending order (after decompression).\n\nThat is, the list of integers \\[1, 5, 7, 13\\] will be encoded as 1 (the first\nvalue) and the deltas \\[4, 2, 6\\].\n\nThe first value is stored in the `firstValue` field and the deltas are encoded\nusing a Golomb-Rice encoder. The Rice parameter `k` (see below) is stored in\n`riceParameter`. The `numEntries` field contains the number of deltas encoded\nin the Rice encoder (3 in our example above, not 4). The `encodedData` field\ncontains the actual encoded deltas.\n\n### Encoder/decoder\n\nIn the Rice encoder/decoder every delta `n` is encoded as `q` and `r` where\n`n = (q\u003c\u003ck) + r` (or, `n = q * (2**k) + r`). `k` is a constant and a parameter\nof the Rice encoder/decoder. The values for `q` and `r` are encoded in the bit\nstream using different encoding schemes.\n\nThe quotient `q` is encoded in unary coding followed by a 0. That is, 3 would be\nencoded as 1110, 4 as 11110 and 7 as 11111110. The quotient `q` is decoded\nfirst.\n\nThe remainder `r` is encoded using truncated binary encoding. Only the least\nsignificant `k` bits of `r` are written (and therefore read) from the bit\nstream. The remainder `r` is decoded after having decoded `q`.\n\n### Bit encoder/decoder\n\nThe Rice encoder relies on a bit encoder/decoder where single bits can be\nappended to the bit encoder; that is, to encode a quotient `q` that could be\nonly two bits long.\n\nThe bit encoder is a list of 8-bit bytes. Bits are set from the lowest\nsignificant bit in the first byte to the highest significant bit in the first\nbyte. If a byte has all its bits already set, a new byte, initialized to zero,\nis appended to the end of the byte list. If the last byte is not fully used, its\nhighest significant bits are set to zero. Example:"]]