[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[651] | 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include <QtGlobal>
|
---|
| 43 |
|
---|
| 44 | #include "qbase64binary_p.h"
|
---|
| 45 | #include "qbuiltintypes_p.h"
|
---|
| 46 | #include "qpatternistlocale_p.h"
|
---|
| 47 | #include "qvalidationerror_p.h"
|
---|
| 48 |
|
---|
| 49 | #include "qhexbinary_p.h"
|
---|
| 50 |
|
---|
| 51 | QT_BEGIN_NAMESPACE
|
---|
| 52 |
|
---|
| 53 | using namespace QPatternist;
|
---|
| 54 |
|
---|
| 55 | HexBinary::HexBinary(const QByteArray &val) : Base64Binary(val)
|
---|
| 56 | {
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | qint8 HexBinary::fromHex(const QChar &c)
|
---|
| 60 | {
|
---|
| 61 | if(c.unicode() > 'f')
|
---|
| 62 | return -1;
|
---|
| 63 |
|
---|
| 64 | const char *const range = "0123456789ABCDEFabcdef";
|
---|
| 65 |
|
---|
| 66 | const char *const in = strchr(range, c.unicode());
|
---|
| 67 |
|
---|
| 68 | if(!in)
|
---|
| 69 | return -1;
|
---|
| 70 |
|
---|
| 71 | /* Pointer arithmetic. */
|
---|
| 72 | int digit = in - range;
|
---|
| 73 |
|
---|
| 74 | if(digit > 15)
|
---|
| 75 | digit -= 6;
|
---|
| 76 |
|
---|
| 77 | return digit;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | AtomicValue::Ptr HexBinary::fromLexical(const NamePool::Ptr &np, const QString &str)
|
---|
| 81 | {
|
---|
| 82 | const QString lexical(str.trimmed());
|
---|
| 83 | const int len = lexical.length();
|
---|
| 84 |
|
---|
| 85 | if(len == 0)
|
---|
| 86 | return AtomicValue::Ptr(new HexBinary(QByteArray()));
|
---|
| 87 |
|
---|
| 88 | if((len & 1) != 0)
|
---|
| 89 | {
|
---|
| 90 | /* Catch a common case. */
|
---|
| 91 | return ValidationError::createError(QtXmlPatterns::tr(
|
---|
| 92 | "A value of type %1 must contain an even number of "
|
---|
| 93 | "digits. The value %2 does not.")
|
---|
| 94 | .arg(formatType(np, BuiltinTypes::xsHexBinary),
|
---|
| 95 | formatData(QString::number(len))));
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | QByteArray val;
|
---|
| 99 | val.resize(len / 2);
|
---|
| 100 |
|
---|
| 101 | for(int i = 0; i < len / 2; ++i)
|
---|
| 102 | {
|
---|
| 103 | qint8 p1 = fromHex(lexical[i * 2]);
|
---|
| 104 | qint8 p2 = fromHex(lexical[i * 2 + 1]);
|
---|
| 105 |
|
---|
| 106 | if(p1 == -1 || p2 == -1)
|
---|
| 107 | {
|
---|
| 108 | const QString hex(QString::fromLatin1("%1%2").arg(lexical[i * 2], lexical[i * 2 + 1]));
|
---|
| 109 |
|
---|
| 110 | return ValidationError::createError(QtXmlPatterns::tr(
|
---|
| 111 | "%1 is not valid as a value of type %2.")
|
---|
| 112 | .arg(formatData(hex),
|
---|
| 113 | formatType(np, BuiltinTypes::xsHexBinary)));
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | val[i] = static_cast<char>(p1 * 16 + p2);
|
---|
| 117 | }
|
---|
| 118 | Q_ASSERT(!val.isEmpty());
|
---|
| 119 |
|
---|
| 120 | return AtomicValue::Ptr(new HexBinary(val));
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | HexBinary::Ptr HexBinary::fromValue(const QByteArray &data)
|
---|
| 124 | {
|
---|
| 125 | return HexBinary::Ptr(new HexBinary(data));
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | QString HexBinary::stringValue() const
|
---|
| 129 | {
|
---|
| 130 | static const char s_toHex[] = "0123456789ABCDEF";
|
---|
| 131 | const int len = m_value.count();
|
---|
| 132 | QString result;
|
---|
| 133 | result.reserve(len * 2);
|
---|
| 134 |
|
---|
| 135 | for(int i = 0; i < len; ++i)
|
---|
| 136 | {
|
---|
| 137 | // This cast is significant.
|
---|
| 138 | const unsigned char val = static_cast<unsigned char>(m_value.at(i));
|
---|
| 139 | result += QLatin1Char(s_toHex[val >> 4]);
|
---|
| 140 | result += QLatin1Char(s_toHex[val & 0x0F]);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | return result;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | ItemType::Ptr HexBinary::type() const
|
---|
| 147 | {
|
---|
| 148 | return BuiltinTypes::xsHexBinary;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | QT_END_NAMESPACE
|
---|