duckdb 1.0.1-dev21.0 → 1.0.1-dev27.0

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 (1390) hide show
  1. package/.github/workflows/HighPriorityIssues.yml +2 -2
  2. package/.github/workflows/NodeJS.yml +1 -1
  3. package/binding.gyp +41 -0
  4. package/package.json +1 -1
  5. package/src/duckdb/extension/icu/icu-dateadd.cpp +4 -2
  6. package/src/duckdb/extension/icu/icu-datefunc.cpp +6 -2
  7. package/src/duckdb/extension/icu/icu-datesub.cpp +13 -2
  8. package/src/duckdb/extension/icu/icu-strptime.cpp +6 -6
  9. package/src/duckdb/extension/icu/icu-table-range.cpp +92 -73
  10. package/src/duckdb/extension/icu/icu-timebucket.cpp +12 -2
  11. package/src/duckdb/extension/icu/icu-timezone.cpp +3 -3
  12. package/src/duckdb/extension/icu/icu_extension.cpp +61 -9
  13. package/src/duckdb/extension/json/include/json_executors.hpp +20 -23
  14. package/src/duckdb/extension/json/include/json_functions.hpp +4 -0
  15. package/src/duckdb/extension/json/include/json_scan.hpp +6 -2
  16. package/src/duckdb/extension/json/include/json_structure.hpp +12 -9
  17. package/src/duckdb/extension/json/json_common.cpp +66 -10
  18. package/src/duckdb/extension/json/json_extension.cpp +13 -5
  19. package/src/duckdb/extension/json/json_functions/json_array_length.cpp +1 -1
  20. package/src/duckdb/extension/json/json_functions/json_create.cpp +21 -4
  21. package/src/duckdb/extension/json/json_functions/json_exists.cpp +32 -0
  22. package/src/duckdb/extension/json/json_functions/json_extract.cpp +2 -2
  23. package/src/duckdb/extension/json/json_functions/json_keys.cpp +1 -1
  24. package/src/duckdb/extension/json/json_functions/json_pretty.cpp +32 -0
  25. package/src/duckdb/extension/json/json_functions/json_serialize_sql.cpp +5 -1
  26. package/src/duckdb/extension/json/json_functions/json_structure.cpp +305 -94
  27. package/src/duckdb/extension/json/json_functions/json_transform.cpp +1 -1
  28. package/src/duckdb/extension/json/json_functions/json_type.cpp +3 -3
  29. package/src/duckdb/extension/json/json_functions/json_value.cpp +42 -0
  30. package/src/duckdb/extension/json/json_functions/read_json.cpp +16 -2
  31. package/src/duckdb/extension/json/json_functions/read_json_objects.cpp +3 -2
  32. package/src/duckdb/extension/json/json_functions.cpp +5 -1
  33. package/src/duckdb/extension/json/json_scan.cpp +13 -12
  34. package/src/duckdb/extension/json/serialize_json.cpp +5 -3
  35. package/src/duckdb/extension/parquet/column_reader.cpp +206 -43
  36. package/src/duckdb/extension/parquet/column_writer.cpp +133 -62
  37. package/src/duckdb/extension/parquet/geo_parquet.cpp +391 -0
  38. package/src/duckdb/extension/parquet/include/boolean_column_reader.hpp +16 -5
  39. package/src/duckdb/extension/parquet/include/column_reader.hpp +37 -12
  40. package/src/duckdb/extension/parquet/include/column_writer.hpp +10 -11
  41. package/src/duckdb/extension/parquet/include/expression_column_reader.hpp +52 -0
  42. package/src/duckdb/extension/parquet/include/geo_parquet.hpp +139 -0
  43. package/src/duckdb/extension/parquet/include/parquet_crypto.hpp +13 -8
  44. package/src/duckdb/extension/parquet/include/parquet_decimal_utils.hpp +3 -0
  45. package/src/duckdb/extension/parquet/include/parquet_file_metadata_cache.hpp +7 -3
  46. package/src/duckdb/extension/parquet/include/parquet_reader.hpp +55 -8
  47. package/src/duckdb/extension/parquet/include/parquet_rle_bp_decoder.hpp +3 -3
  48. package/src/duckdb/extension/parquet/include/parquet_rle_bp_encoder.hpp +1 -1
  49. package/src/duckdb/extension/parquet/include/parquet_timestamp.hpp +8 -0
  50. package/src/duckdb/extension/parquet/include/parquet_writer.hpp +21 -7
  51. package/src/duckdb/extension/parquet/include/resizable_buffer.hpp +33 -11
  52. package/src/duckdb/extension/parquet/include/string_column_reader.hpp +5 -2
  53. package/src/duckdb/extension/parquet/include/templated_column_reader.hpp +48 -14
  54. package/src/duckdb/extension/parquet/parquet_crypto.cpp +109 -61
  55. package/src/duckdb/extension/parquet/parquet_extension.cpp +305 -72
  56. package/src/duckdb/extension/parquet/parquet_metadata.cpp +4 -4
  57. package/src/duckdb/extension/parquet/parquet_reader.cpp +151 -40
  58. package/src/duckdb/extension/parquet/parquet_statistics.cpp +50 -16
  59. package/src/duckdb/extension/parquet/parquet_timestamp.cpp +42 -1
  60. package/src/duckdb/extension/parquet/parquet_writer.cpp +67 -75
  61. package/src/duckdb/extension/parquet/serialize_parquet.cpp +3 -1
  62. package/src/duckdb/extension/parquet/zstd_file_system.cpp +5 -1
  63. package/src/duckdb/src/catalog/catalog.cpp +14 -16
  64. package/src/duckdb/src/catalog/catalog_entry/duck_index_entry.cpp +14 -11
  65. package/src/duckdb/src/catalog/catalog_entry/duck_schema_entry.cpp +39 -19
  66. package/src/duckdb/src/catalog/catalog_entry/duck_table_entry.cpp +92 -78
  67. package/src/duckdb/src/catalog/catalog_entry/index_catalog_entry.cpp +10 -2
  68. package/src/duckdb/src/catalog/catalog_entry/macro_catalog_entry.cpp +10 -3
  69. package/src/duckdb/src/catalog/catalog_entry/schema_catalog_entry.cpp +3 -3
  70. package/src/duckdb/src/catalog/catalog_entry/table_catalog_entry.cpp +7 -7
  71. package/src/duckdb/src/catalog/catalog_entry.cpp +6 -3
  72. package/src/duckdb/src/catalog/catalog_set.cpp +14 -19
  73. package/src/duckdb/src/catalog/default/default_functions.cpp +179 -166
  74. package/src/duckdb/src/catalog/default/default_generator.cpp +24 -0
  75. package/src/duckdb/src/catalog/default/default_schemas.cpp +4 -3
  76. package/src/duckdb/src/catalog/default/default_table_functions.cpp +148 -0
  77. package/src/duckdb/src/catalog/default/default_views.cpp +7 -3
  78. package/src/duckdb/src/catalog/duck_catalog.cpp +7 -1
  79. package/src/duckdb/src/common/adbc/adbc.cpp +120 -58
  80. package/src/duckdb/src/common/allocator.cpp +71 -6
  81. package/src/duckdb/src/common/arrow/appender/bool_data.cpp +8 -7
  82. package/src/duckdb/src/common/arrow/appender/fixed_size_list_data.cpp +1 -1
  83. package/src/duckdb/src/common/arrow/appender/union_data.cpp +4 -5
  84. package/src/duckdb/src/common/arrow/arrow_appender.cpp +55 -21
  85. package/src/duckdb/src/common/arrow/arrow_converter.cpp +85 -10
  86. package/src/duckdb/src/common/arrow/arrow_merge_event.cpp +142 -0
  87. package/src/duckdb/src/common/arrow/arrow_query_result.cpp +56 -0
  88. package/src/duckdb/src/common/arrow/physical_arrow_batch_collector.cpp +37 -0
  89. package/src/duckdb/src/common/arrow/physical_arrow_collector.cpp +128 -0
  90. package/src/duckdb/src/common/arrow/schema_metadata.cpp +101 -0
  91. package/src/duckdb/src/common/cgroups.cpp +189 -0
  92. package/src/duckdb/src/common/compressed_file_system.cpp +6 -3
  93. package/src/duckdb/src/common/encryption_state.cpp +38 -0
  94. package/src/duckdb/src/common/enum_util.cpp +682 -14
  95. package/src/duckdb/src/common/enums/file_compression_type.cpp +24 -0
  96. package/src/duckdb/src/common/enums/metric_type.cpp +208 -0
  97. package/src/duckdb/src/common/enums/optimizer_type.cpp +8 -2
  98. package/src/duckdb/src/common/enums/physical_operator_type.cpp +2 -0
  99. package/src/duckdb/src/common/enums/relation_type.cpp +4 -0
  100. package/src/duckdb/src/common/enums/statement_type.cpp +15 -0
  101. package/src/duckdb/src/common/error_data.cpp +22 -20
  102. package/src/duckdb/src/common/exception/binder_exception.cpp +5 -0
  103. package/src/duckdb/src/common/exception.cpp +11 -1
  104. package/src/duckdb/src/common/extra_type_info.cpp +3 -0
  105. package/src/duckdb/src/common/file_buffer.cpp +1 -1
  106. package/src/duckdb/src/common/file_system.cpp +25 -3
  107. package/src/duckdb/src/common/filename_pattern.cpp +1 -0
  108. package/src/duckdb/src/common/fsst.cpp +15 -14
  109. package/src/duckdb/src/common/gzip_file_system.cpp +3 -1
  110. package/src/duckdb/src/common/hive_partitioning.cpp +103 -43
  111. package/src/duckdb/src/common/http_util.cpp +25 -0
  112. package/src/duckdb/src/common/local_file_system.cpp +48 -27
  113. package/src/duckdb/src/common/multi_file_list.cpp +113 -22
  114. package/src/duckdb/src/common/multi_file_reader.cpp +59 -58
  115. package/src/duckdb/src/common/operator/cast_operators.cpp +133 -34
  116. package/src/duckdb/src/common/operator/string_cast.cpp +42 -11
  117. package/src/duckdb/src/common/progress_bar/progress_bar.cpp +2 -2
  118. package/src/duckdb/src/common/progress_bar/terminal_progress_bar_display.cpp +1 -1
  119. package/src/duckdb/src/common/radix_partitioning.cpp +31 -21
  120. package/src/duckdb/src/common/random_engine.cpp +4 -0
  121. package/src/duckdb/src/common/re2_regex.cpp +47 -12
  122. package/src/duckdb/src/common/render_tree.cpp +243 -0
  123. package/src/duckdb/src/common/row_operations/row_aggregate.cpp +1 -1
  124. package/src/duckdb/src/common/row_operations/row_gather.cpp +2 -2
  125. package/src/duckdb/src/common/row_operations/row_matcher.cpp +58 -5
  126. package/src/duckdb/src/common/row_operations/row_radix_scatter.cpp +79 -43
  127. package/src/duckdb/src/common/serializer/binary_deserializer.cpp +1 -1
  128. package/src/duckdb/src/common/serializer/buffered_file_reader.cpp +6 -4
  129. package/src/duckdb/src/common/serializer/buffered_file_writer.cpp +18 -9
  130. package/src/duckdb/src/common/serializer/memory_stream.cpp +1 -0
  131. package/src/duckdb/src/common/sort/partition_state.cpp +33 -18
  132. package/src/duckdb/src/common/sort/radix_sort.cpp +22 -15
  133. package/src/duckdb/src/common/sort/sort_state.cpp +19 -16
  134. package/src/duckdb/src/common/sort/sorted_block.cpp +11 -10
  135. package/src/duckdb/src/common/string_util.cpp +167 -10
  136. package/src/duckdb/src/common/tree_renderer/graphviz_tree_renderer.cpp +108 -0
  137. package/src/duckdb/src/common/tree_renderer/html_tree_renderer.cpp +267 -0
  138. package/src/duckdb/src/common/tree_renderer/json_tree_renderer.cpp +116 -0
  139. package/src/duckdb/src/common/tree_renderer/text_tree_renderer.cpp +482 -0
  140. package/src/duckdb/src/common/tree_renderer/tree_renderer.cpp +12 -0
  141. package/src/duckdb/src/common/tree_renderer.cpp +16 -508
  142. package/src/duckdb/src/common/types/batched_data_collection.cpp +78 -9
  143. package/src/duckdb/src/common/types/bit.cpp +24 -22
  144. package/src/duckdb/src/common/types/blob.cpp +15 -11
  145. package/src/duckdb/src/common/types/column/column_data_allocator.cpp +18 -9
  146. package/src/duckdb/src/common/types/column/column_data_collection.cpp +4 -4
  147. package/src/duckdb/src/common/types/column/column_data_collection_segment.cpp +3 -4
  148. package/src/duckdb/src/common/types/column/column_data_consumer.cpp +2 -2
  149. package/src/duckdb/src/common/types/column/partitioned_column_data.cpp +70 -21
  150. package/src/duckdb/src/common/types/data_chunk.cpp +10 -1
  151. package/src/duckdb/src/common/types/date.cpp +8 -19
  152. package/src/duckdb/src/common/types/decimal.cpp +3 -2
  153. package/src/duckdb/src/common/types/hugeint.cpp +11 -3
  154. package/src/duckdb/src/common/types/hyperloglog.cpp +212 -227
  155. package/src/duckdb/src/common/types/interval.cpp +1 -1
  156. package/src/duckdb/src/common/types/list_segment.cpp +83 -49
  157. package/src/duckdb/src/common/types/row/partitioned_tuple_data.cpp +22 -83
  158. package/src/duckdb/src/common/types/row/row_data_collection.cpp +2 -2
  159. package/src/duckdb/src/common/types/row/row_data_collection_scanner.cpp +20 -4
  160. package/src/duckdb/src/common/types/row/tuple_data_allocator.cpp +28 -7
  161. package/src/duckdb/src/common/types/row/tuple_data_collection.cpp +29 -14
  162. package/src/duckdb/src/common/types/row/tuple_data_scatter_gather.cpp +152 -102
  163. package/src/duckdb/src/common/types/row/tuple_data_segment.cpp +4 -1
  164. package/src/duckdb/src/common/types/selection_vector.cpp +17 -1
  165. package/src/duckdb/src/common/types/time.cpp +62 -31
  166. package/src/duckdb/src/common/types/timestamp.cpp +70 -12
  167. package/src/duckdb/src/common/types/uuid.cpp +1 -1
  168. package/src/duckdb/src/common/types/validity_mask.cpp +40 -5
  169. package/src/duckdb/src/common/types/value.cpp +50 -8
  170. package/src/duckdb/src/common/types/varint.cpp +295 -0
  171. package/src/duckdb/src/common/types/vector.cpp +165 -54
  172. package/src/duckdb/src/common/types/vector_buffer.cpp +5 -4
  173. package/src/duckdb/src/common/types.cpp +106 -26
  174. package/src/duckdb/src/common/vector_operations/vector_copy.cpp +13 -25
  175. package/src/duckdb/src/common/vector_operations/vector_hash.cpp +6 -0
  176. package/src/duckdb/src/common/virtual_file_system.cpp +3 -3
  177. package/src/duckdb/src/core_functions/aggregate/distributive/approx_count.cpp +35 -82
  178. package/src/duckdb/src/core_functions/aggregate/distributive/arg_min_max.cpp +283 -46
  179. package/src/duckdb/src/core_functions/aggregate/distributive/bitagg.cpp +4 -4
  180. package/src/duckdb/src/core_functions/aggregate/distributive/entropy.cpp +3 -2
  181. package/src/duckdb/src/core_functions/aggregate/distributive/minmax.cpp +226 -338
  182. package/src/duckdb/src/core_functions/aggregate/distributive/sum.cpp +2 -0
  183. package/src/duckdb/src/core_functions/aggregate/holistic/approx_top_k.cpp +388 -0
  184. package/src/duckdb/src/core_functions/aggregate/holistic/approximate_quantile.cpp +63 -21
  185. package/src/duckdb/src/core_functions/aggregate/holistic/mad.cpp +330 -0
  186. package/src/duckdb/src/core_functions/aggregate/holistic/mode.cpp +136 -97
  187. package/src/duckdb/src/core_functions/aggregate/holistic/quantile.cpp +601 -1485
  188. package/src/duckdb/src/core_functions/aggregate/nested/binned_histogram.cpp +405 -0
  189. package/src/duckdb/src/core_functions/aggregate/nested/histogram.cpp +136 -165
  190. package/src/duckdb/src/core_functions/function_list.cpp +35 -8
  191. package/src/duckdb/src/core_functions/lambda_functions.cpp +5 -7
  192. package/src/duckdb/src/core_functions/scalar/array/array_functions.cpp +172 -198
  193. package/src/duckdb/src/core_functions/scalar/blob/create_sort_key.cpp +341 -54
  194. package/src/duckdb/src/core_functions/scalar/date/date_diff.cpp +2 -2
  195. package/src/duckdb/src/core_functions/scalar/date/date_part.cpp +89 -29
  196. package/src/duckdb/src/core_functions/scalar/date/date_trunc.cpp +1 -1
  197. package/src/duckdb/src/core_functions/scalar/date/make_date.cpp +2 -2
  198. package/src/duckdb/src/core_functions/scalar/date/strftime.cpp +133 -71
  199. package/src/duckdb/src/core_functions/scalar/date/to_interval.cpp +1 -1
  200. package/src/duckdb/src/core_functions/scalar/enum/enum_functions.cpp +1 -1
  201. package/src/duckdb/src/core_functions/scalar/generic/can_implicitly_cast.cpp +40 -0
  202. package/src/duckdb/src/core_functions/scalar/generic/error.cpp +1 -1
  203. package/src/duckdb/src/core_functions/scalar/generic/least.cpp +161 -58
  204. package/src/duckdb/src/core_functions/scalar/generic/typeof.cpp +13 -0
  205. package/src/duckdb/src/core_functions/scalar/list/array_slice.cpp +1 -1
  206. package/src/duckdb/src/core_functions/scalar/list/list_aggregates.cpp +59 -75
  207. package/src/duckdb/src/core_functions/scalar/list/list_distance.cpp +93 -40
  208. package/src/duckdb/src/core_functions/scalar/list/list_has_any_or_all.cpp +227 -0
  209. package/src/duckdb/src/core_functions/scalar/list/list_reduce.cpp +20 -19
  210. package/src/duckdb/src/core_functions/scalar/list/list_sort.cpp +0 -2
  211. package/src/duckdb/src/core_functions/scalar/list/list_value.cpp +106 -8
  212. package/src/duckdb/src/core_functions/scalar/map/map_contains.cpp +56 -0
  213. package/src/duckdb/src/core_functions/scalar/map/map_extract.cpp +73 -118
  214. package/src/duckdb/src/core_functions/scalar/math/numeric.cpp +98 -2
  215. package/src/duckdb/src/core_functions/scalar/operators/bitwise.cpp +1 -2
  216. package/src/duckdb/src/core_functions/scalar/random/setseed.cpp +1 -1
  217. package/src/duckdb/src/core_functions/scalar/string/bar.cpp +1 -1
  218. package/src/duckdb/src/core_functions/scalar/string/hex.cpp +5 -1
  219. package/src/duckdb/src/core_functions/scalar/string/md5.cpp +10 -37
  220. package/src/duckdb/src/core_functions/scalar/string/printf.cpp +18 -2
  221. package/src/duckdb/src/core_functions/scalar/string/repeat.cpp +45 -0
  222. package/src/duckdb/src/core_functions/scalar/string/reverse.cpp +4 -5
  223. package/src/duckdb/src/core_functions/scalar/string/sha1.cpp +35 -0
  224. package/src/duckdb/src/core_functions/scalar/string/sha256.cpp +5 -2
  225. package/src/duckdb/src/core_functions/scalar/string/url_encode.cpp +49 -0
  226. package/src/duckdb/src/core_functions/scalar/struct/struct_pack.cpp +1 -2
  227. package/src/duckdb/src/core_functions/scalar/union/union_extract.cpp +4 -2
  228. package/src/duckdb/src/execution/adaptive_filter.cpp +30 -11
  229. package/src/duckdb/src/execution/aggregate_hashtable.cpp +13 -18
  230. package/src/duckdb/src/execution/expression_executor/execute_conjunction.cpp +4 -9
  231. package/src/duckdb/src/execution/expression_executor.cpp +1 -1
  232. package/src/duckdb/src/execution/index/art/art.cpp +683 -670
  233. package/src/duckdb/src/execution/index/art/art_key.cpp +121 -38
  234. package/src/duckdb/src/execution/index/art/base_leaf.cpp +168 -0
  235. package/src/duckdb/src/execution/index/art/base_node.cpp +163 -0
  236. package/src/duckdb/src/execution/index/art/iterator.cpp +148 -77
  237. package/src/duckdb/src/execution/index/art/leaf.cpp +159 -263
  238. package/src/duckdb/src/execution/index/art/node.cpp +493 -247
  239. package/src/duckdb/src/execution/index/art/node256.cpp +31 -91
  240. package/src/duckdb/src/execution/index/art/node256_leaf.cpp +71 -0
  241. package/src/duckdb/src/execution/index/art/node48.cpp +75 -143
  242. package/src/duckdb/src/execution/index/art/prefix.cpp +424 -244
  243. package/src/duckdb/src/execution/index/bound_index.cpp +7 -1
  244. package/src/duckdb/src/execution/index/fixed_size_allocator.cpp +22 -18
  245. package/src/duckdb/src/execution/index/fixed_size_buffer.cpp +22 -73
  246. package/src/duckdb/src/execution/join_hashtable.cpp +637 -179
  247. package/src/duckdb/src/execution/operator/aggregate/aggregate_object.cpp +4 -4
  248. package/src/duckdb/src/execution/operator/aggregate/physical_hash_aggregate.cpp +15 -10
  249. package/src/duckdb/src/execution/operator/aggregate/physical_perfecthash_aggregate.cpp +13 -8
  250. package/src/duckdb/src/execution/operator/aggregate/physical_streaming_window.cpp +525 -132
  251. package/src/duckdb/src/execution/operator/aggregate/physical_ungrouped_aggregate.cpp +147 -138
  252. package/src/duckdb/src/execution/operator/aggregate/physical_window.cpp +531 -312
  253. package/src/duckdb/src/execution/operator/csv_scanner/buffer_manager/csv_buffer.cpp +1 -1
  254. package/src/duckdb/src/execution/operator/csv_scanner/buffer_manager/csv_buffer_manager.cpp +4 -3
  255. package/src/duckdb/src/execution/operator/csv_scanner/buffer_manager/csv_file_handle.cpp +9 -2
  256. package/src/duckdb/src/execution/operator/csv_scanner/scanner/base_scanner.cpp +13 -17
  257. package/src/duckdb/src/execution/operator/csv_scanner/scanner/column_count_scanner.cpp +60 -16
  258. package/src/duckdb/src/execution/operator/csv_scanner/scanner/csv_schema.cpp +105 -0
  259. package/src/duckdb/src/execution/operator/csv_scanner/scanner/scanner_boundary.cpp +24 -24
  260. package/src/duckdb/src/execution/operator/csv_scanner/scanner/skip_scanner.cpp +25 -2
  261. package/src/duckdb/src/execution/operator/csv_scanner/scanner/string_value_scanner.cpp +275 -112
  262. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/csv_sniffer.cpp +106 -11
  263. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/dialect_detection.cpp +253 -115
  264. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/header_detection.cpp +93 -52
  265. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/type_detection.cpp +116 -76
  266. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/type_refinement.cpp +29 -14
  267. package/src/duckdb/src/execution/operator/csv_scanner/sniffer/type_replacement.cpp +1 -1
  268. package/src/duckdb/src/execution/operator/csv_scanner/state_machine/csv_state_machine_cache.cpp +70 -26
  269. package/src/duckdb/src/execution/operator/csv_scanner/table_function/csv_file_scanner.cpp +81 -60
  270. package/src/duckdb/src/execution/operator/csv_scanner/table_function/global_csv_state.cpp +88 -50
  271. package/src/duckdb/src/execution/operator/csv_scanner/util/csv_error.cpp +161 -51
  272. package/src/duckdb/src/execution/operator/csv_scanner/util/csv_reader_options.cpp +59 -17
  273. package/src/duckdb/src/execution/operator/filter/physical_filter.cpp +5 -5
  274. package/src/duckdb/src/execution/operator/helper/physical_batch_collector.cpp +0 -21
  275. package/src/duckdb/src/execution/operator/helper/physical_buffered_batch_collector.cpp +109 -0
  276. package/src/duckdb/src/execution/operator/helper/physical_buffered_collector.cpp +5 -13
  277. package/src/duckdb/src/execution/operator/helper/physical_explain_analyze.cpp +1 -1
  278. package/src/duckdb/src/execution/operator/helper/physical_load.cpp +12 -4
  279. package/src/duckdb/src/execution/operator/helper/physical_materialized_collector.cpp +0 -16
  280. package/src/duckdb/src/execution/operator/helper/physical_reservoir_sample.cpp +4 -2
  281. package/src/duckdb/src/execution/operator/helper/physical_reset.cpp +5 -0
  282. package/src/duckdb/src/execution/operator/helper/physical_result_collector.cpp +3 -1
  283. package/src/duckdb/src/execution/operator/helper/physical_set_variable.cpp +39 -0
  284. package/src/duckdb/src/execution/operator/helper/physical_streaming_sample.cpp +4 -2
  285. package/src/duckdb/src/execution/operator/helper/physical_transaction.cpp +16 -5
  286. package/src/duckdb/src/execution/operator/join/outer_join_marker.cpp +1 -1
  287. package/src/duckdb/src/execution/operator/join/perfect_hash_join_executor.cpp +1 -1
  288. package/src/duckdb/src/execution/operator/join/physical_asof_join.cpp +1 -1
  289. package/src/duckdb/src/execution/operator/join/physical_blockwise_nl_join.cpp +5 -4
  290. package/src/duckdb/src/execution/operator/join/physical_comparison_join.cpp +59 -21
  291. package/src/duckdb/src/execution/operator/join/physical_delim_join.cpp +7 -4
  292. package/src/duckdb/src/execution/operator/join/physical_hash_join.cpp +333 -176
  293. package/src/duckdb/src/execution/operator/join/physical_iejoin.cpp +57 -34
  294. package/src/duckdb/src/execution/operator/join/physical_join.cpp +16 -8
  295. package/src/duckdb/src/execution/operator/join/physical_left_delim_join.cpp +10 -4
  296. package/src/duckdb/src/execution/operator/join/physical_nested_loop_join.cpp +2 -5
  297. package/src/duckdb/src/execution/operator/join/physical_piecewise_merge_join.cpp +3 -3
  298. package/src/duckdb/src/execution/operator/join/physical_range_join.cpp +5 -5
  299. package/src/duckdb/src/execution/operator/join/physical_right_delim_join.cpp +7 -2
  300. package/src/duckdb/src/execution/operator/order/physical_order.cpp +17 -12
  301. package/src/duckdb/src/execution/operator/order/physical_top_n.cpp +12 -9
  302. package/src/duckdb/src/execution/operator/persistent/physical_batch_copy_to_file.cpp +35 -17
  303. package/src/duckdb/src/execution/operator/persistent/physical_batch_insert.cpp +17 -11
  304. package/src/duckdb/src/execution/operator/persistent/physical_copy_database.cpp +5 -1
  305. package/src/duckdb/src/execution/operator/persistent/physical_copy_to_file.cpp +156 -47
  306. package/src/duckdb/src/execution/operator/persistent/physical_insert.cpp +10 -2
  307. package/src/duckdb/src/execution/operator/persistent/physical_update.cpp +1 -3
  308. package/src/duckdb/src/execution/operator/projection/physical_pivot.cpp +2 -2
  309. package/src/duckdb/src/execution/operator/projection/physical_projection.cpp +13 -6
  310. package/src/duckdb/src/execution/operator/projection/physical_tableinout_function.cpp +22 -3
  311. package/src/duckdb/src/execution/operator/projection/physical_unnest.cpp +19 -3
  312. package/src/duckdb/src/execution/operator/scan/physical_column_data_scan.cpp +37 -22
  313. package/src/duckdb/src/execution/operator/scan/physical_table_scan.cpp +77 -21
  314. package/src/duckdb/src/execution/operator/schema/physical_attach.cpp +27 -55
  315. package/src/duckdb/src/execution/operator/schema/physical_create_art_index.cpp +41 -44
  316. package/src/duckdb/src/execution/operator/set/physical_cte.cpp +4 -6
  317. package/src/duckdb/src/execution/operator/set/physical_recursive_cte.cpp +4 -6
  318. package/src/duckdb/src/execution/operator/set/physical_union.cpp +18 -4
  319. package/src/duckdb/src/execution/perfect_aggregate_hashtable.cpp +3 -2
  320. package/src/duckdb/src/execution/physical_operator.cpp +45 -4
  321. package/src/duckdb/src/execution/physical_plan/plan_comparison_join.cpp +18 -7
  322. package/src/duckdb/src/execution/physical_plan/plan_copy_to_file.cpp +8 -3
  323. package/src/duckdb/src/execution/physical_plan/plan_delim_join.cpp +13 -6
  324. package/src/duckdb/src/execution/physical_plan/plan_explain.cpp +3 -3
  325. package/src/duckdb/src/execution/physical_plan/plan_get.cpp +111 -19
  326. package/src/duckdb/src/execution/physical_plan/plan_limit.cpp +19 -2
  327. package/src/duckdb/src/execution/physical_plan/plan_set.cpp +9 -0
  328. package/src/duckdb/src/execution/physical_plan/plan_window.cpp +3 -1
  329. package/src/duckdb/src/execution/physical_plan_generator.cpp +3 -3
  330. package/src/duckdb/src/execution/radix_partitioned_hashtable.cpp +49 -49
  331. package/src/duckdb/src/execution/reservoir_sample.cpp +2 -2
  332. package/src/duckdb/src/execution/window_executor.cpp +556 -318
  333. package/src/duckdb/src/execution/window_segment_tree.cpp +1058 -485
  334. package/src/duckdb/src/function/aggregate/distributive/count.cpp +5 -5
  335. package/src/duckdb/src/function/aggregate/distributive/first.cpp +92 -95
  336. package/src/duckdb/src/function/aggregate/sorted_aggregate_function.cpp +10 -9
  337. package/src/duckdb/src/function/aggregate_function.cpp +8 -0
  338. package/src/duckdb/src/function/cast/cast_function_set.cpp +10 -1
  339. package/src/duckdb/src/function/cast/decimal_cast.cpp +10 -1
  340. package/src/duckdb/src/function/cast/default_casts.cpp +2 -0
  341. package/src/duckdb/src/function/cast/numeric_casts.cpp +3 -0
  342. package/src/duckdb/src/function/cast/string_cast.cpp +8 -5
  343. package/src/duckdb/src/function/cast/time_casts.cpp +2 -2
  344. package/src/duckdb/src/function/cast/union_casts.cpp +1 -1
  345. package/src/duckdb/src/function/cast/varint_casts.cpp +283 -0
  346. package/src/duckdb/src/function/cast/vector_cast_helpers.cpp +3 -1
  347. package/src/duckdb/src/function/cast_rules.cpp +104 -15
  348. package/src/duckdb/src/function/compression_config.cpp +35 -33
  349. package/src/duckdb/src/function/copy_function.cpp +27 -0
  350. package/src/duckdb/src/function/function_binder.cpp +39 -11
  351. package/src/duckdb/src/function/macro_function.cpp +75 -32
  352. package/src/duckdb/src/function/pragma/pragma_queries.cpp +10 -0
  353. package/src/duckdb/src/function/scalar/compressed_materialization/compress_string.cpp +1 -0
  354. package/src/duckdb/src/function/scalar/generic/binning.cpp +507 -0
  355. package/src/duckdb/src/function/scalar/generic/getvariable.cpp +58 -0
  356. package/src/duckdb/src/function/scalar/generic_functions.cpp +1 -0
  357. package/src/duckdb/src/function/scalar/list/contains_or_position.cpp +33 -47
  358. package/src/duckdb/src/function/scalar/list/list_extract.cpp +70 -143
  359. package/src/duckdb/src/function/scalar/list/list_resize.cpp +93 -84
  360. package/src/duckdb/src/function/scalar/list/list_zip.cpp +3 -0
  361. package/src/duckdb/src/function/scalar/operators/arithmetic.cpp +24 -11
  362. package/src/duckdb/src/function/scalar/sequence/nextval.cpp +4 -4
  363. package/src/duckdb/src/function/scalar/strftime_format.cpp +196 -57
  364. package/src/duckdb/src/function/scalar/string/caseconvert.cpp +9 -7
  365. package/src/duckdb/src/function/scalar/string/concat.cpp +239 -123
  366. package/src/duckdb/src/function/scalar/string/concat_ws.cpp +149 -0
  367. package/src/duckdb/src/function/scalar/string/contains.cpp +18 -7
  368. package/src/duckdb/src/function/scalar/string/like.cpp +2 -2
  369. package/src/duckdb/src/function/scalar/string/substring.cpp +6 -11
  370. package/src/duckdb/src/function/scalar/string_functions.cpp +1 -0
  371. package/src/duckdb/src/function/scalar/struct/struct_extract.cpp +7 -3
  372. package/src/duckdb/src/function/scalar/system/aggregate_export.cpp +5 -5
  373. package/src/duckdb/src/function/scalar_function.cpp +5 -2
  374. package/src/duckdb/src/function/scalar_macro_function.cpp +2 -2
  375. package/src/duckdb/src/function/table/arrow/arrow_duck_schema.cpp +20 -39
  376. package/src/duckdb/src/function/table/arrow/arrow_type_info.cpp +135 -0
  377. package/src/duckdb/src/function/table/arrow.cpp +194 -52
  378. package/src/duckdb/src/function/table/arrow_conversion.cpp +212 -69
  379. package/src/duckdb/src/function/table/copy_csv.cpp +43 -14
  380. package/src/duckdb/src/function/table/query_function.cpp +80 -0
  381. package/src/duckdb/src/function/table/range.cpp +222 -142
  382. package/src/duckdb/src/function/table/read_csv.cpp +25 -13
  383. package/src/duckdb/src/function/table/sniff_csv.cpp +55 -35
  384. package/src/duckdb/src/function/table/system/duckdb_constraints.cpp +141 -129
  385. package/src/duckdb/src/function/table/system/duckdb_extensions.cpp +25 -14
  386. package/src/duckdb/src/function/table/system/duckdb_functions.cpp +20 -14
  387. package/src/duckdb/src/function/table/system/duckdb_indexes.cpp +15 -1
  388. package/src/duckdb/src/function/table/system/duckdb_variables.cpp +84 -0
  389. package/src/duckdb/src/function/table/system/test_all_types.cpp +1 -0
  390. package/src/duckdb/src/function/table/system/test_vector_types.cpp +33 -3
  391. package/src/duckdb/src/function/table/system_functions.cpp +1 -0
  392. package/src/duckdb/src/function/table/table_scan.cpp +45 -22
  393. package/src/duckdb/src/function/table/unnest.cpp +2 -2
  394. package/src/duckdb/src/function/table/version/pragma_version.cpp +4 -4
  395. package/src/duckdb/src/function/table_function.cpp +5 -4
  396. package/src/duckdb/src/function/table_macro_function.cpp +2 -2
  397. package/src/duckdb/src/include/duckdb/catalog/catalog.hpp +8 -4
  398. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/duck_index_entry.hpp +5 -2
  399. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/duck_schema_entry.hpp +3 -0
  400. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/index_catalog_entry.hpp +2 -2
  401. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/macro_catalog_entry.hpp +3 -4
  402. package/src/duckdb/src/include/duckdb/catalog/catalog_entry/table_catalog_entry.hpp +5 -5
  403. package/src/duckdb/src/include/duckdb/catalog/default/builtin_types/types.hpp +2 -1
  404. package/src/duckdb/src/include/duckdb/catalog/default/default_functions.hpp +4 -5
  405. package/src/duckdb/src/include/duckdb/catalog/default/default_generator.hpp +4 -5
  406. package/src/duckdb/src/include/duckdb/catalog/default/default_schemas.hpp +2 -1
  407. package/src/duckdb/src/include/duckdb/catalog/default/default_table_functions.hpp +47 -0
  408. package/src/duckdb/src/include/duckdb/catalog/duck_catalog.hpp +2 -0
  409. package/src/duckdb/src/include/duckdb/catalog/similar_catalog_entry.hpp +2 -2
  410. package/src/duckdb/src/include/duckdb/common/allocator.hpp +9 -1
  411. package/src/duckdb/src/include/duckdb/common/array_ptr.hpp +120 -0
  412. package/src/duckdb/src/include/duckdb/common/arrow/appender/append_data.hpp +37 -11
  413. package/src/duckdb/src/include/duckdb/common/arrow/appender/enum_data.hpp +9 -8
  414. package/src/duckdb/src/include/duckdb/common/arrow/appender/list.hpp +1 -0
  415. package/src/duckdb/src/include/duckdb/common/arrow/appender/list_data.hpp +6 -4
  416. package/src/duckdb/src/include/duckdb/common/arrow/appender/list_view_data.hpp +92 -0
  417. package/src/duckdb/src/include/duckdb/common/arrow/appender/map_data.hpp +2 -2
  418. package/src/duckdb/src/include/duckdb/common/arrow/appender/scalar_data.hpp +26 -4
  419. package/src/duckdb/src/include/duckdb/common/arrow/appender/varchar_data.hpp +90 -11
  420. package/src/duckdb/src/include/duckdb/common/arrow/arrow_appender.hpp +6 -6
  421. package/src/duckdb/src/include/duckdb/common/arrow/arrow_buffer.hpp +8 -1
  422. package/src/duckdb/src/include/duckdb/common/arrow/arrow_merge_event.hpp +62 -0
  423. package/src/duckdb/src/include/duckdb/common/arrow/arrow_query_result.hpp +52 -0
  424. package/src/duckdb/src/include/duckdb/common/arrow/arrow_types_extension.hpp +42 -0
  425. package/src/duckdb/src/include/duckdb/common/arrow/physical_arrow_batch_collector.hpp +30 -0
  426. package/src/duckdb/src/include/duckdb/common/arrow/physical_arrow_collector.hpp +65 -0
  427. package/src/duckdb/src/include/duckdb/common/arrow/schema_metadata.hpp +43 -0
  428. package/src/duckdb/src/include/duckdb/common/bswap.hpp +18 -16
  429. package/src/duckdb/src/include/duckdb/common/cgroups.hpp +30 -0
  430. package/src/duckdb/src/include/duckdb/common/compressed_file_system.hpp +3 -0
  431. package/src/duckdb/src/include/duckdb/common/dl.hpp +8 -1
  432. package/src/duckdb/src/include/duckdb/common/encryption_state.hpp +48 -0
  433. package/src/duckdb/src/include/duckdb/common/enum_util.hpp +88 -0
  434. package/src/duckdb/src/include/duckdb/common/enums/checkpoint_type.hpp +2 -2
  435. package/src/duckdb/src/include/duckdb/common/enums/copy_overwrite_mode.hpp +6 -1
  436. package/src/duckdb/src/include/duckdb/common/enums/destroy_buffer_upon.hpp +21 -0
  437. package/src/duckdb/src/include/duckdb/common/enums/explain_format.hpp +17 -0
  438. package/src/duckdb/src/include/duckdb/common/enums/file_compression_type.hpp +4 -0
  439. package/src/duckdb/src/include/duckdb/common/enums/join_type.hpp +2 -2
  440. package/src/duckdb/src/include/duckdb/common/enums/metric_type.hpp +88 -0
  441. package/src/duckdb/src/include/duckdb/common/enums/optimizer_type.hpp +6 -1
  442. package/src/duckdb/src/include/duckdb/common/enums/pending_execution_result.hpp +2 -1
  443. package/src/duckdb/src/include/duckdb/common/enums/physical_operator_type.hpp +1 -0
  444. package/src/duckdb/src/include/duckdb/common/enums/profiler_format.hpp +1 -1
  445. package/src/duckdb/src/include/duckdb/common/enums/relation_type.hpp +3 -1
  446. package/src/duckdb/src/include/duckdb/common/enums/set_scope.hpp +2 -1
  447. package/src/duckdb/src/include/duckdb/common/enums/statement_type.hpp +23 -2
  448. package/src/duckdb/src/include/duckdb/common/enums/stream_execution_result.hpp +25 -0
  449. package/src/duckdb/src/include/duckdb/common/enums/tableref_type.hpp +2 -1
  450. package/src/duckdb/src/include/duckdb/common/enums/wal_type.hpp +1 -0
  451. package/src/duckdb/src/include/duckdb/common/error_data.hpp +5 -2
  452. package/src/duckdb/src/include/duckdb/common/exception/binder_exception.hpp +1 -0
  453. package/src/duckdb/src/include/duckdb/common/exception.hpp +20 -2
  454. package/src/duckdb/src/include/duckdb/common/extra_operator_info.hpp +12 -0
  455. package/src/duckdb/src/include/duckdb/common/file_buffer.hpp +2 -0
  456. package/src/duckdb/src/include/duckdb/common/file_open_flags.hpp +16 -0
  457. package/src/duckdb/src/include/duckdb/common/file_opener.hpp +18 -0
  458. package/src/duckdb/src/include/duckdb/common/file_system.hpp +3 -0
  459. package/src/duckdb/src/include/duckdb/common/filename_pattern.hpp +4 -0
  460. package/src/duckdb/src/include/duckdb/common/fixed_size_map.hpp +160 -96
  461. package/src/duckdb/src/include/duckdb/common/fsst.hpp +9 -2
  462. package/src/duckdb/src/include/duckdb/common/helper.hpp +22 -8
  463. package/src/duckdb/src/include/duckdb/common/hive_partitioning.hpp +16 -7
  464. package/src/duckdb/src/include/duckdb/common/http_util.hpp +19 -0
  465. package/src/duckdb/src/include/duckdb/common/insertion_order_preserving_map.hpp +19 -6
  466. package/src/duckdb/src/include/duckdb/common/limits.hpp +9 -2
  467. package/src/duckdb/src/include/duckdb/common/multi_file_list.hpp +38 -6
  468. package/src/duckdb/src/include/duckdb/common/multi_file_reader.hpp +9 -2
  469. package/src/duckdb/src/include/duckdb/common/multi_file_reader_options.hpp +5 -1
  470. package/src/duckdb/src/include/duckdb/common/numeric_utils.hpp +82 -50
  471. package/src/duckdb/src/include/duckdb/common/operator/abs.hpp +11 -0
  472. package/src/duckdb/src/include/duckdb/common/operator/cast_operators.hpp +7 -3
  473. package/src/duckdb/src/include/duckdb/common/operator/decimal_cast_operators.hpp +23 -1
  474. package/src/duckdb/src/include/duckdb/common/operator/double_cast_operator.hpp +2 -1
  475. package/src/duckdb/src/include/duckdb/common/operator/integer_cast_operator.hpp +1 -1
  476. package/src/duckdb/src/include/duckdb/common/operator/numeric_cast.hpp +4 -0
  477. package/src/duckdb/src/include/duckdb/common/operator/string_cast.hpp +2 -0
  478. package/src/duckdb/src/include/duckdb/common/optional_ptr.hpp +10 -5
  479. package/src/duckdb/src/include/duckdb/common/optionally_owned_ptr.hpp +1 -0
  480. package/src/duckdb/src/include/duckdb/common/owning_string_map.hpp +155 -0
  481. package/src/duckdb/src/include/duckdb/common/perfect_map_set.hpp +2 -3
  482. package/src/duckdb/src/include/duckdb/common/platform.hpp +58 -0
  483. package/src/duckdb/src/include/duckdb/common/radix.hpp +172 -27
  484. package/src/duckdb/src/include/duckdb/common/radix_partitioning.hpp +5 -1
  485. package/src/duckdb/src/include/duckdb/common/random_engine.hpp +1 -0
  486. package/src/duckdb/src/include/duckdb/common/re2_regex.hpp +1 -1
  487. package/src/duckdb/src/include/duckdb/common/render_tree.hpp +77 -0
  488. package/src/duckdb/src/include/duckdb/common/row_operations/row_matcher.hpp +12 -0
  489. package/src/duckdb/src/include/duckdb/common/serializer/binary_serializer.hpp +6 -2
  490. package/src/duckdb/src/include/duckdb/common/serializer/buffered_file_writer.hpp +5 -3
  491. package/src/duckdb/src/include/duckdb/common/serializer/deserializer.hpp +15 -7
  492. package/src/duckdb/src/include/duckdb/common/serializer/memory_stream.hpp +3 -1
  493. package/src/duckdb/src/include/duckdb/common/serializer/serialization_data.hpp +245 -0
  494. package/src/duckdb/src/include/duckdb/common/serializer/serializer.hpp +10 -0
  495. package/src/duckdb/src/include/duckdb/common/sort/duckdb_pdqsort.hpp +10 -11
  496. package/src/duckdb/src/include/duckdb/common/sort/partition_state.hpp +12 -6
  497. package/src/duckdb/src/include/duckdb/common/string_util.hpp +37 -7
  498. package/src/duckdb/src/include/duckdb/common/tree_renderer/graphviz_tree_renderer.hpp +44 -0
  499. package/src/duckdb/src/include/duckdb/common/tree_renderer/html_tree_renderer.hpp +44 -0
  500. package/src/duckdb/src/include/duckdb/common/tree_renderer/json_tree_renderer.hpp +44 -0
  501. package/src/duckdb/src/include/duckdb/common/tree_renderer/text_tree_renderer.hpp +119 -0
  502. package/src/duckdb/src/include/duckdb/common/tree_renderer.hpp +9 -123
  503. package/src/duckdb/src/include/duckdb/common/type_visitor.hpp +96 -0
  504. package/src/duckdb/src/include/duckdb/common/typedefs.hpp +11 -1
  505. package/src/duckdb/src/include/duckdb/common/types/arrow_string_view_type.hpp +84 -0
  506. package/src/duckdb/src/include/duckdb/common/types/batched_data_collection.hpp +36 -1
  507. package/src/duckdb/src/include/duckdb/common/types/bit.hpp +1 -1
  508. package/src/duckdb/src/include/duckdb/common/types/cast_helpers.hpp +2 -2
  509. package/src/duckdb/src/include/duckdb/common/types/column/column_data_allocator.hpp +4 -2
  510. package/src/duckdb/src/include/duckdb/common/types/column/partitioned_column_data.hpp +52 -0
  511. package/src/duckdb/src/include/duckdb/common/types/data_chunk.hpp +2 -0
  512. package/src/duckdb/src/include/duckdb/common/types/date.hpp +0 -3
  513. package/src/duckdb/src/include/duckdb/common/types/date_lookup_cache.hpp +65 -0
  514. package/src/duckdb/src/include/duckdb/common/types/datetime.hpp +5 -2
  515. package/src/duckdb/src/include/duckdb/common/types/hyperloglog.hpp +49 -40
  516. package/src/duckdb/src/include/duckdb/common/types/interval.hpp +5 -1
  517. package/src/duckdb/src/include/duckdb/common/types/list_segment.hpp +2 -1
  518. package/src/duckdb/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp +41 -9
  519. package/src/duckdb/src/include/duckdb/common/types/row/row_data_collection.hpp +4 -3
  520. package/src/duckdb/src/include/duckdb/common/types/row/row_data_collection_scanner.hpp +3 -1
  521. package/src/duckdb/src/include/duckdb/common/types/row/tuple_data_allocator.hpp +4 -0
  522. package/src/duckdb/src/include/duckdb/common/types/row/tuple_data_collection.hpp +4 -0
  523. package/src/duckdb/src/include/duckdb/common/types/row/tuple_data_states.hpp +1 -1
  524. package/src/duckdb/src/include/duckdb/common/types/selection_vector.hpp +4 -0
  525. package/src/duckdb/src/include/duckdb/common/types/string_type.hpp +4 -1
  526. package/src/duckdb/src/include/duckdb/common/types/time.hpp +11 -6
  527. package/src/duckdb/src/include/duckdb/common/types/timestamp.hpp +13 -3
  528. package/src/duckdb/src/include/duckdb/common/types/validity_mask.hpp +103 -12
  529. package/src/duckdb/src/include/duckdb/common/types/value.hpp +12 -3
  530. package/src/duckdb/src/include/duckdb/common/types/varint.hpp +107 -0
  531. package/src/duckdb/src/include/duckdb/common/types/vector.hpp +5 -1
  532. package/src/duckdb/src/include/duckdb/common/types/vector_buffer.hpp +7 -2
  533. package/src/duckdb/src/include/duckdb/common/types.hpp +6 -39
  534. package/src/duckdb/src/include/duckdb/common/union_by_name.hpp +42 -10
  535. package/src/duckdb/src/include/duckdb/common/vector_operations/generic_executor.hpp +29 -0
  536. package/src/duckdb/src/include/duckdb/common/vector_operations/unary_executor.hpp +0 -7
  537. package/src/duckdb/src/include/duckdb/common/vector_operations/vector_operations.hpp +2 -0
  538. package/src/duckdb/src/include/duckdb/common/winapi.hpp +8 -0
  539. package/src/duckdb/src/include/duckdb/core_functions/aggregate/algebraic/covar.hpp +8 -4
  540. package/src/duckdb/src/include/duckdb/core_functions/aggregate/algebraic/stddev.hpp +8 -4
  541. package/src/duckdb/src/include/duckdb/core_functions/aggregate/distributive_functions.hpp +4 -2
  542. package/src/duckdb/src/include/duckdb/core_functions/aggregate/histogram_helpers.hpp +99 -0
  543. package/src/duckdb/src/include/duckdb/core_functions/aggregate/holistic_functions.hpp +16 -7
  544. package/src/duckdb/src/include/duckdb/core_functions/aggregate/minmax_n_helpers.hpp +396 -0
  545. package/src/duckdb/src/include/duckdb/core_functions/aggregate/nested_functions.hpp +10 -0
  546. package/src/duckdb/src/include/duckdb/core_functions/aggregate/quantile_helpers.hpp +65 -0
  547. package/src/duckdb/src/include/duckdb/core_functions/aggregate/quantile_sort_tree.hpp +349 -0
  548. package/src/duckdb/src/include/duckdb/core_functions/aggregate/quantile_state.hpp +300 -0
  549. package/src/duckdb/src/include/duckdb/core_functions/aggregate/regression/regr_slope.hpp +1 -1
  550. package/src/duckdb/src/include/duckdb/core_functions/aggregate/sort_key_helpers.hpp +55 -0
  551. package/src/duckdb/src/include/duckdb/core_functions/array_kernels.hpp +107 -0
  552. package/src/duckdb/src/include/duckdb/core_functions/create_sort_key.hpp +55 -0
  553. package/src/duckdb/src/include/duckdb/core_functions/lambda_functions.hpp +1 -2
  554. package/src/duckdb/src/include/duckdb/core_functions/scalar/array_functions.hpp +24 -0
  555. package/src/duckdb/src/include/duckdb/core_functions/scalar/date_functions.hpp +9 -0
  556. package/src/duckdb/src/include/duckdb/core_functions/scalar/generic_functions.hpp +27 -0
  557. package/src/duckdb/src/include/duckdb/core_functions/scalar/list_functions.hpp +80 -8
  558. package/src/duckdb/src/include/duckdb/core_functions/scalar/map_functions.hpp +9 -0
  559. package/src/duckdb/src/include/duckdb/core_functions/scalar/math_functions.hpp +54 -0
  560. package/src/duckdb/src/include/duckdb/core_functions/scalar/string_functions.hpp +30 -21
  561. package/src/duckdb/src/include/duckdb/execution/adaptive_filter.hpp +25 -14
  562. package/src/duckdb/src/include/duckdb/execution/aggregate_hashtable.hpp +2 -48
  563. package/src/duckdb/src/include/duckdb/execution/executor.hpp +25 -2
  564. package/src/duckdb/src/include/duckdb/execution/ht_entry.hpp +102 -0
  565. package/src/duckdb/src/include/duckdb/execution/index/art/art.hpp +94 -101
  566. package/src/duckdb/src/include/duckdb/execution/index/art/art_key.hpp +43 -25
  567. package/src/duckdb/src/include/duckdb/execution/index/art/base_leaf.hpp +109 -0
  568. package/src/duckdb/src/include/duckdb/execution/index/art/base_node.hpp +140 -0
  569. package/src/duckdb/src/include/duckdb/execution/index/art/iterator.hpp +43 -24
  570. package/src/duckdb/src/include/duckdb/execution/index/art/leaf.hpp +41 -52
  571. package/src/duckdb/src/include/duckdb/execution/index/art/node.hpp +133 -74
  572. package/src/duckdb/src/include/duckdb/execution/index/art/node256.hpp +46 -29
  573. package/src/duckdb/src/include/duckdb/execution/index/art/node256_leaf.hpp +53 -0
  574. package/src/duckdb/src/include/duckdb/execution/index/art/node48.hpp +52 -35
  575. package/src/duckdb/src/include/duckdb/execution/index/art/prefix.hpp +96 -57
  576. package/src/duckdb/src/include/duckdb/execution/index/bound_index.hpp +9 -4
  577. package/src/duckdb/src/include/duckdb/execution/index/fixed_size_allocator.hpp +48 -10
  578. package/src/duckdb/src/include/duckdb/execution/index/fixed_size_buffer.hpp +0 -2
  579. package/src/duckdb/src/include/duckdb/execution/index/index_pointer.hpp +4 -2
  580. package/src/duckdb/src/include/duckdb/execution/join_hashtable.hpp +114 -36
  581. package/src/duckdb/src/include/duckdb/execution/merge_sort_tree.hpp +158 -67
  582. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/aggregate_object.hpp +1 -1
  583. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_hash_aggregate.hpp +1 -1
  584. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_perfecthash_aggregate.hpp +1 -1
  585. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_streaming_window.hpp +19 -2
  586. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_ungrouped_aggregate.hpp +1 -1
  587. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/physical_window.hpp +1 -1
  588. package/src/duckdb/src/include/duckdb/execution/operator/aggregate/ungrouped_aggregate_state.hpp +75 -0
  589. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/base_scanner.hpp +81 -23
  590. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/column_count_scanner.hpp +27 -8
  591. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_buffer_manager.hpp +2 -1
  592. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_error.hpp +31 -22
  593. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_file_handle.hpp +4 -2
  594. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_file_scanner.hpp +48 -5
  595. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_option.hpp +7 -3
  596. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_reader_options.hpp +22 -12
  597. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_schema.hpp +35 -0
  598. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_sniffer.hpp +81 -39
  599. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_state.hpp +2 -1
  600. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_state_machine.hpp +18 -1
  601. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/csv_state_machine_cache.hpp +9 -7
  602. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/global_csv_state.hpp +5 -4
  603. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/header_value.hpp +26 -0
  604. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/scanner_boundary.hpp +6 -9
  605. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/skip_scanner.hpp +3 -0
  606. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/state_machine_options.hpp +5 -3
  607. package/src/duckdb/src/include/duckdb/execution/operator/csv_scanner/string_value_scanner.hpp +36 -19
  608. package/src/duckdb/src/include/duckdb/execution/operator/filter/physical_filter.hpp +1 -1
  609. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_batch_collector.hpp +21 -0
  610. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_buffered_batch_collector.hpp +53 -0
  611. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_buffered_collector.hpp +3 -0
  612. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_explain_analyze.hpp +6 -2
  613. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_materialized_collector.hpp +18 -0
  614. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_reservoir_sample.hpp +1 -1
  615. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_result_collector.hpp +6 -0
  616. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_set.hpp +2 -2
  617. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_set_variable.hpp +43 -0
  618. package/src/duckdb/src/include/duckdb/execution/operator/helper/physical_streaming_sample.hpp +1 -1
  619. package/src/duckdb/src/include/duckdb/execution/operator/join/join_filter_pushdown.hpp +59 -0
  620. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_blockwise_nl_join.hpp +1 -1
  621. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_comparison_join.hpp +8 -1
  622. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_delim_join.hpp +5 -2
  623. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_hash_join.hpp +4 -2
  624. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_iejoin.hpp +2 -0
  625. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_join.hpp +1 -1
  626. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_left_delim_join.hpp +3 -1
  627. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_range_join.hpp +4 -1
  628. package/src/duckdb/src/include/duckdb/execution/operator/join/physical_right_delim_join.hpp +3 -1
  629. package/src/duckdb/src/include/duckdb/execution/operator/order/physical_order.hpp +1 -1
  630. package/src/duckdb/src/include/duckdb/execution/operator/order/physical_top_n.hpp +1 -1
  631. package/src/duckdb/src/include/duckdb/execution/operator/persistent/batch_memory_manager.hpp +5 -37
  632. package/src/duckdb/src/include/duckdb/execution/operator/persistent/physical_batch_copy_to_file.hpp +5 -4
  633. package/src/duckdb/src/include/duckdb/execution/operator/persistent/physical_copy_to_file.hpp +8 -2
  634. package/src/duckdb/src/include/duckdb/execution/operator/projection/physical_projection.hpp +1 -1
  635. package/src/duckdb/src/include/duckdb/execution/operator/projection/physical_tableinout_function.hpp +2 -0
  636. package/src/duckdb/src/include/duckdb/execution/operator/scan/physical_column_data_scan.hpp +9 -3
  637. package/src/duckdb/src/include/duckdb/execution/operator/scan/physical_table_scan.hpp +8 -6
  638. package/src/duckdb/src/include/duckdb/execution/operator/schema/physical_create_art_index.hpp +2 -2
  639. package/src/duckdb/src/include/duckdb/execution/operator/set/physical_cte.hpp +1 -1
  640. package/src/duckdb/src/include/duckdb/execution/operator/set/physical_recursive_cte.hpp +1 -1
  641. package/src/duckdb/src/include/duckdb/execution/physical_operator.hpp +21 -6
  642. package/src/duckdb/src/include/duckdb/execution/physical_operator_states.hpp +3 -2
  643. package/src/duckdb/src/include/duckdb/execution/physical_plan_generator.hpp +3 -0
  644. package/src/duckdb/src/include/duckdb/execution/window_executor.hpp +137 -110
  645. package/src/duckdb/src/include/duckdb/execution/window_segment_tree.hpp +57 -126
  646. package/src/duckdb/src/include/duckdb/function/aggregate_function.hpp +21 -4
  647. package/src/duckdb/src/include/duckdb/function/cast/default_casts.hpp +1 -1
  648. package/src/duckdb/src/include/duckdb/function/compression/compression.hpp +10 -10
  649. package/src/duckdb/src/include/duckdb/function/compression_function.hpp +37 -7
  650. package/src/duckdb/src/include/duckdb/function/copy_function.hpp +24 -11
  651. package/src/duckdb/src/include/duckdb/function/function_binder.hpp +4 -4
  652. package/src/duckdb/src/include/duckdb/function/function_serialization.hpp +41 -1
  653. package/src/duckdb/src/include/duckdb/function/macro_function.hpp +15 -5
  654. package/src/duckdb/src/include/duckdb/function/pragma/pragma_functions.hpp +1 -0
  655. package/src/duckdb/src/include/duckdb/function/replacement_scan.hpp +20 -4
  656. package/src/duckdb/src/include/duckdb/function/scalar/generic_functions.hpp +6 -0
  657. package/src/duckdb/src/include/duckdb/function/scalar/list/contains_or_position.hpp +77 -109
  658. package/src/duckdb/src/include/duckdb/function/scalar/nested_functions.hpp +1 -1
  659. package/src/duckdb/src/include/duckdb/function/scalar/regexp.hpp +6 -3
  660. package/src/duckdb/src/include/duckdb/function/scalar/strftime_format.hpp +25 -12
  661. package/src/duckdb/src/include/duckdb/function/scalar/string_functions.hpp +9 -8
  662. package/src/duckdb/src/include/duckdb/function/scalar_function.hpp +38 -4
  663. package/src/duckdb/src/include/duckdb/function/scalar_macro_function.hpp +1 -1
  664. package/src/duckdb/src/include/duckdb/function/table/arrow/arrow_duck_schema.hpp +11 -57
  665. package/src/duckdb/src/include/duckdb/function/table/arrow/arrow_type_info.hpp +142 -0
  666. package/src/duckdb/src/include/duckdb/function/table/arrow/enum/arrow_datetime_type.hpp +18 -0
  667. package/src/duckdb/src/include/duckdb/function/table/arrow/enum/arrow_type_info_type.hpp +7 -0
  668. package/src/duckdb/src/include/duckdb/function/table/arrow/enum/arrow_variable_size_type.hpp +10 -0
  669. package/src/duckdb/src/include/duckdb/function/table/arrow.hpp +2 -0
  670. package/src/duckdb/src/include/duckdb/function/table/range.hpp +4 -0
  671. package/src/duckdb/src/include/duckdb/function/table/read_csv.hpp +4 -1
  672. package/src/duckdb/src/include/duckdb/function/table/system_functions.hpp +4 -0
  673. package/src/duckdb/src/include/duckdb/function/table/table_scan.hpp +5 -5
  674. package/src/duckdb/src/include/duckdb/function/table_function.hpp +14 -2
  675. package/src/duckdb/src/include/duckdb/function/table_macro_function.hpp +1 -1
  676. package/src/duckdb/src/include/duckdb/main/appender.hpp +14 -4
  677. package/src/duckdb/src/include/duckdb/main/attached_database.hpp +25 -7
  678. package/src/duckdb/src/include/duckdb/main/buffered_data/batched_buffered_data.hpp +79 -0
  679. package/src/duckdb/src/include/duckdb/main/buffered_data/buffered_data.hpp +10 -20
  680. package/src/duckdb/src/include/duckdb/main/buffered_data/simple_buffered_data.hpp +11 -12
  681. package/src/duckdb/src/include/duckdb/main/capi/capi_internal.hpp +7 -2
  682. package/src/duckdb/src/include/duckdb/main/capi/cast/generic.hpp +1 -1
  683. package/src/duckdb/src/include/duckdb/main/capi/cast/utils.hpp +2 -2
  684. package/src/duckdb/src/include/duckdb/main/capi/extension_api.hpp +809 -0
  685. package/src/duckdb/src/include/duckdb/main/chunk_scan_state/batched_data_collection.hpp +35 -0
  686. package/src/duckdb/src/include/duckdb/main/client_config.hpp +68 -2
  687. package/src/duckdb/src/include/duckdb/main/client_context.hpp +30 -22
  688. package/src/duckdb/src/include/duckdb/main/client_context_state.hpp +79 -1
  689. package/src/duckdb/src/include/duckdb/main/client_properties.hpp +9 -3
  690. package/src/duckdb/src/include/duckdb/main/config.hpp +55 -7
  691. package/src/duckdb/src/include/duckdb/main/connection.hpp +5 -1
  692. package/src/duckdb/src/include/duckdb/main/database.hpp +16 -5
  693. package/src/duckdb/src/include/duckdb/main/database_manager.hpp +9 -8
  694. package/src/duckdb/src/include/duckdb/main/db_instance_cache.hpp +21 -6
  695. package/src/duckdb/src/include/duckdb/main/extension.hpp +20 -0
  696. package/src/duckdb/src/include/duckdb/main/extension_entries.hpp +25 -0
  697. package/src/duckdb/src/include/duckdb/main/extension_helper.hpp +29 -23
  698. package/src/duckdb/src/include/duckdb/main/extension_install_info.hpp +6 -0
  699. package/src/duckdb/src/include/duckdb/main/extension_util.hpp +3 -0
  700. package/src/duckdb/src/include/duckdb/main/pending_query_result.hpp +4 -2
  701. package/src/duckdb/src/include/duckdb/main/prepared_statement.hpp +5 -6
  702. package/src/duckdb/src/include/duckdb/main/prepared_statement_data.hpp +2 -5
  703. package/src/duckdb/src/include/duckdb/main/profiling_info.hpp +87 -0
  704. package/src/duckdb/src/include/duckdb/main/profiling_node.hpp +60 -0
  705. package/src/duckdb/src/include/duckdb/main/query_profiler.hpp +72 -34
  706. package/src/duckdb/src/include/duckdb/main/query_result.hpp +1 -1
  707. package/src/duckdb/src/include/duckdb/main/relation/create_table_relation.hpp +2 -1
  708. package/src/duckdb/src/include/duckdb/main/relation/delim_get_relation.hpp +30 -0
  709. package/src/duckdb/src/include/duckdb/main/relation/explain_relation.hpp +3 -1
  710. package/src/duckdb/src/include/duckdb/main/relation/join_relation.hpp +3 -0
  711. package/src/duckdb/src/include/duckdb/main/relation/materialized_relation.hpp +1 -4
  712. package/src/duckdb/src/include/duckdb/main/relation/query_relation.hpp +4 -1
  713. package/src/duckdb/src/include/duckdb/main/relation/read_json_relation.hpp +6 -0
  714. package/src/duckdb/src/include/duckdb/main/relation/table_function_relation.hpp +1 -0
  715. package/src/duckdb/src/include/duckdb/main/relation/view_relation.hpp +2 -0
  716. package/src/duckdb/src/include/duckdb/main/relation.hpp +7 -4
  717. package/src/duckdb/src/include/duckdb/main/secret/default_secrets.hpp +36 -0
  718. package/src/duckdb/src/include/duckdb/main/secret/secret.hpp +108 -0
  719. package/src/duckdb/src/include/duckdb/main/secret/secret_manager.hpp +14 -4
  720. package/src/duckdb/src/include/duckdb/main/settings.hpp +227 -3
  721. package/src/duckdb/src/include/duckdb/main/stream_query_result.hpp +8 -0
  722. package/src/duckdb/src/include/duckdb/optimizer/build_probe_side_optimizer.hpp +51 -0
  723. package/src/duckdb/src/include/duckdb/optimizer/compressed_materialization.hpp +7 -0
  724. package/src/duckdb/src/include/duckdb/optimizer/cte_filter_pusher.hpp +46 -0
  725. package/src/duckdb/src/include/duckdb/optimizer/filter_combiner.hpp +1 -1
  726. package/src/duckdb/src/include/duckdb/optimizer/filter_pushdown.hpp +7 -0
  727. package/src/duckdb/src/include/duckdb/optimizer/join_filter_pushdown_optimizer.hpp +31 -0
  728. package/src/duckdb/src/include/duckdb/optimizer/join_order/cardinality_estimator.hpp +51 -10
  729. package/src/duckdb/src/include/duckdb/optimizer/join_order/cost_model.hpp +1 -0
  730. package/src/duckdb/src/include/duckdb/optimizer/join_order/join_order_optimizer.hpp +17 -5
  731. package/src/duckdb/src/include/duckdb/optimizer/join_order/query_graph.hpp +1 -1
  732. package/src/duckdb/src/include/duckdb/optimizer/join_order/query_graph_manager.hpp +15 -13
  733. package/src/duckdb/src/include/duckdb/optimizer/join_order/relation_manager.hpp +9 -4
  734. package/src/duckdb/src/include/duckdb/optimizer/limit_pushdown.hpp +25 -0
  735. package/src/duckdb/src/include/duckdb/optimizer/optimizer.hpp +1 -0
  736. package/src/duckdb/src/include/duckdb/optimizer/rule/join_dependent_filter.hpp +37 -0
  737. package/src/duckdb/src/include/duckdb/parallel/executor_task.hpp +6 -1
  738. package/src/duckdb/src/include/duckdb/parallel/interrupt.hpp +54 -2
  739. package/src/duckdb/src/include/duckdb/parallel/meta_pipeline.hpp +27 -8
  740. package/src/duckdb/src/include/duckdb/parallel/pipeline.hpp +1 -0
  741. package/src/duckdb/src/include/duckdb/parallel/pipeline_prepare_finish_event.hpp +25 -0
  742. package/src/duckdb/src/include/duckdb/parallel/task_executor.hpp +63 -0
  743. package/src/duckdb/src/include/duckdb/parallel/task_scheduler.hpp +10 -1
  744. package/src/duckdb/src/include/duckdb/parser/expression/function_expression.hpp +4 -1
  745. package/src/duckdb/src/include/duckdb/parser/expression/star_expression.hpp +5 -0
  746. package/src/duckdb/src/include/duckdb/parser/parsed_data/alter_table_info.hpp +5 -0
  747. package/src/duckdb/src/include/duckdb/parser/parsed_data/attach_info.hpp +5 -0
  748. package/src/duckdb/src/include/duckdb/parser/parsed_data/create_index_info.hpp +2 -0
  749. package/src/duckdb/src/include/duckdb/parser/parsed_data/create_macro_info.hpp +11 -1
  750. package/src/duckdb/src/include/duckdb/parser/parsed_data/transaction_info.hpp +9 -0
  751. package/src/duckdb/src/include/duckdb/parser/parsed_expression_iterator.hpp +13 -6
  752. package/src/duckdb/src/include/duckdb/parser/parser_extension.hpp +1 -1
  753. package/src/duckdb/src/include/duckdb/parser/sql_statement.hpp +1 -3
  754. package/src/duckdb/src/include/duckdb/parser/statement/copy_statement.hpp +2 -0
  755. package/src/duckdb/src/include/duckdb/parser/statement/explain_statement.hpp +5 -1
  756. package/src/duckdb/src/include/duckdb/parser/statement/set_statement.hpp +2 -2
  757. package/src/duckdb/src/include/duckdb/parser/statement/transaction_statement.hpp +1 -1
  758. package/src/duckdb/src/include/duckdb/parser/tableref/basetableref.hpp +0 -2
  759. package/src/duckdb/src/include/duckdb/parser/tableref/column_data_ref.hpp +9 -7
  760. package/src/duckdb/src/include/duckdb/parser/tableref/delimgetref.hpp +37 -0
  761. package/src/duckdb/src/include/duckdb/parser/tableref/joinref.hpp +4 -0
  762. package/src/duckdb/src/include/duckdb/parser/tableref/pivotref.hpp +0 -2
  763. package/src/duckdb/src/include/duckdb/parser/tableref/subqueryref.hpp +0 -2
  764. package/src/duckdb/src/include/duckdb/parser/tableref/table_function_ref.hpp +0 -1
  765. package/src/duckdb/src/include/duckdb/parser/tableref.hpp +3 -1
  766. package/src/duckdb/src/include/duckdb/parser/transformer.hpp +17 -9
  767. package/src/duckdb/src/include/duckdb/planner/binder.hpp +24 -14
  768. package/src/duckdb/src/include/duckdb/planner/collation_binding.hpp +44 -0
  769. package/src/duckdb/src/include/duckdb/planner/expression/bound_aggregate_expression.hpp +1 -1
  770. package/src/duckdb/src/include/duckdb/planner/expression/bound_between_expression.hpp +1 -1
  771. package/src/duckdb/src/include/duckdb/planner/expression/bound_case_expression.hpp +1 -1
  772. package/src/duckdb/src/include/duckdb/planner/expression/bound_cast_expression.hpp +1 -1
  773. package/src/duckdb/src/include/duckdb/planner/expression/bound_columnref_expression.hpp +1 -1
  774. package/src/duckdb/src/include/duckdb/planner/expression/bound_comparison_expression.hpp +1 -1
  775. package/src/duckdb/src/include/duckdb/planner/expression/bound_conjunction_expression.hpp +1 -1
  776. package/src/duckdb/src/include/duckdb/planner/expression/bound_constant_expression.hpp +1 -1
  777. package/src/duckdb/src/include/duckdb/planner/expression/bound_default_expression.hpp +1 -1
  778. package/src/duckdb/src/include/duckdb/planner/expression/bound_expanded_expression.hpp +1 -1
  779. package/src/duckdb/src/include/duckdb/planner/expression/bound_function_expression.hpp +1 -1
  780. package/src/duckdb/src/include/duckdb/planner/expression/bound_lambda_expression.hpp +1 -1
  781. package/src/duckdb/src/include/duckdb/planner/expression/bound_lambdaref_expression.hpp +1 -1
  782. package/src/duckdb/src/include/duckdb/planner/expression/bound_operator_expression.hpp +1 -1
  783. package/src/duckdb/src/include/duckdb/planner/expression/bound_parameter_data.hpp +2 -0
  784. package/src/duckdb/src/include/duckdb/planner/expression/bound_parameter_expression.hpp +1 -1
  785. package/src/duckdb/src/include/duckdb/planner/expression/bound_reference_expression.hpp +1 -1
  786. package/src/duckdb/src/include/duckdb/planner/expression/bound_subquery_expression.hpp +2 -2
  787. package/src/duckdb/src/include/duckdb/planner/expression/bound_unnest_expression.hpp +1 -1
  788. package/src/duckdb/src/include/duckdb/planner/expression/bound_window_expression.hpp +1 -1
  789. package/src/duckdb/src/include/duckdb/planner/expression.hpp +2 -2
  790. package/src/duckdb/src/include/duckdb/planner/expression_binder/column_alias_binder.hpp +2 -0
  791. package/src/duckdb/src/include/duckdb/planner/expression_binder/group_binder.hpp +1 -0
  792. package/src/duckdb/src/include/duckdb/planner/expression_binder/order_binder.hpp +6 -5
  793. package/src/duckdb/src/include/duckdb/planner/expression_binder/where_binder.hpp +1 -0
  794. package/src/duckdb/src/include/duckdb/planner/expression_binder.hpp +19 -11
  795. package/src/duckdb/src/include/duckdb/planner/filter/conjunction_filter.hpp +4 -0
  796. package/src/duckdb/src/include/duckdb/planner/filter/constant_filter.hpp +2 -0
  797. package/src/duckdb/src/include/duckdb/planner/filter/null_filter.hpp +4 -0
  798. package/src/duckdb/src/include/duckdb/planner/filter/struct_filter.hpp +2 -0
  799. package/src/duckdb/src/include/duckdb/planner/logical_operator.hpp +7 -2
  800. package/src/duckdb/src/include/duckdb/planner/logical_operator_visitor.hpp +2 -1
  801. package/src/duckdb/src/include/duckdb/planner/operator/logical_aggregate.hpp +1 -1
  802. package/src/duckdb/src/include/duckdb/planner/operator/logical_any_join.hpp +1 -1
  803. package/src/duckdb/src/include/duckdb/planner/operator/logical_comparison_join.hpp +6 -1
  804. package/src/duckdb/src/include/duckdb/planner/operator/logical_copy_to_file.hpp +10 -2
  805. package/src/duckdb/src/include/duckdb/planner/operator/logical_cteref.hpp +1 -0
  806. package/src/duckdb/src/include/duckdb/planner/operator/logical_delim_get.hpp +1 -1
  807. package/src/duckdb/src/include/duckdb/planner/operator/logical_distinct.hpp +1 -1
  808. package/src/duckdb/src/include/duckdb/planner/operator/logical_execute.hpp +1 -1
  809. package/src/duckdb/src/include/duckdb/planner/operator/logical_explain.hpp +4 -2
  810. package/src/duckdb/src/include/duckdb/planner/operator/logical_get.hpp +15 -5
  811. package/src/duckdb/src/include/duckdb/planner/operator/logical_materialized_cte.hpp +1 -0
  812. package/src/duckdb/src/include/duckdb/planner/operator/logical_order.hpp +1 -1
  813. package/src/duckdb/src/include/duckdb/planner/subquery/flatten_dependent_join.hpp +2 -1
  814. package/src/duckdb/src/include/duckdb/planner/table_filter.hpp +24 -2
  815. package/src/duckdb/src/include/duckdb/planner/tableref/bound_delimgetref.hpp +26 -0
  816. package/src/duckdb/src/include/duckdb/planner/tableref/bound_joinref.hpp +6 -0
  817. package/src/duckdb/src/include/duckdb/planner/tableref/bound_subqueryref.hpp +1 -1
  818. package/src/duckdb/src/include/duckdb/planner/tableref/bound_table_function.hpp +2 -0
  819. package/src/duckdb/src/include/duckdb/planner/tableref/list.hpp +2 -0
  820. package/src/duckdb/src/include/duckdb/storage/arena_allocator.hpp +2 -1
  821. package/src/duckdb/src/include/duckdb/storage/block.hpp +4 -2
  822. package/src/duckdb/src/include/duckdb/storage/block_manager.hpp +48 -3
  823. package/src/duckdb/src/include/duckdb/storage/buffer/block_handle.hpp +21 -7
  824. package/src/duckdb/src/include/duckdb/storage/buffer/buffer_pool.hpp +65 -51
  825. package/src/duckdb/src/include/duckdb/storage/buffer_manager.hpp +14 -5
  826. package/src/duckdb/src/include/duckdb/storage/checkpoint/row_group_writer.hpp +0 -4
  827. package/src/duckdb/src/include/duckdb/storage/checkpoint/string_checkpoint_state.hpp +3 -2
  828. package/src/duckdb/src/include/duckdb/storage/checkpoint/table_data_writer.hpp +1 -0
  829. package/src/duckdb/src/include/duckdb/storage/checkpoint/write_overflow_strings_to_disk.hpp +3 -4
  830. package/src/duckdb/src/include/duckdb/storage/checkpoint_manager.hpp +2 -0
  831. package/src/duckdb/src/include/duckdb/storage/compression/alp/algorithm/alp.hpp +4 -4
  832. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_analyze.hpp +6 -4
  833. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_compress.hpp +19 -17
  834. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_constants.hpp +2 -2
  835. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_scan.hpp +3 -4
  836. package/src/duckdb/src/include/duckdb/storage/compression/alp/alp_utils.hpp +3 -2
  837. package/src/duckdb/src/include/duckdb/storage/compression/alprd/algorithm/alprd.hpp +3 -2
  838. package/src/duckdb/src/include/duckdb/storage/compression/alprd/alprd_analyze.hpp +13 -11
  839. package/src/duckdb/src/include/duckdb/storage/compression/alprd/alprd_compress.hpp +19 -19
  840. package/src/duckdb/src/include/duckdb/storage/compression/alprd/alprd_scan.hpp +3 -4
  841. package/src/duckdb/src/include/duckdb/storage/compression/chimp/chimp_scan.hpp +1 -1
  842. package/src/duckdb/src/include/duckdb/storage/compression/patas/patas_scan.hpp +1 -1
  843. package/src/duckdb/src/include/duckdb/storage/data_pointer.hpp +10 -2
  844. package/src/duckdb/src/include/duckdb/storage/data_table.hpp +3 -2
  845. package/src/duckdb/src/include/duckdb/storage/in_memory_block_manager.hpp +15 -0
  846. package/src/duckdb/src/include/duckdb/storage/index_storage_info.hpp +14 -10
  847. package/src/duckdb/src/include/duckdb/storage/metadata/metadata_manager.hpp +6 -8
  848. package/src/duckdb/src/include/duckdb/storage/partial_block_manager.hpp +7 -4
  849. package/src/duckdb/src/include/duckdb/storage/segment/uncompressed.hpp +4 -7
  850. package/src/duckdb/src/include/duckdb/storage/single_file_block_manager.hpp +29 -4
  851. package/src/duckdb/src/include/duckdb/storage/standard_buffer_manager.hpp +22 -7
  852. package/src/duckdb/src/include/duckdb/storage/statistics/base_statistics.hpp +15 -2
  853. package/src/duckdb/src/include/duckdb/storage/statistics/distinct_statistics.hpp +8 -2
  854. package/src/duckdb/src/include/duckdb/storage/statistics/numeric_stats.hpp +5 -16
  855. package/src/duckdb/src/include/duckdb/storage/statistics/numeric_stats_union.hpp +51 -13
  856. package/src/duckdb/src/include/duckdb/storage/statistics/string_stats.hpp +6 -3
  857. package/src/duckdb/src/include/duckdb/storage/storage_info.hpp +29 -19
  858. package/src/duckdb/src/include/duckdb/storage/storage_manager.hpp +23 -7
  859. package/src/duckdb/src/include/duckdb/storage/string_uncompressed.hpp +27 -18
  860. package/src/duckdb/src/include/duckdb/storage/table/append_state.hpp +6 -3
  861. package/src/duckdb/src/include/duckdb/storage/table/array_column_data.hpp +5 -2
  862. package/src/duckdb/src/include/duckdb/storage/table/chunk_info.hpp +3 -0
  863. package/src/duckdb/src/include/duckdb/storage/table/column_checkpoint_state.hpp +5 -1
  864. package/src/duckdb/src/include/duckdb/storage/table/column_data.hpp +77 -6
  865. package/src/duckdb/src/include/duckdb/storage/table/column_segment.hpp +23 -11
  866. package/src/duckdb/src/include/duckdb/storage/table/data_table_info.hpp +3 -0
  867. package/src/duckdb/src/include/duckdb/storage/table/list_column_data.hpp +5 -2
  868. package/src/duckdb/src/include/duckdb/storage/table/row_group.hpp +18 -4
  869. package/src/duckdb/src/include/duckdb/storage/table/row_group_collection.hpp +7 -1
  870. package/src/duckdb/src/include/duckdb/storage/table/row_version_manager.hpp +2 -1
  871. package/src/duckdb/src/include/duckdb/storage/table/scan_state.hpp +89 -14
  872. package/src/duckdb/src/include/duckdb/storage/table/standard_column_data.hpp +4 -2
  873. package/src/duckdb/src/include/duckdb/storage/table/struct_column_data.hpp +4 -2
  874. package/src/duckdb/src/include/duckdb/storage/table/table_index_list.hpp +2 -2
  875. package/src/duckdb/src/include/duckdb/storage/table/validity_column_data.hpp +1 -1
  876. package/src/duckdb/src/include/duckdb/storage/temporary_memory_manager.hpp +33 -15
  877. package/src/duckdb/src/include/duckdb/storage/write_ahead_log.hpp +9 -9
  878. package/src/duckdb/src/include/duckdb/transaction/cleanup_state.hpp +3 -1
  879. package/src/duckdb/src/include/duckdb/transaction/commit_state.hpp +4 -16
  880. package/src/duckdb/src/include/duckdb/transaction/duck_transaction.hpp +27 -4
  881. package/src/duckdb/src/include/duckdb/transaction/duck_transaction_manager.hpp +11 -0
  882. package/src/duckdb/src/include/duckdb/transaction/local_storage.hpp +6 -2
  883. package/src/duckdb/src/include/duckdb/transaction/meta_transaction.hpp +5 -5
  884. package/src/duckdb/src/include/duckdb/transaction/transaction_context.hpp +6 -2
  885. package/src/duckdb/src/include/duckdb/transaction/undo_buffer.hpp +5 -3
  886. package/src/duckdb/src/include/duckdb/transaction/wal_write_state.hpp +48 -0
  887. package/src/duckdb/src/include/duckdb.h +1779 -739
  888. package/src/duckdb/src/include/duckdb_extension.h +921 -0
  889. package/src/duckdb/src/main/appender.cpp +53 -7
  890. package/src/duckdb/src/main/attached_database.cpp +87 -17
  891. package/src/duckdb/src/main/buffered_data/batched_buffered_data.cpp +226 -0
  892. package/src/duckdb/src/main/buffered_data/buffered_data.cpp +35 -0
  893. package/src/duckdb/src/main/buffered_data/simple_buffered_data.cpp +48 -23
  894. package/src/duckdb/src/main/capi/aggregate_function-c.cpp +327 -0
  895. package/src/duckdb/src/main/capi/appender-c.cpp +18 -0
  896. package/src/duckdb/src/main/capi/cast/utils-c.cpp +2 -2
  897. package/src/duckdb/src/main/capi/cast_function-c.cpp +210 -0
  898. package/src/duckdb/src/main/capi/config-c.cpp +3 -3
  899. package/src/duckdb/src/main/capi/data_chunk-c.cpp +18 -7
  900. package/src/duckdb/src/main/capi/duckdb_value-c.cpp +223 -24
  901. package/src/duckdb/src/main/capi/helper-c.cpp +51 -11
  902. package/src/duckdb/src/main/capi/logical_types-c.cpp +105 -46
  903. package/src/duckdb/src/main/capi/pending-c.cpp +7 -6
  904. package/src/duckdb/src/main/capi/prepared-c.cpp +18 -7
  905. package/src/duckdb/src/main/capi/profiling_info-c.cpp +84 -0
  906. package/src/duckdb/src/main/capi/result-c.cpp +139 -37
  907. package/src/duckdb/src/main/capi/scalar_function-c.cpp +269 -0
  908. package/src/duckdb/src/main/capi/table_description-c.cpp +82 -0
  909. package/src/duckdb/src/main/capi/table_function-c.cpp +161 -95
  910. package/src/duckdb/src/main/capi/value-c.cpp +2 -2
  911. package/src/duckdb/src/main/chunk_scan_state/batched_data_collection.cpp +57 -0
  912. package/src/duckdb/src/main/client_config.cpp +17 -0
  913. package/src/duckdb/src/main/client_context.cpp +67 -52
  914. package/src/duckdb/src/main/client_data.cpp +3 -3
  915. package/src/duckdb/src/main/config.cpp +120 -62
  916. package/src/duckdb/src/main/connection.cpp +14 -2
  917. package/src/duckdb/src/main/database.cpp +96 -35
  918. package/src/duckdb/src/main/database_manager.cpp +25 -23
  919. package/src/duckdb/src/main/database_path_and_type.cpp +2 -2
  920. package/src/duckdb/src/main/db_instance_cache.cpp +54 -19
  921. package/src/duckdb/src/main/extension/extension_helper.cpp +47 -42
  922. package/src/duckdb/src/main/extension/extension_install.cpp +155 -87
  923. package/src/duckdb/src/main/extension/extension_load.cpp +180 -26
  924. package/src/duckdb/src/main/extension/extension_util.cpp +8 -0
  925. package/src/duckdb/src/main/extension.cpp +72 -5
  926. package/src/duckdb/src/main/pending_query_result.cpp +20 -12
  927. package/src/duckdb/src/main/prepared_statement.cpp +6 -6
  928. package/src/duckdb/src/main/prepared_statement_data.cpp +28 -17
  929. package/src/duckdb/src/main/profiling_info.cpp +196 -0
  930. package/src/duckdb/src/main/query_profiler.cpp +413 -224
  931. package/src/duckdb/src/main/query_result.cpp +1 -1
  932. package/src/duckdb/src/main/relation/create_table_relation.cpp +4 -2
  933. package/src/duckdb/src/main/relation/create_view_relation.cpp +0 -6
  934. package/src/duckdb/src/main/relation/delim_get_relation.cpp +44 -0
  935. package/src/duckdb/src/main/relation/explain_relation.cpp +4 -3
  936. package/src/duckdb/src/main/relation/join_relation.cpp +5 -0
  937. package/src/duckdb/src/main/relation/limit_relation.cpp +1 -1
  938. package/src/duckdb/src/main/relation/materialized_relation.cpp +3 -3
  939. package/src/duckdb/src/main/relation/query_relation.cpp +42 -15
  940. package/src/duckdb/src/main/relation/read_csv_relation.cpp +7 -14
  941. package/src/duckdb/src/main/relation/read_json_relation.cpp +20 -0
  942. package/src/duckdb/src/main/relation/setop_relation.cpp +1 -1
  943. package/src/duckdb/src/main/relation/table_function_relation.cpp +6 -0
  944. package/src/duckdb/src/main/relation/view_relation.cpp +10 -0
  945. package/src/duckdb/src/main/relation.cpp +12 -8
  946. package/src/duckdb/src/main/secret/default_secrets.cpp +108 -0
  947. package/src/duckdb/src/main/secret/secret.cpp +145 -2
  948. package/src/duckdb/src/main/secret/secret_manager.cpp +85 -35
  949. package/src/duckdb/src/main/secret/secret_storage.cpp +29 -17
  950. package/src/duckdb/src/main/settings/settings.cpp +503 -11
  951. package/src/duckdb/src/main/stream_query_result.cpp +75 -2
  952. package/src/duckdb/src/optimizer/build_probe_side_optimizer.cpp +248 -0
  953. package/src/duckdb/src/optimizer/column_lifetime_analyzer.cpp +28 -6
  954. package/src/duckdb/src/optimizer/compressed_materialization/compress_comparison_join.cpp +152 -0
  955. package/src/duckdb/src/optimizer/compressed_materialization.cpp +11 -1
  956. package/src/duckdb/src/optimizer/cse_optimizer.cpp +3 -0
  957. package/src/duckdb/src/optimizer/cte_filter_pusher.cpp +117 -0
  958. package/src/duckdb/src/optimizer/filter_combiner.cpp +30 -9
  959. package/src/duckdb/src/optimizer/filter_pullup.cpp +54 -2
  960. package/src/duckdb/src/optimizer/filter_pushdown.cpp +71 -3
  961. package/src/duckdb/src/optimizer/join_filter_pushdown_optimizer.cpp +154 -0
  962. package/src/duckdb/src/optimizer/join_order/cardinality_estimator.cpp +245 -114
  963. package/src/duckdb/src/optimizer/join_order/join_order_optimizer.cpp +42 -20
  964. package/src/duckdb/src/optimizer/join_order/join_relation_set.cpp +6 -2
  965. package/src/duckdb/src/optimizer/join_order/plan_enumerator.cpp +32 -10
  966. package/src/duckdb/src/optimizer/join_order/query_graph_manager.cpp +97 -131
  967. package/src/duckdb/src/optimizer/join_order/relation_manager.cpp +265 -51
  968. package/src/duckdb/src/optimizer/join_order/relation_statistics_helper.cpp +21 -17
  969. package/src/duckdb/src/optimizer/limit_pushdown.cpp +42 -0
  970. package/src/duckdb/src/optimizer/optimizer.cpp +51 -8
  971. package/src/duckdb/src/optimizer/pushdown/pushdown_aggregate.cpp +17 -17
  972. package/src/duckdb/src/optimizer/pushdown/pushdown_cross_product.cpp +22 -4
  973. package/src/duckdb/src/optimizer/pushdown/pushdown_get.cpp +1 -18
  974. package/src/duckdb/src/optimizer/pushdown/pushdown_inner_join.cpp +6 -0
  975. package/src/duckdb/src/optimizer/pushdown/pushdown_mark_join.cpp +4 -2
  976. package/src/duckdb/src/optimizer/pushdown/pushdown_window.cpp +91 -0
  977. package/src/duckdb/src/optimizer/remove_unused_columns.cpp +21 -25
  978. package/src/duckdb/src/optimizer/rule/comparison_simplification.cpp +1 -0
  979. package/src/duckdb/src/optimizer/rule/empty_needle_removal.cpp +3 -0
  980. package/src/duckdb/src/optimizer/rule/equal_or_null_simplification.cpp +2 -2
  981. package/src/duckdb/src/optimizer/rule/in_clause_simplification_rule.cpp +8 -2
  982. package/src/duckdb/src/optimizer/rule/join_dependent_filter.cpp +135 -0
  983. package/src/duckdb/src/optimizer/rule/like_optimizations.cpp +1 -1
  984. package/src/duckdb/src/optimizer/rule/regex_optimizations.cpp +1 -1
  985. package/src/duckdb/src/optimizer/statistics/operator/propagate_filter.cpp +6 -1
  986. package/src/duckdb/src/optimizer/statistics/operator/propagate_get.cpp +7 -6
  987. package/src/duckdb/src/optimizer/statistics/operator/propagate_join.cpp +1 -1
  988. package/src/duckdb/src/optimizer/topn_optimizer.cpp +46 -7
  989. package/src/duckdb/src/parallel/executor.cpp +129 -51
  990. package/src/duckdb/src/parallel/executor_task.cpp +16 -3
  991. package/src/duckdb/src/parallel/meta_pipeline.cpp +98 -29
  992. package/src/duckdb/src/parallel/pipeline.cpp +17 -3
  993. package/src/duckdb/src/parallel/pipeline_executor.cpp +14 -2
  994. package/src/duckdb/src/parallel/pipeline_prepare_finish_event.cpp +34 -0
  995. package/src/duckdb/src/parallel/task_executor.cpp +84 -0
  996. package/src/duckdb/src/parallel/task_scheduler.cpp +94 -16
  997. package/src/duckdb/src/parallel/thread_context.cpp +1 -1
  998. package/src/duckdb/src/parser/expression/function_expression.cpp +14 -0
  999. package/src/duckdb/src/parser/expression/star_expression.cpp +35 -2
  1000. package/src/duckdb/src/parser/parsed_data/alter_table_info.cpp +5 -1
  1001. package/src/duckdb/src/parser/parsed_data/attach_info.cpp +17 -0
  1002. package/src/duckdb/src/parser/parsed_data/create_index_info.cpp +37 -28
  1003. package/src/duckdb/src/parser/parsed_data/create_macro_info.cpp +44 -2
  1004. package/src/duckdb/src/parser/parsed_data/transaction_info.cpp +21 -1
  1005. package/src/duckdb/src/parser/parsed_expression_iterator.cpp +29 -25
  1006. package/src/duckdb/src/parser/parser.cpp +41 -1
  1007. package/src/duckdb/src/parser/query_node/recursive_cte_node.cpp +1 -0
  1008. package/src/duckdb/src/parser/statement/explain_statement.cpp +28 -13
  1009. package/src/duckdb/src/parser/statement/relation_statement.cpp +5 -0
  1010. package/src/duckdb/src/parser/statement/set_statement.cpp +4 -2
  1011. package/src/duckdb/src/parser/statement/transaction_statement.cpp +3 -3
  1012. package/src/duckdb/src/parser/tableref/column_data_ref.cpp +1 -27
  1013. package/src/duckdb/src/parser/tableref/delimgetref.cpp +30 -0
  1014. package/src/duckdb/src/parser/tableref/joinref.cpp +4 -0
  1015. package/src/duckdb/src/parser/transform/constraint/transform_constraint.cpp +35 -29
  1016. package/src/duckdb/src/parser/transform/expression/transform_array_access.cpp +32 -32
  1017. package/src/duckdb/src/parser/transform/expression/transform_columnref.cpp +2 -1
  1018. package/src/duckdb/src/parser/transform/expression/transform_constant.cpp +17 -0
  1019. package/src/duckdb/src/parser/transform/expression/transform_function.cpp +5 -0
  1020. package/src/duckdb/src/parser/transform/expression/transform_multi_assign_reference.cpp +36 -34
  1021. package/src/duckdb/src/parser/transform/expression/transform_operator.cpp +30 -14
  1022. package/src/duckdb/src/parser/transform/expression/transform_subquery.cpp +1 -1
  1023. package/src/duckdb/src/parser/transform/helpers/transform_alias.cpp +2 -1
  1024. package/src/duckdb/src/parser/transform/helpers/transform_cte.cpp +27 -19
  1025. package/src/duckdb/src/parser/transform/helpers/transform_orderby.cpp +31 -28
  1026. package/src/duckdb/src/parser/transform/statement/transform_alter_table.cpp +25 -27
  1027. package/src/duckdb/src/parser/transform/statement/transform_copy.cpp +1 -1
  1028. package/src/duckdb/src/parser/transform/statement/transform_create_function.cpp +53 -42
  1029. package/src/duckdb/src/parser/transform/statement/transform_create_table.cpp +6 -6
  1030. package/src/duckdb/src/parser/transform/statement/transform_create_table_as.cpp +1 -1
  1031. package/src/duckdb/src/parser/transform/statement/transform_create_type.cpp +1 -1
  1032. package/src/duckdb/src/parser/transform/statement/transform_create_view.cpp +1 -1
  1033. package/src/duckdb/src/parser/transform/statement/transform_explain.cpp +38 -3
  1034. package/src/duckdb/src/parser/transform/statement/transform_insert.cpp +1 -2
  1035. package/src/duckdb/src/parser/transform/statement/transform_pivot_stmt.cpp +1 -1
  1036. package/src/duckdb/src/parser/transform/statement/transform_prepare.cpp +1 -1
  1037. package/src/duckdb/src/parser/transform/statement/transform_select.cpp +26 -21
  1038. package/src/duckdb/src/parser/transform/statement/transform_set.cpp +8 -8
  1039. package/src/duckdb/src/parser/transform/statement/transform_show.cpp +5 -2
  1040. package/src/duckdb/src/parser/transform/statement/transform_show_select.cpp +6 -4
  1041. package/src/duckdb/src/parser/transform/statement/transform_transaction.cpp +27 -6
  1042. package/src/duckdb/src/parser/transform/statement/transform_update.cpp +8 -9
  1043. package/src/duckdb/src/parser/transform/statement/transform_upsert.cpp +11 -12
  1044. package/src/duckdb/src/parser/transform/statement/transform_vacuum.cpp +3 -3
  1045. package/src/duckdb/src/parser/transform/tableref/transform_join.cpp +16 -10
  1046. package/src/duckdb/src/parser/transform/tableref/transform_pivot.cpp +1 -1
  1047. package/src/duckdb/src/parser/transform/tableref/transform_subquery.cpp +1 -1
  1048. package/src/duckdb/src/parser/transformer.cpp +11 -7
  1049. package/src/duckdb/src/planner/bind_context.cpp +3 -3
  1050. package/src/duckdb/src/planner/binder/expression/bind_aggregate_expression.cpp +22 -7
  1051. package/src/duckdb/src/planner/binder/expression/bind_between_expression.cpp +3 -3
  1052. package/src/duckdb/src/planner/binder/expression/bind_collate_expression.cpp +3 -2
  1053. package/src/duckdb/src/planner/binder/expression/bind_columnref_expression.cpp +11 -4
  1054. package/src/duckdb/src/planner/binder/expression/bind_comparison_expression.cpp +9 -54
  1055. package/src/duckdb/src/planner/binder/expression/bind_function_expression.cpp +3 -5
  1056. package/src/duckdb/src/planner/binder/expression/bind_macro_expression.cpp +24 -27
  1057. package/src/duckdb/src/planner/binder/expression/bind_operator_expression.cpp +7 -7
  1058. package/src/duckdb/src/planner/binder/expression/bind_parameter_expression.cpp +9 -2
  1059. package/src/duckdb/src/planner/binder/expression/bind_star_expression.cpp +26 -7
  1060. package/src/duckdb/src/planner/binder/expression/bind_unnest_expression.cpp +5 -0
  1061. package/src/duckdb/src/planner/binder/expression/bind_unpacked_star_expression.cpp +91 -0
  1062. package/src/duckdb/src/planner/binder/expression/bind_window_expression.cpp +2 -2
  1063. package/src/duckdb/src/planner/binder/query_node/bind_select_node.cpp +11 -8
  1064. package/src/duckdb/src/planner/binder/query_node/bind_setop_node.cpp +1 -1
  1065. package/src/duckdb/src/planner/binder/query_node/bind_table_macro_node.cpp +6 -10
  1066. package/src/duckdb/src/planner/binder/query_node/plan_cte_node.cpp +14 -10
  1067. package/src/duckdb/src/planner/binder/query_node/plan_setop.cpp +3 -3
  1068. package/src/duckdb/src/planner/binder/query_node/plan_subquery.cpp +46 -7
  1069. package/src/duckdb/src/planner/binder/statement/bind_call.cpp +13 -20
  1070. package/src/duckdb/src/planner/binder/statement/bind_copy.cpp +105 -13
  1071. package/src/duckdb/src/planner/binder/statement/bind_copy_database.cpp +7 -3
  1072. package/src/duckdb/src/planner/binder/statement/bind_create.cpp +75 -55
  1073. package/src/duckdb/src/planner/binder/statement/bind_create_table.cpp +1 -1
  1074. package/src/duckdb/src/planner/binder/statement/bind_delete.cpp +5 -4
  1075. package/src/duckdb/src/planner/binder/statement/bind_drop.cpp +2 -2
  1076. package/src/duckdb/src/planner/binder/statement/bind_execute.cpp +24 -8
  1077. package/src/duckdb/src/planner/binder/statement/bind_explain.cpp +2 -2
  1078. package/src/duckdb/src/planner/binder/statement/bind_export.cpp +5 -105
  1079. package/src/duckdb/src/planner/binder/statement/bind_extension.cpp +2 -2
  1080. package/src/duckdb/src/planner/binder/statement/bind_insert.cpp +109 -41
  1081. package/src/duckdb/src/planner/binder/statement/bind_set.cpp +23 -7
  1082. package/src/duckdb/src/planner/binder/statement/bind_simple.cpp +4 -1
  1083. package/src/duckdb/src/planner/binder/statement/bind_summarize.cpp +17 -3
  1084. package/src/duckdb/src/planner/binder/statement/bind_update.cpp +5 -4
  1085. package/src/duckdb/src/planner/binder/statement/bind_vacuum.cpp +8 -6
  1086. package/src/duckdb/src/planner/binder/tableref/bind_basetableref.cpp +55 -42
  1087. package/src/duckdb/src/planner/binder/tableref/bind_column_data_ref.cpp +3 -2
  1088. package/src/duckdb/src/planner/binder/tableref/bind_delimgetref.cpp +16 -0
  1089. package/src/duckdb/src/planner/binder/tableref/bind_joinref.cpp +31 -1
  1090. package/src/duckdb/src/planner/binder/tableref/bind_pivot.cpp +6 -0
  1091. package/src/duckdb/src/planner/binder/tableref/bind_showref.cpp +2 -0
  1092. package/src/duckdb/src/planner/binder/tableref/bind_table_function.cpp +106 -46
  1093. package/src/duckdb/src/planner/binder/tableref/plan_delimgetref.cpp +11 -0
  1094. package/src/duckdb/src/planner/binder/tableref/plan_joinref.cpp +15 -2
  1095. package/src/duckdb/src/planner/binder/tableref/plan_table_function.cpp +4 -0
  1096. package/src/duckdb/src/planner/binder.cpp +172 -15
  1097. package/src/duckdb/src/planner/collation_binding.cpp +99 -0
  1098. package/src/duckdb/src/planner/expression/bound_aggregate_expression.cpp +10 -4
  1099. package/src/duckdb/src/planner/expression/bound_between_expression.cpp +1 -1
  1100. package/src/duckdb/src/planner/expression/bound_case_expression.cpp +1 -1
  1101. package/src/duckdb/src/planner/expression/bound_cast_expression.cpp +14 -12
  1102. package/src/duckdb/src/planner/expression/bound_columnref_expression.cpp +1 -1
  1103. package/src/duckdb/src/planner/expression/bound_comparison_expression.cpp +1 -1
  1104. package/src/duckdb/src/planner/expression/bound_conjunction_expression.cpp +1 -1
  1105. package/src/duckdb/src/planner/expression/bound_constant_expression.cpp +1 -1
  1106. package/src/duckdb/src/planner/expression/bound_expanded_expression.cpp +1 -1
  1107. package/src/duckdb/src/planner/expression/bound_function_expression.cpp +8 -2
  1108. package/src/duckdb/src/planner/expression/bound_lambda_expression.cpp +1 -1
  1109. package/src/duckdb/src/planner/expression/bound_lambdaref_expression.cpp +1 -1
  1110. package/src/duckdb/src/planner/expression/bound_operator_expression.cpp +1 -1
  1111. package/src/duckdb/src/planner/expression/bound_parameter_expression.cpp +1 -1
  1112. package/src/duckdb/src/planner/expression/bound_reference_expression.cpp +1 -1
  1113. package/src/duckdb/src/planner/expression/bound_subquery_expression.cpp +1 -1
  1114. package/src/duckdb/src/planner/expression/bound_unnest_expression.cpp +1 -1
  1115. package/src/duckdb/src/planner/expression/bound_window_expression.cpp +6 -6
  1116. package/src/duckdb/src/planner/expression_binder/aggregate_binder.cpp +1 -1
  1117. package/src/duckdb/src/planner/expression_binder/alter_binder.cpp +2 -2
  1118. package/src/duckdb/src/planner/expression_binder/base_select_binder.cpp +1 -1
  1119. package/src/duckdb/src/planner/expression_binder/column_alias_binder.cpp +7 -0
  1120. package/src/duckdb/src/planner/expression_binder/constant_binder.cpp +3 -3
  1121. package/src/duckdb/src/planner/expression_binder/group_binder.cpp +26 -22
  1122. package/src/duckdb/src/planner/expression_binder/having_binder.cpp +7 -1
  1123. package/src/duckdb/src/planner/expression_binder/index_binder.cpp +2 -2
  1124. package/src/duckdb/src/planner/expression_binder/insert_binder.cpp +2 -2
  1125. package/src/duckdb/src/planner/expression_binder/lateral_binder.cpp +2 -2
  1126. package/src/duckdb/src/planner/expression_binder/order_binder.cpp +61 -43
  1127. package/src/duckdb/src/planner/expression_binder/qualify_binder.cpp +2 -2
  1128. package/src/duckdb/src/planner/expression_binder/relation_binder.cpp +4 -4
  1129. package/src/duckdb/src/planner/expression_binder/returning_binder.cpp +3 -2
  1130. package/src/duckdb/src/planner/expression_binder/table_function_binder.cpp +10 -3
  1131. package/src/duckdb/src/planner/expression_binder/update_binder.cpp +1 -1
  1132. package/src/duckdb/src/planner/expression_binder/where_binder.cpp +9 -2
  1133. package/src/duckdb/src/planner/expression_binder.cpp +121 -21
  1134. package/src/duckdb/src/planner/expression_iterator.cpp +26 -1
  1135. package/src/duckdb/src/planner/filter/conjunction_filter.cpp +33 -0
  1136. package/src/duckdb/src/planner/filter/constant_filter.cpp +15 -0
  1137. package/src/duckdb/src/planner/filter/null_filter.cpp +22 -0
  1138. package/src/duckdb/src/planner/filter/struct_filter.cpp +16 -0
  1139. package/src/duckdb/src/planner/logical_operator.cpp +24 -7
  1140. package/src/duckdb/src/planner/operator/logical_aggregate.cpp +13 -7
  1141. package/src/duckdb/src/planner/operator/logical_any_join.cpp +5 -2
  1142. package/src/duckdb/src/planner/operator/logical_comparison_join.cpp +13 -5
  1143. package/src/duckdb/src/planner/operator/logical_copy_to_file.cpp +64 -8
  1144. package/src/duckdb/src/planner/operator/logical_cteref.cpp +7 -0
  1145. package/src/duckdb/src/planner/operator/logical_distinct.cpp +6 -5
  1146. package/src/duckdb/src/planner/operator/logical_get.cpp +60 -18
  1147. package/src/duckdb/src/planner/operator/logical_materialized_cte.cpp +7 -0
  1148. package/src/duckdb/src/planner/operator/logical_order.cpp +7 -4
  1149. package/src/duckdb/src/planner/operator/logical_top_n.cpp +2 -2
  1150. package/src/duckdb/src/planner/operator/logical_vacuum.cpp +1 -1
  1151. package/src/duckdb/src/planner/planner.cpp +2 -3
  1152. package/src/duckdb/src/planner/subquery/flatten_dependent_join.cpp +27 -10
  1153. package/src/duckdb/src/planner/table_filter.cpp +51 -0
  1154. package/src/duckdb/src/storage/arena_allocator.cpp +28 -10
  1155. package/src/duckdb/src/storage/block.cpp +3 -2
  1156. package/src/duckdb/src/storage/buffer/block_handle.cpp +29 -14
  1157. package/src/duckdb/src/storage/buffer/block_manager.cpp +6 -5
  1158. package/src/duckdb/src/storage/buffer/buffer_handle.cpp +1 -1
  1159. package/src/duckdb/src/storage/buffer/buffer_pool.cpp +264 -125
  1160. package/src/duckdb/src/storage/buffer_manager.cpp +5 -1
  1161. package/src/duckdb/src/storage/checkpoint/row_group_writer.cpp +0 -6
  1162. package/src/duckdb/src/storage/checkpoint/table_data_writer.cpp +26 -3
  1163. package/src/duckdb/src/storage/checkpoint/write_overflow_strings_to_disk.cpp +21 -9
  1164. package/src/duckdb/src/storage/checkpoint_manager.cpp +49 -24
  1165. package/src/duckdb/src/storage/compression/alp/alp.cpp +6 -11
  1166. package/src/duckdb/src/storage/compression/alprd.cpp +5 -9
  1167. package/src/duckdb/src/storage/compression/bitpacking.cpp +35 -31
  1168. package/src/duckdb/src/storage/compression/chimp/chimp.cpp +6 -8
  1169. package/src/duckdb/src/storage/compression/dictionary_compression.cpp +71 -58
  1170. package/src/duckdb/src/storage/compression/fixed_size_uncompressed.cpp +15 -13
  1171. package/src/duckdb/src/storage/compression/fsst.cpp +66 -53
  1172. package/src/duckdb/src/storage/compression/numeric_constant.cpp +4 -5
  1173. package/src/duckdb/src/storage/compression/patas.cpp +6 -17
  1174. package/src/duckdb/src/storage/compression/rle.cpp +20 -18
  1175. package/src/duckdb/src/storage/compression/string_uncompressed.cpp +71 -52
  1176. package/src/duckdb/src/storage/compression/uncompressed.cpp +2 -2
  1177. package/src/duckdb/src/storage/compression/validity_uncompressed.cpp +8 -7
  1178. package/src/duckdb/src/storage/data_pointer.cpp +22 -0
  1179. package/src/duckdb/src/storage/data_table.cpp +41 -12
  1180. package/src/duckdb/src/storage/local_storage.cpp +22 -8
  1181. package/src/duckdb/src/storage/metadata/metadata_manager.cpp +33 -17
  1182. package/src/duckdb/src/storage/metadata/metadata_reader.cpp +4 -4
  1183. package/src/duckdb/src/storage/metadata/metadata_writer.cpp +3 -3
  1184. package/src/duckdb/src/storage/partial_block_manager.cpp +19 -8
  1185. package/src/duckdb/src/storage/serialization/serialize_create_info.cpp +11 -8
  1186. package/src/duckdb/src/storage/serialization/serialize_expression.cpp +1 -1
  1187. package/src/duckdb/src/storage/serialization/serialize_extension_install_info.cpp +2 -0
  1188. package/src/duckdb/src/storage/serialization/serialize_logical_operator.cpp +3 -3
  1189. package/src/duckdb/src/storage/serialization/serialize_nodes.cpp +19 -5
  1190. package/src/duckdb/src/storage/serialization/serialize_parse_info.cpp +21 -1
  1191. package/src/duckdb/src/storage/serialization/serialize_parsed_expression.cpp +4 -2
  1192. package/src/duckdb/src/storage/serialization/serialize_query_node.cpp +2 -2
  1193. package/src/duckdb/src/storage/serialization/serialize_storage.cpp +2 -0
  1194. package/src/duckdb/src/storage/serialization/serialize_tableref.cpp +8 -4
  1195. package/src/duckdb/src/storage/serialization/serialize_types.cpp +4 -4
  1196. package/src/duckdb/src/storage/single_file_block_manager.cpp +170 -34
  1197. package/src/duckdb/src/storage/standard_buffer_manager.cpp +221 -64
  1198. package/src/duckdb/src/storage/statistics/column_statistics.cpp +4 -3
  1199. package/src/duckdb/src/storage/statistics/distinct_statistics.cpp +36 -26
  1200. package/src/duckdb/src/storage/statistics/numeric_stats.cpp +4 -15
  1201. package/src/duckdb/src/storage/statistics/string_stats.cpp +14 -8
  1202. package/src/duckdb/src/storage/statistics/struct_stats.cpp +2 -1
  1203. package/src/duckdb/src/storage/storage_info.cpp +34 -9
  1204. package/src/duckdb/src/storage/storage_manager.cpp +147 -74
  1205. package/src/duckdb/src/storage/table/array_column_data.cpp +37 -17
  1206. package/src/duckdb/src/storage/table/chunk_info.cpp +38 -0
  1207. package/src/duckdb/src/storage/table/column_checkpoint_state.cpp +10 -6
  1208. package/src/duckdb/src/storage/table/column_data.cpp +252 -31
  1209. package/src/duckdb/src/storage/table/column_data_checkpointer.cpp +2 -12
  1210. package/src/duckdb/src/storage/table/column_segment.cpp +63 -34
  1211. package/src/duckdb/src/storage/table/list_column_data.cpp +34 -15
  1212. package/src/duckdb/src/storage/table/row_group.cpp +228 -120
  1213. package/src/duckdb/src/storage/table/row_group_collection.cpp +122 -120
  1214. package/src/duckdb/src/storage/table/row_version_manager.cpp +27 -1
  1215. package/src/duckdb/src/storage/table/scan_state.cpp +101 -18
  1216. package/src/duckdb/src/storage/table/standard_column_data.cpp +20 -34
  1217. package/src/duckdb/src/storage/table/struct_column_data.cpp +39 -42
  1218. package/src/duckdb/src/storage/table/table_statistics.cpp +2 -1
  1219. package/src/duckdb/src/storage/table/update_segment.cpp +9 -8
  1220. package/src/duckdb/src/storage/table/validity_column_data.cpp +2 -2
  1221. package/src/duckdb/src/storage/table_index_list.cpp +8 -7
  1222. package/src/duckdb/src/storage/temporary_file_manager.cpp +11 -9
  1223. package/src/duckdb/src/storage/temporary_memory_manager.cpp +227 -39
  1224. package/src/duckdb/src/storage/wal_replay.cpp +68 -28
  1225. package/src/duckdb/src/storage/write_ahead_log.cpp +56 -47
  1226. package/src/duckdb/src/transaction/cleanup_state.cpp +9 -1
  1227. package/src/duckdb/src/transaction/commit_state.cpp +7 -170
  1228. package/src/duckdb/src/transaction/duck_transaction.cpp +87 -19
  1229. package/src/duckdb/src/transaction/duck_transaction_manager.cpp +65 -10
  1230. package/src/duckdb/src/transaction/meta_transaction.cpp +18 -3
  1231. package/src/duckdb/src/transaction/transaction_context.cpp +21 -17
  1232. package/src/duckdb/src/transaction/undo_buffer.cpp +20 -14
  1233. package/src/duckdb/src/transaction/wal_write_state.cpp +292 -0
  1234. package/src/duckdb/src/verification/prepared_statement_verifier.cpp +0 -1
  1235. package/src/duckdb/third_party/brotli/common/brotli_constants.h +204 -0
  1236. package/src/duckdb/third_party/brotli/common/brotli_platform.h +543 -0
  1237. package/src/duckdb/third_party/brotli/common/constants.cpp +17 -0
  1238. package/src/duckdb/third_party/brotli/common/context.cpp +156 -0
  1239. package/src/duckdb/third_party/brotli/common/context.h +110 -0
  1240. package/src/duckdb/third_party/brotli/common/dictionary.cpp +5912 -0
  1241. package/src/duckdb/third_party/brotli/common/dictionary.h +60 -0
  1242. package/src/duckdb/third_party/brotli/common/platform.cpp +24 -0
  1243. package/src/duckdb/third_party/brotli/common/shared_dictionary.cpp +517 -0
  1244. package/src/duckdb/third_party/brotli/common/shared_dictionary_internal.h +71 -0
  1245. package/src/duckdb/third_party/brotli/common/transform.cpp +287 -0
  1246. package/src/duckdb/third_party/brotli/common/transform.h +77 -0
  1247. package/src/duckdb/third_party/brotli/common/version.h +51 -0
  1248. package/src/duckdb/third_party/brotli/dec/bit_reader.cpp +74 -0
  1249. package/src/duckdb/third_party/brotli/dec/bit_reader.h +419 -0
  1250. package/src/duckdb/third_party/brotli/dec/decode.cpp +2758 -0
  1251. package/src/duckdb/third_party/brotli/dec/huffman.cpp +338 -0
  1252. package/src/duckdb/third_party/brotli/dec/huffman.h +118 -0
  1253. package/src/duckdb/third_party/brotli/dec/prefix.h +733 -0
  1254. package/src/duckdb/third_party/brotli/dec/state.cpp +178 -0
  1255. package/src/duckdb/third_party/brotli/dec/state.h +386 -0
  1256. package/src/duckdb/third_party/brotli/enc/backward_references.cpp +3775 -0
  1257. package/src/duckdb/third_party/brotli/enc/backward_references.h +36 -0
  1258. package/src/duckdb/third_party/brotli/enc/backward_references_hq.cpp +935 -0
  1259. package/src/duckdb/third_party/brotli/enc/backward_references_hq.h +92 -0
  1260. package/src/duckdb/third_party/brotli/enc/bit_cost.cpp +410 -0
  1261. package/src/duckdb/third_party/brotli/enc/bit_cost.h +60 -0
  1262. package/src/duckdb/third_party/brotli/enc/block_splitter.cpp +1653 -0
  1263. package/src/duckdb/third_party/brotli/enc/block_splitter.h +48 -0
  1264. package/src/duckdb/third_party/brotli/enc/brotli_bit_stream.cpp +1431 -0
  1265. package/src/duckdb/third_party/brotli/enc/brotli_bit_stream.h +85 -0
  1266. package/src/duckdb/third_party/brotli/enc/brotli_hash.h +4352 -0
  1267. package/src/duckdb/third_party/brotli/enc/brotli_params.h +47 -0
  1268. package/src/duckdb/third_party/brotli/enc/cluster.cpp +1025 -0
  1269. package/src/duckdb/third_party/brotli/enc/cluster.h +1017 -0
  1270. package/src/duckdb/third_party/brotli/enc/command.cpp +24 -0
  1271. package/src/duckdb/third_party/brotli/enc/command.h +187 -0
  1272. package/src/duckdb/third_party/brotli/enc/compound_dictionary.cpp +209 -0
  1273. package/src/duckdb/third_party/brotli/enc/compound_dictionary.h +75 -0
  1274. package/src/duckdb/third_party/brotli/enc/compress_fragment.cpp +796 -0
  1275. package/src/duckdb/third_party/brotli/enc/compress_fragment.h +82 -0
  1276. package/src/duckdb/third_party/brotli/enc/compress_fragment_two_pass.cpp +653 -0
  1277. package/src/duckdb/third_party/brotli/enc/compress_fragment_two_pass.h +68 -0
  1278. package/src/duckdb/third_party/brotli/enc/dictionary_hash.cpp +1844 -0
  1279. package/src/duckdb/third_party/brotli/enc/dictionary_hash.h +21 -0
  1280. package/src/duckdb/third_party/brotli/enc/encode.cpp +1990 -0
  1281. package/src/duckdb/third_party/brotli/enc/encoder_dict.cpp +636 -0
  1282. package/src/duckdb/third_party/brotli/enc/encoder_dict.h +153 -0
  1283. package/src/duckdb/third_party/brotli/enc/entropy_encode.cpp +500 -0
  1284. package/src/duckdb/third_party/brotli/enc/entropy_encode.h +119 -0
  1285. package/src/duckdb/third_party/brotli/enc/entropy_encode_static.h +538 -0
  1286. package/src/duckdb/third_party/brotli/enc/fast_log.cpp +101 -0
  1287. package/src/duckdb/third_party/brotli/enc/fast_log.h +63 -0
  1288. package/src/duckdb/third_party/brotli/enc/find_match_length.h +68 -0
  1289. package/src/duckdb/third_party/brotli/enc/histogram.cpp +96 -0
  1290. package/src/duckdb/third_party/brotli/enc/histogram.h +210 -0
  1291. package/src/duckdb/third_party/brotli/enc/literal_cost.cpp +176 -0
  1292. package/src/duckdb/third_party/brotli/enc/literal_cost.h +28 -0
  1293. package/src/duckdb/third_party/brotli/enc/memory.cpp +190 -0
  1294. package/src/duckdb/third_party/brotli/enc/memory.h +127 -0
  1295. package/src/duckdb/third_party/brotli/enc/metablock.cpp +1225 -0
  1296. package/src/duckdb/third_party/brotli/enc/metablock.h +102 -0
  1297. package/src/duckdb/third_party/brotli/enc/prefix.h +50 -0
  1298. package/src/duckdb/third_party/brotli/enc/quality.h +202 -0
  1299. package/src/duckdb/third_party/brotli/enc/ringbuffer.h +164 -0
  1300. package/src/duckdb/third_party/brotli/enc/state.h +106 -0
  1301. package/src/duckdb/third_party/brotli/enc/static_dict.cpp +538 -0
  1302. package/src/duckdb/third_party/brotli/enc/static_dict.h +37 -0
  1303. package/src/duckdb/third_party/brotli/enc/static_dict_lut.h +5862 -0
  1304. package/src/duckdb/third_party/brotli/enc/utf8_util.cpp +81 -0
  1305. package/src/duckdb/third_party/brotli/enc/utf8_util.h +29 -0
  1306. package/src/duckdb/third_party/brotli/enc/write_bits.h +84 -0
  1307. package/src/duckdb/third_party/brotli/include/brotli/decode.h +405 -0
  1308. package/src/duckdb/third_party/brotli/include/brotli/encode.h +489 -0
  1309. package/src/duckdb/third_party/brotli/include/brotli/port.h +238 -0
  1310. package/src/duckdb/third_party/brotli/include/brotli/shared_dictionary.h +96 -0
  1311. package/src/duckdb/third_party/brotli/include/brotli/types.h +83 -0
  1312. package/src/duckdb/third_party/fast_float/fast_float/fast_float.h +20 -4
  1313. package/src/duckdb/third_party/fmt/include/fmt/format.h +54 -10
  1314. package/src/duckdb/third_party/fsst/fsst.h +2 -2
  1315. package/src/duckdb/third_party/fsst/libfsst.hpp +2 -2
  1316. package/src/duckdb/third_party/httplib/httplib.hpp +6763 -5580
  1317. package/src/duckdb/third_party/hyperloglog/hyperloglog.cpp +13 -30
  1318. package/src/duckdb/third_party/hyperloglog/hyperloglog.hpp +8 -2
  1319. package/src/duckdb/third_party/libpg_query/include/nodes/nodes.hpp +1 -0
  1320. package/src/duckdb/third_party/libpg_query/include/nodes/parsenodes.hpp +22 -9
  1321. package/src/duckdb/third_party/libpg_query/include/parser/gram.hpp +1041 -554
  1322. package/src/duckdb/third_party/libpg_query/include/parser/kwlist.hpp +1 -0
  1323. package/src/duckdb/third_party/libpg_query/postgres_parser.cpp +2 -1
  1324. package/src/duckdb/third_party/libpg_query/src_backend_parser_gram.cpp +21605 -21752
  1325. package/src/duckdb/third_party/libpg_query/src_backend_parser_scan.cpp +538 -299
  1326. package/src/duckdb/third_party/mbedtls/include/mbedtls/mbedtls_config.h +1 -0
  1327. package/src/duckdb/third_party/mbedtls/include/mbedtls_wrapper.hpp +36 -12
  1328. package/src/duckdb/third_party/mbedtls/library/md.cpp +6 -6
  1329. package/src/duckdb/third_party/mbedtls/library/sha1.cpp +2 -0
  1330. package/src/duckdb/third_party/mbedtls/library/sha256.cpp +3 -0
  1331. package/src/duckdb/third_party/mbedtls/mbedtls_wrapper.cpp +99 -47
  1332. package/src/duckdb/third_party/pcg/pcg_extras.hpp +1 -1
  1333. package/src/duckdb/third_party/re2/re2/prog.cc +2 -2
  1334. package/src/duckdb/third_party/snappy/snappy-internal.h +398 -0
  1335. package/src/duckdb/third_party/snappy/snappy-sinksource.cc +111 -9
  1336. package/src/duckdb/third_party/snappy/snappy-sinksource.h +158 -0
  1337. package/src/duckdb/third_party/snappy/snappy-stubs-internal.h +523 -3
  1338. package/src/duckdb/third_party/snappy/snappy-stubs-public.h +34 -1
  1339. package/src/duckdb/third_party/snappy/snappy.cc +2626 -0
  1340. package/src/duckdb/third_party/snappy/snappy.h +223 -0
  1341. package/src/duckdb/third_party/snappy/snappy_version.hpp +11 -0
  1342. package/src/duckdb/third_party/utf8proc/include/utf8proc.hpp +69 -101
  1343. package/src/duckdb/third_party/utf8proc/include/utf8proc_wrapper.hpp +53 -0
  1344. package/src/duckdb/third_party/utf8proc/utf8proc.cpp +627 -678
  1345. package/src/duckdb/third_party/utf8proc/utf8proc_data.cpp +15008 -12868
  1346. package/src/duckdb/third_party/utf8proc/utf8proc_wrapper.cpp +185 -29
  1347. package/src/duckdb/ub_extension_json_json_functions.cpp +6 -0
  1348. package/src/duckdb/ub_src_catalog_default.cpp +4 -0
  1349. package/src/duckdb/ub_src_common.cpp +7 -1
  1350. package/src/duckdb/ub_src_common_arrow.cpp +10 -0
  1351. package/src/duckdb/ub_src_common_enums.cpp +2 -0
  1352. package/src/duckdb/ub_src_common_tree_renderer.cpp +10 -0
  1353. package/src/duckdb/ub_src_common_types.cpp +2 -0
  1354. package/src/duckdb/ub_src_core_functions_aggregate_holistic.cpp +4 -0
  1355. package/src/duckdb/ub_src_core_functions_aggregate_nested.cpp +2 -0
  1356. package/src/duckdb/ub_src_core_functions_scalar_generic.cpp +2 -0
  1357. package/src/duckdb/ub_src_core_functions_scalar_list.cpp +2 -4
  1358. package/src/duckdb/ub_src_core_functions_scalar_map.cpp +2 -0
  1359. package/src/duckdb/ub_src_core_functions_scalar_string.cpp +4 -0
  1360. package/src/duckdb/ub_src_execution_index_art.cpp +5 -3
  1361. package/src/duckdb/ub_src_execution_operator_csv_scanner_scanner.cpp +2 -0
  1362. package/src/duckdb/ub_src_execution_operator_helper.cpp +4 -0
  1363. package/src/duckdb/ub_src_function.cpp +4 -0
  1364. package/src/duckdb/ub_src_function_cast.cpp +2 -0
  1365. package/src/duckdb/ub_src_function_scalar_generic.cpp +4 -0
  1366. package/src/duckdb/ub_src_function_scalar_list.cpp +0 -2
  1367. package/src/duckdb/ub_src_function_scalar_string.cpp +2 -0
  1368. package/src/duckdb/ub_src_function_table.cpp +2 -0
  1369. package/src/duckdb/ub_src_function_table_arrow.cpp +2 -0
  1370. package/src/duckdb/ub_src_function_table_system.cpp +2 -0
  1371. package/src/duckdb/ub_src_main.cpp +4 -0
  1372. package/src/duckdb/ub_src_main_buffered_data.cpp +4 -0
  1373. package/src/duckdb/ub_src_main_capi.cpp +10 -0
  1374. package/src/duckdb/ub_src_main_chunk_scan_state.cpp +2 -0
  1375. package/src/duckdb/ub_src_main_relation.cpp +2 -0
  1376. package/src/duckdb/ub_src_main_secret.cpp +2 -0
  1377. package/src/duckdb/ub_src_optimizer.cpp +8 -0
  1378. package/src/duckdb/ub_src_optimizer_compressed_materialization.cpp +2 -0
  1379. package/src/duckdb/ub_src_optimizer_pushdown.cpp +2 -0
  1380. package/src/duckdb/ub_src_optimizer_rule.cpp +2 -0
  1381. package/src/duckdb/ub_src_parallel.cpp +4 -0
  1382. package/src/duckdb/ub_src_parser_tableref.cpp +2 -0
  1383. package/src/duckdb/ub_src_planner.cpp +2 -0
  1384. package/src/duckdb/ub_src_planner_binder_expression.cpp +2 -0
  1385. package/src/duckdb/ub_src_planner_binder_tableref.cpp +4 -0
  1386. package/src/duckdb/ub_src_storage_statistics.cpp +0 -2
  1387. package/src/duckdb/ub_src_transaction.cpp +2 -0
  1388. package/test/columns.test.ts +1 -1
  1389. package/test/prepare.test.ts +1 -1
  1390. package/test/test_all_types.test.ts +1 -1
@@ -0,0 +1,4352 @@
1
+ /* Copyright 2010 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
+ /* A (forgetful) hash table to the data seen by the compressor, to
8
+ help create backward references to previous data. */
9
+
10
+ #ifndef BROTLI_ENC_HASH_H_
11
+ #define BROTLI_ENC_HASH_H_
12
+
13
+ #include <stdlib.h> /* exit */
14
+ #include <string.h> /* memcmp, memset */
15
+
16
+ #include <brotli/types.h>
17
+
18
+ #include "../common/brotli_constants.h"
19
+ #include "../common/dictionary.h"
20
+ #include "../common/brotli_platform.h"
21
+ #include "compound_dictionary.h"
22
+ #include "encoder_dict.h"
23
+ #include "fast_log.h"
24
+ #include "find_match_length.h"
25
+ #include "memory.h"
26
+ #include "quality.h"
27
+ #include "static_dict.h"
28
+
29
+ namespace duckdb_brotli {
30
+
31
+ typedef struct {
32
+ /**
33
+ * Dynamically allocated areas; regular hasher uses one or two allocations;
34
+ * "composite" hasher uses up to 4 allocations.
35
+ */
36
+ void* extra[4];
37
+
38
+ /**
39
+ * False before the fisrt invocation of HasherSetup (where "extra" memory)
40
+ * is allocated.
41
+ */
42
+ BROTLI_BOOL is_setup_;
43
+
44
+ size_t dict_num_lookups;
45
+ size_t dict_num_matches;
46
+
47
+ BrotliHasherParams params;
48
+
49
+ /**
50
+ * False if hasher needs to be "prepared" before use (before the first
51
+ * invocation of HasherSetup or after HasherReset). "preparation" is hasher
52
+ * data initialization (using input ringbuffer).
53
+ */
54
+ BROTLI_BOOL is_prepared_;
55
+ } HasherCommon;
56
+
57
+ #define score_t size_t
58
+
59
+ static const uint32_t kCutoffTransformsCount = 10;
60
+ /* 0, 12, 27, 23, 42, 63, 56, 48, 59, 64 */
61
+ /* 0+0, 4+8, 8+19, 12+11, 16+26, 20+43, 24+32, 28+20, 32+27, 36+28 */
62
+ static const uint64_t kCutoffTransforms =
63
+ BROTLI_MAKE_UINT64_T(0x071B520A, 0xDA2D3200);
64
+
65
+ typedef struct HasherSearchResult {
66
+ size_t len;
67
+ size_t distance;
68
+ score_t score;
69
+ int len_code_delta; /* == len_code - len */
70
+ } HasherSearchResult;
71
+
72
+ /* kHashMul32 multiplier has these properties:
73
+ * The multiplier must be odd. Otherwise we may lose the highest bit.
74
+ * No long streaks of ones or zeros.
75
+ * There is no effort to ensure that it is a prime, the oddity is enough
76
+ for this use.
77
+ * The number has been tuned heuristically against compression benchmarks. */
78
+ static const uint32_t kHashMul32 = 0x1E35A7BD;
79
+ static const uint64_t kHashMul64 =
80
+ BROTLI_MAKE_UINT64_T(0x1FE35A7Bu, 0xD3579BD3u);
81
+
82
+ static BROTLI_INLINE uint32_t Hash14(const uint8_t* data) {
83
+ uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
84
+ /* The higher bits contain more mixture from the multiplication,
85
+ so we take our results from there. */
86
+ return h >> (32 - 14);
87
+ }
88
+
89
+ static BROTLI_INLINE void PrepareDistanceCache(
90
+ int* BROTLI_RESTRICT distance_cache, const int num_distances) {
91
+ if (num_distances > 4) {
92
+ int last_distance = distance_cache[0];
93
+ distance_cache[4] = last_distance - 1;
94
+ distance_cache[5] = last_distance + 1;
95
+ distance_cache[6] = last_distance - 2;
96
+ distance_cache[7] = last_distance + 2;
97
+ distance_cache[8] = last_distance - 3;
98
+ distance_cache[9] = last_distance + 3;
99
+ if (num_distances > 10) {
100
+ int next_last_distance = distance_cache[1];
101
+ distance_cache[10] = next_last_distance - 1;
102
+ distance_cache[11] = next_last_distance + 1;
103
+ distance_cache[12] = next_last_distance - 2;
104
+ distance_cache[13] = next_last_distance + 2;
105
+ distance_cache[14] = next_last_distance - 3;
106
+ distance_cache[15] = next_last_distance + 3;
107
+ }
108
+ }
109
+ }
110
+
111
+ #define BROTLI_LITERAL_BYTE_SCORE 135
112
+ #define BROTLI_DISTANCE_BIT_PENALTY 30
113
+ /* Score must be positive after applying maximal penalty. */
114
+ #define BROTLI_SCORE_BASE (BROTLI_DISTANCE_BIT_PENALTY * 8 * sizeof(size_t))
115
+
116
+ /* Usually, we always choose the longest backward reference. This function
117
+ allows for the exception of that rule.
118
+
119
+ If we choose a backward reference that is further away, it will
120
+ usually be coded with more bits. We approximate this by assuming
121
+ log2(distance). If the distance can be expressed in terms of the
122
+ last four distances, we use some heuristic constants to estimate
123
+ the bits cost. For the first up to four literals we use the bit
124
+ cost of the literals from the literal cost model, after that we
125
+ use the average bit cost of the cost model.
126
+
127
+ This function is used to sometimes discard a longer backward reference
128
+ when it is not much longer and the bit cost for encoding it is more
129
+ than the saved literals.
130
+
131
+ backward_reference_offset MUST be positive. */
132
+ static BROTLI_INLINE score_t BackwardReferenceScore(
133
+ size_t copy_length, size_t backward_reference_offset) {
134
+ return BROTLI_SCORE_BASE + BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length -
135
+ BROTLI_DISTANCE_BIT_PENALTY * Log2FloorNonZero(backward_reference_offset);
136
+ }
137
+
138
+ static BROTLI_INLINE score_t BackwardReferenceScoreUsingLastDistance(
139
+ size_t copy_length) {
140
+ return BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length +
141
+ BROTLI_SCORE_BASE + 15;
142
+ }
143
+
144
+ static BROTLI_INLINE score_t BackwardReferencePenaltyUsingLastDistance(
145
+ size_t distance_short_code) {
146
+ return (score_t)39 + ((0x1CA10 >> (distance_short_code & 0xE)) & 0xE);
147
+ }
148
+
149
+ static BROTLI_INLINE BROTLI_BOOL TestStaticDictionaryItem(
150
+ const BrotliEncoderDictionary* dictionary, size_t len, size_t word_idx,
151
+ const uint8_t* data, size_t max_length, size_t max_backward,
152
+ size_t max_distance, HasherSearchResult* out) {
153
+ size_t offset;
154
+ size_t matchlen;
155
+ size_t backward;
156
+ score_t score;
157
+ offset = dictionary->words->offsets_by_length[len] + len * word_idx;
158
+ if (len > max_length) {
159
+ return BROTLI_FALSE;
160
+ }
161
+
162
+ matchlen =
163
+ FindMatchLengthWithLimit(data, &dictionary->words->data[offset], len);
164
+ if (matchlen + dictionary->cutoffTransformsCount <= len || matchlen == 0) {
165
+ return BROTLI_FALSE;
166
+ }
167
+ {
168
+ size_t cut = len - matchlen;
169
+ size_t transform_id = (cut << 2) +
170
+ (size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F);
171
+ backward = max_backward + 1 + word_idx +
172
+ (transform_id << dictionary->words->size_bits_by_length[len]);
173
+ }
174
+ if (backward > max_distance) {
175
+ return BROTLI_FALSE;
176
+ }
177
+ score = BackwardReferenceScore(matchlen, backward);
178
+ if (score < out->score) {
179
+ return BROTLI_FALSE;
180
+ }
181
+ out->len = matchlen;
182
+ out->len_code_delta = (int)len - (int)matchlen;
183
+ out->distance = backward;
184
+ out->score = score;
185
+ return BROTLI_TRUE;
186
+ }
187
+
188
+ static BROTLI_INLINE void SearchInStaticDictionary(
189
+ const BrotliEncoderDictionary* dictionary,
190
+ HasherCommon* common, const uint8_t* data, size_t max_length,
191
+ size_t max_backward, size_t max_distance,
192
+ HasherSearchResult* out, BROTLI_BOOL shallow) {
193
+ size_t key;
194
+ size_t i;
195
+ if (common->dict_num_matches < (common->dict_num_lookups >> 7)) {
196
+ return;
197
+ }
198
+ key = Hash14(data) << 1;
199
+ for (i = 0; i < (shallow ? 1u : 2u); ++i, ++key) {
200
+ common->dict_num_lookups++;
201
+ if (dictionary->hash_table_lengths[key] != 0) {
202
+ BROTLI_BOOL item_matches = TestStaticDictionaryItem(
203
+ dictionary, dictionary->hash_table_lengths[key],
204
+ dictionary->hash_table_words[key], data,
205
+ max_length, max_backward, max_distance, out);
206
+ if (item_matches) {
207
+ common->dict_num_matches++;
208
+ }
209
+ }
210
+ }
211
+ }
212
+
213
+ typedef struct BackwardMatch {
214
+ uint32_t distance;
215
+ uint32_t length_and_code;
216
+ } BackwardMatch;
217
+
218
+ static BROTLI_INLINE void InitBackwardMatch(BackwardMatch* self,
219
+ size_t dist, size_t len) {
220
+ self->distance = (uint32_t)dist;
221
+ self->length_and_code = (uint32_t)(len << 5);
222
+ }
223
+
224
+ static BROTLI_INLINE void InitDictionaryBackwardMatch(BackwardMatch* self,
225
+ size_t dist, size_t len, size_t len_code) {
226
+ self->distance = (uint32_t)dist;
227
+ self->length_and_code =
228
+ (uint32_t)((len << 5) | (len == len_code ? 0 : len_code));
229
+ }
230
+
231
+ static BROTLI_INLINE size_t BackwardMatchLength(const BackwardMatch* self) {
232
+ return self->length_and_code >> 5;
233
+ }
234
+
235
+ static BROTLI_INLINE size_t BackwardMatchLengthCode(const BackwardMatch* self) {
236
+ size_t code = self->length_and_code & 31;
237
+ return code ? code : BackwardMatchLength(self);
238
+ }
239
+
240
+ #define EXPAND_CAT(a, b) CAT(a, b)
241
+ #define CAT(a, b) a ## b
242
+ #define FN(X) EXPAND_CAT(X, HASHER())
243
+
244
+ #define HASHER() H10
245
+ #define BUCKET_BITS 17
246
+ #define MAX_TREE_SEARCH_DEPTH 64
247
+ #define MAX_TREE_COMP_LENGTH 128
248
+ /* NOLINT(build/header_guard) */
249
+ /* Copyright 2016 Google Inc. All Rights Reserved.
250
+
251
+ Distributed under MIT license.
252
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
253
+ */
254
+
255
+ /* template parameters: FN, BUCKET_BITS, MAX_TREE_COMP_LENGTH,
256
+ MAX_TREE_SEARCH_DEPTH */
257
+
258
+ /* A (forgetful) hash table where each hash bucket contains a binary tree of
259
+ sequences whose first 4 bytes share the same hash code.
260
+ Each sequence is MAX_TREE_COMP_LENGTH long and is identified by its starting
261
+ position in the input data. The binary tree is sorted by the lexicographic
262
+ order of the sequences, and it is also a max-heap with respect to the
263
+ starting positions. */
264
+
265
+ #define HashToBinaryTree HASHER()
266
+
267
+ #define BUCKET_SIZE (1 << BUCKET_BITS)
268
+
269
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }
270
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) {
271
+ return MAX_TREE_COMP_LENGTH;
272
+ }
273
+
274
+ static uint32_t FN(HashBytes)(const uint8_t* BROTLI_RESTRICT data) {
275
+ uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
276
+ /* The higher bits contain more mixture from the multiplication,
277
+ so we take our results from there. */
278
+ return h >> (32 - BUCKET_BITS);
279
+ }
280
+
281
+ typedef struct HashToBinaryTree {
282
+ /* The window size minus 1 */
283
+ size_t window_mask_;
284
+
285
+ /* Hash table that maps the 4-byte hashes of the sequence to the last
286
+ position where this hash was found, which is the root of the binary
287
+ tree of sequences that share this hash bucket. */
288
+ uint32_t* buckets_; /* uint32_t[BUCKET_SIZE]; */
289
+
290
+ /* A position used to mark a non-existent sequence, i.e. a tree is empty if
291
+ its root is at invalid_pos_ and a node is a leaf if both its children
292
+ are at invalid_pos_. */
293
+ uint32_t invalid_pos_;
294
+
295
+ /* --- Dynamic size members --- */
296
+
297
+ /* The union of the binary trees of each hash bucket. The root of the tree
298
+ corresponding to a hash is a sequence starting at buckets_[hash] and
299
+ the left and right children of a sequence starting at pos are
300
+ forest_[2 * pos] and forest_[2 * pos + 1]. */
301
+ uint32_t* forest_; /* uint32_t[2 * num_nodes] */
302
+ } HashToBinaryTree;
303
+
304
+ static void FN(Initialize)(
305
+ HasherCommon* common, HashToBinaryTree* BROTLI_RESTRICT self,
306
+ const BrotliEncoderParams* params) {
307
+ self->buckets_ = (uint32_t*)common->extra[0];
308
+ self->forest_ = (uint32_t*)common->extra[1];
309
+
310
+ self->window_mask_ = (1u << params->lgwin) - 1u;
311
+ self->invalid_pos_ = (uint32_t)(0 - self->window_mask_);
312
+ }
313
+
314
+ static void FN(Prepare)
315
+ (HashToBinaryTree* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
316
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
317
+ uint32_t invalid_pos = self->invalid_pos_;
318
+ uint32_t i;
319
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
320
+ BROTLI_UNUSED(data);
321
+ BROTLI_UNUSED(one_shot);
322
+ BROTLI_UNUSED(input_size);
323
+ for (i = 0; i < BUCKET_SIZE; i++) {
324
+ buckets[i] = invalid_pos;
325
+ }
326
+ }
327
+
328
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
329
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
330
+ size_t input_size, size_t* alloc_size) {
331
+ size_t num_nodes = (size_t)1 << params->lgwin;
332
+ if (one_shot && input_size < num_nodes) {
333
+ num_nodes = input_size;
334
+ }
335
+ alloc_size[0] = sizeof(uint32_t) * BUCKET_SIZE;
336
+ alloc_size[1] = 2 * sizeof(uint32_t) * num_nodes;
337
+ }
338
+
339
+ static BROTLI_INLINE size_t FN(LeftChildIndex)(
340
+ HashToBinaryTree* BROTLI_RESTRICT self,
341
+ const size_t pos) {
342
+ return 2 * (pos & self->window_mask_);
343
+ }
344
+
345
+ static BROTLI_INLINE size_t FN(RightChildIndex)(
346
+ HashToBinaryTree* BROTLI_RESTRICT self,
347
+ const size_t pos) {
348
+ return 2 * (pos & self->window_mask_) + 1;
349
+ }
350
+
351
+ /* Stores the hash of the next 4 bytes and in a single tree-traversal, the
352
+ hash bucket's binary tree is searched for matches and is re-rooted at the
353
+ current position.
354
+
355
+ If less than MAX_TREE_COMP_LENGTH data is available, the hash bucket of the
356
+ current position is searched for matches, but the state of the hash table
357
+ is not changed, since we can not know the final sorting order of the
358
+ current (incomplete) sequence.
359
+
360
+ This function must be called with increasing cur_ix positions. */
361
+ static BROTLI_INLINE BackwardMatch* FN(StoreAndFindMatches)(
362
+ HashToBinaryTree* BROTLI_RESTRICT self, const uint8_t* BROTLI_RESTRICT data,
363
+ const size_t cur_ix, const size_t ring_buffer_mask, const size_t max_length,
364
+ const size_t max_backward, size_t* const BROTLI_RESTRICT best_len,
365
+ BackwardMatch* BROTLI_RESTRICT matches) {
366
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
367
+ const size_t max_comp_len =
368
+ BROTLI_MIN(size_t, max_length, MAX_TREE_COMP_LENGTH);
369
+ const BROTLI_BOOL should_reroot_tree =
370
+ TO_BROTLI_BOOL(max_length >= MAX_TREE_COMP_LENGTH);
371
+ const uint32_t key = FN(HashBytes)(&data[cur_ix_masked]);
372
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
373
+ uint32_t* BROTLI_RESTRICT forest = self->forest_;
374
+ size_t prev_ix = buckets[key];
375
+ /* The forest index of the rightmost node of the left subtree of the new
376
+ root, updated as we traverse and re-root the tree of the hash bucket. */
377
+ size_t node_left = FN(LeftChildIndex)(self, cur_ix);
378
+ /* The forest index of the leftmost node of the right subtree of the new
379
+ root, updated as we traverse and re-root the tree of the hash bucket. */
380
+ size_t node_right = FN(RightChildIndex)(self, cur_ix);
381
+ /* The match length of the rightmost node of the left subtree of the new
382
+ root, updated as we traverse and re-root the tree of the hash bucket. */
383
+ size_t best_len_left = 0;
384
+ /* The match length of the leftmost node of the right subtree of the new
385
+ root, updated as we traverse and re-root the tree of the hash bucket. */
386
+ size_t best_len_right = 0;
387
+ size_t depth_remaining;
388
+ if (should_reroot_tree) {
389
+ buckets[key] = (uint32_t)cur_ix;
390
+ }
391
+ for (depth_remaining = MAX_TREE_SEARCH_DEPTH; ; --depth_remaining) {
392
+ const size_t backward = cur_ix - prev_ix;
393
+ const size_t prev_ix_masked = prev_ix & ring_buffer_mask;
394
+ if (backward == 0 || backward > max_backward || depth_remaining == 0) {
395
+ if (should_reroot_tree) {
396
+ forest[node_left] = self->invalid_pos_;
397
+ forest[node_right] = self->invalid_pos_;
398
+ }
399
+ break;
400
+ }
401
+ {
402
+ const size_t cur_len = BROTLI_MIN(size_t, best_len_left, best_len_right);
403
+ size_t len;
404
+ BROTLI_DCHECK(cur_len <= MAX_TREE_COMP_LENGTH);
405
+ len = cur_len +
406
+ FindMatchLengthWithLimit(&data[cur_ix_masked + cur_len],
407
+ &data[prev_ix_masked + cur_len],
408
+ max_length - cur_len);
409
+ BROTLI_DCHECK(
410
+ 0 == memcmp(&data[cur_ix_masked], &data[prev_ix_masked], len));
411
+ if (matches && len > *best_len) {
412
+ *best_len = len;
413
+ InitBackwardMatch(matches++, backward, len);
414
+ }
415
+ if (len >= max_comp_len) {
416
+ if (should_reroot_tree) {
417
+ forest[node_left] = forest[FN(LeftChildIndex)(self, prev_ix)];
418
+ forest[node_right] = forest[FN(RightChildIndex)(self, prev_ix)];
419
+ }
420
+ break;
421
+ }
422
+ if (data[cur_ix_masked + len] > data[prev_ix_masked + len]) {
423
+ best_len_left = len;
424
+ if (should_reroot_tree) {
425
+ forest[node_left] = (uint32_t)prev_ix;
426
+ }
427
+ node_left = FN(RightChildIndex)(self, prev_ix);
428
+ prev_ix = forest[node_left];
429
+ } else {
430
+ best_len_right = len;
431
+ if (should_reroot_tree) {
432
+ forest[node_right] = (uint32_t)prev_ix;
433
+ }
434
+ node_right = FN(LeftChildIndex)(self, prev_ix);
435
+ prev_ix = forest[node_right];
436
+ }
437
+ }
438
+ }
439
+ return matches;
440
+ }
441
+
442
+ /* Finds all backward matches of &data[cur_ix & ring_buffer_mask] up to the
443
+ length of max_length and stores the position cur_ix in the hash table.
444
+
445
+ Sets *num_matches to the number of matches found, and stores the found
446
+ matches in matches[0] to matches[*num_matches - 1]. The matches will be
447
+ sorted by strictly increasing length and (non-strictly) increasing
448
+ distance. */
449
+ static BROTLI_INLINE size_t FN(FindAllMatches)(
450
+ HashToBinaryTree* BROTLI_RESTRICT self,
451
+ const BrotliEncoderDictionary* dictionary,
452
+ const uint8_t* BROTLI_RESTRICT data,
453
+ const size_t ring_buffer_mask, const size_t cur_ix,
454
+ const size_t max_length, const size_t max_backward,
455
+ const size_t dictionary_distance, const BrotliEncoderParams* params,
456
+ BackwardMatch* matches) {
457
+ BackwardMatch* const orig_matches = matches;
458
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
459
+ size_t best_len = 1;
460
+ const size_t short_match_max_backward =
461
+ params->quality != HQ_ZOPFLIFICATION_QUALITY ? 16 : 64;
462
+ size_t stop = cur_ix - short_match_max_backward;
463
+ uint32_t dict_matches[BROTLI_MAX_STATIC_DICTIONARY_MATCH_LEN + 1];
464
+ size_t i;
465
+ if (cur_ix < short_match_max_backward) { stop = 0; }
466
+ for (i = cur_ix - 1; i > stop && best_len <= 2; --i) {
467
+ size_t prev_ix = i;
468
+ const size_t backward = cur_ix - prev_ix;
469
+ if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
470
+ break;
471
+ }
472
+ prev_ix &= ring_buffer_mask;
473
+ if (data[cur_ix_masked] != data[prev_ix] ||
474
+ data[cur_ix_masked + 1] != data[prev_ix + 1]) {
475
+ continue;
476
+ }
477
+ {
478
+ const size_t len =
479
+ FindMatchLengthWithLimit(&data[prev_ix], &data[cur_ix_masked],
480
+ max_length);
481
+ if (len > best_len) {
482
+ best_len = len;
483
+ InitBackwardMatch(matches++, backward, len);
484
+ }
485
+ }
486
+ }
487
+ if (best_len < max_length) {
488
+ matches = FN(StoreAndFindMatches)(self, data, cur_ix,
489
+ ring_buffer_mask, max_length, max_backward, &best_len, matches);
490
+ }
491
+ for (i = 0; i <= BROTLI_MAX_STATIC_DICTIONARY_MATCH_LEN; ++i) {
492
+ dict_matches[i] = kInvalidMatch;
493
+ }
494
+ {
495
+ size_t minlen = BROTLI_MAX(size_t, 4, best_len + 1);
496
+ if (BrotliFindAllStaticDictionaryMatches(dictionary,
497
+ &data[cur_ix_masked], minlen, max_length, &dict_matches[0])) {
498
+ size_t maxlen = BROTLI_MIN(
499
+ size_t, BROTLI_MAX_STATIC_DICTIONARY_MATCH_LEN, max_length);
500
+ size_t l;
501
+ for (l = minlen; l <= maxlen; ++l) {
502
+ uint32_t dict_id = dict_matches[l];
503
+ if (dict_id < kInvalidMatch) {
504
+ size_t distance = dictionary_distance + (dict_id >> 5) + 1;
505
+ if (distance <= params->dist.max_distance) {
506
+ InitDictionaryBackwardMatch(matches++, distance, l, dict_id & 31);
507
+ }
508
+ }
509
+ }
510
+ }
511
+ }
512
+ return (size_t)(matches - orig_matches);
513
+ }
514
+
515
+ /* Stores the hash of the next 4 bytes and re-roots the binary tree at the
516
+ current sequence, without returning any matches.
517
+ REQUIRES: ix + MAX_TREE_COMP_LENGTH <= end-of-current-block */
518
+ static BROTLI_INLINE void FN(Store)(HashToBinaryTree* BROTLI_RESTRICT self,
519
+ const uint8_t* BROTLI_RESTRICT data,
520
+ const size_t mask, const size_t ix) {
521
+ /* Maximum distance is window size - 16, see section 9.1. of the spec. */
522
+ const size_t max_backward = self->window_mask_ - BROTLI_WINDOW_GAP + 1;
523
+ FN(StoreAndFindMatches)(self, data, ix, mask, MAX_TREE_COMP_LENGTH,
524
+ max_backward, NULL, NULL);
525
+ }
526
+
527
+ static BROTLI_INLINE void FN(StoreRange)(HashToBinaryTree* BROTLI_RESTRICT self,
528
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
529
+ const size_t ix_start, const size_t ix_end) {
530
+ size_t i = ix_start;
531
+ size_t j = ix_start;
532
+ if (ix_start + 63 <= ix_end) {
533
+ i = ix_end - 63;
534
+ }
535
+ if (ix_start + 512 <= i) {
536
+ for (; j < i; j += 8) {
537
+ FN(Store)(self, data, mask, j);
538
+ }
539
+ }
540
+ for (; i < ix_end; ++i) {
541
+ FN(Store)(self, data, mask, i);
542
+ }
543
+ }
544
+
545
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
546
+ HashToBinaryTree* BROTLI_RESTRICT self,
547
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
548
+ size_t ringbuffer_mask) {
549
+ if (num_bytes >= FN(HashTypeLength)() - 1 &&
550
+ position >= MAX_TREE_COMP_LENGTH) {
551
+ /* Store the last `MAX_TREE_COMP_LENGTH - 1` positions in the hasher.
552
+ These could not be calculated before, since they require knowledge
553
+ of both the previous and the current block. */
554
+ const size_t i_start = position - MAX_TREE_COMP_LENGTH + 1;
555
+ const size_t i_end = BROTLI_MIN(size_t, position, i_start + num_bytes);
556
+ size_t i;
557
+ for (i = i_start; i < i_end; ++i) {
558
+ /* Maximum distance is window size - 16, see section 9.1. of the spec.
559
+ Furthermore, we have to make sure that we don't look further back
560
+ from the start of the next block than the window size, otherwise we
561
+ could access already overwritten areas of the ring-buffer. */
562
+ const size_t max_backward =
563
+ self->window_mask_ - BROTLI_MAX(size_t,
564
+ BROTLI_WINDOW_GAP - 1,
565
+ position - i);
566
+ /* We know that i + MAX_TREE_COMP_LENGTH <= position + num_bytes, i.e. the
567
+ end of the current block and that we have at least
568
+ MAX_TREE_COMP_LENGTH tail in the ring-buffer. */
569
+ FN(StoreAndFindMatches)(self, ringbuffer, i, ringbuffer_mask,
570
+ MAX_TREE_COMP_LENGTH, max_backward, NULL, NULL);
571
+ }
572
+ }
573
+ }
574
+
575
+ #undef BUCKET_SIZE
576
+
577
+ #undef HashToBinaryTree
578
+ #undef MAX_TREE_SEARCH_DEPTH
579
+ #undef MAX_TREE_COMP_LENGTH
580
+ #undef BUCKET_BITS
581
+ #undef HASHER
582
+ /* MAX_NUM_MATCHES == 64 + MAX_TREE_SEARCH_DEPTH */
583
+ #define MAX_NUM_MATCHES_H10 128
584
+
585
+ /* For BUCKET_SWEEP_BITS == 0, enabling the dictionary lookup makes compression
586
+ a little faster (0.5% - 1%) and it compresses 0.15% better on small text
587
+ and HTML inputs. */
588
+
589
+ #define HASHER() H2
590
+ #define BUCKET_BITS 16
591
+ #define BUCKET_SWEEP_BITS 0
592
+ #define HASH_LEN 5
593
+ #define USE_DICTIONARY 1
594
+ /* NOLINT(build/header_guard) */
595
+ /* Copyright 2010 Google Inc. All Rights Reserved.
596
+
597
+ Distributed under MIT license.
598
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
599
+ */
600
+
601
+ /* template parameters: FN, BUCKET_BITS, BUCKET_SWEEP_BITS, HASH_LEN,
602
+ USE_DICTIONARY
603
+ */
604
+
605
+ #define HashLongestMatchQuickly HASHER()
606
+
607
+ #define BUCKET_SIZE (1 << BUCKET_BITS)
608
+ #define BUCKET_MASK (BUCKET_SIZE - 1)
609
+ #define BUCKET_SWEEP (1 << BUCKET_SWEEP_BITS)
610
+ #define BUCKET_SWEEP_MASK ((BUCKET_SWEEP - 1) << 3)
611
+
612
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; }
613
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }
614
+
615
+ /* HashBytes is the function that chooses the bucket to place
616
+ the address in. The HashLongestMatch and HashLongestMatchQuickly
617
+ classes have separate, different implementations of hashing. */
618
+ static uint32_t FN(HashBytes)(const uint8_t* data) {
619
+ const uint64_t h = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8 * HASH_LEN)) *
620
+ kHashMul64);
621
+ /* The higher bits contain more mixture from the multiplication,
622
+ so we take our results from there. */
623
+ return (uint32_t)(h >> (64 - BUCKET_BITS));
624
+ }
625
+
626
+ /* A (forgetful) hash table to the data seen by the compressor, to
627
+ help create backward references to previous data.
628
+
629
+ This is a hash map of fixed size (BUCKET_SIZE). */
630
+ typedef struct HashLongestMatchQuickly {
631
+ /* Shortcuts. */
632
+ HasherCommon* common;
633
+
634
+ /* --- Dynamic size members --- */
635
+
636
+ uint32_t* buckets_; /* uint32_t[BUCKET_SIZE]; */
637
+ } HashLongestMatchQuickly;
638
+
639
+ static void FN(Initialize)(
640
+ HasherCommon* common, HashLongestMatchQuickly* BROTLI_RESTRICT self,
641
+ const BrotliEncoderParams* params) {
642
+ self->common = common;
643
+
644
+ BROTLI_UNUSED(params);
645
+ self->buckets_ = (uint32_t*)common->extra[0];
646
+ }
647
+
648
+ static void FN(Prepare)(
649
+ HashLongestMatchQuickly* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
650
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
651
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
652
+ /* Partial preparation is 100 times slower (per socket). */
653
+ size_t partial_prepare_threshold = BUCKET_SIZE >> 5;
654
+ if (one_shot && input_size <= partial_prepare_threshold) {
655
+ size_t i;
656
+ for (i = 0; i < input_size; ++i) {
657
+ const uint32_t key = FN(HashBytes)(&data[i]);
658
+ if (BUCKET_SWEEP == 1) {
659
+ buckets[key] = 0;
660
+ } else {
661
+ uint32_t j;
662
+ for (j = 0; j < BUCKET_SWEEP; ++j) {
663
+ buckets[(key + (j << 3)) & BUCKET_MASK] = 0;
664
+ }
665
+ }
666
+ }
667
+ } else {
668
+ /* It is not strictly necessary to fill this buffer here, but
669
+ not filling will make the results of the compression stochastic
670
+ (but correct). This is because random data would cause the
671
+ system to find accidentally good backward references here and there. */
672
+ memset(buckets, 0, sizeof(uint32_t) * BUCKET_SIZE);
673
+ }
674
+ }
675
+
676
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
677
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
678
+ size_t input_size, size_t* alloc_size) {
679
+ BROTLI_UNUSED(params);
680
+ BROTLI_UNUSED(one_shot);
681
+ BROTLI_UNUSED(input_size);
682
+ alloc_size[0] = sizeof(uint32_t) * BUCKET_SIZE;
683
+ }
684
+
685
+ /* Look at 5 bytes at &data[ix & mask].
686
+ Compute a hash from these, and store the value somewhere within
687
+ [ix .. ix+3]. */
688
+ static BROTLI_INLINE void FN(Store)(
689
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
690
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
691
+ const uint32_t key = FN(HashBytes)(&data[ix & mask]);
692
+ if (BUCKET_SWEEP == 1) {
693
+ self->buckets_[key] = (uint32_t)ix;
694
+ } else {
695
+ /* Wiggle the value with the bucket sweep range. */
696
+ const uint32_t off = ix & BUCKET_SWEEP_MASK;
697
+ self->buckets_[(key + off) & BUCKET_MASK] = (uint32_t)ix;
698
+ }
699
+ }
700
+
701
+ static BROTLI_INLINE void FN(StoreRange)(
702
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
703
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
704
+ const size_t ix_start, const size_t ix_end) {
705
+ size_t i;
706
+ for (i = ix_start; i < ix_end; ++i) {
707
+ FN(Store)(self, data, mask, i);
708
+ }
709
+ }
710
+
711
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
712
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
713
+ size_t num_bytes, size_t position,
714
+ const uint8_t* ringbuffer, size_t ringbuffer_mask) {
715
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
716
+ /* Prepare the hashes for three last bytes of the last write.
717
+ These could not be calculated before, since they require knowledge
718
+ of both the previous and the current block. */
719
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3);
720
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2);
721
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1);
722
+ }
723
+ }
724
+
725
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
726
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
727
+ int* BROTLI_RESTRICT distance_cache) {
728
+ BROTLI_UNUSED(self);
729
+ BROTLI_UNUSED(distance_cache);
730
+ }
731
+
732
+ /* Find a longest backward match of &data[cur_ix & ring_buffer_mask]
733
+ up to the length of max_length and stores the position cur_ix in the
734
+ hash table.
735
+
736
+ Does not look for matches longer than max_length.
737
+ Does not look for matches further away than max_backward.
738
+ Writes the best match into |out|.
739
+ |out|->score is updated only if a better match is found. */
740
+ static BROTLI_INLINE void FN(FindLongestMatch)(
741
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
742
+ const BrotliEncoderDictionary* dictionary,
743
+ const uint8_t* BROTLI_RESTRICT data,
744
+ const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
745
+ const size_t cur_ix, const size_t max_length, const size_t max_backward,
746
+ const size_t dictionary_distance, const size_t max_distance,
747
+ HasherSearchResult* BROTLI_RESTRICT out) {
748
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
749
+ const size_t best_len_in = out->len;
750
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
751
+ int compare_char = data[cur_ix_masked + best_len_in];
752
+ size_t key = FN(HashBytes)(&data[cur_ix_masked]);
753
+ size_t key_out;
754
+ score_t min_score = out->score;
755
+ score_t best_score = out->score;
756
+ size_t best_len = best_len_in;
757
+ size_t cached_backward = (size_t)distance_cache[0];
758
+ size_t prev_ix = cur_ix - cached_backward;
759
+ out->len_code_delta = 0;
760
+ if (prev_ix < cur_ix) {
761
+ prev_ix &= (uint32_t)ring_buffer_mask;
762
+ if (compare_char == data[prev_ix + best_len]) {
763
+ const size_t len = FindMatchLengthWithLimit(
764
+ &data[prev_ix], &data[cur_ix_masked], max_length);
765
+ if (len >= 4) {
766
+ const score_t score = BackwardReferenceScoreUsingLastDistance(len);
767
+ if (best_score < score) {
768
+ out->len = len;
769
+ out->distance = cached_backward;
770
+ out->score = score;
771
+ if (BUCKET_SWEEP == 1) {
772
+ buckets[key] = (uint32_t)cur_ix;
773
+ return;
774
+ } else {
775
+ best_len = len;
776
+ best_score = score;
777
+ compare_char = data[cur_ix_masked + len];
778
+ }
779
+ }
780
+ }
781
+ }
782
+ }
783
+ if (BUCKET_SWEEP == 1) {
784
+ size_t backward;
785
+ size_t len;
786
+ /* Only one to look for, don't bother to prepare for a loop. */
787
+ prev_ix = buckets[key];
788
+ buckets[key] = (uint32_t)cur_ix;
789
+ backward = cur_ix - prev_ix;
790
+ prev_ix &= (uint32_t)ring_buffer_mask;
791
+ if (compare_char != data[prev_ix + best_len_in]) {
792
+ return;
793
+ }
794
+ if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
795
+ return;
796
+ }
797
+ len = FindMatchLengthWithLimit(&data[prev_ix],
798
+ &data[cur_ix_masked],
799
+ max_length);
800
+ if (len >= 4) {
801
+ const score_t score = BackwardReferenceScore(len, backward);
802
+ if (best_score < score) {
803
+ out->len = len;
804
+ out->distance = backward;
805
+ out->score = score;
806
+ return;
807
+ }
808
+ }
809
+ } else {
810
+ size_t keys[BUCKET_SWEEP];
811
+ size_t i;
812
+ for (i = 0; i < BUCKET_SWEEP; ++i) {
813
+ keys[i] = (key + (i << 3)) & BUCKET_MASK;
814
+ }
815
+ key_out = keys[(cur_ix & BUCKET_SWEEP_MASK) >> 3];
816
+ for (i = 0; i < BUCKET_SWEEP; ++i) {
817
+ size_t len;
818
+ size_t backward;
819
+ prev_ix = buckets[keys[i]];
820
+ backward = cur_ix - prev_ix;
821
+ prev_ix &= (uint32_t)ring_buffer_mask;
822
+ if (compare_char != data[prev_ix + best_len]) {
823
+ continue;
824
+ }
825
+ if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
826
+ continue;
827
+ }
828
+ len = FindMatchLengthWithLimit(&data[prev_ix],
829
+ &data[cur_ix_masked],
830
+ max_length);
831
+ if (len >= 4) {
832
+ const score_t score = BackwardReferenceScore(len, backward);
833
+ if (best_score < score) {
834
+ best_len = len;
835
+ out->len = len;
836
+ compare_char = data[cur_ix_masked + len];
837
+ best_score = score;
838
+ out->score = score;
839
+ out->distance = backward;
840
+ }
841
+ }
842
+ }
843
+ }
844
+ if (USE_DICTIONARY && min_score == out->score) {
845
+ SearchInStaticDictionary(dictionary,
846
+ self->common, &data[cur_ix_masked], max_length, dictionary_distance,
847
+ max_distance, out, BROTLI_TRUE);
848
+ }
849
+ if (BUCKET_SWEEP != 1) {
850
+ buckets[key_out] = (uint32_t)cur_ix;
851
+ }
852
+ }
853
+
854
+ #undef BUCKET_SWEEP_MASK
855
+ #undef BUCKET_SWEEP
856
+ #undef BUCKET_MASK
857
+ #undef BUCKET_SIZE
858
+
859
+ #undef HashLongestMatchQuickly
860
+ #undef BUCKET_SWEEP_BITS
861
+ #undef USE_DICTIONARY
862
+ #undef HASHER
863
+
864
+ #define HASHER() H3
865
+ #define BUCKET_SWEEP_BITS 1
866
+ #define USE_DICTIONARY 0
867
+ /* NOLINT(build/header_guard) */
868
+ /* Copyright 2010 Google Inc. All Rights Reserved.
869
+
870
+ Distributed under MIT license.
871
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
872
+ */
873
+
874
+ /* template parameters: FN, BUCKET_BITS, BUCKET_SWEEP_BITS, HASH_LEN,
875
+ USE_DICTIONARY
876
+ */
877
+
878
+ #define HashLongestMatchQuickly HASHER()
879
+
880
+ #define BUCKET_SIZE (1 << BUCKET_BITS)
881
+ #define BUCKET_MASK (BUCKET_SIZE - 1)
882
+ #define BUCKET_SWEEP (1 << BUCKET_SWEEP_BITS)
883
+ #define BUCKET_SWEEP_MASK ((BUCKET_SWEEP - 1) << 3)
884
+
885
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; }
886
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }
887
+
888
+ /* HashBytes is the function that chooses the bucket to place
889
+ the address in. The HashLongestMatch and HashLongestMatchQuickly
890
+ classes have separate, different implementations of hashing. */
891
+ static uint32_t FN(HashBytes)(const uint8_t* data) {
892
+ const uint64_t h = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8 * HASH_LEN)) *
893
+ kHashMul64);
894
+ /* The higher bits contain more mixture from the multiplication,
895
+ so we take our results from there. */
896
+ return (uint32_t)(h >> (64 - BUCKET_BITS));
897
+ }
898
+
899
+ /* A (forgetful) hash table to the data seen by the compressor, to
900
+ help create backward references to previous data.
901
+
902
+ This is a hash map of fixed size (BUCKET_SIZE). */
903
+ typedef struct HashLongestMatchQuickly {
904
+ /* Shortcuts. */
905
+ HasherCommon* common;
906
+
907
+ /* --- Dynamic size members --- */
908
+
909
+ uint32_t* buckets_; /* uint32_t[BUCKET_SIZE]; */
910
+ } HashLongestMatchQuickly;
911
+
912
+ static void FN(Initialize)(
913
+ HasherCommon* common, HashLongestMatchQuickly* BROTLI_RESTRICT self,
914
+ const BrotliEncoderParams* params) {
915
+ self->common = common;
916
+
917
+ BROTLI_UNUSED(params);
918
+ self->buckets_ = (uint32_t*)common->extra[0];
919
+ }
920
+
921
+ static void FN(Prepare)(
922
+ HashLongestMatchQuickly* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
923
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
924
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
925
+ /* Partial preparation is 100 times slower (per socket). */
926
+ size_t partial_prepare_threshold = BUCKET_SIZE >> 5;
927
+ if (one_shot && input_size <= partial_prepare_threshold) {
928
+ size_t i;
929
+ for (i = 0; i < input_size; ++i) {
930
+ const uint32_t key = FN(HashBytes)(&data[i]);
931
+ if (BUCKET_SWEEP == 1) {
932
+ buckets[key] = 0;
933
+ } else {
934
+ uint32_t j;
935
+ for (j = 0; j < BUCKET_SWEEP; ++j) {
936
+ buckets[(key + (j << 3)) & BUCKET_MASK] = 0;
937
+ }
938
+ }
939
+ }
940
+ } else {
941
+ /* It is not strictly necessary to fill this buffer here, but
942
+ not filling will make the results of the compression stochastic
943
+ (but correct). This is because random data would cause the
944
+ system to find accidentally good backward references here and there. */
945
+ memset(buckets, 0, sizeof(uint32_t) * BUCKET_SIZE);
946
+ }
947
+ }
948
+
949
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
950
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
951
+ size_t input_size, size_t* alloc_size) {
952
+ BROTLI_UNUSED(params);
953
+ BROTLI_UNUSED(one_shot);
954
+ BROTLI_UNUSED(input_size);
955
+ alloc_size[0] = sizeof(uint32_t) * BUCKET_SIZE;
956
+ }
957
+
958
+ /* Look at 5 bytes at &data[ix & mask].
959
+ Compute a hash from these, and store the value somewhere within
960
+ [ix .. ix+3]. */
961
+ static BROTLI_INLINE void FN(Store)(
962
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
963
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
964
+ const uint32_t key = FN(HashBytes)(&data[ix & mask]);
965
+ if (BUCKET_SWEEP == 1) {
966
+ self->buckets_[key] = (uint32_t)ix;
967
+ } else {
968
+ /* Wiggle the value with the bucket sweep range. */
969
+ const uint32_t off = ix & BUCKET_SWEEP_MASK;
970
+ self->buckets_[(key + off) & BUCKET_MASK] = (uint32_t)ix;
971
+ }
972
+ }
973
+
974
+ static BROTLI_INLINE void FN(StoreRange)(
975
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
976
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
977
+ const size_t ix_start, const size_t ix_end) {
978
+ size_t i;
979
+ for (i = ix_start; i < ix_end; ++i) {
980
+ FN(Store)(self, data, mask, i);
981
+ }
982
+ }
983
+
984
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
985
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
986
+ size_t num_bytes, size_t position,
987
+ const uint8_t* ringbuffer, size_t ringbuffer_mask) {
988
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
989
+ /* Prepare the hashes for three last bytes of the last write.
990
+ These could not be calculated before, since they require knowledge
991
+ of both the previous and the current block. */
992
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3);
993
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2);
994
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1);
995
+ }
996
+ }
997
+
998
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
999
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
1000
+ int* BROTLI_RESTRICT distance_cache) {
1001
+ BROTLI_UNUSED(self);
1002
+ BROTLI_UNUSED(distance_cache);
1003
+ }
1004
+
1005
+ /* Find a longest backward match of &data[cur_ix & ring_buffer_mask]
1006
+ up to the length of max_length and stores the position cur_ix in the
1007
+ hash table.
1008
+
1009
+ Does not look for matches longer than max_length.
1010
+ Does not look for matches further away than max_backward.
1011
+ Writes the best match into |out|.
1012
+ |out|->score is updated only if a better match is found. */
1013
+ static BROTLI_INLINE void FN(FindLongestMatch)(
1014
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
1015
+ const BrotliEncoderDictionary* dictionary,
1016
+ const uint8_t* BROTLI_RESTRICT data,
1017
+ const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
1018
+ const size_t cur_ix, const size_t max_length, const size_t max_backward,
1019
+ const size_t dictionary_distance, const size_t max_distance,
1020
+ HasherSearchResult* BROTLI_RESTRICT out) {
1021
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
1022
+ const size_t best_len_in = out->len;
1023
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
1024
+ int compare_char = data[cur_ix_masked + best_len_in];
1025
+ size_t key = FN(HashBytes)(&data[cur_ix_masked]);
1026
+ size_t key_out;
1027
+ score_t min_score = out->score;
1028
+ score_t best_score = out->score;
1029
+ size_t best_len = best_len_in;
1030
+ size_t cached_backward = (size_t)distance_cache[0];
1031
+ size_t prev_ix = cur_ix - cached_backward;
1032
+ out->len_code_delta = 0;
1033
+ if (prev_ix < cur_ix) {
1034
+ prev_ix &= (uint32_t)ring_buffer_mask;
1035
+ if (compare_char == data[prev_ix + best_len]) {
1036
+ const size_t len = FindMatchLengthWithLimit(
1037
+ &data[prev_ix], &data[cur_ix_masked], max_length);
1038
+ if (len >= 4) {
1039
+ const score_t score = BackwardReferenceScoreUsingLastDistance(len);
1040
+ if (best_score < score) {
1041
+ out->len = len;
1042
+ out->distance = cached_backward;
1043
+ out->score = score;
1044
+ if (BUCKET_SWEEP == 1) {
1045
+ buckets[key] = (uint32_t)cur_ix;
1046
+ return;
1047
+ } else {
1048
+ best_len = len;
1049
+ best_score = score;
1050
+ compare_char = data[cur_ix_masked + len];
1051
+ }
1052
+ }
1053
+ }
1054
+ }
1055
+ }
1056
+ if (BUCKET_SWEEP == 1) {
1057
+ size_t backward;
1058
+ size_t len;
1059
+ /* Only one to look for, don't bother to prepare for a loop. */
1060
+ prev_ix = buckets[key];
1061
+ buckets[key] = (uint32_t)cur_ix;
1062
+ backward = cur_ix - prev_ix;
1063
+ prev_ix &= (uint32_t)ring_buffer_mask;
1064
+ if (compare_char != data[prev_ix + best_len_in]) {
1065
+ return;
1066
+ }
1067
+ if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
1068
+ return;
1069
+ }
1070
+ len = FindMatchLengthWithLimit(&data[prev_ix],
1071
+ &data[cur_ix_masked],
1072
+ max_length);
1073
+ if (len >= 4) {
1074
+ const score_t score = BackwardReferenceScore(len, backward);
1075
+ if (best_score < score) {
1076
+ out->len = len;
1077
+ out->distance = backward;
1078
+ out->score = score;
1079
+ return;
1080
+ }
1081
+ }
1082
+ } else {
1083
+ size_t keys[BUCKET_SWEEP];
1084
+ size_t i;
1085
+ for (i = 0; i < BUCKET_SWEEP; ++i) {
1086
+ keys[i] = (key + (i << 3)) & BUCKET_MASK;
1087
+ }
1088
+ key_out = keys[(cur_ix & BUCKET_SWEEP_MASK) >> 3];
1089
+ for (i = 0; i < BUCKET_SWEEP; ++i) {
1090
+ size_t len;
1091
+ size_t backward;
1092
+ prev_ix = buckets[keys[i]];
1093
+ backward = cur_ix - prev_ix;
1094
+ prev_ix &= (uint32_t)ring_buffer_mask;
1095
+ if (compare_char != data[prev_ix + best_len]) {
1096
+ continue;
1097
+ }
1098
+ if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
1099
+ continue;
1100
+ }
1101
+ len = FindMatchLengthWithLimit(&data[prev_ix],
1102
+ &data[cur_ix_masked],
1103
+ max_length);
1104
+ if (len >= 4) {
1105
+ const score_t score = BackwardReferenceScore(len, backward);
1106
+ if (best_score < score) {
1107
+ best_len = len;
1108
+ out->len = len;
1109
+ compare_char = data[cur_ix_masked + len];
1110
+ best_score = score;
1111
+ out->score = score;
1112
+ out->distance = backward;
1113
+ }
1114
+ }
1115
+ }
1116
+ }
1117
+ if (USE_DICTIONARY && min_score == out->score) {
1118
+ SearchInStaticDictionary(dictionary,
1119
+ self->common, &data[cur_ix_masked], max_length, dictionary_distance,
1120
+ max_distance, out, BROTLI_TRUE);
1121
+ }
1122
+ if (BUCKET_SWEEP != 1) {
1123
+ buckets[key_out] = (uint32_t)cur_ix;
1124
+ }
1125
+ }
1126
+
1127
+ #undef BUCKET_SWEEP_MASK
1128
+ #undef BUCKET_SWEEP
1129
+ #undef BUCKET_MASK
1130
+ #undef BUCKET_SIZE
1131
+
1132
+ #undef HashLongestMatchQuickly
1133
+ #undef USE_DICTIONARY
1134
+ #undef BUCKET_SWEEP_BITS
1135
+ #undef BUCKET_BITS
1136
+ #undef HASHER
1137
+
1138
+ #define HASHER() H4
1139
+ #define BUCKET_BITS 17
1140
+ #define BUCKET_SWEEP_BITS 2
1141
+ #define USE_DICTIONARY 1
1142
+ /* NOLINT(build/header_guard) */
1143
+ /* Copyright 2010 Google Inc. All Rights Reserved.
1144
+
1145
+ Distributed under MIT license.
1146
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
1147
+ */
1148
+
1149
+ /* template parameters: FN, BUCKET_BITS, BUCKET_SWEEP_BITS, HASH_LEN,
1150
+ USE_DICTIONARY
1151
+ */
1152
+
1153
+ #define HashLongestMatchQuickly HASHER()
1154
+
1155
+ #define BUCKET_SIZE (1 << BUCKET_BITS)
1156
+ #define BUCKET_MASK (BUCKET_SIZE - 1)
1157
+ #define BUCKET_SWEEP (1 << BUCKET_SWEEP_BITS)
1158
+ #define BUCKET_SWEEP_MASK ((BUCKET_SWEEP - 1) << 3)
1159
+
1160
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; }
1161
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }
1162
+
1163
+ /* HashBytes is the function that chooses the bucket to place
1164
+ the address in. The HashLongestMatch and HashLongestMatchQuickly
1165
+ classes have separate, different implementations of hashing. */
1166
+ static uint32_t FN(HashBytes)(const uint8_t* data) {
1167
+ const uint64_t h = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8 * HASH_LEN)) *
1168
+ kHashMul64);
1169
+ /* The higher bits contain more mixture from the multiplication,
1170
+ so we take our results from there. */
1171
+ return (uint32_t)(h >> (64 - BUCKET_BITS));
1172
+ }
1173
+
1174
+ /* A (forgetful) hash table to the data seen by the compressor, to
1175
+ help create backward references to previous data.
1176
+
1177
+ This is a hash map of fixed size (BUCKET_SIZE). */
1178
+ typedef struct HashLongestMatchQuickly {
1179
+ /* Shortcuts. */
1180
+ HasherCommon* common;
1181
+
1182
+ /* --- Dynamic size members --- */
1183
+
1184
+ uint32_t* buckets_; /* uint32_t[BUCKET_SIZE]; */
1185
+ } HashLongestMatchQuickly;
1186
+
1187
+ static void FN(Initialize)(
1188
+ HasherCommon* common, HashLongestMatchQuickly* BROTLI_RESTRICT self,
1189
+ const BrotliEncoderParams* params) {
1190
+ self->common = common;
1191
+
1192
+ BROTLI_UNUSED(params);
1193
+ self->buckets_ = (uint32_t*)common->extra[0];
1194
+ }
1195
+
1196
+ static void FN(Prepare)(
1197
+ HashLongestMatchQuickly* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
1198
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
1199
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
1200
+ /* Partial preparation is 100 times slower (per socket). */
1201
+ size_t partial_prepare_threshold = BUCKET_SIZE >> 5;
1202
+ if (one_shot && input_size <= partial_prepare_threshold) {
1203
+ size_t i;
1204
+ for (i = 0; i < input_size; ++i) {
1205
+ const uint32_t key = FN(HashBytes)(&data[i]);
1206
+ if (BUCKET_SWEEP == 1) {
1207
+ buckets[key] = 0;
1208
+ } else {
1209
+ uint32_t j;
1210
+ for (j = 0; j < BUCKET_SWEEP; ++j) {
1211
+ buckets[(key + (j << 3)) & BUCKET_MASK] = 0;
1212
+ }
1213
+ }
1214
+ }
1215
+ } else {
1216
+ /* It is not strictly necessary to fill this buffer here, but
1217
+ not filling will make the results of the compression stochastic
1218
+ (but correct). This is because random data would cause the
1219
+ system to find accidentally good backward references here and there. */
1220
+ memset(buckets, 0, sizeof(uint32_t) * BUCKET_SIZE);
1221
+ }
1222
+ }
1223
+
1224
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
1225
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
1226
+ size_t input_size, size_t* alloc_size) {
1227
+ BROTLI_UNUSED(params);
1228
+ BROTLI_UNUSED(one_shot);
1229
+ BROTLI_UNUSED(input_size);
1230
+ alloc_size[0] = sizeof(uint32_t) * BUCKET_SIZE;
1231
+ }
1232
+
1233
+ /* Look at 5 bytes at &data[ix & mask].
1234
+ Compute a hash from these, and store the value somewhere within
1235
+ [ix .. ix+3]. */
1236
+ static BROTLI_INLINE void FN(Store)(
1237
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
1238
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
1239
+ const uint32_t key = FN(HashBytes)(&data[ix & mask]);
1240
+ if (BUCKET_SWEEP == 1) {
1241
+ self->buckets_[key] = (uint32_t)ix;
1242
+ } else {
1243
+ /* Wiggle the value with the bucket sweep range. */
1244
+ const uint32_t off = ix & BUCKET_SWEEP_MASK;
1245
+ self->buckets_[(key + off) & BUCKET_MASK] = (uint32_t)ix;
1246
+ }
1247
+ }
1248
+
1249
+ static BROTLI_INLINE void FN(StoreRange)(
1250
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
1251
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
1252
+ const size_t ix_start, const size_t ix_end) {
1253
+ size_t i;
1254
+ for (i = ix_start; i < ix_end; ++i) {
1255
+ FN(Store)(self, data, mask, i);
1256
+ }
1257
+ }
1258
+
1259
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
1260
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
1261
+ size_t num_bytes, size_t position,
1262
+ const uint8_t* ringbuffer, size_t ringbuffer_mask) {
1263
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
1264
+ /* Prepare the hashes for three last bytes of the last write.
1265
+ These could not be calculated before, since they require knowledge
1266
+ of both the previous and the current block. */
1267
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3);
1268
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2);
1269
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1);
1270
+ }
1271
+ }
1272
+
1273
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
1274
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
1275
+ int* BROTLI_RESTRICT distance_cache) {
1276
+ BROTLI_UNUSED(self);
1277
+ BROTLI_UNUSED(distance_cache);
1278
+ }
1279
+
1280
+ /* Find a longest backward match of &data[cur_ix & ring_buffer_mask]
1281
+ up to the length of max_length and stores the position cur_ix in the
1282
+ hash table.
1283
+
1284
+ Does not look for matches longer than max_length.
1285
+ Does not look for matches further away than max_backward.
1286
+ Writes the best match into |out|.
1287
+ |out|->score is updated only if a better match is found. */
1288
+ static BROTLI_INLINE void FN(FindLongestMatch)(
1289
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
1290
+ const BrotliEncoderDictionary* dictionary,
1291
+ const uint8_t* BROTLI_RESTRICT data,
1292
+ const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
1293
+ const size_t cur_ix, const size_t max_length, const size_t max_backward,
1294
+ const size_t dictionary_distance, const size_t max_distance,
1295
+ HasherSearchResult* BROTLI_RESTRICT out) {
1296
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
1297
+ const size_t best_len_in = out->len;
1298
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
1299
+ int compare_char = data[cur_ix_masked + best_len_in];
1300
+ size_t key = FN(HashBytes)(&data[cur_ix_masked]);
1301
+ size_t key_out;
1302
+ score_t min_score = out->score;
1303
+ score_t best_score = out->score;
1304
+ size_t best_len = best_len_in;
1305
+ size_t cached_backward = (size_t)distance_cache[0];
1306
+ size_t prev_ix = cur_ix - cached_backward;
1307
+ out->len_code_delta = 0;
1308
+ if (prev_ix < cur_ix) {
1309
+ prev_ix &= (uint32_t)ring_buffer_mask;
1310
+ if (compare_char == data[prev_ix + best_len]) {
1311
+ const size_t len = FindMatchLengthWithLimit(
1312
+ &data[prev_ix], &data[cur_ix_masked], max_length);
1313
+ if (len >= 4) {
1314
+ const score_t score = BackwardReferenceScoreUsingLastDistance(len);
1315
+ if (best_score < score) {
1316
+ out->len = len;
1317
+ out->distance = cached_backward;
1318
+ out->score = score;
1319
+ if (BUCKET_SWEEP == 1) {
1320
+ buckets[key] = (uint32_t)cur_ix;
1321
+ return;
1322
+ } else {
1323
+ best_len = len;
1324
+ best_score = score;
1325
+ compare_char = data[cur_ix_masked + len];
1326
+ }
1327
+ }
1328
+ }
1329
+ }
1330
+ }
1331
+ if (BUCKET_SWEEP == 1) {
1332
+ size_t backward;
1333
+ size_t len;
1334
+ /* Only one to look for, don't bother to prepare for a loop. */
1335
+ prev_ix = buckets[key];
1336
+ buckets[key] = (uint32_t)cur_ix;
1337
+ backward = cur_ix - prev_ix;
1338
+ prev_ix &= (uint32_t)ring_buffer_mask;
1339
+ if (compare_char != data[prev_ix + best_len_in]) {
1340
+ return;
1341
+ }
1342
+ if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
1343
+ return;
1344
+ }
1345
+ len = FindMatchLengthWithLimit(&data[prev_ix],
1346
+ &data[cur_ix_masked],
1347
+ max_length);
1348
+ if (len >= 4) {
1349
+ const score_t score = BackwardReferenceScore(len, backward);
1350
+ if (best_score < score) {
1351
+ out->len = len;
1352
+ out->distance = backward;
1353
+ out->score = score;
1354
+ return;
1355
+ }
1356
+ }
1357
+ } else {
1358
+ size_t keys[BUCKET_SWEEP];
1359
+ size_t i;
1360
+ for (i = 0; i < BUCKET_SWEEP; ++i) {
1361
+ keys[i] = (key + (i << 3)) & BUCKET_MASK;
1362
+ }
1363
+ key_out = keys[(cur_ix & BUCKET_SWEEP_MASK) >> 3];
1364
+ for (i = 0; i < BUCKET_SWEEP; ++i) {
1365
+ size_t len;
1366
+ size_t backward;
1367
+ prev_ix = buckets[keys[i]];
1368
+ backward = cur_ix - prev_ix;
1369
+ prev_ix &= (uint32_t)ring_buffer_mask;
1370
+ if (compare_char != data[prev_ix + best_len]) {
1371
+ continue;
1372
+ }
1373
+ if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
1374
+ continue;
1375
+ }
1376
+ len = FindMatchLengthWithLimit(&data[prev_ix],
1377
+ &data[cur_ix_masked],
1378
+ max_length);
1379
+ if (len >= 4) {
1380
+ const score_t score = BackwardReferenceScore(len, backward);
1381
+ if (best_score < score) {
1382
+ best_len = len;
1383
+ out->len = len;
1384
+ compare_char = data[cur_ix_masked + len];
1385
+ best_score = score;
1386
+ out->score = score;
1387
+ out->distance = backward;
1388
+ }
1389
+ }
1390
+ }
1391
+ }
1392
+ if (USE_DICTIONARY && min_score == out->score) {
1393
+ SearchInStaticDictionary(dictionary,
1394
+ self->common, &data[cur_ix_masked], max_length, dictionary_distance,
1395
+ max_distance, out, BROTLI_TRUE);
1396
+ }
1397
+ if (BUCKET_SWEEP != 1) {
1398
+ buckets[key_out] = (uint32_t)cur_ix;
1399
+ }
1400
+ }
1401
+
1402
+ #undef BUCKET_SWEEP_MASK
1403
+ #undef BUCKET_SWEEP
1404
+ #undef BUCKET_MASK
1405
+ #undef BUCKET_SIZE
1406
+
1407
+ #undef HashLongestMatchQuickly
1408
+ #undef USE_DICTIONARY
1409
+ #undef HASH_LEN
1410
+ #undef BUCKET_SWEEP_BITS
1411
+ #undef BUCKET_BITS
1412
+ #undef HASHER
1413
+
1414
+ #define HASHER() H5
1415
+ /* NOLINT(build/header_guard) */
1416
+ /* Copyright 2010 Google Inc. All Rights Reserved.
1417
+
1418
+ Distributed under MIT license.
1419
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
1420
+ */
1421
+
1422
+ /* template parameters: FN */
1423
+
1424
+ /* A (forgetful) hash table to the data seen by the compressor, to
1425
+ help create backward references to previous data.
1426
+
1427
+ This is a hash map of fixed size (bucket_size_) to a ring buffer of
1428
+ fixed size (block_size_). The ring buffer contains the last block_size_
1429
+ index positions of the given hash key in the compressed data. */
1430
+
1431
+ #define HashLongestMatch HASHER()
1432
+
1433
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }
1434
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }
1435
+
1436
+ /* HashBytes is the function that chooses the bucket to place the address in. */
1437
+ static uint32_t FN(HashBytes)(
1438
+ const uint8_t* BROTLI_RESTRICT data, const int shift) {
1439
+ uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
1440
+ /* The higher bits contain more mixture from the multiplication,
1441
+ so we take our results from there. */
1442
+ return (uint32_t)(h >> shift);
1443
+ }
1444
+
1445
+ typedef struct HashLongestMatch {
1446
+ /* Number of hash buckets. */
1447
+ size_t bucket_size_;
1448
+ /* Only block_size_ newest backward references are kept,
1449
+ and the older are forgotten. */
1450
+ size_t block_size_;
1451
+ /* Left-shift for computing hash bucket index from hash value. */
1452
+ int hash_shift_;
1453
+ /* Mask for accessing entries in a block (in a ring-buffer manner). */
1454
+ uint32_t block_mask_;
1455
+
1456
+ int block_bits_;
1457
+ int num_last_distances_to_check_;
1458
+
1459
+ /* Shortcuts. */
1460
+ HasherCommon* common_;
1461
+
1462
+ /* --- Dynamic size members --- */
1463
+
1464
+ /* Number of entries in a particular bucket. */
1465
+ uint16_t* num_; /* uint16_t[bucket_size]; */
1466
+
1467
+ /* Buckets containing block_size_ of backward references. */
1468
+ uint32_t* buckets_; /* uint32_t[bucket_size * block_size]; */
1469
+ } HashLongestMatch;
1470
+
1471
+ static void FN(Initialize)(
1472
+ HasherCommon* common, HashLongestMatch* BROTLI_RESTRICT self,
1473
+ const BrotliEncoderParams* params) {
1474
+ self->common_ = common;
1475
+
1476
+ BROTLI_UNUSED(params);
1477
+ self->hash_shift_ = 32 - common->params.bucket_bits;
1478
+ self->bucket_size_ = (size_t)1 << common->params.bucket_bits;
1479
+ self->block_size_ = (size_t)1 << common->params.block_bits;
1480
+ self->block_mask_ = (uint32_t)(self->block_size_ - 1);
1481
+ self->num_ = (uint16_t*)common->extra[0];
1482
+ self->buckets_ = (uint32_t*)common->extra[1];
1483
+ self->block_bits_ = common->params.block_bits;
1484
+ self->num_last_distances_to_check_ =
1485
+ common->params.num_last_distances_to_check;
1486
+ }
1487
+
1488
+ static void FN(Prepare)(
1489
+ HashLongestMatch* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
1490
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
1491
+ uint16_t* BROTLI_RESTRICT num = self->num_;
1492
+ /* Partial preparation is 100 times slower (per socket). */
1493
+ size_t partial_prepare_threshold = self->bucket_size_ >> 6;
1494
+ if (one_shot && input_size <= partial_prepare_threshold) {
1495
+ size_t i;
1496
+ for (i = 0; i < input_size; ++i) {
1497
+ const uint32_t key = FN(HashBytes)(&data[i], self->hash_shift_);
1498
+ num[key] = 0;
1499
+ }
1500
+ } else {
1501
+ memset(num, 0, self->bucket_size_ * sizeof(num[0]));
1502
+ }
1503
+ }
1504
+
1505
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
1506
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
1507
+ size_t input_size, size_t* alloc_size) {
1508
+ size_t bucket_size = (size_t)1 << params->hasher.bucket_bits;
1509
+ size_t block_size = (size_t)1 << params->hasher.block_bits;
1510
+ BROTLI_UNUSED(one_shot);
1511
+ BROTLI_UNUSED(input_size);
1512
+ alloc_size[0] = sizeof(uint16_t) * bucket_size;
1513
+ alloc_size[1] = sizeof(uint32_t) * bucket_size * block_size;
1514
+ }
1515
+
1516
+ /* Look at 4 bytes at &data[ix & mask].
1517
+ Compute a hash from these, and store the value of ix at that position. */
1518
+ static BROTLI_INLINE void FN(Store)(
1519
+ HashLongestMatch* BROTLI_RESTRICT self, const uint8_t* BROTLI_RESTRICT data,
1520
+ const size_t mask, const size_t ix) {
1521
+ const uint32_t key = FN(HashBytes)(&data[ix & mask], self->hash_shift_);
1522
+ const size_t minor_ix = self->num_[key] & self->block_mask_;
1523
+ const size_t offset = minor_ix + (key << self->block_bits_);
1524
+ self->buckets_[offset] = (uint32_t)ix;
1525
+ ++self->num_[key];
1526
+ }
1527
+
1528
+ static BROTLI_INLINE void FN(StoreRange)(HashLongestMatch* BROTLI_RESTRICT self,
1529
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
1530
+ const size_t ix_start, const size_t ix_end) {
1531
+ size_t i;
1532
+ for (i = ix_start; i < ix_end; ++i) {
1533
+ FN(Store)(self, data, mask, i);
1534
+ }
1535
+ }
1536
+
1537
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
1538
+ HashLongestMatch* BROTLI_RESTRICT self,
1539
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
1540
+ size_t ringbuffer_mask) {
1541
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
1542
+ /* Prepare the hashes for three last bytes of the last write.
1543
+ These could not be calculated before, since they require knowledge
1544
+ of both the previous and the current block. */
1545
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3);
1546
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2);
1547
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1);
1548
+ }
1549
+ }
1550
+
1551
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
1552
+ HashLongestMatch* BROTLI_RESTRICT self,
1553
+ int* BROTLI_RESTRICT distance_cache) {
1554
+ PrepareDistanceCache(distance_cache, self->num_last_distances_to_check_);
1555
+ }
1556
+
1557
+ /* Find a longest backward match of &data[cur_ix] up to the length of
1558
+ max_length and stores the position cur_ix in the hash table.
1559
+
1560
+ REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache
1561
+ values; if this method is invoked repeatedly with the same distance
1562
+ cache values, it is enough to invoke FN(PrepareDistanceCache) once.
1563
+
1564
+ Does not look for matches longer than max_length.
1565
+ Does not look for matches further away than max_backward.
1566
+ Writes the best match into |out|.
1567
+ |out|->score is updated only if a better match is found. */
1568
+ static BROTLI_INLINE void FN(FindLongestMatch)(
1569
+ HashLongestMatch* BROTLI_RESTRICT self,
1570
+ const BrotliEncoderDictionary* dictionary,
1571
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
1572
+ const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
1573
+ const size_t max_length, const size_t max_backward,
1574
+ const size_t dictionary_distance, const size_t max_distance,
1575
+ HasherSearchResult* BROTLI_RESTRICT out) {
1576
+ uint16_t* BROTLI_RESTRICT num = self->num_;
1577
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
1578
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
1579
+ /* Don't accept a short copy from far away. */
1580
+ score_t min_score = out->score;
1581
+ score_t best_score = out->score;
1582
+ size_t best_len = out->len;
1583
+ size_t i;
1584
+ out->len = 0;
1585
+ out->len_code_delta = 0;
1586
+ /* Try last distance first. */
1587
+ for (i = 0; i < (size_t)self->num_last_distances_to_check_; ++i) {
1588
+ const size_t backward = (size_t)distance_cache[i];
1589
+ size_t prev_ix = (size_t)(cur_ix - backward);
1590
+ if (prev_ix >= cur_ix) {
1591
+ continue;
1592
+ }
1593
+ if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
1594
+ continue;
1595
+ }
1596
+ prev_ix &= ring_buffer_mask;
1597
+
1598
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
1599
+ prev_ix + best_len > ring_buffer_mask ||
1600
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
1601
+ continue;
1602
+ }
1603
+ {
1604
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
1605
+ &data[cur_ix_masked],
1606
+ max_length);
1607
+ if (len >= 3 || (len == 2 && i < 2)) {
1608
+ /* Comparing for >= 2 does not change the semantics, but just saves for
1609
+ a few unnecessary binary logarithms in backward reference score,
1610
+ since we are not interested in such short matches. */
1611
+ score_t score = BackwardReferenceScoreUsingLastDistance(len);
1612
+ if (best_score < score) {
1613
+ if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
1614
+ if (best_score < score) {
1615
+ best_score = score;
1616
+ best_len = len;
1617
+ out->len = best_len;
1618
+ out->distance = backward;
1619
+ out->score = best_score;
1620
+ }
1621
+ }
1622
+ }
1623
+ }
1624
+ }
1625
+ {
1626
+ const uint32_t key =
1627
+ FN(HashBytes)(&data[cur_ix_masked], self->hash_shift_);
1628
+ uint32_t* BROTLI_RESTRICT bucket = &buckets[key << self->block_bits_];
1629
+ const size_t down =
1630
+ (num[key] > self->block_size_) ? (num[key] - self->block_size_) : 0u;
1631
+ for (i = num[key]; i > down;) {
1632
+ size_t prev_ix = bucket[--i & self->block_mask_];
1633
+ const size_t backward = cur_ix - prev_ix;
1634
+ if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
1635
+ break;
1636
+ }
1637
+ prev_ix &= ring_buffer_mask;
1638
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
1639
+ prev_ix + best_len > ring_buffer_mask ||
1640
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
1641
+ continue;
1642
+ }
1643
+ {
1644
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
1645
+ &data[cur_ix_masked],
1646
+ max_length);
1647
+ if (len >= 4) {
1648
+ /* Comparing for >= 3 does not change the semantics, but just saves
1649
+ for a few unnecessary binary logarithms in backward reference
1650
+ score, since we are not interested in such short matches. */
1651
+ score_t score = BackwardReferenceScore(len, backward);
1652
+ if (best_score < score) {
1653
+ best_score = score;
1654
+ best_len = len;
1655
+ out->len = best_len;
1656
+ out->distance = backward;
1657
+ out->score = best_score;
1658
+ }
1659
+ }
1660
+ }
1661
+ }
1662
+ bucket[num[key] & self->block_mask_] = (uint32_t)cur_ix;
1663
+ ++num[key];
1664
+ }
1665
+ if (min_score == out->score) {
1666
+ SearchInStaticDictionary(dictionary,
1667
+ self->common_, &data[cur_ix_masked], max_length, dictionary_distance,
1668
+ max_distance, out, BROTLI_FALSE);
1669
+ }
1670
+ }
1671
+
1672
+ #undef HashLongestMatch
1673
+ #undef HASHER
1674
+
1675
+ #define HASHER() H6
1676
+ /* NOLINT(build/header_guard) */
1677
+ /* Copyright 2010 Google Inc. All Rights Reserved.
1678
+
1679
+ Distributed under MIT license.
1680
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
1681
+ */
1682
+
1683
+ /* template parameters: FN */
1684
+
1685
+ /* A (forgetful) hash table to the data seen by the compressor, to
1686
+ help create backward references to previous data.
1687
+
1688
+ This is a hash map of fixed size (bucket_size_) to a ring buffer of
1689
+ fixed size (block_size_). The ring buffer contains the last block_size_
1690
+ index positions of the given hash key in the compressed data. */
1691
+
1692
+ #define HashLongestMatch HASHER()
1693
+
1694
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; }
1695
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }
1696
+
1697
+ /* HashBytes is the function that chooses the bucket to place the address in. */
1698
+ static BROTLI_INLINE size_t FN(HashBytes)(const uint8_t* BROTLI_RESTRICT data,
1699
+ uint64_t hash_mul) {
1700
+ const uint64_t h = BROTLI_UNALIGNED_LOAD64LE(data) * hash_mul;
1701
+ /* The higher bits contain more mixture from the multiplication,
1702
+ so we take our results from there. */
1703
+ return (size_t)(h >> (64 - 15));
1704
+ }
1705
+
1706
+ typedef struct HashLongestMatch {
1707
+ /* Number of hash buckets. */
1708
+ size_t bucket_size_;
1709
+ /* Only block_size_ newest backward references are kept,
1710
+ and the older are forgotten. */
1711
+ size_t block_size_;
1712
+ /* Hash multiplier tuned to match length. */
1713
+ uint64_t hash_mul_;
1714
+ /* Mask for accessing entries in a block (in a ring-buffer manner). */
1715
+ uint32_t block_mask_;
1716
+
1717
+ int block_bits_;
1718
+ int num_last_distances_to_check_;
1719
+
1720
+ /* Shortcuts. */
1721
+ HasherCommon* common_;
1722
+
1723
+ /* --- Dynamic size members --- */
1724
+
1725
+ /* Number of entries in a particular bucket. */
1726
+ uint16_t* num_; /* uint16_t[bucket_size]; */
1727
+
1728
+ /* Buckets containing block_size_ of backward references. */
1729
+ uint32_t* buckets_; /* uint32_t[bucket_size * block_size]; */
1730
+ } HashLongestMatch;
1731
+
1732
+ static void FN(Initialize)(
1733
+ HasherCommon* common, HashLongestMatch* BROTLI_RESTRICT self,
1734
+ const BrotliEncoderParams* params) {
1735
+ self->common_ = common;
1736
+
1737
+ BROTLI_UNUSED(params);
1738
+ self->hash_mul_ = kHashMul64 << (64 - 5 * 8);
1739
+ BROTLI_DCHECK(common->params.bucket_bits == 15);
1740
+ self->bucket_size_ = (size_t)1 << common->params.bucket_bits;
1741
+ self->block_bits_ = common->params.block_bits;
1742
+ self->block_size_ = (size_t)1 << common->params.block_bits;
1743
+ self->block_mask_ = (uint32_t)(self->block_size_ - 1);
1744
+ self->num_last_distances_to_check_ =
1745
+ common->params.num_last_distances_to_check;
1746
+ self->num_ = (uint16_t*)common->extra[0];
1747
+ self->buckets_ = (uint32_t*)common->extra[1];
1748
+ }
1749
+
1750
+ static void FN(Prepare)(
1751
+ HashLongestMatch* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
1752
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
1753
+ uint16_t* BROTLI_RESTRICT num = self->num_;
1754
+ /* Partial preparation is 100 times slower (per socket). */
1755
+ size_t partial_prepare_threshold = self->bucket_size_ >> 6;
1756
+ if (one_shot && input_size <= partial_prepare_threshold) {
1757
+ size_t i;
1758
+ for (i = 0; i < input_size; ++i) {
1759
+ const size_t key = FN(HashBytes)(&data[i], self->hash_mul_);
1760
+ num[key] = 0;
1761
+ }
1762
+ } else {
1763
+ memset(num, 0, self->bucket_size_ * sizeof(num[0]));
1764
+ }
1765
+ }
1766
+
1767
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
1768
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
1769
+ size_t input_size, size_t* alloc_size) {
1770
+ size_t bucket_size = (size_t)1 << params->hasher.bucket_bits;
1771
+ size_t block_size = (size_t)1 << params->hasher.block_bits;
1772
+ BROTLI_UNUSED(one_shot);
1773
+ BROTLI_UNUSED(input_size);
1774
+ alloc_size[0] = sizeof(uint16_t) * bucket_size;
1775
+ alloc_size[1] = sizeof(uint32_t) * bucket_size * block_size;
1776
+ }
1777
+
1778
+ /* Look at 4 bytes at &data[ix & mask].
1779
+ Compute a hash from these, and store the value of ix at that position. */
1780
+ static BROTLI_INLINE void FN(Store)(
1781
+ HashLongestMatch* BROTLI_RESTRICT self, const uint8_t* BROTLI_RESTRICT data,
1782
+ const size_t mask, const size_t ix) {
1783
+ uint16_t* BROTLI_RESTRICT num = self->num_;
1784
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
1785
+ const size_t key = FN(HashBytes)(&data[ix & mask], self->hash_mul_);
1786
+ const size_t minor_ix = num[key] & self->block_mask_;
1787
+ const size_t offset = minor_ix + (key << self->block_bits_);
1788
+ ++num[key];
1789
+ buckets[offset] = (uint32_t)ix;
1790
+ }
1791
+
1792
+ static BROTLI_INLINE void FN(StoreRange)(HashLongestMatch* BROTLI_RESTRICT self,
1793
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
1794
+ const size_t ix_start, const size_t ix_end) {
1795
+ size_t i;
1796
+ for (i = ix_start; i < ix_end; ++i) {
1797
+ FN(Store)(self, data, mask, i);
1798
+ }
1799
+ }
1800
+
1801
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
1802
+ HashLongestMatch* BROTLI_RESTRICT self,
1803
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
1804
+ size_t ringbuffer_mask) {
1805
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
1806
+ /* Prepare the hashes for three last bytes of the last write.
1807
+ These could not be calculated before, since they require knowledge
1808
+ of both the previous and the current block. */
1809
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3);
1810
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2);
1811
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1);
1812
+ }
1813
+ }
1814
+
1815
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
1816
+ HashLongestMatch* BROTLI_RESTRICT self,
1817
+ int* BROTLI_RESTRICT distance_cache) {
1818
+ PrepareDistanceCache(distance_cache, self->num_last_distances_to_check_);
1819
+ }
1820
+
1821
+ /* Find a longest backward match of &data[cur_ix] up to the length of
1822
+ max_length and stores the position cur_ix in the hash table.
1823
+
1824
+ REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache
1825
+ values; if this method is invoked repeatedly with the same distance
1826
+ cache values, it is enough to invoke FN(PrepareDistanceCache) once.
1827
+
1828
+ Does not look for matches longer than max_length.
1829
+ Does not look for matches further away than max_backward.
1830
+ Writes the best match into |out|.
1831
+ |out|->score is updated only if a better match is found. */
1832
+ static BROTLI_INLINE void FN(FindLongestMatch)(
1833
+ HashLongestMatch* BROTLI_RESTRICT self,
1834
+ const BrotliEncoderDictionary* dictionary,
1835
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
1836
+ const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
1837
+ const size_t max_length, const size_t max_backward,
1838
+ const size_t dictionary_distance, const size_t max_distance,
1839
+ HasherSearchResult* BROTLI_RESTRICT out) {
1840
+ uint16_t* BROTLI_RESTRICT num = self->num_;
1841
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
1842
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
1843
+ /* Don't accept a short copy from far away. */
1844
+ score_t min_score = out->score;
1845
+ score_t best_score = out->score;
1846
+ size_t best_len = out->len;
1847
+ size_t i;
1848
+ out->len = 0;
1849
+ out->len_code_delta = 0;
1850
+ /* Try last distance first. */
1851
+ for (i = 0; i < (size_t)self->num_last_distances_to_check_; ++i) {
1852
+ const size_t backward = (size_t)distance_cache[i];
1853
+ size_t prev_ix = (size_t)(cur_ix - backward);
1854
+ if (prev_ix >= cur_ix) {
1855
+ continue;
1856
+ }
1857
+ if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
1858
+ continue;
1859
+ }
1860
+ prev_ix &= ring_buffer_mask;
1861
+
1862
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
1863
+ prev_ix + best_len > ring_buffer_mask ||
1864
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
1865
+ continue;
1866
+ }
1867
+ {
1868
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
1869
+ &data[cur_ix_masked],
1870
+ max_length);
1871
+ if (len >= 3 || (len == 2 && i < 2)) {
1872
+ /* Comparing for >= 2 does not change the semantics, but just saves for
1873
+ a few unnecessary binary logarithms in backward reference score,
1874
+ since we are not interested in such short matches. */
1875
+ score_t score = BackwardReferenceScoreUsingLastDistance(len);
1876
+ if (best_score < score) {
1877
+ if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
1878
+ if (best_score < score) {
1879
+ best_score = score;
1880
+ best_len = len;
1881
+ out->len = best_len;
1882
+ out->distance = backward;
1883
+ out->score = best_score;
1884
+ }
1885
+ }
1886
+ }
1887
+ }
1888
+ }
1889
+ {
1890
+ const size_t key = FN(HashBytes)(&data[cur_ix_masked], self->hash_mul_);
1891
+ uint32_t* BROTLI_RESTRICT bucket = &buckets[key << self->block_bits_];
1892
+ const size_t down =
1893
+ (num[key] > self->block_size_) ?
1894
+ (num[key] - self->block_size_) : 0u;
1895
+ const uint32_t first4 = BrotliUnalignedRead32(data + cur_ix_masked);
1896
+ const size_t max_length_m4 = max_length - 4;
1897
+ i = num[key];
1898
+ for (; i > down;) {
1899
+ size_t prev_ix = bucket[--i & self->block_mask_];
1900
+ uint32_t current4;
1901
+ const size_t backward = cur_ix - prev_ix;
1902
+ if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
1903
+ break;
1904
+ }
1905
+ prev_ix &= ring_buffer_mask;
1906
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
1907
+ prev_ix + best_len > ring_buffer_mask ||
1908
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
1909
+ continue;
1910
+ }
1911
+ current4 = BrotliUnalignedRead32(data + prev_ix);
1912
+ if (first4 != current4) continue;
1913
+ {
1914
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix + 4],
1915
+ &data[cur_ix_masked + 4],
1916
+ max_length_m4) + 4;
1917
+ const score_t score = BackwardReferenceScore(len, backward);
1918
+ if (best_score < score) {
1919
+ best_score = score;
1920
+ best_len = len;
1921
+ out->len = best_len;
1922
+ out->distance = backward;
1923
+ out->score = best_score;
1924
+ }
1925
+ }
1926
+ }
1927
+ bucket[num[key] & self->block_mask_] = (uint32_t)cur_ix;
1928
+ ++num[key];
1929
+ }
1930
+ if (min_score == out->score) {
1931
+ SearchInStaticDictionary(dictionary,
1932
+ self->common_, &data[cur_ix_masked], max_length, dictionary_distance,
1933
+ max_distance, out, BROTLI_FALSE);
1934
+ }
1935
+ }
1936
+
1937
+ #undef HashLongestMatch
1938
+ #undef HASHER
1939
+
1940
+ #define BUCKET_BITS 15
1941
+
1942
+ #define NUM_LAST_DISTANCES_TO_CHECK 4
1943
+ #define NUM_BANKS 1
1944
+ #define BANK_BITS 16
1945
+ #define HASHER() H40
1946
+ /* NOLINT(build/header_guard) */
1947
+ /* Copyright 2016 Google Inc. All Rights Reserved.
1948
+
1949
+ Distributed under MIT license.
1950
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
1951
+ */
1952
+
1953
+ /* template parameters: FN, BUCKET_BITS, NUM_BANKS, BANK_BITS,
1954
+ NUM_LAST_DISTANCES_TO_CHECK */
1955
+
1956
+ /* A (forgetful) hash table to the data seen by the compressor, to
1957
+ help create backward references to previous data.
1958
+
1959
+ Hashes are stored in chains which are bucketed to groups. Group of chains
1960
+ share a storage "bank". When more than "bank size" chain nodes are added,
1961
+ oldest nodes are replaced; this way several chains may share a tail. */
1962
+
1963
+ #define HashForgetfulChain HASHER()
1964
+
1965
+ #define BANK_SIZE (1 << BANK_BITS)
1966
+
1967
+ /* Number of hash buckets. */
1968
+ #define BUCKET_SIZE (1 << BUCKET_BITS)
1969
+
1970
+ #define CAPPED_CHAINS 0
1971
+
1972
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }
1973
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }
1974
+
1975
+ /* HashBytes is the function that chooses the bucket to place the address in.*/
1976
+ static BROTLI_INLINE size_t FN(HashBytes)(const uint8_t* BROTLI_RESTRICT data) {
1977
+ const uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
1978
+ /* The higher bits contain more mixture from the multiplication,
1979
+ so we take our results from there. */
1980
+ return h >> (32 - BUCKET_BITS);
1981
+ }
1982
+
1983
+ typedef struct FN(Slot) {
1984
+ uint16_t delta;
1985
+ uint16_t next;
1986
+ } FN(Slot);
1987
+
1988
+ typedef struct FN(Bank) {
1989
+ FN(Slot) slots[BANK_SIZE];
1990
+ } FN(Bank);
1991
+
1992
+ typedef struct HashForgetfulChain {
1993
+ uint16_t free_slot_idx[NUM_BANKS]; /* Up to 1KiB. Move to dynamic? */
1994
+ size_t max_hops;
1995
+
1996
+ /* Shortcuts. */
1997
+ void* extra[2];
1998
+ HasherCommon* common;
1999
+
2000
+ /* --- Dynamic size members --- */
2001
+
2002
+ /* uint32_t addr[BUCKET_SIZE]; */
2003
+
2004
+ /* uint16_t head[BUCKET_SIZE]; */
2005
+
2006
+ /* Truncated hash used for quick rejection of "distance cache" candidates. */
2007
+ /* uint8_t tiny_hash[65536];*/
2008
+
2009
+ /* FN(Bank) banks[NUM_BANKS]; */
2010
+ } HashForgetfulChain;
2011
+
2012
+ static uint32_t* FN(Addr)(void* extra) {
2013
+ return (uint32_t*)extra;
2014
+ }
2015
+
2016
+ static uint16_t* FN(Head)(void* extra) {
2017
+ return (uint16_t*)(&FN(Addr)(extra)[BUCKET_SIZE]);
2018
+ }
2019
+
2020
+ static uint8_t* FN(TinyHash)(void* extra) {
2021
+ return (uint8_t*)(&FN(Head)(extra)[BUCKET_SIZE]);
2022
+ }
2023
+
2024
+ static FN(Bank)* FN(Banks)(void* extra) {
2025
+ return (FN(Bank)*)(extra);
2026
+ }
2027
+
2028
+ static void FN(Initialize)(
2029
+ HasherCommon* common, HashForgetfulChain* BROTLI_RESTRICT self,
2030
+ const BrotliEncoderParams* params) {
2031
+ self->common = common;
2032
+ self->extra[0] = common->extra[0];
2033
+ self->extra[1] = common->extra[1];
2034
+
2035
+ self->max_hops = (params->quality > 6 ? 7u : 8u) << (params->quality - 4);
2036
+ }
2037
+
2038
+ static void FN(Prepare)(
2039
+ HashForgetfulChain* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
2040
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
2041
+ uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
2042
+ uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
2043
+ uint8_t* BROTLI_RESTRICT tiny_hash = FN(TinyHash)(self->extra[0]);
2044
+ /* Partial preparation is 100 times slower (per socket). */
2045
+ size_t partial_prepare_threshold = BUCKET_SIZE >> 6;
2046
+ if (one_shot && input_size <= partial_prepare_threshold) {
2047
+ size_t i;
2048
+ for (i = 0; i < input_size; ++i) {
2049
+ size_t bucket = FN(HashBytes)(&data[i]);
2050
+ /* See InitEmpty comment. */
2051
+ addr[bucket] = 0xCCCCCCCC;
2052
+ head[bucket] = 0xCCCC;
2053
+ }
2054
+ } else {
2055
+ /* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position
2056
+ processed by hasher never reaches 3GB + 64M; this makes all new chains
2057
+ to be terminated after the first node. */
2058
+ memset(addr, 0xCC, sizeof(uint32_t) * BUCKET_SIZE);
2059
+ memset(head, 0, sizeof(uint16_t) * BUCKET_SIZE);
2060
+ }
2061
+ memset(tiny_hash, 0, sizeof(uint8_t) * 65536);
2062
+ memset(self->free_slot_idx, 0, sizeof(self->free_slot_idx));
2063
+ }
2064
+
2065
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
2066
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
2067
+ size_t input_size, size_t* alloc_size) {
2068
+ BROTLI_UNUSED(params);
2069
+ BROTLI_UNUSED(one_shot);
2070
+ BROTLI_UNUSED(input_size);
2071
+ alloc_size[0] = sizeof(uint32_t) * BUCKET_SIZE +
2072
+ sizeof(uint16_t) * BUCKET_SIZE + sizeof(uint8_t) * 65536;
2073
+ alloc_size[1] = sizeof(FN(Bank)) * NUM_BANKS;
2074
+ }
2075
+
2076
+ /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend
2077
+ node to corresponding chain; also update tiny_hash for current position. */
2078
+ static BROTLI_INLINE void FN(Store)(HashForgetfulChain* BROTLI_RESTRICT self,
2079
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
2080
+ uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
2081
+ uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
2082
+ uint8_t* BROTLI_RESTRICT tiny_hash = FN(TinyHash)(self->extra[0]);
2083
+ FN(Bank)* BROTLI_RESTRICT banks = FN(Banks)(self->extra[1]);
2084
+ const size_t key = FN(HashBytes)(&data[ix & mask]);
2085
+ const size_t bank = key & (NUM_BANKS - 1);
2086
+ const size_t idx = self->free_slot_idx[bank]++ & (BANK_SIZE - 1);
2087
+ size_t delta = ix - addr[key];
2088
+ tiny_hash[(uint16_t)ix] = (uint8_t)key;
2089
+ if (delta > 0xFFFF) delta = CAPPED_CHAINS ? 0 : 0xFFFF;
2090
+ banks[bank].slots[idx].delta = (uint16_t)delta;
2091
+ banks[bank].slots[idx].next = head[key];
2092
+ addr[key] = (uint32_t)ix;
2093
+ head[key] = (uint16_t)idx;
2094
+ }
2095
+
2096
+ static BROTLI_INLINE void FN(StoreRange)(
2097
+ HashForgetfulChain* BROTLI_RESTRICT self,
2098
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
2099
+ const size_t ix_start, const size_t ix_end) {
2100
+ size_t i;
2101
+ for (i = ix_start; i < ix_end; ++i) {
2102
+ FN(Store)(self, data, mask, i);
2103
+ }
2104
+ }
2105
+
2106
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
2107
+ HashForgetfulChain* BROTLI_RESTRICT self,
2108
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
2109
+ size_t ring_buffer_mask) {
2110
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
2111
+ /* Prepare the hashes for three last bytes of the last write.
2112
+ These could not be calculated before, since they require knowledge
2113
+ of both the previous and the current block. */
2114
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 3);
2115
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 2);
2116
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 1);
2117
+ }
2118
+ }
2119
+
2120
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
2121
+ HashForgetfulChain* BROTLI_RESTRICT self,
2122
+ int* BROTLI_RESTRICT distance_cache) {
2123
+ BROTLI_UNUSED(self);
2124
+ PrepareDistanceCache(distance_cache, NUM_LAST_DISTANCES_TO_CHECK);
2125
+ }
2126
+
2127
+ /* Find a longest backward match of &data[cur_ix] up to the length of
2128
+ max_length and stores the position cur_ix in the hash table.
2129
+
2130
+ REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache
2131
+ values; if this method is invoked repeatedly with the same distance
2132
+ cache values, it is enough to invoke FN(PrepareDistanceCache) once.
2133
+
2134
+ Does not look for matches longer than max_length.
2135
+ Does not look for matches further away than max_backward.
2136
+ Writes the best match into |out|.
2137
+ |out|->score is updated only if a better match is found. */
2138
+ static BROTLI_INLINE void FN(FindLongestMatch)(
2139
+ HashForgetfulChain* BROTLI_RESTRICT self,
2140
+ const BrotliEncoderDictionary* dictionary,
2141
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
2142
+ const int* BROTLI_RESTRICT distance_cache,
2143
+ const size_t cur_ix, const size_t max_length, const size_t max_backward,
2144
+ const size_t dictionary_distance, const size_t max_distance,
2145
+ HasherSearchResult* BROTLI_RESTRICT out) {
2146
+ uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
2147
+ uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
2148
+ uint8_t* BROTLI_RESTRICT tiny_hashes = FN(TinyHash)(self->extra[0]);
2149
+ FN(Bank)* BROTLI_RESTRICT banks = FN(Banks)(self->extra[1]);
2150
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
2151
+ /* Don't accept a short copy from far away. */
2152
+ score_t min_score = out->score;
2153
+ score_t best_score = out->score;
2154
+ size_t best_len = out->len;
2155
+ size_t i;
2156
+ const size_t key = FN(HashBytes)(&data[cur_ix_masked]);
2157
+ const uint8_t tiny_hash = (uint8_t)(key);
2158
+ out->len = 0;
2159
+ out->len_code_delta = 0;
2160
+ /* Try last distance first. */
2161
+ for (i = 0; i < NUM_LAST_DISTANCES_TO_CHECK; ++i) {
2162
+ const size_t backward = (size_t)distance_cache[i];
2163
+ size_t prev_ix = (cur_ix - backward);
2164
+ /* For distance code 0 we want to consider 2-byte matches. */
2165
+ if (i > 0 && tiny_hashes[(uint16_t)prev_ix] != tiny_hash) continue;
2166
+ if (prev_ix >= cur_ix || backward > max_backward) {
2167
+ continue;
2168
+ }
2169
+ prev_ix &= ring_buffer_mask;
2170
+ {
2171
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
2172
+ &data[cur_ix_masked],
2173
+ max_length);
2174
+ if (len >= 2) {
2175
+ score_t score = BackwardReferenceScoreUsingLastDistance(len);
2176
+ if (best_score < score) {
2177
+ if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
2178
+ if (best_score < score) {
2179
+ best_score = score;
2180
+ best_len = len;
2181
+ out->len = best_len;
2182
+ out->distance = backward;
2183
+ out->score = best_score;
2184
+ }
2185
+ }
2186
+ }
2187
+ }
2188
+ }
2189
+ {
2190
+ const size_t bank = key & (NUM_BANKS - 1);
2191
+ size_t backward = 0;
2192
+ size_t hops = self->max_hops;
2193
+ size_t delta = cur_ix - addr[key];
2194
+ size_t slot = head[key];
2195
+ while (hops--) {
2196
+ size_t prev_ix;
2197
+ size_t last = slot;
2198
+ backward += delta;
2199
+ if (backward > max_backward || (CAPPED_CHAINS && !delta)) break;
2200
+ prev_ix = (cur_ix - backward) & ring_buffer_mask;
2201
+ slot = banks[bank].slots[last].next;
2202
+ delta = banks[bank].slots[last].delta;
2203
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
2204
+ prev_ix + best_len > ring_buffer_mask ||
2205
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
2206
+ continue;
2207
+ }
2208
+ {
2209
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
2210
+ &data[cur_ix_masked],
2211
+ max_length);
2212
+ if (len >= 4) {
2213
+ /* Comparing for >= 3 does not change the semantics, but just saves
2214
+ for a few unnecessary binary logarithms in backward reference
2215
+ score, since we are not interested in such short matches. */
2216
+ score_t score = BackwardReferenceScore(len, backward);
2217
+ if (best_score < score) {
2218
+ best_score = score;
2219
+ best_len = len;
2220
+ out->len = best_len;
2221
+ out->distance = backward;
2222
+ out->score = best_score;
2223
+ }
2224
+ }
2225
+ }
2226
+ }
2227
+ FN(Store)(self, data, ring_buffer_mask, cur_ix);
2228
+ }
2229
+ if (out->score == min_score) {
2230
+ SearchInStaticDictionary(dictionary,
2231
+ self->common, &data[cur_ix_masked], max_length, dictionary_distance,
2232
+ max_distance, out, BROTLI_FALSE);
2233
+ }
2234
+ }
2235
+
2236
+ #undef BANK_SIZE
2237
+ #undef BUCKET_SIZE
2238
+ #undef CAPPED_CHAINS
2239
+
2240
+ #undef HashForgetfulChain
2241
+ #undef HASHER
2242
+ #undef NUM_LAST_DISTANCES_TO_CHECK
2243
+
2244
+ #define NUM_LAST_DISTANCES_TO_CHECK 10
2245
+ #define HASHER() H41
2246
+ /* NOLINT(build/header_guard) */
2247
+ /* Copyright 2016 Google Inc. All Rights Reserved.
2248
+
2249
+ Distributed under MIT license.
2250
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
2251
+ */
2252
+
2253
+ /* template parameters: FN, BUCKET_BITS, NUM_BANKS, BANK_BITS,
2254
+ NUM_LAST_DISTANCES_TO_CHECK */
2255
+
2256
+ /* A (forgetful) hash table to the data seen by the compressor, to
2257
+ help create backward references to previous data.
2258
+
2259
+ Hashes are stored in chains which are bucketed to groups. Group of chains
2260
+ share a storage "bank". When more than "bank size" chain nodes are added,
2261
+ oldest nodes are replaced; this way several chains may share a tail. */
2262
+
2263
+ #define HashForgetfulChain HASHER()
2264
+
2265
+ #define BANK_SIZE (1 << BANK_BITS)
2266
+
2267
+ /* Number of hash buckets. */
2268
+ #define BUCKET_SIZE (1 << BUCKET_BITS)
2269
+
2270
+ #define CAPPED_CHAINS 0
2271
+
2272
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }
2273
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }
2274
+
2275
+ /* HashBytes is the function that chooses the bucket to place the address in.*/
2276
+ static BROTLI_INLINE size_t FN(HashBytes)(const uint8_t* BROTLI_RESTRICT data) {
2277
+ const uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
2278
+ /* The higher bits contain more mixture from the multiplication,
2279
+ so we take our results from there. */
2280
+ return h >> (32 - BUCKET_BITS);
2281
+ }
2282
+
2283
+ typedef struct FN(Slot) {
2284
+ uint16_t delta;
2285
+ uint16_t next;
2286
+ } FN(Slot);
2287
+
2288
+ typedef struct FN(Bank) {
2289
+ FN(Slot) slots[BANK_SIZE];
2290
+ } FN(Bank);
2291
+
2292
+ typedef struct HashForgetfulChain {
2293
+ uint16_t free_slot_idx[NUM_BANKS]; /* Up to 1KiB. Move to dynamic? */
2294
+ size_t max_hops;
2295
+
2296
+ /* Shortcuts. */
2297
+ void* extra[2];
2298
+ HasherCommon* common;
2299
+
2300
+ /* --- Dynamic size members --- */
2301
+
2302
+ /* uint32_t addr[BUCKET_SIZE]; */
2303
+
2304
+ /* uint16_t head[BUCKET_SIZE]; */
2305
+
2306
+ /* Truncated hash used for quick rejection of "distance cache" candidates. */
2307
+ /* uint8_t tiny_hash[65536];*/
2308
+
2309
+ /* FN(Bank) banks[NUM_BANKS]; */
2310
+ } HashForgetfulChain;
2311
+
2312
+ static uint32_t* FN(Addr)(void* extra) {
2313
+ return (uint32_t*)extra;
2314
+ }
2315
+
2316
+ static uint16_t* FN(Head)(void* extra) {
2317
+ return (uint16_t*)(&FN(Addr)(extra)[BUCKET_SIZE]);
2318
+ }
2319
+
2320
+ static uint8_t* FN(TinyHash)(void* extra) {
2321
+ return (uint8_t*)(&FN(Head)(extra)[BUCKET_SIZE]);
2322
+ }
2323
+
2324
+ static FN(Bank)* FN(Banks)(void* extra) {
2325
+ return (FN(Bank)*)(extra);
2326
+ }
2327
+
2328
+ static void FN(Initialize)(
2329
+ HasherCommon* common, HashForgetfulChain* BROTLI_RESTRICT self,
2330
+ const BrotliEncoderParams* params) {
2331
+ self->common = common;
2332
+ self->extra[0] = common->extra[0];
2333
+ self->extra[1] = common->extra[1];
2334
+
2335
+ self->max_hops = (params->quality > 6 ? 7u : 8u) << (params->quality - 4);
2336
+ }
2337
+
2338
+ static void FN(Prepare)(
2339
+ HashForgetfulChain* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
2340
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
2341
+ uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
2342
+ uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
2343
+ uint8_t* BROTLI_RESTRICT tiny_hash = FN(TinyHash)(self->extra[0]);
2344
+ /* Partial preparation is 100 times slower (per socket). */
2345
+ size_t partial_prepare_threshold = BUCKET_SIZE >> 6;
2346
+ if (one_shot && input_size <= partial_prepare_threshold) {
2347
+ size_t i;
2348
+ for (i = 0; i < input_size; ++i) {
2349
+ size_t bucket = FN(HashBytes)(&data[i]);
2350
+ /* See InitEmpty comment. */
2351
+ addr[bucket] = 0xCCCCCCCC;
2352
+ head[bucket] = 0xCCCC;
2353
+ }
2354
+ } else {
2355
+ /* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position
2356
+ processed by hasher never reaches 3GB + 64M; this makes all new chains
2357
+ to be terminated after the first node. */
2358
+ memset(addr, 0xCC, sizeof(uint32_t) * BUCKET_SIZE);
2359
+ memset(head, 0, sizeof(uint16_t) * BUCKET_SIZE);
2360
+ }
2361
+ memset(tiny_hash, 0, sizeof(uint8_t) * 65536);
2362
+ memset(self->free_slot_idx, 0, sizeof(self->free_slot_idx));
2363
+ }
2364
+
2365
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
2366
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
2367
+ size_t input_size, size_t* alloc_size) {
2368
+ BROTLI_UNUSED(params);
2369
+ BROTLI_UNUSED(one_shot);
2370
+ BROTLI_UNUSED(input_size);
2371
+ alloc_size[0] = sizeof(uint32_t) * BUCKET_SIZE +
2372
+ sizeof(uint16_t) * BUCKET_SIZE + sizeof(uint8_t) * 65536;
2373
+ alloc_size[1] = sizeof(FN(Bank)) * NUM_BANKS;
2374
+ }
2375
+
2376
+ /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend
2377
+ node to corresponding chain; also update tiny_hash for current position. */
2378
+ static BROTLI_INLINE void FN(Store)(HashForgetfulChain* BROTLI_RESTRICT self,
2379
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
2380
+ uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
2381
+ uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
2382
+ uint8_t* BROTLI_RESTRICT tiny_hash = FN(TinyHash)(self->extra[0]);
2383
+ FN(Bank)* BROTLI_RESTRICT banks = FN(Banks)(self->extra[1]);
2384
+ const size_t key = FN(HashBytes)(&data[ix & mask]);
2385
+ const size_t bank = key & (NUM_BANKS - 1);
2386
+ const size_t idx = self->free_slot_idx[bank]++ & (BANK_SIZE - 1);
2387
+ size_t delta = ix - addr[key];
2388
+ tiny_hash[(uint16_t)ix] = (uint8_t)key;
2389
+ if (delta > 0xFFFF) delta = CAPPED_CHAINS ? 0 : 0xFFFF;
2390
+ banks[bank].slots[idx].delta = (uint16_t)delta;
2391
+ banks[bank].slots[idx].next = head[key];
2392
+ addr[key] = (uint32_t)ix;
2393
+ head[key] = (uint16_t)idx;
2394
+ }
2395
+
2396
+ static BROTLI_INLINE void FN(StoreRange)(
2397
+ HashForgetfulChain* BROTLI_RESTRICT self,
2398
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
2399
+ const size_t ix_start, const size_t ix_end) {
2400
+ size_t i;
2401
+ for (i = ix_start; i < ix_end; ++i) {
2402
+ FN(Store)(self, data, mask, i);
2403
+ }
2404
+ }
2405
+
2406
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
2407
+ HashForgetfulChain* BROTLI_RESTRICT self,
2408
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
2409
+ size_t ring_buffer_mask) {
2410
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
2411
+ /* Prepare the hashes for three last bytes of the last write.
2412
+ These could not be calculated before, since they require knowledge
2413
+ of both the previous and the current block. */
2414
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 3);
2415
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 2);
2416
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 1);
2417
+ }
2418
+ }
2419
+
2420
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
2421
+ HashForgetfulChain* BROTLI_RESTRICT self,
2422
+ int* BROTLI_RESTRICT distance_cache) {
2423
+ BROTLI_UNUSED(self);
2424
+ PrepareDistanceCache(distance_cache, NUM_LAST_DISTANCES_TO_CHECK);
2425
+ }
2426
+
2427
+ /* Find a longest backward match of &data[cur_ix] up to the length of
2428
+ max_length and stores the position cur_ix in the hash table.
2429
+
2430
+ REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache
2431
+ values; if this method is invoked repeatedly with the same distance
2432
+ cache values, it is enough to invoke FN(PrepareDistanceCache) once.
2433
+
2434
+ Does not look for matches longer than max_length.
2435
+ Does not look for matches further away than max_backward.
2436
+ Writes the best match into |out|.
2437
+ |out|->score is updated only if a better match is found. */
2438
+ static BROTLI_INLINE void FN(FindLongestMatch)(
2439
+ HashForgetfulChain* BROTLI_RESTRICT self,
2440
+ const BrotliEncoderDictionary* dictionary,
2441
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
2442
+ const int* BROTLI_RESTRICT distance_cache,
2443
+ const size_t cur_ix, const size_t max_length, const size_t max_backward,
2444
+ const size_t dictionary_distance, const size_t max_distance,
2445
+ HasherSearchResult* BROTLI_RESTRICT out) {
2446
+ uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
2447
+ uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
2448
+ uint8_t* BROTLI_RESTRICT tiny_hashes = FN(TinyHash)(self->extra[0]);
2449
+ FN(Bank)* BROTLI_RESTRICT banks = FN(Banks)(self->extra[1]);
2450
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
2451
+ /* Don't accept a short copy from far away. */
2452
+ score_t min_score = out->score;
2453
+ score_t best_score = out->score;
2454
+ size_t best_len = out->len;
2455
+ size_t i;
2456
+ const size_t key = FN(HashBytes)(&data[cur_ix_masked]);
2457
+ const uint8_t tiny_hash = (uint8_t)(key);
2458
+ out->len = 0;
2459
+ out->len_code_delta = 0;
2460
+ /* Try last distance first. */
2461
+ for (i = 0; i < NUM_LAST_DISTANCES_TO_CHECK; ++i) {
2462
+ const size_t backward = (size_t)distance_cache[i];
2463
+ size_t prev_ix = (cur_ix - backward);
2464
+ /* For distance code 0 we want to consider 2-byte matches. */
2465
+ if (i > 0 && tiny_hashes[(uint16_t)prev_ix] != tiny_hash) continue;
2466
+ if (prev_ix >= cur_ix || backward > max_backward) {
2467
+ continue;
2468
+ }
2469
+ prev_ix &= ring_buffer_mask;
2470
+ {
2471
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
2472
+ &data[cur_ix_masked],
2473
+ max_length);
2474
+ if (len >= 2) {
2475
+ score_t score = BackwardReferenceScoreUsingLastDistance(len);
2476
+ if (best_score < score) {
2477
+ if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
2478
+ if (best_score < score) {
2479
+ best_score = score;
2480
+ best_len = len;
2481
+ out->len = best_len;
2482
+ out->distance = backward;
2483
+ out->score = best_score;
2484
+ }
2485
+ }
2486
+ }
2487
+ }
2488
+ }
2489
+ {
2490
+ const size_t bank = key & (NUM_BANKS - 1);
2491
+ size_t backward = 0;
2492
+ size_t hops = self->max_hops;
2493
+ size_t delta = cur_ix - addr[key];
2494
+ size_t slot = head[key];
2495
+ while (hops--) {
2496
+ size_t prev_ix;
2497
+ size_t last = slot;
2498
+ backward += delta;
2499
+ if (backward > max_backward || (CAPPED_CHAINS && !delta)) break;
2500
+ prev_ix = (cur_ix - backward) & ring_buffer_mask;
2501
+ slot = banks[bank].slots[last].next;
2502
+ delta = banks[bank].slots[last].delta;
2503
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
2504
+ prev_ix + best_len > ring_buffer_mask ||
2505
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
2506
+ continue;
2507
+ }
2508
+ {
2509
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
2510
+ &data[cur_ix_masked],
2511
+ max_length);
2512
+ if (len >= 4) {
2513
+ /* Comparing for >= 3 does not change the semantics, but just saves
2514
+ for a few unnecessary binary logarithms in backward reference
2515
+ score, since we are not interested in such short matches. */
2516
+ score_t score = BackwardReferenceScore(len, backward);
2517
+ if (best_score < score) {
2518
+ best_score = score;
2519
+ best_len = len;
2520
+ out->len = best_len;
2521
+ out->distance = backward;
2522
+ out->score = best_score;
2523
+ }
2524
+ }
2525
+ }
2526
+ }
2527
+ FN(Store)(self, data, ring_buffer_mask, cur_ix);
2528
+ }
2529
+ if (out->score == min_score) {
2530
+ SearchInStaticDictionary(dictionary,
2531
+ self->common, &data[cur_ix_masked], max_length, dictionary_distance,
2532
+ max_distance, out, BROTLI_FALSE);
2533
+ }
2534
+ }
2535
+
2536
+ #undef BANK_SIZE
2537
+ #undef BUCKET_SIZE
2538
+ #undef CAPPED_CHAINS
2539
+
2540
+ #undef HashForgetfulChain
2541
+ #undef HASHER
2542
+ #undef NUM_LAST_DISTANCES_TO_CHECK
2543
+ #undef NUM_BANKS
2544
+ #undef BANK_BITS
2545
+
2546
+ #define NUM_LAST_DISTANCES_TO_CHECK 16
2547
+ #define NUM_BANKS 512
2548
+ #define BANK_BITS 9
2549
+ #define HASHER() H42
2550
+ /* NOLINT(build/header_guard) */
2551
+ /* Copyright 2016 Google Inc. All Rights Reserved.
2552
+
2553
+ Distributed under MIT license.
2554
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
2555
+ */
2556
+
2557
+ /* template parameters: FN, BUCKET_BITS, NUM_BANKS, BANK_BITS,
2558
+ NUM_LAST_DISTANCES_TO_CHECK */
2559
+
2560
+ /* A (forgetful) hash table to the data seen by the compressor, to
2561
+ help create backward references to previous data.
2562
+
2563
+ Hashes are stored in chains which are bucketed to groups. Group of chains
2564
+ share a storage "bank". When more than "bank size" chain nodes are added,
2565
+ oldest nodes are replaced; this way several chains may share a tail. */
2566
+
2567
+ #define HashForgetfulChain HASHER()
2568
+
2569
+ #define BANK_SIZE (1 << BANK_BITS)
2570
+
2571
+ /* Number of hash buckets. */
2572
+ #define BUCKET_SIZE (1 << BUCKET_BITS)
2573
+
2574
+ #define CAPPED_CHAINS 0
2575
+
2576
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }
2577
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }
2578
+
2579
+ /* HashBytes is the function that chooses the bucket to place the address in.*/
2580
+ static BROTLI_INLINE size_t FN(HashBytes)(const uint8_t* BROTLI_RESTRICT data) {
2581
+ const uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
2582
+ /* The higher bits contain more mixture from the multiplication,
2583
+ so we take our results from there. */
2584
+ return h >> (32 - BUCKET_BITS);
2585
+ }
2586
+
2587
+ typedef struct FN(Slot) {
2588
+ uint16_t delta;
2589
+ uint16_t next;
2590
+ } FN(Slot);
2591
+
2592
+ typedef struct FN(Bank) {
2593
+ FN(Slot) slots[BANK_SIZE];
2594
+ } FN(Bank);
2595
+
2596
+ typedef struct HashForgetfulChain {
2597
+ uint16_t free_slot_idx[NUM_BANKS]; /* Up to 1KiB. Move to dynamic? */
2598
+ size_t max_hops;
2599
+
2600
+ /* Shortcuts. */
2601
+ void* extra[2];
2602
+ HasherCommon* common;
2603
+
2604
+ /* --- Dynamic size members --- */
2605
+
2606
+ /* uint32_t addr[BUCKET_SIZE]; */
2607
+
2608
+ /* uint16_t head[BUCKET_SIZE]; */
2609
+
2610
+ /* Truncated hash used for quick rejection of "distance cache" candidates. */
2611
+ /* uint8_t tiny_hash[65536];*/
2612
+
2613
+ /* FN(Bank) banks[NUM_BANKS]; */
2614
+ } HashForgetfulChain;
2615
+
2616
+ static uint32_t* FN(Addr)(void* extra) {
2617
+ return (uint32_t*)extra;
2618
+ }
2619
+
2620
+ static uint16_t* FN(Head)(void* extra) {
2621
+ return (uint16_t*)(&FN(Addr)(extra)[BUCKET_SIZE]);
2622
+ }
2623
+
2624
+ static uint8_t* FN(TinyHash)(void* extra) {
2625
+ return (uint8_t*)(&FN(Head)(extra)[BUCKET_SIZE]);
2626
+ }
2627
+
2628
+ static FN(Bank)* FN(Banks)(void* extra) {
2629
+ return (FN(Bank)*)(extra);
2630
+ }
2631
+
2632
+ static void FN(Initialize)(
2633
+ HasherCommon* common, HashForgetfulChain* BROTLI_RESTRICT self,
2634
+ const BrotliEncoderParams* params) {
2635
+ self->common = common;
2636
+ self->extra[0] = common->extra[0];
2637
+ self->extra[1] = common->extra[1];
2638
+
2639
+ self->max_hops = (params->quality > 6 ? 7u : 8u) << (params->quality - 4);
2640
+ }
2641
+
2642
+ static void FN(Prepare)(
2643
+ HashForgetfulChain* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
2644
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
2645
+ uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
2646
+ uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
2647
+ uint8_t* BROTLI_RESTRICT tiny_hash = FN(TinyHash)(self->extra[0]);
2648
+ /* Partial preparation is 100 times slower (per socket). */
2649
+ size_t partial_prepare_threshold = BUCKET_SIZE >> 6;
2650
+ if (one_shot && input_size <= partial_prepare_threshold) {
2651
+ size_t i;
2652
+ for (i = 0; i < input_size; ++i) {
2653
+ size_t bucket = FN(HashBytes)(&data[i]);
2654
+ /* See InitEmpty comment. */
2655
+ addr[bucket] = 0xCCCCCCCC;
2656
+ head[bucket] = 0xCCCC;
2657
+ }
2658
+ } else {
2659
+ /* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position
2660
+ processed by hasher never reaches 3GB + 64M; this makes all new chains
2661
+ to be terminated after the first node. */
2662
+ memset(addr, 0xCC, sizeof(uint32_t) * BUCKET_SIZE);
2663
+ memset(head, 0, sizeof(uint16_t) * BUCKET_SIZE);
2664
+ }
2665
+ memset(tiny_hash, 0, sizeof(uint8_t) * 65536);
2666
+ memset(self->free_slot_idx, 0, sizeof(self->free_slot_idx));
2667
+ }
2668
+
2669
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
2670
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
2671
+ size_t input_size, size_t* alloc_size) {
2672
+ BROTLI_UNUSED(params);
2673
+ BROTLI_UNUSED(one_shot);
2674
+ BROTLI_UNUSED(input_size);
2675
+ alloc_size[0] = sizeof(uint32_t) * BUCKET_SIZE +
2676
+ sizeof(uint16_t) * BUCKET_SIZE + sizeof(uint8_t) * 65536;
2677
+ alloc_size[1] = sizeof(FN(Bank)) * NUM_BANKS;
2678
+ }
2679
+
2680
+ /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend
2681
+ node to corresponding chain; also update tiny_hash for current position. */
2682
+ static BROTLI_INLINE void FN(Store)(HashForgetfulChain* BROTLI_RESTRICT self,
2683
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
2684
+ uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
2685
+ uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
2686
+ uint8_t* BROTLI_RESTRICT tiny_hash = FN(TinyHash)(self->extra[0]);
2687
+ FN(Bank)* BROTLI_RESTRICT banks = FN(Banks)(self->extra[1]);
2688
+ const size_t key = FN(HashBytes)(&data[ix & mask]);
2689
+ const size_t bank = key & (NUM_BANKS - 1);
2690
+ const size_t idx = self->free_slot_idx[bank]++ & (BANK_SIZE - 1);
2691
+ size_t delta = ix - addr[key];
2692
+ tiny_hash[(uint16_t)ix] = (uint8_t)key;
2693
+ if (delta > 0xFFFF) delta = CAPPED_CHAINS ? 0 : 0xFFFF;
2694
+ banks[bank].slots[idx].delta = (uint16_t)delta;
2695
+ banks[bank].slots[idx].next = head[key];
2696
+ addr[key] = (uint32_t)ix;
2697
+ head[key] = (uint16_t)idx;
2698
+ }
2699
+
2700
+ static BROTLI_INLINE void FN(StoreRange)(
2701
+ HashForgetfulChain* BROTLI_RESTRICT self,
2702
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
2703
+ const size_t ix_start, const size_t ix_end) {
2704
+ size_t i;
2705
+ for (i = ix_start; i < ix_end; ++i) {
2706
+ FN(Store)(self, data, mask, i);
2707
+ }
2708
+ }
2709
+
2710
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
2711
+ HashForgetfulChain* BROTLI_RESTRICT self,
2712
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
2713
+ size_t ring_buffer_mask) {
2714
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
2715
+ /* Prepare the hashes for three last bytes of the last write.
2716
+ These could not be calculated before, since they require knowledge
2717
+ of both the previous and the current block. */
2718
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 3);
2719
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 2);
2720
+ FN(Store)(self, ringbuffer, ring_buffer_mask, position - 1);
2721
+ }
2722
+ }
2723
+
2724
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
2725
+ HashForgetfulChain* BROTLI_RESTRICT self,
2726
+ int* BROTLI_RESTRICT distance_cache) {
2727
+ BROTLI_UNUSED(self);
2728
+ PrepareDistanceCache(distance_cache, NUM_LAST_DISTANCES_TO_CHECK);
2729
+ }
2730
+
2731
+ /* Find a longest backward match of &data[cur_ix] up to the length of
2732
+ max_length and stores the position cur_ix in the hash table.
2733
+
2734
+ REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache
2735
+ values; if this method is invoked repeatedly with the same distance
2736
+ cache values, it is enough to invoke FN(PrepareDistanceCache) once.
2737
+
2738
+ Does not look for matches longer than max_length.
2739
+ Does not look for matches further away than max_backward.
2740
+ Writes the best match into |out|.
2741
+ |out|->score is updated only if a better match is found. */
2742
+ static BROTLI_INLINE void FN(FindLongestMatch)(
2743
+ HashForgetfulChain* BROTLI_RESTRICT self,
2744
+ const BrotliEncoderDictionary* dictionary,
2745
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
2746
+ const int* BROTLI_RESTRICT distance_cache,
2747
+ const size_t cur_ix, const size_t max_length, const size_t max_backward,
2748
+ const size_t dictionary_distance, const size_t max_distance,
2749
+ HasherSearchResult* BROTLI_RESTRICT out) {
2750
+ uint32_t* BROTLI_RESTRICT addr = FN(Addr)(self->extra[0]);
2751
+ uint16_t* BROTLI_RESTRICT head = FN(Head)(self->extra[0]);
2752
+ uint8_t* BROTLI_RESTRICT tiny_hashes = FN(TinyHash)(self->extra[0]);
2753
+ FN(Bank)* BROTLI_RESTRICT banks = FN(Banks)(self->extra[1]);
2754
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
2755
+ /* Don't accept a short copy from far away. */
2756
+ score_t min_score = out->score;
2757
+ score_t best_score = out->score;
2758
+ size_t best_len = out->len;
2759
+ size_t i;
2760
+ const size_t key = FN(HashBytes)(&data[cur_ix_masked]);
2761
+ const uint8_t tiny_hash = (uint8_t)(key);
2762
+ out->len = 0;
2763
+ out->len_code_delta = 0;
2764
+ /* Try last distance first. */
2765
+ for (i = 0; i < NUM_LAST_DISTANCES_TO_CHECK; ++i) {
2766
+ const size_t backward = (size_t)distance_cache[i];
2767
+ size_t prev_ix = (cur_ix - backward);
2768
+ /* For distance code 0 we want to consider 2-byte matches. */
2769
+ if (i > 0 && tiny_hashes[(uint16_t)prev_ix] != tiny_hash) continue;
2770
+ if (prev_ix >= cur_ix || backward > max_backward) {
2771
+ continue;
2772
+ }
2773
+ prev_ix &= ring_buffer_mask;
2774
+ {
2775
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
2776
+ &data[cur_ix_masked],
2777
+ max_length);
2778
+ if (len >= 2) {
2779
+ score_t score = BackwardReferenceScoreUsingLastDistance(len);
2780
+ if (best_score < score) {
2781
+ if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
2782
+ if (best_score < score) {
2783
+ best_score = score;
2784
+ best_len = len;
2785
+ out->len = best_len;
2786
+ out->distance = backward;
2787
+ out->score = best_score;
2788
+ }
2789
+ }
2790
+ }
2791
+ }
2792
+ }
2793
+ {
2794
+ const size_t bank = key & (NUM_BANKS - 1);
2795
+ size_t backward = 0;
2796
+ size_t hops = self->max_hops;
2797
+ size_t delta = cur_ix - addr[key];
2798
+ size_t slot = head[key];
2799
+ while (hops--) {
2800
+ size_t prev_ix;
2801
+ size_t last = slot;
2802
+ backward += delta;
2803
+ if (backward > max_backward || (CAPPED_CHAINS && !delta)) break;
2804
+ prev_ix = (cur_ix - backward) & ring_buffer_mask;
2805
+ slot = banks[bank].slots[last].next;
2806
+ delta = banks[bank].slots[last].delta;
2807
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
2808
+ prev_ix + best_len > ring_buffer_mask ||
2809
+ data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
2810
+ continue;
2811
+ }
2812
+ {
2813
+ const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
2814
+ &data[cur_ix_masked],
2815
+ max_length);
2816
+ if (len >= 4) {
2817
+ /* Comparing for >= 3 does not change the semantics, but just saves
2818
+ for a few unnecessary binary logarithms in backward reference
2819
+ score, since we are not interested in such short matches. */
2820
+ score_t score = BackwardReferenceScore(len, backward);
2821
+ if (best_score < score) {
2822
+ best_score = score;
2823
+ best_len = len;
2824
+ out->len = best_len;
2825
+ out->distance = backward;
2826
+ out->score = best_score;
2827
+ }
2828
+ }
2829
+ }
2830
+ }
2831
+ FN(Store)(self, data, ring_buffer_mask, cur_ix);
2832
+ }
2833
+ if (out->score == min_score) {
2834
+ SearchInStaticDictionary(dictionary,
2835
+ self->common, &data[cur_ix_masked], max_length, dictionary_distance,
2836
+ max_distance, out, BROTLI_FALSE);
2837
+ }
2838
+ }
2839
+
2840
+ #undef BANK_SIZE
2841
+ #undef BUCKET_SIZE
2842
+ #undef CAPPED_CHAINS
2843
+
2844
+ #undef HashForgetfulChain
2845
+ #undef HASHER
2846
+ #undef NUM_LAST_DISTANCES_TO_CHECK
2847
+ #undef NUM_BANKS
2848
+ #undef BANK_BITS
2849
+
2850
+ #undef BUCKET_BITS
2851
+
2852
+ #define HASHER() H54
2853
+ #define BUCKET_BITS 20
2854
+ #define BUCKET_SWEEP_BITS 2
2855
+ #define HASH_LEN 7
2856
+ #define USE_DICTIONARY 0
2857
+ /* NOLINT(build/header_guard) */
2858
+ /* Copyright 2010 Google Inc. All Rights Reserved.
2859
+
2860
+ Distributed under MIT license.
2861
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
2862
+ */
2863
+
2864
+ /* template parameters: FN, BUCKET_BITS, BUCKET_SWEEP_BITS, HASH_LEN,
2865
+ USE_DICTIONARY
2866
+ */
2867
+
2868
+ #define HashLongestMatchQuickly HASHER()
2869
+
2870
+ #define BUCKET_SIZE (1 << BUCKET_BITS)
2871
+ #define BUCKET_MASK (BUCKET_SIZE - 1)
2872
+ #define BUCKET_SWEEP (1 << BUCKET_SWEEP_BITS)
2873
+ #define BUCKET_SWEEP_MASK ((BUCKET_SWEEP - 1) << 3)
2874
+
2875
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; }
2876
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }
2877
+
2878
+ /* HashBytes is the function that chooses the bucket to place
2879
+ the address in. The HashLongestMatch and HashLongestMatchQuickly
2880
+ classes have separate, different implementations of hashing. */
2881
+ static uint32_t FN(HashBytes)(const uint8_t* data) {
2882
+ const uint64_t h = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8 * HASH_LEN)) *
2883
+ kHashMul64);
2884
+ /* The higher bits contain more mixture from the multiplication,
2885
+ so we take our results from there. */
2886
+ return (uint32_t)(h >> (64 - BUCKET_BITS));
2887
+ }
2888
+
2889
+ /* A (forgetful) hash table to the data seen by the compressor, to
2890
+ help create backward references to previous data.
2891
+
2892
+ This is a hash map of fixed size (BUCKET_SIZE). */
2893
+ typedef struct HashLongestMatchQuickly {
2894
+ /* Shortcuts. */
2895
+ HasherCommon* common;
2896
+
2897
+ /* --- Dynamic size members --- */
2898
+
2899
+ uint32_t* buckets_; /* uint32_t[BUCKET_SIZE]; */
2900
+ } HashLongestMatchQuickly;
2901
+
2902
+ static void FN(Initialize)(
2903
+ HasherCommon* common, HashLongestMatchQuickly* BROTLI_RESTRICT self,
2904
+ const BrotliEncoderParams* params) {
2905
+ self->common = common;
2906
+
2907
+ BROTLI_UNUSED(params);
2908
+ self->buckets_ = (uint32_t*)common->extra[0];
2909
+ }
2910
+
2911
+ static void FN(Prepare)(
2912
+ HashLongestMatchQuickly* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
2913
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
2914
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
2915
+ /* Partial preparation is 100 times slower (per socket). */
2916
+ size_t partial_prepare_threshold = BUCKET_SIZE >> 5;
2917
+ if (one_shot && input_size <= partial_prepare_threshold) {
2918
+ size_t i;
2919
+ for (i = 0; i < input_size; ++i) {
2920
+ const uint32_t key = FN(HashBytes)(&data[i]);
2921
+ if (BUCKET_SWEEP == 1) {
2922
+ buckets[key] = 0;
2923
+ } else {
2924
+ uint32_t j;
2925
+ for (j = 0; j < BUCKET_SWEEP; ++j) {
2926
+ buckets[(key + (j << 3)) & BUCKET_MASK] = 0;
2927
+ }
2928
+ }
2929
+ }
2930
+ } else {
2931
+ /* It is not strictly necessary to fill this buffer here, but
2932
+ not filling will make the results of the compression stochastic
2933
+ (but correct). This is because random data would cause the
2934
+ system to find accidentally good backward references here and there. */
2935
+ memset(buckets, 0, sizeof(uint32_t) * BUCKET_SIZE);
2936
+ }
2937
+ }
2938
+
2939
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
2940
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
2941
+ size_t input_size, size_t* alloc_size) {
2942
+ BROTLI_UNUSED(params);
2943
+ BROTLI_UNUSED(one_shot);
2944
+ BROTLI_UNUSED(input_size);
2945
+ alloc_size[0] = sizeof(uint32_t) * BUCKET_SIZE;
2946
+ }
2947
+
2948
+ /* Look at 5 bytes at &data[ix & mask].
2949
+ Compute a hash from these, and store the value somewhere within
2950
+ [ix .. ix+3]. */
2951
+ static BROTLI_INLINE void FN(Store)(
2952
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
2953
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
2954
+ const uint32_t key = FN(HashBytes)(&data[ix & mask]);
2955
+ if (BUCKET_SWEEP == 1) {
2956
+ self->buckets_[key] = (uint32_t)ix;
2957
+ } else {
2958
+ /* Wiggle the value with the bucket sweep range. */
2959
+ const uint32_t off = ix & BUCKET_SWEEP_MASK;
2960
+ self->buckets_[(key + off) & BUCKET_MASK] = (uint32_t)ix;
2961
+ }
2962
+ }
2963
+
2964
+ static BROTLI_INLINE void FN(StoreRange)(
2965
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
2966
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
2967
+ const size_t ix_start, const size_t ix_end) {
2968
+ size_t i;
2969
+ for (i = ix_start; i < ix_end; ++i) {
2970
+ FN(Store)(self, data, mask, i);
2971
+ }
2972
+ }
2973
+
2974
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
2975
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
2976
+ size_t num_bytes, size_t position,
2977
+ const uint8_t* ringbuffer, size_t ringbuffer_mask) {
2978
+ if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
2979
+ /* Prepare the hashes for three last bytes of the last write.
2980
+ These could not be calculated before, since they require knowledge
2981
+ of both the previous and the current block. */
2982
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3);
2983
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2);
2984
+ FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1);
2985
+ }
2986
+ }
2987
+
2988
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
2989
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
2990
+ int* BROTLI_RESTRICT distance_cache) {
2991
+ BROTLI_UNUSED(self);
2992
+ BROTLI_UNUSED(distance_cache);
2993
+ }
2994
+
2995
+ /* Find a longest backward match of &data[cur_ix & ring_buffer_mask]
2996
+ up to the length of max_length and stores the position cur_ix in the
2997
+ hash table.
2998
+
2999
+ Does not look for matches longer than max_length.
3000
+ Does not look for matches further away than max_backward.
3001
+ Writes the best match into |out|.
3002
+ |out|->score is updated only if a better match is found. */
3003
+ static BROTLI_INLINE void FN(FindLongestMatch)(
3004
+ HashLongestMatchQuickly* BROTLI_RESTRICT self,
3005
+ const BrotliEncoderDictionary* dictionary,
3006
+ const uint8_t* BROTLI_RESTRICT data,
3007
+ const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
3008
+ const size_t cur_ix, const size_t max_length, const size_t max_backward,
3009
+ const size_t dictionary_distance, const size_t max_distance,
3010
+ HasherSearchResult* BROTLI_RESTRICT out) {
3011
+ uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
3012
+ const size_t best_len_in = out->len;
3013
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
3014
+ int compare_char = data[cur_ix_masked + best_len_in];
3015
+ size_t key = FN(HashBytes)(&data[cur_ix_masked]);
3016
+ size_t key_out;
3017
+ score_t min_score = out->score;
3018
+ score_t best_score = out->score;
3019
+ size_t best_len = best_len_in;
3020
+ size_t cached_backward = (size_t)distance_cache[0];
3021
+ size_t prev_ix = cur_ix - cached_backward;
3022
+ out->len_code_delta = 0;
3023
+ if (prev_ix < cur_ix) {
3024
+ prev_ix &= (uint32_t)ring_buffer_mask;
3025
+ if (compare_char == data[prev_ix + best_len]) {
3026
+ const size_t len = FindMatchLengthWithLimit(
3027
+ &data[prev_ix], &data[cur_ix_masked], max_length);
3028
+ if (len >= 4) {
3029
+ const score_t score = BackwardReferenceScoreUsingLastDistance(len);
3030
+ if (best_score < score) {
3031
+ out->len = len;
3032
+ out->distance = cached_backward;
3033
+ out->score = score;
3034
+ if (BUCKET_SWEEP == 1) {
3035
+ buckets[key] = (uint32_t)cur_ix;
3036
+ return;
3037
+ } else {
3038
+ best_len = len;
3039
+ best_score = score;
3040
+ compare_char = data[cur_ix_masked + len];
3041
+ }
3042
+ }
3043
+ }
3044
+ }
3045
+ }
3046
+ if (BUCKET_SWEEP == 1) {
3047
+ size_t backward;
3048
+ size_t len;
3049
+ /* Only one to look for, don't bother to prepare for a loop. */
3050
+ prev_ix = buckets[key];
3051
+ buckets[key] = (uint32_t)cur_ix;
3052
+ backward = cur_ix - prev_ix;
3053
+ prev_ix &= (uint32_t)ring_buffer_mask;
3054
+ if (compare_char != data[prev_ix + best_len_in]) {
3055
+ return;
3056
+ }
3057
+ if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
3058
+ return;
3059
+ }
3060
+ len = FindMatchLengthWithLimit(&data[prev_ix],
3061
+ &data[cur_ix_masked],
3062
+ max_length);
3063
+ if (len >= 4) {
3064
+ const score_t score = BackwardReferenceScore(len, backward);
3065
+ if (best_score < score) {
3066
+ out->len = len;
3067
+ out->distance = backward;
3068
+ out->score = score;
3069
+ return;
3070
+ }
3071
+ }
3072
+ } else {
3073
+ size_t keys[BUCKET_SWEEP];
3074
+ size_t i;
3075
+ for (i = 0; i < BUCKET_SWEEP; ++i) {
3076
+ keys[i] = (key + (i << 3)) & BUCKET_MASK;
3077
+ }
3078
+ key_out = keys[(cur_ix & BUCKET_SWEEP_MASK) >> 3];
3079
+ for (i = 0; i < BUCKET_SWEEP; ++i) {
3080
+ size_t len;
3081
+ size_t backward;
3082
+ prev_ix = buckets[keys[i]];
3083
+ backward = cur_ix - prev_ix;
3084
+ prev_ix &= (uint32_t)ring_buffer_mask;
3085
+ if (compare_char != data[prev_ix + best_len]) {
3086
+ continue;
3087
+ }
3088
+ if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
3089
+ continue;
3090
+ }
3091
+ len = FindMatchLengthWithLimit(&data[prev_ix],
3092
+ &data[cur_ix_masked],
3093
+ max_length);
3094
+ if (len >= 4) {
3095
+ const score_t score = BackwardReferenceScore(len, backward);
3096
+ if (best_score < score) {
3097
+ best_len = len;
3098
+ out->len = len;
3099
+ compare_char = data[cur_ix_masked + len];
3100
+ best_score = score;
3101
+ out->score = score;
3102
+ out->distance = backward;
3103
+ }
3104
+ }
3105
+ }
3106
+ }
3107
+ if (USE_DICTIONARY && min_score == out->score) {
3108
+ SearchInStaticDictionary(dictionary,
3109
+ self->common, &data[cur_ix_masked], max_length, dictionary_distance,
3110
+ max_distance, out, BROTLI_TRUE);
3111
+ }
3112
+ if (BUCKET_SWEEP != 1) {
3113
+ buckets[key_out] = (uint32_t)cur_ix;
3114
+ }
3115
+ }
3116
+
3117
+ #undef BUCKET_SWEEP_MASK
3118
+ #undef BUCKET_SWEEP
3119
+ #undef BUCKET_MASK
3120
+ #undef BUCKET_SIZE
3121
+
3122
+ #undef HashLongestMatchQuickly
3123
+ #undef USE_DICTIONARY
3124
+ #undef HASH_LEN
3125
+ #undef BUCKET_SWEEP_BITS
3126
+ #undef BUCKET_BITS
3127
+ #undef HASHER
3128
+
3129
+ /* fast large window hashers */
3130
+
3131
+ #define HASHER() HROLLING_FAST
3132
+ #define CHUNKLEN 32
3133
+ #define JUMP 4
3134
+ #define NUMBUCKETS 16777216
3135
+ #define MASK ((NUMBUCKETS * 64) - 1)
3136
+ /* NOLINT(build/header_guard) */
3137
+ /* Copyright 2018 Google Inc. All Rights Reserved.
3138
+
3139
+ Distributed under MIT license.
3140
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
3141
+ */
3142
+
3143
+ /* template parameters: FN, JUMP, NUMBUCKETS, MASK, CHUNKLEN */
3144
+ /* NUMBUCKETS / (MASK + 1) = probability of storing and using hash code. */
3145
+ /* JUMP = skip bytes for speedup */
3146
+
3147
+ /* Rolling hash for long distance long string matches. Stores one position
3148
+ per bucket, bucket key is computed over a long region. */
3149
+
3150
+ #define HashRolling HASHER()
3151
+
3152
+ static const uint32_t FN(kRollingHashMul32) = 69069;
3153
+ static const uint32_t FN(kInvalidPos) = 0xffffffff;
3154
+
3155
+ /* This hasher uses a longer forward length, but returning a higher value here
3156
+ will hurt compression by the main hasher when combined with a composite
3157
+ hasher. The hasher tests for forward itself instead. */
3158
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }
3159
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }
3160
+
3161
+ /* Computes a code from a single byte. A lookup table of 256 values could be
3162
+ used, but simply adding 1 works about as good. */
3163
+ static uint32_t FN(HashByte)(uint8_t byte) {
3164
+ return (uint32_t)byte + 1u;
3165
+ }
3166
+
3167
+ static uint32_t FN(HashRollingFunctionInitial)(uint32_t state, uint8_t add,
3168
+ uint32_t factor) {
3169
+ return (uint32_t)(factor * state + FN(HashByte)(add));
3170
+ }
3171
+
3172
+ static uint32_t FN(HashRollingFunction)(uint32_t state, uint8_t add,
3173
+ uint8_t rem, uint32_t factor,
3174
+ uint32_t factor_remove) {
3175
+ return (uint32_t)(factor * state +
3176
+ FN(HashByte)(add) - factor_remove * FN(HashByte)(rem));
3177
+ }
3178
+
3179
+ typedef struct HashRolling {
3180
+ uint32_t state;
3181
+ uint32_t* table;
3182
+ size_t next_ix;
3183
+
3184
+ uint32_t chunk_len;
3185
+ uint32_t factor;
3186
+ uint32_t factor_remove;
3187
+ } HashRolling;
3188
+
3189
+ static void FN(Initialize)(
3190
+ HasherCommon* common, HashRolling* BROTLI_RESTRICT self,
3191
+ const BrotliEncoderParams* params) {
3192
+ size_t i;
3193
+ self->state = 0;
3194
+ self->next_ix = 0;
3195
+
3196
+ self->factor = FN(kRollingHashMul32);
3197
+
3198
+ /* Compute the factor of the oldest byte to remove: factor**steps modulo
3199
+ 0xffffffff (the multiplications rely on 32-bit overflow) */
3200
+ self->factor_remove = 1;
3201
+ for (i = 0; i < CHUNKLEN; i += JUMP) {
3202
+ self->factor_remove *= self->factor;
3203
+ }
3204
+
3205
+ self->table = (uint32_t*)common->extra[0];
3206
+ for (i = 0; i < NUMBUCKETS; i++) {
3207
+ self->table[i] = FN(kInvalidPos);
3208
+ }
3209
+
3210
+ BROTLI_UNUSED(params);
3211
+ }
3212
+
3213
+ static void FN(Prepare)(HashRolling* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
3214
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
3215
+ size_t i;
3216
+ /* Too small size, cannot use this hasher. */
3217
+ if (input_size < CHUNKLEN) return;
3218
+ self->state = 0;
3219
+ for (i = 0; i < CHUNKLEN; i += JUMP) {
3220
+ self->state = FN(HashRollingFunctionInitial)(
3221
+ self->state, data[i], self->factor);
3222
+ }
3223
+ BROTLI_UNUSED(one_shot);
3224
+ }
3225
+
3226
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
3227
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
3228
+ size_t input_size, size_t* alloc_size) {
3229
+ BROTLI_UNUSED(params);
3230
+ BROTLI_UNUSED(one_shot);
3231
+ BROTLI_UNUSED(input_size);
3232
+ alloc_size[0] = NUMBUCKETS * sizeof(uint32_t);
3233
+ }
3234
+
3235
+ static BROTLI_INLINE void FN(Store)(HashRolling* BROTLI_RESTRICT self,
3236
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
3237
+ BROTLI_UNUSED(self);
3238
+ BROTLI_UNUSED(data);
3239
+ BROTLI_UNUSED(mask);
3240
+ BROTLI_UNUSED(ix);
3241
+ }
3242
+
3243
+ static BROTLI_INLINE void FN(StoreRange)(HashRolling* BROTLI_RESTRICT self,
3244
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
3245
+ const size_t ix_start, const size_t ix_end) {
3246
+ BROTLI_UNUSED(self);
3247
+ BROTLI_UNUSED(data);
3248
+ BROTLI_UNUSED(mask);
3249
+ BROTLI_UNUSED(ix_start);
3250
+ BROTLI_UNUSED(ix_end);
3251
+ }
3252
+
3253
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
3254
+ HashRolling* BROTLI_RESTRICT self,
3255
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
3256
+ size_t ring_buffer_mask) {
3257
+ /* In this case we must re-initialize the hasher from scratch from the
3258
+ current position. */
3259
+ size_t position_masked;
3260
+ size_t available = num_bytes;
3261
+ if ((position & (JUMP - 1)) != 0) {
3262
+ size_t diff = JUMP - (position & (JUMP - 1));
3263
+ available = (diff > available) ? 0 : (available - diff);
3264
+ position += diff;
3265
+ }
3266
+ position_masked = position & ring_buffer_mask;
3267
+ /* wrapping around ringbuffer not handled. */
3268
+ if (available > ring_buffer_mask - position_masked) {
3269
+ available = ring_buffer_mask - position_masked;
3270
+ }
3271
+
3272
+ FN(Prepare)(self, BROTLI_FALSE, available,
3273
+ ringbuffer + (position & ring_buffer_mask));
3274
+ self->next_ix = position;
3275
+ BROTLI_UNUSED(num_bytes);
3276
+ }
3277
+
3278
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
3279
+ HashRolling* BROTLI_RESTRICT self,
3280
+ int* BROTLI_RESTRICT distance_cache) {
3281
+ BROTLI_UNUSED(self);
3282
+ BROTLI_UNUSED(distance_cache);
3283
+ }
3284
+
3285
+ static BROTLI_INLINE void FN(FindLongestMatch)(
3286
+ HashRolling* BROTLI_RESTRICT self,
3287
+ const BrotliEncoderDictionary* dictionary,
3288
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
3289
+ const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
3290
+ const size_t max_length, const size_t max_backward,
3291
+ const size_t dictionary_distance, const size_t max_distance,
3292
+ HasherSearchResult* BROTLI_RESTRICT out) {
3293
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
3294
+ size_t pos;
3295
+
3296
+ if ((cur_ix & (JUMP - 1)) != 0) return;
3297
+
3298
+ /* Not enough lookahead */
3299
+ if (max_length < CHUNKLEN) return;
3300
+
3301
+ for (pos = self->next_ix; pos <= cur_ix; pos += JUMP) {
3302
+ uint32_t code = self->state & MASK;
3303
+
3304
+ uint8_t rem = data[pos & ring_buffer_mask];
3305
+ uint8_t add = data[(pos + CHUNKLEN) & ring_buffer_mask];
3306
+ size_t found_ix = FN(kInvalidPos);
3307
+
3308
+ self->state = FN(HashRollingFunction)(
3309
+ self->state, add, rem, self->factor, self->factor_remove);
3310
+
3311
+ if (code < NUMBUCKETS) {
3312
+ found_ix = self->table[code];
3313
+ self->table[code] = (uint32_t)pos;
3314
+ if (pos == cur_ix && found_ix != FN(kInvalidPos)) {
3315
+ /* The cast to 32-bit makes backward distances up to 4GB work even
3316
+ if cur_ix is above 4GB, despite using 32-bit values in the table. */
3317
+ size_t backward = (uint32_t)(cur_ix - found_ix);
3318
+ if (backward <= max_backward) {
3319
+ const size_t found_ix_masked = found_ix & ring_buffer_mask;
3320
+ const size_t len = FindMatchLengthWithLimit(&data[found_ix_masked],
3321
+ &data[cur_ix_masked],
3322
+ max_length);
3323
+ if (len >= 4 && len > out->len) {
3324
+ score_t score = BackwardReferenceScore(len, backward);
3325
+ if (score > out->score) {
3326
+ out->len = len;
3327
+ out->distance = backward;
3328
+ out->score = score;
3329
+ out->len_code_delta = 0;
3330
+ }
3331
+ }
3332
+ }
3333
+ }
3334
+ }
3335
+ }
3336
+
3337
+ self->next_ix = cur_ix + JUMP;
3338
+
3339
+ /* NOTE: this hasher does not search in the dictionary. It is used as
3340
+ backup-hasher, the main hasher already searches in it. */
3341
+ BROTLI_UNUSED(dictionary);
3342
+ BROTLI_UNUSED(distance_cache);
3343
+ BROTLI_UNUSED(dictionary_distance);
3344
+ BROTLI_UNUSED(max_distance);
3345
+ }
3346
+
3347
+ #undef HashRolling
3348
+ #undef JUMP
3349
+ #undef HASHER
3350
+
3351
+
3352
+ #define HASHER() HROLLING
3353
+ #define JUMP 1
3354
+ /* NOLINT(build/header_guard) */
3355
+ /* Copyright 2018 Google Inc. All Rights Reserved.
3356
+
3357
+ Distributed under MIT license.
3358
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
3359
+ */
3360
+
3361
+ /* template parameters: FN, JUMP, NUMBUCKETS, MASK, CHUNKLEN */
3362
+ /* NUMBUCKETS / (MASK + 1) = probability of storing and using hash code. */
3363
+ /* JUMP = skip bytes for speedup */
3364
+
3365
+ /* Rolling hash for long distance long string matches. Stores one position
3366
+ per bucket, bucket key is computed over a long region. */
3367
+
3368
+ #define HashRolling HASHER()
3369
+
3370
+ static const uint32_t FN(kRollingHashMul32) = 69069;
3371
+ static const uint32_t FN(kInvalidPos) = 0xffffffff;
3372
+
3373
+ /* This hasher uses a longer forward length, but returning a higher value here
3374
+ will hurt compression by the main hasher when combined with a composite
3375
+ hasher. The hasher tests for forward itself instead. */
3376
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }
3377
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }
3378
+
3379
+ /* Computes a code from a single byte. A lookup table of 256 values could be
3380
+ used, but simply adding 1 works about as good. */
3381
+ static uint32_t FN(HashByte)(uint8_t byte) {
3382
+ return (uint32_t)byte + 1u;
3383
+ }
3384
+
3385
+ static uint32_t FN(HashRollingFunctionInitial)(uint32_t state, uint8_t add,
3386
+ uint32_t factor) {
3387
+ return (uint32_t)(factor * state + FN(HashByte)(add));
3388
+ }
3389
+
3390
+ static uint32_t FN(HashRollingFunction)(uint32_t state, uint8_t add,
3391
+ uint8_t rem, uint32_t factor,
3392
+ uint32_t factor_remove) {
3393
+ return (uint32_t)(factor * state +
3394
+ FN(HashByte)(add) - factor_remove * FN(HashByte)(rem));
3395
+ }
3396
+
3397
+ typedef struct HashRolling {
3398
+ uint32_t state;
3399
+ uint32_t* table;
3400
+ size_t next_ix;
3401
+
3402
+ uint32_t chunk_len;
3403
+ uint32_t factor;
3404
+ uint32_t factor_remove;
3405
+ } HashRolling;
3406
+
3407
+ static void FN(Initialize)(
3408
+ HasherCommon* common, HashRolling* BROTLI_RESTRICT self,
3409
+ const BrotliEncoderParams* params) {
3410
+ size_t i;
3411
+ self->state = 0;
3412
+ self->next_ix = 0;
3413
+
3414
+ self->factor = FN(kRollingHashMul32);
3415
+
3416
+ /* Compute the factor of the oldest byte to remove: factor**steps modulo
3417
+ 0xffffffff (the multiplications rely on 32-bit overflow) */
3418
+ self->factor_remove = 1;
3419
+ for (i = 0; i < CHUNKLEN; i += JUMP) {
3420
+ self->factor_remove *= self->factor;
3421
+ }
3422
+
3423
+ self->table = (uint32_t*)common->extra[0];
3424
+ for (i = 0; i < NUMBUCKETS; i++) {
3425
+ self->table[i] = FN(kInvalidPos);
3426
+ }
3427
+
3428
+ BROTLI_UNUSED(params);
3429
+ }
3430
+
3431
+ static void FN(Prepare)(HashRolling* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
3432
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
3433
+ size_t i;
3434
+ /* Too small size, cannot use this hasher. */
3435
+ if (input_size < CHUNKLEN) return;
3436
+ self->state = 0;
3437
+ for (i = 0; i < CHUNKLEN; i += JUMP) {
3438
+ self->state = FN(HashRollingFunctionInitial)(
3439
+ self->state, data[i], self->factor);
3440
+ }
3441
+ BROTLI_UNUSED(one_shot);
3442
+ }
3443
+
3444
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
3445
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
3446
+ size_t input_size, size_t* alloc_size) {
3447
+ BROTLI_UNUSED(params);
3448
+ BROTLI_UNUSED(one_shot);
3449
+ BROTLI_UNUSED(input_size);
3450
+ alloc_size[0] = NUMBUCKETS * sizeof(uint32_t);
3451
+ }
3452
+
3453
+ static BROTLI_INLINE void FN(Store)(HashRolling* BROTLI_RESTRICT self,
3454
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
3455
+ BROTLI_UNUSED(self);
3456
+ BROTLI_UNUSED(data);
3457
+ BROTLI_UNUSED(mask);
3458
+ BROTLI_UNUSED(ix);
3459
+ }
3460
+
3461
+ static BROTLI_INLINE void FN(StoreRange)(HashRolling* BROTLI_RESTRICT self,
3462
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask,
3463
+ const size_t ix_start, const size_t ix_end) {
3464
+ BROTLI_UNUSED(self);
3465
+ BROTLI_UNUSED(data);
3466
+ BROTLI_UNUSED(mask);
3467
+ BROTLI_UNUSED(ix_start);
3468
+ BROTLI_UNUSED(ix_end);
3469
+ }
3470
+
3471
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
3472
+ HashRolling* BROTLI_RESTRICT self,
3473
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
3474
+ size_t ring_buffer_mask) {
3475
+ /* In this case we must re-initialize the hasher from scratch from the
3476
+ current position. */
3477
+ size_t position_masked;
3478
+ size_t available = num_bytes;
3479
+ if ((position & (JUMP - 1)) != 0) {
3480
+ size_t diff = JUMP - (position & (JUMP - 1));
3481
+ available = (diff > available) ? 0 : (available - diff);
3482
+ position += diff;
3483
+ }
3484
+ position_masked = position & ring_buffer_mask;
3485
+ /* wrapping around ringbuffer not handled. */
3486
+ if (available > ring_buffer_mask - position_masked) {
3487
+ available = ring_buffer_mask - position_masked;
3488
+ }
3489
+
3490
+ FN(Prepare)(self, BROTLI_FALSE, available,
3491
+ ringbuffer + (position & ring_buffer_mask));
3492
+ self->next_ix = position;
3493
+ BROTLI_UNUSED(num_bytes);
3494
+ }
3495
+
3496
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
3497
+ HashRolling* BROTLI_RESTRICT self,
3498
+ int* BROTLI_RESTRICT distance_cache) {
3499
+ BROTLI_UNUSED(self);
3500
+ BROTLI_UNUSED(distance_cache);
3501
+ }
3502
+
3503
+ static BROTLI_INLINE void FN(FindLongestMatch)(
3504
+ HashRolling* BROTLI_RESTRICT self,
3505
+ const BrotliEncoderDictionary* dictionary,
3506
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
3507
+ const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
3508
+ const size_t max_length, const size_t max_backward,
3509
+ const size_t dictionary_distance, const size_t max_distance,
3510
+ HasherSearchResult* BROTLI_RESTRICT out) {
3511
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
3512
+ size_t pos;
3513
+
3514
+ if ((cur_ix & (JUMP - 1)) != 0) return;
3515
+
3516
+ /* Not enough lookahead */
3517
+ if (max_length < CHUNKLEN) return;
3518
+
3519
+ for (pos = self->next_ix; pos <= cur_ix; pos += JUMP) {
3520
+ uint32_t code = self->state & MASK;
3521
+
3522
+ uint8_t rem = data[pos & ring_buffer_mask];
3523
+ uint8_t add = data[(pos + CHUNKLEN) & ring_buffer_mask];
3524
+ size_t found_ix = FN(kInvalidPos);
3525
+
3526
+ self->state = FN(HashRollingFunction)(
3527
+ self->state, add, rem, self->factor, self->factor_remove);
3528
+
3529
+ if (code < NUMBUCKETS) {
3530
+ found_ix = self->table[code];
3531
+ self->table[code] = (uint32_t)pos;
3532
+ if (pos == cur_ix && found_ix != FN(kInvalidPos)) {
3533
+ /* The cast to 32-bit makes backward distances up to 4GB work even
3534
+ if cur_ix is above 4GB, despite using 32-bit values in the table. */
3535
+ size_t backward = (uint32_t)(cur_ix - found_ix);
3536
+ if (backward <= max_backward) {
3537
+ const size_t found_ix_masked = found_ix & ring_buffer_mask;
3538
+ const size_t len = FindMatchLengthWithLimit(&data[found_ix_masked],
3539
+ &data[cur_ix_masked],
3540
+ max_length);
3541
+ if (len >= 4 && len > out->len) {
3542
+ score_t score = BackwardReferenceScore(len, backward);
3543
+ if (score > out->score) {
3544
+ out->len = len;
3545
+ out->distance = backward;
3546
+ out->score = score;
3547
+ out->len_code_delta = 0;
3548
+ }
3549
+ }
3550
+ }
3551
+ }
3552
+ }
3553
+ }
3554
+
3555
+ self->next_ix = cur_ix + JUMP;
3556
+
3557
+ /* NOTE: this hasher does not search in the dictionary. It is used as
3558
+ backup-hasher, the main hasher already searches in it. */
3559
+ BROTLI_UNUSED(dictionary);
3560
+ BROTLI_UNUSED(distance_cache);
3561
+ BROTLI_UNUSED(dictionary_distance);
3562
+ BROTLI_UNUSED(max_distance);
3563
+ }
3564
+
3565
+ #undef HashRolling
3566
+ #undef MASK
3567
+ #undef NUMBUCKETS
3568
+ #undef JUMP
3569
+ #undef CHUNKLEN
3570
+ #undef HASHER
3571
+
3572
+ #define HASHER() H35
3573
+ #define HASHER_A H3
3574
+ #define HASHER_B HROLLING_FAST
3575
+ /* NOLINT(build/header_guard) */
3576
+ /* Copyright 2018 Google Inc. All Rights Reserved.
3577
+
3578
+ Distributed under MIT license.
3579
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
3580
+ */
3581
+
3582
+ /* template parameters: FN, HASHER_A, HASHER_B */
3583
+
3584
+ /* Composite hasher: This hasher allows to combine two other hashers, HASHER_A
3585
+ and HASHER_B. */
3586
+
3587
+ #define HashComposite HASHER()
3588
+
3589
+ #define FN_A(X) EXPAND_CAT(X, HASHER_A)
3590
+ #define FN_B(X) EXPAND_CAT(X, HASHER_B)
3591
+
3592
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) {
3593
+ size_t a = FN_A(HashTypeLength)();
3594
+ size_t b = FN_B(HashTypeLength)();
3595
+ return a > b ? a : b;
3596
+ }
3597
+
3598
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) {
3599
+ size_t a = FN_A(StoreLookahead)();
3600
+ size_t b = FN_B(StoreLookahead)();
3601
+ return a > b ? a : b;
3602
+ }
3603
+
3604
+ typedef struct HashComposite {
3605
+ HASHER_A ha;
3606
+ HASHER_B hb;
3607
+ HasherCommon ha_common;
3608
+ HasherCommon hb_common;
3609
+
3610
+ /* Shortcuts. */
3611
+ HasherCommon* common;
3612
+
3613
+ BROTLI_BOOL fresh;
3614
+ const BrotliEncoderParams* params;
3615
+ } HashComposite;
3616
+
3617
+ static void FN(Initialize)(HasherCommon* common,
3618
+ HashComposite* BROTLI_RESTRICT self, const BrotliEncoderParams* params) {
3619
+ self->common = common;
3620
+
3621
+ self->ha_common = *self->common;
3622
+ self->hb_common = *self->common;
3623
+ self->fresh = BROTLI_TRUE;
3624
+ self->params = params;
3625
+ /* TODO(lode): Initialize of the hashers is deferred to Prepare (and params
3626
+ remembered here) because we don't get the one_shot and input_size params
3627
+ here that are needed to know the memory size of them. Instead provide
3628
+ those params to all hashers FN(Initialize) */
3629
+ }
3630
+
3631
+ static void FN(Prepare)(
3632
+ HashComposite* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
3633
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
3634
+ if (self->fresh) {
3635
+ self->fresh = BROTLI_FALSE;
3636
+ self->ha_common.extra[0] = self->common->extra[0];
3637
+ self->ha_common.extra[1] = self->common->extra[1];
3638
+ self->ha_common.extra[2] = NULL;
3639
+ self->ha_common.extra[3] = NULL;
3640
+ self->hb_common.extra[0] = self->common->extra[2];
3641
+ self->hb_common.extra[1] = self->common->extra[3];
3642
+ self->hb_common.extra[2] = NULL;
3643
+ self->hb_common.extra[3] = NULL;
3644
+
3645
+ FN_A(Initialize)(&self->ha_common, &self->ha, self->params);
3646
+ FN_B(Initialize)(&self->hb_common, &self->hb, self->params);
3647
+ }
3648
+ FN_A(Prepare)(&self->ha, one_shot, input_size, data);
3649
+ FN_B(Prepare)(&self->hb, one_shot, input_size, data);
3650
+ }
3651
+
3652
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
3653
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
3654
+ size_t input_size, size_t* alloc_size) {
3655
+ size_t alloc_size_a[4] = {0};
3656
+ size_t alloc_size_b[4] = {0};
3657
+ FN_A(HashMemAllocInBytes)(params, one_shot, input_size, alloc_size_a);
3658
+ FN_B(HashMemAllocInBytes)(params, one_shot, input_size, alloc_size_b);
3659
+ /* Should never happen. */
3660
+ if (alloc_size_a[2] != 0 || alloc_size_a[3] != 0) exit(EXIT_FAILURE);
3661
+ if (alloc_size_b[2] != 0 || alloc_size_b[3] != 0) exit(EXIT_FAILURE);
3662
+ alloc_size[0] = alloc_size_a[0];
3663
+ alloc_size[1] = alloc_size_a[1];
3664
+ alloc_size[2] = alloc_size_b[0];
3665
+ alloc_size[3] = alloc_size_b[1];
3666
+ }
3667
+
3668
+ static BROTLI_INLINE void FN(Store)(HashComposite* BROTLI_RESTRICT self,
3669
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
3670
+ FN_A(Store)(&self->ha, data, mask, ix);
3671
+ FN_B(Store)(&self->hb, data, mask, ix);
3672
+ }
3673
+
3674
+ static BROTLI_INLINE void FN(StoreRange)(
3675
+ HashComposite* BROTLI_RESTRICT self, const uint8_t* BROTLI_RESTRICT data,
3676
+ const size_t mask, const size_t ix_start,
3677
+ const size_t ix_end) {
3678
+ FN_A(StoreRange)(&self->ha, data, mask, ix_start, ix_end);
3679
+ FN_B(StoreRange)(&self->hb, data, mask, ix_start, ix_end);
3680
+ }
3681
+
3682
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
3683
+ HashComposite* BROTLI_RESTRICT self,
3684
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
3685
+ size_t ring_buffer_mask) {
3686
+ FN_A(StitchToPreviousBlock)(&self->ha, num_bytes, position,
3687
+ ringbuffer, ring_buffer_mask);
3688
+ FN_B(StitchToPreviousBlock)(&self->hb, num_bytes, position,
3689
+ ringbuffer, ring_buffer_mask);
3690
+ }
3691
+
3692
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
3693
+ HashComposite* BROTLI_RESTRICT self, int* BROTLI_RESTRICT distance_cache) {
3694
+ FN_A(PrepareDistanceCache)(&self->ha, distance_cache);
3695
+ FN_B(PrepareDistanceCache)(&self->hb, distance_cache);
3696
+ }
3697
+
3698
+ static BROTLI_INLINE void FN(FindLongestMatch)(
3699
+ HashComposite* BROTLI_RESTRICT self,
3700
+ const BrotliEncoderDictionary* dictionary,
3701
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
3702
+ const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
3703
+ const size_t max_length, const size_t max_backward,
3704
+ const size_t dictionary_distance, const size_t max_distance,
3705
+ HasherSearchResult* BROTLI_RESTRICT out) {
3706
+ FN_A(FindLongestMatch)(&self->ha, dictionary, data, ring_buffer_mask,
3707
+ distance_cache, cur_ix, max_length, max_backward, dictionary_distance,
3708
+ max_distance, out);
3709
+ FN_B(FindLongestMatch)(&self->hb, dictionary, data, ring_buffer_mask,
3710
+ distance_cache, cur_ix, max_length, max_backward, dictionary_distance,
3711
+ max_distance, out);
3712
+ }
3713
+
3714
+ #undef HashComposite
3715
+ #undef HASHER_A
3716
+ #undef HASHER_B
3717
+ #undef HASHER
3718
+
3719
+ #define HASHER() H55
3720
+ #define HASHER_A H54
3721
+ #define HASHER_B HROLLING_FAST
3722
+ /* NOLINT(build/header_guard) */
3723
+ /* Copyright 2018 Google Inc. All Rights Reserved.
3724
+
3725
+ Distributed under MIT license.
3726
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
3727
+ */
3728
+
3729
+ /* template parameters: FN, HASHER_A, HASHER_B */
3730
+
3731
+ /* Composite hasher: This hasher allows to combine two other hashers, HASHER_A
3732
+ and HASHER_B. */
3733
+
3734
+ #define HashComposite HASHER()
3735
+
3736
+ #define FN_A(X) EXPAND_CAT(X, HASHER_A)
3737
+ #define FN_B(X) EXPAND_CAT(X, HASHER_B)
3738
+
3739
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) {
3740
+ size_t a = FN_A(HashTypeLength)();
3741
+ size_t b = FN_B(HashTypeLength)();
3742
+ return a > b ? a : b;
3743
+ }
3744
+
3745
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) {
3746
+ size_t a = FN_A(StoreLookahead)();
3747
+ size_t b = FN_B(StoreLookahead)();
3748
+ return a > b ? a : b;
3749
+ }
3750
+
3751
+ typedef struct HashComposite {
3752
+ HASHER_A ha;
3753
+ HASHER_B hb;
3754
+ HasherCommon ha_common;
3755
+ HasherCommon hb_common;
3756
+
3757
+ /* Shortcuts. */
3758
+ HasherCommon* common;
3759
+
3760
+ BROTLI_BOOL fresh;
3761
+ const BrotliEncoderParams* params;
3762
+ } HashComposite;
3763
+
3764
+ static void FN(Initialize)(HasherCommon* common,
3765
+ HashComposite* BROTLI_RESTRICT self, const BrotliEncoderParams* params) {
3766
+ self->common = common;
3767
+
3768
+ self->ha_common = *self->common;
3769
+ self->hb_common = *self->common;
3770
+ self->fresh = BROTLI_TRUE;
3771
+ self->params = params;
3772
+ /* TODO(lode): Initialize of the hashers is deferred to Prepare (and params
3773
+ remembered here) because we don't get the one_shot and input_size params
3774
+ here that are needed to know the memory size of them. Instead provide
3775
+ those params to all hashers FN(Initialize) */
3776
+ }
3777
+
3778
+ static void FN(Prepare)(
3779
+ HashComposite* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
3780
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
3781
+ if (self->fresh) {
3782
+ self->fresh = BROTLI_FALSE;
3783
+ self->ha_common.extra[0] = self->common->extra[0];
3784
+ self->ha_common.extra[1] = self->common->extra[1];
3785
+ self->ha_common.extra[2] = NULL;
3786
+ self->ha_common.extra[3] = NULL;
3787
+ self->hb_common.extra[0] = self->common->extra[2];
3788
+ self->hb_common.extra[1] = self->common->extra[3];
3789
+ self->hb_common.extra[2] = NULL;
3790
+ self->hb_common.extra[3] = NULL;
3791
+
3792
+ FN_A(Initialize)(&self->ha_common, &self->ha, self->params);
3793
+ FN_B(Initialize)(&self->hb_common, &self->hb, self->params);
3794
+ }
3795
+ FN_A(Prepare)(&self->ha, one_shot, input_size, data);
3796
+ FN_B(Prepare)(&self->hb, one_shot, input_size, data);
3797
+ }
3798
+
3799
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
3800
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
3801
+ size_t input_size, size_t* alloc_size) {
3802
+ size_t alloc_size_a[4] = {0};
3803
+ size_t alloc_size_b[4] = {0};
3804
+ FN_A(HashMemAllocInBytes)(params, one_shot, input_size, alloc_size_a);
3805
+ FN_B(HashMemAllocInBytes)(params, one_shot, input_size, alloc_size_b);
3806
+ /* Should never happen. */
3807
+ if (alloc_size_a[2] != 0 || alloc_size_a[3] != 0) exit(EXIT_FAILURE);
3808
+ if (alloc_size_b[2] != 0 || alloc_size_b[3] != 0) exit(EXIT_FAILURE);
3809
+ alloc_size[0] = alloc_size_a[0];
3810
+ alloc_size[1] = alloc_size_a[1];
3811
+ alloc_size[2] = alloc_size_b[0];
3812
+ alloc_size[3] = alloc_size_b[1];
3813
+ }
3814
+
3815
+ static BROTLI_INLINE void FN(Store)(HashComposite* BROTLI_RESTRICT self,
3816
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
3817
+ FN_A(Store)(&self->ha, data, mask, ix);
3818
+ FN_B(Store)(&self->hb, data, mask, ix);
3819
+ }
3820
+
3821
+ static BROTLI_INLINE void FN(StoreRange)(
3822
+ HashComposite* BROTLI_RESTRICT self, const uint8_t* BROTLI_RESTRICT data,
3823
+ const size_t mask, const size_t ix_start,
3824
+ const size_t ix_end) {
3825
+ FN_A(StoreRange)(&self->ha, data, mask, ix_start, ix_end);
3826
+ FN_B(StoreRange)(&self->hb, data, mask, ix_start, ix_end);
3827
+ }
3828
+
3829
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
3830
+ HashComposite* BROTLI_RESTRICT self,
3831
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
3832
+ size_t ring_buffer_mask) {
3833
+ FN_A(StitchToPreviousBlock)(&self->ha, num_bytes, position,
3834
+ ringbuffer, ring_buffer_mask);
3835
+ FN_B(StitchToPreviousBlock)(&self->hb, num_bytes, position,
3836
+ ringbuffer, ring_buffer_mask);
3837
+ }
3838
+
3839
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
3840
+ HashComposite* BROTLI_RESTRICT self, int* BROTLI_RESTRICT distance_cache) {
3841
+ FN_A(PrepareDistanceCache)(&self->ha, distance_cache);
3842
+ FN_B(PrepareDistanceCache)(&self->hb, distance_cache);
3843
+ }
3844
+
3845
+ static BROTLI_INLINE void FN(FindLongestMatch)(
3846
+ HashComposite* BROTLI_RESTRICT self,
3847
+ const BrotliEncoderDictionary* dictionary,
3848
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
3849
+ const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
3850
+ const size_t max_length, const size_t max_backward,
3851
+ const size_t dictionary_distance, const size_t max_distance,
3852
+ HasherSearchResult* BROTLI_RESTRICT out) {
3853
+ FN_A(FindLongestMatch)(&self->ha, dictionary, data, ring_buffer_mask,
3854
+ distance_cache, cur_ix, max_length, max_backward, dictionary_distance,
3855
+ max_distance, out);
3856
+ FN_B(FindLongestMatch)(&self->hb, dictionary, data, ring_buffer_mask,
3857
+ distance_cache, cur_ix, max_length, max_backward, dictionary_distance,
3858
+ max_distance, out);
3859
+ }
3860
+
3861
+ #undef HashComposite
3862
+ #undef HASHER_A
3863
+ #undef HASHER_B
3864
+ #undef HASHER
3865
+
3866
+ #define HASHER() H65
3867
+ #define HASHER_A H6
3868
+ #define HASHER_B HROLLING
3869
+ /* NOLINT(build/header_guard) */
3870
+ /* Copyright 2018 Google Inc. All Rights Reserved.
3871
+
3872
+ Distributed under MIT license.
3873
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
3874
+ */
3875
+
3876
+ /* template parameters: FN, HASHER_A, HASHER_B */
3877
+
3878
+ /* Composite hasher: This hasher allows to combine two other hashers, HASHER_A
3879
+ and HASHER_B. */
3880
+
3881
+ #define HashComposite HASHER()
3882
+
3883
+ #define FN_A(X) EXPAND_CAT(X, HASHER_A)
3884
+ #define FN_B(X) EXPAND_CAT(X, HASHER_B)
3885
+
3886
+ static BROTLI_INLINE size_t FN(HashTypeLength)(void) {
3887
+ size_t a = FN_A(HashTypeLength)();
3888
+ size_t b = FN_B(HashTypeLength)();
3889
+ return a > b ? a : b;
3890
+ }
3891
+
3892
+ static BROTLI_INLINE size_t FN(StoreLookahead)(void) {
3893
+ size_t a = FN_A(StoreLookahead)();
3894
+ size_t b = FN_B(StoreLookahead)();
3895
+ return a > b ? a : b;
3896
+ }
3897
+
3898
+ typedef struct HashComposite {
3899
+ HASHER_A ha;
3900
+ HASHER_B hb;
3901
+ HasherCommon ha_common;
3902
+ HasherCommon hb_common;
3903
+
3904
+ /* Shortcuts. */
3905
+ HasherCommon* common;
3906
+
3907
+ BROTLI_BOOL fresh;
3908
+ const BrotliEncoderParams* params;
3909
+ } HashComposite;
3910
+
3911
+ static void FN(Initialize)(HasherCommon* common,
3912
+ HashComposite* BROTLI_RESTRICT self, const BrotliEncoderParams* params) {
3913
+ self->common = common;
3914
+
3915
+ self->ha_common = *self->common;
3916
+ self->hb_common = *self->common;
3917
+ self->fresh = BROTLI_TRUE;
3918
+ self->params = params;
3919
+ /* TODO(lode): Initialize of the hashers is deferred to Prepare (and params
3920
+ remembered here) because we don't get the one_shot and input_size params
3921
+ here that are needed to know the memory size of them. Instead provide
3922
+ those params to all hashers FN(Initialize) */
3923
+ }
3924
+
3925
+ static void FN(Prepare)(
3926
+ HashComposite* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
3927
+ size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
3928
+ if (self->fresh) {
3929
+ self->fresh = BROTLI_FALSE;
3930
+ self->ha_common.extra[0] = self->common->extra[0];
3931
+ self->ha_common.extra[1] = self->common->extra[1];
3932
+ self->ha_common.extra[2] = NULL;
3933
+ self->ha_common.extra[3] = NULL;
3934
+ self->hb_common.extra[0] = self->common->extra[2];
3935
+ self->hb_common.extra[1] = self->common->extra[3];
3936
+ self->hb_common.extra[2] = NULL;
3937
+ self->hb_common.extra[3] = NULL;
3938
+
3939
+ FN_A(Initialize)(&self->ha_common, &self->ha, self->params);
3940
+ FN_B(Initialize)(&self->hb_common, &self->hb, self->params);
3941
+ }
3942
+ FN_A(Prepare)(&self->ha, one_shot, input_size, data);
3943
+ FN_B(Prepare)(&self->hb, one_shot, input_size, data);
3944
+ }
3945
+
3946
+ static BROTLI_INLINE void FN(HashMemAllocInBytes)(
3947
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
3948
+ size_t input_size, size_t* alloc_size) {
3949
+ size_t alloc_size_a[4] = {0};
3950
+ size_t alloc_size_b[4] = {0};
3951
+ FN_A(HashMemAllocInBytes)(params, one_shot, input_size, alloc_size_a);
3952
+ FN_B(HashMemAllocInBytes)(params, one_shot, input_size, alloc_size_b);
3953
+ /* Should never happen. */
3954
+ if (alloc_size_a[2] != 0 || alloc_size_a[3] != 0) exit(EXIT_FAILURE);
3955
+ if (alloc_size_b[2] != 0 || alloc_size_b[3] != 0) exit(EXIT_FAILURE);
3956
+ alloc_size[0] = alloc_size_a[0];
3957
+ alloc_size[1] = alloc_size_a[1];
3958
+ alloc_size[2] = alloc_size_b[0];
3959
+ alloc_size[3] = alloc_size_b[1];
3960
+ }
3961
+
3962
+ static BROTLI_INLINE void FN(Store)(HashComposite* BROTLI_RESTRICT self,
3963
+ const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
3964
+ FN_A(Store)(&self->ha, data, mask, ix);
3965
+ FN_B(Store)(&self->hb, data, mask, ix);
3966
+ }
3967
+
3968
+ static BROTLI_INLINE void FN(StoreRange)(
3969
+ HashComposite* BROTLI_RESTRICT self, const uint8_t* BROTLI_RESTRICT data,
3970
+ const size_t mask, const size_t ix_start,
3971
+ const size_t ix_end) {
3972
+ FN_A(StoreRange)(&self->ha, data, mask, ix_start, ix_end);
3973
+ FN_B(StoreRange)(&self->hb, data, mask, ix_start, ix_end);
3974
+ }
3975
+
3976
+ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
3977
+ HashComposite* BROTLI_RESTRICT self,
3978
+ size_t num_bytes, size_t position, const uint8_t* ringbuffer,
3979
+ size_t ring_buffer_mask) {
3980
+ FN_A(StitchToPreviousBlock)(&self->ha, num_bytes, position,
3981
+ ringbuffer, ring_buffer_mask);
3982
+ FN_B(StitchToPreviousBlock)(&self->hb, num_bytes, position,
3983
+ ringbuffer, ring_buffer_mask);
3984
+ }
3985
+
3986
+ static BROTLI_INLINE void FN(PrepareDistanceCache)(
3987
+ HashComposite* BROTLI_RESTRICT self, int* BROTLI_RESTRICT distance_cache) {
3988
+ FN_A(PrepareDistanceCache)(&self->ha, distance_cache);
3989
+ FN_B(PrepareDistanceCache)(&self->hb, distance_cache);
3990
+ }
3991
+
3992
+ static BROTLI_INLINE void FN(FindLongestMatch)(
3993
+ HashComposite* BROTLI_RESTRICT self,
3994
+ const BrotliEncoderDictionary* dictionary,
3995
+ const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
3996
+ const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
3997
+ const size_t max_length, const size_t max_backward,
3998
+ const size_t dictionary_distance, const size_t max_distance,
3999
+ HasherSearchResult* BROTLI_RESTRICT out) {
4000
+ FN_A(FindLongestMatch)(&self->ha, dictionary, data, ring_buffer_mask,
4001
+ distance_cache, cur_ix, max_length, max_backward, dictionary_distance,
4002
+ max_distance, out);
4003
+ FN_B(FindLongestMatch)(&self->hb, dictionary, data, ring_buffer_mask,
4004
+ distance_cache, cur_ix, max_length, max_backward, dictionary_distance,
4005
+ max_distance, out);
4006
+ }
4007
+
4008
+ #undef HashComposite
4009
+ #undef HASHER_A
4010
+ #undef HASHER_B
4011
+ #undef HASHER
4012
+
4013
+ #undef FN
4014
+ #undef CAT
4015
+ #undef EXPAND_CAT
4016
+
4017
+ #define FOR_SIMPLE_HASHERS(H) H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54)
4018
+ #define FOR_COMPOSITE_HASHERS(H) H(35) H(55) H(65)
4019
+ #define FOR_GENERIC_HASHERS(H) FOR_SIMPLE_HASHERS(H) FOR_COMPOSITE_HASHERS(H)
4020
+ #define FOR_ALL_HASHERS(H) FOR_GENERIC_HASHERS(H) H(10)
4021
+
4022
+ typedef struct {
4023
+ HasherCommon common;
4024
+
4025
+ union {
4026
+ #define MEMBER_(N) \
4027
+ H ## N _H ## N;
4028
+ FOR_ALL_HASHERS(MEMBER_)
4029
+ #undef MEMBER_
4030
+ } privat;
4031
+ } Hasher;
4032
+
4033
+ /* MUST be invoked before any other method. */
4034
+ static BROTLI_INLINE void HasherInit(Hasher* hasher) {
4035
+ hasher->common.is_setup_ = BROTLI_FALSE;
4036
+ hasher->common.extra[0] = NULL;
4037
+ hasher->common.extra[1] = NULL;
4038
+ hasher->common.extra[2] = NULL;
4039
+ hasher->common.extra[3] = NULL;
4040
+ }
4041
+
4042
+ static BROTLI_INLINE void DestroyHasher(MemoryManager* m, Hasher* hasher) {
4043
+ if (hasher->common.extra[0] != NULL) BROTLI_FREE(m, hasher->common.extra[0]);
4044
+ if (hasher->common.extra[1] != NULL) BROTLI_FREE(m, hasher->common.extra[1]);
4045
+ if (hasher->common.extra[2] != NULL) BROTLI_FREE(m, hasher->common.extra[2]);
4046
+ if (hasher->common.extra[3] != NULL) BROTLI_FREE(m, hasher->common.extra[3]);
4047
+ }
4048
+
4049
+ static BROTLI_INLINE void HasherReset(Hasher* hasher) {
4050
+ hasher->common.is_prepared_ = BROTLI_FALSE;
4051
+ }
4052
+
4053
+ static BROTLI_INLINE void HasherSize(const BrotliEncoderParams* params,
4054
+ BROTLI_BOOL one_shot, const size_t input_size, size_t* alloc_size) {
4055
+ switch (params->hasher.type) {
4056
+ #define SIZE_(N) \
4057
+ case N: \
4058
+ HashMemAllocInBytesH ## N(params, one_shot, input_size, alloc_size); \
4059
+ break;
4060
+ FOR_ALL_HASHERS(SIZE_)
4061
+ #undef SIZE_
4062
+ default:
4063
+ break;
4064
+ }
4065
+ }
4066
+
4067
+ static BROTLI_INLINE void HasherSetup(MemoryManager* m, Hasher* hasher,
4068
+ BrotliEncoderParams* params, const uint8_t* data, size_t position,
4069
+ size_t input_size, BROTLI_BOOL is_last) {
4070
+ BROTLI_BOOL one_shot = (position == 0 && is_last);
4071
+ if (!hasher->common.is_setup_) {
4072
+ size_t alloc_size[4] = {0};
4073
+ size_t i;
4074
+ ChooseHasher(params, &params->hasher);
4075
+ hasher->common.params = params->hasher;
4076
+ hasher->common.dict_num_lookups = 0;
4077
+ hasher->common.dict_num_matches = 0;
4078
+ HasherSize(params, one_shot, input_size, alloc_size);
4079
+ for (i = 0; i < 4; ++i) {
4080
+ if (alloc_size[i] == 0) continue;
4081
+ hasher->common.extra[i] = BROTLI_ALLOC(m, uint8_t, alloc_size[i]);
4082
+ if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(hasher->common.extra[i])) return;
4083
+ }
4084
+ switch (hasher->common.params.type) {
4085
+ #define INITIALIZE_(N) \
4086
+ case N: \
4087
+ InitializeH ## N(&hasher->common, \
4088
+ &hasher->privat._H ## N, params); \
4089
+ break;
4090
+ FOR_ALL_HASHERS(INITIALIZE_);
4091
+ #undef INITIALIZE_
4092
+ default:
4093
+ break;
4094
+ }
4095
+ HasherReset(hasher);
4096
+ hasher->common.is_setup_ = BROTLI_TRUE;
4097
+ }
4098
+
4099
+ if (!hasher->common.is_prepared_) {
4100
+ switch (hasher->common.params.type) {
4101
+ #define PREPARE_(N) \
4102
+ case N: \
4103
+ PrepareH ## N( \
4104
+ &hasher->privat._H ## N, \
4105
+ one_shot, input_size, data); \
4106
+ break;
4107
+ FOR_ALL_HASHERS(PREPARE_)
4108
+ #undef PREPARE_
4109
+ default: break;
4110
+ }
4111
+ hasher->common.is_prepared_ = BROTLI_TRUE;
4112
+ }
4113
+ }
4114
+
4115
+ static BROTLI_INLINE void InitOrStitchToPreviousBlock(
4116
+ MemoryManager* m, Hasher* hasher, const uint8_t* data, size_t mask,
4117
+ BrotliEncoderParams* params, size_t position, size_t input_size,
4118
+ BROTLI_BOOL is_last) {
4119
+ HasherSetup(m, hasher, params, data, position, input_size, is_last);
4120
+ if (BROTLI_IS_OOM(m)) return;
4121
+ switch (hasher->common.params.type) {
4122
+ #define INIT_(N) \
4123
+ case N: \
4124
+ StitchToPreviousBlockH ## N( \
4125
+ &hasher->privat._H ## N, \
4126
+ input_size, position, data, mask); \
4127
+ break;
4128
+ FOR_ALL_HASHERS(INIT_)
4129
+ #undef INIT_
4130
+ default: break;
4131
+ }
4132
+ }
4133
+
4134
+ /* NB: when seamless dictionary-ring-buffer copies are implemented, don't forget
4135
+ to add proper guards for non-zero-BROTLI_PARAM_STREAM_OFFSET. */
4136
+ static BROTLI_INLINE void FindCompoundDictionaryMatch(
4137
+ const PreparedDictionary* self, const uint8_t* BROTLI_RESTRICT data,
4138
+ const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
4139
+ const size_t cur_ix, const size_t max_length, const size_t distance_offset,
4140
+ const size_t max_distance, HasherSearchResult* BROTLI_RESTRICT out) {
4141
+ const uint32_t source_size = self->source_size;
4142
+ const size_t boundary = distance_offset - source_size;
4143
+ const uint32_t hash_bits = self->hash_bits;
4144
+ const uint32_t bucket_bits = self->bucket_bits;
4145
+ const uint32_t slot_bits = self->slot_bits;
4146
+
4147
+ const uint32_t hash_shift = 64u - bucket_bits;
4148
+ const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits);
4149
+ const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits);
4150
+
4151
+ const uint32_t* slot_offsets = (uint32_t*)(&self[1]);
4152
+ const uint16_t* heads = (uint16_t*)(&slot_offsets[1u << slot_bits]);
4153
+ const uint32_t* items = (uint32_t*)(&heads[1u << bucket_bits]);
4154
+ const uint8_t* source = NULL;
4155
+
4156
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
4157
+ score_t best_score = out->score;
4158
+ size_t best_len = out->len;
4159
+ size_t i;
4160
+ const uint64_t h =
4161
+ (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) *
4162
+ kPreparedDictionaryHashMul64Long;
4163
+ const uint32_t key = (uint32_t)(h >> hash_shift);
4164
+ const uint32_t slot = key & slot_mask;
4165
+ const uint32_t head = heads[key];
4166
+ const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head];
4167
+ uint32_t item = (head == 0xFFFF) ? 1 : 0;
4168
+
4169
+ const void* tail = (void*)&items[self->num_items];
4170
+ if (self->magic == kPreparedDictionaryMagic) {
4171
+ source = (const uint8_t*)tail;
4172
+ } else {
4173
+ /* kLeanPreparedDictionaryMagic */
4174
+ source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail);
4175
+ }
4176
+
4177
+ for (i = 0; i < 4; ++i) {
4178
+ const size_t distance = (size_t)distance_cache[i];
4179
+ size_t offset;
4180
+ size_t limit;
4181
+ size_t len;
4182
+ if (distance <= boundary || distance > distance_offset) continue;
4183
+ offset = distance_offset - distance;
4184
+ limit = source_size - offset;
4185
+ limit = limit > max_length ? max_length : limit;
4186
+ len = FindMatchLengthWithLimit(&source[offset], &data[cur_ix_masked],
4187
+ limit);
4188
+ if (len >= 2) {
4189
+ score_t score = BackwardReferenceScoreUsingLastDistance(len);
4190
+ if (best_score < score) {
4191
+ if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
4192
+ if (best_score < score) {
4193
+ best_score = score;
4194
+ if (len > best_len) best_len = len;
4195
+ out->len = len;
4196
+ out->len_code_delta = 0;
4197
+ out->distance = distance;
4198
+ out->score = best_score;
4199
+ }
4200
+ }
4201
+ }
4202
+ }
4203
+ while (item == 0) {
4204
+ size_t offset;
4205
+ size_t distance;
4206
+ size_t limit;
4207
+ item = *chain;
4208
+ chain++;
4209
+ offset = item & 0x7FFFFFFF;
4210
+ item &= 0x80000000;
4211
+ distance = distance_offset - offset;
4212
+ limit = source_size - offset;
4213
+ limit = (limit > max_length) ? max_length : limit;
4214
+ if (distance > max_distance) continue;
4215
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
4216
+ best_len >= limit ||
4217
+ data[cur_ix_masked + best_len] != source[offset + best_len]) {
4218
+ continue;
4219
+ }
4220
+ {
4221
+ const size_t len = FindMatchLengthWithLimit(&source[offset],
4222
+ &data[cur_ix_masked],
4223
+ limit);
4224
+ if (len >= 4) {
4225
+ score_t score = BackwardReferenceScore(len, distance);
4226
+ if (best_score < score) {
4227
+ best_score = score;
4228
+ best_len = len;
4229
+ out->len = best_len;
4230
+ out->len_code_delta = 0;
4231
+ out->distance = distance;
4232
+ out->score = best_score;
4233
+ }
4234
+ }
4235
+ }
4236
+ }
4237
+ }
4238
+
4239
+ /* NB: when seamless dictionary-ring-buffer copies are implemented, don't forget
4240
+ to add proper guards for non-zero-BROTLI_PARAM_STREAM_OFFSET. */
4241
+ static BROTLI_INLINE size_t FindAllCompoundDictionaryMatches(
4242
+ const PreparedDictionary* self, const uint8_t* BROTLI_RESTRICT data,
4243
+ const size_t ring_buffer_mask, const size_t cur_ix, const size_t min_length,
4244
+ const size_t max_length, const size_t distance_offset,
4245
+ const size_t max_distance, BackwardMatch* matches, size_t match_limit) {
4246
+ const uint32_t source_size = self->source_size;
4247
+ const uint32_t hash_bits = self->hash_bits;
4248
+ const uint32_t bucket_bits = self->bucket_bits;
4249
+ const uint32_t slot_bits = self->slot_bits;
4250
+
4251
+ const uint32_t hash_shift = 64u - bucket_bits;
4252
+ const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits);
4253
+ const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits);
4254
+
4255
+ const uint32_t* slot_offsets = (uint32_t*)(&self[1]);
4256
+ const uint16_t* heads = (uint16_t*)(&slot_offsets[1u << slot_bits]);
4257
+ const uint32_t* items = (uint32_t*)(&heads[1u << bucket_bits]);
4258
+ const uint8_t* source = NULL;
4259
+
4260
+ const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
4261
+ size_t best_len = min_length;
4262
+ const uint64_t h =
4263
+ (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) *
4264
+ kPreparedDictionaryHashMul64Long;
4265
+ const uint32_t key = (uint32_t)(h >> hash_shift);
4266
+ const uint32_t slot = key & slot_mask;
4267
+ const uint32_t head = heads[key];
4268
+ const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head];
4269
+ uint32_t item = (head == 0xFFFF) ? 1 : 0;
4270
+ size_t found = 0;
4271
+
4272
+ const void* tail = (void*)&items[self->num_items];
4273
+ if (self->magic == kPreparedDictionaryMagic) {
4274
+ source = (const uint8_t*)tail;
4275
+ } else {
4276
+ /* kLeanPreparedDictionaryMagic */
4277
+ source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail);
4278
+ }
4279
+
4280
+ while (item == 0) {
4281
+ size_t offset;
4282
+ size_t distance;
4283
+ size_t limit;
4284
+ size_t len;
4285
+ item = *chain;
4286
+ chain++;
4287
+ offset = item & 0x7FFFFFFF;
4288
+ item &= 0x80000000;
4289
+ distance = distance_offset - offset;
4290
+ limit = source_size - offset;
4291
+ limit = (limit > max_length) ? max_length : limit;
4292
+ if (distance > max_distance) continue;
4293
+ if (cur_ix_masked + best_len > ring_buffer_mask ||
4294
+ best_len >= limit ||
4295
+ data[cur_ix_masked + best_len] != source[offset + best_len]) {
4296
+ continue;
4297
+ }
4298
+ len = FindMatchLengthWithLimit(
4299
+ &source[offset], &data[cur_ix_masked], limit);
4300
+ if (len > best_len) {
4301
+ best_len = len;
4302
+ InitBackwardMatch(matches++, distance, len);
4303
+ found++;
4304
+ if (found == match_limit) break;
4305
+ }
4306
+ }
4307
+ return found;
4308
+ }
4309
+
4310
+ static BROTLI_INLINE void LookupCompoundDictionaryMatch(
4311
+ const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data,
4312
+ const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
4313
+ const size_t cur_ix, const size_t max_length,
4314
+ const size_t max_ring_buffer_distance, const size_t max_distance,
4315
+ HasherSearchResult* sr) {
4316
+ size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1;
4317
+ size_t d;
4318
+ for (d = 0; d < addon->num_chunks; ++d) {
4319
+ /* Only one prepared dictionary type is currently supported. */
4320
+ FindCompoundDictionaryMatch(
4321
+ (const PreparedDictionary*)addon->chunks[d], data, ring_buffer_mask,
4322
+ distance_cache, cur_ix, max_length,
4323
+ base_offset - addon->chunk_offsets[d], max_distance, sr);
4324
+ }
4325
+ }
4326
+
4327
+ static BROTLI_INLINE size_t LookupAllCompoundDictionaryMatches(
4328
+ const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data,
4329
+ const size_t ring_buffer_mask, const size_t cur_ix, size_t min_length,
4330
+ const size_t max_length, const size_t max_ring_buffer_distance,
4331
+ const size_t max_distance, BackwardMatch* matches,
4332
+ size_t match_limit) {
4333
+ size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1;
4334
+ size_t d;
4335
+ size_t total_found = 0;
4336
+ for (d = 0; d < addon->num_chunks; ++d) {
4337
+ /* Only one prepared dictionary type is currently supported. */
4338
+ total_found += FindAllCompoundDictionaryMatches(
4339
+ (const PreparedDictionary*)addon->chunks[d], data, ring_buffer_mask,
4340
+ cur_ix, min_length, max_length, base_offset - addon->chunk_offsets[d],
4341
+ max_distance, matches + total_found, match_limit - total_found);
4342
+ if (total_found == match_limit) break;
4343
+ if (total_found > 0) {
4344
+ min_length = BackwardMatchLength(&matches[total_found - 1]);
4345
+ }
4346
+ }
4347
+ return total_found;
4348
+ }
4349
+
4350
+ }
4351
+
4352
+ #endif /* BROTLI_ENC_HASH_H_ */