xmltok_ns.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* This file is included!
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000 Clark Cooper <coopercc@users.sourceforge.net>
  10. Copyright (c) 2002 Greg Stein <gstein@users.sourceforge.net>
  11. Copyright (c) 2002 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
  12. Copyright (c) 2002-2006 Karl Waclawek <karl@waclawek.net>
  13. Copyright (c) 2017-2021 Sebastian Pipping <sebastian@pipping.org>
  14. Licensed under the MIT license:
  15. Permission is hereby granted, free of charge, to any person obtaining
  16. a copy of this software and associated documentation files (the
  17. "Software"), to deal in the Software without restriction, including
  18. without limitation the rights to use, copy, modify, merge, publish,
  19. distribute, sublicense, and/or sell copies of the Software, and to permit
  20. persons to whom the Software is furnished to do so, subject to the
  21. following conditions:
  22. The above copyright notice and this permission notice shall be included
  23. in all copies or substantial portions of the Software.
  24. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  27. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  28. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  29. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  30. USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. */
  32. #ifdef XML_TOK_NS_C
  33. const ENCODING *
  34. NS(XmlGetUtf8InternalEncoding)(void) {
  35. return &ns(internal_utf8_encoding).enc;
  36. }
  37. const ENCODING *
  38. NS(XmlGetUtf16InternalEncoding)(void) {
  39. # if BYTEORDER == 1234
  40. return &ns(internal_little2_encoding).enc;
  41. # elif BYTEORDER == 4321
  42. return &ns(internal_big2_encoding).enc;
  43. # else
  44. const short n = 1;
  45. return (*(const char *)&n ? &ns(internal_little2_encoding).enc
  46. : &ns(internal_big2_encoding).enc);
  47. # endif
  48. }
  49. static const ENCODING *const NS(encodings)[] = {
  50. &ns(latin1_encoding).enc, &ns(ascii_encoding).enc,
  51. &ns(utf8_encoding).enc, &ns(big2_encoding).enc,
  52. &ns(big2_encoding).enc, &ns(little2_encoding).enc,
  53. &ns(utf8_encoding).enc /* NO_ENC */
  54. };
  55. static int PTRCALL
  56. NS(initScanProlog)(const ENCODING *enc, const char *ptr, const char *end,
  57. const char **nextTokPtr) {
  58. return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_PROLOG_STATE,
  59. ptr, end, nextTokPtr);
  60. }
  61. static int PTRCALL
  62. NS(initScanContent)(const ENCODING *enc, const char *ptr, const char *end,
  63. const char **nextTokPtr) {
  64. return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_CONTENT_STATE,
  65. ptr, end, nextTokPtr);
  66. }
  67. int
  68. NS(XmlInitEncoding)(INIT_ENCODING *p, const ENCODING **encPtr,
  69. const char *name) {
  70. int i = getEncodingIndex(name);
  71. if (i == UNKNOWN_ENC)
  72. return 0;
  73. SET_INIT_ENC_INDEX(p, i);
  74. p->initEnc.scanners[XML_PROLOG_STATE] = NS(initScanProlog);
  75. p->initEnc.scanners[XML_CONTENT_STATE] = NS(initScanContent);
  76. p->initEnc.updatePosition = initUpdatePosition;
  77. p->encPtr = encPtr;
  78. *encPtr = &(p->initEnc);
  79. return 1;
  80. }
  81. static const ENCODING *
  82. NS(findEncoding)(const ENCODING *enc, const char *ptr, const char *end) {
  83. # define ENCODING_MAX 128
  84. char buf[ENCODING_MAX] = "";
  85. char *p = buf;
  86. int i;
  87. XmlUtf8Convert(enc, &ptr, end, &p, p + ENCODING_MAX - 1);
  88. if (ptr != end)
  89. return 0;
  90. *p = 0;
  91. if (streqci(buf, KW_UTF_16) && enc->minBytesPerChar == 2)
  92. return enc;
  93. i = getEncodingIndex(buf);
  94. if (i == UNKNOWN_ENC)
  95. return 0;
  96. return NS(encodings)[i];
  97. }
  98. int
  99. NS(XmlParseXmlDecl)(int isGeneralTextEntity, const ENCODING *enc,
  100. const char *ptr, const char *end, const char **badPtr,
  101. const char **versionPtr, const char **versionEndPtr,
  102. const char **encodingName, const ENCODING **encoding,
  103. int *standalone) {
  104. return doParseXmlDecl(NS(findEncoding), isGeneralTextEntity, enc, ptr, end,
  105. badPtr, versionPtr, versionEndPtr, encodingName,
  106. encoding, standalone);
  107. }
  108. #endif /* XML_TOK_NS_C */