collate.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* collate.c - implementation of indexing, using a Collation */
  13. #include "collate.h"
  14. #include <string.h> /* memcpy */
  15. #include <unicode/ucol.h> /* Collation */
  16. #include <unicode/ucnv.h> /* Conversion */
  17. #include <unicode/ustring.h> /* UTF8 conversion */
  18. #include <ldap.h> /* LDAP_UTF8LEN */
  19. #include <slap.h> /* for strcasecmp on non-UNIX platforms and correct debug macro */
  20. void
  21. collation_init( char *configpath )
  22. /* Called once per process, to initialize globals. */
  23. {
  24. /* ICU needs no initialization? */
  25. }
  26. typedef struct coll_profile_t { /* Collator characteristics */
  27. const char* language;
  28. const char* country;
  29. const char* variant;
  30. UColAttributeValue strength; /* one of UCOL_PRIMARY = 0, UCOL_SECONDARY = 1, UCOL_TERTIARY = 2, UCOL_QUATERNARY = 3, UCOL_IDENTICAL = 4 */
  31. UColAttributeValue decomposition; /* one of UCOL_OFF = 0, UCOL_DEFAULT = 1, UCOL_ON = 2 */
  32. } coll_profile_t;
  33. typedef struct coll_id_t { /* associates an OID with a coll_profile_t */
  34. char* oid;
  35. coll_profile_t* profile;
  36. } coll_id_t;
  37. /* A list of all OIDs that identify collator profiles: */
  38. static const coll_id_t** collation_id = NULL;
  39. static size_t collation_ids = 0;
  40. int
  41. collation_config (size_t cargc, char** cargv,
  42. const char* fname, size_t lineno)
  43. /* Process one line from a configuration file.
  44. Return 0 if it's OK, -1 if it's not recognized.
  45. Any other return value is a process exit code.
  46. */
  47. {
  48. if (cargc <= 0) { /* Bizarre. Oh, well... */
  49. } else if (!strcasecmp (cargv[0], "NLS")) {
  50. /* ignore - not needed anymore with ICU - was used to get path for NLS_Initialize */
  51. } else if (!strcasecmp (cargv[0], "collation")) {
  52. if ( cargc < 7 ) {
  53. LDAPDebug (LDAP_DEBUG_ANY,
  54. "%s: line %lu ignored: only %lu arguments (expected "
  55. "collation language country variant strength decomposition oid ...)\n",
  56. fname, (unsigned long)lineno, (unsigned long)cargc );
  57. } else {
  58. auto size_t arg;
  59. auto coll_profile_t* profile = (coll_profile_t*) slapi_ch_calloc (1, sizeof (coll_profile_t));
  60. if (*cargv[1]) profile->language = slapi_ch_strdup (cargv[1]);
  61. if (*cargv[2]) profile->country = slapi_ch_strdup (cargv[2]);
  62. if (*cargv[3]) profile->variant = slapi_ch_strdup (cargv[3]);
  63. switch (atoi(cargv[4])) {
  64. case 1: profile->strength = UCOL_PRIMARY; break;
  65. case 2: profile->strength = UCOL_SECONDARY; /* no break here? fall through? wtf? */
  66. case 3: profile->strength = UCOL_TERTIARY; break;
  67. case 4: profile->strength = UCOL_IDENTICAL; break;
  68. default: profile->strength = UCOL_SECONDARY;
  69. LDAPDebug (LDAP_DEBUG_ANY,
  70. "%s: line %lu: strength \"%s\" not supported (will use 2)\n",
  71. fname, (unsigned long)lineno, cargv[4]);
  72. break;
  73. }
  74. switch (atoi(cargv[5])) {
  75. case 1: profile->decomposition = UCOL_OFF; break;
  76. case 2: profile->decomposition = UCOL_DEFAULT; /* no break here? fall through? wtf? */
  77. case 3: profile->decomposition = UCOL_ON; break;
  78. default: profile->decomposition = UCOL_DEFAULT;
  79. LDAPDebug (LDAP_DEBUG_ANY,
  80. "%s: line %lu: decomposition \"%s\" not supported (will use 2)\n",
  81. fname, (unsigned long)lineno, cargv[5]);
  82. break;
  83. }
  84. {
  85. char descStr[256];
  86. char nameOrder[256];
  87. char nameSubstring[256];
  88. char oidString[256];
  89. char *tmpStr=NULL;
  90. Slapi_MatchingRuleEntry *mrentry=slapi_matchingrule_new();
  91. if(UCOL_PRIMARY == profile->strength) {
  92. strcpy(nameOrder,"caseIgnoreOrderingMatch");
  93. strcpy(nameSubstring,"caseIgnoreSubstringMatch");
  94. }
  95. else {
  96. strcpy(nameOrder,"caseExactOrderingMatch");
  97. strcpy(nameSubstring,"caseExactSubstringMatch");
  98. }
  99. /* PAR: this looks broken
  100. the "extra" text based oids that are actually used
  101. to form the name and description are always derived
  102. from the language and country fields so there should
  103. be no need to have two separate code paths to
  104. set the name and description fields of the schema
  105. as language is always available, and if country is
  106. not, it is not in the name anyway.
  107. Is it safe to assume all matching rules will follow
  108. this convention? The answer, or lack of it, probably
  109. explains the reasoning for doing things the way they
  110. are currently.
  111. */
  112. if(cargc > 7) {
  113. PL_strcatn(nameOrder,sizeof(nameOrder),"-");
  114. PL_strcatn(nameOrder,sizeof(nameOrder),cargv[7]);
  115. PL_strcatn(nameSubstring,sizeof(nameSubstring),"-");
  116. PL_strcatn(nameSubstring,sizeof(nameSubstring),cargv[7]);
  117. slapi_matchingrule_set(mrentry,SLAPI_MATCHINGRULE_NAME,
  118. (void *)slapi_ch_strdup(nameOrder));
  119. }
  120. else {
  121. if(0 != cargv[1][0]) {
  122. PL_strcatn(nameOrder,sizeof(nameOrder),"-");
  123. PL_strcatn(nameSubstring,sizeof(nameSubstring),"-");
  124. } else {
  125. nameOrder[0] = 0;
  126. nameSubstring[0] = 0;
  127. }
  128. PL_strcatn(nameOrder,sizeof(nameOrder),cargv[1]);
  129. PL_strcatn(nameSubstring,sizeof(nameSubstring),cargv[1]);
  130. slapi_matchingrule_set(mrentry,SLAPI_MATCHINGRULE_NAME,
  131. (void *)slapi_ch_strdup(nameOrder));
  132. }
  133. PL_strncpyz(oidString,cargv[6], sizeof(oidString));
  134. slapi_matchingrule_set(mrentry,SLAPI_MATCHINGRULE_OID,
  135. (void *)slapi_ch_strdup(oidString));
  136. if(0 != cargv[2][0]) {
  137. PR_snprintf(descStr, sizeof(descStr), "%s-%s",cargv[1],cargv[2]);
  138. }
  139. else {
  140. PL_strncpyz(descStr,cargv[1], sizeof(descStr));
  141. }
  142. slapi_matchingrule_set(mrentry,SLAPI_MATCHINGRULE_DESC,
  143. (void *)slapi_ch_strdup(descStr));
  144. slapi_matchingrule_set(mrentry,SLAPI_MATCHINGRULE_SYNTAX,
  145. (void *)slapi_ch_strdup(DIRSTRING_SYNTAX_OID));
  146. slapi_matchingrule_register(mrentry);
  147. slapi_matchingrule_get(mrentry,SLAPI_MATCHINGRULE_NAME,
  148. (void *)&tmpStr);
  149. slapi_ch_free((void **)&tmpStr);
  150. slapi_matchingrule_get(mrentry,SLAPI_MATCHINGRULE_OID,
  151. (void *)&tmpStr);
  152. slapi_ch_free((void **)&tmpStr);
  153. slapi_matchingrule_set(mrentry,SLAPI_MATCHINGRULE_NAME,
  154. (void *)slapi_ch_strdup(nameSubstring));
  155. PL_strcatn(oidString,sizeof(oidString),".6");
  156. slapi_matchingrule_set(mrentry,SLAPI_MATCHINGRULE_OID,
  157. (void *)slapi_ch_strdup(oidString));
  158. slapi_matchingrule_register(mrentry);
  159. slapi_matchingrule_free(&mrentry,1);
  160. }
  161. for (arg = 6; arg < cargc; ++arg) {
  162. auto coll_id_t* id = (coll_id_t*) slapi_ch_malloc (sizeof (coll_id_t));
  163. id->oid = slapi_ch_strdup (cargv[arg]);
  164. id->profile = profile;
  165. if (collation_ids <= 0) {
  166. collation_id = (const coll_id_t**) slapi_ch_malloc (2 * sizeof (coll_id_t*));
  167. } else {
  168. collation_id = (const coll_id_t**) slapi_ch_realloc
  169. ((void*)collation_id, (collation_ids + 2) * sizeof (coll_id_t*));
  170. }
  171. collation_id [collation_ids++] = id;
  172. collation_id [collation_ids] = NULL;
  173. }
  174. }
  175. } else {
  176. return -1; /* unrecognized */
  177. }
  178. return 0; /* success */
  179. }
  180. typedef struct collation_indexer_t
  181. /* A kind of indexer, implemented using an ICU Collator */
  182. {
  183. UCollator* collator;
  184. UConverter* converter;
  185. struct berval** ix_keys;
  186. } collation_indexer_t;
  187. /*
  188. Caller must ensure that U == NULL and Ulen == 0 the first time called
  189. */
  190. static UErrorCode
  191. SetUnicodeStringFromUTF_8 (UChar** U, int32_t* Ulen, int *isAlloced, const struct berval* bv)
  192. /* Copy the UTF-8 string bv into the UnicodeString U,
  193. but remove leading and trailing whitespace, and
  194. convert consecutive whitespaces into a single space.
  195. Ulen is set to the number of UChars in the array (not necessarily the number of bytes!)
  196. */
  197. {
  198. size_t n;
  199. int32_t len = 0; /* length of non-space string */
  200. UErrorCode err = U_ZERO_ERROR;
  201. const char* s = bv->bv_val;
  202. const char* begin = NULL; /* will point to beginning of non-space in val */
  203. const char* end = NULL; /* will point to the first space after the last non-space char in val */
  204. int32_t nUchars = 0;
  205. if (!bv->bv_len) { /* no value? */
  206. return U_INVALID_FORMAT_ERROR; /* don't know what else to use here */
  207. }
  208. /* first, set s to the first non-space char in bv->bv_val */
  209. for (n = 0; (n < bv->bv_len) && ldap_utf8isspace((char *)s); ) { /* cast away const */
  210. const char *next = LDAP_UTF8NEXT((char *)s); /* cast away const */
  211. n += (next - s); /* count bytes, not chars */
  212. s = next;
  213. }
  214. begin = s; /* begin points to first non-space char in val */
  215. if (n >= bv->bv_len) { /* value is all spaces? */
  216. return U_INVALID_FORMAT_ERROR; /* don't know what else to use here */
  217. }
  218. s = bv->bv_val + (bv->bv_len-1); /* move s to last char of bv_val */
  219. end = s; /* end points at last char of bv_val - may change below */
  220. /* find the last non-null and non-space char of val */
  221. for (n = bv->bv_len; (n > 0) && (!*s || ldap_utf8isspace((char *)s));) {
  222. const char *prev = LDAP_UTF8PREV((char *)s);
  223. end = prev;
  224. n -= (s - prev); /* count bytes, not chars */
  225. s = prev;
  226. }
  227. /* end now points at last non-null/non-space of val */
  228. if (n == 0) { /* bogus */
  229. return U_INVALID_FORMAT_ERROR; /* don't know what else to use here */
  230. }
  231. len = LDAP_UTF8NEXT((char *)end) - begin;
  232. u_strFromUTF8(*U, *Ulen, &nUchars, begin, len, &err);
  233. if (nUchars > *Ulen) { /* need more space */
  234. if (*isAlloced) { /* realloc space */
  235. *U = (UChar *)slapi_ch_realloc((char *)*U, sizeof(UChar) * nUchars);
  236. } else { /* must use malloc */
  237. *U = (UChar *)slapi_ch_malloc(sizeof(UChar) * nUchars);
  238. *isAlloced = 1; /* no longer using fixed buffer */
  239. }
  240. *Ulen = nUchars;
  241. err = U_ZERO_ERROR; /* reset */
  242. u_strFromUTF8(*U, *Ulen, NULL, begin, len, &err);
  243. } else {
  244. *Ulen = nUchars;
  245. }
  246. return err;
  247. }
  248. static struct berval**
  249. collation_index (indexer_t* ix, struct berval** bvec, struct berval** prefixes)
  250. {
  251. collation_indexer_t* etc = (collation_indexer_t*) ix->ix_etc;
  252. struct berval** keys = NULL;
  253. if (bvec) {
  254. char keyBuffer[128]; /* try to use static space buffer to avoid malloc */
  255. int32_t keyLen = sizeof(keyBuffer);
  256. char* key = keyBuffer; /* but key can grow if necessary */
  257. size_t keyn = 0;
  258. struct berval** bv;
  259. UChar charBuffer[128]; /* try to use static space buffer */
  260. int32_t nChars = sizeof(charBuffer)/sizeof(UChar); /* but grow if necessary */
  261. UChar *chars = charBuffer; /* try to reuse this */
  262. int isAlloced = 0; /* using fixed buffer */
  263. for (bv = bvec; *bv; ++bv) {
  264. /* if chars is allocated, nChars will be the capacity and the number of chars in chars */
  265. /* otherwise, nChars will be the number of chars, which may be less than the capacity */
  266. if (!isAlloced) {
  267. nChars = sizeof(charBuffer)/sizeof(UChar); /* reset */
  268. }
  269. if (U_ZERO_ERROR == SetUnicodeStringFromUTF_8 (&chars, &nChars, &isAlloced, *bv)) {
  270. /* nChars is now the number of UChar in chars, which may be less than the
  271. capacity of charBuffer if not allocated */
  272. struct berval* prefix = prefixes ? prefixes[bv-bvec] : NULL;
  273. const size_t prefixLen = prefix ? prefix->bv_len : 0;
  274. struct berval* bk = NULL;
  275. int32_t realLen; /* real length of key, not keyLen which is buffer size */
  276. /* try to get the sort key using key and keyLen; only grow key
  277. if we need to */
  278. /* can use -1 for char len since the conversion from UTF8
  279. null terminates the string */
  280. realLen = ucol_getSortKey(etc->collator, chars, nChars, (uint8_t *)key, keyLen);
  281. if (realLen > keyLen) { /* need more space */
  282. if (key == keyBuffer) {
  283. key = (char*)slapi_ch_malloc(sizeof(char) * realLen);
  284. } else {
  285. key = (char*)slapi_ch_realloc(key, sizeof(char) * realLen);
  286. }
  287. keyLen = ucol_getSortKey(etc->collator, chars, nChars, (uint8_t *)key, realLen);
  288. }
  289. if (realLen > 0) {
  290. bk = (struct berval*) slapi_ch_malloc (sizeof(struct berval));
  291. bk->bv_len = prefixLen + realLen;
  292. bk->bv_val = slapi_ch_malloc (bk->bv_len + 1);
  293. if (prefixLen) {
  294. memcpy(bk->bv_val, prefix->bv_val, prefixLen);
  295. }
  296. memcpy(bk->bv_val + prefixLen, key, realLen);
  297. bk->bv_val[bk->bv_len] = '\0';
  298. LDAPDebug (LDAP_DEBUG_FILTER, "collation_index(%.*s) %lu bytes\n",
  299. bk->bv_len, bk->bv_val, (unsigned long)bk->bv_len);
  300. keys = (struct berval**)
  301. slapi_ch_realloc ((void*)keys, sizeof(struct berval*) * (keyn + 2));
  302. keys[keyn++] = bk;
  303. keys[keyn] = NULL;
  304. }
  305. }
  306. }
  307. if (chars != charBuffer) { /* realloc'ed, need to free */
  308. slapi_ch_free((void **)&chars);
  309. }
  310. if (key != keyBuffer) { /* realloc'ed, need to free */
  311. slapi_ch_free_string(&key);
  312. }
  313. }
  314. if (etc->ix_keys != NULL) ber_bvecfree (etc->ix_keys);
  315. etc->ix_keys = keys;
  316. return keys;
  317. }
  318. static void
  319. collation_indexer_destroy (indexer_t* ix)
  320. /* The destructor function for a collation-based indexer. */
  321. {
  322. collation_indexer_t* etc = (collation_indexer_t*) ix->ix_etc;
  323. if (etc->converter) {
  324. ucnv_close(etc->converter);
  325. etc->converter = NULL;
  326. }
  327. if (etc->collator) {
  328. ucol_close(etc->collator);
  329. etc->collator = NULL;
  330. }
  331. if (etc->ix_keys != NULL) {
  332. ber_bvecfree (etc->ix_keys);
  333. etc->ix_keys = NULL;
  334. }
  335. slapi_ch_free((void**)&ix->ix_etc);
  336. ix->ix_etc = NULL; /* just for hygiene */
  337. }
  338. static UErrorCode
  339. s_newNamedLocaleFromComponents(char **locale, const char *lang, const char *country, const char *variant)
  340. {
  341. UErrorCode err = U_ZERO_ERROR;
  342. int hasLang = (lang && *lang);
  343. int hasC = (country && *country);
  344. int hasVar = (variant && *variant);
  345. *locale = NULL;
  346. if (hasLang) {
  347. *locale = PR_smprintf("%s%s%s%s%s", lang, (hasC ? "_" : ""), (hasC ? country : ""),
  348. (hasVar ? "_" : ""), (hasVar ? variant : ""));
  349. } else {
  350. err = U_INVALID_FORMAT_ERROR; /* don't know what else to use here */
  351. }
  352. return err;
  353. }
  354. indexer_t*
  355. collation_indexer_create (const char* oid)
  356. /* Return a new indexer, based on the collation identified by oid.
  357. Return NULL if this can't be done.
  358. */
  359. {
  360. indexer_t* ix = NULL;
  361. const coll_id_t** id = collation_id;
  362. char* locale = NULL; /* NULL == default locale */
  363. UCollator* coll = NULL;
  364. collation_indexer_t* etc = NULL;
  365. if (id) for (; *id; ++id) {
  366. if (!strcasecmp (oid, (*id)->oid)) {
  367. const coll_profile_t* profile = (*id)->profile;
  368. const int is_default = (profile->language == NULL &&
  369. profile->country == NULL &&
  370. profile->variant == NULL);
  371. UErrorCode err = U_ZERO_ERROR;
  372. if ( ! is_default) {
  373. err = s_newNamedLocaleFromComponents(&locale,
  374. profile->language,
  375. profile->country,
  376. profile->variant);
  377. }
  378. if (err == U_ZERO_ERROR) {
  379. coll = ucol_open(locale, &err);
  380. /*
  381. * If we found exactly the right collator for this locale,
  382. * or if we found a fallback one, or if we are happy with
  383. * the default, use it.
  384. */
  385. if (U_SUCCESS(err)) {
  386. etc = (collation_indexer_t*) slapi_ch_calloc (1, sizeof (collation_indexer_t));
  387. ix = (indexer_t*) slapi_ch_calloc (1, sizeof (indexer_t));
  388. ucol_setAttribute (coll, UCOL_STRENGTH, profile->strength, &err);
  389. if (U_FAILURE(err)) {
  390. LDAPDebug (LDAP_DEBUG_ANY, "collation_indexer_create: could not "
  391. "set the collator strength for oid %s to %d: err %d\n",
  392. oid, profile->strength, err);
  393. }
  394. ucol_setAttribute (coll, UCOL_DECOMPOSITION_MODE, profile->decomposition, &err);
  395. if (U_FAILURE(err)) {
  396. LDAPDebug (LDAP_DEBUG_ANY, "collation_indexer_create: could not "
  397. "set the collator decomposition mode for oid %s to %d: err %d\n",
  398. oid, profile->decomposition, err);
  399. }
  400. etc->collator = coll;
  401. for (id = collation_id; *id; ++id) {
  402. if ((*id)->profile == profile) {
  403. break; /* found the 'official' id */
  404. }
  405. }
  406. if (!*id) {
  407. LDAPDebug (LDAP_DEBUG_ANY, "collation_indexer_create: id not found\n", 0, 0, 0);
  408. goto error;
  409. }
  410. ix->ix_etc = etc;
  411. ix->ix_oid = (*id)->oid;
  412. ix->ix_index = collation_index;
  413. ix->ix_destroy = collation_indexer_destroy;
  414. break; /* return */
  415. /* free (etc); */
  416. /* free (ix); */
  417. } else if (err == U_USING_DEFAULT_WARNING) {
  418. LDAPDebug (LDAP_DEBUG_FILTER, "collation_indexer_create: could not "
  419. "create an indexer for OID %s for locale %s and could not "
  420. "use default locale\n",
  421. oid, (locale ? locale : "(default)"), NULL);
  422. } else { /* error */
  423. LDAPDebug (LDAP_DEBUG_FILTER, "collation_indexer_create: could not "
  424. "create an indexer for OID %s for locale %s: err = %d\n",
  425. oid, (locale ? locale : "(default)"), err);
  426. }
  427. if (coll) {
  428. ucol_close (coll);
  429. coll = NULL;
  430. }
  431. }
  432. break; /* failed to create the specified collator */
  433. }
  434. }
  435. goto done;
  436. error:
  437. slapi_ch_free((void **)&etc);
  438. slapi_ch_free((void **)&ix);
  439. if (coll) {
  440. ucol_close (coll);
  441. coll = NULL;
  442. }
  443. done:
  444. if (locale) {
  445. PR_smprintf_free(locale);
  446. locale = NULL;
  447. }
  448. return ix;
  449. }