d59bcff3bc22ea85f62a00122fc8e55664d999d5
[libi2ncommon] / src / restricted_html.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
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.
13
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.
16
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.
19 */
20 /** @file
21  * @brief restricts html messages to an allowed group of tags.
22  *
23  * @copyright © Copyright 2017 Intra2net AG
24  *
25  */
26
27 #include <string>
28 #include <sstream>
29 #include <iomanip>
30
31 #include <stringfunc.hxx>
32 #include <restricted_html.hpp>
33
34 using namespace std;
35
36
37 namespace I2n
38 {
39
40
41
42 /**
43  * @brief Replace all "+" characters found in s to spaces (" ").
44  *
45  * @param s string that will be modified.
46  */
47 static void unescape_space(string &s)
48 {
49     string::size_type pos;
50     while ((pos=s.find('+')) != string::npos)
51         s[pos]=' ';
52 }
53
54 /**
55  * @brief Converts a hexadecimal sequence to its respective character.
56  *
57  * @param s string of size 2. Example: "77"
58  * @return respective character represented by the hex sequence.
59  */
60 static char x2c(const string& s)
61 {
62     char digit;
63     digit=(s[0]>='A' ? ((s[0] & 0xdf)-'A')+10 : (s[0]-'0'));
64     digit*=16;
65     digit+=(s[1]>='A' ? ((s[1] & 0xdf)-'A')+10 : (s[1]-'0'));
66     return digit;
67 }
68
69 /**
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."
73  *
74  * @param s String that will be modified.
75  */
76 static void unescape_hex(string& s)
77 {
78     static char hex_escape='%';
79     string::size_type escape_pos;
80     string hex_seq;
81     string rest=s;
82     for (s=""; ((escape_pos=rest.find(hex_escape)) != string::npos);)
83     {
84         if (escape_pos+2<static_cast<unsigned int>(rest.length())
85             && ::isalnum(rest[escape_pos+1]) && ::isalnum(rest[escape_pos+2]))
86         {
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);
90         }
91         else
92         {
93             s=s+rest.substr(0,escape_pos+1);
94             rest=rest.erase(0,escape_pos+1);
95         }
96     }
97     s+=rest;
98 }
99
100 /**
101 * @brief Decode url that contains percent-encoding. Replace space " " with "+".
102 * Example: "%77%77%77%2E" becomes "www."
103 *
104 * @param s url string.
105 * @return the decoded string.
106 */
107 string decode_url(string s)
108 {
109     unescape_space (s);
110     unescape_hex (s);
111     return (s);
112 }
113
114 /**
115  * @brief Verify if the parameter character requires encoding, If it is non
116  * alphanumeric or valid ascii signs.
117  *
118  * @param c character to be verified.
119  * @return true if the character should be encoded.
120  */
121 bool needs_encoding (const char &c)
122 {
123     // some valid ascii signs
124     if (c == '_' || c == '-')
125         return false;
126
127     // is digit?
128     if (c > 47 && c < 58)
129         return false;
130
131     // is uppercase letter?
132     if (c > 64 && c < 91)
133         return false;
134
135     // is lowercase letter?
136     if (c > 96 && c < 123)
137         return false;
138
139     return true;
140 }
141
142 /**
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 "+".
146 *
147 * @param s url string.
148 * @return the encoded url string.
149 */
150 string encode_url(string s)
151 {
152     // convert non-alphanumeric characters to hex, convert space to +
153     ostringstream out;
154     for (string::iterator pos2=s.begin(); pos2 != s.end(); pos2++)
155     {
156         if (*pos2 == ' ')
157             out << '+';
158         else if (needs_encoding (*pos2))
159             out << '%' << std::uppercase << setw(2) << setfill('0') << \
160                 std::hex << (int)(unsigned char)*pos2;
161         else
162             out << (*pos2);
163     }
164
165     return out.str();
166 }
167 } // eo namespace I2n