aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/utils/field_information.cpp
blob: aac03277c5a48c5e0264b7cad2a2d88db40814f8 (plain)
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
/* field_information.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#include <stdint.h>

#include <ui/qt/utils/field_information.h>

FieldInformation::FieldInformation(field_info * fi, QObject * parent)
:QObject(parent)
{
    fi_ = fi;
    parent_fi_ = 0;
}

bool FieldInformation::isValid()
{
    bool ret = false;

    if ( fi_ )
    {
        if (fi_->hfinfo->blurb != 0 && fi_->hfinfo->blurb[0] != '\0') {
            ret = true;
        } else {
            ret = ((QString().fromUtf8(fi_->hfinfo->name)).length() > 0 );
        }
    }

    return ret;
}

void FieldInformation::setParentField(field_info * par_fi)
{
    parent_fi_ = par_fi;
}

field_info * FieldInformation::fieldInfo() const
{
    return fi_;
}

FieldInformation::HeaderInfo FieldInformation::headerInfo() const
{
    HeaderInfo header;
    header.isValid = false;

    if ( fi_ && fi_->hfinfo )
    {
        header.isValid = true;
        header.name = QString().fromUtf8(fi_->hfinfo->name);
        header.description = QString().fromUtf8(fi_->hfinfo->blurb);
        header.abbreviation = QString().fromUtf8(fi_->hfinfo->abbrev);
    }

    return header;
}

FieldInformation * FieldInformation::parentField() const
{
    return new FieldInformation(parent_fi_);
}

bool FieldInformation::tvbContains(FieldInformation *child)
{
    if ( fi_ && child && fi_->ds_tvb == child->fieldInfo()->ds_tvb )
        return true;

    return false;
}

FieldInformation::Position FieldInformation::position() const
{
    Position pos = {-1, -1};
    if ( fi_ && fi_->ds_tvb )
    {
        int len = (int) tvb_captured_length(fi_->ds_tvb);

        pos.start = fi_->start;
        pos.length = fi_->length;

        if (pos.start < 0 || pos.length < 0 || pos.start >= len)
        {
            if ( fi_->appendix_start >= 0 && fi_->appendix_length > 0 && fi_->appendix_start < len )
            {
                pos.start = fi_->appendix_start;
                pos.length = fi_->appendix_length;
            }
        }
    }

    return pos;
}

FieldInformation::Position FieldInformation::appendix() const
{
    Position pos = {-1, -1};
    if ( fi_ && fi_->ds_tvb )
    {
        pos.start = fi_->appendix_start;
        pos.length = fi_->appendix_length;
    }

    return pos;
}

QByteArray FieldInformation::printableData()
{
    QByteArray data;

    if ( fi_ && fi_->ds_tvb )
    {
        FieldInformation::Position pos = position();
        int rem_length = tvb_captured_length_remaining(fi_->ds_tvb, pos.start);

        int length = pos.length;
        if ( length > rem_length )
            length = rem_length;
        uint8_t * dataSet = (uint8_t *)tvb_memdup(wmem_file_scope(), fi_->ds_tvb, pos.start, length );
        data = QByteArray::fromRawData((char *)dataSet, length);
    }

    return data;
}
/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */