value.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. /* value.c - routines for dealing with values */
  39. #undef DEBUG /* disable counters */
  40. #include <prcountr.h>
  41. #include "slap.h"
  42. #include "slapi-private.h"
  43. /*
  44. * Functions needed when a berval is embedded in a struct or
  45. * allocated on the stack rather than the heap.
  46. */
  47. static void ber_bvdone(struct berval *bvp)
  48. {
  49. if (bvp == NULL) return;
  50. slapi_ch_free_string(&bvp->bv_val);
  51. bvp->bv_len = 0;
  52. return;
  53. }
  54. static void ber_bvcpy(struct berval *bvd, const struct berval *bvs)
  55. {
  56. size_t len;
  57. if (bvd == NULL || bvs == NULL) return;
  58. len = bvs->bv_len;
  59. bvd->bv_val = slapi_ch_malloc(len+1);
  60. bvd->bv_len = len;
  61. memcpy(bvd->bv_val, bvs->bv_val, len);
  62. bvd->bv_val[len] = '\0';
  63. return;
  64. }
  65. /* <=========================== Slapi_Value ==========================> */
  66. #ifdef VALUE_DEBUG
  67. static void value_dump( const Slapi_Value *value, const char *text);
  68. #define VALUE_DUMP(value,name) value_dump(value,name)
  69. #else
  70. #define VALUE_DUMP(value,name) ((void)0)
  71. #endif
  72. static int counters_created= 0;
  73. PR_DEFINE_COUNTER(slapi_value_counter_created);
  74. PR_DEFINE_COUNTER(slapi_value_counter_deleted);
  75. PR_DEFINE_COUNTER(slapi_value_counter_exist);
  76. Slapi_Value *
  77. slapi_value_new()
  78. {
  79. return value_new(NULL,CSN_TYPE_NONE,NULL);
  80. }
  81. Slapi_Value *
  82. slapi_value_new_berval(const struct berval *bval)
  83. {
  84. return value_new(bval,CSN_TYPE_NONE,NULL);
  85. }
  86. Slapi_Value *
  87. slapi_value_new_value(const Slapi_Value *v)
  88. {
  89. return slapi_value_dup(v);
  90. }
  91. Slapi_Value *
  92. slapi_value_new_string(const char *s)
  93. {
  94. Slapi_Value *v= value_new(NULL,CSN_TYPE_UNKNOWN,NULL);
  95. slapi_value_set_string(v, s);
  96. return v;
  97. }
  98. Slapi_Value *
  99. slapi_value_new_string_passin(char *s)
  100. {
  101. Slapi_Value *v= value_new(NULL,CSN_TYPE_UNKNOWN,NULL);
  102. slapi_value_set_string_passin(v, s);
  103. return v;
  104. }
  105. Slapi_Value *
  106. slapi_value_init(Slapi_Value *v)
  107. {
  108. return value_init(v,NULL,CSN_TYPE_NONE,NULL);
  109. }
  110. Slapi_Value *
  111. slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
  112. {
  113. return value_init(v,bval,CSN_TYPE_NONE,NULL);
  114. }
  115. Slapi_Value *
  116. slapi_value_init_string(Slapi_Value *v,const char *s)
  117. {
  118. value_init(v,NULL,CSN_TYPE_UNKNOWN,NULL);
  119. slapi_value_set_string(v,s);
  120. return v;
  121. }
  122. Slapi_Value *
  123. slapi_value_init_string_passin(Slapi_Value *v, char *s)
  124. {
  125. value_init(v,NULL,CSN_TYPE_UNKNOWN,NULL);
  126. slapi_value_set_string_passin(v,s);
  127. return v;
  128. }
  129. Slapi_Value *
  130. slapi_value_dup(const Slapi_Value *v)
  131. {
  132. Slapi_Value *newvalue= value_new(&v->bv,CSN_TYPE_UNKNOWN,NULL);
  133. newvalue->v_csnset= csnset_dup(v->v_csnset);
  134. return newvalue;
  135. }
  136. Slapi_Value *
  137. value_new(const struct berval *bval,CSNType t,const CSN *csn)
  138. {
  139. Slapi_Value *v;
  140. v = (Slapi_Value *)slapi_ch_malloc(sizeof(Slapi_Value));
  141. value_init(v, bval, t, csn);
  142. if(!counters_created)
  143. {
  144. PR_CREATE_COUNTER(slapi_value_counter_created,"Slapi_Value","created","");
  145. PR_CREATE_COUNTER(slapi_value_counter_deleted,"Slapi_Value","deleted","");
  146. PR_CREATE_COUNTER(slapi_value_counter_exist,"Slapi_Value","exist","");
  147. counters_created= 1;
  148. }
  149. PR_INCREMENT_COUNTER(slapi_value_counter_created);
  150. PR_INCREMENT_COUNTER(slapi_value_counter_exist);
  151. VALUE_DUMP(v,"value_new");
  152. return v;
  153. }
  154. Slapi_Value *
  155. value_init(Slapi_Value *v, const struct berval *bval,CSNType t,const CSN *csn)
  156. {
  157. PR_ASSERT(v!=NULL);
  158. memset(v,0,sizeof(Slapi_Value));
  159. if(csn!=NULL)
  160. {
  161. value_update_csn(v,t,csn);
  162. }
  163. slapi_value_set_berval(v,bval);
  164. return v;
  165. }
  166. void
  167. slapi_value_free(Slapi_Value **v)
  168. {
  169. if(v!=NULL && *v!=NULL)
  170. {
  171. VALUE_DUMP(*v,"value_free");
  172. value_done(*v);
  173. slapi_ch_free((void **)v);
  174. *v= NULL;
  175. PR_INCREMENT_COUNTER(slapi_value_counter_deleted);
  176. PR_DECREMENT_COUNTER(slapi_value_counter_exist);
  177. }
  178. }
  179. void
  180. value_done(Slapi_Value *v)
  181. {
  182. if(v!=NULL)
  183. {
  184. if(NULL != v->v_csnset)
  185. {
  186. csnset_free(&(v->v_csnset));
  187. }
  188. ber_bvdone(&v->bv);
  189. }
  190. }
  191. const CSNSet *
  192. value_get_csnset ( const Slapi_Value *value)
  193. {
  194. if (value)
  195. return value->v_csnset;
  196. else
  197. return NULL;
  198. }
  199. const CSN *
  200. value_get_csn( const Slapi_Value *value, CSNType t)
  201. {
  202. const CSN *csn= NULL;
  203. if(NULL!=value)
  204. {
  205. csn= csnset_get_csn_of_type(value->v_csnset, t);
  206. }
  207. return csn;
  208. }
  209. int
  210. value_contains_csn( const Slapi_Value *value, CSN *csn)
  211. {
  212. int r= 0;
  213. if(NULL!=value)
  214. {
  215. r= csnset_contains(value->v_csnset, csn);
  216. }
  217. return r;
  218. }
  219. Slapi_Value *
  220. value_update_csn( Slapi_Value *value, CSNType t, const CSN *csn)
  221. {
  222. if(value!=NULL)
  223. {
  224. csnset_update_csn(&value->v_csnset,t,csn);
  225. }
  226. return value;
  227. }
  228. Slapi_Value *
  229. value_add_csn( Slapi_Value *value, CSNType t, const CSN *csn)
  230. {
  231. if(value!=NULL)
  232. {
  233. csnset_add_csn(&value->v_csnset,t,csn);
  234. }
  235. return value;
  236. }
  237. const struct berval *
  238. slapi_value_get_berval( const Slapi_Value *value )
  239. {
  240. const struct berval *bval= NULL;
  241. if(NULL != value)
  242. {
  243. bval = &value->bv;
  244. }
  245. return bval;
  246. }
  247. Slapi_Value *
  248. slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
  249. {
  250. struct berval bv;
  251. bv.bv_len= len;
  252. bv.bv_val= (void*)val; /* We cast away the const, but we're not going to change anything */
  253. slapi_value_set_berval( value, &bv);
  254. return value;
  255. }
  256. Slapi_Value *
  257. slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
  258. {
  259. slapi_value_set_berval( value, &vfrom->bv );
  260. csnset_free(&value->v_csnset);
  261. value->v_csnset= csnset_dup(vfrom->v_csnset);
  262. return value;
  263. }
  264. Slapi_Value *
  265. value_remove_csn( Slapi_Value *value, CSNType t)
  266. {
  267. if(value!=NULL)
  268. {
  269. csnset_remove_csn(&value->v_csnset,t);
  270. }
  271. return value;
  272. }
  273. Slapi_Value *
  274. slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
  275. {
  276. if(value!=NULL)
  277. {
  278. ber_bvdone(&value->bv);
  279. if(bval!=NULL)
  280. {
  281. ber_bvcpy(&value->bv, bval);
  282. }
  283. }
  284. return value;
  285. }
  286. int
  287. slapi_value_set_string(Slapi_Value *value, const char *strVal)
  288. {
  289. return slapi_value_set_string_passin(value, slapi_ch_strdup(strVal));
  290. }
  291. int
  292. slapi_value_set_string_passin(Slapi_Value *value, char *strVal)
  293. {
  294. int rc= -1;
  295. if(NULL != value)
  296. {
  297. ber_bvdone(&value->bv);
  298. value->bv.bv_val = strVal;
  299. value->bv.bv_len = strlen(strVal);
  300. rc= 0;
  301. }
  302. return rc;
  303. }
  304. int
  305. slapi_value_set_int(Slapi_Value *value, int intVal)
  306. {
  307. int rc= -1;
  308. if(NULL != value)
  309. {
  310. char valueBuf[80];
  311. ber_bvdone(&value->bv);
  312. sprintf(valueBuf,"%d",intVal);
  313. value->bv.bv_val = slapi_ch_strdup(valueBuf);
  314. value->bv.bv_len = strlen(value->bv.bv_val);
  315. rc= 0;
  316. }
  317. return rc;
  318. }
  319. /*
  320. * Warning: The value may not be '\0' terminated!
  321. * Make sure that you know this is a C string.
  322. */
  323. const char *
  324. slapi_value_get_string(const Slapi_Value *value)
  325. {
  326. const char *r= NULL;
  327. if(value!=NULL)
  328. {
  329. r= (const char*)value->bv.bv_val;
  330. }
  331. return r;
  332. }
  333. size_t
  334. slapi_value_get_length(const Slapi_Value *value)
  335. {
  336. size_t r= 0;
  337. if(NULL!=value)
  338. {
  339. r= value->bv.bv_len;
  340. }
  341. return r;
  342. }
  343. int
  344. slapi_value_get_int(const Slapi_Value *value)
  345. {
  346. int r= 0;
  347. if(NULL!=value)
  348. {
  349. char *p;
  350. p = slapi_ch_malloc(value->bv.bv_len + 1);
  351. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  352. p [value->bv.bv_len] = '\0';
  353. r= atoi(p);
  354. slapi_ch_free((void **)&p);
  355. }
  356. return r;
  357. }
  358. unsigned int
  359. slapi_value_get_uint(const Slapi_Value *value)
  360. {
  361. unsigned int r= 0;
  362. if(NULL!=value)
  363. {
  364. char *p;
  365. p = slapi_ch_malloc(value->bv.bv_len + 1);
  366. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  367. p [value->bv.bv_len] = '\0';
  368. r= (unsigned int)atoi(p);
  369. slapi_ch_free((void **)&p);
  370. }
  371. return r;
  372. }
  373. long
  374. slapi_value_get_long(const Slapi_Value *value)
  375. {
  376. long r= 0;
  377. if(NULL!=value)
  378. {
  379. char *p;
  380. p = slapi_ch_malloc(value->bv.bv_len + 1);
  381. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  382. p [value->bv.bv_len] = '\0';
  383. r = atol(p);
  384. slapi_ch_free((void **)&p);
  385. }
  386. return r;
  387. }
  388. unsigned long
  389. slapi_value_get_ulong(const Slapi_Value *value)
  390. {
  391. unsigned long r= 0;
  392. if(NULL!=value)
  393. {
  394. char *p;
  395. p = slapi_ch_malloc(value->bv.bv_len + 1);
  396. memcpy (p, value->bv.bv_val, value->bv.bv_len);
  397. p [value->bv.bv_len] = '\0';
  398. r = (unsigned long)atol(p);
  399. slapi_ch_free((void **)&p);
  400. }
  401. return r;
  402. }
  403. int
  404. slapi_value_compare(const Slapi_Attr *a,const Slapi_Value *v1,const Slapi_Value *v2)
  405. {
  406. int r= 0;
  407. if(v1!=NULL && v2!=NULL)
  408. {
  409. r= slapi_attr_value_cmp( a, &v1->bv, &v2->bv);
  410. }
  411. else if(v1!=NULL && v2==NULL)
  412. {
  413. r= 1; /* v1>v2 */
  414. }
  415. else if (v1==NULL && v2!=NULL)
  416. {
  417. r= -1; /* v1<v2 */
  418. }
  419. else /* (v1==NULL && v2==NULL) */
  420. {
  421. r= 0; /* The same */
  422. }
  423. return r;
  424. }
  425. size_t
  426. value_size(const Slapi_Value *v)
  427. {
  428. size_t s= v->bv.bv_len;
  429. s += csnset_size(v->v_csnset);
  430. s += sizeof(Slapi_Value);
  431. return s;
  432. }
  433. #ifdef VALUE_DEBUG
  434. static void
  435. value_dump( const Slapi_Value *value, const char *text)
  436. {
  437. LDAPDebug( LDAP_DEBUG_ANY, "Slapi_Value %s ptr=%lx\n", text, value, 0);
  438. /* JCM - Dump value contents... */
  439. }
  440. #endif