Back to index

lightning-sunbird  0.9+nobinonly
encode.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Copyright (c) 1990 Regents of the University of Michigan.
00040  * All rights reserved.
00041  *
00042  * Redistribution and use in source and binary forms are permitted
00043  * provided that this notice is preserved and that due credit is given
00044  * to the University of Michigan at Ann Arbor. The name of the University
00045  * may not be used to endorse or promote products derived from this
00046  * software without specific prior written permission. This software
00047  * is provided ``as is'' without express or implied warranty.
00048  */
00049 
00050 /* encode.c - ber output encoding routines */
00051 
00052 #include "lber-int.h"
00053 
00054 static int
00055 ber_calc_taglen( unsigned long tag )
00056 {
00057        int    i;
00058        long   mask;
00059 
00060        /* find the first non-all-zero byte in the tag */
00061        for ( i = sizeof(long) - 1; i > 0; i-- ) {
00062               mask = (0xffL << (i * 8));
00063               /* not all zero */
00064               if ( tag & mask )
00065                      break;
00066        }
00067 
00068        return( i + 1 );
00069 }
00070 
00071 static int
00072 ber_put_tag( BerElement     *ber, unsigned long tag, int nosos )
00073 {
00074        int           taglen;
00075        unsigned long ntag;
00076 
00077        taglen = ber_calc_taglen( tag );
00078 
00079        ntag = LBER_HTONL( tag );
00080 
00081        return( ber_write( ber, ((char *) &ntag) + sizeof(long) - taglen,
00082            taglen, nosos ) );
00083 }
00084 
00085 static int
00086 ber_calc_lenlen( unsigned long len )
00087 {
00088        /*
00089         * short len if it's less than 128 - one byte giving the len,
00090         * with bit 8 0.
00091         */
00092 
00093        if ( len <= 0x7F )
00094               return( 1 );
00095 
00096        /*
00097         * long len otherwise - one byte with bit 8 set, giving the
00098         * length of the length, followed by the length itself.
00099         */
00100 
00101        if ( len <= 0xFF )
00102               return( 2 );
00103        if ( len <= 0xFFFFL )
00104               return( 3 );
00105        if ( len <= 0xFFFFFFL )
00106               return( 4 );
00107 
00108        return( 5 );
00109 }
00110 
00111 static int
00112 ber_put_len( BerElement *ber, unsigned long len, int nosos )
00113 {
00114        int           i;
00115        char          lenlen;
00116        long          mask;
00117        unsigned long netlen;
00118 
00119        /*
00120         * short len if it's less than 128 - one byte giving the len,
00121         * with bit 8 0.
00122         */
00123 
00124        if ( len <= 127 ) {
00125               netlen = LBER_HTONL( len );
00126               return( ber_write( ber, (char *) &netlen + sizeof(long) - 1,
00127                   1, nosos ) );
00128        }
00129 
00130        /*
00131         * long len otherwise - one byte with bit 8 set, giving the
00132         * length of the length, followed by the length itself.
00133         */
00134 
00135        /* find the first non-all-zero byte */
00136        for ( i = sizeof(long) - 1; i > 0; i-- ) {
00137               mask = (0xffL << (i * 8));
00138               /* not all zero */
00139               if ( len & mask )
00140                      break;
00141        }
00142        lenlen = ++i;
00143        if ( lenlen > 4 )
00144               return( -1 );
00145        lenlen |= 0x80;
00146 
00147        /* write the length of the length */
00148        if ( ber_write( ber, &lenlen, 1, nosos ) != 1 )
00149               return( -1 );
00150 
00151        /* write the length itself */
00152        netlen = LBER_HTONL( len );
00153        if ( ber_write( ber, (char *) &netlen + (sizeof(long) - i), i, nosos )
00154            != i )
00155               return( -1 );
00156 
00157        return( i + 1 );
00158 }
00159 
00160 static int
00161 ber_put_int_or_enum( BerElement *ber, long num, unsigned long tag )
00162 {
00163        int    i, sign, taglen;
00164        int    len, lenlen;
00165        long   netnum, mask;
00166 
00167        sign = (num < 0);
00168 
00169        /*
00170         * high bit is set - look for first non-all-one byte
00171         * high bit is clear - look for first non-all-zero byte
00172         */
00173        for ( i = sizeof(long) - 1; i > 0; i-- ) {
00174               mask = (0xffL << (i * 8));
00175 
00176               if ( sign ) {
00177                      /* not all ones */
00178                      if ( (num & mask) != mask )
00179                             break;
00180               } else {
00181                      /* not all zero */
00182                      if ( num & mask )
00183                             break;
00184               }
00185        }
00186 
00187        /*
00188         * we now have the "leading byte".  if the high bit on this
00189         * byte matches the sign bit, we need to "back up" a byte.
00190         */
00191        mask = (num & (0x80L << (i * 8)));
00192        if ( (mask && !sign) || (sign && !mask) )
00193               i++;
00194 
00195        len = i + 1;
00196 
00197        if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
00198               return( -1 );
00199 
00200        if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 )
00201               return( -1 );
00202        i++;
00203        netnum = LBER_HTONL( num );
00204        if ( ber_write( ber, (char *) &netnum + (sizeof(long) - i), i, 0 ) 
00205               == i) 
00206               /* length of tag + length + contents */
00207               return( taglen + lenlen + i );
00208 
00209        return( -1 );
00210 }
00211 
00212 int
00213 LDAP_CALL
00214 ber_put_enum( BerElement *ber, long num, unsigned long tag )
00215 {
00216        if ( tag == LBER_DEFAULT )
00217               tag = LBER_ENUMERATED;
00218 
00219        return( ber_put_int_or_enum( ber, num, tag ) );
00220 }
00221 
00222 int
00223 LDAP_CALL
00224 ber_put_int( BerElement *ber, long num, unsigned long tag )
00225 {
00226        if ( tag == LBER_DEFAULT )
00227               tag = LBER_INTEGER;
00228 
00229        return( ber_put_int_or_enum( ber, num, tag ) );
00230 }
00231 
00232 int
00233 LDAP_CALL
00234 ber_put_ostring( BerElement *ber, char *str, unsigned long len,
00235        unsigned long tag )
00236 {
00237        int    taglen, lenlen, rc;
00238 #ifdef STR_TRANSLATION
00239        int    free_str;
00240 #endif /* STR_TRANSLATION */
00241 
00242        if ( tag == LBER_DEFAULT )
00243               tag = LBER_OCTETSTRING;
00244 
00245        if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
00246               return( -1 );
00247 
00248 #ifdef STR_TRANSLATION
00249        if ( len > 0 && ( ber->ber_options & LBER_OPT_TRANSLATE_STRINGS ) != 0
00250            && ber->ber_encode_translate_proc != NULL ) {
00251               if ( (*(ber->ber_encode_translate_proc))( &str, &len, 0 )
00252                   != 0 ) {
00253                      return( -1 );
00254               }
00255               free_str = 1;
00256        } else {
00257               free_str = 0;
00258        }
00259 #endif /* STR_TRANSLATION */
00260 
00261     /*  
00262      *  Note:  below is a spot where we limit ber_write 
00263      *         to signed long (instead of unsigned long)
00264      */
00265 
00266        if ( (lenlen = ber_put_len( ber, len, 0 )) == -1 ||
00267               ber_write( ber, str, len, 0 ) != (long) len ) {
00268               rc = -1;
00269        } else {
00270               /* return length of tag + length + contents */
00271               rc = taglen + lenlen + len;
00272        }
00273 
00274 #ifdef STR_TRANSLATION
00275        if ( free_str ) {
00276               NSLBERI_FREE( str );
00277        }
00278 #endif /* STR_TRANSLATION */
00279 
00280        return( rc );
00281 }
00282 
00283 int
00284 LDAP_CALL
00285 ber_put_string( BerElement *ber, char *str, unsigned long tag )
00286 {
00287        return( ber_put_ostring( ber, str, strlen( str ), tag ));
00288 }
00289 
00290 int
00291 LDAP_CALL
00292 ber_put_bitstring( BerElement *ber, char *str,
00293        unsigned long blen /* in bits */, unsigned long tag )
00294 {
00295        int           taglen, lenlen, len;
00296        unsigned char unusedbits;
00297 
00298        if ( tag == LBER_DEFAULT )
00299               tag = LBER_BITSTRING;
00300 
00301        if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
00302               return( -1 );
00303 
00304        len = ( blen + 7 ) / 8;
00305        unusedbits = (unsigned char) (len * 8 - blen);
00306        if ( (lenlen = ber_put_len( ber, len + 1, 0 )) == -1 )
00307               return( -1 );
00308 
00309        if ( ber_write( ber, (char *)&unusedbits, 1, 0 ) != 1 )
00310               return( -1 );
00311 
00312        if ( ber_write( ber, str, len, 0 ) != len )
00313               return( -1 );
00314 
00315        /* return length of tag + length + unused bit count + contents */
00316        return( taglen + 1 + lenlen + len );
00317 }
00318 
00319 int
00320 LDAP_CALL
00321 ber_put_null( BerElement *ber, unsigned long tag )
00322 {
00323        int    taglen;
00324 
00325        if ( tag == LBER_DEFAULT )
00326               tag = LBER_NULL;
00327 
00328        if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
00329               return( -1 );
00330 
00331        if ( ber_put_len( ber, 0, 0 ) != 1 )
00332               return( -1 );
00333 
00334        return( taglen + 1 );
00335 }
00336 
00337 int
00338 LDAP_CALL
00339 ber_put_boolean( BerElement *ber, int boolval, unsigned long tag )
00340 {
00341        int           taglen;
00342        unsigned char trueval = 0xff;
00343        unsigned char falseval = 0x00;
00344 
00345        if ( tag == LBER_DEFAULT )
00346               tag = LBER_BOOLEAN;
00347 
00348        if ( (taglen = ber_put_tag( ber, tag, 0 )) == -1 )
00349               return( -1 );
00350 
00351        if ( ber_put_len( ber, 1, 0 ) != 1 )
00352               return( -1 );
00353 
00354        if ( ber_write( ber, (char *)(boolval ? &trueval : &falseval), 1, 0 )
00355            != 1 )
00356               return( -1 );
00357 
00358        return( taglen + 2 );
00359 }
00360 
00361 #define FOUR_BYTE_LEN       5
00362 
00363 
00364 /* the idea here is roughly this: we maintain a stack of these Seqorset
00365  * structures. This is pushed when we see the beginning of a new set or
00366  * sequence. It is popped when we see the end of a set or sequence.
00367  * Since we don't want to malloc and free these structures all the time,
00368  * we pre-allocate a small set of them within the ber element structure.
00369  * thus we need to spot when we've overflowed this stack and fall back to
00370  * malloc'ing instead.
00371  */
00372 static int
00373 ber_start_seqorset( BerElement *ber, unsigned long tag )
00374 {
00375        Seqorset      *new_sos;
00376 
00377        /* can we fit into the local stack ? */
00378        if (ber->ber_sos_stack_posn < SOS_STACK_SIZE) {
00379               /* yes */
00380               new_sos = &ber->ber_sos_stack[ber->ber_sos_stack_posn];
00381        } else {
00382               /* no */
00383               if ( (new_sos = (Seqorset *)NSLBERI_MALLOC( sizeof(Seqorset)))
00384                   == NULLSEQORSET ) {
00385                      return( -1 );
00386               }
00387        }
00388        ber->ber_sos_stack_posn++;
00389 
00390        if ( ber->ber_sos == NULLSEQORSET )
00391               new_sos->sos_first = ber->ber_ptr;
00392        else
00393               new_sos->sos_first = ber->ber_sos->sos_ptr;
00394 
00395        /* Set aside room for a 4 byte length field */
00396        new_sos->sos_ptr = new_sos->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN;
00397        new_sos->sos_tag = tag;
00398 
00399        new_sos->sos_next = ber->ber_sos;
00400        new_sos->sos_clen = 0;
00401 
00402        ber->ber_sos = new_sos;
00403     if (ber->ber_sos->sos_ptr > ber->ber_end) {
00404         nslberi_ber_realloc(ber, ber->ber_sos->sos_ptr - ber->ber_end);
00405     }
00406        return( 0 );
00407 }
00408 
00409 int
00410 LDAP_CALL
00411 ber_start_seq( BerElement *ber, unsigned long tag )
00412 {
00413        if ( tag == LBER_DEFAULT )
00414               tag = LBER_SEQUENCE;
00415 
00416        return( ber_start_seqorset( ber, tag ) );
00417 }
00418 
00419 int
00420 LDAP_CALL
00421 ber_start_set( BerElement *ber, unsigned long tag )
00422 {
00423        if ( tag == LBER_DEFAULT )
00424               tag = LBER_SET;
00425 
00426        return( ber_start_seqorset( ber, tag ) );
00427 }
00428 
00429 static int
00430 ber_put_seqorset( BerElement *ber )
00431 {
00432        unsigned long len, netlen;
00433        int           taglen, lenlen;
00434        unsigned char ltag = 0x80 + FOUR_BYTE_LEN - 1;
00435        Seqorset      *next;
00436        Seqorset      **sos = &ber->ber_sos;
00437 
00438        /*
00439         * If this is the toplevel sequence or set, we need to actually
00440         * write the stuff out.  Otherwise, it's already been put in
00441         * the appropriate buffer and will be written when the toplevel
00442         * one is written.  In this case all we need to do is update the
00443         * length and tag.
00444         */
00445 
00446        len = (*sos)->sos_clen;
00447        netlen = LBER_HTONL( len );
00448        if ( sizeof(long) > 4 && len > 0xFFFFFFFFUL )
00449               return( -1 );
00450 
00451        if ( ber->ber_options & LBER_OPT_USE_DER ) {
00452               lenlen = ber_calc_lenlen( len );
00453        } else {
00454               lenlen = FOUR_BYTE_LEN;
00455        }
00456 
00457        if ( (next = (*sos)->sos_next) == NULLSEQORSET ) {
00458               /* write the tag */
00459               if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
00460                      return( -1 );
00461 
00462               if ( ber->ber_options & LBER_OPT_USE_DER ) {
00463                      /* Write the length in the minimum # of octets */
00464                      if ( ber_put_len( ber, len, 1 ) == -1 )
00465                             return( -1 );
00466 
00467                      if (lenlen != FOUR_BYTE_LEN) {
00468                             /*
00469                              * We set aside FOUR_BYTE_LEN bytes for
00470                              * the length field.  Move the data if
00471                              * we don't actually need that much
00472                              */
00473                             SAFEMEMCPY( (*sos)->sos_first + taglen +
00474                                 lenlen, (*sos)->sos_first + taglen +
00475                                 FOUR_BYTE_LEN, len );
00476                      }
00477               } else {
00478                      /* Fill FOUR_BYTE_LEN bytes for length field */
00479                      /* one byte of length length */
00480                      if ( ber_write( ber, (char *)&ltag, 1, 1 ) != 1 )
00481                             return( -1 );
00482 
00483                      /* the length itself */
00484                      if ( ber_write( ber, (char *) &netlen + sizeof(long)
00485                          - (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1, 1 )
00486                          != FOUR_BYTE_LEN - 1 )
00487                             return( -1 );
00488               }
00489               /* The ber_ptr is at the set/seq start - move it to the end */
00490               ber->ber_ptr += len;
00491        } else {
00492               unsigned long ntag;
00493 
00494               /* the tag */
00495               taglen = ber_calc_taglen( (*sos)->sos_tag );
00496               ntag = LBER_HTONL( (*sos)->sos_tag );
00497               SAFEMEMCPY( (*sos)->sos_first, (char *) &ntag +
00498                   sizeof(long) - taglen, taglen );
00499 
00500               if ( ber->ber_options & LBER_OPT_USE_DER ) {
00501                      ltag = (lenlen == 1) ? (unsigned char)len :  
00502                 (unsigned char) (0x80 + (lenlen - 1));
00503               }
00504 
00505               /* one byte of length length */
00506               SAFEMEMCPY( (*sos)->sos_first + 1, &ltag, 1 );
00507 
00508               if ( ber->ber_options & LBER_OPT_USE_DER ) {
00509                      if (lenlen > 1) {
00510                             /* Write the length itself */
00511                             SAFEMEMCPY( (*sos)->sos_first + 2,
00512                                 (char *)&netlen + sizeof(unsigned long) -
00513                                 (lenlen - 1),
00514                                 lenlen - 1 );
00515                      }
00516                      if (lenlen != FOUR_BYTE_LEN) {
00517                             /*
00518                              * We set aside FOUR_BYTE_LEN bytes for
00519                              * the length field.  Move the data if
00520                              * we don't actually need that much
00521                              */
00522                             SAFEMEMCPY( (*sos)->sos_first + taglen +
00523                                 lenlen, (*sos)->sos_first + taglen +
00524                                 FOUR_BYTE_LEN, len );
00525                      }
00526               } else {
00527                      /* the length itself */
00528                      SAFEMEMCPY( (*sos)->sos_first + taglen + 1,
00529                          (char *) &netlen + sizeof(long) -
00530                          (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1 );
00531               }
00532 
00533               next->sos_clen += (taglen + lenlen + len);
00534               next->sos_ptr += (taglen + lenlen + len);
00535        }
00536 
00537        /* we're done with this seqorset, so free it up */
00538        /* was this one from the local stack ? */
00539        if (ber->ber_sos_stack_posn <= SOS_STACK_SIZE) {
00540               /* yes */
00541        } else {
00542               /* no */
00543               NSLBERI_FREE( (char *) (*sos) );
00544        }
00545        ber->ber_sos_stack_posn--;
00546        *sos = next;
00547 
00548        return( taglen + lenlen + len );
00549 }
00550 
00551 int
00552 LDAP_CALL
00553 ber_put_seq( BerElement *ber )
00554 {
00555        return( ber_put_seqorset( ber ) );
00556 }
00557 
00558 int
00559 LDAP_CALL
00560 ber_put_set( BerElement *ber )
00561 {
00562        return( ber_put_seqorset( ber ) );
00563 }
00564 
00565 /* VARARGS */
00566 int
00567 LDAP_C
00568 ber_printf( BerElement *ber, const char *fmt, ... )
00569 {
00570        va_list              ap;
00571        char          *s, **ss;
00572        struct berval **bv;
00573        int           rc, i;
00574        unsigned long len;
00575 
00576        va_start( ap, fmt );
00577 
00578 #ifdef LDAP_DEBUG
00579        if ( lber_debug & 64 ) {
00580               char msg[80];
00581               sprintf( msg, "ber_printf fmt (%s)\n", fmt );
00582               ber_err_print( msg );
00583        }
00584 #endif
00585 
00586        for ( rc = 0; *fmt && rc != -1; fmt++ ) {
00587               switch ( *fmt ) {
00588               case 'b':     /* boolean */
00589                      i = va_arg( ap, int );
00590                      rc = ber_put_boolean( ber, i, ber->ber_tag );
00591                      break;
00592 
00593               case 'i':     /* int */
00594                      i = va_arg( ap, int );
00595                      rc = ber_put_int( ber, (long)i, ber->ber_tag );
00596                      break;
00597 
00598               case 'e':     /* enumeration */
00599                      i = va_arg( ap, int );
00600                      rc = ber_put_enum( ber, (long)i, ber->ber_tag );
00601                      break;
00602 
00603               case 'n':     /* null */
00604                      rc = ber_put_null( ber, ber->ber_tag );
00605                      break;
00606 
00607               case 'o':     /* octet string (non-null terminated) */
00608                      s = va_arg( ap, char * );
00609                      len = va_arg( ap, int );
00610                      rc = ber_put_ostring( ber, s, len, ber->ber_tag );
00611                      break;
00612 
00613               case 's':     /* string */
00614                      s = va_arg( ap, char * );
00615                      rc = ber_put_string( ber, s, ber->ber_tag );
00616                      break;
00617 
00618               case 'B':     /* bit string */
00619                      s = va_arg( ap, char * );
00620                      len = va_arg( ap, int );    /* in bits */
00621                      rc = ber_put_bitstring( ber, s, len, ber->ber_tag );
00622                      break;
00623 
00624               case 't':     /* tag for the next element */
00625                      ber->ber_tag = va_arg( ap, unsigned long );
00626                      ber->ber_usertag = 1;
00627                      break;
00628 
00629               case 'v':     /* vector of strings */
00630                      if ( (ss = va_arg( ap, char ** )) == NULL )
00631                             break;
00632                      for ( i = 0; ss[i] != NULL; i++ ) {
00633                             if ( (rc = ber_put_string( ber, ss[i],
00634                                 ber->ber_tag )) == -1 )
00635                                    break;
00636                      }
00637                      break;
00638 
00639               case 'V':     /* sequences of strings + lengths */
00640                      if ( (bv = va_arg( ap, struct berval ** )) == NULL )
00641                             break;
00642                      for ( i = 0; bv[i] != NULL; i++ ) {
00643                             if ( (rc = ber_put_ostring( ber, bv[i]->bv_val,
00644                                 bv[i]->bv_len, ber->ber_tag )) == -1 )
00645                                    break;
00646                      }
00647                      break;
00648 
00649               case '{':     /* begin sequence */
00650                      rc = ber_start_seq( ber, ber->ber_tag );
00651                      break;
00652 
00653               case '}':     /* end sequence */
00654                      rc = ber_put_seqorset( ber );
00655                      break;
00656 
00657               case '[':     /* begin set */
00658                      rc = ber_start_set( ber, ber->ber_tag );
00659                      break;
00660 
00661               case ']':     /* end set */
00662                      rc = ber_put_seqorset( ber );
00663                      break;
00664 
00665               default: {
00666                             char msg[80];
00667                             sprintf( msg, "unknown fmt %c\n", *fmt );
00668                             ber_err_print( msg );
00669                             rc = -1;
00670                             break;
00671                      }
00672               }
00673 
00674               if ( ber->ber_usertag == 0 )
00675                      ber->ber_tag = LBER_DEFAULT;
00676               else
00677                      ber->ber_usertag = 0;
00678        }
00679 
00680        va_end( ap );
00681 
00682        return( rc );
00683 }