ConvertUTF.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2001-2004 Unicode, Inc.
  3. *
  4. * Disclaimer
  5. *
  6. * This source code is provided as is by Unicode, Inc. No claims are
  7. * made as to fitness for any particular purpose. No warranties of any
  8. * kind are expressed or implied. The recipient agrees to determine
  9. * applicability of information provided. If this file has been
  10. * purchased on magnetic or optical media from Unicode, Inc., the
  11. * sole remedy for any claim will be exchange of defective media
  12. * within 90 days of receipt.
  13. *
  14. * Limitations on Rights to Redistribute This Code
  15. *
  16. * Unicode, Inc. hereby grants the right to freely use the information
  17. * supplied in this file in the creation of products supporting the
  18. * Unicode Standard, and to make copies of this file in any form
  19. * for internal or external distribution as long as this notice
  20. * remains attached.
  21. */
  22. #ifndef CONVERTUTF_H
  23. #define CONVERTUTF_H
  24. /* ---------------------------------------------------------------------
  25. Conversions between UTF32, UTF-16, and UTF-8. Header file.
  26. Several funtions are included here, forming a complete set of
  27. conversions between the three formats. UTF-7 is not included
  28. here, but is handled in a separate source file.
  29. Each of these routines takes pointers to input buffers and output
  30. buffers. The input buffers are const.
  31. Each routine converts the text between *sourceStart and sourceEnd,
  32. putting the result into the buffer between *targetStart and
  33. targetEnd. Note: the end pointers are *after* the last item: e.g.
  34. *(sourceEnd - 1) is the last item.
  35. The return result indicates whether the conversion was successful,
  36. and if not, whether the problem was in the source or target buffers.
  37. (Only the first encountered problem is indicated.)
  38. After the conversion, *sourceStart and *targetStart are both
  39. updated to point to the end of last text successfully converted in
  40. the respective buffers.
  41. Input parameters:
  42. sourceStart - pointer to a pointer to the source buffer.
  43. The contents of this are modified on return so that
  44. it points at the next thing to be converted.
  45. targetStart - similarly, pointer to pointer to the target buffer.
  46. sourceEnd, targetEnd - respectively pointers to the ends of the
  47. two buffers, for overflow checking only.
  48. These conversion functions take a ConversionFlags argument. When this
  49. flag is set to strict, both irregular sequences and isolated surrogates
  50. will cause an error. When the flag is set to lenient, both irregular
  51. sequences and isolated surrogates are converted.
  52. Whether the flag is strict or lenient, all illegal sequences will cause
  53. an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
  54. or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
  55. must check for illegal sequences.
  56. When the flag is set to lenient, characters over 0x10FFFF are converted
  57. to the replacement character; otherwise (when the flag is set to strict)
  58. they constitute an error.
  59. Output parameters:
  60. The value "sourceIllegal" is returned from some routines if the input
  61. sequence is malformed. When "sourceIllegal" is returned, the source
  62. value will point to the illegal value that caused the problem. E.g.,
  63. in UTF-8 when a sequence is malformed, it points to the start of the
  64. malformed sequence.
  65. Author: Mark E. Davis, 1994.
  66. Rev History: Rick McGowan, fixes & updates May 2001.
  67. Fixes & updates, Sept 2001.
  68. ------------------------------------------------------------------------ */
  69. /* ---------------------------------------------------------------------
  70. The following 4 definitions are compiler-specific.
  71. The C standard does not guarantee that wchar_t has at least
  72. 16 bits, so wchar_t is no less portable than unsigned short!
  73. All should be unsigned values to avoid sign extension during
  74. bit mask & shift operations.
  75. ------------------------------------------------------------------------ */
  76. typedef unsigned long UTF32; /* at least 32 bits */
  77. typedef unsigned short UTF16; /* at least 16 bits */
  78. typedef unsigned char UTF8; /* typically 8 bits */
  79. typedef unsigned char Boolean; /* 0 or 1 */
  80. /* Some fundamental constants */
  81. #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
  82. #define UNI_MAX_BMP (UTF32)0x0000FFFF
  83. #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
  84. #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
  85. #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
  86. typedef enum {
  87. conversionOK, /* conversion successful */
  88. sourceExhausted, /* partial character in source, but hit end */
  89. targetExhausted, /* insuff. room in target for conversion */
  90. sourceIllegal /* source sequence is illegal/malformed */
  91. } ConversionResult;
  92. typedef enum {
  93. strictConversion = 0,
  94. lenientConversion
  95. } ConversionFlags;
  96. /* This is for C++ and does no harm in C */
  97. #ifdef __cplusplus
  98. extern "C" {
  99. #endif
  100. ConversionResult ConvertUTF8toUTF16 (
  101. const UTF8** sourceStart, const UTF8* sourceEnd,
  102. UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
  103. ConversionResult ConvertUTF16toUTF8 (
  104. const UTF16** sourceStart, const UTF16* sourceEnd,
  105. UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
  106. ConversionResult ConvertUTF8toUTF32 (
  107. const UTF8** sourceStart, const UTF8* sourceEnd,
  108. UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
  109. ConversionResult ConvertUTF32toUTF8 (
  110. const UTF32** sourceStart, const UTF32* sourceEnd,
  111. UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
  112. ConversionResult ConvertUTF16toUTF32 (
  113. const UTF16** sourceStart, const UTF16* sourceEnd,
  114. UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
  115. ConversionResult ConvertUTF32toUTF16 (
  116. const UTF32** sourceStart, const UTF32* sourceEnd,
  117. UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
  118. Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. /* --------------------------------------------------------------------- */
  123. #endif // CONVERTUTF_H