value.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* value.c - routines for dealing with values */
  13. #undef DEBUG /* disable counters */
  14. #include <prcountr.h>
  15. #include "slap.h"
  16. #include "slapi-private.h"
  17. /*
  18. * Functions needed when a berval is embedded in a struct or
  19. * allocated on the stack rather than the heap.
  20. */
  21. static void ber_bvdone(struct berval *bvp)
  22. {
  23. if (bvp == NULL) return;
  24. slapi_ch_free_string(&bvp->bv_val);
  25. bvp->bv_len = 0;
  26. return;
  27. }
  28. static void ber_bvcpy(struct berval *bvd, const struct berval *bvs)
  29. {
  30. size_t len;
  31. if (bvd == NULL || bvs == NULL) return;
  32. len = bvs->bv_len;
  33. bvd->bv_val = slapi_ch_malloc(len+1);
  34. bvd->bv_len = len;
  35. memcpy(bvd->bv_val, bvs->bv_val, len);
  36. bvd->bv_val[len] = '\0';
  37. return;
  38. }
  39. void
  40. slapi_ber_bvdone(struct berval *bvp)
  41. {
  42. ber_bvdone(bvp);
  43. }
  44. void
  45. slapi_ber_bvcpy(struct berval *bvd, const struct berval *bvs)
  46. {
  47. ber_bvcpy(bvd, bvs);
  48. }
  49. /* <=========================== Slapi_Value ==========================> */
  50. #ifdef VALUE_DEBUG
  51. static void value_dump( const Slapi_Value *value, const char *text);
  52. #define VALUE_DUMP(value,name) value_dump(value,name)
  53. #else
  54. #define VALUE_DUMP(value,name) ((void)0)
  55. #endif
  56. static int counters_created= 0;
  57. PR_DEFINE_COUNTER(slapi_value_counter_created);
  58. PR_DEFINE_COUNTER(slapi_value_counter_deleted);
  59. PR_DEFINE_COUNTER(slapi_value_counter_exist);
  60. Slapi_Value *
  61. slapi_value_new()
  62. {
  63. return value_new(NULL,CSN_TYPE_NONE,NULL);
  64. }
  65. Slapi_Value *
  66. slapi_value_new_berval(const struct berval *bval)
  67. {
  68. return value_new(bval,CSN_TYPE_NONE,NULL);
  69. }
  70. Slapi_Value *
  71. slapi_value_new_value(const Slapi_Value *v)
  72. {
  73. return slapi_value_dup(v);
  74. }
  75. Slapi_Value *
  76. slapi_value_new_string(const char *s)
  77. {
  78. Slapi_Value *v= value_new(NULL,CSN_TYPE_UNKNOWN,NULL);
  79. slapi_value_set_string(v, s);
  80. return v;
  81. }
  82. Slapi_Value *
  83. slapi_value_new_string_passin(char *s)
  84. {
  85. Slapi_Value *v= value_new(NULL,CSN_TYPE_UNKNOWN,NULL);
  86. slapi_value_set_string_passin(v, s);
  87. return v;
  88. }
  89. Slapi_Value *
  90. slapi_value_init(Slapi_Value *v)
  91. {
  92. return value_init(v,NULL,CSN_TYPE_NONE,NULL);
  93. }
  94. Slapi_Value *
  95. slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
  96. {
  97. return value_init(v,bval,CSN_TYPE_NONE,NULL);
  98. }
  99. Slapi_Value *
  100. slapi_value_init_string(Slapi_Value *v,const char *s)
  101. {
  102. value_init(v,NULL,CSN_TYPE_UNKNOWN,NULL);
  103. slapi_value_set_string(v,s);
  104. return v;
  105. }
  106. Slapi_Value *
  107. slapi_value_init_string_passin(Slapi_Value *v, char *s)
  108. {
  109. value_init(v,NULL,CSN_TYPE_UNKNOWN,NULL);
  110. slapi_value_set_string_passin(v,s);
  111. return v;
  112. }
  113. Slapi_Value *
  114. slapi_value_dup(const Slapi_Value *v)
  115. {
  116. Slapi_Value *newvalue= value_new(&v->bv,CSN_TYPE_UNKNOWN,NULL);
  117. newvalue->v_csnset= csnset_dup(v->v_csnset);
  118. newvalue->v_flags = v->v_flags;
  119. return newvalue;
  120. }
  121. Slapi_Value *
  122. value_new(const struct berval *bval,CSNType t,const CSN *csn)
  123. {
  124. Slapi_Value *v;
  125. v = (Slapi_Value *)slapi_ch_malloc(sizeof(Slapi_Value));
  126. value_init(v, bval, t, csn);
  127. if(!counters_created)
  128. {
  129. PR_CREATE_COUNTER(slapi_value_counter_created,"Slapi_Value","created","");
  130. PR_CREATE_COUNTER(slapi_value_counter_deleted,"Slapi_Value","deleted","");
  131. PR_CREATE_COUNTER(slapi_value_counter_exist,"Slapi_Value","exist","");
  132. counters_created= 1;
  133. }
  134. PR_INCREMENT_COUNTER(slapi_value_counter_created);
  135. PR_INCREMENT_COUNTER(slapi_value_counter_exist);
  136. VALUE_DUMP(v,"value_new");
  137. return v;
  138. }
  139. Slapi_Value *
  140. value_init(Slapi_Value *v, const struct berval *bval,CSNType t,const CSN *csn)
  141. {
  142. PR_ASSERT(v!=NULL);
  143. memset(v,0,sizeof(Slapi_Value));
  144. if(csn!=NULL)
  145. {
  146. value_update_csn(v,t,csn);
  147. }
  148. slapi_value_set_berval(v,bval);
  149. return v;
  150. }
  151. void
  152. slapi_value_set_flags(Slapi_Value *v, unsigned long flags)
  153. {
  154. PR_ASSERT(v!=NULL);
  155. v->v_flags = flags;
  156. }
  157. void
  158. slapi_values_set_flags(Slapi_Value **vs, unsigned long flags)
  159. {
  160. PR_ASSERT(vs!=NULL);
  161. Slapi_Value **v;
  162. for (v = vs; v && *v; v++) {
  163. slapi_value_set_flags(*v, flags);
  164. }
  165. }
  166. unsigned long
  167. slapi_value_get_flags(Slapi_Value *v)
  168. {
  169. PR_ASSERT(v!=NULL);
  170. return v->v_flags;
  171. }
  172. void
  173. slapi_value_free(Slapi_Value **v)
  174. {
  175. if(v!=NULL && *v!=NULL)
  176. {
  177. VALUE_DUMP(*v,"value_free");
  178. value_done(*v);
  179. slapi_ch_free((void **)v);
  180. *v= NULL;
  181. PR_INCREMENT_COUNTER(slapi_value_counter_deleted);
  182. PR_DECREMENT_COUNTER(slapi_value_counter_exist);
  183. }
  184. }
  185. void
  186. value_done(Slapi_Value *v)
  187. {
  188. if(v!=NULL)
  189. {
  190. if(NULL != v->v_csnset)
  191. {
  192. csnset_free(&(v->v_csnset));
  193. }
  194. ber_bvdone(&v->bv);
  195. }
  196. }
  197. const CSNSet *
  198. value_get_csnset ( const Slapi_Value *value)
  199. {
  200. if (value)
  201. return value->v_csnset;
  202. else
  203. return NULL;
  204. }
  205. const CSN *
  206. value_get_csn( const Slapi_Value *value, CSNType t)
  207. {
  208. const CSN *csn= NULL;
  209. if(NULL!=value)
  210. {
  211. csn= csnset_get_csn_of_type(value->v_csnset, t);
  212. }
  213. return csn;
  214. }
  215. int
  216. value_contains_csn( const Slapi_Value *value, CSN *csn)
  217. {
  218. int r= 0;
  219. if(NULL!=value)
  220. {
  221. r= csnset_contains(value->v_csnset, csn);
  222. }
  223. return r;
  224. }
  225. Slapi_Value *
  226. value_update_csn( Slapi_Value *value, CSNType t, const CSN *csn)
  227. {
  228. if(value!=NULL)
  229. {
  230. csnset_update_csn(&value->v_csnset,t,csn);
  231. }
  232. return value;
  233. }
  234. Slapi_Value *
  235. value_add_csn( Slapi_Value *value, CSNType t, const CSN *csn)
  236. {
  237. if(value!=NULL)
  238. {
  239. csnset_add_csn(&value->v_csnset,t,csn);
  240. }
  241. return value;
  242. }
  243. const struct berval *
  244. slapi_value_get_berval( const Slapi_Value *value )
  245. {
  246. const struct berval *bval= NULL;
  247. if(NULL != value)
  248. {
  249. bval = &value->bv;
  250. }
  251. return bval;
  252. }
  253. Slapi_Value *
  254. slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
  255. {
  256. struct berval bv;
  257. bv.bv_len= len;
  258. bv.bv_val= val; /* We cast away the const, but we're not going to change anything */
  259. slapi_value_set_berval( value, &bv);
  260. return value;
  261. }
  262. Slapi_Value *
  263. slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
  264. {
  265. slapi_value_set_berval( value, &vfrom->bv );
  266. csnset_free(&value->v_csnset);
  267. value->v_csnset= csnset_dup(vfrom->v_csnset);
  268. return value;
  269. }
  270. Slapi_Value *
  271. value_remove_csn( Slapi_Value *value, CSNType t)
  272. {
  273. if(value!=NULL)
  274. {
  275. csnset_remove_csn(&value->v_csnset,t);
  276. }
  277. return value;
  278. }
  279. Slapi_Value *
  280. slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
  281. {
  282. if(value!=NULL)
  283. {
  284. ber_bvdone(&value->bv);
  285. if(bval!=NULL)
  286. {
  287. ber_bvcpy(&value->bv, bval);
  288. }
  289. }
  290. return value;
  291. }
  292. int
  293. slapi_value_set_string(Slapi_Value *value, const char *strVal)
  294. {
  295. return slapi_value_set_string_passin(value, slapi_ch_strdup(strVal));
  296. }
  297. int
  298. slapi_value_set_string_passin(Slapi_Value *value, char *strVal)
  299. {
  300. int rc= -1;
  301. if(NULL != value)
  302. {
  303. ber_bvdone(&value->bv);
  304. value->bv.bv_val = strVal;
  305. value->bv.bv_len = strlen(strVal);
  306. rc= 0;
  307. }
  308. return rc;
  309. }
  310. int
  311. slapi_value_set_int(Slapi_Value *value, int intVal)
  312. {
  313. int rc= -1;
  314. if(NULL != value)
  315. {
  316. char valueBuf[80];
  317. ber_bvdone(&value->bv);
  318. sprintf(valueBuf,"%d",intVal);
  319. value->bv.bv_val = slapi_ch_strdup(valueBuf);
  320. value->bv.bv_len = strlen(value->bv.bv_val);
  321. rc= 0;
  322. }
  323. return rc;
  324. }
  325. /*
  326. * Warning: The value may not be '\0' terminated!
  327. * Make sure that you know this is a C string.
  328. */
  329. const char *
  330. slapi_value_get_string(const Slapi_Value *value)
  331. {
  332. const char *r= NULL;
  333. if(value!=NULL)
  334. {
  335. r= (const char*)value->bv.bv_val;
  336. }
  337. return r;
  338. }
  339. size_t
  340. slapi_value_get_length(const Slapi_Value *value)
  341. {
  342. size_t r= 0;
  343. if(NULL!=value)
  344. {
  345. r= value->bv.bv_len;
  346. }
  347. return r;
  348. }
  349. int
  350. slapi_value_get_int(const Slapi_Value *value)
  351. {
  352. int r= 0;
  353. if(NULL!=value)
  354. {
  355. char *p;
  356. p = slapi_ch_malloc(value->bv.bv_len + 1);
  357. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  358. p [value->bv.bv_len] = '\0';
  359. r= atoi(p);
  360. slapi_ch_free((void **)&p);
  361. }
  362. return r;
  363. }
  364. unsigned int
  365. slapi_value_get_uint(const Slapi_Value *value)
  366. {
  367. unsigned int r= 0;
  368. if(NULL!=value)
  369. {
  370. char *p;
  371. p = slapi_ch_malloc(value->bv.bv_len + 1);
  372. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  373. p [value->bv.bv_len] = '\0';
  374. r= (unsigned int)atoi(p);
  375. slapi_ch_free((void **)&p);
  376. }
  377. return r;
  378. }
  379. long
  380. slapi_value_get_long(const Slapi_Value *value)
  381. {
  382. long r= 0;
  383. if(NULL!=value)
  384. {
  385. char *p;
  386. p = slapi_ch_malloc(value->bv.bv_len + 1);
  387. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  388. p [value->bv.bv_len] = '\0';
  389. r = atol(p);
  390. slapi_ch_free((void **)&p);
  391. }
  392. return r;
  393. }
  394. unsigned long
  395. slapi_value_get_ulong(const Slapi_Value *value)
  396. {
  397. unsigned long r= 0;
  398. if(NULL!=value)
  399. {
  400. char *p;
  401. p = slapi_ch_malloc(value->bv.bv_len + 1);
  402. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  403. p [value->bv.bv_len] = '\0';
  404. r = (unsigned long)atol(p);
  405. slapi_ch_free((void **)&p);
  406. }
  407. return r;
  408. }
  409. long long
  410. slapi_value_get_longlong(const Slapi_Value *value)
  411. {
  412. long long r= 0;
  413. if(NULL!=value)
  414. {
  415. char *p;
  416. p = slapi_ch_malloc(value->bv.bv_len + 1);
  417. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  418. p [value->bv.bv_len] = '\0';
  419. r = strtoll(p, (char **)NULL, 0);
  420. slapi_ch_free((void **)&p);
  421. }
  422. return r;
  423. }
  424. unsigned long long
  425. slapi_value_get_ulonglong(const Slapi_Value *value)
  426. {
  427. unsigned long long r= 0;
  428. if(NULL!=value)
  429. {
  430. char *p;
  431. p = slapi_ch_malloc(value->bv.bv_len + 1);
  432. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  433. p [value->bv.bv_len] = '\0';
  434. r = strtoull(p, (char **)NULL, 0);
  435. slapi_ch_free((void **)&p);
  436. }
  437. return r;
  438. }
  439. long
  440. slapi_value_get_timelong(const Slapi_Value *value)
  441. {
  442. long r= 0;
  443. if(NULL!=value)
  444. {
  445. char *p;
  446. p = slapi_ch_malloc(value->bv.bv_len + 1);
  447. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  448. p [value->bv.bv_len] = '\0';
  449. r = parse_duration(p);
  450. slapi_ch_free((void **)&p);
  451. }
  452. return r;
  453. }
  454. int
  455. slapi_value_compare(const Slapi_Attr *a,const Slapi_Value *v1,const Slapi_Value *v2)
  456. {
  457. int r= 0;
  458. if(v1!=NULL && v2!=NULL)
  459. {
  460. r= slapi_attr_value_cmp_ext(a, (Slapi_Value *)v1, (Slapi_Value *)v2);
  461. }
  462. else if(v1!=NULL && v2==NULL)
  463. {
  464. r= 1; /* v1>v2 */
  465. }
  466. else if (v1==NULL && v2!=NULL)
  467. {
  468. r= -1; /* v1<v2 */
  469. }
  470. else /* (v1==NULL && v2==NULL) */
  471. {
  472. r= 0; /* The same */
  473. }
  474. return r;
  475. }
  476. size_t
  477. value_size(const Slapi_Value *v)
  478. {
  479. size_t s= v->bv.bv_len;
  480. s += csnset_size(v->v_csnset);
  481. s += sizeof(Slapi_Value);
  482. return s;
  483. }
  484. #ifdef VALUE_DEBUG
  485. static void
  486. value_dump( const Slapi_Value *value, const char *text)
  487. {
  488. LDAPDebug( LDAP_DEBUG_ANY, "Slapi_Value %s ptr=%lx\n", text, value, 0);
  489. /* JCM - Dump value contents... */
  490. }
  491. #endif
  492. int
  493. value_dn_normalize_value(Slapi_Value *value)
  494. {
  495. Slapi_DN *sdn = NULL;
  496. int rc = 0;
  497. if (NULL == value) {
  498. return rc;
  499. }
  500. sdn = slapi_sdn_new_dn_passin(value->bv.bv_val);
  501. if (slapi_sdn_get_dn(sdn)) {
  502. value->bv.bv_val = slapi_ch_strdup(slapi_sdn_get_dn(sdn));
  503. value->bv.bv_len = slapi_sdn_get_ndn_len(sdn);
  504. slapi_sdn_free(&sdn);
  505. slapi_value_set_flags(value, SLAPI_ATTR_FLAG_NORMALIZED_CES);
  506. } else {
  507. rc = 1;
  508. slapi_ch_free((void **)&sdn); /* free just Slapi_DN */
  509. }
  510. return rc;
  511. }