// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The static dictionary (RFC 7932 §8, Appendix A). //! //! 122,784 bytes of words, grouped by length from 4 to 24, which a stream //! may refer to with a distance reaching further back than the output it has //! produced. `dictionary.bin` is the DICT array of Appendix A, transcribed //! from the RFC's hexadecimal and checked against its CRC below. const std = @import("std"); /// The DICT array. pub const data: *const [size]u8 = @embedFile("dictionary.bin"); pub const min_word_length = 4; pub const max_word_length = 24; /// NDBITS: log2 of how many words of each length the dictionary holds. pub const size_bits_by_length = [max_word_length + 1]u5{ 0, 0, 0, 0, 10, 10, 11, 11, 10, 10, 10, 10, 10, 9, 9, 8, 7, 7, 8, 7, 7, 6, 6, 5, 5, }; /// DOFFSET: where the words of each length start in `data`. pub const offsets_by_length = blk: { var offsets: [max_word_length + 2]u32 = undefined; offsets[0] = 0; for (0..max_word_length + 1) |len| { const words = if (len < min_word_length) 0 else @as(u32, 1) << size_bits_by_length[len]; offsets[len + 1] = offsets[len] + @as(u32, len) * words; } break :blk offsets; }; /// DICTSIZE. pub const size = offsets_by_length[max_word_length + 1]; /// The base word of the given length and index. `index` must be below /// `1 << size_bits_by_length[length]`. pub fn word(length: usize, index: usize) []const u8 { std.debug.assert(length >= min_word_length and length <= max_word_length); std.debug.assert(index < @as(usize, 1) << size_bits_by_length[length]); const start = offsets_by_length[length] + index * length; return data[start..][0..length]; } test "dictionary matches the check value in RFC 7932 Appendix A" { try std.testing.expectEqual(122784, size); try std.testing.expectEqual(0x5136cb04, std.hash.Crc32.hash(data)); } test "words" { try std.testing.expectEqualStrings("time", word(4, 0)); try std.testing.expectEqualStrings("down", word(4, 1)); }