| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 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 The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include <QByteArray> | 
| 41 | #include <QtGlobal> | 
| 42 |  | 
| 43 | #include "qbuiltintypes_p.h" | 
| 44 | #include "qvalidationerror_p.h" | 
| 45 |  | 
| 46 | #include "qbase64binary_p.h" | 
| 47 |  | 
| 48 | QT_BEGIN_NAMESPACE | 
| 49 |  | 
| 50 | using namespace QPatternist; | 
| 51 |  | 
| 52 | Base64Binary::Base64Binary(const QByteArray &val) : m_value(val) | 
| 53 | { | 
| 54 | } | 
| 55 |  | 
| 56 | const char Base64Binary::Base64DecMap[128] = | 
| 57 | { | 
| 58 |      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
| 59 |      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
| 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, 0x3E, 0x00, 0x00, 0x00, 0x3F, | 
| 64 |      0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, | 
| 65 |      0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 
| 66 |      0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, | 
| 67 |      0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, | 
| 68 |      0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, | 
| 69 |      0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, | 
| 70 |      0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, | 
| 71 |      0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, | 
| 72 |      0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, | 
| 73 |      0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00 | 
| 74 | }; | 
| 75 |  | 
| 76 | void Base64Binary::base64Decode(const QByteArray &in, QByteArray &out, bool &ok) | 
| 77 | { | 
| 78 |     out.resize(size: 0); | 
| 79 |  | 
| 80 |     if(in.isEmpty()) | 
| 81 |     { | 
| 82 |         ok = false; | 
| 83 |         return; | 
| 84 |     } | 
| 85 |  | 
| 86 |     ok = true; | 
| 87 |     int len = in.size(), tail = len; | 
| 88 |     const char *const data = in.data(); | 
| 89 |     unsigned int eqCount = 0; | 
| 90 |  | 
| 91 |     // Find the tail end of the actual encoded data even if | 
| 92 |     // there is/are trailing CR and/or LF. | 
| 93 |     while(data[tail - 1] == '=') | 
| 94 |     { | 
| 95 |         --tail; | 
| 96 |         if(data[tail] != '=') | 
| 97 |             len = tail; | 
| 98 |         else | 
| 99 |             ++eqCount; | 
| 100 |     } | 
| 101 |  | 
| 102 |     if(eqCount > 2) | 
| 103 |     { | 
| 104 |         ok = false; | 
| 105 |         return; | 
| 106 |     } | 
| 107 |  | 
| 108 |     unsigned int outIdx = 0; | 
| 109 |     const int count = len; // We modify len below | 
| 110 |     out.resize(size: (count)); | 
| 111 |  | 
| 112 |     for(int idx = 0; idx < count; ++idx) | 
| 113 |     { | 
| 114 |         const unsigned char ch = data[idx]; | 
| 115 |         if((ch > 47 && ch < 58) || | 
| 116 |             (ch > 64 && ch < 91) || | 
| 117 |             (ch > 96 && ch < 123) || | 
| 118 |             ch == '+' || | 
| 119 |             ch == '/') | 
| 120 |         { | 
| 121 |             out[outIdx++] = Base64DecMap[ch]; | 
| 122 |         } | 
| 123 |         else if(ch == '=') | 
| 124 |         { | 
| 125 |             if((idx + 1) == count || data[idx + 1] == '=') | 
| 126 |             { | 
| 127 |                 out[++outIdx] = Base64DecMap[ch]; | 
| 128 |                 continue; | 
| 129 |             } | 
| 130 |  | 
| 131 |             ok = false; | 
| 132 |             return; | 
| 133 |         } | 
| 134 |         else if(ch == ' ') | 
| 135 |         { | 
| 136 |             /* One space is ok, and the previously applied whitespace facet(not implemented | 
| 137 |              * at this time of writing) have ensured it's only one space, so we assume that. */ | 
| 138 |             --tail; | 
| 139 |             --len; | 
| 140 |             continue; | 
| 141 |         } | 
| 142 |         else | 
| 143 |         { | 
| 144 |             ok = false; | 
| 145 |             return; | 
| 146 |         } | 
| 147 |     } | 
| 148 |  | 
| 149 |     if(outIdx % 4 != 0) | 
| 150 |     { | 
| 151 |         ok = false; | 
| 152 |         return; | 
| 153 |     } | 
| 154 |  | 
| 155 |     out.resize(size: len); | 
| 156 |  | 
| 157 |     // 4-byte to 3-byte conversion | 
| 158 |     len = (tail > (len / 4)) ? tail - (len / 4) : 0; | 
| 159 |     int sidx = 0, didx = 0; | 
| 160 |     if(len > 1) | 
| 161 |     { | 
| 162 |       while(didx < len - 2) | 
| 163 |       { | 
| 164 |           out[didx] =(((out[sidx] << 2) & 255) | ((out[sidx + 1] >> 4) & 003)); | 
| 165 |           out[didx + 1] =(((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 017)); | 
| 166 |           out[didx + 2] =(((out[sidx + 2] << 6) & 255) | (out[sidx + 3] & 077)); | 
| 167 |           sidx += 4; | 
| 168 |           didx += 3; | 
| 169 |       } | 
| 170 |     } | 
| 171 |  | 
| 172 |     if(didx < len) | 
| 173 |         out[didx] =(((out[sidx] << 2) & 255) | ((out[sidx + 1] >> 4) & 003)); | 
| 174 |  | 
| 175 |     if(++didx < len) | 
| 176 |         out[didx] =(((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 017)); | 
| 177 |  | 
| 178 |     // Resize the output buffer | 
| 179 |     if(len == 0 || len < out.size()) | 
| 180 |       out.resize(size: len); | 
| 181 | } | 
| 182 |  | 
| 183 | AtomicValue::Ptr Base64Binary::fromLexical(const QString &str) | 
| 184 | { | 
| 185 |     const QString simple(str.simplified()); | 
| 186 |     if(simple.isEmpty()) | 
| 187 |         return AtomicValue::Ptr(new Base64Binary(QByteArray())); | 
| 188 |  | 
| 189 |     bool ok = false; | 
| 190 |     QByteArray result; | 
| 191 |     base64Decode(in: simple.toUtf8(), out&: result, ok); | 
| 192 |  | 
| 193 |     if(ok) | 
| 194 |         return AtomicValue::Ptr(new Base64Binary(result)); | 
| 195 |     else | 
| 196 |         return ValidationError::createError(); | 
| 197 | } | 
| 198 |  | 
| 199 | Base64Binary::Ptr Base64Binary::fromValue(const QByteArray &data) | 
| 200 | { | 
| 201 |     return Base64Binary::Ptr(new Base64Binary(data)); | 
| 202 | } | 
| 203 |  | 
| 204 | QString Base64Binary::stringValue() const | 
| 205 | { | 
| 206 |     return QString::fromLatin1(str: m_value.toBase64().constData()); | 
| 207 | } | 
| 208 |  | 
| 209 | ItemType::Ptr Base64Binary::type() const | 
| 210 | { | 
| 211 |     return BuiltinTypes::xsBase64Binary; | 
| 212 | } | 
| 213 |  | 
| 214 | QT_END_NAMESPACE | 
| 215 |  |