daemon: improve escaping functions
This commit is contained in:
parent
8ac048d601
commit
5bb20eda52
|
@ -776,7 +776,7 @@ static void callback_elend(void * data, const char * el)
|
||||||
else if (context_ptr->element[context_ptr->depth] == PARSE_CONTEXT_APPLICATION)
|
else if (context_ptr->element[context_ptr->depth] == PARSE_CONTEXT_APPLICATION)
|
||||||
{
|
{
|
||||||
context_ptr->data[unescape(context_ptr->data, context_ptr->data_used, context_ptr->data)] = 0;
|
context_ptr->data[unescape(context_ptr->data, context_ptr->data_used, context_ptr->data)] = 0;
|
||||||
unescape(context_ptr->str, strlen(context_ptr->str) + 1, context_ptr->str);
|
unescape_simple(context_ptr->str);
|
||||||
|
|
||||||
log_info("application '%s' (%s, %s, level %u) with commandline '%s'", context_ptr->str, context_ptr->terminal ? "terminal" : "shell", context_ptr->autorun ? "autorun" : "stopped", (unsigned int)context_ptr->level, context_ptr->data);
|
log_info("application '%s' (%s, %s, level %u) with commandline '%s'", context_ptr->str, context_ptr->terminal ? "terminal" : "shell", context_ptr->autorun ? "autorun" : "stopped", (unsigned int)context_ptr->level, context_ptr->data);
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ write_jack_parameter(
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
*dst++ = '/';
|
*dst++ = '/';
|
||||||
escape(&src, &dst);
|
escape(&src, &dst, LADISH_ESCAPE_FLAG_ALL);
|
||||||
src++;
|
src++;
|
||||||
}
|
}
|
||||||
while (*src != 0);
|
while (*src != 0);
|
||||||
|
|
|
@ -30,7 +30,7 @@ static char hex_digits[] = "0123456789ABCDEF";
|
||||||
|
|
||||||
#define HEX_TO_INT(hexchar) ((hexchar) <= '9' ? hexchar - '0' : 10 + (hexchar - 'A'))
|
#define HEX_TO_INT(hexchar) ((hexchar) <= '9' ? hexchar - '0' : 10 + (hexchar - 'A'))
|
||||||
|
|
||||||
void escape(const char ** src_ptr, char ** dst_ptr)
|
void escape(const char ** src_ptr, char ** dst_ptr, unsigned int flags)
|
||||||
{
|
{
|
||||||
const char * src;
|
const char * src;
|
||||||
char * dst;
|
char * dst;
|
||||||
|
@ -43,30 +43,38 @@ void escape(const char ** src_ptr, char ** dst_ptr)
|
||||||
switch (*src)
|
switch (*src)
|
||||||
{
|
{
|
||||||
case '/': /* used as separator for address components */
|
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 '\'':
|
case '\'':
|
||||||
case '>':
|
case '>':
|
||||||
case '%':
|
case '%':
|
||||||
|
if ((flags & LADISH_ESCAPE_FLAG_OTHER) == 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
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) */
|
||||||
|
if ((flags & LADISH_ESCAPE_FLAG_XML_ATTR) == 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
dst[0] = '%';
|
dst[0] = '%';
|
||||||
dst[1] = hex_digits[*src >> 4];
|
dst[1] = hex_digits[*src >> 4];
|
||||||
dst[2] = hex_digits[*src & 0x0F];
|
dst[2] = hex_digits[*src & 0x0F];
|
||||||
dst += 3;
|
dst += 3;
|
||||||
src++;
|
src++;
|
||||||
break;
|
continue;
|
||||||
default:
|
|
||||||
*dst++ = *src++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*dst++ = *src++;
|
||||||
}
|
}
|
||||||
|
|
||||||
*src_ptr = src;
|
*src_ptr = src;
|
||||||
*dst_ptr = dst;
|
*dst_ptr = dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
void escape_simple(const char * src_ptr, char * dst_ptr)
|
void escape_simple(const char * src_ptr, char * dst_ptr, unsigned int flags)
|
||||||
{
|
{
|
||||||
escape(&src_ptr, &dst_ptr);
|
escape(&src_ptr, &dst_ptr, flags);
|
||||||
*dst_ptr = 0;
|
*dst_ptr = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,3 +110,24 @@ size_t unescape(const char * src, size_t src_len, char * dst)
|
||||||
|
|
||||||
return dst_len;
|
return dst_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void unescape_simple(char * buffer)
|
||||||
|
{
|
||||||
|
unescape(buffer, strlen(buffer) + 1, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
char * unescape_dup(const char * src)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
char * dst;
|
||||||
|
|
||||||
|
len = strlen(src) + 1;
|
||||||
|
|
||||||
|
dst = malloc(len);
|
||||||
|
if (dst != NULL)
|
||||||
|
{
|
||||||
|
unescape(src, len, dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
|
@ -28,10 +28,17 @@
|
||||||
#define ESCAPE_H__FABBE484_1093_41C1_9FC9_62ED0106E542__INCLUDED
|
#define ESCAPE_H__FABBE484_1093_41C1_9FC9_62ED0106E542__INCLUDED
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include <limits.h> /* UINT_MAX */
|
||||||
|
|
||||||
void escape(const char ** src_ptr, char ** dst_ptr);
|
#define LADISH_ESCAPE_FLAG_XML_ATTR ((unsigned int)1 << 0)
|
||||||
void escape_simple(const char * src_ptr, char * dst_ptr);
|
#define LADISH_ESCAPE_FLAG_OTHER ((unsigned int)1 << 1)
|
||||||
|
#define LADISH_ESCAPE_FLAG_ALL UINT_MAX
|
||||||
|
|
||||||
|
void escape(const char ** src_ptr, char ** dst_ptr, unsigned int flags);
|
||||||
|
void escape_simple(const char * src_ptr, char * dst_ptr, unsigned int flags);
|
||||||
size_t unescape(const char * src, size_t src_len, char * dst);
|
size_t unescape(const char * src, size_t src_len, char * dst);
|
||||||
|
void unescape_simple(char * buffer);
|
||||||
|
char * unescape_dup(const char * src);
|
||||||
|
|
||||||
/* encode each char in three bytes (percent encoding) */
|
/* encode each char in three bytes (percent encoding) */
|
||||||
#define max_escaped_length(unescaped_length) ((unescaped_length) * 3)
|
#define max_escaped_length(unescaped_length) ((unescaped_length) * 3)
|
||||||
|
|
|
@ -655,7 +655,7 @@ static void callback_elend(void * data, const char * el)
|
||||||
else if (context_ptr->element[context_ptr->depth] == PARSE_CONTEXT_APPLICATION)
|
else if (context_ptr->element[context_ptr->depth] == PARSE_CONTEXT_APPLICATION)
|
||||||
{
|
{
|
||||||
context_ptr->data[unescape(context_ptr->data, context_ptr->data_used, context_ptr->data)] = 0;
|
context_ptr->data[unescape(context_ptr->data, context_ptr->data_used, context_ptr->data)] = 0;
|
||||||
unescape(context_ptr->str, strlen(context_ptr->str) + 1, context_ptr->str);
|
unescape_simple(context_ptr->str);
|
||||||
|
|
||||||
log_info("application '%s' (%s, %s, level %u) with commandline '%s'", context_ptr->str, context_ptr->terminal ? "terminal" : "shell", context_ptr->autorun ? "autorun" : "stopped", (unsigned int)context_ptr->level, context_ptr->data);
|
log_info("application '%s' (%s, %s, level %u) with commandline '%s'", context_ptr->str, context_ptr->terminal ? "terminal" : "shell", context_ptr->autorun ? "autorun" : "stopped", (unsigned int)context_ptr->level, context_ptr->data);
|
||||||
|
|
||||||
|
|
|
@ -267,7 +267,7 @@ char * compose_project_dir_from_name(const char * project_name)
|
||||||
|
|
||||||
memcpy(project_dir, home_dir, home_dir_len);
|
memcpy(project_dir, home_dir, home_dir_len);
|
||||||
memcpy(project_dir + home_dir_len, DEFAULT_PROJECT_BASE_DIR, DEFAULT_PROJECT_BASE_DIR_LEN);
|
memcpy(project_dir + home_dir_len, DEFAULT_PROJECT_BASE_DIR, DEFAULT_PROJECT_BASE_DIR_LEN);
|
||||||
escape_simple(project_name, project_dir + home_dir_len + DEFAULT_PROJECT_BASE_DIR_LEN);
|
escape_simple(project_name, project_dir + home_dir_len + DEFAULT_PROJECT_BASE_DIR_LEN, LADISH_ESCAPE_FLAG_ALL);
|
||||||
|
|
||||||
return project_dir;
|
return project_dir;
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,7 @@ bool ladish_write_indented_string(int fd, int indent, const char * string)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ladish_write_string_escape(int fd, const char * string)
|
bool ladish_write_string_escape_ex(int fd, const char * string, unsigned int flags)
|
||||||
{
|
{
|
||||||
bool ret;
|
bool ret;
|
||||||
char * escaped_buffer;
|
char * escaped_buffer;
|
||||||
|
@ -83,7 +83,7 @@ bool ladish_write_string_escape(int fd, const char * string)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
escape_simple(string, escaped_buffer);
|
escape_simple(string, escaped_buffer, flags);
|
||||||
|
|
||||||
ret = ladish_write_string(fd, escaped_buffer);
|
ret = ladish_write_string(fd, escaped_buffer);
|
||||||
|
|
||||||
|
@ -92,6 +92,11 @@ bool ladish_write_string_escape(int fd, const char * string)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ladish_write_string_escape(int fd, const char * string)
|
||||||
|
{
|
||||||
|
return ladish_write_string_escape_ex(fd, string, LADISH_ESCAPE_FLAG_ALL);
|
||||||
|
}
|
||||||
|
|
||||||
#define fd (((struct ladish_write_context *)context)->fd)
|
#define fd (((struct ladish_write_context *)context)->fd)
|
||||||
#define indent (((struct ladish_write_context *)context)->indent)
|
#define indent (((struct ladish_write_context *)context)->indent)
|
||||||
|
|
||||||
|
@ -462,7 +467,7 @@ ladish_save_app(
|
||||||
|
|
||||||
unescaped_string = name;
|
unescaped_string = name;
|
||||||
escaped_string = escaped_buffer;
|
escaped_string = escaped_buffer;
|
||||||
escape(&unescaped_string, &escaped_string);
|
escape(&unescaped_string, &escaped_string, LADISH_ESCAPE_FLAG_ALL);
|
||||||
*escaped_string = 0;
|
*escaped_string = 0;
|
||||||
if (!ladish_write_string(fd, escaped_buffer))
|
if (!ladish_write_string(fd, escaped_buffer))
|
||||||
{
|
{
|
||||||
|
@ -508,7 +513,7 @@ ladish_save_app(
|
||||||
|
|
||||||
unescaped_string = command;
|
unescaped_string = command;
|
||||||
escaped_string = escaped_buffer;
|
escaped_string = escaped_buffer;
|
||||||
escape(&unescaped_string, &escaped_string);
|
escape(&unescaped_string, &escaped_string, LADISH_ESCAPE_FLAG_ALL);
|
||||||
*escaped_string = 0;
|
*escaped_string = 0;
|
||||||
if (!ladish_write_string(fd, escaped_buffer))
|
if (!ladish_write_string(fd, escaped_buffer))
|
||||||
{
|
{
|
||||||
|
|
|
@ -44,6 +44,7 @@ struct ladish_write_context
|
||||||
bool ladish_write_string(int fd, const char * string);
|
bool ladish_write_string(int fd, const char * string);
|
||||||
bool ladish_write_indented_string(int fd, int indent, const char * string);
|
bool ladish_write_indented_string(int fd, int indent, const char * string);
|
||||||
bool ladish_write_string_escape(int fd, const char * string);
|
bool ladish_write_string_escape(int fd, const char * string);
|
||||||
|
bool ladish_write_string_escape_ex(int fd, const char * string, unsigned int flags);
|
||||||
bool ladish_write_dict(int fd, int indent, ladish_dict_handle dict);
|
bool ladish_write_dict(int fd, int indent, ladish_dict_handle dict);
|
||||||
bool ladish_write_vgraph(int fd, int indent, ladish_graph_handle vgraph, ladish_app_supervisor_handle app_supervisor);
|
bool ladish_write_vgraph(int fd, int indent, ladish_graph_handle vgraph, ladish_app_supervisor_handle app_supervisor);
|
||||||
bool ladish_write_room_link_ports(int fd, int indent, ladish_room_handle room);
|
bool ladish_write_room_link_ports(int fd, int indent, ladish_room_handle room);
|
||||||
|
|
|
@ -733,7 +733,7 @@ bool ladish_studio_compose_filename(const char * name, char ** filename_ptr_ptr,
|
||||||
*p++ = '/';
|
*p++ = '/';
|
||||||
|
|
||||||
src = name;
|
src = name;
|
||||||
escape(&src, &p);
|
escape(&src, &p, LADISH_ESCAPE_FLAG_ALL);
|
||||||
strcpy(p, ".xml");
|
strcpy(p, ".xml");
|
||||||
|
|
||||||
*filename_ptr_ptr = filename_ptr;
|
*filename_ptr_ptr = filename_ptr;
|
||||||
|
|
Loading…
Reference in New Issue