1
0

string.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 = {0, NULL};
  60. struct berval *pbvfilter_norm = &bvfilter_norm;
  61. char *alt = NULL;
  62. if(retVal) {
  63. *retVal = NULL;
  64. }
  65. if ( ftype == LDAP_FILTER_APPROX ) {
  66. return( string_filter_approx( bvfilter, bvals, retVal ) );
  67. }
  68. if (syntax & SYNTAX_NORM_FILT) {
  69. pbvfilter_norm = bvfilter; /* already normalized */
  70. } else {
  71. slapi_ber_bvcpy(&bvfilter_norm, bvfilter);
  72. /* 3rd arg: 1 - trim leading blanks */
  73. value_normalize_ext( bvfilter_norm.bv_val, syntax, 1, &alt );
  74. if (alt) {
  75. slapi_ber_bvdone(&bvfilter_norm);
  76. bvfilter_norm.bv_val = alt;
  77. alt = NULL;
  78. }
  79. bvfilter_norm.bv_len = strlen(bvfilter_norm.bv_val);
  80. }
  81. for ( i = 0; (bvals != NULL) && (bvals[i] != NULL); i++ ) {
  82. int norm_val = 1; /* normalize the first value only */
  83. /* if the NORMALIZED flag is set, skip normalizing */
  84. if (slapi_value_get_flags(bvals[i]) & SLAPI_ATTR_FLAG_NORMALIZED) {
  85. norm_val = 0;
  86. }
  87. /* note - do not return the normalized value in retVal - the
  88. caller will usually want the "raw" value, and can normalize it later
  89. */
  90. rc = value_cmp( (struct berval*)slapi_value_get_berval(bvals[i]), pbvfilter_norm, syntax, norm_val );
  91. switch ( ftype ) {
  92. case LDAP_FILTER_GE:
  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. case LDAP_FILTER_LE:
  102. if ( rc <= 0 ) {
  103. if(retVal) {
  104. *retVal = bvals[i];
  105. }
  106. slapi_ch_free ((void**)&bvfilter_norm.bv_val);
  107. return( 0 );
  108. }
  109. break;
  110. case LDAP_FILTER_EQUALITY:
  111. if ( rc == 0 ) {
  112. if(retVal) {
  113. *retVal = bvals[i];
  114. }
  115. slapi_ch_free ((void**)&bvfilter_norm.bv_val);
  116. return( 0 );
  117. }
  118. break;
  119. }
  120. }
  121. slapi_ch_free ((void**)&bvfilter_norm.bv_val);
  122. return( -1 );
  123. }
  124. /*
  125. * return value: 0 -- approximately matched
  126. * -1 -- did not match
  127. */
  128. static int
  129. string_filter_approx( struct berval *bvfilter, Slapi_Value **bvals,
  130. Slapi_Value **retVal)
  131. {
  132. int i, rc;
  133. int ava_wordcount;
  134. char *w1, *w2, *c1, *c2;
  135. /*
  136. * try to match words in each filter value in order
  137. * in the attribute value.
  138. * XXX should do this once for the filter and save it XXX
  139. */
  140. rc = -1;
  141. if(retVal) {
  142. *retVal = NULL;
  143. }
  144. for ( i = 0; (bvals != NULL) && (bvals[i] != NULL); i++ ) {
  145. w2 = (char*)slapi_value_get_string(bvals[i]); /* JCM cast */
  146. ava_wordcount = 0;
  147. /* for each word in the filter value */
  148. for ( w1 = first_word( bvfilter->bv_val ); w1 != NULL;
  149. w1 = next_word( w1 ) ) {
  150. ++ava_wordcount;
  151. if ( (c1 = phonetic( w1 )) == NULL ) {
  152. break;
  153. }
  154. /*
  155. * for each word in the attribute value from
  156. * where we left off...
  157. */
  158. for ( w2 = first_word( w2 ); w2 != NULL;
  159. w2 = next_word( w2 ) ) {
  160. c2 = phonetic( w2 );
  161. rc = strcmp( c1, c2 );
  162. slapi_ch_free((void**)&c2 );
  163. if ( rc == 0 ) {
  164. if(retVal) {
  165. *retVal = bvals[i];
  166. }
  167. break;
  168. }
  169. }
  170. slapi_ch_free((void**)&c1 );
  171. /*
  172. * if we stopped because we ran out of words
  173. * before making a match, go on to the next
  174. * value. otherwise try to keep matching
  175. * words in this value from where we left off.
  176. */
  177. if ( w2 == NULL ) {
  178. break;
  179. } else {
  180. w2 = next_word( w2 );
  181. }
  182. }
  183. /*
  184. * if we stopped because we ran out of words and
  185. * we found at leasy one word, we have a match.
  186. */
  187. if ( w1 == NULL && ava_wordcount > 0 ) {
  188. rc = 0;
  189. break;
  190. }
  191. }
  192. if (0 != rc) {
  193. rc = -1;
  194. }
  195. LDAPDebug( LDAP_DEBUG_TRACE, "<= string_filter_approx %d\n",
  196. rc, 0, 0 );
  197. return( rc );
  198. }
  199. int
  200. string_filter_sub( Slapi_PBlock *pb, char *initial, char **any, char *final,
  201. Slapi_Value **bvals, int syntax )
  202. {
  203. int i, j, rc, size=0;
  204. char *p, *end, *realval, *tmpbuf = NULL, *bigpat = NULL;
  205. size_t tmpbufsize;
  206. char pat[BUFSIZ];
  207. char buf[BUFSIZ];
  208. char ebuf[BUFSIZ];
  209. time_t curtime = 0;
  210. time_t time_up = 0;
  211. time_t optime = 0; /* time op was initiated */
  212. int timelimit = 0; /* search timelimit */
  213. Operation *op = NULL;
  214. Slapi_Regex *re = NULL;
  215. const char *re_result = NULL;
  216. char *alt = NULL;
  217. int filter_normalized = 0;
  218. int free_re = 1;
  219. struct subfilt *sf = NULL;
  220. LDAPDebug( LDAP_DEBUG_FILTER, "=> string_filter_sub\n", 0, 0, 0 );
  221. if (pb) {
  222. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  223. }
  224. if (NULL != op) {
  225. slapi_pblock_get( pb, SLAPI_SEARCH_TIMELIMIT, &timelimit );
  226. slapi_pblock_get( pb, SLAPI_OPINITIATED_TIME, &optime );
  227. } else {
  228. /* timelimit is not passed via pblock */
  229. timelimit = -1;
  230. }
  231. /*
  232. * (timelimit==-1) means no time limit
  233. */
  234. time_up = ( timelimit==-1 ? -1 : optime + timelimit);
  235. if (pb) {
  236. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized );
  237. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_DATA, &sf );
  238. }
  239. if ( sf ) {
  240. re = (Slapi_Regex *)sf->sf_private;
  241. if ( re ) {
  242. free_re = 0;
  243. }
  244. }
  245. if (!re) {
  246. /*
  247. * construct a regular expression corresponding to the
  248. * filter and let regex do the work for each value
  249. * XXX should do this once and save it somewhere XXX
  250. */
  251. pat[0] = '\0';
  252. p = pat;
  253. end = pat + sizeof(pat) - 2; /* leave room for null */
  254. if ( initial != NULL ) {
  255. size = strlen( initial ) + 1; /* add 1 for "^" */
  256. }
  257. if ( any != NULL ) {
  258. i = 0;
  259. while ( any[i] ) {
  260. size += strlen(any[i++]) + 2; /* add 2 for ".*" */
  261. }
  262. }
  263. if ( final != NULL ) {
  264. size += strlen( final ) + 3; /* add 3 for ".*" and "$" */
  265. }
  266. size *= 2; /* doubled in case all filter chars need escaping */
  267. size++; /* add 1 for null */
  268. if ( p + size > end ) {
  269. bigpat = slapi_ch_malloc( size );
  270. p = bigpat;
  271. }
  272. if ( initial != NULL ) {
  273. /* 3rd arg: 1 - trim leading blanks */
  274. if (!filter_normalized) {
  275. value_normalize_ext( initial, syntax, 1, &alt );
  276. }
  277. *p++ = '^';
  278. if (alt) {
  279. filter_strcpy_special_ext( p, alt, FILTER_STRCPY_ESCAPE_RECHARS );
  280. slapi_ch_free_string(&alt);
  281. } else {
  282. filter_strcpy_special_ext( p, initial, FILTER_STRCPY_ESCAPE_RECHARS );
  283. }
  284. p = strchr( p, '\0' );
  285. }
  286. if ( any != NULL ) {
  287. for ( i = 0; any[i] != NULL; i++ ) {
  288. /* 3rd arg: 0 - DO NOT trim leading blanks */
  289. if (!filter_normalized) {
  290. value_normalize_ext( any[i], syntax, 0, &alt );
  291. }
  292. /* ".*" + value */
  293. *p++ = '.';
  294. *p++ = '*';
  295. if (alt) {
  296. filter_strcpy_special_ext( p, alt, FILTER_STRCPY_ESCAPE_RECHARS );
  297. slapi_ch_free_string(&alt);
  298. } else {
  299. filter_strcpy_special_ext( p, any[i], FILTER_STRCPY_ESCAPE_RECHARS );
  300. }
  301. p = strchr( p, '\0' );
  302. }
  303. }
  304. if ( final != NULL ) {
  305. /* 3rd arg: 0 - DO NOT trim leading blanks */
  306. if (!filter_normalized) {
  307. value_normalize_ext( final, syntax, 0, &alt );
  308. }
  309. /* ".*" + value */
  310. *p++ = '.';
  311. *p++ = '*';
  312. if (alt) {
  313. filter_strcpy_special_ext( p, alt, FILTER_STRCPY_ESCAPE_RECHARS );
  314. slapi_ch_free_string(&alt);
  315. } else {
  316. filter_strcpy_special_ext( p, final, FILTER_STRCPY_ESCAPE_RECHARS );
  317. }
  318. strcat( p, "$" );
  319. }
  320. /* compile the regex */
  321. p = (bigpat) ? bigpat : pat;
  322. tmpbuf = NULL;
  323. re = slapi_re_comp( p, &re_result );
  324. if (NULL == re) {
  325. LDAPDebug( LDAP_DEBUG_ANY, "re_comp (%s) failed (%s): %s\n",
  326. pat, p, re_result?re_result:"unknown" );
  327. rc = LDAP_OPERATIONS_ERROR;
  328. goto bailout;
  329. } else {
  330. LDAPDebug( LDAP_DEBUG_TRACE, "re_comp (%s)\n",
  331. escape_string( p, ebuf ), 0, 0 );
  332. }
  333. }
  334. curtime = current_time();
  335. if ( time_up != -1 && curtime > time_up ) {
  336. rc = LDAP_TIMELIMIT_EXCEEDED;
  337. goto bailout;
  338. }
  339. /*
  340. * test the regex against each value
  341. */
  342. rc = -1;
  343. tmpbuf = NULL;
  344. tmpbufsize = 0;
  345. for ( j = 0; (bvals != NULL) && (bvals[j] != NULL); j++ ) {
  346. int tmprc;
  347. size_t len;
  348. const struct berval *bvp = slapi_value_get_berval(bvals[j]);
  349. len = bvp->bv_len;
  350. if ( len < sizeof(buf) ) {
  351. realval = buf;
  352. strncpy( realval, bvp->bv_val, sizeof(buf) );
  353. } else if ( len < tmpbufsize ) {
  354. realval = tmpbuf;
  355. strncpy( realval, bvp->bv_val, tmpbufsize );
  356. } else {
  357. tmpbufsize = len + 1;
  358. realval = tmpbuf = (char *) slapi_ch_realloc( tmpbuf, tmpbufsize );
  359. strncpy( realval, bvp->bv_val, tmpbufsize );
  360. }
  361. /* 3rd arg: 1 - trim leading blanks */
  362. if (!(slapi_value_get_flags(bvals[j]) & SLAPI_ATTR_FLAG_NORMALIZED)) {
  363. value_normalize_ext( realval, syntax, 1, &alt );
  364. }
  365. if (alt) {
  366. tmprc = slapi_re_exec( re, alt, time_up );
  367. slapi_ch_free_string(&alt);
  368. } else {
  369. tmprc = slapi_re_exec( re, realval, time_up );
  370. }
  371. LDAPDebug( LDAP_DEBUG_TRACE, "re_exec (%s) %i\n",
  372. escape_string( realval, ebuf ), tmprc, 0 );
  373. if ( tmprc == 1 ) {
  374. rc = 0;
  375. break;
  376. } else if ( tmprc != 0 ) {
  377. rc = tmprc;
  378. break;
  379. }
  380. }
  381. bailout:
  382. if (free_re) {
  383. slapi_re_free(re);
  384. }
  385. slapi_ch_free((void**)&tmpbuf ); /* NULL is fine */
  386. slapi_ch_free((void**)&bigpat ); /* NULL is fine */
  387. LDAPDebug( LDAP_DEBUG_FILTER, "<= string_filter_sub %d\n",
  388. rc, 0, 0 );
  389. return( rc );
  390. }
  391. int
  392. string_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
  393. Slapi_Value ***ivals, int syntax, int ftype )
  394. {
  395. int nsubs, numbvals = 0, n;
  396. Slapi_Value **nbvals, **nbvlp;
  397. Slapi_Value **bvlp;
  398. char *w, *c, *p;
  399. char *alt = NULL;
  400. if (NULL == ivals) {
  401. return 1;
  402. }
  403. *ivals = NULL;
  404. if (NULL == bvals) {
  405. return 1;
  406. }
  407. switch ( ftype ) {
  408. case LDAP_FILTER_EQUALITY:
  409. /* allocate a new array for the normalized values */
  410. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  411. numbvals++;
  412. }
  413. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *));
  414. for ( bvlp = bvals, nbvlp = nbvals; bvlp && *bvlp; bvlp++, nbvlp++ )
  415. {
  416. unsigned long value_flags = slapi_value_get_flags(*bvlp);
  417. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  418. /* if the NORMALIZED flag is set, skip normalizing */
  419. if (!(value_flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  420. /* 3rd arg: 1 - trim leading blanks */
  421. value_normalize_ext( c, syntax, 1, &alt );
  422. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  423. } else if ((syntax & SYNTAX_DN) &&
  424. (value_flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  425. /* This dn value is normalized, but not case-normalized. */
  426. slapi_dn_ignore_case(c);
  427. /* This dn value is case-normalized */
  428. value_flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  429. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  430. }
  431. if (alt) {
  432. slapi_ch_free_string(&c);
  433. *nbvlp = slapi_value_new_string_passin(alt);
  434. alt = NULL;
  435. } else {
  436. *nbvlp = slapi_value_new_string_passin(c);
  437. c = NULL;
  438. }
  439. /* new value is normalized */
  440. slapi_value_set_flags(*nbvlp, value_flags);
  441. }
  442. *ivals = nbvals;
  443. break;
  444. case LDAP_FILTER_APPROX:
  445. /* XXX should not do this twice! XXX */
  446. /* get an upper bound on the number of ivals */
  447. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  448. for ( w = first_word( (char*)slapi_value_get_string(*bvlp) );
  449. w != NULL; w = next_word( w ) ) {
  450. numbvals++;
  451. }
  452. }
  453. nbvals = (Slapi_Value **) slapi_ch_calloc( (numbvals + 1), sizeof(Slapi_Value *) );
  454. n = 0;
  455. nbvlp = nbvals;
  456. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  457. for ( w = first_word( (char*)slapi_value_get_string(*bvlp) );
  458. w != NULL; w = next_word( w ) ) {
  459. if ( (c = phonetic( w )) != NULL ) {
  460. *nbvlp = slapi_value_new_string_passin(c);
  461. nbvlp++;
  462. }
  463. }
  464. }
  465. /* even if (n == 0), we should return the array nbvals w/ NULL items */
  466. *ivals = nbvals;
  467. break;
  468. case LDAP_FILTER_SUBSTRINGS:
  469. {
  470. /* XXX should remove duplicates! XXX */
  471. Slapi_Value *bvdup;
  472. const struct berval *bvp;
  473. char *buf;
  474. int i;
  475. int *substrlens = NULL;
  476. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  477. int maxsublen;
  478. /*
  479. * Substring key has 3 types:
  480. * begin (e.g., *^a)
  481. * middle (e.g., *abc)
  482. * end (e.g., *xy$)
  483. *
  484. * the each has its own key length, which can be configured as follows:
  485. * Usage: turn an index object to extensibleobject and
  486. * set an integer value for each.
  487. * dn: cn=sn, cn=index, cn=userRoot, cn=ldbm database, cn=plugins,
  488. * cn=config
  489. * objectClass: extensibleObject
  490. * nsSubStrBegin: 2
  491. * nsSubStrMiddle: 3
  492. * nsSubStrEnd: 2
  493. * [...]
  494. *
  495. * By default, begin == 3, middle == 3, end == 3 (defined in syntax.h)
  496. */
  497. /* If nsSubStrLen is specified in each index entry,
  498. respect the length for the substring index key length.
  499. Otherwise, the deafult value SUBLEN is used */
  500. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  501. if (NULL == substrlens) {
  502. substrlens = localsublens;
  503. }
  504. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  505. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  506. }
  507. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  508. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  509. }
  510. if (0 == substrlens[INDEX_SUBSTREND]) {
  511. substrlens[INDEX_SUBSTREND] = SUBEND;
  512. }
  513. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  514. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  515. buf = (char *)slapi_ch_calloc(1, maxsublen + 1);
  516. nsubs = 0;
  517. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  518. /*
  519. * Note: this calculation may err on the high side,
  520. * because value_normalize_ext(), which is called below
  521. * before we actually create the substring keys, may
  522. * reduce the length of the value in some cases or
  523. * increase the length in other cases. For example,
  524. * spaces are removed when space insensitive strings
  525. * are normalized. Or if the value includes '\"' (2 bytes),
  526. * it's normalized to '\22' (3 bytes). But it's okay
  527. * for nsubs to be too big. Since the ivals array is
  528. * NULL terminated, the only downside is that we
  529. * allocate more space than we really need.
  530. */
  531. nsubs += slapi_value_get_length(*bvlp) - substrlens[INDEX_SUBSTRMIDDLE] + 3;
  532. }
  533. nsubs += substrlens[INDEX_SUBSTRMIDDLE] * 2 - substrlens[INDEX_SUBSTRBEGIN] - substrlens[INDEX_SUBSTREND];
  534. *ivals = (Slapi_Value **) slapi_ch_calloc( (nsubs + 1), sizeof(Slapi_Value *) );
  535. n = 0;
  536. bvdup= slapi_value_new();
  537. for ( bvlp = bvals; bvlp && *bvlp; bvlp++ ) {
  538. unsigned long value_flags = slapi_value_get_flags(*bvlp);
  539. /* 3rd arg: 1 - trim leading blanks */
  540. if (!(value_flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  541. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  542. value_normalize_ext( c, syntax, 1, &alt );
  543. if (alt) {
  544. slapi_ch_free_string(&c);
  545. slapi_value_set_string_passin(bvdup, alt);
  546. alt = NULL;
  547. } else {
  548. slapi_value_set_string_passin(bvdup, c);
  549. c = NULL;
  550. }
  551. bvp = slapi_value_get_berval(bvdup);
  552. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  553. } else if ((syntax & SYNTAX_DN) &&
  554. (value_flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  555. /* This dn value is normalized, but not case-normalized. */
  556. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  557. slapi_dn_ignore_case(c);
  558. slapi_value_set_string_passin(bvdup, c);
  559. c = NULL;
  560. /* This dn value is case-normalized */
  561. value_flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  562. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  563. bvp = slapi_value_get_berval(bvdup);
  564. } else {
  565. bvp = slapi_value_get_berval(*bvlp);
  566. }
  567. /* leading */
  568. if ( bvp->bv_len > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  569. buf[0] = '^';
  570. for ( i = 0; i < substrlens[INDEX_SUBSTRBEGIN] - 1; i++ ) {
  571. buf[i + 1] = bvp->bv_val[i];
  572. }
  573. buf[substrlens[INDEX_SUBSTRBEGIN]] = '\0';
  574. (*ivals)[n] = slapi_value_new_string(buf);
  575. slapi_value_set_flags((*ivals)[n], value_flags);
  576. n++;
  577. }
  578. /* any */
  579. for ( p = bvp->bv_val;
  580. p < (bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTRMIDDLE] + 1);
  581. p++ ) {
  582. for ( i = 0; i < substrlens[INDEX_SUBSTRMIDDLE]; i++ ) {
  583. buf[i] = p[i];
  584. }
  585. buf[substrlens[INDEX_SUBSTRMIDDLE]] = '\0';
  586. (*ivals)[n] = slapi_value_new_string(buf);
  587. slapi_value_set_flags((*ivals)[n], value_flags);
  588. n++;
  589. }
  590. /* trailing */
  591. if ( bvp->bv_len > substrlens[INDEX_SUBSTREND] - 2 ) {
  592. p = bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTREND] + 1;
  593. for ( i = 0; i < substrlens[INDEX_SUBSTREND] - 1; i++ ) {
  594. buf[i] = p[i];
  595. }
  596. buf[substrlens[INDEX_SUBSTREND] - 1] = '$';
  597. buf[substrlens[INDEX_SUBSTREND]] = '\0';
  598. (*ivals)[n] = slapi_value_new_string(buf);
  599. slapi_value_set_flags((*ivals)[n], value_flags);
  600. n++;
  601. }
  602. }
  603. slapi_value_free(&bvdup);
  604. slapi_ch_free_string(&buf);
  605. }
  606. break;
  607. }
  608. return( 0 );
  609. }
  610. /* we've added code to make our equality filter processing faster */
  611. int
  612. string_assertion2keys_ava(
  613. Slapi_PBlock *pb,
  614. Slapi_Value *val,
  615. Slapi_Value ***ivals,
  616. int syntax,
  617. int ftype
  618. )
  619. {
  620. int i, numbvals;
  621. size_t len;
  622. char *w, *c;
  623. Slapi_Value *tmpval=NULL;
  624. char *alt = NULL;
  625. unsigned long flags = val ? slapi_value_get_flags(val) : 0;
  626. switch ( ftype ) {
  627. case LDAP_FILTER_EQUALITY_FAST:
  628. /* this code is trying to avoid multiple malloc/frees */
  629. len=slapi_value_get_length(val);
  630. tmpval=(*ivals)[0];
  631. if (len >= tmpval->bv.bv_len) {
  632. tmpval->bv.bv_val=(char *)slapi_ch_malloc(len+1);
  633. }
  634. memcpy(tmpval->bv.bv_val,slapi_value_get_string(val),len);
  635. tmpval->bv.bv_val[len]='\0';
  636. if (!(flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  637. /* 3rd arg: 1 - trim leading blanks */
  638. value_normalize_ext(tmpval->bv.bv_val, syntax, 1, &alt );
  639. if (alt) {
  640. if (len >= tmpval->bv.bv_len) {
  641. slapi_ch_free_string(&tmpval->bv.bv_val);
  642. }
  643. tmpval->bv.bv_val = alt;
  644. alt = NULL;
  645. }
  646. tmpval->bv.bv_len=strlen(tmpval->bv.bv_val);
  647. flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  648. } else if ((syntax & SYNTAX_DN) &&
  649. (flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  650. /* This dn value is normalized, but not case-normalized. */
  651. slapi_dn_ignore_case(tmpval->bv.bv_val);
  652. /* This dn value is case-normalized */
  653. flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  654. flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  655. }
  656. slapi_value_set_flags(tmpval, flags);
  657. break;
  658. case LDAP_FILTER_EQUALITY:
  659. (*ivals) = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
  660. (*ivals)[0] = val ? slapi_value_dup( val ) : NULL;
  661. if (val && !(flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  662. /* 3rd arg: 1 - trim leading blanks */
  663. value_normalize_ext( (*ivals)[0]->bv.bv_val, syntax, 1, &alt );
  664. if (alt) {
  665. slapi_ch_free_string(&(*ivals)[0]->bv.bv_val);
  666. (*ivals)[0]->bv.bv_val = alt;
  667. (*ivals)[0]->bv.bv_len = strlen( (*ivals)[0]->bv.bv_val );
  668. alt = NULL;
  669. }
  670. flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  671. } else if ((syntax & SYNTAX_DN) &&
  672. (flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  673. /* This dn value is normalized, but not case-normalized. */
  674. slapi_dn_ignore_case((*ivals)[0]->bv.bv_val);
  675. /* This dn value is case-normalized */
  676. flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  677. flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  678. }
  679. slapi_value_set_flags((*ivals)[0], flags);
  680. (*ivals)[1] = NULL;
  681. break;
  682. case LDAP_FILTER_APPROX:
  683. /* XXX should not do this twice! XXX */
  684. /* get an upper bound on the number of ivals */
  685. numbvals = 0;
  686. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  687. w = next_word( w ) ) {
  688. numbvals++;
  689. }
  690. (*ivals) = (Slapi_Value **) slapi_ch_malloc( (numbvals + 1) *
  691. sizeof(Slapi_Value *) );
  692. i = 0;
  693. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  694. w = next_word( w ) ) {
  695. if ( (c = phonetic( w )) != NULL ) {
  696. (*ivals)[i] = slapi_value_new_string_passin(c);
  697. i++;
  698. }
  699. }
  700. (*ivals)[i] = NULL;
  701. if ( i == 0 ) {
  702. slapi_ch_free((void**)ivals );
  703. return( 0 );
  704. }
  705. break;
  706. default:
  707. LDAPDebug( LDAP_DEBUG_ANY,
  708. "string_assertion2keys_ava: unknown ftype 0x%x\n",
  709. ftype, 0, 0 );
  710. break;
  711. }
  712. return( 0 );
  713. }
  714. int
  715. string_assertion2keys_sub(
  716. Slapi_PBlock *pb,
  717. char *initial,
  718. char **any,
  719. char *final,
  720. Slapi_Value ***ivals,
  721. int syntax
  722. )
  723. {
  724. int nsubs, i, len;
  725. int initiallen = 0, finallen = 0;
  726. int *substrlens = NULL;
  727. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  728. int maxsublen;
  729. char *comp_buf = NULL;
  730. /* altinit|any|final: store alt string from value_normalize_ext if any,
  731. * otherwise the original string. And use for the real job */
  732. char *altinit = NULL;
  733. char **altany = NULL;
  734. char *altfinal = NULL;
  735. /* oaltinit|any|final: prepared to free altinit|any|final if allocated in
  736. * value_normalize_ext */
  737. char *oaltinit = NULL;
  738. char **oaltany = NULL;
  739. char *oaltfinal = NULL;
  740. int anysize = 0;
  741. if (pb) {
  742. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  743. }
  744. if (NULL == substrlens) {
  745. substrlens = localsublens;
  746. }
  747. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  748. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  749. }
  750. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  751. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  752. }
  753. if (0 == substrlens[INDEX_SUBSTREND]) {
  754. substrlens[INDEX_SUBSTREND] = SUBEND;
  755. }
  756. *ivals = NULL;
  757. /*
  758. * First figure out how many keys we will return. The answer is based
  759. * on the length of each assertion value. Since normalization may
  760. * reduce the length (such as when spaces are removed from space
  761. * insensitive strings), we call value_normalize_ext() before checking
  762. * the length.
  763. */
  764. nsubs = 0;
  765. if ( initial != NULL ) {
  766. /* 3rd arg: 0 - DO NOT trim leading blanks */
  767. value_normalize_ext( initial, syntax, 0, &altinit );
  768. oaltinit = altinit;
  769. if (NULL == altinit) {
  770. altinit = initial;
  771. }
  772. initiallen = strlen( altinit );
  773. if ( initiallen > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  774. nsubs += 1; /* for the initial begin string key */
  775. /* the rest of the sub keys are "any" keys for this case */
  776. if ( initiallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  777. nsubs += initiallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  778. }
  779. } else {
  780. altinit = NULL; /* save some work later */
  781. }
  782. }
  783. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  784. anysize++;
  785. }
  786. altany = (char **)slapi_ch_calloc(anysize + 1, sizeof(char *));
  787. oaltany = (char **)slapi_ch_calloc(anysize + 1, sizeof(char *));
  788. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  789. /* 3rd arg: 0 - DO NOT trim leading blanks */
  790. value_normalize_ext( any[i], syntax, 0, &altany[i] );
  791. if (NULL == altany[i]) {
  792. altany[i] = any[i];
  793. } else {
  794. oaltany[i] = altany[i];
  795. }
  796. len = strlen( altany[i] );
  797. if ( len >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  798. nsubs += len - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  799. }
  800. }
  801. if ( final != NULL ) {
  802. /* 3rd arg: 0 - DO NOT trim leading blanks */
  803. value_normalize_ext( final, syntax, 0, &altfinal );
  804. oaltfinal = altfinal;
  805. if (NULL == altfinal) {
  806. altfinal = final;
  807. }
  808. finallen = strlen( altfinal );
  809. if ( finallen > substrlens[INDEX_SUBSTREND] - 2 ) {
  810. nsubs += 1; /* for the final end string key */
  811. /* the rest of the sub keys are "any" keys for this case */
  812. if ( finallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  813. nsubs += finallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  814. }
  815. } else {
  816. altfinal = NULL; /* save some work later */
  817. }
  818. }
  819. if ( nsubs == 0 ) { /* no keys to return */
  820. goto done;
  821. }
  822. /*
  823. * Next, allocated the ivals array and fill it in with the actual
  824. * keys. *ivals is a NULL terminated array of Slapi_Value pointers.
  825. */
  826. *ivals = (Slapi_Value **) slapi_ch_malloc( (nsubs + 1) * sizeof(Slapi_Value *) );
  827. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  828. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  829. nsubs = 0;
  830. comp_buf = (char *)slapi_ch_malloc(maxsublen + 1);
  831. if ( altinit != NULL ) {
  832. substring_comp_keys( ivals, &nsubs, altinit, initiallen, '^', syntax,
  833. comp_buf, substrlens );
  834. }
  835. for ( i = 0; altany != NULL && altany[i] != NULL; i++ ) {
  836. len = strlen( altany[i] );
  837. if ( len < substrlens[INDEX_SUBSTRMIDDLE] ) {
  838. continue;
  839. }
  840. substring_comp_keys( ivals, &nsubs, altany[i], len, 0, syntax,
  841. comp_buf, substrlens );
  842. }
  843. if ( altfinal != NULL ) {
  844. substring_comp_keys( ivals, &nsubs, altfinal, finallen, '$', syntax,
  845. comp_buf, substrlens );
  846. }
  847. (*ivals)[nsubs] = NULL;
  848. done:
  849. slapi_ch_free_string(&oaltinit);
  850. for ( i = 0; altany != NULL && altany[i] != NULL; i++ ) {
  851. slapi_ch_free_string(&oaltany[i]);
  852. }
  853. slapi_ch_free((void **)&oaltany);
  854. slapi_ch_free_string(&oaltfinal);
  855. slapi_ch_free((void **)&altany);
  856. slapi_ch_free_string(&comp_buf);
  857. return( 0 );
  858. }
  859. static void
  860. substring_comp_keys(
  861. Slapi_Value ***ivals,
  862. int *nsubs,
  863. char *str,
  864. int lenstr,
  865. int prepost,
  866. int syntax,
  867. char *comp_buf,
  868. int *substrlens
  869. )
  870. {
  871. int i, substrlen;
  872. char *p;
  873. PR_ASSERT(NULL != comp_buf);
  874. PR_ASSERT(NULL != substrlens);
  875. LDAPDebug( LDAP_DEBUG_TRACE, "=> substring_comp_keys (%s) %d\n",
  876. str, prepost, 0 );
  877. /* prepend ^ for initial substring */
  878. if ( prepost == '^' )
  879. {
  880. substrlen = substrlens[INDEX_SUBSTRBEGIN];
  881. comp_buf[0] = '^';
  882. for ( i = 0; i < substrlen - 1; i++ )
  883. {
  884. comp_buf[i + 1] = str[i];
  885. }
  886. comp_buf[substrlen] = '\0';
  887. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  888. (*nsubs)++;
  889. }
  890. substrlen = substrlens[INDEX_SUBSTRMIDDLE];
  891. for ( p = str; p < (str + lenstr - substrlen + 1); p++ )
  892. {
  893. for ( i = 0; i < substrlen; i++ )
  894. {
  895. comp_buf[i] = p[i];
  896. }
  897. comp_buf[substrlen] = '\0';
  898. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  899. (*nsubs)++;
  900. }
  901. if ( prepost == '$' )
  902. {
  903. substrlen = substrlens[INDEX_SUBSTREND];
  904. p = str + lenstr - substrlen + 1;
  905. for ( i = 0; i < substrlen - 1; i++ )
  906. {
  907. comp_buf[i] = p[i];
  908. }
  909. comp_buf[substrlen - 1] = '$';
  910. comp_buf[substrlen] = '\0';
  911. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  912. (*nsubs)++;
  913. }
  914. LDAPDebug( LDAP_DEBUG_TRACE, "<= substring_comp_keys\n", 0, 0, 0 );
  915. }