forked from ashdnazg/pyreshark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_ws_consts.py
More file actions
executable file
·143 lines (117 loc) · 4.55 KB
/
generate_ws_consts.py
File metadata and controls
executable file
·143 lines (117 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'''
@summary: Generates python\cal\ws_consts.py according to Wireshark's source.
'''
# generate_ws_consts.py
#
# Pyreshark Plugin for Wireshark. (https://github.com/ashdnazg/pyreshark)
#
# Copyright (c) 2013 by Eshed Shaham.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import sys
import os.path
COMMENT_CHAR = "#"
CAPTION_WIDTH = 40
HEADER = """'''
@summary: Auto Generated by generate_ws_consts.py. Sets all the required constants from Wireshark.
'''
"""
ENUM_FILE_NAME = 0
ENUM_NAME = 1
ENUM_CAPTION = 2
ENUMS = [(os.path.join("..","..","epan","ftypes","ftypes.h"), "ftenum", "FIELD TYPES"),
(os.path.join("..","..","epan","proto.h"), "base_display_e", "DISPLAY VALUES"),
(os.path.join("..","..","epan","time_fmt.h"), "absolute_time_display_e", "DISPLAY VALUES (TIME)"),
(os.path.join("..","..","epan","column_info.h"), "", "COLUMN IDS"),]
DEFINE = "#define"
DEFINE_FILE_NAME = 0
DEFINE_PREFIX = 1
DEFINE_CAPTION = 2
DEFINES = [(os.path.join("..","..","epan","proto.h"), "enc_", "ENCODINGS"),
(os.path.join("..","..","epan","proto.h"), "base_","DISPLAY VALUES (MORE)")]
MISC_CAPTION = "MISC"
MISC = """
if "ENC_ASCII" in dir():
ENC_TEXT_DEFAULT = ENC_ASCII
elif "ENC_UTF8" in dir():
ENC_TEXT_DEFAULT = ENC_UTF8
else:
ENC_TEXT_DEFAULT = ENC_NA
HFILL = (0, 0, 0, 0, None, None)
DATA = "data"
REMAINING_LENGTH = -1
"""
def get_enum(file_name, enum_name):
all_values = []
current_value = 0
in_file = open(file_name, "rb")
found = False
line = in_file.readline()
while True:
while line != "" and not "enum" in line:
line = in_file.readline()
if line == "":
break
if enum_name in line:
found = True
line = line = in_file.readline()
while "}" not in line:
entry = line.split("/")[0].split(",")[0].strip()
if entry != "":
if "=" in entry:
(name, current_value) = entry.split("=")
current_value = int(current_value)
else:
name = entry
all_values.append((name, str(current_value)))
current_value += 1
line = in_file.readline()
if found or enum_name in line:
break
else:
all_values = []
in_file.close()
return all_values
def get_define(file_name, define_prefix):
all_values = []
in_file = open(file_name, "rb")
line = in_file.readline()
while line != "":
if line.strip().lower().startswith(DEFINE) and line.strip().lower().replace(DEFINE,"").strip().startswith(define_prefix):
entry = line.split("/")[0].strip()[len(DEFINE):].strip().replace("\t", " ")
space_position = entry.index(" ")
all_values.append((entry[:space_position].strip(), entry[space_position:].strip()))
line = in_file.readline()
return all_values
def main(argv):
out_file = open(argv[1], "wb")
out_file.write(HEADER)
for enum in ENUMS:
out_file.write("%s\n" % ((" %s " % (enum[ENUM_CAPTION],)).center(CAPTION_WIDTH, COMMENT_CHAR),))
for name, value in get_enum(enum[ENUM_FILE_NAME], enum[ENUM_NAME]):
out_file.write("%s = %s\n" % (name, value))
out_file.write("\n")
for define in DEFINES:
out_file.write("%s\n" % ((" %s " % (define[DEFINE_CAPTION],)).center(CAPTION_WIDTH, COMMENT_CHAR),))
for name, value in get_define(define[DEFINE_FILE_NAME], define[DEFINE_PREFIX]):
out_file.write("%s = %s\n" % (name, value))
out_file.write("\n")
out_file.write("%s\n%s" % ((" %s " % (MISC_CAPTION,)).center(CAPTION_WIDTH, COMMENT_CHAR), MISC))
out_file.close()
if '__main__' == __name__:
if len(sys.argv) < 2:
print "Usage: %s <out_file>" % (sys.argv[0],)
exit(1)
main(sys.argv)