1 /* line64.c - routines for dealing with the slapd line format */
10 #include <ac/string.h>
11 #include <ac/socket.h>
22 #define CONTINUED_LINE_MARKER '\001'
24 static const char nib2b64[0x40] =
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
27 static const unsigned char b642nib[0x80] = {
28 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
29 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
30 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
31 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
32 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
33 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
34 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
35 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
37 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
38 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
39 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
40 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
41 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
42 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
43 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
47 * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
48 * into components "type" and "value". if a double colon separates type from
49 * value, then value is encoded in base 64, and parse_line un-decodes it
50 * (in place) before returning.
55 LDAP_CONST char *line,
64 char *freeme, *type, *value;
71 /* skip any leading space */
72 while ( isspace( (unsigned char) *line ) ) {
76 freeme = ber_strdup( line );
78 if( freeme == NULL ) {
79 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
80 "ldif_parse_line: line malloc failed\n");
86 s = strchr( type, ':' );
89 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
90 "ldif_parse_line: missing ':' after %s\n",
92 ber_memfree( freeme );
96 /* trim any space between type and : */
97 for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) {
115 } else if ( *s == ':' ) {
116 /* base 64 encoded value */
121 /* skip space between : and value */
122 while ( isspace( (unsigned char) *s ) ) {
126 /* if no value is present, error out */
128 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
129 "ldif_parse_line: %s missing %svalue\n", type,
130 url ? "URL " : b64 ? "base64 " : "" );
136 /* check for continued line markers that should be deleted */
137 for ( p = s, d = s; *p; p++ ) {
138 if ( *p != CONTINUED_LINE_MARKER )
148 for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) {
150 for ( i = 0; i < 4; i++ ) {
151 if ( p[i] != '=' && (p[i] & 0x80 ||
152 b642nib[ p[i] & 0x7f ] > 0x3f) ) {
153 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
154 "ldif_parse_line: %s: invalid base64 encoding"
157 ber_memfree( freeme );
163 nib = b642nib[ p[0] & 0x7f ];
166 nib = b642nib[ p[1] & 0x7f ];
168 byte[1] = (nib & RIGHT4) << 4;
174 nib = b642nib[ p[2] & 0x7f ];
176 byte[2] = (nib & RIGHT2) << 6;
182 nib = b642nib[ p[3] & 0x7f ];
190 if( ldif_fetch_url( s, &value, &vlen ) ) {
191 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
192 "ldif_parse_line: %s: URL \"%s\" fetch failed\n",
194 ber_memfree( freeme );
200 vlen = (int) (d - s);
204 type = ber_strdup( type );
207 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
208 "ldif_parse_line: type malloc failed\n");
209 ber_memfree( freeme );
213 if( !url && value != NULL ) {
214 value = ber_strdup( value );
215 if( value == NULL ) {
216 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
217 "ldif_parse_line: value malloc failed\n");
219 ber_memfree( freeme );
224 ber_memfree( freeme );
234 * ldif_getline - return the next "line" (minus newline) of input from a
235 * string buffer of lines separated by newlines, terminated by \n\n
236 * or \0. this routine handles continued lines, bundling them into
237 * a single big line before returning. if a line begins with a white
238 * space character, it is a continuation of the previous line. the white
239 * space character (nb: only one char), and preceeding newline are changed
240 * into CONTINUED_LINE_MARKER chars, to be deleted later by the
241 * ldif_parse_line() routine above.
243 * ldif_getline will skip over any line which starts '#'.
245 * ldif_getline takes a pointer to a pointer to the buffer on the first call,
246 * which it updates and must be supplied on subsequent calls.
250 ldif_getline( char **next )
255 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
261 while ( (*next = strchr( *next, '\n' )) != NULL ) {
262 unsigned char c = *(*next + 1);
264 if ( !isspace( c ) || c == '\n' ) {
269 **next = CONTINUED_LINE_MARKER;
270 *(*next+1) = CONTINUED_LINE_MARKER;
273 } while( *line == '#' );
278 /* compatibility with U-Mich off by one bug */
279 #define LDIF_KLUDGE 1
285 LDAP_CONST char *name,
286 LDAP_CONST char *val,
289 unsigned char *byte, *stop;
290 unsigned char buf[3];
301 case LDIF_PUT_COMMENT:
302 if( name != NULL ) break;
313 /* no value, fall-thru */
320 /* name (attribute type) */
322 /* put the name + ":" */
323 for ( i=0 ; name[i]; i++ ) {
328 if( type != LDIF_PUT_COMMENT ) {
335 assert( type == LDIF_PUT_COMMENT );
340 case LDIF_PUT_NOVALUE:
344 case LDIF_PUT_URL: /* url value */
349 case LDIF_PUT_B64: /* base64 value */
363 case LDIF_PUT_COMMENT:
364 /* pre-encoded names */
365 for ( i=0; i < vlen; i++ ) {
366 if ( len > LDIF_LINE_WIDTH ) {
390 stop = (unsigned char *) (val + vlen);
392 if ( type == LDIF_PUT_VALUE &&
393 isgraph( val[0] ) && val[0] != ':' && val[0] != '<' &&
394 isgraph( val[vlen-1] ) )
398 for ( byte = (unsigned char *) val; byte < stop;
401 if ( !isascii( *byte ) || !isprint( *byte ) ) {
405 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
424 /* convert to base 64 (3 bytes => 4 base 64 digits) */
425 for ( byte = (unsigned char *) val;
429 bits = (byte[0] & 0xff) << 16;
430 bits |= (byte[1] & 0xff) << 8;
431 bits |= (byte[2] & 0xff);
433 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
434 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
440 /* get b64 digit from high order 6 bits */
441 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
445 /* add padding if necessary */
447 for ( i = 0; byte + i < stop; i++ ) {
450 for ( pad = 0; i < 3; i++, pad++ ) {
454 bits = (byte[0] & 0xff) << 16;
455 bits |= (byte[1] & 0xff) << 8;
456 bits |= (byte[2] & 0xff);
458 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
459 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
466 /* get b64 digit from low order 6 bits */
467 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
478 * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
483 LDAP_CONST char *name,
484 LDAP_CONST char *val,
490 nlen = ( name != NULL ) ? strlen( name ) : 0;
492 buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
495 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
496 "ldif_type_and_value: malloc failed!" );
501 ldif_sput( &p, type, name, val, vlen );
507 int ldif_is_not_printable(
508 LDAP_CONST char *val,
511 if( vlen == 0 || val == NULL ) {
515 if( isgraph( val[0] ) && val[0] != ':' && val[0] != '<' &&
516 isgraph( val[vlen-1] ) )
520 for ( i = 0; val[i]; i++ ) {
521 if ( !isascii( val[i] ) || !isprint( val[i] ) ) {