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"
|
2018-11-24 15:04:34 +03:00
|
|
|
#include <functional>
|
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
|
2021-08-20 21:54:03 +03:00
|
|
|
url_Path = 1u << 2, // As url_XAlphas, but don't escape slash ('/')
|
|
|
|
url_AppleExtra = 1u << 3 // Extra encoding required for Apple's
|
|
|
|
// NSURL compatibility
|
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
|
|
|
|
2017-08-21 01:58:28 +03:00
|
|
|
/**
|
|
|
|
* Decodes '%'-escaped hex codes into character values, modifies the parameter,
|
|
|
|
* returns the same buffer
|
2014-05-05 21:30:43 +04:00
|
|
|
*/
|
2017-08-21 01:58:28 +03:00
|
|
|
char* nsUnescape(char* aStr);
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2017-08-21 01:58:28 +03:00
|
|
|
/**
|
|
|
|
* Decodes '%'-escaped hex codes into character values, modifies the parameter
|
|
|
|
* buffer, returns the length of the result (result may contain \0's).
|
2014-05-05 21:30:43 +04:00
|
|
|
*/
|
2017-08-21 01:58:28 +03:00
|
|
|
int32_t nsUnescapeCount(char* aStr);
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2002-01-12 02:02:22 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-18 05:00:59 +03:00
|
|
|
/**
|
|
|
|
* Infallibly append aSrc to aDst, escaping chars that are problematic for HTML
|
|
|
|
* display.
|
|
|
|
*/
|
|
|
|
void nsAppendEscapedHTML(const nsACString& aSrc, nsACString& aDst);
|
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 */
|
2018-06-18 16:06:25 +03:00
|
|
|
esc_SkipControl = 1u << 15, /* skips C0 and DEL from unescaping */
|
|
|
|
esc_Spaces = 1u << 16 /* forces escape of spaces */
|
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
|
|
|
|
2018-10-16 15:04:12 +03:00
|
|
|
bool NS_EscapeURLSpan(mozilla::Span<const char> aStr, uint32_t aFlags,
|
|
|
|
nsACString& aResult);
|
|
|
|
|
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
|
|
|
|
2018-02-02 21:42:42 +03:00
|
|
|
/**
|
|
|
|
* Fallible version of |NS_UnescapeURL|. See above for details.
|
|
|
|
*
|
|
|
|
* @param aAppended Out param: true if aResult was written to (i.e. at least
|
|
|
|
* one character was unescaped or esc_AlwaysCopy was
|
|
|
|
* requested), false otherwise.
|
|
|
|
*/
|
|
|
|
nsresult NS_UnescapeURL(const char* aStr, int32_t aLen, uint32_t aFlags,
|
|
|
|
nsACString& aResult, bool& aAppended,
|
|
|
|
const mozilla::fallible_t&);
|
|
|
|
|
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); }
|
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) {
|
2018-10-16 15:04:12 +03:00
|
|
|
if (NS_EscapeURLSpan(aStr, aFlags, aResult)) {
|
2014-05-15 00:15:46 +04:00
|
|
|
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.
|
|
|
|
*/
|
2017-06-20 12:19:52 +03:00
|
|
|
nsresult NS_EscapeURL(const nsACString& aStr, uint32_t aFlags,
|
2016-06-27 21:10:46 +03:00
|
|
|
nsACString& aResult, const mozilla::fallible_t&);
|
|
|
|
|
2017-08-13 11:03:34 +03:00
|
|
|
// Forward declaration for nsASCIIMask.h
|
|
|
|
typedef std::array<bool, 128> ASCIIMaskArray;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The same as NS_EscapeURL, except it also filters out characters that match
|
|
|
|
* aFilterMask.
|
|
|
|
*/
|
|
|
|
nsresult NS_EscapeAndFilterURL(const nsACString& aStr, uint32_t aFlags,
|
|
|
|
const ASCIIMaskArray* aFilterMask,
|
|
|
|
nsACString& aResult, 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
|
|
|
}
|
2018-02-02 21:42:42 +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
|
2018-11-24 15:04:34 +03:00
|
|
|
* @param aFunction returns true for characters that should be escaped
|
2014-12-01 18:55:15 +03:00
|
|
|
* @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)
|
|
|
|
*/
|
2018-11-24 15:04:34 +03:00
|
|
|
const nsAString& NS_EscapeURL(const nsString& aStr,
|
|
|
|
const std::function<bool(char16_t)>& aFunction,
|
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.
|
|
|
|
*/
|
2016-05-13 23:48:03 +03:00
|
|
|
inline bool 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) {
|
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_
|