Commits

Alex Mayfield  committed fd5bf1e

Return unencoded message in encoder if unencoded length is the same as encoded message. Also fix some bugs with zero-length padding.

  • Participants
  • Parent commits bc9fde0

Comments (0)

Files changed (2)

File pyskull/huffman.py

             binary = binary_string[i:i+8]
             encoded_string += chr(int(binary[::-1], 2))
 
+        # If the huffman-coded string is longer than the original
+        # string, return the original string instead.  Putting an
+        # ASCII value 0xff where the padding bit should be signals to
+        # the decoder that the message is not encoded.
+        if len(data_string) <= len(encoded_string):
+            return ''.join(['\xff', data_string])
+
         # In the first byte, store the number of padding bits
-        padding_value = 8 - (len(binary_string) % 8)
+        padding_value = (8 - (len(binary_string) % 8)) % 8
         encoded_string = chr(padding_value) + encoded_string
 
         return encoded_string
         padding_length = ord(data_string[0])
         data_string = data_string[1:]
 
-        # If the padding is set to 0xff the data is already decoded
+        # If the padding bit is set to 0xff the message is not encoded.
         if padding_length == 0xff:
             return data_string
 
             binary_string += '{0:08b}'.format(ord(char))[::-1]
 
         # Remove padding bits from the end
-        binary_string = binary_string[:-padding_length]
+        binary_string = binary_string[:len(binary_string) - padding_length]
 
         # Match binary to entries in the huffman tree
         decoded_string = '';

File pyskull/tests/huffman_test.py

 
 def basic_encode_test():
     h = huffman.HuffmanObject(huffman.SKULLTAG_FREQS)
-    encoded = h.encode("Hello World")
-    assert encoded == '\x04\xeb\x91F\x8c\x18\xe5\x91G5\x8e\xd8\x08'
+    encoded = h.encode('\x00\x73\x03\x05')
+    assert encoded == '\x02\x1a\x4b\x28'
 
 def basic_decode_test():
     h = huffman.HuffmanObject(huffman.SKULLTAG_FREQS)
-    decoded = h.decode('\x04\xeb\x91F\x8c\x18\xe5\x91G5\x8e\xd8\x08')
-    assert decoded == "Hello World"
+    decoded = h.decode('\x02\x1a\x4b\x28')
+    assert decoded == '\x00\x73\x03\x05'
+
+def basic_encode_zeropad_test():
+    h = huffman.HuffmanObject(huffman.SKULLTAG_FREQS)
+    encoded = h.encode('\x1d\x03\x59\x05')
+    assert encoded == '\x00\xbd\xc9\xa5'
+
+def basic_decode_zeropad_test():
+    h = huffman.HuffmanObject(huffman.SKULLTAG_FREQS)
+    decoded = h.decode('\x00\xbd\xc9\xa5')
+    assert decoded == '\x1d\x03\x59\x05'
+
+def unencoded_encode_test():
+    h = huffman.HuffmanObject(huffman.SKULLTAG_FREQS)
+    encoded = h.encode("Hello World")
+    assert encoded == '\xffHello World'
 
 def unencoded_decode_test():
     h = huffman.HuffmanObject(huffman.SKULLTAG_FREQS)