Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : /* First checked in on 98/12/03 by John R. McMullen, derived from net.h/mkparse.c. */
8 :
9 : #ifndef _ESCAPE_H_
10 : #define _ESCAPE_H_
11 :
12 : #include "nscore.h"
13 : #include "nsError.h"
14 : #include "nsString.h"
15 :
16 : /**
17 : * Valid mask values for nsEscape
18 : * Note: these values are copied in nsINetUtil.idl. Any changes should be kept
19 : * in sync.
20 : */
21 : typedef enum {
22 : url_All = 0, // %-escape every byte unconditionally
23 : url_XAlphas = 1u << 0, // Normal escape - leave alphas intact, escape the rest
24 : url_XPAlphas = 1u << 1, // As url_XAlphas, but convert spaces (0x20) to '+' and plus to %2B
25 : url_Path = 1u << 2 // As url_XAlphas, but don't escape slash ('/')
26 : } nsEscapeMask;
27 :
28 : #ifdef __cplusplus
29 : extern "C" {
30 : #endif
31 :
32 : /**
33 : * Escape the given string according to mask
34 : * @param aSstr The string to escape
35 : * @param aLength The length of the string to escape
36 : * @param aOutputLen A pointer that will be used to store the length of the
37 : * output string, if not null
38 : * @param aMask How to escape the string
39 : * @return A newly allocated escaped string that must be free'd with
40 : * nsCRT::free, or null on failure
41 : * @note: Please, don't use this function. Use NS_Escape instead!
42 : */
43 : char* nsEscape(const char* aStr, size_t aLength, size_t* aOutputLen,
44 : nsEscapeMask aMask);
45 :
46 : char* nsUnescape(char* aStr);
47 : /* decode % escaped hex codes into character values,
48 : * modifies the parameter, returns the same buffer
49 : */
50 :
51 : int32_t nsUnescapeCount(char* aStr);
52 : /* decode % escaped hex codes into character values,
53 : * modifies the parameter buffer, returns the length of the result
54 : * (result may contain \0's).
55 : */
56 :
57 : char*
58 : nsEscapeHTML(const char* aString);
59 :
60 : char16_t*
61 : nsEscapeHTML2(const char16_t* aSourceBuffer,
62 : int32_t aSourceBufferLen = -1);
63 : /*
64 : * Escape problem char's for HTML display
65 : */
66 :
67 :
68 : #ifdef __cplusplus
69 : }
70 : #endif
71 :
72 :
73 : /**
74 : * NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter:
75 : *
76 : * Note: These values are copied to nsINetUtil.idl
77 : * Any changes should be kept in sync
78 : */
79 : enum EscapeMask {
80 : /** url components **/
81 : esc_Scheme = 1u << 0,
82 : esc_Username = 1u << 1,
83 : esc_Password = 1u << 2,
84 : esc_Host = 1u << 3,
85 : esc_Directory = 1u << 4,
86 : esc_FileBaseName = 1u << 5,
87 : esc_FileExtension = 1u << 6,
88 : esc_FilePath = esc_Directory | esc_FileBaseName | esc_FileExtension,
89 : esc_Param = 1u << 7,
90 : esc_Query = 1u << 8,
91 : esc_Ref = 1u << 9,
92 : /** special flags **/
93 : esc_Minimal = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref,
94 : esc_Forced = 1u << 10, /* forces escaping of existing escape sequences */
95 : esc_OnlyASCII = 1u << 11, /* causes non-ascii octets to be skipped */
96 : esc_OnlyNonASCII = 1u << 12, /* causes _graphic_ ascii octets (0x20-0x7E)
97 : * to be skipped when escaping. causes all
98 : * ascii octets (<= 0x7F) to be skipped when unescaping */
99 : esc_AlwaysCopy = 1u << 13, /* copy input to result buf even if escaping is unnecessary */
100 : esc_Colon = 1u << 14, /* forces escape of colon */
101 : esc_SkipControl = 1u << 15 /* skips C0 and DEL from unescaping */
102 : };
103 :
104 : /**
105 : * NS_EscapeURL
106 : *
107 : * Escapes invalid char's in an URL segment. Has no side-effect if the URL
108 : * segment is already escaped, unless aFlags has the esc_Forced bit in which
109 : * case % will also be escaped. Iff some part of aStr is escaped is the
110 : * final result appended to aResult. You can also request that aStr is
111 : * always appended to aResult with esc_AlwaysCopy.
112 : *
113 : * @param aStr url segment string
114 : * @param aLen url segment string length (-1 if unknown)
115 : * @param aFlags url segment type flag (see EscapeMask above)
116 : * @param aResult result buffer, untouched if aStr is already escaped unless
117 : * aFlags has esc_AlwaysCopy
118 : *
119 : * @return true if aResult was written to (i.e. at least one character was
120 : * escaped or esc_AlwaysCopy was requested), false otherwise.
121 : */
122 : bool NS_EscapeURL(const char* aStr,
123 : int32_t aLen,
124 : uint32_t aFlags,
125 : nsACString& aResult);
126 :
127 : /**
128 : * Expands URL escape sequences... beware embedded null bytes!
129 : *
130 : * @param aStr url string to unescape
131 : * @param aLen length of aStr
132 : * @param aFlags only esc_OnlyNonASCII, esc_SkipControl and esc_AlwaysCopy
133 : * are recognized
134 : * @param aResult result buffer, untouched if aStr is already unescaped unless
135 : * aFlags has esc_AlwaysCopy
136 : *
137 : * @return true if aResult was written to (i.e. at least one character was
138 : * unescaped or esc_AlwaysCopy was requested), false otherwise.
139 : */
140 : bool NS_UnescapeURL(const char* aStr,
141 : int32_t aLen,
142 : uint32_t aFlags,
143 : nsACString& aResult);
144 :
145 : /** returns resultant string length **/
146 : inline int32_t
147 0 : NS_UnescapeURL(char* aStr)
148 : {
149 0 : return nsUnescapeCount(aStr);
150 : }
151 :
152 : /**
153 : * String friendly versions...
154 : */
155 : inline const nsACString&
156 0 : NS_EscapeURL(const nsACString& aStr, uint32_t aFlags, nsACString& aResult)
157 : {
158 0 : if (NS_EscapeURL(aStr.Data(), aStr.Length(), aFlags, aResult)) {
159 0 : return aResult;
160 : }
161 0 : return aStr;
162 : }
163 :
164 : /**
165 : * Fallible version of NS_EscapeURL. On success aResult will point to either
166 : * the original string or an escaped copy.
167 : */
168 : nsresult
169 : NS_EscapeURL(const nsACString& aStr, uint32_t aFlags, nsACString& aResult,
170 : const mozilla::fallible_t&);
171 :
172 : inline const nsACString&
173 2 : NS_UnescapeURL(const nsACString& aStr, uint32_t aFlags, nsACString& aResult)
174 : {
175 2 : if (NS_UnescapeURL(aStr.Data(), aStr.Length(), aFlags, aResult)) {
176 2 : return aResult;
177 : }
178 0 : return aStr;
179 : }
180 : const nsAString&
181 : NS_EscapeURL(const nsAString& aStr, uint32_t aFlags, nsAString& aResult);
182 :
183 : /**
184 : * Percent-escapes all characters in aStr that occurs in aForbidden.
185 : * @param aStr the input URL string
186 : * @param aForbidden the characters that should be escaped if found in aStr
187 : * @note that aForbidden MUST be sorted (low to high)
188 : * @param aResult the result if some characters were escaped
189 : * @return aResult if some characters were escaped, or aStr otherwise (aResult
190 : * is unmodified in that case)
191 : */
192 : const nsAString&
193 : NS_EscapeURL(const nsString& aStr, const nsTArray<char16_t>& aForbidden,
194 : nsAString& aResult);
195 :
196 : /**
197 : * CString version of nsEscape. Returns true on success, false
198 : * on out of memory. To reverse this function, use NS_UnescapeURL.
199 : */
200 : inline bool
201 0 : NS_Escape(const nsACString& aOriginal, nsACString& aEscaped,
202 : nsEscapeMask aMask)
203 : {
204 0 : size_t escLen = 0;
205 0 : char* esc = nsEscape(aOriginal.BeginReading(), aOriginal.Length(), &escLen,
206 0 : aMask);
207 0 : if (! esc) {
208 0 : return false;
209 : }
210 0 : aEscaped.Adopt(esc, escLen);
211 0 : return true;
212 : }
213 :
214 : /**
215 : * Inline unescape of mutable string object.
216 : */
217 : inline nsACString&
218 2855 : NS_UnescapeURL(nsACString& aStr)
219 : {
220 2855 : aStr.SetLength(nsUnescapeCount(aStr.BeginWriting()));
221 2855 : return aStr;
222 : }
223 :
224 : #endif // _ESCAPE_H_
|