diff --git a/thing.rb b/thing.rb index d26258a..67f7093 100755 --- a/thing.rb +++ b/thing.rb @@ -385,7 +385,35 @@ if $0 == __FILE__ # size. node_count = node_list.count + 1 - file.write([node_count].pack("c")) + if node_count < 128 + file.write([node_count].pack("C")) + else + bits = Math.log(node_count, 2).ceil + bytes = (bits / 8).ceil + if bytes == 3 + # must write this as a 32-bit int + bytes = 4 + end + file.write([128 + bytes].pack("C")) + + # get the correct integer directive for pack() + case bytes + when 1 + # 8-bit unsigned + directive = "C" + when 2 + # 16-bit unsigned big-endian + directive = "n" + when 4 + # 32-bit unsigned big-endian + directive = "N" + else + # Give up. This is an impractical number of nodes. + puts("Too many nodes.") + exit(false) + end + file.write([node_count].pack(directive)) + end bit_file = BitWriter.new file binary_write(bit_file, [parent] + node_list, string_huff, node_huff)