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 | /**
|
---|
49 | * Valid mask values for nsEscape
|
---|
50 | */
|
---|
51 | typedef enum {
|
---|
52 | url_XAlphas = PR_BIT(0) /**< Normal escape - leave alphas intact, escape the rest */
|
---|
53 | , url_XPAlphas = PR_BIT(1) /**< As url_XAlphas, but convert spaces (0x20) to '+' and plus to %2B */
|
---|
54 | , url_Path = PR_BIT(2) /**< As url_XAlphas, but don't escape slash ('/') */
|
---|
55 | } nsEscapeMask;
|
---|
56 |
|
---|
57 | #ifdef __cplusplus
|
---|
58 | extern "C" {
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Escape the given string according to mask
|
---|
63 | * @param str The string to escape
|
---|
64 | * @param mask How to escape the string
|
---|
65 | * @return A newly allocated escaped string that must be free'd with
|
---|
66 | * nsCRT::free, or null on failure
|
---|
67 | */
|
---|
68 | NS_COM char * nsEscape(const char * str, nsEscapeMask mask);
|
---|
69 |
|
---|
70 | NS_COM char * nsUnescape(char * str);
|
---|
71 | /* decode % escaped hex codes into character values,
|
---|
72 | * modifies the parameter, returns the same buffer
|
---|
73 | */
|
---|
74 |
|
---|
75 | NS_COM PRInt32 nsUnescapeCount (char * str);
|
---|
76 | /* decode % escaped hex codes into character values,
|
---|
77 | * modifies the parameter buffer, returns the length of the result
|
---|
78 | * (result may contain \0's).
|
---|
79 | */
|
---|
80 |
|
---|
81 | NS_COM char *
|
---|
82 | nsEscapeHTML(const char * string);
|
---|
83 |
|
---|
84 | NS_COM PRUnichar *
|
---|
85 | nsEscapeHTML2(const PRUnichar *aSourceBuffer,
|
---|
86 | PRInt32 aSourceBufferLen = -1);
|
---|
87 | /*
|
---|
88 | * Escape problem char's for HTML display
|
---|
89 | */
|
---|
90 |
|
---|
91 |
|
---|
92 | #ifdef __cplusplus
|
---|
93 | }
|
---|
94 | #endif
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter:
|
---|
99 | */
|
---|
100 | enum EscapeMask {
|
---|
101 | /** url components **/
|
---|
102 | esc_Scheme = PR_BIT(0),
|
---|
103 | esc_Username = PR_BIT(1),
|
---|
104 | esc_Password = PR_BIT(2),
|
---|
105 | esc_Host = PR_BIT(3),
|
---|
106 | esc_Directory = PR_BIT(4),
|
---|
107 | esc_FileBaseName = PR_BIT(5),
|
---|
108 | esc_FileExtension = PR_BIT(6),
|
---|
109 | esc_FilePath = esc_Directory | esc_FileBaseName | esc_FileExtension,
|
---|
110 | esc_Param = PR_BIT(7),
|
---|
111 | esc_Query = PR_BIT(8),
|
---|
112 | esc_Ref = PR_BIT(9),
|
---|
113 | /** special flags **/
|
---|
114 | esc_Minimal = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref,
|
---|
115 | esc_Forced = PR_BIT(10), /* forces escaping of existing escape sequences */
|
---|
116 | esc_OnlyASCII = PR_BIT(11), /* causes non-ascii octets to be skipped */
|
---|
117 | esc_OnlyNonASCII = PR_BIT(12), /* causes _graphic_ ascii octets (0x20-0x7E)
|
---|
118 | * to be skipped when escaping. causes all
|
---|
119 | * ascii octets to be skipped when unescaping */
|
---|
120 | esc_AlwaysCopy = PR_BIT(13), /* copy input to result buf even if escaping is unnecessary */
|
---|
121 | esc_Colon = PR_BIT(14), /* forces escape of colon */
|
---|
122 | esc_SkipControl = PR_BIT(15) /* skips C0 and DEL from unescaping */
|
---|
123 | };
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * NS_EscapeURL
|
---|
127 | *
|
---|
128 | * Escapes invalid char's in an URL segment. Has no side-effect if the URL
|
---|
129 | * segment is already escaped. Otherwise, the escaped URL segment is appended
|
---|
130 | * to |result|.
|
---|
131 | *
|
---|
132 | * @param str url segment string
|
---|
133 | * @param len url segment string length (-1 if unknown)
|
---|
134 | * @param flags url segment type flag
|
---|
135 | * @param result result buffer, untouched if part is already escaped
|
---|
136 | *
|
---|
137 | * @return TRUE if escaping was performed, FALSE otherwise.
|
---|
138 | */
|
---|
139 | NS_COM PRBool NS_EscapeURL(const char *str,
|
---|
140 | PRInt32 len,
|
---|
141 | PRInt16 flags,
|
---|
142 | nsACString &result);
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Expands URL escape sequences... beware embedded null bytes!
|
---|
146 | *
|
---|
147 | * @param str url string to unescape
|
---|
148 | * @param len length of |str|
|
---|
149 | * @param flags only esc_OnlyNonASCII, esc_SkipControl and esc_AlwaysCopy
|
---|
150 | * are recognized
|
---|
151 | * @param result result buffer, untouched if |str| is already unescaped
|
---|
152 | *
|
---|
153 | * @return TRUE if unescaping was performed, FALSE otherwise.
|
---|
154 | */
|
---|
155 | NS_COM PRBool NS_UnescapeURL(const char *str,
|
---|
156 | PRInt32 len,
|
---|
157 | PRInt16 flags,
|
---|
158 | nsACString &result);
|
---|
159 |
|
---|
160 | /** returns resultant string length **/
|
---|
161 | inline PRInt32 NS_UnescapeURL(char *str) { return nsUnescapeCount(str); }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * string friendly versions...
|
---|
165 | */
|
---|
166 | inline const nsACString &
|
---|
167 | NS_EscapeURL(const nsASingleFragmentCString &part, PRInt16 partType, nsACString &result) {
|
---|
168 | const char *temp;
|
---|
169 | if (NS_EscapeURL(part.BeginReading(temp), part.Length(), partType, result))
|
---|
170 | return result;
|
---|
171 | return part;
|
---|
172 | }
|
---|
173 | inline const nsACString &
|
---|
174 | NS_UnescapeURL(const nsASingleFragmentCString &str, PRInt16 flags, nsACString &result) {
|
---|
175 | const char *temp;
|
---|
176 | if (NS_UnescapeURL(str.BeginReading(temp), str.Length(), flags, result))
|
---|
177 | return result;
|
---|
178 | return str;
|
---|
179 | }
|
---|
180 | // inline unescape
|
---|
181 | inline nsAFlatCString &
|
---|
182 | NS_UnescapeURL(nsAFlatCString &str)
|
---|
183 | {
|
---|
184 | str.SetLength(nsUnescapeCount(str.BeginWriting()));
|
---|
185 | return str;
|
---|
186 | }
|
---|
187 |
|
---|
188 | #endif // _ESCAPE_H_
|
---|