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 AttributeDescription *ad;
47 * LDIF is used as the string format.
48 * An entry looks like this:
51 * [<attr>:[:] <value>\n]
52 * [<tab><continuedvalue>\n]*
55 * If a double colon is used after a type, it means the
56 * following value is encoded as a base 64 string. This
57 * happens if the value contains a non-printing character
61 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
62 s ? s : "NULL", 0, 0 );
64 /* initialize reader/writer lock */
65 e = (Entry *) ch_malloc( sizeof(Entry) );
68 Debug( LDAP_DEBUG_TRACE,
69 "<= str2entry NULL (entry allocation failed)\n",
74 /* initialize entry */
86 while ( (s = ldif_getline( &next )) != NULL ) {
87 if ( *s == '\n' || *s == '\0' ) {
91 if ( ldif_parse_line( s, &type, &value.bv_val, &value.bv_len ) != 0 ) {
92 Debug( LDAP_DEBUG_TRACE,
93 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
97 if ( strcasecmp( type, "dn" ) == 0 ) {
100 if ( e->e_dn != NULL ) {
101 Debug( LDAP_DEBUG_ANY,
102 "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
104 value.bv_val != NULL ? value.bv_val : "" );
105 if( value.bv_val != NULL ) free( value.bv_val );
109 e->e_dn = value.bv_val != NULL ? value.bv_val : ch_strdup( "" );
114 rc = slap_str2ad( type, &ad, &text );
116 if( rc != LDAP_SUCCESS ) {
117 Debug( LDAP_DEBUG_TRACE,
118 "<= str2entry: str2ad(%s): %s\n", type, text, 0 );
120 rc = slap_str2undef_ad( type, &ad, &text );
122 if( rc != LDAP_SUCCESS ) {
123 Debug( LDAP_DEBUG_TRACE,
124 "<= str2entry: str2undef_ad(%s): %s\n",
127 free( value.bv_val );
133 rc = attr_merge( e, ad, vals );
138 Debug( LDAP_DEBUG_TRACE,
139 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
141 free( value.bv_val );
147 free( value.bv_val );
150 /* check to make sure there was a dn: line */
151 if ( e->e_dn == NULL ) {
152 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
158 /* generate normalized dn */
159 e->e_ndn = ch_strdup( e->e_dn );
160 (void) dn_normalize( e->e_ndn );
162 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
163 e->e_dn, e->e_id, (unsigned long)e );
169 #define GRABSIZE BUFSIZ
171 #define MAKE_SPACE( n ) { \
172 while ( ecur + (n) > ebuf + emaxsize ) { \
174 offset = (int) (ecur - ebuf); \
175 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
176 emaxsize + GRABSIZE ); \
177 emaxsize += GRABSIZE; \
178 ecur = ebuf + offset; \
192 * In string format, an entry looks like this:
194 * [<attr>: <value>\n]*
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 = a->a_desc->ad_cname->bv_len;
213 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
214 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE,
215 a->a_desc->ad_cname->bv_val,
216 bv->bv_val, bv->bv_len );
223 return( (char *) ebuf );
227 entry_free( Entry *e )
231 if ( e->e_dn != NULL ) {
235 if ( e->e_ndn != NULL ) {
239 for ( a = e->e_attrs; a != NULL; a = next ) {
249 * These routines are used only by Backend.
251 * the Entry has three entry points (ways to find things):
253 * by entry e.g., if you already have an entry from the cache
254 * and want to delete it. (really by entry ptr)
255 * by dn e.g., when looking for the base object of a search
256 * by id e.g., for search candidates
258 * these correspond to three different avl trees that are maintained.
262 entry_cmp( Entry *e1, Entry *e2 )
264 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
268 entry_dn_cmp( Entry *e1, Entry *e2 )
270 /* compare their normalized UPPERCASED dn's */
271 return( strcmp( e1->e_ndn, e2->e_ndn ) );
275 entry_id_cmp( Entry *e1, Entry *e2 )
277 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );