Utf16Decoder.h 2.6 KB

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