string.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. }
  365. if (alt) {
  366. tmprc = slapi_re_exec( re, alt, time_up );
  367. slapi_ch_free_string(&alt);
  368. } else {
  369. tmprc = slapi_re_exec( re, realval, time_up );
  370. }
  371. LDAPDebug( LDAP_DEBUG_TRACE, "re_exec (%s) %i\n",
  372. escape_string( realval, ebuf ), tmprc, 0 );
  373. if ( tmprc == 1 ) {
  374. rc = 0;
  375. break;
  376. } else if ( tmprc != 0 ) {
  377. rc = tmprc;
  378. break;
  379. }
  380. }
  381. bailout:
  382. if (free_re) {
  383. slapi_re_free(re);
  384. }
  385. slapi_ch_free((void**)&tmpbuf ); /* NULL is fine */
  386. slapi_ch_free((void**)&bigpat ); /* NULL is fine */
  387. LDAPDebug( LDAP_DEBUG_FILTER, "<= string_filter_sub %d\n",
  388. rc, 0, 0 );
  389. return( rc );
  390. }
  391. int
  392. string_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
  393. Slapi_Value ***ivals, int syntax, int ftype )
  394. {
  395. int nsubs, numbvals = 0, n;
  396. Slapi_Value **nbvals, **nbvlp;
  397. Slapi_Value **bvlp;
  398. char *w, *c, *p;
  399. char *alt = NULL;
  400. if (NULL == ivals) {
  401. return 1;
  402. }
  403. *ivals = NULL;
  404. if (NULL == bvals) {
  405. return 1;
  406. }
  407. switch ( ftype ) {
  408. case LDAP_FILTER_EQUALITY:
  409. /* allocate a new array for the normalized values */
  410. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  411. numbvals++;
  412. }
  413. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *));
  414. for ( bvlp = bvals, nbvlp = nbvals; bvlp && *bvlp; bvlp++, nbvlp++ )
  415. {
  416. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  417. /* if the NORMALIZED flag is set, skip normalizing */
  418. if (!(slapi_value_get_flags(*bvlp) & SLAPI_ATTR_FLAG_NORMALIZED)) {
  419. /* 3rd arg: 1 - trim leading blanks */
  420. value_normalize_ext( c, syntax, 1, &alt );
  421. }
  422. if (alt) {
  423. slapi_ch_free_string(&c);
  424. *nbvlp = slapi_value_new_string_passin(alt);
  425. alt = NULL;
  426. } else {
  427. *nbvlp = slapi_value_new_string_passin(c);
  428. }
  429. /* new value is normalized */
  430. slapi_value_set_flags(*nbvlp, slapi_value_get_flags(*bvlp)|SLAPI_ATTR_FLAG_NORMALIZED);
  431. }
  432. *ivals = nbvals;
  433. break;
  434. case LDAP_FILTER_APPROX:
  435. /* XXX should not do this twice! XXX */
  436. /* get an upper bound on the number of ivals */
  437. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  438. for ( w = first_word( (char*)slapi_value_get_string(*bvlp) );
  439. w != NULL; w = next_word( w ) ) {
  440. numbvals++;
  441. }
  442. }
  443. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *) );
  444. n = 0;
  445. nbvlp = nbvals;
  446. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  447. for ( w = first_word( (char*)slapi_value_get_string(*bvlp) );
  448. w != NULL; w = next_word( w ) ) {
  449. if ( (c = phonetic( w )) != NULL ) {
  450. *nbvlp = slapi_value_new_string_passin(c);
  451. nbvlp++;
  452. }
  453. }
  454. }
  455. /* even if (n == 0), we should return the array nbvals w/ NULL items */
  456. *ivals = nbvals;
  457. break;
  458. case LDAP_FILTER_SUBSTRINGS:
  459. {
  460. /* XXX should remove duplicates! XXX */
  461. Slapi_Value *bvdup;
  462. const struct berval *bvp;
  463. char *buf;
  464. int i;
  465. int *substrlens = NULL;
  466. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  467. int maxsublen;
  468. /*
  469. * Substring key has 3 types:
  470. * begin (e.g., *^a)
  471. * middle (e.g., *abc)
  472. * end (e.g., *xy$)
  473. *
  474. * the each has its own key length, which can be configured as follows:
  475. * Usage: turn an index object to extensibleobject and
  476. * set an integer value for each.
  477. * dn: cn=sn, cn=index, cn=userRoot, cn=ldbm database, cn=plugins,
  478. * cn=config
  479. * objectClass: extensibleObject
  480. * nsSubStrBegin: 2
  481. * nsSubStrMiddle: 3
  482. * nsSubStrEnd: 2
  483. * [...]
  484. *
  485. * By default, begin == 3, middle == 3, end == 3 (defined in syntax.h)
  486. */
  487. /* If nsSubStrLen is specified in each index entry,
  488. respect the length for the substring index key length.
  489. Otherwise, the deafult value SUBLEN is used */
  490. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  491. if (NULL == substrlens) {
  492. substrlens = localsublens;
  493. }
  494. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  495. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  496. }
  497. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  498. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  499. }
  500. if (0 == substrlens[INDEX_SUBSTREND]) {
  501. substrlens[INDEX_SUBSTREND] = SUBEND;
  502. }
  503. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  504. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  505. buf = (char *)slapi_ch_calloc(1, maxsublen + 1);
  506. nsubs = 0;
  507. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  508. /*
  509. * Note: this calculation may err on the high side,
  510. * because value_normalize_ext(), which is called below
  511. * before we actually create the substring keys, may
  512. * reduce the length of the value in some cases or
  513. * increase the length in other cases. For example,
  514. * spaces are removed when space insensitive strings
  515. * are normalized. Or if the value includes '\"' (2 bytes),
  516. * it's normalized to '\22' (3 bytes). But it's okay
  517. * for nsubs to be too big. Since the ivals array is
  518. * NULL terminated, the only downside is that we
  519. * allocate more space than we really need.
  520. */
  521. nsubs += slapi_value_get_length(*bvlp) - substrlens[INDEX_SUBSTRMIDDLE] + 3;
  522. }
  523. nsubs += substrlens[INDEX_SUBSTRMIDDLE] * 2 - substrlens[INDEX_SUBSTRBEGIN] - substrlens[INDEX_SUBSTREND];
  524. *ivals = (Slapi_Value **) slapi_ch_calloc( (nsubs + 1), sizeof(Slapi_Value *) );
  525. n = 0;
  526. bvdup= slapi_value_new();
  527. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  528. /* 3rd arg: 1 - trim leading blanks */
  529. if (!(slapi_value_get_flags(*bvlp) & SLAPI_ATTR_FLAG_NORMALIZED)) {
  530. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  531. value_normalize_ext( c, syntax, 1, &alt );
  532. if (alt) {
  533. slapi_ch_free_string(&c);
  534. slapi_value_set_string_passin(bvdup, alt);
  535. alt = NULL;
  536. } else {
  537. slapi_value_set_string_passin(bvdup, c);
  538. c = NULL;
  539. }
  540. bvp = slapi_value_get_berval(bvdup);
  541. } else {
  542. bvp = slapi_value_get_berval(*bvlp);
  543. }
  544. /* leading */
  545. if ( bvp->bv_len > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  546. buf[0] = '^';
  547. for ( i = 0; i < substrlens[INDEX_SUBSTRBEGIN] - 1; i++ ) {
  548. buf[i + 1] = bvp->bv_val[i];
  549. }
  550. buf[substrlens[INDEX_SUBSTRBEGIN]] = '\0';
  551. (*ivals)[n] = slapi_value_new_string(buf);
  552. slapi_value_set_flags((*ivals)[n], slapi_value_get_flags(*bvlp)|SLAPI_ATTR_FLAG_NORMALIZED);
  553. n++;
  554. }
  555. /* any */
  556. for ( p = bvp->bv_val;
  557. p < (bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTRMIDDLE] + 1);
  558. p++ ) {
  559. for ( i = 0; i < substrlens[INDEX_SUBSTRMIDDLE]; i++ ) {
  560. buf[i] = p[i];
  561. }
  562. buf[substrlens[INDEX_SUBSTRMIDDLE]] = '\0';
  563. (*ivals)[n] = slapi_value_new_string(buf);
  564. slapi_value_set_flags((*ivals)[n], slapi_value_get_flags(*bvlp)|SLAPI_ATTR_FLAG_NORMALIZED);
  565. n++;
  566. }
  567. /* trailing */
  568. if ( bvp->bv_len > substrlens[INDEX_SUBSTREND] - 2 ) {
  569. p = bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTREND] + 1;
  570. for ( i = 0; i < substrlens[INDEX_SUBSTREND] - 1; i++ ) {
  571. buf[i] = p[i];
  572. }
  573. buf[substrlens[INDEX_SUBSTREND] - 1] = '$';
  574. buf[substrlens[INDEX_SUBSTREND]] = '\0';
  575. (*ivals)[n] = slapi_value_new_string(buf);
  576. slapi_value_set_flags((*ivals)[n], slapi_value_get_flags(*bvlp)|SLAPI_ATTR_FLAG_NORMALIZED);
  577. n++;
  578. }
  579. }
  580. slapi_value_free(&bvdup);
  581. slapi_ch_free_string(&buf);
  582. }
  583. break;
  584. }
  585. return( 0 );
  586. }
  587. /* we've added code to make our equality filter processing faster */
  588. int
  589. string_assertion2keys_ava(
  590. Slapi_PBlock *pb,
  591. Slapi_Value *val,
  592. Slapi_Value ***ivals,
  593. int syntax,
  594. int ftype
  595. )
  596. {
  597. int i, numbvals;
  598. size_t len;
  599. char *w, *c;
  600. Slapi_Value *tmpval=NULL;
  601. char *alt = NULL;
  602. unsigned long flags = val ? slapi_value_get_flags(val) : 0;
  603. switch ( ftype ) {
  604. case LDAP_FILTER_EQUALITY_FAST:
  605. /* this code is trying to avoid multiple malloc/frees */
  606. len=slapi_value_get_length(val);
  607. tmpval=(*ivals)[0];
  608. if (len >= tmpval->bv.bv_len) {
  609. tmpval->bv.bv_val=(char *)slapi_ch_malloc(len+1);
  610. }
  611. memcpy(tmpval->bv.bv_val,slapi_value_get_string(val),len);
  612. tmpval->bv.bv_val[len]='\0';
  613. if (!(flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  614. /* 3rd arg: 1 - trim leading blanks */
  615. value_normalize_ext(tmpval->bv.bv_val, syntax, 1, &alt );
  616. if (alt) {
  617. if (len >= tmpval->bv.bv_len) {
  618. slapi_ch_free_string(&tmpval->bv.bv_val);
  619. }
  620. tmpval->bv.bv_val = alt;
  621. alt = NULL;
  622. }
  623. tmpval->bv.bv_len=strlen(tmpval->bv.bv_val);
  624. }
  625. slapi_value_set_flags(tmpval, flags|SLAPI_ATTR_FLAG_NORMALIZED);
  626. break;
  627. case LDAP_FILTER_EQUALITY:
  628. (*ivals) = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
  629. (*ivals)[0] = val ? slapi_value_dup( val ) : NULL;
  630. if (val && !(flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  631. /* 3rd arg: 1 - trim leading blanks */
  632. value_normalize_ext( (*ivals)[0]->bv.bv_val, syntax, 1, &alt );
  633. if (alt) {
  634. slapi_ch_free_string(&(*ivals)[0]->bv.bv_val);
  635. (*ivals)[0]->bv.bv_val = alt;
  636. (*ivals)[0]->bv.bv_len = strlen( (*ivals)[0]->bv.bv_val );
  637. alt = NULL;
  638. }
  639. slapi_value_set_flags((*ivals)[0], flags|SLAPI_ATTR_FLAG_NORMALIZED);
  640. }
  641. (*ivals)[1] = NULL;
  642. break;
  643. case LDAP_FILTER_APPROX:
  644. /* XXX should not do this twice! XXX */
  645. /* get an upper bound on the number of ivals */
  646. numbvals = 0;
  647. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  648. w = next_word( w ) ) {
  649. numbvals++;
  650. }
  651. (*ivals) = (Slapi_Value **) slapi_ch_malloc( (numbvals + 1) *
  652. sizeof(Slapi_Value *) );
  653. i = 0;
  654. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  655. w = next_word( w ) ) {
  656. if ( (c = phonetic( w )) != NULL ) {
  657. (*ivals)[i] = slapi_value_new_string_passin(c);
  658. i++;
  659. }
  660. }
  661. (*ivals)[i] = NULL;
  662. if ( i == 0 ) {
  663. slapi_ch_free((void**)ivals );
  664. return( 0 );
  665. }
  666. break;
  667. default:
  668. LDAPDebug( LDAP_DEBUG_ANY,
  669. "string_assertion2keys_ava: unknown ftype 0x%x\n",
  670. ftype, 0, 0 );
  671. break;
  672. }
  673. return( 0 );
  674. }
  675. int
  676. string_assertion2keys_sub(
  677. Slapi_PBlock *pb,
  678. char *initial,
  679. char **any,
  680. char *final,
  681. Slapi_Value ***ivals,
  682. int syntax
  683. )
  684. {
  685. int nsubs, i, len;
  686. int initiallen = 0, finallen = 0;
  687. int *substrlens = NULL;
  688. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  689. int maxsublen;
  690. char *comp_buf = NULL;
  691. /* altinit|any|final: store alt string from value_normalize_ext if any,
  692. * otherwise the original string. And use for the real job */
  693. char *altinit = NULL;
  694. char **altany = NULL;
  695. char *altfinal = NULL;
  696. /* oaltinit|any|final: prepared to free altinit|any|final if allocated in
  697. * value_normalize_ext */
  698. char *oaltinit = NULL;
  699. char **oaltany = NULL;
  700. char *oaltfinal = NULL;
  701. int anysize = 0;
  702. if (pb) {
  703. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  704. }
  705. if (NULL == substrlens) {
  706. substrlens = localsublens;
  707. }
  708. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  709. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  710. }
  711. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  712. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  713. }
  714. if (0 == substrlens[INDEX_SUBSTREND]) {
  715. substrlens[INDEX_SUBSTREND] = SUBEND;
  716. }
  717. *ivals = NULL;
  718. /*
  719. * First figure out how many keys we will return. The answer is based
  720. * on the length of each assertion value. Since normalization may
  721. * reduce the length (such as when spaces are removed from space
  722. * insensitive strings), we call value_normalize_ext() before checking
  723. * the length.
  724. */
  725. nsubs = 0;
  726. if ( initial != NULL ) {
  727. /* 3rd arg: 0 - DO NOT trim leading blanks */
  728. value_normalize_ext( initial, syntax, 0, &altinit );
  729. oaltinit = altinit;
  730. if (NULL == altinit) {
  731. altinit = initial;
  732. }
  733. initiallen = strlen( altinit );
  734. if ( initiallen > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  735. nsubs += 1; /* for the initial begin string key */
  736. /* the rest of the sub keys are "any" keys for this case */
  737. if ( initiallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  738. nsubs += initiallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  739. }
  740. } else {
  741. altinit = NULL; /* save some work later */
  742. }
  743. }
  744. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  745. anysize++;
  746. }
  747. altany = (char **)slapi_ch_calloc(anysize + 1, sizeof(char *));
  748. oaltany = (char **)slapi_ch_calloc(anysize + 1, sizeof(char *));
  749. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  750. /* 3rd arg: 0 - DO NOT trim leading blanks */
  751. value_normalize_ext( any[i], syntax, 0, &altany[i] );
  752. if (NULL == altany[i]) {
  753. altany[i] = any[i];
  754. } else {
  755. oaltany[i] = altany[i];
  756. }
  757. len = strlen( altany[i] );
  758. if ( len >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  759. nsubs += len - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  760. }
  761. }
  762. if ( final != NULL ) {
  763. /* 3rd arg: 0 - DO NOT trim leading blanks */
  764. value_normalize_ext( final, syntax, 0, &altfinal );
  765. oaltfinal = altfinal;
  766. if (NULL == altfinal) {
  767. altfinal = final;
  768. }
  769. finallen = strlen( altfinal );
  770. if ( finallen > substrlens[INDEX_SUBSTREND] - 2 ) {
  771. nsubs += 1; /* for the final end string key */
  772. /* the rest of the sub keys are "any" keys for this case */
  773. if ( finallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  774. nsubs += finallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  775. }
  776. } else {
  777. altfinal = NULL; /* save some work later */
  778. }
  779. }
  780. if ( nsubs == 0 ) { /* no keys to return */
  781. goto done;
  782. }
  783. /*
  784. * Next, allocated the ivals array and fill it in with the actual
  785. * keys. *ivals is a NULL terminated array of Slapi_Value pointers.
  786. */
  787. *ivals = (Slapi_Value **) slapi_ch_malloc( (nsubs + 1) * sizeof(Slapi_Value *) );
  788. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  789. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  790. nsubs = 0;
  791. comp_buf = (char *)slapi_ch_malloc(maxsublen + 1);
  792. if ( altinit != NULL ) {
  793. substring_comp_keys( ivals, &nsubs, altinit, initiallen, '^', syntax,
  794. comp_buf, substrlens );
  795. }
  796. for ( i = 0; altany != NULL && altany[i] != NULL; i++ ) {
  797. len = strlen( altany[i] );
  798. if ( len < substrlens[INDEX_SUBSTRMIDDLE] ) {
  799. continue;
  800. }
  801. substring_comp_keys( ivals, &nsubs, altany[i], len, 0, syntax,
  802. comp_buf, substrlens );
  803. }
  804. if ( altfinal != NULL ) {
  805. substring_comp_keys( ivals, &nsubs, altfinal, finallen, '$', syntax,
  806. comp_buf, substrlens );
  807. }
  808. (*ivals)[nsubs] = NULL;
  809. done:
  810. slapi_ch_free_string(&oaltinit);
  811. for ( i = 0; altany != NULL && altany[i] != NULL; i++ ) {
  812. slapi_ch_free_string(&oaltany[i]);
  813. }
  814. slapi_ch_free((void **)&oaltany);
  815. slapi_ch_free_string(&oaltfinal);
  816. slapi_ch_free((void **)&altany);
  817. slapi_ch_free_string(&comp_buf);
  818. return( 0 );
  819. }
  820. static void
  821. substring_comp_keys(
  822. Slapi_Value ***ivals,
  823. int *nsubs,
  824. char *str,
  825. int lenstr,
  826. int prepost,
  827. int syntax,
  828. char *comp_buf,
  829. int *substrlens
  830. )
  831. {
  832. int i, substrlen;
  833. char *p;
  834. PR_ASSERT(NULL != comp_buf);
  835. PR_ASSERT(NULL != substrlens);
  836. LDAPDebug( LDAP_DEBUG_TRACE, "=> substring_comp_keys (%s) %d\n",
  837. str, prepost, 0 );
  838. /* prepend ^ for initial substring */
  839. if ( prepost == '^' )
  840. {
  841. substrlen = substrlens[INDEX_SUBSTRBEGIN];
  842. comp_buf[0] = '^';
  843. for ( i = 0; i < substrlen - 1; i++ )
  844. {
  845. comp_buf[i + 1] = str[i];
  846. }
  847. comp_buf[substrlen] = '\0';
  848. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  849. (*nsubs)++;
  850. }
  851. substrlen = substrlens[INDEX_SUBSTRMIDDLE];
  852. for ( p = str; p < (str + lenstr - substrlen + 1); p++ )
  853. {
  854. for ( i = 0; i < substrlen; i++ )
  855. {
  856. comp_buf[i] = p[i];
  857. }
  858. comp_buf[substrlen] = '\0';
  859. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  860. (*nsubs)++;
  861. }
  862. if ( prepost == '$' )
  863. {
  864. substrlen = substrlens[INDEX_SUBSTREND];
  865. p = str + lenstr - substrlen + 1;
  866. for ( i = 0; i < substrlen - 1; i++ )
  867. {
  868. comp_buf[i] = p[i];
  869. }
  870. comp_buf[substrlen - 1] = '$';
  871. comp_buf[substrlen] = '\0';
  872. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  873. (*nsubs)++;
  874. }
  875. LDAPDebug( LDAP_DEBUG_TRACE, "<= substring_comp_keys\n", 0, 0, 0 );
  876. }