xpcom/io/nsEscape.h

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:f58fae541c5f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /* First checked in on 98/12/03 by John R. McMullen, derived from net.h/mkparse.c. */
7
8 #ifndef _ESCAPE_H_
9 #define _ESCAPE_H_
10
11 #include "nscore.h"
12 #include "nsError.h"
13 #include "nsString.h"
14
15 /**
16 * Valid mask values for nsEscape
17 * Note: these values are copied in nsINetUtil.idl. Any changes should be kept
18 * in sync.
19 */
20 typedef enum {
21 url_All = 0 /**< %-escape every byte unconditionally */
22 , url_XAlphas = 1u << 0 /**< Normal escape - leave alphas intact, escape the rest */
23 , url_XPAlphas = 1u << 1 /**< As url_XAlphas, but convert spaces (0x20) to '+' and plus to %2B */
24 , url_Path = 1u << 2 /**< As url_XAlphas, but don't escape slash ('/') */
25 } nsEscapeMask;
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /**
32 * Escape the given string according to mask
33 * @param str The string to escape
34 * @param mask How to escape the string
35 * @return A newly allocated escaped string that must be free'd with
36 * nsCRT::free, or null on failure
37 */
38 char * nsEscape(const char * str, nsEscapeMask mask);
39
40 char * nsUnescape(char * str);
41 /* decode % escaped hex codes into character values,
42 * modifies the parameter, returns the same buffer
43 */
44
45 int32_t nsUnescapeCount (char * str);
46 /* decode % escaped hex codes into character values,
47 * modifies the parameter buffer, returns the length of the result
48 * (result may contain \0's).
49 */
50
51 char *
52 nsEscapeHTML(const char * string);
53
54 char16_t *
55 nsEscapeHTML2(const char16_t *aSourceBuffer,
56 int32_t aSourceBufferLen = -1);
57 /*
58 * Escape problem char's for HTML display
59 */
60
61
62 #ifdef __cplusplus
63 }
64 #endif
65
66
67 /**
68 * NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter:
69 *
70 * Note: These values are copied to nsINetUtil.idl
71 * Any changes should be kept in sync
72 */
73 enum EscapeMask {
74 /** url components **/
75 esc_Scheme = 1u << 0,
76 esc_Username = 1u << 1,
77 esc_Password = 1u << 2,
78 esc_Host = 1u << 3,
79 esc_Directory = 1u << 4,
80 esc_FileBaseName = 1u << 5,
81 esc_FileExtension = 1u << 6,
82 esc_FilePath = esc_Directory | esc_FileBaseName | esc_FileExtension,
83 esc_Param = 1u << 7,
84 esc_Query = 1u << 8,
85 esc_Ref = 1u << 9,
86 /** special flags **/
87 esc_Minimal = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref,
88 esc_Forced = 1u << 10, /* forces escaping of existing escape sequences */
89 esc_OnlyASCII = 1u << 11, /* causes non-ascii octets to be skipped */
90 esc_OnlyNonASCII = 1u << 12, /* causes _graphic_ ascii octets (0x20-0x7E)
91 * to be skipped when escaping. causes all
92 * ascii octets (<= 0x7F) to be skipped when unescaping */
93 esc_AlwaysCopy = 1u << 13, /* copy input to result buf even if escaping is unnecessary */
94 esc_Colon = 1u << 14, /* forces escape of colon */
95 esc_SkipControl = 1u << 15 /* skips C0 and DEL from unescaping */
96 };
97
98 /**
99 * NS_EscapeURL
100 *
101 * Escapes invalid char's in an URL segment. Has no side-effect if the URL
102 * segment is already escaped. Otherwise, the escaped URL segment is appended
103 * to |result|.
104 *
105 * @param str url segment string
106 * @param len url segment string length (-1 if unknown)
107 * @param flags url segment type flag
108 * @param result result buffer, untouched if part is already escaped
109 *
110 * @return TRUE if escaping was performed, FALSE otherwise.
111 */
112 bool NS_EscapeURL(const char *str,
113 int32_t len,
114 uint32_t flags,
115 nsACString &result);
116
117 /**
118 * Expands URL escape sequences... beware embedded null bytes!
119 *
120 * @param str url string to unescape
121 * @param len length of |str|
122 * @param flags only esc_OnlyNonASCII, esc_SkipControl and esc_AlwaysCopy
123 * are recognized
124 * @param result result buffer, untouched if |str| is already unescaped
125 *
126 * @return TRUE if unescaping was performed, FALSE otherwise.
127 */
128 bool NS_UnescapeURL(const char *str,
129 int32_t len,
130 uint32_t flags,
131 nsACString &result);
132
133 /** returns resultant string length **/
134 inline int32_t NS_UnescapeURL(char *str) {
135 return nsUnescapeCount(str);
136 }
137
138 /**
139 * String friendly versions...
140 */
141 inline const nsCSubstring &
142 NS_EscapeURL(const nsCSubstring &str, uint32_t flags, nsCSubstring &result) {
143 if (NS_EscapeURL(str.Data(), str.Length(), flags, result))
144 return result;
145 return str;
146 }
147 inline const nsCSubstring &
148 NS_UnescapeURL(const nsCSubstring &str, uint32_t flags, nsCSubstring &result) {
149 if (NS_UnescapeURL(str.Data(), str.Length(), flags, result))
150 return result;
151 return str;
152 }
153
154 /**
155 * CString version of nsEscape. Returns true on success, false
156 * on out of memory. To reverse this function, use NS_UnescapeURL.
157 */
158 inline bool
159 NS_Escape(const nsCString& aOriginal, nsCString& aEscaped,
160 nsEscapeMask aMask)
161 {
162 char* esc = nsEscape(aOriginal.get(), aMask);
163 if (! esc)
164 return false;
165 aEscaped.Adopt(esc);
166 return true;
167 }
168
169 /**
170 * Inline unescape of mutable string object.
171 */
172 inline nsCString &
173 NS_UnescapeURL(nsCString &str)
174 {
175 str.SetLength(nsUnescapeCount(str.BeginWriting()));
176 return str;
177 }
178
179 #endif // _ESCAPE_H_

mercurial