_binary.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # Binary input/output support routines.
  6. #
  7. # Copyright (c) 1997-2003 by Secret Labs AB
  8. # Copyright (c) 1995-2003 by Fredrik Lundh
  9. # Copyright (c) 2012 by Brian Crowell
  10. #
  11. # See the README file for information on usage and redistribution.
  12. #
  13. """Binary input/output support routines."""
  14. from __future__ import annotations
  15. __lazy_modules__ = {"struct"}
  16. from struct import unpack_from
  17. def i8(c: bytes) -> int:
  18. return c[0]
  19. def o8(i: int) -> bytes:
  20. return (i & 255).to_bytes()
  21. # Input, le = little endian, be = big endian
  22. def i16le(c: bytes, o: int = 0) -> int:
  23. """
  24. Converts a 2-bytes (16 bits) string to an unsigned integer.
  25. :param c: string containing bytes to convert
  26. :param o: offset of bytes to convert in string
  27. """
  28. return unpack_from("<H", c, o)[0]
  29. def si16le(c: bytes, o: int = 0) -> int:
  30. """
  31. Converts a 2-bytes (16 bits) string to a signed integer.
  32. :param c: string containing bytes to convert
  33. :param o: offset of bytes to convert in string
  34. """
  35. return unpack_from("<h", c, o)[0]
  36. def si16be(c: bytes, o: int = 0) -> int:
  37. """
  38. Converts a 2-bytes (16 bits) string to a signed integer, big endian.
  39. :param c: string containing bytes to convert
  40. :param o: offset of bytes to convert in string
  41. """
  42. return unpack_from(">h", c, o)[0]
  43. def i32le(c: bytes, o: int = 0) -> int:
  44. """
  45. Converts a 4-bytes (32 bits) string to an unsigned integer.
  46. :param c: string containing bytes to convert
  47. :param o: offset of bytes to convert in string
  48. """
  49. return unpack_from("<I", c, o)[0]
  50. def si32le(c: bytes, o: int = 0) -> int:
  51. """
  52. Converts a 4-bytes (32 bits) string to a signed integer.
  53. :param c: string containing bytes to convert
  54. :param o: offset of bytes to convert in string
  55. """
  56. return unpack_from("<i", c, o)[0]
  57. def si32be(c: bytes, o: int = 0) -> int:
  58. """
  59. Converts a 4-bytes (32 bits) string to a signed integer, big endian.
  60. :param c: string containing bytes to convert
  61. :param o: offset of bytes to convert in string
  62. """
  63. return unpack_from(">i", c, o)[0]
  64. def i16be(c: bytes, o: int = 0) -> int:
  65. return unpack_from(">H", c, o)[0]
  66. def i32be(c: bytes, o: int = 0) -> int:
  67. return unpack_from(">I", c, o)[0]
  68. # Output, le = little endian, be = big endian
  69. def o16le(i: int) -> bytes:
  70. return i.to_bytes(2, "little")
  71. def o32le(i: int) -> bytes:
  72. return i.to_bytes(4, "little")
  73. def o16be(i: int) -> bytes:
  74. return i.to_bytes(2, "big")
  75. def o32be(i: int) -> bytes:
  76. return i.to_bytes(4, "big")