string.c 22 KB

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