1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "sshpacketparser_p.h"
41 namespace { quint32 size(const QByteArray &data) { return data.size(); } }
43 QString SshPacketParser::asUserString(const QByteArray &rawString)
45 QByteArray filteredString;
46 filteredString.resize(rawString.size());
47 for (int i = 0; i < rawString.size(); ++i) {
48 const char c = rawString.at(i);
50 = std::isprint(c) || c == '\n' || c == '\r' || c == '\t' ? c : '?';
52 return QString::fromUtf8(filteredString);
55 bool SshPacketParser::asBool(const QByteArray &data, quint32 offset)
57 if (size(data) <= offset)
58 throw SshPacketParseException();
59 return data.at(offset);
62 bool SshPacketParser::asBool(const QByteArray &data, quint32 *offset)
64 bool b = asBool(data, *offset);
70 quint32 SshPacketParser::asUint32(const QByteArray &data, quint32 offset)
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);
80 quint32 SshPacketParser::asUint32(const QByteArray &data, quint32 *offset)
82 const quint32 v = asUint32(data, *offset);
87 quint64 SshPacketParser::asUint64(const QByteArray &data, quint32 offset)
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);
102 quint64 SshPacketParser::asUint64(const QByteArray &data, quint32 *offset)
104 const quint64 val = asUint64(data, *offset);
109 QByteArray SshPacketParser::asString(const QByteArray &data, quint32 *offset)
111 const quint32 length = asUint32(data, offset);
112 if (size(data) < *offset + length)
113 throw SshPacketParseException();
114 const QByteArray &string = data.mid(*offset, length);
119 QString SshPacketParser::asUserString(const QByteArray &data, quint32 *offset)
121 return asUserString(asString(data, offset));
124 SshNameList SshPacketParser::asNameList(const QByteArray &data, quint32 *offset)
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);
145 Botan::BigInt SshPacketParser::asBigInt(const QByteArray &data, quint32 *offset)
147 const quint32 length = asUint32(data, offset);
149 return Botan::BigInt();
150 const Botan::byte *numberStart
151 = reinterpret_cast<const Botan::byte *>(data.constData() + *offset);
153 return Botan::BigInt::decode(numberStart, length);
156 } // namespace Internal