Back to index

lightning-sunbird  0.9+nobinonly
Enumerations | Functions
nsEscape.h File Reference
#include "prtypes.h"
#include "nscore.h"
#include "nsError.h"
#include "nsString.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  nsEscapeMask {
  url_XAlphas = (1<<0), url_XPAlphas = (1<<1), url_Path = (1<<2), url_All = 0,
  url_XAlphas = PR_BIT(0), url_XPAlphas = PR_BIT(1), url_Path = PR_BIT(2), url_XAlphas = (1<<0),
  url_XPAlphas = (1<<1), url_Path = (1<<2), url_XAlphas = (1<<0), url_XPAlphas = (1<<1),
  url_Path = (1<<2)
}
 Valid mask values for nsEscape. More...
enum  EscapeMask {
  esc_Scheme = PR_BIT(0), esc_Username = PR_BIT(1), esc_Password = PR_BIT(2), esc_Host = PR_BIT(3),
  esc_Directory = PR_BIT(4), esc_FileBaseName = PR_BIT(5), esc_FileExtension = PR_BIT(6), esc_FilePath = esc_Directory | esc_FileBaseName | esc_FileExtension,
  esc_Param = PR_BIT(7), esc_Query = PR_BIT(8), esc_Ref = PR_BIT(9), esc_Minimal = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref,
  esc_Forced = PR_BIT(10), esc_OnlyASCII = PR_BIT(11), esc_OnlyNonASCII = PR_BIT(12), esc_AlwaysCopy = PR_BIT(13),
  esc_Colon = PR_BIT(14), esc_SkipControl = PR_BIT(15)
}
 NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter: More...

Functions

NS_COM char * nsEscape (const char *str, nsEscapeMask mask)
 Escape the given string according to mask.
NS_COM char * nsUnescape (char *str)
NS_COM PRInt32 nsUnescapeCount (char *str)
NS_COM char * nsEscapeHTML (const char *string)
NS_COM PRUnicharnsEscapeHTML2 (const PRUnichar *aSourceBuffer, PRInt32 aSourceBufferLen=-1)
NS_COM PRBool NS_EscapeURL (const char *str, PRInt32 len, PRUint32 flags, nsACString &result)
 NS_EscapeURL.
NS_COM PRBool NS_UnescapeURL (const char *str, PRInt32 len, PRUint32 flags, nsACString &result)
 Expands URL escape sequences...
PRInt32 NS_UnescapeURL (char *str)
 returns resultant string length
const nsCSubstringNS_EscapeURL (const nsCSubstring &str, PRUint32 flags, nsCSubstring &result)
 String friendly versions...
const nsCSubstringNS_UnescapeURL (const nsCSubstring &str, PRUint32 flags, nsCSubstring &result)
PRBool NS_Escape (const nsCString &aOriginal, nsCString &aEscaped, nsEscapeMask aMask)
 CString version of nsEscape.
nsCStringNS_UnescapeURL (nsCString &str)
 Inline unescape of mutable string object.

Enumeration Type Documentation

enum EscapeMask

NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter:

Enumerator:
esc_Scheme 

url components

esc_Username 
esc_Password 
esc_Host 
esc_Directory 
esc_FileBaseName 
esc_FileExtension 
esc_FilePath 
esc_Param 
esc_Query 
esc_Ref 
esc_Minimal 

special flags

esc_Forced 
esc_OnlyASCII 
esc_OnlyNonASCII 
esc_AlwaysCopy 
esc_Colon 
esc_SkipControl 

Definition at line 101 of file nsEscape.h.

                {
  esc_Scheme         = PR_BIT(0),
  esc_Username       = PR_BIT(1),
  esc_Password       = PR_BIT(2),
  esc_Host           = PR_BIT(3),
  esc_Directory      = PR_BIT(4),
  esc_FileBaseName   = PR_BIT(5),
  esc_FileExtension  = PR_BIT(6),
  esc_FilePath       = esc_Directory | esc_FileBaseName | esc_FileExtension,
  esc_Param          = PR_BIT(7),
  esc_Query          = PR_BIT(8),
  esc_Ref            = PR_BIT(9),
  esc_Minimal        = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref, 
  esc_Forced         = PR_BIT(10), /* forces escaping of existing escape sequences */
  esc_OnlyASCII      = PR_BIT(11), /* causes non-ascii octets to be skipped */
  esc_OnlyNonASCII   = PR_BIT(12), /* causes _graphic_ ascii octets (0x20-0x7E) 
                                    * to be skipped when escaping. causes all
                                    * ascii octets to be skipped when unescaping */
  esc_AlwaysCopy     = PR_BIT(13), /* copy input to result buf even if escaping is unnecessary */
  esc_Colon          = PR_BIT(14), /* forces escape of colon */
  esc_SkipControl    = PR_BIT(15)  /* skips C0 and DEL from unescaping */
};

Valid mask values for nsEscape.

Enumerator:
url_XAlphas 
url_XPAlphas 
url_Path 
url_All 

%-escape every byte uncondtionally

url_XAlphas 

Normal escape - leave alphas intact, escape the rest.

url_XPAlphas 

As url_XAlphas, but convert spaces (0x20) to '+' and plus to %2B.

url_Path 

As url_XAlphas, but don't escape slash ('/')

url_XAlphas 
url_XPAlphas 
url_Path 
url_XAlphas 
url_XPAlphas 
url_Path 

Definition at line 51 of file nsEscape.h.


Function Documentation

PRBool NS_Escape ( const nsCString aOriginal,
nsCString aEscaped,
nsEscapeMask  aMask 
) [inline]

CString version of nsEscape.

Returns true on success, false on out of memory. To reverse this function, use NS_UnescapeURL.

Definition at line 223 of file nsEscape.h.

{
  char* esc = nsEscape(aOriginal.get(), aMask);
  if (! esc)
    return PR_FALSE;
  aEscaped.Adopt(esc);
  return PR_TRUE;
}

Here is the call graph for this function:

NS_COM PRBool NS_EscapeURL ( const char *  str,
PRInt32  len,
PRUint32  flags,
nsACString &  result 
)

NS_EscapeURL.

Escapes invalid char's in an URL segment. Has no side-effect if the URL segment is already escaped. Otherwise, the escaped URL segment is appended to |result|.

Parameters:
strurl segment string
lenurl segment string length (-1 if unknown)
flagsurl segment type flag
resultresult buffer, untouched if part is already escaped
Returns:
TRUE if escaping was performed, FALSE otherwise.

Definition at line 385 of file nsEscape.cpp.

{
    if (!part) {
        NS_NOTREACHED("null pointer");
        return PR_FALSE;
    }

    int i = 0;
    static const char hexChars[] = "0123456789ABCDEF";
    if (partLen < 0)
        partLen = strlen(part);
    PRBool forced = (flags & esc_Forced);
    PRBool ignoreNonAscii = (flags & esc_OnlyASCII);
    PRBool ignoreAscii = (flags & esc_OnlyNonASCII);
    PRBool writing = (flags & esc_AlwaysCopy);
    PRBool colon = (flags & esc_Colon);

    register const unsigned char* src = (const unsigned char *) part;

    char tempBuffer[100];
    unsigned int tempBufferPos = 0;

    PRBool previousIsNonASCII = PR_FALSE;
    for (i = 0; i < partLen; i++)
    {
      unsigned char c = *src++;

      // if the char has not to be escaped or whatever follows % is 
      // a valid escaped string, just copy the char.
      //
      // Also the % will not be escaped until forced
      // See bugzilla bug 61269 for details why we changed this
      //
      // And, we will not escape non-ascii characters if requested.
      // On special request we will also escape the colon even when
      // not covered by the matrix.
      // ignoreAscii is not honored for control characters (C0 and DEL)
      //
      // And, we should escape the '|' character when it occurs after any
      // non-ASCII character as it may be part of a multi-byte character.
      if ((NO_NEED_ESC(c) || (c == HEX_ESCAPE && !forced)
                          || (c > 0x7f && ignoreNonAscii)
                          || (c > 0x1f && c < 0x7f && ignoreAscii))
          && !(c == ':' && colon)
          && !(previousIsNonASCII && c == '|' && !ignoreNonAscii))
      {
        if (writing)
          tempBuffer[tempBufferPos++] = c;
      }
      else /* do the escape magic */
      {
        if (!writing)
        {
          result.Append(part, i);
          writing = PR_TRUE;
        }
        tempBuffer[tempBufferPos++] = HEX_ESCAPE;
        tempBuffer[tempBufferPos++] = hexChars[c >> 4]; /* high nibble */
        tempBuffer[tempBufferPos++] = hexChars[c & 0x0f]; /* low nibble */
      }

      if (tempBufferPos >= sizeof(tempBuffer) - 4)
      {
        NS_ASSERTION(writing, "should be writing");
        tempBuffer[tempBufferPos] = '\0';
        result += tempBuffer;
        tempBufferPos = 0;
      }

      previousIsNonASCII = (c > 0x7f);
    }
    if (writing) {
      tempBuffer[tempBufferPos] = '\0';
      result += tempBuffer;
    }
    return writing;
}

Here is the caller graph for this function:

const nsCSubstring& NS_EscapeURL ( const nsCSubstring str,
PRUint32  flags,
nsCSubstring result 
) [inline]

String friendly versions...

Definition at line 170 of file nsEscape.h.

                                                                            {
    if (NS_EscapeURL(str.Data(), str.Length(), flags, result))
        return result;
    return str;
}

Here is the call graph for this function:

NS_COM PRBool NS_UnescapeURL ( const char *  str,
PRInt32  len,
PRUint32  flags,
nsACString &  result 
)

Expands URL escape sequences...

beware embedded null bytes!

Parameters:
strurl string to unescape
lenlength of |str|
flagsonly esc_OnlyNonASCII, esc_SkipControl and esc_AlwaysCopy are recognized
resultresult buffer, untouched if |str| is already unescaped
Returns:
TRUE if unescaping was performed, FALSE otherwise.

Definition at line 468 of file nsEscape.cpp.

{
    if (!str) {
        NS_NOTREACHED("null pointer");
        return PR_FALSE;
    }

    if (len < 0)
        len = strlen(str);

    PRBool ignoreNonAscii = (flags & esc_OnlyASCII);
    PRBool ignoreAscii = (flags & esc_OnlyNonASCII);
    PRBool writing = (flags & esc_AlwaysCopy);
    PRBool skipControl = (flags & esc_SkipControl); 

    static const char hexChars[] = "0123456789ABCDEFabcdef";

    const char *last = str;
    const char *p = str;

    for (int i=0; i<len; ++i, ++p) {
        //printf("%c [i=%d of len=%d]\n", *p, i, len);
        if (*p == HEX_ESCAPE && i < len-2) {
            unsigned char *p1 = ((unsigned char *) p) + 1;
            unsigned char *p2 = ((unsigned char *) p) + 2;
            if (ISHEX(*p1) && ISHEX(*p2) && 
                ((*p1 < '8' && !ignoreAscii) || (*p1 >= '8' && !ignoreNonAscii)) &&
                !(skipControl && 
                  (*p1 < '2' || (*p1 == '7' && (*p2 == 'f' || *p2 == 'F'))))) {
                //printf("- p1=%c p2=%c\n", *p1, *p2);
                writing = PR_TRUE;
                if (p > last) {
                    //printf("- p=%p, last=%p\n", p, last);
                    result.Append(last, p - last);
                    last = p;
                }
                char u = (UNHEX(*p1) << 4) + UNHEX(*p2);
                //printf("- u=%c\n", u);
                result.Append(u);
                i += 2;
                p += 2;
                last += 3;
            }
        }
    }
    if (writing && last < str + len)
        result.Append(last, str + len - last);

    return writing;
}

Here is the caller graph for this function:

PRInt32 NS_UnescapeURL ( char *  str) [inline]

returns resultant string length

Definition at line 162 of file nsEscape.h.

                                         {
    return nsUnescapeCount(str);
}

Here is the call graph for this function:

const nsCSubstring& NS_UnescapeURL ( const nsCSubstring str,
PRUint32  flags,
nsCSubstring result 
) [inline]

Definition at line 176 of file nsEscape.h.

                                                                              {
    if (NS_UnescapeURL(str.Data(), str.Length(), flags, result))
        return result;
    return str;
}

Here is the call graph for this function:

nsCString& NS_UnescapeURL ( nsCString str) [inline]

Inline unescape of mutable string object.

Definition at line 237 of file nsEscape.h.

{
    str.SetLength(nsUnescapeCount(str.BeginWriting()));
    return str;
}

Here is the call graph for this function:

NS_COM char* nsEscape ( const char *  str,
nsEscapeMask  mask 
)

Escape the given string according to mask.

Parameters:
strThe string to escape
maskHow to escape the string
Returns:
A newly allocated escaped string that must be free'd with nsCRT::free, or null on failure

Definition at line 89 of file nsEscape.cpp.

{
    if(!str)
        return NULL;
    return nsEscapeCount(str, lstrlen(str), mask, NULL);
}
NS_COM char* nsEscapeHTML ( const char *  string)

Definition at line 223 of file nsEscape.cpp.

{
       /* XXX Hardcoded max entity len. The +1 is for the trailing null. */
       char *rv = (char *) nsMemory::Alloc(strlen(string) * 6 + 1);
       char *ptr = rv;

       if(rv)
         {
              for(; *string != '\0'; string++)
                {
                     if(*string == '<')
                       {
                            *ptr++ = '&';
                            *ptr++ = 'l';
                            *ptr++ = 't';
                            *ptr++ = ';';
                       }
                     else if(*string == '>')
                       {
                            *ptr++ = '&';
                            *ptr++ = 'g';
                            *ptr++ = 't';
                            *ptr++ = ';';
                       }
                     else if(*string == '&')
                       {
                            *ptr++ = '&';
                            *ptr++ = 'a';
                            *ptr++ = 'm';
                            *ptr++ = 'p';
                            *ptr++ = ';';
                       }
                     else if (*string == '"')
                       {
                            *ptr++ = '&';
                            *ptr++ = 'q';
                            *ptr++ = 'u';
                            *ptr++ = 'o';
                            *ptr++ = 't';
                            *ptr++ = ';';
                       }                  
                     else if (*string == '\'')
                       {
                            *ptr++ = '&';
                            *ptr++ = '#';
                            *ptr++ = '3';
                            *ptr++ = '9';
                            *ptr++ = ';';
                       }
                     else
                       {
                            *ptr++ = *string;
                       }
                }
              *ptr = '\0';
         }

       return(rv);
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM PRUnichar* nsEscapeHTML2 ( const PRUnichar aSourceBuffer,
PRInt32  aSourceBufferLen = -1 
)

Definition at line 284 of file nsEscape.cpp.

{
  // if the caller didn't calculate the length
  if (aSourceBufferLen == -1) {
    aSourceBufferLen = nsCRT::strlen(aSourceBuffer); // ...then I will
  }

  /* XXX Hardcoded max entity len. */
  PRUnichar *resultBuffer = (PRUnichar *)nsMemory::Alloc(aSourceBufferLen *
                            6 * sizeof(PRUnichar) + sizeof(PRUnichar('\0')));
  PRUnichar *ptr = resultBuffer;

  if (resultBuffer) {
    PRInt32 i;

    for(i = 0; i < aSourceBufferLen; i++) {
      if(aSourceBuffer[i] == '<') {
        *ptr++ = '&';
        *ptr++ = 'l';
        *ptr++ = 't';
        *ptr++ = ';';
      } else if(aSourceBuffer[i] == '>') {
        *ptr++ = '&';
        *ptr++ = 'g';
        *ptr++ = 't';
        *ptr++ = ';';
      } else if(aSourceBuffer[i] == '&') {
        *ptr++ = '&';
        *ptr++ = 'a';
        *ptr++ = 'm';
        *ptr++ = 'p';
        *ptr++ = ';';
      } else if (aSourceBuffer[i] == '"') {
        *ptr++ = '&';
        *ptr++ = 'q';
        *ptr++ = 'u';
        *ptr++ = 'o';
        *ptr++ = 't';
        *ptr++ = ';';
      } else if (aSourceBuffer[i] == '\'') {
        *ptr++ = '&';
        *ptr++ = '#';
        *ptr++ = '3';
        *ptr++ = '9';
        *ptr++ = ';';
      } else {
        *ptr++ = aSourceBuffer[i];
      }
    }
    *ptr = 0;
  }

  return resultBuffer;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM char* nsUnescape ( char *  str)

Definition at line 164 of file nsEscape.cpp.

{
       nsUnescapeCount(str);
       return str;
}
NS_COM PRInt32 nsUnescapeCount ( char *  str)

Definition at line 172 of file nsEscape.cpp.

{
    register char *src = str;
    register char *dst = str;

    while (*src)
        if (*src != HEX_ESCAPE)
              *dst++ = *src++;
        else  
              {
              src++; /* walk over escape */
              if (*src)
            {
              *dst = UNHEX(*src) << 4;
              src++;
            }
              if (*src)
            {
              *dst = (*dst + UNHEX(*src));
              src++;
            }
              dst++;
        }

    *dst = 0;
    return (int)(dst - str);

} /* NET_UnEscapeCnt */