Kyle Altendorf <s...@fstab.net> added the comment:

Python 3.7 works with 2-byte elements, I managed to find the wrong section in 
the doc-linked docs.

4.5 Extensible data fields
    --------------------------
    
       4.5.1 In order to allow different programs and different types
       of information to be stored in the 'extra' field in .ZIP
       files, the following structure MUST be used for all
       programs storing data in this field:
    
           header1+data1 + header2+data2 . . .
    
       Each header should consist of:
    
           Header ID - 2 bytes
           Data Size - 2 bytes
    
       Note: all fields stored in Intel low-byte/high-byte order.


And the test showing it working.

----
    import sys
    import zipfile
    
    print(sys.version)
    
    fn = sys.argv[1]
    print(fn)
    
    options = {
        'endianness': ('little', 'big'),
        'header_element_bytes': (2, 4),
        'additional_size': (0, 4, 4 + 4),
    }
    
    for endianness in options['endianness']:
        for additional_size in options['additional_size']:
            for header_element_bytes in options['header_element_bytes']:
                print('\n\n --- trying endianness: {}, additional_size: {}, 
header_element_bytes: {}'.format(endianness, additional_size, 
header_element_bytes))
                with zipfile.ZipFile(fn, 'w') as zf:
                    zi = zipfile.ZipInfo("0")
                    extra_data = b"hello, extra, and some more just to make it 
longer and such so yeah"
                    zi.extra = (
                        (42).to_bytes(header_element_bytes, endianness)
                        + (len(extra_data) + 
additional_size).to_bytes(header_element_bytes, endianness)
                        + extra_data
                    )
                    zf.writestr(zi, b"the real data")
    
                try:
                    zipfile.ZipFile(fn)
                except Exception as e:
                    print(e)
                else:
                    print('success')

----
    altendky@lt:~/twisted$ python3.7 ../z.py 37.py
    3.7.0 (default, Jul  7 2018, 15:49:24)
    [GCC 6.3.0 20170516]
    37.py
    
    
     --- trying endianness: little, additional_size: 0, header_element_bytes: 2
    success
    
    
     --- trying endianness: little, additional_size: 0, header_element_bytes: 4
    Corrupt extra field 6568 (size=27756)
    
    
     --- trying endianness: little, additional_size: 4, header_element_bytes: 2
    Corrupt extra field 002a (size=71)
    
    
     --- trying endianness: little, additional_size: 4, header_element_bytes: 4
    Corrupt extra field 6568 (size=27756)
    
    
     --- trying endianness: little, additional_size: 8, header_element_bytes: 2
    Corrupt extra field 002a (size=75)
    
    
     --- trying endianness: little, additional_size: 8, header_element_bytes: 4
    Corrupt extra field 6568 (size=27756)
    
    
     --- trying endianness: big, additional_size: 0, header_element_bytes: 2
    Corrupt extra field 2a00 (size=17152)
    
    
     --- trying endianness: big, additional_size: 0, header_element_bytes: 4
    Corrupt extra field 0000 (size=10752)
    
    
     --- trying endianness: big, additional_size: 4, header_element_bytes: 2
    Corrupt extra field 2a00 (size=18176)
    
    
     --- trying endianness: big, additional_size: 4, header_element_bytes: 4
    Corrupt extra field 0000 (size=10752)
    
    
     --- trying endianness: big, additional_size: 8, header_element_bytes: 2
    Corrupt extra field 2a00 (size=19200)
    
    
     --- trying endianness: big, additional_size: 8, header_element_bytes: 4
    Corrupt extra field 0000 (size=10752)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue34606>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to