string.c 27 KB

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