2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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.
|
|
|
|
|
|
|
|
#include "nsEscape.h"
|
2000-06-03 13:46:12 +04:00
|
|
|
#include "nsMemory.h"
|
1999-09-04 20:33:12 +04:00
|
|
|
#include "nsCRT.h"
|
2002-03-06 10:48:55 +03:00
|
|
|
#include "nsReadableUtils.h"
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
const int netCharType[256] =
|
|
|
|
/* Bit 0 xalpha -- the alphas
|
|
|
|
** Bit 1 xpalpha -- as xalpha but
|
2004-06-01 23:55:01 +04:00
|
|
|
** converts spaces to plus and plus to %2B
|
1998-12-08 05:15:50 +03:00
|
|
|
** Bit 3 ... path -- as xalphas but doesn't escape '/'
|
|
|
|
*/
|
|
|
|
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
|
|
|
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x */
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 1x */
|
|
|
|
0,0,0,0,0,0,0,0,0,0,7,4,0,7,7,4, /* 2x !"#$%&'()*+,-./ */
|
|
|
|
7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */
|
|
|
|
0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */
|
|
|
|
/* bits for '@' changed from 7 to 0 so '@' can be escaped */
|
|
|
|
/* in usernames and passwords in publishing. */
|
|
|
|
7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */
|
|
|
|
0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */
|
|
|
|
7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0, /* 7X pqrstuvwxyz{\}~ DEL */
|
|
|
|
0, };
|
|
|
|
|
|
|
|
/* decode % escaped hex codes into character values
|
|
|
|
*/
|
|
|
|
#define UNHEX(C) \
|
|
|
|
((C >= '0' && C <= '9') ? C - '0' : \
|
|
|
|
((C >= 'A' && C <= 'F') ? C - 'A' + 10 : \
|
|
|
|
((C >= 'a' && C <= 'f') ? C - 'a' + 10 : 0)))
|
|
|
|
|
|
|
|
|
2005-06-01 22:34:53 +04:00
|
|
|
#define IS_OK(C) (netCharType[((unsigned int) (C))] & (flags))
|
1998-12-08 05:15:50 +03:00
|
|
|
#define HEX_ESCAPE '%'
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
2003-03-15 04:04:32 +03:00
|
|
|
static char* nsEscapeCount(
|
1999-03-16 22:12:51 +03:00
|
|
|
const char * str,
|
2005-06-01 22:34:53 +04:00
|
|
|
nsEscapeMask flags,
|
2008-08-19 17:57:12 +04:00
|
|
|
size_t* out_len)
|
1998-12-08 05:15:50 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
1999-03-16 22:12:51 +03:00
|
|
|
if (!str)
|
|
|
|
return 0;
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2008-08-19 17:57:12 +04:00
|
|
|
size_t i, len = 0, charsToEscape = 0;
|
2001-11-07 09:24:10 +03:00
|
|
|
static const char hexChars[] = "0123456789ABCDEF";
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2013-11-21 19:42:40 +04:00
|
|
|
const unsigned char* src = (const unsigned char *) str;
|
2008-08-19 17:57:12 +04:00
|
|
|
while (*src)
|
1999-03-16 22:12:51 +03:00
|
|
|
{
|
2008-08-19 17:57:12 +04:00
|
|
|
len++;
|
1999-03-16 22:12:51 +03:00
|
|
|
if (!IS_OK(*src++))
|
2008-08-19 17:57:12 +04:00
|
|
|
charsToEscape++;
|
1999-03-16 22:12:51 +03:00
|
|
|
}
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2008-08-19 17:57:12 +04:00
|
|
|
// calculate how much memory should be allocated
|
|
|
|
// original length + 2 bytes for each escaped character + terminating '\0'
|
|
|
|
// do the sum in steps to check for overflow
|
|
|
|
size_t dstSize = len + 1 + charsToEscape;
|
|
|
|
if (dstSize <= len)
|
|
|
|
return 0;
|
|
|
|
dstSize += charsToEscape;
|
|
|
|
if (dstSize < len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// fail if we need more than 4GB
|
|
|
|
// size_t is likely to be long unsigned int but nsMemory::Alloc(size_t)
|
2012-08-22 19:56:38 +04:00
|
|
|
// calls NS_Alloc_P(size_t) which calls PR_Malloc(uint32_t), so there is
|
2008-08-19 17:57:12 +04:00
|
|
|
// no chance to allocate more than 4GB using nsMemory::Alloc()
|
2012-09-28 10:57:33 +04:00
|
|
|
if (dstSize > UINT32_MAX)
|
2008-08-19 17:57:12 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
char* result = (char *)nsMemory::Alloc(dstSize);
|
1998-12-08 05:15:50 +03:00
|
|
|
if (!result)
|
1999-03-16 22:12:51 +03:00
|
|
|
return 0;
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2013-11-21 19:42:40 +04:00
|
|
|
unsigned char* dst = (unsigned char *) result;
|
1999-03-16 22:12:51 +03:00
|
|
|
src = (const unsigned char *) str;
|
2005-06-01 22:34:53 +04:00
|
|
|
if (flags == url_XPAlphas)
|
1999-03-16 22:12:51 +03:00
|
|
|
{
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
unsigned char c = *src++;
|
|
|
|
if (IS_OK(c))
|
|
|
|
*dst++ = c;
|
|
|
|
else if (c == ' ')
|
|
|
|
*dst++ = '+'; /* convert spaces to pluses */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*dst++ = HEX_ESCAPE;
|
1999-09-04 20:33:12 +04:00
|
|
|
*dst++ = hexChars[c >> 4]; /* high nibble */
|
1999-03-16 22:12:51 +03:00
|
|
|
*dst++ = hexChars[c & 0x0f]; /* low nibble */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
unsigned char c = *src++;
|
|
|
|
if (IS_OK(c))
|
|
|
|
*dst++ = c;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*dst++ = HEX_ESCAPE;
|
1999-09-04 20:33:12 +04:00
|
|
|
*dst++ = hexChars[c >> 4]; /* high nibble */
|
1999-03-16 22:12:51 +03:00
|
|
|
*dst++ = hexChars[c & 0x0f]; /* low nibble */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
*dst = '\0'; /* tack on eos */
|
|
|
|
if(out_len)
|
|
|
|
*out_len = dst - (unsigned char *) result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-03-15 04:04:32 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
2011-08-18 17:46:39 +04:00
|
|
|
char* nsEscape(const char * str, nsEscapeMask flags)
|
2003-03-15 04:04:32 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if(!str)
|
2013-10-11 00:41:39 +04:00
|
|
|
return nullptr;
|
|
|
|
return nsEscapeCount(str, flags, nullptr);
|
2003-03-15 04:04:32 +03:00
|
|
|
}
|
|
|
|
|
1998-12-08 05:15:50 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
2011-08-18 17:46:39 +04:00
|
|
|
char* nsUnescape(char * str)
|
1998-12-08 05:15:50 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsUnescapeCount(str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nsUnescapeCount(char * str)
|
1998-12-08 05:15:50 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
2013-11-21 19:42:40 +04:00
|
|
|
char *src = str;
|
|
|
|
char *dst = str;
|
2001-11-16 05:09:13 +03:00
|
|
|
static const char hexChars[] = "0123456789ABCDEFabcdef";
|
|
|
|
|
|
|
|
char c1[] = " ";
|
|
|
|
char c2[] = " ";
|
|
|
|
char* const pc1 = c1;
|
|
|
|
char* const pc2 = c2;
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
while (*src)
|
2001-11-16 05:09:13 +03:00
|
|
|
{
|
|
|
|
c1[0] = *(src+1);
|
|
|
|
if (*(src+1) == '\0')
|
|
|
|
c2[0] = '\0';
|
|
|
|
else
|
|
|
|
c2[0] = *(src+2);
|
|
|
|
|
|
|
|
if (*src != HEX_ESCAPE || PL_strpbrk(pc1, hexChars) == 0 ||
|
|
|
|
PL_strpbrk(pc2, hexChars) == 0 )
|
1998-12-08 05:15:50 +03:00
|
|
|
*dst++ = *src++;
|
|
|
|
else
|
1999-03-16 22:12:51 +03:00
|
|
|
{
|
1998-12-08 05:15:50 +03:00
|
|
|
src++; /* walk over escape */
|
|
|
|
if (*src)
|
1999-03-16 22:12:51 +03:00
|
|
|
{
|
1998-12-08 05:15:50 +03:00
|
|
|
*dst = UNHEX(*src) << 4;
|
|
|
|
src++;
|
1999-03-16 22:12:51 +03:00
|
|
|
}
|
1998-12-08 05:15:50 +03:00
|
|
|
if (*src)
|
1999-03-16 22:12:51 +03:00
|
|
|
{
|
1998-12-08 05:15:50 +03:00
|
|
|
*dst = (*dst + UNHEX(*src));
|
|
|
|
src++;
|
1999-03-16 22:12:51 +03:00
|
|
|
}
|
1998-12-08 05:15:50 +03:00
|
|
|
dst++;
|
1999-03-16 22:12:51 +03:00
|
|
|
}
|
2001-11-16 05:09:13 +03:00
|
|
|
}
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2013-11-06 09:12:56 +04:00
|
|
|
*dst = 0;
|
1998-12-08 05:15:50 +03:00
|
|
|
return (int)(dst - str);
|
|
|
|
|
|
|
|
} /* NET_UnEscapeCnt */
|
|
|
|
|
1999-04-13 00:23:21 +04:00
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
char *
|
1999-04-13 00:23:21 +04:00
|
|
|
nsEscapeHTML(const char * string)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
char *rv = nullptr;
|
2008-10-25 03:15:33 +04:00
|
|
|
/* XXX Hardcoded max entity len. The +1 is for the trailing null. */
|
2013-05-08 07:40:12 +04:00
|
|
|
uint32_t len = strlen(string);
|
2012-09-28 10:57:33 +04:00
|
|
|
if (len >= (UINT32_MAX / 6))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
1999-04-13 00:23:21 +04:00
|
|
|
|
2008-10-25 03:15:33 +04:00
|
|
|
rv = (char *)NS_Alloc( (6 * len) + 1 );
|
|
|
|
char *ptr = rv;
|
1999-04-13 00:23:21 +04:00
|
|
|
|
2008-10-25 03:15:33 +04:00
|
|
|
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);
|
1999-04-13 00:23:21 +04:00
|
|
|
}
|
2000-06-06 02:01:32 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t *
|
|
|
|
nsEscapeHTML2(const char16_t *aSourceBuffer, int32_t aSourceBufferLen)
|
2000-06-06 02:01:32 +04:00
|
|
|
{
|
2012-02-18 17:47:01 +04:00
|
|
|
// Calculate the length, if the caller didn't.
|
2010-04-29 04:58:00 +04:00
|
|
|
if (aSourceBufferLen < 0) {
|
2012-05-30 21:43:39 +04:00
|
|
|
aSourceBufferLen = NS_strlen(aSourceBuffer);
|
2000-10-12 02:50:14 +04:00
|
|
|
}
|
2000-06-06 02:01:32 +04:00
|
|
|
|
2004-06-08 14:47:09 +04:00
|
|
|
/* XXX Hardcoded max entity len. */
|
2012-08-22 19:56:38 +04:00
|
|
|
if (uint32_t(aSourceBufferLen) >=
|
2014-01-04 19:02:17 +04:00
|
|
|
((UINT32_MAX - sizeof(char16_t)) / (6 * sizeof(char16_t))) )
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-10-25 03:15:33 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t *resultBuffer = (char16_t *)nsMemory::Alloc(aSourceBufferLen *
|
|
|
|
6 * sizeof(char16_t) + sizeof(char16_t('\0')));
|
|
|
|
char16_t *ptr = resultBuffer;
|
2000-06-06 02:01:32 +04:00
|
|
|
|
2000-10-12 02:50:14 +04:00
|
|
|
if (resultBuffer) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i;
|
2000-10-12 02:50:14 +04:00
|
|
|
|
|
|
|
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++ = ';';
|
2004-06-06 02:26:46 +04:00
|
|
|
} else if (aSourceBuffer[i] == '\'') {
|
|
|
|
*ptr++ = '&';
|
2004-06-08 14:47:09 +04:00
|
|
|
*ptr++ = '#';
|
|
|
|
*ptr++ = '3';
|
|
|
|
*ptr++ = '9';
|
2004-06-06 02:26:46 +04:00
|
|
|
*ptr++ = ';';
|
2000-10-12 02:50:14 +04:00
|
|
|
} else {
|
|
|
|
*ptr++ = aSourceBuffer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultBuffer;
|
2000-06-06 02:01:32 +04:00
|
|
|
}
|
|
|
|
|
2001-06-22 02:02:47 +04:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const int EscapeChars[256] =
|
|
|
|
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
|
|
|
{
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
|
2007-06-17 16:29:08 +04:00
|
|
|
0,1023, 0, 512,1023, 0,1023, 0,1023,1023,1023,1023,1023,1023, 953, 784, /* 2x !"#$%&'()*+,-./ */
|
2011-08-22 18:51:52 +04:00
|
|
|
1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1008,1008, 0,1008, 0, 768, /* 3x 0123456789:;<=>? */
|
2001-12-22 01:29:02 +03:00
|
|
|
1008,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023, /* 4x @ABCDEFGHIJKLMNO */
|
2001-06-22 02:02:47 +04:00
|
|
|
1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023, 896, 896, 896, 896,1023, /* 5x PQRSTUVWXYZ[\]^_ */
|
|
|
|
0,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023, /* 6x `abcdefghijklmno */
|
2001-12-22 01:29:02 +03:00
|
|
|
1023,1023,1023,1023,1023,1023,1023,1023,1023,1023,1023, 896,1012, 896,1023, 0, /* 7x pqrstuvwxyz{|}~ */
|
2001-06-22 02:02:47 +04:00
|
|
|
0 /* 8x DEL */
|
|
|
|
};
|
|
|
|
|
2005-06-01 22:34:53 +04:00
|
|
|
#define NO_NEED_ESC(C) (EscapeChars[((unsigned int) (C))] & (flags))
|
2001-06-22 02:02:47 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/* returns an escaped string */
|
|
|
|
|
2005-06-01 22:34:53 +04:00
|
|
|
/* use the following flags to specify which
|
2001-06-22 02:02:47 +04:00
|
|
|
part of an URL you want to escape:
|
|
|
|
|
|
|
|
esc_Scheme = 1
|
|
|
|
esc_Username = 2
|
|
|
|
esc_Password = 4
|
|
|
|
esc_Host = 8
|
|
|
|
esc_Directory = 16
|
|
|
|
esc_FileBaseName = 32
|
|
|
|
esc_FileExtension = 64
|
|
|
|
esc_Param = 128
|
|
|
|
esc_Query = 256
|
|
|
|
esc_Ref = 512
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* by default this function will not escape parts of a string
|
|
|
|
that already look escaped, which means it already includes
|
|
|
|
a valid hexcode. This is done to avoid multiple escapes of
|
2005-06-01 22:34:53 +04:00
|
|
|
a string. Use the following flags to force escaping of a
|
2001-06-22 02:02:47 +04:00
|
|
|
string:
|
|
|
|
|
|
|
|
esc_Forced = 1024
|
|
|
|
*/
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool NS_EscapeURL(const char *part,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t partLen,
|
|
|
|
uint32_t flags,
|
2002-03-06 10:48:55 +03:00
|
|
|
nsACString &result)
|
2001-11-16 05:09:13 +03:00
|
|
|
{
|
2002-03-06 10:48:55 +03:00
|
|
|
if (!part) {
|
|
|
|
NS_NOTREACHED("null pointer");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
2001-06-22 02:02:47 +04:00
|
|
|
|
|
|
|
int i = 0;
|
2001-11-07 09:24:10 +03:00
|
|
|
static const char hexChars[] = "0123456789ABCDEF";
|
2002-01-12 02:02:22 +03:00
|
|
|
if (partLen < 0)
|
|
|
|
partLen = strlen(part);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool forced = !!(flags & esc_Forced);
|
|
|
|
bool ignoreNonAscii = !!(flags & esc_OnlyASCII);
|
|
|
|
bool ignoreAscii = !!(flags & esc_OnlyNonASCII);
|
|
|
|
bool writing = !!(flags & esc_AlwaysCopy);
|
|
|
|
bool colon = !!(flags & esc_Colon);
|
2001-06-22 02:02:47 +04:00
|
|
|
|
2013-11-21 19:42:40 +04:00
|
|
|
const unsigned char* src = (const unsigned char *) part;
|
2001-06-22 02:02:47 +04:00
|
|
|
|
|
|
|
char tempBuffer[100];
|
|
|
|
unsigned int tempBufferPos = 0;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool previousIsNonASCII = false;
|
2002-01-12 02:02:22 +03:00
|
|
|
for (i = 0; i < partLen; i++)
|
2001-06-22 02:02:47 +04:00
|
|
|
{
|
|
|
|
unsigned char c = *src++;
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
// 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.
|
2003-02-27 16:15:19 +03:00
|
|
|
// On special request we will also escape the colon even when
|
2004-01-07 06:57:20 +03:00
|
|
|
// not covered by the matrix.
|
|
|
|
// ignoreAscii is not honored for control characters (C0 and DEL)
|
2005-07-28 18:51:17 +04:00
|
|
|
//
|
|
|
|
// And, we should escape the '|' character when it occurs after any
|
|
|
|
// non-ASCII character as it may be part of a multi-byte character.
|
2007-07-24 07:30:00 +04:00
|
|
|
//
|
|
|
|
// 0x20..0x7e are the valid ASCII characters. We also escape spaces
|
|
|
|
// (0x20) since they are not legal in URLs.
|
2003-02-27 16:15:19 +03:00
|
|
|
if ((NO_NEED_ESC(c) || (c == HEX_ESCAPE && !forced)
|
|
|
|
|| (c > 0x7f && ignoreNonAscii)
|
2007-07-24 04:28:42 +04:00
|
|
|
|| (c > 0x20 && c < 0x7f && ignoreAscii))
|
2005-07-28 18:51:17 +04:00
|
|
|
&& !(c == ':' && colon)
|
|
|
|
&& !(previousIsNonASCII && c == '|' && !ignoreNonAscii))
|
2001-11-16 05:09:13 +03:00
|
|
|
{
|
|
|
|
if (writing)
|
|
|
|
tempBuffer[tempBufferPos++] = c;
|
2001-06-22 02:02:47 +04:00
|
|
|
}
|
2001-11-16 05:09:13 +03:00
|
|
|
else /* do the escape magic */
|
2001-06-22 02:02:47 +04:00
|
|
|
{
|
2001-11-16 05:09:13 +03:00
|
|
|
if (!writing)
|
|
|
|
{
|
2002-01-12 02:02:22 +03:00
|
|
|
result.Append(part, i);
|
2011-10-17 18:59:28 +04:00
|
|
|
writing = true;
|
2001-11-16 05:09:13 +03:00
|
|
|
}
|
|
|
|
tempBuffer[tempBufferPos++] = HEX_ESCAPE;
|
|
|
|
tempBuffer[tempBufferPos++] = hexChars[c >> 4]; /* high nibble */
|
|
|
|
tempBuffer[tempBufferPos++] = hexChars[c & 0x0f]; /* low nibble */
|
2001-06-22 02:02:47 +04:00
|
|
|
}
|
2005-07-28 18:51:17 +04:00
|
|
|
|
2001-11-16 05:09:13 +03:00
|
|
|
if (tempBufferPos >= sizeof(tempBuffer) - 4)
|
2005-07-28 18:51:17 +04:00
|
|
|
{
|
2001-11-16 05:09:13 +03:00
|
|
|
NS_ASSERTION(writing, "should be writing");
|
|
|
|
tempBuffer[tempBufferPos] = '\0';
|
|
|
|
result += tempBuffer;
|
|
|
|
tempBufferPos = 0;
|
2005-07-28 18:51:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
previousIsNonASCII = (c > 0x7f);
|
|
|
|
}
|
2001-11-16 05:09:13 +03:00
|
|
|
if (writing) {
|
|
|
|
tempBuffer[tempBufferPos] = '\0';
|
|
|
|
result += tempBuffer;
|
2001-06-22 02:02:47 +04:00
|
|
|
}
|
2002-01-12 02:02:22 +03:00
|
|
|
return writing;
|
2001-06-22 02:02:47 +04:00
|
|
|
}
|
2000-06-06 02:01:32 +04:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
#define ISHEX(c) memchr(hexChars, c, sizeof(hexChars)-1)
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool NS_UnescapeURL(const char *str, int32_t len, uint32_t flags, nsACString &result)
|
2002-01-12 02:02:22 +03:00
|
|
|
{
|
2002-03-06 10:48:55 +03:00
|
|
|
if (!str) {
|
|
|
|
NS_NOTREACHED("null pointer");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = strlen(str);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ignoreNonAscii = !!(flags & esc_OnlyASCII);
|
|
|
|
bool ignoreAscii = !!(flags & esc_OnlyNonASCII);
|
|
|
|
bool writing = !!(flags & esc_AlwaysCopy);
|
|
|
|
bool skipControl = !!(flags & esc_SkipControl);
|
2002-03-06 10:48:55 +03:00
|
|
|
|
|
|
|
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;
|
2004-10-12 12:48:31 +04:00
|
|
|
if (ISHEX(*p1) && ISHEX(*p2) &&
|
|
|
|
((*p1 < '8' && !ignoreAscii) || (*p1 >= '8' && !ignoreNonAscii)) &&
|
2004-01-07 07:17:40 +03:00
|
|
|
!(skipControl &&
|
2004-01-07 06:57:20 +03:00
|
|
|
(*p1 < '2' || (*p1 == '7' && (*p2 == 'f' || *p2 == 'F'))))) {
|
2002-03-06 10:48:55 +03:00
|
|
|
//printf("- p1=%c p2=%c\n", *p1, *p2);
|
2011-10-17 18:59:28 +04:00
|
|
|
writing = true;
|
2002-03-06 10:48:55 +03:00
|
|
|
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;
|
2002-01-12 02:02:22 +03:00
|
|
|
}
|