Qt 4.8
qhexbinary.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** 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.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
52 
53 using namespace QPatternist;
54 
56 {
57 }
58 
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 
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.")
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),
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 
124 {
125  return HexBinary::Ptr(new HexBinary(data));
126 }
127 
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 
147 {
149 }
150 
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QExplicitlySharedDataPointer< AtomicValue > Ptr
Definition: qitem_p.h:127
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static AtomicValue::Ptr createError(const QString &description=QString(), const ReportContext::ErrorCode=ReportContext::FORG0001)
static AtomicValue::Ptr fromLexical(const NamePool::Ptr &np, const QString &value)
Definition: qhexbinary.cpp:80
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
AtomicValue::Ptr Ptr
Definition: qhexbinary_p.h:79
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
virtual ItemType::Ptr type() const
Definition: qhexbinary.cpp:146
Q_CORE_EXPORT QTextStream & hex(QTextStream &s)
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
signed char qint8
Definition: qglobal.h:933
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static qint8 fromHex(const QChar &c)
Returns -1 on invalid input.
Definition: qhexbinary.cpp:59
QString trimmed() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end.
Definition: qstring.cpp:4506
The namespace for the internal API of QtXmlPatterns.
static const char * data(const QByteArray &arr)
HexBinary(const QByteArray &val)
Definition: qhexbinary.cpp:55
static QString formatData(const QString &data)
Contains functions used for formatting arguments, such as keywords and paths, in translated strings...
int count(char c) const
Returns the number of occurrences of character ch in the byte array.
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
void resize(int size)
Sets the size of the byte array to size bytes.
static HexBinary::Ptr fromValue(const QByteArray &data)
Definition: qhexbinary.cpp:123
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
virtual QString stringValue() const
Definition: qhexbinary.cpp:128
char at(int i) const
Returns the character at index position i in the byte array.
Definition: qbytearray.h:413
Implements the value instance of the xs:base64Binary type.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static const AtomicType::Ptr xsHexBinary
const QByteArray m_value