From 3e2759323c532c2dcfd543205ad69f826a47d4dd Mon Sep 17 00:00:00 2001 From: Nedko Arnaudov Date: Sat, 12 Sep 2009 11:11:52 +0300 Subject: [PATCH] Move escape helpers to separate file --- daemon/escape.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ daemon/escape.h | 35 ++++++++++++++++++ daemon/studio.c | 69 +---------------------------------- wscript | 1 + 4 files changed, 133 insertions(+), 68 deletions(-) create mode 100644 daemon/escape.c create mode 100644 daemon/escape.h diff --git a/daemon/escape.c b/daemon/escape.c new file mode 100644 index 00000000..c6218667 --- /dev/null +++ b/daemon/escape.c @@ -0,0 +1,96 @@ +/* -*- Mode: C ; c-basic-offset: 2 -*- */ +/* + * LADI Session Handler (ladish) + * + * Copyright (C) 2009 Nedko Arnaudov + * + ************************************************************************** + * This file contains implementation of the escape helper functions + ************************************************************************** + * + * LADI Session Handler is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * LADI Session Handler is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LADI Session Handler. If not, see + * or write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "escape.h" + +static char hex_digits[] = "0123456789ABCDEF"; + +#define HEX_TO_INT(hexchar) ((hexchar) <= '9' ? hexchar - '0' : 10 + (hexchar - 'A')) + +void escape(const char ** src_ptr, char ** dst_ptr) +{ + const char * src; + char * dst; + + src = *src_ptr; + dst = *dst_ptr; + + while (*src != 0) + { + switch (*src) + { + case '/': /* used as separator for address components */ + case '<': /* invalid attribute value char (XML spec) */ + case '&': /* invalid attribute value char (XML spec) */ + case '"': /* we store attribute values in double quotes - invalid attribute value char (XML spec) */ + case '%': + dst[0] = '%'; + dst[1] = hex_digits[*src >> 4]; + dst[2] = hex_digits[*src & 0x0F]; + dst += 3; + src++; + break; + default: + *dst++ = *src++; + } + } + + *src_ptr = src; + *dst_ptr = dst; +} + +size_t unescape(const char * src, size_t src_len, char * dst) +{ + size_t dst_len; + + dst_len = 0; + + while (src_len) + { + if (src_len >= 3 && + src[0] == '%' && + ((src[1] >= '0' && src[1] <= '9') || + (src[1] >= 'A' && src[1] <= 'F')) && + ((src[2] >= '0' && src[2] <= '9') || + (src[2] >= 'A' && src[2] <= 'F'))) + { + *dst = (HEX_TO_INT(src[1]) << 4) | HEX_TO_INT(src[2]); + //lash_info("unescaping %c%c%c to '%c'", src[0], src[1], src[2], *dst); + src_len -= 3; + src += 3; + } + else + { + *dst = *src; + src_len--; + src++; + } + dst++; + dst_len++; + } + + return dst_len; +} diff --git a/daemon/escape.h b/daemon/escape.h new file mode 100644 index 00000000..9cf55bbc --- /dev/null +++ b/daemon/escape.h @@ -0,0 +1,35 @@ +/* -*- Mode: C ; c-basic-offset: 2 -*- */ +/* + * LADI Session Handler (ladish) + * + * Copyright (C) 2009 Nedko Arnaudov + * + ************************************************************************** + * This file contains the prototypes of the escape helper functions + ************************************************************************** + * + * LADI Session Handler is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * LADI Session Handler is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LADI Session Handler. If not, see + * or write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef ESCAPE_H__FABBE484_1093_41C1_9FC9_62ED0106E542__INCLUDED +#define ESCAPE_H__FABBE484_1093_41C1_9FC9_62ED0106E542__INCLUDED + +#include "common.h" + +void escape(const char ** src_ptr, char ** dst_ptr); +size_t unescape(const char * src, size_t src_len, char * dst); + +#endif /* #ifndef ESCAPE_H__FABBE484_1093_41C1_9FC9_62ED0106E542__INCLUDED */ diff --git a/daemon/studio.c b/daemon/studio.c index 52784012..d84d7dcc 100644 --- a/daemon/studio.c +++ b/daemon/studio.c @@ -43,6 +43,7 @@ #include "dirhelpers.h" #include "jack_dispatch.h" #include "graph_dict.h" +#include "escape.h" #define STUDIOS_DIR "/studios/" char * g_studios_dir; @@ -839,74 +840,6 @@ bool studio_is_loaded(void) return g_studio.dbus_object != NULL; } -void escape(const char ** src_ptr, char ** dst_ptr) -{ - const char * src; - char * dst; - static char hex_digits[] = "0123456789ABCDEF"; - - src = *src_ptr; - dst = *dst_ptr; - - while (*src != 0) - { - switch (*src) - { - case '/': /* used as separator for address components */ - case '<': /* invalid attribute value char (XML spec) */ - case '&': /* invalid attribute value char (XML spec) */ - case '"': /* we store attribute values in double quotes - invalid attribute value char (XML spec) */ - case '%': - dst[0] = '%'; - dst[1] = hex_digits[*src >> 4]; - dst[2] = hex_digits[*src & 0x0F]; - dst += 3; - src++; - break; - default: - *dst++ = *src++; - } - } - - *src_ptr = src; - *dst_ptr = dst; -} - -#define HEX_TO_INT(hexchar) ((hexchar) <= '9' ? hexchar - '0' : 10 + (hexchar - 'A')) - -static size_t unescape(const char * src, size_t src_len, char * dst) -{ - size_t dst_len; - - dst_len = 0; - - while (src_len) - { - if (src_len >= 3 && - src[0] == '%' && - ((src[1] >= '0' && src[1] <= '9') || - (src[1] >= 'A' && src[1] <= 'F')) && - ((src[2] >= '0' && src[2] <= '9') || - (src[2] >= 'A' && src[2] <= 'F'))) - { - *dst = (HEX_TO_INT(src[1]) << 4) | HEX_TO_INT(src[2]); - //lash_info("unescaping %c%c%c to '%c'", src[0], src[1], src[2], *dst); - src_len -= 3; - src += 3; - } - else - { - *dst = *src; - src_len--; - src++; - } - dst++; - dst_len++; - } - - return dst_len; -} - static bool compose_filename(const char * name, char ** filename_ptr_ptr, char ** backup_filename_ptr_ptr) { size_t len_dir; diff --git a/wscript b/wscript index 58bb58b5..deb76456 100644 --- a/wscript +++ b/wscript @@ -205,6 +205,7 @@ def build(bld): 'jack_dispatch.c', 'dict.c', 'graph_dict.c', + 'escape.c', ]: daemon.source.append(os.path.join("daemon", source))