charset.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef GRUB_CHARSET_HEADER
  19. #define GRUB_CHARSET_HEADER 1
  20. #include <grub/types.h>
  21. #define GRUB_UINT8_1_LEADINGBIT 0x80
  22. #define GRUB_UINT8_2_LEADINGBITS 0xc0
  23. #define GRUB_UINT8_3_LEADINGBITS 0xe0
  24. #define GRUB_UINT8_4_LEADINGBITS 0xf0
  25. #define GRUB_UINT8_5_LEADINGBITS 0xf8
  26. #define GRUB_UINT8_6_LEADINGBITS 0xfc
  27. #define GRUB_UINT8_7_LEADINGBITS 0xfe
  28. #define GRUB_UINT8_1_TRAILINGBIT 0x01
  29. #define GRUB_UINT8_2_TRAILINGBITS 0x03
  30. #define GRUB_UINT8_3_TRAILINGBITS 0x07
  31. #define GRUB_UINT8_4_TRAILINGBITS 0x0f
  32. #define GRUB_UINT8_5_TRAILINGBITS 0x1f
  33. #define GRUB_UINT8_6_TRAILINGBITS 0x3f
  34. #define GRUB_UCS2_LIMIT 0x10000
  35. #define GRUB_UTF16_UPPER_SURROGATE(code) \
  36. (0xD800 + ((((code) - GRUB_UCS2_LIMIT) >> 12) & 0xfff))
  37. #define GRUB_UTF16_LOWER_SURROGATE(code) \
  38. (0xDC00 + (((code) - GRUB_UCS2_LIMIT) & 0xfff))
  39. grub_ssize_t
  40. grub_utf8_to_utf16 (grub_uint16_t *dest, grub_size_t destsize,
  41. const grub_uint8_t *src, grub_size_t srcsize,
  42. const grub_uint8_t **srcend);
  43. /* Convert UTF-16 to UTF-8. */
  44. static inline grub_uint8_t *
  45. grub_utf16_to_utf8 (grub_uint8_t *dest, grub_uint16_t *src,
  46. grub_size_t size)
  47. {
  48. grub_uint32_t code_high = 0;
  49. while (size--)
  50. {
  51. grub_uint32_t code = *src++;
  52. if (code_high)
  53. {
  54. if (code >= 0xDC00 && code <= 0xDFFF)
  55. {
  56. /* Surrogate pair. */
  57. code = ((code_high - 0xD800) << 12) + (code - 0xDC00) + 0x10000;
  58. *dest++ = (code >> 18) | 0xF0;
  59. *dest++ = ((code >> 12) & 0x3F) | 0x80;
  60. *dest++ = ((code >> 6) & 0x3F) | 0x80;
  61. *dest++ = (code & 0x3F) | 0x80;
  62. }
  63. else
  64. {
  65. /* Error... */
  66. *dest++ = '?';
  67. }
  68. code_high = 0;
  69. }
  70. else
  71. {
  72. if (code <= 0x007F)
  73. *dest++ = code;
  74. else if (code <= 0x07FF)
  75. {
  76. *dest++ = (code >> 6) | 0xC0;
  77. *dest++ = (code & 0x3F) | 0x80;
  78. }
  79. else if (code >= 0xD800 && code <= 0xDBFF)
  80. {
  81. code_high = code;
  82. continue;
  83. }
  84. else if (code >= 0xDC00 && code <= 0xDFFF)
  85. {
  86. /* Error... */
  87. *dest++ = '?';
  88. }
  89. else
  90. {
  91. *dest++ = (code >> 12) | 0xE0;
  92. *dest++ = ((code >> 6) & 0x3F) | 0x80;
  93. *dest++ = (code & 0x3F) | 0x80;
  94. }
  95. }
  96. }
  97. return dest;
  98. }
  99. /* Convert UCS-4 to UTF-8. */
  100. char *grub_ucs4_to_utf8_alloc (grub_uint32_t *src, grub_size_t size);
  101. int
  102. grub_is_valid_utf8 (const grub_uint8_t *src, grub_size_t srcsize);
  103. int grub_utf8_to_ucs4_alloc (const char *msg, grub_uint32_t **unicode_msg,
  104. grub_uint32_t **last_position);
  105. #endif