From 1abcc9b425a82c6e670800a778ed8b823c2d2c5d Mon Sep 17 00:00:00 2001 From: Michael Hrivnak Date: Tue, 23 Oct 2012 17:26:39 -0400 Subject: [PATCH 1/2] implementing the byte length field for number of nodes. this makes it possible to correctly represent 128 or more nodes. --- thing.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/thing.rb b/thing.rb index d26258a..dac966e 100755 --- a/thing.rb +++ b/thing.rb @@ -385,7 +385,31 @@ 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 + 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 3..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) From e6bef92ab7e35a99f2dda84d9bbacd08b79d4984 Mon Sep 17 00:00:00 2001 From: Michael Hrivnak Date: Tue, 23 Oct 2012 19:49:23 -0400 Subject: [PATCH 2/2] fixed bug where 4 bytes of data would be written with only 3 expected. --- thing.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/thing.rb b/thing.rb index dac966e..67f7093 100755 --- a/thing.rb +++ b/thing.rb @@ -390,6 +390,10 @@ if $0 == __FILE__ 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() @@ -400,7 +404,7 @@ if $0 == __FILE__ when 2 # 16-bit unsigned big-endian directive = "n" - when 3..4 + when 4 # 32-bit unsigned big-endian directive = "N" else