memory.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2024 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. #include "portable.h"
  16. #include <ac/stdlib.h>
  17. #include <ac/string.h>
  18. #include "lber-int.h"
  19. #ifdef LDAP_MEMORY_TRACE
  20. #include <stdio.h>
  21. #endif
  22. #ifdef LDAP_MEMORY_DEBUG
  23. /*
  24. * LDAP_MEMORY_DEBUG should only be enabled for the purposes of
  25. * debugging memory management within OpenLDAP libraries and slapd.
  26. *
  27. * It should only be enabled by an experienced developer as it causes
  28. * the inclusion of numerous assert()'s, many of which may be triggered
  29. * by a perfectly valid program. If LDAP_MEMORY_DEBUG & 2 is true,
  30. * that includes asserts known to break both slapd and current clients.
  31. *
  32. * The code behind this macro is subject to change as needed to
  33. * support this testing.
  34. */
  35. struct ber_mem_hdr {
  36. ber_int_t bm_top; /* Pattern to detect buf overrun from prev buffer */
  37. ber_int_t bm_length; /* Length of user allocated area */
  38. #ifdef LDAP_MEMORY_TRACE
  39. ber_int_t bm_sequence; /* Allocation sequence number */
  40. #endif
  41. union bmu_align_u { /* Force alignment, pattern to detect back clobber */
  42. ber_len_t bmu_len_t;
  43. ber_tag_t bmu_tag_t;
  44. ber_int_t bmu_int_t;
  45. size_t bmu_size_t;
  46. void * bmu_voidp;
  47. double bmu_double;
  48. long bmu_long;
  49. long (*bmu_funcp)( double );
  50. unsigned char bmu_char[4];
  51. } ber_align;
  52. #define bm_junk ber_align.bmu_len_t
  53. #define bm_data ber_align.bmu_char[1]
  54. #define bm_char ber_align.bmu_char
  55. };
  56. /* Pattern at top of allocated space */
  57. #define LBER_MEM_JUNK ((ber_int_t) 0xdeaddada)
  58. static const struct ber_mem_hdr ber_int_mem_hdr = { LBER_MEM_JUNK };
  59. /* Note sequence and ber_int_meminuse are counters, but are not
  60. * thread safe. If you want to use these values for multithreaded applications,
  61. * you must put mutexes around them, otherwise they will have incorrect values.
  62. * When debugging, if you sort the debug output, the sequence number will
  63. * put allocations/frees together. It is then a simple matter to write a script
  64. * to find any allocations that don't have a buffer free function.
  65. */
  66. long ber_int_meminuse = 0;
  67. #ifdef LDAP_MEMORY_TRACE
  68. static ber_int_t sequence = 0;
  69. #endif
  70. /* Pattern placed just before user data */
  71. static unsigned char toppattern[4] = { 0xde, 0xad, 0xba, 0xde };
  72. /* Pattern placed just after user data */
  73. static unsigned char endpattern[4] = { 0xd1, 0xed, 0xde, 0xca };
  74. #define mbu_len sizeof(ber_int_mem_hdr.ber_align)
  75. /* Test if pattern placed just before user data is good */
  76. #define testdatatop(val) ( \
  77. *(val->bm_char+mbu_len-4)==toppattern[0] && \
  78. *(val->bm_char+mbu_len-3)==toppattern[1] && \
  79. *(val->bm_char+mbu_len-2)==toppattern[2] && \
  80. *(val->bm_char+mbu_len-1)==toppattern[3] )
  81. /* Place pattern just before user data */
  82. #define setdatatop(val) *(val->bm_char+mbu_len-4)=toppattern[0]; \
  83. *(val->bm_char+mbu_len-3)=toppattern[1]; \
  84. *(val->bm_char+mbu_len-2)=toppattern[2]; \
  85. *(val->bm_char+mbu_len-1)=toppattern[3];
  86. /* Test if pattern placed just after user data is good */
  87. #define testend(val) ( *((unsigned char *)val+0)==endpattern[0] && \
  88. *((unsigned char *)val+1)==endpattern[1] && \
  89. *((unsigned char *)val+2)==endpattern[2] && \
  90. *((unsigned char *)val+3)==endpattern[3] )
  91. /* Place pattern just after user data */
  92. #define setend(val) *((unsigned char *)val+0)=endpattern[0]; \
  93. *((unsigned char *)val+1)=endpattern[1]; \
  94. *((unsigned char *)val+2)=endpattern[2]; \
  95. *((unsigned char *)val+3)=endpattern[3];
  96. #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
  97. #define BER_MEM_VALID(p) do { \
  98. assert( (p) != BER_MEM_BADADDR ); \
  99. assert( (p) != (void *) &ber_int_mem_hdr ); \
  100. } while(0)
  101. #else
  102. #define BER_MEM_VALID(p) /* no-op */
  103. #endif
  104. BerMemoryFunctions *ber_int_memory_fns = NULL;
  105. void
  106. ber_memfree_x( void *p, void *ctx )
  107. {
  108. if( p == NULL ) {
  109. return;
  110. }
  111. BER_MEM_VALID( p );
  112. if( ber_int_memory_fns == NULL || ctx == NULL ) {
  113. #ifdef LDAP_MEMORY_DEBUG
  114. struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
  115. ((char *)p - sizeof(struct ber_mem_hdr));
  116. assert( mh->bm_top == LBER_MEM_JUNK);
  117. assert( testdatatop( mh));
  118. assert( testend( (char *)&mh[1] + mh->bm_length) );
  119. ber_int_meminuse -= mh->bm_length;
  120. #ifdef LDAP_MEMORY_TRACE
  121. fprintf(stderr, "0x%08lx 0x%08lx -f- %ld ber_memfree %ld\n",
  122. (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
  123. ber_int_meminuse);
  124. #endif
  125. /* Fill the free space with poison */
  126. memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t));
  127. free( mh );
  128. #else
  129. free( p );
  130. #endif
  131. return;
  132. }
  133. assert( ber_int_memory_fns->bmf_free != 0 );
  134. (*ber_int_memory_fns->bmf_free)( p, ctx );
  135. }
  136. void
  137. ber_memfree( void *p )
  138. {
  139. ber_memfree_x(p, NULL);
  140. }
  141. void
  142. ber_memvfree_x( void **vec, void *ctx )
  143. {
  144. int i;
  145. if( vec == NULL ) {
  146. return;
  147. }
  148. BER_MEM_VALID( vec );
  149. for ( i = 0; vec[i] != NULL; i++ ) {
  150. ber_memfree_x( vec[i], ctx );
  151. }
  152. ber_memfree_x( vec, ctx );
  153. }
  154. void
  155. ber_memvfree( void **vec )
  156. {
  157. ber_memvfree_x( vec, NULL );
  158. }
  159. void *
  160. ber_memalloc_x( ber_len_t s, void *ctx )
  161. {
  162. void *new;
  163. if( s == 0 ) {
  164. LDAP_MEMORY_DEBUG_ASSERT( s != 0 );
  165. return NULL;
  166. }
  167. if( ber_int_memory_fns == NULL || ctx == NULL ) {
  168. #ifdef LDAP_MEMORY_DEBUG
  169. new = malloc(s + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t));
  170. if( new )
  171. {
  172. struct ber_mem_hdr *mh = new;
  173. mh->bm_top = LBER_MEM_JUNK;
  174. mh->bm_length = s;
  175. setdatatop( mh);
  176. setend( (char *)&mh[1] + mh->bm_length );
  177. ber_int_meminuse += mh->bm_length; /* Count mem inuse */
  178. #ifdef LDAP_MEMORY_TRACE
  179. mh->bm_sequence = sequence++;
  180. fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memalloc %ld\n",
  181. (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
  182. ber_int_meminuse);
  183. #endif
  184. /* poison new memory */
  185. memset( (char *)&mh[1], 0xff, s);
  186. BER_MEM_VALID( &mh[1] );
  187. new = &mh[1];
  188. }
  189. #else
  190. new = malloc( s );
  191. #endif
  192. } else {
  193. new = (*ber_int_memory_fns->bmf_malloc)( s, ctx );
  194. }
  195. if( new == NULL ) {
  196. ber_errno = LBER_ERROR_MEMORY;
  197. }
  198. return new;
  199. }
  200. void *
  201. ber_memalloc( ber_len_t s )
  202. {
  203. return ber_memalloc_x( s, NULL );
  204. }
  205. void *
  206. ber_memcalloc_x( ber_len_t n, ber_len_t s, void *ctx )
  207. {
  208. void *new;
  209. if( n == 0 || s == 0 ) {
  210. LDAP_MEMORY_DEBUG_ASSERT( n != 0 && s != 0);
  211. return NULL;
  212. }
  213. if( ber_int_memory_fns == NULL || ctx == NULL ) {
  214. #ifdef LDAP_MEMORY_DEBUG
  215. new = n < (-sizeof(struct ber_mem_hdr) - sizeof(ber_int_t)) / s
  216. ? calloc(1, n*s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t))
  217. : NULL;
  218. if( new )
  219. {
  220. struct ber_mem_hdr *mh = new;
  221. mh->bm_top = LBER_MEM_JUNK;
  222. mh->bm_length = n*s;
  223. setdatatop( mh);
  224. setend( (char *)&mh[1] + mh->bm_length );
  225. ber_int_meminuse += mh->bm_length;
  226. #ifdef LDAP_MEMORY_TRACE
  227. mh->bm_sequence = sequence++;
  228. fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memcalloc %ld\n",
  229. (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
  230. ber_int_meminuse);
  231. #endif
  232. BER_MEM_VALID( &mh[1] );
  233. new = &mh[1];
  234. }
  235. #else
  236. new = calloc( n, s );
  237. #endif
  238. } else {
  239. new = (*ber_int_memory_fns->bmf_calloc)( n, s, ctx );
  240. }
  241. if( new == NULL ) {
  242. ber_errno = LBER_ERROR_MEMORY;
  243. }
  244. return new;
  245. }
  246. void *
  247. ber_memcalloc( ber_len_t n, ber_len_t s )
  248. {
  249. return ber_memcalloc_x( n, s, NULL );
  250. }
  251. void *
  252. ber_memrealloc_x( void* p, ber_len_t s, void *ctx )
  253. {
  254. void *new = NULL;
  255. /* realloc(NULL,s) -> malloc(s) */
  256. if( p == NULL ) {
  257. return ber_memalloc_x( s, ctx );
  258. }
  259. /* realloc(p,0) -> free(p) */
  260. if( s == 0 ) {
  261. ber_memfree_x( p, ctx );
  262. return NULL;
  263. }
  264. BER_MEM_VALID( p );
  265. if( ber_int_memory_fns == NULL || ctx == NULL ) {
  266. #ifdef LDAP_MEMORY_DEBUG
  267. ber_int_t oldlen;
  268. struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
  269. ((char *)p - sizeof(struct ber_mem_hdr));
  270. assert( mh->bm_top == LBER_MEM_JUNK);
  271. assert( testdatatop( mh));
  272. assert( testend( (char *)&mh[1] + mh->bm_length) );
  273. oldlen = mh->bm_length;
  274. p = realloc( mh, s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
  275. if( p == NULL ) {
  276. ber_errno = LBER_ERROR_MEMORY;
  277. return NULL;
  278. }
  279. mh = p;
  280. mh->bm_length = s;
  281. setend( (char *)&mh[1] + mh->bm_length );
  282. if( s > oldlen ) {
  283. /* poison any new memory */
  284. memset( (char *)&mh[1] + oldlen, 0xff, s - oldlen);
  285. }
  286. assert( mh->bm_top == LBER_MEM_JUNK);
  287. assert( testdatatop( mh));
  288. ber_int_meminuse += s - oldlen;
  289. #ifdef LDAP_MEMORY_TRACE
  290. fprintf(stderr, "0x%08lx 0x%08lx -a- %ld ber_memrealloc %ld\n",
  291. (long)mh->bm_sequence, (long)mh, (long)mh->bm_length,
  292. ber_int_meminuse);
  293. #endif
  294. BER_MEM_VALID( &mh[1] );
  295. return &mh[1];
  296. #else
  297. new = realloc( p, s );
  298. #endif
  299. } else {
  300. new = (*ber_int_memory_fns->bmf_realloc)( p, s, ctx );
  301. }
  302. if( new == NULL ) {
  303. ber_errno = LBER_ERROR_MEMORY;
  304. }
  305. return new;
  306. }
  307. void *
  308. ber_memrealloc( void* p, ber_len_t s )
  309. {
  310. return ber_memrealloc_x( p, s, NULL );
  311. }
  312. void
  313. ber_bvfree_x( struct berval *bv, void *ctx )
  314. {
  315. if( bv == NULL ) {
  316. return;
  317. }
  318. BER_MEM_VALID( bv );
  319. if ( bv->bv_val != NULL ) {
  320. ber_memfree_x( bv->bv_val, ctx );
  321. }
  322. ber_memfree_x( (char *) bv, ctx );
  323. }
  324. void
  325. ber_bvfree( struct berval *bv )
  326. {
  327. ber_bvfree_x( bv, NULL );
  328. }
  329. void
  330. ber_bvecfree_x( struct berval **bv, void *ctx )
  331. {
  332. int i;
  333. if( bv == NULL ) {
  334. return;
  335. }
  336. BER_MEM_VALID( bv );
  337. /* count elements */
  338. for ( i = 0; bv[i] != NULL; i++ ) ;
  339. /* free in reverse order */
  340. for ( i--; i >= 0; i-- ) {
  341. ber_bvfree_x( bv[i], ctx );
  342. }
  343. ber_memfree_x( (char *) bv, ctx );
  344. }
  345. void
  346. ber_bvecfree( struct berval **bv )
  347. {
  348. ber_bvecfree_x( bv, NULL );
  349. }
  350. int
  351. ber_bvecadd_x( struct berval ***bvec, struct berval *bv, void *ctx )
  352. {
  353. ber_len_t i;
  354. struct berval **new;
  355. if( *bvec == NULL ) {
  356. if( bv == NULL ) {
  357. /* nothing to add */
  358. return 0;
  359. }
  360. *bvec = ber_memalloc_x( 2 * sizeof(struct berval *), ctx );
  361. if( *bvec == NULL ) {
  362. return -1;
  363. }
  364. (*bvec)[0] = bv;
  365. (*bvec)[1] = NULL;
  366. return 1;
  367. }
  368. BER_MEM_VALID( bvec );
  369. /* count entries */
  370. for ( i = 0; (*bvec)[i] != NULL; i++ ) {
  371. /* EMPTY */;
  372. }
  373. if( bv == NULL ) {
  374. return i;
  375. }
  376. new = ber_memrealloc_x( *bvec, (i+2) * sizeof(struct berval *), ctx);
  377. if( new == NULL ) {
  378. return -1;
  379. }
  380. *bvec = new;
  381. (*bvec)[i++] = bv;
  382. (*bvec)[i] = NULL;
  383. return i;
  384. }
  385. int
  386. ber_bvecadd( struct berval ***bvec, struct berval *bv )
  387. {
  388. return ber_bvecadd_x( bvec, bv, NULL );
  389. }
  390. struct berval *
  391. ber_dupbv_x(
  392. struct berval *dst, struct berval *src, void *ctx )
  393. {
  394. struct berval *new, tmp;
  395. if( src == NULL ) {
  396. ber_errno = LBER_ERROR_PARAM;
  397. return NULL;
  398. }
  399. if ( dst ) {
  400. new = &tmp;
  401. } else {
  402. if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
  403. return NULL;
  404. }
  405. }
  406. if ( src->bv_val == NULL ) {
  407. new->bv_val = NULL;
  408. new->bv_len = 0;
  409. } else {
  410. if(( new->bv_val = ber_memalloc_x( src->bv_len + 1, ctx )) == NULL ) {
  411. if ( !dst )
  412. ber_memfree_x( new, ctx );
  413. return NULL;
  414. }
  415. AC_MEMCPY( new->bv_val, src->bv_val, src->bv_len );
  416. new->bv_val[src->bv_len] = '\0';
  417. new->bv_len = src->bv_len;
  418. }
  419. if ( dst ) {
  420. *dst = *new;
  421. new = dst;
  422. }
  423. return new;
  424. }
  425. struct berval *
  426. ber_dupbv(
  427. struct berval *dst, struct berval *src )
  428. {
  429. return ber_dupbv_x( dst, src, NULL );
  430. }
  431. struct berval *
  432. ber_bvdup(
  433. struct berval *src )
  434. {
  435. return ber_dupbv_x( NULL, src, NULL );
  436. }
  437. struct berval *
  438. ber_str2bv_x(
  439. LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
  440. void *ctx)
  441. {
  442. struct berval *new;
  443. if( s == NULL ) {
  444. ber_errno = LBER_ERROR_PARAM;
  445. return NULL;
  446. }
  447. if( bv ) {
  448. new = bv;
  449. } else {
  450. if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
  451. return NULL;
  452. }
  453. }
  454. new->bv_len = len ? len : strlen( s );
  455. if ( dup ) {
  456. if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
  457. if ( !bv )
  458. ber_memfree_x( new, ctx );
  459. return NULL;
  460. }
  461. AC_MEMCPY( new->bv_val, s, new->bv_len );
  462. new->bv_val[new->bv_len] = '\0';
  463. } else {
  464. new->bv_val = (char *) s;
  465. }
  466. return( new );
  467. }
  468. struct berval *
  469. ber_str2bv(
  470. LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
  471. {
  472. return ber_str2bv_x( s, len, dup, bv, NULL );
  473. }
  474. struct berval *
  475. ber_mem2bv_x(
  476. LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
  477. void *ctx)
  478. {
  479. struct berval *new;
  480. if( s == NULL ) {
  481. ber_errno = LBER_ERROR_PARAM;
  482. return NULL;
  483. }
  484. if( bv ) {
  485. new = bv;
  486. } else {
  487. if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
  488. return NULL;
  489. }
  490. }
  491. new->bv_len = len;
  492. if ( dup ) {
  493. if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
  494. if ( !bv ) {
  495. ber_memfree_x( new, ctx );
  496. }
  497. return NULL;
  498. }
  499. AC_MEMCPY( new->bv_val, s, new->bv_len );
  500. new->bv_val[new->bv_len] = '\0';
  501. } else {
  502. new->bv_val = (char *) s;
  503. }
  504. return( new );
  505. }
  506. struct berval *
  507. ber_mem2bv(
  508. LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
  509. {
  510. return ber_mem2bv_x( s, len, dup, bv, NULL );
  511. }
  512. char *
  513. ber_strdup_x( LDAP_CONST char *s, void *ctx )
  514. {
  515. char *p;
  516. size_t len;
  517. #ifdef LDAP_MEMORY_DEBUG
  518. assert(s != NULL); /* bv damn better point to something */
  519. #endif
  520. if( s == NULL ) {
  521. ber_errno = LBER_ERROR_PARAM;
  522. return NULL;
  523. }
  524. len = strlen( s ) + 1;
  525. if ( (p = ber_memalloc_x( len, ctx )) != NULL ) {
  526. AC_MEMCPY( p, s, len );
  527. }
  528. return p;
  529. }
  530. char *
  531. ber_strdup( LDAP_CONST char *s )
  532. {
  533. return ber_strdup_x( s, NULL );
  534. }
  535. ber_len_t
  536. ber_strnlen( LDAP_CONST char *s, ber_len_t len )
  537. {
  538. ber_len_t l;
  539. for ( l = 0; l < len && s[l] != '\0'; l++ ) ;
  540. return l;
  541. }
  542. char *
  543. ber_strndup_x( LDAP_CONST char *s, ber_len_t l, void *ctx )
  544. {
  545. char *p;
  546. size_t len;
  547. #ifdef LDAP_MEMORY_DEBUG
  548. assert(s != NULL); /* bv damn better point to something */
  549. #endif
  550. if( s == NULL ) {
  551. ber_errno = LBER_ERROR_PARAM;
  552. return NULL;
  553. }
  554. len = ber_strnlen( s, l );
  555. if ( (p = ber_memalloc_x( len + 1, ctx )) != NULL ) {
  556. AC_MEMCPY( p, s, len );
  557. p[len] = '\0';
  558. }
  559. return p;
  560. }
  561. char *
  562. ber_strndup( LDAP_CONST char *s, ber_len_t l )
  563. {
  564. return ber_strndup_x( s, l, NULL );
  565. }
  566. /*
  567. * dst is resized as required by src and the value of src is copied into dst
  568. * dst->bv_val must be NULL (and dst->bv_len must be 0), or it must be
  569. * alloc'ed with the context ctx
  570. */
  571. struct berval *
  572. ber_bvreplace_x( struct berval *dst, LDAP_CONST struct berval *src, void *ctx )
  573. {
  574. assert( dst != NULL );
  575. assert( !BER_BVISNULL( src ) );
  576. if ( BER_BVISNULL( dst ) || dst->bv_len < src->bv_len ) {
  577. dst->bv_val = ber_memrealloc_x( dst->bv_val, src->bv_len + 1, ctx );
  578. }
  579. AC_MEMCPY( dst->bv_val, src->bv_val, src->bv_len + 1 );
  580. dst->bv_len = src->bv_len;
  581. return dst;
  582. }
  583. struct berval *
  584. ber_bvreplace( struct berval *dst, LDAP_CONST struct berval *src )
  585. {
  586. return ber_bvreplace_x( dst, src, NULL );
  587. }
  588. void
  589. ber_bvarray_free_x( BerVarray a, void *ctx )
  590. {
  591. int i;
  592. if (a) {
  593. BER_MEM_VALID( a );
  594. /* count elements */
  595. for (i=0; a[i].bv_val; i++) ;
  596. /* free in reverse order */
  597. for (i--; i>=0; i--) {
  598. ber_memfree_x(a[i].bv_val, ctx);
  599. }
  600. ber_memfree_x(a, ctx);
  601. }
  602. }
  603. void
  604. ber_bvarray_free( BerVarray a )
  605. {
  606. ber_bvarray_free_x(a, NULL);
  607. }
  608. int
  609. ber_bvarray_dup_x( BerVarray *dst, BerVarray src, void *ctx )
  610. {
  611. int i, j;
  612. BerVarray new;
  613. if ( !src ) {
  614. *dst = NULL;
  615. return 0;
  616. }
  617. for (i=0; !BER_BVISNULL( &src[i] ); i++) ;
  618. new = ber_memalloc_x(( i+1 ) * sizeof(BerValue), ctx );
  619. if ( !new )
  620. return -1;
  621. for (j=0; j<i; j++) {
  622. ber_dupbv_x( &new[j], &src[j], ctx );
  623. if ( BER_BVISNULL( &new[j] )) {
  624. ber_bvarray_free_x( new, ctx );
  625. return -1;
  626. }
  627. }
  628. BER_BVZERO( &new[j] );
  629. *dst = new;
  630. return 0;
  631. }
  632. int
  633. ber_bvarray_add_x( BerVarray *a, BerValue *bv, void *ctx )
  634. {
  635. int n;
  636. if ( *a == NULL ) {
  637. if (bv == NULL) {
  638. return 0;
  639. }
  640. n = 0;
  641. *a = (BerValue *) ber_memalloc_x( 2 * sizeof(BerValue), ctx );
  642. if ( *a == NULL ) {
  643. return -1;
  644. }
  645. } else {
  646. BerVarray atmp;
  647. BER_MEM_VALID( a );
  648. for ( n = 0; *a != NULL && (*a)[n].bv_val != NULL; n++ ) {
  649. ; /* just count them */
  650. }
  651. if (bv == NULL) {
  652. return n;
  653. }
  654. atmp = (BerValue *) ber_memrealloc_x( (char *) *a,
  655. (n + 2) * sizeof(BerValue), ctx );
  656. if( atmp == NULL ) {
  657. return -1;
  658. }
  659. *a = atmp;
  660. }
  661. (*a)[n++] = *bv;
  662. (*a)[n].bv_val = NULL;
  663. (*a)[n].bv_len = 0;
  664. return n;
  665. }
  666. int
  667. ber_bvarray_add( BerVarray *a, BerValue *bv )
  668. {
  669. return ber_bvarray_add_x( a, bv, NULL );
  670. }