Utf16Decoder.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @file Utf16Decoder.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_UTF16DECODER_H
  30. #define BADVPN_UTF16DECODER_H
  31. #include <stdint.h>
  32. #include <misc/debug.h>
  33. /**
  34. * Decodes UTF-16 data into Unicode characters.
  35. */
  36. typedef struct {
  37. int cont;
  38. uint32_t ch;
  39. } Utf16Decoder;
  40. /**
  41. * Initializes the UTF-16 decoder.
  42. *
  43. * @param o the object
  44. */
  45. static void Utf16Decoder_Init (Utf16Decoder *o);
  46. /**
  47. * Inputs a 16-bit value to the decoder.
  48. *
  49. * @param o the object
  50. * @param b 16-bit value to input
  51. * @param out_ch will receive a Unicode character if this function returns 1.
  52. * If written, the character will be in the range 0 - 0x10FFFF,
  53. * excluding the surrogate range 0xD800 - 0xDFFF.
  54. * @return 1 if a Unicode character has been written to *out_ch, 0 if not
  55. */
  56. static int Utf16Decoder_Input (Utf16Decoder *o, uint16_t b, uint32_t *out_ch);
  57. void Utf16Decoder_Init (Utf16Decoder *o)
  58. {
  59. o->cont = 0;
  60. }
  61. int Utf16Decoder_Input (Utf16Decoder *o, uint16_t b, uint32_t *out_ch)
  62. {
  63. // high surrogate
  64. if (b >= UINT16_C(0xD800) && b <= UINT16_C(0xDBFF)) {
  65. // set continuation state
  66. o->cont = 1;
  67. // add high bits
  68. o->ch = (uint32_t)(b - UINT16_C(0xD800)) << 10;
  69. return 0;
  70. }
  71. // low surrogate
  72. if (b >= UINT16_C(0xDC00) && b <= UINT16_C(0xDFFF)) {
  73. // check continuation
  74. if (!o->cont) {
  75. return 0;
  76. }
  77. // add low bits
  78. o->ch |= (b - UINT16_C(0xDC00));
  79. // reset state
  80. o->cont = 0;
  81. // don't report surrogates
  82. if (o->ch >= UINT32_C(0xD800) && o->ch <= UINT32_C(0xDFFF)) {
  83. return 0;
  84. }
  85. // return character
  86. *out_ch = o->ch;
  87. return 1;
  88. }
  89. // reset state
  90. o->cont = 0;
  91. // return character
  92. *out_ch = b;
  93. return 1;
  94. }
  95. #endif