📄 xml.c
字号:
/*
* xml.c: xml helper code shared among the Subversion libraries.
*
* ====================================================================
* Copyright (c) 2000-2004 CollabNet. All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://subversion.tigris.org/license-1.html.
* If newer versions of this license are posted there, you may use a
* newer version instead, at your option.
*
* This software consists of voluntary contributions made by many
* individuals. For exact contribution history, see the revision
* history and logs, available at http://subversion.tigris.org/.
* ====================================================================
*/
#include <string.h>
#include <assert.h>
#include "svn_private_config.h" /* for SVN_HAVE_OLD_EXPAT */
#include "svn_pools.h"
#include "svn_xml.h"
#ifdef SVN_HAVE_OLD_EXPAT
#include "xmlparse.h"
#else
#include "expat.h"
#endif
#ifdef XML_UNICODE
#error Expat is unusable -- it has been compiled for wide characters
#endif
/* The private internals for a parser object. */
struct svn_xml_parser_t
{
/** the expat parser */
XML_Parser parser;
/** the SVN callbacks to call from the Expat callbacks */
svn_xml_start_elem start_handler;
svn_xml_end_elem end_handler;
svn_xml_char_data data_handler;
/** the user's baton for private data */
void *baton;
/** if non-@c NULL, an error happened while parsing */
svn_error_t *error;
/** where this object is allocated, so we can free it easily */
apr_pool_t *pool;
};
/*** XML character validation ***/
/* The values in this table represent validity of char input for
Subversion's XML encoders. Basically, we'll claim that we can
handle anything between 0x20 and 0x7F, plus 0x09, 0x0A, and
0x0D (the XML-safe whitespace chars). */
static const int xml_char_validity[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 64 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 128 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 192 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
svn_boolean_t
svn_xml_is_xml_safe (const char *data, apr_size_t len)
{
const char *end = data + len;
const char *p;
/* ### We could accept valid UTF-8 as being XML-safe, but what's wrong
### with being conservative? */
for (p = data; p < end; p++)
{
if (! xml_char_validity[(unsigned char)*p])
return FALSE;
}
return TRUE;
}
/*** XML escaping. ***/
static void
xml_escape_cdata (svn_stringbuf_t **outstr,
const char *data,
apr_size_t len,
apr_pool_t *pool)
{
const char *end = data + len;
const char *p = data, *q;
if (*outstr == NULL)
*outstr = svn_stringbuf_create ("", pool);
while (1)
{
/* Find a character which needs to be quoted and append bytes up
to that point. Strictly speaking, '>' only needs to be
quoted if it follows "]]", but it's easier to quote it all
the time.
So, why are we escaping '\r' here? Well, according to the
XML spec, '\r\n' gets converted to '\n' during XML parsing.
Also, any '\r' not followed by '\n' is converted to '\n'. By
golly, if we say we want to escape a '\r', we want to make
sure it remains a '\r'! */
q = p;
while (q < end && *q != '&' && *q != '<' && *q != '>' && *q != '\r')
q++;
svn_stringbuf_appendbytes (*outstr, p, q - p);
/* We may already be a winner. */
if (q == end)
break;
/* Append the entity reference for the character. */
if (*q == '&')
svn_stringbuf_appendcstr (*outstr, "&");
else if (*q == '<')
svn_stringbuf_appendcstr (*outstr, "<");
else if (*q == '>')
svn_stringbuf_appendcstr (*outstr, ">");
else if (*q == '\r')
svn_stringbuf_appendcstr (*outstr, " ");
p = q + 1;
}
}
/* Essentially the same as xml_escape_cdata, with the addition of
whitespace and quote characters. */
static void
xml_escape_attr (svn_stringbuf_t **outstr,
const char *data,
apr_size_t len,
apr_pool_t *pool)
{
const char *end = data + len;
const char *p = data, *q;
if (*outstr == NULL)
*outstr = svn_stringbuf_create ("", pool);
while (1)
{
/* Find a character which needs to be quoted and append bytes up
to that point. */
q = p;
while (q < end && *q != '&' && *q != '<' && *q != '>'
&& *q != '"' && *q != '\'' && *q != '\r'
&& *q != '\n' && *q != '\t')
q++;
svn_stringbuf_appendbytes (*outstr, p, q - p);
/* We may already be a winner. */
if (q == end)
break;
/* Append the entity reference for the character. */
if (*q == '&')
svn_stringbuf_appendcstr (*outstr, "&");
else if (*q == '<')
svn_stringbuf_appendcstr (*outstr, "<");
else if (*q == '>')
svn_stringbuf_appendcstr (*outstr, ">");
else if (*q == '"')
svn_stringbuf_appendcstr (*outstr, """);
else if (*q == '\'')
svn_stringbuf_appendcstr (*outstr, "'");
else if (*q == '\r')
svn_stringbuf_appendcstr (*outstr, " ");
else if (*q == '\n')
svn_stringbuf_appendcstr (*outstr, " ");
else if (*q == '\t')
svn_stringbuf_appendcstr (*outstr, "	");
p = q + 1;
}
}
void
svn_xml_escape_cdata_stringbuf (svn_stringbuf_t **outstr,
const svn_stringbuf_t *string,
apr_pool_t *pool)
{
xml_escape_cdata (outstr, string->data, string->len, pool);
}
void
svn_xml_escape_cdata_string (svn_stringbuf_t **outstr,
const svn_string_t *string,
apr_pool_t *pool)
{
xml_escape_cdata (outstr, string->data, string->len, pool);
}
void
svn_xml_escape_cdata_cstring (svn_stringbuf_t **outstr,
const char *string,
apr_pool_t *pool)
{
xml_escape_cdata (outstr, string, (apr_size_t) strlen (string), pool);
}
void
svn_xml_escape_attr_stringbuf (svn_stringbuf_t **outstr,
const svn_stringbuf_t *string,
apr_pool_t *pool)
{
xml_escape_attr (outstr, string->data, string->len, pool);
}
void
svn_xml_escape_attr_string (svn_stringbuf_t **outstr,
const svn_string_t *string,
apr_pool_t *pool)
{
xml_escape_attr (outstr, string->data, string->len, pool);
}
void
svn_xml_escape_attr_cstring (svn_stringbuf_t **outstr,
const char *string,
apr_pool_t *pool)
{
xml_escape_attr (outstr, string, (apr_size_t) strlen (string), pool);
}
/*** Map from the Expat callback types to the SVN XML types. ***/
static void expat_start_handler(void *userData,
const XML_Char *name,
const XML_Char **atts)
{
svn_xml_parser_t *svn_parser = userData;
(*svn_parser->start_handler)(svn_parser->baton, name, atts);
}
static void expat_end_handler(void *userData, const XML_Char *name)
{
svn_xml_parser_t *svn_parser = userData;
(*svn_parser->end_handler)(svn_parser->baton, name);
}
static void expat_data_handler(void *userData, const XML_Char *s, int len)
{
svn_xml_parser_t *svn_parser = userData;
(*svn_parser->data_handler)(svn_parser->baton, s, (apr_size_t)len);
}
/*** Making a parser. ***/
svn_xml_parser_t *
svn_xml_make_parser (void *baton,
svn_xml_start_elem start_handler,
svn_xml_end_elem end_handler,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -