brotli 0.6.0 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +163 -2
  3. data/ext/brotli/brotli.c +971 -218
  4. data/ext/brotli/brotli.h +1 -3
  5. data/ext/brotli/buffer.c +63 -60
  6. data/ext/brotli/buffer.h +8 -9
  7. data/ext/brotli/extconf.rb +130 -40
  8. data/lib/brotli/reader.rb +167 -0
  9. data/lib/brotli/version.rb +1 -1
  10. data/lib/brotli/writer.rb +81 -0
  11. data/lib/brotli.rb +2 -0
  12. data/vendor/brotli/c/common/constants.c +7 -7
  13. data/vendor/brotli/c/common/constants.h +2 -5
  14. data/vendor/brotli/c/common/context.c +2 -2
  15. data/vendor/brotli/c/common/context.h +1 -2
  16. data/vendor/brotli/c/common/dictionary.c +4 -5856
  17. data/vendor/brotli/c/common/dictionary.h +1 -2
  18. data/vendor/brotli/c/common/dictionary_inc.h +5847 -0
  19. data/vendor/brotli/c/common/platform.c +0 -4
  20. data/vendor/brotli/c/common/platform.h +182 -43
  21. data/vendor/brotli/c/common/shared_dictionary.c +3 -7
  22. data/vendor/brotli/c/common/shared_dictionary_internal.h +1 -1
  23. data/vendor/brotli/c/common/static_init.h +56 -0
  24. data/vendor/brotli/c/common/transform.c +6 -4
  25. data/vendor/brotli/c/common/transform.h +1 -2
  26. data/vendor/brotli/c/common/version.h +5 -5
  27. data/vendor/brotli/c/dec/bit_reader.c +2 -3
  28. data/vendor/brotli/c/dec/bit_reader.h +0 -4
  29. data/vendor/brotli/c/dec/decode.c +187 -69
  30. data/vendor/brotli/c/dec/huffman.c +2 -5
  31. data/vendor/brotli/c/dec/huffman.h +0 -2
  32. data/vendor/brotli/c/dec/prefix.c +67 -0
  33. data/vendor/brotli/c/dec/prefix.h +18 -708
  34. data/vendor/brotli/c/dec/prefix_inc.h +707 -0
  35. data/vendor/brotli/c/dec/state.c +18 -15
  36. data/vendor/brotli/c/dec/state.h +2 -6
  37. data/vendor/brotli/c/dec/static_init.c +53 -0
  38. data/vendor/brotli/c/dec/static_init.h +30 -0
  39. data/vendor/brotli/c/enc/backward_references.c +32 -8
  40. data/vendor/brotli/c/enc/backward_references.h +1 -5
  41. data/vendor/brotli/c/enc/backward_references_hq.c +15 -15
  42. data/vendor/brotli/c/enc/backward_references_hq.h +1 -5
  43. data/vendor/brotli/c/enc/bit_cost.c +28 -4
  44. data/vendor/brotli/c/enc/bit_cost.h +8 -40
  45. data/vendor/brotli/c/enc/bit_cost_inc.h +1 -1
  46. data/vendor/brotli/c/enc/block_splitter.c +9 -12
  47. data/vendor/brotli/c/enc/block_splitter.h +0 -3
  48. data/vendor/brotli/c/enc/block_splitter_inc.h +14 -8
  49. data/vendor/brotli/c/enc/brotli_bit_stream.c +10 -9
  50. data/vendor/brotli/c/enc/brotli_bit_stream.h +0 -6
  51. data/vendor/brotli/c/enc/cluster.c +0 -2
  52. data/vendor/brotli/c/enc/cluster.h +0 -2
  53. data/vendor/brotli/c/enc/command.c +1 -1
  54. data/vendor/brotli/c/enc/command.h +8 -10
  55. data/vendor/brotli/c/enc/compound_dictionary.c +3 -5
  56. data/vendor/brotli/c/enc/compound_dictionary.h +1 -4
  57. data/vendor/brotli/c/enc/compress_fragment.c +3 -13
  58. data/vendor/brotli/c/enc/compress_fragment.h +0 -2
  59. data/vendor/brotli/c/enc/compress_fragment_two_pass.c +5 -15
  60. data/vendor/brotli/c/enc/compress_fragment_two_pass.h +0 -2
  61. data/vendor/brotli/c/enc/dictionary_hash.c +127 -1830
  62. data/vendor/brotli/c/enc/dictionary_hash.h +23 -3
  63. data/vendor/brotli/c/enc/dictionary_hash_inc.h +1829 -0
  64. data/vendor/brotli/c/enc/encode.c +77 -52
  65. data/vendor/brotli/c/enc/encoder_dict.c +9 -7
  66. data/vendor/brotli/c/enc/encoder_dict.h +2 -4
  67. data/vendor/brotli/c/enc/entropy_encode.c +3 -6
  68. data/vendor/brotli/c/enc/entropy_encode.h +2 -4
  69. data/vendor/brotli/c/enc/entropy_encode_static.h +18 -12
  70. data/vendor/brotli/c/enc/fast_log.c +1 -1
  71. data/vendor/brotli/c/enc/fast_log.h +2 -3
  72. data/vendor/brotli/c/enc/find_match_length.h +0 -2
  73. data/vendor/brotli/c/enc/hash.h +38 -31
  74. data/vendor/brotli/c/enc/hash_base.h +38 -0
  75. data/vendor/brotli/c/enc/hash_forgetful_chain_inc.h +11 -1
  76. data/vendor/brotli/c/enc/hash_longest_match64_inc.h +24 -7
  77. data/vendor/brotli/c/enc/hash_longest_match64_simd_inc.h +304 -0
  78. data/vendor/brotli/c/enc/hash_longest_match_inc.h +30 -11
  79. data/vendor/brotli/c/enc/hash_longest_match_quickly_inc.h +4 -0
  80. data/vendor/brotli/c/enc/hash_longest_match_simd_inc.h +278 -0
  81. data/vendor/brotli/c/enc/histogram.c +1 -0
  82. data/vendor/brotli/c/enc/histogram.h +0 -4
  83. data/vendor/brotli/c/enc/literal_cost.c +4 -6
  84. data/vendor/brotli/c/enc/literal_cost.h +0 -2
  85. data/vendor/brotli/c/enc/matching_tag_mask.h +69 -0
  86. data/vendor/brotli/c/enc/memory.c +0 -5
  87. data/vendor/brotli/c/enc/memory.h +0 -4
  88. data/vendor/brotli/c/enc/metablock.c +7 -9
  89. data/vendor/brotli/c/enc/metablock.h +3 -3
  90. data/vendor/brotli/c/enc/metablock_inc.h +4 -4
  91. data/vendor/brotli/c/enc/params.h +0 -1
  92. data/vendor/brotli/c/enc/prefix.h +0 -2
  93. data/vendor/brotli/c/enc/quality.h +17 -10
  94. data/vendor/brotli/c/enc/ringbuffer.h +1 -4
  95. data/vendor/brotli/c/enc/state.h +2 -2
  96. data/vendor/brotli/c/enc/static_dict.c +5 -11
  97. data/vendor/brotli/c/enc/static_dict.h +1 -3
  98. data/vendor/brotli/c/enc/static_dict_lut.c +224 -0
  99. data/vendor/brotli/c/enc/static_dict_lut.h +20 -5837
  100. data/vendor/brotli/c/enc/static_dict_lut_inc.h +5830 -0
  101. data/vendor/brotli/c/enc/static_init.c +59 -0
  102. data/vendor/brotli/c/enc/static_init.h +30 -0
  103. data/vendor/brotli/c/enc/utf8_util.c +1 -1
  104. data/vendor/brotli/c/enc/utf8_util.h +0 -2
  105. data/vendor/brotli/c/enc/write_bits.h +0 -2
  106. data/vendor/brotli/c/include/brotli/decode.h +2 -4
  107. data/vendor/brotli/c/include/brotli/encode.h +5 -1
  108. data/vendor/brotli/c/include/brotli/port.h +4 -7
  109. data/vendor/brotli/c/include/brotli/types.h +2 -2
  110. metadata +25 -31
  111. data/.github/workflows/main.yml +0 -37
  112. data/.github/workflows/publish.yml +0 -24
  113. data/.gitignore +0 -18
  114. data/.gitmodules +0 -3
  115. data/.rspec +0 -2
  116. data/Gemfile +0 -10
  117. data/Rakefile +0 -25
  118. data/bin/before_install.sh +0 -9
  119. data/bin/console +0 -14
  120. data/bin/setup +0 -8
  121. data/brotli.gemspec +0 -27
  122. data/smoke.sh +0 -8
  123. data/test/brotli_test.rb +0 -117
  124. data/test/brotli_writer_test.rb +0 -36
  125. data/test/test_helper.rb +0 -9
  126. data/vendor/brotli/c/common/dictionary.bin +0 -432
  127. data/vendor/brotli/c/common/dictionary.bin.br +0 -0
@@ -6,9 +6,6 @@
6
6
 
7
7
  #include <brotli/decode.h>
8
8
 
9
- #include <stdlib.h> /* free, malloc */
10
- #include <string.h> /* memcpy, memset */
11
-
12
9
  #include "../common/constants.h"
13
10
  #include "../common/context.h"
14
11
  #include "../common/dictionary.h"
@@ -20,6 +17,7 @@
20
17
  #include "huffman.h"
21
18
  #include "prefix.h"
22
19
  #include "state.h"
20
+ #include "static_init.h"
23
21
 
24
22
  #if defined(BROTLI_TARGET_NEON)
25
23
  #include <arm_neon.h>
@@ -46,16 +44,19 @@ extern "C" {
46
44
  255 prefix + 32 base + 255 suffix */
47
45
  static const brotli_reg_t kRingBufferWriteAheadSlack = 542;
48
46
 
49
- static const uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = {
47
+ static const BROTLI_MODEL("small")
48
+ uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = {
50
49
  1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
51
50
  };
52
51
 
53
52
  /* Static prefix code for the complex code length code lengths. */
54
- static const uint8_t kCodeLengthPrefixLength[16] = {
53
+ static const BROTLI_MODEL("small")
54
+ uint8_t kCodeLengthPrefixLength[16] = {
55
55
  2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4,
56
56
  };
57
57
 
58
- static const uint8_t kCodeLengthPrefixValue[16] = {
58
+ static const BROTLI_MODEL("small")
59
+ uint8_t kCodeLengthPrefixValue[16] = {
59
60
  0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5,
60
61
  };
61
62
 
@@ -78,6 +79,10 @@ BROTLI_BOOL BrotliDecoderSetParameter(
78
79
  BrotliDecoderState* BrotliDecoderCreateInstance(
79
80
  brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
80
81
  BrotliDecoderState* state = 0;
82
+ if (!BrotliDecoderEnsureStaticInit()) {
83
+ BROTLI_DUMP();
84
+ return 0;
85
+ }
81
86
  if (!alloc_func && !free_func) {
82
87
  state = (BrotliDecoderState*)malloc(sizeof(BrotliDecoderState));
83
88
  } else if (alloc_func && free_func) {
@@ -217,7 +222,7 @@ static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8(
217
222
  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG;
218
223
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
219
224
  }
220
- *value = (1U << *value) + bits;
225
+ *value = ((brotli_reg_t)1U << *value) + bits;
221
226
  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
222
227
  return BROTLI_DECODER_SUCCESS;
223
228
 
@@ -466,6 +471,53 @@ static BROTLI_INLINE brotli_reg_t ReadPreloadedSymbol(const HuffmanCode* table,
466
471
  return result;
467
472
  }
468
473
 
474
+ /* Reads up to limit symbols from br and copies them into ringbuffer,
475
+ starting from pos. Caller must ensure that there is enough space
476
+ for the write. Returns the amount of symbols actually copied. */
477
+ static BROTLI_INLINE int BrotliCopyPreloadedSymbolsToU8(const HuffmanCode* table,
478
+ BrotliBitReader* br,
479
+ brotli_reg_t* bits,
480
+ brotli_reg_t* value,
481
+ uint8_t* ringbuffer,
482
+ int pos,
483
+ const int limit) {
484
+ /* Calculate range where CheckInputAmount is always true.
485
+ Start with the number of bytes we can read. */
486
+ int64_t new_lim = br->guard_in - br->next_in;
487
+ /* Convert to bits, since symbols use variable number of bits. */
488
+ new_lim *= 8;
489
+ /* At most 15 bits per symbol, so this is safe. */
490
+ new_lim /= 15;
491
+ const int kMaximalOverread = 4;
492
+ int pos_limit = limit;
493
+ int copies = 0;
494
+ if ((new_lim - kMaximalOverread) <= limit) {
495
+ // Safe cast, since new_lim is already < num_steps
496
+ pos_limit = (int)(new_lim - kMaximalOverread);
497
+ }
498
+ if (pos_limit < 0) {
499
+ pos_limit = 0;
500
+ }
501
+ copies = pos_limit;
502
+ pos_limit += pos;
503
+ /* Fast path, caller made sure it is safe to write,
504
+ we verified that is is safe to read. */
505
+ for (; pos < pos_limit; pos++) {
506
+ BROTLI_DCHECK(BrotliCheckInputAmount(br));
507
+ ringbuffer[pos] = (uint8_t)ReadPreloadedSymbol(table, br, bits, value);
508
+ BROTLI_LOG_ARRAY_INDEX(ringbuffer, pos);
509
+ }
510
+ /* Do the remainder, caller made sure it is safe to write,
511
+ we need to bverify that it is safe to read. */
512
+ while (BrotliCheckInputAmount(br) && copies < limit) {
513
+ ringbuffer[pos] = (uint8_t)ReadPreloadedSymbol(table, br, bits, value);
514
+ BROTLI_LOG_ARRAY_INDEX(ringbuffer, pos);
515
+ pos++;
516
+ copies++;
517
+ }
518
+ return copies;
519
+ }
520
+
469
521
  static BROTLI_INLINE brotli_reg_t Log2Floor(brotli_reg_t x) {
470
522
  brotli_reg_t result = 0;
471
523
  while (x) {
@@ -1089,7 +1141,7 @@ static BrotliDecoderErrorCode DecodeContextMap(brotli_reg_t context_map_size,
1089
1141
  h->context_index = context_index;
1090
1142
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
1091
1143
  }
1092
- reps += 1U << code;
1144
+ reps += (brotli_reg_t)1U << code;
1093
1145
  BROTLI_LOG_UINT(reps);
1094
1146
  if (context_index + reps > context_map_size) {
1095
1147
  return
@@ -1124,7 +1176,7 @@ static BrotliDecoderErrorCode DecodeContextMap(brotli_reg_t context_map_size,
1124
1176
 
1125
1177
  /* Decodes a command or literal and updates block type ring-buffer.
1126
1178
  Reads 3..54 bits. */
1127
- static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
1179
+ static BROTLI_INLINE BrotliDecoderErrorCode DecodeBlockTypeAndLength(
1128
1180
  int safe, BrotliDecoderState* s, int tree_type) {
1129
1181
  brotli_reg_t max_block_type = s->num_block_types[tree_type];
1130
1182
  const HuffmanCode* type_tree = &s->block_type_trees[
@@ -1135,7 +1187,7 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
1135
1187
  brotli_reg_t* ringbuffer = &s->block_type_rb[tree_type * 2];
1136
1188
  brotli_reg_t block_type;
1137
1189
  if (max_block_type <= 1) {
1138
- return BROTLI_FALSE;
1190
+ return BROTLI_DECODER_ERROR_FORMAT_BLOCK_SWITCH;
1139
1191
  }
1140
1192
 
1141
1193
  /* Read 0..15 + 3..39 bits. */
@@ -1145,11 +1197,13 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
1145
1197
  } else {
1146
1198
  BrotliBitReaderState memento;
1147
1199
  BrotliBitReaderSaveState(br, &memento);
1148
- if (!SafeReadSymbol(type_tree, br, &block_type)) return BROTLI_FALSE;
1200
+ if (!SafeReadSymbol(type_tree, br, &block_type)) {
1201
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
1202
+ }
1149
1203
  if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) {
1150
1204
  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
1151
1205
  BrotliBitReaderRestoreState(br, &memento);
1152
- return BROTLI_FALSE;
1206
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
1153
1207
  }
1154
1208
  }
1155
1209
 
@@ -1165,7 +1219,7 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
1165
1219
  }
1166
1220
  ringbuffer[0] = ringbuffer[1];
1167
1221
  ringbuffer[1] = block_type;
1168
- return BROTLI_TRUE;
1222
+ return BROTLI_DECODER_SUCCESS;
1169
1223
  }
1170
1224
 
1171
1225
  static BROTLI_INLINE void DetectTrivialLiteralBlockTypes(
@@ -1202,62 +1256,68 @@ static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) {
1202
1256
 
1203
1257
  /* Decodes the block type and updates the state for literal context.
1204
1258
  Reads 3..54 bits. */
1205
- static BROTLI_INLINE BROTLI_BOOL DecodeLiteralBlockSwitchInternal(
1259
+ static BROTLI_INLINE BrotliDecoderErrorCode DecodeLiteralBlockSwitchInternal(
1206
1260
  int safe, BrotliDecoderState* s) {
1207
- if (!DecodeBlockTypeAndLength(safe, s, 0)) {
1208
- return BROTLI_FALSE;
1261
+ BrotliDecoderErrorCode result = DecodeBlockTypeAndLength(safe, s, 0);
1262
+ if (result != BROTLI_DECODER_SUCCESS) {
1263
+ return result;
1209
1264
  }
1210
1265
  PrepareLiteralDecoding(s);
1211
- return BROTLI_TRUE;
1266
+ return BROTLI_DECODER_SUCCESS;
1212
1267
  }
1213
1268
 
1214
- static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliDecoderState* s) {
1215
- DecodeLiteralBlockSwitchInternal(0, s);
1269
+ static BROTLI_NOINLINE BrotliDecoderErrorCode
1270
+ DecodeLiteralBlockSwitch(BrotliDecoderState* s) {
1271
+ return DecodeLiteralBlockSwitchInternal(0, s);
1216
1272
  }
1217
1273
 
1218
- static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch(
1274
+ static BROTLI_NOINLINE BrotliDecoderErrorCode SafeDecodeLiteralBlockSwitch(
1219
1275
  BrotliDecoderState* s) {
1220
1276
  return DecodeLiteralBlockSwitchInternal(1, s);
1221
1277
  }
1222
1278
 
1223
1279
  /* Block switch for insert/copy length.
1224
1280
  Reads 3..54 bits. */
1225
- static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal(
1281
+ static BROTLI_INLINE BrotliDecoderErrorCode DecodeCommandBlockSwitchInternal(
1226
1282
  int safe, BrotliDecoderState* s) {
1227
- if (!DecodeBlockTypeAndLength(safe, s, 1)) {
1228
- return BROTLI_FALSE;
1283
+ BrotliDecoderErrorCode result = DecodeBlockTypeAndLength(safe, s, 1);
1284
+ if (result != BROTLI_DECODER_SUCCESS) {
1285
+ return result;
1229
1286
  }
1230
1287
  s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]];
1231
- return BROTLI_TRUE;
1288
+ return BROTLI_DECODER_SUCCESS;
1232
1289
  }
1233
1290
 
1234
- static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) {
1235
- DecodeCommandBlockSwitchInternal(0, s);
1291
+ static BROTLI_NOINLINE BrotliDecoderErrorCode
1292
+ DecodeCommandBlockSwitch(BrotliDecoderState* s) {
1293
+ return DecodeCommandBlockSwitchInternal(0, s);
1236
1294
  }
1237
1295
 
1238
- static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch(
1239
- BrotliDecoderState* s) {
1296
+ static BROTLI_NOINLINE BrotliDecoderErrorCode
1297
+ SafeDecodeCommandBlockSwitch(BrotliDecoderState* s) {
1240
1298
  return DecodeCommandBlockSwitchInternal(1, s);
1241
1299
  }
1242
1300
 
1243
1301
  /* Block switch for distance codes.
1244
1302
  Reads 3..54 bits. */
1245
- static BROTLI_INLINE BROTLI_BOOL DecodeDistanceBlockSwitchInternal(
1303
+ static BROTLI_INLINE BrotliDecoderErrorCode DecodeDistanceBlockSwitchInternal(
1246
1304
  int safe, BrotliDecoderState* s) {
1247
- if (!DecodeBlockTypeAndLength(safe, s, 2)) {
1248
- return BROTLI_FALSE;
1305
+ BrotliDecoderErrorCode result = DecodeBlockTypeAndLength(safe, s, 2);
1306
+ if (result != BROTLI_DECODER_SUCCESS) {
1307
+ return result;
1249
1308
  }
1250
1309
  s->dist_context_map_slice = s->dist_context_map +
1251
1310
  (s->block_type_rb[5] << BROTLI_DISTANCE_CONTEXT_BITS);
1252
1311
  s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
1253
- return BROTLI_TRUE;
1312
+ return BROTLI_DECODER_SUCCESS;
1254
1313
  }
1255
1314
 
1256
- static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliDecoderState* s) {
1257
- DecodeDistanceBlockSwitchInternal(0, s);
1315
+ static BROTLI_NOINLINE BrotliDecoderErrorCode
1316
+ DecodeDistanceBlockSwitch(BrotliDecoderState* s) {
1317
+ return DecodeDistanceBlockSwitchInternal(0, s);
1258
1318
  }
1259
1319
 
1260
- static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch(
1320
+ static BROTLI_NOINLINE BrotliDecoderErrorCode SafeDecodeDistanceBlockSwitch(
1261
1321
  BrotliDecoderState* s) {
1262
1322
  return DecodeDistanceBlockSwitchInternal(1, s);
1263
1323
  }
@@ -1363,6 +1423,7 @@ static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer(
1363
1423
  static BrotliDecoderErrorCode BROTLI_NOINLINE
1364
1424
  SkipMetadataBlock(BrotliDecoderState* s) {
1365
1425
  BrotliBitReader* br = &s->br;
1426
+ int nbytes;
1366
1427
 
1367
1428
  if (s->meta_block_remaining_len == 0) {
1368
1429
  return BROTLI_DECODER_SUCCESS;
@@ -1373,7 +1434,7 @@ SkipMetadataBlock(BrotliDecoderState* s) {
1373
1434
  /* Drain accumulator. */
1374
1435
  if (BrotliGetAvailableBits(br) >= 8) {
1375
1436
  uint8_t buffer[8];
1376
- int nbytes = (int)(BrotliGetAvailableBits(br)) >> 3;
1437
+ nbytes = (int)(BrotliGetAvailableBits(br)) >> 3;
1377
1438
  BROTLI_DCHECK(nbytes <= 8);
1378
1439
  if (nbytes > s->meta_block_remaining_len) {
1379
1440
  nbytes = s->meta_block_remaining_len;
@@ -1390,7 +1451,7 @@ SkipMetadataBlock(BrotliDecoderState* s) {
1390
1451
  }
1391
1452
 
1392
1453
  /* Direct access to metadata is possible. */
1393
- int nbytes = (int)BrotliGetRemainingBytes(br);
1454
+ nbytes = (int)BrotliGetRemainingBytes(br);
1394
1455
  if (nbytes > s->meta_block_remaining_len) {
1395
1456
  nbytes = s->meta_block_remaining_len;
1396
1457
  }
@@ -1486,7 +1547,7 @@ static BROTLI_BOOL AttachCompoundDictionary(
1486
1547
  return BROTLI_TRUE;
1487
1548
  }
1488
1549
 
1489
- static void EnsureCoumpoundDictionaryInitialized(BrotliDecoderState* state) {
1550
+ static void EnsureCompoundDictionaryInitialized(BrotliDecoderState* state) {
1490
1551
  BrotliDecoderCompoundDictionary* addon = state->compound_dictionary;
1491
1552
  /* 256 = (1 << 8) slots in block map. */
1492
1553
  int block_bits = 8;
@@ -1507,7 +1568,7 @@ static BROTLI_BOOL InitializeCompoundDictionaryCopy(BrotliDecoderState* s,
1507
1568
  int address, int length) {
1508
1569
  BrotliDecoderCompoundDictionary* addon = s->compound_dictionary;
1509
1570
  int index;
1510
- EnsureCoumpoundDictionaryInitialized(s);
1571
+ EnsureCompoundDictionaryInitialized(s);
1511
1572
  index = addon->block_map[address >> addon->block_bits];
1512
1573
  while (address >= addon->chunk_offsets[index + 1]) index++;
1513
1574
  if (addon->total_size < address + length) return BROTLI_FALSE;
@@ -1753,7 +1814,7 @@ static void CalculateDistanceLut(BrotliDecoderState* s) {
1753
1814
  brotli_reg_t npostfix = s->distance_postfix_bits;
1754
1815
  brotli_reg_t ndirect = s->num_direct_distance_codes;
1755
1816
  brotli_reg_t alphabet_size_limit = s->distance_hgroup.alphabet_size_limit;
1756
- brotli_reg_t postfix = 1u << npostfix;
1817
+ brotli_reg_t postfix = (brotli_reg_t)1u << npostfix;
1757
1818
  brotli_reg_t j;
1758
1819
  brotli_reg_t bits = 1;
1759
1820
  brotli_reg_t half = 0;
@@ -1887,6 +1948,9 @@ static BROTLI_INLINE BROTLI_BOOL CheckInputAmount(
1887
1948
  return BrotliCheckInputAmount(br);
1888
1949
  }
1889
1950
 
1951
+ /* NB: METHOD should return BROTLI_FALSE only in case there is not enough input;
1952
+ in case of "unsafe" execution, when input is guaranteed to be sufficient,
1953
+ result is ignored. */
1890
1954
  #define BROTLI_SAFE(METHOD) \
1891
1955
  { \
1892
1956
  if (safe) { \
@@ -1899,6 +1963,22 @@ static BROTLI_INLINE BROTLI_BOOL CheckInputAmount(
1899
1963
  } \
1900
1964
  }
1901
1965
 
1966
+ /* NB: METHOD should return BROTLI_DECODER_SUCCESS, BROTLI_DECODER_ERROR_*, or
1967
+ BROTLI_DECODER_NEEDS_MORE_INPUT; the later two break the processing. */
1968
+ #define BROTLI_SAFE_WITH_STATUS(METHOD) \
1969
+ { \
1970
+ BrotliDecoderErrorCode status; \
1971
+ if (safe) { \
1972
+ status = Safe##METHOD; \
1973
+ } else { \
1974
+ status = METHOD; \
1975
+ } \
1976
+ if (status != BROTLI_DECODER_SUCCESS) { \
1977
+ result = status; \
1978
+ goto saveStateAndReturn; \
1979
+ } \
1980
+ }
1981
+
1902
1982
  static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal(
1903
1983
  int safe, BrotliDecoderState* s) {
1904
1984
  int pos = s->pos;
@@ -1938,7 +2018,7 @@ CommandBegin:
1938
2018
  goto saveStateAndReturn;
1939
2019
  }
1940
2020
  if (BROTLI_PREDICT_FALSE(s->block_length[1] == 0)) {
1941
- BROTLI_SAFE(DecodeCommandBlockSwitch(s));
2021
+ BROTLI_SAFE_WITH_STATUS(DecodeCommandBlockSwitch(s));
1942
2022
  goto CommandBegin;
1943
2023
  }
1944
2024
  /* Read the insert/copy length in the command. */
@@ -1959,35 +2039,72 @@ CommandInner:
1959
2039
  brotli_reg_t bits;
1960
2040
  brotli_reg_t value;
1961
2041
  PreloadSymbol(safe, s->literal_htree, br, &bits, &value);
1962
- do {
1963
- if (!CheckInputAmount(safe, br)) {
1964
- s->state = BROTLI_STATE_COMMAND_INNER;
1965
- result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1966
- goto saveStateAndReturn;
2042
+ if (!safe) {
2043
+ // This is a hottest part of the decode, so we copy the loop below
2044
+ // and optimize it by calculating the number of steps where all checks
2045
+ // evaluate to false (ringbuffer size/block size/input size).
2046
+ // Since all checks are loop invariant, we just need to find
2047
+ // minimal number of iterations for a simple loop, and run
2048
+ // the full version for the remainder.
2049
+ int num_steps = i - 1;
2050
+ if (num_steps > 0 && ((brotli_reg_t)(num_steps) > s->block_length[0])) {
2051
+ // Safe cast, since block_length < steps
2052
+ num_steps = (int)s->block_length[0];
1967
2053
  }
1968
- if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) {
1969
- goto NextLiteralBlock;
2054
+ if (s->ringbuffer_size >= pos &&
2055
+ (s->ringbuffer_size - pos) <= num_steps) {
2056
+ num_steps = s->ringbuffer_size - pos - 1;
1970
2057
  }
1971
- if (!safe) {
1972
- s->ringbuffer[pos] =
1973
- (uint8_t)ReadPreloadedSymbol(s->literal_htree, br, &bits, &value);
1974
- } else {
2058
+ if (num_steps < 0) {
2059
+ num_steps = 0;
2060
+ }
2061
+ num_steps = BrotliCopyPreloadedSymbolsToU8(s->literal_htree, br, &bits,
2062
+ &value, s->ringbuffer, pos,
2063
+ num_steps);
2064
+ pos += num_steps;
2065
+ s->block_length[0] -= (brotli_reg_t)num_steps;
2066
+ i -= num_steps;
2067
+ do {
2068
+ if (!CheckInputAmount(safe, br)) {
2069
+ s->state = BROTLI_STATE_COMMAND_INNER;
2070
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2071
+ goto saveStateAndReturn;
2072
+ }
2073
+ if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) {
2074
+ goto NextLiteralBlock;
2075
+ }
2076
+ BrotliCopyPreloadedSymbolsToU8(s->literal_htree, br, &bits, &value,
2077
+ s->ringbuffer, pos, 1);
2078
+ --s->block_length[0];
2079
+ BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos);
2080
+ ++pos;
2081
+ if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) {
2082
+ s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
2083
+ --i;
2084
+ goto saveStateAndReturn;
2085
+ }
2086
+ } while (--i != 0);
2087
+ } else { /* safe */
2088
+ do {
2089
+ if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) {
2090
+ goto NextLiteralBlock;
2091
+ }
1975
2092
  brotli_reg_t literal;
1976
2093
  if (!SafeReadSymbol(s->literal_htree, br, &literal)) {
1977
2094
  result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1978
2095
  goto saveStateAndReturn;
1979
2096
  }
1980
2097
  s->ringbuffer[pos] = (uint8_t)literal;
1981
- }
1982
- --s->block_length[0];
1983
- BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos);
1984
- ++pos;
1985
- if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) {
1986
- s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
1987
- --i;
1988
- goto saveStateAndReturn;
1989
- }
1990
- } while (--i != 0);
2098
+ --s->block_length[0];
2099
+ BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos);
2100
+ ++pos;
2101
+ if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) {
2102
+ s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
2103
+ --i;
2104
+ goto saveStateAndReturn;
2105
+ }
2106
+ } while (--i != 0);
2107
+ }
1991
2108
  } else {
1992
2109
  uint8_t p1 = s->ringbuffer[(pos - 1) & s->ringbuffer_mask];
1993
2110
  uint8_t p2 = s->ringbuffer[(pos - 2) & s->ringbuffer_mask];
@@ -2046,7 +2163,7 @@ CommandPostDecodeLiterals:
2046
2163
  } else {
2047
2164
  /* Read distance code in the command, unless it was implicitly zero. */
2048
2165
  if (BROTLI_PREDICT_FALSE(s->block_length[2] == 0)) {
2049
- BROTLI_SAFE(DecodeDistanceBlockSwitch(s));
2166
+ BROTLI_SAFE_WITH_STATUS(DecodeDistanceBlockSwitch(s));
2050
2167
  }
2051
2168
  BROTLI_SAFE(ReadDistance(s, br));
2052
2169
  }
@@ -2240,7 +2357,7 @@ CommandPostWrapCopy:
2240
2357
  }
2241
2358
 
2242
2359
  NextLiteralBlock:
2243
- BROTLI_SAFE(DecodeLiteralBlockSwitch(s));
2360
+ BROTLI_SAFE_WITH_STATUS(DecodeLiteralBlockSwitch(s));
2244
2361
  goto CommandInner;
2245
2362
 
2246
2363
  saveStateAndReturn:
@@ -2462,6 +2579,8 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2462
2579
  if (result != BROTLI_DECODER_SUCCESS) {
2463
2580
  break;
2464
2581
  }
2582
+ BROTLI_DCHECK(s->meta_block_remaining_len <=
2583
+ (int)BROTLI_BLOCK_SIZE_CAP);
2465
2584
  BROTLI_LOG_UINT(s->is_last_metablock);
2466
2585
  BROTLI_LOG_UINT(s->meta_block_remaining_len);
2467
2586
  BROTLI_LOG_UINT(s->is_metadata);
@@ -2855,16 +2974,15 @@ void BrotliDecoderSetMetadataCallbacks(
2855
2974
 
2856
2975
  /* Escalate internal functions visibility; for testing purposes only. */
2857
2976
  #if defined(BROTLI_TEST)
2858
- BROTLI_BOOL SafeReadSymbolForTest(
2977
+ BROTLI_BOOL BrotliSafeReadSymbolForTest(
2859
2978
  const HuffmanCode*, BrotliBitReader*, brotli_reg_t*);
2860
- BROTLI_BOOL SafeReadSymbolForTest(
2979
+ BROTLI_BOOL BrotliSafeReadSymbolForTest(
2861
2980
  const HuffmanCode* table, BrotliBitReader* br, brotli_reg_t* result) {
2862
2981
  return SafeReadSymbol(table, br, result);
2863
2982
  }
2864
-
2865
- void InverseMoveToFrontTransformForTest(
2983
+ void BrotliInverseMoveToFrontTransformForTest(
2866
2984
  uint8_t*, brotli_reg_t, BrotliDecoderState*);
2867
- void InverseMoveToFrontTransformForTest(
2985
+ void BrotliInverseMoveToFrontTransformForTest(
2868
2986
  uint8_t* v, brotli_reg_t l, BrotliDecoderState* s) {
2869
2987
  InverseMoveToFrontTransform(v, l, s);
2870
2988
  }
@@ -8,10 +8,6 @@
8
8
 
9
9
  #include "huffman.h"
10
10
 
11
- #include <string.h> /* memcpy, memset */
12
-
13
- #include <brotli/types.h>
14
-
15
11
  #include "../common/constants.h"
16
12
  #include "../common/platform.h"
17
13
 
@@ -26,7 +22,8 @@ extern "C" {
26
22
  ((sizeof(brotli_reg_t) << 3) - BROTLI_REVERSE_BITS_MAX)
27
23
  #else
28
24
  #define BROTLI_REVERSE_BITS_BASE 0
29
- static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
25
+ static BROTLI_MODEL("small")
26
+ uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
30
27
  0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
31
28
  0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
32
29
  0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
@@ -9,8 +9,6 @@
9
9
  #ifndef BROTLI_DEC_HUFFMAN_H_
10
10
  #define BROTLI_DEC_HUFFMAN_H_
11
11
 
12
- #include <brotli/types.h>
13
-
14
12
  #include "../common/platform.h"
15
13
 
16
14
  #if defined(__cplusplus) || defined(c_plusplus)
@@ -0,0 +1,67 @@
1
+ /* Copyright 2025 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
+ #include "prefix.h"
8
+
9
+ #include "../common/platform.h" /* IWYU pragma: keep */
10
+ #include "../common/static_init.h"
11
+
12
+ #if (BROTLI_STATIC_INIT != BROTLI_STATIC_INIT_NONE)
13
+ #include "../common/constants.h"
14
+ #endif
15
+
16
+ #if defined(__cplusplus) || defined(c_plusplus)
17
+ extern "C" {
18
+ #endif
19
+
20
+ #if (BROTLI_STATIC_INIT == BROTLI_STATIC_INIT_NONE)
21
+ /* Embed kCmdLut. */
22
+ #include "prefix_inc.h"
23
+ #else
24
+ BROTLI_COLD BROTLI_BOOL BrotliDecoderInitCmdLut(CmdLutElement* items) {
25
+ static const uint8_t kInsertLengthExtraBits[24] = {
26
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03,
27
+ 0x04, 0x04, 0x05, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0C, 0x0E, 0x18};
28
+ static const uint8_t kCopyLengthExtraBits[24] = {
29
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02,
30
+ 0x03, 0x03, 0x04, 0x04, 0x05, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x18};
31
+ static const uint8_t kCellPos[11] = {0, 1, 0, 1, 8, 9, 2, 16, 10, 17, 18};
32
+
33
+ uint16_t insert_length_offsets[24];
34
+ uint16_t copy_length_offsets[24];
35
+ insert_length_offsets[0] = 0;
36
+ copy_length_offsets[0] = 2;
37
+ for (size_t i = 0; i < 23; ++i) {
38
+ insert_length_offsets[i + 1] =
39
+ insert_length_offsets[i] + (uint16_t)(1u << kInsertLengthExtraBits[i]);
40
+ copy_length_offsets[i + 1] =
41
+ copy_length_offsets[i] + (uint16_t)(1u << kCopyLengthExtraBits[i]);
42
+ }
43
+
44
+ for (size_t symbol = 0; symbol < BROTLI_NUM_COMMAND_SYMBOLS; ++symbol) {
45
+ CmdLutElement* item = items + symbol;
46
+ const size_t cell_idx = symbol >> 6;
47
+ const size_t cell_pos = kCellPos[cell_idx];
48
+ const size_t copy_code = ((cell_pos << 3) & 0x18) + (symbol & 0x7);
49
+ const uint16_t copy_len_offset = copy_length_offsets[copy_code];
50
+ const size_t insert_code = (cell_pos & 0x18) + ((symbol >> 3) & 0x7);
51
+ item->copy_len_extra_bits = kCopyLengthExtraBits[copy_code];
52
+ item->context = (copy_len_offset > 4) ? 3 : ((uint8_t)copy_len_offset - 2);
53
+ item->copy_len_offset = copy_len_offset;
54
+ item->distance_code = (cell_idx >= 2) ? -1 : 0;
55
+ item->insert_len_extra_bits = kInsertLengthExtraBits[insert_code];
56
+ item->insert_len_offset = insert_length_offsets[insert_code];
57
+ }
58
+ return BROTLI_TRUE;
59
+ }
60
+
61
+ BROTLI_MODEL("small")
62
+ CmdLutElement kCmdLut[BROTLI_NUM_COMMAND_SYMBOLS];
63
+ #endif /* BROTLI_STATIC_INIT */
64
+
65
+ #if defined(__cplusplus) || defined(c_plusplus)
66
+ } /* extern "C" */
67
+ #endif