]> git.sur5r.net Git - openldap/blobdiff - libraries/liblber/encode.c
use lutil_ato*() whenever appropriate
[openldap] / libraries / liblber / encode.c
index 3b86dc0d4f03f69262545c080e2f09c100ba8353..f0ea2c8698c9420611e3ccf058a40dbb1d0303b1 100644 (file)
@@ -1,11 +1,19 @@
-/* Encode.c - ber output encoding routines */
+/* encode.c - ber output encoding routines */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2005 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
-/* Portions
- * Copyright (c) 1990 Regents of the University of Michigan.
+/* Portions Copyright (c) 1990 Regents of the University of Michigan.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
  * software without specific prior written permission. This software
  * is provided ``as is'' without express or implied warranty.
  */
+/* ACKNOWLEDGEMENTS:
+ * This work was originally developed by the University of Michigan
+ * (as part of U-MICH LDAP).
+ */
 
 #include "portable.h"
 
@@ -44,18 +56,20 @@ static int ber_put_int_or_enum LDAP_P((
        ber_int_t num,
        ber_tag_t tag ));
 
+#define        BER_TOP_BYTE(type)      (sizeof(type)-1)
+#define        BER_TOP_MASK(type)      ((type)0xffU << (BER_TOP_BYTE(type)*8))
 
 static int
 ber_calc_taglen( ber_tag_t tag )
 {
-       int     i;
-       ber_tag_t       mask;
+       int     i = BER_TOP_BYTE(ber_tag_t);
+       ber_tag_t       mask = BER_TOP_MASK(ber_tag_t);
 
        /* find the first non-all-zero byte in the tag */
-       for ( i = sizeof(ber_tag_t) - 1; i > 0; i-- ) {
-               mask = ((ber_tag_t)0xffU << (i * 8));
+       for ( ; i > 0; i-- ) {
                /* not all zero */
                if ( tag & mask ) break;
+               mask >>= 8;
        }
 
        return i + 1;
@@ -68,7 +82,7 @@ ber_put_tag(
        int nosos )
 {
        int rc;
-       int     taglen;
+       int taglen;
        int     i;
        unsigned char nettag[sizeof(ber_tag_t)];
 
@@ -77,14 +91,12 @@ ber_put_tag(
 
        taglen = ber_calc_taglen( tag );
 
-       for( i=0; i<taglen; i++ ) {
-               nettag[(sizeof(ber_tag_t)-1) - i] = (unsigned char)(tag & 0xffU);
+       for( i=taglen-1; i>=0; i-- ) {
+               nettag[i] = (unsigned char)(tag & 0xffU);
                tag >>= 8;
        }
 
-       rc = ber_write( ber,
-               (char *) &nettag[sizeof(ber_tag_t) - taglen],
-           taglen, nosos );
+       rc = ber_write( ber, (char *) nettag, taglen, nosos );
 
        return rc;
 }
@@ -139,10 +151,12 @@ ber_put_len( BerElement *ber, ber_len_t len, int nosos )
         */
 
        /* find the first non-all-zero byte */
-       for ( i = sizeof(ber_len_t) - 1; i > 0; i-- ) {
-               mask = ((ber_len_t)0xffU << (i * 8));
+       i = BER_TOP_BYTE(ber_len_t);
+       mask = BER_TOP_MASK(ber_len_t);
+       for ( ; i > 0; i-- ) {
                /* not all zero */
                if ( len & mask ) break;
+               mask >>= 8;
        }
        lenlen = (unsigned char) ++i;
        if ( lenlen > 4 ) return -1;
@@ -152,15 +166,13 @@ ber_put_len( BerElement *ber, ber_len_t len, int nosos )
        /* write the length of the length */
        if ( ber_write( ber, &lenlen, 1, nosos ) != 1 ) return -1;
 
-       for( j=0; j<i; j++) {
-               netlen[(sizeof(ber_len_t)-1) - j] = (unsigned char)(len & 0xffU);
+       for( j=i-1; j>=0; j-- ) {
+               netlen[j] = (unsigned char)(len & 0xffU);
                len >>= 8;
        }
 
        /* write the length itself */
-       rc = ber_write( ber,
-               (char *) &netlen[sizeof(ber_len_t)-i],
-               i, nosos );
+       rc = ber_write( ber, (char *) netlen, i, nosos );
 
        return rc == i ?  i+1 : -1;
 }
@@ -187,9 +199,9 @@ ber_put_int_or_enum(
         * high bit is set - look for first non-all-one byte
         * high bit is clear - look for first non-all-zero byte
         */
-       for ( i = sizeof(ber_int_t) - 1; i > 0; i-- ) {
-               mask = ((ber_uint_t)0xffU << (i * 8));
-
+       i = BER_TOP_BYTE(ber_int_t);
+       mask = BER_TOP_MASK(ber_uint_t);
+       for ( ; i > 0; i-- ) {
                if ( sign ) {
                        /* not all ones */
                        if ( (unum & mask) != mask ) break;
@@ -197,6 +209,7 @@ ber_put_int_or_enum(
                        /* not all zero */
                        if ( unum & mask ) break;
                }
+               mask >>= 8;
        }
 
        /*
@@ -219,14 +232,12 @@ ber_put_int_or_enum(
        }
        i++;
 
-       for( j=0; j<i; j++ ) {
-               netnum[(sizeof(ber_int_t)-1) - j] = (unsigned char)(unum & 0xffU);
+       for( j=i-1; j>=0; j-- ) {
+               netnum[j] = (unsigned char)(unum & 0xffU);
                unum >>= 8;
        }
 
-       rc = ber_write( ber,
-               (char *) &netnum[sizeof(ber_int_t) - i],
-               i, 0 );
+       rc = ber_write( ber, (char *) netnum, i, 0 );
 
        /* length of tag + length + contents */
        return rc == i ? taglen + lenlen + i : -1;
@@ -524,19 +535,21 @@ ber_put_seqorset( BerElement *ber )
        }
 
        if( lenlen > 1 ) {
-               ber_len_t i;
-               for( i=0; i < lenlen-1; i++ ) {
-                       netlen[(sizeof(ber_len_t)-1) - i] = 
-                               (unsigned char)((len >> i*8) & 0xffU);
+               int i;
+               ber_len_t j = len;
+               for( i=lenlen-2; i >= 0; i-- ) {
+                       netlen[i] = j & 0xffU;
+                       j >>= 8;
                }
        } else {
-               netlen[sizeof(ber_len_t)-1] = (unsigned char)(len & 0x7fU);
+               netlen[0] = (unsigned char)(len & 0x7fU);
        }
 
        if ( (next = (*sos)->sos_next) == NULL ) {
                /* write the tag */
-               if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 )
+               if ( (taglen = ber_put_tag( ber, (*sos)->sos_tag, 1 )) == -1 ) {
                        return( -1 );
+               }
 
                if ( ber->ber_options & LBER_USE_DER ) {
                        /* Write the length in the minimum # of octets */
@@ -562,9 +575,7 @@ ber_put_seqorset( BerElement *ber )
                        }
 
                        /* the length itself */
-                       rc  = ber_write( ber,
-                               (char *) &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN-1)],
-                               FOUR_BYTE_LEN-1, 1 );
+                       rc  = ber_write( ber, (char *) netlen, FOUR_BYTE_LEN-1, 1 );
 
                        if( rc != FOUR_BYTE_LEN - 1 ) {
                                return -1;
@@ -596,14 +607,12 @@ ber_put_seqorset( BerElement *ber )
                /* the tag */
                taglen = ber_calc_taglen( tmptag );
 
-               for( i = 0; i < taglen; i++ ) {
-                       nettag[(sizeof(ber_tag_t)-1) - i] = (unsigned char)(tmptag & 0xffU);
+               for( i = taglen-1; i >= 0; i-- ) {
+                       nettag[i] = (unsigned char)(tmptag & 0xffU);
                        tmptag >>= 8;
                }
 
-               AC_FMEMCPY( (*sos)->sos_first,
-                       &nettag[sizeof(ber_tag_t) - taglen],
-                       taglen );
+               AC_FMEMCPY( (*sos)->sos_first, nettag, taglen );
 
                if ( ber->ber_options & LBER_USE_DER ) {
                        ltag = (lenlen == 1)
@@ -617,9 +626,7 @@ ber_put_seqorset( BerElement *ber )
                if ( ber->ber_options & LBER_USE_DER ) {
                        if (lenlen > 1) {
                                /* Write the length itself */
-                               AC_FMEMCPY( (*sos)->sos_first + 2,
-                                   &netlen[sizeof(ber_len_t) - (lenlen - 1)],
-                                       lenlen - 1 );
+                               AC_FMEMCPY( (*sos)->sos_first + 2, netlen, lenlen - 1 );
                        }
                        if (lenlen != FOUR_BYTE_LEN) {
                                /*
@@ -634,8 +641,7 @@ ber_put_seqorset( BerElement *ber )
                } else {
                        /* the length itself */
                        AC_FMEMCPY( (*sos)->sos_first + taglen + 1,
-                           &netlen[sizeof(ber_len_t) - (FOUR_BYTE_LEN - 1)],
-                               FOUR_BYTE_LEN - 1 );
+                           netlen, FOUR_BYTE_LEN - 1 );
                }
 
                next->sos_clen += (taglen + lenlen + len);
@@ -805,13 +811,8 @@ ber_printf( BerElement *ber, LDAP_CONST char *fmt, ... )
 
                default:
                        if( ber->ber_debug ) {
-#ifdef NEW_LOGGING
-                               LDAP_LOG( BER, ERR, 
-                                       "ber_printf: unknown fmt %c\n", *fmt, 0, 0 );
-#else
                                ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
                                        "ber_printf: unknown fmt %c\n", *fmt );
-#endif
                        }
                        rc = -1;
                        break;