1 /* entry.c - routines for dealing with entries */
10 #include <ac/string.h>
14 static unsigned char *ebuf; /* buf returned by entry2str */
15 static unsigned char *ecur; /* pointer to end of currently used ebuf */
16 static int emaxsize;/* max size of ebuf */
27 int vlen, nvals, maxvals;
29 struct berval *vals[2];
33 * In string format, an entry looks like this:
37 * [<attr>:[:] <value>\n]
38 * [<tab><continuedvalue>\n]*
41 * If a double colon is used after a type, it means the
42 * following value is encoded as a base 64 string. This
43 * happens if the value contains a non-printing character
47 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
48 s ? s : "NULL", 0, 0 );
50 /* check to see if there's an id included */
52 if ( isdigit( (unsigned char) *s ) ) {
54 if ( (s = ldif_getline( &next )) == NULL ) {
55 Debug( LDAP_DEBUG_TRACE,
56 "<= str2entry NULL (missing newline after id)\n",
62 /* initialize reader/writer lock */
63 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
66 Debug( LDAP_DEBUG_TRACE,
67 "<= str2entry NULL (entry allocation failed)\n",
80 while ( (s = ldif_getline( &next )) != NULL ) {
81 if ( *s == '\n' || *s == '\0' ) {
85 if ( ldif_parse_line( s, &type, &value, &vlen ) != 0 ) {
86 Debug( LDAP_DEBUG_TRACE,
87 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
91 if ( strcasecmp( type, ptype ) != 0 ) {
92 strncpy( ptype, type, sizeof(ptype) - 1 );
98 if ( strcasecmp( type, "dn" ) == 0 ) {
99 if ( e->e_dn != NULL ) {
100 Debug( LDAP_DEBUG_ANY,
101 "str2entry: entry %ld has multiple dns \"%s\" and \"%s\" (second ignored)\n",
102 e->e_id, e->e_dn, value );
105 e->e_dn = ch_strdup( value );
107 if ( e->e_ndn != NULL ) {
108 Debug( LDAP_DEBUG_ANY,
109 "str2entry: entry %ld already has a normalized dn \"%s\" for \"%s\" (first ignored)\n",
110 e->e_id, e->e_ndn, value );
113 e->e_ndn = dn_normalize_case( ch_strdup( value ) );
119 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
121 Debug( LDAP_DEBUG_TRACE,
122 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
129 /* check to make sure there was a dn: line */
130 if ( e->e_dn == NULL ) {
131 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
137 if ( e->e_ndn == NULL ) {
138 Debug( LDAP_DEBUG_ANY,
139 "str2entry: entry %ld (\"%s\") has no normalized dn\n",
140 e->e_id, e->e_dn, 0 );
145 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
146 e->e_dn, e->e_id, (unsigned long)e );
151 #define GRABSIZE BUFSIZ
153 #define MAKE_SPACE( n ) { \
154 while ( ecur + (n) > ebuf + emaxsize ) { \
156 offset = (int) (ecur - ebuf); \
157 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
158 emaxsize + GRABSIZE ); \
159 emaxsize += GRABSIZE; \
160 ecur = ebuf + offset; \
176 * In string format, an entry looks like this:
179 * [<attr>: <value>\n]*
187 sprintf( (char *) ecur, "%ld\n", e->e_id );
188 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
192 if ( e->e_dn != NULL ) {
194 tmplen = strlen( e->e_dn );
195 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
196 ldif_put_type_and_value( (char **) &ecur, "dn", e->e_dn, tmplen );
199 /* put the attributes */
200 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
201 /* put "<type>:[:] <value>" line for each value */
202 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
204 tmplen = strlen( a->a_type );
205 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
206 ldif_put_type_and_value( (char **) &ecur, a->a_type,
207 bv->bv_val, bv->bv_len );
214 return( (char *) ebuf );
218 entry_free( Entry *e )
222 if ( e->e_dn != NULL ) {
226 if ( e->e_ndn != NULL ) {
230 for ( a = e->e_attrs; a != NULL; a = next ) {
240 * These routines are used only by Backend.
242 * the Entry has three entry points (ways to find things):
244 * by entry e.g., if you already have an entry from the cache
245 * and want to delete it. (really by entry ptr)
246 * by dn e.g., when looking for the base object of a search
247 * by id e.g., for search candidates
249 * these correspond to three different avl trees that are maintained.
253 entry_cmp( Entry *e1, Entry *e2 )
255 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
259 entry_dn_cmp( Entry *e1, Entry *e2 )
261 /* compare their normalized UPPERCASED dn's */
262 return( strcmp( e1->e_ndn, e2->e_ndn ) );
266 entry_id_cmp( Entry *e1, Entry *e2 )
268 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );