libpng/contrib/pngexif/bytepack.py
Cosmin Truta ec2e58c16a pngexif: Import pngexifinfo as an externally-contributed project
We used this experimental project in the development of the PNG-EXIF
("eXIf") specification, back in 2017. The project evolved together
with the draft specification, which was finalized on 2017-Jun-15 and
approved by the PNG Group on 2017-Jul-13.

The EXIF specification, outside of the scope of PNG and libpng, is
quite complex. The libpng implementation cannot grow too much beyond
performing basic integrity checks on top of serialization. In order
to create and manipulate PNG-EXIF image files, the use of external
libraries and tools such as ExifTool is necessary.

Now, with the addition of contrib/pngexif to the libpng repository,
offline tasks like metadata inspection and linting can be performed
without importing external dependencies.
2024-02-22 11:32:53 +02:00

49 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python
"""
Byte packing and unpacking utilities.
Copyright (C) 2017-2020 Cosmin Truta.
Use, modification and distribution are subject to the MIT License.
Please see the accompanying file LICENSE_MIT.txt
"""
from __future__ import absolute_import, division, print_function
import struct
def unpack_uint32be(buffer, offset=0):
"""Unpack an unsigned int from its 32-bit big-endian representation."""
return struct.unpack(">I", buffer[offset:offset + 4])[0]
def unpack_uint32le(buffer, offset=0):
"""Unpack an unsigned int from its 32-bit little-endian representation."""
return struct.unpack("<I", buffer[offset:offset + 4])[0]
def unpack_uint16be(buffer, offset=0):
"""Unpack an unsigned int from its 16-bit big-endian representation."""
return struct.unpack(">H", buffer[offset:offset + 2])[0]
def unpack_uint16le(buffer, offset=0):
"""Unpack an unsigned int from its 16-bit little-endian representation."""
return struct.unpack("<H", buffer[offset:offset + 2])[0]
def unpack_uint8(buffer, offset=0):
"""Unpack an unsigned int from its 8-bit representation."""
return struct.unpack("B", buffer[offset:offset + 1])[0]
if __name__ == "__main__":
# For testing only.
assert unpack_uint32be(b"ABCDEF", 1) == 0x42434445
assert unpack_uint32le(b"ABCDEF", 1) == 0x45444342
assert unpack_uint16be(b"ABCDEF", 1) == 0x4243
assert unpack_uint16le(b"ABCDEF", 1) == 0x4342
assert unpack_uint8(b"ABCDEF", 1) == 0x42