utf8n.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2014 SGI.
  4. * All rights reserved.
  5. */
  6. #ifndef UTF8NORM_H
  7. #define UTF8NORM_H
  8. #include <linux/types.h>
  9. #include <linux/export.h>
  10. #include <linux/string.h>
  11. #include <linux/module.h>
  12. /* Encoding a unicode version number as a single unsigned int. */
  13. #define UNICODE_MAJ_SHIFT (16)
  14. #define UNICODE_MIN_SHIFT (8)
  15. #define UNICODE_AGE(MAJ, MIN, REV) \
  16. (((unsigned int)(MAJ) << UNICODE_MAJ_SHIFT) | \
  17. ((unsigned int)(MIN) << UNICODE_MIN_SHIFT) | \
  18. ((unsigned int)(REV)))
  19. /* Highest unicode version supported by the data tables. */
  20. extern int utf8version_is_supported(u8 maj, u8 min, u8 rev);
  21. extern int utf8version_latest(void);
  22. /*
  23. * Look for the correct const struct utf8data for a unicode version.
  24. * Returns NULL if the version requested is too new.
  25. *
  26. * Two normalization forms are supported: nfdi and nfdicf.
  27. *
  28. * nfdi:
  29. * - Apply unicode normalization form NFD.
  30. * - Remove any Default_Ignorable_Code_Point.
  31. *
  32. * nfdicf:
  33. * - Apply unicode normalization form NFD.
  34. * - Remove any Default_Ignorable_Code_Point.
  35. * - Apply a full casefold (C + F).
  36. */
  37. extern const struct utf8data *utf8nfdi(unsigned int maxage);
  38. extern const struct utf8data *utf8nfdicf(unsigned int maxage);
  39. /*
  40. * Determine the maximum age of any unicode character in the string.
  41. * Returns 0 if only unassigned code points are present.
  42. * Returns -1 if the input is not valid UTF-8.
  43. */
  44. extern int utf8agemax(const struct utf8data *data, const char *s);
  45. extern int utf8nagemax(const struct utf8data *data, const char *s, size_t len);
  46. /*
  47. * Determine the minimum age of any unicode character in the string.
  48. * Returns 0 if any unassigned code points are present.
  49. * Returns -1 if the input is not valid UTF-8.
  50. */
  51. extern int utf8agemin(const struct utf8data *data, const char *s);
  52. extern int utf8nagemin(const struct utf8data *data, const char *s, size_t len);
  53. /*
  54. * Determine the length of the normalized from of the string,
  55. * excluding any terminating NULL byte.
  56. * Returns 0 if only ignorable code points are present.
  57. * Returns -1 if the input is not valid UTF-8.
  58. */
  59. extern ssize_t utf8len(const struct utf8data *data, const char *s);
  60. extern ssize_t utf8nlen(const struct utf8data *data, const char *s, size_t len);
  61. /* Needed in struct utf8cursor below. */
  62. #define UTF8HANGULLEAF (12)
  63. /*
  64. * Cursor structure used by the normalizer.
  65. */
  66. struct utf8cursor {
  67. const struct utf8data *data;
  68. const char *s;
  69. const char *p;
  70. const char *ss;
  71. const char *sp;
  72. unsigned int len;
  73. unsigned int slen;
  74. short int ccc;
  75. short int nccc;
  76. unsigned char hangul[UTF8HANGULLEAF];
  77. };
  78. /*
  79. * Initialize a utf8cursor to normalize a string.
  80. * Returns 0 on success.
  81. * Returns -1 on failure.
  82. */
  83. extern int utf8cursor(struct utf8cursor *u8c, const struct utf8data *data,
  84. const char *s);
  85. extern int utf8ncursor(struct utf8cursor *u8c, const struct utf8data *data,
  86. const char *s, size_t len);
  87. /*
  88. * Get the next byte in the normalization.
  89. * Returns a value > 0 && < 256 on success.
  90. * Returns 0 when the end of the normalization is reached.
  91. * Returns -1 if the string being normalized is not valid UTF-8.
  92. */
  93. extern int utf8byte(struct utf8cursor *u8c);
  94. #endif /* UTF8NORM_H */