/* line64.c - routines for dealing with the slapd line format */
+/* $OpenLDAP$ */
/*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#define RIGHT2 0x03
#define RIGHT4 0x0f
-#define CONTINUED_LINE_MARKER '\001'
+#define CONTINUED_LINE_MARKER '\r'
+
+#ifdef CSRIMALLOC
+#define ber_memalloc malloc
+#define ber_memcalloc calloc
+#define ber_memrealloc realloc
+#define ber_strdup strdup
+#endif
static const char nib2b64[0x40] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}
*s++ = '\0';
+ url = 0;
+ b64 = 0;
+
if ( *s == '\0' ) {
/* no value */
- value = NULL;
+ value = "";
vlen = 0;
goto done;
}
- url = 0;
- b64 = 0;
-
if ( *s == '<' ) {
s++;
url = 1;
s++;
}
+ /* check for continued line markers that should be deleted */
+ for ( p = s, d = s; *p; p++ ) {
+ if ( *p != CONTINUED_LINE_MARKER )
+ *d++ = *p;
+ }
+ *d = '\0';
+
/* if no value is present, error out */
if ( *s == '\0' ) {
ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
goto done;
}
- /* check for continued line markers that should be deleted */
- for ( p = s, d = s; *p; p++ ) {
- if ( *p != CONTINUED_LINE_MARKER )
- *d++ = *p;
- }
- *d = '\0';
-
if ( b64 ) {
char *byte = s;
}
if( !url && value != NULL ) {
- value = ber_strdup( value );
- if( value == NULL ) {
+ p = ber_memalloc( vlen + 1 );
+ if( p == NULL ) {
ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
"ldif_parse_line: value malloc failed\n");
ber_memfree( type );
ber_memfree( freeme );
return( -1 );
}
+ AC_MEMCPY( p, value, vlen );
+ p[vlen] = '\0';
+ value = p;
}
ber_memfree( freeme );
line = *next;
while ( (*next = strchr( *next, '\n' )) != NULL ) {
- unsigned char c = *(*next + 1);
+#if CONTINUED_LINE_MARKER != '\r'
+ if ( (*next)[-1] == '\r' ) {
+ (*next)[-1] = CONTINUED_LINE_MARKER;
+ }
+#endif
- if ( !isspace( c ) || c == '\n' ) {
+ if ( (*next)[1] != ' ' ) {
+ if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
+ *(*next)++ = '\0';
+ }
*(*next)++ = '\0';
break;
}
**next = CONTINUED_LINE_MARKER;
- *(*next+1) = CONTINUED_LINE_MARKER;
+ (*next)[1] = CONTINUED_LINE_MARKER;
(*next)++;
}
} while( *line == '#' );
LDAP_CONST char *val,
ber_len_t vlen )
{
- unsigned char *byte, *stop;
+ const unsigned char *byte, *stop;
unsigned char buf[3];
unsigned long bits;
char *save;
int pad;
+ int namelen = 0;
ber_len_t savelen;
ber_len_t len=0;
/* prefix */
switch( type ) {
case LDIF_PUT_COMMENT:
- if( name != NULL ) break;
-
*(*out)++ = '#';
len++;
if( vlen ) {
*(*out)++ = ' ';
len++;
- break;
}
- /* no value, fall-thru */
+ break;
case LDIF_PUT_SEP:
*(*out)++ = '\n';
/* name (attribute type) */
if( name != NULL ) {
/* put the name + ":" */
- for ( i=0 ; name[i]; i++ ) {
- *(*out)++ = name[i];
- len++;
- }
+ namelen = strlen(name);
+ strcpy(*out, name);
+ *out += namelen;
+ len += namelen;
if( type != LDIF_PUT_COMMENT ) {
*(*out)++ = ':';
len++;
}
+ }
#ifdef LDAP_DEBUG
- } else {
+ else {
assert( type == LDIF_PUT_COMMENT );
+ }
#endif
+
+ if( vlen == 0 ) {
+ *(*out)++ = '\n';
+ return;
}
switch( type ) {
*(*out)++ = ' ';
len++;
- if( vlen == 0 ) {
- *(*out)++ = '\n';
- return;
- }
-
- stop = (unsigned char *) (val + vlen);
+ stop = (const unsigned char *) (val + vlen);
if ( type == LDIF_PUT_VALUE
- && isgraph( val[0] ) && val[0] != ':' && val[0] != '<'
- && isgraph( val[vlen-1] )
- && strcasecmp( name, "userPassword" ) != 0 /* encode userPassword */
- && strcasecmp( name, "2.5.4.35" ) != 0 /* encode userPassword */
+ && isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<'
+ && isgraph( (unsigned char) val[vlen-1] )
+#ifndef LDAP_BINARY_DEBUG
+ && strstr( name, ";binary" ) == NULL
+#endif
+#ifndef LDAP_PASSWD_DEBUG
+ && (namelen != (sizeof("userPassword")-1)
+ || strcasecmp( name, "userPassword" ) != 0) /* encode userPassword */
+ && (namelen != (sizeof("2.5.4.35")-1)
+ || strcasecmp( name, "2.5.4.35" ) != 0) /* encode userPassword */
+#endif
) {
int b64 = 0;
- for ( byte = (unsigned char *) val; byte < stop;
+ for ( byte = (const unsigned char *) val; byte < stop;
byte++, len++ )
{
if ( !isascii( *byte ) || !isprint( *byte ) ) {
len = savelen + 2;
/* convert to base 64 (3 bytes => 4 base 64 digits) */
- for ( byte = (unsigned char *) val;
+ for ( byte = (const unsigned char *) val;
byte < stop - 2;
byte += 3 )
{
return -1;
}
- if( isgraph( val[0] ) && val[0] != ':' && val[0] != '<' &&
- isgraph( val[vlen-1] ) )
+ if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
+ isgraph( (unsigned char) val[vlen-1] ) )
{
ber_len_t i;
return 1;
}
+
+/*
+ * slap_read_ldif - read an ldif record. Return 1 for success, 0 for EOF.
+ */
+int
+ldif_read_record(
+ FILE *fp,
+ int *lno, /* ptr to line number counter */
+ char **bufp, /* ptr to malloced output buffer */
+ int *buflenp ) /* ptr to length of *bufp */
+{
+ char linebuf[BUFSIZ], *line, *nbufp;
+ ber_len_t lcur = 0, len, linesize;
+ int last_ch = '\n', found_entry = 0, stop;
+
+ line = linebuf;
+ linesize = sizeof( linebuf );
+
+ for ( stop = feof( fp ); !stop; last_ch = line[len-1] ) {
+ if ( fgets( line, linesize, fp ) == NULL ) {
+ stop = 1;
+ /* Add \n in case the file does not end with newline */
+ line = "\n";
+ }
+ len = strlen( line );
+
+ if ( last_ch == '\n' ) {
+ (*lno)++;
+
+ if ( line[0] == '\n' ) {
+ if ( !found_entry )
+ continue;
+ break;
+ }
+
+ if ( !found_entry ) {
+ /* Found a new entry */
+ found_entry = 1;
+
+ if ( isdigit( (unsigned char) line[0] ) ) {
+ /* skip index */
+ continue;
+ }
+ }
+ }
+
+ if ( *buflenp - lcur <= len ) {
+ *buflenp += len + BUFSIZ;
+ nbufp = ber_memrealloc( *bufp, *buflenp );
+ if( nbufp == NULL ) {
+ return 0;
+ }
+ *bufp = nbufp;
+ }
+ strcpy( *bufp + lcur, line );
+ lcur += len;
+ }
+
+ return( found_entry );
+}