/*
*/
/*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
/* line64.c - routines for dealing with the slapd line format */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#ifndef macintosh
#endif
#ifdef _WIN32
#include <windows.h>
#endif
#include "ldaplog.h"
#include "ldif.h"
#ifndef isascii
#endif
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
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
};
/*
* str_parse_line - takes a line of the form "type:[:] value" and splits it
* into components "type" and "value". if a double colon separates type from
* value, then value is encoded in base 64, and parse_line un-decodes it
* (in place) before returning.
*/
int
char *line,
char **type,
char **value,
int *vlen
)
{
char *p, *s, *d;
int b64;
/* skip any leading space */
line++;
}
for ( s = line; *s && *s != ':'; s++ )
; /* NULL */
if ( *s == '\0' ) {
/* Comment-out while we address calling libldif from ns-back-ldbm
on NT. 1 of 3 */
#if defined( _WIN32 )
/*
#endif
LDAPDebug( LDAP_DEBUG_PARSE, "str_parse_line: missing ':' "
"on line \"%s\"\n", line, 0, 0 );
#if defined( _WIN32 )
*/
#endif
return( -1 );
}
/* trim any space between type and : */
*p = '\0';
}
*s++ = '\0';
/* check for double : - indicates base 64 encoded value */
if ( *s == ':' ) {
s++;
b64 = 1;
/* single : - normally encoded value */
} else {
b64 = 0;
}
/* skip space between : and value */
while ( ISBLANK( *s ) ) {
s++;
}
/*
* If no value is present, return a zero-length string for
* *value, with *vlen set to zero.
*/
if ( *s == '\0' ) {
*value = s;
*vlen = 0;
return( 0 );
}
/* check for continued line markers that should be deleted */
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 ) {
/* Comment-out while we address calling libldif from ns-back-ldbm
on NT. 3 of 3 */
#if defined( _WIN32 )
/*
#endif
LDAPDebug( LDAP_DEBUG_ANY,
"str_parse_line: invalid base 64 char on line \"%s\"\n",
line, 0, 0 );
#if defined( _WIN32 )
*/
#endif
return( -1 );
}
s[ *vlen ] = '\0';
} else {
*vlen = (int) (d - s);
}
return( 0 );
}
/*
* ldif_base64_decode - take the BASE64-encoded characters in "src"
* (a zero-terminated string) and decode them into the the buffer "dst".
* "src" and "dst" can be the same if in-place decoding is desired.
* "dst" must be large enough to hold the decoded octets. No more than
* 3 * strlen( src ) / 4 bytes will be produced.
* "dst" may contain zero octets anywhere within it, but it is not
* zero-terminated by this function.
*
* The number of bytes copied to "dst" is returned if all goes well.
* -1 is returned if the BASE64 encoding in "src" is invalid.
*/
int
{
char *p, *stop;
int i, len;
for ( i = 0; i < 4; i++ ) {
if ( p[i] != '=' && (p[i] & 0x80 ||
return( -1 );
}
}
/* first digit */
/* second digit */
/* third digit */
if ( p[2] == '=' ) {
len += 1;
break;
}
/* fourth digit */
if ( p[3] == '=' ) {
len += 2;
break;
}
byte += 3;
}
return( len );
}
/*
* str_getline - return the next "line" (minus newline) of input from a
* string buffer of lines separated by newlines, terminated by \n\n
* or \0. this routine handles continued lines, bundling them into
* a single big line before returning. if a line begins with a white
* space character, it is a continuation of the previous line. the white
* space character (nb: only one char), and preceding newline are changed
* into CONTINUED_LINE_MARKER chars, to be deleted later by the
* str_parse_line() routine above.
*
* it takes a pointer to a pointer to the buffer on the first call,
* which it updates and must be supplied on subsequent calls.
*
* XXX need to update this function to also support <CR><LF> as EOL.
* XXX supports <CR><LF> as of 07/29/1998 (richm)
*/
char *
{
char *l;
char c;
char *p;
return( NULL );
}
return( NULL );
}
(*next)++;
}
l = *next;
if ( ISBLANK( c ) && c != '\n' ) {
/* DOS EOL is \r\n, so if the character before */
/* the \n is \r, continue it too */
if (*p == '\r')
*p = CONTINUED_LINE_MARKER;
} else {
/* DOS EOL is \r\n, so if the character before */
/* the \n is \r, null it too */
if (*p == '\r')
*p = '\0';
*(*next)++ = '\0';
break;
}
(*next)++;
}
return( l );
}
&& ( isprint((c)) || (c) == '\t' ))
&& (c) != ' ' && (c) != '<' )
void
{
char *save;
len = 0;
wraplen = -1;
} else {
}
/* put the type + ": " */
for ( p = (unsigned char *) t; *p; p++, len++ ) {
*(*out)++ = *p;
}
*(*out)++ = ':';
len++;
len++;
}
b64 = 0;
*(*out)++ = ' ';
if ( !LDIF_SAFE_INITCHAR( val[0] )) {
b64 = 1;
}
} else {
if ( !LDIF_CONSERVATIVE_INITCHAR( val[0] ) ||
b64 = 1;
}
}
}
if ( !b64 ) {
if ( LDIF_OPT_ISSET( options,
if ( !LDIF_SAFE_CHAR( *byte )) {
b64 = 1;
break;
}
} else if ( !LDIF_CONSERVATIVE_CHAR( *byte )) {
b64 = 1;
break;
}
*(*out)++ = '\n';
*(*out)++ = ' ';
len = 1;
}
}
}
if ( b64 ) {
*(*out)++ = ':';
*(*out)++ = ' ';
}
*(*out)++ = '\n';
}
void
{
}
void
{
}
/*
* ldif_base64_encode_internal - encode "srclen" bytes in "src", place BASE64
* encoded bytes in "dst" and return the length of the BASE64
* encoded string. "dst" is also zero-terminated by this function.
*
* If "lenused" >= 0, newlines will be included in "dst" and "lenused" if
* appropriate. "lenused" should be a count of characters already used
* on the current line. The LDIF lines we create will contain at most
* "wraplen" characters on each line, unless "wraplen" is -1, in which
* case output line length is unlimited.
*
* If "lenused" < 0, no newlines will be included, and the LDIF_BASE64_LEN()
* macro can be used to determine how many bytes will be placed in "dst."
*/
static int
{
char *out;
unsigned long bits;
len = 0;
/* convert to base 64 (3 bytes => 4 base 64 digits) */
*out++ = '\n';
*out++ = ' ';
lenused = 2;
}
/* get b64 digit from high order 6 bits */
}
}
/* add padding if necessary */
}
buf[i] = '\0';
}
*out++ = '\n';
*out++ = ' ';
lenused = 2;
}
/* Pad as appropriate */
*out++ = '=';
} else {
/* get b64 digit from low order 6 bits */
}
}
}
*out = '\0';
}
int
{
}
int
{
}
/*
* return malloc'd, zero-terminated LDIF line
*/
char *
unsigned long options )
{
char *buf, *p;
int tlen;
NULL ) {
p = buf;
*p = '\0';
}
return( buf );
}
char *
{
}
char *
{
}
/*
* ldif_get_entry - read the next ldif entry from the FILE referenced
* by fp. return a pointer to a malloc'd, null-terminated buffer. also
* returned is the last line number read, in *lineno.
*/
char *
{
char *buf;
(*lineno)++;
}
/* ldif entries are terminated by a \n on a line by itself */
#if !defined( XP_WIN32 )
#endif
) {
if ( gotsome ) {
break;
} else {
continue;
}
} else if ( line[0] == '#' ) {
continue;
}
gotsome = 1;
#if !defined( XP_WIN32 )
/* DOS format */
--len;
--len;
}
#endif
} else {
max *= 2;
}
return( NULL );
}
}
}
return( buf );
}