2020-11-03 19:22:14 +00:00
|
|
|
#!/usr/bin/env python3
|
2009-03-11 22:12:05 +00:00
|
|
|
#
|
|
|
|
# rdps.py
|
|
|
|
#
|
|
|
|
# Wireshark - Network traffic analyzer
|
|
|
|
# By Gerald Combs <gerald@wireshark.org>
|
|
|
|
# Copyright 1998 Gerald Combs
|
|
|
|
#
|
2018-02-12 13:07:48 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2009-03-11 22:12:05 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
'''\
|
|
|
|
takes the file listed as the first argument and creates the file listed
|
2009-03-12 07:04:46 +00:00
|
|
|
as the second argument. It takes a PostScript file and creates a C source
|
2009-03-11 22:12:05 +00:00
|
|
|
with 2 functions:
|
|
|
|
print_ps_preamble()
|
|
|
|
print_ps_finale()
|
|
|
|
|
|
|
|
Ported to Python from rdps.c.
|
|
|
|
'''
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os.path
|
|
|
|
|
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
|
|
|
|
2009-03-11 22:12:05 +00:00
|
|
|
def ps_clean_string(raw_str):
|
|
|
|
ps_str = ''
|
|
|
|
for c in raw_str:
|
|
|
|
if c == '\\':
|
|
|
|
ps_str += '\\\\'
|
|
|
|
elif c == '%':
|
|
|
|
ps_str += '%%'
|
|
|
|
elif c == '\n':
|
|
|
|
ps_str += '\\n'
|
|
|
|
else:
|
|
|
|
ps_str += c
|
|
|
|
return ps_str
|
|
|
|
|
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
|
|
|
|
2009-03-11 22:12:05 +00:00
|
|
|
def start_code(fd, func):
|
|
|
|
script_name = os.path.split(__file__)[-1]
|
|
|
|
fd.write("void print_ps_%s(FILE *fd) {\n" % func)
|
|
|
|
|
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
|
|
|
|
2009-03-11 22:12:05 +00:00
|
|
|
def write_code(fd, raw_str):
|
|
|
|
ps_str = ps_clean_string(raw_str)
|
|
|
|
fd.write("\tfprintf(fd, \"%s\");\n" % ps_str)
|
|
|
|
|
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
|
|
|
|
2009-03-11 22:12:05 +00:00
|
|
|
def end_code(fd):
|
|
|
|
fd.write("}\n\n\n")
|
|
|
|
|
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
|
|
|
|
2009-03-12 07:04:46 +00:00
|
|
|
def exit_err(msg=None, *param):
|
2009-03-11 22:12:05 +00:00
|
|
|
if msg is not None:
|
2009-03-12 07:04:46 +00:00
|
|
|
sys.stderr.write(msg % param)
|
2009-03-11 22:12:05 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
|
|
|
|
2009-03-11 22:12:05 +00:00
|
|
|
# Globals
|
|
|
|
STATE_NULL = 'null'
|
|
|
|
STATE_PREAMBLE = 'preamble'
|
|
|
|
STATE_FINALE = 'finale'
|
|
|
|
|
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
|
|
|
|
2009-03-11 22:12:05 +00:00
|
|
|
def main():
|
Fix issues discovered by common python linters
Fix some issues discovered by common python linters including:
* switch `None` comparisons to use `is` rather than `==`. Identity !=
equality, and I've spent 40+ hours before tracking down a subtle bug
caused by exactly this issue. Note that this may introduce a problem if
one of the scripts is depending on this behavior, in which case the
comparison should be changed to `True`/`False` rather than `None`.
* Use `except Exception:` as bare `except:` statements have been
discouraged for years. Ideally for some of these we'd examine if there
were specific exceptions that should be caught, but for now I simply
caught all. Again, this could introduce very subtle behavioral changes
under Python 2, but IIUC, that was all fixed in Python 3, so safe to
move to `except Exception:`.
* Use more idiomatic `if not x in y`--> `if x not in y`
* Use more idiomatic 2 blank lines. I only did this at the beginning,
until I realized how overwhelming this was going to be to apply, then I
stopped.
* Add a TODO where an undefined function name is called, so will fail
whenever that code is run.
* Add more idiomatic spacing around `:`. This is also only partially
cleaned up, as I gave up when I saw how `asn2wrs.py` was clearly
infatuated with the construct.
* Various other small cleanups, removed some trailing whitespace and
improper indentation that wasn't a multiple of 4, etc.
There is still _much_ to do, but I haven't been heavily involved with
this project before, so thought this was a sufficient amount to put up
and see what the feedback is.
Linters that I have enabled which highlighted some of these issues
include:
* `pylint`
* `flake8`
* `pycodestyle`
2020-09-21 05:44:41 +00:00
|
|
|
state = STATE_NULL
|
2009-03-11 22:12:05 +00:00
|
|
|
|
|
|
|
if len(sys.argv) != 3:
|
|
|
|
exit_err("%s: input_file output_file\n", __file__)
|
|
|
|
|
|
|
|
input = open(sys.argv[1], 'r')
|
|
|
|
output = open(sys.argv[2], 'w')
|
|
|
|
|
2009-03-12 07:04:46 +00:00
|
|
|
script_name = os.path.split(__file__)[-1]
|
|
|
|
|
2009-03-11 22:12:05 +00:00
|
|
|
output.write('''\
|
2009-10-04 07:32:06 +00:00
|
|
|
/* DO NOT EDIT
|
|
|
|
*
|
|
|
|
* Created by %s.
|
|
|
|
*
|
|
|
|
* ps.c
|
|
|
|
* Definitions for generating PostScript(R) packet output.
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-02-09 09:14:34 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2009-10-04 07:32:06 +00:00
|
|
|
*/
|
2009-03-11 22:12:05 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "ps.h"
|
|
|
|
|
2009-03-12 07:04:46 +00:00
|
|
|
''' % script_name)
|
2009-03-11 22:12:05 +00:00
|
|
|
|
2009-03-12 07:04:46 +00:00
|
|
|
for line in input:
|
2009-03-11 22:12:05 +00:00
|
|
|
#line = line.rstrip()
|
2021-01-24 16:21:00 +00:00
|
|
|
if state == STATE_NULL:
|
2009-03-11 22:12:05 +00:00
|
|
|
if line.startswith("% ---- wireshark preamble start ---- %"):
|
|
|
|
state = STATE_PREAMBLE
|
|
|
|
start_code(output, "preamble")
|
|
|
|
continue
|
|
|
|
elif line.startswith("% ---- wireshark finale start ---- %"):
|
|
|
|
state = STATE_FINALE
|
|
|
|
start_code(output, "finale")
|
|
|
|
continue
|
2021-01-24 16:21:00 +00:00
|
|
|
elif state == STATE_PREAMBLE:
|
2009-03-11 22:12:05 +00:00
|
|
|
if line.startswith("% ---- wireshark preamble end ---- %"):
|
|
|
|
state = STATE_NULL
|
|
|
|
end_code(output)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
write_code(output, line)
|
2021-01-24 16:21:00 +00:00
|
|
|
elif state == STATE_FINALE:
|
2009-03-11 22:12:05 +00:00
|
|
|
if line.startswith("% ---- wireshark finale end ---- %"):
|
|
|
|
state = STATE_NULL
|
|
|
|
end_code(output)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
write_code(output, line)
|
|
|
|
else:
|
|
|
|
exit_err("NO MATCH:%s", line)
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
|
2013-12-01 01:53:26 +00:00
|
|
|
#
|
2019-07-26 18:43:17 +00:00
|
|
|
# Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2013-12-01 01:53:26 +00:00
|
|
|
#
|
|
|
|
# Local variables:
|
|
|
|
# c-basic-offset: 4
|
|
|
|
# indent-tabs-mode: nil
|
|
|
|
# End:
|
|
|
|
#
|
|
|
|
# vi: set shiftwidth=4 expandtab:
|
|
|
|
# :indentSize=4:noTabs=true:
|
|
|
|
#
|