1 /* field_information.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include <ui/qt/utils/field_information.h>
14 FieldInformation::FieldInformation(field_info
*fi
, QObject
* parent
)
21 FieldInformation::FieldInformation(const ProtoNode
*node
, QObject
* parent
)
25 if (node
&& node
->isValid()) {
26 fi_
= node
->protoNode()->finfo
;
31 bool FieldInformation::isValid() const
35 if (fi_
&& fi_
->hfinfo
)
37 if (fi_
->hfinfo
->blurb
!= NULL
&& fi_
->hfinfo
->blurb
[0] != '\0') {
40 ret
= QString((fi_
->hfinfo
->name
)).length() > 0;
47 bool FieldInformation::isLink() const
49 if (fi_
&& fi_
->hfinfo
) {
50 if ((fi_
->hfinfo
->type
== FT_FRAMENUM
) ||
51 (FI_GET_FLAG(fi_
, FI_URL
) && FT_IS_STRING(fi_
->hfinfo
->type
))) {
58 void FieldInformation::setParentField(field_info
* par_fi
)
63 int FieldInformation::treeType()
66 Q_ASSERT(fi_
->tree_type
>= -1 && fi_
->tree_type
< num_tree_types
);
67 return fi_
->tree_type
;
72 field_info
* FieldInformation::fieldInfo() const
77 FieldInformation::HeaderInfo
FieldInformation::headerInfo() const
81 if (fi_
&& fi_
->hfinfo
)
83 header
.name
= fi_
->hfinfo
->name
;
84 header
.description
= fi_
->hfinfo
->blurb
;
85 header
.abbreviation
= fi_
->hfinfo
->abbrev
;
86 header
.isValid
= true;
87 header
.type
= fi_
->hfinfo
->type
;
88 header
.parent
= fi_
->hfinfo
->parent
;
89 header
.id
= fi_
->hfinfo
->id
;
94 header
.description
= "";
95 header
.abbreviation
= "";
96 header
.isValid
= false;
97 header
.type
= FT_NONE
;
105 FieldInformation
* FieldInformation::parentField() const
107 return new FieldInformation(parent_fi_
, parent());
110 bool FieldInformation::tvbContains(FieldInformation
*child
)
112 if (fi_
&& child
&& fi_
->ds_tvb
== child
->fieldInfo()->ds_tvb
)
118 unsigned FieldInformation::flag(unsigned mask
)
121 return FI_GET_FLAG(fi_
, mask
);
126 const QString
FieldInformation::moduleName()
130 if (headerInfo().parent
== -1) {
131 module_name
= fi_
->hfinfo
->abbrev
;
133 module_name
= proto_registrar_get_abbrev(headerInfo().parent
);
139 QString
FieldInformation::toString()
141 QByteArray display_label
;
143 display_label
.resize(80); // Arbitrary.
144 int label_len
= proto_item_fill_display_label(fi_
, display_label
.data(), static_cast<int>(display_label
.size())-1);
145 display_label
.resize(label_len
);
147 if (display_label
.isEmpty()) {
148 return "[no value for field]";
150 return QString(display_label
);
153 QString
FieldInformation::url()
156 if (flag(FI_URL
) && headerInfo().isValid
&& FT_IS_STRING(fi_
->hfinfo
->type
)) {
162 FieldInformation::Position
FieldInformation::position() const
164 Position pos
= {-1, -1};
165 if (fi_
&& fi_
->ds_tvb
)
167 int len
= (int) tvb_captured_length(fi_
->ds_tvb
);
169 pos
.start
= fi_
->start
;
170 pos
.length
= fi_
->length
;
172 if (pos
.start
< 0 || pos
.length
< 0 || pos
.start
>= len
)
174 if (fi_
->appendix_start
>= 0 && fi_
->appendix_length
> 0 && fi_
->appendix_start
< len
)
176 pos
.start
= fi_
->appendix_start
;
177 pos
.length
= fi_
->appendix_length
;
185 FieldInformation::Position
FieldInformation::appendix() const
187 Position pos
= {-1, -1};
188 if (fi_
&& fi_
->ds_tvb
)
190 pos
.start
= fi_
->appendix_start
;
191 pos
.length
= fi_
->appendix_length
;
197 const QByteArray
FieldInformation::printableData()
201 if (fi_
&& fi_
->ds_tvb
)
203 FieldInformation::Position pos
= position();
204 int rem_length
= tvb_captured_length_remaining(fi_
->ds_tvb
, pos
.start
);
206 int length
= pos
.length
;
207 if (length
> rem_length
)
209 uint8_t * dataSet
= (uint8_t *)tvb_memdup(wmem_file_scope(), fi_
->ds_tvb
, pos
.start
, length
);
210 data
= QByteArray::fromRawData((char *)dataSet
, length
);