1 /* entry.c - routines for dealing with entries */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
18 static unsigned char *ebuf; /* buf returned by entry2str */
19 static unsigned char *ecur; /* pointer to end of currently used ebuf */
20 static int emaxsize;/* max size of ebuf */
34 struct berval *vals[2];
38 * In string format, an entry looks like this:
42 * [<attr>:[:] <value>\n]
43 * [<tab><continuedvalue>\n]*
46 * If a double colon is used after a type, it means the
47 * following value is encoded as a base 64 string. This
48 * happens if the value contains a non-printing character
52 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
53 s ? s : "NULL", 0, 0 );
55 /* check to see if there's an id included */
57 if ( isdigit( (unsigned char) *s ) ) {
59 if ( (s = ldif_getline( &next )) == NULL ) {
60 Debug( LDAP_DEBUG_TRACE,
61 "<= str2entry NULL (missing newline after id)\n",
67 /* initialize reader/writer lock */
68 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
71 Debug( LDAP_DEBUG_TRACE,
72 "<= str2entry NULL (entry allocation failed)\n",
85 while ( (s = ldif_getline( &next )) != NULL ) {
86 if ( *s == '\n' || *s == '\0' ) {
90 if ( ldif_parse_line( s, &type, &value, &vlen ) != 0 ) {
91 Debug( LDAP_DEBUG_TRACE,
92 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
96 if ( strcasecmp( type, ptype ) != 0 ) {
97 strncpy( ptype, type, sizeof(ptype) - 1 );
103 if ( strcasecmp( type, "dn" ) == 0 ) {
104 if ( e->e_dn != NULL ) {
105 Debug( LDAP_DEBUG_ANY,
106 "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
108 value != NULL ? value : NULL );
111 e->e_dn = ch_strdup( value != NULL ? value : "" );
113 if ( e->e_ndn != NULL ) {
114 Debug( LDAP_DEBUG_ANY,
115 "str2entry: entry %ld already has a normalized dn \"%s\" for \"%s\" (first ignored)\n",
117 value != NULL ? value : NULL );
120 e->e_ndn = ch_strdup( e->e_dn );
121 (void) dn_normalize_case( e->e_ndn );
127 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
129 Debug( LDAP_DEBUG_TRACE,
130 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
137 /* check to make sure there was a dn: line */
138 if ( e->e_dn == NULL ) {
139 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
145 if ( e->e_ndn == NULL ) {
146 Debug( LDAP_DEBUG_ANY,
147 "str2entry: entry %ld (\"%s\") has no normalized dn\n",
148 e->e_id, e->e_dn, 0 );
153 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
154 e->e_dn, e->e_id, (unsigned long)e );
159 #define GRABSIZE BUFSIZ
161 #define MAKE_SPACE( n ) { \
162 while ( ecur + (n) > ebuf + emaxsize ) { \
164 offset = (int) (ecur - ebuf); \
165 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
166 emaxsize + GRABSIZE ); \
167 emaxsize += GRABSIZE; \
168 ecur = ebuf + offset; \
184 * In string format, an entry looks like this:
187 * [<attr>: <value>\n]*
195 sprintf( (char *) ecur, "%ld\n", e->e_id );
196 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
200 if ( e->e_dn != NULL ) {
202 tmplen = strlen( e->e_dn );
203 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
204 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
207 /* put the attributes */
208 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
209 /* put "<type>:[:] <value>" line for each value */
210 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
212 tmplen = strlen( a->a_type );
213 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
214 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, a->a_type,
215 bv->bv_val, bv->bv_len );
222 return( (char *) ebuf );
226 entry_free( Entry *e )
230 if ( e->e_dn != NULL ) {
234 if ( e->e_ndn != NULL ) {
238 for ( a = e->e_attrs; a != NULL; a = next ) {
248 * These routines are used only by Backend.
250 * the Entry has three entry points (ways to find things):
252 * by entry e.g., if you already have an entry from the cache
253 * and want to delete it. (really by entry ptr)
254 * by dn e.g., when looking for the base object of a search
255 * by id e.g., for search candidates
257 * these correspond to three different avl trees that are maintained.
261 entry_cmp( Entry *e1, Entry *e2 )
263 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
267 entry_dn_cmp( Entry *e1, Entry *e2 )
269 /* compare their normalized UPPERCASED dn's */
270 return( strcmp( e1->e_ndn, e2->e_ndn ) );
274 entry_id_cmp( Entry *e1, Entry *e2 )
276 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );