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.
19 #include <ac/stdarg.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
25 static int ber_put_len LDAP_P(( BerElement *ber,
26 unsigned long len, int nosos ));
28 static int ber_start_seqorset LDAP_P(( BerElement *ber,
31 static int ber_put_seqorset LDAP_P(( BerElement *ber ));
33 static int ber_put_int_or_enum LDAP_P(( BerElement *ber,
34 long num, unsigned long tag ));
38 ber_calc_taglen( unsigned long tag )
43 /* find the first non-all-zero byte in the tag */
44 for ( i = sizeof(long) - 1; i > 0; i-- ) {
45 mask = (0xffL << (i * 8));
55 ber_put_tag( BerElement *ber, unsigned long tag, int nosos )
60 taglen = ber_calc_taglen( tag );
62 ntag = AC_HTONL( tag );
64 return( ber_write( ber, ((char *) &ntag) + sizeof(long) - taglen,
69 ber_calc_lenlen( unsigned long len )
72 * short len if it's less than 128 - one byte giving the len,
80 * long len otherwise - one byte with bit 8 set, giving the
81 * length of the length, followed by the length itself.
88 if ( len <= 0xFFFFFFL )
95 ber_put_len( BerElement *ber, unsigned long len, int nosos )
100 unsigned long netlen;
103 * short len if it's less than 128 - one byte giving the len,
108 netlen = AC_HTONL( len );
109 return( ber_write( ber, (char *) &netlen + sizeof(long) - 1,
114 * long len otherwise - one byte with bit 8 set, giving the
115 * length of the length, followed by the length itself.
118 /* find the first non-all-zero byte */
119 for ( i = sizeof(long) - 1; i > 0; i-- ) {
120 mask = (0xffL << (i * 8));
125 lenlen = (unsigned char) ++i;
130 /* write the length of the length */
131 if ( ber_write( ber, &lenlen, 1, nosos ) != 1 )
134 /* write the length itself */
135 netlen = AC_HTONL( len );
136 if ( ber_write( ber, (char *) &netlen + (sizeof(long) - i), i, nosos )
144 ber_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
153 * high bit is set - look for first non-all-one byte
154 * high bit is clear - look for first non-all-zero byte
156 for ( i = sizeof(long) - 1; i > 0; i-- ) {
157 mask = (0xffL << (i * 8));
161 if ( (num & mask) != mask )
171 * we now have the "leading byte". if the high bit on this
172 * byte matches the sign bit, we need to "back up" a byte.
174 mask = (num & (0x80L << (i * 8)));
175 if ( (mask && !sign) || (sign && !mask) )
180 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
183 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
186 netnum = AC_HTONL( num );
187 if ( ber_write( ber, (char *) &netnum + (sizeof(long) - i), i, 0 )
191 /* length of tag + length + contents */
192 return( taglen + lenlen + i );
196 ber_put_enum( BerElement *ber, long num, unsigned long tag )
198 if ( tag == LBER_DEFAULT )
199 tag = LBER_ENUMERATED;
201 return( ber_put_int_or_enum( ber, num, tag ) );
205 ber_put_int( BerElement *ber, long num, unsigned long tag )
207 if ( tag == LBER_DEFAULT )
210 return( ber_put_int_or_enum( ber, num, tag ) );
214 ber_put_ostring( BerElement *ber, char *str, unsigned long len,
217 int taglen, lenlen, rc;
218 #ifdef STR_TRANSLATION
220 #endif /* STR_TRANSLATION */
222 if ( tag == LBER_DEFAULT )
223 tag = LBER_OCTETSTRING;
225 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
228 #ifdef STR_TRANSLATION
229 if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0 &&
230 ber->ber_encode_translate_proc != NULL ) {
231 if ( (*(ber->ber_encode_translate_proc))( &str, &len, 0 )
239 #endif /* STR_TRANSLATION */
241 if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
242 (unsigned long) ber_write( ber, str, len, 0 ) != len ) {
245 /* return length of tag + length + contents */
246 rc = taglen + lenlen + len;
249 #ifdef STR_TRANSLATION
253 #endif /* STR_TRANSLATION */
259 ber_put_string( BerElement *ber, char *str, unsigned long tag )
261 return( ber_put_ostring( ber, str, strlen( str ), tag ));
265 ber_put_bitstring( BerElement *ber, char *str,
266 unsigned long blen /* in bits */, unsigned long tag )
268 int taglen, lenlen, len;
269 unsigned char unusedbits;
271 if ( tag == LBER_DEFAULT )
272 tag = LBER_BITSTRING;
274 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
277 len = ( blen + 7 ) / 8;
278 unusedbits = (unsigned char) ((len * 8) - blen);
279 if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 )
282 if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
285 if ( ber_write( ber, str, len, 0 ) != len )
288 /* return length of tag + length + unused bit count + contents */
289 return( taglen + 1 + lenlen + len );
293 ber_put_null( BerElement *ber, unsigned long tag )
297 if ( tag == LBER_DEFAULT )
300 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
303 if ( ber_put_len( ber, 0, 0 ) != 1 )
306 return( taglen + 1 );
310 ber_put_boolean( BerElement *ber, int boolval, unsigned long tag )
313 unsigned char trueval = 0xff;
314 unsigned char falseval = 0x00;
316 if ( tag == LBER_DEFAULT )
319 if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
322 if ( ber_put_len( ber, 1, 0 ) != 1 )
325 if ( ber_write( ber, (char *)(boolval ? &trueval : &falseval), 1, 0 )
329 return( taglen + 2 );
332 #define FOUR_BYTE_LEN 5
335 ber_start_seqorset( BerElement *ber, unsigned long tag )
339 if ( (new = (Seqorset *) calloc( sizeof(Seqorset), 1 ))
343 if ( ber->ber_sos == NULLSEQORSET )
344 new->sos_first = ber->ber_ptr;
346 new->sos_first = ber->ber_sos->sos_ptr;
348 /* Set aside room for a 4 byte length field */
349 new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
352 new->sos_next = ber->ber_sos;
359 ber_start_seq( BerElement *ber, unsigned long tag )
361 if ( tag == LBER_DEFAULT )
364 return( ber_start_seqorset( ber, tag ) );
368 ber_start_set( BerElement *ber, unsigned long tag )
370 if ( tag == LBER_DEFAULT )
373 return( ber_start_seqorset( ber, tag ) );
377 ber_put_seqorset( BerElement *ber )
379 unsigned long len, netlen;
381 unsigned char ltag = 0x80 + FOUR_BYTE_LEN - 1;
383 Seqorset **sos = &ber->ber_sos;
386 * If this is the toplevel sequence or set, we need to actually
387 * write the stuff out. Otherwise, it's already been put in
388 * the appropriate buffer and will be written when the toplevel
389 * one is written. In this case all we need to do is update the
393 len = (*sos)->sos_clen;
394 netlen = AC_HTONL( len );
395 if ( sizeof(long) > 4 && len > 0xFFFFFFFFL )
398 if ( ber->ber_options & LBER_USE_DER ) {
399 lenlen = ber_calc_lenlen( len );
401 lenlen = FOUR_BYTE_LEN;
404 if ( (next = (*sos)->sos_next) == NULLSEQORSET ) {
406 if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
409 if ( ber->ber_options & LBER_USE_DER ) {
410 /* Write the length in the minimum # of octets */
411 if ( ber_put_len( ber, len, 1 ) == -1 )
414 if (lenlen != FOUR_BYTE_LEN) {
416 * We set aside FOUR_BYTE_LEN bytes for
417 * the length field. Move the data if
418 * we don't actually need that much
420 SAFEMEMCPY( (*sos)->sos_first + taglen +
421 lenlen, (*sos)->sos_first + taglen +
422 FOUR_BYTE_LEN, len );
425 /* Fill FOUR_BYTE_LEN bytes for length field */
426 /* one byte of length length */
427 if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 )
430 /* the length itself */
431 if ( ber_write( ber, (char *) &netlen + sizeof(long)
432 - (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1, 1 )
433 != FOUR_BYTE_LEN - 1 )
436 /* The ber_ptr is at the set/seq start - move it to the end */
437 (*sos)->sos_ber->ber_ptr += len;
442 taglen = ber_calc_taglen( (*sos)->sos_tag );
443 ntag = AC_HTONL( (*sos)->sos_tag );
444 SAFEMEMCPY( (*sos)->sos_first, (char *) &ntag +
445 sizeof(long) - taglen, taglen );
447 if ( ber->ber_options & LBER_USE_DER ) {
449 ? (unsigned char) len
450 : 0x80 + (lenlen - 1);
453 /* one byte of length length */
454 SAFEMEMCPY( (*sos)->sos_first + 1, <ag, 1 );
456 if ( ber->ber_options & LBER_USE_DER ) {
458 /* Write the length itself */
459 SAFEMEMCPY( (*sos)->sos_first + 2,
460 (char *)&netlen + sizeof(unsigned long) -
464 if (lenlen != FOUR_BYTE_LEN) {
466 * We set aside FOUR_BYTE_LEN bytes for
467 * the length field. Move the data if
468 * we don't actually need that much
470 SAFEMEMCPY( (*sos)->sos_first + taglen +
471 lenlen, (*sos)->sos_first + taglen +
472 FOUR_BYTE_LEN, len );
475 /* the length itself */
476 SAFEMEMCPY( (*sos)->sos_first + taglen + 1,
477 (char *) &netlen + sizeof(long) -
478 (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1 );
481 next->sos_clen += (taglen + lenlen + len);
482 next->sos_ptr += (taglen + lenlen + len);
485 /* we're done with this seqorset, so free it up */
486 free( (char *) (*sos) );
489 return( taglen + lenlen + len );
493 ber_put_seq( BerElement *ber )
495 return( ber_put_seqorset( ber ) );
499 ber_put_set( BerElement *ber )
501 return( ber_put_seqorset( ber ) );
508 ( BerElement *ber, char *fmt, ... )
528 ber = va_arg( ap, BerElement * );
529 fmt = va_arg( ap, char * );
532 for ( rc = 0; *fmt && rc != -1; fmt++ ) {
534 case 'b': /* boolean */
535 i = va_arg( ap, int );
536 rc = ber_put_boolean( ber, i, ber->ber_tag );
540 i = va_arg( ap, int );
541 rc = ber_put_int( ber, i, ber->ber_tag );
544 case 'e': /* enumeration */
545 i = va_arg( ap, int );
546 rc = ber_put_enum( ber, i, ber->ber_tag );
550 rc = ber_put_null( ber, ber->ber_tag );
553 case 'o': /* octet string (non-null terminated) */
554 s = va_arg( ap, char * );
555 len = va_arg( ap, int );
556 rc = ber_put_ostring( ber, s, len, ber->ber_tag );
559 case 's': /* string */
560 s = va_arg( ap, char * );
561 rc = ber_put_string( ber, s, ber->ber_tag );
564 case 'B': /* bit string */
565 s = va_arg( ap, char * );
566 len = va_arg( ap, int ); /* in bits */
567 rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
570 case 't': /* tag for the next element */
571 ber->ber_tag = va_arg( ap, unsigned long );
572 ber->ber_usertag = 1;
575 case 'v': /* vector of strings */
576 if ( (ss = va_arg( ap, char ** )) == NULL )
578 for ( i = 0; ss[i] != NULL; i++ ) {
579 if ( (rc = ber_put_string( ber, ss[i],
580 ber->ber_tag )) == -1 )
585 case 'V': /* sequences of strings + lengths */
586 if ( (bv = va_arg( ap, struct berval ** )) == NULL )
588 for ( i = 0; bv[i] != NULL; i++ ) {
589 if ( (rc = ber_put_ostring( ber, bv[i]->bv_val,
590 bv[i]->bv_len, ber->ber_tag )) == -1 )
595 case '{': /* begin sequence */
596 rc = ber_start_seq( ber, ber->ber_tag );
599 case '}': /* end sequence */
600 rc = ber_put_seqorset( ber );
603 case '[': /* begin set */
604 rc = ber_start_set( ber, ber->ber_tag );
607 case ']': /* end set */
608 rc = ber_put_seqorset( ber );
613 fprintf( stderr, "unknown fmt %c\n", *fmt );
614 #endif /* LDAP_LIBUI */
619 if ( ber->ber_usertag == 0 )
620 ber->ber_tag = LBER_DEFAULT;
622 ber->ber_usertag = 0;