hexlify_codec.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #! python
  2. #
  3. # This is a codec to create and decode hexdumps with spaces between characters. used by miniterm.
  4. #
  5. # This file is part of pySerial. https://github.com/pyserial/pyserial
  6. # (C) 2015-2016 Chris Liechti <cliechti@gmx.net>
  7. #
  8. # SPDX-License-Identifier: BSD-3-Clause
  9. """\
  10. Python 'hex' Codec - 2-digit hex with spaces content transfer encoding.
  11. Encode and decode may be a bit missleading at first sight...
  12. The textual representation is a hex dump: e.g. "40 41"
  13. The "encoded" data of this is the binary form, e.g. b"@A"
  14. Therefore decoding is binary to text and thus converting binary data to hex dump.
  15. """
  16. from __future__ import absolute_import
  17. import codecs
  18. import serial
  19. try:
  20. unicode
  21. except (NameError, AttributeError):
  22. unicode = str # for Python 3, pylint: disable=redefined-builtin,invalid-name
  23. HEXDIGITS = '0123456789ABCDEF'
  24. # Codec APIs
  25. def hex_encode(data, errors='strict'):
  26. """'40 41 42' -> b'@ab'"""
  27. return (serial.to_bytes([int(h, 16) for h in data.split()]), len(data))
  28. def hex_decode(data, errors='strict'):
  29. """b'@ab' -> '40 41 42'"""
  30. return (unicode(''.join('{:02X} '.format(ord(b)) for b in serial.iterbytes(data))), len(data))
  31. class Codec(codecs.Codec):
  32. def encode(self, data, errors='strict'):
  33. """'40 41 42' -> b'@ab'"""
  34. return serial.to_bytes([int(h, 16) for h in data.split()])
  35. def decode(self, data, errors='strict'):
  36. """b'@ab' -> '40 41 42'"""
  37. return unicode(''.join('{:02X} '.format(ord(b)) for b in serial.iterbytes(data)))
  38. class IncrementalEncoder(codecs.IncrementalEncoder):
  39. """Incremental hex encoder"""
  40. def __init__(self, errors='strict'):
  41. self.errors = errors
  42. self.state = 0
  43. def reset(self):
  44. self.state = 0
  45. def getstate(self):
  46. return self.state
  47. def setstate(self, state):
  48. self.state = state
  49. def encode(self, data, final=False):
  50. """\
  51. Incremental encode, keep track of digits and emit a byte when a pair
  52. of hex digits is found. The space is optional unless the error
  53. handling is defined to be 'strict'.
  54. """
  55. state = self.state
  56. encoded = []
  57. for c in data.upper():
  58. if c in HEXDIGITS:
  59. z = HEXDIGITS.index(c)
  60. if state:
  61. encoded.append(z + (state & 0xf0))
  62. state = 0
  63. else:
  64. state = 0x100 + (z << 4)
  65. elif c == ' ': # allow spaces to separate values
  66. if state and self.errors == 'strict':
  67. raise UnicodeError('odd number of hex digits')
  68. state = 0
  69. else:
  70. if self.errors == 'strict':
  71. raise UnicodeError('non-hex digit found: {!r}'.format(c))
  72. self.state = state
  73. return serial.to_bytes(encoded)
  74. class IncrementalDecoder(codecs.IncrementalDecoder):
  75. """Incremental decoder"""
  76. def decode(self, data, final=False):
  77. return unicode(''.join('{:02X} '.format(ord(b)) for b in serial.iterbytes(data)))
  78. class StreamWriter(Codec, codecs.StreamWriter):
  79. """Combination of hexlify codec and StreamWriter"""
  80. class StreamReader(Codec, codecs.StreamReader):
  81. """Combination of hexlify codec and StreamReader"""
  82. def getregentry():
  83. """encodings module API"""
  84. return codecs.CodecInfo(
  85. name='hexlify',
  86. encode=hex_encode,
  87. decode=hex_decode,
  88. incrementalencoder=IncrementalEncoder,
  89. incrementaldecoder=IncrementalDecoder,
  90. streamwriter=StreamWriter,
  91. streamreader=StreamReader,
  92. #~ _is_text_encoding=True,
  93. )