2003-11-26 07:17:08 +08:00
|
|
|
/* encode.c - ber output encoding routines */
|
1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2006-01-04 07:11:52 +08:00
|
|
|
* Copyright 1998-2006 The OpenLDAP Foundation.
|
2003-11-26 07:17:08 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
1998-12-29 04:38:04 +08:00
|
|
|
*/
|
2003-11-26 07:17:08 +08:00
|
|
|
/* Portions Copyright (c) 1990 Regents of the University of Michigan.
|
1998-08-09 08:43:13 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that this notice is preserved and that due credit is given
|
|
|
|
* to the University of Michigan at Ann Arbor. The name of the University
|
|
|
|
* may not be used to endorse or promote products derived from this
|
|
|
|
* software without specific prior written permission. This software
|
|
|
|
* is provided ``as is'' without express or implied warranty.
|
|
|
|
*/
|
2003-11-27 06:51:15 +08:00
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was originally developed by the University of Michigan
|
|
|
|
* (as part of U-MICH LDAP).
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1998-11-06 09:18:49 +08:00
|
|
|
#include <ac/stdarg.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
|
1998-10-26 09:18:41 +08:00
|
|
|
#include "lber-int.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
static int ber_put_len LDAP_P((
|
|
|
|
BerElement *ber,
|
|
|
|
ber_len_t len,
|
|
|
|
int nosos ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
static int ber_start_seqorset LDAP_P((
|
|
|
|
BerElement *ber,
|
|
|
|
ber_tag_t tag ));
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
static int ber_put_seqorset LDAP_P(( BerElement *ber ));
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
static int ber_put_int_or_enum LDAP_P((
|
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t num,
|
|
|
|
ber_tag_t tag ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2005-09-13 15:51:51 +08:00
|
|
|
#define BER_TOP_BYTE(type) (sizeof(type)-1)
|
|
|
|
#define BER_TOP_MASK(type) ((type)0xffU << (BER_TOP_BYTE(type)*8))
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2002-04-11 16:47:37 +08:00
|
|
|
static int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_calc_taglen( ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2005-09-13 15:51:51 +08:00
|
|
|
int i = BER_TOP_BYTE(ber_tag_t);
|
|
|
|
ber_tag_t mask = BER_TOP_MASK(ber_tag_t);
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* find the first non-all-zero byte in the tag */
|
2005-09-13 15:51:51 +08:00
|
|
|
for ( ; i > 0; i-- ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
/* not all zero */
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( tag & mask ) break;
|
2005-09-13 15:51:51 +08:00
|
|
|
mask >>= 8;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return i + 1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_put_tag(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_tag_t tag,
|
|
|
|
int nosos )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
int rc;
|
2005-09-13 15:51:51 +08:00
|
|
|
int taglen;
|
2002-04-11 16:47:37 +08:00
|
|
|
int i;
|
1999-11-27 07:32:18 +08:00
|
|
|
unsigned char nettag[sizeof(ber_tag_t)];
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
taglen = ber_calc_taglen( tag );
|
|
|
|
|
2005-09-13 15:51:51 +08:00
|
|
|
for( i=taglen-1; i>=0; i-- ) {
|
|
|
|
nettag[i] = (unsigned char)(tag & 0xffU);
|
1999-11-27 07:32:18 +08:00
|
|
|
tag >>= 8;
|
|
|
|
}
|
1999-06-19 07:53:05 +08:00
|
|
|
|
2005-09-13 15:51:51 +08:00
|
|
|
rc = ber_write( ber, (char *) nettag, taglen, nosos );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
return rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
static ber_len_t
|
|
|
|
ber_calc_lenlen( ber_len_t len )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* short len if it's less than 128 - one byte giving the len,
|
|
|
|
* with bit 8 0.
|
|
|
|
*/
|
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( len <= (ber_len_t) 0x7FU ) return 1;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* long len otherwise - one byte with bit 8 set, giving the
|
|
|
|
* length of the length, followed by the length itself.
|
|
|
|
*/
|
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( len <= (ber_len_t) 0xffU ) return 2;
|
|
|
|
if ( len <= (ber_len_t) 0xffffU ) return 3;
|
|
|
|
if ( len <= (ber_len_t) 0xffffffU ) return 4;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return 5;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_put_len( BerElement *ber, ber_len_t len, int nosos )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-11-27 07:32:18 +08:00
|
|
|
int rc;
|
|
|
|
int i,j;
|
1998-08-09 08:43:13 +08:00
|
|
|
char lenlen;
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t mask;
|
1999-11-27 07:32:18 +08:00
|
|
|
unsigned char netlen[sizeof(ber_len_t)];
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* short len if it's less than 128 - one byte giving the len,
|
|
|
|
* with bit 8 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( len <= 127 ) {
|
1999-08-02 09:50:08 +08:00
|
|
|
char length_byte = (char) len;
|
2000-10-11 10:43:14 +08:00
|
|
|
return ber_write( ber, &length_byte, 1, nosos );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* long len otherwise - one byte with bit 8 set, giving the
|
|
|
|
* length of the length, followed by the length itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* find the first non-all-zero byte */
|
2005-09-13 15:51:51 +08:00
|
|
|
i = BER_TOP_BYTE(ber_len_t);
|
|
|
|
mask = BER_TOP_MASK(ber_len_t);
|
|
|
|
for ( ; i > 0; i-- ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
/* not all zero */
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( len & mask ) break;
|
2005-09-13 15:51:51 +08:00
|
|
|
mask >>= 8;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-10-25 09:41:42 +08:00
|
|
|
lenlen = (unsigned char) ++i;
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( lenlen > 4 ) return -1;
|
1999-11-27 07:32:18 +08:00
|
|
|
|
1999-05-19 14:13:44 +08:00
|
|
|
lenlen |= 0x80UL;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* write the length of the length */
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( ber_write( ber, &lenlen, 1, nosos ) != 1 ) return -1;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2005-09-13 15:51:51 +08:00
|
|
|
for( j=i-1; j>=0; j-- ) {
|
|
|
|
netlen[j] = (unsigned char)(len & 0xffU);
|
1999-11-27 07:32:18 +08:00
|
|
|
len >>= 8;
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* write the length itself */
|
2005-09-13 15:51:51 +08:00
|
|
|
rc = ber_write( ber, (char *) netlen, i, nosos );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-11-27 07:32:18 +08:00
|
|
|
return rc == i ? i+1 : -1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_put_int_or_enum(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t num,
|
|
|
|
ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-11-27 07:32:18 +08:00
|
|
|
int rc;
|
2002-04-11 16:47:37 +08:00
|
|
|
int i, j, sign, taglen, lenlen;
|
|
|
|
ber_len_t len;
|
1999-12-03 14:26:41 +08:00
|
|
|
ber_uint_t unum, mask;
|
1999-11-27 07:32:18 +08:00
|
|
|
unsigned char netnum[sizeof(ber_uint_t)];
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
sign = (num < 0);
|
2001-01-18 01:08:44 +08:00
|
|
|
unum = num; /* Bit fiddling should be done with unsigned values */
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* high bit is set - look for first non-all-one byte
|
|
|
|
* high bit is clear - look for first non-all-zero byte
|
|
|
|
*/
|
2005-09-13 15:51:51 +08:00
|
|
|
i = BER_TOP_BYTE(ber_int_t);
|
|
|
|
mask = BER_TOP_MASK(ber_uint_t);
|
|
|
|
for ( ; i > 0; i-- ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( sign ) {
|
|
|
|
/* not all ones */
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (unum & mask) != mask ) break;
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
/* not all zero */
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( unum & mask ) break;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2005-09-13 15:51:51 +08:00
|
|
|
mask >>= 8;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we now have the "leading byte". if the high bit on this
|
|
|
|
* byte matches the sign bit, we need to "back up" a byte.
|
|
|
|
*/
|
1999-08-02 09:55:48 +08:00
|
|
|
mask = (unum & ((ber_uint_t)0x80U << (i * 8)));
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (mask && !sign) || (sign && !mask) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
i++;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
len = i + 1;
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return -1;
|
2003-07-08 11:46:20 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
i++;
|
1999-11-27 07:32:18 +08:00
|
|
|
|
2005-09-13 15:51:51 +08:00
|
|
|
for( j=i-1; j>=0; j-- ) {
|
|
|
|
netnum[j] = (unsigned char)(unum & 0xffU);
|
1999-11-27 07:32:18 +08:00
|
|
|
unum >>= 8;
|
|
|
|
}
|
|
|
|
|
2005-09-13 15:51:51 +08:00
|
|
|
rc = ber_write( ber, (char *) netnum, i, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* length of tag + length + contents */
|
1999-11-27 07:32:18 +08:00
|
|
|
return rc == i ? taglen + lenlen + i : -1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_put_enum(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t num,
|
|
|
|
ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( tag == LBER_DEFAULT ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
tag = LBER_ENUMERATED;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return ber_put_int_or_enum( ber, num, tag );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_put_int(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t num,
|
|
|
|
ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( tag == LBER_DEFAULT ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
tag = LBER_INTEGER;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return ber_put_int_or_enum( ber, num, tag );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_put_ostring(
|
|
|
|
BerElement *ber,
|
|
|
|
LDAP_CONST char *str,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t len,
|
|
|
|
ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2002-04-11 16:47:37 +08:00
|
|
|
int taglen, lenlen, rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( str != NULL );
|
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( tag == LBER_DEFAULT ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
tag = LBER_OCTETSTRING;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
|
2000-10-11 10:43:14 +08:00
|
|
|
return -1;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
|
2003-07-08 11:46:20 +08:00
|
|
|
(ber_len_t) ber_write( ber, str, len, 0 ) != len )
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = -1;
|
|
|
|
} else {
|
|
|
|
/* return length of tag + length + contents */
|
|
|
|
rc = taglen + lenlen + len;
|
|
|
|
}
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-06-19 07:53:05 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
int
|
|
|
|
ber_put_berval(
|
|
|
|
BerElement *ber,
|
2002-01-06 02:54:04 +08:00
|
|
|
struct berval *bv,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t tag )
|
1999-05-19 09:12:33 +08:00
|
|
|
{
|
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
1999-06-29 04:12:30 +08:00
|
|
|
if( bv == NULL || bv->bv_len == 0 ) {
|
|
|
|
return ber_put_ostring( ber, "", (ber_len_t) 0, tag );
|
1999-05-19 09:12:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ber_put_ostring( ber, bv->bv_val, bv->bv_len, tag );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_put_string(
|
|
|
|
BerElement *ber,
|
|
|
|
LDAP_CONST char *str,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( str != NULL );
|
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return ber_put_ostring( ber, str, strlen( str ), tag );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_put_bitstring(
|
|
|
|
BerElement *ber,
|
|
|
|
LDAP_CONST char *str,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t blen /* in bits */,
|
|
|
|
ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2002-04-11 16:47:37 +08:00
|
|
|
int taglen, lenlen;
|
|
|
|
ber_len_t len;
|
1998-08-09 08:43:13 +08:00
|
|
|
unsigned char unusedbits;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( str != NULL );
|
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( tag == LBER_DEFAULT ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
tag = LBER_BITSTRING;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
len = ( blen + 7 ) / 8;
|
1998-10-25 09:41:42 +08:00
|
|
|
unusedbits = (unsigned char) ((len * 8) - blen);
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (ber_len_t) ber_write( ber, str, len, 0 ) != len ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* return length of tag + length + unused bit count + contents */
|
2000-10-11 10:43:14 +08:00
|
|
|
return taglen + 1 + lenlen + len;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_put_null( BerElement *ber, ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2002-04-11 16:47:37 +08:00
|
|
|
int taglen;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( tag == LBER_DEFAULT ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
tag = LBER_NULL;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( ber_put_len( ber, 0, 0 ) != 1 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return taglen + 1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_put_boolean(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t boolval,
|
|
|
|
ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2002-04-11 16:47:37 +08:00
|
|
|
int taglen;
|
2002-02-14 05:26:24 +08:00
|
|
|
unsigned char c;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( tag == LBER_DEFAULT )
|
|
|
|
tag = LBER_BOOLEAN;
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( ber_put_len( ber, 1, 0 ) != 1 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2002-02-14 05:26:24 +08:00
|
|
|
c = boolval ? (unsigned char) ~0U : (unsigned char) 0U;
|
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( ber_write( ber, (char *) &c, 1, 0 ) != 1 ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return taglen + 2;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define FOUR_BYTE_LEN 5
|
|
|
|
|
|
|
|
static int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_start_seqorset(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
Seqorset *new;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
2003-04-10 14:14:16 +08:00
|
|
|
new = (Seqorset *) ber_memcalloc_x( 1, sizeof(Seqorset), ber->ber_memctx );
|
1999-05-29 09:19:14 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( new == NULL ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1999-05-29 09:19:14 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
new->sos_ber = ber;
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( ber->ber_sos == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
new->sos_first = ber->ber_ptr;
|
2000-10-11 10:43:14 +08:00
|
|
|
} else {
|
1998-08-09 08:43:13 +08:00
|
|
|
new->sos_first = ber->ber_sos->sos_ptr;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* Set aside room for a 4 byte length field */
|
|
|
|
new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
|
|
|
|
new->sos_tag = tag;
|
|
|
|
|
|
|
|
new->sos_next = ber->ber_sos;
|
|
|
|
ber->ber_sos = new;
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_start_seq( BerElement *ber, ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( tag == LBER_DEFAULT ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
tag = LBER_SEQUENCE;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return ber_start_seqorset( ber, tag );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_start_set( BerElement *ber, ber_tag_t tag )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( tag == LBER_DEFAULT ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
tag = LBER_SET;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return ber_start_seqorset( ber, tag );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ber_put_seqorset( BerElement *ber )
|
|
|
|
{
|
1999-12-03 14:26:41 +08:00
|
|
|
int rc;
|
1999-11-27 07:32:18 +08:00
|
|
|
ber_len_t len;
|
|
|
|
unsigned char netlen[sizeof(ber_len_t)];
|
2002-04-11 16:47:37 +08:00
|
|
|
int taglen;
|
|
|
|
ber_len_t lenlen;
|
1999-05-19 14:13:44 +08:00
|
|
|
unsigned char ltag = 0x80U + FOUR_BYTE_LEN - 1;
|
1998-08-09 08:43:13 +08:00
|
|
|
Seqorset *next;
|
|
|
|
Seqorset **sos = &ber->ber_sos;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
2002-01-04 07:25:25 +08:00
|
|
|
if( *sos == NULL ) return -1;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* If this is the toplevel sequence or set, we need to actually
|
2001-01-18 01:08:44 +08:00
|
|
|
* write the stuff out. Otherwise, it's already been put in
|
1998-08-09 08:43:13 +08:00
|
|
|
* the appropriate buffer and will be written when the toplevel
|
|
|
|
* one is written. In this case all we need to do is update the
|
|
|
|
* length and tag.
|
|
|
|
*/
|
|
|
|
|
|
|
|
len = (*sos)->sos_clen;
|
1999-11-27 07:32:18 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( sizeof(ber_len_t) > 4 && len > 0xffffffffUL ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( ber->ber_options & LBER_USE_DER ) {
|
|
|
|
lenlen = ber_calc_lenlen( len );
|
1999-11-27 07:32:18 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
lenlen = FOUR_BYTE_LEN;
|
|
|
|
}
|
|
|
|
|
1999-11-27 07:32:18 +08:00
|
|
|
if( lenlen > 1 ) {
|
2005-09-13 15:51:51 +08:00
|
|
|
int i;
|
|
|
|
ber_len_t j = len;
|
|
|
|
for( i=lenlen-2; i >= 0; i-- ) {
|
|
|
|
netlen[i] = j & 0xffU;
|
|
|
|
j >>= 8;
|
1999-11-27 07:32:18 +08:00
|
|
|
}
|
|
|
|
} else {
|
2005-09-13 15:51:51 +08:00
|
|
|
netlen[0] = (unsigned char)(len & 0x7fU);
|
1999-11-27 07:32:18 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
if ( (next = (*sos)->sos_next) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
/* write the tag */
|
2004-07-01 06:28:15 +08:00
|
|
|
if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
2004-07-01 06:28:15 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( ber->ber_options & LBER_USE_DER ) {
|
|
|
|
/* Write the length in the minimum # of octets */
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( ber_put_len( ber, len, 1 ) == -1 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if (lenlen != FOUR_BYTE_LEN) {
|
|
|
|
/*
|
|
|
|
* We set aside FOUR_BYTE_LEN bytes for
|
|
|
|
* the length field. Move the data if
|
|
|
|
* we don't actually need that much
|
|
|
|
*/
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY( (*sos)->sos_first + taglen +
|
1998-08-09 08:43:13 +08:00
|
|
|
lenlen, (*sos)->sos_first + taglen +
|
|
|
|
FOUR_BYTE_LEN, len );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Fill FOUR_BYTE_LEN bytes for length field */
|
|
|
|
/* one byte of length length */
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* the length itself */
|
2005-09-13 15:51:51 +08:00
|
|
|
rc = ber_write( ber, (char *) netlen, FOUR_BYTE_LEN-1, 1 );
|
1999-11-27 07:32:18 +08:00
|
|
|
|
|
|
|
if( rc != FOUR_BYTE_LEN - 1 ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return -1;
|
1999-11-27 07:32:18 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
/* The ber_ptr is at the set/seq start - move it to the end */
|
|
|
|
(*sos)->sos_ber->ber_ptr += len;
|
1999-11-27 07:32:18 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
2002-04-11 16:47:37 +08:00
|
|
|
int i;
|
1999-11-27 07:32:18 +08:00
|
|
|
unsigned char nettag[sizeof(ber_tag_t)];
|
|
|
|
ber_tag_t tmptag = (*sos)->sos_tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-03-16 03:47:22 +08:00
|
|
|
if( ber->ber_sos->sos_ptr > ber->ber_end ) {
|
|
|
|
/* The sos_ptr exceeds the end of the BerElement
|
|
|
|
* this can happen, for example, when the sos_ptr
|
|
|
|
* is near the end and no data was written for the
|
2001-01-18 01:08:44 +08:00
|
|
|
* 'V'. We must realloc the BerElement to ensure
|
2000-03-16 03:47:22 +08:00
|
|
|
* we don't overwrite the buffer when writing
|
|
|
|
* the tag and length fields.
|
|
|
|
*/
|
|
|
|
ber_len_t ext = ber->ber_sos->sos_ptr - ber->ber_end;
|
|
|
|
|
|
|
|
if( ber_realloc( ber, ext ) != 0 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* the tag */
|
1999-11-27 07:32:18 +08:00
|
|
|
taglen = ber_calc_taglen( tmptag );
|
|
|
|
|
2005-09-13 15:51:51 +08:00
|
|
|
for( i = taglen-1; i >= 0; i-- ) {
|
|
|
|
nettag[i] = (unsigned char)(tmptag & 0xffU);
|
1999-11-27 07:32:18 +08:00
|
|
|
tmptag >>= 8;
|
|
|
|
}
|
|
|
|
|
2005-09-13 15:51:51 +08:00
|
|
|
AC_FMEMCPY( (*sos)->sos_first, nettag, taglen );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( ber->ber_options & LBER_USE_DER ) {
|
1998-10-25 09:41:42 +08:00
|
|
|
ltag = (lenlen == 1)
|
|
|
|
? (unsigned char) len
|
1999-06-19 07:53:05 +08:00
|
|
|
: (unsigned char) (0x80U + (lenlen - 1));
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* one byte of length length */
|
2000-07-28 09:07:07 +08:00
|
|
|
(*sos)->sos_first[1] = ltag;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( ber->ber_options & LBER_USE_DER ) {
|
|
|
|
if (lenlen > 1) {
|
|
|
|
/* Write the length itself */
|
2005-09-13 15:51:51 +08:00
|
|
|
AC_FMEMCPY( (*sos)->sos_first + 2, netlen, lenlen - 1 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
if (lenlen != FOUR_BYTE_LEN) {
|
|
|
|
/*
|
|
|
|
* We set aside FOUR_BYTE_LEN bytes for
|
|
|
|
* the length field. Move the data if
|
|
|
|
* we don't actually need that much
|
|
|
|
*/
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_FMEMCPY( (*sos)->sos_first + taglen +
|
1998-08-09 08:43:13 +08:00
|
|
|
lenlen, (*sos)->sos_first + taglen +
|
|
|
|
FOUR_BYTE_LEN, len );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* the length itself */
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_FMEMCPY( (*sos)->sos_first + taglen + 1,
|
2005-09-13 15:51:51 +08:00
|
|
|
netlen, FOUR_BYTE_LEN - 1 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
next->sos_clen += (taglen + lenlen + len);
|
|
|
|
next->sos_ptr += (taglen + lenlen + len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we're done with this seqorset, so free it up */
|
2003-04-10 14:14:16 +08:00
|
|
|
ber_memfree_x( (char *) (*sos), ber->ber_memctx );
|
1998-08-09 08:43:13 +08:00
|
|
|
*sos = next;
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return taglen + lenlen + len;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ber_put_seq( BerElement *ber )
|
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return ber_put_seqorset( ber );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ber_put_set( BerElement *ber )
|
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return ber_put_seqorset( ber );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
/* N tag */
|
|
|
|
static ber_tag_t lber_int_null = 0;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* VARARGS */
|
|
|
|
int
|
1999-08-24 08:27:53 +08:00
|
|
|
ber_printf( BerElement *ber, LDAP_CONST char *fmt, ... )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *s, **ss;
|
1999-05-19 09:12:33 +08:00
|
|
|
struct berval *bv, **bvp;
|
1999-06-19 07:53:05 +08:00
|
|
|
int rc;
|
|
|
|
ber_int_t i;
|
|
|
|
ber_len_t len;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( fmt != NULL );
|
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
1999-08-24 08:27:53 +08:00
|
|
|
va_start( ap, fmt );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
for ( rc = 0; *fmt && rc != -1; fmt++ ) {
|
|
|
|
switch ( *fmt ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
case '!': { /* hook */
|
|
|
|
BEREncodeCallback *f;
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
f = va_arg( ap, BEREncodeCallback * );
|
|
|
|
p = va_arg( ap, void * );
|
|
|
|
|
|
|
|
rc = (*f)( ber, p );
|
|
|
|
} break;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
case 'b': /* boolean */
|
1999-06-19 07:53:05 +08:00
|
|
|
i = va_arg( ap, ber_int_t );
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = ber_put_boolean( ber, i, ber->ber_tag );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i': /* int */
|
1999-06-19 07:53:05 +08:00
|
|
|
i = va_arg( ap, ber_int_t );
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = ber_put_int( ber, i, ber->ber_tag );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e': /* enumeration */
|
1999-06-19 07:53:05 +08:00
|
|
|
i = va_arg( ap, ber_int_t );
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = ber_put_enum( ber, i, ber->ber_tag );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n': /* null */
|
|
|
|
rc = ber_put_null( ber, ber->ber_tag );
|
|
|
|
break;
|
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
case 'N': /* Debug NULL */
|
|
|
|
if( lber_int_null != 0 ) {
|
|
|
|
/* Insert NULL to ensure peer ignores unknown tags */
|
|
|
|
rc = ber_put_null( ber, lber_int_null );
|
|
|
|
} else {
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
case 'o': /* octet string (non-null terminated) */
|
|
|
|
s = va_arg( ap, char * );
|
1999-06-19 07:53:05 +08:00
|
|
|
len = va_arg( ap, ber_len_t );
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = ber_put_ostring( ber, s, len, ber->ber_tag );
|
|
|
|
break;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
case 'O': /* berval octet string */
|
|
|
|
bv = va_arg( ap, struct berval * );
|
|
|
|
if( bv == NULL ) break;
|
|
|
|
rc = ber_put_berval( ber, bv, ber->ber_tag );
|
|
|
|
break;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
case 's': /* string */
|
|
|
|
s = va_arg( ap, char * );
|
|
|
|
rc = ber_put_string( ber, s, ber->ber_tag );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B': /* bit string */
|
1999-11-08 23:38:59 +08:00
|
|
|
case 'X': /* bit string (deprecated) */
|
1998-08-09 08:43:13 +08:00
|
|
|
s = va_arg( ap, char * );
|
|
|
|
len = va_arg( ap, int ); /* in bits */
|
|
|
|
rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't': /* tag for the next element */
|
1999-06-19 07:53:05 +08:00
|
|
|
ber->ber_tag = va_arg( ap, ber_tag_t );
|
1998-08-09 08:43:13 +08:00
|
|
|
ber->ber_usertag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v': /* vector of strings */
|
|
|
|
if ( (ss = va_arg( ap, char ** )) == NULL )
|
|
|
|
break;
|
|
|
|
for ( i = 0; ss[i] != NULL; i++ ) {
|
|
|
|
if ( (rc = ber_put_string( ber, ss[i],
|
|
|
|
ber->ber_tag )) == -1 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V': /* sequences of strings + lengths */
|
1999-05-19 09:12:33 +08:00
|
|
|
if ( (bvp = va_arg( ap, struct berval ** )) == NULL )
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
1999-05-19 09:12:33 +08:00
|
|
|
for ( i = 0; bvp[i] != NULL; i++ ) {
|
1999-06-29 04:12:30 +08:00
|
|
|
if ( (rc = ber_put_berval( ber, bvp[i],
|
|
|
|
ber->ber_tag )) == -1 )
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-01-14 09:43:17 +08:00
|
|
|
case 'W': /* BerVarray */
|
|
|
|
if ( (bv = va_arg( ap, BerVarray )) == NULL )
|
2002-01-02 16:50:07 +08:00
|
|
|
break;
|
|
|
|
for ( i = 0; bv[i].bv_val != NULL; i++ ) {
|
|
|
|
if ( (rc = ber_put_berval( ber, &bv[i],
|
|
|
|
ber->ber_tag )) == -1 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
case '{': /* begin sequence */
|
|
|
|
rc = ber_start_seq( ber, ber->ber_tag );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '}': /* end sequence */
|
|
|
|
rc = ber_put_seqorset( ber );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '[': /* begin set */
|
|
|
|
rc = ber_start_set( ber, ber->ber_tag );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ']': /* end set */
|
|
|
|
rc = ber_put_seqorset( ber );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1998-12-22 09:34:01 +08:00
|
|
|
if( ber->ber_debug ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
|
1998-12-22 09:34:01 +08:00
|
|
|
"ber_printf: unknown fmt %c\n", *fmt );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( ber->ber_usertag == 0 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ber->ber_tag = LBER_DEFAULT;
|
2003-07-08 11:46:20 +08:00
|
|
|
} else {
|
1998-08-09 08:43:13 +08:00
|
|
|
ber->ber_usertag = 0;
|
2003-07-08 11:46:20 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
va_end( ap );
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|