| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 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 | ** | 
|---|
| 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. | 
|---|
| 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 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QByteArray> | 
|---|
| 43 | #include <QtGlobal> | 
|---|
| 44 |  | 
|---|
| 45 | #include "qbuiltintypes_p.h" | 
|---|
| 46 | #include "qvalidationerror_p.h" | 
|---|
| 47 |  | 
|---|
| 48 | #include "qbase64binary_p.h" | 
|---|
| 49 |  | 
|---|
| 50 | QT_BEGIN_NAMESPACE | 
|---|
| 51 |  | 
|---|
| 52 | using namespace QPatternist; | 
|---|
| 53 |  | 
|---|
| 54 | Base64Binary::Base64Binary(const QByteArray &val) : m_value(val) | 
|---|
| 55 | { | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | const char Base64Binary::Base64DecMap[128] = | 
|---|
| 59 | { | 
|---|
| 60 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
|---|
| 61 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
|---|
| 62 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
|---|
| 63 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
|---|
| 64 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
|---|
| 65 | 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F, | 
|---|
| 66 | 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, | 
|---|
| 67 | 0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
|---|
| 68 | 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, | 
|---|
| 69 | 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, | 
|---|
| 70 | 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, | 
|---|
| 71 | 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, | 
|---|
| 72 | 0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, | 
|---|
| 73 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, | 
|---|
| 74 | 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, | 
|---|
| 75 | 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00 | 
|---|
| 76 | }; | 
|---|
| 77 |  | 
|---|
| 78 | void Base64Binary::base64Decode(const QByteArray &in, QByteArray &out, bool &ok) | 
|---|
| 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 | } | 
|---|
| 184 |  | 
|---|
| 185 | AtomicValue::Ptr Base64Binary::fromLexical(const QString &str) | 
|---|
| 186 | { | 
|---|
| 187 | const QString simple(str.simplified()); | 
|---|
| 188 | if(simple.isEmpty()) | 
|---|
| 189 | return AtomicValue::Ptr(new Base64Binary(QByteArray())); | 
|---|
| 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 | 
|---|
| 198 | return ValidationError::createError(); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | Base64Binary::Ptr Base64Binary::fromValue(const QByteArray &data) | 
|---|
| 202 | { | 
|---|
| 203 | return Base64Binary::Ptr(new Base64Binary(data)); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | QString Base64Binary::stringValue() const | 
|---|
| 207 | { | 
|---|
| 208 | return QString::fromLatin1(m_value.toBase64().constData()); | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | ItemType::Ptr Base64Binary::type() const | 
|---|
| 212 | { | 
|---|
| 213 | return BuiltinTypes::xsBase64Binary; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | QT_END_NAMESPACE | 
|---|