OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / ssh / sshpacketparser.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "sshpacketparser_p.h"
35
36 #include <cctype>
37
38 namespace Core {
39 namespace Internal {
40
41 namespace { quint32 size(const QByteArray &data) { return data.size(); } }
42
43 QString SshPacketParser::asUserString(const QByteArray &rawString)
44 {
45     QByteArray filteredString;
46     filteredString.resize(rawString.size());
47     for (int i = 0; i < rawString.size(); ++i) {
48         const char c = rawString.at(i);
49         filteredString[i]
50             = std::isprint(c) || c == '\n' || c == '\r' || c == '\t' ? c : '?';
51     }
52     return QString::fromUtf8(filteredString);
53 }
54
55 bool SshPacketParser::asBool(const QByteArray &data, quint32 offset)
56 {
57     if (size(data) <= offset)
58         throw SshPacketParseException();
59     return data.at(offset);
60 }
61
62 bool SshPacketParser::asBool(const QByteArray &data, quint32 *offset)
63 {
64     bool b = asBool(data, *offset);
65     ++(*offset);
66     return b;
67 }
68
69
70 quint32 SshPacketParser::asUint32(const QByteArray &data, quint32 offset)
71 {
72     if (size(data) < offset + 4)
73         throw SshPacketParseException();
74     const quint32 value = ((data.at(offset) & 0xff) << 24)
75         + ((data.at(offset + 1) & 0xff) << 16)
76         + ((data.at(offset + 2) & 0xff) << 8) + (data.at(offset + 3) & 0xff);
77     return value;
78 }
79
80 quint32 SshPacketParser::asUint32(const QByteArray &data, quint32 *offset)
81 {
82     const quint32 v = asUint32(data, *offset);
83     *offset += 4;
84     return v;
85 }
86
87 quint64 SshPacketParser::asUint64(const QByteArray &data, quint32 offset)
88 {
89     if (size(data) < offset + 8)
90         throw SshPacketParseException();
91     const quint64 value = (static_cast<quint64>(data.at(offset) & 0xff) << 56)
92         + (static_cast<quint64>(data.at(offset + 1) & 0xff) << 48)
93         + (static_cast<quint64>(data.at(offset + 2) & 0xff) << 40)
94         + (static_cast<quint64>(data.at(offset + 3) & 0xff) << 32)
95         + ((data.at(offset + 4) & 0xff) << 24)
96         + ((data.at(offset + 5) & 0xff) << 16)
97         + ((data.at(offset + 6) & 0xff) << 8)
98         + (data.at(offset + 7) & 0xff);
99     return value;
100 }
101
102 quint64 SshPacketParser::asUint64(const QByteArray &data, quint32 *offset)
103 {
104     const quint64 val = asUint64(data, *offset);
105     *offset += 8;
106     return val;
107 }
108
109 QByteArray SshPacketParser::asString(const QByteArray &data, quint32 *offset)
110 {
111     const quint32 length = asUint32(data, offset);
112     if (size(data) < *offset + length)
113         throw SshPacketParseException();
114     const QByteArray &string = data.mid(*offset, length);
115     *offset += length;
116     return string;
117 }
118
119 QString SshPacketParser::asUserString(const QByteArray &data, quint32 *offset)
120 {
121     return asUserString(asString(data, offset));
122 }
123
124 SshNameList SshPacketParser::asNameList(const QByteArray &data, quint32 *offset)
125 {
126     const quint32 length = asUint32(data, offset);
127     const int listEndPos = *offset + length;
128     if (data.size() < listEndPos)
129         throw SshPacketParseException();
130     SshNameList names(length + 4);
131     int nextNameOffset = *offset;
132     int nextCommaOffset = data.indexOf(',', nextNameOffset);
133     while (nextNameOffset > 0 && nextNameOffset < listEndPos) {
134         const int stringEndPos = nextCommaOffset == -1
135             || nextCommaOffset > listEndPos ? listEndPos : nextCommaOffset;
136         names.names << QByteArray(data.constData() + nextNameOffset,
137             stringEndPos - nextNameOffset);
138         nextNameOffset = nextCommaOffset + 1;
139         nextCommaOffset = data.indexOf(',', nextNameOffset);
140     }
141     *offset += length;
142     return names;
143 }
144
145 Botan::BigInt SshPacketParser::asBigInt(const QByteArray &data, quint32 *offset)
146 {
147     const quint32 length = asUint32(data, offset);
148     if (length == 0)
149         return Botan::BigInt();
150     const Botan::byte *numberStart
151         = reinterpret_cast<const Botan::byte *>(data.constData() + *offset);
152     *offset += length;
153     return Botan::BigInt::decode(numberStart, length);
154 }
155
156 } // namespace Internal
157 } // namespace Core