]> git.sur5r.net Git - openldap/blob - libraries/liblber/encode.c
Add simple copyright notice
[openldap] / libraries / liblber / encode.c
1 /* encode.c - ber output encoding routines */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1990 Regents of the University of Michigan.
8  * All rights reserved.
9  *
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.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include <ac/stdarg.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
26
27 #include "lber-int.h"
28
29 static int ber_put_len LDAP_P(( BerElement *ber,
30         unsigned long len, int nosos ));
31
32 static int ber_start_seqorset LDAP_P(( BerElement *ber,
33         unsigned long tag ));
34
35 static int ber_put_seqorset LDAP_P(( BerElement *ber ));
36
37 static int ber_put_int_or_enum LDAP_P(( BerElement *ber,
38         long num, unsigned long tag ));
39
40
41 static int
42 ber_calc_taglen( unsigned long tag )
43 {
44         int     i;
45         long    mask;
46
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));
50                 /* not all zero */
51                 if ( tag & mask )
52                         break;
53         }
54
55         return( i + 1 );
56 }
57
58 static int
59 ber_put_tag( BerElement *ber, unsigned long tag, int nosos )
60 {
61         int             taglen;
62         unsigned long   ntag;
63
64         taglen = ber_calc_taglen( tag );
65
66         ntag = AC_HTONL( tag );
67
68         return( ber_write( ber, ((char *) &ntag) + sizeof(long) - taglen,
69             taglen, nosos ) );
70 }
71
72 static int
73 ber_calc_lenlen( unsigned long len )
74 {
75         /*
76          * short len if it's less than 128 - one byte giving the len,
77          * with bit 8 0.
78          */
79
80         if ( len <= 0x7F )
81                 return( 1 );
82
83         /*
84          * long len otherwise - one byte with bit 8 set, giving the
85          * length of the length, followed by the length itself.
86          */
87
88         if ( len <= 0xFF )
89                 return( 2 );
90         if ( len <= 0xFFFFL )
91                 return( 3 );
92         if ( len <= 0xFFFFFFL )
93                 return( 4 );
94
95         return( 5 );
96 }
97
98 static int
99 ber_put_len( BerElement *ber, unsigned long len, int nosos )
100 {
101         int             i;
102         char            lenlen;
103         long            mask;
104         unsigned long   netlen;
105
106         /*
107          * short len if it's less than 128 - one byte giving the len,
108          * with bit 8 0.
109          */
110
111         if ( len <= 127 ) {
112                 netlen = AC_HTONL( len );
113                 return( ber_write( ber, (char *) &netlen + sizeof(long) - 1,
114                     1, nosos ) );
115         }
116
117         /*
118          * long len otherwise - one byte with bit 8 set, giving the
119          * length of the length, followed by the length itself.
120          */
121
122         /* find the first non-all-zero byte */
123         for ( i = sizeof(long) - 1; i > 0; i-- ) {
124                 mask = (0xffL << (i * 8));
125                 /* not all zero */
126                 if ( len & mask )
127                         break;
128         }
129         lenlen = (unsigned char) ++i;
130         if ( lenlen > 4 )
131                 return( -1 );
132         lenlen |= 0x80;
133
134         /* write the length of the length */
135         if ( ber_write( ber, &lenlen, 1, nosos ) != 1 )
136                 return( -1 );
137
138         /* write the length itself */
139         netlen = AC_HTONL( len );
140         if ( ber_write( ber, (char *) &netlen + (sizeof(long) - i), i, nosos )
141             != i )
142                 return( -1 );
143
144         return( i + 1 );
145 }
146
147 static int
148 ber_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
149 {
150         int     i, sign, taglen;
151         int     len, lenlen;
152         long    netnum, mask;
153
154         sign = (num < 0);
155
156         /*
157          * high bit is set - look for first non-all-one byte
158          * high bit is clear - look for first non-all-zero byte
159          */
160         for ( i = sizeof(long) - 1; i > 0; i-- ) {
161                 mask = (0xffL << (i * 8));
162
163                 if ( sign ) {
164                         /* not all ones */
165                         if ( (num & mask) != mask )
166                                 break;
167                 } else {
168                         /* not all zero */
169                         if ( num & mask )
170                                 break;
171                 }
172         }
173
174         /*
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.
177          */
178         mask = (num & (0x80L << (i * 8)));
179         if ( (mask && !sign) || (sign && !mask) )
180                 i++;
181
182         len = i + 1;
183
184         if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
185                 return( -1 );
186
187         if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
188                 return( -1 );
189         i++;
190         netnum = AC_HTONL( num );
191         if ( ber_write( ber, (char *) &netnum + (sizeof(long) - i), i, 0 )
192            != i )
193                 return( -1 );
194
195         /* length of tag + length + contents */
196         return( taglen + lenlen + i );
197 }
198
199 int
200 ber_put_enum( BerElement *ber, long num, unsigned long tag )
201 {
202         if ( tag == LBER_DEFAULT )
203                 tag = LBER_ENUMERATED;
204
205         return( ber_put_int_or_enum( ber, num, tag ) );
206 }
207
208 int
209 ber_put_int( BerElement *ber, long num, unsigned long tag )
210 {
211         if ( tag == LBER_DEFAULT )
212                 tag = LBER_INTEGER;
213
214         return( ber_put_int_or_enum( ber, num, tag ) );
215 }
216
217 int
218 ber_put_ostring( BerElement *ber, char *str, unsigned long len,
219         unsigned long tag )
220 {
221         int     taglen, lenlen, rc;
222 #ifdef STR_TRANSLATION
223         int     free_str;
224 #endif /* STR_TRANSLATION */
225
226         if ( tag == LBER_DEFAULT )
227                 tag = LBER_OCTETSTRING;
228
229         if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
230                 return( -1 );
231
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 )
236                     != 0 ) {
237                         return( -1 );
238                 }
239                 free_str = 1;
240         } else {
241                 free_str = 0;
242         }
243 #endif /* STR_TRANSLATION */
244
245         if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
246                 (unsigned long) ber_write( ber, str, len, 0 ) != len ) {
247                 rc = -1;
248         } else {
249                 /* return length of tag + length + contents */
250                 rc = taglen + lenlen + len;
251         }
252
253 #ifdef STR_TRANSLATION
254         if ( free_str ) {
255                 free( str );
256         }
257 #endif /* STR_TRANSLATION */
258
259         return( rc );
260 }
261
262 int
263 ber_put_string( BerElement *ber, char *str, unsigned long tag )
264 {
265         return( ber_put_ostring( ber, str, strlen( str ), tag ));
266 }
267
268 int
269 ber_put_bitstring( BerElement *ber, char *str,
270         unsigned long blen /* in bits */, unsigned long tag )
271 {
272         int             taglen, lenlen, len;
273         unsigned char   unusedbits;
274
275         if ( tag == LBER_DEFAULT )
276                 tag = LBER_BITSTRING;
277
278         if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
279                 return( -1 );
280
281         len = ( blen + 7 ) / 8;
282         unusedbits = (unsigned char) ((len * 8) - blen);
283         if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 )
284                 return( -1 );
285
286         if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
287                 return( -1 );
288
289         if ( ber_write( ber, str, len, 0 ) != len )
290                 return( -1 );
291
292         /* return length of tag + length + unused bit count + contents */
293         return( taglen + 1 + lenlen + len );
294 }
295
296 int
297 ber_put_null( BerElement *ber, unsigned long tag )
298 {
299         int     taglen;
300
301         if ( tag == LBER_DEFAULT )
302                 tag = LBER_NULL;
303
304         if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
305                 return( -1 );
306
307         if ( ber_put_len( ber, 0, 0 ) != 1 )
308                 return( -1 );
309
310         return( taglen + 1 );
311 }
312
313 int
314 ber_put_boolean( BerElement *ber, int boolval, unsigned long tag )
315 {
316         int             taglen;
317         unsigned char   trueval = 0xff;
318         unsigned char   falseval = 0x00;
319
320         if ( tag == LBER_DEFAULT )
321                 tag = LBER_BOOLEAN;
322
323         if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
324                 return( -1 );
325
326         if ( ber_put_len( ber, 1, 0 ) != 1 )
327                 return( -1 );
328
329         if ( ber_write( ber, (char *)(boolval ? &trueval : &falseval), 1, 0 )
330             != 1 )
331                 return( -1 );
332
333         return( taglen + 2 );
334 }
335
336 #define FOUR_BYTE_LEN   5
337
338 static int
339 ber_start_seqorset( BerElement *ber, unsigned long tag )
340 {
341         Seqorset        *new;
342
343         if ( (new = (Seqorset *) calloc( sizeof(Seqorset), 1 ))
344             == NULLSEQORSET )
345                 return( -1 );
346         new->sos_ber = ber;
347         if ( ber->ber_sos == NULLSEQORSET )
348                 new->sos_first = ber->ber_ptr;
349         else
350                 new->sos_first = ber->ber_sos->sos_ptr;
351
352         /* Set aside room for a 4 byte length field */
353         new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
354         new->sos_tag = tag;
355
356         new->sos_next = ber->ber_sos;
357         ber->ber_sos = new;
358
359         return( 0 );
360 }
361
362 int
363 ber_start_seq( BerElement *ber, unsigned long tag )
364 {
365         if ( tag == LBER_DEFAULT )
366                 tag = LBER_SEQUENCE;
367
368         return( ber_start_seqorset( ber, tag ) );
369 }
370
371 int
372 ber_start_set( BerElement *ber, unsigned long tag )
373 {
374         if ( tag == LBER_DEFAULT )
375                 tag = LBER_SET;
376
377         return( ber_start_seqorset( ber, tag ) );
378 }
379
380 static int
381 ber_put_seqorset( BerElement *ber )
382 {
383         unsigned long   len, netlen;
384         int             taglen, lenlen;
385         unsigned char   ltag = 0x80 + FOUR_BYTE_LEN - 1;
386         Seqorset        *next;
387         Seqorset        **sos = &ber->ber_sos;
388
389         /*
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
394          * length and tag.
395          */
396
397         len = (*sos)->sos_clen;
398         netlen = AC_HTONL( len );
399         if ( sizeof(long) > 4 && len > 0xFFFFFFFFL )
400                 return( -1 );
401
402         if ( ber->ber_options & LBER_USE_DER ) {
403                 lenlen = ber_calc_lenlen( len );
404         } else {
405                 lenlen = FOUR_BYTE_LEN;
406         }
407
408         if ( (next = (*sos)->sos_next) == NULLSEQORSET ) {
409                 /* write the tag */
410                 if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
411                         return( -1 );
412
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 )
416                                 return( -1 );
417
418                         if (lenlen != FOUR_BYTE_LEN) {
419                                 /*
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
423                                  */
424                                 SAFEMEMCPY( (*sos)->sos_first + taglen +
425                                     lenlen, (*sos)->sos_first + taglen +
426                                     FOUR_BYTE_LEN, len );
427                         }
428                 } else {
429                         /* Fill FOUR_BYTE_LEN bytes for length field */
430                         /* one byte of length length */
431                         if ( ber_write( ber, (char *)&ltag, 1, 1 ) != 1 )
432                                 return( -1 );
433
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 )
438                                 return( -1 );
439                 }
440                 /* The ber_ptr is at the set/seq start - move it to the end */
441                 (*sos)->sos_ber->ber_ptr += len;
442         } else {
443                 unsigned long   ntag;
444
445                 /* the tag */
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 );
450
451                 if ( ber->ber_options & LBER_USE_DER ) {
452                         ltag = (lenlen == 1)
453                                 ? (unsigned char) len
454                                 : 0x80 + (lenlen - 1);
455                 }
456
457                 /* one byte of length length */
458                 SAFEMEMCPY( (*sos)->sos_first + 1, &ltag, 1 );
459
460                 if ( ber->ber_options & LBER_USE_DER ) {
461                         if (lenlen > 1) {
462                                 /* Write the length itself */
463                                 SAFEMEMCPY( (*sos)->sos_first + 2,
464                                     (char *)&netlen + sizeof(unsigned long) -
465                                     (lenlen - 1),
466                                     lenlen - 1 );
467                         }
468                         if (lenlen != FOUR_BYTE_LEN) {
469                                 /*
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
473                                  */
474                                 SAFEMEMCPY( (*sos)->sos_first + taglen +
475                                     lenlen, (*sos)->sos_first + taglen +
476                                     FOUR_BYTE_LEN, len );
477                         }
478                 } else {
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 );
483                 }
484
485                 next->sos_clen += (taglen + lenlen + len);
486                 next->sos_ptr += (taglen + lenlen + len);
487         }
488
489         /* we're done with this seqorset, so free it up */
490         free( (char *) (*sos) );
491         *sos = next;
492
493         return( taglen + lenlen + len );
494 }
495
496 int
497 ber_put_seq( BerElement *ber )
498 {
499         return( ber_put_seqorset( ber ) );
500 }
501
502 int
503 ber_put_set( BerElement *ber )
504 {
505         return( ber_put_seqorset( ber ) );
506 }
507
508 /* VARARGS */
509 int
510 ber_printf
511 #ifdef HAVE_STDARG
512         ( BerElement *ber, char *fmt, ... )
513 #else
514         ( va_alist )
515 va_dcl
516 #endif
517 {
518         va_list         ap;
519 #ifndef HAVE_STDARG
520         BerElement      *ber;
521         char            *fmt;
522 #endif
523         char            *s, **ss;
524         struct berval   **bv;
525         int             rc, i;
526         unsigned long   len;
527
528 #ifdef HAVE_STDARG
529         va_start( ap, fmt );
530 #else
531         va_start( ap );
532         ber = va_arg( ap, BerElement * );
533         fmt = va_arg( ap, char * );
534 #endif
535
536         for ( rc = 0; *fmt && rc != -1; fmt++ ) {
537                 switch ( *fmt ) {
538                 case 'b':       /* boolean */
539                         i = va_arg( ap, int );
540                         rc = ber_put_boolean( ber, i, ber->ber_tag );
541                         break;
542
543                 case 'i':       /* int */
544                         i = va_arg( ap, int );
545                         rc = ber_put_int( ber, i, ber->ber_tag );
546                         break;
547
548                 case 'e':       /* enumeration */
549                         i = va_arg( ap, int );
550                         rc = ber_put_enum( ber, i, ber->ber_tag );
551                         break;
552
553                 case 'n':       /* null */
554                         rc = ber_put_null( ber, ber->ber_tag );
555                         break;
556
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 );
561                         break;
562
563                 case 's':       /* string */
564                         s = va_arg( ap, char * );
565                         rc = ber_put_string( ber, s, ber->ber_tag );
566                         break;
567
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 );
572                         break;
573
574                 case 't':       /* tag for the next element */
575                         ber->ber_tag = va_arg( ap, unsigned long );
576                         ber->ber_usertag = 1;
577                         break;
578
579                 case 'v':       /* vector of strings */
580                         if ( (ss = va_arg( ap, char ** )) == NULL )
581                                 break;
582                         for ( i = 0; ss[i] != NULL; i++ ) {
583                                 if ( (rc = ber_put_string( ber, ss[i],
584                                     ber->ber_tag )) == -1 )
585                                         break;
586                         }
587                         break;
588
589                 case 'V':       /* sequences of strings + lengths */
590                         if ( (bv = va_arg( ap, struct berval ** )) == NULL )
591                                 break;
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 )
595                                         break;
596                         }
597                         break;
598
599                 case '{':       /* begin sequence */
600                         rc = ber_start_seq( ber, ber->ber_tag );
601                         break;
602
603                 case '}':       /* end sequence */
604                         rc = ber_put_seqorset( ber );
605                         break;
606
607                 case '[':       /* begin set */
608                         rc = ber_start_set( ber, ber->ber_tag );
609                         break;
610
611                 case ']':       /* end set */
612                         rc = ber_put_seqorset( ber );
613                         break;
614
615                 default:
616                         if( ber->ber_debug ) {
617                                 lber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
618                                         "ber_printf: unknown fmt %c\n", *fmt );
619                         }
620                         rc = -1;
621                         break;
622                 }
623
624                 if ( ber->ber_usertag == 0 )
625                         ber->ber_tag = LBER_DEFAULT;
626                 else
627                         ber->ber_usertag = 0;
628         }
629
630         va_end( ap );
631
632         return( rc );
633 }