17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright 2001-2002 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
87c478bd9Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
97c478bd9Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
107c478bd9Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
117c478bd9Sstevel@tonic-gate *
127c478bd9Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
137c478bd9Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
147c478bd9Sstevel@tonic-gate * implied. See the License for the specific language governing
157c478bd9Sstevel@tonic-gate * rights and limitations under the License.
167c478bd9Sstevel@tonic-gate *
177c478bd9Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
187c478bd9Sstevel@tonic-gate * March 31, 1998.
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
217c478bd9Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
227c478bd9Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
237c478bd9Sstevel@tonic-gate * Rights Reserved.
247c478bd9Sstevel@tonic-gate *
257c478bd9Sstevel@tonic-gate * Contributor(s):
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /* line64.c - routines for dealing with the slapd line format */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #ifndef macintosh
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate #ifdef _WIN32
387c478bd9Sstevel@tonic-gate #include <windows.h>
397c478bd9Sstevel@tonic-gate #elif !defined( macintosh )
407c478bd9Sstevel@tonic-gate #include <sys/socket.h>
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate #include "ldaplog.h"
437c478bd9Sstevel@tonic-gate #include "ldif.h"
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #ifndef isascii
467c478bd9Sstevel@tonic-gate #define isascii( c ) (!((c) & ~0177))
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #define RIGHT2 0x03
507c478bd9Sstevel@tonic-gate #define RIGHT4 0x0f
517c478bd9Sstevel@tonic-gate #define CONTINUED_LINE_MARKER '\001'
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #define ISBLANK(c) ((c) == ' ' || (c) == '\t' || (c) == '\n') /* not "\r\v\f" */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #define LDIF_OPT_ISSET( value, opt ) (((value) & (opt)) != 0 )
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate static char nib2b64[0x40] =
587c478bd9Sstevel@tonic-gate "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate static unsigned char b642nib[0x80] = {
617c478bd9Sstevel@tonic-gate 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
627c478bd9Sstevel@tonic-gate 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
637c478bd9Sstevel@tonic-gate 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
647c478bd9Sstevel@tonic-gate 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
657c478bd9Sstevel@tonic-gate 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
667c478bd9Sstevel@tonic-gate 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
677c478bd9Sstevel@tonic-gate 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
687c478bd9Sstevel@tonic-gate 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
697c478bd9Sstevel@tonic-gate 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
707c478bd9Sstevel@tonic-gate 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
717c478bd9Sstevel@tonic-gate 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
727c478bd9Sstevel@tonic-gate 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
737c478bd9Sstevel@tonic-gate 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
747c478bd9Sstevel@tonic-gate 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
757c478bd9Sstevel@tonic-gate 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
767c478bd9Sstevel@tonic-gate 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate static int ldif_base64_encode_internal( unsigned char *src, char *dst, int srclen,
807c478bd9Sstevel@tonic-gate int lenused, int wraplen );
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate * str_parse_line - takes a line of the form "type:[:] value" and splits it
847c478bd9Sstevel@tonic-gate * into components "type" and "value". if a double colon separates type from
857c478bd9Sstevel@tonic-gate * value, then value is encoded in base 64, and parse_line un-decodes it
867c478bd9Sstevel@tonic-gate * (in place) before returning.
877c478bd9Sstevel@tonic-gate */
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate int
str_parse_line(char * line,char ** type,char ** value,int * vlen)907c478bd9Sstevel@tonic-gate str_parse_line(
917c478bd9Sstevel@tonic-gate char *line,
927c478bd9Sstevel@tonic-gate char **type,
937c478bd9Sstevel@tonic-gate char **value,
947c478bd9Sstevel@tonic-gate int *vlen
957c478bd9Sstevel@tonic-gate )
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate char *p, *s, *d;
987c478bd9Sstevel@tonic-gate int b64;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /* skip any leading space */
1017c478bd9Sstevel@tonic-gate while ( ISBLANK( *line ) ) {
1027c478bd9Sstevel@tonic-gate line++;
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate *type = line;
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate for ( s = line; *s && *s != ':'; s++ )
1077c478bd9Sstevel@tonic-gate ; /* NULL */
1087c478bd9Sstevel@tonic-gate if ( *s == '\0' ) {
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate /* Comment-out while we address calling libldif from ns-back-ldbm
1117c478bd9Sstevel@tonic-gate on NT. 1 of 3 */
1127c478bd9Sstevel@tonic-gate #if defined( _WIN32 )
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_PARSE, "str_parse_line: missing ':' "
116*1da57d55SToomas Soome "on line \"%s\"\n", line, 0, 0 );
1177c478bd9Sstevel@tonic-gate #if defined( _WIN32 )
1187c478bd9Sstevel@tonic-gate */
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate return( -1 );
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /* trim any space between type and : */
1247c478bd9Sstevel@tonic-gate for ( p = s - 1; p > line && ISBLANK( *p ); p-- ) {
1257c478bd9Sstevel@tonic-gate *p = '\0';
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate *s++ = '\0';
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate /* check for double : - indicates base 64 encoded value */
1307c478bd9Sstevel@tonic-gate if ( *s == ':' ) {
1317c478bd9Sstevel@tonic-gate s++;
1327c478bd9Sstevel@tonic-gate b64 = 1;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate /* single : - normally encoded value */
1357c478bd9Sstevel@tonic-gate } else {
1367c478bd9Sstevel@tonic-gate b64 = 0;
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate /* skip space between : and value */
1407c478bd9Sstevel@tonic-gate while ( ISBLANK( *s ) ) {
1417c478bd9Sstevel@tonic-gate s++;
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
144*1da57d55SToomas Soome /*
1457c478bd9Sstevel@tonic-gate * If no value is present, return a zero-length string for
1467c478bd9Sstevel@tonic-gate * *value, with *vlen set to zero.
1477c478bd9Sstevel@tonic-gate */
1487c478bd9Sstevel@tonic-gate if ( *s == '\0' ) {
1497c478bd9Sstevel@tonic-gate *value = s;
1507c478bd9Sstevel@tonic-gate *vlen = 0;
1517c478bd9Sstevel@tonic-gate return( 0 );
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate /* check for continued line markers that should be deleted */
1557c478bd9Sstevel@tonic-gate for ( p = s, d = s; *p; p++ ) {
1567c478bd9Sstevel@tonic-gate if ( *p != CONTINUED_LINE_MARKER )
1577c478bd9Sstevel@tonic-gate *d++ = *p;
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate *d = '\0';
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate *value = s;
1627c478bd9Sstevel@tonic-gate if ( b64 ) {
1637c478bd9Sstevel@tonic-gate if (( *vlen = ldif_base64_decode( s, (unsigned char *)s ))
1647c478bd9Sstevel@tonic-gate < 0 ) {
1657c478bd9Sstevel@tonic-gate /* Comment-out while we address calling libldif from ns-back-ldbm
1667c478bd9Sstevel@tonic-gate on NT. 3 of 3 */
1677c478bd9Sstevel@tonic-gate #if defined( _WIN32 )
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate #endif
1707c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_ANY,
1717c478bd9Sstevel@tonic-gate "str_parse_line: invalid base 64 char on line \"%s\"\n",
172*1da57d55SToomas Soome line, 0, 0 );
1737c478bd9Sstevel@tonic-gate #if defined( _WIN32 )
1747c478bd9Sstevel@tonic-gate */
1757c478bd9Sstevel@tonic-gate #endif
1767c478bd9Sstevel@tonic-gate return( -1 );
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate s[ *vlen ] = '\0';
1797c478bd9Sstevel@tonic-gate } else {
1807c478bd9Sstevel@tonic-gate *vlen = (int) (d - s);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate return( 0 );
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * ldif_base64_decode - take the BASE64-encoded characters in "src"
1897c478bd9Sstevel@tonic-gate * (a zero-terminated string) and decode them into the the buffer "dst".
1907c478bd9Sstevel@tonic-gate * "src" and "dst" can be the same if in-place decoding is desired.
1917c478bd9Sstevel@tonic-gate * "dst" must be large enough to hold the decoded octets. No more than
1927c478bd9Sstevel@tonic-gate * 3 * strlen( src ) / 4 bytes will be produced.
1937c478bd9Sstevel@tonic-gate * "dst" may contain zero octets anywhere within it, but it is not
1947c478bd9Sstevel@tonic-gate * zero-terminated by this function.
1957c478bd9Sstevel@tonic-gate *
1967c478bd9Sstevel@tonic-gate * The number of bytes copied to "dst" is returned if all goes well.
1977c478bd9Sstevel@tonic-gate * -1 is returned if the BASE64 encoding in "src" is invalid.
1987c478bd9Sstevel@tonic-gate */
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate int
ldif_base64_decode(char * src,unsigned char * dst)2017c478bd9Sstevel@tonic-gate ldif_base64_decode( char *src, unsigned char *dst )
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate char *p, *stop;
2047c478bd9Sstevel@tonic-gate unsigned char nib, *byte;
2057c478bd9Sstevel@tonic-gate int i, len;
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate stop = strchr( src, '\0' );
2087c478bd9Sstevel@tonic-gate byte = dst;
2097c478bd9Sstevel@tonic-gate for ( p = src, len = 0; p < stop; p += 4, len += 3 ) {
2107c478bd9Sstevel@tonic-gate for ( i = 0; i < 4; i++ ) {
2117c478bd9Sstevel@tonic-gate if ( p[i] != '=' && (p[i] & 0x80 ||
2127c478bd9Sstevel@tonic-gate b642nib[ p[i] & 0x7f ] > 0x3f) ) {
2137c478bd9Sstevel@tonic-gate return( -1 );
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate /* first digit */
2187c478bd9Sstevel@tonic-gate nib = b642nib[ p[0] & 0x7f ];
2197c478bd9Sstevel@tonic-gate byte[0] = nib << 2;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /* second digit */
2227c478bd9Sstevel@tonic-gate nib = b642nib[ p[1] & 0x7f ];
2237c478bd9Sstevel@tonic-gate byte[0] |= nib >> 4;
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /* third digit */
2267c478bd9Sstevel@tonic-gate if ( p[2] == '=' ) {
2277c478bd9Sstevel@tonic-gate len += 1;
2287c478bd9Sstevel@tonic-gate break;
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate byte[1] = (nib & RIGHT4) << 4;
2317c478bd9Sstevel@tonic-gate nib = b642nib[ p[2] & 0x7f ];
2327c478bd9Sstevel@tonic-gate byte[1] |= nib >> 2;
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate /* fourth digit */
2357c478bd9Sstevel@tonic-gate if ( p[3] == '=' ) {
2367c478bd9Sstevel@tonic-gate len += 2;
2377c478bd9Sstevel@tonic-gate break;
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate byte[2] = (nib & RIGHT2) << 6;
2407c478bd9Sstevel@tonic-gate nib = b642nib[ p[3] & 0x7f ];
2417c478bd9Sstevel@tonic-gate byte[2] |= nib;
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate byte += 3;
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate return( len );
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate * str_getline - return the next "line" (minus newline) of input from a
2517c478bd9Sstevel@tonic-gate * string buffer of lines separated by newlines, terminated by \n\n
2527c478bd9Sstevel@tonic-gate * or \0. this routine handles continued lines, bundling them into
2537c478bd9Sstevel@tonic-gate * a single big line before returning. if a line begins with a white
2547c478bd9Sstevel@tonic-gate * space character, it is a continuation of the previous line. the white
2557c478bd9Sstevel@tonic-gate * space character (nb: only one char), and preceeding newline are changed
2567c478bd9Sstevel@tonic-gate * into CONTINUED_LINE_MARKER chars, to be deleted later by the
2577c478bd9Sstevel@tonic-gate * str_parse_line() routine above.
2587c478bd9Sstevel@tonic-gate *
2597c478bd9Sstevel@tonic-gate * it takes a pointer to a pointer to the buffer on the first call,
2607c478bd9Sstevel@tonic-gate * which it updates and must be supplied on subsequent calls.
2617c478bd9Sstevel@tonic-gate *
2627c478bd9Sstevel@tonic-gate * XXX need to update this function to also support <CR><LF> as EOL.
2637c478bd9Sstevel@tonic-gate * XXX supports <CR><LF> as of 07/29/1998 (richm)
2647c478bd9Sstevel@tonic-gate */
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate char *
str_getline(char ** next)2677c478bd9Sstevel@tonic-gate str_getline( char **next )
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate char *l;
2707c478bd9Sstevel@tonic-gate char c;
2717c478bd9Sstevel@tonic-gate char *p;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate if ( *next == NULL || **next == '\n' || **next == '\0' ) {
2747c478bd9Sstevel@tonic-gate return( NULL );
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate while ( **next == '#' ) { /* skip comment lines */
2787c478bd9Sstevel@tonic-gate if (( *next = strchr( *next, '\n' )) == NULL ) {
2797c478bd9Sstevel@tonic-gate return( NULL );
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate (*next)++;
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate l = *next;
2857c478bd9Sstevel@tonic-gate while ( (*next = strchr( *next, '\n' )) != NULL ) {
2867c478bd9Sstevel@tonic-gate p = *next - 1; /* pointer to character previous to the newline */
2877c478bd9Sstevel@tonic-gate c = *(*next + 1); /* character after the newline */
2887c478bd9Sstevel@tonic-gate if ( ISBLANK( c ) && c != '\n' ) {
2897c478bd9Sstevel@tonic-gate /* DOS EOL is \r\n, so if the character before */
2907c478bd9Sstevel@tonic-gate /* the \n is \r, continue it too */
2917c478bd9Sstevel@tonic-gate if (*p == '\r')
2927c478bd9Sstevel@tonic-gate *p = CONTINUED_LINE_MARKER;
2937c478bd9Sstevel@tonic-gate **next = CONTINUED_LINE_MARKER;
2947c478bd9Sstevel@tonic-gate *(*next+1) = CONTINUED_LINE_MARKER;
2957c478bd9Sstevel@tonic-gate } else {
2967c478bd9Sstevel@tonic-gate /* DOS EOL is \r\n, so if the character before */
2977c478bd9Sstevel@tonic-gate /* the \n is \r, null it too */
2987c478bd9Sstevel@tonic-gate if (*p == '\r')
2997c478bd9Sstevel@tonic-gate *p = '\0';
3007c478bd9Sstevel@tonic-gate *(*next)++ = '\0';
3017c478bd9Sstevel@tonic-gate break;
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate (*next)++;
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate return( l );
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate #define LDIF_SAFE_CHAR( c ) ( (c) != '\r' && (c) != '\n' )
3117c478bd9Sstevel@tonic-gate #define LDIF_CONSERVATIVE_CHAR( c ) ( LDIF_SAFE_CHAR(c) && isascii((c)) \
3127c478bd9Sstevel@tonic-gate && ( isprint((c)) || (c) == '\t' ))
3137c478bd9Sstevel@tonic-gate #define LDIF_SAFE_INITCHAR( c ) ( LDIF_SAFE_CHAR(c) && (c) != ':' \
3147c478bd9Sstevel@tonic-gate && (c) != ' ' && (c) != '<' )
3157c478bd9Sstevel@tonic-gate #define LDIF_CONSERVATIVE_INITCHAR( c ) ( LDIF_SAFE_INITCHAR( c ) && \
3167c478bd9Sstevel@tonic-gate ! ( isascii((c)) && isspace((c))))
3177c478bd9Sstevel@tonic-gate #define LDIF_CONSERVATIVE_FINALCHAR( c ) ( (c) != ' ' )
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate void
ldif_put_type_and_value_with_options(char ** out,char * t,char * val,int vlen,unsigned long options)3217c478bd9Sstevel@tonic-gate ldif_put_type_and_value_with_options( char **out, char *t, char *val,
3227c478bd9Sstevel@tonic-gate int vlen, unsigned long options )
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate unsigned char *p, *byte, *stop;
3257c478bd9Sstevel@tonic-gate char *save;
3267c478bd9Sstevel@tonic-gate int b64, len, savelen, wraplen;
3277c478bd9Sstevel@tonic-gate len = 0;
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate if ( LDIF_OPT_ISSET( options, LDIF_OPT_NOWRAP )) {
3307c478bd9Sstevel@tonic-gate wraplen = -1;
3317c478bd9Sstevel@tonic-gate } else {
3327c478bd9Sstevel@tonic-gate wraplen = LDIF_MAX_LINE_WIDTH;
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate /* put the type + ": " */
3367c478bd9Sstevel@tonic-gate for ( p = (unsigned char *) t; *p; p++, len++ ) {
3377c478bd9Sstevel@tonic-gate *(*out)++ = *p;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate *(*out)++ = ':';
3407c478bd9Sstevel@tonic-gate len++;
3417c478bd9Sstevel@tonic-gate if ( LDIF_OPT_ISSET( options, LDIF_OPT_VALUE_IS_URL )) {
3427c478bd9Sstevel@tonic-gate *(*out)++ = '<'; /* add '<' for URLs */
3437c478bd9Sstevel@tonic-gate len++;
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate save = *out;
3467c478bd9Sstevel@tonic-gate savelen = len;
3477c478bd9Sstevel@tonic-gate b64 = 0;
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate stop = (unsigned char *)val;
3507c478bd9Sstevel@tonic-gate if ( val && vlen > 0 ) {
3517c478bd9Sstevel@tonic-gate *(*out)++ = ' ';
3527c478bd9Sstevel@tonic-gate stop = (unsigned char *) (val + vlen);
3537c478bd9Sstevel@tonic-gate if ( LDIF_OPT_ISSET( options, LDIF_OPT_MINIMAL_ENCODING )) {
3547c478bd9Sstevel@tonic-gate if ( !LDIF_SAFE_INITCHAR( val[0] )) {
3557c478bd9Sstevel@tonic-gate b64 = 1;
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate } else {
3587c478bd9Sstevel@tonic-gate if ( !LDIF_CONSERVATIVE_INITCHAR( val[0] ) ||
3597c478bd9Sstevel@tonic-gate !LDIF_CONSERVATIVE_FINALCHAR( val[vlen-1] )) {
3607c478bd9Sstevel@tonic-gate b64 = 1;
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate if ( !b64 ) {
3667c478bd9Sstevel@tonic-gate for ( byte = (unsigned char *) val; byte < stop;
3677c478bd9Sstevel@tonic-gate byte++, len++ ) {
3687c478bd9Sstevel@tonic-gate if ( LDIF_OPT_ISSET( options,
3697c478bd9Sstevel@tonic-gate LDIF_OPT_MINIMAL_ENCODING )) {
3707c478bd9Sstevel@tonic-gate if ( !LDIF_SAFE_CHAR( *byte )) {
3717c478bd9Sstevel@tonic-gate b64 = 1;
3727c478bd9Sstevel@tonic-gate break;
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate } else if ( !LDIF_CONSERVATIVE_CHAR( *byte )) {
3757c478bd9Sstevel@tonic-gate b64 = 1;
3767c478bd9Sstevel@tonic-gate break;
3777c478bd9Sstevel@tonic-gate }
378*1da57d55SToomas Soome
3797c478bd9Sstevel@tonic-gate if ( wraplen != -1 && len > wraplen ) {
3807c478bd9Sstevel@tonic-gate *(*out)++ = '\n';
3817c478bd9Sstevel@tonic-gate *(*out)++ = ' ';
3827c478bd9Sstevel@tonic-gate len = 1;
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate *(*out)++ = *byte;
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate if ( b64 ) {
3897c478bd9Sstevel@tonic-gate *out = save;
3907c478bd9Sstevel@tonic-gate *(*out)++ = ':';
3917c478bd9Sstevel@tonic-gate *(*out)++ = ' ';
3927c478bd9Sstevel@tonic-gate len = ldif_base64_encode_internal( (unsigned char *)val, *out, vlen,
3937c478bd9Sstevel@tonic-gate savelen + 2, wraplen );
3947c478bd9Sstevel@tonic-gate *out += len;
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate *(*out)++ = '\n';
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate
400*1da57d55SToomas Soome void
ldif_put_type_and_value(char ** out,char * t,char * val,int vlen)4017c478bd9Sstevel@tonic-gate ldif_put_type_and_value( char **out, char *t, char *val, int vlen )
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate ldif_put_type_and_value_with_options( out, t, val, vlen, 0 );
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate
406*1da57d55SToomas Soome void
ldif_put_type_and_value_nowrap(char ** out,char * t,char * val,int vlen)4077c478bd9Sstevel@tonic-gate ldif_put_type_and_value_nowrap( char **out, char *t, char *val, int vlen )
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate ldif_put_type_and_value_with_options( out, t, val, vlen, LDIF_OPT_NOWRAP );
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate * ldif_base64_encode_internal - encode "srclen" bytes in "src", place BASE64
4147c478bd9Sstevel@tonic-gate * encoded bytes in "dst" and return the length of the BASE64
4157c478bd9Sstevel@tonic-gate * encoded string. "dst" is also zero-terminated by this function.
4167c478bd9Sstevel@tonic-gate *
4177c478bd9Sstevel@tonic-gate * If "lenused" >= 0, newlines will be included in "dst" and "lenused" if
4187c478bd9Sstevel@tonic-gate * appropriate. "lenused" should be a count of characters already used
4197c478bd9Sstevel@tonic-gate * on the current line. The LDIF lines we create will contain at most
4207c478bd9Sstevel@tonic-gate * "wraplen" characters on each line, unless "wraplen" is -1, in which
4217c478bd9Sstevel@tonic-gate * case output line length is unlimited.
4227c478bd9Sstevel@tonic-gate *
4237c478bd9Sstevel@tonic-gate * If "lenused" < 0, no newlines will be included, and the LDIF_BASE64_LEN()
4247c478bd9Sstevel@tonic-gate * macro can be used to determine how many bytes will be placed in "dst."
4257c478bd9Sstevel@tonic-gate */
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate static int
ldif_base64_encode_internal(unsigned char * src,char * dst,int srclen,int lenused,int wraplen)4287c478bd9Sstevel@tonic-gate ldif_base64_encode_internal( unsigned char *src, char *dst, int srclen, int lenused, int wraplen )
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate unsigned char *byte, *stop;
4317c478bd9Sstevel@tonic-gate unsigned char buf[3];
4327c478bd9Sstevel@tonic-gate char *out;
4337c478bd9Sstevel@tonic-gate unsigned long bits;
4347c478bd9Sstevel@tonic-gate int i, pad, len;
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate len = 0;
4377c478bd9Sstevel@tonic-gate out = dst;
4387c478bd9Sstevel@tonic-gate stop = src + srclen;
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate /* convert to base 64 (3 bytes => 4 base 64 digits) */
4417c478bd9Sstevel@tonic-gate for ( byte = src; byte < stop - 2; byte += 3 ) {
4427c478bd9Sstevel@tonic-gate bits = (byte[0] & 0xff) << 16;
4437c478bd9Sstevel@tonic-gate bits |= (byte[1] & 0xff) << 8;
4447c478bd9Sstevel@tonic-gate bits |= (byte[2] & 0xff);
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate for ( i = 0; i < 4; i++, bits <<= 6 ) {
4477c478bd9Sstevel@tonic-gate if ( wraplen != -1 && lenused >= 0 && lenused++ > wraplen ) {
4487c478bd9Sstevel@tonic-gate *out++ = '\n';
4497c478bd9Sstevel@tonic-gate *out++ = ' ';
4507c478bd9Sstevel@tonic-gate lenused = 2;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate /* get b64 digit from high order 6 bits */
4547c478bd9Sstevel@tonic-gate *out++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate /* add padding if necessary */
4597c478bd9Sstevel@tonic-gate if ( byte < stop ) {
4607c478bd9Sstevel@tonic-gate for ( i = 0; byte + i < stop; i++ ) {
4617c478bd9Sstevel@tonic-gate buf[i] = byte[i];
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate for ( pad = 0; i < 3; i++, pad++ ) {
4647c478bd9Sstevel@tonic-gate buf[i] = '\0';
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate byte = buf;
4677c478bd9Sstevel@tonic-gate bits = (byte[0] & 0xff) << 16;
4687c478bd9Sstevel@tonic-gate bits |= (byte[1] & 0xff) << 8;
4697c478bd9Sstevel@tonic-gate bits |= (byte[2] & 0xff);
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate for ( i = 0; i < 4; i++, bits <<= 6 ) {
4727c478bd9Sstevel@tonic-gate if ( wraplen != -1 && lenused >= 0 && lenused++ > wraplen ) {
4737c478bd9Sstevel@tonic-gate *out++ = '\n';
4747c478bd9Sstevel@tonic-gate *out++ = ' ';
4757c478bd9Sstevel@tonic-gate lenused = 2;
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate
4787c478bd9Sstevel@tonic-gate if (( i == 3 && pad > 0 ) || ( i == 2 && pad == 2 )) {
4797c478bd9Sstevel@tonic-gate /* Pad as appropriate */
4807c478bd9Sstevel@tonic-gate *out++ = '=';
4817c478bd9Sstevel@tonic-gate } else {
4827c478bd9Sstevel@tonic-gate /* get b64 digit from low order 6 bits */
4837c478bd9Sstevel@tonic-gate *out++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate *out = '\0';
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate return( out - dst );
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate int
ldif_base64_encode(unsigned char * src,char * dst,int srclen,int lenused)4947c478bd9Sstevel@tonic-gate ldif_base64_encode( unsigned char *src, char *dst, int srclen, int lenused )
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate return ldif_base64_encode_internal( src, dst, srclen, lenused, LDIF_MAX_LINE_WIDTH );
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate int
ldif_base64_encode_nowrap(unsigned char * src,char * dst,int srclen,int lenused)5007c478bd9Sstevel@tonic-gate ldif_base64_encode_nowrap( unsigned char *src, char *dst, int srclen, int lenused )
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate return ldif_base64_encode_internal( src, dst, srclen, lenused, -1 );
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate * return malloc'd, zero-terminated LDIF line
5087c478bd9Sstevel@tonic-gate */
5097c478bd9Sstevel@tonic-gate char *
ldif_type_and_value_with_options(char * type,char * val,int vlen,unsigned long options)5107c478bd9Sstevel@tonic-gate ldif_type_and_value_with_options( char *type, char *val, int vlen,
5117c478bd9Sstevel@tonic-gate unsigned long options )
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate char *buf, *p;
5147c478bd9Sstevel@tonic-gate int tlen;
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate tlen = strlen( type );
5177c478bd9Sstevel@tonic-gate if (( buf = (char *)malloc( LDIF_SIZE_NEEDED( tlen, vlen ) + 1 )) !=
5187c478bd9Sstevel@tonic-gate NULL ) {
5197c478bd9Sstevel@tonic-gate p = buf;
5207c478bd9Sstevel@tonic-gate ldif_put_type_and_value_with_options( &p, type, val, vlen, options );
5217c478bd9Sstevel@tonic-gate *p = '\0';
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate return( buf );
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate char *
ldif_type_and_value(char * type,char * val,int vlen)5287c478bd9Sstevel@tonic-gate ldif_type_and_value( char *type, char *val, int vlen )
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate return ldif_type_and_value_with_options( type, val, vlen, 0 );
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate
5337c478bd9Sstevel@tonic-gate char *
ldif_type_and_value_nowrap(char * type,char * val,int vlen)5347c478bd9Sstevel@tonic-gate ldif_type_and_value_nowrap( char *type, char *val, int vlen )
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate return ldif_type_and_value_with_options( type, val, vlen, LDIF_OPT_NOWRAP );
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate * ldif_get_entry - read the next ldif entry from the FILE referenced
5417c478bd9Sstevel@tonic-gate * by fp. return a pointer to a malloc'd, null-terminated buffer. also
5427c478bd9Sstevel@tonic-gate * returned is the last line number read, in *lineno.
5437c478bd9Sstevel@tonic-gate */
5447c478bd9Sstevel@tonic-gate char *
ldif_get_entry(FILE * fp,int * lineno)5457c478bd9Sstevel@tonic-gate ldif_get_entry( FILE *fp, int *lineno )
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate char line[BUFSIZ];
5487c478bd9Sstevel@tonic-gate char *buf;
5497c478bd9Sstevel@tonic-gate int max, cur, len, gotsome;
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate buf = NULL;
5527c478bd9Sstevel@tonic-gate max = cur = gotsome = 0;
5537c478bd9Sstevel@tonic-gate while ( fgets( line, sizeof(line), fp ) != NULL ) {
5547c478bd9Sstevel@tonic-gate if ( lineno != NULL ) {
5557c478bd9Sstevel@tonic-gate (*lineno)++;
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate /* ldif entries are terminated by a \n on a line by itself */
5587c478bd9Sstevel@tonic-gate if ( line[0] == '\0' || line[0] == '\n'
5597c478bd9Sstevel@tonic-gate #if !defined( XP_WIN32 )
5607c478bd9Sstevel@tonic-gate || ( line[0] == '\r' && line[1] == '\n' ) /* DOS format */
5617c478bd9Sstevel@tonic-gate #endif
5627c478bd9Sstevel@tonic-gate ) {
5637c478bd9Sstevel@tonic-gate if ( gotsome ) {
5647c478bd9Sstevel@tonic-gate break;
5657c478bd9Sstevel@tonic-gate } else {
5667c478bd9Sstevel@tonic-gate continue;
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate } else if ( line[0] == '#' ) {
5697c478bd9Sstevel@tonic-gate continue;
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate gotsome = 1;
5727c478bd9Sstevel@tonic-gate len = strlen( line );
5737c478bd9Sstevel@tonic-gate #if !defined( XP_WIN32 )
5747c478bd9Sstevel@tonic-gate /* DOS format */
5757c478bd9Sstevel@tonic-gate if ( len > 0 && line[len-1] == '\r' ) {
5767c478bd9Sstevel@tonic-gate --len;
5777c478bd9Sstevel@tonic-gate line[len] = '\0';
5787c478bd9Sstevel@tonic-gate } else if ( len > 1 && line[len-2] == '\r' && line[len-1] == '\n' ) {
5797c478bd9Sstevel@tonic-gate --len;
5807c478bd9Sstevel@tonic-gate line[len-1] = line[len];
5817c478bd9Sstevel@tonic-gate line[len] = '\0';
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate #endif
5847c478bd9Sstevel@tonic-gate while ( cur + (len + 1) > max ) {
5857c478bd9Sstevel@tonic-gate if ( buf == NULL ) {
5867c478bd9Sstevel@tonic-gate max += BUFSIZ;
5877c478bd9Sstevel@tonic-gate buf = (char *) malloc( max );
5887c478bd9Sstevel@tonic-gate } else {
5897c478bd9Sstevel@tonic-gate max *= 2;
5907c478bd9Sstevel@tonic-gate buf = (char *) realloc( buf, max );
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate if ( buf == NULL ) {
5937c478bd9Sstevel@tonic-gate return( NULL );
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate memcpy( buf + cur, line, len + 1 );
5987c478bd9Sstevel@tonic-gate cur += len;
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate return( buf );
6027c478bd9Sstevel@tonic-gate }
603