1 /* encode.c - ber output encoding routines */
3 * Copyright (c) 1990 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
25 #include <ac/socket.h>
26 #include <ac/string.h>
30 static int ber_put_len LDAP_P(( BerElement *ber,
31 unsigned long len, int nosos ));
33 static int ber_start_seqorset LDAP_P(( BerElement *ber,
36 static int ber_put_seqorset LDAP_P(( BerElement *ber ));
38 static int ber_put_int_or_enum LDAP_P(( BerElement *ber,
39 long num, unsigned long tag ));
43 ber_calc_taglen( unsigned long tag )
48 /* find the first non-all-zero byte in the tag */
49 for ( i = sizeof(long) - 1; i > 0; i-- ) {
50 mask = (0xffL << (i * 8));
60 ber_put_tag( BerElement *ber, unsigned long tag, int nosos )
65 taglen = ber_calc_taglen( tag );
67 ntag = AC_HTONL( tag );
69 return( ber_write( ber, ((char *) &ntag) + sizeof(long) - taglen,
74 ber_calc_lenlen( unsigned long len )
77 * short len if it's less than 128 - one byte giving the len,
85 * long len otherwise - one byte with bit 8 set, giving the
86 * length of the length, followed by the length itself.
93 if ( len <= 0xFFFFFFL )
100 ber_put_len( BerElement *ber, unsigned long len, int nosos )
105 unsigned long netlen;
108 * short len if it's less than 128 - one byte giving the len,
113 netlen = AC_HTONL( len );
114 return( ber_write( ber, (char *) &netlen + sizeof(long) - 1,
119 * long len otherwise - one byte with bit 8 set, giving the
120 * length of the length, followed by the length itself.
123 /* find the first non-all-zero byte */
124 for ( i = sizeof(long) - 1; i > 0; i-- ) {
125 mask = (0xffL << (i * 8));
130 lenlen = (unsigned char) ++i;
135 /* write the length of the length */
136 if ( ber_write( ber, &lenlen, 1, nosos ) != 1 )
139 /* write the length itself */
140 netlen = AC_HTONL( len );
141 if ( ber_write( ber, (char *) &netlen + (sizeof(long) - i), i, nosos )
149 ber_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
158 * high bit is set - look for first non-all-one byte
159 * high bit is clear - look for first non-all-zero byte
161 for ( i = sizeof(long) - 1; i > 0; i-- ) {
162 mask = (0xffL << (i * 8));
166 if ( (num & mask) != mask )
176 * we now have the "leading byte". if the high bit on this
177 * byte matches the sign bit, we need to "back up" a byte.
179 mask = (num & (0x80L << (i * 8)));
180 if ( (mask && !sign) || (sign && !mask) )
185 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
188 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
191 netnum = AC_HTONL( num );
192 if ( ber_write( ber, (char *) &netnum + (sizeof(long) - i), i, 0 )
196 /* length of tag + length + contents */
197 return( taglen + lenlen + i );
201 ber_put_enum( BerElement *ber, long num, unsigned long tag )
203 if ( tag == LBER_DEFAULT )
204 tag = LBER_ENUMERATED;
206 return( ber_put_int_or_enum( ber, num, tag ) );
210 ber_put_int( BerElement *ber, long num, unsigned long tag )
212 if ( tag == LBER_DEFAULT )
215 return( ber_put_int_or_enum( ber, num, tag ) );
219 ber_put_ostring( BerElement *ber, char *str, unsigned long len,
222 int taglen, lenlen, rc;
223 #ifdef STR_TRANSLATION
225 #endif /* STR_TRANSLATION */
227 if ( tag == LBER_DEFAULT )
228 tag = LBER_OCTETSTRING;
230 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
233 #ifdef STR_TRANSLATION
234 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0 &&
235 ber->ber_encode_translate_proc != NULL ) {
236 if ( (*(ber->ber_encode_translate_proc))( &str, &len, 0 )
244 #endif /* STR_TRANSLATION */
246 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
247 (unsigned long) ber_write( ber, str, len, 0 ) != len ) {
250 /* return length of tag + length + contents */
251 rc = taglen + lenlen + len;
254 #ifdef STR_TRANSLATION
258 #endif /* STR_TRANSLATION */
264 ber_put_string( BerElement *ber, char *str, unsigned long tag )
266 return( ber_put_ostring( ber, str, strlen( str ), tag ));
270 ber_put_bitstring( BerElement *ber, char *str,
271 unsigned long blen /* in bits */, unsigned long tag )
273 int taglen, lenlen, len;
274 unsigned char unusedbits;
276 if ( tag == LBER_DEFAULT )
277 tag = LBER_BITSTRING;
279 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
282 len = ( blen + 7 ) / 8;
283 unusedbits = (unsigned char) ((len * 8) - blen);
284 if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 )
287 if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
290 if ( ber_write( ber, str, len, 0 ) != len )
293 /* return length of tag + length + unused bit count + contents */
294 return( taglen + 1 + lenlen + len );
298 ber_put_null( BerElement *ber, unsigned long tag )
302 if ( tag == LBER_DEFAULT )
305 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
308 if ( ber_put_len( ber, 0, 0 ) != 1 )
311 return( taglen + 1 );
315 ber_put_boolean( BerElement *ber, int boolval, unsigned long tag )
318 unsigned char trueval = 0xff;
319 unsigned char falseval = 0x00;
321 if ( tag == LBER_DEFAULT )
324 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
327 if ( ber_put_len( ber, 1, 0 ) != 1 )
330 if ( ber_write( ber, (char *)(boolval ? &trueval : &falseval), 1, 0 )
334 return( taglen + 2 );
337 #define FOUR_BYTE_LEN 5
340 ber_start_seqorset( BerElement *ber, unsigned long tag )
344 if ( (new = (Seqorset *) calloc( sizeof(Seqorset), 1 ))
348 if ( ber->ber_sos == NULLSEQORSET )
349 new->sos_first = ber->ber_ptr;
351 new->sos_first = ber->ber_sos->sos_ptr;
353 /* Set aside room for a 4 byte length field */
354 new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
357 new->sos_next = ber->ber_sos;
364 ber_start_seq( BerElement *ber, unsigned long tag )
366 if ( tag == LBER_DEFAULT )
369 return( ber_start_seqorset( ber, tag ) );
373 ber_start_set( BerElement *ber, unsigned long tag )
375 if ( tag == LBER_DEFAULT )
378 return( ber_start_seqorset( ber, tag ) );
382 ber_put_seqorset( BerElement *ber )
384 unsigned long len, netlen;
386 unsigned char ltag = 0x80 + FOUR_BYTE_LEN - 1;
388 Seqorset **sos = &ber->ber_sos;
391 * If this is the toplevel sequence or set, we need to actually
392 * write the stuff out. Otherwise, it's already been put in
393 * the appropriate buffer and will be written when the toplevel
394 * one is written. In this case all we need to do is update the
398 len = (*sos)->sos_clen;
399 netlen = AC_HTONL( len );
400 if ( sizeof(long) > 4 && len > 0xFFFFFFFFL )
403 if ( ber->ber_options & LBER_USE_DER ) {
404 lenlen = ber_calc_lenlen( len );
406 lenlen = FOUR_BYTE_LEN;
409 if ( (next = (*sos)->sos_next) == NULLSEQORSET ) {
411 if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
414 if ( ber->ber_options & LBER_USE_DER ) {
415 /* Write the length in the minimum # of octets */
416 if ( ber_put_len( ber, len, 1 ) == -1 )
419 if (lenlen != FOUR_BYTE_LEN) {
421 * We set aside FOUR_BYTE_LEN bytes for
422 * the length field. Move the data if
423 * we don't actually need that much
425 SAFEMEMCPY( (*sos)->sos_first + taglen +
426 lenlen, (*sos)->sos_first + taglen +
427 FOUR_BYTE_LEN, len );
430 /* Fill FOUR_BYTE_LEN bytes for length field */
431 /* one byte of length length */
432 if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 )
435 /* the length itself */
436 if ( ber_write( ber, (char *) &netlen + sizeof(long)
437 - (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1, 1 )
438 != FOUR_BYTE_LEN - 1 )
441 /* The ber_ptr is at the set/seq start - move it to the end */
442 (*sos)->sos_ber->ber_ptr += len;
447 taglen = ber_calc_taglen( (*sos)->sos_tag );
448 ntag = AC_HTONL( (*sos)->sos_tag );
449 SAFEMEMCPY( (*sos)->sos_first, (char *) &ntag +
450 sizeof(long) - taglen, taglen );
452 if ( ber->ber_options & LBER_USE_DER ) {
454 ? (unsigned char) len
455 : 0x80 + (lenlen - 1);
458 /* one byte of length length */
459 SAFEMEMCPY( (*sos)->sos_first + 1, <ag, 1 );
461 if ( ber->ber_options & LBER_USE_DER ) {
463 /* Write the length itself */
464 SAFEMEMCPY( (*sos)->sos_first + 2,
465 (char *)&netlen + sizeof(unsigned long) -
469 if (lenlen != FOUR_BYTE_LEN) {
471 * We set aside FOUR_BYTE_LEN bytes for
472 * the length field. Move the data if
473 * we don't actually need that much
475 SAFEMEMCPY( (*sos)->sos_first + taglen +
476 lenlen, (*sos)->sos_first + taglen +
477 FOUR_BYTE_LEN, len );
480 /* the length itself */
481 SAFEMEMCPY( (*sos)->sos_first + taglen + 1,
482 (char *) &netlen + sizeof(long) -
483 (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1 );
486 next->sos_clen += (taglen + lenlen + len);
487 next->sos_ptr += (taglen + lenlen + len);
490 /* we're done with this seqorset, so free it up */
491 free( (char *) (*sos) );
494 return( taglen + lenlen + len );
498 ber_put_seq( BerElement *ber )
500 return( ber_put_seqorset( ber ) );
504 ber_put_set( BerElement *ber )
506 return( ber_put_seqorset( ber ) );
513 ( BerElement *ber, char *fmt, ... )
533 ber = va_arg( ap, BerElement * );
534 fmt = va_arg( ap, char * );
537 for ( rc = 0; *fmt && rc != -1; fmt++ ) {
539 case 'b': /* boolean */
540 i = va_arg( ap, int );
541 rc = ber_put_boolean( ber, i, ber->ber_tag );
545 i = va_arg( ap, int );
546 rc = ber_put_int( ber, i, ber->ber_tag );
549 case 'e': /* enumeration */
550 i = va_arg( ap, int );
551 rc = ber_put_enum( ber, i, ber->ber_tag );
555 rc = ber_put_null( ber, ber->ber_tag );
558 case 'o': /* octet string (non-null terminated) */
559 s = va_arg( ap, char * );
560 len = va_arg( ap, int );
561 rc = ber_put_ostring( ber, s, len, ber->ber_tag );
564 case 's': /* string */
565 s = va_arg( ap, char * );
566 rc = ber_put_string( ber, s, ber->ber_tag );
569 case 'B': /* bit string */
570 s = va_arg( ap, char * );
571 len = va_arg( ap, int ); /* in bits */
572 rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
575 case 't': /* tag for the next element */
576 ber->ber_tag = va_arg( ap, unsigned long );
577 ber->ber_usertag = 1;
580 case 'v': /* vector of strings */
581 if ( (ss = va_arg( ap, char ** )) == NULL )
583 for ( i = 0; ss[i] != NULL; i++ ) {
584 if ( (rc = ber_put_string( ber, ss[i],
585 ber->ber_tag )) == -1 )
590 case 'V': /* sequences of strings + lengths */
591 if ( (bv = va_arg( ap, struct berval ** )) == NULL )
593 for ( i = 0; bv[i] != NULL; i++ ) {
594 if ( (rc = ber_put_ostring( ber, bv[i]->bv_val,
595 bv[i]->bv_len, ber->ber_tag )) == -1 )
600 case '{': /* begin sequence */
601 rc = ber_start_seq( ber, ber->ber_tag );
604 case '}': /* end sequence */
605 rc = ber_put_seqorset( ber );
608 case '[': /* begin set */
609 rc = ber_start_set( ber, ber->ber_tag );
612 case ']': /* end set */
613 rc = ber_put_seqorset( ber );
618 fprintf( stderr, "unknown fmt %c\n", *fmt );
619 #endif /* LDAP_LIBUI */
624 if ( ber->ber_usertag == 0 )
625 ber->ber_tag = LBER_DEFAULT;
627 ber->ber_usertag = 0;