string.c 22 KB

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