| 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 | /* This class models a sequence of literals and a backward reference copy. */ |
| 8 | |
| 9 | #ifndef BROTLI_ENC_COMMAND_H_ |
| 10 | #define BROTLI_ENC_COMMAND_H_cxx . . $ `c/enc/command.h:10:9`! |
| 11 | |
| 12 | #include "../common/constants.h"cxx . . $ `<file>/c/common/constants.h`/ |
| 13 | #include "../common/platform.h"cxx . . $ `<file>/c/common/platform.h`/ |
| 14 | #include "fast_log.h"cxx . . $ `<file>/c/enc/fast_log.h`/ |
| 15 | #include "params.h"cxx . . $ `<file>/c/enc/params.h`/ |
| 16 | #include "prefix.h"cxx . . $ `<file>/c/enc/prefix.h`/ |
| 17 | |
| 18 | #if defined(__cplusplus) || defined(c_plusplus) |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
| 22 | BROTLI_INTERNALcxx . . $ `c/include/brotli/port.h:252:9`! extern const BROTLI_MODELcxx . . $ `c/common/platform.h:674:9`!("small") |
| 23 | uint32_tcxx . . $ uint32_t# kBrotliInsBasecxx . . $ kBrotliInsBase.[BROTLI_NUM_INS_COPY_CODEScxx . . $ `c/common/constants.h:88:9`!]; |
| 24 | BROTLI_INTERNALcxx . . $ `c/include/brotli/port.h:252:9`! extern const BROTLI_MODELcxx . . $ `c/common/platform.h:674:9`!("small") |
| 25 | uint32_tcxx . . $ uint32_t# kBrotliInsExtracxx . . $ kBrotliInsExtra.[BROTLI_NUM_INS_COPY_CODEScxx . . $ `c/common/constants.h:88:9`!]; |
| 26 | BROTLI_INTERNALcxx . . $ `c/include/brotli/port.h:252:9`! extern const BROTLI_MODELcxx . . $ `c/common/platform.h:674:9`!("small") |
| 27 | uint32_tcxx . . $ uint32_t# kBrotliCopyBasecxx . . $ kBrotliCopyBase.[BROTLI_NUM_INS_COPY_CODEScxx . . $ `c/common/constants.h:88:9`!]; |
| 28 | BROTLI_INTERNALcxx . . $ `c/include/brotli/port.h:252:9`! extern const BROTLI_MODELcxx . . $ `c/common/platform.h:674:9`!("small") |
| 29 | uint32_tcxx . . $ uint32_t# kBrotliCopyExtracxx . . $ kBrotliCopyExtra.[BROTLI_NUM_INS_COPY_CODEScxx . . $ `c/common/constants.h:88:9`!]; |
| 30 | |
| 31 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint16_tcxx . . $ uint16_t# GetInsertLengthCodecxx . . $ GetInsertLengthCode(91f8ed6d6bcecca7).(size_t insertlen) { |
| 32 | if (insertlen < 6) { |
| 33 | return (uint16_tcxx . . $ uint16_t#)insertlen; |
| 34 | } else if (insertlen < 130) { |
| 35 | uint32_tcxx . . $ uint32_t# nbits = Log2FloorNonZerocxx . . $ Log2FloorNonZero(ea88b852781e8a38).(insertlen - 2) - 1u; |
| 36 | return (uint16_tcxx . . $ uint16_t#)((nbits << 1) + ((insertlen - 2) >> nbits) + 2); |
| 37 | } else if (insertlen < 2114) { |
| 38 | return (uint16_tcxx . . $ uint16_t#)(Log2FloorNonZerocxx . . $ Log2FloorNonZero(ea88b852781e8a38).(insertlen - 66) + 10); |
| 39 | } else if (insertlen < 6210) { |
| 40 | return 21u; |
| 41 | } else if (insertlen < 22594) { |
| 42 | return 22u; |
| 43 | } else { |
| 44 | return 23u; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint16_tcxx . . $ uint16_t# GetCopyLengthCodecxx . . $ GetCopyLengthCode(91f8ed6d6bcecca7).(size_t copylen) { |
| 49 | if (copylen < 10) { |
| 50 | return (uint16_tcxx . . $ uint16_t#)(copylen - 2); |
| 51 | } else if (copylen < 134) { |
| 52 | uint32_tcxx . . $ uint32_t# nbits = Log2FloorNonZerocxx . . $ Log2FloorNonZero(ea88b852781e8a38).(copylen - 6) - 1u; |
| 53 | return (uint16_tcxx . . $ uint16_t#)((nbits << 1) + ((copylen - 6) >> nbits) + 4); |
| 54 | } else if (copylen < 2118) { |
| 55 | return (uint16_tcxx . . $ uint16_t#)(Log2FloorNonZerocxx . . $ Log2FloorNonZero(ea88b852781e8a38).(copylen - 70) + 12); |
| 56 | } else { |
| 57 | return 23u; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint16_tcxx . . $ uint16_t# CombineLengthCodescxx . . $ CombineLengthCodes(e376afdf389bac60).( |
| 62 | uint16_tcxx . . $ uint16_t# inscode, uint16_tcxx . . $ uint16_t# copycode, BROTLI_BOOLcxx . . $ `c/include/brotli/types.h:49:9`! use_last_distance) { |
| 63 | uint16_tcxx . . $ uint16_t# bits64 = |
| 64 | (uint16_tcxx . . $ uint16_t#)((copycode & 0x7u) | ((inscode & 0x7u) << 3u)); |
| 65 | if (use_last_distance && inscode < 8u && copycode < 16u) { |
| 66 | return (copycode < 8u) ? bits64 : (bits64 | 64u); |
| 67 | } else { |
| 68 | /* Specification: 5 Encoding of ... (last table) */ |
| 69 | /* offset = 2 * index, where index is in range [0..8] */ |
| 70 | uint32_tcxx . . $ uint32_t# offset = 2u * ((copycode >> 3u) + 3u * (inscode >> 3u)); |
| 71 | /* All values in specification are K * 64, |
| 72 | where K = [2, 3, 6, 4, 5, 8, 7, 9, 10], |
| 73 | i + 1 = [1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 74 | K - i - 1 = [1, 1, 3, 0, 0, 2, 0, 1, 2] = D. |
| 75 | All values in D require only 2 bits to encode. |
| 76 | Magic constant is shifted 6 bits left, to avoid final multiplication. */ |
| 77 | offset = (offset << 5u) + 0x40u + ((0x520D40u >> offset) & 0xC0u); |
| 78 | return (uint16_tcxx . . $ uint16_t#)(offset | bits64); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void GetLengthCodecxx . . $ GetLengthCode(880176f624c20427).(size_t insertlen, size_t copylen, |
| 83 | BROTLI_BOOLcxx . . $ `c/include/brotli/types.h:49:9`! use_last_distance, |
| 84 | uint16_tcxx . . $ uint16_t#* code) { |
| 85 | uint16_tcxx . . $ uint16_t# inscode = GetInsertLengthCodecxx . . $ GetInsertLengthCode(91f8ed6d6bcecca7).(insertlen); |
| 86 | uint16_tcxx . . $ uint16_t# copycode = GetCopyLengthCodecxx . . $ GetCopyLengthCode(91f8ed6d6bcecca7).(copylen); |
| 87 | *code = CombineLengthCodescxx . . $ CombineLengthCodes(e376afdf389bac60).(inscode, copycode, use_last_distance); |
| 88 | } |
| 89 | |
| 90 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint32_tcxx . . $ uint32_t# GetInsertBasecxx . . $ GetInsertBase(909d59720f161188).(uint16_tcxx . . $ uint16_t# inscode) { |
| 91 | return kBrotliInsBasecxx . . $ kBrotliInsBase.[inscode]; |
| 92 | } |
| 93 | |
| 94 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint32_tcxx . . $ uint32_t# GetInsertExtracxx . . $ GetInsertExtra(909d59720f161188).(uint16_tcxx . . $ uint16_t# inscode) { |
| 95 | return kBrotliInsExtracxx . . $ kBrotliInsExtra.[inscode]; |
| 96 | } |
| 97 | |
| 98 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint32_tcxx . . $ uint32_t# GetCopyBasecxx . . $ GetCopyBase(909d59720f161188).(uint16_tcxx . . $ uint16_t# copycode) { |
| 99 | return kBrotliCopyBasecxx . . $ kBrotliCopyBase.[copycode]; |
| 100 | } |
| 101 | |
| 102 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint32_tcxx . . $ uint32_t# GetCopyExtracxx . . $ GetCopyExtra(909d59720f161188).(uint16_tcxx . . $ uint16_t# copycode) { |
| 103 | return kBrotliCopyExtracxx . . $ kBrotliCopyExtra.[copycode]; |
| 104 | } |
| 105 | |
| 106 | typedef struct Commandcxx . . $ Command# { |
| 107 | uint32_tcxx . . $ uint32_t# insert_len_cxx . . $ Command#insert_len_.; |
| 108 | /* Stores copy_len in low 25 bits and copy_code - copy_len in high 7 bit. */ |
| 109 | uint32_tcxx . . $ uint32_t# copy_len_cxx . . $ Command#copy_len_.; |
| 110 | /* Stores distance extra bits. */ |
| 111 | uint32_tcxx . . $ uint32_t# dist_extra_cxx . . $ Command#dist_extra_.; |
| 112 | uint16_tcxx . . $ uint16_t# cmd_prefix_cxx . . $ Command#cmd_prefix_.; |
| 113 | /* Stores distance code in low 10 bits |
| 114 | and number of extra bits in high 6 bits. */ |
| 115 | uint16_tcxx . . $ uint16_t# dist_prefix_cxx . . $ Command#dist_prefix_.; |
| 116 | } Commandcxx . . $ Command#; |
| 117 | |
| 118 | /* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */ |
| 119 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void InitCommandcxx . . $ InitCommand(a0b400081f9cd273).(Commandcxx . . $ Command#* self, |
| 120 | const BrotliDistanceParamscxx . . $ BrotliDistanceParams#* dist, size_t insertlen, |
| 121 | size_t copylen, int copylen_code_delta, size_t distance_code) { |
| 122 | /* Don't rely on signed int representation, use honest casts. */ |
| 123 | uint32_tcxx . . $ uint32_t# delta = (uint8_tcxx . . $ uint8_t#)((int8_tcxx . . $ int8_t#)copylen_code_delta); |
| 124 | self->insert_len_cxx . . $ Command#insert_len_. = (uint32_tcxx . . $ uint32_t#)insertlen; |
| 125 | self->copy_len_cxx . . $ Command#copy_len_. = (uint32_tcxx . . $ uint32_t#)(copylen | (delta << 25)); |
| 126 | /* The distance prefix and extra bits are stored in this Command as if |
| 127 | npostfix and ndirect were 0, they are only recomputed later after the |
| 128 | clustering if needed. */ |
| 129 | PrefixEncodeCopyDistancecxx . . $ PrefixEncodeCopyDistance(ddce29df8a9022a3).( |
| 130 | distance_code, dist->num_direct_distance_codescxx . . $ BrotliDistanceParams#num_direct_distance_codes., |
| 131 | dist->distance_postfix_bitscxx . . $ BrotliDistanceParams#distance_postfix_bits., &self->dist_prefix_cxx . . $ Command#dist_prefix_., &self->dist_extra_cxx . . $ Command#dist_extra_.); |
| 132 | GetLengthCodecxx . . $ GetLengthCode(880176f624c20427).( |
| 133 | insertlen, (size_t)((int)copylen + copylen_code_delta), |
| 134 | TO_BROTLI_BOOLcxx . . $ `c/include/brotli/types.h:55:9`!((self->dist_prefix_ & 0x3FF) == 0), &self->cmd_prefix_cxx . . $ Command#cmd_prefix_.); |
| 135 | } |
| 136 | |
| 137 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void InitInsertCommandcxx . . $ InitInsertCommand(93920b43ede9fcde).(Commandcxx . . $ Command#* self, size_t insertlen) { |
| 138 | self->insert_len_cxx . . $ Command#insert_len_. = (uint32_tcxx . . $ uint32_t#)insertlen; |
| 139 | self->copy_len_cxx . . $ Command#copy_len_. = 4 << 25; |
| 140 | self->dist_extra_cxx . . $ Command#dist_extra_. = 0; |
| 141 | self->dist_prefix_cxx . . $ Command#dist_prefix_. = BROTLI_NUM_DISTANCE_SHORT_CODEScxx . . $ `c/common/constants.h:58:9`!; |
| 142 | GetLengthCodecxx . . $ GetLengthCode(880176f624c20427).(insertlen, 4, BROTLI_FALSEcxx . . $ `c/include/brotli/types.h:53:9`!, &self->cmd_prefix_cxx . . $ Command#cmd_prefix_.); |
| 143 | } |
| 144 | |
| 145 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint32_tcxx . . $ uint32_t# CommandRestoreDistanceCodecxx . . $ CommandRestoreDistanceCode(cf5fcb3ffd6846bc).( |
| 146 | const Commandcxx . . $ Command#* self, const BrotliDistanceParamscxx . . $ BrotliDistanceParams#* dist) { |
| 147 | if ((self->dist_prefix_cxx . . $ Command#dist_prefix_. & 0x3FFu) < |
| 148 | BROTLI_NUM_DISTANCE_SHORT_CODEScxx . . $ `c/common/constants.h:58:9`! + dist->num_direct_distance_codescxx . . $ BrotliDistanceParams#num_direct_distance_codes.) { |
| 149 | return self->dist_prefix_cxx . . $ Command#dist_prefix_. & 0x3FFu; |
| 150 | } else { |
| 151 | uint32_tcxx . . $ uint32_t# dcode = self->dist_prefix_cxx . . $ Command#dist_prefix_. & 0x3FFu; |
| 152 | uint32_tcxx . . $ uint32_t# nbits = self->dist_prefix_cxx . . $ Command#dist_prefix_. >> 10; |
| 153 | uint32_tcxx . . $ uint32_t# extra = self->dist_extra_cxx . . $ Command#dist_extra_.; |
| 154 | uint32_tcxx . . $ uint32_t# postfix_mask = (1U << dist->distance_postfix_bitscxx . . $ BrotliDistanceParams#distance_postfix_bits.) - 1U; |
| 155 | uint32_tcxx . . $ uint32_t# hcode = (dcode - dist->num_direct_distance_codescxx . . $ BrotliDistanceParams#num_direct_distance_codes. - |
| 156 | BROTLI_NUM_DISTANCE_SHORT_CODEScxx . . $ `c/common/constants.h:58:9`!) >> |
| 157 | dist->distance_postfix_bitscxx . . $ BrotliDistanceParams#distance_postfix_bits.; |
| 158 | uint32_tcxx . . $ uint32_t# lcode = (dcode - dist->num_direct_distance_codescxx . . $ BrotliDistanceParams#num_direct_distance_codes. - |
| 159 | BROTLI_NUM_DISTANCE_SHORT_CODEScxx . . $ `c/common/constants.h:58:9`!) & postfix_mask; |
| 160 | uint32_tcxx . . $ uint32_t# offset = ((2U + (hcode & 1U)) << nbits) - 4U; |
| 161 | return ((offset + extra) << dist->distance_postfix_bitscxx . . $ BrotliDistanceParams#distance_postfix_bits.) + lcode + |
| 162 | dist->num_direct_distance_codescxx . . $ BrotliDistanceParams#num_direct_distance_codes. + BROTLI_NUM_DISTANCE_SHORT_CODEScxx . . $ `c/common/constants.h:58:9`!; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint32_tcxx . . $ uint32_t# CommandDistanceContextcxx . . $ CommandDistanceContext(4be527375b26559e).(const Commandcxx . . $ Command#* self) { |
| 167 | uint32_tcxx . . $ uint32_t# r = self->cmd_prefix_cxx . . $ Command#cmd_prefix_. >> 6; |
| 168 | uint32_tcxx . . $ uint32_t# c = self->cmd_prefix_cxx . . $ Command#cmd_prefix_. & 7; |
| 169 | if ((r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2)) { |
| 170 | return c; |
| 171 | } |
| 172 | return 3; |
| 173 | } |
| 174 | |
| 175 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint32_tcxx . . $ uint32_t# CommandCopyLencxx . . $ CommandCopyLen(4be527375b26559e).(const Commandcxx . . $ Command#* self) { |
| 176 | return self->copy_len_cxx . . $ Command#copy_len_. & 0x1FFFFFF; |
| 177 | } |
| 178 | |
| 179 | static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! uint32_tcxx . . $ uint32_t# CommandCopyLenCodecxx . . $ CommandCopyLenCode(4be527375b26559e).(const Commandcxx . . $ Command#* self) { |
| 180 | uint32_tcxx . . $ uint32_t# modifier = self->copy_len_cxx . . $ Command#copy_len_. >> 25; |
| 181 | int32_tcxx . . $ int32_t# delta = (int8_tcxx . . $ int8_t#)((uint8_tcxx . . $ uint8_t#)(modifier | ((modifier & 0x40) << 1))); |
| 182 | return (uint32_tcxx . . $ uint32_t#)((int32_tcxx . . $ int32_t#)(self->copy_len_cxx . . $ Command#copy_len_. & 0x1FFFFFF) + delta); |
| 183 | } |
| 184 | |
| 185 | #if defined(__cplusplus) || defined(c_plusplus) |
| 186 | } /* extern "C" */ |
| 187 | #endif |
| 188 | |
| 189 | #endif /* BROTLI_ENC_COMMAND_H_ */ |