xmltok_ns.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
  2. See the file COPYING for copying permission.
  3. */
  4. /* This file is included! */
  5. #ifdef XML_TOK_NS_C
  6. const ENCODING *
  7. NS(XmlGetUtf8InternalEncoding)(void)
  8. {
  9. return &ns(internal_utf8_encoding).enc;
  10. }
  11. const ENCODING *
  12. NS(XmlGetUtf16InternalEncoding)(void)
  13. {
  14. #if BYTEORDER == 1234
  15. return &ns(internal_little2_encoding).enc;
  16. #elif BYTEORDER == 4321
  17. return &ns(internal_big2_encoding).enc;
  18. #else
  19. const short n = 1;
  20. return (*(const char *)&n
  21. ? &ns(internal_little2_encoding).enc
  22. : &ns(internal_big2_encoding).enc);
  23. #endif
  24. }
  25. static const ENCODING * const NS(encodings)[] = {
  26. &ns(latin1_encoding).enc,
  27. &ns(ascii_encoding).enc,
  28. &ns(utf8_encoding).enc,
  29. &ns(big2_encoding).enc,
  30. &ns(big2_encoding).enc,
  31. &ns(little2_encoding).enc,
  32. &ns(utf8_encoding).enc /* NO_ENC */
  33. };
  34. static int PTRCALL
  35. NS(initScanProlog)(const ENCODING *enc, const char *ptr, const char *end,
  36. const char **nextTokPtr)
  37. {
  38. return initScan(NS(encodings), (const INIT_ENCODING *)enc,
  39. XML_PROLOG_STATE, ptr, end, nextTokPtr);
  40. }
  41. static int PTRCALL
  42. NS(initScanContent)(const ENCODING *enc, const char *ptr, const char *end,
  43. const char **nextTokPtr)
  44. {
  45. return initScan(NS(encodings), (const INIT_ENCODING *)enc,
  46. XML_CONTENT_STATE, ptr, end, nextTokPtr);
  47. }
  48. int
  49. NS(XmlInitEncoding)(INIT_ENCODING *p, const ENCODING **encPtr,
  50. const char *name)
  51. {
  52. int i = getEncodingIndex(name);
  53. if (i == UNKNOWN_ENC)
  54. return 0;
  55. SET_INIT_ENC_INDEX(p, i);
  56. p->initEnc.scanners[XML_PROLOG_STATE] = NS(initScanProlog);
  57. p->initEnc.scanners[XML_CONTENT_STATE] = NS(initScanContent);
  58. p->initEnc.updatePosition = initUpdatePosition;
  59. p->encPtr = encPtr;
  60. *encPtr = &(p->initEnc);
  61. return 1;
  62. }
  63. static const ENCODING *
  64. NS(findEncoding)(const ENCODING *enc, const char *ptr, const char *end)
  65. {
  66. #define ENCODING_MAX 128
  67. char buf[ENCODING_MAX];
  68. char *p = buf;
  69. int i;
  70. XmlUtf8Convert(enc, &ptr, end, &p, p + ENCODING_MAX - 1);
  71. if (ptr != end)
  72. return 0;
  73. *p = 0;
  74. if (streqci(buf, KW_UTF_16) && enc->minBytesPerChar == 2)
  75. return enc;
  76. i = getEncodingIndex(buf);
  77. if (i == UNKNOWN_ENC)
  78. return 0;
  79. return NS(encodings)[i];
  80. }
  81. int
  82. NS(XmlParseXmlDecl)(int isGeneralTextEntity,
  83. const ENCODING *enc,
  84. const char *ptr,
  85. const char *end,
  86. const char **badPtr,
  87. const char **versionPtr,
  88. const char **versionEndPtr,
  89. const char **encodingName,
  90. const ENCODING **encoding,
  91. int *standalone)
  92. {
  93. return doParseXmlDecl(NS(findEncoding),
  94. isGeneralTextEntity,
  95. enc,
  96. ptr,
  97. end,
  98. badPtr,
  99. versionPtr,
  100. versionEndPtr,
  101. encodingName,
  102. encoding,
  103. standalone);
  104. }
  105. #endif /* XML_TOK_NS_C */