1
0

string.c 22 KB

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