From 7574b70c6bb14b9aa636889ece7b354f61de7edc Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Wed, 7 Jul 1999 17:58:21 +0000 Subject: [PATCH] Add charray (from slapd) to -lldap/-lldap_r (not yet used) Add digest md5 codes to -lldap/-lldap_r (work in progress) --- include/ldap_pvt.h | 29 +++ libraries/libldap/Makefile.in | 6 +- libraries/libldap/charray.c | 208 +++++++++++++++++ libraries/libldap/digest.c | 377 ++++++++++++++++++++++++++++++ libraries/libldap/libldap.dsp | 8 + libraries/libldap_r/Makefile.in | 6 +- libraries/libldap_r/libldap_r.dsp | 8 + 7 files changed, 638 insertions(+), 4 deletions(-) create mode 100644 libraries/libldap/charray.c create mode 100644 libraries/libldap/digest.c diff --git a/include/ldap_pvt.h b/include/ldap_pvt.h index eb6b6dbe69..3bede83b2a 100644 --- a/include/ldap_pvt.h +++ b/include/ldap_pvt.h @@ -44,6 +44,35 @@ ldap_pvt_gethostbyaddr_a LDAP_P(( struct hostent **result, int *herrno_ptr )); + +/* charray.c */ + +LDAP_F( int ) +ldap_charray_add LDAP_P(( + char ***a, + char *s )); + +LDAP_F( int ) +ldap_charray_merge LDAP_P(( + char ***a, + char **s )); + +LDAP_F( void ) +ldap_charray_free LDAP_P(( char **a )); + +LDAP_F( int ) +ldap_charray_inlist LDAP_P(( + char **a, + char *s )); + +LDAP_F( char ** ) +ldap_charray_dup LDAP_P(( char **a )); + +LDAP_F( char ** ) +ldap_str2charray LDAP_P(( + char *str, + char *brkstr )); + LDAP_END_DECL #endif diff --git a/libraries/libldap/Makefile.in b/libraries/libldap/Makefile.in index 0f4b322382..c57240e416 100644 --- a/libraries/libldap/Makefile.in +++ b/libraries/libldap/Makefile.in @@ -15,7 +15,8 @@ SRCS = bind.c open.c result.c error.c compare.c search.c \ free.c disptmpl.c srchpref.c dsparse.c tmplout.c sort.c \ getdn.c getentry.c getattr.c getvalues.c addentry.c \ request.c getdxbyname.c os-ip.c url.c charset.c \ - init.c options.c print.c string.c util-int.c schema.c + init.c options.c print.c string.c util-int.c schema.c \ + charray.c digest.c OBJS = bind.lo open.lo result.lo error.lo compare.lo search.lo \ controls.lo messages.lo references.lo extended.lo \ modify.lo add.lo modrdn.lo delete.lo abandon.lo ufn.lo cache.lo \ @@ -23,7 +24,8 @@ OBJS = bind.lo open.lo result.lo error.lo compare.lo search.lo \ free.lo disptmpl.lo srchpref.lo dsparse.lo tmplout.lo sort.lo \ getdn.lo getentry.lo getattr.lo getvalues.lo addentry.lo \ request.lo getdxbyname.lo os-ip.lo url.lo charset.lo \ - init.lo options.lo print.lo string.lo util-int.lo schema.lo + init.lo options.lo print.lo string.lo util-int.lo schema.lo \ + charray.lo digest.lo LDAP_INCDIR= ../../include LDAP_LIBDIR= ../../libraries diff --git a/libraries/libldap/charray.c b/libraries/libldap/charray.c new file mode 100644 index 0000000000..fefe1d9166 --- /dev/null +++ b/libraries/libldap/charray.c @@ -0,0 +1,208 @@ +/* + * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + */ +/* charray.c - routines for dealing with char * arrays */ + +#include "portable.h" + +#include + +#include +#include + +#include "ldap-int.h" + +int +ldap_charray_add( + char ***a, + char *s +) +{ + int n; + + if ( *a == NULL ) { + *a = (char **) LDAP_MALLOC( 2 * sizeof(char *) ); + n = 0; + } else { + for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) { + ; /* NULL */ + } + + *a = (char **) LDAP_REALLOC( (char *) *a, + (n + 2) * sizeof(char *) ); + } + + if( *a == NULL ) { + return -1; + } + + (*a)[n] = LDAP_STRDUP(s); + + if( (*a)[n] == NULL ) { + return 1; + } + + (*a)[++n] = NULL; + + return 0; +} + +int +ldap_charray_merge( + char ***a, + char **s +) +{ + int i, n, nn; + char **aa; + + for ( n = 0; *a != NULL && (*a)[n] != NULL; n++ ) { + ; /* NULL */ + } + for ( nn = 0; s[nn] != NULL; nn++ ) { + ; /* NULL */ + } + + aa = (char **) LDAP_REALLOC( (char *) *a, (n + nn + 1) * sizeof(char *) ); + + if( aa == NULL ) + return -1; + + *a = aa; + + for ( i = 0; i < nn; i++ ) { + (*a)[n + i] = LDAP_STRDUP(s[i]); + + if( (*a)[n + i] == NULL ) { + for( --i ; i >= 0 ; i-- ) { + LDAP_FREE( (*a)[n + i] ); + (*a)[n + i] = NULL; + } + return -1; + } + } + + (*a)[n + nn] = NULL; + return 0; +} + +void +ldap_charray_free( char **a ) +{ + char **p; + + if ( a == NULL ) { + return; + } + + for ( p = a; *p != NULL; p++ ) { + if ( *p != NULL ) { + LDAP_FREE( *p ); + } + } + + LDAP_FREE( (char *) a ); +} + +int +ldap_charray_inlist( + char **a, + char *s +) +{ + int i; + + for ( i = 0; a[i] != NULL; i++ ) { + if ( strcasecmp( s, a[i] ) == 0 ) { + return( 1 ); + } + } + + return( 0 ); +} + +char ** +ldap_charray_dup( char **a ) +{ + int i; + char **new; + + for ( i = 0; a[i] != NULL; i++ ) + ; /* NULL */ + + new = (char **) LDAP_MALLOC( (i + 1) * sizeof(char *) ); + + if( new == NULL ) { + return NULL; + } + + for ( i = 0; a[i] != NULL; i++ ) { + new[i] = LDAP_STRDUP( a[i] ); + + if( new[i] == NULL ) { + for( --i ; i >= 0 ; i-- ) { + LDAP_FREE( new[i] ); + } + LDAP_FREE( new ); + return NULL; + } + } + new[i] = NULL; + + return( new ); +} + +char ** +ldap_str2charray( char *str, char *brkstr ) +{ + char **res; + char *s; + char *lasts; + int i; + + /* protect the input string from strtok */ + str = LDAP_STRDUP( str ); + if( str == NULL ) { + return NULL; + } + + i = 1; + for ( s = str; *s; s++ ) { + if ( strchr( brkstr, *s ) != NULL ) { + i++; + } + } + + res = (char **) LDAP_MALLOC( (i + 1) * sizeof(char *) ); + + if( res == NULL ) { + LDAP_FREE( str ); + return NULL; + } + + i = 0; + + for ( s = ldap_pvt_strtok( str, brkstr, &lasts ); + s != NULL; + s = ldap_pvt_strtok( NULL, brkstr, &lasts ) ) + { + res[i] = LDAP_STRDUP( s ); + + if(res[i] == NULL) { + for( --i ; i >= 0 ; i-- ) { + LDAP_FREE( res[i] ); + } + LDAP_FREE( res ); + LDAP_FREE( str ); + return NULL; + } + + i++; + } + + res[i] = NULL; + + LDAP_FREE( str ); + return( res ); +} diff --git a/libraries/libldap/digest.c b/libraries/libldap/digest.c new file mode 100644 index 0000000000..a0ca3a7a2a --- /dev/null +++ b/libraries/libldap/digest.c @@ -0,0 +1,377 @@ +/* + * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + */ +/* digest.c: + * DIGEST-MD5 routines + */ + +#include + +#include +#include +#include + +#include "ldap-int.h" +#include + +#define TK_NOENDQUOTE -2 +#define TK_OUTOFMEM -1 + +#define TK_EOS 0 +#define TK_UNEXPCHAR 1 +#define TK_BAREWORD 2 +#define TK_COMMA 3 +#define TK_EQUALS 4 +#define TK_QDSTRING 5 + +struct token { + int type; + char *sval; +}; + +static int +get_token(const char ** sp, char ** token_val) +{ + int kind; + const char * p; + const char * q; + char * res; + + *token_val = NULL; + + if( (**sp) != '\0' || iscntrl(**sp) || !isascii(**sp) ) { + (*sp)++; + return TK_UNEXPCHAR; + } + + switch (**sp) { + case '\0': + kind = TK_EOS; + (*sp)++; + break; + + case ',': + kind = TK_COMMA; + (*sp)++; + break; + + case '=': + kind = TK_EQUALS; + (*sp)++; + break; + + case '\"': + kind = TK_QDSTRING; + (*sp)++; + + for (p = *sp; + **sp != '\"' && **sp != '\0' && isascii(**sp); + (*sp)++ ) + { + if(**sp == '\\') { + (*sp)++; + if( **sp == '\0' ) break; + } + } + + if ( **sp == '\"' ) { + q = *sp; + res = LDAP_MALLOC(q-p+1); + if ( !res ) { + kind = TK_OUTOFMEM; + } else { + strncpy(res,p,q-p); + res[q-p] = '\0'; + *token_val = res; + } + (*sp)++; + + } else { + kind = TK_NOENDQUOTE; + } + + break; + + default: + kind = TK_BAREWORD; + p = *sp; + while ( isascii(**sp) && + !iscntrl(**sp) && + !isspace(**sp) && + **sp != '(' && + **sp != ')' && + **sp != '<' && + **sp != '>' && + **sp != '@' && + **sp != ',' && + **sp != ';' && + **sp != ':' && + **sp != '\\'&& + **sp != '\"'&& + **sp != '/' && + **sp != '[' && + **sp != ']' && + **sp != '?' && + **sp != '=' && + **sp != '{' && + **sp != '}' && + **sp != '\0' ) + (*sp)++; + q = *sp; + res = LDAP_MALLOC(q-p+1); + if ( !res ) { + kind = TK_OUTOFMEM; + } else { + strncpy(res,p,q-p); + res[q-p] = '\0'; + *token_val = res; + } + } + + return kind; +} + +struct kv { + char *key; + char *value; +}; + +static void kv_destory( struct kv **kv ) +{ + int i; + + if( kv == NULL ) return; + + for( i=0; kv[i] != NULL; i++ ) { + if( kv[i]->key != NULL ) { + LDAP_FREE( kv[i]->key ); + } + + if( kv[i]->value != NULL ) { + LDAP_FREE( kv[i]->value ); + } + + LDAP_FREE( kv[i] ); + } + + LDAP_FREE( kv ); +} + +static int kv_add( struct kv ***kvs, const struct kv *kv ) +{ + int n; + struct kv **tmp_kvs; + struct kv *tmp_kv; + + assert( kvs != NULL ); + assert( kv != NULL ); + + tmp_kv = LDAP_MALLOC( sizeof(struct kv) ); + + if( tmp_kv == NULL ) { + return -1; + } + + *tmp_kv = *kv; + + if( *kvs == NULL ) { + tmp_kvs = LDAP_MALLOC( 2 * sizeof(struct kv *) ); + n = 0; + + } else { + for( n=0; (*kvs)[n] != NULL; n++ ) { + /* EMPTY */ ; + } + + tmp_kvs = LDAP_REALLOC( kvs, (n+2) * sizeof(struct kv *) ); + } + + if( tmp_kvs == NULL ) { + LDAP_FREE( tmp_kv ); + return -1; + } + + *kvs = tmp_kvs; + kvs[n] = tmp_kvs; + kvs[n+1] = NULL; + + return 0; +} + +#define ST_ERROR -1 +#define ST_DONE 0 +#define ST_KEY 1 +#define ST_EQUALS 2 +#define ST_VALUE 3 +#define ST_SEP 4 + +static int +parse_key_value( + struct kv ***kvsp, + const char *str ) +{ + int rc = 0; + int kind, state; + const char *ss = str; + char *sval; + + struct kv **kvs = NULL; + struct kv kv; + + assert( kvsp != NULL ); + assert( str != NULL ); + + kv.key = NULL; + kv.value = NULL; + + state = ST_KEY; + + while( state > ST_DONE ) { + kind = get_token( &ss, &sval ); + switch( kind ) { + case TK_EOS: + state = ( state == ST_SEP ) + ? ST_DONE : ST_ERROR; + break; + + case TK_BAREWORD: + if( state == ST_KEY ) { + state = ST_EQUALS; + + assert( kv.key == NULL ); + assert( kv.value == NULL ); + kv.key = sval; + + } else if ( state == ST_VALUE ) { + state = ST_SEP; + + assert( kv.key != NULL ); + assert( kv.value == NULL ); + kv.value = sval; + + } else { + state = ST_ERROR; + } + break; + + case TK_COMMA: + state = ( state == ST_SEP ) + ? ST_KEY : ST_ERROR; + break; + + case TK_EQUALS: + state = ( state == ST_EQUALS ) + ? ST_VALUE : ST_ERROR; + break; + + case TK_QDSTRING: + if( state == ST_VALUE ) { + state = ST_SEP; + + assert( kv.key != NULL ); + assert( kv.value == NULL ); + kv.value = sval; + + } else { + state = ST_ERROR; + } + break; + + default: + state = ST_ERROR; + } + + if( state == ST_SEP ) { + /* add kv to return */ + if( kv_add( &kvs, &kv ) != 0 ) { + state = ST_ERROR; + kind = TK_OUTOFMEM; + + } else { + kv.key = NULL; + kv.value = NULL; + } + } + } + + if( state == ST_ERROR ) { + if( kv.key != NULL ) LDAP_FREE(kv.key); + if( kv.value != NULL ) LDAP_FREE( kv.value ); + + kv_destory( kvs ); + kvs = NULL; + + rc = ( kind == TK_OUTOFMEM ) ? -1 : 1 ; + } + + *kvsp = kvs; + return rc; +} + +static int +parse_value_list( + char ***valuesp, + const char* str ) +{ + int rc = 0; + char **values = NULL; + + int kind, state; + const char *ss = str; + char *sval; + + assert( valuesp != NULL ); + assert( str != NULL ); + + state = ST_VALUE; + + while( state > ST_DONE ) { + kind = get_token( &ss, &sval ); + switch( kind ) { + case TK_EOS: + state = ( state == ST_SEP ) + ? ST_DONE : ST_ERROR; + break; + + case TK_BAREWORD: + if( state == ST_VALUE ) { + state = ST_SEP; + + } else { + state = ST_ERROR; + } + break; + + case TK_COMMA: + state = ( state == ST_SEP ) + ? ST_VALUE : ST_ERROR; + break; + + default: + state = ST_ERROR; + } + + if( state == ST_SEP ) { + if( ldap_charray_add( &values, sval ) != 0 ) { + state = ST_ERROR; + kind = TK_OUTOFMEM; + } + + LDAP_FREE( sval ); + sval = NULL; + } + } + + if( state == ST_ERROR ) { + if( sval != NULL ) LDAP_FREE( sval ); + + LDAP_VFREE( values ); + values = NULL; + + rc = ( kind == TK_OUTOFMEM ) ? -1 : 1 ; + } + + *valuesp = values; + return rc; +} diff --git a/libraries/libldap/libldap.dsp b/libraries/libldap/libldap.dsp index 3d440bf9f4..081c82e4df 100644 --- a/libraries/libldap/libldap.dsp +++ b/libraries/libldap/libldap.dsp @@ -143,6 +143,10 @@ SOURCE=.\cache.c # End Source File # Begin Source File +SOURCE=.\charray.c +# End Source File +# Begin Source File + SOURCE=.\charset.c # End Source File # Begin Source File @@ -163,6 +167,10 @@ SOURCE=.\delete.c # End Source File # Begin Source File +SOURCE=.\digest.c +# End Source File +# Begin Source File + SOURCE=.\disptmpl.c # End Source File # Begin Source File diff --git a/libraries/libldap_r/Makefile.in b/libraries/libldap_r/Makefile.in index 028b728453..4c8d643fe7 100644 --- a/libraries/libldap_r/Makefile.in +++ b/libraries/libldap_r/Makefile.in @@ -14,7 +14,8 @@ XXSRCS = apitest.c test.c tmpltest.c extended.c \ free.c disptmpl.c srchpref.c dsparse.c tmplout.c sort.c \ getdn.c getentry.c getattr.c getvalues.c addentry.c \ request.c getdxbyname.c os-ip.c url.c charset.c \ - init.c options.c print.c string.c util-int.c schema.c + init.c options.c print.c string.c util-int.c schema.c \ + charray.c digest.c SRCS = thr_posix.c thr_cthreads.c thr_thr.c thr_lwp.c thr_nt.c \ thr_sleep.c thr_stub.c rdwr.c OBJS = extended.lo \ @@ -26,7 +27,8 @@ OBJS = extended.lo \ request.lo getdxbyname.lo os-ip.lo url.lo charset.lo \ init.lo options.lo print.lo string.lo util-int.lo schema.lo \ thr_posix.lo thr_cthreads.lo thr_thr.lo thr_lwp.lo thr_nt.lo \ - thr_sleep.lo thr_stub.lo rdwr.lo + thr_sleep.lo thr_stub.lo rdwr.lo \ + charray.lo digest.lo LDAP_INCDIR= ../../include LDAP_LIBDIR= ../../libraries diff --git a/libraries/libldap_r/libldap_r.dsp b/libraries/libldap_r/libldap_r.dsp index be1a2629b2..a48e95f2c2 100644 --- a/libraries/libldap_r/libldap_r.dsp +++ b/libraries/libldap_r/libldap_r.dsp @@ -144,6 +144,10 @@ SOURCE=..\libldap\cache.c # End Source File # Begin Source File +SOURCE=..\libldap\charray.c +# End Source File +# Begin Source File + SOURCE=..\libldap\charset.c # End Source File # Begin Source File @@ -164,6 +168,10 @@ SOURCE=..\libldap\delete.c # End Source File # Begin Source File +SOURCE=..\libldap\digest.c +# End Source File +# Begin Source File + SOURCE=..\libldap\disptmpl.c # End Source File # Begin Source File -- 2.39.5