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'
28 static const char nib2b64[0x40] =
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31 static const unsigned char b642nib[0x80] = {
32 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
33 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
34 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
35 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
37 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
38 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
39 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
40 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
41 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
42 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
43 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
44 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
45 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
46 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
47 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
51 * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
52 * into components "type" and "value". if a double colon separates type from
53 * value, then value is encoded in base 64, and parse_line un-decodes it
54 * (in place) before returning.
59 LDAP_CONST char *line,
68 char *freeme, *type, *value;
75 /* skip any leading space */
76 while ( isspace( (unsigned char) *line ) ) {
80 freeme = ber_strdup( line );
82 if( freeme == NULL ) {
83 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
84 "ldif_parse_line: line malloc failed\n");
90 s = strchr( type, ':' );
93 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
94 "ldif_parse_line: missing ':' after %s\n",
96 ber_memfree( freeme );
100 /* trim any space between type and : */
101 for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) {
119 } else if ( *s == ':' ) {
120 /* base 64 encoded value */
125 /* skip space between : and value */
126 while ( isspace( (unsigned char) *s ) ) {
130 /* if no value is present, error out */
132 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
133 "ldif_parse_line: %s missing %svalue\n", type,
134 url ? "URL " : b64 ? "base64 " : "" );
140 /* check for continued line markers that should be deleted */
141 for ( p = s, d = s; *p; p++ ) {
142 if ( *p != CONTINUED_LINE_MARKER )
152 for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) {
154 for ( i = 0; i < 4; i++ ) {
155 if ( p[i] != '=' && (p[i] & 0x80 ||
156 b642nib[ p[i] & 0x7f ] > 0x3f) ) {
157 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
158 "ldif_parse_line: %s: invalid base64 encoding"
161 ber_memfree( freeme );
167 nib = b642nib[ p[0] & 0x7f ];
170 nib = b642nib[ p[1] & 0x7f ];
172 byte[1] = (nib & RIGHT4) << 4;
178 nib = b642nib[ p[2] & 0x7f ];
180 byte[2] = (nib & RIGHT2) << 6;
186 nib = b642nib[ p[3] & 0x7f ];
194 if( ldif_fetch_url( s, &value, &vlen ) ) {
195 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
196 "ldif_parse_line: %s: URL \"%s\" fetch failed\n",
198 ber_memfree( freeme );
204 vlen = (int) (d - s);
208 type = ber_strdup( type );
211 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
212 "ldif_parse_line: type malloc failed\n");
213 ber_memfree( freeme );
217 if( !url && value != NULL ) {
218 value = ber_strdup( value );
219 if( value == NULL ) {
220 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
221 "ldif_parse_line: value malloc failed\n");
223 ber_memfree( freeme );
228 ber_memfree( freeme );
238 * ldif_getline - return the next "line" (minus newline) of input from a
239 * string buffer of lines separated by newlines, terminated by \n\n
240 * or \0. this routine handles continued lines, bundling them into
241 * a single big line before returning. if a line begins with a white
242 * space character, it is a continuation of the previous line. the white
243 * space character (nb: only one char), and preceeding newline are changed
244 * into CONTINUED_LINE_MARKER chars, to be deleted later by the
245 * ldif_parse_line() routine above.
247 * ldif_getline will skip over any line which starts '#'.
249 * ldif_getline takes a pointer to a pointer to the buffer on the first call,
250 * which it updates and must be supplied on subsequent calls.
254 ldif_getline( char **next )
259 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
265 while ( (*next = strchr( *next, '\n' )) != NULL ) {
266 unsigned char c = *(*next + 1);
268 if ( !isspace( c ) || c == '\n' ) {
273 **next = CONTINUED_LINE_MARKER;
274 *(*next+1) = CONTINUED_LINE_MARKER;
277 } while( *line == '#' );
282 /* compatibility with U-Mich off by one bug */
283 #define LDIF_KLUDGE 1
289 LDAP_CONST char *name,
290 LDAP_CONST char *val,
293 unsigned char *byte, *stop;
294 unsigned char buf[3];
305 case LDIF_PUT_COMMENT:
306 if( name != NULL ) break;
317 /* no value, fall-thru */
324 /* name (attribute type) */
326 /* put the name + ":" */
327 for ( i=0 ; name[i]; i++ ) {
332 if( type != LDIF_PUT_COMMENT ) {
339 assert( type == LDIF_PUT_COMMENT );
344 case LDIF_PUT_NOVALUE:
348 case LDIF_PUT_URL: /* url value */
353 case LDIF_PUT_B64: /* base64 value */
367 case LDIF_PUT_COMMENT:
368 /* pre-encoded names */
369 for ( i=0; i < vlen; i++ ) {
370 if ( len > LDIF_LINE_WIDTH ) {
394 stop = (unsigned char *) (val + vlen);
396 if ( type == LDIF_PUT_VALUE
397 && isgraph( val[0] ) && val[0] != ':' && val[0] != '<'
398 && isgraph( val[vlen-1] )
399 && strcasecmp( name, "userPassword" ) != 0 /* encode userPassword */
400 && strcasecmp( name, "2.5.4.35" ) != 0 /* encode userPassword */
404 for ( byte = (unsigned char *) val; byte < stop;
407 if ( !isascii( *byte ) || !isprint( *byte ) ) {
411 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
430 /* convert to base 64 (3 bytes => 4 base 64 digits) */
431 for ( byte = (unsigned char *) val;
435 bits = (byte[0] & 0xff) << 16;
436 bits |= (byte[1] & 0xff) << 8;
437 bits |= (byte[2] & 0xff);
439 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
440 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
446 /* get b64 digit from high order 6 bits */
447 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
451 /* add padding if necessary */
453 for ( i = 0; byte + i < stop; i++ ) {
456 for ( pad = 0; i < 3; i++, pad++ ) {
460 bits = (byte[0] & 0xff) << 16;
461 bits |= (byte[1] & 0xff) << 8;
462 bits |= (byte[2] & 0xff);
464 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
465 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
472 /* get b64 digit from low order 6 bits */
473 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
484 * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
489 LDAP_CONST char *name,
490 LDAP_CONST char *val,
496 nlen = ( name != NULL ) ? strlen( name ) : 0;
498 buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
501 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
502 "ldif_type_and_value: malloc failed!" );
507 ldif_sput( &p, type, name, val, vlen );
513 int ldif_is_not_printable(
514 LDAP_CONST char *val,
517 if( vlen == 0 || val == NULL ) {
521 if( isgraph( val[0] ) && val[0] != ':' && val[0] != '<' &&
522 isgraph( val[vlen-1] ) )
526 for ( i = 0; val[i]; i++ ) {
527 if ( !isascii( val[i] ) || !isprint( val[i] ) ) {