Utf8Decoder.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file Utf8Decoder.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_UTF8DECODER_H
  30. #define BADVPN_UTF8DECODER_H
  31. #include <stdint.h>
  32. #include <misc/debug.h>
  33. /**
  34. * Decodes UTF-8 data into Unicode characters.
  35. */
  36. typedef struct {
  37. int bytes;
  38. int pos;
  39. uint32_t ch;
  40. } Utf8Decoder;
  41. /**
  42. * Initializes the UTF-8 decoder.
  43. *
  44. * @param o the object
  45. */
  46. static void Utf8Decoder_Init (Utf8Decoder *o);
  47. /**
  48. * Inputs a byte to the decoder.
  49. *
  50. * @param o the object
  51. * @param b byte to input
  52. * @param out_ch will receive a Unicode character if this function returns 1.
  53. * If written, the character will be in the range 0 - 0x10FFFF,
  54. * excluding the surrogate range 0xD800 - 0xDFFF.
  55. * @return 1 if a Unicode character has been written to *out_ch, 0 if not
  56. */
  57. static int Utf8Decoder_Input (Utf8Decoder *o, uint8_t b, uint32_t *out_ch);
  58. void Utf8Decoder_Init (Utf8Decoder *o)
  59. {
  60. o->bytes = 0;
  61. }
  62. int Utf8Decoder_Input (Utf8Decoder *o, uint8_t b, uint32_t *out_ch)
  63. {
  64. // one-byte character
  65. if ((b & 128) == 0) {
  66. o->bytes = 0;
  67. *out_ch = b;
  68. return 1;
  69. }
  70. // start of two-byte character
  71. if ((b & 224) == 192) {
  72. o->bytes = 2;
  73. o->pos = 1;
  74. o->ch = (uint32_t)(b & 31) << 6;
  75. return 0;
  76. }
  77. // start of three-byte character
  78. if ((b & 240) == 224) {
  79. o->bytes = 3;
  80. o->pos = 1;
  81. o->ch = (uint32_t)(b & 15) << 12;
  82. return 0;
  83. }
  84. // start of four-byte character
  85. if ((b & 248) == 240) {
  86. o->bytes = 4;
  87. o->pos = 1;
  88. o->ch = (uint32_t)(b & 7) << 18;
  89. return 0;
  90. }
  91. // continuation of multi-byte character
  92. if ((b & 192) == 128 && o->bytes > 0) {
  93. ASSERT(o->bytes <= 4)
  94. ASSERT(o->pos > 0)
  95. ASSERT(o->pos < o->bytes)
  96. // add bits from this byte
  97. o->ch |= (uint32_t)(b & 63) << (6 * (o->bytes - o->pos - 1));
  98. // end of multi-byte character?
  99. if (o->pos == o->bytes - 1) {
  100. // reset state
  101. o->bytes = 0;
  102. // don't report out-of-range characters
  103. if (o->ch > UINT32_C(0x10FFFF)) {
  104. return 0;
  105. }
  106. // don't report surrogates
  107. if (o->ch >= UINT32_C(0xD800) && o->ch <= UINT32_C(0xDFFF)) {
  108. return 0;
  109. }
  110. *out_ch = o->ch;
  111. return 1;
  112. }
  113. // increment byte index
  114. o->pos++;
  115. return 0;
  116. }
  117. // error, reset state
  118. o->bytes = 0;
  119. return 0;
  120. }
  121. #endif