string.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. if (NULL == ivals) {
  328. return 1;
  329. }
  330. *ivals = NULL;
  331. if (NULL == bvals) {
  332. return 1;
  333. }
  334. switch ( ftype ) {
  335. case LDAP_FILTER_EQUALITY:
  336. /* allocate a new array for the normalized values */
  337. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  338. numbvals++;
  339. }
  340. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *));
  341. for ( bvlp = bvals, nbvlp = nbvals; bvlp && *bvlp; bvlp++, nbvlp++ )
  342. {
  343. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  344. /* if the NORMALIZED flag is set, skip normalizing */
  345. if (!(slapi_value_get_flags(*bvlp) & SLAPI_ATTR_FLAG_NORMALIZED))
  346. value_normalize( c, syntax, 1 /* trim leading blanks */ );
  347. *nbvlp = slapi_value_new_string_passin(c);
  348. }
  349. *ivals = nbvals;
  350. break;
  351. case LDAP_FILTER_APPROX:
  352. /* XXX should not do this twice! XXX */
  353. /* get an upper bound on the number of ivals */
  354. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  355. for ( w = first_word( (char*)slapi_value_get_string(*bvlp) );
  356. w != NULL; w = next_word( w ) ) {
  357. numbvals++;
  358. }
  359. }
  360. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *) );
  361. n = 0;
  362. nbvlp = nbvals;
  363. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  364. for ( w = first_word( (char*)slapi_value_get_string(*bvlp) );
  365. w != NULL; w = next_word( w ) ) {
  366. if ( (c = phonetic( w )) != NULL ) {
  367. *nbvlp = slapi_value_new_string_passin(c);
  368. nbvlp++;
  369. }
  370. }
  371. }
  372. /* even if (n == 0), we should return the array nbvals w/ NULL items */
  373. *ivals = nbvals;
  374. break;
  375. case LDAP_FILTER_SUBSTRINGS:
  376. {
  377. /* XXX should remove duplicates! XXX */
  378. Slapi_Value *bvdup;
  379. const struct berval *bvp;
  380. char *buf;
  381. int i;
  382. int *substrlens = NULL;
  383. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  384. int maxsublen;
  385. /*
  386. * Substring key has 3 types:
  387. * begin (e.g., *^a)
  388. * middle (e.g., *abc)
  389. * end (e.g., *xy$)
  390. *
  391. * the each has its own key length, which can be configured as follows:
  392. * Usage: turn an index object to extensibleobject and
  393. * set an integer value for each.
  394. * dn: cn=sn, cn=index, cn=userRoot, cn=ldbm database, cn=plugins,
  395. * cn=config
  396. * objectClass: extensibleObject
  397. * nsSubStrBegin: 2
  398. * nsSubStrMiddle: 3
  399. * nsSubStrEnd: 2
  400. * [...]
  401. *
  402. * By default, begin == 3, middle == 3, end == 3 (defined in syntax.h)
  403. */
  404. /* If nsSubStrLen is specified in each index entry,
  405. respect the length for the substring index key length.
  406. Otherwise, the deafult value SUBLEN is used */
  407. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  408. if (NULL == substrlens) {
  409. substrlens = localsublens;
  410. }
  411. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  412. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  413. }
  414. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  415. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  416. }
  417. if (0 == substrlens[INDEX_SUBSTREND]) {
  418. substrlens[INDEX_SUBSTREND] = SUBEND;
  419. }
  420. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  421. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  422. buf = (char *)slapi_ch_calloc(1, maxsublen + 1);
  423. nsubs = 0;
  424. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  425. /*
  426. * Note: this calculation may err on the high side,
  427. * because value_normalize(), which is called below
  428. * before we actually create the substring keys, may
  429. * reduce the length of the value in some cases. For
  430. * example, spaces are removed when space insensitive
  431. * strings are normalized. But it's okay for nsubs to
  432. * be too big. Since the ivals array is NULL terminated,
  433. * the only downside is that we allocate more space than
  434. * we really need.
  435. */
  436. nsubs += slapi_value_get_length(*bvlp) - substrlens[INDEX_SUBSTRMIDDLE] + 3;
  437. }
  438. nsubs += substrlens[INDEX_SUBSTRMIDDLE] * 2 - substrlens[INDEX_SUBSTRBEGIN] - substrlens[INDEX_SUBSTREND];
  439. *ivals = (Slapi_Value **) slapi_ch_calloc( (nsubs + 1), sizeof(Slapi_Value *) );
  440. n = 0;
  441. bvdup= slapi_value_new();
  442. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  443. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  444. value_normalize( c, syntax, 1 /* trim leading blanks */ );
  445. slapi_value_set_string_passin(bvdup, c);
  446. bvp = slapi_value_get_berval(bvdup);
  447. /* leading */
  448. if ( bvp->bv_len > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  449. buf[0] = '^';
  450. for ( i = 0; i < substrlens[INDEX_SUBSTRBEGIN] - 1; i++ ) {
  451. buf[i + 1] = bvp->bv_val[i];
  452. }
  453. buf[substrlens[INDEX_SUBSTRBEGIN]] = '\0';
  454. (*ivals)[n] = slapi_value_new_string(buf);
  455. n++;
  456. }
  457. /* any */
  458. for ( p = bvp->bv_val;
  459. p < (bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTRMIDDLE] + 1);
  460. p++ ) {
  461. for ( i = 0; i < substrlens[INDEX_SUBSTRMIDDLE]; i++ ) {
  462. buf[i] = p[i];
  463. }
  464. buf[substrlens[INDEX_SUBSTRMIDDLE]] = '\0';
  465. (*ivals)[n] = slapi_value_new_string(buf);
  466. n++;
  467. }
  468. /* trailing */
  469. if ( bvp->bv_len > substrlens[INDEX_SUBSTREND] - 2 ) {
  470. p = bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTREND] + 1;
  471. for ( i = 0; i < substrlens[INDEX_SUBSTREND] - 1; i++ ) {
  472. buf[i] = p[i];
  473. }
  474. buf[substrlens[INDEX_SUBSTREND] - 1] = '$';
  475. buf[substrlens[INDEX_SUBSTREND]] = '\0';
  476. (*ivals)[n] = slapi_value_new_string(buf);
  477. n++;
  478. }
  479. }
  480. slapi_value_free(&bvdup);
  481. slapi_ch_free_string(&buf);
  482. }
  483. break;
  484. }
  485. return( 0 );
  486. }
  487. /* we've added code to make our equality filter processing faster */
  488. int
  489. string_assertion2keys_ava(
  490. Slapi_PBlock *pb,
  491. Slapi_Value *val,
  492. Slapi_Value ***ivals,
  493. int syntax,
  494. int ftype
  495. )
  496. {
  497. int i, numbvals;
  498. size_t len;
  499. char *w, *c;
  500. Slapi_Value *tmpval=NULL;
  501. switch ( ftype ) {
  502. case LDAP_FILTER_EQUALITY_FAST:
  503. /* this code is trying to avoid multiple malloc/frees */
  504. len=slapi_value_get_length(val);
  505. tmpval=(*ivals)[0];
  506. if (len >= tmpval->bv.bv_len) {
  507. tmpval->bv.bv_val=(char *)slapi_ch_malloc(len+1);
  508. }
  509. memcpy(tmpval->bv.bv_val,slapi_value_get_string(val),len);
  510. tmpval->bv.bv_val[len]='\0';
  511. value_normalize(tmpval->bv.bv_val, syntax, 1 /* trim leading blanks */ );
  512. tmpval->bv.bv_len=strlen(tmpval->bv.bv_val);
  513. break;
  514. case LDAP_FILTER_EQUALITY:
  515. (*ivals) = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
  516. (*ivals)[0] = slapi_value_dup( val );
  517. value_normalize( (*ivals)[0]->bv.bv_val, syntax, 1 /* trim leading blanks */ );
  518. (*ivals)[0]->bv.bv_len = strlen( (*ivals)[0]->bv.bv_val );
  519. (*ivals)[1] = NULL;
  520. break;
  521. case LDAP_FILTER_APPROX:
  522. /* XXX should not do this twice! XXX */
  523. /* get an upper bound on the number of ivals */
  524. numbvals = 0;
  525. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  526. w = next_word( w ) ) {
  527. numbvals++;
  528. }
  529. (*ivals) = (Slapi_Value **) slapi_ch_malloc( (numbvals + 1) *
  530. sizeof(Slapi_Value *) );
  531. i = 0;
  532. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  533. w = next_word( w ) ) {
  534. if ( (c = phonetic( w )) != NULL ) {
  535. (*ivals)[i] = slapi_value_new_string_passin(c);
  536. i++;
  537. }
  538. }
  539. (*ivals)[i] = NULL;
  540. if ( i == 0 ) {
  541. slapi_ch_free((void**)ivals );
  542. return( 0 );
  543. }
  544. break;
  545. default:
  546. LDAPDebug( LDAP_DEBUG_ANY,
  547. "string_assertion2keys_ava: unknown ftype 0x%x\n",
  548. ftype, 0, 0 );
  549. break;
  550. }
  551. return( 0 );
  552. }
  553. int
  554. string_assertion2keys_sub(
  555. Slapi_PBlock *pb,
  556. char *initial,
  557. char **any,
  558. char *final,
  559. Slapi_Value ***ivals,
  560. int syntax
  561. )
  562. {
  563. int nsubs, i, len;
  564. int initiallen = 0, finallen = 0;
  565. int *substrlens = NULL;
  566. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  567. int maxsublen;
  568. char *comp_buf = NULL;
  569. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  570. if (NULL == substrlens) {
  571. substrlens = localsublens;
  572. }
  573. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  574. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  575. }
  576. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  577. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  578. }
  579. if (0 == substrlens[INDEX_SUBSTREND]) {
  580. substrlens[INDEX_SUBSTREND] = SUBEND;
  581. }
  582. *ivals = NULL;
  583. /*
  584. * First figure out how many keys we will return. The answer is based
  585. * on the length of each assertion value. Since normalization may
  586. * reduce the length (such as when spaces are removed from space
  587. * insensitive strings), we call value_normalize() before checking
  588. * the length.
  589. */
  590. nsubs = 0;
  591. if ( initial != NULL ) {
  592. value_normalize( initial, syntax, 0 /* do not trim leading blanks */ );
  593. initiallen = strlen( initial );
  594. if ( initiallen > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  595. nsubs += 1; /* for the initial begin string key */
  596. /* the rest of the sub keys are "any" keys for this case */
  597. if ( initiallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  598. nsubs += initiallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  599. }
  600. } else {
  601. initial = NULL; /* save some work later */
  602. }
  603. }
  604. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  605. value_normalize( any[i], syntax, 0 /* do not trim leading blanks */ );
  606. len = strlen( any[i] );
  607. if ( len >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  608. nsubs += len - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  609. }
  610. }
  611. if ( final != NULL ) {
  612. value_normalize( final, syntax, 0 /* do not trim leading blanks */ );
  613. finallen = strlen( final );
  614. if ( finallen > substrlens[INDEX_SUBSTREND] - 2 ) {
  615. nsubs += 1; /* for the final end string key */
  616. /* the rest of the sub keys are "any" keys for this case */
  617. if ( finallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  618. nsubs += finallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  619. }
  620. } else {
  621. final = NULL; /* save some work later */
  622. }
  623. }
  624. if ( nsubs == 0 ) { /* no keys to return */
  625. return( 0 );
  626. }
  627. /*
  628. * Next, allocated the ivals array and fill it in with the actual
  629. * keys. *ivals is a NULL terminated array of Slapi_Value pointers.
  630. */
  631. *ivals = (Slapi_Value **) slapi_ch_malloc( (nsubs + 1) * sizeof(Slapi_Value *) );
  632. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  633. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  634. nsubs = 0;
  635. comp_buf = (char *)slapi_ch_malloc(maxsublen + 1);
  636. if ( initial != NULL ) {
  637. substring_comp_keys( ivals, &nsubs, initial, initiallen, '^', syntax,
  638. comp_buf, substrlens );
  639. }
  640. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  641. len = strlen( any[i] );
  642. if ( len < substrlens[INDEX_SUBSTRMIDDLE] ) {
  643. continue;
  644. }
  645. substring_comp_keys( ivals, &nsubs, any[i], len, 0, syntax,
  646. comp_buf, substrlens );
  647. }
  648. if ( final != NULL ) {
  649. substring_comp_keys( ivals, &nsubs, final, finallen, '$', syntax,
  650. comp_buf, substrlens );
  651. }
  652. (*ivals)[nsubs] = NULL;
  653. slapi_ch_free_string(&comp_buf);
  654. return( 0 );
  655. }
  656. static void
  657. substring_comp_keys(
  658. Slapi_Value ***ivals,
  659. int *nsubs,
  660. char *str,
  661. int lenstr,
  662. int prepost,
  663. int syntax,
  664. char *comp_buf,
  665. int *substrlens
  666. )
  667. {
  668. int i, substrlen;
  669. char *p;
  670. PR_ASSERT(NULL != comp_buf);
  671. PR_ASSERT(NULL != substrlens);
  672. LDAPDebug( LDAP_DEBUG_TRACE, "=> substring_comp_keys (%s) %d\n",
  673. str, prepost, 0 );
  674. /* prepend ^ for initial substring */
  675. if ( prepost == '^' )
  676. {
  677. substrlen = substrlens[INDEX_SUBSTRBEGIN];
  678. comp_buf[0] = '^';
  679. for ( i = 0; i < substrlen - 1; i++ )
  680. {
  681. comp_buf[i + 1] = str[i];
  682. }
  683. comp_buf[substrlen] = '\0';
  684. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  685. (*nsubs)++;
  686. }
  687. substrlen = substrlens[INDEX_SUBSTRMIDDLE];
  688. for ( p = str; p < (str + lenstr - substrlen + 1); p++ )
  689. {
  690. for ( i = 0; i < substrlen; i++ )
  691. {
  692. comp_buf[i] = p[i];
  693. }
  694. comp_buf[substrlen] = '\0';
  695. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  696. (*nsubs)++;
  697. }
  698. if ( prepost == '$' )
  699. {
  700. substrlen = substrlens[INDEX_SUBSTREND];
  701. p = str + lenstr - substrlen + 1;
  702. for ( i = 0; i < substrlen - 1; i++ )
  703. {
  704. comp_buf[i] = p[i];
  705. }
  706. comp_buf[substrlen - 1] = '$';
  707. comp_buf[substrlen] = '\0';
  708. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  709. (*nsubs)++;
  710. }
  711. LDAPDebug( LDAP_DEBUG_TRACE, "<= substring_comp_keys\n", 0, 0, 0 );
  712. }