1 /* line64.c - routines for dealing with the slapd line format */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/stdlib.h>
15 #include <ac/string.h>
16 #include <ac/socket.h>
27 #define CONTINUED_LINE_MARKER '\001'
30 #define ber_memalloc malloc
31 #define ber_memcalloc calloc
32 #define ber_memrealloc realloc
33 #define ber_strdup strdup
36 static const char nib2b64[0x40] =
37 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
39 static const unsigned char b642nib[0x80] = {
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
46 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
47 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
48 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
49 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
50 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
51 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
52 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
53 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
54 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
55 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
59 * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
60 * into components "type" and "value". if a double colon separates type from
61 * value, then value is encoded in base 64, and parse_line un-decodes it
62 * (in place) before returning.
67 LDAP_CONST char *line,
76 char *freeme, *type, *value;
83 /* skip any leading space */
84 while ( isspace( (unsigned char) *line ) ) {
88 freeme = ber_strdup( line );
90 if( freeme == NULL ) {
91 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
92 "ldif_parse_line: line malloc failed\n");
98 s = strchr( type, ':' );
101 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
102 "ldif_parse_line: missing ':' after %s\n",
104 ber_memfree( freeme );
108 /* trim any space between type and : */
109 for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) {
127 } else if ( *s == ':' ) {
128 /* base 64 encoded value */
133 /* skip space between : and value */
134 while ( isspace( (unsigned char) *s ) ) {
138 /* check for continued line markers that should be deleted */
139 for ( p = s, d = s; *p; p++ ) {
140 if ( *p != CONTINUED_LINE_MARKER )
145 /* if no value is present, error out */
147 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
148 "ldif_parse_line: %s missing %svalue\n", type,
149 url ? "URL " : b64 ? "base64 " : "" );
160 for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) {
162 for ( i = 0; i < 4; i++ ) {
163 if ( p[i] != '=' && (p[i] & 0x80 ||
164 b642nib[ p[i] & 0x7f ] > 0x3f) ) {
165 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
166 "ldif_parse_line: %s: invalid base64 encoding"
169 ber_memfree( freeme );
175 nib = b642nib[ p[0] & 0x7f ];
178 nib = b642nib[ p[1] & 0x7f ];
180 byte[1] = (nib & RIGHT4) << 4;
186 nib = b642nib[ p[2] & 0x7f ];
188 byte[2] = (nib & RIGHT2) << 6;
194 nib = b642nib[ p[3] & 0x7f ];
202 if( ldif_fetch_url( s, &value, &vlen ) ) {
203 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
204 "ldif_parse_line: %s: URL \"%s\" fetch failed\n",
206 ber_memfree( freeme );
212 vlen = (int) (d - s);
216 type = ber_strdup( type );
219 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
220 "ldif_parse_line: type malloc failed\n");
221 ber_memfree( freeme );
225 if( !url && value != NULL ) {
226 p = ber_memalloc( vlen + 1 );
228 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
229 "ldif_parse_line: value malloc failed\n");
231 ber_memfree( freeme );
234 memcpy( p, value, vlen );
239 ber_memfree( freeme );
249 * ldif_getline - return the next "line" (minus newline) of input from a
250 * string buffer of lines separated by newlines, terminated by \n\n
251 * or \0. this routine handles continued lines, bundling them into
252 * a single big line before returning. if a line begins with a white
253 * space character, it is a continuation of the previous line. the white
254 * space character (nb: only one char), and preceeding newline are changed
255 * into CONTINUED_LINE_MARKER chars, to be deleted later by the
256 * ldif_parse_line() routine above.
258 * ldif_getline will skip over any line which starts '#'.
260 * ldif_getline takes a pointer to a pointer to the buffer on the first call,
261 * which it updates and must be supplied on subsequent calls.
265 ldif_getline( char **next )
270 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
276 while ( (*next = strchr( *next, '\n' )) != NULL ) {
277 unsigned char c = *(*next + 1);
279 if ( !isspace( c ) || c == '\n' ) {
284 **next = CONTINUED_LINE_MARKER;
285 *(*next+1) = CONTINUED_LINE_MARKER;
288 } while( *line == '#' );
293 /* compatibility with U-Mich off by one bug */
294 #define LDIF_KLUDGE 1
300 LDAP_CONST char *name,
301 LDAP_CONST char *val,
304 const unsigned char *byte, *stop;
305 unsigned char buf[3];
316 case LDIF_PUT_COMMENT:
332 /* name (attribute type) */
334 /* put the name + ":" */
335 for ( i=0 ; name[i]; i++ ) {
340 if( type != LDIF_PUT_COMMENT ) {
348 assert( type == LDIF_PUT_COMMENT );
358 case LDIF_PUT_NOVALUE:
362 case LDIF_PUT_URL: /* url value */
367 case LDIF_PUT_B64: /* base64 value */
381 case LDIF_PUT_COMMENT:
382 /* pre-encoded names */
383 for ( i=0; i < vlen; i++ ) {
384 if ( len > LDIF_LINE_WIDTH ) {
403 stop = (const unsigned char *) (val + vlen);
405 if ( type == LDIF_PUT_VALUE
406 && isgraph( val[0] ) && val[0] != ':' && val[0] != '<'
407 && isgraph( val[vlen-1] )
408 #ifndef LDAP_BINARY_DEBUG
409 && strstr( name, ";binary" ) == NULL
411 #ifndef LDAP_PASSWD_DEBUG
412 && strcasecmp( name, "userPassword" ) != 0 /* encode userPassword */
413 && strcasecmp( name, "2.5.4.35" ) != 0 /* encode userPassword */
418 for ( byte = (const unsigned char *) val; byte < stop;
421 if ( !isascii( *byte ) || !isprint( *byte ) ) {
425 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
444 /* convert to base 64 (3 bytes => 4 base 64 digits) */
445 for ( byte = (const unsigned char *) val;
449 bits = (byte[0] & 0xff) << 16;
450 bits |= (byte[1] & 0xff) << 8;
451 bits |= (byte[2] & 0xff);
453 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
454 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
460 /* get b64 digit from high order 6 bits */
461 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
465 /* add padding if necessary */
467 for ( i = 0; byte + i < stop; i++ ) {
470 for ( pad = 0; i < 3; i++, pad++ ) {
474 bits = (byte[0] & 0xff) << 16;
475 bits |= (byte[1] & 0xff) << 8;
476 bits |= (byte[2] & 0xff);
478 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
479 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
486 /* get b64 digit from low order 6 bits */
487 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
498 * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
503 LDAP_CONST char *name,
504 LDAP_CONST char *val,
510 nlen = ( name != NULL ) ? strlen( name ) : 0;
512 buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
515 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
516 "ldif_type_and_value: malloc failed!" );
521 ldif_sput( &p, type, name, val, vlen );
527 int ldif_is_not_printable(
528 LDAP_CONST char *val,
531 if( vlen == 0 || val == NULL ) {
535 if( isgraph( val[0] ) && val[0] != ':' && val[0] != '<' &&
536 isgraph( val[vlen-1] ) )
540 for ( i = 0; val[i]; i++ ) {
541 if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
553 * slap_read_ldif - read an ldif record. Return 1 for success, 0 for EOF.
558 int *lno, /* ptr to line number counter */
559 char **bufp, /* ptr to malloced output buffer */
560 int *buflenp ) /* ptr to length of *bufp */
562 char linebuf[BUFSIZ], *line, *nbufp;
563 ber_len_t lcur = 0, len, linesize;
564 int last_ch = '\n', found_entry = 0, stop;
567 linesize = sizeof( linebuf );
569 for ( stop = feof( fp ); !stop; last_ch = line[len-1] ) {
570 if ( fgets( line, linesize, fp ) == NULL ) {
572 /* Add \n in case the file does not end with newline */
575 len = strlen( line );
577 if ( last_ch == '\n' ) {
580 if ( line[0] == '\n' ) {
586 if ( !found_entry ) {
587 /* Found a new entry */
590 if ( isdigit( (unsigned char) line[0] ) ) {
597 if ( *buflenp - lcur <= len ) {
598 *buflenp += len + BUFSIZ;
599 nbufp = ber_memrealloc( *bufp, *buflenp );
600 if( nbufp == NULL ) {
605 strcpy( *bufp + lcur, line );
609 return( found_entry );