hexdump.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. pygments.lexers.hexdump
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for hexadecimal dumps.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, include
  9. from pygments.token import Name, Number, String, Punctuation, Whitespace
  10. __all__ = ['HexdumpLexer']
  11. class HexdumpLexer(RegexLexer):
  12. """
  13. For typical hex dump output formats by the UNIX and GNU/Linux tools ``hexdump``,
  14. ``hd``, ``hexcat``, ``od`` and ``xxd``, and the DOS tool ``DEBUG``. For example:
  15. .. sourcecode:: hexdump
  16. 00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
  17. 00000010 02 00 3e 00 01 00 00 00 c5 48 40 00 00 00 00 00 |..>......H@.....|
  18. The specific supported formats are the outputs of:
  19. * ``hexdump FILE``
  20. * ``hexdump -C FILE`` -- the `canonical` format used in the example.
  21. * ``hd FILE`` -- same as ``hexdump -C FILE``.
  22. * ``hexcat FILE``
  23. * ``od -t x1z FILE``
  24. * ``xxd FILE``
  25. * ``DEBUG.EXE FILE.COM`` and entering ``d`` to the prompt.
  26. """
  27. name = 'Hexdump'
  28. aliases = ['hexdump']
  29. url = 'https://en.wikipedia.org/wiki/Hex_dump'
  30. version_added = '2.1'
  31. hd = r'[0-9A-Ha-h]'
  32. tokens = {
  33. 'root': [
  34. (r'\n', Whitespace),
  35. include('offset'),
  36. (r'('+hd+r'{2})(\-)('+hd+r'{2})',
  37. bygroups(Number.Hex, Punctuation, Number.Hex)),
  38. (hd+r'{2}', Number.Hex),
  39. (r'(\s{2,3})(\>)(.{16})(\<)$',
  40. bygroups(Whitespace, Punctuation, String, Punctuation), 'bracket-strings'),
  41. (r'(\s{2,3})(\|)(.{16})(\|)$',
  42. bygroups(Whitespace, Punctuation, String, Punctuation), 'piped-strings'),
  43. (r'(\s{2,3})(\>)(.{1,15})(\<)$',
  44. bygroups(Whitespace, Punctuation, String, Punctuation)),
  45. (r'(\s{2,3})(\|)(.{1,15})(\|)$',
  46. bygroups(Whitespace, Punctuation, String, Punctuation)),
  47. (r'(\s{2,3})(.{1,15})$', bygroups(Whitespace, String)),
  48. (r'(\s{2,3})(.{16}|.{20})$', bygroups(Whitespace, String), 'nonpiped-strings'),
  49. (r'\s', Whitespace),
  50. (r'^\*', Punctuation),
  51. ],
  52. 'offset': [
  53. (r'^('+hd+'+)(:)', bygroups(Name.Label, Punctuation), 'offset-mode'),
  54. (r'^'+hd+'+', Name.Label),
  55. ],
  56. 'offset-mode': [
  57. (r'\s', Whitespace, '#pop'),
  58. (hd+'+', Name.Label),
  59. (r':', Punctuation)
  60. ],
  61. 'piped-strings': [
  62. (r'\n', Whitespace),
  63. include('offset'),
  64. (hd+r'{2}', Number.Hex),
  65. (r'(\s{2,3})(\|)(.{1,16})(\|)$',
  66. bygroups(Whitespace, Punctuation, String, Punctuation)),
  67. (r'\s', Whitespace),
  68. (r'^\*', Punctuation),
  69. ],
  70. 'bracket-strings': [
  71. (r'\n', Whitespace),
  72. include('offset'),
  73. (hd+r'{2}', Number.Hex),
  74. (r'(\s{2,3})(\>)(.{1,16})(\<)$',
  75. bygroups(Whitespace, Punctuation, String, Punctuation)),
  76. (r'\s', Whitespace),
  77. (r'^\*', Punctuation),
  78. ],
  79. 'nonpiped-strings': [
  80. (r'\n', Whitespace),
  81. include('offset'),
  82. (r'('+hd+r'{2})(\-)('+hd+r'{2})',
  83. bygroups(Number.Hex, Punctuation, Number.Hex)),
  84. (hd+r'{2}', Number.Hex),
  85. (r'(\s{19,})(.{1,20}?)$', bygroups(Whitespace, String)),
  86. (r'(\s{2,3})(.{1,20})$', bygroups(Whitespace, String)),
  87. (r'\s', Whitespace),
  88. (r'^\*', Punctuation),
  89. ],
  90. }