[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: url protection
From: |
Per Bothner |
Subject: |
Re: url protection |
Date: |
Fri, 5 Aug 2022 10:48:25 -0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 |
On 8/5/22 10:35, Gavin Smith wrote:
Could we write or copy the code for escaping a URL as it should
be very short and simple? This would avoid an extra module dependency.
Here is C/C++ code written by me.
It works in two passes - the first counts the number of bytes
that need to be escaped. For Perl a single pass may make more sense.
/* Returns either NULL or a freshly malloc'd urlencoding of 'in'. */
char *
url_encode(const char *in, int mode)
{
static unsigned char b16[] = "0123456789ABCDEF";
int bad_count = 0;
char *out = NULL;
for (int pass = 0; pass < 2; pass++) {
const char *p = in;
char *q = out;
while (*p) {
int ch = *p++;
bool ok = (ch >= '0' && ch <= '9')
|| (ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| (ch == '/') /* may depend on mode */
|| (ch == '.' || ch == '-' || ch == '_' || ch == '*');
if (pass == 0) {
if (! ok)
bad_count++;
} else {
if (ok)
*q++ = ch;
else {
*q++ = '%';
*q++ = b16[(ch>>4) & 0xF];
*q++ = b16[ch & 0xF];
}
}
}
if (pass == 0) {
if (bad_count == 0)
return NULL;
size_t in_size = (char*) p - in;
out = challoc(in_size + 2 * bad_count + 1);
} else
*q = 0;
}
return out;
}
--
--Per Bothner
per@bothner.com http://per.bothner.com/
- Re: url protection, (continued)
- Re: url protection, Gavin Smith, 2022/08/05
- Re: url protection, Eli Zaretskii, 2022/08/04
- Message not available
- Re: url protection, Eli Zaretskii, 2022/08/05
- Re: url protection, Patrice Dumas, 2022/08/05
- Re: url protection, Per Bothner, 2022/08/05
- Re: url protection, Gavin Smith, 2022/08/04
- Re: url protection, Patrice Dumas, 2022/08/04
- Re: url protection, Gavin Smith, 2022/08/05
- Re: url protection, Patrice Dumas, 2022/08/05
Re: url protection, Gavin Smith, 2022/08/05
- Re: url protection,
Per Bothner <=
Re: url protection, Patrice Dumas, 2022/08/06