1 /* Encode.c - ber output encoding routines */
4 * Copyright 1998-2000 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( BER_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( BER_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;
182 ber_len_t len, lenlen, taglen;
183 ber_uint_t unum, mask;
184 unsigned char netnum[sizeof(ber_uint_t)];
186 assert( ber != NULL );
187 assert( BER_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( BER_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( BER_VALID( ber ) );
265 if ( tag == LBER_DEFAULT ) {
269 return ber_put_int_or_enum( ber, num, tag );
275 LDAP_CONST char *str,
279 ber_len_t taglen, lenlen;
282 assert( ber != NULL );
283 assert( str != NULL );
285 assert( BER_VALID( ber ) );
287 if ( tag == LBER_DEFAULT ) {
288 tag = LBER_OCTETSTRING;
291 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
294 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
295 (ber_len_t) ber_write( ber, str, len, 0 ) != len ) {
298 /* return length of tag + length + contents */
299 rc = taglen + lenlen + len;
308 LDAP_CONST struct berval *bv,
311 assert( ber != NULL );
312 assert( BER_VALID( ber ) );
314 if( bv == NULL || bv->bv_len == 0 ) {
315 return ber_put_ostring( ber, "", (ber_len_t) 0, tag );
318 return ber_put_ostring( ber, bv->bv_val, bv->bv_len, tag );
324 LDAP_CONST char *str,
327 assert( ber != NULL );
328 assert( str != NULL );
330 assert( BER_VALID( ber ) );
332 return ber_put_ostring( ber, str, strlen( str ), tag );
338 LDAP_CONST char *str,
339 ber_len_t blen /* in bits */,
342 ber_len_t taglen, lenlen, len;
343 unsigned char unusedbits;
345 assert( ber != NULL );
346 assert( str != NULL );
348 assert( BER_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( BER_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 ) {
406 unsigned char trueval = (unsigned char) -1;
407 unsigned char falseval = 0;
409 assert( ber != NULL );
410 assert( BER_VALID( ber ) );
412 if ( tag == LBER_DEFAULT )
415 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 ) {
419 if ( ber_put_len( ber, 1, 0 ) != 1 ) {
423 if ( ber_write( ber, (char *)(boolval ? &trueval : &falseval), 1, 0 )
432 #define FOUR_BYTE_LEN 5
441 assert( ber != NULL );
442 assert( BER_VALID( ber ) );
444 new = (Seqorset *) LBER_CALLOC( 1, sizeof(Seqorset) );
451 if ( ber->ber_sos == NULL ) {
452 new->sos_first = ber->ber_ptr;
454 new->sos_first = ber->ber_sos->sos_ptr;
457 /* Set aside room for a 4 byte length field */
458 new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
461 new->sos_next = ber->ber_sos;
468 ber_start_seq( BerElement *ber, ber_tag_t tag )
470 assert( ber != NULL );
471 assert( BER_VALID( ber ) );
473 if ( tag == LBER_DEFAULT ) {
477 return ber_start_seqorset( ber, tag );
481 ber_start_set( BerElement *ber, ber_tag_t tag )
483 assert( ber != NULL );
484 assert( BER_VALID( ber ) );
486 if ( tag == LBER_DEFAULT ) {
490 return ber_start_seqorset( ber, tag );
494 ber_put_seqorset( BerElement *ber )
498 unsigned char netlen[sizeof(ber_len_t)];
499 ber_len_t taglen, lenlen;
500 unsigned char ltag = 0x80U + FOUR_BYTE_LEN - 1;
502 Seqorset **sos = &ber->ber_sos;
504 assert( ber != NULL );
505 assert( BER_VALID( ber ) );
508 * If this is the toplevel sequence or set, we need to actually
509 * write the stuff out. Otherwise, it's already been put in
510 * the appropriate buffer and will be written when the toplevel
511 * one is written. In this case all we need to do is update the
515 len = (*sos)->sos_clen;
517 if ( sizeof(ber_len_t) > 4 && len > 0xffffffffUL ) {
521 if ( ber->ber_options & LBER_USE_DER ) {
522 lenlen = ber_calc_lenlen( len );
525 lenlen = FOUR_BYTE_LEN;
530 for( i=0; i < lenlen-1; i++ ) {
531 netlen[(sizeof(ber_len_t)-1) - i] =
532 (unsigned char)((len >> i*8) & 0xffU);
535 netlen[sizeof(ber_len_t)-1] = (unsigned char)(len & 0x7fU);
538 if ( (next = (*sos)->sos_next) == NULL ) {
540 if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
543 if ( ber->ber_options & LBER_USE_DER ) {
544 /* Write the length in the minimum # of octets */
545 if ( ber_put_len( ber, len, 1 ) == -1 ) {
549 if (lenlen != FOUR_BYTE_LEN) {
551 * We set aside FOUR_BYTE_LEN bytes for
552 * the length field. Move the data if
553 * we don't actually need that much
555 AC_MEMCPY( (*sos)->sos_first + taglen +
556 lenlen, (*sos)->sos_first + taglen +
557 FOUR_BYTE_LEN, len );
560 /* Fill FOUR_BYTE_LEN bytes for length field */
561 /* one byte of length length */
562 if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 ) {
566 /* the length itself */
568 &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN-1)],
569 FOUR_BYTE_LEN-1, 1 );
571 if( rc != FOUR_BYTE_LEN - 1 ) {
575 /* The ber_ptr is at the set/seq start - move it to the end */
576 (*sos)->sos_ber->ber_ptr += len;
580 unsigned char nettag[sizeof(ber_tag_t)];
581 ber_tag_t tmptag = (*sos)->sos_tag;
583 if( ber->ber_sos->sos_ptr > ber->ber_end ) {
584 /* The sos_ptr exceeds the end of the BerElement
585 * this can happen, for example, when the sos_ptr
586 * is near the end and no data was written for the
587 * 'V'. We must realloc the BerElement to ensure
588 * we don't overwrite the buffer when writing
589 * the tag and length fields.
591 ber_len_t ext = ber->ber_sos->sos_ptr - ber->ber_end;
593 if( ber_realloc( ber, ext ) != 0 ) {
599 taglen = ber_calc_taglen( tmptag );
601 for( i = 0; i < taglen; i++ ) {
602 nettag[(sizeof(ber_tag_t)-1) - i] = (unsigned char)(tmptag & 0xffU);
606 AC_FMEMCPY( (*sos)->sos_first,
607 &nettag[sizeof(ber_tag_t) - taglen],
610 if ( ber->ber_options & LBER_USE_DER ) {
612 ? (unsigned char) len
613 : (unsigned char) (0x80U + (lenlen - 1));
616 /* one byte of length length */
617 (*sos)->sos_first[1] = ltag;
619 if ( ber->ber_options & LBER_USE_DER ) {
621 /* Write the length itself */
622 AC_FMEMCPY( (*sos)->sos_first + 2,
623 &netlen[sizeof(ber_len_t) - (lenlen - 1)],
626 if (lenlen != FOUR_BYTE_LEN) {
628 * We set aside FOUR_BYTE_LEN bytes for
629 * the length field. Move the data if
630 * we don't actually need that much
632 AC_FMEMCPY( (*sos)->sos_first + taglen +
633 lenlen, (*sos)->sos_first + taglen +
634 FOUR_BYTE_LEN, len );
637 /* the length itself */
638 AC_FMEMCPY( (*sos)->sos_first + taglen + 1,
639 &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN - 1)],
643 next->sos_clen += (taglen + lenlen + len);
644 next->sos_ptr += (taglen + lenlen + len);
647 /* we're done with this seqorset, so free it up */
648 LBER_FREE( (char *) (*sos) );
651 return taglen + lenlen + len;
655 ber_put_seq( BerElement *ber )
657 assert( ber != NULL );
658 assert( BER_VALID( ber ) );
660 return ber_put_seqorset( ber );
664 ber_put_set( BerElement *ber )
666 assert( ber != NULL );
667 assert( BER_VALID( ber ) );
669 return ber_put_seqorset( ber );
673 static ber_tag_t lber_int_null = 0;
677 ber_printf( BerElement *ber, LDAP_CONST char *fmt, ... )
681 struct berval *bv, **bvp;
686 assert( ber != NULL );
687 assert( fmt != NULL );
689 assert( BER_VALID( ber ) );
693 for ( rc = 0; *fmt && rc != -1; fmt++ ) {
695 case '!': { /* hook */
696 BEREncodeCallback *f;
699 f = va_arg( ap, BEREncodeCallback * );
700 p = va_arg( ap, void * );
705 case 'b': /* boolean */
706 i = va_arg( ap, ber_int_t );
707 rc = ber_put_boolean( ber, i, ber->ber_tag );
711 i = va_arg( ap, ber_int_t );
712 rc = ber_put_int( ber, i, ber->ber_tag );
715 case 'e': /* enumeration */
716 i = va_arg( ap, ber_int_t );
717 rc = ber_put_enum( ber, i, ber->ber_tag );
721 rc = ber_put_null( ber, ber->ber_tag );
724 case 'N': /* Debug NULL */
725 if( lber_int_null != 0 ) {
726 /* Insert NULL to ensure peer ignores unknown tags */
727 rc = ber_put_null( ber, lber_int_null );
733 case 'o': /* octet string (non-null terminated) */
734 s = va_arg( ap, char * );
735 len = va_arg( ap, ber_len_t );
736 rc = ber_put_ostring( ber, s, len, ber->ber_tag );
739 case 'O': /* berval octet string */
740 bv = va_arg( ap, struct berval * );
741 if( bv == NULL ) break;
742 rc = ber_put_berval( ber, bv, ber->ber_tag );
745 case 's': /* string */
746 s = va_arg( ap, char * );
747 rc = ber_put_string( ber, s, ber->ber_tag );
750 case 'B': /* bit string */
751 case 'X': /* bit string (deprecated) */
752 s = va_arg( ap, char * );
753 len = va_arg( ap, int ); /* in bits */
754 rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
757 case 't': /* tag for the next element */
758 ber->ber_tag = va_arg( ap, ber_tag_t );
759 ber->ber_usertag = 1;
762 case 'v': /* vector of strings */
763 if ( (ss = va_arg( ap, char ** )) == NULL )
765 for ( i = 0; ss[i] != NULL; i++ ) {
766 if ( (rc = ber_put_string( ber, ss[i],
767 ber->ber_tag )) == -1 )
772 case 'V': /* sequences of strings + lengths */
773 if ( (bvp = va_arg( ap, struct berval ** )) == NULL )
775 for ( i = 0; bvp[i] != NULL; i++ ) {
776 if ( (rc = ber_put_berval( ber, bvp[i],
777 ber->ber_tag )) == -1 )
782 case '{': /* begin sequence */
783 rc = ber_start_seq( ber, ber->ber_tag );
786 case '}': /* end sequence */
787 rc = ber_put_seqorset( ber );
790 case '[': /* begin set */
791 rc = ber_start_set( ber, ber->ber_tag );
794 case ']': /* end set */
795 rc = ber_put_seqorset( ber );
799 if( ber->ber_debug ) {
801 LDAP_LOG(( "liblber", LDAP_LEVEL_ERR,
802 "ber_printf: unknown fmt %c\n", *fmt ));
804 ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
805 "ber_printf: unknown fmt %c\n", *fmt );
812 if ( ber->ber_usertag == 0 )
813 ber->ber_tag = LBER_DEFAULT;
815 ber->ber_usertag = 0;