sq epan/dissectors/pidl/rcg/rcg.cnf
[wireshark-sm.git] / tools / make-packet-dcm.py
blobd122dbff9f3b3f97830818ec61238108e5c828c7
1 #!/usr/bin/env python3
2 import os.path
3 import sys
4 import itertools
5 import lxml.etree
7 # This utility scrapes the DICOM standard document in DocBook format, finds the appropriate tables,
8 # and extracts the data needed to build the lists of DICOM attributes, UIDs and value representations.
10 # If the files part05.xml, part06.xml and part07.xml exist in the current directory, use them.
11 # Otherwise, download the current release from the current DICOM official sources.
12 if os.path.exists("part05.xml"):
13 print("Using local part05 docbook.", file=sys.stderr)
14 part05 = lxml.etree.parse("part05.xml")
15 else:
16 print("Downloading part05 docbook...", file=sys.stderr)
17 part05 = lxml.etree.parse("http://dicom.nema.org/medical/dicom/current/source/docbook/part05/part05.xml")
18 if os.path.exists("part06.xml"):
19 print("Using local part06 docbook.", file=sys.stderr)
20 part06 = lxml.etree.parse("part06.xml")
21 else:
22 print("Downloading part06 docbook...", file=sys.stderr)
23 part06 = lxml.etree.parse("http://dicom.nema.org/medical/dicom/current/source/docbook/part06/part06.xml")
24 if os.path.exists("part07.xml"):
25 print("Using local part07 docbook.", file=sys.stderr)
26 part07 = lxml.etree.parse("part07.xml")
27 else:
28 print("Downloading part07 docbook...", file=sys.stderr)
29 part07 = lxml.etree.parse("http://dicom.nema.org/medical/dicom/current/source/docbook/part07/part07.xml")
30 dbns = {'db':'http://docbook.org/ns/docbook', 'xml':'http://www.w3.org/XML/1998/namespace'}
32 # When displaying the dissected packets, some attributes are nice to include in the description of their parent.
33 include_in_parent = {"Patient Position",
34 "ROI Number",
35 "ROI Name",
36 "Contour Geometric Type",
37 "Observation Number",
38 "ROI Observation Label",
39 "RT ROI Interpreted Type",
40 "Dose Reference Structure Type",
41 "Dose Reference Description",
42 "Dose Reference Type",
43 "Target Prescription Dose",
44 "Tolerance Table Label",
45 "Beam Limiting Device Position Tolerance",
46 "Number of Fractions Planned",
47 "Treatment Machine Name",
48 "RT Beam Limiting Device Type",
49 "Beam Number",
50 "Beam Name",
51 "Beam Type",
52 "Radiation Type",
53 "Wedge Type",
54 "Wedge ID",
55 "Wedge Angle",
56 "Material ID",
57 "Block Tray ID",
58 "Block Name",
59 "Applicator ID",
60 "Applicator Type",
61 "Control Point Index",
62 "Nominal Beam Energy",
63 "Cumulative Meterset Weight",
64 "Patient Setup Number"}
66 # Data elements are listed in three tables in Part 6:
67 # * Table 6-1. Registry of DICOM Data Elements
68 # * Table 7-1. Registry of DICOM File Meta Elements
69 # * Table 8-1. Registry of DICOM Directory Structuring Elements
70 # All three tables are in the same format and can be merged for processing.
72 # The Command data elements (used only in networking), are listed in two tables in Part 7:
73 # * Table E.1-1. Command Fields
74 # * Table E.2-1. Retired Command Fields
75 # The Retired Command Fields are missing the last column. For processing here,
76 # we just add a last column with "RET", and they can be parsed with the same
77 # as for the Data elements.
79 data_element_tables=["table_6-1", "table_7-1", "table_8-1"]
81 def get_trs(document, table_id):
82 return document.findall(f"//db:table[@xml:id='{table_id}']/db:tbody/db:tr",
83 namespaces=dbns)
85 data_trs = sum((get_trs(part06, table_id) for table_id in data_element_tables), [])
86 cmd_trs = get_trs(part07, "table_E.1-1")
87 retired_cmd_trs = get_trs(part07, "table_E.2-1")
89 def get_texts_in_row(tr):
90 tds = tr.findall("db:td", namespaces=dbns)
91 texts = [" ".join(x.replace('\u200b', '').replace('\u00b5', 'u').strip() for x in td.itertext() if x.strip() != '') for td in tds]
92 return texts
94 data_rows = [get_texts_in_row(x) for x in data_trs]
95 retired_cmd_rows = [get_texts_in_row(x) for x in retired_cmd_trs]
96 cmd_rows = ([get_texts_in_row(x) for x in cmd_trs] +
97 [x + ["RET"] for x in retired_cmd_rows])
99 def parse_tag(tag):
100 # To handle some old cases where "x" is included as part of the tag number
101 tag = tag.replace("x", "0")
102 return f"0x{tag[1:5]}{tag[6:10]}"
103 def parse_ret(ret):
104 if ret.startswith("RET"):
105 return -1
106 else:
107 return 0
108 def include_in_parent_bit(name):
109 if name in include_in_parent:
110 return -1
111 else:
112 return 0
113 def text_for_row(row):
114 return f' {{ {parse_tag(row[0])}, "{row[1]}", "{row[3]}", "{row[4]}", {parse_ret(row[5])}, {include_in_parent_bit(row[1])}}},'
116 def text_for_rows(rows):
117 return "\n".join(text_for_row(row) for row in rows)
119 vrs = {i+1: get_texts_in_row(x)[0].split(maxsplit=1) for i,x in enumerate(get_trs(part05, "table_6.2-1"))}
122 # Table A-1. UID Values
123 uid_trs = get_trs(part06, "table_A-1")
124 uid_rows = [get_texts_in_row(x) for x in uid_trs]
126 wkfr_trs = get_trs(part06, "table_A-2")
127 wkfr_rows = [get_texts_in_row(x) for x in wkfr_trs]
128 uid_rows += [x[:3] + ['Well-known frame of reference'] + x[3:] for x in wkfr_rows]
130 def uid_define_name(uid):
131 if uid[1] == "(Retired)":
132 return f'"{uid[0]}"'
133 uid_type = uid[3]
134 uid_name = uid[1]
135 uid_name = re.sub(":.*", "", uid[1])
136 if uid_name.endswith(uid_type):
137 uid_name = uid_name[:-len(uid_type)].strip()
138 return f"DCM_UID_{definify(uid_type)}_{definify(uid_name)}"
140 import re
141 def definify(s):
142 return re.sub('[^A-Z0-9]+', '_', re.sub(' +', ' ', re.sub('[^-A-Z0-9 ]+', '', s.upper())))
144 uid_rows = sorted(uid_rows, key=lambda uid_row: [int(i) for i in uid_row[0].split(".")])
145 packet_dcm_h = """/* packet-dcm.h
146 * Definitions for DICOM dissection
147 * Copyright 2003, Rich Coe <richcoe2@gmail.com>
148 * Copyright 2008-2018, David Aggeler <david_aggeler@hispeed.ch>
150 * DICOM communication protocol: https://www.dicomstandard.org/current/
152 * Generated automatically by """ + os.path.basename(sys.argv[0]) + """ from the following sources:
154 * """ + part05.find("./db:subtitle", namespaces=dbns).text + """
155 * """ + part06.find("./db:subtitle", namespaces=dbns).text + """
156 * """ + part07.find("./db:subtitle", namespaces=dbns).text + """
158 * Wireshark - Network traffic analyzer
159 * By Gerald Combs <gerald@wireshark.org>
160 * Copyright 1998 Gerald Combs
162 * SPDX-License-Identifier: GPL-2.0-or-later
165 #ifndef __PACKET_DCM_H__
166 #define __PACKET_DCM_H__
168 #ifdef __cplusplus
169 extern "C" {
170 #endif /* __cplusplus */
172 """ + "\n".join(f"#define DCM_VR_{vr[0]} {i:2d} /* {vr[1]:25s} */" for i,vr in vrs.items()) + """
174 /* Following must be in the same order as the definitions above */
175 static const char* dcm_tag_vr_lookup[] = {
176 " ",
177 """ + ",\n ".join(",".join(f'"{x[1][0]}"' for x in j[1]) for j in itertools.groupby(vrs.items(), lambda i: (i[0]-1)//8)) + """
181 /* ---------------------------------------------------------------------
182 * DICOM Tag Definitions
184 * Some Tags can have different VRs
186 * Group 1000 is not supported, multiple tags with same description (retired anyhow)
187 * Group 7Fxx is not supported, multiple tags with same description (retired anyhow)
189 * Tags (0020,3100 to 0020, 31FF) not supported, multiple tags with same description (retired anyhow)
191 * Repeating groups (50xx & 60xx) are manually added. Declared as 5000 & 6000
194 typedef struct dcm_tag {
195 const uint32_t tag;
196 const char *description;
197 const char *vr;
198 const char *vm;
199 const bool is_retired;
200 const bool add_to_summary; /* Add to parent's item description */
201 } dcm_tag_t;
203 static dcm_tag_t const dcm_tag_data[] = {
205 /* Command Tags */
206 """ + text_for_rows(cmd_rows) + """
208 /* Data Tags */
209 """ + text_for_rows(data_rows) + """
212 /* ---------------------------------------------------------------------
213 * DICOM UID Definitions
215 * Part 6 lists following different UID Types (2006-2008)
217 * Application Context Name
218 * Coding Scheme
219 * DICOM UIDs as a Coding Scheme
220 * LDAP OID
221 * Meta SOP Class
222 * SOP Class
223 * Service Class
224 * Transfer Syntax
225 * Well-known Print Queue SOP Instance
226 * Well-known Printer SOP Instance
227 * Well-known SOP Instance
228 * Well-known frame of reference
231 typedef struct dcm_uid {
232 const char *value;
233 const char *name;
234 const char *type;
235 } dcm_uid_t;
237 """ + "\n".join(f'#define {uid_define_name(uid)} "{uid[0]}"'
238 for uid in uid_rows if uid[1] != '(Retired)') + """
240 static dcm_uid_t const dcm_uid_data[] = {
241 """ + "\n".join(f' {{ {uid_define_name(uid)}, "{uid[1]}", "{uid[3]}"}},'
242 for uid in uid_rows)+ """
245 #ifdef __cplusplus
247 #endif /* __cplusplus */
249 #endif /* packet-dcm.h */"""
251 print(packet_dcm_h)