]> git.sur5r.net Git - openldap/blobdiff - libraries/liblber/encode.c
fix test when slapo-memberof(5) built as module (ITS#5132)
[openldap] / libraries / liblber / encode.c
index f0ea2c8698c9420611e3ccf058a40dbb1d0303b1..9b20518ba363b34b0818ca414917a090fa5c5e97 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2005 The OpenLDAP Foundation.
+ * Copyright 1998-2007 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -30,6 +30,7 @@
 
 #include "portable.h"
 
+#include <ctype.h>
 #include <stdio.h>
 
 #include <ac/stdlib.h>
@@ -177,6 +178,63 @@ ber_put_len( BerElement *ber, ber_len_t len, int nosos )
        return rc == i ?  i+1 : -1;
 }
 
+/* out->bv_len should be the buffer size on input */
+int
+ber_encode_oid( BerValue *in, BerValue *out )
+{
+       unsigned char *der;
+       unsigned long val1, val;
+       int i, j, len;
+       char *ptr, *end, *inend;
+
+       assert( in != NULL );
+       assert( out != NULL );
+
+       if ( !out->bv_val || out->bv_len < in->bv_len/2 )
+               return -1;
+
+       der = (unsigned char *) out->bv_val;
+       ptr = in->bv_val;
+       inend = ptr + in->bv_len;
+
+       /* OIDs start with <0-1>.<0-39> or 2.<any>, DER-encoded 40*val1+val2 */
+       if ( !isdigit( (unsigned char) *ptr )) return -1;
+       val1 = strtoul( ptr, &end, 10 );
+       if ( end == ptr || val1 > 2 ) return -1;
+       if ( *end++ != '.' || !isdigit( (unsigned char) *end )) return -1;
+       val = strtoul( end, &ptr, 10 );
+       if ( ptr == end ) return -1;
+       if ( val > (val1 < 2 ? 39 : LBER_OID_COMPONENT_MAX - 80) ) return -1;
+       val += val1 * 40;
+
+       for (;;) {
+               if ( ptr > inend ) return -1;
+
+               len = 0;
+               do {
+                       der[len++] = (val & 0xff) | 0x80;
+               } while ( (val >>= 7) != 0 );
+               der[0] &= 0x7f;
+               for ( i = 0, j = len; i < --j; i++ ) {
+                       unsigned char tmp = der[i];
+                       der[i] = der[j];
+                       der[j] = tmp;
+               }
+               der += len;
+               if ( ptr == inend )
+                       break;
+
+               if ( *ptr++ != '.' ) return -1;
+               if ( !isdigit( (unsigned char) *ptr )) return -1;
+               val = strtoul( ptr, &end, 10 );
+               if ( end == ptr || val > LBER_OID_COMPONENT_MAX ) return -1;
+               ptr = end;
+       }
+
+       out->bv_len = (char *)der - out->bv_val;
+       return 0;
+}
+
 static int
 ber_put_int_or_enum(
        BerElement *ber,