~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/xpcom/io/nsEscape.h

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

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