1 /* line64.c - routines for dealing with the slapd line format */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/string.h>
15 #include <ac/socket.h>
26 #define CONTINUED_LINE_MARKER '\001'
29 #define ber_memalloc malloc
30 #define ber_memcalloc calloc
31 #define ber_memrealloc realloc
32 #define ber_strdup strdup
35 static const char nib2b64[0x40] =
36 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
38 static const unsigned char b642nib[0x80] = {
39 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
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, 0x3e, 0xff, 0xff, 0xff, 0x3f,
45 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
46 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
48 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
49 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
50 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
51 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
52 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
53 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
54 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
58 * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
59 * into components "type" and "value". if a double colon separates type from
60 * value, then value is encoded in base 64, and parse_line un-decodes it
61 * (in place) before returning.
66 LDAP_CONST char *line,
75 char *freeme, *type, *value;
82 /* skip any leading space */
83 while ( isspace( (unsigned char) *line ) ) {
87 freeme = ber_strdup( line );
89 if( freeme == NULL ) {
90 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
91 "ldif_parse_line: line malloc failed\n");
97 s = strchr( type, ':' );
100 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
101 "ldif_parse_line: missing ':' after %s\n",
103 ber_memfree( freeme );
107 /* trim any space between type and : */
108 for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) {
126 } else if ( *s == ':' ) {
127 /* base 64 encoded value */
132 /* skip space between : and value */
133 while ( isspace( (unsigned char) *s ) ) {
137 /* if no value is present, error out */
139 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
140 "ldif_parse_line: %s missing %svalue\n", type,
141 url ? "URL " : b64 ? "base64 " : "" );
147 /* check for continued line markers that should be deleted */
148 for ( p = s, d = s; *p; p++ ) {
149 if ( *p != CONTINUED_LINE_MARKER )
159 for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) {
161 for ( i = 0; i < 4; i++ ) {
162 if ( p[i] != '=' && (p[i] & 0x80 ||
163 b642nib[ p[i] & 0x7f ] > 0x3f) ) {
164 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
165 "ldif_parse_line: %s: invalid base64 encoding"
168 ber_memfree( freeme );
174 nib = b642nib[ p[0] & 0x7f ];
177 nib = b642nib[ p[1] & 0x7f ];
179 byte[1] = (nib & RIGHT4) << 4;
185 nib = b642nib[ p[2] & 0x7f ];
187 byte[2] = (nib & RIGHT2) << 6;
193 nib = b642nib[ p[3] & 0x7f ];
201 if( ldif_fetch_url( s, &value, &vlen ) ) {
202 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
203 "ldif_parse_line: %s: URL \"%s\" fetch failed\n",
205 ber_memfree( freeme );
211 vlen = (int) (d - s);
215 type = ber_strdup( type );
218 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
219 "ldif_parse_line: type malloc failed\n");
220 ber_memfree( freeme );
224 if( !url && value != NULL ) {
225 value = ber_strdup( value );
226 if( value == NULL ) {
227 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
228 "ldif_parse_line: value malloc failed\n");
230 ber_memfree( freeme );
235 ber_memfree( freeme );
245 * ldif_getline - return the next "line" (minus newline) of input from a
246 * string buffer of lines separated by newlines, terminated by \n\n
247 * or \0. this routine handles continued lines, bundling them into
248 * a single big line before returning. if a line begins with a white
249 * space character, it is a continuation of the previous line. the white
250 * space character (nb: only one char), and preceeding newline are changed
251 * into CONTINUED_LINE_MARKER chars, to be deleted later by the
252 * ldif_parse_line() routine above.
254 * ldif_getline will skip over any line which starts '#'.
256 * ldif_getline takes a pointer to a pointer to the buffer on the first call,
257 * which it updates and must be supplied on subsequent calls.
261 ldif_getline( char **next )
266 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
272 while ( (*next = strchr( *next, '\n' )) != NULL ) {
273 unsigned char c = *(*next + 1);
275 if ( !isspace( c ) || c == '\n' ) {
280 **next = CONTINUED_LINE_MARKER;
281 *(*next+1) = CONTINUED_LINE_MARKER;
284 } while( *line == '#' );
289 /* compatibility with U-Mich off by one bug */
290 #define LDIF_KLUDGE 1
296 LDAP_CONST char *name,
297 LDAP_CONST char *val,
300 const unsigned char *byte, *stop;
301 unsigned char buf[3];
312 case LDIF_PUT_COMMENT:
313 if( name != NULL ) break;
324 /* no value, fall-thru */
331 /* name (attribute type) */
333 /* put the name + ":" */
334 for ( i=0 ; name[i]; i++ ) {
339 if( type != LDIF_PUT_COMMENT ) {
346 assert( type == LDIF_PUT_COMMENT );
351 case LDIF_PUT_NOVALUE:
355 case LDIF_PUT_URL: /* url value */
360 case LDIF_PUT_B64: /* base64 value */
374 case LDIF_PUT_COMMENT:
375 /* pre-encoded names */
376 for ( i=0; i < vlen; i++ ) {
377 if ( len > LDIF_LINE_WIDTH ) {
401 stop = (const unsigned char *) (val + vlen);
403 if ( type == LDIF_PUT_VALUE
404 && isgraph( val[0] ) && val[0] != ':' && val[0] != '<'
405 && isgraph( val[vlen-1] )
406 && strcasecmp( name, "userPassword" ) != 0 /* encode userPassword */
407 && strcasecmp( name, "2.5.4.35" ) != 0 /* encode userPassword */
411 for ( byte = (const unsigned char *) val; byte < stop;
414 if ( !isascii( *byte ) || !isprint( *byte ) ) {
418 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
437 /* convert to base 64 (3 bytes => 4 base 64 digits) */
438 for ( byte = (const unsigned char *) val;
442 bits = (byte[0] & 0xff) << 16;
443 bits |= (byte[1] & 0xff) << 8;
444 bits |= (byte[2] & 0xff);
446 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
447 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
453 /* get b64 digit from high order 6 bits */
454 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
458 /* add padding if necessary */
460 for ( i = 0; byte + i < stop; i++ ) {
463 for ( pad = 0; i < 3; i++, pad++ ) {
467 bits = (byte[0] & 0xff) << 16;
468 bits |= (byte[1] & 0xff) << 8;
469 bits |= (byte[2] & 0xff);
471 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
472 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
479 /* get b64 digit from low order 6 bits */
480 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
491 * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
496 LDAP_CONST char *name,
497 LDAP_CONST char *val,
503 nlen = ( name != NULL ) ? strlen( name ) : 0;
505 buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
508 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
509 "ldif_type_and_value: malloc failed!" );
514 ldif_sput( &p, type, name, val, vlen );
520 int ldif_is_not_printable(
521 LDAP_CONST char *val,
524 if( vlen == 0 || val == NULL ) {
528 if( isgraph( val[0] ) && val[0] != ':' && val[0] != '<' &&
529 isgraph( val[vlen-1] ) )
533 for ( i = 0; val[i]; i++ ) {
534 if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
546 * slap_read_ldif - read an ldif record. Return 1 for success, 0 for EOF.
551 int *lno, /* ptr to line number counter */
552 char **bufp, /* ptr to malloced output buffer */
553 int *buflenp ) /* ptr to length of *bufp */
555 char linebuf[BUFSIZ], *line;
556 ber_len_t lcur = 0, len, linesize;
557 int last_ch = '\n', found_entry = 0, stop;
560 linesize = sizeof( linebuf );
562 for ( stop = feof( fp ); !stop; last_ch = line[len-1] ) {
563 if ( fgets( line, linesize, fp ) == NULL ) {
565 /* Add \n in case the file does not end with newline */
568 len = strlen( line );
570 if ( last_ch == '\n' ) {
573 if ( line[0] == '\n' ) {
579 if ( !found_entry ) {
580 /* Found a new entry */
583 if ( isdigit( (unsigned char) line[0] ) ) {
590 if ( *buflenp - lcur <= len ) {
591 char *nbufp = ber_memrealloc( *bufp, *buflenp += len + BUFSIZ );
592 if( nbufp == NULL ) {
597 strcpy( *bufp + lcur, line );
601 return( found_entry );