string.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. /* string.c - common string syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. #if defined(IRIX)
  18. #include <unistd.h>
  19. #endif
  20. #define MAX_VAL(x,y) ((x)>(y)?(x):(y))
  21. static int string_filter_approx( struct berval *bvfilter,
  22. Slapi_Value **bvals, Slapi_Value **retVal );
  23. static void substring_comp_keys( Slapi_Value ***ivals, int *nsubs, char *str,
  24. int lenstr, int prepost, int syntax, char *comp_buf, int *substrlens );
  25. int
  26. string_filter_ava( struct berval *bvfilter, Slapi_Value **bvals, int syntax,
  27. int ftype, Slapi_Value **retVal )
  28. {
  29. int i, rc;
  30. struct berval bvfilter_norm = {0, NULL};
  31. struct berval *pbvfilter_norm = &bvfilter_norm;
  32. char *alt = NULL;
  33. if(retVal) {
  34. *retVal = NULL;
  35. }
  36. if ( ftype == LDAP_FILTER_APPROX ) {
  37. return( string_filter_approx( bvfilter, bvals, retVal ) );
  38. }
  39. if (syntax & SYNTAX_NORM_FILT) {
  40. pbvfilter_norm = bvfilter; /* already normalized */
  41. } else {
  42. slapi_ber_bvcpy(&bvfilter_norm, bvfilter);
  43. /* 3rd arg: 1 - trim leading blanks */
  44. value_normalize_ext( bvfilter_norm.bv_val, syntax, 1, &alt );
  45. if (alt) {
  46. slapi_ber_bvdone(&bvfilter_norm);
  47. bvfilter_norm.bv_val = alt;
  48. alt = NULL;
  49. }
  50. if(bvfilter_norm.bv_val){
  51. bvfilter_norm.bv_len = strlen(bvfilter_norm.bv_val);
  52. } else {
  53. bvfilter_norm.bv_len = 0;
  54. }
  55. }
  56. for ( i = 0; (bvals != NULL) && (bvals[i] != NULL); i++ ) {
  57. int norm_val = 1; /* normalize the first value only */
  58. /* if the NORMALIZED flag is set, skip normalizing */
  59. if (slapi_value_get_flags(bvals[i]) & SLAPI_ATTR_FLAG_NORMALIZED) {
  60. norm_val = 0;
  61. }
  62. /* note - do not return the normalized value in retVal - the
  63. caller will usually want the "raw" value, and can normalize it later
  64. */
  65. rc = value_cmp( (struct berval*)slapi_value_get_berval(bvals[i]), pbvfilter_norm, syntax, norm_val );
  66. switch ( ftype ) {
  67. case LDAP_FILTER_GE:
  68. if ( rc >= 0 ) {
  69. if(retVal) {
  70. *retVal = bvals[i];
  71. }
  72. slapi_ch_free_string(&bvfilter_norm.bv_val);
  73. return( 0 );
  74. }
  75. break;
  76. case LDAP_FILTER_LE:
  77. if ( rc <= 0 ) {
  78. if(retVal) {
  79. *retVal = bvals[i];
  80. }
  81. slapi_ch_free_string(&bvfilter_norm.bv_val);
  82. return( 0 );
  83. }
  84. break;
  85. case LDAP_FILTER_EQUALITY:
  86. if ( rc == 0 ) {
  87. if(retVal) {
  88. *retVal = bvals[i];
  89. }
  90. slapi_ch_free_string(&bvfilter_norm.bv_val);
  91. return( 0 );
  92. }
  93. break;
  94. }
  95. }
  96. slapi_ch_free_string(&bvfilter_norm.bv_val);
  97. return( -1 );
  98. }
  99. /*
  100. * return value: 0 -- approximately matched
  101. * -1 -- did not match
  102. */
  103. static int
  104. string_filter_approx( struct berval *bvfilter, Slapi_Value **bvals,
  105. Slapi_Value **retVal)
  106. {
  107. int i, rc;
  108. int ava_wordcount;
  109. char *w1, *w2, *c1, *c2;
  110. /*
  111. * try to match words in each filter value in order
  112. * in the attribute value.
  113. * XXX should do this once for the filter and save it XXX
  114. */
  115. rc = -1;
  116. if(retVal) {
  117. *retVal = NULL;
  118. }
  119. for ( i = 0; (bvals != NULL) && (bvals[i] != NULL); i++ ) {
  120. w2 = (char*)slapi_value_get_string(bvals[i]); /* JCM cast */
  121. ava_wordcount = 0;
  122. /* for each word in the filter value */
  123. for ( w1 = first_word( bvfilter->bv_val ); w1 != NULL;
  124. w1 = next_word( w1 ) ) {
  125. ++ava_wordcount;
  126. if ( (c1 = phonetic( w1 )) == NULL ) {
  127. break;
  128. }
  129. /*
  130. * for each word in the attribute value from
  131. * where we left off...
  132. */
  133. for ( w2 = first_word( w2 ); w2 != NULL;
  134. w2 = next_word( w2 ) ) {
  135. c2 = phonetic( w2 );
  136. rc = strcmp( c1, c2 );
  137. slapi_ch_free((void**)&c2 );
  138. if ( rc == 0 ) {
  139. if(retVal) {
  140. *retVal = bvals[i];
  141. }
  142. break;
  143. }
  144. }
  145. slapi_ch_free((void**)&c1 );
  146. /*
  147. * if we stopped because we ran out of words
  148. * before making a match, go on to the next
  149. * value. otherwise try to keep matching
  150. * words in this value from where we left off.
  151. */
  152. if ( w2 == NULL ) {
  153. break;
  154. } else {
  155. w2 = next_word( w2 );
  156. }
  157. }
  158. /*
  159. * if we stopped because we ran out of words and
  160. * we found at leasy one word, we have a match.
  161. */
  162. if ( w1 == NULL && ava_wordcount > 0 ) {
  163. rc = 0;
  164. break;
  165. }
  166. }
  167. if (0 != rc) {
  168. rc = -1;
  169. }
  170. LDAPDebug( LDAP_DEBUG_TRACE, "<= string_filter_approx %d\n",
  171. rc, 0, 0 );
  172. return( rc );
  173. }
  174. int
  175. string_filter_sub( Slapi_PBlock *pb, char *initial, char **any, char *final,
  176. Slapi_Value **bvals, int syntax )
  177. {
  178. int i, j, rc, size=0;
  179. char *p, *end, *realval, *tmpbuf = NULL, *bigpat = NULL;
  180. size_t tmpbufsize;
  181. char pat[BUFSIZ];
  182. char buf[BUFSIZ];
  183. time_t curtime = 0;
  184. time_t time_up = 0;
  185. time_t optime = 0; /* time op was initiated */
  186. int timelimit = 0; /* search timelimit */
  187. Operation *op = NULL;
  188. Slapi_Regex *re = NULL;
  189. const char *re_result = NULL;
  190. char *alt = NULL;
  191. int filter_normalized = 0;
  192. int free_re = 1;
  193. struct subfilt *sf = NULL;
  194. LDAPDebug( LDAP_DEBUG_FILTER, "=> string_filter_sub\n", 0, 0, 0 );
  195. if (pb) {
  196. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  197. }
  198. if (NULL != op) {
  199. slapi_pblock_get( pb, SLAPI_SEARCH_TIMELIMIT, &timelimit );
  200. slapi_pblock_get( pb, SLAPI_OPINITIATED_TIME, &optime );
  201. } else {
  202. /* timelimit is not passed via pblock */
  203. timelimit = -1;
  204. }
  205. /*
  206. * (timelimit==-1) means no time limit
  207. */
  208. time_up = ( timelimit==-1 ? -1 : optime + timelimit);
  209. if (pb) {
  210. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized );
  211. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_DATA, &sf );
  212. }
  213. if ( sf ) {
  214. re = (Slapi_Regex *)sf->sf_private;
  215. if ( re ) {
  216. free_re = 0;
  217. }
  218. }
  219. if (!re) {
  220. /*
  221. * construct a regular expression corresponding to the
  222. * filter and let regex do the work for each value
  223. * XXX should do this once and save it somewhere XXX
  224. */
  225. pat[0] = '\0';
  226. p = pat;
  227. end = pat + sizeof(pat) - 2; /* leave room for null */
  228. if ( initial != NULL ) {
  229. size = strlen( initial ) + 1; /* add 1 for "^" */
  230. }
  231. if ( any != NULL ) {
  232. i = 0;
  233. while ( any[i] ) {
  234. size += strlen(any[i++]) + 2; /* add 2 for ".*" */
  235. }
  236. }
  237. if ( final != NULL ) {
  238. size += strlen( final ) + 3; /* add 3 for ".*" and "$" */
  239. }
  240. size *= 2; /* doubled in case all filter chars need escaping */
  241. size++; /* add 1 for null */
  242. if ( p + size > end ) {
  243. bigpat = slapi_ch_malloc( size );
  244. p = bigpat;
  245. }
  246. if ( initial != NULL ) {
  247. /* 3rd arg: 1 - trim leading blanks */
  248. if (!filter_normalized) {
  249. value_normalize_ext( initial, syntax, 1, &alt );
  250. }
  251. *p++ = '^';
  252. if (alt) {
  253. filter_strcpy_special_ext( p, alt, FILTER_STRCPY_ESCAPE_RECHARS );
  254. slapi_ch_free_string(&alt);
  255. } else {
  256. filter_strcpy_special_ext( p, initial, FILTER_STRCPY_ESCAPE_RECHARS );
  257. }
  258. p = strchr( p, '\0' );
  259. }
  260. if ( any != NULL ) {
  261. for ( i = 0; any[i] != NULL; i++ ) {
  262. /* 3rd arg: 0 - DO NOT trim leading blanks */
  263. if (!filter_normalized) {
  264. value_normalize_ext( any[i], syntax, 0, &alt );
  265. }
  266. /* ".*" + value */
  267. *p++ = '.';
  268. *p++ = '*';
  269. if (alt) {
  270. filter_strcpy_special_ext( p, alt, FILTER_STRCPY_ESCAPE_RECHARS );
  271. slapi_ch_free_string(&alt);
  272. } else {
  273. filter_strcpy_special_ext( p, any[i], FILTER_STRCPY_ESCAPE_RECHARS );
  274. }
  275. p = strchr( p, '\0' );
  276. }
  277. }
  278. if ( final != NULL ) {
  279. /* 3rd arg: 0 - DO NOT trim leading blanks */
  280. if (!filter_normalized) {
  281. value_normalize_ext( final, syntax, 0, &alt );
  282. }
  283. /* ".*" + value */
  284. *p++ = '.';
  285. *p++ = '*';
  286. if (alt) {
  287. filter_strcpy_special_ext( p, alt, FILTER_STRCPY_ESCAPE_RECHARS );
  288. slapi_ch_free_string(&alt);
  289. } else {
  290. filter_strcpy_special_ext( p, final, FILTER_STRCPY_ESCAPE_RECHARS );
  291. }
  292. strcat( p, "$" );
  293. }
  294. /* compile the regex */
  295. p = (bigpat) ? bigpat : pat;
  296. tmpbuf = NULL;
  297. re = slapi_re_comp( p, &re_result );
  298. if (NULL == re) {
  299. LDAPDebug( LDAP_DEBUG_ANY, "re_comp (%s) failed (%s): %s\n",
  300. pat, p, re_result?re_result:"unknown" );
  301. rc = LDAP_OPERATIONS_ERROR;
  302. goto bailout;
  303. } else if (slapi_is_loglevel_set(SLAPI_LOG_TRACE)) {
  304. char ebuf[BUFSIZ];
  305. LDAPDebug(LDAP_DEBUG_TRACE, "re_comp (%s)\n", escape_string(p, ebuf), 0, 0);
  306. }
  307. }
  308. curtime = current_time();
  309. if ( time_up != -1 && curtime > time_up ) {
  310. rc = LDAP_TIMELIMIT_EXCEEDED;
  311. goto bailout;
  312. }
  313. /*
  314. * test the regex against each value
  315. */
  316. rc = -1;
  317. tmpbuf = NULL;
  318. tmpbufsize = 0;
  319. for ( j = 0; (bvals != NULL) && (bvals[j] != NULL); j++ ) {
  320. int tmprc;
  321. size_t len;
  322. const struct berval *bvp = slapi_value_get_berval(bvals[j]);
  323. len = bvp->bv_len;
  324. if ( len < sizeof(buf) ) {
  325. realval = buf;
  326. strncpy( realval, bvp->bv_val, sizeof(buf) );
  327. } else if ( len < tmpbufsize ) {
  328. realval = tmpbuf;
  329. strncpy( realval, bvp->bv_val, tmpbufsize );
  330. } else {
  331. tmpbufsize = len + 1;
  332. realval = tmpbuf = (char *) slapi_ch_realloc( tmpbuf, tmpbufsize );
  333. strncpy( realval, bvp->bv_val, tmpbufsize );
  334. }
  335. /* 3rd arg: 1 - trim leading blanks */
  336. if (!(slapi_value_get_flags(bvals[j]) & SLAPI_ATTR_FLAG_NORMALIZED)) {
  337. value_normalize_ext( realval, syntax, 1, &alt );
  338. } else if (syntax & SYNTAX_DN) {
  339. slapi_dn_ignore_case(realval);
  340. }
  341. if (alt) {
  342. tmprc = slapi_re_exec( re, alt, time_up );
  343. slapi_ch_free_string(&alt);
  344. } else {
  345. tmprc = slapi_re_exec( re, realval, time_up );
  346. }
  347. if (slapi_is_loglevel_set(SLAPI_LOG_TRACE)) {
  348. char ebuf[BUFSIZ];
  349. LDAPDebug(LDAP_DEBUG_TRACE, "re_exec (%s) %i\n", escape_string(realval, ebuf), tmprc, 0);
  350. }
  351. if ( tmprc == 1 ) {
  352. rc = 0;
  353. break;
  354. } else if ( tmprc != 0 ) {
  355. rc = tmprc;
  356. break;
  357. }
  358. }
  359. bailout:
  360. if (free_re) {
  361. slapi_re_free(re);
  362. }
  363. slapi_ch_free((void**)&tmpbuf ); /* NULL is fine */
  364. slapi_ch_free((void**)&bigpat ); /* NULL is fine */
  365. LDAPDebug( LDAP_DEBUG_FILTER, "<= string_filter_sub %d\n",
  366. rc, 0, 0 );
  367. return( rc );
  368. }
  369. int
  370. string_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
  371. Slapi_Value ***ivals, int syntax, int ftype )
  372. {
  373. int nsubs, numbvals = 0, n;
  374. Slapi_Value **nbvals, **nbvlp;
  375. Slapi_Value **bvlp;
  376. char *w, *c, *p;
  377. char *alt = NULL;
  378. if (NULL == ivals) {
  379. return 1;
  380. }
  381. *ivals = NULL;
  382. if (NULL == bvals) {
  383. return 1;
  384. }
  385. switch ( ftype ) {
  386. case LDAP_FILTER_EQUALITY:
  387. /* allocate a new array for the normalized values */
  388. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  389. numbvals++;
  390. }
  391. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *));
  392. for ( bvlp = bvals, nbvlp = nbvals; bvlp && *bvlp; bvlp++, nbvlp++ )
  393. {
  394. unsigned long value_flags = slapi_value_get_flags(*bvlp);
  395. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  396. /* if the NORMALIZED flag is set, skip normalizing */
  397. if (!(value_flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  398. /* 3rd arg: 1 - trim leading blanks */
  399. value_normalize_ext( c, syntax, 1, &alt );
  400. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  401. } else if ((syntax & SYNTAX_DN) &&
  402. (value_flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  403. /* This dn value is normalized, but not case-normalized. */
  404. slapi_dn_ignore_case(c);
  405. /* This dn value is case-normalized */
  406. value_flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  407. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  408. }
  409. if (alt) {
  410. slapi_ch_free_string(&c);
  411. *nbvlp = slapi_value_new_string_passin(alt);
  412. alt = NULL;
  413. } else {
  414. *nbvlp = slapi_value_new_string_passin(c);
  415. c = NULL;
  416. }
  417. /* new value is normalized */
  418. slapi_value_set_flags(*nbvlp, value_flags);
  419. }
  420. *ivals = nbvals;
  421. break;
  422. case LDAP_FILTER_APPROX:
  423. /* XXX should not do this twice! XXX */
  424. /* get an upper bound on the number of ivals */
  425. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  426. for ( w = first_word( (char*)slapi_value_get_string(*bvlp) );
  427. w != NULL; w = next_word( w ) ) {
  428. numbvals++;
  429. }
  430. }
  431. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *) );
  432. n = 0;
  433. nbvlp = nbvals;
  434. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  435. for ( w = first_word( (char*)slapi_value_get_string(*bvlp) );
  436. w != NULL; w = next_word( w ) ) {
  437. if ( (c = phonetic( w )) != NULL ) {
  438. *nbvlp = slapi_value_new_string_passin(c);
  439. nbvlp++;
  440. }
  441. }
  442. }
  443. /* even if (n == 0), we should return the array nbvals w/ NULL items */
  444. *ivals = nbvals;
  445. break;
  446. case LDAP_FILTER_SUBSTRINGS:
  447. {
  448. /* XXX should remove duplicates! XXX */
  449. Slapi_Value *bvdup;
  450. const struct berval *bvp;
  451. char *buf;
  452. int i;
  453. int *substrlens = NULL;
  454. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  455. int maxsublen;
  456. /*
  457. * Substring key has 3 types:
  458. * begin (e.g., *^a)
  459. * middle (e.g., *abc)
  460. * end (e.g., *xy$)
  461. *
  462. * the each has its own key length, which can be configured as follows:
  463. * Usage: turn an index object to extensibleobject and
  464. * set an integer value for each.
  465. * dn: cn=sn, cn=index, cn=userRoot, cn=ldbm database, cn=plugins,
  466. * cn=config
  467. * objectClass: extensibleObject
  468. * nsSubStrBegin: 2
  469. * nsSubStrMiddle: 3
  470. * nsSubStrEnd: 2
  471. * [...]
  472. *
  473. * By default, begin == 3, middle == 3, end == 3 (defined in syntax.h)
  474. */
  475. /* If nsSubStrLen is specified in each index entry,
  476. respect the length for the substring index key length.
  477. Otherwise, the deafult value SUBLEN is used */
  478. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  479. if (NULL == substrlens) {
  480. substrlens = localsublens;
  481. }
  482. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  483. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  484. }
  485. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  486. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  487. }
  488. if (0 == substrlens[INDEX_SUBSTREND]) {
  489. substrlens[INDEX_SUBSTREND] = SUBEND;
  490. }
  491. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  492. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  493. buf = (char *)slapi_ch_calloc(1, maxsublen + 1);
  494. nsubs = 0;
  495. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  496. /*
  497. * Note: this calculation may err on the high side,
  498. * because value_normalize_ext(), which is called below
  499. * before we actually create the substring keys, may
  500. * reduce the length of the value in some cases or
  501. * increase the length in other cases. For example,
  502. * spaces are removed when space insensitive strings
  503. * are normalized. Or if the value includes '\"' (2 bytes),
  504. * it's normalized to '\22' (3 bytes). But it's okay
  505. * for nsubs to be too big. Since the ivals array is
  506. * NULL terminated, the only downside is that we
  507. * allocate more space than we really need.
  508. */
  509. nsubs += slapi_value_get_length(*bvlp) - substrlens[INDEX_SUBSTRMIDDLE] + 3;
  510. }
  511. nsubs += substrlens[INDEX_SUBSTRMIDDLE] * 2 - substrlens[INDEX_SUBSTRBEGIN] - substrlens[INDEX_SUBSTREND];
  512. *ivals = (Slapi_Value **) slapi_ch_calloc( (nsubs + 1), sizeof(Slapi_Value *) );
  513. n = 0;
  514. bvdup= slapi_value_new();
  515. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  516. unsigned long value_flags = slapi_value_get_flags(*bvlp);
  517. /* 3rd arg: 1 - trim leading blanks */
  518. if (!(value_flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  519. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  520. value_normalize_ext( c, syntax, 1, &alt );
  521. if (alt) {
  522. slapi_ch_free_string(&c);
  523. slapi_value_set_string_passin(bvdup, alt);
  524. alt = NULL;
  525. } else {
  526. slapi_value_set_string_passin(bvdup, c);
  527. c = NULL;
  528. }
  529. bvp = slapi_value_get_berval(bvdup);
  530. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  531. } else if ((syntax & SYNTAX_DN) &&
  532. (value_flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  533. /* This dn value is normalized, but not case-normalized. */
  534. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  535. slapi_dn_ignore_case(c);
  536. slapi_value_set_string_passin(bvdup, c);
  537. c = NULL;
  538. /* This dn value is case-normalized */
  539. value_flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  540. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  541. bvp = slapi_value_get_berval(bvdup);
  542. } else {
  543. bvp = slapi_value_get_berval(*bvlp);
  544. }
  545. /* leading */
  546. if ( bvp->bv_len > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  547. buf[0] = '^';
  548. for ( i = 0; i < substrlens[INDEX_SUBSTRBEGIN] - 1; i++ ) {
  549. buf[i + 1] = bvp->bv_val[i];
  550. }
  551. buf[substrlens[INDEX_SUBSTRBEGIN]] = '\0';
  552. (*ivals)[n] = slapi_value_new_string(buf);
  553. slapi_value_set_flags((*ivals)[n], value_flags);
  554. n++;
  555. }
  556. /* any */
  557. for ( p = bvp->bv_val;
  558. p < (bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTRMIDDLE] + 1);
  559. p++ ) {
  560. for ( i = 0; i < substrlens[INDEX_SUBSTRMIDDLE]; i++ ) {
  561. buf[i] = p[i];
  562. }
  563. buf[substrlens[INDEX_SUBSTRMIDDLE]] = '\0';
  564. (*ivals)[n] = slapi_value_new_string(buf);
  565. slapi_value_set_flags((*ivals)[n], value_flags);
  566. n++;
  567. }
  568. /* trailing */
  569. if ( bvp->bv_len > substrlens[INDEX_SUBSTREND] - 2 ) {
  570. p = bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTREND] + 1;
  571. for ( i = 0; i < substrlens[INDEX_SUBSTREND] - 1; i++ ) {
  572. buf[i] = p[i];
  573. }
  574. buf[substrlens[INDEX_SUBSTREND] - 1] = '$';
  575. buf[substrlens[INDEX_SUBSTREND]] = '\0';
  576. (*ivals)[n] = slapi_value_new_string(buf);
  577. slapi_value_set_flags((*ivals)[n], value_flags);
  578. n++;
  579. }
  580. }
  581. slapi_value_free(&bvdup);
  582. slapi_ch_free_string(&buf);
  583. }
  584. break;
  585. }
  586. return( 0 );
  587. }
  588. /* we've added code to make our equality filter processing faster */
  589. int
  590. string_assertion2keys_ava(
  591. Slapi_PBlock *pb,
  592. Slapi_Value *val,
  593. Slapi_Value ***ivals,
  594. int syntax,
  595. int ftype
  596. )
  597. {
  598. int i, numbvals;
  599. size_t len;
  600. char *w, *c;
  601. Slapi_Value *tmpval=NULL;
  602. char *alt = NULL;
  603. unsigned long flags = val ? slapi_value_get_flags(val) : 0;
  604. switch ( ftype ) {
  605. case LDAP_FILTER_EQUALITY_FAST:
  606. /* this code is trying to avoid multiple malloc/frees */
  607. len=slapi_value_get_length(val);
  608. tmpval=(*ivals)[0];
  609. if (len >= tmpval->bv.bv_len) {
  610. tmpval->bv.bv_val=(char *)slapi_ch_malloc(len+1);
  611. }
  612. memcpy(tmpval->bv.bv_val,slapi_value_get_string(val),len);
  613. tmpval->bv.bv_val[len]='\0';
  614. if (!(flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  615. /* 3rd arg: 1 - trim leading blanks */
  616. value_normalize_ext(tmpval->bv.bv_val, syntax, 1, &alt );
  617. if (alt) {
  618. if (len >= tmpval->bv.bv_len) {
  619. slapi_ch_free_string(&tmpval->bv.bv_val);
  620. }
  621. tmpval->bv.bv_val = alt;
  622. alt = NULL;
  623. }
  624. tmpval->bv.bv_len=strlen(tmpval->bv.bv_val);
  625. flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  626. } else if ((syntax & SYNTAX_DN) &&
  627. (flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  628. /* This dn value is normalized, but not case-normalized. */
  629. slapi_dn_ignore_case(tmpval->bv.bv_val);
  630. /* This dn value is case-normalized */
  631. flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  632. flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  633. }
  634. slapi_value_set_flags(tmpval, flags);
  635. break;
  636. case LDAP_FILTER_EQUALITY:
  637. (*ivals) = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
  638. (*ivals)[0] = val ? slapi_value_dup( val ) : NULL;
  639. if (val && !(flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  640. /* 3rd arg: 1 - trim leading blanks */
  641. value_normalize_ext( (*ivals)[0]->bv.bv_val, syntax, 1, &alt );
  642. if (alt) {
  643. slapi_ch_free_string(&(*ivals)[0]->bv.bv_val);
  644. (*ivals)[0]->bv.bv_val = alt;
  645. (*ivals)[0]->bv.bv_len = strlen( (*ivals)[0]->bv.bv_val );
  646. alt = NULL;
  647. }
  648. flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  649. } else if ((syntax & SYNTAX_DN) &&
  650. (flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  651. /* This dn value is normalized, but not case-normalized. */
  652. slapi_dn_ignore_case((*ivals)[0]->bv.bv_val);
  653. /* This dn value is case-normalized */
  654. flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  655. flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  656. }
  657. slapi_value_set_flags((*ivals)[0], flags);
  658. (*ivals)[1] = NULL;
  659. break;
  660. case LDAP_FILTER_APPROX:
  661. /* XXX should not do this twice! XXX */
  662. /* get an upper bound on the number of ivals */
  663. numbvals = 0;
  664. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  665. w = next_word( w ) ) {
  666. numbvals++;
  667. }
  668. (*ivals) = (Slapi_Value **) slapi_ch_malloc( (numbvals + 1) *
  669. sizeof(Slapi_Value *) );
  670. i = 0;
  671. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  672. w = next_word( w ) ) {
  673. if ( (c = phonetic( w )) != NULL ) {
  674. (*ivals)[i] = slapi_value_new_string_passin(c);
  675. i++;
  676. }
  677. }
  678. (*ivals)[i] = NULL;
  679. if ( i == 0 ) {
  680. slapi_ch_free((void**)ivals );
  681. return( 0 );
  682. }
  683. break;
  684. default:
  685. LDAPDebug( LDAP_DEBUG_ANY,
  686. "string_assertion2keys_ava: unknown ftype 0x%x\n",
  687. ftype, 0, 0 );
  688. break;
  689. }
  690. return( 0 );
  691. }
  692. int
  693. string_assertion2keys_sub(
  694. Slapi_PBlock *pb,
  695. char *initial,
  696. char **any,
  697. char *final,
  698. Slapi_Value ***ivals,
  699. int syntax
  700. )
  701. {
  702. int nsubs, i, len;
  703. int initiallen = 0, finallen = 0;
  704. int *substrlens = NULL;
  705. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  706. int maxsublen;
  707. char *comp_buf = NULL;
  708. /* altinit|any|final: store alt string from value_normalize_ext if any,
  709. * otherwise the original string. And use for the real job */
  710. char *altinit = NULL;
  711. char **altany = NULL;
  712. char *altfinal = NULL;
  713. /* oaltinit|any|final: prepared to free altinit|any|final if allocated in
  714. * value_normalize_ext */
  715. char *oaltinit = NULL;
  716. char **oaltany = NULL;
  717. char *oaltfinal = NULL;
  718. int anysize = 0;
  719. if (pb) {
  720. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  721. }
  722. if (NULL == substrlens) {
  723. substrlens = localsublens;
  724. }
  725. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  726. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  727. }
  728. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  729. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  730. }
  731. if (0 == substrlens[INDEX_SUBSTREND]) {
  732. substrlens[INDEX_SUBSTREND] = SUBEND;
  733. }
  734. *ivals = NULL;
  735. /*
  736. * First figure out how many keys we will return. The answer is based
  737. * on the length of each assertion value. Since normalization may
  738. * reduce the length (such as when spaces are removed from space
  739. * insensitive strings), we call value_normalize_ext() before checking
  740. * the length.
  741. */
  742. nsubs = 0;
  743. if ( initial != NULL ) {
  744. /* 3rd arg: 0 - DO NOT trim leading blanks */
  745. value_normalize_ext( initial, syntax, 0, &altinit );
  746. oaltinit = altinit;
  747. if (NULL == altinit) {
  748. altinit = initial;
  749. }
  750. initiallen = strlen( altinit );
  751. if ( initiallen > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  752. nsubs += 1; /* for the initial begin string key */
  753. /* the rest of the sub keys are "any" keys for this case */
  754. if ( initiallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  755. nsubs += initiallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  756. }
  757. } else {
  758. altinit = NULL; /* save some work later */
  759. }
  760. }
  761. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  762. anysize++;
  763. }
  764. altany = (char **)slapi_ch_calloc(anysize + 1, sizeof(char *));
  765. oaltany = (char **)slapi_ch_calloc(anysize + 1, sizeof(char *));
  766. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  767. /* 3rd arg: 0 - DO NOT trim leading blanks */
  768. value_normalize_ext( any[i], syntax, 0, &altany[i] );
  769. if (NULL == altany[i]) {
  770. altany[i] = any[i];
  771. } else {
  772. oaltany[i] = altany[i];
  773. }
  774. len = strlen( altany[i] );
  775. if ( len >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  776. nsubs += len - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  777. }
  778. }
  779. if ( final != NULL ) {
  780. /* 3rd arg: 0 - DO NOT trim leading blanks */
  781. value_normalize_ext( final, syntax, 0, &altfinal );
  782. oaltfinal = altfinal;
  783. if (NULL == altfinal) {
  784. altfinal = final;
  785. }
  786. finallen = strlen( altfinal );
  787. if ( finallen > substrlens[INDEX_SUBSTREND] - 2 ) {
  788. nsubs += 1; /* for the final end string key */
  789. /* the rest of the sub keys are "any" keys for this case */
  790. if ( finallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  791. nsubs += finallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  792. }
  793. } else {
  794. altfinal = NULL; /* save some work later */
  795. }
  796. }
  797. if ( nsubs == 0 ) { /* no keys to return */
  798. goto done;
  799. }
  800. /*
  801. * Next, allocated the ivals array and fill it in with the actual
  802. * keys. *ivals is a NULL terminated array of Slapi_Value pointers.
  803. */
  804. *ivals = (Slapi_Value **) slapi_ch_malloc( (nsubs + 1) * sizeof(Slapi_Value *) );
  805. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  806. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  807. nsubs = 0;
  808. comp_buf = (char *)slapi_ch_malloc(maxsublen + 1);
  809. if ( altinit != NULL ) {
  810. substring_comp_keys( ivals, &nsubs, altinit, initiallen, '^', syntax,
  811. comp_buf, substrlens );
  812. }
  813. for ( i = 0; altany != NULL && altany[i] != NULL; i++ ) {
  814. len = strlen( altany[i] );
  815. if ( len < substrlens[INDEX_SUBSTRMIDDLE] ) {
  816. continue;
  817. }
  818. substring_comp_keys( ivals, &nsubs, altany[i], len, 0, syntax,
  819. comp_buf, substrlens );
  820. }
  821. if ( altfinal != NULL ) {
  822. substring_comp_keys( ivals, &nsubs, altfinal, finallen, '$', syntax,
  823. comp_buf, substrlens );
  824. }
  825. (*ivals)[nsubs] = NULL;
  826. done:
  827. slapi_ch_free_string(&oaltinit);
  828. for ( i = 0; altany != NULL && altany[i] != NULL; i++ ) {
  829. slapi_ch_free_string(&oaltany[i]);
  830. }
  831. slapi_ch_free((void **)&oaltany);
  832. slapi_ch_free_string(&oaltfinal);
  833. slapi_ch_free((void **)&altany);
  834. slapi_ch_free_string(&comp_buf);
  835. return( 0 );
  836. }
  837. static void
  838. substring_comp_keys(
  839. Slapi_Value ***ivals,
  840. int *nsubs,
  841. char *str,
  842. int lenstr,
  843. int prepost,
  844. int syntax,
  845. char *comp_buf,
  846. int *substrlens
  847. )
  848. {
  849. int i, substrlen;
  850. char *p;
  851. PR_ASSERT(NULL != comp_buf);
  852. PR_ASSERT(NULL != substrlens);
  853. LDAPDebug( LDAP_DEBUG_TRACE, "=> substring_comp_keys (%s) %d\n",
  854. str, prepost, 0 );
  855. /* prepend ^ for initial substring */
  856. if ( prepost == '^' )
  857. {
  858. substrlen = substrlens[INDEX_SUBSTRBEGIN];
  859. comp_buf[0] = '^';
  860. for ( i = 0; i < substrlen - 1; i++ )
  861. {
  862. comp_buf[i + 1] = str[i];
  863. }
  864. comp_buf[substrlen] = '\0';
  865. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  866. (*nsubs)++;
  867. }
  868. substrlen = substrlens[INDEX_SUBSTRMIDDLE];
  869. for ( p = str; p < (str + lenstr - substrlen + 1); p++ )
  870. {
  871. for ( i = 0; i < substrlen; i++ )
  872. {
  873. comp_buf[i] = p[i];
  874. }
  875. comp_buf[substrlen] = '\0';
  876. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  877. (*nsubs)++;
  878. }
  879. if ( prepost == '$' )
  880. {
  881. substrlen = substrlens[INDEX_SUBSTREND];
  882. p = str + lenstr - substrlen + 1;
  883. for ( i = 0; i < substrlen - 1; i++ )
  884. {
  885. comp_buf[i] = p[i];
  886. }
  887. comp_buf[substrlen - 1] = '$';
  888. comp_buf[substrlen] = '\0';
  889. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  890. (*nsubs)++;
  891. }
  892. LDAPDebug( LDAP_DEBUG_TRACE, "<= substring_comp_keys\n", 0, 0, 0 );
  893. }