utf-8.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2018 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. *******************************************************************************/
  16. /**
  17. * @file
  18. * \brief Functions for checking that strings contain UTF-8 characters only
  19. *
  20. * See page 104 of the Unicode Standard 5.0 for the list of well formed
  21. * UTF-8 byte sequences.
  22. *
  23. */
  24. #include "utf-8.h"
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "StackTrace.h"
  28. /**
  29. * Macro to determine the number of elements in a single-dimension array
  30. */
  31. #if !defined(ARRAY_SIZE)
  32. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  33. #endif
  34. /**
  35. * Structure to hold the valid ranges of UTF-8 characters, for each byte up to 4
  36. */
  37. struct
  38. {
  39. int len; /**< number of elements in the following array (1 to 4) */
  40. struct
  41. {
  42. char lower; /**< lower limit of valid range */
  43. char upper; /**< upper limit of valid range */
  44. } bytes[4]; /**< up to 4 bytes can be used per character */
  45. }
  46. valid_ranges[] =
  47. {
  48. {1, { {00, 0x7F} } },
  49. {2, { {0xC2, 0xDF}, {0x80, 0xBF} } },
  50. {3, { {0xE0, 0xE0}, {0xA0, 0xBF}, {0x80, 0xBF} } },
  51. {3, { {0xE1, 0xEC}, {0x80, 0xBF}, {0x80, 0xBF} } },
  52. {3, { {0xED, 0xED}, {0x80, 0x9F}, {0x80, 0xBF} } },
  53. {3, { {0xEE, 0xEF}, {0x80, 0xBF}, {0x80, 0xBF} } },
  54. {4, { {0xF0, 0xF0}, {0x90, 0xBF}, {0x80, 0xBF}, {0x80, 0xBF} } },
  55. {4, { {0xF1, 0xF3}, {0x80, 0xBF}, {0x80, 0xBF}, {0x80, 0xBF} } },
  56. {4, { {0xF4, 0xF4}, {0x80, 0x8F}, {0x80, 0xBF}, {0x80, 0xBF} } },
  57. };
  58. static const char* UTF8_char_validate(int len, const char* data);
  59. /**
  60. * Validate a single UTF-8 character
  61. * @param len the length of the string in "data"
  62. * @param data the bytes to check for a valid UTF-8 char
  63. * @return pointer to the start of the next UTF-8 character in "data"
  64. */
  65. static const char* UTF8_char_validate(int len, const char* data)
  66. {
  67. int good = 0;
  68. int charlen = 2;
  69. int i, j;
  70. const char *rc = NULL;
  71. /* first work out how many bytes this char is encoded in */
  72. if ((data[0] & 128) == 0)
  73. charlen = 1;
  74. else if ((data[0] & 0xF0) == 0xF0)
  75. charlen = 4;
  76. else if ((data[0] & 0xE0) == 0xE0)
  77. charlen = 3;
  78. if (charlen > len)
  79. goto exit; /* not enough characters in the string we were given */
  80. for (i = 0; i < ARRAY_SIZE(valid_ranges); ++i)
  81. { /* just has to match one of these rows */
  82. if (valid_ranges[i].len == charlen)
  83. {
  84. good = 1;
  85. for (j = 0; j < charlen; ++j)
  86. {
  87. if (data[j] < valid_ranges[i].bytes[j].lower ||
  88. data[j] > valid_ranges[i].bytes[j].upper)
  89. {
  90. good = 0; /* failed the check */
  91. break;
  92. }
  93. }
  94. if (good)
  95. break;
  96. }
  97. }
  98. if (good)
  99. rc = data + charlen;
  100. exit:
  101. return rc;
  102. }
  103. /**
  104. * Validate a length-delimited string has only UTF-8 characters
  105. * @param len the length of the string in "data"
  106. * @param data the bytes to check for valid UTF-8 characters
  107. * @return 1 (true) if the string has only UTF-8 characters, 0 (false) otherwise
  108. */
  109. int UTF8_validate(int len, const char* data)
  110. {
  111. const char* curdata = NULL;
  112. int rc = 0;
  113. FUNC_ENTRY;
  114. if (len == 0)
  115. {
  116. rc = 1;
  117. goto exit;
  118. }
  119. curdata = UTF8_char_validate(len, data);
  120. while (curdata && (curdata < data + len))
  121. curdata = UTF8_char_validate((int)(data + len - curdata), curdata);
  122. rc = curdata != NULL;
  123. exit:
  124. FUNC_EXIT_RC(rc);
  125. return rc;
  126. }
  127. /**
  128. * Validate a null-terminated string has only UTF-8 characters
  129. * @param string the string to check for valid UTF-8 characters
  130. * @return 1 (true) if the string has only UTF-8 characters, 0 (false) otherwise
  131. */
  132. int UTF8_validateString(const char* string)
  133. {
  134. int rc = 0;
  135. FUNC_ENTRY;
  136. rc = UTF8_validate((int)strlen(string), string);
  137. FUNC_EXIT_RC(rc);
  138. return rc;
  139. }
  140. #if defined(UNIT_TESTS)
  141. #include <stdio.h>
  142. typedef struct
  143. {
  144. int len;
  145. char data[20];
  146. } tests;
  147. tests valid_strings[] =
  148. {
  149. {3, "hjk" },
  150. {7, {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E} },
  151. {3, {'f', 0xC9, 0xB1 } },
  152. {9, {0xED, 0x95, 0x9C, 0xEA, 0xB5, 0xAD, 0xEC, 0x96, 0xB4} },
  153. {9, {0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E} },
  154. {4, {0x2F, 0x2E, 0x2E, 0x2F} },
  155. {7, {0xEF, 0xBB, 0xBF, 0xF0, 0xA3, 0x8E, 0xB4} },
  156. };
  157. tests invalid_strings[] =
  158. {
  159. {2, {0xC0, 0x80} },
  160. {5, {0x2F, 0xC0, 0xAE, 0x2E, 0x2F} },
  161. {6, {0xED, 0xA1, 0x8C, 0xED, 0xBE, 0xB4} },
  162. {1, {0xF4} },
  163. };
  164. int main (int argc, char *argv[])
  165. {
  166. int i, failed = 0;
  167. for (i = 0; i < ARRAY_SIZE(valid_strings); ++i)
  168. {
  169. if (!UTF8_validate(valid_strings[i].len, valid_strings[i].data))
  170. {
  171. printf("valid test %d failed\n", i);
  172. failed = 1;
  173. }
  174. else
  175. printf("valid test %d passed\n", i);
  176. }
  177. for (i = 0; i < ARRAY_SIZE(invalid_strings); ++i)
  178. {
  179. if (UTF8_validate(invalid_strings[i].len, invalid_strings[i].data))
  180. {
  181. printf("invalid test %d failed\n", i);
  182. failed = 1;
  183. }
  184. else
  185. printf("invalid test %d passed\n", i);
  186. }
  187. if (failed)
  188. printf("Failed\n");
  189. else
  190. printf("Passed\n");
  191. return 0;
  192. } /* End of main function*/
  193. #endif