1 /* encode.c - ber output encoding routines */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
23 #include <ac/stdarg.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
29 static int ber_put_len LDAP_P(( BerElement *ber,
30 unsigned long len, int nosos ));
32 static int ber_start_seqorset LDAP_P(( BerElement *ber,
35 static int ber_put_seqorset LDAP_P(( BerElement *ber ));
37 static int ber_put_int_or_enum LDAP_P(( BerElement *ber,
38 long num, unsigned long tag ));
42 ber_calc_taglen( unsigned long tag )
47 /* find the first non-all-zero byte in the tag */
48 for ( i = sizeof(long) - 1; i > 0; i-- ) {
49 mask = (0xffL << (i * 8));
59 ber_put_tag( BerElement *ber, unsigned long tag, int nosos )
64 taglen = ber_calc_taglen( tag );
66 ntag = AC_HTONL( tag );
68 return( ber_write( ber, ((char *) &ntag) + sizeof(long) - taglen,
73 ber_calc_lenlen( unsigned long len )
76 * short len if it's less than 128 - one byte giving the len,
84 * long len otherwise - one byte with bit 8 set, giving the
85 * length of the length, followed by the length itself.
92 if ( len <= 0xFFFFFFL )
99 ber_put_len( BerElement *ber, unsigned long len, int nosos )
104 unsigned long netlen;
107 * short len if it's less than 128 - one byte giving the len,
112 netlen = AC_HTONL( len );
113 return( ber_write( ber, (char *) &netlen + sizeof(long) - 1,
118 * long len otherwise - one byte with bit 8 set, giving the
119 * length of the length, followed by the length itself.
122 /* find the first non-all-zero byte */
123 for ( i = sizeof(long) - 1; i > 0; i-- ) {
124 mask = (0xffL << (i * 8));
129 lenlen = (unsigned char) ++i;
134 /* write the length of the length */
135 if ( ber_write( ber, &lenlen, 1, nosos ) != 1 )
138 /* write the length itself */
139 netlen = AC_HTONL( len );
140 if ( ber_write( ber, (char *) &netlen + (sizeof(long) - i), i, nosos )
148 ber_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
157 * high bit is set - look for first non-all-one byte
158 * high bit is clear - look for first non-all-zero byte
160 for ( i = sizeof(long) - 1; i > 0; i-- ) {
161 mask = (0xffL << (i * 8));
165 if ( (num & mask) != mask )
175 * we now have the "leading byte". if the high bit on this
176 * byte matches the sign bit, we need to "back up" a byte.
178 mask = (num & (0x80L << (i * 8)));
179 if ( (mask && !sign) || (sign && !mask) )
184 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
187 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
190 netnum = AC_HTONL( num );
191 if ( ber_write( ber, (char *) &netnum + (sizeof(long) - i), i, 0 )
195 /* length of tag + length + contents */
196 return( taglen + lenlen + i );
200 ber_put_enum( BerElement *ber, long num, unsigned long tag )
202 if ( tag == LBER_DEFAULT )
203 tag = LBER_ENUMERATED;
205 return( ber_put_int_or_enum( ber, num, tag ) );
209 ber_put_int( BerElement *ber, long num, unsigned long tag )
211 if ( tag == LBER_DEFAULT )
214 return( ber_put_int_or_enum( ber, num, tag ) );
218 ber_put_ostring( BerElement *ber, char *str, unsigned long len,
221 int taglen, lenlen, rc;
222 #ifdef STR_TRANSLATION
224 #endif /* STR_TRANSLATION */
226 if ( tag == LBER_DEFAULT )
227 tag = LBER_OCTETSTRING;
229 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
232 #ifdef STR_TRANSLATION
233 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0 &&
234 ber->ber_encode_translate_proc != NULL ) {
235 if ( (*(ber->ber_encode_translate_proc))( &str, &len, 0 )
243 #endif /* STR_TRANSLATION */
245 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
246 (unsigned long) ber_write( ber, str, len, 0 ) != len ) {
249 /* return length of tag + length + contents */
250 rc = taglen + lenlen + len;
253 #ifdef STR_TRANSLATION
257 #endif /* STR_TRANSLATION */
263 ber_put_string( BerElement *ber, char *str, unsigned long tag )
265 return( ber_put_ostring( ber, str, strlen( str ), tag ));
269 ber_put_bitstring( BerElement *ber, char *str,
270 unsigned long blen /* in bits */, unsigned long tag )
272 int taglen, lenlen, len;
273 unsigned char unusedbits;
275 if ( tag == LBER_DEFAULT )
276 tag = LBER_BITSTRING;
278 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
281 len = ( blen + 7 ) / 8;
282 unusedbits = (unsigned char) ((len * 8) - blen);
283 if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 )
286 if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
289 if ( ber_write( ber, str, len, 0 ) != len )
292 /* return length of tag + length + unused bit count + contents */
293 return( taglen + 1 + lenlen + len );
297 ber_put_null( BerElement *ber, unsigned long tag )
301 if ( tag == LBER_DEFAULT )
304 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
307 if ( ber_put_len( ber, 0, 0 ) != 1 )
310 return( taglen + 1 );
314 ber_put_boolean( BerElement *ber, int boolval, unsigned long tag )
317 unsigned char trueval = 0xff;
318 unsigned char falseval = 0x00;
320 if ( tag == LBER_DEFAULT )
323 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
326 if ( ber_put_len( ber, 1, 0 ) != 1 )
329 if ( ber_write( ber, (char *)(boolval ? &trueval : &falseval), 1, 0 )
333 return( taglen + 2 );
336 #define FOUR_BYTE_LEN 5
339 ber_start_seqorset( BerElement *ber, unsigned long tag )
343 if ( (new = (Seqorset *) calloc( sizeof(Seqorset), 1 ))
347 if ( ber->ber_sos == NULLSEQORSET )
348 new->sos_first = ber->ber_ptr;
350 new->sos_first = ber->ber_sos->sos_ptr;
352 /* Set aside room for a 4 byte length field */
353 new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
356 new->sos_next = ber->ber_sos;
363 ber_start_seq( BerElement *ber, unsigned long tag )
365 if ( tag == LBER_DEFAULT )
368 return( ber_start_seqorset( ber, tag ) );
372 ber_start_set( BerElement *ber, unsigned long tag )
374 if ( tag == LBER_DEFAULT )
377 return( ber_start_seqorset( ber, tag ) );
381 ber_put_seqorset( BerElement *ber )
383 unsigned long len, netlen;
385 unsigned char ltag = 0x80 + FOUR_BYTE_LEN - 1;
387 Seqorset **sos = &ber->ber_sos;
390 * If this is the toplevel sequence or set, we need to actually
391 * write the stuff out. Otherwise, it's already been put in
392 * the appropriate buffer and will be written when the toplevel
393 * one is written. In this case all we need to do is update the
397 len = (*sos)->sos_clen;
398 netlen = AC_HTONL( len );
399 if ( sizeof(long) > 4 && len > 0xFFFFFFFFL )
402 if ( ber->ber_options & LBER_USE_DER ) {
403 lenlen = ber_calc_lenlen( len );
405 lenlen = FOUR_BYTE_LEN;
408 if ( (next = (*sos)->sos_next) == NULLSEQORSET ) {
410 if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
413 if ( ber->ber_options & LBER_USE_DER ) {
414 /* Write the length in the minimum # of octets */
415 if ( ber_put_len( ber, len, 1 ) == -1 )
418 if (lenlen != FOUR_BYTE_LEN) {
420 * We set aside FOUR_BYTE_LEN bytes for
421 * the length field. Move the data if
422 * we don't actually need that much
424 SAFEMEMCPY( (*sos)->sos_first + taglen +
425 lenlen, (*sos)->sos_first + taglen +
426 FOUR_BYTE_LEN, len );
429 /* Fill FOUR_BYTE_LEN bytes for length field */
430 /* one byte of length length */
431 if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 )
434 /* the length itself */
435 if ( ber_write( ber, (char *) &netlen + sizeof(long)
436 - (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1, 1 )
437 != FOUR_BYTE_LEN - 1 )
440 /* The ber_ptr is at the set/seq start - move it to the end */
441 (*sos)->sos_ber->ber_ptr += len;
446 taglen = ber_calc_taglen( (*sos)->sos_tag );
447 ntag = AC_HTONL( (*sos)->sos_tag );
448 SAFEMEMCPY( (*sos)->sos_first, (char *) &ntag +
449 sizeof(long) - taglen, taglen );
451 if ( ber->ber_options & LBER_USE_DER ) {
453 ? (unsigned char) len
454 : 0x80 + (lenlen - 1);
457 /* one byte of length length */
458 SAFEMEMCPY( (*sos)->sos_first + 1, <ag, 1 );
460 if ( ber->ber_options & LBER_USE_DER ) {
462 /* Write the length itself */
463 SAFEMEMCPY( (*sos)->sos_first + 2,
464 (char *)&netlen + sizeof(unsigned long) -
468 if (lenlen != FOUR_BYTE_LEN) {
470 * We set aside FOUR_BYTE_LEN bytes for
471 * the length field. Move the data if
472 * we don't actually need that much
474 SAFEMEMCPY( (*sos)->sos_first + taglen +
475 lenlen, (*sos)->sos_first + taglen +
476 FOUR_BYTE_LEN, len );
479 /* the length itself */
480 SAFEMEMCPY( (*sos)->sos_first + taglen + 1,
481 (char *) &netlen + sizeof(long) -
482 (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1 );
485 next->sos_clen += (taglen + lenlen + len);
486 next->sos_ptr += (taglen + lenlen + len);
489 /* we're done with this seqorset, so free it up */
490 free( (char *) (*sos) );
493 return( taglen + lenlen + len );
497 ber_put_seq( BerElement *ber )
499 return( ber_put_seqorset( ber ) );
503 ber_put_set( BerElement *ber )
505 return( ber_put_seqorset( ber ) );
512 ( BerElement *ber, char *fmt, ... )
532 ber = va_arg( ap, BerElement * );
533 fmt = va_arg( ap, char * );
536 for ( rc = 0; *fmt && rc != -1; fmt++ ) {
538 case 'b': /* boolean */
539 i = va_arg( ap, int );
540 rc = ber_put_boolean( ber, i, ber->ber_tag );
544 i = va_arg( ap, int );
545 rc = ber_put_int( ber, i, ber->ber_tag );
548 case 'e': /* enumeration */
549 i = va_arg( ap, int );
550 rc = ber_put_enum( ber, i, ber->ber_tag );
554 rc = ber_put_null( ber, ber->ber_tag );
557 case 'o': /* octet string (non-null terminated) */
558 s = va_arg( ap, char * );
559 len = va_arg( ap, int );
560 rc = ber_put_ostring( ber, s, len, ber->ber_tag );
563 case 's': /* string */
564 s = va_arg( ap, char * );
565 rc = ber_put_string( ber, s, ber->ber_tag );
568 case 'B': /* bit string */
569 s = va_arg( ap, char * );
570 len = va_arg( ap, int ); /* in bits */
571 rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
574 case 't': /* tag for the next element */
575 ber->ber_tag = va_arg( ap, unsigned long );
576 ber->ber_usertag = 1;
579 case 'v': /* vector of strings */
580 if ( (ss = va_arg( ap, char ** )) == NULL )
582 for ( i = 0; ss[i] != NULL; i++ ) {
583 if ( (rc = ber_put_string( ber, ss[i],
584 ber->ber_tag )) == -1 )
589 case 'V': /* sequences of strings + lengths */
590 if ( (bv = va_arg( ap, struct berval ** )) == NULL )
592 for ( i = 0; bv[i] != NULL; i++ ) {
593 if ( (rc = ber_put_ostring( ber, bv[i]->bv_val,
594 bv[i]->bv_len, ber->ber_tag )) == -1 )
599 case '{': /* begin sequence */
600 rc = ber_start_seq( ber, ber->ber_tag );
603 case '}': /* end sequence */
604 rc = ber_put_seqorset( ber );
607 case '[': /* begin set */
608 rc = ber_start_set( ber, ber->ber_tag );
611 case ']': /* end set */
612 rc = ber_put_seqorset( ber );
616 if( ber->ber_debug ) {
617 lber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
618 "ber_printf: unknown fmt %c\n", *fmt );
624 if ( ber->ber_usertag == 0 )
625 ber->ber_tag = LBER_DEFAULT;
627 ber->ber_usertag = 0;