1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
26 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /* First checked in on 98/12/03 by John R. McMullen, derived from net.h/mkparse.c. */
|
---|
39 |
|
---|
40 | #ifndef _ESCAPE_H_
|
---|
41 | #define _ESCAPE_H_
|
---|
42 |
|
---|
43 | #include "prtypes.h"
|
---|
44 | #include "nscore.h"
|
---|
45 | #include "nsError.h"
|
---|
46 | #include "nsString.h"
|
---|
47 |
|
---|
48 | #ifdef VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
|
---|
49 | #define nsEscape VBoxNsxpnsEscape
|
---|
50 | #define nsUnescape VBoxNsxpnsUnescape
|
---|
51 | #define nsUnescapeCount VBoxNsxpnsUnescapeCount
|
---|
52 | #define nsEscapeHTML VBoxNsxpnsEscapeHTML
|
---|
53 | #define nsEscapeHTML2 VBoxNsxpnsEscapeHTML2
|
---|
54 | #endif /* VBOX_WITH_XPCOM_NAMESPACE_CLEANUP */
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Valid mask values for nsEscape
|
---|
58 | */
|
---|
59 | typedef enum {
|
---|
60 | url_XAlphas = PR_BIT(0) /**< Normal escape - leave alphas intact, escape the rest */
|
---|
61 | , url_XPAlphas = PR_BIT(1) /**< As url_XAlphas, but convert spaces (0x20) to '+' and plus to %2B */
|
---|
62 | , url_Path = PR_BIT(2) /**< As url_XAlphas, but don't escape slash ('/') */
|
---|
63 | } nsEscapeMask;
|
---|
64 |
|
---|
65 | #ifdef __cplusplus
|
---|
66 | extern "C" {
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Escape the given string according to mask
|
---|
71 | * @param str The string to escape
|
---|
72 | * @param mask How to escape the string
|
---|
73 | * @return A newly allocated escaped string that must be free'd with
|
---|
74 | * nsCRT::free, or null on failure
|
---|
75 | */
|
---|
76 | NS_COM char * nsEscape(const char * str, nsEscapeMask mask);
|
---|
77 |
|
---|
78 | NS_COM char * nsUnescape(char * str);
|
---|
79 | /* decode % escaped hex codes into character values,
|
---|
80 | * modifies the parameter, returns the same buffer
|
---|
81 | */
|
---|
82 |
|
---|
83 | NS_COM PRInt32 nsUnescapeCount (char * str);
|
---|
84 | /* decode % escaped hex codes into character values,
|
---|
85 | * modifies the parameter buffer, returns the length of the result
|
---|
86 | * (result may contain \0's).
|
---|
87 | */
|
---|
88 |
|
---|
89 | NS_COM char *
|
---|
90 | nsEscapeHTML(const char * string);
|
---|
91 |
|
---|
92 | NS_COM PRUnichar *
|
---|
93 | nsEscapeHTML2(const PRUnichar *aSourceBuffer,
|
---|
94 | PRInt32 aSourceBufferLen = -1);
|
---|
95 | /*
|
---|
96 | * Escape problem char's for HTML display
|
---|
97 | */
|
---|
98 |
|
---|
99 |
|
---|
100 | #ifdef __cplusplus
|
---|
101 | }
|
---|
102 | #endif
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter:
|
---|
107 | */
|
---|
108 | enum EscapeMask {
|
---|
109 | /** url components **/
|
---|
110 | esc_Scheme = PR_BIT(0),
|
---|
111 | esc_Username = PR_BIT(1),
|
---|
112 | esc_Password = PR_BIT(2),
|
---|
113 | esc_Host = PR_BIT(3),
|
---|
114 | esc_Directory = PR_BIT(4),
|
---|
115 | esc_FileBaseName = PR_BIT(5),
|
---|
116 | esc_FileExtension = PR_BIT(6),
|
---|
117 | esc_FilePath = esc_Directory | esc_FileBaseName | esc_FileExtension,
|
---|
118 | esc_Param = PR_BIT(7),
|
---|
119 | esc_Query = PR_BIT(8),
|
---|
120 | esc_Ref = PR_BIT(9),
|
---|
121 | /** special flags **/
|
---|
122 | esc_Minimal = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref,
|
---|
123 | esc_Forced = PR_BIT(10), /* forces escaping of existing escape sequences */
|
---|
124 | esc_OnlyASCII = PR_BIT(11), /* causes non-ascii octets to be skipped */
|
---|
125 | esc_OnlyNonASCII = PR_BIT(12), /* causes _graphic_ ascii octets (0x20-0x7E)
|
---|
126 | * to be skipped when escaping. causes all
|
---|
127 | * ascii octets to be skipped when unescaping */
|
---|
128 | esc_AlwaysCopy = PR_BIT(13), /* copy input to result buf even if escaping is unnecessary */
|
---|
129 | esc_Colon = PR_BIT(14), /* forces escape of colon */
|
---|
130 | esc_SkipControl = PR_BIT(15) /* skips C0 and DEL from unescaping */
|
---|
131 | };
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * NS_EscapeURL
|
---|
135 | *
|
---|
136 | * Escapes invalid char's in an URL segment. Has no side-effect if the URL
|
---|
137 | * segment is already escaped. Otherwise, the escaped URL segment is appended
|
---|
138 | * to |result|.
|
---|
139 | *
|
---|
140 | * @param str url segment string
|
---|
141 | * @param len url segment string length (-1 if unknown)
|
---|
142 | * @param flags url segment type flag
|
---|
143 | * @param result result buffer, untouched if part is already escaped
|
---|
144 | *
|
---|
145 | * @return TRUE if escaping was performed, FALSE otherwise.
|
---|
146 | */
|
---|
147 | NS_COM PRBool NS_EscapeURL(const char *str,
|
---|
148 | PRInt32 len,
|
---|
149 | PRInt16 flags,
|
---|
150 | nsACString &result);
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Expands URL escape sequences... beware embedded null bytes!
|
---|
154 | *
|
---|
155 | * @param str url string to unescape
|
---|
156 | * @param len length of |str|
|
---|
157 | * @param flags only esc_OnlyNonASCII, esc_SkipControl and esc_AlwaysCopy
|
---|
158 | * are recognized
|
---|
159 | * @param result result buffer, untouched if |str| is already unescaped
|
---|
160 | *
|
---|
161 | * @return TRUE if unescaping was performed, FALSE otherwise.
|
---|
162 | */
|
---|
163 | NS_COM PRBool NS_UnescapeURL(const char *str,
|
---|
164 | PRInt32 len,
|
---|
165 | PRInt16 flags,
|
---|
166 | nsACString &result);
|
---|
167 |
|
---|
168 | /** returns resultant string length **/
|
---|
169 | inline PRInt32 NS_UnescapeURL(char *str) { return nsUnescapeCount(str); }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * string friendly versions...
|
---|
173 | */
|
---|
174 | inline const nsACString &
|
---|
175 | NS_EscapeURL(const nsASingleFragmentCString &part, PRInt16 partType, nsACString &result) {
|
---|
176 | const char *temp;
|
---|
177 | if (NS_EscapeURL(part.BeginReading(temp), part.Length(), partType, result))
|
---|
178 | return result;
|
---|
179 | return part;
|
---|
180 | }
|
---|
181 | inline const nsACString &
|
---|
182 | NS_UnescapeURL(const nsASingleFragmentCString &str, PRInt16 flags, nsACString &result) {
|
---|
183 | const char *temp;
|
---|
184 | if (NS_UnescapeURL(str.BeginReading(temp), str.Length(), flags, result))
|
---|
185 | return result;
|
---|
186 | return str;
|
---|
187 | }
|
---|
188 | // inline unescape
|
---|
189 | inline nsAFlatCString &
|
---|
190 | NS_UnescapeURL(nsAFlatCString &str)
|
---|
191 | {
|
---|
192 | str.SetLength(nsUnescapeCount(str.BeginWriting()));
|
---|
193 | return str;
|
---|
194 | }
|
---|
195 |
|
---|
196 | #endif // _ESCAPE_H_
|
---|