Commits

Mikhail Korobov committed a154243

read_pascal_string is fixed

Comments (0)

Files changed (3)

psd_tools/reader/image_resources.py

 
     resource_section_length = read_fmt("I", fp)[0]
     position = fp.tell()
+    end_position = position + resource_section_length
+
     blocks = []
-    while fp.tell() < position + resource_section_length:
+    while fp.tell() < end_position:
         block = _read_block(fp, encoding)
         logger.debug("%r", block)
         blocks.append(block)
 
     resource_id = read_fmt("H", fp)[0]
     name = read_pascal_string(fp, encoding, 2)
-    fp.seek(1, 1) # XXX: why is this needed??
 
     data_size = pad(read_fmt("I", fp)[0], 2)
     data = fp.read(data_size)

psd_tools/reader/layers.py

     """
     Reads single layer record.
     """
-    top, left, bottom, right = read_fmt("4i", fp)
-    num_channels = read_fmt("H", fp)[0]
+    top, left, bottom, right, num_channels = read_fmt("4i H", fp)
 
     channel_info = []
     for channel_num in range(num_channels):
     start = fp.tell()
     mask_data = _read_layer_mask_data(fp)
     blending_ranges = _read_layer_blending_ranges(fp)
-    name = read_pascal_string(fp, encoding, 1) # XXX: spec says padding should be 4?
+    name = read_pascal_string(fp, encoding, 4)
 
     remaining_length = extra_length - (fp.tell()-start)
     tagged_blocks = _read_layer_tagged_blocks(fp, remaining_length)
             break
         blocks.append(block)
         read_bytes = fp.tell() - start_pos
+
     return blocks
 
 def _read_additional_layer_info_block(fp):

psd_tools/utils.py

     return number
 
 def read_pascal_string(fp, encoding, padding=1):
-    length = pad(read_fmt("B", fp)[0], padding)
-    return fp.read(length).decode(encoding, 'replace')
+    length = read_fmt("B", fp)[0]
+
+    if length == 0:
+        fp.seek(padding-1, 1)
+        return ''
+
+    res = fp.read(length)
+
+    fp.seek(pad(length, padding) - length, 1)
+    return res.decode(encoding, 'replace')
 
 def read_unicode_string(fp):
     num_chars = read_fmt("I", fp)[0]