- /* Check for table overrun. This can only happen if the
- * given lengths do not correspond to a valid Huffman
- * tree. */
- if (decode_table_pos >= table_num_entries) {
- ERROR("Huffman decoding table overrun: "
- "pos = %u, num_entries = %u",
- decode_table_pos, table_num_entries);
- return 1;
+ /* Fill entries for codewords short enough for a direct mapping. We can
+ * take advantage of the ordering of the codewords, since the Huffman
+ * code is canonical. It must be the case that all the codewords of
+ * some length L numerically precede all the codewords of length L + 1.
+ * Furthermore, if we have 2 symbols A and B with the same codeword
+ * length but symbol A is sorted before symbol B, then then we know that
+ * the codeword for A numerically precedes the codeword for B. */
+ unsigned decode_table_pos = 0;
+ unsigned i = 0;
+
+ wimlib_assert2(num_used_syms != 0);
+ while (1) {
+ unsigned sym = sorted_syms[i];
+ unsigned codeword_len = lens[sym];
+ if (codeword_len > table_bits)
+ break;
+
+ unsigned num_entries = 1 << (table_bits - codeword_len);
+ const unsigned entries_per_long = sizeof(unsigned long) /
+ sizeof(decode_table[0]);
+ if (num_entries >= entries_per_long) {
+ /* Fill in the Huffman decode table entries one unsigned
+ * long at a time. On 32-bit systems this is 2 entries
+ * per store, while on 64-bit systems this is 4 entries
+ * per store. */
+ wimlib_assert2(decode_table_pos % entries_per_long == 0);
+ BUILD_BUG_ON(sizeof(unsigned long) != 4 &&
+ sizeof(unsigned long) != 8);
+
+ unsigned long *p = (unsigned long *)&decode_table[decode_table_pos];
+ unsigned n = num_entries / entries_per_long;
+ unsigned long v = sym;
+ if (sizeof(unsigned long) >= 4)
+ v |= v << 16;
+ if (sizeof(unsigned long) >= 8) {
+ /* This may produce a compiler warning if an
+ * unsigned long is 32 bits, but this won't be
+ * executed unless an unsigned long is at least
+ * 64 bits anyway. */
+ v |= v << 32;