@@ -260,6 +260,18 @@ class FileStructure(object):
260260 _mode: 0x0
261261 _unused2: 0x0
262262 vtable: 0x0}
263+
264+ Bytes fields are padded to the correct field size.
265+ For example, _unused2 is 40 bytes on i386, so a short value gets
266+ zero-padded to 40, not to context.bytes (4):
267+
268+ >>> context.clear(arch='i386')
269+ >>> fileStr2 = FileStructure(null=0)
270+ >>> fileStr2.vtable = 0x561859f0
271+ >>> old_len = len(bytes(fileStr2))
272+ >>> fileStr2._unused2 = b'AB'
273+ >>> len(bytes(fileStr2)) == old_len
274+ True
263275 """
264276
265277 vars_ = []
@@ -300,7 +312,7 @@ def __bytes__(self):
300312 structure = b''
301313 for val in self .vars_ :
302314 if isinstance (getattr (self , val ), bytes ):
303- structure += getattr (self , val ).ljust (context . bytes , b'\x00 ' )
315+ structure += getattr (self , val ).ljust (self . length [ val ] , b'\x00 ' )
304316 else :
305317 if self .length [val ] > 0 :
306318 structure += pack (int (getattr (self , val )), self .length [val ]* 8 )
@@ -329,7 +341,7 @@ def struntil(self,v):
329341 structure = b''
330342 for val in self .vars_ :
331343 if isinstance (getattr (self , val ), bytes ):
332- structure += getattr (self , val ).ljust (context . bytes , b'\x00 ' )
344+ structure += getattr (self , val ).ljust (self . length [ val ] , b'\x00 ' )
333345 else :
334346 structure += pack (int (getattr (self , val )), self .length [val ]* 8 )
335347 if val == v :
0 commit comments