xpcom/io/nsEscape.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /* First checked in on 98/12/03 by John R. McMullen, derived from net.h/mkparse.c. */
michael@0 7
michael@0 8 #ifndef _ESCAPE_H_
michael@0 9 #define _ESCAPE_H_
michael@0 10
michael@0 11 #include "nscore.h"
michael@0 12 #include "nsError.h"
michael@0 13 #include "nsString.h"
michael@0 14
michael@0 15 /**
michael@0 16 * Valid mask values for nsEscape
michael@0 17 * Note: these values are copied in nsINetUtil.idl. Any changes should be kept
michael@0 18 * in sync.
michael@0 19 */
michael@0 20 typedef enum {
michael@0 21 url_All = 0 /**< %-escape every byte unconditionally */
michael@0 22 , url_XAlphas = 1u << 0 /**< Normal escape - leave alphas intact, escape the rest */
michael@0 23 , url_XPAlphas = 1u << 1 /**< As url_XAlphas, but convert spaces (0x20) to '+' and plus to %2B */
michael@0 24 , url_Path = 1u << 2 /**< As url_XAlphas, but don't escape slash ('/') */
michael@0 25 } nsEscapeMask;
michael@0 26
michael@0 27 #ifdef __cplusplus
michael@0 28 extern "C" {
michael@0 29 #endif
michael@0 30
michael@0 31 /**
michael@0 32 * Escape the given string according to mask
michael@0 33 * @param str The string to escape
michael@0 34 * @param mask How to escape the string
michael@0 35 * @return A newly allocated escaped string that must be free'd with
michael@0 36 * nsCRT::free, or null on failure
michael@0 37 */
michael@0 38 char * nsEscape(const char * str, nsEscapeMask mask);
michael@0 39
michael@0 40 char * nsUnescape(char * str);
michael@0 41 /* decode % escaped hex codes into character values,
michael@0 42 * modifies the parameter, returns the same buffer
michael@0 43 */
michael@0 44
michael@0 45 int32_t nsUnescapeCount (char * str);
michael@0 46 /* decode % escaped hex codes into character values,
michael@0 47 * modifies the parameter buffer, returns the length of the result
michael@0 48 * (result may contain \0's).
michael@0 49 */
michael@0 50
michael@0 51 char *
michael@0 52 nsEscapeHTML(const char * string);
michael@0 53
michael@0 54 char16_t *
michael@0 55 nsEscapeHTML2(const char16_t *aSourceBuffer,
michael@0 56 int32_t aSourceBufferLen = -1);
michael@0 57 /*
michael@0 58 * Escape problem char's for HTML display
michael@0 59 */
michael@0 60
michael@0 61
michael@0 62 #ifdef __cplusplus
michael@0 63 }
michael@0 64 #endif
michael@0 65
michael@0 66
michael@0 67 /**
michael@0 68 * NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter:
michael@0 69 *
michael@0 70 * Note: These values are copied to nsINetUtil.idl
michael@0 71 * Any changes should be kept in sync
michael@0 72 */
michael@0 73 enum EscapeMask {
michael@0 74 /** url components **/
michael@0 75 esc_Scheme = 1u << 0,
michael@0 76 esc_Username = 1u << 1,
michael@0 77 esc_Password = 1u << 2,
michael@0 78 esc_Host = 1u << 3,
michael@0 79 esc_Directory = 1u << 4,
michael@0 80 esc_FileBaseName = 1u << 5,
michael@0 81 esc_FileExtension = 1u << 6,
michael@0 82 esc_FilePath = esc_Directory | esc_FileBaseName | esc_FileExtension,
michael@0 83 esc_Param = 1u << 7,
michael@0 84 esc_Query = 1u << 8,
michael@0 85 esc_Ref = 1u << 9,
michael@0 86 /** special flags **/
michael@0 87 esc_Minimal = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref,
michael@0 88 esc_Forced = 1u << 10, /* forces escaping of existing escape sequences */
michael@0 89 esc_OnlyASCII = 1u << 11, /* causes non-ascii octets to be skipped */
michael@0 90 esc_OnlyNonASCII = 1u << 12, /* causes _graphic_ ascii octets (0x20-0x7E)
michael@0 91 * to be skipped when escaping. causes all
michael@0 92 * ascii octets (<= 0x7F) to be skipped when unescaping */
michael@0 93 esc_AlwaysCopy = 1u << 13, /* copy input to result buf even if escaping is unnecessary */
michael@0 94 esc_Colon = 1u << 14, /* forces escape of colon */
michael@0 95 esc_SkipControl = 1u << 15 /* skips C0 and DEL from unescaping */
michael@0 96 };
michael@0 97
michael@0 98 /**
michael@0 99 * NS_EscapeURL
michael@0 100 *
michael@0 101 * Escapes invalid char's in an URL segment. Has no side-effect if the URL
michael@0 102 * segment is already escaped. Otherwise, the escaped URL segment is appended
michael@0 103 * to |result|.
michael@0 104 *
michael@0 105 * @param str url segment string
michael@0 106 * @param len url segment string length (-1 if unknown)
michael@0 107 * @param flags url segment type flag
michael@0 108 * @param result result buffer, untouched if part is already escaped
michael@0 109 *
michael@0 110 * @return TRUE if escaping was performed, FALSE otherwise.
michael@0 111 */
michael@0 112 bool NS_EscapeURL(const char *str,
michael@0 113 int32_t len,
michael@0 114 uint32_t flags,
michael@0 115 nsACString &result);
michael@0 116
michael@0 117 /**
michael@0 118 * Expands URL escape sequences... beware embedded null bytes!
michael@0 119 *
michael@0 120 * @param str url string to unescape
michael@0 121 * @param len length of |str|
michael@0 122 * @param flags only esc_OnlyNonASCII, esc_SkipControl and esc_AlwaysCopy
michael@0 123 * are recognized
michael@0 124 * @param result result buffer, untouched if |str| is already unescaped
michael@0 125 *
michael@0 126 * @return TRUE if unescaping was performed, FALSE otherwise.
michael@0 127 */
michael@0 128 bool NS_UnescapeURL(const char *str,
michael@0 129 int32_t len,
michael@0 130 uint32_t flags,
michael@0 131 nsACString &result);
michael@0 132
michael@0 133 /** returns resultant string length **/
michael@0 134 inline int32_t NS_UnescapeURL(char *str) {
michael@0 135 return nsUnescapeCount(str);
michael@0 136 }
michael@0 137
michael@0 138 /**
michael@0 139 * String friendly versions...
michael@0 140 */
michael@0 141 inline const nsCSubstring &
michael@0 142 NS_EscapeURL(const nsCSubstring &str, uint32_t flags, nsCSubstring &result) {
michael@0 143 if (NS_EscapeURL(str.Data(), str.Length(), flags, result))
michael@0 144 return result;
michael@0 145 return str;
michael@0 146 }
michael@0 147 inline const nsCSubstring &
michael@0 148 NS_UnescapeURL(const nsCSubstring &str, uint32_t flags, nsCSubstring &result) {
michael@0 149 if (NS_UnescapeURL(str.Data(), str.Length(), flags, result))
michael@0 150 return result;
michael@0 151 return str;
michael@0 152 }
michael@0 153
michael@0 154 /**
michael@0 155 * CString version of nsEscape. Returns true on success, false
michael@0 156 * on out of memory. To reverse this function, use NS_UnescapeURL.
michael@0 157 */
michael@0 158 inline bool
michael@0 159 NS_Escape(const nsCString& aOriginal, nsCString& aEscaped,
michael@0 160 nsEscapeMask aMask)
michael@0 161 {
michael@0 162 char* esc = nsEscape(aOriginal.get(), aMask);
michael@0 163 if (! esc)
michael@0 164 return false;
michael@0 165 aEscaped.Adopt(esc);
michael@0 166 return true;
michael@0 167 }
michael@0 168
michael@0 169 /**
michael@0 170 * Inline unescape of mutable string object.
michael@0 171 */
michael@0 172 inline nsCString &
michael@0 173 NS_UnescapeURL(nsCString &str)
michael@0 174 {
michael@0 175 str.SetLength(nsUnescapeCount(str.BeginWriting()));
michael@0 176 return str;
michael@0 177 }
michael@0 178
michael@0 179 #endif // _ESCAPE_H_

mercurial