⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml.c

📁 subversion-1.4.3-1.tar.gz 配置svn的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * xml.c:  xml helper code shared among the Subversion libraries. * * ==================================================================== * Copyright (c) 2000-2006 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"#include "svn_error.h"#include "svn_ctype.h"#include "utf_impl.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 ***/svn_boolean_tsvn_xml_is_xml_safe(const char *data, apr_size_t len){  const char *end = data + len;  const char *p;  if (! svn_utf__is_valid(data, len))    return FALSE;  for (p = data; p < end; p++)    {      unsigned char c = *p;      if (svn_ctype_iscntrl(c))        {          if ((c != SVN_CTYPE_ASCII_TAB)              && (c != SVN_CTYPE_ASCII_LINEFEED)              && (c != SVN_CTYPE_ASCII_CARRIAGERETURN)              && (c != SVN_CTYPE_ASCII_DELETE))            return FALSE;        }    }  return TRUE;}/*** XML escaping. ***/static voidxml_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, "&amp;");      else if (*q == '<')        svn_stringbuf_appendcstr(*outstr, "&lt;");      else if (*q == '>')        svn_stringbuf_appendcstr(*outstr, "&gt;");      else if (*q == '\r')        svn_stringbuf_appendcstr(*outstr, "&#13;");      p = q + 1;    }}/* Essentially the same as xml_escape_cdata, with the addition of   whitespace and quote characters. */static voidxml_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, "&amp;");      else if (*q == '<')        svn_stringbuf_appendcstr(*outstr, "&lt;");      else if (*q == '>')        svn_stringbuf_appendcstr(*outstr, "&gt;");      else if (*q == '"')        svn_stringbuf_appendcstr(*outstr, "&quot;");      else if (*q == '\'')        svn_stringbuf_appendcstr(*outstr, "&apos;");      else if (*q == '\r')        svn_stringbuf_appendcstr(*outstr, "&#13;");      else if (*q == '\n')        svn_stringbuf_appendcstr(*outstr, "&#10;");      else if (*q == '\t')        svn_stringbuf_appendcstr(*outstr, "&#9;");      p = q + 1;    }}voidsvn_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);}voidsvn_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);}voidsvn_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);}voidsvn_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);}voidsvn_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);}voidsvn_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);}const char *svn_xml_fuzzy_escape(const char *string, apr_pool_t *pool){  const char *end = string + strlen(string);  const char *p = string, *q;  svn_stringbuf_t *outstr;  char escaped_char[6];   /* ? \ u u u \0 */  for (q = p; q < end; q++)    {      if (svn_ctype_iscntrl(*q)          && ! ((*q == '\n') || (*q == '\r') || (*q == '\t')))        break;    }  /* Return original string if no unsafe characters found. */  if (q == end)    return string;  outstr = svn_stringbuf_create("", pool);  while (1)    {      q = p;      /* Traverse till either unsafe character or eos. */      while ((q < end)             && ((! svn_ctype_iscntrl(*q))                 || (*q == '\n') || (*q == '\r') || (*q == '\t')))        q++;      /* copy chunk before marker */      svn_stringbuf_appendbytes(outstr, p, q - p);      if (q == end)        break;      /* Append an escaped version of the unsafe character.                  ### This format was chosen for consistency with         ### svn_utf__cstring_from_utf8_fuzzy().  The two functions         ### should probably share code, even though they escape         ### different characters.      */      sprintf(escaped_char, "?\\%03u", (unsigned char) *q);      svn_stringbuf_appendcstr(outstr, escaped_char);      p = q + 1;    }  return outstr->data;}/*** 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;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -