valueset.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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. /* valueset.c - routines for dealing with value sets */
  42. #include "slap.h"
  43. #include "slapi-private.h"
  44. /* <=========================== Berval Array ==========================> */
  45. /*
  46. * vals - The existing values.
  47. * addval - The value to add.
  48. * nvals - The number of existing values.
  49. * maxvals - The number of elements in the existing values array.
  50. */
  51. void
  52. bervalarray_add_berval_fast(
  53. struct berval ***vals,
  54. const struct berval *addval,
  55. int nvals,
  56. int *maxvals
  57. )
  58. {
  59. int need = nvals + 2;
  60. if(need>*maxvals)
  61. {
  62. if (*maxvals==0)
  63. {
  64. *maxvals = 2;
  65. }
  66. while ( *maxvals < need )
  67. {
  68. *maxvals *= 2;
  69. }
  70. if(*vals==NULL)
  71. {
  72. *vals = (struct berval **) slapi_ch_malloc( *maxvals * sizeof(struct berval *));
  73. }
  74. else
  75. {
  76. *vals = (struct berval **) slapi_ch_realloc( (char *) *vals, *maxvals * sizeof(struct berval *));
  77. }
  78. }
  79. (*vals)[nvals] = ber_bvdup( (struct berval *)addval );
  80. (*vals)[nvals+1] = NULL;
  81. }
  82. /* <=========================== Value Array ==========================> */
  83. /*
  84. * vals - The existing values.
  85. * addval - The value to add.
  86. * nvals - The number of existing values.
  87. * maxvals - The number of elements in the existing values array.
  88. */
  89. void
  90. valuearray_add_value_fast(Slapi_Value ***vals,
  91. Slapi_Value *addval,
  92. int nvals,
  93. int *maxvals,
  94. int exact, /* Don't create an array bigger than needed */
  95. int passin) /* The values are being passed in */
  96. {
  97. Slapi_Value *oneval[2];
  98. oneval[0]= addval;
  99. oneval[1]= NULL;
  100. valuearray_add_valuearray_fast(vals,oneval,nvals,1,maxvals,exact,passin);
  101. }
  102. void
  103. valuearray_add_valuearray_fast(Slapi_Value ***vals,
  104. Slapi_Value **addvals,
  105. int nvals,
  106. int naddvals,
  107. int *maxvals,
  108. int exact, /* Don't create an array bigger than needed */
  109. int passin) /* The values are being passed in */
  110. {
  111. int i, j;
  112. int allocate= 0;
  113. int need = nvals + naddvals + 1;
  114. if(exact)
  115. {
  116. /* Create an array exactly the right size. */
  117. if(need>*maxvals)
  118. {
  119. allocate= need;
  120. }
  121. }
  122. else
  123. {
  124. if (*maxvals==0) /* empty; create with 4 by default */
  125. {
  126. allocate= 4;
  127. }
  128. else if (need > *maxvals)
  129. {
  130. /* Exponentially expand the array */
  131. allocate= *maxvals;
  132. while ( allocate < need )
  133. {
  134. allocate *= 2;
  135. }
  136. }
  137. }
  138. if(allocate>0)
  139. {
  140. if(*vals==NULL)
  141. {
  142. *vals = (Slapi_Value **) slapi_ch_malloc( allocate * sizeof(Slapi_Value *));
  143. }
  144. else
  145. {
  146. *vals = (Slapi_Value **) slapi_ch_realloc( (char *) *vals, allocate * sizeof(Slapi_Value *));
  147. }
  148. *maxvals= allocate;
  149. }
  150. for ( i = 0, j = 0; i < naddvals; i++)
  151. {
  152. if ( addvals[i]!=NULL )
  153. {
  154. if(passin)
  155. {
  156. /* We consume the values */
  157. (*vals)[nvals + j] = addvals[i];
  158. }
  159. else
  160. {
  161. /* We copy the values */
  162. (*vals)[nvals + j] = slapi_value_dup(addvals[i]);
  163. }
  164. j++;
  165. }
  166. }
  167. (*vals)[nvals + j] = NULL;
  168. }
  169. void
  170. valuearray_add_value(Slapi_Value ***vals, const Slapi_Value *addval)
  171. {
  172. Slapi_Value *oneval[2];
  173. oneval[0]= (Slapi_Value*)addval;
  174. oneval[1]= NULL;
  175. valuearray_add_valuearray(vals,oneval,0);
  176. }
  177. void
  178. valuearray_add_valuearray(Slapi_Value ***vals, Slapi_Value **addvals, PRUint32 flags)
  179. {
  180. int valslen;
  181. int addvalslen;
  182. int maxvals;
  183. if(vals == NULL){
  184. return;
  185. }
  186. addvalslen= valuearray_count(addvals);
  187. if(*vals == NULL)
  188. {
  189. valslen= 0;
  190. maxvals= 0;
  191. }
  192. else
  193. {
  194. valslen= valuearray_count(*vals);
  195. maxvals= valslen+1;
  196. }
  197. valuearray_add_valuearray_fast(vals,addvals,valslen,addvalslen,&maxvals,1/*Exact*/,flags & SLAPI_VALUE_FLAG_PASSIN);
  198. }
  199. int
  200. valuearray_count( Slapi_Value **va)
  201. {
  202. int i=0;
  203. if(va!=NULL)
  204. {
  205. while(NULL != va[i]) i++;
  206. }
  207. return(i);
  208. }
  209. int
  210. valuearray_isempty( Slapi_Value **va)
  211. {
  212. return va==NULL || va[0]==NULL;
  213. }
  214. /*
  215. * JCM SLOW FUNCTION
  216. *
  217. * WARNING: Use only if you absolutley need to...
  218. * This function mostly exists to map from the old slapi berval
  219. * based interface to the new Slapi_Value based interfaces.
  220. */
  221. int
  222. valuearray_init_bervalarray(struct berval **bvals, Slapi_Value ***cvals)
  223. {
  224. int n;
  225. for(n=0; bvals != NULL && bvals[n] != NULL; n++);
  226. if(n==0)
  227. {
  228. *cvals = NULL;
  229. }
  230. else
  231. {
  232. int i;
  233. *cvals = (Slapi_Value **) slapi_ch_malloc((n + 1) * sizeof(Slapi_Value *));
  234. for(i=0;i<n;i++)
  235. {
  236. (*cvals)[i] = slapi_value_new_berval(bvals[i]);
  237. }
  238. (*cvals)[i] = NULL;
  239. }
  240. return n;
  241. }
  242. /*
  243. * JCM SLOW FUNCTION
  244. *
  245. * WARNING: Use only if you absolutley need to...
  246. * This function mostly exists to map from the old slapi berval
  247. * based interface to the new Slapi_Value based interfaces.
  248. */
  249. int
  250. valuearray_init_bervalarray_with_flags(struct berval **bvals, Slapi_Value ***cvals, unsigned long flags)
  251. {
  252. int n;
  253. for(n=0; bvals != NULL && bvals[n] != NULL; n++);
  254. if(n==0)
  255. {
  256. *cvals = NULL;
  257. }
  258. else
  259. {
  260. int i;
  261. *cvals = (Slapi_Value **) slapi_ch_malloc((n + 1) * sizeof(Slapi_Value *));
  262. for(i=0;i<n;i++)
  263. {
  264. (*cvals)[i] = slapi_value_new_berval(bvals[i]);
  265. slapi_value_set_flags((*cvals)[i], flags);
  266. }
  267. (*cvals)[i] = NULL;
  268. }
  269. return n;
  270. }
  271. /*
  272. * JCM SLOW FUNCTION
  273. *
  274. * Use only if you absolutley need to...
  275. * This function mostly exists to map from the old slapi berval
  276. * based interface to the new Slapi_Value based interfaces.
  277. */
  278. int
  279. valuearray_get_bervalarray(Slapi_Value **cvals,struct berval ***bvals)
  280. {
  281. int i,n;
  282. n= valuearray_count(cvals);
  283. if (0 == n)
  284. {
  285. *bvals = NULL;
  286. }
  287. else
  288. {
  289. *bvals = (struct berval **)slapi_ch_malloc((n + 1) * sizeof(struct berval *));
  290. for(i=0;i<n;i++)
  291. {
  292. (*bvals)[i] = ber_bvdup((struct berval *)slapi_value_get_berval(cvals[i]));
  293. }
  294. (*bvals)[i] = NULL;
  295. }
  296. return(0);
  297. }
  298. void
  299. valuearray_free_ext(Slapi_Value ***va, int idx)
  300. {
  301. if(va!=NULL && *va!=NULL)
  302. {
  303. for(; (*va)[idx]!=NULL; idx++)
  304. {
  305. slapi_value_free(&(*va)[idx]);
  306. }
  307. slapi_ch_free((void **)va);
  308. *va = NULL;
  309. }
  310. }
  311. void
  312. valuearray_free(Slapi_Value ***va)
  313. {
  314. valuearray_free_ext(va, 0);
  315. }
  316. int
  317. valuearray_next_value( Slapi_Value **va, int index, Slapi_Value **v)
  318. {
  319. int r= -1;
  320. if(va!=NULL && va[0]!=NULL)
  321. {
  322. r= index+1;
  323. *v= va[r];
  324. if(*v==NULL)
  325. {
  326. r= -1;
  327. }
  328. }
  329. else
  330. {
  331. *v= NULL;
  332. }
  333. return r;
  334. }
  335. int
  336. valuearray_first_value( Slapi_Value **va, Slapi_Value **v )
  337. {
  338. return valuearray_next_value( va, -1, v);
  339. }
  340. /*
  341. * Find the value and return an index number to it.
  342. */
  343. int
  344. valuearray_find(const Slapi_Attr *a, Slapi_Value **va, const Slapi_Value *v)
  345. {
  346. int i= 0;
  347. int found= -1;
  348. while(found==-1 && va!=NULL && va[i]!=NULL)
  349. {
  350. if(slapi_value_compare( a, v, va[i])==0)
  351. {
  352. found= i;
  353. }
  354. else
  355. {
  356. i++;
  357. }
  358. }
  359. return found;
  360. }
  361. /*
  362. * Shunt up the array to cover the value to remove.
  363. */
  364. void
  365. valuearray_remove_value_atindex(Slapi_Value **va, int index)
  366. {
  367. if(va!=NULL && va[0]!=NULL)
  368. {
  369. int k;
  370. for ( k = index + 1; va[k] != NULL; k++ )
  371. {
  372. va[k - 1] = va[k];
  373. }
  374. va[k - 1] = NULL;
  375. }
  376. }
  377. /*
  378. * Subtract bvalues from value array
  379. * return value: subtracted count
  380. */
  381. int
  382. valuearray_subtract_bvalues(Slapi_Value **va, struct berval **bvals)
  383. {
  384. Slapi_Value **vap;
  385. struct berval **bvp;
  386. int rv = 0;
  387. if (NULL == va || NULL == *va || NULL == bvals || NULL == *bvals) {
  388. return rv; /* No op */
  389. }
  390. for (vap = va; vap && *vap; vap++) {
  391. for (bvp = bvals; bvp && *bvp; bvp++) {
  392. if (0 == slapi_berval_cmp(&(*vap)->bv, *bvp)) {
  393. Slapi_Value **vapp;
  394. slapi_value_free(vap);
  395. for (vapp = vap; vapp && *vapp; vapp++) {
  396. *vapp = *(vapp + 1);
  397. }
  398. vapp++;
  399. rv++;
  400. }
  401. }
  402. }
  403. return rv;
  404. }
  405. /*
  406. * Find the value in the array,
  407. * shunt up the array to cover it,
  408. * return a ptr to the value.
  409. * The caller is responsible for freeing the value.
  410. */
  411. Slapi_Value *
  412. valuearray_remove_value(const Slapi_Attr *a, Slapi_Value **va, const Slapi_Value *v)
  413. {
  414. Slapi_Value *r= NULL;
  415. int i= 0;
  416. i= valuearray_find(a, va, v);
  417. if(i!=-1)
  418. {
  419. r= va[i];
  420. valuearray_remove_value_atindex(va,i);
  421. }
  422. return r;
  423. }
  424. /*
  425. * Remove any values older than the CSN.
  426. */
  427. int
  428. valuearray_purge(Slapi_Value ***va, const CSN *csn)
  429. {
  430. int numValues=0;
  431. int i=0;
  432. int nextValue=0;
  433. PR_ASSERT(va!=NULL && *va!=NULL);
  434. /* Loop over all the values freeing the old ones. */
  435. for(i=0; (*va)[i]; i++)
  436. {
  437. csnset_purge(&((*va)[i]->v_csnset),csn);
  438. if ((*va)[i]->v_csnset == NULL)
  439. {
  440. slapi_value_free(&(*va)[i]);
  441. (*va)[i] = NULL;
  442. }
  443. }
  444. /* Now compact the value list. */
  445. numValues=i;
  446. nextValue = 0;
  447. i = 0;
  448. for(i=0;i<numValues;i++)
  449. {
  450. while((nextValue < numValues) && (NULL == (*va)[nextValue]))
  451. {
  452. nextValue++;
  453. }
  454. if(nextValue < numValues)
  455. {
  456. (*va)[i] = (*va)[nextValue];
  457. nextValue++;
  458. }
  459. else
  460. {
  461. break;
  462. }
  463. }
  464. (*va)[i] = NULL;
  465. /* All the values were deleted, we can discard the whole array. */
  466. if(NULL == (*va)[0])
  467. {
  468. slapi_ch_free((void**)va);
  469. *va= NULL;
  470. }
  471. /* return the number of remaining values */
  472. return(i);
  473. }
  474. size_t
  475. valuearray_size(Slapi_Value **va)
  476. {
  477. size_t s= 0;
  478. if(va!=NULL && va[0]!=NULL)
  479. {
  480. int i;
  481. for (i = 0; va[i]; i++)
  482. {
  483. s += value_size(va[i]);
  484. }
  485. s += (i + 1) * sizeof(Slapi_Value*);
  486. }
  487. return s;
  488. }
  489. void
  490. valuearray_update_csn(Slapi_Value **va, CSNType t, const CSN *csn)
  491. {
  492. int i;
  493. for (i = 0; va!=NULL && va[i]; i++)
  494. {
  495. value_update_csn(va[i],t,csn);
  496. }
  497. }
  498. /* <=========================== Value Array Fast ==========================> */
  499. void
  500. valuearrayfast_init(struct valuearrayfast *vaf,Slapi_Value **va)
  501. {
  502. vaf->num= valuearray_count(va);
  503. vaf->max= vaf->num;
  504. vaf->va= va;
  505. }
  506. void
  507. valuearrayfast_done(struct valuearrayfast *vaf)
  508. {
  509. if(vaf->va!=NULL)
  510. {
  511. int i;
  512. for(i=0; i<vaf->num; i++)
  513. {
  514. slapi_value_free(&vaf->va[i]);
  515. }
  516. slapi_ch_free((void **)&vaf->va);
  517. vaf->num= 0;
  518. vaf->max= 0;
  519. }
  520. }
  521. void
  522. valuearrayfast_add_value(struct valuearrayfast *vaf,const Slapi_Value *v)
  523. {
  524. valuearray_add_value_fast(&vaf->va,(Slapi_Value *)v,vaf->num,&vaf->max,0/*Exact*/,0/*!PassIn*/);
  525. vaf->num++;
  526. }
  527. void
  528. valuearrayfast_add_value_passin(struct valuearrayfast *vaf,Slapi_Value *v)
  529. {
  530. valuearray_add_value_fast(&vaf->va,v,vaf->num,&vaf->max,0/*Exact*/,1/*PassIn*/);
  531. vaf->num++;
  532. }
  533. /* <=========================== Value Set =======================> */
  534. #define VALUESET_ARRAY_SORT_THRESHOLD 10
  535. #define VALUESET_ARRAY_MINSIZE 2
  536. #define VALUESET_ARRAY_MAXINCREMENT 4096
  537. Slapi_ValueSet *
  538. slapi_valueset_new()
  539. {
  540. Slapi_ValueSet *vs = (Slapi_ValueSet *)slapi_ch_calloc(1,sizeof(Slapi_ValueSet));
  541. if(vs)
  542. slapi_valueset_init(vs);
  543. return vs;
  544. }
  545. void
  546. slapi_valueset_init(Slapi_ValueSet *vs)
  547. {
  548. if(vs!=NULL)
  549. {
  550. vs->va= NULL;
  551. vs->sorted = NULL;
  552. vs->num = 0;
  553. vs->max = 0;
  554. }
  555. }
  556. void
  557. slapi_valueset_done(Slapi_ValueSet *vs)
  558. {
  559. if(vs!=NULL)
  560. {
  561. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  562. if(vs->va!=NULL)
  563. {
  564. valuearray_free(&vs->va);
  565. vs->va= NULL;
  566. }
  567. if (vs->sorted != NULL)
  568. {
  569. slapi_ch_free ((void **)&vs->sorted);
  570. vs->sorted = NULL;
  571. }
  572. vs->num = 0;
  573. vs->max = 0;
  574. }
  575. }
  576. void
  577. slapi_valueset_free(Slapi_ValueSet *vs)
  578. {
  579. if(vs!=NULL)
  580. {
  581. slapi_valueset_done(vs);
  582. slapi_ch_free((void **)&vs);
  583. }
  584. }
  585. void
  586. slapi_valueset_set_from_smod(Slapi_ValueSet *vs, Slapi_Mod *smod)
  587. {
  588. Slapi_Value **va= NULL;
  589. valuearray_init_bervalarray(slapi_mod_get_ldapmod_byref(smod)->mod_bvalues, &va);
  590. valueset_set_valuearray_passin(vs, va);
  591. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  592. }
  593. void
  594. valueset_set_valuearray_byval(Slapi_ValueSet *vs, Slapi_Value **addvals)
  595. {
  596. int i, j=0;
  597. slapi_valueset_init(vs);
  598. vs->num = valuearray_count(addvals);
  599. vs->max = vs->num + 1;
  600. vs->va = (Slapi_Value **) slapi_ch_malloc( vs->max * sizeof(Slapi_Value *));
  601. for ( i = 0, j = 0; i < vs->num; i++)
  602. {
  603. if ( addvals[i]!=NULL )
  604. {
  605. /* We copy the values */
  606. vs->va[j] = slapi_value_dup(addvals[i]);
  607. j++;
  608. }
  609. }
  610. vs->va[j] = NULL;
  611. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  612. }
  613. /* WARNING: you must call this function with a new vs - if it points to existing data, it
  614. * will leak - call slapi_valueset_done to free it first if necessary
  615. */
  616. void
  617. valueset_set_valuearray_passin(Slapi_ValueSet *vs, Slapi_Value **addvals)
  618. {
  619. slapi_valueset_init(vs);
  620. vs->va= addvals;
  621. vs->num = valuearray_count(addvals);
  622. vs->max = vs->num + 1;
  623. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  624. }
  625. /* WARNING: you must call this function with a new vs1 - if it points to existing data, it
  626. * will leak - call slapi_valueset_done(vs1) to free it first if necessary
  627. */
  628. void
  629. slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
  630. {
  631. slapi_valueset_init(vs1);
  632. valueset_set_valueset(vs1,vs2);
  633. }
  634. void
  635. slapi_valueset_join_attr_valueset(const Slapi_Attr *a, Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
  636. {
  637. if (slapi_valueset_isempty(vs1))
  638. valueset_set_valueset(vs1,vs2);
  639. else
  640. slapi_valueset_add_attr_valuearray_ext (a, vs1, vs2->va, vs2->num, 0, NULL);
  641. }
  642. int
  643. slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
  644. {
  645. if (NULL == vs) {
  646. if (v) {
  647. *v = NULL;
  648. }
  649. return 0;
  650. }
  651. return valuearray_first_value(vs->va,v);
  652. }
  653. int
  654. slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
  655. {
  656. if (NULL == vs) {
  657. if (v) {
  658. *v = NULL;
  659. }
  660. return index;
  661. }
  662. return valuearray_next_value(vs->va,index,v);
  663. }
  664. int
  665. slapi_valueset_count( const Slapi_ValueSet *vs)
  666. {
  667. if (NULL != vs)
  668. {
  669. return (vs->num);
  670. }
  671. return 0;
  672. }
  673. int
  674. slapi_valueset_isempty( const Slapi_ValueSet *vs)
  675. {
  676. if (NULL != vs)
  677. {
  678. return (vs->num == 0);
  679. }
  680. return 1;
  681. }
  682. int
  683. valueset_isempty( const Slapi_ValueSet *vs)
  684. {
  685. if (NULL == vs) {
  686. return 1; /* true */
  687. }
  688. return valuearray_isempty(vs->va);
  689. }
  690. Slapi_Value *
  691. slapi_valueset_find(const Slapi_Attr *a, const Slapi_ValueSet *vs, const Slapi_Value *v)
  692. {
  693. Slapi_Value *r= NULL;
  694. if(vs && (vs->num > 0))
  695. {
  696. if (vs->sorted) {
  697. r = valueset_find_sorted(a,vs,v,NULL);
  698. } else {
  699. int i= valuearray_find(a,vs->va,v);
  700. if(i!=-1) {
  701. r= vs->va[i];
  702. }
  703. }
  704. }
  705. return r;
  706. }
  707. /*
  708. * The value is found in the set, removed and returned.
  709. * The caller is responsible for freeing the value.
  710. *
  711. * The _sorted function also handles the cleanup of the sorted array
  712. */
  713. Slapi_Value *
  714. valueset_remove_value_sorted(const Slapi_Attr *a, Slapi_ValueSet *vs, const Slapi_Value *v)
  715. {
  716. Slapi_Value *r= NULL;
  717. int i, position = 0;
  718. r = valueset_find_sorted(a,vs,v,&position);
  719. if (r) {
  720. /* the value was found, remove from valuearray */
  721. int index = vs->sorted[position];
  722. memmove(&vs->sorted[position],&vs->sorted[position+1],(vs->num - position)*sizeof(int));
  723. memmove(&vs->va[index],&vs->va[index+1],(vs->num - index)*sizeof(Slapi_Value *));
  724. vs->num--;
  725. /* unfortunately the references in the sorted array
  726. * to values past the removed one are no longer correct
  727. * need to adjust */
  728. for (i=0; i < vs->num; i++) {
  729. if (vs->sorted[i] > index) vs->sorted[i]--;
  730. }
  731. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  732. }
  733. return r;
  734. }
  735. Slapi_Value *
  736. valueset_remove_value(const Slapi_Attr *a, Slapi_ValueSet *vs, const Slapi_Value *v)
  737. {
  738. Slapi_Value *r = NULL;
  739. if (vs->sorted) {
  740. r = valueset_remove_value_sorted(a, vs, v);
  741. } else {
  742. if(!valuearray_isempty(vs->va))
  743. {
  744. r= valuearray_remove_value(a, vs->va, v);
  745. if (r)
  746. vs->num--;
  747. }
  748. }
  749. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  750. return r;
  751. }
  752. /*
  753. * Remove any values older than the CSN.
  754. */
  755. int
  756. valueset_purge(Slapi_ValueSet *vs, const CSN *csn)
  757. {
  758. int r= 0;
  759. if(!valuearray_isempty(vs->va)) {
  760. /* valuearray_purge is not valueset and sorting aware,
  761. * maybe need to rewrite, at least keep the valueset
  762. * consistent
  763. */
  764. r= valuearray_purge(&vs->va, csn);
  765. vs->num = r;
  766. if (vs->va == NULL) {
  767. /* va was freed */
  768. vs->max = 0;
  769. }
  770. /* we can no longer rely on the sorting */
  771. if (vs->sorted != NULL)
  772. {
  773. slapi_ch_free ((void **)&vs->sorted);
  774. vs->sorted = NULL;
  775. }
  776. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  777. }
  778. return 0;
  779. }
  780. Slapi_Value **
  781. valueset_get_valuearray(const Slapi_ValueSet *vs)
  782. {
  783. return (Slapi_Value**)vs->va;
  784. }
  785. size_t
  786. valueset_size(const Slapi_ValueSet *vs)
  787. {
  788. size_t s= 0;
  789. if(!valuearray_isempty(vs->va))
  790. {
  791. s= valuearray_size(vs->va);
  792. }
  793. return s;
  794. }
  795. /*
  796. * The value array is passed in by value.
  797. */
  798. void
  799. slapi_valueset_add_valuearray(const Slapi_Attr *a, Slapi_ValueSet *vs, Slapi_Value **addvals)
  800. {
  801. if(!valuearray_isempty(addvals))
  802. {
  803. slapi_valueset_add_attr_valuearray_ext (a, vs, addvals, valuearray_count(addvals), 0, NULL);
  804. }
  805. }
  806. void
  807. valueset_add_valuearray(Slapi_ValueSet *vs, Slapi_Value **addvals)
  808. {
  809. if(!valuearray_isempty(addvals))
  810. {
  811. slapi_valueset_add_attr_valuearray_ext (NULL, vs, addvals, valuearray_count(addvals), 0, NULL);
  812. }
  813. }
  814. void
  815. valueset_add_valuearray_ext(Slapi_ValueSet *vs, Slapi_Value **addvals, PRUint32 flags)
  816. {
  817. if(!valuearray_isempty(addvals))
  818. {
  819. slapi_valueset_add_attr_valuearray_ext (NULL, vs, addvals, valuearray_count(addvals), flags, NULL);
  820. }
  821. }
  822. /*
  823. * The value is passed in by value.
  824. */
  825. void
  826. slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
  827. {
  828. slapi_valueset_add_value_ext(vs, addval, 0);
  829. }
  830. void
  831. slapi_valueset_add_value_ext(Slapi_ValueSet *vs, const Slapi_Value *addval, unsigned long flags)
  832. {
  833. Slapi_Value *oneval[2];
  834. oneval[0]= (Slapi_Value*)addval;
  835. oneval[1]= NULL;
  836. slapi_valueset_add_attr_valuearray_ext(NULL, vs, oneval, 1, flags, NULL);
  837. }
  838. /* find value v in the sorted array of values, using syntax of attribut a for comparison
  839. *
  840. */
  841. static int
  842. valueset_value_syntax_cmp( const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2 )
  843. {
  844. /* this looks like a huge overhead, but there are no simple functions to normalize and
  845. * compare available
  846. */
  847. const Slapi_Value *oneval[3];
  848. Slapi_Value **keyvals;
  849. int rc = -1;
  850. keyvals = NULL;
  851. oneval[0] = v1;
  852. oneval[1] = v2;
  853. oneval[2] = NULL;
  854. if ( slapi_attr_values2keys_sv( a, (Slapi_Value**)oneval, &keyvals, LDAP_FILTER_EQUALITY ) != 0
  855. || keyvals == NULL
  856. || keyvals[0] == NULL || keyvals[1] == NULL)
  857. {
  858. /* this should never happen since always a syntax plugin to
  859. * generate the keys will be found (there exists a default plugin)
  860. * log an error and continue.
  861. */
  862. LDAPDebug( LDAP_DEBUG_ANY, "valueset_value_syntax_cmp: "
  863. "slapi_attr_values2keys_sv failed for type %s\n",
  864. a->a_type, 0, 0 );
  865. } else {
  866. struct berval *bv1, *bv2;
  867. bv1 = &keyvals[0]->bv;
  868. bv2 = &keyvals[1]->bv;
  869. if ( bv1->bv_len < bv2->bv_len ) {
  870. rc = -1;
  871. } else if ( bv1->bv_len > bv2->bv_len ) {
  872. rc = 1;
  873. } else {
  874. rc = memcmp( bv1->bv_val, bv2->bv_val, bv1->bv_len );
  875. }
  876. }
  877. if (keyvals != NULL)
  878. valuearray_free( &keyvals );
  879. return (rc);
  880. }
  881. static int
  882. valueset_value_cmp( const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2 )
  883. {
  884. if ( a == NULL || slapi_attr_is_dn_syntax_attr((Slapi_Attr *)a)) {
  885. /* if no attr is provided just do a utf8compare */
  886. /* for all the values the first step of normalization is done,
  887. * case folding still needs to be done
  888. */
  889. /* would this be enough ?: return (strcasecmp(v1->bv.bv_val, v2->bv.bv_val)); */
  890. return (slapi_utf8casecmp((unsigned char*)v1->bv.bv_val, (unsigned char*)v2->bv.bv_val));
  891. } else {
  892. /* slapi_value_compare doesn't work, it only returns 0 or -1
  893. return (slapi_value_compare(a, v1, v2));
  894. * use special compare, base on what valuetree_find did
  895. */
  896. return(valueset_value_syntax_cmp(a, v1, v2));
  897. }
  898. }
  899. /* find a value in the sorted valuearray.
  900. * If the value is found the pointer to the value is returned and if index is provided
  901. * it will return the index of the value in the valuearray
  902. * If the value is not found, index will contain the place where the value would be inserted
  903. */
  904. Slapi_Value *
  905. valueset_find_sorted (const Slapi_Attr *a, const Slapi_ValueSet *vs, const Slapi_Value *v, int *index)
  906. {
  907. int cmp = -1;
  908. int bot = -1;
  909. int top;
  910. if (vs->num == 0) {
  911. /* empty valueset */
  912. if (index) *index = 0;
  913. return (NULL);
  914. } else {
  915. top = vs->num;
  916. }
  917. while (top - bot > 1) {
  918. int mid = (top + bot)/2;
  919. if ( (cmp = valueset_value_cmp(a, v, vs->va[vs->sorted[mid]])) > 0)
  920. bot = mid;
  921. else
  922. top = mid;
  923. }
  924. if (index) *index = top;
  925. /* check if the value is found */
  926. if ( top < vs->num && (0 == valueset_value_cmp(a, v, vs->va[vs->sorted[top]])))
  927. return (vs->va[vs->sorted[top]]);
  928. else
  929. return (NULL);
  930. }
  931. void
  932. valueset_array_to_sorted (const Slapi_Attr *a, Slapi_ValueSet *vs)
  933. {
  934. int i, j, swap;
  935. /* initialize sort array */
  936. for (i = 0; i < vs->num; i++)
  937. vs->sorted[i] = i;
  938. /* now sort it, use a simple insertion sort as the array will always
  939. * be very small when initially sorted
  940. */
  941. for (i = 1; i < vs->num; i++) {
  942. swap = vs->sorted[i];
  943. j = i -1;
  944. while ( j >= 0 && valueset_value_cmp (a, vs->va[vs->sorted[j]], vs->va[swap]) > 0 ) {
  945. vs->sorted[j+1] = vs->sorted[j];
  946. j--;
  947. }
  948. vs->sorted[j+1] = swap;
  949. }
  950. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  951. }
  952. /* insert a value into a sorted array, if dupcheck is set no duplicate values will be accepted
  953. * (is there a reason to allow duplicates ? LK
  954. * if the value is inserted the the function returns the index where it was inserted
  955. * if the value already exists -index is returned to indicate anerror an the index of the existing value
  956. */
  957. int
  958. valueset_insert_value_to_sorted(const Slapi_Attr *a, Slapi_ValueSet *vs, Slapi_Value *vi, int dupcheck)
  959. {
  960. int index = -1;
  961. Slapi_Value *v;
  962. /* test for pre sorted array and to avoid boundary condition */
  963. if (vs->num == 0) {
  964. vs->sorted[0] = 0;
  965. vs->num++;
  966. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  967. return(0);
  968. } else if (valueset_value_cmp (a, vi, vs->va[vs->sorted[vs->num-1]]) > 0 ) {
  969. vs->sorted[vs->num] = vs->num;
  970. vs->num++;
  971. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  972. return (vs->num);
  973. }
  974. v = valueset_find_sorted (a, vs, vi, &index);
  975. if (v && dupcheck) {
  976. /* value already exists, do not insert duplicates */
  977. return (-1);
  978. } else {
  979. memmove(&vs->sorted[index+1],&vs->sorted[index],(vs->num - index)* sizeof(int));
  980. vs->sorted[index] = vs->num;
  981. vs->num++;
  982. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  983. return(index);
  984. }
  985. }
  986. /*
  987. * If this function returns an error, it is safe to do both
  988. * slapi_valueset_done(vs);
  989. * and
  990. * valuearray_free(&addvals);
  991. * if there is an error and the PASSIN flag is used, the addvals array will own all of the values
  992. * vs will own none of the values - so you should do both slapi_valueset_done(vs) and valuearray_free(&addvals)
  993. * to clean up
  994. */
  995. int
  996. slapi_valueset_add_attr_valuearray_ext(const Slapi_Attr *a, Slapi_ValueSet *vs,
  997. Slapi_Value **addvals, int naddvals, unsigned long flags, int *dup_index)
  998. {
  999. int rc = LDAP_SUCCESS;
  1000. int i, dup;
  1001. int allocate = 0;
  1002. int need;
  1003. int passin = flags & SLAPI_VALUE_FLAG_PASSIN;
  1004. int dupcheck = flags & SLAPI_VALUE_FLAG_DUPCHECK;
  1005. if (naddvals == 0)
  1006. return (rc);
  1007. need = vs->num + naddvals + 1;
  1008. if (need > vs->max) {
  1009. /* Expand the array */
  1010. allocate= vs->max;
  1011. if ( allocate == 0 ) /* initial allocation */
  1012. allocate = VALUESET_ARRAY_MINSIZE;
  1013. while ( allocate < need )
  1014. {
  1015. if (allocate > VALUESET_ARRAY_MAXINCREMENT )
  1016. /* do not grow exponentially */
  1017. allocate += VALUESET_ARRAY_MAXINCREMENT;
  1018. else
  1019. allocate *= 2;
  1020. }
  1021. }
  1022. if(allocate>0)
  1023. {
  1024. if(vs->va==NULL)
  1025. {
  1026. vs->va = (Slapi_Value **) slapi_ch_malloc( allocate * sizeof(Slapi_Value *));
  1027. }
  1028. else
  1029. {
  1030. vs->va = (Slapi_Value **) slapi_ch_realloc( (char *) vs->va, allocate * sizeof(Slapi_Value *));
  1031. if (vs->sorted) {
  1032. vs->sorted = (int *) slapi_ch_realloc( (char *) vs->sorted, allocate * sizeof(int));
  1033. }
  1034. }
  1035. vs->max= allocate;
  1036. }
  1037. if ( (vs->num + naddvals > VALUESET_ARRAY_SORT_THRESHOLD || dupcheck ) &&
  1038. !vs->sorted ) {
  1039. /* initialize sort array and do initial sort */
  1040. vs->sorted = (int *) slapi_ch_malloc( vs->max* sizeof(int));
  1041. valueset_array_to_sorted (a, vs);
  1042. }
  1043. for ( i = 0; i < naddvals; i++)
  1044. {
  1045. if ( addvals[i]!=NULL )
  1046. {
  1047. if(passin)
  1048. {
  1049. /* We consume the values */
  1050. (vs->va)[vs->num] = addvals[i];
  1051. }
  1052. else
  1053. {
  1054. /* We copy the values */
  1055. (vs->va)[vs->num] = slapi_value_dup(addvals[i]);
  1056. }
  1057. if (vs->sorted) {
  1058. dup = valueset_insert_value_to_sorted(a, vs, (vs->va)[vs->num], dupcheck);
  1059. if (dup < 0 ) {
  1060. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  1061. if (dup_index) *dup_index = i;
  1062. if (passin) {
  1063. PR_ASSERT((i == 0) || dup_index);
  1064. /* caller must provide dup_index to know how far we got in addvals */
  1065. (vs->va)[vs->num] = NULL;
  1066. } else {
  1067. slapi_value_free(&(vs->va)[vs->num]);
  1068. }
  1069. break;
  1070. }
  1071. } else {
  1072. vs->num++;
  1073. }
  1074. }
  1075. }
  1076. (vs->va)[vs->num] = NULL;
  1077. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  1078. return (rc);
  1079. }
  1080. int
  1081. slapi_valueset_add_attr_value_ext(const Slapi_Attr *a, Slapi_ValueSet *vs, Slapi_Value *addval, unsigned long flags)
  1082. {
  1083. Slapi_Value *oneval[2];
  1084. int rc;
  1085. oneval[0]= (Slapi_Value*)addval;
  1086. oneval[1]= NULL;
  1087. rc = slapi_valueset_add_attr_valuearray_ext(a, vs, oneval, 1, flags, NULL );
  1088. return (rc);
  1089. }
  1090. /*
  1091. * The string is passed in by value.
  1092. */
  1093. void
  1094. valueset_add_string(const Slapi_Attr *a, Slapi_ValueSet *vs, const char *s, CSNType t, const CSN *csn)
  1095. {
  1096. Slapi_Value v;
  1097. value_init(&v,NULL,t,csn);
  1098. slapi_value_set_string(&v,s);
  1099. slapi_valueset_add_attr_value_ext(a, vs, &v, 0 );
  1100. value_done(&v);
  1101. }
  1102. /*
  1103. * The value set is passed in by value.
  1104. */
  1105. void
  1106. valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
  1107. {
  1108. int i;
  1109. if (vs1 && vs2) {
  1110. int oldmax = vs1->max;
  1111. /* pre-condition - vs1 empty - otherwise, existing data is overwritten */
  1112. PR_ASSERT(vs1->num == 0);
  1113. if (vs2->va) {
  1114. /* need to copy valuearray */
  1115. if (vs2->max == 0) {
  1116. /* temporary hack, not all valuesets were created properly. fix it now */
  1117. vs1->num = valuearray_count(vs2->va);
  1118. vs1->max = vs1->num + 1;
  1119. } else {
  1120. vs1->num = vs2->num;
  1121. vs1->max = vs2->max;
  1122. }
  1123. /* do we need more room? */
  1124. if ((NULL == vs1->va) || (oldmax < vs1->max)) {
  1125. vs1->va = (Slapi_Value **)slapi_ch_realloc((char *)vs1->va, vs1->max * sizeof(Slapi_Value *));
  1126. }
  1127. for (i=0; i< vs1->num;i++) {
  1128. vs1->va[i] = slapi_value_dup(vs2->va[i]);
  1129. }
  1130. vs1->va[vs1->num] = NULL;
  1131. } else {
  1132. valuearray_free(&vs1->va);
  1133. }
  1134. if (vs2->sorted) {
  1135. if ((NULL == vs1->sorted) || (oldmax < vs1->max)) {
  1136. vs1->sorted = (int *)slapi_ch_realloc((char *)vs1->sorted, vs1->max * sizeof(int));
  1137. }
  1138. memcpy(&vs1->sorted[0], &vs2->sorted[0], vs1->num * sizeof(int));
  1139. } else {
  1140. slapi_ch_free((void **)&vs1->sorted);
  1141. }
  1142. /* post-condition */
  1143. PR_ASSERT((vs1->sorted == NULL) || (vs1->num == 0) || ((vs1->sorted[0] >= 0) && (vs1->sorted[0] < vs1->num)));
  1144. }
  1145. }
  1146. void
  1147. valueset_remove_string(const Slapi_Attr *a, Slapi_ValueSet *vs, const char *s)
  1148. {
  1149. Slapi_Value v;
  1150. Slapi_Value *removed;
  1151. value_init(&v,NULL,CSN_TYPE_NONE,NULL);
  1152. slapi_value_set_string(&v,s);
  1153. removed = valueset_remove_value(a, vs, &v);
  1154. if(removed) {
  1155. slapi_value_free(&removed);
  1156. }
  1157. value_done(&v);
  1158. }
  1159. void
  1160. valueset_update_csn(Slapi_ValueSet *vs, CSNType t, const CSN *csn)
  1161. {
  1162. if(!valuearray_isempty(vs->va))
  1163. {
  1164. valuearray_update_csn(vs->va,t,csn);
  1165. }
  1166. }
  1167. /*
  1168. * Remove an array of values from a value set.
  1169. * The removed values are passed back in an array.
  1170. *
  1171. * Flags
  1172. * SLAPI_VALUE_FLAG_PRESERVECSNSET - csnset in the value set is duplicated and
  1173. * preserved in the matched element of the
  1174. * array of values.
  1175. * SLAPI_VALUE_FLAG_IGNOREERROR - ignore an error: Couldn't find the value to
  1176. * be deleted.
  1177. * SLAPI_VALUE_FLAG_USENEWVALUE - replace the value between the value set and
  1178. * the matched element of the array of values
  1179. * (used by entry_add_present_values_wsi).
  1180. *
  1181. * Returns
  1182. * LDAP_SUCCESS - OK.
  1183. * LDAP_NO_SUCH_ATTRIBUTE - A value to be deleted was not in the value set.
  1184. * LDAP_OPERATIONS_ERROR - Something very bad happened.
  1185. */
  1186. int
  1187. valueset_remove_valuearray(Slapi_ValueSet *vs, const Slapi_Attr *a, Slapi_Value **valuestodelete, int flags, Slapi_Value ***va_out)
  1188. {
  1189. int rc= LDAP_SUCCESS;
  1190. if(vs->num > 0)
  1191. {
  1192. int i;
  1193. struct valuearrayfast vaf_out;
  1194. if ( va_out )
  1195. {
  1196. valuearrayfast_init(&vaf_out,*va_out);
  1197. }
  1198. /*
  1199. * For larger valuesets the valuarray is sorted, values can be deleted individually
  1200. *
  1201. */
  1202. for ( i = 0; rc==LDAP_SUCCESS && valuestodelete[i] != NULL; ++i )
  1203. {
  1204. Slapi_Value *found = valueset_remove_value(a, vs, valuestodelete[i]);
  1205. if(found!=NULL)
  1206. {
  1207. if ( va_out )
  1208. {
  1209. if (found->v_csnset &&
  1210. (flags & (SLAPI_VALUE_FLAG_PRESERVECSNSET|
  1211. SLAPI_VALUE_FLAG_USENEWVALUE)))
  1212. {
  1213. valuestodelete[i]->v_csnset = csnset_dup (found->v_csnset);
  1214. }
  1215. if (flags & SLAPI_VALUE_FLAG_USENEWVALUE)
  1216. {
  1217. valuearrayfast_add_value_passin(&vaf_out,valuestodelete[i]);
  1218. valuestodelete[i] = found;
  1219. }
  1220. else
  1221. {
  1222. valuearrayfast_add_value_passin(&vaf_out,found);
  1223. }
  1224. }
  1225. else
  1226. {
  1227. if (flags & SLAPI_VALUE_FLAG_PRESERVECSNSET)
  1228. {
  1229. valuestodelete[i]->v_csnset = found->v_csnset;
  1230. found->v_csnset = NULL;
  1231. }
  1232. slapi_value_free ( & found );
  1233. }
  1234. }
  1235. else
  1236. {
  1237. if((flags & SLAPI_VALUE_FLAG_IGNOREERROR) == 0)
  1238. {
  1239. LDAPDebug( LDAP_DEBUG_ARGS,"could not find value %d for attr %s\n", i-1, a->a_type, 0 );
  1240. rc= LDAP_NO_SUCH_ATTRIBUTE;
  1241. }
  1242. }
  1243. }
  1244. if ( va_out )
  1245. {
  1246. *va_out= vaf_out.va;
  1247. if(rc!=LDAP_SUCCESS)
  1248. {
  1249. valuearray_free(va_out);
  1250. }
  1251. }
  1252. }
  1253. return rc;
  1254. }
  1255. Slapi_ValueSet *
  1256. valueset_dup(const Slapi_ValueSet *dupee)
  1257. {
  1258. Slapi_ValueSet *duped = slapi_valueset_new();
  1259. if (NULL!=duped)
  1260. {
  1261. valueset_set_valuearray_byval(duped,dupee->va);
  1262. }
  1263. return duped;
  1264. }
  1265. /* quickly throw away any old contents of this valueset, and stick in the
  1266. * new ones.
  1267. *
  1268. * return value: LDAP_SUCCESS - OK
  1269. * : LDAP_OPERATIONS_ERROR - duplicated values given
  1270. */
  1271. int
  1272. valueset_replace_valuearray(Slapi_Attr *a, Slapi_ValueSet *vs, Slapi_Value **valstoreplace)
  1273. {
  1274. return (valueset_replace_valuearray_ext(a, vs,valstoreplace, 1));
  1275. }
  1276. int
  1277. valueset_replace_valuearray_ext(Slapi_Attr *a, Slapi_ValueSet *vs, Slapi_Value **valstoreplace, int dupcheck)
  1278. {
  1279. int rc = LDAP_SUCCESS;
  1280. int vals_count = valuearray_count(valstoreplace);
  1281. if (vals_count == 0) {
  1282. /* no new values, just clear the valueset */
  1283. slapi_valueset_done(vs);
  1284. } else if (vals_count == 1 || !dupcheck) {
  1285. /* just repelace the valuearray and adjus num, max */
  1286. slapi_valueset_done(vs);
  1287. vs->va = valstoreplace;
  1288. vs->num = vals_count;
  1289. vs->max = vals_count + 1;
  1290. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  1291. } else {
  1292. /* verify the given values are not duplicated. */
  1293. unsigned long flags = SLAPI_VALUE_FLAG_PASSIN|SLAPI_VALUE_FLAG_DUPCHECK;
  1294. int dupindex = 0;
  1295. Slapi_ValueSet *vs_new = slapi_valueset_new();
  1296. rc = slapi_valueset_add_attr_valuearray_ext (a, vs_new, valstoreplace, vals_count, flags, &dupindex);
  1297. if ( rc == LDAP_SUCCESS )
  1298. {
  1299. /* used passin, so vs_new owns all of the Slapi_Value* in valstoreplace
  1300. * so tell valuearray_free_ext to start at index vals_count, which is
  1301. * NULL, then just free valstoreplace
  1302. */
  1303. valuearray_free_ext(&valstoreplace, vals_count);
  1304. /* values look good - replace the values in the attribute */
  1305. if(!valuearray_isempty(vs->va))
  1306. {
  1307. /* remove old values */
  1308. slapi_valueset_done(vs);
  1309. }
  1310. vs->va = vs_new->va;
  1311. vs_new->va = NULL;
  1312. vs->sorted = vs_new->sorted;
  1313. vs_new->sorted = NULL;
  1314. vs->num = vs_new->num;
  1315. vs->max = vs_new->max;
  1316. slapi_valueset_free (vs_new);
  1317. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  1318. }
  1319. else
  1320. {
  1321. /* caller expects us to own valstoreplace - since we cannot
  1322. use them, just delete them */
  1323. /* using PASSIN, some of the Slapi_Value* are in vs_new, and the rest
  1324. * after dupindex are in valstoreplace
  1325. */
  1326. slapi_valueset_free(vs_new);
  1327. valuearray_free_ext(&valstoreplace, dupindex);
  1328. PR_ASSERT((vs->sorted == NULL) || (vs->num == 0) || ((vs->sorted[0] >= 0) && (vs->sorted[0] < vs->num)));
  1329. }
  1330. }
  1331. return rc;
  1332. }
  1333. /*
  1334. * Search the value set for each value to be update,
  1335. * and update the value with the CSN provided.
  1336. * Updated values are moved from the valuestoupdate
  1337. * array to the valueupdated array.
  1338. */
  1339. void
  1340. valueset_update_csn_for_valuearray(Slapi_ValueSet *vs, const Slapi_Attr *a, Slapi_Value **valuestoupdate, CSNType t, const CSN *csn, Slapi_Value ***valuesupdated)
  1341. {
  1342. valueset_update_csn_for_valuearray_ext(vs, a, valuestoupdate, t, csn, valuesupdated, 0);
  1343. }
  1344. void
  1345. valueset_update_csn_for_valuearray_ext(Slapi_ValueSet *vs, const Slapi_Attr *a, Slapi_Value **valuestoupdate, CSNType t, const CSN *csn, Slapi_Value ***valuesupdated, int csnref_updated)
  1346. {
  1347. if(!valuearray_isempty(valuestoupdate) &&
  1348. !valuearray_isempty(vs->va))
  1349. {
  1350. struct valuearrayfast vaf_valuesupdated;
  1351. valuearrayfast_init(&vaf_valuesupdated,*valuesupdated);
  1352. int i;
  1353. int del_index = -1, del_count = 0;
  1354. for (i=0;valuestoupdate[i]!=NULL;++i)
  1355. {
  1356. int index= valuearray_find(a, vs->va, valuestoupdate[i]);
  1357. if(index!=-1)
  1358. {
  1359. value_update_csn(vs->va[index],t,csn);
  1360. if (csnref_updated)
  1361. valuestoupdate[i]->v_csnset = (CSNSet *)value_get_csnset(vs->va[index]);
  1362. valuearrayfast_add_value_passin(&vaf_valuesupdated,valuestoupdate[i]);
  1363. valuestoupdate[i]= NULL;
  1364. del_count++;
  1365. if (del_index < 0) del_index = i;
  1366. }
  1367. else
  1368. { /* keep the value in valuestoupdate, to keep array compressed, move to first free slot*/
  1369. if (del_index >= 0) {
  1370. valuestoupdate[del_index] = valuestoupdate[i];
  1371. del_index++;
  1372. }
  1373. }
  1374. }
  1375. /* complete compression */
  1376. for (i=0; i<del_count;i++)
  1377. valuestoupdate[del_index+i]= NULL;
  1378. *valuesupdated= vaf_valuesupdated.va;
  1379. }
  1380. }
  1381. int
  1382. valuearray_dn_normalize_value(Slapi_Value **vals)
  1383. {
  1384. int rc = 0;
  1385. Slapi_Value **vp = NULL;
  1386. for (vp = vals; vp && *vp; vp++) {
  1387. rc |= value_dn_normalize_value(*vp);
  1388. }
  1389. return rc;
  1390. }