Utf8Decoder.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @file Utf8Decoder.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #ifndef BADVPN_UTF8DECODER_H
  23. #define BADVPN_UTF8DECODER_H
  24. #include <stdint.h>
  25. #include <misc/debug.h>
  26. /**
  27. * Decodes UTF-8 data into Unicode characters.
  28. */
  29. typedef struct {
  30. int bytes;
  31. int pos;
  32. uint32_t ch;
  33. } Utf8Decoder;
  34. /**
  35. * Initializes the UTF-8 decoder.
  36. *
  37. * @param o the object
  38. */
  39. static void Utf8Decoder_Init (Utf8Decoder *o);
  40. /**
  41. * Inputs a byte to the decoder.
  42. *
  43. * @param o the object
  44. * @param b byte to input
  45. * @param out_ch will receive a Unicode character if this function returns 1.
  46. * If written, the character will be in the range 0 - 0x10FFFF,
  47. * excluding the surrogate range 0xD800 - 0xDFFF.
  48. * @return 1 if a Unicode character has been written to *out_ch, 0 if not
  49. */
  50. static int Utf8Decoder_Input (Utf8Decoder *o, uint8_t b, uint32_t *out_ch);
  51. void Utf8Decoder_Init (Utf8Decoder *o)
  52. {
  53. o->bytes = 0;
  54. }
  55. int Utf8Decoder_Input (Utf8Decoder *o, uint8_t b, uint32_t *out_ch)
  56. {
  57. // one-byte character
  58. if ((b & 128) == 0) {
  59. o->bytes = 0;
  60. *out_ch = b;
  61. return 1;
  62. }
  63. // start of two-byte character
  64. if ((b & 224) == 192) {
  65. o->bytes = 2;
  66. o->pos = 1;
  67. o->ch = (uint32_t)(b & 31) << 6;
  68. return 0;
  69. }
  70. // start of three-byte character
  71. if ((b & 240) == 224) {
  72. o->bytes = 3;
  73. o->pos = 1;
  74. o->ch = (uint32_t)(b & 15) << 12;
  75. return 0;
  76. }
  77. // start of four-byte character
  78. if ((b & 248) == 240) {
  79. o->bytes = 4;
  80. o->pos = 1;
  81. o->ch = (uint32_t)(b & 7) << 18;
  82. return 0;
  83. }
  84. // continuation of multi-byte character
  85. if ((b & 192) == 128 && o->bytes > 0) {
  86. ASSERT(o->bytes <= 4)
  87. ASSERT(o->pos > 0)
  88. ASSERT(o->pos < o->bytes)
  89. // add bits from this byte
  90. o->ch |= (uint32_t)(b & 63) << (6 * (o->bytes - o->pos - 1));
  91. // end of multi-byte character?
  92. if (o->pos == o->bytes - 1) {
  93. // reset state
  94. o->bytes = 0;
  95. // don't report out-of-range characters
  96. if (o->ch > UINT32_C(0x10FFFF)) {
  97. return 0;
  98. }
  99. // don't report surrogates
  100. if (o->ch >= UINT32_C(0xD800) && o->ch <= UINT32_C(0xDFFF)) {
  101. return 0;
  102. }
  103. *out_ch = o->ch;
  104. return 1;
  105. }
  106. // increment byte index
  107. o->pos++;
  108. return 0;
  109. }
  110. // error, reset state
  111. o->bytes = 0;
  112. return 0;
  113. }
  114. #endif