2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief restricts html messages to an allowed group of tags.
23 * @copyright © Copyright 2017 Intra2net AG
31 #include <stringfunc.hxx>
32 #include <restricted_html.hpp>
43 * @brief Replace all "+" characters found in s to spaces (" ").
45 * @param s string that will be modified.
47 static void unescape_space(string &s)
49 string::size_type pos;
50 while ((pos=s.find('+')) != string::npos)
55 * @brief Converts a hexadecimal sequence to its respective character.
57 * @param s string of size 2. Example: "77"
58 * @return respective character represented by the hex sequence.
60 static char x2c(const string& s)
63 digit=(s[0]>='A' ? ((s[0] & 0xdf)-'A')+10 : (s[0]-'0'));
65 digit+=(s[1]>='A' ? ((s[1] & 0xdf)-'A')+10 : (s[1]-'0'));
70 * @brief Scan a string to find escaped hex chars in the format "%HH" and replace
71 * for their respective character.
72 * Example: "www%2E" becomes "www."
74 * @param s String that will be modified.
76 static void unescape_hex(string& s)
78 static char hex_escape='%';
79 string::size_type escape_pos;
82 for (s=""; ((escape_pos=rest.find(hex_escape)) != string::npos);)
84 if (escape_pos+2<static_cast<unsigned int>(rest.length())
85 && ::isalnum(rest[escape_pos+1]) && ::isalnum(rest[escape_pos+2]))
87 hex_seq=rest.substr(escape_pos+1,2);
88 s=s+rest.substr(0,escape_pos)+x2c(hex_seq);
89 rest=rest.erase(0,escape_pos+3);
93 s=s+rest.substr(0,escape_pos+1);
94 rest=rest.erase(0,escape_pos+1);
101 * @brief Decode url that contains percent-encoding. Replace space " " with "+".
102 * Example: "%77%77%77%2E" becomes "www."
104 * @param s url string.
105 * @return the decoded string.
107 string decode_url(string s)
115 * @brief Verify if the parameter character requires encoding, If it is non
116 * alphanumeric or valid ascii signs.
118 * @param c character to be verified.
119 * @return true if the character should be encoded.
121 bool needs_encoding (const char &c)
123 // some valid ascii signs
124 if (c == '_' || c == '-')
128 if (c > 47 && c < 58)
131 // is uppercase letter?
132 if (c > 64 && c < 91)
135 // is lowercase letter?
136 if (c > 96 && c < 123)
143 * @brief Encode url with percent-encoding. Any non-alphanumeric character is
144 * converted to its hex value with the percent character (%) as prefix, except "_"
145 * and "-". Replace space " " with "+".
147 * @param s url string.
148 * @return the encoded url string.
150 string encode_url(string s)
152 // convert non-alphanumeric characters to hex, convert space to +
154 for (string::iterator pos2=s.begin(); pos2 != s.end(); pos2++)
158 else if (needs_encoding (*pos2))
159 out << '%' << std::uppercase << setw(2) << setfill('0') << \
160 std::hex << (int)(unsigned char)*pos2;
167 } // eo namespace I2n