From: Enar Väikene Date: Wed, 30 Nov 2011 11:08:46 +0000 (+0200) Subject: Added functions to escape binary arrays and renamed string escaping functions. X-Git-Url: https://vaikene.ee/gitweb/index.html?a=commitdiff_plain;h=bb631d7ca67c86dd758428fb0e18b905514a16e3;p=evaf Added functions to escape binary arrays and renamed string escaping functions. --- diff --git a/src/libs/Common/util.cpp b/src/libs/Common/util.cpp index 1a07a4a..33f6b4d 100644 --- a/src/libs/Common/util.cpp +++ b/src/libs/Common/util.cpp @@ -98,12 +98,12 @@ QVariant eVaf::Common::toVariant(QString const & value, QVariant const & default } } -QByteArray eVaf::Common::toEscapedString(QString const & str) +QByteArray eVaf::Common::strToEscapedCharArray(QString const & str) { QByteArray rval; foreach (QChar c, str) { if (c.unicode() < 32 || c.unicode() >= 127) - rval.append("&#" + QByteArray::number(c.unicode()) + ";"); + rval.append("&#x" + QByteArray::number(c.unicode(), 16) + ";"); else if (c == '\"') rval.append("""); else if (c == '&') @@ -121,7 +121,7 @@ QByteArray eVaf::Common::toEscapedString(QString const & str) return rval; } -QString eVaf::Common::fromEscapedString(QByteArray const & str) +QString eVaf::Common::strFromEscapedCharArray(QByteArray const & str) { QString rval; @@ -181,3 +181,87 @@ QString eVaf::Common::fromEscapedString(QByteArray const & str) return rval; } + +QByteArray eVaf::Common::binToEscapedCharArray(QByteArray const & src) +{ + QByteArray rval; + foreach (uchar c, src) { + if (c < 32 || c >= 127) + rval.append("&#x" + QByteArray::number(c, 16) + ";"); + else if (c == '\"') + rval.append("""); + else if (c == '&') + rval.append("&"); + else if (c == '\'') + rval.append("'"); + else if (c == '<') + rval.append("<"); + else if (c == '>') + rval.append(">"); + else + rval.append(c); + } + + return rval; +} + +QByteArray eVaf::Common::binFromEscapedCharArray(QByteArray const & str) +{ + QByteArray rval; + + bool e = false; + QByteArray ref; + foreach (char c, str) { + if (!e) { + if (c == '&') { + e = true; + ref = "&"; + } + else + rval.append(c); + } + else { + ref.append(c); + if (c == ';') { + e = false; + ref = ref.toLower(); + + if (ref.startsWith("&#x")) { + // Numeric character reference in the HEX format + bool ok; + uchar ucode = ref.mid(3, ref.size() - 4).toUInt(&ok, 16); + if (ok) + rval.append(ucode); + else + // Invalid numeric character reference; output as is + rval.append(ref); + } + else if (ref.startsWith("&#")) { + // Numeric character reference in the DEC format + bool ok; + uchar ucode = ref.mid(2, ref.size() - 3).toUInt(&ok, 10); + if (ok) + rval.append(ucode); + else + // Invalid numeric character reference; output as is + rval.append(ref); + } + else if (ref == """) + rval.append('\"'); + else if (ref == "&") + rval.append('&'); + else if (ref == "'") + rval.append('\''); + else if (ref == "<") + rval.append('<'); + else if (ref == ">") + rval.append('>'); + else + // Unknown reference, output as is + rval.append(ref); + } + } + } + + return rval; +} diff --git a/src/libs/Common/util.h b/src/libs/Common/util.h index de094b3..8237e7c 100644 --- a/src/libs/Common/util.h +++ b/src/libs/Common/util.h @@ -83,20 +83,42 @@ COMMON_EXPORT QVariant toVariant(QString const & value, QVariant const & default * * This function converts a unicode (or any) string to the escaped 7-bit character array. Characters that cannot * be output directly as a printable 7-bit character are output as numeric character references. The result can be - * directly inserted into XML or HTML documents and later converted back with the eVaf::Common::fromEscapedString() + * directly inserted into XML or HTML documents and later converted back with the eVaf::Common::strFromEscapedCharArray() * function. */ -COMMON_EXPORT QByteArray toEscapedString(QString const & str); +COMMON_EXPORT QByteArray strToEscapedCharArray(QString const & str); /** * Converts escaped 7-bit character arrays to unicode string. * @param str Escaped 7-bit character array * @return Unicode string * - * This function converts an escaped 7-bit character array to a unicode string. Numeric character references and + * This function converts an escaped 7-bit character array to the unicode string. Numeric character references and * character entity references are expanded to actual unicode characters. */ -COMMON_EXPORT QString fromEscapedString(QByteArray const & str); +COMMON_EXPORT QString strFromEscapedCharArray(QByteArray const & str); + +/** + * Converts binary arrays to escaped 7-bit character arrays + * @param src Binary array + * @return Escaped 7-bit character array + * + * This function converts a binary array to the escaped 7-bit character array. Bytes that cannot be output + * directly as printable 7-bit characters are output as numeric character references. The result can be directly + * inserted into XML or HTML documents and later converted back with the eVaf::Common::binFromEscapedCharArray() + * function. + */ +COMMON_EXPORT QByteArray binToEscapedCharArray(QByteArray const & src); + +/** + * Converts escaped 7-bit character arrays to binary arrays + * @param str Escaped 7-bit character array + * @return Binary array + * + * This function converts an escaped 7-bit character array to the binary array. Numeric character references and + * character entoty references are expanded to characters and binary bytes. + */ +COMMON_EXPORT QByteArray binFromEscapedCharArray(QByteArray const & str); } // namespace eVaf::Common } // namespace eVaf diff --git a/src/libs/Common/version.h b/src/libs/Common/version.h index dc569f4..e6068b9 100644 --- a/src/libs/Common/version.h +++ b/src/libs/Common/version.h @@ -25,12 +25,12 @@ /** * Module/library version number in the form major,minor,release,build */ -#define VER_FILE_VERSION 0,2,1,8 +#define VER_FILE_VERSION 0,2,2,9 /** * Module/library version number in the string format (shall end with \0) */ -#define VER_FILE_VERSION_STR "0.2.1.8\0" +#define VER_FILE_VERSION_STR "0.2.2.9\0" /** * Module/library name (shall end with \0)