-#ifdef LDAP_SLAPI
-/*
- * Synthesise an LDAPMod array from a Modifications list to pass
- * to SLAPI. This synthesis is destructive and as such the
- * Modifications list may not be used after calling this
- * function.
- *
- * This function must also be called before slap_mods_check().
- */
-static LDAPMod **Modifications2LDAPMods(Modifications **pmodlist)
-{
- Modifications *ml, *modlist;
- LDAPMod **mods, *modp;
- int i, j;
-
- modlist = *pmodlist;
-
- for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
- ;
-
- mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
-
- for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
- modp = mods[i];
- modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
-
- /* Take ownership of original type. */
- modp->mod_type = ml->sml_type.bv_val;
- ml->sml_type.bv_val = NULL;
-
- if ( ml->sml_bvalues != NULL ) {
- for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
- ;
- modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
- sizeof(struct berval *) );
- for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
- /* Take ownership of original values. */
- modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
- modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
- modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
- ml->sml_bvalues[j].bv_len = 0;
- ml->sml_bvalues[j].bv_val = NULL;
- }
- modp->mod_bvalues[j] = NULL;
- } else {
- modp->mod_bvalues = NULL;
- }
- i++;
- }
-
- mods[i] = NULL;
-
- slap_mods_free( modlist );
- *pmodlist = NULL;
-
- return mods;
-}
-
-/*
- * Convert a potentially modified array of LDAPMods back to a
- * Modification list.
- *
- * The returned Modification list contains pointers into the
- * LDAPMods array; the latter MUST be freed with FreeLDAPMods()
- * (see below).
- */
-static Modifications *LDAPMods2Modifications (LDAPMod **mods)
-{
- Modifications *modlist, **modtail;
- LDAPMod **modp;
-
- modtail = &modlist;
-
- for( modp = mods; *modp != NULL; modp++ ) {
- Modifications *mod;
- int i;
- char **p;
- struct berval **bvp;
-
- mod = (Modifications *) ch_malloc( sizeof(Modifications) );
- mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
- mod->sml_type.bv_val = (*modp)->mod_type;
- mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
- mod->sml_desc = NULL;
- mod->sml_next = NULL;
-
- if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
- for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ )
- ;
- } else {
- for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ )
- ;
- }
-
- mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
-
- /* NB: This implicitly trusts a plugin to return valid modifications. */
- if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
- for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ ) {
- mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
- mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
- }
- } else {
- for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ ) {
- mod->sml_bvalues[i].bv_val = *p;
- mod->sml_bvalues[i].bv_len = strlen( *p );
- }
- }
- mod->sml_bvalues[i].bv_val = NULL;
-
- *modtail = mod;
- modtail = &mod->sml_next;
- }
-
- return modlist;
-}
-
-/*
- * This function only frees the parts of the mods array that
- * are not shared with the Modification list that was created
- * by LDAPMods2Modifications.
- *
- */
-static void FreeLDAPMods (LDAPMod **mods)
-{
- int i, j;
-
- if (mods == NULL)
- return;
-
- for ( i = 0; mods[i] != NULL; i++ ) {
- /*
- * Don't free values themselves; they're owned by the
- * Modification list. Do free the containing array.
- */
- if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
- for ( j = 0; mods[i]->mod_bvalues[j] != NULL; j++ ) {
- ch_free( mods[i]->mod_bvalues[j] );
- }
- ch_free( mods[i]->mod_bvalues );
- } else {
- ch_free( mods[i]->mod_values );
- }
- /* Don't free type, for same reasons. */
- ch_free( mods[i] );
- }
- ch_free( mods );
-}
-
-#endif /* LDAP_SLAPI */
-