Back to index

lightning-sunbird  0.9+nobinonly
nsEscape.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*     First checked in on 98/12/03 by John R. McMullen, derived from net.h/mkparse.c. */
00039 
00040 #ifndef _ESCAPE_H_
00041 #define _ESCAPE_H_
00042 
00043 #include "prtypes.h"
00044 #include "nscore.h"
00045 #include "nsError.h"
00046 #include "nsString.h"
00047 
00051 typedef enum {
00052        url_All       = 0         
00053 ,      url_XAlphas   = PR_BIT(0) 
00054 ,      url_XPAlphas  = PR_BIT(1) 
00055 ,      url_Path      = PR_BIT(2) 
00056 } nsEscapeMask;
00057 
00058 #ifdef __cplusplus
00059 extern "C" {
00060 #endif
00061 
00069 NS_COM char * nsEscape(const char * str, nsEscapeMask mask);
00070 
00071 NS_COM char * nsUnescape(char * str);
00072        /* decode % escaped hex codes into character values,
00073         * modifies the parameter, returns the same buffer
00074         */
00075 
00076 NS_COM PRInt32 nsUnescapeCount (char * str);
00077        /* decode % escaped hex codes into character values,
00078         * modifies the parameter buffer, returns the length of the result
00079         * (result may contain \0's).
00080         */
00081 
00082 NS_COM char *
00083 nsEscapeHTML(const char * string);
00084 
00085 NS_COM PRUnichar *
00086 nsEscapeHTML2(const PRUnichar *aSourceBuffer,
00087               PRInt32 aSourceBufferLen = -1);
00088  /*
00089   * Escape problem char's for HTML display 
00090   */
00091 
00092 
00093 #ifdef __cplusplus
00094 }
00095 #endif
00096 
00097 
00101 enum EscapeMask {
00103   esc_Scheme         = PR_BIT(0),
00104   esc_Username       = PR_BIT(1),
00105   esc_Password       = PR_BIT(2),
00106   esc_Host           = PR_BIT(3),
00107   esc_Directory      = PR_BIT(4),
00108   esc_FileBaseName   = PR_BIT(5),
00109   esc_FileExtension  = PR_BIT(6),
00110   esc_FilePath       = esc_Directory | esc_FileBaseName | esc_FileExtension,
00111   esc_Param          = PR_BIT(7),
00112   esc_Query          = PR_BIT(8),
00113   esc_Ref            = PR_BIT(9),
00115   esc_Minimal        = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref, 
00116   esc_Forced         = PR_BIT(10), /* forces escaping of existing escape sequences */
00117   esc_OnlyASCII      = PR_BIT(11), /* causes non-ascii octets to be skipped */
00118   esc_OnlyNonASCII   = PR_BIT(12), /* causes _graphic_ ascii octets (0x20-0x7E) 
00119                                     * to be skipped when escaping. causes all
00120                                     * ascii octets to be skipped when unescaping */
00121   esc_AlwaysCopy     = PR_BIT(13), /* copy input to result buf even if escaping is unnecessary */
00122   esc_Colon          = PR_BIT(14), /* forces escape of colon */
00123   esc_SkipControl    = PR_BIT(15)  /* skips C0 and DEL from unescaping */
00124 };
00125 
00140 NS_COM PRBool NS_EscapeURL(const char *str,
00141                            PRInt32 len,
00142                            PRUint32 flags,
00143                            nsACString &result);
00144 
00156 NS_COM PRBool NS_UnescapeURL(const char *str,
00157                              PRInt32 len,
00158                              PRUint32 flags,
00159                              nsACString &result);
00160 
00162 inline PRInt32 NS_UnescapeURL(char *str) {
00163     return nsUnescapeCount(str);
00164 }
00165 
00169 inline const nsCSubstring &
00170 NS_EscapeURL(const nsCSubstring &str, PRUint32 flags, nsCSubstring &result) {
00171     if (NS_EscapeURL(str.Data(), str.Length(), flags, result))
00172         return result;
00173     return str;
00174 }
00175 inline const nsCSubstring &
00176 NS_UnescapeURL(const nsCSubstring &str, PRUint32 flags, nsCSubstring &result) {
00177     if (NS_UnescapeURL(str.Data(), str.Length(), flags, result))
00178         return result;
00179     return str;
00180 }
00181 
00182 // nsACString is nsCSubstring when MOZ_V1_STRING_ABI is undefined.
00183 #ifdef MOZ_V1_STRING_ABI
00184 inline const nsACString &
00185 NS_EscapeURL(const nsACString &str, PRUint32 flags, nsACString &result) {
00186     // The iterator version of BeginReading provides us with both the data
00187     // pointer and the length with only one function call.
00188     nsACString::const_iterator iter;
00189     str.BeginReading(iter);
00190     if (NS_EscapeURL(iter.get(), iter.size_forward(), flags, result))
00191         return result;
00192     return str;
00193 }
00194 inline const nsACString &
00195 NS_EscapeURL(const nsCSubstring &str, PRUint32 flags, nsACString &result) {
00196     if (NS_EscapeURL(str.Data(), str.Length(), flags, result))
00197         return result;
00198     return str;
00199 }
00200 inline const nsACString &
00201 NS_UnescapeURL(const nsACString &str, PRUint32 flags, nsACString &result) {
00202     // The iterator version of BeginReading provides us with both the data
00203     // pointer and the length with only one function call.
00204     nsACString::const_iterator iter;
00205     str.BeginReading(iter);
00206     if (NS_UnescapeURL(iter.get(), iter.size_forward(), flags, result))
00207         return result;
00208     return str;
00209 }
00210 inline const nsACString &
00211 NS_UnescapeURL(const nsCSubstring &str, PRUint32 flags, nsACString &result) {
00212     if (NS_UnescapeURL(str.Data(), str.Length(), flags, result))
00213         return result;
00214     return str;
00215 }
00216 #endif  // MOZ_V1_STRING_ABI
00217 
00222 inline PRBool
00223 NS_Escape(const nsCString& aOriginal, nsCString& aEscaped,
00224           nsEscapeMask aMask)
00225 {
00226   char* esc = nsEscape(aOriginal.get(), aMask);
00227   if (! esc)
00228     return PR_FALSE;
00229   aEscaped.Adopt(esc);
00230   return PR_TRUE;
00231 }
00232 
00236 inline nsCString &
00237 NS_UnescapeURL(nsCString &str)
00238 {
00239     str.SetLength(nsUnescapeCount(str.BeginWriting()));
00240     return str;
00241 }
00242 
00243 #endif //  _ESCAPE_H_