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 */
30 struct berval *vals[2];
34 * In string format, an entry looks like this:
38 * [<attr>:[:] <value>\n]
39 * [<tab><continuedvalue>\n]*
42 * If a double colon is used after a type, it means the
43 * following value is encoded as a base 64 string. This
44 * happens if the value contains a non-printing character
48 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n",
49 s ? s : "NULL", 0, 0 );
51 /* check to see if there's an id included */
53 if ( isdigit( (unsigned char) *s ) ) {
55 if ( (s = ldif_getline( &next )) == NULL ) {
56 Debug( LDAP_DEBUG_TRACE,
57 "<= str2entry NULL (missing newline after id)\n",
63 /* initialize reader/writer lock */
64 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
67 Debug( LDAP_DEBUG_TRACE,
68 "<= str2entry NULL (entry allocation failed)\n",
81 while ( (s = ldif_getline( &next )) != NULL ) {
82 if ( *s == '\n' || *s == '\0' ) {
86 if ( ldif_parse_line( s, &type, &value, &vlen ) != 0 ) {
87 Debug( LDAP_DEBUG_TRACE,
88 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
92 if ( strcasecmp( type, ptype ) != 0 ) {
93 strncpy( ptype, type, sizeof(ptype) - 1 );
99 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",
103 e->e_id, e->e_dn, value );
106 e->e_dn = ch_strdup( value );
108 if ( e->e_ndn != NULL ) {
109 Debug( LDAP_DEBUG_ANY,
110 "str2entry: entry %ld already has a normalized dn \"%s\" for \"%s\" (first ignored)\n",
111 e->e_id, e->e_ndn, value );
114 e->e_ndn = ch_strdup( value );
115 (void) dn_normalize_case( e->e_ndn );
121 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
123 Debug( LDAP_DEBUG_TRACE,
124 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
131 /* check to make sure there was a dn: line */
132 if ( e->e_dn == NULL ) {
133 Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
139 if ( e->e_ndn == NULL ) {
140 Debug( LDAP_DEBUG_ANY,
141 "str2entry: entry %ld (\"%s\") has no normalized dn\n",
142 e->e_id, e->e_dn, 0 );
147 Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> %ld (0x%lx)\n",
148 e->e_dn, e->e_id, (unsigned long)e );
153 #define GRABSIZE BUFSIZ
155 #define MAKE_SPACE( n ) { \
156 while ( ecur + (n) > ebuf + emaxsize ) { \
158 offset = (int) (ecur - ebuf); \
159 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
160 emaxsize + GRABSIZE ); \
161 emaxsize += GRABSIZE; \
162 ecur = ebuf + offset; \
178 * In string format, an entry looks like this:
181 * [<attr>: <value>\n]*
189 sprintf( (char *) ecur, "%ld\n", e->e_id );
190 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
194 if ( e->e_dn != NULL ) {
196 tmplen = strlen( e->e_dn );
197 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
198 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
201 /* put the attributes */
202 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
203 /* put "<type>:[:] <value>" line for each value */
204 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
206 tmplen = strlen( a->a_type );
207 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
208 ldif_sput( (char **) &ecur, LDIF_PUT_VALUE, a->a_type,
209 bv->bv_val, bv->bv_len );
216 return( (char *) ebuf );
220 entry_free( Entry *e )
224 if ( e->e_dn != NULL ) {
228 if ( e->e_ndn != NULL ) {
232 for ( a = e->e_attrs; a != NULL; a = next ) {
242 * These routines are used only by Backend.
244 * the Entry has three entry points (ways to find things):
246 * by entry e.g., if you already have an entry from the cache
247 * and want to delete it. (really by entry ptr)
248 * by dn e.g., when looking for the base object of a search
249 * by id e.g., for search candidates
251 * these correspond to three different avl trees that are maintained.
255 entry_cmp( Entry *e1, Entry *e2 )
257 return( e1 < e2 ? -1 : (e1 > e2 ? 1 : 0) );
261 entry_dn_cmp( Entry *e1, Entry *e2 )
263 /* compare their normalized UPPERCASED dn's */
264 return( strcmp( e1->e_ndn, e2->e_ndn ) );
268 entry_id_cmp( Entry *e1, Entry *e2 )
270 return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );