2014-05-05 21:30:43 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
/* First checked in on 98/12/03 by John R. McMullen, derived from net.h/mkparse.c. */
|
|
|
|
|
|
|
|
#ifndef _ESCAPE_H_
|
|
|
|
#define _ESCAPE_H_
|
|
|
|
|
1999-01-28 03:30:57 +03:00
|
|
|
#include "nscore.h"
|
2001-06-22 02:02:47 +04:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsString.h"
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2004-06-01 23:55:01 +04:00
|
|
|
/**
|
|
|
|
* Valid mask values for nsEscape
|
2006-05-02 20:27:23 +04:00
|
|
|
* Note: these values are copied in nsINetUtil.idl. Any changes should be kept
|
|
|
|
* in sync.
|
2004-06-01 23:55:01 +04:00
|
|
|
*/
|
1998-12-08 05:15:50 +03:00
|
|
|
typedef enum {
|
2014-05-15 00:15:46 +04:00
|
|
|
url_All = 0, // %-escape every byte unconditionally
|
|
|
|
url_XAlphas = 1u << 0, // Normal escape - leave alphas intact, escape the rest
|
|
|
|
url_XPAlphas = 1u << 1, // As url_XAlphas, but convert spaces (0x20) to '+' and plus to %2B
|
|
|
|
url_Path = 1u << 2 // As url_XAlphas, but don't escape slash ('/')
|
1998-12-08 05:15:50 +03:00
|
|
|
} nsEscapeMask;
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2004-06-01 23:55:01 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape the given string according to mask
|
2016-05-13 23:48:03 +03:00
|
|
|
* @param aSstr The string to escape
|
|
|
|
* @param aLength The length of the string to escape
|
|
|
|
* @param aOutputLen A pointer that will be used to store the length of the
|
|
|
|
* output string, if not null
|
|
|
|
* @param aMask How to escape the string
|
|
|
|
* @return A newly allocated escaped string that must be free'd with
|
|
|
|
* nsCRT::free, or null on failure
|
2016-05-18 19:21:56 +03:00
|
|
|
* @note: Please, don't use this function. Use NS_Escape instead!
|
2016-05-13 23:48:03 +03:00
|
|
|
*/
|
2016-05-18 19:21:56 +03:00
|
|
|
char* nsEscape(const char* aStr, size_t aLength, size_t* aOutputLen,
|
|
|
|
nsEscapeMask aMask);
|
1999-01-28 03:30:57 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
char* nsUnescape(char* aStr);
|
2014-05-05 21:30:43 +04:00
|
|
|
/* decode % escaped hex codes into character values,
|
|
|
|
* modifies the parameter, returns the same buffer
|
|
|
|
*/
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
int32_t nsUnescapeCount(char* aStr);
|
2014-05-05 21:30:43 +04:00
|
|
|
/* decode % escaped hex codes into character values,
|
|
|
|
* modifies the parameter buffer, returns the length of the result
|
|
|
|
* (result may contain \0's).
|
|
|
|
*/
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
char*
|
|
|
|
nsEscapeHTML(const char* aString);
|
2000-06-06 02:01:32 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
char16_t*
|
|
|
|
nsEscapeHTML2(const char16_t* aSourceBuffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aSourceBufferLen = -1);
|
2014-05-05 21:30:43 +04:00
|
|
|
/*
|
|
|
|
* Escape problem char's for HTML display
|
|
|
|
*/
|
1999-04-13 00:23:21 +04:00
|
|
|
|
2001-06-22 02:02:47 +04:00
|
|
|
|
2002-01-12 02:02:22 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-06-22 02:02:47 +04:00
|
|
|
|
2002-01-12 02:02:22 +03:00
|
|
|
/**
|
2002-03-06 10:48:55 +03:00
|
|
|
* NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter:
|
2007-06-22 00:18:25 +04:00
|
|
|
*
|
|
|
|
* Note: These values are copied to nsINetUtil.idl
|
|
|
|
* Any changes should be kept in sync
|
2002-01-12 02:02:22 +03:00
|
|
|
*/
|
2001-06-22 02:02:47 +04:00
|
|
|
enum EscapeMask {
|
2002-03-06 10:48:55 +03:00
|
|
|
/** url components **/
|
2012-10-20 06:01:43 +04:00
|
|
|
esc_Scheme = 1u << 0,
|
|
|
|
esc_Username = 1u << 1,
|
|
|
|
esc_Password = 1u << 2,
|
|
|
|
esc_Host = 1u << 3,
|
|
|
|
esc_Directory = 1u << 4,
|
|
|
|
esc_FileBaseName = 1u << 5,
|
|
|
|
esc_FileExtension = 1u << 6,
|
2002-03-06 10:48:55 +03:00
|
|
|
esc_FilePath = esc_Directory | esc_FileBaseName | esc_FileExtension,
|
2012-10-20 06:01:43 +04:00
|
|
|
esc_Param = 1u << 7,
|
|
|
|
esc_Query = 1u << 8,
|
|
|
|
esc_Ref = 1u << 9,
|
2002-03-06 10:48:55 +03:00
|
|
|
/** special flags **/
|
2014-05-05 21:30:43 +04:00
|
|
|
esc_Minimal = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref,
|
2012-10-20 06:01:43 +04:00
|
|
|
esc_Forced = 1u << 10, /* forces escaping of existing escape sequences */
|
|
|
|
esc_OnlyASCII = 1u << 11, /* causes non-ascii octets to be skipped */
|
2014-05-05 21:30:43 +04:00
|
|
|
esc_OnlyNonASCII = 1u << 12, /* causes _graphic_ ascii octets (0x20-0x7E)
|
2004-01-07 06:57:20 +03:00
|
|
|
* to be skipped when escaping. causes all
|
2007-09-01 00:19:37 +04:00
|
|
|
* ascii octets (<= 0x7F) to be skipped when unescaping */
|
2012-10-20 06:01:43 +04:00
|
|
|
esc_AlwaysCopy = 1u << 13, /* copy input to result buf even if escaping is unnecessary */
|
|
|
|
esc_Colon = 1u << 14, /* forces escape of colon */
|
|
|
|
esc_SkipControl = 1u << 15 /* skips C0 and DEL from unescaping */
|
2001-06-22 02:02:47 +04:00
|
|
|
};
|
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
/**
|
2002-03-06 10:48:55 +03:00
|
|
|
* NS_EscapeURL
|
2002-01-12 02:02:22 +03:00
|
|
|
*
|
|
|
|
* Escapes invalid char's in an URL segment. Has no side-effect if the URL
|
2014-11-08 05:42:04 +03:00
|
|
|
* segment is already escaped, unless aFlags has the esc_Forced bit in which
|
|
|
|
* case % will also be escaped. Iff some part of aStr is escaped is the
|
|
|
|
* final result appended to aResult. You can also request that aStr is
|
|
|
|
* always appended to aResult with esc_AlwaysCopy.
|
2001-11-16 05:09:13 +03:00
|
|
|
*
|
2014-11-08 05:42:04 +03:00
|
|
|
* @param aStr url segment string
|
|
|
|
* @param aLen url segment string length (-1 if unknown)
|
|
|
|
* @param aFlags url segment type flag (see EscapeMask above)
|
|
|
|
* @param aResult result buffer, untouched if aStr is already escaped unless
|
|
|
|
* aFlags has esc_AlwaysCopy
|
2001-11-16 05:09:13 +03:00
|
|
|
*
|
2014-11-08 05:42:04 +03:00
|
|
|
* @return true if aResult was written to (i.e. at least one character was
|
|
|
|
* escaped or esc_AlwaysCopy was requested), false otherwise.
|
2001-11-16 05:09:13 +03:00
|
|
|
*/
|
2014-05-15 00:15:46 +04:00
|
|
|
bool NS_EscapeURL(const char* aStr,
|
|
|
|
int32_t aLen,
|
|
|
|
uint32_t aFlags,
|
|
|
|
nsACString& aResult);
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2002-01-12 02:02:22 +03:00
|
|
|
/**
|
2002-03-06 10:48:55 +03:00
|
|
|
* Expands URL escape sequences... beware embedded null bytes!
|
|
|
|
*
|
2014-11-08 05:42:04 +03:00
|
|
|
* @param aStr url string to unescape
|
|
|
|
* @param aLen length of aStr
|
|
|
|
* @param aFlags only esc_OnlyNonASCII, esc_SkipControl and esc_AlwaysCopy
|
2004-01-07 06:57:20 +03:00
|
|
|
* are recognized
|
2014-11-08 05:42:04 +03:00
|
|
|
* @param aResult result buffer, untouched if aStr is already unescaped unless
|
|
|
|
* aFlags has esc_AlwaysCopy
|
2002-03-06 10:48:55 +03:00
|
|
|
*
|
2014-11-08 05:42:04 +03:00
|
|
|
* @return true if aResult was written to (i.e. at least one character was
|
|
|
|
* unescaped or esc_AlwaysCopy was requested), false otherwise.
|
2002-01-12 02:02:22 +03:00
|
|
|
*/
|
2014-05-15 00:15:46 +04:00
|
|
|
bool NS_UnescapeURL(const char* aStr,
|
|
|
|
int32_t aLen,
|
|
|
|
uint32_t aFlags,
|
|
|
|
nsACString& aResult);
|
2002-03-06 10:48:55 +03:00
|
|
|
|
|
|
|
/** returns resultant string length **/
|
2014-05-15 00:15:46 +04:00
|
|
|
inline int32_t
|
|
|
|
NS_UnescapeURL(char* aStr)
|
|
|
|
{
|
|
|
|
return nsUnescapeCount(aStr);
|
2005-11-16 00:41:57 +03:00
|
|
|
}
|
2002-03-06 10:48:55 +03:00
|
|
|
|
|
|
|
/**
|
2005-11-16 00:41:57 +03:00
|
|
|
* String friendly versions...
|
2002-03-06 10:48:55 +03:00
|
|
|
*/
|
2017-06-20 12:19:52 +03:00
|
|
|
inline const nsACString&
|
|
|
|
NS_EscapeURL(const nsACString& aStr, uint32_t aFlags, nsACString& aResult)
|
2014-05-15 00:15:46 +04:00
|
|
|
{
|
|
|
|
if (NS_EscapeURL(aStr.Data(), aStr.Length(), aFlags, aResult)) {
|
|
|
|
return aResult;
|
|
|
|
}
|
|
|
|
return aStr;
|
2005-11-16 00:41:57 +03:00
|
|
|
}
|
2016-06-27 21:10:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fallible version of NS_EscapeURL. On success aResult will point to either
|
|
|
|
* the original string or an escaped copy.
|
|
|
|
*/
|
|
|
|
nsresult
|
2017-06-20 12:19:52 +03:00
|
|
|
NS_EscapeURL(const nsACString& aStr, uint32_t aFlags, nsACString& aResult,
|
2016-06-27 21:10:46 +03:00
|
|
|
const mozilla::fallible_t&);
|
|
|
|
|
2017-06-20 12:19:52 +03:00
|
|
|
inline const nsACString&
|
|
|
|
NS_UnescapeURL(const nsACString& aStr, uint32_t aFlags, nsACString& aResult)
|
2014-05-15 00:15:46 +04:00
|
|
|
{
|
|
|
|
if (NS_UnescapeURL(aStr.Data(), aStr.Length(), aFlags, aResult)) {
|
|
|
|
return aResult;
|
|
|
|
}
|
|
|
|
return aStr;
|
2005-11-16 00:41:57 +03:00
|
|
|
}
|
2017-06-20 12:19:52 +03:00
|
|
|
const nsAString&
|
|
|
|
NS_EscapeURL(const nsAString& aStr, uint32_t aFlags, nsAString& aResult);
|
2005-11-16 00:41:57 +03:00
|
|
|
|
2014-12-01 18:55:15 +03:00
|
|
|
/**
|
|
|
|
* Percent-escapes all characters in aStr that occurs in aForbidden.
|
|
|
|
* @param aStr the input URL string
|
|
|
|
* @param aForbidden the characters that should be escaped if found in aStr
|
|
|
|
* @note that aForbidden MUST be sorted (low to high)
|
|
|
|
* @param aResult the result if some characters were escaped
|
|
|
|
* @return aResult if some characters were escaped, or aStr otherwise (aResult
|
|
|
|
* is unmodified in that case)
|
|
|
|
*/
|
2017-06-20 12:19:52 +03:00
|
|
|
const nsAString&
|
2017-06-20 12:19:05 +03:00
|
|
|
NS_EscapeURL(const nsString& aStr, const nsTArray<char16_t>& aForbidden,
|
2017-06-20 12:19:52 +03:00
|
|
|
nsAString& aResult);
|
2014-12-01 18:55:15 +03:00
|
|
|
|
2005-11-17 22:28:05 +03:00
|
|
|
/**
|
|
|
|
* CString version of nsEscape. Returns true on success, false
|
|
|
|
* on out of memory. To reverse this function, use NS_UnescapeURL.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
inline bool
|
2016-05-13 23:48:03 +03:00
|
|
|
NS_Escape(const nsACString& aOriginal, nsACString& aEscaped,
|
2005-11-17 22:28:05 +03:00
|
|
|
nsEscapeMask aMask)
|
|
|
|
{
|
2016-05-13 23:48:03 +03:00
|
|
|
size_t escLen = 0;
|
2016-05-18 19:21:56 +03:00
|
|
|
char* esc = nsEscape(aOriginal.BeginReading(), aOriginal.Length(), &escLen,
|
|
|
|
aMask);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (! esc) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2016-05-13 23:48:03 +03:00
|
|
|
aEscaped.Adopt(esc, escLen);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-11-17 22:28:05 +03:00
|
|
|
}
|
|
|
|
|
2005-11-16 00:41:57 +03:00
|
|
|
/**
|
|
|
|
* Inline unescape of mutable string object.
|
|
|
|
*/
|
2016-05-13 23:48:03 +03:00
|
|
|
inline nsACString&
|
|
|
|
NS_UnescapeURL(nsACString& aStr)
|
2002-03-06 10:48:55 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
aStr.SetLength(nsUnescapeCount(aStr.BeginWriting()));
|
|
|
|
return aStr;
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
#endif // _ESCAPE_H_
|