string.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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;
  60. if(retVal) {
  61. *retVal = NULL;
  62. }
  63. if ( ftype == LDAP_FILTER_APPROX ) {
  64. return( string_filter_approx( bvfilter, bvals, retVal ) );
  65. }
  66. bvfilter_norm.bv_val = slapi_ch_malloc( bvfilter->bv_len + 1 );
  67. SAFEMEMCPY( bvfilter_norm.bv_val, bvfilter->bv_val, bvfilter->bv_len );
  68. bvfilter_norm.bv_val[bvfilter->bv_len] = '\0';
  69. value_normalize( bvfilter_norm.bv_val, syntax, 1 /* trim leading blanks */ );
  70. bvfilter_norm.bv_len = strlen(bvfilter_norm.bv_val);
  71. for ( i = 0; bvals[i] != NULL; i++ ) {
  72. rc = value_cmp( (struct berval*)slapi_value_get_berval(bvals[i]), &bvfilter_norm, syntax, 1/* Normalise the first value only */ );
  73. switch ( ftype ) {
  74. case LDAP_FILTER_GE:
  75. if ( rc >= 0 ) {
  76. if(retVal) {
  77. *retVal = bvals[i];
  78. }
  79. slapi_ch_free ((void**)&bvfilter_norm.bv_val);
  80. return( 0 );
  81. }
  82. break;
  83. case LDAP_FILTER_LE:
  84. if ( rc <= 0 ) {
  85. if(retVal) {
  86. *retVal = bvals[i];
  87. }
  88. slapi_ch_free ((void**)&bvfilter_norm.bv_val);
  89. return( 0 );
  90. }
  91. break;
  92. case LDAP_FILTER_EQUALITY:
  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. }
  102. }
  103. slapi_ch_free ((void**)&bvfilter_norm.bv_val);
  104. return( -1 );
  105. }
  106. static int
  107. string_filter_approx( struct berval *bvfilter, Slapi_Value **bvals,
  108. Slapi_Value **retVal)
  109. {
  110. int i, rc;
  111. int ava_wordcount;
  112. char *w1, *w2, *c1, *c2;
  113. /*
  114. * try to match words in each filter value in order
  115. * in the attribute value.
  116. * XXX should do this once for the filter and save it XXX
  117. */
  118. rc = -1;
  119. if(retVal) {
  120. *retVal = NULL;
  121. }
  122. for ( i = 0; bvals[i] != NULL; i++ ) {
  123. w2 = (char*)slapi_value_get_string(bvals[i]); /* JCM cast */
  124. ava_wordcount = 0;
  125. /* for each word in the filter value */
  126. for ( w1 = first_word( bvfilter->bv_val ); w1 != NULL;
  127. w1 = next_word( w1 ) ) {
  128. ++ava_wordcount;
  129. if ( (c1 = phonetic( w1 )) == NULL ) {
  130. break;
  131. }
  132. /*
  133. * for each word in the attribute value from
  134. * where we left off...
  135. */
  136. for ( w2 = first_word( w2 ); w2 != NULL;
  137. w2 = next_word( w2 ) ) {
  138. c2 = phonetic( w2 );
  139. rc = strcmp( c1, c2 );
  140. slapi_ch_free((void**)&c2 );
  141. if ( rc == 0 ) {
  142. if(retVal) {
  143. *retVal = bvals[i];
  144. }
  145. break;
  146. }
  147. }
  148. slapi_ch_free((void**)&c1 );
  149. /*
  150. * if we stopped because we ran out of words
  151. * before making a match, go on to the next
  152. * value. otherwise try to keep matching
  153. * words in this value from where we left off.
  154. */
  155. if ( w2 == NULL ) {
  156. break;
  157. } else {
  158. w2 = next_word( w2 );
  159. }
  160. }
  161. /*
  162. * if we stopped because we ran out of words and
  163. * we found at leasy one word, we have a match.
  164. */
  165. if ( w1 == NULL && ava_wordcount > 0 ) {
  166. rc = 0;
  167. break;
  168. }
  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, *bigpat = NULL;
  180. size_t tmpbufsize;
  181. char pat[BUFSIZ];
  182. char buf[BUFSIZ];
  183. char ebuf[BUFSIZ];
  184. time_t curtime = 0;
  185. time_t time_up = 0;
  186. time_t optime = 0; /* time op was initiated */
  187. int timelimit = 0; /* search timelimit */
  188. Operation *op = NULL;
  189. LDAPDebug( LDAP_DEBUG_FILTER, "=> string_filter_sub\n",
  190. 0, 0, 0 );
  191. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  192. if (NULL != op) {
  193. slapi_pblock_get( pb, SLAPI_SEARCH_TIMELIMIT, &timelimit );
  194. slapi_pblock_get( pb, SLAPI_OPINITIATED_TIME, &optime );
  195. } else {
  196. /* timelimit is not passed via pblock */
  197. timelimit = -1;
  198. }
  199. /*
  200. * (timelimit==-1) means no time limit
  201. */
  202. time_up = ( timelimit==-1 ? -1 : optime + timelimit);
  203. /*
  204. * construct a regular expression corresponding to the
  205. * filter and let regex do the work for each value
  206. * XXX should do this once and save it somewhere XXX
  207. */
  208. pat[0] = '\0';
  209. p = pat;
  210. end = pat + sizeof(pat) - 2; /* leave room for null */
  211. if ( initial != NULL ) {
  212. size = strlen( initial ) + 1; /* add 1 for "^" */
  213. }
  214. if ( any != NULL ) {
  215. i = 0;
  216. while ( any[i] ) {
  217. size += strlen(any[i++]) + 2; /* add 2 for ".*" */
  218. }
  219. }
  220. if ( final != NULL ) {
  221. size += strlen( final ) + 3; /* add 3 for ".*" and "$" */
  222. }
  223. size *= 2; /* doubled in case all filter chars need escaping */
  224. size++; /* add 1 for null */
  225. if ( p + size > end ) {
  226. bigpat = slapi_ch_malloc( size );
  227. p = bigpat;
  228. }
  229. if ( initial != NULL ) {
  230. value_normalize( initial, syntax, 1 /* trim leading blanks */ );
  231. *p++ = '^';
  232. filter_strcpy_special( p, initial );
  233. p = strchr( p, '\0' );
  234. }
  235. if ( any != NULL ) {
  236. for ( i = 0; any[i] != NULL; i++ ) {
  237. value_normalize( any[i], syntax, 0 /* DO NOT trim leading blanks */ );
  238. /* ".*" + value */
  239. *p++ = '.';
  240. *p++ = '*';
  241. filter_strcpy_special( p, any[i] );
  242. p = strchr( p, '\0' );
  243. }
  244. }
  245. if ( final != NULL ) {
  246. value_normalize( final, syntax, 0 /* DO NOT trim leading blanks */ );
  247. /* ".*" + value */
  248. *p++ = '.';
  249. *p++ = '*';
  250. filter_strcpy_special( p, final );
  251. strcat( p, "$" );
  252. }
  253. /* compile the regex */
  254. p = (bigpat) ? bigpat : pat;
  255. slapd_re_lock();
  256. if ( (tmpbuf = slapd_re_comp( p )) != 0 ) {
  257. LDAPDebug( LDAP_DEBUG_ANY, "re_comp (%s) failed (%s): %s\n",
  258. pat, p, tmpbuf );
  259. rc = LDAP_OPERATIONS_ERROR;
  260. goto bailout;
  261. } else {
  262. LDAPDebug( LDAP_DEBUG_TRACE, "re_comp (%s)\n",
  263. escape_string( p, ebuf ), 0, 0 );
  264. }
  265. curtime = current_time();
  266. if ( time_up != -1 && curtime > time_up ) {
  267. rc = LDAP_TIMELIMIT_EXCEEDED;
  268. goto bailout;
  269. }
  270. /*
  271. * test the regex against each value
  272. */
  273. rc = -1;
  274. tmpbuf = NULL;
  275. tmpbufsize = 0;
  276. for ( j = 0; bvals[j] != NULL; j++ ) {
  277. int tmprc;
  278. size_t len;
  279. const struct berval *bvp = slapi_value_get_berval(bvals[j]);
  280. len = bvp->bv_len;
  281. if ( len < sizeof(buf) ) {
  282. strcpy( buf, bvp->bv_val );
  283. realval = buf;
  284. } else if ( len < tmpbufsize ) {
  285. strcpy( buf, bvp->bv_val );
  286. realval = tmpbuf;
  287. } else {
  288. tmpbuf = (char *) slapi_ch_realloc( tmpbuf, len + 1 );
  289. strcpy( tmpbuf, bvp->bv_val );
  290. realval = tmpbuf;
  291. }
  292. value_normalize( realval, syntax, 1 /* trim leading blanks */ );
  293. tmprc = slapd_re_exec( realval, time_up );
  294. LDAPDebug( LDAP_DEBUG_TRACE, "re_exec (%s) %i\n",
  295. escape_string( realval, ebuf ), tmprc, 0 );
  296. if ( tmprc == 1 ) {
  297. rc = 0;
  298. break;
  299. } else if ( tmprc != 0 ) {
  300. rc = tmprc;
  301. break;
  302. }
  303. }
  304. bailout:
  305. slapd_re_unlock();
  306. slapi_ch_free((void**)&tmpbuf ); /* NULL is fine */
  307. slapi_ch_free((void**)&bigpat ); /* NULL is fine */
  308. LDAPDebug( LDAP_DEBUG_FILTER, "<= string_filter_sub %d\n",
  309. rc, 0, 0 );
  310. return( rc );
  311. }
  312. int
  313. string_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
  314. Slapi_Value ***ivals, int syntax, int ftype )
  315. {
  316. int nsubs, numbvals = 0, n;
  317. Slapi_Value **nbvals, **nbvlp;
  318. Slapi_Value **bvlp;
  319. char *w, *c, *p;
  320. switch ( ftype ) {
  321. case LDAP_FILTER_EQUALITY:
  322. /* allocate a new array for the normalized values */
  323. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  324. numbvals++;
  325. }
  326. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *));
  327. for ( bvlp = bvals, nbvlp = nbvals; bvlp && *bvlp; bvlp++, nbvlp++ )
  328. {
  329. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  330. /* if the NORMALIZED flag is set, skip normalizing */
  331. if (!(slapi_value_get_flags(*bvlp) & SLAPI_ATTR_FLAG_NORMALIZED))
  332. value_normalize( c, syntax, 1 /* trim leading blanks */ );
  333. *nbvlp = slapi_value_new_string_passin(c);
  334. }
  335. *ivals = nbvals;
  336. break;
  337. case LDAP_FILTER_APPROX:
  338. /* XXX should not do this twice! XXX */
  339. /* get an upper bound on the number of ivals */
  340. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  341. for ( w = first_word( (char*)slapi_value_get_string(*bvlp) );
  342. w != NULL; w = next_word( w ) ) {
  343. numbvals++;
  344. }
  345. }
  346. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *) );
  347. n = 0;
  348. nbvlp = nbvals;
  349. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  350. for ( w = first_word( (char*)slapi_value_get_string(*bvlp) );
  351. w != NULL; w = next_word( w ) ) {
  352. if ( (c = phonetic( w )) != NULL ) {
  353. *nbvlp = slapi_value_new_string_passin(c);
  354. nbvlp++;
  355. }
  356. }
  357. }
  358. /* even if (n == 0), we should return the array nbvals w/ NULL items */
  359. *ivals = nbvals;
  360. break;
  361. case LDAP_FILTER_SUBSTRINGS:
  362. {
  363. /* XXX should remove duplicates! XXX */
  364. Slapi_Value *bvdup;
  365. const struct berval *bvp;
  366. char *buf;
  367. int i;
  368. int *substrlens = NULL;
  369. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  370. int maxsublen;
  371. /*
  372. * Substring key has 3 types:
  373. * begin (e.g., *^a)
  374. * middle (e.g., *abc)
  375. * end (e.g., *xy$)
  376. *
  377. * the each has its own key length, which can be configured as follows:
  378. * Usage: turn an index object to extensibleobject and
  379. * set an integer value for each.
  380. * dn: cn=sn, cn=index, cn=userRoot, cn=ldbm database, cn=plugins,
  381. * cn=config
  382. * objectClass: extensibleObject
  383. * nsSubStrBegin: 2
  384. * nsSubStrMiddle: 3
  385. * nsSubStrEnd: 2
  386. * [...]
  387. *
  388. * By default, begin == 2, middle == 3, end == 2 (defined in syntax.h)
  389. */
  390. /* If nsSubStrLen is specified in each index entry,
  391. respect the length for the substring index key length.
  392. Otherwise, the deafult value SUBLEN is used */
  393. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  394. if (NULL == substrlens) {
  395. substrlens = localsublens;
  396. }
  397. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  398. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  399. }
  400. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  401. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  402. }
  403. if (0 == substrlens[INDEX_SUBSTREND]) {
  404. substrlens[INDEX_SUBSTREND] = SUBEND;
  405. }
  406. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  407. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  408. buf = (char *)slapi_ch_calloc(1, maxsublen + 1);
  409. nsubs = 0;
  410. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  411. /*
  412. * Note: this calculation may err on the high side,
  413. * because value_normalize(), which is called below
  414. * before we actually create the substring keys, may
  415. * reduce the length of the value in some cases. For
  416. * example, spaces are removed when space insensitive
  417. * strings are normalized. But it's okay for nsubs to
  418. * be too big. Since the ivals array is NULL terminated,
  419. * the only downside is that we allocate more space than
  420. * we really need.
  421. */
  422. nsubs += slapi_value_get_length(*bvlp) - substrlens[INDEX_SUBSTRMIDDLE] + 3;
  423. }
  424. nsubs += substrlens[INDEX_SUBSTRMIDDLE] * 2 - substrlens[INDEX_SUBSTRBEGIN] - substrlens[INDEX_SUBSTREND];
  425. *ivals = (Slapi_Value **) slapi_ch_calloc( (nsubs + 1), sizeof(Slapi_Value *) );
  426. n = 0;
  427. bvdup= slapi_value_new();
  428. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  429. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  430. value_normalize( c, syntax, 1 /* trim leading blanks */ );
  431. slapi_value_set_string_passin(bvdup, c);
  432. bvp = slapi_value_get_berval(bvdup);
  433. /* leading */
  434. if ( bvp->bv_len > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  435. buf[0] = '^';
  436. for ( i = 0; i < substrlens[INDEX_SUBSTRBEGIN] - 1; i++ ) {
  437. buf[i + 1] = bvp->bv_val[i];
  438. }
  439. buf[substrlens[INDEX_SUBSTRBEGIN]] = '\0';
  440. (*ivals)[n] = slapi_value_new_string(buf);
  441. n++;
  442. }
  443. /* any */
  444. for ( p = bvp->bv_val;
  445. p < (bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTRMIDDLE] + 1);
  446. p++ ) {
  447. for ( i = 0; i < substrlens[INDEX_SUBSTRMIDDLE]; i++ ) {
  448. buf[i] = p[i];
  449. }
  450. buf[substrlens[INDEX_SUBSTRMIDDLE]] = '\0';
  451. (*ivals)[n] = slapi_value_new_string(buf);
  452. n++;
  453. }
  454. /* trailing */
  455. if ( bvp->bv_len > substrlens[INDEX_SUBSTREND] - 2 ) {
  456. p = bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTREND] + 1;
  457. for ( i = 0; i < substrlens[INDEX_SUBSTREND] - 1; i++ ) {
  458. buf[i] = p[i];
  459. }
  460. buf[substrlens[INDEX_SUBSTREND] - 1] = '$';
  461. buf[substrlens[INDEX_SUBSTREND]] = '\0';
  462. (*ivals)[n] = slapi_value_new_string(buf);
  463. n++;
  464. }
  465. }
  466. slapi_value_free(&bvdup);
  467. slapi_ch_free_string(&buf);
  468. }
  469. break;
  470. }
  471. return( 0 );
  472. }
  473. /* we've added code to make our equality filter processing faster */
  474. int
  475. string_assertion2keys_ava(
  476. Slapi_PBlock *pb,
  477. Slapi_Value *val,
  478. Slapi_Value ***ivals,
  479. int syntax,
  480. int ftype
  481. )
  482. {
  483. int i, numbvals;
  484. size_t len;
  485. char *w, *c;
  486. Slapi_Value *tmpval=NULL;
  487. switch ( ftype ) {
  488. case LDAP_FILTER_EQUALITY_FAST:
  489. /* this code is trying to avoid multiple malloc/frees */
  490. len=slapi_value_get_length(val);
  491. tmpval=(*ivals)[0];
  492. if (len >= tmpval->bv.bv_len) {
  493. tmpval->bv.bv_val=(char *)slapi_ch_malloc(len+1);
  494. }
  495. memcpy(tmpval->bv.bv_val,slapi_value_get_string(val),len);
  496. tmpval->bv.bv_val[len]='\0';
  497. value_normalize(tmpval->bv.bv_val, syntax, 1 /* trim leading blanks */ );
  498. tmpval->bv.bv_len=strlen(tmpval->bv.bv_val);
  499. break;
  500. case LDAP_FILTER_EQUALITY:
  501. (*ivals) = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
  502. (*ivals)[0] = slapi_value_dup( val );
  503. value_normalize( (*ivals)[0]->bv.bv_val, syntax, 1 /* trim leading blanks */ );
  504. (*ivals)[0]->bv.bv_len = strlen( (*ivals)[0]->bv.bv_val );
  505. (*ivals)[1] = NULL;
  506. break;
  507. case LDAP_FILTER_APPROX:
  508. /* XXX should not do this twice! XXX */
  509. /* get an upper bound on the number of ivals */
  510. numbvals = 0;
  511. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  512. w = next_word( w ) ) {
  513. numbvals++;
  514. }
  515. (*ivals) = (Slapi_Value **) slapi_ch_malloc( (numbvals + 1) *
  516. sizeof(Slapi_Value *) );
  517. i = 0;
  518. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  519. w = next_word( w ) ) {
  520. if ( (c = phonetic( w )) != NULL ) {
  521. (*ivals)[i] = slapi_value_new_string_passin(c);
  522. i++;
  523. }
  524. }
  525. (*ivals)[i] = NULL;
  526. if ( i == 0 ) {
  527. slapi_ch_free((void**)ivals );
  528. return( 0 );
  529. }
  530. break;
  531. default:
  532. LDAPDebug( LDAP_DEBUG_ANY,
  533. "string_assertion2keys_ava: unknown ftype 0x%x\n",
  534. ftype, 0, 0 );
  535. break;
  536. }
  537. return( 0 );
  538. }
  539. int
  540. string_assertion2keys_sub(
  541. Slapi_PBlock *pb,
  542. char *initial,
  543. char **any,
  544. char *final,
  545. Slapi_Value ***ivals,
  546. int syntax
  547. )
  548. {
  549. int nsubs, i, len;
  550. int initiallen = 0, finallen = 0;
  551. int *substrlens = NULL;
  552. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  553. int maxsublen;
  554. char *comp_buf = NULL;
  555. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  556. if (NULL == substrlens) {
  557. substrlens = localsublens;
  558. }
  559. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  560. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  561. }
  562. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  563. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  564. }
  565. if (0 == substrlens[INDEX_SUBSTREND]) {
  566. substrlens[INDEX_SUBSTREND] = SUBEND;
  567. }
  568. *ivals = NULL;
  569. /*
  570. * First figure out how many keys we will return. The answer is based
  571. * on the length of each assertion value. Since normalization may
  572. * reduce the length (such as when spaces are removed from space
  573. * insensitive strings), we call value_normalize() before checking
  574. * the length.
  575. */
  576. nsubs = 0;
  577. if ( initial != NULL ) {
  578. value_normalize( initial, syntax, 0 /* do not trim leading blanks */ );
  579. initiallen = strlen( initial );
  580. if ( initiallen > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  581. nsubs += 1; /* for the initial begin string key */
  582. /* the rest of the sub keys are "any" keys for this case */
  583. if ( initiallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  584. nsubs += initiallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  585. }
  586. } else {
  587. initial = NULL; /* save some work later */
  588. }
  589. }
  590. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  591. value_normalize( any[i], syntax, 0 /* do not trim leading blanks */ );
  592. len = strlen( any[i] );
  593. if ( len >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  594. nsubs += len - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  595. }
  596. }
  597. if ( final != NULL ) {
  598. value_normalize( final, syntax, 0 /* do not trim leading blanks */ );
  599. finallen = strlen( final );
  600. if ( finallen > substrlens[INDEX_SUBSTREND] - 2 ) {
  601. nsubs += 1; /* for the final end string key */
  602. /* the rest of the sub keys are "any" keys for this case */
  603. if ( finallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  604. nsubs += finallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  605. }
  606. } else {
  607. final = NULL; /* save some work later */
  608. }
  609. }
  610. if ( nsubs == 0 ) { /* no keys to return */
  611. return( 0 );
  612. }
  613. /*
  614. * Next, allocated the ivals array and fill it in with the actual
  615. * keys. *ivals is a NULL terminated array of Slapi_Value pointers.
  616. */
  617. *ivals = (Slapi_Value **) slapi_ch_malloc( (nsubs + 1) * sizeof(Slapi_Value *) );
  618. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  619. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  620. nsubs = 0;
  621. comp_buf = (char *)slapi_ch_malloc(maxsublen + 1);
  622. if ( initial != NULL ) {
  623. substring_comp_keys( ivals, &nsubs, initial, initiallen, '^', syntax,
  624. comp_buf, substrlens );
  625. }
  626. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  627. len = strlen( any[i] );
  628. if ( len < substrlens[INDEX_SUBSTRMIDDLE] ) {
  629. continue;
  630. }
  631. substring_comp_keys( ivals, &nsubs, any[i], len, 0, syntax,
  632. comp_buf, substrlens );
  633. }
  634. if ( final != NULL ) {
  635. substring_comp_keys( ivals, &nsubs, final, finallen, '$', syntax,
  636. comp_buf, substrlens );
  637. }
  638. (*ivals)[nsubs] = NULL;
  639. slapi_ch_free_string(&comp_buf);
  640. return( 0 );
  641. }
  642. static void
  643. substring_comp_keys(
  644. Slapi_Value ***ivals,
  645. int *nsubs,
  646. char *str,
  647. int lenstr,
  648. int prepost,
  649. int syntax,
  650. char *comp_buf,
  651. int *substrlens
  652. )
  653. {
  654. int i, substrlen;
  655. char *p;
  656. PR_ASSERT(NULL != comp_buf);
  657. PR_ASSERT(NULL != substrlens);
  658. LDAPDebug( LDAP_DEBUG_TRACE, "=> substring_comp_keys (%s) %d\n",
  659. str, prepost, 0 );
  660. /* prepend ^ for initial substring */
  661. if ( prepost == '^' )
  662. {
  663. substrlen = substrlens[INDEX_SUBSTRBEGIN];
  664. comp_buf[0] = '^';
  665. for ( i = 0; i < substrlen - 1; i++ )
  666. {
  667. comp_buf[i + 1] = str[i];
  668. }
  669. comp_buf[substrlen] = '\0';
  670. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  671. (*nsubs)++;
  672. }
  673. substrlen = substrlens[INDEX_SUBSTRMIDDLE];
  674. for ( p = str; p < (str + lenstr - substrlen + 1); p++ )
  675. {
  676. for ( i = 0; i < substrlen; i++ )
  677. {
  678. comp_buf[i] = p[i];
  679. }
  680. comp_buf[substrlen] = '\0';
  681. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  682. (*nsubs)++;
  683. }
  684. if ( prepost == '$' )
  685. {
  686. substrlen = substrlens[INDEX_SUBSTREND];
  687. p = str + lenstr - substrlen + 1;
  688. for ( i = 0; i < substrlen - 1; i++ )
  689. {
  690. comp_buf[i] = p[i];
  691. }
  692. comp_buf[substrlen - 1] = '$';
  693. comp_buf[substrlen] = '\0';
  694. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  695. (*nsubs)++;
  696. }
  697. LDAPDebug( LDAP_DEBUG_TRACE, "<= substring_comp_keys\n", 0, 0, 0 );
  698. }