ldap_pvt_uc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /*
  16. * ldap_pvt_uc.h - Header for Unicode functions.
  17. * These are meant to be used by the OpenLDAP distribution only.
  18. * These should be named ldap_pvt_....()
  19. */
  20. #ifndef _LDAP_PVT_UC_H
  21. #define _LDAP_PVT_UC_H 1
  22. #include <lber.h> /* get ber_slen_t */
  23. #include <ac/bytes.h>
  24. #include "../libraries/liblunicode/ucdata/ucdata.h"
  25. LDAP_BEGIN_DECL
  26. /*
  27. * UTF-8 (in utf-8.c)
  28. */
  29. /* UCDATA uses UCS-2 passed in a 4 byte unsigned int */
  30. typedef ac_uint4 ldap_unicode_t;
  31. /* Convert a string with csize octets per character to UTF-8 */
  32. LDAP_F( int ) ldap_ucs_to_utf8s LDAP_P((
  33. struct berval *ucs, int csize, struct berval *utf8s ));
  34. /* returns the number of bytes in the UTF-8 string */
  35. LDAP_F (ber_len_t) ldap_utf8_bytes( const char * );
  36. /* returns the number of UTF-8 characters in the string */
  37. LDAP_F (ber_len_t) ldap_utf8_chars( const char * );
  38. /* returns the length (in bytes) of the UTF-8 character */
  39. LDAP_F (int) ldap_utf8_offset( const char * );
  40. /* returns the length (in bytes) indicated by the UTF-8 character */
  41. LDAP_F (int) ldap_utf8_charlen( const char * );
  42. /* returns the length (in bytes) indicated by the UTF-8 character
  43. * also checks that shortest possible encoding was used
  44. */
  45. LDAP_F (int) ldap_utf8_charlen2( const char * );
  46. /* copies a UTF-8 character and returning number of bytes copied */
  47. LDAP_F (int) ldap_utf8_copy( char *, const char *);
  48. /* returns pointer of next UTF-8 character in string */
  49. LDAP_F (char*) ldap_utf8_next( const char * );
  50. /* returns pointer of previous UTF-8 character in string */
  51. LDAP_F (char*) ldap_utf8_prev( const char * );
  52. /* primitive ctype routines -- not aware of non-ascii characters */
  53. LDAP_F (int) ldap_utf8_isascii( const char * );
  54. LDAP_F (int) ldap_utf8_isalpha( const char * );
  55. LDAP_F (int) ldap_utf8_isalnum( const char * );
  56. LDAP_F (int) ldap_utf8_isdigit( const char * );
  57. LDAP_F (int) ldap_utf8_isxdigit( const char * );
  58. LDAP_F (int) ldap_utf8_isspace( const char * );
  59. /* span characters not in set, return bytes spanned */
  60. LDAP_F (ber_len_t) ldap_utf8_strcspn( const char* str, const char *set);
  61. /* span characters in set, return bytes spanned */
  62. LDAP_F (ber_len_t) ldap_utf8_strspn( const char* str, const char *set);
  63. /* return first occurrence of character in string */
  64. LDAP_F (char *) ldap_utf8_strchr( const char* str, const char *chr);
  65. /* return first character of set in string */
  66. LDAP_F (char *) ldap_utf8_strpbrk( const char* str, const char *set);
  67. /* reentrant tokenizer */
  68. LDAP_F (char*) ldap_utf8_strtok( char* sp, const char* sep, char **last);
  69. /* Optimizations */
  70. LDAP_V (const char) ldap_utf8_lentab[128];
  71. LDAP_V (const char) ldap_utf8_mintab[32];
  72. #define LDAP_UTF8_ISASCII(p) ( !(*(const unsigned char *)(p) & 0x80 ) )
  73. #define LDAP_UTF8_CHARLEN(p) ( LDAP_UTF8_ISASCII(p) \
  74. ? 1 : ldap_utf8_lentab[*(const unsigned char *)(p) ^ 0x80] )
  75. /* This is like CHARLEN but additionally validates to make sure
  76. * the char used the shortest possible encoding.
  77. * 'l' is used to temporarily hold the result of CHARLEN.
  78. */
  79. #define LDAP_UTF8_CHARLEN2(p, l) ( ( ( l = LDAP_UTF8_CHARLEN( p )) < 3 || \
  80. ( ldap_utf8_mintab[*(const unsigned char *)(p) & 0x1f] & (p)[1] ) ) ? \
  81. l : 0 )
  82. #define LDAP_UTF8_OFFSET(p) ( LDAP_UTF8_ISASCII(p) \
  83. ? 1 : ldap_utf8_offset((p)) )
  84. #define LDAP_UTF8_COPY(d,s) ( LDAP_UTF8_ISASCII(s) \
  85. ? (*(d) = *(s), 1) : ldap_utf8_copy((d),(s)) )
  86. #define LDAP_UTF8_NEXT(p) ( LDAP_UTF8_ISASCII(p) \
  87. ? (char *)(p)+1 : ldap_utf8_next((p)) )
  88. #define LDAP_UTF8_INCR(p) ((p) = LDAP_UTF8_NEXT(p))
  89. /* For symmetry */
  90. #define LDAP_UTF8_PREV(p) (ldap_utf8_prev((p)))
  91. #define LDAP_UTF8_DECR(p) ((p)=LDAP_UTF8_PREV((p)))
  92. /* these probably should be renamed */
  93. LDAP_LUNICODE_F(int) ucstrncmp(
  94. const ldap_unicode_t *,
  95. const ldap_unicode_t *,
  96. ber_len_t );
  97. LDAP_LUNICODE_F(int) ucstrncasecmp(
  98. const ldap_unicode_t *,
  99. const ldap_unicode_t *,
  100. ber_len_t );
  101. LDAP_LUNICODE_F(ldap_unicode_t *) ucstrnchr(
  102. const ldap_unicode_t *,
  103. ber_len_t,
  104. ldap_unicode_t );
  105. LDAP_LUNICODE_F(ldap_unicode_t *) ucstrncasechr(
  106. const ldap_unicode_t *,
  107. ber_len_t,
  108. ldap_unicode_t );
  109. LDAP_LUNICODE_F(void) ucstr2upper(
  110. ldap_unicode_t *,
  111. ber_len_t );
  112. #define LDAP_UTF8_NOCASEFOLD 0x0U
  113. #define LDAP_UTF8_CASEFOLD 0x1U
  114. #define LDAP_UTF8_ARG1NFC 0x2U
  115. #define LDAP_UTF8_ARG2NFC 0x4U
  116. #define LDAP_UTF8_APPROX 0x8U
  117. LDAP_LUNICODE_F(struct berval *) UTF8bvnormalize(
  118. struct berval *,
  119. struct berval *,
  120. unsigned,
  121. void *memctx );
  122. LDAP_LUNICODE_F(int) UTF8bvnormcmp(
  123. struct berval *,
  124. struct berval *,
  125. unsigned,
  126. void *memctx );
  127. LDAP_END_DECL
  128. #endif