2000-09-12 12:05:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
1998-12-08 05:15:50 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1998-12-08 05:15:50 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-12-08 05:15:50 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-12-08 05:15:50 +03:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
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"
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
const int netCharType[256] =
|
|
|
|
/* Bit 0 xalpha -- the alphas
|
|
|
|
** Bit 1 xpalpha -- as xalpha but
|
|
|
|
** converts spaces to plus and plus to %20
|
|
|
|
** 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)))
|
|
|
|
|
|
|
|
|
|
|
|
#define IS_OK(C) (netCharType[((unsigned int) (C))] & (mask))
|
|
|
|
#define HEX_ESCAPE '%'
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_COM char* nsEscape(const char * str, nsEscapeMask mask)
|
1998-12-08 05:15:50 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
if(!str)
|
|
|
|
return NULL;
|
1999-09-04 20:33:12 +04:00
|
|
|
return nsEscapeCount(str, (PRInt32)nsCRT::strlen(str), mask, NULL);
|
1998-12-08 05:15:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_COM char* nsEscapeCount(
|
1999-03-16 22:12:51 +03:00
|
|
|
const char * str,
|
|
|
|
PRInt32 len,
|
|
|
|
nsEscapeMask mask,
|
|
|
|
PRInt32* 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
|
|
|
|
1999-03-16 22:12:51 +03:00
|
|
|
int i, extra = 0;
|
|
|
|
char* hexChars = "0123456789ABCDEF";
|
1998-12-08 05:15:50 +03:00
|
|
|
|
1999-03-16 22:12:51 +03:00
|
|
|
register const unsigned char* src = (const unsigned char *) str;
|
1998-12-08 05:15:50 +03:00
|
|
|
for (i = 0; i < len; i++)
|
1999-03-16 22:12:51 +03:00
|
|
|
{
|
|
|
|
if (!IS_OK(*src++))
|
|
|
|
extra += 2; /* the escape, plus an extra byte for each nibble */
|
|
|
|
}
|
1998-12-08 05:15:50 +03:00
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
char* result = (char *)nsMemory::Alloc(len + extra + 1);
|
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
|
|
|
|
|
|
|
register unsigned char* dst = (unsigned char *) result;
|
1999-03-16 22:12:51 +03:00
|
|
|
src = (const unsigned char *) str;
|
|
|
|
if (mask == url_XPAlphas)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_COM char* nsUnescape(char * str)
|
1998-12-08 05:15:50 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
nsUnescapeCount(str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_COM PRInt32 nsUnescapeCount(char * str)
|
1998-12-08 05:15:50 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
register char *src = str;
|
|
|
|
register char *dst = str;
|
|
|
|
|
|
|
|
while (*src)
|
|
|
|
if (*src != HEX_ESCAPE)
|
|
|
|
*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
|
|
|
}
|
1998-12-08 05:15:50 +03:00
|
|
|
|
|
|
|
*dst = 0;
|
|
|
|
return (int)(dst - str);
|
|
|
|
|
|
|
|
} /* NET_UnEscapeCnt */
|
|
|
|
|
1999-04-13 00:23:21 +04:00
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
NS_COM char *
|
1999-04-13 00:23:21 +04:00
|
|
|
nsEscapeHTML(const char * string)
|
|
|
|
{
|
2000-06-03 13:46:12 +04:00
|
|
|
char *rv = (char *) nsMemory::Alloc(nsCRT::strlen(string)*6 + 1); /* The +1 is for the trailing null! */
|
1999-04-13 00:23:21 +04:00
|
|
|
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++ = ';';
|
|
|
|
}
|
2000-02-11 04:32:20 +03:00
|
|
|
else if (*string == '"')
|
|
|
|
{
|
|
|
|
*ptr++ = '&';
|
|
|
|
*ptr++ = 'q';
|
|
|
|
*ptr++ = 'u';
|
|
|
|
*ptr++ = 'o';
|
|
|
|
*ptr++ = 't';
|
|
|
|
*ptr++ = ';';
|
|
|
|
}
|
1999-04-13 00:23:21 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*ptr++ = *string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return(rv);
|
|
|
|
}
|
2000-06-06 02:01:32 +04:00
|
|
|
|
|
|
|
NS_COM PRUnichar *
|
|
|
|
nsEscapeHTML2(const PRUnichar * string)
|
|
|
|
{
|
|
|
|
PRUnichar *rv = (PRUnichar *) nsMemory::Alloc(nsCRT::strlen(string)*6*sizeof(PRUnichar) + sizeof(PRUnichar));
|
|
|
|
PRUnichar *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
|
|
|
|
{
|
|
|
|
*ptr++ = *string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|