17c478bd9Sstevel@tonic-gate /* 2*e6a862fbSDouglas Leavitt * CDDL HEADER START 3*e6a862fbSDouglas Leavitt * 4*e6a862fbSDouglas Leavitt * The contents of this file are subject to the terms of the 5*e6a862fbSDouglas Leavitt * Common Development and Distribution License (the "License"). 6*e6a862fbSDouglas Leavitt * You may not use this file except in compliance with the License. 7*e6a862fbSDouglas Leavitt * 8*e6a862fbSDouglas Leavitt * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*e6a862fbSDouglas Leavitt * or http://www.opensolaris.org/os/licensing. 10*e6a862fbSDouglas Leavitt * See the License for the specific language governing permissions 11*e6a862fbSDouglas Leavitt * and limitations under the License. 12*e6a862fbSDouglas Leavitt * 13*e6a862fbSDouglas Leavitt * When distributing Covered Code, include this CDDL HEADER in each 14*e6a862fbSDouglas Leavitt * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*e6a862fbSDouglas Leavitt * If applicable, add the following below this CDDL HEADER, with the 16*e6a862fbSDouglas Leavitt * fields enclosed by brackets "[]" replaced with your own identifying 17*e6a862fbSDouglas Leavitt * information: Portions Copyright [yyyy] [name of copyright owner] 18*e6a862fbSDouglas Leavitt * 19*e6a862fbSDouglas Leavitt * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 22*e6a862fbSDouglas Leavitt /* 23*e6a862fbSDouglas Leavitt * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24*e6a862fbSDouglas Leavitt * Use is subject to license terms. 25*e6a862fbSDouglas Leavitt */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 297c478bd9Sstevel@tonic-gate * 307c478bd9Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public License 317c478bd9Sstevel@tonic-gate * Version 1.0 (the "NPL"); you may not use this file except in 327c478bd9Sstevel@tonic-gate * compliance with the NPL. You may obtain a copy of the NPL at 337c478bd9Sstevel@tonic-gate * http://www.mozilla.org/NPL/ 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * Software distributed under the NPL is distributed on an "AS IS" basis, 367c478bd9Sstevel@tonic-gate * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL 377c478bd9Sstevel@tonic-gate * for the specific language governing rights and limitations under the 387c478bd9Sstevel@tonic-gate * NPL. 397c478bd9Sstevel@tonic-gate * 407c478bd9Sstevel@tonic-gate * The Initial Developer of this code under the NPL is Netscape 417c478bd9Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are 427c478bd9Sstevel@tonic-gate * Copyright (C) 1998 Netscape Communications Corporation. All Rights 437c478bd9Sstevel@tonic-gate * Reserved. 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* 477c478bd9Sstevel@tonic-gate * Copyright (c) 1990 Regents of the University of Michigan. 487c478bd9Sstevel@tonic-gate * All rights reserved. 497c478bd9Sstevel@tonic-gate * 507c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted 517c478bd9Sstevel@tonic-gate * provided that this notice is preserved and that due credit is given 527c478bd9Sstevel@tonic-gate * to the University of Michigan at Ann Arbor. The name of the University 537c478bd9Sstevel@tonic-gate * may not be used to endorse or promote products derived from this 547c478bd9Sstevel@tonic-gate * software without specific prior written permission. This software 557c478bd9Sstevel@tonic-gate * is provided ``as is'' without express or implied warranty. 567c478bd9Sstevel@tonic-gate */ 577c478bd9Sstevel@tonic-gate /* io.c - ber general i/o routines */ 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #include "lber-int.h" 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #define bergetc( sb, len ) ( sb->sb_ber.ber_end > sb->sb_ber.ber_ptr ? \ 627c478bd9Sstevel@tonic-gate (unsigned char)*sb->sb_ber.ber_ptr++ : \ 637c478bd9Sstevel@tonic-gate ber_filbuf( sb, len )) 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate # ifdef macintosh 667c478bd9Sstevel@tonic-gate /* 677c478bd9Sstevel@tonic-gate * MacTCP/OpenTransport 687c478bd9Sstevel@tonic-gate */ 697c478bd9Sstevel@tonic-gate # define read( s, b, l ) tcpread( s, 0, (unsigned char *)b, l, NULL ) 707c478bd9Sstevel@tonic-gate # define MAX_WRITE 65535 717c478bd9Sstevel@tonic-gate # define BerWrite( sb, b, l ) tcpwrite( sb->sb_sd, (unsigned char *)(b), (l<MAX_WRITE)? l : MAX_WRITE ) 727c478bd9Sstevel@tonic-gate # else /* macintosh */ 737c478bd9Sstevel@tonic-gate # if defined(_WIN32) || defined(_WINDOWS) || defined(XP_OS2) 747c478bd9Sstevel@tonic-gate /* 757c478bd9Sstevel@tonic-gate * 32-bit Windows Socket API (under Windows NT or Windows 95) 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate # define read( s, b, l ) recv( s, b, l, 0 ) 787c478bd9Sstevel@tonic-gate # define BerWrite( s, b, l ) send( s->sb_sd, b, l, 0 ) 797c478bd9Sstevel@tonic-gate # else /* _WIN32 */ 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * everything else (Unix/BSD 4.3 socket API) 827c478bd9Sstevel@tonic-gate */ 837c478bd9Sstevel@tonic-gate # define BerWrite( sb, b, l ) write( sb->sb_sd, b, l ) 847c478bd9Sstevel@tonic-gate # define udp_read( sb, b, l, al ) recvfrom(sb->sb_sd, (char *)b, l, 0, \ 857c478bd9Sstevel@tonic-gate (struct sockaddr *)sb->sb_fromaddr, \ 867c478bd9Sstevel@tonic-gate (al = sizeof(struct sockaddr), &al)) 877c478bd9Sstevel@tonic-gate # define udp_write( sb, b, l ) sendto(sb->sb_sd, (char *)(b), l, 0, \ 887c478bd9Sstevel@tonic-gate (struct sockaddr *)sb->sb_useaddr, sizeof(struct sockaddr)) 897c478bd9Sstevel@tonic-gate # endif /* _WIN32 */ 907c478bd9Sstevel@tonic-gate # endif /* macintosh */ 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate #ifndef udp_read 937c478bd9Sstevel@tonic-gate #define udp_read( sb, b, l, al ) CLDAP NOT SUPPORTED 947c478bd9Sstevel@tonic-gate #define udp_write( sb, b, l ) CLDAP NOT SUPPORTED 957c478bd9Sstevel@tonic-gate #endif /* udp_read */ 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate #define EXBUFSIZ 1024 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG 1007c478bd9Sstevel@tonic-gate int lber_debug; 1017c478bd9Sstevel@tonic-gate #endif 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* 1047c478bd9Sstevel@tonic-gate * function prototypes 1057c478bd9Sstevel@tonic-gate */ 1067c478bd9Sstevel@tonic-gate static void nslberi_install_compat_io_fns( Sockbuf *sb ); 1077c478bd9Sstevel@tonic-gate static int nslberi_extread_compat( int s, void *buf, int len, 1087c478bd9Sstevel@tonic-gate struct lextiof_socket_private *arg ); 1097c478bd9Sstevel@tonic-gate static int nslberi_extwrite_compat( int s, const void *buf, int len, 1107c478bd9Sstevel@tonic-gate struct lextiof_socket_private *arg ); 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate /* 1147c478bd9Sstevel@tonic-gate * internal global structure for memory allocation callback functions 1157c478bd9Sstevel@tonic-gate */ 1167c478bd9Sstevel@tonic-gate static struct lber_memalloc_fns nslberi_memalloc_fns; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * buffered read from "sb". 1217c478bd9Sstevel@tonic-gate * returns value of first character read on success and -1 on error. 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate static int 1247c478bd9Sstevel@tonic-gate ber_filbuf( Sockbuf *sb, ber_slen_t len ) 1257c478bd9Sstevel@tonic-gate { 1267c478bd9Sstevel@tonic-gate ssize_t rc; 1277c478bd9Sstevel@tonic-gate #ifdef CLDAP 1287c478bd9Sstevel@tonic-gate int addrlen; 1297c478bd9Sstevel@tonic-gate #endif /* CLDAP */ 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate if ( sb->sb_ber.ber_buf == NULL ) { 1327c478bd9Sstevel@tonic-gate if ( (sb->sb_ber.ber_buf = (char *)NSLBERI_MALLOC( 1337c478bd9Sstevel@tonic-gate READBUFSIZ )) == NULL ) { 1347c478bd9Sstevel@tonic-gate return( -1 ); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate sb->sb_ber.ber_flags &= ~LBER_FLAG_NO_FREE_BUFFER; 1377c478bd9Sstevel@tonic-gate sb->sb_ber.ber_ptr = sb->sb_ber.ber_buf; 1387c478bd9Sstevel@tonic-gate sb->sb_ber.ber_end = sb->sb_ber.ber_buf; 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate if ( sb->sb_naddr > 0 ) { 1427c478bd9Sstevel@tonic-gate #ifdef CLDAP 1437c478bd9Sstevel@tonic-gate rc = udp_read(sb, sb->sb_ber.ber_buf, READBUFSIZ, addrlen ); 1447c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG 1457c478bd9Sstevel@tonic-gate if ( lber_debug ) { 1467c478bd9Sstevel@tonic-gate char msg[80]; 1477c478bd9Sstevel@tonic-gate sprintf( msg, "ber_filbuf udp_read %d bytes\n", 1487c478bd9Sstevel@tonic-gate rc ); 1497c478bd9Sstevel@tonic-gate ber_err_print( msg ); 1507c478bd9Sstevel@tonic-gate if ( lber_debug > 1 && rc > 0 ) 1517c478bd9Sstevel@tonic-gate lber_bprint( sb->sb_ber.ber_buf, rc ); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate #endif /* LDAP_DEBUG */ 1547c478bd9Sstevel@tonic-gate #else /* CLDAP */ 1557c478bd9Sstevel@tonic-gate rc = -1; 1567c478bd9Sstevel@tonic-gate #endif /* CLDAP */ 1577c478bd9Sstevel@tonic-gate } else { 1587c478bd9Sstevel@tonic-gate if ( sb->sb_ext_io_fns.lbextiofn_read != NULL ) { 1597c478bd9Sstevel@tonic-gate rc = sb->sb_ext_io_fns.lbextiofn_read( 1607c478bd9Sstevel@tonic-gate sb->sb_sd, sb->sb_ber.ber_buf, 1617c478bd9Sstevel@tonic-gate ((sb->sb_options & LBER_SOCKBUF_OPT_NO_READ_AHEAD) 1627c478bd9Sstevel@tonic-gate && (len < READBUFSIZ)) ? len : READBUFSIZ, 1637c478bd9Sstevel@tonic-gate sb->sb_ext_io_fns.lbextiofn_socket_arg ); 1647c478bd9Sstevel@tonic-gate } else { 1657c478bd9Sstevel@tonic-gate rc = read( sb->sb_sd, sb->sb_ber.ber_buf, 1667c478bd9Sstevel@tonic-gate ((sb->sb_options & LBER_SOCKBUF_OPT_NO_READ_AHEAD) 1677c478bd9Sstevel@tonic-gate && (len < READBUFSIZ)) ? len : READBUFSIZ ); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate if ( rc > 0 ) { 1727c478bd9Sstevel@tonic-gate sb->sb_ber.ber_ptr = sb->sb_ber.ber_buf + 1; 1737c478bd9Sstevel@tonic-gate sb->sb_ber.ber_end = sb->sb_ber.ber_buf + rc; 1747c478bd9Sstevel@tonic-gate return( (unsigned char)*sb->sb_ber.ber_buf ); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate return( -1 ); 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate static ber_int_t 1827c478bd9Sstevel@tonic-gate BerRead( Sockbuf *sb, char *buf, ber_slen_t len ) 1837c478bd9Sstevel@tonic-gate { 1847c478bd9Sstevel@tonic-gate int c; 1857c478bd9Sstevel@tonic-gate ber_int_t nread = 0; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate while ( len > 0 ) { 1887c478bd9Sstevel@tonic-gate if ( (c = bergetc( sb, len )) < 0 ) { 1897c478bd9Sstevel@tonic-gate if ( nread > 0 ) 1907c478bd9Sstevel@tonic-gate break; 1917c478bd9Sstevel@tonic-gate return( c ); 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate *buf++ = c; 1947c478bd9Sstevel@tonic-gate nread++; 1957c478bd9Sstevel@tonic-gate len--; 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate return( nread ); 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate /* 2037c478bd9Sstevel@tonic-gate * Note: ber_read() only uses the ber_end and ber_ptr elements of ber. 2047c478bd9Sstevel@tonic-gate * Functions like ber_get_tag(), ber_skip_tag, and ber_peek_tag() rely on 2057c478bd9Sstevel@tonic-gate * that fact, so if this code is changed to use any additional elements of 2067c478bd9Sstevel@tonic-gate * the ber structure, those functions will need to be changed as well. 2077c478bd9Sstevel@tonic-gate */ 2087c478bd9Sstevel@tonic-gate ber_int_t 2097c478bd9Sstevel@tonic-gate LDAP_CALL 2107c478bd9Sstevel@tonic-gate ber_read( BerElement *ber, char *buf, ber_len_t len ) 2117c478bd9Sstevel@tonic-gate { 2127c478bd9Sstevel@tonic-gate ber_len_t actuallen; 2137c478bd9Sstevel@tonic-gate ber_uint_t nleft; 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate nleft = ber->ber_end - ber->ber_ptr; 2167c478bd9Sstevel@tonic-gate actuallen = nleft < len ? nleft : len; 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate SAFEMEMCPY( buf, ber->ber_ptr, (size_t)actuallen ); 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate ber->ber_ptr += actuallen; 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate return( (ber_int_t)actuallen ); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate /* 2267c478bd9Sstevel@tonic-gate * enlarge the ber buffer. 2277c478bd9Sstevel@tonic-gate * return 0 on success, -1 on error. 2287c478bd9Sstevel@tonic-gate */ 2297c478bd9Sstevel@tonic-gate int 2307c478bd9Sstevel@tonic-gate nslberi_ber_realloc( BerElement *ber, ber_len_t len ) 2317c478bd9Sstevel@tonic-gate { 2327c478bd9Sstevel@tonic-gate ber_uint_t need, have, total; 2337c478bd9Sstevel@tonic-gate size_t have_bytes; 2347c478bd9Sstevel@tonic-gate Seqorset *s; 2357c478bd9Sstevel@tonic-gate ber_int_t off; 2367c478bd9Sstevel@tonic-gate char *oldbuf; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate have_bytes = ber->ber_end - ber->ber_buf; 2397c478bd9Sstevel@tonic-gate have = have_bytes / EXBUFSIZ; 2407c478bd9Sstevel@tonic-gate need = (len < EXBUFSIZ ? 1 : (len + (EXBUFSIZ - 1)) / EXBUFSIZ); 2417c478bd9Sstevel@tonic-gate total = have * EXBUFSIZ + need * EXBUFSIZ; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate oldbuf = ber->ber_buf; 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate if (ber->ber_buf == NULL) { 2467c478bd9Sstevel@tonic-gate if ( (ber->ber_buf = (char *)NSLBERI_MALLOC( (size_t)total )) 2477c478bd9Sstevel@tonic-gate == NULL ) { 2487c478bd9Sstevel@tonic-gate return( -1 ); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate ber->ber_flags &= ~LBER_FLAG_NO_FREE_BUFFER; 2517c478bd9Sstevel@tonic-gate } else { 2527c478bd9Sstevel@tonic-gate if ( ber->ber_flags & LBER_FLAG_NO_FREE_BUFFER ) { 2537c478bd9Sstevel@tonic-gate /* transition to malloc'd buffer */ 2547c478bd9Sstevel@tonic-gate if ( (ber->ber_buf = (char *)NSLBERI_MALLOC( 2557c478bd9Sstevel@tonic-gate (size_t)total )) == NULL ) { 2567c478bd9Sstevel@tonic-gate return( -1 ); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate ber->ber_flags &= ~LBER_FLAG_NO_FREE_BUFFER; 2597c478bd9Sstevel@tonic-gate /* copy existing data into new malloc'd buffer */ 2607c478bd9Sstevel@tonic-gate SAFEMEMCPY( ber->ber_buf, oldbuf, have_bytes ); 2617c478bd9Sstevel@tonic-gate } else { 2627c478bd9Sstevel@tonic-gate if ( (ber->ber_buf = (char *)NSLBERI_REALLOC( 2637c478bd9Sstevel@tonic-gate ber->ber_buf,(size_t)total )) == NULL ) { 2647c478bd9Sstevel@tonic-gate return( -1 ); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate ber->ber_end = ber->ber_buf + total; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate /* 2727c478bd9Sstevel@tonic-gate * If the stinking thing was moved, we need to go through and 2737c478bd9Sstevel@tonic-gate * reset all the sos and ber pointers. Offsets would've been 2747c478bd9Sstevel@tonic-gate * a better idea... oh well. 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate if ( ber->ber_buf != oldbuf ) { 2787c478bd9Sstevel@tonic-gate ber->ber_ptr = ber->ber_buf + (ber->ber_ptr - oldbuf); 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate for ( s = ber->ber_sos; s != NULLSEQORSET; s = s->sos_next ) { 2817c478bd9Sstevel@tonic-gate off = s->sos_first - oldbuf; 2827c478bd9Sstevel@tonic-gate s->sos_first = ber->ber_buf + off; 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate off = s->sos_ptr - oldbuf; 2857c478bd9Sstevel@tonic-gate s->sos_ptr = ber->ber_buf + off; 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate return( 0 ); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate /* 2937c478bd9Sstevel@tonic-gate * returns "len" on success and -1 on failure. 2947c478bd9Sstevel@tonic-gate */ 2957c478bd9Sstevel@tonic-gate ber_int_t 2967c478bd9Sstevel@tonic-gate LDAP_CALL 2977c478bd9Sstevel@tonic-gate ber_write( BerElement *ber, char *buf, ber_len_t len, int nosos ) 2987c478bd9Sstevel@tonic-gate { 2997c478bd9Sstevel@tonic-gate if ( nosos || ber->ber_sos == NULL ) { 3007c478bd9Sstevel@tonic-gate if ( ber->ber_ptr + len > ber->ber_end ) { 3017c478bd9Sstevel@tonic-gate if ( nslberi_ber_realloc( ber, len ) != 0 ) 3027c478bd9Sstevel@tonic-gate return( -1 ); 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate SAFEMEMCPY( ber->ber_ptr, buf, (size_t)len ); 3057c478bd9Sstevel@tonic-gate ber->ber_ptr += len; 3067c478bd9Sstevel@tonic-gate return( len ); 3077c478bd9Sstevel@tonic-gate } else { 3087c478bd9Sstevel@tonic-gate if ( ber->ber_sos->sos_ptr + len > ber->ber_end ) { 3097c478bd9Sstevel@tonic-gate if ( nslberi_ber_realloc( ber, len ) != 0 ) 3107c478bd9Sstevel@tonic-gate return( -1 ); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate SAFEMEMCPY( ber->ber_sos->sos_ptr, buf, (size_t)len ); 3137c478bd9Sstevel@tonic-gate ber->ber_sos->sos_ptr += len; 3147c478bd9Sstevel@tonic-gate ber->ber_sos->sos_clen += len; 3157c478bd9Sstevel@tonic-gate return( len ); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate void 3207c478bd9Sstevel@tonic-gate LDAP_CALL 3217c478bd9Sstevel@tonic-gate ber_free( BerElement *ber, int freebuf ) 3227c478bd9Sstevel@tonic-gate { 3237c478bd9Sstevel@tonic-gate if ( ber != NULL ) { 3247c478bd9Sstevel@tonic-gate if ( freebuf && 3257c478bd9Sstevel@tonic-gate !(ber->ber_flags & LBER_FLAG_NO_FREE_BUFFER)) { 3267c478bd9Sstevel@tonic-gate NSLBERI_FREE(ber->ber_buf); 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate NSLBERI_FREE( (char *) ber ); 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate /* 3337c478bd9Sstevel@tonic-gate * return >= 0 on success, -1 on failure. 3347c478bd9Sstevel@tonic-gate */ 3357c478bd9Sstevel@tonic-gate int 3367c478bd9Sstevel@tonic-gate LDAP_CALL 3377c478bd9Sstevel@tonic-gate ber_flush( Sockbuf *sb, BerElement *ber, int freeit ) 3387c478bd9Sstevel@tonic-gate { 3397c478bd9Sstevel@tonic-gate ssize_t nwritten, towrite, rc; 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate if ( ber->ber_rwptr == NULL ) { 3427c478bd9Sstevel@tonic-gate ber->ber_rwptr = ber->ber_buf; 3437c478bd9Sstevel@tonic-gate } else if (ber->ber_rwptr >= ber->ber_end) { 3447c478bd9Sstevel@tonic-gate /* we will use the ber_rwptr to continue an exited flush, 3457c478bd9Sstevel@tonic-gate so if rwptr is not within the buffer we return an error. */ 3467c478bd9Sstevel@tonic-gate return( -1 ); 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate towrite = ber->ber_ptr - ber->ber_rwptr; 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG 3517c478bd9Sstevel@tonic-gate if ( lber_debug ) { 3527c478bd9Sstevel@tonic-gate char msg[80]; 3537c478bd9Sstevel@tonic-gate sprintf( msg, "ber_flush: %ld bytes to sd %ld%s\n", towrite, 3547c478bd9Sstevel@tonic-gate sb->sb_sd, ber->ber_rwptr != ber->ber_buf ? " (re-flush)" 3557c478bd9Sstevel@tonic-gate : "" ); 3567c478bd9Sstevel@tonic-gate ber_err_print( msg ); 3577c478bd9Sstevel@tonic-gate if ( lber_debug > 1 ) 3587c478bd9Sstevel@tonic-gate lber_bprint( ber->ber_rwptr, towrite ); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate #endif 3617c478bd9Sstevel@tonic-gate #if !defined(macintosh) && !defined(DOS) 3627c478bd9Sstevel@tonic-gate if ( sb->sb_options & (LBER_SOCKBUF_OPT_TO_FILE | LBER_SOCKBUF_OPT_TO_FILE_ONLY) ) { 3637c478bd9Sstevel@tonic-gate rc = write( sb->sb_copyfd, ber->ber_buf, towrite ); 3647c478bd9Sstevel@tonic-gate if ( sb->sb_options & LBER_SOCKBUF_OPT_TO_FILE_ONLY ) { 3657c478bd9Sstevel@tonic-gate return( (int)rc ); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate #endif 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate nwritten = 0; 3717c478bd9Sstevel@tonic-gate do { 3727c478bd9Sstevel@tonic-gate if (sb->sb_naddr > 0) { 3737c478bd9Sstevel@tonic-gate #ifdef CLDAP 3747c478bd9Sstevel@tonic-gate rc = udp_write( sb, ber->ber_buf + nwritten, 3757c478bd9Sstevel@tonic-gate (size_t)towrite ); 3767c478bd9Sstevel@tonic-gate #else /* CLDAP */ 3777c478bd9Sstevel@tonic-gate rc = -1; 3787c478bd9Sstevel@tonic-gate #endif /* CLDAP */ 3797c478bd9Sstevel@tonic-gate if ( rc <= 0 ) 3807c478bd9Sstevel@tonic-gate return( -1 ); 3817c478bd9Sstevel@tonic-gate /* fake error if write was not atomic */ 3827c478bd9Sstevel@tonic-gate if (rc < towrite) { 3837c478bd9Sstevel@tonic-gate #if !defined( macintosh ) && !defined( DOS ) 3847c478bd9Sstevel@tonic-gate errno = EMSGSIZE; /* For Win32, see portable.h */ 3857c478bd9Sstevel@tonic-gate #endif 3867c478bd9Sstevel@tonic-gate return( -1 ); 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate } else { 3897c478bd9Sstevel@tonic-gate if ( sb->sb_ext_io_fns.lbextiofn_write != NULL ) { 3907c478bd9Sstevel@tonic-gate if ( (rc = sb->sb_ext_io_fns.lbextiofn_write( 3917c478bd9Sstevel@tonic-gate sb->sb_sd, ber->ber_rwptr, (size_t)towrite, 3927c478bd9Sstevel@tonic-gate sb->sb_ext_io_fns.lbextiofn_socket_arg )) 3937c478bd9Sstevel@tonic-gate <= 0 ) { 3947c478bd9Sstevel@tonic-gate return( -1 ); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate } else { 3977c478bd9Sstevel@tonic-gate if ( (rc = BerWrite( sb, ber->ber_rwptr, 3987c478bd9Sstevel@tonic-gate (size_t) towrite )) <= 0 ) { 3997c478bd9Sstevel@tonic-gate return( -1 ); 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate towrite -= rc; 4047c478bd9Sstevel@tonic-gate nwritten += rc; 4057c478bd9Sstevel@tonic-gate ber->ber_rwptr += rc; 4067c478bd9Sstevel@tonic-gate } while ( towrite > 0 ); 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate if ( freeit ) 4097c478bd9Sstevel@tonic-gate ber_free( ber, 1 ); 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate return( 0 ); 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate /* we pre-allocate a buffer to save the extra malloc later */ 4167c478bd9Sstevel@tonic-gate BerElement * 4177c478bd9Sstevel@tonic-gate LDAP_CALL 4187c478bd9Sstevel@tonic-gate ber_alloc_t( int options ) 4197c478bd9Sstevel@tonic-gate { 4207c478bd9Sstevel@tonic-gate BerElement *ber; 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate if ( (ber = (BerElement*)NSLBERI_CALLOC( 1, 4237c478bd9Sstevel@tonic-gate sizeof(struct berelement) + EXBUFSIZ )) == NULL ) { 4247c478bd9Sstevel@tonic-gate return( NULL ); 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate /* 4287c478bd9Sstevel@tonic-gate * for compatibility with the C LDAP API standard, we recognize 4297c478bd9Sstevel@tonic-gate * LBER_USE_DER as LBER_OPT_USE_DER. See lber.h for a bit more info. 4307c478bd9Sstevel@tonic-gate */ 4317c478bd9Sstevel@tonic-gate if ( options & LBER_USE_DER ) { 4327c478bd9Sstevel@tonic-gate options &= ~LBER_USE_DER; 4337c478bd9Sstevel@tonic-gate options |= LBER_OPT_USE_DER; 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate ber->ber_tag = LBER_DEFAULT; 4377c478bd9Sstevel@tonic-gate ber->ber_options = options; 4387c478bd9Sstevel@tonic-gate ber->ber_buf = (char*)ber + sizeof(struct berelement); 4397c478bd9Sstevel@tonic-gate ber->ber_ptr = ber->ber_buf; 4407c478bd9Sstevel@tonic-gate ber->ber_end = ber->ber_buf + EXBUFSIZ; 4417c478bd9Sstevel@tonic-gate ber->ber_flags = LBER_FLAG_NO_FREE_BUFFER; 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate return( ber ); 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate BerElement * 4487c478bd9Sstevel@tonic-gate LDAP_CALL 4497c478bd9Sstevel@tonic-gate ber_alloc() 4507c478bd9Sstevel@tonic-gate { 4517c478bd9Sstevel@tonic-gate return( ber_alloc_t( 0 ) ); 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate BerElement * 4557c478bd9Sstevel@tonic-gate LDAP_CALL 4567c478bd9Sstevel@tonic-gate der_alloc() 4577c478bd9Sstevel@tonic-gate { 4587c478bd9Sstevel@tonic-gate return( ber_alloc_t( LBER_OPT_USE_DER ) ); 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate BerElement * 4627c478bd9Sstevel@tonic-gate LDAP_CALL 4637c478bd9Sstevel@tonic-gate ber_dup( BerElement *ber ) 4647c478bd9Sstevel@tonic-gate { 4657c478bd9Sstevel@tonic-gate BerElement *new; 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate if ( (new = ber_alloc()) == NULL ) 4687c478bd9Sstevel@tonic-gate return( NULL ); 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate *new = *ber; 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate return( new ); 4737c478bd9Sstevel@tonic-gate } 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate void 4777c478bd9Sstevel@tonic-gate LDAP_CALL 4787c478bd9Sstevel@tonic-gate ber_init_w_nullchar( BerElement *ber, int options ) 4797c478bd9Sstevel@tonic-gate { 4807c478bd9Sstevel@tonic-gate (void) memset( (char *)ber, '\0', sizeof(struct berelement) ); 4817c478bd9Sstevel@tonic-gate ber->ber_tag = LBER_DEFAULT; 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate /* 4847c478bd9Sstevel@tonic-gate * For compatibility with the C LDAP API standard, we recognize 4857c478bd9Sstevel@tonic-gate * LBER_USE_DER as LBER_OPT_USE_DER. See lber.h for a bit more info. 4867c478bd9Sstevel@tonic-gate */ 4877c478bd9Sstevel@tonic-gate if ( options & LBER_USE_DER ) { 4887c478bd9Sstevel@tonic-gate options &= ~LBER_USE_DER; 4897c478bd9Sstevel@tonic-gate options |= LBER_OPT_USE_DER; 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate ber->ber_options = options; 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate void 4977c478bd9Sstevel@tonic-gate LDAP_CALL 4987c478bd9Sstevel@tonic-gate ber_reset( BerElement *ber, int was_writing ) 4997c478bd9Sstevel@tonic-gate { 5007c478bd9Sstevel@tonic-gate if ( was_writing ) { 5017c478bd9Sstevel@tonic-gate ber->ber_end = ber->ber_ptr; 5027c478bd9Sstevel@tonic-gate ber->ber_ptr = ber->ber_buf; 5037c478bd9Sstevel@tonic-gate } else { 5047c478bd9Sstevel@tonic-gate ber->ber_ptr = ber->ber_end; 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate ber->ber_rwptr = NULL; 5087c478bd9Sstevel@tonic-gate } 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate void 5147c478bd9Sstevel@tonic-gate ber_dump( BerElement *ber, int inout ) 5157c478bd9Sstevel@tonic-gate { 5167c478bd9Sstevel@tonic-gate char msg[128]; 5177c478bd9Sstevel@tonic-gate sprintf( msg, "ber_dump: buf 0x%lx, ptr 0x%lx, rwptr 0x%lx, end 0x%lx\n", 5187c478bd9Sstevel@tonic-gate ber->ber_buf, ber->ber_ptr, ber->ber_rwptr, ber->ber_end ); 5197c478bd9Sstevel@tonic-gate ber_err_print( msg ); 5207c478bd9Sstevel@tonic-gate if ( inout == 1 ) { 5217c478bd9Sstevel@tonic-gate sprintf( msg, " current len %ld, contents:\n", 5227c478bd9Sstevel@tonic-gate ber->ber_end - ber->ber_ptr ); 5237c478bd9Sstevel@tonic-gate ber_err_print( msg ); 5247c478bd9Sstevel@tonic-gate lber_bprint( ber->ber_ptr, ber->ber_end - ber->ber_ptr ); 5257c478bd9Sstevel@tonic-gate } else { 5267c478bd9Sstevel@tonic-gate sprintf( msg, " current len %ld, contents:\n", 5277c478bd9Sstevel@tonic-gate ber->ber_ptr - ber->ber_buf ); 5287c478bd9Sstevel@tonic-gate ber_err_print( msg ); 5297c478bd9Sstevel@tonic-gate lber_bprint( ber->ber_buf, ber->ber_ptr - ber->ber_buf ); 5307c478bd9Sstevel@tonic-gate } 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate void 5347c478bd9Sstevel@tonic-gate ber_sos_dump( Seqorset *sos ) 5357c478bd9Sstevel@tonic-gate { 5367c478bd9Sstevel@tonic-gate char msg[80]; 5377c478bd9Sstevel@tonic-gate ber_err_print ( "*** sos dump ***\n" ); 5387c478bd9Sstevel@tonic-gate while ( sos != NULLSEQORSET ) { 5397c478bd9Sstevel@tonic-gate sprintf( msg, "ber_sos_dump: clen %ld first 0x%lx ptr 0x%lx\n", 5407c478bd9Sstevel@tonic-gate sos->sos_clen, sos->sos_first, sos->sos_ptr ); 5417c478bd9Sstevel@tonic-gate ber_err_print( msg ); 5427c478bd9Sstevel@tonic-gate sprintf( msg, " current len %ld contents:\n", 5437c478bd9Sstevel@tonic-gate sos->sos_ptr - sos->sos_first ); 5447c478bd9Sstevel@tonic-gate ber_err_print( msg ); 5457c478bd9Sstevel@tonic-gate lber_bprint( sos->sos_first, sos->sos_ptr - sos->sos_first ); 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate sos = sos->sos_next; 5487c478bd9Sstevel@tonic-gate } 5497c478bd9Sstevel@tonic-gate ber_err_print( "*** end dump ***\n" ); 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate #endif 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate /* return the tag - LBER_DEFAULT returned means trouble */ 5557c478bd9Sstevel@tonic-gate static ber_tag_t 5567c478bd9Sstevel@tonic-gate get_tag( Sockbuf *sb ) 5577c478bd9Sstevel@tonic-gate { 5587c478bd9Sstevel@tonic-gate unsigned char xbyte; 5597c478bd9Sstevel@tonic-gate ber_tag_t tag; 5607c478bd9Sstevel@tonic-gate char *tagp; 5617c478bd9Sstevel@tonic-gate int i; 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate if ( (i = BerRead( sb, (char *) &xbyte, 1 )) != 1 ) { 5647c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) { 5687c478bd9Sstevel@tonic-gate return( (ber_uint_t) xbyte ); 5697c478bd9Sstevel@tonic-gate } 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate tagp = (char *) &tag; 5727c478bd9Sstevel@tonic-gate tagp[0] = xbyte; 5737c478bd9Sstevel@tonic-gate for ( i = 1; i < sizeof(ber_int_t); i++ ) { 5747c478bd9Sstevel@tonic-gate if ( BerRead( sb, (char *) &xbyte, 1 ) != 1 ) 5757c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate tagp[i] = xbyte; 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate if ( ! (xbyte & LBER_MORE_TAG_MASK) ) 5807c478bd9Sstevel@tonic-gate break; 5817c478bd9Sstevel@tonic-gate } 5827c478bd9Sstevel@tonic-gate 5837c478bd9Sstevel@tonic-gate /* tag too big! */ 5847c478bd9Sstevel@tonic-gate if ( i == sizeof(ber_int_t) ) 5857c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate /* want leading, not trailing 0's */ 5887c478bd9Sstevel@tonic-gate return( tag >> (sizeof(ber_int_t) - i - 1) ); 5897c478bd9Sstevel@tonic-gate } 5907c478bd9Sstevel@tonic-gate 5917c478bd9Sstevel@tonic-gate ber_tag_t 5927c478bd9Sstevel@tonic-gate LDAP_CALL 5937c478bd9Sstevel@tonic-gate ber_get_next( Sockbuf *sb, ber_len_t *len, BerElement *ber ) 5947c478bd9Sstevel@tonic-gate { 5957c478bd9Sstevel@tonic-gate ber_tag_t tag = 0; 5967c478bd9Sstevel@tonic-gate ber_len_t netlen; 5977c478bd9Sstevel@tonic-gate ber_uint_t toread; 5987c478bd9Sstevel@tonic-gate unsigned char lc; 5997c478bd9Sstevel@tonic-gate ber_int_t rc; 6007c478bd9Sstevel@tonic-gate int noctets, diff; 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG 6037c478bd9Sstevel@tonic-gate if ( lber_debug ) 6047c478bd9Sstevel@tonic-gate ber_err_print( "ber_get_next\n" ); 6057c478bd9Sstevel@tonic-gate #endif 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate /* 6087c478bd9Sstevel@tonic-gate * Any ber element looks like this: tag length contents. 6097c478bd9Sstevel@tonic-gate * Assuming everything's ok, we return the tag byte (we 6107c478bd9Sstevel@tonic-gate * can assume a single byte), return the length in len, 6117c478bd9Sstevel@tonic-gate * and the rest of the undecoded element in buf. 6127c478bd9Sstevel@tonic-gate * 6137c478bd9Sstevel@tonic-gate * Assumptions: 6147c478bd9Sstevel@tonic-gate * 1) small tags (less than 128) 6157c478bd9Sstevel@tonic-gate * 2) definite lengths 6167c478bd9Sstevel@tonic-gate * 3) primitive encodings used whenever possible 6177c478bd9Sstevel@tonic-gate */ 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate /* 6207c478bd9Sstevel@tonic-gate * first time through - malloc the buffer, set up ptrs, and 6217c478bd9Sstevel@tonic-gate * read the tag and the length and as much of the rest as we can 6227c478bd9Sstevel@tonic-gate */ 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate if ( ber->ber_rwptr == NULL ) { 6257c478bd9Sstevel@tonic-gate /* 6267c478bd9Sstevel@tonic-gate * First, we read the tag. 6277c478bd9Sstevel@tonic-gate */ 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate if ( (tag = get_tag( sb )) == LBER_DEFAULT ) { 6307c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate ber->ber_tag = tag; 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate /* 6357c478bd9Sstevel@tonic-gate * Next, read the length. The first byte contains the length 6367c478bd9Sstevel@tonic-gate * of the length. If bit 8 is set, the length is the long 6377c478bd9Sstevel@tonic-gate * form, otherwise it's the short form. We don't allow a 6387c478bd9Sstevel@tonic-gate * length that's greater than what we can hold in an unsigned 6397c478bd9Sstevel@tonic-gate * long. 6407c478bd9Sstevel@tonic-gate */ 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate *len = netlen = 0; 6437c478bd9Sstevel@tonic-gate if ( BerRead( sb, (char *) &lc, 1 ) != 1 ) { 6447c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate if ( lc & 0x80 ) { 6477c478bd9Sstevel@tonic-gate noctets = (lc & 0x7f); 6487c478bd9Sstevel@tonic-gate if ( noctets > sizeof(ber_uint_t) ) 6497c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 6507c478bd9Sstevel@tonic-gate diff = sizeof(ber_uint_t) - noctets; 6517c478bd9Sstevel@tonic-gate if ( BerRead( sb, (char *) &netlen + diff, noctets ) != 6527c478bd9Sstevel@tonic-gate noctets ) { 6537c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate *len = LBER_NTOHL( netlen ); 6567c478bd9Sstevel@tonic-gate } else { 6577c478bd9Sstevel@tonic-gate *len = lc; 6587c478bd9Sstevel@tonic-gate } 6597c478bd9Sstevel@tonic-gate ber->ber_len = *len; 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate /* 6627c478bd9Sstevel@tonic-gate * Finally, malloc a buffer for the contents and read it in. 6637c478bd9Sstevel@tonic-gate * It's this buffer that's passed to all the other ber decoding 6647c478bd9Sstevel@tonic-gate * routines. 6657c478bd9Sstevel@tonic-gate */ 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate #if defined( DOS ) && !( defined( _WIN32 ) || defined(XP_OS2) ) 6687c478bd9Sstevel@tonic-gate if ( *len > 65535 ) { /* DOS can't allocate > 64K */ 6697c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 6707c478bd9Sstevel@tonic-gate } 6717c478bd9Sstevel@tonic-gate #endif /* DOS && !_WIN32 */ 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate if ( ( sb->sb_options & LBER_SOCKBUF_OPT_MAX_INCOMING_SIZE ) 6747c478bd9Sstevel@tonic-gate && *len > sb->sb_max_incoming ) { 6757c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 6767c478bd9Sstevel@tonic-gate } 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate if ( (ber->ber_buf = (char *)NSLBERI_CALLOC( 1,(size_t)*len )) 6797c478bd9Sstevel@tonic-gate == NULL ) { 6807c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate ber->ber_flags &= ~LBER_FLAG_NO_FREE_BUFFER; 6837c478bd9Sstevel@tonic-gate ber->ber_ptr = ber->ber_buf; 6847c478bd9Sstevel@tonic-gate ber->ber_end = ber->ber_buf + *len; 6857c478bd9Sstevel@tonic-gate ber->ber_rwptr = ber->ber_buf; 6867c478bd9Sstevel@tonic-gate } 6877c478bd9Sstevel@tonic-gate 6887c478bd9Sstevel@tonic-gate toread = (uintptr_t)ber->ber_end - (uintptr_t)ber->ber_rwptr; 6897c478bd9Sstevel@tonic-gate do { 6907c478bd9Sstevel@tonic-gate if ( (rc = BerRead( sb, ber->ber_rwptr, (ber_int_t)toread )) <= 0 ) { 6917c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate toread -= rc; 6957c478bd9Sstevel@tonic-gate ber->ber_rwptr += rc; 6967c478bd9Sstevel@tonic-gate } while ( toread > 0 ); 6977c478bd9Sstevel@tonic-gate 6987c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG 6997c478bd9Sstevel@tonic-gate if ( lber_debug ) { 7007c478bd9Sstevel@tonic-gate char msg[80]; 7017c478bd9Sstevel@tonic-gate sprintf( msg, "ber_get_next: tag 0x%lx len %ld contents:\n", 7027c478bd9Sstevel@tonic-gate tag, ber->ber_len ); 7037c478bd9Sstevel@tonic-gate ber_err_print( msg ); 7047c478bd9Sstevel@tonic-gate if ( lber_debug > 1 ) 7057c478bd9Sstevel@tonic-gate ber_dump( ber, 1 ); 7067c478bd9Sstevel@tonic-gate } 7077c478bd9Sstevel@tonic-gate #endif 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate *len = ber->ber_len; 7107c478bd9Sstevel@tonic-gate ber->ber_rwptr = NULL; 7117c478bd9Sstevel@tonic-gate return( ber->ber_tag ); 7127c478bd9Sstevel@tonic-gate } 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate Sockbuf * 7157c478bd9Sstevel@tonic-gate LDAP_CALL 7167c478bd9Sstevel@tonic-gate ber_sockbuf_alloc() 7177c478bd9Sstevel@tonic-gate { 7187c478bd9Sstevel@tonic-gate return( (Sockbuf *)NSLBERI_CALLOC( 1, sizeof(struct sockbuf) ) ); 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate 7217c478bd9Sstevel@tonic-gate void 7227c478bd9Sstevel@tonic-gate LDAP_CALL 7237c478bd9Sstevel@tonic-gate ber_sockbuf_free(Sockbuf *p) 7247c478bd9Sstevel@tonic-gate { 7257c478bd9Sstevel@tonic-gate if ( p != NULL ) { 726*e6a862fbSDouglas Leavitt #ifdef LDAP_SASLIO_HOOKS 727*e6a862fbSDouglas Leavitt if ( p->sb_sasl_ctx != NULL) 728*e6a862fbSDouglas Leavitt sasl_dispose(&p->sb_sasl_ctx); 729*e6a862fbSDouglas Leavitt if ( p->sb_sasl_ibuf != NULL) { 730*e6a862fbSDouglas Leavitt NSLBERI_FREE( p->sb_sasl_ibuf ); 731*e6a862fbSDouglas Leavitt } 732*e6a862fbSDouglas Leavitt #endif 7337c478bd9Sstevel@tonic-gate if ( p->sb_ber.ber_buf != NULL && 7347c478bd9Sstevel@tonic-gate !(p->sb_ber.ber_flags & LBER_FLAG_NO_FREE_BUFFER) ) { 7357c478bd9Sstevel@tonic-gate NSLBERI_FREE( p->sb_ber.ber_buf ); 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate NSLBERI_FREE(p); 7387c478bd9Sstevel@tonic-gate } 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate /* 7427c478bd9Sstevel@tonic-gate * return 0 on success and -1 on error 7437c478bd9Sstevel@tonic-gate */ 7447c478bd9Sstevel@tonic-gate int 7457c478bd9Sstevel@tonic-gate LDAP_CALL 7467c478bd9Sstevel@tonic-gate ber_set_option( struct berelement *ber, int option, void *value ) 7477c478bd9Sstevel@tonic-gate { 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate /* 7507c478bd9Sstevel@tonic-gate * memory allocation callbacks are global, so it is OK to pass 7517c478bd9Sstevel@tonic-gate * NULL for ber. Handle this as a special case. 7527c478bd9Sstevel@tonic-gate */ 7537c478bd9Sstevel@tonic-gate if ( option == LBER_OPT_MEMALLOC_FN_PTRS ) { 7547c478bd9Sstevel@tonic-gate /* struct copy */ 7557c478bd9Sstevel@tonic-gate nslberi_memalloc_fns = *((struct lber_memalloc_fns *)value); 7567c478bd9Sstevel@tonic-gate return( 0 ); 7577c478bd9Sstevel@tonic-gate } 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate /* 7607c478bd9Sstevel@tonic-gate * lber_debug is global, so it is OK to pass 7617c478bd9Sstevel@tonic-gate * NULL for ber. Handle this as a special case. 7627c478bd9Sstevel@tonic-gate */ 7637c478bd9Sstevel@tonic-gate if ( option == LBER_OPT_DEBUG_LEVEL ) { 7647c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG 7657c478bd9Sstevel@tonic-gate lber_debug = *(int *)value; 7667c478bd9Sstevel@tonic-gate #endif 7677c478bd9Sstevel@tonic-gate return( 0 ); 7687c478bd9Sstevel@tonic-gate } 7697c478bd9Sstevel@tonic-gate 7707c478bd9Sstevel@tonic-gate /* 7717c478bd9Sstevel@tonic-gate * all the rest require a non-NULL ber 7727c478bd9Sstevel@tonic-gate */ 7737c478bd9Sstevel@tonic-gate if ( !NSLBERI_VALID_BERELEMENT_POINTER( ber )) { 7747c478bd9Sstevel@tonic-gate return( -1 ); 7757c478bd9Sstevel@tonic-gate } 7767c478bd9Sstevel@tonic-gate 7777c478bd9Sstevel@tonic-gate switch ( option ) { 7787c478bd9Sstevel@tonic-gate case LBER_OPT_USE_DER: 7797c478bd9Sstevel@tonic-gate case LBER_OPT_TRANSLATE_STRINGS: 7807c478bd9Sstevel@tonic-gate if ( value != NULL ) { 7817c478bd9Sstevel@tonic-gate ber->ber_options |= option; 7827c478bd9Sstevel@tonic-gate } else { 7837c478bd9Sstevel@tonic-gate ber->ber_options &= ~option; 7847c478bd9Sstevel@tonic-gate } 7857c478bd9Sstevel@tonic-gate break; 7867c478bd9Sstevel@tonic-gate case LBER_OPT_REMAINING_BYTES: 7877c478bd9Sstevel@tonic-gate ber->ber_end = ber->ber_ptr + *((ber_uint_t *)value); 7887c478bd9Sstevel@tonic-gate break; 7897c478bd9Sstevel@tonic-gate case LBER_OPT_TOTAL_BYTES: 7907c478bd9Sstevel@tonic-gate ber->ber_end = ber->ber_buf + *((ber_uint_t *)value); 7917c478bd9Sstevel@tonic-gate break; 7927c478bd9Sstevel@tonic-gate case LBER_OPT_BYTES_TO_WRITE: 7937c478bd9Sstevel@tonic-gate ber->ber_ptr = ber->ber_buf + *((ber_uint_t *)value); 7947c478bd9Sstevel@tonic-gate break; 7957c478bd9Sstevel@tonic-gate default: 7967c478bd9Sstevel@tonic-gate return( -1 ); 7977c478bd9Sstevel@tonic-gate } 7987c478bd9Sstevel@tonic-gate 7997c478bd9Sstevel@tonic-gate return( 0 ); 8007c478bd9Sstevel@tonic-gate } 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate /* 8037c478bd9Sstevel@tonic-gate * return 0 on success and -1 on error 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate int 8067c478bd9Sstevel@tonic-gate LDAP_CALL 8077c478bd9Sstevel@tonic-gate ber_get_option( struct berelement *ber, int option, void *value ) 8087c478bd9Sstevel@tonic-gate { 8097c478bd9Sstevel@tonic-gate /* 8107c478bd9Sstevel@tonic-gate * memory callocation callbacks are global, so it is OK to pass 8117c478bd9Sstevel@tonic-gate * NULL for ber. Handle this as a special case 8127c478bd9Sstevel@tonic-gate */ 8137c478bd9Sstevel@tonic-gate if ( option == LBER_OPT_MEMALLOC_FN_PTRS ) { 8147c478bd9Sstevel@tonic-gate /* struct copy */ 8157c478bd9Sstevel@tonic-gate *((struct lber_memalloc_fns *)value) = nslberi_memalloc_fns; 8167c478bd9Sstevel@tonic-gate return( 0 ); 8177c478bd9Sstevel@tonic-gate } 8187c478bd9Sstevel@tonic-gate 8197c478bd9Sstevel@tonic-gate /* 8207c478bd9Sstevel@tonic-gate * lber_debug is global, so it is OK to pass 8217c478bd9Sstevel@tonic-gate * NULL for ber. Handle this as a special case. 8227c478bd9Sstevel@tonic-gate */ 8237c478bd9Sstevel@tonic-gate if ( option == LBER_OPT_DEBUG_LEVEL ) { 8247c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG 8257c478bd9Sstevel@tonic-gate *(int *)value = lber_debug; 8267c478bd9Sstevel@tonic-gate #endif 8277c478bd9Sstevel@tonic-gate return( 0 ); 8287c478bd9Sstevel@tonic-gate } 8297c478bd9Sstevel@tonic-gate /* 8307c478bd9Sstevel@tonic-gate * all the rest require a non-NULL ber 8317c478bd9Sstevel@tonic-gate */ 8327c478bd9Sstevel@tonic-gate if ( !NSLBERI_VALID_BERELEMENT_POINTER( ber )) { 8337c478bd9Sstevel@tonic-gate return( -1 ); 8347c478bd9Sstevel@tonic-gate } 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate switch ( option ) { 8377c478bd9Sstevel@tonic-gate case LBER_OPT_USE_DER: 8387c478bd9Sstevel@tonic-gate case LBER_OPT_TRANSLATE_STRINGS: 8397c478bd9Sstevel@tonic-gate *((int *) value) = (ber->ber_options & option); 8407c478bd9Sstevel@tonic-gate break; 8417c478bd9Sstevel@tonic-gate case LBER_OPT_REMAINING_BYTES: 8427c478bd9Sstevel@tonic-gate *((ber_uint_t *) value) = ber->ber_end - ber->ber_ptr; 8437c478bd9Sstevel@tonic-gate break; 8447c478bd9Sstevel@tonic-gate case LBER_OPT_TOTAL_BYTES: 8457c478bd9Sstevel@tonic-gate *((ber_uint_t *) value) = ber->ber_end - ber->ber_buf; 8467c478bd9Sstevel@tonic-gate break; 8477c478bd9Sstevel@tonic-gate case LBER_OPT_BYTES_TO_WRITE: 8487c478bd9Sstevel@tonic-gate *((ber_uint_t *) value) = ber->ber_ptr - ber->ber_buf; 8497c478bd9Sstevel@tonic-gate break; 8507c478bd9Sstevel@tonic-gate default: 8517c478bd9Sstevel@tonic-gate return( -1 ); 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate return( 0 ); 8557c478bd9Sstevel@tonic-gate } 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate /* 8587c478bd9Sstevel@tonic-gate * return 0 on success and -1 on error 8597c478bd9Sstevel@tonic-gate */ 8607c478bd9Sstevel@tonic-gate int 8617c478bd9Sstevel@tonic-gate LDAP_CALL 8627c478bd9Sstevel@tonic-gate ber_sockbuf_set_option( Sockbuf *sb, int option, void *value ) 8637c478bd9Sstevel@tonic-gate { 8647c478bd9Sstevel@tonic-gate struct lber_x_ext_io_fns *extiofns; 8657c478bd9Sstevel@tonic-gate 8667c478bd9Sstevel@tonic-gate if ( !NSLBERI_VALID_SOCKBUF_POINTER( sb )) { 8677c478bd9Sstevel@tonic-gate return( -1 ); 8687c478bd9Sstevel@tonic-gate } 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate switch ( option ) { 8717c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_MAX_INCOMING_SIZE: 8727c478bd9Sstevel@tonic-gate sb->sb_max_incoming = *((ber_uint_t *) value); 8737c478bd9Sstevel@tonic-gate /* FALL */ 8747c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_TO_FILE: 8757c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_TO_FILE_ONLY: 8767c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_NO_READ_AHEAD: 8777c478bd9Sstevel@tonic-gate if ( value != NULL ) { 8787c478bd9Sstevel@tonic-gate sb->sb_options |= option; 8797c478bd9Sstevel@tonic-gate } else { 8807c478bd9Sstevel@tonic-gate sb->sb_options &= ~option; 8817c478bd9Sstevel@tonic-gate } 8827c478bd9Sstevel@tonic-gate break; 8837c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_DESC: 8847c478bd9Sstevel@tonic-gate sb->sb_sd = *((LBER_SOCKET *) value); 8857c478bd9Sstevel@tonic-gate break; 8867c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_COPYDESC: 8877c478bd9Sstevel@tonic-gate sb->sb_copyfd = *((LBER_SOCKET *) value); 8887c478bd9Sstevel@tonic-gate break; 8897c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_READ_FN: 8907c478bd9Sstevel@tonic-gate sb->sb_io_fns.lbiof_read = (LDAP_IOF_READ_CALLBACK *) value; 8917c478bd9Sstevel@tonic-gate nslberi_install_compat_io_fns( sb ); 8927c478bd9Sstevel@tonic-gate break; 8937c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_WRITE_FN: 8947c478bd9Sstevel@tonic-gate sb->sb_io_fns.lbiof_write = (LDAP_IOF_WRITE_CALLBACK *) value; 8957c478bd9Sstevel@tonic-gate nslberi_install_compat_io_fns( sb ); 8967c478bd9Sstevel@tonic-gate break; 8977c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_EXT_IO_FNS: 8987c478bd9Sstevel@tonic-gate extiofns = (struct lber_x_ext_io_fns *) value; 8997c478bd9Sstevel@tonic-gate if ( extiofns == NULL ) { /* remove */ 9007c478bd9Sstevel@tonic-gate (void)memset( (char *)&sb->sb_ext_io_fns, '\0', 9017c478bd9Sstevel@tonic-gate sizeof(sb->sb_ext_io_fns )); 9027c478bd9Sstevel@tonic-gate } else if ( extiofns->lbextiofn_size 9037c478bd9Sstevel@tonic-gate == LBER_X_EXTIO_FNS_SIZE ) { 9047c478bd9Sstevel@tonic-gate /* struct copy */ 9057c478bd9Sstevel@tonic-gate sb->sb_ext_io_fns = *extiofns; 9067c478bd9Sstevel@tonic-gate } else { 9077c478bd9Sstevel@tonic-gate return( -1 ); 9087c478bd9Sstevel@tonic-gate } 9097c478bd9Sstevel@tonic-gate break; 9107c478bd9Sstevel@tonic-gate default: 9117c478bd9Sstevel@tonic-gate return( -1 ); 9127c478bd9Sstevel@tonic-gate } 9137c478bd9Sstevel@tonic-gate 9147c478bd9Sstevel@tonic-gate return( 0 ); 9157c478bd9Sstevel@tonic-gate } 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate /* 9187c478bd9Sstevel@tonic-gate * return 0 on success and -1 on error 9197c478bd9Sstevel@tonic-gate */ 9207c478bd9Sstevel@tonic-gate int 9217c478bd9Sstevel@tonic-gate LDAP_CALL 9227c478bd9Sstevel@tonic-gate ber_sockbuf_get_option( Sockbuf *sb, int option, void *value ) 9237c478bd9Sstevel@tonic-gate { 9247c478bd9Sstevel@tonic-gate struct lber_x_ext_io_fns *extiofns; 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate if ( !NSLBERI_VALID_SOCKBUF_POINTER( sb )) { 9277c478bd9Sstevel@tonic-gate return( -1 ); 9287c478bd9Sstevel@tonic-gate } 9297c478bd9Sstevel@tonic-gate 9307c478bd9Sstevel@tonic-gate switch ( option ) { 9317c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_MAX_INCOMING_SIZE: 9327c478bd9Sstevel@tonic-gate *((ber_uint_t *) value) = sb->sb_max_incoming; 9337c478bd9Sstevel@tonic-gate break; 9347c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_TO_FILE: 9357c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_TO_FILE_ONLY: 9367c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_NO_READ_AHEAD: 9377c478bd9Sstevel@tonic-gate *((int *) value) = (sb->sb_options & option); 9387c478bd9Sstevel@tonic-gate break; 9397c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_DESC: 9407c478bd9Sstevel@tonic-gate *((LBER_SOCKET *) value) = sb->sb_sd; 9417c478bd9Sstevel@tonic-gate break; 9427c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_COPYDESC: 9437c478bd9Sstevel@tonic-gate *((LBER_SOCKET *) value) = sb->sb_copyfd; 9447c478bd9Sstevel@tonic-gate break; 9457c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_READ_FN: 9467c478bd9Sstevel@tonic-gate *((LDAP_IOF_READ_CALLBACK **) value) 9477c478bd9Sstevel@tonic-gate = sb->sb_io_fns.lbiof_read; 9487c478bd9Sstevel@tonic-gate break; 9497c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_WRITE_FN: 9507c478bd9Sstevel@tonic-gate *((LDAP_IOF_WRITE_CALLBACK **) value) 9517c478bd9Sstevel@tonic-gate = sb->sb_io_fns.lbiof_write; 9527c478bd9Sstevel@tonic-gate break; 9537c478bd9Sstevel@tonic-gate case LBER_SOCKBUF_OPT_EXT_IO_FNS: 9547c478bd9Sstevel@tonic-gate extiofns = (struct lber_x_ext_io_fns *) value; 9557c478bd9Sstevel@tonic-gate if ( extiofns == NULL || extiofns->lbextiofn_size 9567c478bd9Sstevel@tonic-gate != LBER_X_EXTIO_FNS_SIZE ) { 9577c478bd9Sstevel@tonic-gate return( -1 ); 9587c478bd9Sstevel@tonic-gate } 9597c478bd9Sstevel@tonic-gate /* struct copy */ 9607c478bd9Sstevel@tonic-gate *extiofns = sb->sb_ext_io_fns; 9617c478bd9Sstevel@tonic-gate break; 9627c478bd9Sstevel@tonic-gate default: 9637c478bd9Sstevel@tonic-gate return( -1 ); 9647c478bd9Sstevel@tonic-gate } 9657c478bd9Sstevel@tonic-gate 9667c478bd9Sstevel@tonic-gate return( 0 ); 9677c478bd9Sstevel@tonic-gate } 9687c478bd9Sstevel@tonic-gate 9697c478bd9Sstevel@tonic-gate 9707c478bd9Sstevel@tonic-gate /* new dboreham code below: */ 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate struct byte_buffer { 9737c478bd9Sstevel@tonic-gate unsigned char *p; 9747c478bd9Sstevel@tonic-gate int offset; 9757c478bd9Sstevel@tonic-gate int length; 9767c478bd9Sstevel@tonic-gate }; 9777c478bd9Sstevel@tonic-gate typedef struct byte_buffer byte_buffer; 9787c478bd9Sstevel@tonic-gate 9797c478bd9Sstevel@tonic-gate 9807c478bd9Sstevel@tonic-gate /* This call allocates us a BerElement structure plus some extra memory. 9817c478bd9Sstevel@tonic-gate * It returns a pointer to the BerElement, plus a pointer to the extra memory. 9827c478bd9Sstevel@tonic-gate * This routine also allocates a ber data buffer within the same block, thus 9837c478bd9Sstevel@tonic-gate * saving a call to calloc later when we read data. 9847c478bd9Sstevel@tonic-gate */ 9857c478bd9Sstevel@tonic-gate void* 9867c478bd9Sstevel@tonic-gate LDAP_CALL 9877c478bd9Sstevel@tonic-gate ber_special_alloc(size_t size, BerElement **ppBer) 9887c478bd9Sstevel@tonic-gate { 9897c478bd9Sstevel@tonic-gate char *mem = NULL; 9907c478bd9Sstevel@tonic-gate 9917c478bd9Sstevel@tonic-gate /* Make sure mem size requested is aligned */ 9927c478bd9Sstevel@tonic-gate if (0 != ( size & 0x03 )) { 9937c478bd9Sstevel@tonic-gate size += (sizeof(ber_int_t) - (size & 0x03)); 9947c478bd9Sstevel@tonic-gate } 9957c478bd9Sstevel@tonic-gate 9967c478bd9Sstevel@tonic-gate mem = NSLBERI_MALLOC(sizeof(struct berelement) + EXBUFSIZ + size ); 9977c478bd9Sstevel@tonic-gate if (NULL == mem) { 9987c478bd9Sstevel@tonic-gate return NULL; 9997c478bd9Sstevel@tonic-gate } 10007c478bd9Sstevel@tonic-gate *ppBer = (BerElement*) (mem + size); 10017c478bd9Sstevel@tonic-gate memset(*ppBer,0,sizeof(struct berelement)); 10027c478bd9Sstevel@tonic-gate (*ppBer)->ber_tag = LBER_DEFAULT; 10037c478bd9Sstevel@tonic-gate (*ppBer)->ber_buf = mem + size + sizeof(struct berelement); 10047c478bd9Sstevel@tonic-gate (*ppBer)->ber_ptr = (*ppBer)->ber_buf; 10057c478bd9Sstevel@tonic-gate (*ppBer)->ber_end = (*ppBer)->ber_buf + EXBUFSIZ; 10067c478bd9Sstevel@tonic-gate (*ppBer)->ber_flags = LBER_FLAG_NO_FREE_BUFFER; 10077c478bd9Sstevel@tonic-gate return (void*)mem; 10087c478bd9Sstevel@tonic-gate } 10097c478bd9Sstevel@tonic-gate 10107c478bd9Sstevel@tonic-gate void 10117c478bd9Sstevel@tonic-gate LDAP_CALL 10127c478bd9Sstevel@tonic-gate ber_special_free(void* buf, BerElement *ber) 10137c478bd9Sstevel@tonic-gate { 10147c478bd9Sstevel@tonic-gate if (!(ber->ber_flags & LBER_FLAG_NO_FREE_BUFFER)) { 10157c478bd9Sstevel@tonic-gate NSLBERI_FREE(ber->ber_buf); 10167c478bd9Sstevel@tonic-gate } 10177c478bd9Sstevel@tonic-gate NSLBERI_FREE( buf ); 10187c478bd9Sstevel@tonic-gate } 10197c478bd9Sstevel@tonic-gate 10207c478bd9Sstevel@tonic-gate static int 10217c478bd9Sstevel@tonic-gate read_bytes(byte_buffer *b, unsigned char *return_buffer, int bytes_to_read) 10227c478bd9Sstevel@tonic-gate { 10237c478bd9Sstevel@tonic-gate /* copy up to bytes_to_read bytes into the caller's buffer, return the number of bytes copied */ 10247c478bd9Sstevel@tonic-gate int bytes_to_copy = 0; 10257c478bd9Sstevel@tonic-gate 10267c478bd9Sstevel@tonic-gate if (bytes_to_read <= (b->length - b->offset) ) { 10277c478bd9Sstevel@tonic-gate bytes_to_copy = bytes_to_read; 10287c478bd9Sstevel@tonic-gate } else { 10297c478bd9Sstevel@tonic-gate bytes_to_copy = (b->length - b->offset); 10307c478bd9Sstevel@tonic-gate } 10317c478bd9Sstevel@tonic-gate if (1 == bytes_to_copy) { 10327c478bd9Sstevel@tonic-gate *return_buffer = *(b->p+b->offset++); 10337c478bd9Sstevel@tonic-gate } else 10347c478bd9Sstevel@tonic-gate if (0 == bytes_to_copy) { 10357c478bd9Sstevel@tonic-gate ; 10367c478bd9Sstevel@tonic-gate } else 10377c478bd9Sstevel@tonic-gate { 10387c478bd9Sstevel@tonic-gate memcpy(return_buffer,b->p+b->offset,bytes_to_copy); 10397c478bd9Sstevel@tonic-gate b->offset += bytes_to_copy; 10407c478bd9Sstevel@tonic-gate } 10417c478bd9Sstevel@tonic-gate return bytes_to_copy; 10427c478bd9Sstevel@tonic-gate } 10437c478bd9Sstevel@tonic-gate 10447c478bd9Sstevel@tonic-gate /* return the tag - LBER_DEFAULT returned means trouble */ 10457c478bd9Sstevel@tonic-gate static ber_tag_t 10467c478bd9Sstevel@tonic-gate get_buffer_tag(byte_buffer *sb ) 10477c478bd9Sstevel@tonic-gate { 10487c478bd9Sstevel@tonic-gate unsigned char xbyte; 10497c478bd9Sstevel@tonic-gate ber_tag_t tag; 10507c478bd9Sstevel@tonic-gate char *tagp; 10517c478bd9Sstevel@tonic-gate int i; 10527c478bd9Sstevel@tonic-gate 10537c478bd9Sstevel@tonic-gate if ( (i = read_bytes( sb, &xbyte, 1 )) != 1 ) { 10547c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 10557c478bd9Sstevel@tonic-gate } 10567c478bd9Sstevel@tonic-gate 10577c478bd9Sstevel@tonic-gate if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) { 10587c478bd9Sstevel@tonic-gate return( (ber_uint_t) xbyte ); 10597c478bd9Sstevel@tonic-gate } 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate tagp = (char *) &tag; 10627c478bd9Sstevel@tonic-gate tagp[0] = xbyte; 10637c478bd9Sstevel@tonic-gate for ( i = 1; i < sizeof(ber_int_t); i++ ) { 10647c478bd9Sstevel@tonic-gate if ( read_bytes( sb, &xbyte, 1 ) != 1 ) 10657c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 10667c478bd9Sstevel@tonic-gate 10677c478bd9Sstevel@tonic-gate tagp[i] = xbyte; 10687c478bd9Sstevel@tonic-gate 10697c478bd9Sstevel@tonic-gate if ( ! (xbyte & LBER_MORE_TAG_MASK) ) 10707c478bd9Sstevel@tonic-gate break; 10717c478bd9Sstevel@tonic-gate } 10727c478bd9Sstevel@tonic-gate 10737c478bd9Sstevel@tonic-gate /* tag too big! */ 10747c478bd9Sstevel@tonic-gate if ( i == sizeof(ber_int_t) ) 10757c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 10767c478bd9Sstevel@tonic-gate 10777c478bd9Sstevel@tonic-gate /* want leading, not trailing 0's */ 10787c478bd9Sstevel@tonic-gate return( tag >> (sizeof(ber_int_t) - i - 1) ); 10797c478bd9Sstevel@tonic-gate } 10807c478bd9Sstevel@tonic-gate 10817c478bd9Sstevel@tonic-gate /* Like ber_get_next, but from a byte buffer the caller already has. */ 10827c478bd9Sstevel@tonic-gate /* Bytes_Scanned returns the number of bytes we actually looked at in the buffer. */ 10837c478bd9Sstevel@tonic-gate /* ber_get_next_buffer is now implemented in terms of ber_get_next_buffer_ext */ 10847c478bd9Sstevel@tonic-gate /* and is here for backward compatibility. This new function allows us to pass */ 10857c478bd9Sstevel@tonic-gate /* the Sockbuf structure along */ 10867c478bd9Sstevel@tonic-gate 10877c478bd9Sstevel@tonic-gate ber_uint_t 10887c478bd9Sstevel@tonic-gate LDAP_CALL 10897c478bd9Sstevel@tonic-gate ber_get_next_buffer( void *buffer, size_t buffer_size, ber_len_t *len, 10907c478bd9Sstevel@tonic-gate BerElement *ber, ber_uint_t *Bytes_Scanned ) 10917c478bd9Sstevel@tonic-gate { 10927c478bd9Sstevel@tonic-gate return (ber_get_next_buffer_ext( buffer, buffer_size, len, ber, 10937c478bd9Sstevel@tonic-gate Bytes_Scanned, NULL)); 10947c478bd9Sstevel@tonic-gate } 10957c478bd9Sstevel@tonic-gate 10967c478bd9Sstevel@tonic-gate ber_uint_t 10977c478bd9Sstevel@tonic-gate LDAP_CALL 10987c478bd9Sstevel@tonic-gate ber_get_next_buffer_ext( void *buffer, size_t buffer_size, ber_len_t *len, 10997c478bd9Sstevel@tonic-gate BerElement *ber, ber_uint_t *Bytes_Scanned, Sockbuf *sock ) 11007c478bd9Sstevel@tonic-gate { 11017c478bd9Sstevel@tonic-gate ber_tag_t tag = 0; 11027c478bd9Sstevel@tonic-gate ber_len_t netlen; 11037c478bd9Sstevel@tonic-gate ber_uint_t toread; 11047c478bd9Sstevel@tonic-gate unsigned char lc; 11057c478bd9Sstevel@tonic-gate ssize_t rc; 11067c478bd9Sstevel@tonic-gate int noctets, diff; 11077c478bd9Sstevel@tonic-gate byte_buffer sb = {0}; 11087c478bd9Sstevel@tonic-gate 11097c478bd9Sstevel@tonic-gate 11107c478bd9Sstevel@tonic-gate /* 11117c478bd9Sstevel@tonic-gate * Any ber element looks like this: tag length contents. 11127c478bd9Sstevel@tonic-gate * Assuming everything's ok, we return the tag byte (we 11137c478bd9Sstevel@tonic-gate * can assume a single byte), return the length in len, 11147c478bd9Sstevel@tonic-gate * and the rest of the undecoded element in buf. 11157c478bd9Sstevel@tonic-gate * 11167c478bd9Sstevel@tonic-gate * Assumptions: 11177c478bd9Sstevel@tonic-gate * 1) small tags (less than 128) 11187c478bd9Sstevel@tonic-gate * 2) definite lengths 11197c478bd9Sstevel@tonic-gate * 3) primitive encodings used whenever possible 11207c478bd9Sstevel@tonic-gate */ 11217c478bd9Sstevel@tonic-gate 11227c478bd9Sstevel@tonic-gate /* 11237c478bd9Sstevel@tonic-gate * first time through - malloc the buffer, set up ptrs, and 11247c478bd9Sstevel@tonic-gate * read the tag and the length and as much of the rest as we can 11257c478bd9Sstevel@tonic-gate */ 11267c478bd9Sstevel@tonic-gate 11277c478bd9Sstevel@tonic-gate sb.p = buffer; 11287c478bd9Sstevel@tonic-gate sb.length = buffer_size; 11297c478bd9Sstevel@tonic-gate 11307c478bd9Sstevel@tonic-gate if ( ber->ber_rwptr == NULL ) { 11317c478bd9Sstevel@tonic-gate /* 11327c478bd9Sstevel@tonic-gate * First, we read the tag. 11337c478bd9Sstevel@tonic-gate */ 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate if ( (tag = get_buffer_tag( &sb )) == LBER_DEFAULT ) { 11367c478bd9Sstevel@tonic-gate goto premature_exit; 11377c478bd9Sstevel@tonic-gate } 11387c478bd9Sstevel@tonic-gate ber->ber_tag = tag; 11397c478bd9Sstevel@tonic-gate 11407c478bd9Sstevel@tonic-gate /* 11417c478bd9Sstevel@tonic-gate * Next, read the length. The first byte contains the length 11427c478bd9Sstevel@tonic-gate * of the length. If bit 8 is set, the length is the long 11437c478bd9Sstevel@tonic-gate * form, otherwise it's the short form. We don't allow a 11447c478bd9Sstevel@tonic-gate * length that's greater than what we can hold in an unsigned 11457c478bd9Sstevel@tonic-gate * long. 11467c478bd9Sstevel@tonic-gate */ 11477c478bd9Sstevel@tonic-gate 11487c478bd9Sstevel@tonic-gate *len = netlen = 0; 11497c478bd9Sstevel@tonic-gate if ( read_bytes( &sb, &lc, 1 ) != 1 ) { 11507c478bd9Sstevel@tonic-gate goto premature_exit; 11517c478bd9Sstevel@tonic-gate } 11527c478bd9Sstevel@tonic-gate if ( lc & 0x80 ) { 11537c478bd9Sstevel@tonic-gate noctets = (lc & 0x7f); 11547c478bd9Sstevel@tonic-gate if ( noctets > sizeof(ber_uint_t) ) 11557c478bd9Sstevel@tonic-gate goto premature_exit; 11567c478bd9Sstevel@tonic-gate diff = sizeof(ber_uint_t) - noctets; 11577c478bd9Sstevel@tonic-gate if ( read_bytes( &sb, (unsigned char *)&netlen + diff, 11587c478bd9Sstevel@tonic-gate noctets ) != noctets ) { 11597c478bd9Sstevel@tonic-gate goto premature_exit; 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate *len = LBER_NTOHL( netlen ); 11627c478bd9Sstevel@tonic-gate } else { 11637c478bd9Sstevel@tonic-gate *len = lc; 11647c478bd9Sstevel@tonic-gate } 11657c478bd9Sstevel@tonic-gate ber->ber_len = *len; 11667c478bd9Sstevel@tonic-gate 11677c478bd9Sstevel@tonic-gate /* 11687c478bd9Sstevel@tonic-gate * Finally, malloc a buffer for the contents and read it in. 11697c478bd9Sstevel@tonic-gate * It's this buffer that's passed to all the other ber decoding 11707c478bd9Sstevel@tonic-gate * routines. 11717c478bd9Sstevel@tonic-gate */ 11727c478bd9Sstevel@tonic-gate 11737c478bd9Sstevel@tonic-gate #if defined( DOS ) && !defined( _WIN32 ) 11747c478bd9Sstevel@tonic-gate if ( *len > 65535 ) { /* DOS can't allocate > 64K */ 11757c478bd9Sstevel@tonic-gate goto premature_exit; 11767c478bd9Sstevel@tonic-gate } 11777c478bd9Sstevel@tonic-gate #endif /* DOS && !_WIN32 */ 11787c478bd9Sstevel@tonic-gate 11797c478bd9Sstevel@tonic-gate if ( (sock != NULL) && 11807c478bd9Sstevel@tonic-gate ( sock->sb_options & LBER_SOCKBUF_OPT_MAX_INCOMING_SIZE ) 11817c478bd9Sstevel@tonic-gate && (*len > sock->sb_max_incoming) ) { 11827c478bd9Sstevel@tonic-gate return( LBER_DEFAULT ); 11837c478bd9Sstevel@tonic-gate } 11847c478bd9Sstevel@tonic-gate 11857c478bd9Sstevel@tonic-gate if ( ber->ber_buf + *len > ber->ber_end ) { 11867c478bd9Sstevel@tonic-gate if ( nslberi_ber_realloc( ber, *len ) != 0 ) 11877c478bd9Sstevel@tonic-gate goto premature_exit; 11887c478bd9Sstevel@tonic-gate } 11897c478bd9Sstevel@tonic-gate ber->ber_ptr = ber->ber_buf; 11907c478bd9Sstevel@tonic-gate ber->ber_end = ber->ber_buf + *len; 11917c478bd9Sstevel@tonic-gate ber->ber_rwptr = ber->ber_buf; 11927c478bd9Sstevel@tonic-gate } 11937c478bd9Sstevel@tonic-gate 11947c478bd9Sstevel@tonic-gate toread = (uintptr_t)ber->ber_end - (uintptr_t)ber->ber_rwptr; 11957c478bd9Sstevel@tonic-gate do { 11967c478bd9Sstevel@tonic-gate if ( (rc = read_bytes( &sb, (unsigned char *)ber->ber_rwptr, 11977c478bd9Sstevel@tonic-gate (ber_int_t)toread )) <= 0 ) { 11987c478bd9Sstevel@tonic-gate goto premature_exit; 11997c478bd9Sstevel@tonic-gate } 12007c478bd9Sstevel@tonic-gate 12017c478bd9Sstevel@tonic-gate toread -= rc; 12027c478bd9Sstevel@tonic-gate ber->ber_rwptr += rc; 12037c478bd9Sstevel@tonic-gate } while ( toread > 0 ); 12047c478bd9Sstevel@tonic-gate 12057c478bd9Sstevel@tonic-gate *len = ber->ber_len; 12067c478bd9Sstevel@tonic-gate *Bytes_Scanned = sb.offset; 12077c478bd9Sstevel@tonic-gate return( ber->ber_tag ); 12087c478bd9Sstevel@tonic-gate 12097c478bd9Sstevel@tonic-gate premature_exit: 12107c478bd9Sstevel@tonic-gate /* 12117c478bd9Sstevel@tonic-gate * we're here because we hit the end of the buffer before seeing 12127c478bd9Sstevel@tonic-gate * all of the PDU 12137c478bd9Sstevel@tonic-gate */ 12147c478bd9Sstevel@tonic-gate *Bytes_Scanned = sb.offset; 12157c478bd9Sstevel@tonic-gate return(LBER_DEFAULT); 12167c478bd9Sstevel@tonic-gate } 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate 12197c478bd9Sstevel@tonic-gate /* The ber_flatten routine allocates a struct berval whose contents 12207c478bd9Sstevel@tonic-gate * are a BER encoding taken from the ber argument. The bvPtr pointer 12217c478bd9Sstevel@tonic-gate * points to the returned berval, which must be freed using 12227c478bd9Sstevel@tonic-gate * ber_bvfree(). This routine returns 0 on success and -1 on error. 12237c478bd9Sstevel@tonic-gate * The use of ber_flatten on a BerElement in which all '{' and '}' 12247c478bd9Sstevel@tonic-gate * format modifiers have not been properly matched can result in a 12257c478bd9Sstevel@tonic-gate * berval whose contents are not a valid BER encoding. 12267c478bd9Sstevel@tonic-gate * Note that the ber_ptr is not modified. 12277c478bd9Sstevel@tonic-gate */ 12287c478bd9Sstevel@tonic-gate int 12297c478bd9Sstevel@tonic-gate LDAP_CALL 12307c478bd9Sstevel@tonic-gate ber_flatten( BerElement *ber, struct berval **bvPtr ) 12317c478bd9Sstevel@tonic-gate { 12327c478bd9Sstevel@tonic-gate struct berval *new; 12337c478bd9Sstevel@tonic-gate ber_len_t len; 12347c478bd9Sstevel@tonic-gate 12357c478bd9Sstevel@tonic-gate /* allocate a struct berval */ 12367c478bd9Sstevel@tonic-gate if ( (new = (struct berval *)NSLBERI_MALLOC( sizeof(struct berval) )) 12377c478bd9Sstevel@tonic-gate == NULL ) { 12387c478bd9Sstevel@tonic-gate return( -1 ); 12397c478bd9Sstevel@tonic-gate } 12407c478bd9Sstevel@tonic-gate 12417c478bd9Sstevel@tonic-gate /* 12427c478bd9Sstevel@tonic-gate * Copy everything from the BerElement's ber_buf to ber_ptr 12437c478bd9Sstevel@tonic-gate * into the berval structure. 12447c478bd9Sstevel@tonic-gate */ 12457c478bd9Sstevel@tonic-gate if ( ber == NULL ) { 12467c478bd9Sstevel@tonic-gate new->bv_val = NULL; 12477c478bd9Sstevel@tonic-gate new->bv_len = 0; 12487c478bd9Sstevel@tonic-gate } else { 12497c478bd9Sstevel@tonic-gate len = ber->ber_ptr - ber->ber_buf; 12507c478bd9Sstevel@tonic-gate if ( ( new->bv_val = (char *)NSLBERI_MALLOC( len + 1 )) == NULL ) { 12517c478bd9Sstevel@tonic-gate ber_bvfree( new ); 12527c478bd9Sstevel@tonic-gate return( -1 ); 12537c478bd9Sstevel@tonic-gate } 12547c478bd9Sstevel@tonic-gate SAFEMEMCPY( new->bv_val, ber->ber_buf, (size_t)len ); 12557c478bd9Sstevel@tonic-gate new->bv_val[len] = '\0'; 12567c478bd9Sstevel@tonic-gate new->bv_len = len; 12577c478bd9Sstevel@tonic-gate } 12587c478bd9Sstevel@tonic-gate 12597c478bd9Sstevel@tonic-gate /* set bvPtr pointer to point to the returned berval */ 12607c478bd9Sstevel@tonic-gate *bvPtr = new; 12617c478bd9Sstevel@tonic-gate 12627c478bd9Sstevel@tonic-gate return( 0 ); 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate 12657c478bd9Sstevel@tonic-gate 12667c478bd9Sstevel@tonic-gate /* 12677c478bd9Sstevel@tonic-gate * The ber_init function constructs and returns a new BerElement 12687c478bd9Sstevel@tonic-gate * containing a copy of the data in the bv argument. ber_init 12697c478bd9Sstevel@tonic-gate * returns the null pointer on error. 12707c478bd9Sstevel@tonic-gate */ 12717c478bd9Sstevel@tonic-gate BerElement * 12727c478bd9Sstevel@tonic-gate LDAP_CALL 12737c478bd9Sstevel@tonic-gate ber_init( const struct berval *bv ) 12747c478bd9Sstevel@tonic-gate { 12757c478bd9Sstevel@tonic-gate BerElement *ber; 12767c478bd9Sstevel@tonic-gate 12777c478bd9Sstevel@tonic-gate /* construct BerElement */ 12787c478bd9Sstevel@tonic-gate if (( ber = ber_alloc_t( 0 )) != NULLBER ) { 12797c478bd9Sstevel@tonic-gate /* copy data from the bv argument into BerElement */ 12807c478bd9Sstevel@tonic-gate /* XXXmcs: had to cast unsigned long bv_len to long */ 12817c478bd9Sstevel@tonic-gate if ( (ber_write ( ber, bv->bv_val, bv->bv_len, 0 )) 12827c478bd9Sstevel@tonic-gate != (ber_slen_t)bv->bv_len ) { 12837c478bd9Sstevel@tonic-gate ber_free( ber, 1 ); 12847c478bd9Sstevel@tonic-gate return( NULL ); 12857c478bd9Sstevel@tonic-gate } 12867c478bd9Sstevel@tonic-gate } 12877c478bd9Sstevel@tonic-gate 12887c478bd9Sstevel@tonic-gate /* 12897c478bd9Sstevel@tonic-gate * reset ber_ptr back to the beginning of buffer so that this new 12907c478bd9Sstevel@tonic-gate * and initialized ber element can be READ 12917c478bd9Sstevel@tonic-gate */ 12927c478bd9Sstevel@tonic-gate ber_reset( ber, 1); 12937c478bd9Sstevel@tonic-gate 12947c478bd9Sstevel@tonic-gate /* 12957c478bd9Sstevel@tonic-gate * return a ptr to a new BerElement containing a copy of the data 12967c478bd9Sstevel@tonic-gate * in the bv argument or a null pointer on error 12977c478bd9Sstevel@tonic-gate */ 12987c478bd9Sstevel@tonic-gate return( ber ); 12997c478bd9Sstevel@tonic-gate } 13007c478bd9Sstevel@tonic-gate 13017c478bd9Sstevel@tonic-gate 13027c478bd9Sstevel@tonic-gate /* 13037c478bd9Sstevel@tonic-gate * memory allocation functions. 13047c478bd9Sstevel@tonic-gate */ 13057c478bd9Sstevel@tonic-gate void * 13067c478bd9Sstevel@tonic-gate nslberi_malloc( size_t size ) 13077c478bd9Sstevel@tonic-gate { 13087c478bd9Sstevel@tonic-gate return( nslberi_memalloc_fns.lbermem_malloc == NULL ? 13097c478bd9Sstevel@tonic-gate malloc( size ) : 13107c478bd9Sstevel@tonic-gate nslberi_memalloc_fns.lbermem_malloc( size )); 13117c478bd9Sstevel@tonic-gate } 13127c478bd9Sstevel@tonic-gate 13137c478bd9Sstevel@tonic-gate 13147c478bd9Sstevel@tonic-gate void * 13157c478bd9Sstevel@tonic-gate nslberi_calloc( size_t nelem, size_t elsize ) 13167c478bd9Sstevel@tonic-gate { 13177c478bd9Sstevel@tonic-gate return( nslberi_memalloc_fns.lbermem_calloc == NULL ? 13187c478bd9Sstevel@tonic-gate calloc( nelem, elsize ) : 13197c478bd9Sstevel@tonic-gate nslberi_memalloc_fns.lbermem_calloc( nelem, elsize )); 13207c478bd9Sstevel@tonic-gate } 13217c478bd9Sstevel@tonic-gate 13227c478bd9Sstevel@tonic-gate 13237c478bd9Sstevel@tonic-gate void * 13247c478bd9Sstevel@tonic-gate nslberi_realloc( void *ptr, size_t size ) 13257c478bd9Sstevel@tonic-gate { 13267c478bd9Sstevel@tonic-gate return( nslberi_memalloc_fns.lbermem_realloc == NULL ? 13277c478bd9Sstevel@tonic-gate realloc( ptr, size ) : 13287c478bd9Sstevel@tonic-gate nslberi_memalloc_fns.lbermem_realloc( ptr, size )); 13297c478bd9Sstevel@tonic-gate } 13307c478bd9Sstevel@tonic-gate 13317c478bd9Sstevel@tonic-gate 13327c478bd9Sstevel@tonic-gate void 13337c478bd9Sstevel@tonic-gate nslberi_free( void *ptr ) 13347c478bd9Sstevel@tonic-gate { 13357c478bd9Sstevel@tonic-gate if ( nslberi_memalloc_fns.lbermem_free == NULL ) { 13367c478bd9Sstevel@tonic-gate free( ptr ); 13377c478bd9Sstevel@tonic-gate } else { 13387c478bd9Sstevel@tonic-gate nslberi_memalloc_fns.lbermem_free( ptr ); 13397c478bd9Sstevel@tonic-gate } 13407c478bd9Sstevel@tonic-gate } 13417c478bd9Sstevel@tonic-gate 13427c478bd9Sstevel@tonic-gate 13437c478bd9Sstevel@tonic-gate /* 13447c478bd9Sstevel@tonic-gate ****************************************************************************** 13457c478bd9Sstevel@tonic-gate * functions to bridge the gap between new extended I/O functions that are 13467c478bd9Sstevel@tonic-gate * installed using ber_sockbuf_set_option( ..., LBER_SOCKBUF_OPT_EXT_IO_FNS, 13477c478bd9Sstevel@tonic-gate * ... ). 13487c478bd9Sstevel@tonic-gate * 13497c478bd9Sstevel@tonic-gate * the basic strategy is to use the new extended arg to hold a pointer to the 13507c478bd9Sstevel@tonic-gate * Sockbuf itself so we can find the old functions and call them. 13517c478bd9Sstevel@tonic-gate * note that the integer socket s passed in is not used. we use the sb_sd 13527c478bd9Sstevel@tonic-gate * from the Sockbuf itself because it is the correct type. 13537c478bd9Sstevel@tonic-gate */ 13547c478bd9Sstevel@tonic-gate static int 13557c478bd9Sstevel@tonic-gate nslberi_extread_compat( int s, void *buf, int len, 13567c478bd9Sstevel@tonic-gate struct lextiof_socket_private *arg ) 13577c478bd9Sstevel@tonic-gate { 13587c478bd9Sstevel@tonic-gate Sockbuf *sb = (Sockbuf *)arg; 13597c478bd9Sstevel@tonic-gate 13607c478bd9Sstevel@tonic-gate return( sb->sb_io_fns.lbiof_read( sb->sb_sd, buf, len )); 13617c478bd9Sstevel@tonic-gate } 13627c478bd9Sstevel@tonic-gate 13637c478bd9Sstevel@tonic-gate 13647c478bd9Sstevel@tonic-gate static int 13657c478bd9Sstevel@tonic-gate nslberi_extwrite_compat( int s, const void *buf, int len, 13667c478bd9Sstevel@tonic-gate struct lextiof_socket_private *arg ) 13677c478bd9Sstevel@tonic-gate { 13687c478bd9Sstevel@tonic-gate Sockbuf *sb = (Sockbuf *)arg; 13697c478bd9Sstevel@tonic-gate 13707c478bd9Sstevel@tonic-gate return( sb->sb_io_fns.lbiof_write( sb->sb_sd, buf, len )); 13717c478bd9Sstevel@tonic-gate } 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate 13747c478bd9Sstevel@tonic-gate /* 13757c478bd9Sstevel@tonic-gate * Install I/O compatiblity functions. This can't fail. 13767c478bd9Sstevel@tonic-gate */ 13777c478bd9Sstevel@tonic-gate static void 13787c478bd9Sstevel@tonic-gate nslberi_install_compat_io_fns( Sockbuf *sb ) 13797c478bd9Sstevel@tonic-gate { 13807c478bd9Sstevel@tonic-gate sb->sb_ext_io_fns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE; 13817c478bd9Sstevel@tonic-gate sb->sb_ext_io_fns.lbextiofn_read = nslberi_extread_compat; 13827c478bd9Sstevel@tonic-gate sb->sb_ext_io_fns.lbextiofn_write = nslberi_extwrite_compat; 13837c478bd9Sstevel@tonic-gate sb->sb_ext_io_fns.lbextiofn_socket_arg = (void *)sb; 13847c478bd9Sstevel@tonic-gate } 13857c478bd9Sstevel@tonic-gate /* 13867c478bd9Sstevel@tonic-gate * end of compat I/O functions 13877c478bd9Sstevel@tonic-gate ****************************************************************************** 13887c478bd9Sstevel@tonic-gate */ 1389