Qt 4.8
Public Types | Public Functions | Static Public Functions | Protected Functions | Protected Variables | Static Private Functions | Static Private Attributes | Friends | List of all members
QPatternist::Base64Binary Class Reference

Implements the value instance of the xs:base64Binary type. More...

#include <qbase64binary_p.h>

Inheritance diagram for QPatternist::Base64Binary:
QPatternist::AtomicValue QSharedData QPatternist::CppCastingHelper< AtomicValue > QPatternist::HexBinary

Public Types

typedef AtomicValue::Ptr Ptr
 
- Public Types inherited from QPatternist::AtomicValue
typedef QList< AtomicValue::PtrList
 
typedef QExplicitlySharedDataPointer< AtomicValuePtr
 

Public Functions

const QByteArrayasByteArray () const
 
virtual QString stringValue () const
 
virtual ItemType::Ptr type () const
 
- Public Functions inherited from QPatternist::AtomicValue
virtual bool evaluateEBV (const QExplicitlySharedDataPointer< DynamicContext > &context) const
 
virtual bool hasError () const
 
virtual ~AtomicValue ()
 
- Public Functions inherited from QSharedData
 QSharedData ()
 Constructs a QSharedData object with a reference count of 0. More...
 
 QSharedData (const QSharedData &)
 Constructs a QSharedData object with reference count 0. More...
 
- Public Functions inherited from QPatternist::CppCastingHelper< AtomicValue >
const TCastTarget * as () const
 
TCastTarget * as ()
 

Static Public Functions

static AtomicValue::Ptr fromLexical (const QString &value)
 
static Base64Binary::Ptr fromValue (const QByteArray &data)
 
- Static Public Functions inherited from QPatternist::AtomicValue
static ItemType::Ptr qtToXDMType (const QXmlItem &item)
 
static QVariant toQt (const AtomicValue *const value)
 
static QVariant toQt (const AtomicValue::Ptr &value)
 
static Item toXDM (const QVariant &value)
 

Protected Functions

 Base64Binary (const QByteArray &val)
 
- Protected Functions inherited from QPatternist::AtomicValue
 AtomicValue ()
 
- Protected Functions inherited from QPatternist::CppCastingHelper< AtomicValue >
 CppCastingHelper ()
 

Protected Variables

const QByteArray m_value
 

Static Private Functions

static void base64Decode (const QByteArray &in, QByteArray &out, bool &ok)
 Assumes in is a lexical representation of xs:base64Binary, and converts it to the binary data set in out. More...
 

Static Private Attributes

static const char Base64DecMap [128]
 

Friends

class CommonValues
 

Additional Inherited Members

- Public Variables inherited from QSharedData
QAtomicInt ref
 

Detailed Description

Implements the value instance of the xs:base64Binary type.

Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 70 of file qbase64binary_p.h.

Typedefs

◆ Ptr

Definition at line 75 of file qbase64binary_p.h.

Constructors and Destructors

◆ Base64Binary()

Base64Binary::Base64Binary ( const QByteArray val)
protected

Definition at line 54 of file qbase64binary.cpp.

Referenced by asByteArray(), fromLexical(), and fromValue().

54  : m_value(val)
55 {
56 }
const QByteArray m_value

Functions

◆ asByteArray()

const QByteArray& QPatternist::Base64Binary::asByteArray ( ) const
inline

◆ base64Decode()

void Base64Binary::base64Decode ( const QByteArray in,
QByteArray out,
bool &  ok 
)
staticprivate

Assumes in is a lexical representation of xs:base64Binary, and converts it to the binary data set in out.

If instr is invalid Base64 content, ok is set to false, and the returned QByteArray has an undefined value.

We cannot use QByteArray::fromBase64() because it doesn't do the necessary validation that we need to properly implement W3C XML Schema's xs:base64Binary type.

Definition at line 78 of file qbase64binary.cpp.

Referenced by fromLexical().

79 {
80  out.resize(0);
81 
82  if(in.isEmpty())
83  {
84  ok = false;
85  return;
86  }
87 
88  ok = true;
89  int len = in.size(), tail = len;
90  const char *const data = in.data();
91  unsigned int eqCount = 0;
92 
93  // Find the tail end of the actual encoded data even if
94  // there is/are trailing CR and/or LF.
95  while(data[tail - 1] == '=')
96  {
97  --tail;
98  if(data[tail] != '=')
99  len = tail;
100  else
101  ++eqCount;
102  }
103 
104  if(eqCount > 2)
105  {
106  ok = false;
107  return;
108  }
109 
110  unsigned int outIdx = 0;
111  const int count = len; // We modify len below
112  out.resize((count));
113 
114  for(int idx = 0; idx < count; ++idx)
115  {
116  const unsigned char ch = data[idx];
117  if((ch > 47 && ch < 58) ||
118  (ch > 64 && ch < 91) ||
119  (ch > 96 && ch < 123) ||
120  ch == '+' ||
121  ch == '/')
122  {
123  out[outIdx++] = Base64DecMap[ch];
124  }
125  else if(ch == '=')
126  {
127  if((idx + 1) == count || data[idx + 1] == '=')
128  {
129  out[++outIdx] = Base64DecMap[ch];
130  continue;
131  }
132 
133  ok = false;
134  return;
135  }
136  else if(ch == ' ')
137  {
138  /* One space is ok, and the previously applied whitespace facet(not implemented
139  * at this time of writing) have ensured it's only one space, so we assume that. */
140  --tail;
141  --len;
142  continue;
143  }
144  else
145  {
146  ok = false;
147  return;
148  }
149  }
150 
151  if(outIdx % 4 != 0)
152  {
153  ok = false;
154  return;
155  }
156 
157  out.resize(len);
158 
159  // 4-byte to 3-byte conversion
160  len = (tail > (len / 4)) ? tail - (len / 4) : 0;
161  int sidx = 0, didx = 0;
162  if(len > 1)
163  {
164  while(didx < len - 2)
165  {
166  out[didx] =(((out[sidx] << 2) & 255) | ((out[sidx + 1] >> 4) & 003));
167  out[didx + 1] =(((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 017));
168  out[didx + 2] =(((out[sidx + 2] << 6) & 255) | (out[sidx + 3] & 077));
169  sidx += 4;
170  didx += 3;
171  }
172  }
173 
174  if(didx < len)
175  out[didx] =(((out[sidx] << 2) & 255) | ((out[sidx + 1] >> 4) & 003));
176 
177  if(++didx < len)
178  out[didx] =(((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 017));
179 
180  // Resize the output buffer
181  if(len == 0 || len < out.size())
182  out.resize(len);
183 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
static const char * data(const QByteArray &arr)
void resize(int size)
Sets the size of the byte array to size bytes.
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
static const char Base64DecMap[128]

◆ fromLexical()

AtomicValue::Ptr Base64Binary::fromLexical ( const QString value)
static

Creates an instance representing value.

Definition at line 185 of file qbase64binary.cpp.

Referenced by QPatternist::StringToBase64BinaryCaster::castFrom().

186 {
187  const QString simple(str.simplified());
188  if(simple.isEmpty())
190 
191  bool ok = false;
192  QByteArray result;
193  base64Decode(simple.toUtf8(), result, ok);
194 
195  if(ok)
196  return AtomicValue::Ptr(new Base64Binary(result));
197  else
199 }
QExplicitlySharedDataPointer< AtomicValue > Ptr
Definition: qitem_p.h:127
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
Base64Binary(const QByteArray &val)
static AtomicValue::Ptr createError(const QString &description=QString(), const ReportContext::ErrorCode=ReportContext::FORG0001)
The QString class provides a Unicode character string.
Definition: qstring.h:83
static void base64Decode(const QByteArray &in, QByteArray &out, bool &ok)
Assumes in is a lexical representation of xs:base64Binary, and converts it to the binary data set in ...

◆ fromValue()

Base64Binary::Ptr Base64Binary::fromValue ( const QByteArray data)
static

Definition at line 201 of file qbase64binary.cpp.

Referenced by QPatternist::HexBinaryToBase64BinaryCaster::castFrom().

202 {
203  return Base64Binary::Ptr(new Base64Binary(data));
204 }
Base64Binary(const QByteArray &val)

◆ stringValue()

QString Base64Binary::stringValue ( ) const
virtual

Implements QPatternist::AtomicValue.

Reimplemented in QPatternist::HexBinary.

Definition at line 206 of file qbase64binary.cpp.

207 {
209 }
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
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
QByteArray toBase64() const
Returns a copy of the byte array, encoded as Base64.
const QByteArray m_value

◆ type()

ItemType::Ptr Base64Binary::type ( ) const
virtual

Implements QPatternist::AtomicValue.

Reimplemented in QPatternist::HexBinary.

Definition at line 211 of file qbase64binary.cpp.

212 {
214 }
static const AtomicType::Ptr xsBase64Binary

Friends and Related Functions

◆ CommonValues

friend class CommonValues
friend

Definition at line 73 of file qbase64binary_p.h.

Properties

◆ Base64DecMap

const char Base64Binary::Base64DecMap
staticprivate
Initial value:
=
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00
}

Definition at line 110 of file qbase64binary_p.h.

Referenced by Base64Binary(), and base64Decode().

◆ m_value

const QByteArray QPatternist::Base64Binary::m_value
protected

Definition at line 94 of file qbase64binary_p.h.

Referenced by asByteArray(), QPatternist::HexBinary::stringValue(), and stringValue().


The documentation for this class was generated from the following files: