michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* First checked in on 98/12/03 by John R. McMullen, derived from net.h/mkparse.c. */ michael@0: michael@0: #ifndef _ESCAPE_H_ michael@0: #define _ESCAPE_H_ michael@0: michael@0: #include "nscore.h" michael@0: #include "nsError.h" michael@0: #include "nsString.h" michael@0: michael@0: /** michael@0: * Valid mask values for nsEscape michael@0: * Note: these values are copied in nsINetUtil.idl. Any changes should be kept michael@0: * in sync. michael@0: */ michael@0: typedef enum { michael@0: url_All = 0 /**< %-escape every byte unconditionally */ michael@0: , url_XAlphas = 1u << 0 /**< Normal escape - leave alphas intact, escape the rest */ michael@0: , url_XPAlphas = 1u << 1 /**< As url_XAlphas, but convert spaces (0x20) to '+' and plus to %2B */ michael@0: , url_Path = 1u << 2 /**< As url_XAlphas, but don't escape slash ('/') */ michael@0: } nsEscapeMask; michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: /** michael@0: * Escape the given string according to mask michael@0: * @param str The string to escape michael@0: * @param mask How to escape the string michael@0: * @return A newly allocated escaped string that must be free'd with michael@0: * nsCRT::free, or null on failure michael@0: */ michael@0: char * nsEscape(const char * str, nsEscapeMask mask); michael@0: michael@0: char * nsUnescape(char * str); michael@0: /* decode % escaped hex codes into character values, michael@0: * modifies the parameter, returns the same buffer michael@0: */ michael@0: michael@0: int32_t nsUnescapeCount (char * str); michael@0: /* decode % escaped hex codes into character values, michael@0: * modifies the parameter buffer, returns the length of the result michael@0: * (result may contain \0's). michael@0: */ michael@0: michael@0: char * michael@0: nsEscapeHTML(const char * string); michael@0: michael@0: char16_t * michael@0: nsEscapeHTML2(const char16_t *aSourceBuffer, michael@0: int32_t aSourceBufferLen = -1); michael@0: /* michael@0: * Escape problem char's for HTML display michael@0: */ michael@0: michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: michael@0: /** michael@0: * NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter: michael@0: * michael@0: * Note: These values are copied to nsINetUtil.idl michael@0: * Any changes should be kept in sync michael@0: */ michael@0: enum EscapeMask { michael@0: /** url components **/ michael@0: esc_Scheme = 1u << 0, michael@0: esc_Username = 1u << 1, michael@0: esc_Password = 1u << 2, michael@0: esc_Host = 1u << 3, michael@0: esc_Directory = 1u << 4, michael@0: esc_FileBaseName = 1u << 5, michael@0: esc_FileExtension = 1u << 6, michael@0: esc_FilePath = esc_Directory | esc_FileBaseName | esc_FileExtension, michael@0: esc_Param = 1u << 7, michael@0: esc_Query = 1u << 8, michael@0: esc_Ref = 1u << 9, michael@0: /** special flags **/ michael@0: esc_Minimal = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref, michael@0: esc_Forced = 1u << 10, /* forces escaping of existing escape sequences */ michael@0: esc_OnlyASCII = 1u << 11, /* causes non-ascii octets to be skipped */ michael@0: esc_OnlyNonASCII = 1u << 12, /* causes _graphic_ ascii octets (0x20-0x7E) michael@0: * to be skipped when escaping. causes all michael@0: * ascii octets (<= 0x7F) to be skipped when unescaping */ michael@0: esc_AlwaysCopy = 1u << 13, /* copy input to result buf even if escaping is unnecessary */ michael@0: esc_Colon = 1u << 14, /* forces escape of colon */ michael@0: esc_SkipControl = 1u << 15 /* skips C0 and DEL from unescaping */ michael@0: }; michael@0: michael@0: /** michael@0: * NS_EscapeURL michael@0: * michael@0: * Escapes invalid char's in an URL segment. Has no side-effect if the URL michael@0: * segment is already escaped. Otherwise, the escaped URL segment is appended michael@0: * to |result|. michael@0: * michael@0: * @param str url segment string michael@0: * @param len url segment string length (-1 if unknown) michael@0: * @param flags url segment type flag michael@0: * @param result result buffer, untouched if part is already escaped michael@0: * michael@0: * @return TRUE if escaping was performed, FALSE otherwise. michael@0: */ michael@0: bool NS_EscapeURL(const char *str, michael@0: int32_t len, michael@0: uint32_t flags, michael@0: nsACString &result); michael@0: michael@0: /** michael@0: * Expands URL escape sequences... beware embedded null bytes! michael@0: * michael@0: * @param str url string to unescape michael@0: * @param len length of |str| michael@0: * @param flags only esc_OnlyNonASCII, esc_SkipControl and esc_AlwaysCopy michael@0: * are recognized michael@0: * @param result result buffer, untouched if |str| is already unescaped michael@0: * michael@0: * @return TRUE if unescaping was performed, FALSE otherwise. michael@0: */ michael@0: bool NS_UnescapeURL(const char *str, michael@0: int32_t len, michael@0: uint32_t flags, michael@0: nsACString &result); michael@0: michael@0: /** returns resultant string length **/ michael@0: inline int32_t NS_UnescapeURL(char *str) { michael@0: return nsUnescapeCount(str); michael@0: } michael@0: michael@0: /** michael@0: * String friendly versions... michael@0: */ michael@0: inline const nsCSubstring & michael@0: NS_EscapeURL(const nsCSubstring &str, uint32_t flags, nsCSubstring &result) { michael@0: if (NS_EscapeURL(str.Data(), str.Length(), flags, result)) michael@0: return result; michael@0: return str; michael@0: } michael@0: inline const nsCSubstring & michael@0: NS_UnescapeURL(const nsCSubstring &str, uint32_t flags, nsCSubstring &result) { michael@0: if (NS_UnescapeURL(str.Data(), str.Length(), flags, result)) michael@0: return result; michael@0: return str; michael@0: } michael@0: michael@0: /** michael@0: * CString version of nsEscape. Returns true on success, false michael@0: * on out of memory. To reverse this function, use NS_UnescapeURL. michael@0: */ michael@0: inline bool michael@0: NS_Escape(const nsCString& aOriginal, nsCString& aEscaped, michael@0: nsEscapeMask aMask) michael@0: { michael@0: char* esc = nsEscape(aOriginal.get(), aMask); michael@0: if (! esc) michael@0: return false; michael@0: aEscaped.Adopt(esc); michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * Inline unescape of mutable string object. michael@0: */ michael@0: inline nsCString & michael@0: NS_UnescapeURL(nsCString &str) michael@0: { michael@0: str.SetLength(nsUnescapeCount(str.BeginWriting())); michael@0: return str; michael@0: } michael@0: michael@0: #endif // _ESCAPE_H_