string.c 29 KB

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