From 56d6f96154b7f0406a1fe5ed94032594faef5a6e Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Thu, 30 Jun 2005 17:28:33 +0000 Subject: [PATCH] added url_enescape_friendly() for cards.c: it prepares a printable version of the URL (with interpretted spaces and delimiters, hence it cannot be parsed back), but it does not convert characters above 0x7f so that the target string is a valid utf-8 string --- lib/url.c | 27 +++++++++++++++++++++++++-- lib/url.h | 2 +- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/url.c b/lib/url.c index e38238ec..152b2d29 100644 --- a/lib/url.c +++ b/lib/url.c @@ -2,7 +2,7 @@ * UCW Library -- URL Functions * * (c) 1997--2004 Martin Mares - * (c) 2001 Robert Spalek + * (c) 2001--2005 Robert Spalek * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -50,7 +50,7 @@ static void CONSTRUCTOR url_init_config(void) /* Escaping and de-escaping */ -uns +static uns enhex(uns x) { return (x<10) ? (x + '0') : (x - 10 + 'A'); @@ -149,6 +149,29 @@ url_enescape(byte *s, byte *d) return 0; } +int +url_enescape_friendly(byte *src, byte *dest) +{ + byte *end = dest + MAX_URL_SIZE - 10; + while (*src) + { + if (dest >= end) + return URL_ERR_TOO_LONG; + if (*src < NCC_MAX) + *dest++ = NCC_CHARS[*src++]; + else if (*src < 0x80) + *dest++ = *src++; + else + { + *dest++ = '%'; + *dest++ = enhex(*src >> 4); + *dest++ = enhex(*src++ & 0x0f); + } + } + *dest = 0; + return 0; +} + /* Split an URL (several parts may be copied to the destination buffer) */ byte *url_proto_names[URL_PROTO_MAX] = URL_PNAMES; diff --git a/lib/url.h b/lib/url.h index b7cb8bfb..8e92d7c5 100644 --- a/lib/url.h +++ b/lib/url.h @@ -31,6 +31,7 @@ int url_deescape(byte *s, byte *d); int url_enescape(byte *s, byte *d); +int url_enescape_friendly(byte *src, byte *dest); // for cards.c only /* URL splitting and normalization */ @@ -45,7 +46,6 @@ struct url { byte *buf, *bufend; }; -uns enhex(uns x); int url_split(byte *s, struct url *u, byte *d); int url_normalize(struct url *u, struct url *b); int url_canonicalize(struct url *u); -- 2.39.2