Project

General

Profile

Feature #2705 ยป eofldap4.patch

suggested fix - Milan Jurik, 2012-05-18 10:06 AM

View differences:

new/usr/src/cmd/abi/appcert/etc/etc.warn.in Fri May 18 11:52:09 2012
1 1
#
2
# ident	"%Z%%M%	%I%	%E% SMI"
3
#
4 2
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
5 3
# Use is subject to license terms.
6 4
#
......
77 75
###############################################################################
78 76
WARNING:libldap.so.3:gettext("calls obsolete LDAP library libldap.so.3"):libldap.so.3|*|*DIRECT*:
79 77
###############################################################################
78
WARNING:libldap.so.4:gettext("calls obsolete LDAP library libldap.so.4"):libldap.so.4|*|*DIRECT*:
79
###############################################################################
80 80
WARNING:libxfn:gettext("calls obsolete federated naming library libxfn.so"):libxfn.so.1|*|*DIRECT*,libxfn.so.2|*|*DIRECT*:
81 81
###############################################################################
82 82
WARNING:libXinput:gettext("calls obsolete input library libXinput.so.0"):libXinput.so.0|*|*DIRECT*:
83
-- old/usr/src/lib/Makefile	Fri May 18 11:52:10 2012
83
++ new/usr/src/lib/Makefile	Fri May 18 11:52:10 2012
......
125 125
	libumem		\
126 126
	libnvpair	.WAIT	\
127 127
	libexacct	\
128
	libldap4	\
129 128
	libsasl		\
130 129
	libldap5	\
131 130
	libsldap	.WAIT	\
132
-- old/usr/src/lib/libldap4/Makefile	Fri May 18 11:52:11 2012
131
++ /dev/null	Fri May 18 05:11:28 2012
......
1
#
2
# CDDL HEADER START
3
#
4
# The contents of this file are subject to the terms of the
5
# Common Development and Distribution License (the "License").
6
# You may not use this file except in compliance with the License.
7
#
8
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9
# or http://www.opensolaris.org/os/licensing.
10
# See the License for the specific language governing permissions
11
# and limitations under the License.
12
#
13
# When distributing Covered Code, include this CDDL HEADER in each
14
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15
# If applicable, add the following below this CDDL HEADER, with the
16
# fields enclosed by brackets "[]" replaced with your own identifying
17
# information: Portions Copyright [yyyy] [name of copyright owner]
18
#
19
# CDDL HEADER END
20
#
21
#
22
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23
# Use is subject to license terms.
24
#
25
# ident	"%Z%%M%	%I%	%E% SMI"
26
#
27

  
28
include			../Makefile.lib
29

  
30
SUBDIRS =	$(MACH)
31
$(BUILD64)SUBDIRS += $(MACH64)
32

  
33
all :=		TARGET= all
34
clean :=	TARGET= clean
35
clobber :=	TARGET= clobber
36
delete :=	TARGET= delete
37
install :=	TARGET= install
38
lint :=		TARGET= lint
39
catalog :=	TARGET= catalog
40
package :=	TARGET= package
41

  
42
.KEEP_STATE:
43

  
44
all clean clobber delete install lint catalog package: $(SUBDIRS)
45

  
46
# install rule for install_h target
47
$(ROOTHDRDIR)/%: %
48
	$(INS.file)
49

  
50
install_h: $(ROOTHDRS)
51

  
52
check: $(CHECKHDRS)
53

  
54
$(SUBDIRS):	FRC
55
	@cd $@; pwd; $(MAKE) $(TARGET)
56

  
57
FRC:
58
-- old/usr/src/lib/libldap4/Makefile.com	Fri May 18 11:52:12 2012
......
0
++ /dev/null	Fri May 18 05:11:28 2012
......
1
#
2
# CDDL HEADER START
3
#
4
# The contents of this file are subject to the terms of the
5
# Common Development and Distribution License (the "License").
6
# You may not use this file except in compliance with the License.
7
#
8
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9
# or http://www.opensolaris.org/os/licensing.
10
# See the License for the specific language governing permissions
11
# and limitations under the License.
12
#
13
# When distributing Covered Code, include this CDDL HEADER in each
14
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15
# If applicable, add the following below this CDDL HEADER, with the
16
# fields enclosed by brackets "[]" replaced with your own identifying
17
# information: Portions Copyright [yyyy] [name of copyright owner]
18
#
19
# CDDL HEADER END
20
#
21
#
22
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23
# Use is subject to license terms.
24
#
25
# ident	"%Z%%M%	%I%	%E% SMI"
26
#
27

  
28
LIBRARY= libldap.a
29
VERS= .4
30

  
31
LDAPOBJS=	abandon.o             getentry.o            referral.o \
32
		add.o                 getfilter.o           regex.o \
33
		addentry.o            getmsg.o              rename.o \
34
		bind.o                getref.o              request.o \
35
		cache.o               getvalues.o           result.o \
36
		charset.o             kbind.o               saslbind.o \
37
		cldap.o               sbind.o 		    compare.o    \
38
		search.o 	      controls.o            sort.o \
39
		delete.o              srchpref.o	    disptmpl.o \
40
		tmplout.o 	      dsparse.o             \
41
		error.o               ufn.o \
42
		extensions.o          unbind.o 	            extop.o    \
43
		url.o         \
44
		free.o   modify.o              utils.o \
45
		friendly.o            modrdn.o    notif.o    Version.o \
46
		getattr.o             open.o                \
47
		getdn.o               option.o \
48
		getdxbyname.o         os-ip.o               sortctrl.o \
49
		vlistctrl.o
50

  
51
BEROBJS=	bprint.o	      decode.o \
52
		encode.o 	   \
53
		io.o		      i18n.o
54

  
55
UTILOBJS=	line64.o	log.o
56

  
57

  
58
SECOBJS=	cram_md5.o	secutil.o
59

  
60
OBJECTS=	$(LDAPOBJS)	$(BEROBJS)	$(UTILOBJS)	$(SECOBJS)
61

  
62
include ../../Makefile.lib
63

  
64
LDAPINC=	$(SRC)/lib/libldap4/include
65
LDAP_FLAGS=	-DLDAP_REFERRALS -DCLDAP -DLDAP_DNS -DSUN
66

  
67
SRCDIR =	../common
68
SRCS=		$(LDAPOBJS:%.o=../common/%.c)	$(BEROBJS:%.o=../ber/%.c) \
69
		$(UTILOBJS:%.o=../util/%.c)	$(SECOBJS:%.o=../sec/%.c) 
70

  
71
LIBS =		$(DYNLIB)
72

  
73
$(LINTLIB):= 	SRCS=../common/llib-lldap
74

  
75
LINTSRC=	$(LINTLIB:%.ln=%)
76
ROOTLINTDIR=	$(ROOTLIBDIR)
77
ROOTLINT=	$(LINTSRC:%=$(ROOTLINTDIR)/%)
78

  
79

  
80
CLEANFILES += 	$(LINTOUT) $(LINTLIB)
81

  
82
# Local Libldap definitions
83

  
84
LOCFLAGS +=	-D_SYS_STREAM_H -D_REENTRANT -DSVR4 -DSUNW_OPTIONS \
85
		-DTHREAD_SUNOS5_LWP -DSOUNDEX -DSTR_TRANSLATION \
86
		$(LDAP_FLAGS) -I$(LDAPINC)
87

  
88
CPPFLAGS =	$(LOCFLAGS) $(CPPFLAGS.master)
89
CFLAGS +=	$(CCVERBOSE)
90
LDLIBS +=	-lsocket -lnsl -lresolv -lc -lmd
91

  
92
.KEEP_STATE:
93

  
94
lint: lintcheck
95

  
96
# include library targets
97
include ../../Makefile.targ
98

  
99
objs/%.o pics/%.o: ../common/%.c
100
	$(COMPILE.c) -o $@ $<
101
	$(POST_PROCESS_O)
102

  
103
objs/%.o pics/%.o: ../ber/%.c
104
	$(COMPILE.c) -o $@ $<
105
	$(POST_PROCESS_O)
106

  
107
objs/%.o pics/%.o: ../util/%.c
108
	$(COMPILE.c) -o $@ $<
109
	$(POST_PROCESS_O)
110

  
111
objs/%.o pics/%.o: ../sec/%.c
112
	$(COMPILE.c) -o $@ $<
113
	$(POST_PROCESS_O)
114
-- old/usr/src/lib/libldap4/THIRDPARTYLICENSE	Fri May 18 11:52:13 2012
......
0
++ /dev/null	Fri May 18 05:11:28 2012
......
1
 * Copyright (c) 1991 Regents of the University of Michigan.
2
 * All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms are permitted
5
 * provided that this notice is preserved and that due credit is given
6
 * to the University of Michigan at Ann Arbor. The name of the University
7
 * may not be used to endorse or promote products derived from this
8
 * software without specific prior written permission. This software
9
 * is provided ``as is'' without express or implied warranty.
10

  
11
 * Copyright (c) 1994 Enrique Silvestre Mora, Universitat Jaume I, Spain.
12
 * All rights reserved.
13
 *
14
 * Redistribution and use in source and binary forms are permitted
15
 * provided that this notice is preserved and that due credit is given
16
 * to the Universitat Jaume I. The name of the University
17
 * may not be used to endorse or promote products derived from this
18
 * software without specific prior written permission. This software
19
 * is provided ``as is'' without express or implied warranty.
20
-- old/usr/src/lib/libldap4/THIRDPARTYLICENSE.descrip	Fri May 18 11:52:14 2012
......
0
++ /dev/null	Fri May 18 05:11:28 2012
......
1
LDAP4 LIBRARY
2
-- old/usr/src/lib/libldap4/amd64/Makefile	Fri May 18 11:52:14 2012
......
0
++ /dev/null	Fri May 18 05:11:28 2012
......
1
#
2
# CDDL HEADER START
3
#
4
# The contents of this file are subject to the terms of the
5
# Common Development and Distribution License (the "License").
6
# You may not use this file except in compliance with the License.
7
#
8
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9
# or http://www.opensolaris.org/os/licensing.
10
# See the License for the specific language governing permissions
11
# and limitations under the License.
12
#
13
# When distributing Covered Code, include this CDDL HEADER in each
14
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15
# If applicable, add the following below this CDDL HEADER, with the
16
# fields enclosed by brackets "[]" replaced with your own identifying
17
# information: Portions Copyright [yyyy] [name of copyright owner]
18
#
19
# CDDL HEADER END
20
#
21
#
22
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23
# Use is subject to license terms.
24
#
25
# ident	"%Z%%M%	%I%	%E% SMI"
26
#
27

  
28
include ../Makefile.com
29
include ../../Makefile.lib.64
30

  
31
LIBS = $(DYNLIB)
32

  
33
.KEEP_STATE:
34

  
35
all: $(LIBS)
36

  
37
install: all $(ROOTLIBS64)
38
-- old/usr/src/lib/libldap4/ber/Version.c	Fri May 18 11:52:15 2012
......
0
++ /dev/null	Fri May 18 05:11:28 2012
......
1
/*
2
 * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
3
 * Use is subject to license terms.
4
 */
5

  
6
#pragma ident	"%Z%%M%	%I%	%E% SMI"
7

  
8
/*
9
 * Copyright (c) 1991 Regents of the University of Michigan.
10
 * All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms are permitted
13
 * provided that this notice is preserved and that due credit is given
14
 * to the University of Michigan at Ann Arbor. The name of the University
15
 * may not be used to endorse or promote products derived from this
16
 * software without specific prior written permission. This software
17
 * is provided ``as is'' without express or implied warranty.
18
 */
19

  
20
static char Version[] = "  liblber.a v%VERSION% (%WHEN%)\n\t%WHOANDWHERE%\n";
21
-- old/usr/src/lib/libldap4/ber/bprint.c	Fri May 18 11:52:16 2012
......
0
++ /dev/null	Fri May 18 05:11:28 2012
......
1
/*
2
 * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
3
 * Use is subject to license terms.
4
 */
5

  
6
#pragma ident	"%Z%%M%	%I%	%E% SMI"
7

  
8
#include <stdio.h>
9
#include <string.h>
10
#include <ctype.h>
11
#include "lber.h"
12

  
13
/*
14
 * Print arbitrary stuff, for debugging.
15
 */
16

  
17
#ifdef LDAP_DEBUG
18

  
19
#ifndef NO_USERINTERFACE
20
#define BPLEN	48
21

  
22
void
23
lber_bprint( char *data, int len )
24
{
25
    static char	hexdig[] = "0123456789abcdef";
26
    char	out[ BPLEN ];
27
    int		i = 0;
28

  
29
    (void) memset( out, 0, BPLEN );
30
    for ( ;; ) {
31
	if ( len < 1 ) {
32
	    (void) fprintf( stderr, "\t%s\n", ( i == 0 ) ? catgets(slapdcat, 1, 72, "(end)") : out );
33
	    break;
34
	}
35

  
36
#ifndef HEX
37
	if ( isgraph( (unsigned char)*data )) {
38
	    out[ i ] = ' ';
39
	    out[ i+1 ] = *data;
40
	} else {
41
#endif
42
	    out[ i ] = hexdig[ ( *data & 0xf0 ) >> 4 ];
43
	    out[ i+1 ] = hexdig[ *data & 0x0f ];
44
#ifndef HEX
45
	}
46
#endif
47
	i += 2;
48
	len--;
49
	data++;
50

  
51
	if ( i > BPLEN - 2 ) {
52
	    (void) fprintf( stderr, "\t%s\n", out );
53
	    (void) memset( out, 0, BPLEN );
54
	    i = 0;
55
	    continue;
56
	}
57
	out[ i++ ] = ' ';
58
    }
59
}
60
#else /* NO_USERINTERFACE */
61
void
62
lber_bprint( char *data, int len )
63
{
64
}
65
#endif /* NO_USERINTERFACE */
66

  
67
#endif
68
-- old/usr/src/lib/libldap4/ber/decode.c	Fri May 18 11:52:17 2012
......
0
++ /dev/null	Fri May 18 05:11:28 2012
......
1
/*
2
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3
 * Use is subject to license terms.
4
 */
5

  
6
#pragma ident	"%Z%%M%	%I%	%E% SMI"
7

  
8
/* decode.c - ber input decoding routines */
9
/*
10
 * Copyright (c) 1990 Regents of the University of Michigan.
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms are permitted
14
 * provided that this notice is preserved and that due credit is given
15
 * to the University of Michigan at Ann Arbor. The name of the University
16
 * may not be used to endorse or promote products derived from this
17
 * software without specific prior written permission. This software
18
 * is provided ``as is'' without express or implied warranty.
19
 */
20

  
21
#include <stdio.h>
22
#ifdef MACOS
23
#include <stdlib.h>
24
#include <stdarg.h>
25
#include "macos.h"
26
#else /* MACOS */
27
#if defined(NeXT) || defined(VMS)
28
#include <stdlib.h>
29
#else /* next || vms */
30
#include <malloc.h>
31
#endif /* next || vms */
32
#if defined(BC31) || defined(_WIN32) || defined(__sun)
33
#include <stdarg.h>
34
#else /* BC31 || _WIN32 */
35
#include <varargs.h>
36
#endif /* BC31 || _WIN32 */
37
#include <sys/types.h>
38
#include <sys/socket.h>
39
#include <netinet/in.h>
40
#ifdef PCNFS
41
#include <tklib.h>
42
#endif /* PCNFS */
43
#endif /* MACOS */
44

  
45
#if defined( DOS ) || defined( _WIN32 )
46
#include "msdos.h"
47
#endif /* DOS */
48

  
49
#include <string.h>
50
#include "lber.h"
51
#include "ldap.h"
52
#include "ldap-private.h"
53
#include "ldap-int.h"
54

  
55
#ifdef LDAP_DEBUG
56
int	lber_debug;
57
#endif
58

  
59
#ifdef NEEDPROTOS
60
static int ber_getnint( BerElement *ber, int *num, int len );
61
#endif /* NEEDPROTOS */
62

  
63

  
64
/* return the tag - LBER_DEFAULT returned means trouble */
65
unsigned int
66
ber_get_tag( BerElement *ber )
67
{
68
	unsigned char	xbyte;
69
	unsigned int	tag;
70
	char		*tagp;
71
	int		i;
72

  
73
	if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
74
		return( LBER_DEFAULT );
75

  
76
	if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
77
		return( (unsigned int) xbyte );
78

  
79
	tagp = (char *) &tag;
80
	tagp[0] = xbyte;
81
	for ( i = 1; i < sizeof(int); i++ ) {
82
		if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
83
			return( LBER_DEFAULT );
84

  
85
		tagp[i] = xbyte;
86

  
87
		if ( ! (xbyte & LBER_MORE_TAG_MASK) )
88
			break;
89
	}
90

  
91
	/* tag too big! */
92
	if ( i == sizeof(int) )
93
		return( LBER_DEFAULT );
94

  
95
	/* want leading, not trailing 0's */
96
	return( tag >> (sizeof(int) - i - 1) );
97
}
98

  
99
unsigned int
100
ber_skip_tag( BerElement *ber, unsigned int *len )
101
{
102
	unsigned int	tag;
103
	unsigned char	lc;
104
	int		noctets, diff;
105
	unsigned int	netlen;
106

  
107
	/*
108
	 * Any ber element looks like this: tag length contents.
109
	 * Assuming everything's ok, we return the tag byte (we
110
	 * can assume a single byte), and return the length in len.
111
	 *
112
	 * Assumptions:
113
	 *	1) definite lengths
114
	 *	2) primitive encodings used whenever possible
115
	 */
116

  
117
	/*
118
	 * First, we read the tag.
119
	 */
120

  
121
	if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
122
		return( LBER_DEFAULT );
123

  
124
	/*
125
	 * Next, read the length.  The first byte contains the length of
126
	 * the length.  If bit 8 is set, the length is the int form,
127
	 * otherwise it's the short form.  We don't allow a length that's
128
	 * greater than what we can hold in an unsigned int.
129
	 */
130

  
131
	*len = netlen = 0;
132
	if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
133
		return( LBER_DEFAULT );
134
	if ( lc & 0x80 ) {
135
		noctets = (lc & 0x7f);
136
		if ( noctets > sizeof(unsigned int) )
137
			return( LBER_DEFAULT );
138
		diff = (int)sizeof(unsigned int) - noctets;
139
		if ( ber_read( ber, (char *) &netlen + diff, noctets )
140
		    != noctets )
141
			return( LBER_DEFAULT );
142
		*len = LBER_NTOHL( netlen );
143
	} else {
144
		*len = lc;
145
	}
146

  
147
	return( tag );
148
}
149

  
150
unsigned int
151
ber_peek_tag( BerElement *ber, unsigned int *len )
152
{
153
	char		*save;
154
	unsigned int	tag;
155

  
156
	save = ber->ber_ptr;
157
	tag = ber_skip_tag( ber, len );
158
	ber->ber_ptr = save;
159

  
160
	return( tag );
161
}
162

  
163
static int
164
ber_getnint( BerElement *ber, int *num, int len )
165
{	/* New patch much cleaner, from David Wilson, Isode. Old code not kept*/
166
 	int	i;
167
 	unsigned char buffer[sizeof(int)];
168
 	int	value;
169
  
170
  	/*
171
  	 * The tag and length have already been stripped off.  We should
172
  	 * be sitting right before len bytes of 2's complement integer,
173
 	 * ready to be read straight into an int.
174
  	 */
175
	
176
  	if ( len > sizeof(int) )
177
  		return( -1 );
178
  
179
 	if ( ber_read( ber, (char *) buffer, len ) != len )
180
  		return( -1 );
181
  
182
 	/* This sets the required sign extension */
183
 	value = 0x80 & buffer[0] ? (-1) : 0;
184
 
185
 	for ( i = 0; i < len; i++ )
186
 	    value = (value << 8) | buffer[i];
187

  
188
	*num = value;
189
	
190
  	return( len );
191
}
192

  
193
unsigned int
194
ber_get_int( BerElement *ber, int *num )
195
{
196
	unsigned int	tag, len;
197

  
198
	if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
199
		return( LBER_DEFAULT );
200

  
201
	if ( ber_getnint( ber, num, (int)len ) != len )
202
		return( LBER_DEFAULT );
203
	else
204
		return( tag );
205
}
206

  
207
unsigned int
208
ber_get_stringb( BerElement *ber, char *buf, unsigned int *len )
209
{
210
	unsigned int	datalen, tag;
211
#ifdef STR_TRANSLATION
212
	char		*transbuf;
213
#endif /* STR_TRANSLATION */
214

  
215
	if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
216
		return( LBER_DEFAULT );
217
	if ( datalen > (*len - 1) )
218
		return( LBER_DEFAULT );
219

  
220
	if ( ber_read( ber, buf, datalen ) != datalen )
221
		return( LBER_DEFAULT );
222

  
223
	buf[datalen] = '\0';
224

  
225
#ifdef STR_TRANSLATION
226
	if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
227
	    && ber->ber_decode_translate_proc != NULL ) {
228
		transbuf = buf;
229
		++datalen;
230
		if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
231
		    0 ) != 0 ) {
232
			return( LBER_DEFAULT );
233
		}
234
		if ( datalen > *len ) {
235
			free( transbuf );
236
			return( LBER_DEFAULT );
237
		}
238
		(void) SAFEMEMCPY( buf, transbuf, datalen );
239
		free( transbuf );
240
		--datalen;
241
	}
242
#endif /* STR_TRANSLATION */
243

  
244
	*len = datalen;
245
	return( tag );
246
}
247

  
248
unsigned int
249
ber_get_stringa( BerElement *ber, char **buf )
250
{
251
	unsigned int	datalen, tag;
252

  
253
	if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
254
		return( LBER_DEFAULT );
255

  
256
	if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL )
257
		return( LBER_DEFAULT );
258

  
259
	if ( ber_read( ber, *buf, datalen ) != datalen )
260
		return( LBER_DEFAULT );
261
	(*buf)[datalen] = '\0';
262

  
263
#ifdef STR_TRANSLATION
264
	if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
265
	    && ber->ber_decode_translate_proc != NULL ) {
266
		++datalen;
267
		if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
268
		    != 0 ) {
269
			free( *buf );
270
			return( LBER_DEFAULT );
271
		}
272
	}
273
#endif /* STR_TRANSLATION */
274

  
275
	return( tag );
276
}
277

  
278
unsigned int
279
ber_get_stringal( BerElement *ber, struct berval **bv )
280
{
281
	unsigned int	len, tag;
282

  
283
	if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL )
284
		return( LBER_DEFAULT );
285

  
286
	if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
287
		return( LBER_DEFAULT );
288

  
289
	if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL )
290
		return( LBER_DEFAULT );
291

  
292
	if ( ber_read( ber, (*bv)->bv_val, len ) != len )
293
		return( LBER_DEFAULT );
294
	((*bv)->bv_val)[len] = '\0';
295
	(*bv)->bv_len = len;
296

  
297
#ifdef STR_TRANSLATION
298
	if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
299
	    && ber->ber_decode_translate_proc != NULL ) {
300
		++len;
301
		if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
302
		    &len, 1 ) != 0 ) {
303
			free( (*bv)->bv_val );
304
			return( LBER_DEFAULT );
305
		}
306
		(*bv)->bv_len = len - 1;
307
	}
308
#endif /* STR_TRANSLATION */
309

  
310
	return( tag );
311
}
312

  
313
unsigned int
314
ber_get_bitstringa( BerElement *ber, char **buf, unsigned int *blen )
315
{
316
	unsigned int	datalen, tag;
317
	unsigned char	unusedbits;
318

  
319
	if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
320
		return( LBER_DEFAULT );
321
	--datalen;
322

  
323
	if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL )
324
		return( LBER_DEFAULT );
325

  
326
	if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 )
327
		return( LBER_DEFAULT );
328

  
329
	if ( ber_read( ber, *buf, datalen ) != datalen )
330
		return( LBER_DEFAULT );
331

  
332
	*blen = datalen * 8 - unusedbits;
333
	return( tag );
334
}
335

  
336
unsigned int
337
ber_get_null( BerElement *ber )
338
{
339
	unsigned int	len, tag;
340

  
341
	if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
342
		return( LBER_DEFAULT );
343

  
344
	if ( len != 0 )
345
		return( LBER_DEFAULT );
346

  
347
	return( tag );
348
}
349

  
350
unsigned int
351
ber_get_boolean( BerElement *ber, int *boolval )
352
{
353
	int	longbool;
354
	int	rc;
355

  
356
	rc = ber_get_int( ber, &longbool );
357
	*boolval = longbool;
358

  
359
	return( rc );
360
}
361

  
362
unsigned int
363
ber_first_element( BerElement *ber, unsigned int *len, char **last )
364
{
365
	/* skip the sequence header, use the len to mark where to stop */
366
	if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
367
		return( LBER_DEFAULT );
368
	}
369

  
370
	*last = ber->ber_ptr + *len;
371

  
372
	if ( *last == ber->ber_ptr ) {
373
		return( LBER_DEFAULT );
374
	}
375

  
376
	return( ber_peek_tag( ber, len ) );
377
}
378

  
379
unsigned int
380
ber_next_element( BerElement *ber, unsigned int *len, char *last )
381
{
382
	if ( ber->ber_ptr == last ) {
383
		return( LBER_DEFAULT );
384
	}
385

  
386
	return( ber_peek_tag( ber, len ) );
387
}
388

  
389
/* VARARGS */
390
unsigned int
391
ber_scanf(
392
#if defined(MACOS) || defined(BC31) || defined(_WIN32) || defined(__sun)
393
	BerElement *ber, char *fmt, ... )
394
#else
395
	va_alist )
396
va_dcl
397
#endif
398
{
399
	va_list		ap;
400
#if !defined(MACOS) && !defined(BC31) && !defined(_WIN32) && !defined(__sun)
401
	BerElement	*ber;
402
	char		*fmt;
403
#endif
404
	char		*last;
405
	char		*s, **ss, ***sss;
406
	struct berval 	***bv, **bvp, *bval;
407
	int		*i, j;
408
	int		*l, rc, tag;
409
	unsigned int	len;
410

  
411
#if defined(MACOS) || defined(BC31) || defined(_WIN32) || defined(__sun)
412
	va_start( ap, fmt );
413
#else
414
	va_start( ap );
415
	ber = va_arg( ap, BerElement * );
416
	fmt = va_arg( ap, char * );
417
#endif
418

  
419
#ifdef LDAP_DEBUG
420
	if ( lber_debug & 64 ) {
421
		(void) fprintf( stderr, catgets(slapdcat, 1, 73, "ber_scanf fmt (%s) ber:\n"), fmt );
422
		ber_dump( ber, 1 );
423
	}
424
#endif
425

  
426
	for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
427
		switch ( *fmt ) {
428
		case 'a':	/* octet string - allocate storage as needed */
429
			ss = va_arg( ap, char ** );
430
			rc = ber_get_stringa( ber, ss );
431
			break;
432

  
433
		case 'b':	/* boolean */
434
			i = va_arg( ap, int * );
435
			rc = ber_get_boolean( ber, i );
436
			break;
437

  
438
		case 'e':	/* enumerated */
439
		case 'i':	/* int */
440
			l = va_arg( ap, int * );
441
			rc = ber_get_int( ber, l );
442
			break;
443

  
444
		case 'l':	/* length of next item */
445
			l = va_arg( ap, int * );
446
			rc = ber_peek_tag( ber, (unsigned int *)l );
447
			break;
448

  
449
		case 'n':	/* null */
450
			rc = ber_get_null( ber );
451
			break;
452

  
453
		case 's':	/* octet string - in a buffer */
454
			s = va_arg( ap, char * );
455
			l = va_arg( ap, int * );
456
			rc = ber_get_stringb( ber, s, (unsigned int *)l );
457
			break;
458

  
459
		case 'o':	/* octet string in a supplied berval */
460
			bval = va_arg( ap, struct berval * );
461
			ber_peek_tag( ber, &bval->bv_len );
462
			rc = ber_get_stringa( ber, &bval->bv_val );
463
			break;
464

  
465
		case 'O':	/* octet string - allocate & include length */
466
			bvp = va_arg( ap, struct berval ** );
467
			rc = ber_get_stringal( ber, bvp );
468
			break;
469

  
470
		case 'B':	/* bit string - allocate storage as needed */
471
			ss = va_arg( ap, char ** );
472
			l = va_arg( ap, int * ); /* for length, in bits */
473
			rc = ber_get_bitstringa( ber, ss, (unsigned int *)l );
474
			break;
475

  
476
		case 't':	/* tag of next item */
477
			i = va_arg( ap, int * );
478
			*i = rc = ber_peek_tag( ber, &len );
479
			break;
480

  
481
		case 'T':	/* skip tag of next item */
482
			i = va_arg( ap, int * );
483
			*i = rc = ber_skip_tag( ber, &len );
484
			break;
485

  
486
		case 'v':	/* sequence of strings */
487
			sss = va_arg( ap, char *** );
488
			*sss = NULL;
489
			j = 0;
490
			for ( tag = ber_first_element( ber, &len, &last );
491
			    tag != LBER_DEFAULT && rc != LBER_DEFAULT;
492
			    tag = ber_next_element( ber, &len, last ) ) {
493
				if ( *sss == NULL ) {
494
					*sss = (char **) malloc(
495
					    2 * sizeof(char *) );
496
				} else {
497
					*sss = (char **) realloc( *sss,
498
					    (j + 2) * sizeof(char *) );
499
				}
500
				rc = ber_get_stringa( ber, &((*sss)[j]) );
501
				j++;
502
			}
503
			if ( j > 0 )
504
				(*sss)[j] = NULL;
505
			break;
506

  
507
		case 'V':	/* sequence of strings + lengths */
508
			bv = va_arg( ap, struct berval *** );
509
			*bv = NULL;
510
			j = 0;
511
			for ( tag = ber_first_element( ber, &len, &last );
512
			    tag != LBER_DEFAULT && rc != LBER_DEFAULT;
513
			    tag = ber_next_element( ber, &len, last ) ) {
514
				if ( *bv == NULL ) {
515
					*bv = (struct berval **) malloc(
516
					    2 * sizeof(struct berval *) );
517
				} else {
518
					*bv = (struct berval **) realloc( *bv,
519
					    (j + 2) * sizeof(struct berval *) );
520
				}
521
				rc = ber_get_stringal( ber, &((*bv)[j]) );
522
				j++;
523
			}
524
			if ( j > 0 )
525
				(*bv)[j] = NULL;
526
			break;
527

  
528
		case 'x':	/* skip the next element - whatever it is */
529
			if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
530
				break;
531
			ber->ber_ptr += len;
532
			break;
533

  
534
		case '{':	/* begin sequence */
535
		case '[':	/* begin set */
536
			if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
537
				rc = ber_skip_tag( ber, &len );
538
			break;
539

  
540
		case '}':	/* end sequence */
541
		case ']':	/* end set */
542
			break;
543

  
544
		default:
545
#ifndef NO_USERINTERFACE
546
			(void) fprintf( stderr, catgets(slapdcat, 1, 74, "unknown fmt %c\n"), *fmt );
547
#endif /* NO_USERINTERFACE */
548
			rc = (int) LBER_DEFAULT;
549
			break;
550
		}
551
	}
552

  
553
	va_end( ap );
554

  
555
	return( rc );
556
}
557

  
558
void
559
ber_bvfree( struct berval *bv )
560
{
561
	if ( bv->bv_val != NULL )
562
		free( bv->bv_val );
563
	free( (char *) bv );
564
}
565

  
566
void
567
ber_bvecfree( struct berval **bv )
568
{
569
	int	i;
570

  
571
	for ( i = 0; bv[i] != NULL; i++ )
572
		ber_bvfree( bv[i] );
573
	free( (char *) bv );
574
}
575

  
576
struct berval *
577
ber_bvdup( struct berval *bv )
578
{
579
	struct berval	*new;
580

  
581
	if ( (new = (struct berval *) malloc( sizeof(struct berval) ))
582
	    == NULL ) {
583
		return( NULL );
584
	}
585
	if ( (new->bv_val = (char *) malloc( bv->bv_len + 1 )) == NULL ) {
586
		free(new);
587
		return( NULL );
588
	}
589
	SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
590
	new->bv_val[bv->bv_len] = '\0';
591
	new->bv_len = bv->bv_len;
592

  
593
	return( new );
594
}
595

  
596

  
597
#ifdef STR_TRANSLATION
598
void
599
ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
600
	BERTranslateProc decode_proc )
601
{
602
    ber->ber_encode_translate_proc = encode_proc;
603
    ber->ber_decode_translate_proc = decode_proc;
604
}
605
#endif /* STR_TRANSLATION */
606

  
607
int ber_flatten(BerElement *ber, struct berval **bvPtr)
608
{
609
	struct berval * bv;
610
	int len;
611

  
612
	if ((ber == NULL) || (ber->ber_buf == NULL))
613
		return (-1);
614

  
615
	len = ber->ber_ptr - ber->ber_buf;
616

  
617
	if ((bv = (struct berval *)malloc(sizeof(struct berval))) == NULL)
618
		return (-1);
619
	if ((bv->bv_val = (char *) malloc(len + 1)) == NULL) {
620
		free(bv);
621
		return (-1);
622
	}
623

  
624
	SAFEMEMCPY(bv->bv_val, ber->ber_buf, (size_t)len);
625
	bv->bv_val[len] = '\0';
626
	bv->bv_len = len;
627

  
628
	*bvPtr = bv;
629
	return (0);
630
}
631
-- old/usr/src/lib/libldap4/ber/encode.c	Fri May 18 11:52:18 2012
......
0
++ /dev/null	Fri May 18 05:11:28 2012
......
1
/*
2
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3
 * Use is subject to license terms.
4
 */
5

  
6
#pragma ident	"%Z%%M%	%I%	%E% SMI"
7

  
8
/* encode.c - ber output encoding routines */
9
/*
10
 * Copyright (c) 1990 Regents of the University of Michigan.
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms are permitted
14
 * provided that this notice is preserved and that due credit is given
15
 * to the University of Michigan at Ann Arbor. The name of the University
16
 * may not be used to endorse or promote products derived from this
17
 * software without specific prior written permission. This software
18
 * is provided ``as is'' without express or implied warranty.
19
 */
20

  
21
#include <stdio.h>
22
#ifdef MACOS
23
#include <stdlib.h>
24
#include <stdarg.h>
25
#include "macos.h"
26
#else /* MACOS */
27
#if defined(NeXT) || defined(VMS)
28
#include <stdlib.h>
29
#else /* next || vms */
30
#include <malloc.h>
31
#endif /* next || vms */
32
#if defined( BC31 ) || defined( _WIN32 ) || defined(__sun)
33
#include <stdarg.h>
34
#else /* BC31 || _WIN32 */
35
#include <varargs.h>
36
#endif /* BC31 || _WIN32 */
37
#include <sys/types.h>
38
#include <sys/socket.h>
39
#include <netinet/in.h>
40
#ifdef PCNFS
41
#include <tklib.h>
42
#endif /* PCNFS */
43
#endif /* MACOS */
44
#ifndef VMS
45
#include <memory.h>
46
#endif
47
#include <string.h>
48
#include "lber.h"
49
#include "ldap.h"
50
#include "ldap-private.h"
51
#include "ldap-int.h"
52

  
53
#if defined( DOS ) || defined( _WIN32 )
54
#include "msdos.h"
55
#endif /* DOS */
56

  
57
#ifdef NEEDPROTOS
58
static int ber_put_len( BerElement *ber, unsigned int len, int nosos );
59
static int ber_start_seqorset( BerElement *ber, unsigned int tag );
60
static int ber_put_seqorset( BerElement *ber );
61
static int ber_put_int_or_enum( BerElement *ber, int num, unsigned int tag );
62
#endif /* NEEDPROTOS */
63

  
64
extern int ber_realloc(BerElement *ber, unsigned int len);
65

  
66
static int
67
ber_calc_taglen( unsigned int tag )
68
{
69
	int	i;
70
	int	mask;
71

  
72
	/* find the first non-all-zero byte in the tag */
73
	for ( i = sizeof(int) - 1; i > 0; i-- ) {
74
		mask = (0xffL << (i * 8));
75
		/* not all zero */
76
		if ( tag & mask )
77
			break;
78
	}
79

  
80
	return( i + 1 );
81
}
82

  
83
static int
84
ber_put_tag( BerElement	*ber, unsigned int tag, int nosos )
85
{
86
	int		taglen;
87
	unsigned int	ntag;
88

  
89
	taglen = ber_calc_taglen( tag );
90

  
91
	ntag = LBER_HTONL( tag );
92

  
93
	return( ber_write( ber, ((char *) &ntag) + sizeof(int) - taglen,
94
	    taglen, nosos ) );
95
}
96

  
97
static int
98
ber_calc_lenlen( unsigned int len )
99
{
100
	/*
101
	 * short len if it's less than 128 - one byte giving the len,
102
	 * with bit 8 0.
103
	 */
104

  
105
	if ( len <= 0x7F )
106
		return( 1 );
107

  
108
	/*
109
	 * int len otherwise - one byte with bit 8 set, giving the
110
	 * length of the length, followed by the length itself.
111
	 */
112

  
113
	if ( len <= 0xFF )
114
		return( 2 );
115
	if ( len <= 0xFFFF )
116
		return( 3 );
117
	if ( len <= 0xFFFFFF )
118
		return( 4 );
119

  
120
	return( 5 );
121
}
122

  
123
static int
124
ber_put_len( BerElement *ber, unsigned int len, int nosos )
125
{
126
	int		i;
127
	char		lenlen;
128
	int		mask;
129
	unsigned int	netlen;
130

  
131
	/*
132
	 * short len if it's less than 128 - one byte giving the len,
133
	 * with bit 8 0.
134
	 */
135

  
136
	if ( len <= 127 ) {
137
		netlen = LBER_HTONL( len );
138
		return( ber_write( ber, (char *) &netlen + sizeof(int) - 1,
139
		    1, nosos ) );
140
	}
141

  
142
	/*
143
	 * int len otherwise - one byte with bit 8 set, giving the
144
	 * length of the length, followed by the length itself.
145
	 */
146

  
147
	/* find the first non-all-zero byte */
148
	for ( i = sizeof(int) - 1; i > 0; i-- ) {
149
		mask = (0xff << (i * 8));
150
		/* not all zero */
151
		if ( len & mask )
152
			break;
153
	}
154
	lenlen = ++i;
155
	if ( lenlen > 4 )
156
		return( -1 );
157
	lenlen |= 0x80;
158

  
159
	/* write the length of the length */
160
	if ( ber_write( ber, &lenlen, 1, nosos ) != 1 )
161
		return( -1 );
162

  
163
	/* write the length itself */
164
	netlen = LBER_HTONL( len );
165
	if ( ber_write( ber, (char *) &netlen + (sizeof(int) - i), i, nosos )
166
	    != i )
167
		return( -1 );
168

  
169
	return( i + 1 );
170
}
171

  
172
static int
173
ber_put_int_or_enum( BerElement *ber, int num, unsigned int tag )
174
{
175
	int	i, sign, taglen;
176
	int	len, lenlen;
177
	int	netnum, mask;
178

  
179
	sign = (num < 0);
180

  
181
	/*
182
	 * high bit is set - look for first non-all-one byte
183
	 * high bit is clear - look for first non-all-zero byte
184
	 */
185
	for ( i = sizeof(int) - 1; i > 0; i-- ) {
186
		mask = (0xff << (i * 8));
187

  
188
		if ( sign ) {
189
			/* not all ones */
190
			if ( (num & mask) != mask )
191
				break;
192
		} else {
193
			/* not all zero */
194
			if ( num & mask )
195
				break;
196
		}
197
	}
198

  
199
	/*
200
	 * we now have the "leading byte".  if the high bit on this
201
	 * byte matches the sign bit, we need to "back up" a byte.
202
	 */
203
	mask = (num & (0x80 << (i * 8)));
204
	if ( (mask && !sign) || (sign && !mask) )
205
		i++;
206

  
207
	len = i + 1;
208

  
209
	if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
210
		return( -1 );
211

  
212
	if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
213
		return( -1 );
214
	i++;
215
	netnum = LBER_HTONL( num );
216
	if ( ber_write( ber, (char *) &netnum + (sizeof(int) - i), i, 0 )
217
	   != i )
218
		return( -1 );
219

  
220
	/* length of tag + length + contents */
221
	return( taglen + lenlen + i );
222
}
223

  
224
int
225
ber_put_enum( BerElement *ber, int num, unsigned int tag )
226
{
227
	if ( tag == LBER_DEFAULT )
228
		tag = LBER_ENUMERATED;
229

  
230
	return( ber_put_int_or_enum( ber, num, tag ) );
231
}
232

  
233
int
234
ber_put_int( BerElement *ber, int num, unsigned int tag )
235
{
236
	if ( tag == LBER_DEFAULT )
237
		tag = LBER_INTEGER;
238

  
239
	return( ber_put_int_or_enum( ber, num, tag ) );
240
}
241

  
242
int
243
ber_put_ostring( BerElement *ber, char *str, unsigned int len,
244
	unsigned int tag )
245
{
246
	int	taglen, lenlen, rc;
247
#ifdef STR_TRANSLATION
248
	int	free_str;
249
#endif /* STR_TRANSLATION */
250

  
251
	if ( tag == LBER_DEFAULT )
252
		tag = LBER_OCTETSTRING;
253

  
254
	if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
255
		return( -1 );
256

  
257
#ifdef STR_TRANSLATION
258
	if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0 &&
259
	    ber->ber_encode_translate_proc != NULL ) {
260
		if ( (*(ber->ber_encode_translate_proc))( &str, &len, 0 )
261
		    != 0 ) {
262
			return( -1 );
263
		}
264
		free_str = 1;
265
	} else {
266
		free_str = 0;
267
	}
268
#endif /* STR_TRANSLATION */
269

  
270
	if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
271
		ber_write( ber, str, len, 0 ) != len ) {
272
		rc = -1;
273
	} else {
274
		/* return length of tag + length + contents */
275
		rc = taglen + lenlen + len;
276
	}
277

  
278
#ifdef STR_TRANSLATION
279
	if ( free_str ) {
280
		free( str );
281
	}
282
#endif /* STR_TRANSLATION */
283

  
284
	return( rc );
285
}
286

  
287
int
288
ber_put_string( BerElement *ber, char *str, unsigned int tag )
289
{
290
	return( ber_put_ostring( ber, str, (unsigned int)strlen( str ), tag ));
291
}
292

  
293
int
294
ber_put_bitstring( BerElement *ber, char *str,
295
	unsigned int blen /* in bits */, unsigned int tag )
296
{
297
	int		taglen, lenlen, len;
298
	unsigned char	unusedbits;
299

  
300
	if ( tag == LBER_DEFAULT )
301
		tag = LBER_BITSTRING;
302

  
303
	if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
304
		return( -1 );
305

  
306
	len = ( blen + 7 ) / 8;
307
	unusedbits = len * 8 - blen;
308
	if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 )
309
		return( -1 );
310

  
311
	if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
312
		return( -1 );
313

  
314
	if ( ber_write( ber, str, len, 0 ) != len )
315
		return( -1 );
316

  
317
	/* return length of tag + length + unused bit count + contents */
318
	return( taglen + 1 + lenlen + len );
319
}
320

  
321
int
322
ber_put_null( BerElement *ber, unsigned int tag )
323
{
324
	int	taglen;
325

  
326
	if ( tag == LBER_DEFAULT )
327
		tag = LBER_NULL;
328

  
329
	if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
330
		return( -1 );
331

  
332
	if ( ber_put_len( ber, 0, 0 ) != 1 )
333
		return( -1 );
334

  
335
	return( taglen + 1 );
336
}
337

  
338
int
339
ber_put_boolean( BerElement *ber, int boolval, unsigned int tag )
340
{
341
	int		taglen;
342
	unsigned char	trueval = 0xff;
343
	unsigned char	falseval = 0x00;
344

  
345
	if ( tag == LBER_DEFAULT )
346
		tag = LBER_BOOLEAN;
347

  
348
	if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
349
		return( -1 );
350

  
351
	if ( ber_put_len( ber, 1, 0 ) != 1 )
352
		return( -1 );
353

  
354
	if ( ber_write( ber, (char *)(boolval ? &trueval : &falseval), 1, 0 )
355
	    != 1 )
356
		return( -1 );
357

  
358
	return( taglen + 2 );
359
}
360

  
361
#define FOUR_BYTE_LEN	5
362

  
363
static int
364
ber_start_seqorset( BerElement *ber, unsigned int tag )
365
{
366
	Seqorset	*new;
367

  
368
	if ( (new = (Seqorset *) calloc( sizeof(Seqorset), 1 ))
369
	    == NULLSEQORSET )
370
		return( -1 );
371
	new->sos_ber = ber;
372
	if ( ber->ber_sos == NULLSEQORSET )
373
		new->sos_first = ber->ber_ptr;
374
	else
375
		new->sos_first = ber->ber_sos->sos_ptr;
376

  
377
	/* Set aside room for a 4 byte length field */
378
	new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
379
	new->sos_tag = tag;
380

  
381
	new->sos_next = ber->ber_sos;
382
	ber->ber_sos = new;
383
	if (ber->ber_sos->sos_ptr > ber->ber_end)
384
		ber_realloc(ber, ber->ber_sos->sos_ptr - ber->ber_end);
385

  
386
	return( 0 );
387
}
388

  
389
int
390
ber_start_seq( BerElement *ber, unsigned int tag )
391
{
392
	if ( tag == LBER_DEFAULT )
393
		tag = LBER_SEQUENCE;
394

  
395
	return( ber_start_seqorset( ber, tag ) );
396
}
397

  
398
int
399
ber_start_set( BerElement *ber, unsigned int tag )
400
{
401
	if ( tag == LBER_DEFAULT )
402
		tag = LBER_SET;
403

  
404
	return( ber_start_seqorset( ber, tag ) );
405
}
406

  
407
static int
408
ber_put_seqorset( BerElement *ber )
409
{
410
	unsigned int	len, netlen;
411
	int		taglen, lenlen;
412
	unsigned char	ltag = 0x80 + FOUR_BYTE_LEN - 1;
413
	Seqorset	*next;
414
	Seqorset	**sos = &ber->ber_sos;
415

  
416
	/*
417
	 * If this is the toplevel sequence or set, we need to actually
418
	 * write the stuff out.  Otherwise, it's already been put in
419
	 * the appropriate buffer and will be written when the toplevel
420
	 * one is written.  In this case all we need to do is update the
421
	 * length and tag.
422
	 */
423

  
424
	len = (*sos)->sos_clen;
425
	netlen = LBER_HTONL( len );
426
	/* CONSTCOND */
427
	if ( sizeof(int) > 4 && len > 0xFFFFFFFF )
428
		return( -1 );
429

  
430
	if ( ber->ber_options & LBER_USE_DER ) {
431
		lenlen = ber_calc_lenlen( len );
432
	} else {
433
		lenlen = FOUR_BYTE_LEN;
434
	}
435

  
436
	if ( (next = (*sos)->sos_next) == NULLSEQORSET ) {
437
		/* write the tag */
438
		if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
439
			return( -1 );
440

  
441
		if ( ber->ber_options & LBER_USE_DER ) {
442
			/* Write the length in the minimum # of octets */
443
			if ( ber_put_len( ber, len, 1 ) == -1 )
444
				return( -1 );
445

  
446
			if (lenlen != FOUR_BYTE_LEN) {
447
				/*
448
				 * We set aside FOUR_BYTE_LEN bytes for
449
				 * the length field.  Move the data if
450
				 * we don't actually need that much
451
				 */
452
				(void) SAFEMEMCPY( (*sos)->sos_first + taglen +
453
				    lenlen, (*sos)->sos_first + taglen +
454
				    FOUR_BYTE_LEN, len );
455
			}
456
		} else {
457
			/* Fill FOUR_BYTE_LEN bytes for length field */
458
			/* one byte of length length */
459
			if ( ber_write( ber, (char *)&ltag, 1, 1 ) != 1 )
460
				return( -1 );
461

  
462
			/* the length itself */
463
			if ( ber_write( ber, (char *) &netlen + sizeof(int)
464
			    - (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1, 1 )
465
			    != FOUR_BYTE_LEN - 1 )
466
				return( -1 );
467
		}
468
		/* The ber_ptr is at the set/seq start - move it to the end */
469
		(*sos)->sos_ber->ber_ptr += len;
470
	} else {
471
		unsigned int	ntag;
472

  
473
		/* the tag */
474
		taglen = ber_calc_taglen( (*sos)->sos_tag );
475
		ntag = LBER_HTONL( (*sos)->sos_tag );
476
		(void) SAFEMEMCPY( (*sos)->sos_first, (char *) &ntag +
477
		    sizeof(int) - taglen, taglen );
478

  
479
		if ( ber->ber_options & LBER_USE_DER ) {
480
			ltag = (lenlen == 1) ? len : 0x80 + (lenlen - 1);
481
		}
482

  
483
		/* one byte of length length */
484
		(void) SAFEMEMCPY( (*sos)->sos_first + 1, &ltag, 1 );
485

  
486
		if ( ber->ber_options & LBER_USE_DER ) {
487
			if (lenlen > 1) {
488
				/* Write the length itself */
489
				(void) SAFEMEMCPY( (*sos)->sos_first + 2,
490
				    (char *)&netlen + sizeof(unsigned int) -
491
				    (lenlen - 1),
492
				    lenlen - 1 );
493
			}
494
			if (lenlen != FOUR_BYTE_LEN) {
495
				/*
496
				 * We set aside FOUR_BYTE_LEN bytes for
497
				 * the length field.  Move the data if
498
				 * we don't actually need that much
499
				 */
500
				(void) SAFEMEMCPY( (*sos)->sos_first + taglen +
501
				    lenlen, (*sos)->sos_first + taglen +
502
				    FOUR_BYTE_LEN, len );
... This diff was truncated because it exceeds the maximum size that can be displayed.
    (1-1/1)