1 /* Encode.c - ber output encoding routines */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * Copyright (c) 1990 Regents of the University of Michigan.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that this notice is preserved and that due credit is given
13 * to the University of Michigan at Ann Arbor. The name of the University
14 * may not be used to endorse or promote products derived from this
15 * software without specific prior written permission. This software
16 * is provided ``as is'' without express or implied warranty.
23 #include <ac/stdlib.h>
25 #include <ac/stdarg.h>
26 #include <ac/socket.h>
27 #include <ac/string.h>
31 static int ber_put_len LDAP_P((
36 static int ber_start_seqorset LDAP_P((
40 static int ber_put_seqorset LDAP_P(( BerElement *ber ));
42 static int ber_put_int_or_enum LDAP_P((
49 ber_calc_taglen( ber_tag_t tag )
54 /* find the first non-all-zero byte in the tag */
55 for ( i = sizeof(ber_tag_t) - 1; i > 0; i-- ) {
56 mask = ((ber_tag_t)0xffU << (i * 8));
58 if ( tag & mask ) break;
73 unsigned char nettag[sizeof(ber_tag_t)];
75 assert( ber != NULL );
76 assert( LBER_VALID( ber ) );
78 taglen = ber_calc_taglen( tag );
80 for( i=0; i<taglen; i++ ) {
81 nettag[(sizeof(ber_tag_t)-1) - i] = (unsigned char)(tag & 0xffU);
86 &nettag[sizeof(ber_tag_t) - taglen],
93 ber_calc_lenlen( ber_len_t len )
96 * short len if it's less than 128 - one byte giving the len,
100 if ( len <= (ber_len_t) 0x7FU )
104 * long len otherwise - one byte with bit 8 set, giving the
105 * length of the length, followed by the length itself.
108 if ( len <= (ber_len_t) 0xffU )
110 if ( len <= (ber_len_t) 0xffffU )
112 if ( len <= (ber_len_t) 0xffffffU )
119 ber_put_len( BerElement *ber, ber_len_t len, int nosos )
125 unsigned char netlen[sizeof(ber_len_t)];
127 assert( ber != NULL );
128 assert( LBER_VALID( ber ) );
131 * short len if it's less than 128 - one byte giving the len,
136 char length_byte = (char) len;
137 return ber_write( ber, &length_byte, 1, nosos );
141 * long len otherwise - one byte with bit 8 set, giving the
142 * length of the length, followed by the length itself.
145 /* find the first non-all-zero byte */
146 for ( i = sizeof(ber_len_t) - 1; i > 0; i-- ) {
147 mask = ((ber_len_t)0xffU << (i * 8));
149 if ( len & mask ) break;
151 lenlen = (unsigned char) ++i;
157 /* write the length of the length */
158 if ( ber_write( ber, &lenlen, 1, nosos ) != 1 )
161 for( j=0; j<i; j++) {
162 netlen[(sizeof(ber_len_t)-1) - j] = (unsigned char)(len & 0xffU);
166 /* write the length itself */
168 &netlen[sizeof(ber_len_t)-i],
171 return rc == i ? i+1 : -1;
181 int i, j, sign, taglen, lenlen;
183 ber_uint_t unum, mask;
184 unsigned char netnum[sizeof(ber_uint_t)];
186 assert( ber != NULL );
187 assert( LBER_VALID( ber ) );
190 unum = num; /* Bit fiddling should be done with unsigned values */
193 * high bit is set - look for first non-all-one byte
194 * high bit is clear - look for first non-all-zero byte
196 for ( i = sizeof(ber_int_t) - 1; i > 0; i-- ) {
197 mask = ((ber_uint_t)0xffU << (i * 8));
201 if ( (unum & mask) != mask ) break;
204 if ( unum & mask ) break;
209 * we now have the "leading byte". if the high bit on this
210 * byte matches the sign bit, we need to "back up" a byte.
212 mask = (unum & ((ber_uint_t)0x80U << (i * 8)));
213 if ( (mask && !sign) || (sign && !mask) ) {
219 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
223 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
227 for( j=0; j<i; j++ ) {
228 netnum[(sizeof(ber_int_t)-1) - j] = (unsigned char)(unum & 0xffU);
233 &netnum[sizeof(ber_int_t) - i],
236 /* length of tag + length + contents */
237 return rc == i ? taglen + lenlen + i : -1;
246 assert( ber != NULL );
247 assert( LBER_VALID( ber ) );
249 if ( tag == LBER_DEFAULT ) {
250 tag = LBER_ENUMERATED;
253 return ber_put_int_or_enum( ber, num, tag );
262 assert( ber != NULL );
263 assert( LBER_VALID( ber ) );
265 if ( tag == LBER_DEFAULT ) {
269 return ber_put_int_or_enum( ber, num, tag );
275 LDAP_CONST char *str,
279 int taglen, lenlen, rc;
281 assert( ber != NULL );
282 assert( str != NULL );
284 assert( LBER_VALID( ber ) );
286 if ( tag == LBER_DEFAULT ) {
287 tag = LBER_OCTETSTRING;
290 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
293 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
294 (ber_len_t) ber_write( ber, str, len, 0 ) != len ) {
297 /* return length of tag + length + contents */
298 rc = taglen + lenlen + len;
310 assert( ber != NULL );
311 assert( LBER_VALID( ber ) );
313 if( bv == NULL || bv->bv_len == 0 ) {
314 return ber_put_ostring( ber, "", (ber_len_t) 0, tag );
317 return ber_put_ostring( ber, bv->bv_val, bv->bv_len, tag );
323 LDAP_CONST char *str,
326 assert( ber != NULL );
327 assert( str != NULL );
329 assert( LBER_VALID( ber ) );
331 return ber_put_ostring( ber, str, strlen( str ), tag );
337 LDAP_CONST char *str,
338 ber_len_t blen /* in bits */,
343 unsigned char unusedbits;
345 assert( ber != NULL );
346 assert( str != NULL );
348 assert( LBER_VALID( ber ) );
350 if ( tag == LBER_DEFAULT ) {
351 tag = LBER_BITSTRING;
354 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
358 len = ( blen + 7 ) / 8;
359 unusedbits = (unsigned char) ((len * 8) - blen);
360 if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 ) {
364 if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 ) {
368 if ( (ber_len_t) ber_write( ber, str, len, 0 ) != len ) {
372 /* return length of tag + length + unused bit count + contents */
373 return taglen + 1 + lenlen + len;
377 ber_put_null( BerElement *ber, ber_tag_t tag )
381 assert( ber != NULL );
382 assert( LBER_VALID( ber ) );
384 if ( tag == LBER_DEFAULT ) {
388 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
392 if ( ber_put_len( ber, 0, 0 ) != 1 ) {
408 assert( ber != NULL );
409 assert( LBER_VALID( ber ) );
411 if ( tag == LBER_DEFAULT )
414 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
418 if ( ber_put_len( ber, 1, 0 ) != 1 ) {
422 c = boolval ? (unsigned char) ~0U : (unsigned char) 0U;
424 if ( ber_write( ber, (char *) &c, 1, 0 )
433 #define FOUR_BYTE_LEN 5
442 assert( ber != NULL );
443 assert( LBER_VALID( ber ) );
445 new = (Seqorset *) LBER_CALLOC( 1, sizeof(Seqorset) );
452 if ( ber->ber_sos == NULL ) {
453 new->sos_first = ber->ber_ptr;
455 new->sos_first = ber->ber_sos->sos_ptr;
458 /* Set aside room for a 4 byte length field */
459 new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
462 new->sos_next = ber->ber_sos;
469 ber_start_seq( BerElement *ber, ber_tag_t tag )
471 assert( ber != NULL );
472 assert( LBER_VALID( ber ) );
474 if ( tag == LBER_DEFAULT ) {
478 return ber_start_seqorset( ber, tag );
482 ber_start_set( BerElement *ber, ber_tag_t tag )
484 assert( ber != NULL );
485 assert( LBER_VALID( ber ) );
487 if ( tag == LBER_DEFAULT ) {
491 return ber_start_seqorset( ber, tag );
495 ber_put_seqorset( BerElement *ber )
499 unsigned char netlen[sizeof(ber_len_t)];
502 unsigned char ltag = 0x80U + FOUR_BYTE_LEN - 1;
504 Seqorset **sos = &ber->ber_sos;
506 assert( ber != NULL );
507 assert( LBER_VALID( ber ) );
509 if( *sos == NULL ) return -1;
512 * If this is the toplevel sequence or set, we need to actually
513 * write the stuff out. Otherwise, it's already been put in
514 * the appropriate buffer and will be written when the toplevel
515 * one is written. In this case all we need to do is update the
519 len = (*sos)->sos_clen;
521 if ( sizeof(ber_len_t) > 4 && len > 0xffffffffUL ) {
525 if ( ber->ber_options & LBER_USE_DER ) {
526 lenlen = ber_calc_lenlen( len );
529 lenlen = FOUR_BYTE_LEN;
534 for( i=0; i < lenlen-1; i++ ) {
535 netlen[(sizeof(ber_len_t)-1) - i] =
536 (unsigned char)((len >> i*8) & 0xffU);
539 netlen[sizeof(ber_len_t)-1] = (unsigned char)(len & 0x7fU);
542 if ( (next = (*sos)->sos_next) == NULL ) {
544 if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
547 if ( ber->ber_options & LBER_USE_DER ) {
548 /* Write the length in the minimum # of octets */
549 if ( ber_put_len( ber, len, 1 ) == -1 ) {
553 if (lenlen != FOUR_BYTE_LEN) {
555 * We set aside FOUR_BYTE_LEN bytes for
556 * the length field. Move the data if
557 * we don't actually need that much
559 AC_MEMCPY( (*sos)->sos_first + taglen +
560 lenlen, (*sos)->sos_first + taglen +
561 FOUR_BYTE_LEN, len );
564 /* Fill FOUR_BYTE_LEN bytes for length field */
565 /* one byte of length length */
566 if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 ) {
570 /* the length itself */
572 &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN-1)],
573 FOUR_BYTE_LEN-1, 1 );
575 if( rc != FOUR_BYTE_LEN - 1 ) {
579 /* The ber_ptr is at the set/seq start - move it to the end */
580 (*sos)->sos_ber->ber_ptr += len;
584 unsigned char nettag[sizeof(ber_tag_t)];
585 ber_tag_t tmptag = (*sos)->sos_tag;
587 if( ber->ber_sos->sos_ptr > ber->ber_end ) {
588 /* The sos_ptr exceeds the end of the BerElement
589 * this can happen, for example, when the sos_ptr
590 * is near the end and no data was written for the
591 * 'V'. We must realloc the BerElement to ensure
592 * we don't overwrite the buffer when writing
593 * the tag and length fields.
595 ber_len_t ext = ber->ber_sos->sos_ptr - ber->ber_end;
597 if( ber_realloc( ber, ext ) != 0 ) {
603 taglen = ber_calc_taglen( tmptag );
605 for( i = 0; i < taglen; i++ ) {
606 nettag[(sizeof(ber_tag_t)-1) - i] = (unsigned char)(tmptag & 0xffU);
610 AC_FMEMCPY( (*sos)->sos_first,
611 &nettag[sizeof(ber_tag_t) - taglen],
614 if ( ber->ber_options & LBER_USE_DER ) {
616 ? (unsigned char) len
617 : (unsigned char) (0x80U + (lenlen - 1));
620 /* one byte of length length */
621 (*sos)->sos_first[1] = ltag;
623 if ( ber->ber_options & LBER_USE_DER ) {
625 /* Write the length itself */
626 AC_FMEMCPY( (*sos)->sos_first + 2,
627 &netlen[sizeof(ber_len_t) - (lenlen - 1)],
630 if (lenlen != FOUR_BYTE_LEN) {
632 * We set aside FOUR_BYTE_LEN bytes for
633 * the length field. Move the data if
634 * we don't actually need that much
636 AC_FMEMCPY( (*sos)->sos_first + taglen +
637 lenlen, (*sos)->sos_first + taglen +
638 FOUR_BYTE_LEN, len );
641 /* the length itself */
642 AC_FMEMCPY( (*sos)->sos_first + taglen + 1,
643 &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN - 1)],
647 next->sos_clen += (taglen + lenlen + len);
648 next->sos_ptr += (taglen + lenlen + len);
651 /* we're done with this seqorset, so free it up */
652 LBER_FREE( (char *) (*sos) );
655 return taglen + lenlen + len;
659 ber_put_seq( BerElement *ber )
661 assert( ber != NULL );
662 assert( LBER_VALID( ber ) );
664 return ber_put_seqorset( ber );
668 ber_put_set( BerElement *ber )
670 assert( ber != NULL );
671 assert( LBER_VALID( ber ) );
673 return ber_put_seqorset( ber );
677 static ber_tag_t lber_int_null = 0;
681 ber_printf( BerElement *ber, LDAP_CONST char *fmt, ... )
685 struct berval *bv, **bvp;
690 assert( ber != NULL );
691 assert( fmt != NULL );
693 assert( LBER_VALID( ber ) );
697 for ( rc = 0; *fmt && rc != -1; fmt++ ) {
699 case '!': { /* hook */
700 BEREncodeCallback *f;
703 f = va_arg( ap, BEREncodeCallback * );
704 p = va_arg( ap, void * );
709 case 'b': /* boolean */
710 i = va_arg( ap, ber_int_t );
711 rc = ber_put_boolean( ber, i, ber->ber_tag );
715 i = va_arg( ap, ber_int_t );
716 rc = ber_put_int( ber, i, ber->ber_tag );
719 case 'e': /* enumeration */
720 i = va_arg( ap, ber_int_t );
721 rc = ber_put_enum( ber, i, ber->ber_tag );
725 rc = ber_put_null( ber, ber->ber_tag );
728 case 'N': /* Debug NULL */
729 if( lber_int_null != 0 ) {
730 /* Insert NULL to ensure peer ignores unknown tags */
731 rc = ber_put_null( ber, lber_int_null );
737 case 'o': /* octet string (non-null terminated) */
738 s = va_arg( ap, char * );
739 len = va_arg( ap, ber_len_t );
740 rc = ber_put_ostring( ber, s, len, ber->ber_tag );
743 case 'O': /* berval octet string */
744 bv = va_arg( ap, struct berval * );
745 if( bv == NULL ) break;
746 rc = ber_put_berval( ber, bv, ber->ber_tag );
749 case 's': /* string */
750 s = va_arg( ap, char * );
751 rc = ber_put_string( ber, s, ber->ber_tag );
754 case 'B': /* bit string */
755 case 'X': /* bit string (deprecated) */
756 s = va_arg( ap, char * );
757 len = va_arg( ap, int ); /* in bits */
758 rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
761 case 't': /* tag for the next element */
762 ber->ber_tag = va_arg( ap, ber_tag_t );
763 ber->ber_usertag = 1;
766 case 'v': /* vector of strings */
767 if ( (ss = va_arg( ap, char ** )) == NULL )
769 for ( i = 0; ss[i] != NULL; i++ ) {
770 if ( (rc = ber_put_string( ber, ss[i],
771 ber->ber_tag )) == -1 )
776 case 'V': /* sequences of strings + lengths */
777 if ( (bvp = va_arg( ap, struct berval ** )) == NULL )
779 for ( i = 0; bvp[i] != NULL; i++ ) {
780 if ( (rc = ber_put_berval( ber, bvp[i],
781 ber->ber_tag )) == -1 )
786 case 'W': /* BerVarray */
787 if ( (bv = va_arg( ap, BerVarray )) == NULL )
789 for ( i = 0; bv[i].bv_val != NULL; i++ ) {
790 if ( (rc = ber_put_berval( ber, &bv[i],
791 ber->ber_tag )) == -1 )
796 case '{': /* begin sequence */
797 rc = ber_start_seq( ber, ber->ber_tag );
800 case '}': /* end sequence */
801 rc = ber_put_seqorset( ber );
804 case '[': /* begin set */
805 rc = ber_start_set( ber, ber->ber_tag );
808 case ']': /* end set */
809 rc = ber_put_seqorset( ber );
813 if( ber->ber_debug ) {
816 "ber_printf: unknown fmt %c\n", *fmt, 0, 0 );
818 ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
819 "ber_printf: unknown fmt %c\n", *fmt );
826 if ( ber->ber_usertag == 0 )
827 ber->ber_tag = LBER_DEFAULT;
829 ber->ber_usertag = 0;