SVN commit 455584 by englich: * Implement casting to string from xs:base64Binary with KCodecs::base64Encode. Seems to work fine, and I see no reason to why it shouldn't. * Implement constructing from the lexical space for xs:base64Binary. Seems to work fine for valid input; invalid input should currently not be trapped, and needs implementation. Main algorithms consists of a copy of KCodecs::base64Decode(copyrights added, licenses compatible). Remaining for xs:base64Binary & xs:hexBinary is now trapping invalid lexical space in base64Binary, and the weird string cast case for hexBinary. CC-COOKIES: Dawit et al. M +133 -18 data/Base64Binary.cpp M +3 -0 data/String.cpp M +10 -0 tests/data/casting.xml --- branches/work/kdom/xpath/data/Base64Binary.cpp #455583:455584 @@ -1,27 +1,33 @@ /* - Copyright(C) 2005 Frans Englich + Copyright(C) 2005 Frans Englich - This file is part of the KDE project + The base64Decode function is covered by the following copyright: + Copyright(C) 2005 Frans Englich + Copyright(C) 2000 - 2001 Dawit Alemayehu + Copyright(C) 2001 Rik Hemsley (rikkus) - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or(at your option) any later version. + This file is part of the KDE project - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or(at your option) any later version. - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ #include #include +#include #include #include "DOMString.h" @@ -44,10 +50,120 @@ { } -AtomicValue *Base64Binary::CreateFromRepresentation(const KDOM::DOMString &) +const char Base64DecMap[128] = { + 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 +}; + +QByteArray base64Decode(const QCString& instr) +{ + QByteArray in(instr.length()); + memcpy(in.data(), instr.data(), instr.length()); + + QByteArray out; + + if(in.isEmpty()) + return out; + + unsigned int count = 0; + unsigned int len = in.size(), tail = len; + const char* data = in.data(); + + // Deal with possible *nix "BEGIN" marker!! + while(count < len && (data[count] == '\n' || data[count] == '\r' || + data[count] == '\t' || data[count] == ' ')) + count++; + + if(strncasecmp(data+count, "begin", 5) == 0) + { + count += 5; + while(count < len && data[count] != '\n' && data[count] != '\r') + count++; + + while(count < len && (data[count] == '\n' || data[count] == '\r')) + count ++; + + data += count; + tail = (len -= count); + } + + // Find the tail end of the actual encoded data even if + // there is/are trailing CR and/or LF. + while(data[tail-1] == '=' || data[tail-1] == '\n' || + data[tail-1] == '\r') + { + if(data[--tail] != '=') + len = tail; + } + + unsigned int outIdx = 0; + out.resize((count=len)); + for(unsigned int idx = 0; idx < count; idx++) + { + // Adhere to RFC 2045 and ignore characters + // that are not part of the encoding table. + unsigned char ch = data[idx]; + if((ch > 47 && ch < 58) || + (ch > 64 && ch < 91) || + (ch > 96 && ch < 123) || + ch == '+' || ch == '/' || ch == '=') + { + out[outIdx++] = Base64DecMap[ch]; + } + else + { + len--; + tail--; + } + } + + // 4-byte to 3-byte conversion + len = (tail>(len/4)) ? tail - (len/4) : 0; + unsigned int sidx = 0, didx = 0; + if(len > 1) + { + while(didx < len-2) + { + out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003)); + out[didx+1] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017)); + out[didx+2] = (((out[sidx+2] << 6) & 255) | (out[sidx+3] & 077)); + sidx += 4; + didx += 3; + } + } + + if(didx < len) + out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003)); + + if(++didx < len) + out[didx] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017)); + + // Resize the output buffer + if(len == 0 || len < out.size()) + out.resize(len); + + return out; +} + +AtomicValue *Base64Binary::CreateFromRepresentation(const KDOM::DOMString &str) +{ QByteArray val; - return new Base64Binary(val); + return new Base64Binary(base64Decode(str.string().utf8())); } Base64Binary *Base64Binary::CreateFromNative(const QByteArray &data) @@ -57,8 +173,7 @@ KDOM::DOMString Base64Binary::stringValue() const { - QString result; - return KDOM::DOMString(result); + return KDOM::DOMString(QString(KCodecs::base64Encode(m_value))); } bool Base64Binary::equals(AtomicValue *other) const --- branches/work/kdom/xpath/data/String.cpp #455583:455584 @@ -24,6 +24,7 @@ #include "DOMString.h" #include "AnyURI.h" +#include "Base64Binary.h" #include "Boolean.h" #include "BuiltinAtomicType.h" #include "BuiltinTypes.h" @@ -113,6 +114,8 @@ return Float::CreateFromRepresentation(m_value); else if(type->wxsMatchesType(BuiltinTypes::xsHexBinary)) return HexBinary::CreateFromRepresentation(m_value); + else if(type->wxsMatchesType(BuiltinTypes::xsBase64Binary)) + return Base64Binary::CreateFromRepresentation(m_value); else /* Error out */ return AtomicValue::castToBuiltin(type, context); } --- branches/work/kdom/xpath/tests/data/casting.xml #455583:455584 @@ -203,5 +203,15 @@ xs:base64Binary(xs:hexBinary("03")) ne xs:base64Binary(xs:hexBinary("13")) count(xs:base64Binary(xs:base64Binary(xs:hexBinary("03")))) eq 1 + xs:string(xs:base64Binary("0w==")) eq "0w==" + xs:string(xs:base64Binary(xs:hexBinary("03"))) eq "Aw==" + xs:string(xs:base64Binary("0A044c7F")) eq "0A044c7F" + xs:string(xs:base64Binary(xs:hexBinary("0f3c"))) eq "Dzw=" + xs:string(xs:base64Binary(xs:hexBinary("12"))) eq "Eg==" + xs:string(xs:base64Binary(xs:hexBinary("f0"))) eq "8A==" + xs:string(xs:base64Binary(xs:hexBinary("ffff"))) eq "//8=" + xs:string(xs:base64Binary(xs:hexBinary("ffff34564321deac9876"))) + eq "//80VkMh3qyYdg==" +