1 /* encode.c - ber output encoding routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
35 #include <ac/stdlib.h>
37 #include <ac/stdarg.h>
38 #include <ac/socket.h>
39 #include <ac/string.h>
43 static int ber_put_len LDAP_P((
48 static int ber_start_seqorset LDAP_P((
52 static int ber_put_seqorset LDAP_P(( BerElement *ber ));
54 static int ber_put_int_or_enum LDAP_P((
61 ber_calc_taglen( ber_tag_t tag )
66 /* find the first non-all-zero byte in the tag */
67 for ( i = sizeof(ber_tag_t) - 1; i > 0; i-- ) {
68 mask = ((ber_tag_t)0xffU << (i * 8));
70 if ( tag & mask ) break;
85 unsigned char nettag[sizeof(ber_tag_t)];
87 assert( ber != NULL );
88 assert( LBER_VALID( ber ) );
90 taglen = ber_calc_taglen( tag );
92 for( i=0; i<taglen; i++ ) {
93 nettag[(sizeof(ber_tag_t)-1) - i] = (unsigned char)(tag & 0xffU);
98 (char *) &nettag[sizeof(ber_tag_t) - taglen],
105 ber_calc_lenlen( ber_len_t len )
108 * short len if it's less than 128 - one byte giving the len,
112 if ( len <= (ber_len_t) 0x7FU ) return 1;
115 * long len otherwise - one byte with bit 8 set, giving the
116 * length of the length, followed by the length itself.
119 if ( len <= (ber_len_t) 0xffU ) return 2;
120 if ( len <= (ber_len_t) 0xffffU ) return 3;
121 if ( len <= (ber_len_t) 0xffffffU ) return 4;
127 ber_put_len( BerElement *ber, ber_len_t len, int nosos )
133 unsigned char netlen[sizeof(ber_len_t)];
135 assert( ber != NULL );
136 assert( LBER_VALID( ber ) );
139 * short len if it's less than 128 - one byte giving the len,
144 char length_byte = (char) len;
145 return ber_write( ber, &length_byte, 1, nosos );
149 * long len otherwise - one byte with bit 8 set, giving the
150 * length of the length, followed by the length itself.
153 /* find the first non-all-zero byte */
154 for ( i = sizeof(ber_len_t) - 1; i > 0; i-- ) {
155 mask = ((ber_len_t)0xffU << (i * 8));
157 if ( len & mask ) break;
159 lenlen = (unsigned char) ++i;
160 if ( lenlen > 4 ) return -1;
164 /* write the length of the length */
165 if ( ber_write( ber, &lenlen, 1, nosos ) != 1 ) return -1;
167 for( j=0; j<i; j++) {
168 netlen[(sizeof(ber_len_t)-1) - j] = (unsigned char)(len & 0xffU);
172 /* write the length itself */
174 (char *) &netlen[sizeof(ber_len_t)-i],
177 return rc == i ? i+1 : -1;
187 int i, j, sign, taglen, lenlen;
189 ber_uint_t unum, mask;
190 unsigned char netnum[sizeof(ber_uint_t)];
192 assert( ber != NULL );
193 assert( LBER_VALID( ber ) );
196 unum = num; /* Bit fiddling should be done with unsigned values */
199 * high bit is set - look for first non-all-one byte
200 * high bit is clear - look for first non-all-zero byte
202 for ( i = sizeof(ber_int_t) - 1; i > 0; i-- ) {
203 mask = ((ber_uint_t)0xffU << (i * 8));
207 if ( (unum & mask) != mask ) break;
210 if ( unum & mask ) break;
215 * we now have the "leading byte". if the high bit on this
216 * byte matches the sign bit, we need to "back up" a byte.
218 mask = (unum & ((ber_uint_t)0x80U << (i * 8)));
219 if ( (mask && !sign) || (sign && !mask) ) {
225 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
229 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ) {
234 for( j=0; j<i; j++ ) {
235 netnum[(sizeof(ber_int_t)-1) - j] = (unsigned char)(unum & 0xffU);
240 (char *) &netnum[sizeof(ber_int_t) - i],
243 /* length of tag + length + contents */
244 return rc == i ? taglen + lenlen + i : -1;
253 assert( ber != NULL );
254 assert( LBER_VALID( ber ) );
256 if ( tag == LBER_DEFAULT ) {
257 tag = LBER_ENUMERATED;
260 return ber_put_int_or_enum( ber, num, tag );
269 assert( ber != NULL );
270 assert( LBER_VALID( ber ) );
272 if ( tag == LBER_DEFAULT ) {
276 return ber_put_int_or_enum( ber, num, tag );
282 LDAP_CONST char *str,
286 int taglen, lenlen, rc;
288 assert( ber != NULL );
289 assert( str != NULL );
291 assert( LBER_VALID( ber ) );
293 if ( tag == LBER_DEFAULT ) {
294 tag = LBER_OCTETSTRING;
297 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
300 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
301 (ber_len_t) ber_write( ber, str, len, 0 ) != len )
305 /* return length of tag + length + contents */
306 rc = taglen + lenlen + len;
318 assert( ber != NULL );
319 assert( LBER_VALID( ber ) );
321 if( bv == NULL || bv->bv_len == 0 ) {
322 return ber_put_ostring( ber, "", (ber_len_t) 0, tag );
325 return ber_put_ostring( ber, bv->bv_val, bv->bv_len, tag );
331 LDAP_CONST char *str,
334 assert( ber != NULL );
335 assert( str != NULL );
337 assert( LBER_VALID( ber ) );
339 return ber_put_ostring( ber, str, strlen( str ), tag );
345 LDAP_CONST char *str,
346 ber_len_t blen /* in bits */,
351 unsigned char unusedbits;
353 assert( ber != NULL );
354 assert( str != NULL );
356 assert( LBER_VALID( ber ) );
358 if ( tag == LBER_DEFAULT ) {
359 tag = LBER_BITSTRING;
362 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
366 len = ( blen + 7 ) / 8;
367 unusedbits = (unsigned char) ((len * 8) - blen);
368 if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 ) {
372 if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 ) {
376 if ( (ber_len_t) ber_write( ber, str, len, 0 ) != len ) {
380 /* return length of tag + length + unused bit count + contents */
381 return taglen + 1 + lenlen + len;
385 ber_put_null( BerElement *ber, ber_tag_t tag )
389 assert( ber != NULL );
390 assert( LBER_VALID( ber ) );
392 if ( tag == LBER_DEFAULT ) {
396 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
400 if ( ber_put_len( ber, 0, 0 ) != 1 ) {
416 assert( ber != NULL );
417 assert( LBER_VALID( ber ) );
419 if ( tag == LBER_DEFAULT )
422 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
426 if ( ber_put_len( ber, 1, 0 ) != 1 ) {
430 c = boolval ? (unsigned char) ~0U : (unsigned char) 0U;
432 if ( ber_write( ber, (char *) &c, 1, 0 ) != 1 ) {
439 #define FOUR_BYTE_LEN 5
448 assert( ber != NULL );
449 assert( LBER_VALID( ber ) );
451 new = (Seqorset *) ber_memcalloc_x( 1, sizeof(Seqorset), ber->ber_memctx );
458 if ( ber->ber_sos == NULL ) {
459 new->sos_first = ber->ber_ptr;
461 new->sos_first = ber->ber_sos->sos_ptr;
464 /* Set aside room for a 4 byte length field */
465 new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
468 new->sos_next = ber->ber_sos;
475 ber_start_seq( BerElement *ber, ber_tag_t tag )
477 assert( ber != NULL );
478 assert( LBER_VALID( ber ) );
480 if ( tag == LBER_DEFAULT ) {
484 return ber_start_seqorset( ber, tag );
488 ber_start_set( BerElement *ber, ber_tag_t tag )
490 assert( ber != NULL );
491 assert( LBER_VALID( ber ) );
493 if ( tag == LBER_DEFAULT ) {
497 return ber_start_seqorset( ber, tag );
501 ber_put_seqorset( BerElement *ber )
505 unsigned char netlen[sizeof(ber_len_t)];
508 unsigned char ltag = 0x80U + FOUR_BYTE_LEN - 1;
510 Seqorset **sos = &ber->ber_sos;
512 assert( ber != NULL );
513 assert( LBER_VALID( ber ) );
515 if( *sos == NULL ) return -1;
518 * If this is the toplevel sequence or set, we need to actually
519 * write the stuff out. Otherwise, it's already been put in
520 * the appropriate buffer and will be written when the toplevel
521 * one is written. In this case all we need to do is update the
525 len = (*sos)->sos_clen;
527 if ( sizeof(ber_len_t) > 4 && len > 0xffffffffUL ) {
531 if ( ber->ber_options & LBER_USE_DER ) {
532 lenlen = ber_calc_lenlen( len );
535 lenlen = FOUR_BYTE_LEN;
540 for( i=0; i < lenlen-1; i++ ) {
541 netlen[(sizeof(ber_len_t)-1) - i] =
542 (unsigned char)((len >> i*8) & 0xffU);
545 netlen[sizeof(ber_len_t)-1] = (unsigned char)(len & 0x7fU);
548 if ( (next = (*sos)->sos_next) == NULL ) {
550 if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 ) {
554 if ( ber->ber_options & LBER_USE_DER ) {
555 /* Write the length in the minimum # of octets */
556 if ( ber_put_len( ber, len, 1 ) == -1 ) {
560 if (lenlen != FOUR_BYTE_LEN) {
562 * We set aside FOUR_BYTE_LEN bytes for
563 * the length field. Move the data if
564 * we don't actually need that much
566 AC_MEMCPY( (*sos)->sos_first + taglen +
567 lenlen, (*sos)->sos_first + taglen +
568 FOUR_BYTE_LEN, len );
571 /* Fill FOUR_BYTE_LEN bytes for length field */
572 /* one byte of length length */
573 if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 ) {
577 /* the length itself */
579 (char *) &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN-1)],
580 FOUR_BYTE_LEN-1, 1 );
582 if( rc != FOUR_BYTE_LEN - 1 ) {
586 /* The ber_ptr is at the set/seq start - move it to the end */
587 (*sos)->sos_ber->ber_ptr += len;
591 unsigned char nettag[sizeof(ber_tag_t)];
592 ber_tag_t tmptag = (*sos)->sos_tag;
594 if( ber->ber_sos->sos_ptr > ber->ber_end ) {
595 /* The sos_ptr exceeds the end of the BerElement
596 * this can happen, for example, when the sos_ptr
597 * is near the end and no data was written for the
598 * 'V'. We must realloc the BerElement to ensure
599 * we don't overwrite the buffer when writing
600 * the tag and length fields.
602 ber_len_t ext = ber->ber_sos->sos_ptr - ber->ber_end;
604 if( ber_realloc( ber, ext ) != 0 ) {
610 taglen = ber_calc_taglen( tmptag );
612 for( i = 0; i < taglen; i++ ) {
613 nettag[(sizeof(ber_tag_t)-1) - i] = (unsigned char)(tmptag & 0xffU);
617 AC_FMEMCPY( (*sos)->sos_first,
618 &nettag[sizeof(ber_tag_t) - taglen],
621 if ( ber->ber_options & LBER_USE_DER ) {
623 ? (unsigned char) len
624 : (unsigned char) (0x80U + (lenlen - 1));
627 /* one byte of length length */
628 (*sos)->sos_first[1] = ltag;
630 if ( ber->ber_options & LBER_USE_DER ) {
632 /* Write the length itself */
633 AC_FMEMCPY( (*sos)->sos_first + 2,
634 &netlen[sizeof(ber_len_t) - (lenlen - 1)],
637 if (lenlen != FOUR_BYTE_LEN) {
639 * We set aside FOUR_BYTE_LEN bytes for
640 * the length field. Move the data if
641 * we don't actually need that much
643 AC_FMEMCPY( (*sos)->sos_first + taglen +
644 lenlen, (*sos)->sos_first + taglen +
645 FOUR_BYTE_LEN, len );
648 /* the length itself */
649 AC_FMEMCPY( (*sos)->sos_first + taglen + 1,
650 &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN - 1)],
654 next->sos_clen += (taglen + lenlen + len);
655 next->sos_ptr += (taglen + lenlen + len);
658 /* we're done with this seqorset, so free it up */
659 ber_memfree_x( (char *) (*sos), ber->ber_memctx );
662 return taglen + lenlen + len;
666 ber_put_seq( BerElement *ber )
668 assert( ber != NULL );
669 assert( LBER_VALID( ber ) );
671 return ber_put_seqorset( ber );
675 ber_put_set( BerElement *ber )
677 assert( ber != NULL );
678 assert( LBER_VALID( ber ) );
680 return ber_put_seqorset( ber );
684 static ber_tag_t lber_int_null = 0;
688 ber_printf( BerElement *ber, LDAP_CONST char *fmt, ... )
692 struct berval *bv, **bvp;
697 assert( ber != NULL );
698 assert( fmt != NULL );
700 assert( LBER_VALID( ber ) );
704 for ( rc = 0; *fmt && rc != -1; fmt++ ) {
706 case '!': { /* hook */
707 BEREncodeCallback *f;
710 f = va_arg( ap, BEREncodeCallback * );
711 p = va_arg( ap, void * );
716 case 'b': /* boolean */
717 i = va_arg( ap, ber_int_t );
718 rc = ber_put_boolean( ber, i, ber->ber_tag );
722 i = va_arg( ap, ber_int_t );
723 rc = ber_put_int( ber, i, ber->ber_tag );
726 case 'e': /* enumeration */
727 i = va_arg( ap, ber_int_t );
728 rc = ber_put_enum( ber, i, ber->ber_tag );
732 rc = ber_put_null( ber, ber->ber_tag );
735 case 'N': /* Debug NULL */
736 if( lber_int_null != 0 ) {
737 /* Insert NULL to ensure peer ignores unknown tags */
738 rc = ber_put_null( ber, lber_int_null );
744 case 'o': /* octet string (non-null terminated) */
745 s = va_arg( ap, char * );
746 len = va_arg( ap, ber_len_t );
747 rc = ber_put_ostring( ber, s, len, ber->ber_tag );
750 case 'O': /* berval octet string */
751 bv = va_arg( ap, struct berval * );
752 if( bv == NULL ) break;
753 rc = ber_put_berval( ber, bv, ber->ber_tag );
756 case 's': /* string */
757 s = va_arg( ap, char * );
758 rc = ber_put_string( ber, s, ber->ber_tag );
761 case 'B': /* bit string */
762 case 'X': /* bit string (deprecated) */
763 s = va_arg( ap, char * );
764 len = va_arg( ap, int ); /* in bits */
765 rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
768 case 't': /* tag for the next element */
769 ber->ber_tag = va_arg( ap, ber_tag_t );
770 ber->ber_usertag = 1;
773 case 'v': /* vector of strings */
774 if ( (ss = va_arg( ap, char ** )) == NULL )
776 for ( i = 0; ss[i] != NULL; i++ ) {
777 if ( (rc = ber_put_string( ber, ss[i],
778 ber->ber_tag )) == -1 )
783 case 'V': /* sequences of strings + lengths */
784 if ( (bvp = va_arg( ap, struct berval ** )) == NULL )
786 for ( i = 0; bvp[i] != NULL; i++ ) {
787 if ( (rc = ber_put_berval( ber, bvp[i],
788 ber->ber_tag )) == -1 )
793 case 'W': /* BerVarray */
794 if ( (bv = va_arg( ap, BerVarray )) == NULL )
796 for ( i = 0; bv[i].bv_val != NULL; i++ ) {
797 if ( (rc = ber_put_berval( ber, &bv[i],
798 ber->ber_tag )) == -1 )
803 case '{': /* begin sequence */
804 rc = ber_start_seq( ber, ber->ber_tag );
807 case '}': /* end sequence */
808 rc = ber_put_seqorset( ber );
811 case '[': /* begin set */
812 rc = ber_start_set( ber, ber->ber_tag );
815 case ']': /* end set */
816 rc = ber_put_seqorset( ber );
820 if( ber->ber_debug ) {
821 ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
822 "ber_printf: unknown fmt %c\n", *fmt );
828 if ( ber->ber_usertag == 0 ) {
829 ber->ber_tag = LBER_DEFAULT;
831 ber->ber_usertag = 0;