]> mj.ucw.cz Git - libucw.git/commitdiff
added url_enescape_friendly() for cards.c: it prepares a printable version
authorRobert Spalek <robert@ucw.cz>
Thu, 30 Jun 2005 17:28:33 +0000 (17:28 +0000)
committerRobert Spalek <robert@ucw.cz>
Thu, 30 Jun 2005 17:28:33 +0000 (17:28 +0000)
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
lib/url.h

index e38238ec3973718b0badf8f56580eac6baac5ebd..152b2d291257764c80350750194277a0b31ee4e7 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -2,7 +2,7 @@
  *     UCW Library -- URL Functions
  *
  *     (c) 1997--2004 Martin Mares <mj@ucw.cz>
- *     (c) 2001 Robert Spalek <robert@ucw.cz>
+ *     (c) 2001--2005 Robert Spalek <robert@ucw.cz>
  *
  *     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;
index b7cb8bfb1888ae9b3026b4b56fa721b8e7bdf21e..8e92d7c57f3365f93304a3ff17c65cc5f4e4c411 100644 (file)
--- 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);