string.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. } else {
  564. bvp = slapi_value_get_berval(*bvlp);
  565. }
  566. /* leading */
  567. if ( bvp->bv_len > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  568. buf[0] = '^';
  569. for ( i = 0; i < substrlens[INDEX_SUBSTRBEGIN] - 1; i++ ) {
  570. buf[i + 1] = bvp->bv_val[i];
  571. }
  572. buf[substrlens[INDEX_SUBSTRBEGIN]] = '\0';
  573. (*ivals)[n] = slapi_value_new_string(buf);
  574. slapi_value_set_flags((*ivals)[n], value_flags);
  575. n++;
  576. }
  577. /* any */
  578. for ( p = bvp->bv_val;
  579. p < (bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTRMIDDLE] + 1);
  580. p++ ) {
  581. for ( i = 0; i < substrlens[INDEX_SUBSTRMIDDLE]; i++ ) {
  582. buf[i] = p[i];
  583. }
  584. buf[substrlens[INDEX_SUBSTRMIDDLE]] = '\0';
  585. (*ivals)[n] = slapi_value_new_string(buf);
  586. slapi_value_set_flags((*ivals)[n], value_flags);
  587. n++;
  588. }
  589. /* trailing */
  590. if ( bvp->bv_len > substrlens[INDEX_SUBSTREND] - 2 ) {
  591. p = bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTREND] + 1;
  592. for ( i = 0; i < substrlens[INDEX_SUBSTREND] - 1; i++ ) {
  593. buf[i] = p[i];
  594. }
  595. buf[substrlens[INDEX_SUBSTREND] - 1] = '$';
  596. buf[substrlens[INDEX_SUBSTREND]] = '\0';
  597. (*ivals)[n] = slapi_value_new_string(buf);
  598. slapi_value_set_flags((*ivals)[n], value_flags);
  599. n++;
  600. }
  601. }
  602. slapi_value_free(&bvdup);
  603. slapi_ch_free_string(&buf);
  604. }
  605. break;
  606. }
  607. return( 0 );
  608. }
  609. /* we've added code to make our equality filter processing faster */
  610. int
  611. string_assertion2keys_ava(
  612. Slapi_PBlock *pb,
  613. Slapi_Value *val,
  614. Slapi_Value ***ivals,
  615. int syntax,
  616. int ftype
  617. )
  618. {
  619. int i, numbvals;
  620. size_t len;
  621. char *w, *c;
  622. Slapi_Value *tmpval=NULL;
  623. char *alt = NULL;
  624. unsigned long flags = val ? slapi_value_get_flags(val) : 0;
  625. switch ( ftype ) {
  626. case LDAP_FILTER_EQUALITY_FAST:
  627. /* this code is trying to avoid multiple malloc/frees */
  628. len=slapi_value_get_length(val);
  629. tmpval=(*ivals)[0];
  630. if (len >= tmpval->bv.bv_len) {
  631. tmpval->bv.bv_val=(char *)slapi_ch_malloc(len+1);
  632. }
  633. memcpy(tmpval->bv.bv_val,slapi_value_get_string(val),len);
  634. tmpval->bv.bv_val[len]='\0';
  635. if (!(flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  636. /* 3rd arg: 1 - trim leading blanks */
  637. value_normalize_ext(tmpval->bv.bv_val, syntax, 1, &alt );
  638. if (alt) {
  639. if (len >= tmpval->bv.bv_len) {
  640. slapi_ch_free_string(&tmpval->bv.bv_val);
  641. }
  642. tmpval->bv.bv_val = alt;
  643. alt = NULL;
  644. }
  645. tmpval->bv.bv_len=strlen(tmpval->bv.bv_val);
  646. flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  647. } else if ((syntax & SYNTAX_DN) &&
  648. (flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  649. /* This dn value is normalized, but not case-normalized. */
  650. slapi_dn_ignore_case(tmpval->bv.bv_val);
  651. /* This dn value is case-normalized */
  652. flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  653. flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  654. }
  655. slapi_value_set_flags(tmpval, flags);
  656. break;
  657. case LDAP_FILTER_EQUALITY:
  658. (*ivals) = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
  659. (*ivals)[0] = val ? slapi_value_dup( val ) : NULL;
  660. if (val && !(flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  661. /* 3rd arg: 1 - trim leading blanks */
  662. value_normalize_ext( (*ivals)[0]->bv.bv_val, syntax, 1, &alt );
  663. if (alt) {
  664. slapi_ch_free_string(&(*ivals)[0]->bv.bv_val);
  665. (*ivals)[0]->bv.bv_val = alt;
  666. (*ivals)[0]->bv.bv_len = strlen( (*ivals)[0]->bv.bv_val );
  667. alt = NULL;
  668. }
  669. flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  670. } else if ((syntax & SYNTAX_DN) &&
  671. (flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  672. /* This dn value is normalized, but not case-normalized. */
  673. slapi_dn_ignore_case((*ivals)[0]->bv.bv_val);
  674. /* This dn value is case-normalized */
  675. flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  676. flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  677. }
  678. slapi_value_set_flags((*ivals)[0], flags);
  679. (*ivals)[1] = NULL;
  680. break;
  681. case LDAP_FILTER_APPROX:
  682. /* XXX should not do this twice! XXX */
  683. /* get an upper bound on the number of ivals */
  684. numbvals = 0;
  685. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  686. w = next_word( w ) ) {
  687. numbvals++;
  688. }
  689. (*ivals) = (Slapi_Value **) slapi_ch_malloc( (numbvals + 1) *
  690. sizeof(Slapi_Value *) );
  691. i = 0;
  692. for ( w = first_word( (char*)slapi_value_get_string(val) ); w != NULL;
  693. w = next_word( w ) ) {
  694. if ( (c = phonetic( w )) != NULL ) {
  695. (*ivals)[i] = slapi_value_new_string_passin(c);
  696. i++;
  697. }
  698. }
  699. (*ivals)[i] = NULL;
  700. if ( i == 0 ) {
  701. slapi_ch_free((void**)ivals );
  702. return( 0 );
  703. }
  704. break;
  705. default:
  706. LDAPDebug( LDAP_DEBUG_ANY,
  707. "string_assertion2keys_ava: unknown ftype 0x%x\n",
  708. ftype, 0, 0 );
  709. break;
  710. }
  711. return( 0 );
  712. }
  713. int
  714. string_assertion2keys_sub(
  715. Slapi_PBlock *pb,
  716. char *initial,
  717. char **any,
  718. char *final,
  719. Slapi_Value ***ivals,
  720. int syntax
  721. )
  722. {
  723. int nsubs, i, len;
  724. int initiallen = 0, finallen = 0;
  725. int *substrlens = NULL;
  726. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND};/* default values */
  727. int maxsublen;
  728. char *comp_buf = NULL;
  729. /* altinit|any|final: store alt string from value_normalize_ext if any,
  730. * otherwise the original string. And use for the real job */
  731. char *altinit = NULL;
  732. char **altany = NULL;
  733. char *altfinal = NULL;
  734. /* oaltinit|any|final: prepared to free altinit|any|final if allocated in
  735. * value_normalize_ext */
  736. char *oaltinit = NULL;
  737. char **oaltany = NULL;
  738. char *oaltfinal = NULL;
  739. int anysize = 0;
  740. if (pb) {
  741. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  742. }
  743. if (NULL == substrlens) {
  744. substrlens = localsublens;
  745. }
  746. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  747. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  748. }
  749. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  750. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  751. }
  752. if (0 == substrlens[INDEX_SUBSTREND]) {
  753. substrlens[INDEX_SUBSTREND] = SUBEND;
  754. }
  755. *ivals = NULL;
  756. /*
  757. * First figure out how many keys we will return. The answer is based
  758. * on the length of each assertion value. Since normalization may
  759. * reduce the length (such as when spaces are removed from space
  760. * insensitive strings), we call value_normalize_ext() before checking
  761. * the length.
  762. */
  763. nsubs = 0;
  764. if ( initial != NULL ) {
  765. /* 3rd arg: 0 - DO NOT trim leading blanks */
  766. value_normalize_ext( initial, syntax, 0, &altinit );
  767. oaltinit = altinit;
  768. if (NULL == altinit) {
  769. altinit = initial;
  770. }
  771. initiallen = strlen( altinit );
  772. if ( initiallen > substrlens[INDEX_SUBSTRBEGIN] - 2 ) {
  773. nsubs += 1; /* for the initial begin string key */
  774. /* the rest of the sub keys are "any" keys for this case */
  775. if ( initiallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  776. nsubs += initiallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  777. }
  778. } else {
  779. altinit = NULL; /* save some work later */
  780. }
  781. }
  782. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  783. anysize++;
  784. }
  785. altany = (char **)slapi_ch_calloc(anysize + 1, sizeof(char *));
  786. oaltany = (char **)slapi_ch_calloc(anysize + 1, sizeof(char *));
  787. for ( i = 0; any != NULL && any[i] != NULL; i++ ) {
  788. /* 3rd arg: 0 - DO NOT trim leading blanks */
  789. value_normalize_ext( any[i], syntax, 0, &altany[i] );
  790. if (NULL == altany[i]) {
  791. altany[i] = any[i];
  792. } else {
  793. oaltany[i] = altany[i];
  794. }
  795. len = strlen( altany[i] );
  796. if ( len >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  797. nsubs += len - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  798. }
  799. }
  800. if ( final != NULL ) {
  801. /* 3rd arg: 0 - DO NOT trim leading blanks */
  802. value_normalize_ext( final, syntax, 0, &altfinal );
  803. oaltfinal = altfinal;
  804. if (NULL == altfinal) {
  805. altfinal = final;
  806. }
  807. finallen = strlen( altfinal );
  808. if ( finallen > substrlens[INDEX_SUBSTREND] - 2 ) {
  809. nsubs += 1; /* for the final end string key */
  810. /* the rest of the sub keys are "any" keys for this case */
  811. if ( finallen >= substrlens[INDEX_SUBSTRMIDDLE] ) {
  812. nsubs += finallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  813. }
  814. } else {
  815. altfinal = NULL; /* save some work later */
  816. }
  817. }
  818. if ( nsubs == 0 ) { /* no keys to return */
  819. goto done;
  820. }
  821. /*
  822. * Next, allocated the ivals array and fill it in with the actual
  823. * keys. *ivals is a NULL terminated array of Slapi_Value pointers.
  824. */
  825. *ivals = (Slapi_Value **) slapi_ch_malloc( (nsubs + 1) * sizeof(Slapi_Value *) );
  826. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  827. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  828. nsubs = 0;
  829. comp_buf = (char *)slapi_ch_malloc(maxsublen + 1);
  830. if ( altinit != NULL ) {
  831. substring_comp_keys( ivals, &nsubs, altinit, initiallen, '^', syntax,
  832. comp_buf, substrlens );
  833. }
  834. for ( i = 0; altany != NULL && altany[i] != NULL; i++ ) {
  835. len = strlen( altany[i] );
  836. if ( len < substrlens[INDEX_SUBSTRMIDDLE] ) {
  837. continue;
  838. }
  839. substring_comp_keys( ivals, &nsubs, altany[i], len, 0, syntax,
  840. comp_buf, substrlens );
  841. }
  842. if ( altfinal != NULL ) {
  843. substring_comp_keys( ivals, &nsubs, altfinal, finallen, '$', syntax,
  844. comp_buf, substrlens );
  845. }
  846. (*ivals)[nsubs] = NULL;
  847. done:
  848. slapi_ch_free_string(&oaltinit);
  849. for ( i = 0; altany != NULL && altany[i] != NULL; i++ ) {
  850. slapi_ch_free_string(&oaltany[i]);
  851. }
  852. slapi_ch_free((void **)&oaltany);
  853. slapi_ch_free_string(&oaltfinal);
  854. slapi_ch_free((void **)&altany);
  855. slapi_ch_free_string(&comp_buf);
  856. return( 0 );
  857. }
  858. static void
  859. substring_comp_keys(
  860. Slapi_Value ***ivals,
  861. int *nsubs,
  862. char *str,
  863. int lenstr,
  864. int prepost,
  865. int syntax,
  866. char *comp_buf,
  867. int *substrlens
  868. )
  869. {
  870. int i, substrlen;
  871. char *p;
  872. PR_ASSERT(NULL != comp_buf);
  873. PR_ASSERT(NULL != substrlens);
  874. LDAPDebug( LDAP_DEBUG_TRACE, "=> substring_comp_keys (%s) %d\n",
  875. str, prepost, 0 );
  876. /* prepend ^ for initial substring */
  877. if ( prepost == '^' )
  878. {
  879. substrlen = substrlens[INDEX_SUBSTRBEGIN];
  880. comp_buf[0] = '^';
  881. for ( i = 0; i < substrlen - 1; i++ )
  882. {
  883. comp_buf[i + 1] = str[i];
  884. }
  885. comp_buf[substrlen] = '\0';
  886. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  887. (*nsubs)++;
  888. }
  889. substrlen = substrlens[INDEX_SUBSTRMIDDLE];
  890. for ( p = str; p < (str + lenstr - substrlen + 1); p++ )
  891. {
  892. for ( i = 0; i < substrlen; i++ )
  893. {
  894. comp_buf[i] = p[i];
  895. }
  896. comp_buf[substrlen] = '\0';
  897. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  898. (*nsubs)++;
  899. }
  900. if ( prepost == '$' )
  901. {
  902. substrlen = substrlens[INDEX_SUBSTREND];
  903. p = str + lenstr - substrlen + 1;
  904. for ( i = 0; i < substrlen - 1; i++ )
  905. {
  906. comp_buf[i] = p[i];
  907. }
  908. comp_buf[substrlen - 1] = '$';
  909. comp_buf[substrlen] = '\0';
  910. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  911. (*nsubs)++;
  912. }
  913. LDAPDebug( LDAP_DEBUG_TRACE, "<= substring_comp_keys\n", 0, 0, 0 );
  914. }