Skip to content
Permalink
Browse files
Fix guththila serialization of special chars in attr
JIRA: AXIS2C-1627
  • Loading branch information
bblough committed May 2, 2018
1 parent 3db7676 commit cc11414fa7c11a66443b7c48994377d851450819
Showing 4 changed files with 97 additions and 43 deletions.
@@ -259,6 +259,19 @@ GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_escape_character(
char *buff,
const axutil_env_t * env);

/*
* Write a buffer with special chars on it
* The special chars will be escaped
* @param wr pointer to the writer
* @param buff character string
* @param env pointer to the environment
*/
GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_escaped_buffer(
guththila_xml_writer_t * wr,
char *buff,
const axutil_env_t * env);


/*
* Start to write an empty element with the given name.
* @param wr pointer to the writer
@@ -725,7 +725,7 @@ guththila_write_characters(
guththila_char_t *buff,
const axutil_env_t * env)
{
size_t len = strlen(buff);

if(wr->status == START)
{
wr->status = BEGINING;
@@ -741,39 +741,7 @@ guththila_write_characters(
{
return GUTHTHILA_FAILURE;
}
while(len > 0)
{
size_t i = 0;
/* scan buffer until the next special character (&, <, >, ', ") these need to be escaped,
* otherwise XML will not be valid*/
guththila_char_t *pos = (guththila_char_t*)strpbrk(buff, "&<>'\"");
if(pos)
{
i = pos - buff;
}
else
{
i = len;
}

/* write everything until the special character */
if(i > 0)
{
guththila_write(wr, buff, i, env);
buff += i;
len -= i;
}
/* replace the character with the appropriate sequence */
if(len > 0)
{
if(AXIS2_SUCCESS != guththila_write_escape_character(wr, buff, env))
return GUTHTHILA_FAILURE;
/* skip the character */
buff++;
len--;
}
}
return GUTHTHILA_SUCCESS;
return guththila_write_escaped_buffer(wr,buff,env);
}

GUTHTHILA_EXPORT int GUTHTHILA_CALL
@@ -841,6 +809,50 @@ guththila_write_escape_character(
return GUTHTHILA_SUCCESS;
}

GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_write_escaped_buffer(
guththila_xml_writer_t * wr,
char *buff,
const axutil_env_t * env)
{
size_t len = strlen(buff);

while(len > 0)
{
size_t i = 0;
/* scan buffer until the next special character (&, <, >, ', ") these need to be escaped,
* otherwise XML will not be valid*/
guththila_char_t *pos = (guththila_char_t*)strpbrk(buff, "&<>'\"");
if(pos)
{
i = pos - buff;
}
else
{
i = len;
}

/* write everything until the special character */
if(i > 0)
{
guththila_write(wr, buff, i, env);
buff += i;
len -= i;
}
/* replace the character with the appropriate sequence */
if(len > 0)
{
if(AXIS2_SUCCESS != guththila_write_escape_character(wr, buff, env))
return GUTHTHILA_FAILURE;
/* skip the character */
buff++;
len--;
}
}
return GUTHTHILA_SUCCESS;

}

GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_write_empty_element(
guththila_xml_writer_t * wr,
@@ -1087,7 +1099,8 @@ guththila_write_attribute(
guththila_write(wr, " ", 1u, env);
guththila_write(wr, localname, strlen(localname), env);
guththila_write(wr, "=\"", 2u, env);
guththila_write(wr, value, strlen(value), env);
if(guththila_write_escaped_buffer(wr, value, env)!=GUTHTHILA_SUCCESS)
return GUTHTHILA_FAILURE;
guththila_write(wr, "\"", 1u, env);
return GUTHTHILA_SUCCESS;
}
@@ -1147,7 +1160,8 @@ guththila_write_attribute_with_prefix(
guththila_write(wr, ":", 1u, env);
guththila_write(wr, localname, strlen(localname), env);
guththila_write(wr, "=\"", 2u, env);
guththila_write(wr, value, strlen(value), env);
if(guththila_write_escaped_buffer(wr, value, env)!=GUTHTHILA_SUCCESS)
return GUTHTHILA_FAILURE;
guththila_write(wr, "\"", 1u, env);
return GUTHTHILA_SUCCESS;
}
@@ -1194,7 +1208,8 @@ guththila_write_attribute_with_namespace(
guththila_write(wr, ":", 1u, env);
guththila_write(wr, loc_name, strlen(loc_name), env);
guththila_write(wr, "=\"", 2u, env);
guththila_write(wr, value, strlen(value), env);
if(guththila_write_escaped_buffer(wr, value, env)!=GUTHTHILA_SUCCESS)
return GUTHTHILA_FAILURE;
guththila_write(wr, "\"", 1u, env);
return GUTHTHILA_SUCCESS;
}
@@ -20,7 +20,8 @@ SUBDIRS =
test_guththila_SOURCES = test_guththila.cc

AM_CPPFLAGS = -I$(top_builddir)/include \
-I ../../util/include
-I ../../util/include \
-I ../../axiom/include

test_guththila_LDADD = \
$(top_builddir)/src/libguththila.la \
@@ -33,5 +34,6 @@ test_attribute_SOURCES = test_attribute.cc
test_attribute_LDADD = \
$(top_builddir)/src/libguththila.la \
../../util/src/libaxutil.la \
../../axiom/src/om/libaxis2_axiom.la \
$(top_builddir)/$(GTEST)/libgtest.a \
$(top_builddir)/$(GTEST)/libgtest_main.a
@@ -18,6 +18,7 @@
#include <gtest/gtest.h>

#include <guththila.h>
#include <axiom.h>


#define BUF_SIZE 256
@@ -43,15 +44,11 @@ class TestAttribute: public ::testing::Test
void TearDown()
{

guththila_reader_free(m_reader, m_env);
m_reader = nullptr;
guththila_un_init(m_parser, m_env);
m_parser = nullptr;
axutil_env_free(m_env);

}

guththila_reader_t *m_reader;
guththila_reader_t *m_reader = nullptr;
axutil_allocator_t *m_allocator;
axutil_env_t *m_env;
guththila_t *m_parser;
@@ -85,6 +82,11 @@ TEST_F(TestAttribute, test_attribute) {
ASSERT_STREQ(guththila_get_attribute_name(m_parser, att, m_env), "color");
ASSERT_STREQ(guththila_get_attribute_value(m_parser, att, m_env), "brown");

guththila_reader_free(m_reader, m_env);
m_reader = nullptr;
guththila_un_init(m_parser, m_env);
m_parser = nullptr;

}


@@ -129,4 +131,26 @@ TEST_F(TestAttribute, test_attribute_prefix) {
ASSERT_STREQ(guththila_get_attribute_name(m_parser, att, m_env), "mustUnderstand");
ASSERT_STREQ(guththila_get_attribute_value(m_parser, att, m_env), "0");

guththila_reader_free(m_reader, m_env);
m_reader = nullptr;
guththila_un_init(m_parser, m_env);
m_parser = nullptr;

}

/* AXIS2C-1627 */
TEST_F(TestAttribute, test_special_char_serialization)
{
axiom_namespace_t * ns = axiom_namespace_create(m_env, "namespace", "ns");

axiom_node_t * node;
axiom_element_t * element = axiom_element_create(m_env, NULL, "el", ns, &node);

axiom_element_set_text(element, m_env, "T1 & T2", node);
axiom_element_add_attribute(element, m_env, axiom_attribute_create(m_env, "name", "A1 & A2", NULL), node);

axis2_char_t * xml = axiom_node_to_string(node, m_env);

ASSERT_STREQ(xml, "<ns:el xmlns:ns=\"namespace\" name=\"A1 &amp; A2\">T1 &amp; T2</ns:el>");

}

0 comments on commit cc11414

Please sign in to comment.