#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#ifndef macintosh
#include <sys/types.h>
#endif
#ifdef _WIN32
#include <windows.h>
#elif !defined( macintosh )
#include <sys/socket.h>
#endif
#include "ldaplog.h"
#include "ldif.h"
#ifndef isascii
#define isascii( c ) (!((c) & ~0177))
#endif
#define RIGHT2 0x03
#define RIGHT4 0x0f
#define CONTINUED_LINE_MARKER '\001'
#define ISBLANK(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
#define LDIF_OPT_ISSET( value, opt ) (((value) & (opt)) != 0 )
static char nib2b64[0x40] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static unsigned char b642nib[0x80] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
};
static int ldif_base64_encode_internal( unsigned char *src, char *dst, int srclen,
int lenused, int wraplen );
int
str_parse_line(
char *line,
char **type,
char **value,
int *vlen
)
{
char *p, *s, *d;
int b64;
while ( ISBLANK( *line ) ) {
line++;
}
*type = line;
for ( s = line; *s && *s != ':'; s++ )
;
if ( *s == '\0' ) {
#if defined( _WIN32 )
#endif
return( -1 );
}
for ( p = s - 1; p > line && ISBLANK( *p ); p-- ) {
*p = '\0';
}
*s++ = '\0';
if ( *s == ':' ) {
s++;
b64 = 1;
} else {
b64 = 0;
}
while ( ISBLANK( *s ) ) {
s++;
}
if ( *s == '\0' ) {
*value = s;
*vlen = 0;
return( 0 );
}
for ( p = s, d = s; *p; p++ ) {
if ( *p != CONTINUED_LINE_MARKER )
*d++ = *p;
}
*d = '\0';
*value = s;
if ( b64 ) {
if (( *vlen = ldif_base64_decode( s, (unsigned char *)s ))
< 0 ) {
#if defined( _WIN32 )
#endif
return( -1 );
}
s[ *vlen ] = '\0';
} else {
*vlen = (int) (d - s);
}
return( 0 );
}
int
ldif_base64_decode( char *src, unsigned char *dst )
{
char *p, *stop;
unsigned char nib, *byte;
int i, len;
stop = strchr( src, '\0' );
byte = dst;
for ( p = src, len = 0; p < stop; p += 4, len += 3 ) {
for ( i = 0; i < 4; i++ ) {
if ( p[i] != '=' && (p[i] & 0x80 ||
b642nib[ p[i] & 0x7f ] > 0x3f) ) {
return( -1 );
}
}
nib = b642nib[ p[0] & 0x7f ];
byte[0] = nib << 2;
nib = b642nib[ p[1] & 0x7f ];
byte[0] |= nib >> 4;
if ( p[2] == '=' ) {
len += 1;
break;
}
byte[1] = (nib & RIGHT4) << 4;
nib = b642nib[ p[2] & 0x7f ];
byte[1] |= nib >> 2;
if ( p[3] == '=' ) {
len += 2;
break;
}
byte[2] = (nib & RIGHT2) << 6;
nib = b642nib[ p[3] & 0x7f ];
byte[2] |= nib;
byte += 3;
}
return( len );
}
char *
str_getline( char **next )
{
char *l;
char c;
char *p;
if ( *next == NULL || **next == '\n' || **next == '\0' ) {
return( NULL );
}
while ( **next == '#' ) {
if (( *next = strchr( *next, '\n' )) == NULL ) {
return( NULL );
}
(*next)++;
}
l = *next;
while ( (*next = strchr( *next, '\n' )) != NULL ) {
p = *next - 1;
c = *(*next + 1);
if ( ISBLANK( c ) && c != '\n' ) {
if (*p == '\r')
*p = CONTINUED_LINE_MARKER;
**next = CONTINUED_LINE_MARKER;
*(*next+1) = CONTINUED_LINE_MARKER;
} else {
if (*p == '\r')
*p = '\0';
*(*next)++ = '\0';
break;
}
(*next)++;
}
return( l );
}
#define LDIF_SAFE_CHAR( c ) ( (c) != '\r' && (c) != '\n' )
#define LDIF_CONSERVATIVE_CHAR( c ) ( LDIF_SAFE_CHAR(c) && isascii((c)) \
&& ( isprint((c)) || (c) == '\t' ))
#define LDIF_SAFE_INITCHAR( c ) ( LDIF_SAFE_CHAR(c) && (c) != ':' \
&& (c) != ' ' && (c) != '<' )
#define LDIF_CONSERVATIVE_INITCHAR( c ) ( LDIF_SAFE_INITCHAR( c ) && \
! ( isascii((c)) && isspace((c))))
#define LDIF_CONSERVATIVE_FINALCHAR( c ) ( (c) != ' ' )
void
ldif_put_type_and_value_with_options( char **out, char *t, char *val,
int vlen, unsigned long options )
{
unsigned char *p, *byte, *stop;
char *save;
int b64, len, savelen, wraplen;
len = 0;
if ( LDIF_OPT_ISSET( options, LDIF_OPT_NOWRAP )) {
wraplen = -1;
} else {
wraplen = LDIF_MAX_LINE_WIDTH;
}
for ( p = (unsigned char *) t; *p; p++, len++ ) {
*(*out)++ = *p;
}
*(*out)++ = ':';
len++;
if ( LDIF_OPT_ISSET( options, LDIF_OPT_VALUE_IS_URL )) {
*(*out)++ = '<';
len++;
}
save = *out;
savelen = len;
b64 = 0;
stop = (unsigned char *)val;
if ( val && vlen > 0 ) {
*(*out)++ = ' ';
stop = (unsigned char *) (val + vlen);
if ( LDIF_OPT_ISSET( options, LDIF_OPT_MINIMAL_ENCODING )) {
if ( !LDIF_SAFE_INITCHAR( val[0] )) {
b64 = 1;
}
} else {
if ( !LDIF_CONSERVATIVE_INITCHAR( val[0] ) ||
!LDIF_CONSERVATIVE_FINALCHAR( val[vlen-1] )) {
b64 = 1;
}
}
}
if ( !b64 ) {
for ( byte = (unsigned char *) val; byte < stop;
byte++, len++ ) {
if ( LDIF_OPT_ISSET( options,
LDIF_OPT_MINIMAL_ENCODING )) {
if ( !LDIF_SAFE_CHAR( *byte )) {
b64 = 1;
break;
}
} else if ( !LDIF_CONSERVATIVE_CHAR( *byte )) {
b64 = 1;
break;
}
if ( wraplen != -1 && len > wraplen ) {
*(*out)++ = '\n';
*(*out)++ = ' ';
len = 1;
}
*(*out)++ = *byte;
}
}
if ( b64 ) {
*out = save;
*(*out)++ = ':';
*(*out)++ = ' ';
len = ldif_base64_encode_internal( (unsigned char *)val, *out, vlen,
savelen + 2, wraplen );
*out += len;
}
*(*out)++ = '\n';
}
void
ldif_put_type_and_value( char **out, char *t, char *val, int vlen )
{
ldif_put_type_and_value_with_options( out, t, val, vlen, 0 );
}
void
ldif_put_type_and_value_nowrap( char **out, char *t, char *val, int vlen )
{
ldif_put_type_and_value_with_options( out, t, val, vlen, LDIF_OPT_NOWRAP );
}
static int
ldif_base64_encode_internal( unsigned char *src, char *dst, int srclen, int lenused, int wraplen )
{
unsigned char *byte, *stop;
unsigned char buf[3];
char *out;
unsigned long bits;
int i, pad, len;
len = 0;
out = dst;
stop = src + srclen;
for ( byte = src; byte < stop - 2; byte += 3 ) {
bits = (byte[0] & 0xff) << 16;
bits |= (byte[1] & 0xff) << 8;
bits |= (byte[2] & 0xff);
for ( i = 0; i < 4; i++, bits <<= 6 ) {
if ( wraplen != -1 && lenused >= 0 && lenused++ > wraplen ) {
*out++ = '\n';
*out++ = ' ';
lenused = 2;
}
*out++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
}
}
if ( byte < stop ) {
for ( i = 0; byte + i < stop; i++ ) {
buf[i] = byte[i];
}
for ( pad = 0; i < 3; i++, pad++ ) {
buf[i] = '\0';
}
byte = buf;
bits = (byte[0] & 0xff) << 16;
bits |= (byte[1] & 0xff) << 8;
bits |= (byte[2] & 0xff);
for ( i = 0; i < 4; i++, bits <<= 6 ) {
if ( wraplen != -1 && lenused >= 0 && lenused++ > wraplen ) {
*out++ = '\n';
*out++ = ' ';
lenused = 2;
}
if (( i == 3 && pad > 0 ) || ( i == 2 && pad == 2 )) {
*out++ = '=';
} else {
*out++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
}
}
}
*out = '\0';
return( out - dst );
}
int
ldif_base64_encode( unsigned char *src, char *dst, int srclen, int lenused )
{
return ldif_base64_encode_internal( src, dst, srclen, lenused, LDIF_MAX_LINE_WIDTH );
}
int
ldif_base64_encode_nowrap( unsigned char *src, char *dst, int srclen, int lenused )
{
return ldif_base64_encode_internal( src, dst, srclen, lenused, -1 );
}
char *
ldif_type_and_value_with_options( char *type, char *val, int vlen,
unsigned long options )
{
char *buf, *p;
int tlen;
tlen = strlen( type );
if (( buf = (char *)malloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 )) !=
NULL ) {
p = buf;
ldif_put_type_and_value_with_options( &p, type, val, vlen, options );
*p = '\0';
}
return( buf );
}
char *
ldif_type_and_value( char *type, char *val, int vlen )
{
return ldif_type_and_value_with_options( type, val, vlen, 0 );
}
char *
ldif_type_and_value_nowrap( char *type, char *val, int vlen )
{
return ldif_type_and_value_with_options( type, val, vlen, LDIF_OPT_NOWRAP );
}
char *
ldif_get_entry( FILE *fp, int *lineno )
{
char line[BUFSIZ];
char *buf;
int max, cur, len, gotsome;
buf = NULL;
max = cur = gotsome = 0;
while ( fgets( line, sizeof(line), fp ) != NULL ) {
if ( lineno != NULL ) {
(*lineno)++;
}
if ( line[0] == '\0' || line[0] == '\n'
#if !defined( XP_WIN32 )
|| ( line[0] == '\r' && line[1] == '\n' )
#endif
) {
if ( gotsome ) {
break;
} else {
continue;
}
} else if ( line[0] == '#' ) {
continue;
}
gotsome = 1;
len = strlen( line );
#if !defined( XP_WIN32 )
if ( len > 0 && line[len-1] == '\r' ) {
--len;
line[len] = '\0';
} else if ( len > 1 && line[len-2] == '\r' && line[len-1] == '\n' ) {
--len;
line[len-1] = line[len];
line[len] = '\0';
}
#endif
while ( cur + (len + 1) > max ) {
if ( buf == NULL ) {
max += BUFSIZ;
buf = (char *) malloc( max );
} else {
max *= 2;
buf = (char *) realloc( buf, max );
}
if ( buf == NULL ) {
return( NULL );
}
}
memcpy( buf + cur, line, len + 1 );
cur += len;
}
return( buf );
}