utf8_decode.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright (c) 2008-2010 Bjoern Hoehrmann <[email protected]>
  3. *
  4. * MIT License
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
  24. */
  25. #ifdef EMBEDDED_UTF8_DECODE
  26. #ifndef _UTF8_DECODE_H_
  27. #define _UTF8_DECODE_H_
  28. #include <stdint.h>
  29. #include <stddef.h>
  30. #define UTF8_ACCEPT 0
  31. #define UTF8_REJECT 12
  32. static const uint8_t utf8d[] = {
  33. // The first part of the table maps bytes to character classes that
  34. // to reduce the size of the transition table and create bitmasks.
  35. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  36. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  37. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  38. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  39. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  40. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  41. 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  42. 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
  43. // The second part is a transition table that maps a combination
  44. // of a state of the automaton and a character class to a state.
  45. 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
  46. 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
  47. 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
  48. 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
  49. 12,36,12,12,12,12,12,12,12,12,12,12,
  50. };
  51. static inline uint32_t decode(uint32_t* state, uint32_t* codep, uint32_t byte)
  52. {
  53. uint32_t type = utf8d[byte];
  54. *codep = (*state != UTF8_ACCEPT) ?
  55. (byte & 0x3fu) | (*codep << 6) :
  56. (0xff >> type) & (byte);
  57. *state = utf8d[256 + *state + type];
  58. return *state;
  59. }
  60. static inline int IsUTF8(uint8_t* s, size_t len)
  61. {
  62. uint32_t codepoint, state = 0;
  63. while (len--)
  64. decode(&state, &codepoint, *s++);
  65. return state == UTF8_ACCEPT;
  66. }
  67. #endif
  68. #endif