1 /* entry.c - routines for dealing with entries */
7 #include <sys/socket.h>
13 static unsigned char *ebuf; /* buf returned by entry2str */
14 static unsigned char *ecur; /* pointer to end of currently used ebuf */
15 static int emaxsize;/* max size of ebuf */
26 int vlen, nvals, maxvals;
28 struct berval *vals[2];
32 * In string format, an entry looks like this:
36 * [<attr>:[:] <value>\n]
37 * [<tab><continuedvalue>\n]*
40 * If a double colon is used after a type, it means the
41 * following value is encoded as a base 64 string. This
42 * happens if the value contains a non-printing character
46 Debug( LDAP_DEBUG_TRACE, "=> str2entry\n", s, 0, 0 );
48 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
50 /* check to see if there's an id included */
52 if ( isdigit( *s ) ) {
54 if ( (s = str_getline( &next )) == NULL ) {
55 Debug( LDAP_DEBUG_TRACE,
56 "<= str2entry NULL (missing newline after id)\n",
67 while ( (s = str_getline( &next )) != NULL ) {
68 if ( *s == '\n' || *s == '\0' ) {
72 if ( str_parse_line( s, &type, &value, &vlen ) != 0 ) {
73 Debug( LDAP_DEBUG_TRACE,
74 "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
78 if ( strcasecmp( type, ptype ) != 0 ) {
79 strncpy( ptype, type, sizeof(ptype) - 1 );
84 if ( strcasecmp( type, "dn" ) == 0 ) {
85 if ( e->e_dn != NULL ) {
86 Debug( LDAP_DEBUG_ANY,
87 "str2entry: entry %d has multiple dns \"%s\" and \"%s\" (second ignored)\n",
88 e->e_id, e->e_dn, value );
91 e->e_dn = strdup( value );
97 if ( attr_merge_fast( e, type, vals, nvals, 1, &maxvals, &a )
99 Debug( LDAP_DEBUG_TRACE,
100 "<= str2entry NULL (attr_merge)\n", 0, 0, 0 );
106 /* check to make sure there was a dn: line */
107 if ( e->e_dn == NULL ) {
108 Debug( LDAP_DEBUG_ANY, "str2entry: entry %d has no dn\n",
114 Debug( LDAP_DEBUG_TRACE, "<= str2entry 0x%x\n", e, 0, 0 );
118 #define GRABSIZE BUFSIZ
120 #define MAKE_SPACE( n ) { \
121 while ( ecur + (n) > ebuf + emaxsize ) { \
123 offset = (int) (ecur - ebuf); \
124 ebuf = (unsigned char *) ch_realloc( (char *) ebuf, \
125 emaxsize + GRABSIZE ); \
126 emaxsize += GRABSIZE; \
127 ecur = ebuf + offset; \
143 * In string format, an entry looks like this:
146 * [<attr>: <value>\n]*
154 sprintf( (char *) ecur, "%ld\n", e->e_id );
155 ecur = (unsigned char *) strchr( (char *) ecur, '\0' );
159 if ( e->e_dn != NULL ) {
161 tmplen = strlen( e->e_dn );
162 MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
163 put_type_and_value( (char **) &ecur, "dn", e->e_dn, tmplen );
166 /* put the attributes */
167 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
168 /* put "<type>:[:] <value>" line for each value */
169 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
171 tmplen = strlen( a->a_type );
172 MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
173 put_type_and_value( (char **) &ecur, a->a_type,
174 bv->bv_val, bv->bv_len );
181 return( (char *) ebuf );
185 entry_free( Entry *e )
190 if ( e->e_dn != NULL ) {
193 for ( a = e->e_attrs; a != NULL; a = next ) {