From: Florian Forster Date: Wed, 9 Jun 2010 17:01:25 +0000 (+0200) Subject: utils_params.[ch]: Implement "uri_escape". X-Git-Tag: v4.0.0~289 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=7758a9edc32335f94c9b93ead8e7cf4d153699de;p=collection4.git utils_params.[ch]: Implement "uri_escape". --- diff --git a/utils_params.c b/utils_params.c index ec943b5..6e5dc89 100644 --- a/utils_params.c +++ b/utils_params.c @@ -215,4 +215,45 @@ const char *param (const char *key) /* {{{ */ return (parameter_lookup (key)); } /* }}} const char *param */ +int uri_escape (char *dst, const char *src, size_t size) /* {{{ */ +{ + size_t in; + size_t out; + + in = 0; + out = 0; + while (42) + { + if (src[in] == 0) + { + dst[out] = 0; + return (0); + } + else if ((src[in] < 32) + || (src[in] == '&') + || (src[in] == ';') + || (src[in] >= 128)) + { + char esc[4]; + + if ((size - out) < 4) + break; + + snprintf (esc, sizeof (esc), "%%%02x", (unsigned int) src[in]); + dst[out] = esc[0]; + dst[out+1] = esc[1]; + dst[out+2] = esc[2]; + + out += 3; + in++; + } + else + { + dst[out] = src[in]; + out++; + in++; + } + } /* while (42) */ +} /* }}} int uri_escape */ + /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/utils_params.h b/utils_params.h index b68ad4d..3c7ea0a 100644 --- a/utils_params.h +++ b/utils_params.h @@ -6,4 +6,6 @@ void param_finish (void); const char *param (const char *key); +int uri_escape (char *dst, const char *src, size_t size); + #endif /* UTILS_PARAMS_H */