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
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/
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
15 * The Original Code is mozilla.org code.
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.
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.
36 * ***** END LICENSE BLOCK ***** */
38 /* First checked in on 98/12/03 by John R. McMullen, derived from net.h/mkparse.c. */
49 * Valid mask values for nsEscape
50 * Note: these values are copied in nsINetUtil.idl. Any changes should be kept
54 url_All
= 0 /**< %-escape every byte uncondtionally */
55 , url_XAlphas
= PR_BIT(0) /**< Normal escape - leave alphas intact, escape the rest */
56 , url_XPAlphas
= PR_BIT(1) /**< As url_XAlphas, but convert spaces (0x20) to '+' and plus to %2B */
57 , url_Path
= PR_BIT(2) /**< As url_XAlphas, but don't escape slash ('/') */
65 * Escape the given string according to mask
66 * @param str The string to escape
67 * @param mask How to escape the string
68 * @return A newly allocated escaped string that must be free'd with
69 * nsCRT::free, or null on failure
71 NS_COM
char * nsEscape(const char * str
, nsEscapeMask mask
);
73 NS_COM
char * nsUnescape(char * str
);
74 /* decode % escaped hex codes into character values,
75 * modifies the parameter, returns the same buffer
78 NS_COM PRInt32
nsUnescapeCount (char * str
);
79 /* decode % escaped hex codes into character values,
80 * modifies the parameter buffer, returns the length of the result
81 * (result may contain \0's).
85 nsEscapeHTML(const char * string
);
88 nsEscapeHTML2(const PRUnichar
*aSourceBuffer
,
89 PRInt32 aSourceBufferLen
= -1);
91 * Escape problem char's for HTML display
101 * NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter:
103 * Note: These values are copied to nsINetUtil.idl
104 * Any changes should be kept in sync
107 /** url components **/
108 esc_Scheme
= PR_BIT(0),
109 esc_Username
= PR_BIT(1),
110 esc_Password
= PR_BIT(2),
111 esc_Host
= PR_BIT(3),
112 esc_Directory
= PR_BIT(4),
113 esc_FileBaseName
= PR_BIT(5),
114 esc_FileExtension
= PR_BIT(6),
115 esc_FilePath
= esc_Directory
| esc_FileBaseName
| esc_FileExtension
,
116 esc_Param
= PR_BIT(7),
117 esc_Query
= PR_BIT(8),
119 /** special flags **/
120 esc_Minimal
= esc_Scheme
| esc_Username
| esc_Password
| esc_Host
| esc_FilePath
| esc_Param
| esc_Query
| esc_Ref
,
121 esc_Forced
= PR_BIT(10), /* forces escaping of existing escape sequences */
122 esc_OnlyASCII
= PR_BIT(11), /* causes non-ascii octets to be skipped */
123 esc_OnlyNonASCII
= PR_BIT(12), /* causes _graphic_ ascii octets (0x20-0x7E)
124 * to be skipped when escaping. causes all
125 * ascii octets (<= 0x7F) to be skipped when unescaping */
126 esc_AlwaysCopy
= PR_BIT(13), /* copy input to result buf even if escaping is unnecessary */
127 esc_Colon
= PR_BIT(14), /* forces escape of colon */
128 esc_SkipControl
= PR_BIT(15) /* skips C0 and DEL from unescaping */
134 * Escapes invalid char's in an URL segment. Has no side-effect if the URL
135 * segment is already escaped. Otherwise, the escaped URL segment is appended
138 * @param str url segment string
139 * @param len url segment string length (-1 if unknown)
140 * @param flags url segment type flag
141 * @param result result buffer, untouched if part is already escaped
143 * @return TRUE if escaping was performed, FALSE otherwise.
145 NS_COM PRBool
NS_EscapeURL(const char *str
,
151 * Expands URL escape sequences... beware embedded null bytes!
153 * @param str url string to unescape
154 * @param len length of |str|
155 * @param flags only esc_OnlyNonASCII, esc_SkipControl and esc_AlwaysCopy
157 * @param result result buffer, untouched if |str| is already unescaped
159 * @return TRUE if unescaping was performed, FALSE otherwise.
161 NS_COM PRBool
NS_UnescapeURL(const char *str
,
166 /** returns resultant string length **/
167 inline PRInt32
NS_UnescapeURL(char *str
) {
168 return nsUnescapeCount(str
);
172 * String friendly versions...
174 inline const nsCSubstring
&
175 NS_EscapeURL(const nsCSubstring
&str
, PRUint32 flags
, nsCSubstring
&result
) {
176 if (NS_EscapeURL(str
.Data(), str
.Length(), flags
, result
))
180 inline const nsCSubstring
&
181 NS_UnescapeURL(const nsCSubstring
&str
, PRUint32 flags
, nsCSubstring
&result
) {
182 if (NS_UnescapeURL(str
.Data(), str
.Length(), flags
, result
))
187 // nsACString is nsCSubstring when MOZ_V1_STRING_ABI is undefined.
188 #ifdef MOZ_V1_STRING_ABI
189 inline const nsACString
&
190 NS_EscapeURL(const nsACString
&str
, PRUint32 flags
, nsACString
&result
) {
191 // The iterator version of BeginReading provides us with both the data
192 // pointer and the length with only one function call.
193 nsACString::const_iterator iter
;
194 str
.BeginReading(iter
);
195 if (NS_EscapeURL(iter
.get(), iter
.size_forward(), flags
, result
))
199 inline const nsACString
&
200 NS_EscapeURL(const nsCSubstring
&str
, PRUint32 flags
, nsACString
&result
) {
201 if (NS_EscapeURL(str
.Data(), str
.Length(), flags
, result
))
205 inline const nsACString
&
206 NS_UnescapeURL(const nsACString
&str
, PRUint32 flags
, nsACString
&result
) {
207 // The iterator version of BeginReading provides us with both the data
208 // pointer and the length with only one function call.
209 nsACString::const_iterator iter
;
210 str
.BeginReading(iter
);
211 if (NS_UnescapeURL(iter
.get(), iter
.size_forward(), flags
, result
))
215 inline const nsACString
&
216 NS_UnescapeURL(const nsCSubstring
&str
, PRUint32 flags
, nsACString
&result
) {
217 if (NS_UnescapeURL(str
.Data(), str
.Length(), flags
, result
))
221 #endif // MOZ_V1_STRING_ABI
224 * CString version of nsEscape. Returns true on success, false
225 * on out of memory. To reverse this function, use NS_UnescapeURL.
228 NS_Escape(const nsCString
& aOriginal
, nsCString
& aEscaped
,
231 char* esc
= nsEscape(aOriginal
.get(), aMask
);
239 * Inline unescape of mutable string object.
242 NS_UnescapeURL(nsCString
&str
)
244 str
.SetLength(nsUnescapeCount(str
.BeginWriting()));