1 /* entry.c - routines for dealing with entries */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
14 #include <ac/socket.h>
15 #include <ac/string.h>
19 static unsigned char *ebuf; /* buf returned by entry2str */
20 static unsigned char *ecur; /* pointer to end of currently used ebuf */
21 static int emaxsize;/* max size of ebuf */
23 int entry_destroy(void)
41 struct berval *vals[2];
42 #ifdef SLAPD_SCHEMA_NOT_COMPAT
43 AttributeDescription *ad;
48 char ptype[64]; /* fixed size buffer */
53 * LDIF is used as the string format.
54 * An entry looks like this:
57 * [<attr>:[:] <value>\n]
58 * [<tab><continuedvalue>\n]*
61 * If a double colon is used after a type, it means the
62 * following value is encoded as a base 64 string. This
63 * happens if the value contains a non-printing character
67 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
68 s ? s : "NULL", 0, 0 );
70 /* initialize reader/writer lock */
71 e = (Entry *) ch_malloc( sizeof(Entry) );
74 Debug( LDAP_DEBUG_TRACE,
75 "<= str2entry NULL (entry allocation failed)\n",
80 /* initialize entry */
90 #ifndef SLAPD_SCHEMA_NOT_COMPAT
95 while ( (s = ldif_getline( &next )) != NULL ) {
96 if ( *s == '\n' || *s == '\0' ) {
100 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
101 Debug( LDAP_DEBUG_TRACE,
102 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
106 #ifndef SLAPD_SCHEMA_NOT_COMPAT
107 if ( strcasecmp( type, ptype ) != 0 ) {
108 strncpy( ptype, type, sizeof(ptype) - 1 );
115 if ( strcasecmp( type, "dn" ) == 0 ) {
118 if ( e->e_dn != NULL ) {
119 Debug( LDAP_DEBUG_ANY,
120 "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
122 value.bv_val != NULL ? value.bv_val : "" );
123 if( value.bv_val != NULL ) free( value.bv_val );
127 e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
131 #ifdef SLAPD_SCHEMA_NOT_COMPAT
133 rc = slap_str2ad( type, &ad, &text );
135 if( rc != LDAP_SUCCESS ) {
136 Debug( LDAP_DEBUG_TRACE,
137 "<= str2entry NULL (str2ad=%s)\n", text, 0, 0 );
139 free( value.bv_val );
144 rc = attr_merge( e, ad, vals );
148 rc = attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a );
152 Debug( LDAP_DEBUG_TRACE,
153 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
155 free( value.bv_val );
161 free( value.bv_val );
162 #ifndef SLAPD_SCHEMA_NOT_COMPAT
167 /* check to make sure there was a dn: line */
168 if ( e->e_dn == NULL ) {
169 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
175 /* generate normalized dn */
176 e->e_ndn = ch_strdup( e->e_dn );
177 (void) dn_normalize( e->e_ndn );
179 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
180 e->e_dn, e->e_id, (unsigned long)e );
186 #define GRABSIZE BUFSIZ
188 #define MAKE_SPACE( n ) { \
189 while ( ecur + (n) > ebuf + emaxsize ) { \
191 offset = (int) (ecur - ebuf); \
192 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
193 emaxsize + GRABSIZE ); \
194 emaxsize += GRABSIZE; \
195 ecur = ebuf + offset; \
209 * In string format, an entry looks like this:
211 * [<attr>: <value>\n]*
217 if ( e->e_dn != NULL ) {
219 tmplen = strlen( e->e_dn );
220 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
221 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
224 /* put the attributes */
225 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
226 /* put "<type>:[:] <value>" line for each value */
227 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
229 #ifdef SLAPD_SCHEMA_NOT_COMPAT
230 tmplen = a->a_desc->ad_cname->bv_len;
232 tmplen = strlen( a->a_type );
234 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
235 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
236 #ifdef SLAPD_SCHEMA_NOT_COMPAT
237 a->a_desc->ad_cname->bv_val,
241 bv->bv_val, bv->bv_len );
248 return( (char *) ebuf );
252 entry_free( Entry *e )
256 if ( e->e_dn != NULL ) {
260 if ( e->e_ndn != NULL ) {
264 for ( a = e->e_attrs; a != NULL; a = next ) {
274 * These routines are used only by Backend.
276 * the Entry has three entry points (ways to find things):
278 * by entry e.g., if you already have an entry from the cache
279 * and want to delete it. (really by entry ptr)
280 * by dn e.g., when looking for the base object of a search
281 * by id e.g., for search candidates
283 * these correspond to three different avl trees that are maintained.
287 entry_cmp( Entry *e1, Entry *e2 )
289 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
293 entry_dn_cmp( Entry *e1, Entry *e2 )
295 /* compare their normalized UPPERCASED dn's */
296 return( strcmp( e1->e_ndn, e2->e_ndn ) );
300 entry_id_cmp( Entry *e1, Entry *e2 )
302 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );