Back to index

openldap  2.4.31
componentlib.c
Go to the documentation of this file.
00001 /* Copyright 2004 IBM Corporation
00002  * All rights reserved.
00003  * Redisribution and use in source and binary forms, with or without
00004  * modification, are permitted only as authorizd by the OpenLADP
00005  * Public License.
00006  */
00007 /* ACKNOWLEDGEMENTS
00008  * This work originally developed by Sang Seok Lim
00009  * 2004/06/18 03:20:00      slim@OpenLDAP.org
00010  */
00011 
00012 #include "portable.h"
00013 #include <ac/string.h>
00014 #include <ac/socket.h>
00015 #include <ldap_pvt.h>
00016 #include "lutil.h"
00017 #include <ldap.h>
00018 #include "slap.h"
00019 #include "component.h"
00020 
00021 #include "componentlib.h"
00022 #include "asn.h"
00023 #include <asn-gser.h>
00024 #include <stdlib.h>
00025 
00026 #include <string.h>
00027 
00028 #ifndef SLAPD_COMP_MATCH
00029 #define SLAPD_COMP_MATCH SLAPD_MOD_DYNAMIC
00030 #endif
00031 
00032 #ifdef SLAPD_COMP_MATCH
00033 /*
00034  * Matching function : BIT STRING
00035  */
00036 int
00037 MatchingComponentBits ( char* oid, ComponentSyntaxInfo *csi_attr,
00038                      ComponentSyntaxInfo *csi_assert )
00039 {
00040        int rc;
00041         MatchingRule* mr;
00042         ComponentBits *a, *b;
00043                                                                           
00044         if ( oid ) {
00045                 mr = retrieve_matching_rule(oid, (AsnTypeId)csi_attr->csi_comp_desc->cd_type_id );
00046                 if ( mr )
00047                         return component_value_match( mr, csi_attr , csi_assert );
00048         }
00049         a = ((ComponentBits*)csi_attr);
00050         b = ((ComponentBits*)csi_assert);
00051        rc = ( a->value.bitLen == b->value.bitLen && 
00052               strncmp( a->value.bits,b->value.bits,a->value.bitLen ) == 0 );
00053         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
00054 }
00055 
00056 /*
00057  * Free function: BIT STRING
00058  */
00059 void
00060 FreeComponentBits ( ComponentBits* v ) {
00061        FreeAsnBits( &v->value );
00062 }
00063 
00064 /*
00065  * GSER Encoder : BIT STRING
00066  */
00067 int
00068 GEncComponentBits ( GenBuf *b, ComponentBits *in )
00069 {
00070        GAsnBits bits = {0};
00071 
00072        bits.value = in->value;
00073        if ( !in )
00074               return (-1);
00075        return GEncAsnBitsContent ( b, &bits);
00076 }
00077 
00078 
00079 /*
00080  * GSER Decoder : BIT STRING
00081  */
00082 int
00083 GDecComponentBits ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
00084 {
00085         char* peek_head;
00086         int i, strLen;
00087         void* component_values;
00088         ComponentBits* k, **k2;
00089        GAsnBits result;
00090 
00091         k = (ComponentBits*) v;
00092                                                                           
00093         if ( mode & DEC_ALLOC_MODE_0 ) {
00094                 k2 = (ComponentBits**) v;
00095                 *k2 = (ComponentBits*) CompAlloc( mem_op, sizeof( ComponentBits ) );
00096               if ( !*k2 ) return LDAP_DECODING_ERROR;
00097                 k = *k2;
00098         }
00099         
00100        if ( GDecAsnBitsContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
00101               if ( k ) CompFree( mem_op,  k );
00102               return LDAP_DECODING_ERROR;
00103        }
00104        k->value = result.value;
00105        k->comp_desc = get_component_description (BASICTYPE_BITSTRING);
00106 
00107        return LDAP_SUCCESS;
00108 }
00109 
00110 /*
00111  * Component BER Decoder : BIT STRING
00112  */
00113 int
00114 BDecComponentBitsTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
00115        return BDecComponentBits ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
00116 }
00117 
00118 int
00119 BDecComponentBits ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
00120                      AsnLen *bytesDecoded, int mode )
00121 {
00122         char* peek_head;
00123         int i, strLen, rc;
00124         void* component_values;
00125         ComponentBits* k, **k2;
00126        AsnBits result;
00127                                                                           
00128         k = (ComponentBits*) v;
00129                                                                           
00130         if ( mode & DEC_ALLOC_MODE_0 ) {
00131                 k2 = (ComponentBits**) v;
00132                 *k2 = (ComponentBits*) CompAlloc( mem_op, sizeof( ComponentBits ) );
00133               if ( !*k2 ) return LDAP_DECODING_ERROR;
00134                 k = *k2;
00135         }
00136         
00137        if ( mode & CALL_TAG_DECODER ){
00138               mode = mode & CALL_CONTENT_DECODER;
00139               rc = BDecAsnBits ( mem_op, b, &result, bytesDecoded );
00140        } else {
00141               rc = BDecAsnBitsContent ( mem_op, b, tagId, len, &result, bytesDecoded );
00142        }
00143 
00144        if ( rc < 0 ) {
00145               if ( k ) CompFree( mem_op,  k );
00146               return LDAP_DECODING_ERROR;
00147        }
00148 
00149        k->value = result;
00150        k->comp_desc = get_component_description (BASICTYPE_BITSTRING);
00151  
00152        return LDAP_SUCCESS;
00153 }
00154 
00155 /*
00156  * Component GSER BMPString Encoder
00157  */
00158 int
00159 GEncComponentBMPString ( GenBuf *b, ComponentBMPString *in )
00160 {
00161        GBMPString t = {0};
00162 
00163        if ( !in || in->value.octetLen <= 0 )
00164               return (-1);
00165        t.value = in->value;
00166        return GEncBMPStringContent ( b, &t );
00167 }
00168 
00169 /*
00170  * Component GSER BMPString Decoder
00171  */
00172 int
00173 GDecComponentBMPString ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode)
00174 {
00175         char* peek_head;
00176         int i, strLen;
00177         void* component_values;
00178         ComponentBMPString* k, **k2;
00179        GBMPString result;
00180                                                                           
00181         k = (ComponentBMPString*) v;
00182                                                                           
00183         if ( mode & DEC_ALLOC_MODE_0 ) {
00184                 k2 = (ComponentBMPString**) v;
00185                 *k2 = (ComponentBMPString*) CompAlloc( mem_op, sizeof( ComponentBMPString ) );
00186               if ( !*k2 ) return LDAP_DECODING_ERROR;
00187                 k = *k2;
00188         }
00189 
00190         *bytesDecoded = 0;
00191 
00192        if ( GDecBMPStringContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
00193               if ( k ) CompFree( mem_op, k );
00194               return LDAP_DECODING_ERROR;
00195        }
00196 
00197        k->value = result.value;
00198        k->comp_desc = get_component_description (BASICTYPE_BMP_STR);
00199  
00200        return LDAP_SUCCESS;
00201 
00202 }
00203 
00204 /*
00205  * Component BER BMPString Decoder
00206  */
00207 int
00208 BDecComponentBMPStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
00209        return BDecComponentBMPString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
00210 }
00211 
00212 int
00213 BDecComponentBMPString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
00214                      AsnLen *bytesDecoded, int mode )
00215 {
00216         char* peek_head;
00217         int i, strLen, rc;
00218         void* component_values;
00219         ComponentBMPString* k, **k2;
00220        BMPString result;
00221                                                                           
00222         k = (ComponentBMPString*) v;
00223                                                                           
00224         if ( mode & DEC_ALLOC_MODE_0 ) {
00225                 k2 = (ComponentBMPString**) v;
00226                 *k2 = (ComponentBMPString*) CompAlloc( mem_op, sizeof( ComponentBMPString ) );
00227               if ( !*k2 ) return LDAP_DECODING_ERROR;
00228                 k = *k2;
00229         }
00230 
00231        if ( mode & CALL_TAG_DECODER ){
00232               mode = mode & CALL_CONTENT_DECODER;
00233               rc = BDecBMPString ( mem_op, b, &result, bytesDecoded );
00234        } else {
00235               rc = BDecBMPStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
00236        }
00237 
00238        if ( rc < 0 ) {
00239               if ( k ) CompFree( mem_op, k );
00240               return LDAP_DECODING_ERROR;
00241        }
00242 
00243        k->value = result;
00244        k->comp_desc = get_component_description (BASICTYPE_BMP_STR);
00245  
00246        return LDAP_SUCCESS;
00247 
00248 }
00249 
00250 /*
00251  * Component GSER Encoder : UTF8 String
00252  */
00253 int
00254 GEncComponentUTF8String ( GenBuf *b, ComponentUTF8String *in )
00255 {
00256        GUTF8String t = {0};
00257        if ( !in || in->value.octetLen <= 0 )
00258               return (-1);
00259        t.value = in->value;
00260        return GEncUTF8StringContent ( b, &t );
00261 }
00262 
00263 /*
00264  * Component GSER Decoder :  UTF8 String
00265  */
00266 int
00267 GDecComponentUTF8String ( void* mem_op, GenBuf *b, void *v,
00268                             AsnLen *bytesDecoded, int mode) {
00269         char* peek_head;
00270         int i, strLen;
00271         void* component_values;
00272         ComponentUTF8String* k, **k2;
00273        GUTF8String result;
00274                                                                           
00275         k = (ComponentUTF8String*) v;
00276                                                                           
00277         if ( mode & DEC_ALLOC_MODE_0 ) {
00278                 k2 = (ComponentUTF8String**) v;
00279                 *k2 = (ComponentUTF8String*)CompAlloc( mem_op, sizeof( ComponentUTF8String ) );
00280               if ( !*k2 ) return LDAP_DECODING_ERROR;
00281                 k = *k2;
00282         }
00283 
00284         *bytesDecoded = 0;
00285 
00286        if ( GDecUTF8StringContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
00287               if ( k ) CompFree( mem_op,  k );
00288               return LDAP_DECODING_ERROR;
00289        }
00290        
00291        k->value = result.value;
00292        k->comp_desc = get_component_description (BASICTYPE_UTF8_STR);
00293  
00294        return LDAP_SUCCESS;
00295 }
00296 
00297 /*
00298  * Component BER Decoder : UTF8String
00299  */
00300 int
00301 BDecComponentUTF8StringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
00302        return BDecComponentUTF8String ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
00303 }
00304 
00305 int
00306 BDecComponentUTF8String ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len,
00307                             void *v, AsnLen *bytesDecoded, int mode )
00308 {
00309         char* peek_head;
00310         int i, strLen, rc;
00311         void* component_values;
00312         ComponentUTF8String* k, **k2;
00313        UTF8String result;
00314                                                                           
00315         k = (ComponentUTF8String*) v;
00316                                                                           
00317         if ( mode & DEC_ALLOC_MODE_0 ) {
00318                 k2 = (ComponentUTF8String**) v;
00319                 *k2 = (ComponentUTF8String*) CompAlloc( mem_op, sizeof( ComponentUTF8String ) );
00320               if ( !*k2 ) return LDAP_DECODING_ERROR;
00321                 k = *k2;
00322         }
00323        
00324        if ( mode & CALL_TAG_DECODER ){
00325               mode = mode & CALL_CONTENT_DECODER;
00326               rc = BDecUTF8String ( mem_op, b, &result, bytesDecoded );
00327        } else {
00328               rc = BDecUTF8StringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
00329        }
00330        if ( rc < 0 ) {
00331               if ( k ) CompFree( mem_op,  k );
00332               return LDAP_DECODING_ERROR;
00333        }
00334 
00335        k->value = result;
00336        k->comp_desc = get_component_description (BASICTYPE_UTF8_STR);
00337 
00338        return LDAP_SUCCESS;
00339 }
00340 
00341 /*
00342  * Component GSER Encoder :  Teletex String
00343  */
00344 int
00345 GEncComponentTeletexString ( GenBuf *b, ComponentTeletexString *in )
00346 {
00347        GTeletexString t = {0};
00348 
00349        if ( !in || in->value.octetLen <= 0 )
00350               return (-1);
00351        t.value = in->value;
00352        return GEncTeletexStringContent ( b, &t );
00353 }
00354 
00355 /*
00356  * Component GSER Decoder :  Teletex String
00357  */
00358 int
00359 GDecComponentTeletexString  ( void* mem_op, GenBuf *b, void *v,
00360                                    AsnLen *bytesDecoded, int mode) {
00361         char* peek_head;
00362         int i, strLen;
00363         void* component_values;
00364         ComponentTeletexString* k, **k2;
00365        GTeletexString result;
00366                                                                           
00367         k = (ComponentTeletexString*) v;
00368                                                                           
00369         if ( mode & DEC_ALLOC_MODE_0 ) {
00370                 k2 = (ComponentTeletexString**) v;
00371                 *k2 = (ComponentTeletexString*)CompAlloc( mem_op, sizeof( ComponentTeletexString ) );
00372               if ( !*k2 ) return LDAP_DECODING_ERROR;
00373                 k = *k2;
00374         }
00375 
00376         *bytesDecoded = 0;
00377 
00378        if ( GDecTeletexStringContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
00379               if ( k ) CompFree( mem_op,  k );
00380               return LDAP_DECODING_ERROR;
00381        }
00382 
00383        k->value = result.value;
00384        k->comp_desc = get_component_description (BASICTYPE_VIDEOTEX_STR);
00385  
00386        return LDAP_SUCCESS;
00387 }
00388 
00389 
00390 /*
00391  * Matching function : BOOLEAN
00392  */
00393 int
00394 MatchingComponentBool(char* oid, ComponentSyntaxInfo* csi_attr,
00395                         ComponentSyntaxInfo* csi_assert )
00396 {
00397         MatchingRule* mr;
00398         ComponentBool *a, *b;
00399                                                                           
00400         if( oid ) {
00401                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
00402                 if ( mr )
00403                         return component_value_match( mr, csi_attr , csi_assert );
00404         }
00405 
00406         a = ((ComponentBool*)csi_attr);
00407         b = ((ComponentBool*)csi_assert);
00408 
00409         return (a->value == b->value) ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
00410 }
00411 
00412 /*
00413  * GSER Encoder : BOOLEAN
00414  */
00415 int
00416 GEncComponentBool ( GenBuf *b, ComponentBool *in )
00417 {
00418        GAsnBool t = {0};
00419 
00420        if ( !in )
00421               return (-1);
00422        t.value = in->value;
00423        return GEncAsnBoolContent ( b, &t );
00424 }
00425 
00426 /*
00427  * GSER Decoder : BOOLEAN
00428  */
00429 int
00430 GDecComponentBool ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
00431 {
00432         char* peek_head;
00433         int i, strLen;
00434         ComponentBool* k, **k2;
00435        GAsnBool result;
00436                                                                           
00437         k = (ComponentBool*) v;
00438                                                                           
00439         if ( mode & DEC_ALLOC_MODE_0 ) {
00440                 k2 = (ComponentBool**) v;
00441                 *k2 = (ComponentBool*) CompAlloc( mem_op, sizeof( ComponentBool ) );
00442               if ( !*k2 ) return LDAP_DECODING_ERROR;
00443                 k = *k2;
00444         }
00445 
00446        if ( GDecAsnBoolContent( mem_op, b, &result, bytesDecoded ) < 0 ) {
00447               if ( k ) CompFree ( mem_op, k );
00448               return LDAP_DECODING_ERROR;
00449        }
00450 
00451        k->value = result.value;
00452        k->comp_desc = get_component_description (BASICTYPE_BOOLEAN);
00453  
00454         return LDAP_SUCCESS;
00455 }
00456 
00457 /*
00458  * Component BER Decoder : BOOLEAN
00459  */
00460 int
00461 BDecComponentBoolTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
00462        return BDecComponentBool ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
00463 }
00464 
00465 int
00466 BDecComponentBool ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
00467                      AsnLen *bytesDecoded, int mode )
00468 {
00469         char* peek_head;
00470         int i, strLen, rc;
00471         ComponentBool* k, **k2;
00472        AsnBool result;
00473                                                                           
00474         k = (ComponentBool*) v;
00475                                                                           
00476         if ( mode & DEC_ALLOC_MODE_0 ) {
00477                 k2 = (ComponentBool**) v;
00478                 *k2 = (ComponentBool*) CompAlloc( mem_op, sizeof( ComponentBool ) );
00479               if ( !*k2 ) return LDAP_DECODING_ERROR;
00480                 k = *k2;
00481         }
00482 
00483        if ( mode & CALL_TAG_DECODER ){
00484               mode = mode & CALL_CONTENT_DECODER;
00485               rc = BDecAsnBool ( mem_op, b, &result, bytesDecoded );
00486        } else {
00487               rc = BDecAsnBoolContent( mem_op, b, tagId, len, &result, bytesDecoded );
00488        }
00489        if ( rc < 0 ) {
00490               if ( k ) CompFree ( mem_op, k );
00491               return LDAP_DECODING_ERROR;
00492        }
00493 
00494        k->value = result;
00495        k->comp_desc = get_component_description (BASICTYPE_BOOLEAN);
00496 
00497         return LDAP_SUCCESS;
00498 }
00499 
00500 /*
00501  * Matching function : ENUMERATE
00502  */
00503 int
00504 MatchingComponentEnum ( char* oid, ComponentSyntaxInfo *csi_attr,
00505                      ComponentSyntaxInfo *csi_assert )
00506 {
00507         int rc;
00508         MatchingRule* mr;
00509         ComponentEnum *a, *b;
00510                                                                           
00511         if( oid ) {
00512                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
00513                 if ( mr )
00514                         return component_value_match( mr, csi_attr , csi_assert );
00515         }
00516         a = ((ComponentEnum*)csi_attr);
00517         b = ((ComponentEnum*)csi_assert);
00518         rc = (a->value == b->value);
00519                                                                           
00520         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
00521 }
00522 
00523 /*
00524  * GSER Encoder : ENUMERATE
00525  */
00526 int
00527 GEncComponentEnum ( GenBuf *b, ComponentEnum *in )
00528 {
00529        GAsnEnum t = {0};
00530 
00531        if ( !in )
00532               return (-1);
00533        t.value = in->value;
00534        return GEncAsnEnumContent ( b, &t );
00535 }
00536 
00537 /*
00538  * GSER Decoder : ENUMERATE
00539  */
00540 int
00541 GDecComponentEnum ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
00542 {
00543         char* peek_head;
00544         int i, strLen;
00545         void* component_values;
00546         ComponentEnum* k, **k2;
00547        GAsnEnum result;
00548                                                                           
00549         k = (ComponentEnum*) v;
00550                                                                           
00551         if ( mode & DEC_ALLOC_MODE_0 ) {
00552                 k2 = (ComponentEnum**) v;
00553                 *k2 = (ComponentEnum*) CompAlloc( mem_op, sizeof( ComponentEnum ) );
00554               if ( !*k2 ) return LDAP_DECODING_ERROR;
00555                 k = *k2;
00556         }
00557 
00558        if ( GDecAsnEnumContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
00559               if ( k ) CompFree ( mem_op, k );
00560               return LDAP_DECODING_ERROR;
00561        }
00562 
00563        k->value_identifier.bv_val = result.value_identifier;
00564        k->value_identifier.bv_len = result.len;
00565 
00566        k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
00567        if ( !k->comp_desc )  {
00568               if ( k ) CompFree ( mem_op, k );
00569               return LDAP_DECODING_ERROR;
00570        }
00571        k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentEnum;
00572        k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentEnum;
00573        k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentEnum;
00574        k->comp_desc->cd_free = (comp_free_func*)NULL;
00575        k->comp_desc->cd_extract_i = NULL;
00576        k->comp_desc->cd_type = ASN_BASIC;
00577        k->comp_desc->cd_type_id = BASICTYPE_ENUMERATED;
00578        k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentEnum;
00579 
00580        return LDAP_SUCCESS;
00581 }
00582 
00583 /*
00584  * Component BER Decoder : ENUMERATE
00585  */
00586 int
00587 BDecComponentEnumTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
00588        return BDecComponentEnum ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
00589 }
00590 
00591 int
00592 BDecComponentEnum ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
00593                      AsnLen *bytesDecoded, int mode )
00594 {
00595         char* peek_head;
00596         int i, strLen, rc;
00597         void* component_values;
00598         ComponentEnum* k, **k2;
00599        AsnEnum result;
00600                                                                           
00601         k = (ComponentEnum*) v;
00602                                                                           
00603         if ( mode & DEC_ALLOC_MODE_0 ) {
00604                 k2 = (ComponentEnum**) v;
00605                 *k2 = (ComponentEnum*) CompAlloc( mem_op, sizeof( ComponentEnum ) );
00606               if ( k ) return LDAP_DECODING_ERROR;
00607                 k = *k2;
00608         }
00609 
00610        if ( mode & CALL_TAG_DECODER ){
00611               mode = mode & CALL_CONTENT_DECODER;
00612               rc = BDecAsnEnum ( mem_op, b, &result, bytesDecoded );
00613        } else {
00614               rc = BDecAsnEnumContent ( mem_op, b, tagId, len, &result, bytesDecoded );
00615        }
00616        if ( rc < 0 ) {
00617               if ( k ) CompFree ( mem_op, k );
00618               return LDAP_DECODING_ERROR;
00619        }
00620 
00621        k->value = result;
00622 
00623         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
00624        if ( !k->comp_desc )  {
00625               if ( k  ) CompFree ( mem_op, k );
00626               return LDAP_DECODING_ERROR;
00627        }
00628        k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentEnum;
00629         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentEnum;
00630         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentEnum;
00631        k->comp_desc->cd_free = (comp_free_func*)NULL;
00632         k->comp_desc->cd_extract_i = NULL;
00633         k->comp_desc->cd_type = ASN_BASIC;
00634         k->comp_desc->cd_type_id = BASICTYPE_ENUMERATED;
00635         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentEnum;
00636 
00637        return LDAP_SUCCESS;
00638 }
00639 
00640 /*
00641  * Component GSER Encoder : IA5String
00642  */
00643 int
00644 GEncComponentIA5Stirng ( GenBuf *b, ComponentIA5String* in )
00645 {
00646        GIA5String t = {0};
00647        t.value = in->value;
00648        if ( !in || in->value.octetLen <= 0 ) return (-1);
00649        return GEncIA5StringContent( b, &t );
00650 }
00651 
00652 /*
00653  * Component BER Decoder : IA5String
00654  */
00655 int
00656 BDecComponentIA5StringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
00657        return BDecComponentIA5String ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
00658 }
00659 
00660 int
00661 BDecComponentIA5String ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
00662                      AsnLen *bytesDecoded, int mode )
00663 {
00664         char* peek_head;
00665         int i, strLen, rc;
00666         void* component_values;
00667         ComponentIA5String* k, **k2;
00668        IA5String result;
00669                                                                           
00670         k = (ComponentIA5String*) v;
00671                                                                           
00672         if ( mode & DEC_ALLOC_MODE_0 ) {
00673                 k2 = (ComponentIA5String**) v;
00674                 *k2 = (ComponentIA5String*) CompAlloc( mem_op, sizeof( ComponentIA5String ) );
00675               if ( !*k2 ) return LDAP_DECODING_ERROR;
00676                 k = *k2;
00677         }
00678 
00679        if ( mode & CALL_TAG_DECODER ){
00680               mode = mode & CALL_CONTENT_DECODER;
00681               rc = BDecIA5String ( mem_op, b, &result, bytesDecoded );
00682        } else {
00683               rc = BDecIA5StringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
00684        }
00685        if ( rc < 0 ) {
00686               if ( k ) CompFree ( mem_op, k );
00687               return LDAP_DECODING_ERROR;
00688        }
00689 
00690        k->value = result;
00691 
00692         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
00693        if ( !k->comp_desc )  {
00694               if ( k ) CompFree ( mem_op, k );
00695               return LDAP_DECODING_ERROR;
00696        }
00697        k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentIA5String;
00698         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentIA5String;
00699         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentIA5String;
00700        k->comp_desc->cd_free = (comp_free_func*)FreeComponentIA5String;
00701         k->comp_desc->cd_extract_i = NULL;
00702         k->comp_desc->cd_type = ASN_BASIC;
00703         k->comp_desc->cd_type_id = BASICTYPE_IA5_STR;
00704         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentIA5String;
00705 
00706        return LDAP_SUCCESS;
00707 }
00708 
00709 /*
00710  * Matching function : INTEGER
00711  */
00712 int
00713 MatchingComponentInt(char* oid, ComponentSyntaxInfo* csi_attr,
00714                         ComponentSyntaxInfo* csi_assert )
00715 {
00716         MatchingRule* mr;
00717         ComponentInt *a, *b;
00718                                                                           
00719         if( oid ) {
00720                 /* check if this ASN type's matching rule is overrided */
00721                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
00722                 /* if existing function is overrided, call the overriding
00723 function*/
00724                 if ( mr )
00725                         return component_value_match( mr, csi_attr , csi_assert );
00726         }
00727         a = ((ComponentInt*)csi_attr);
00728         b = ((ComponentInt*)csi_assert);
00729                                                                           
00730         return ( a->value == b->value ) ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
00731 }
00732 
00733 /*
00734  * GSER Encoder : INTEGER
00735  */
00736 int
00737 GEncComponentInt ( GenBuf *b, ComponentInt* in )
00738 {
00739        GAsnInt t = {0};
00740 
00741        if ( !in )
00742               return (-1);
00743        t.value = in->value;
00744        return GEncAsnIntContent ( b, &t );
00745 }
00746 
00747 /*
00748  * GSER Decoder : INTEGER 
00749  */
00750 int
00751 GDecComponentInt( void* mem_op, GenBuf * b, void *v, AsnLen *bytesDecoded, int mode)
00752 {
00753         char* peek_head;
00754         int i, strLen;
00755         void* component_values;
00756         ComponentInt* k, **k2;
00757        GAsnInt result;
00758                                                                           
00759         k = (ComponentInt*) v;
00760                                                                           
00761         if ( mode & DEC_ALLOC_MODE_0 ) {
00762                 k2 = (ComponentInt**) v;
00763                 *k2 = (ComponentInt*) CompAlloc( mem_op, sizeof( ComponentInt ) );
00764               if ( !*k2 ) return LDAP_DECODING_ERROR;
00765                 k = *k2;
00766         }
00767 
00768        if ( GDecAsnIntContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
00769               if ( k ) CompFree ( mem_op, k );
00770               return LDAP_DECODING_ERROR;
00771        }
00772        k->value = result.value;
00773        k->comp_desc = get_component_description (BASICTYPE_INTEGER );
00774 
00775         return LDAP_SUCCESS;
00776 }
00777 
00778 /*
00779  * Component BER Decoder : INTEGER 
00780  */
00781 int
00782 BDecComponentIntTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
00783        return BDecComponentInt ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
00784 }
00785 
00786 int
00787 BDecComponentInt ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
00788                      AsnLen *bytesDecoded, int mode )
00789 {
00790         char* peek_head;
00791         int i, strLen, rc;
00792         void* component_values;
00793         ComponentInt* k, **k2;
00794        AsnInt result;
00795                                                                           
00796         k = (ComponentInt*) v;
00797                                                                           
00798         if ( mode & DEC_ALLOC_MODE_0 ) {
00799                 k2 = (ComponentInt**) v;
00800                 *k2 = (ComponentInt*) CompAlloc( mem_op, sizeof( ComponentInt ) );
00801               if ( !*k2 ) return LDAP_DECODING_ERROR;
00802                 k = *k2;
00803         }
00804 
00805        if ( mode & CALL_TAG_DECODER ){
00806               mode = mode & CALL_CONTENT_DECODER;
00807               rc = BDecAsnInt ( mem_op, b, &result, bytesDecoded );
00808        } else {
00809               rc = BDecAsnIntContent ( mem_op, b, tagId, len, &result, bytesDecoded );
00810        }
00811        k->value = result;
00812 
00813        k->comp_desc = get_component_description (BASICTYPE_INTEGER );
00814         
00815         return LDAP_SUCCESS;
00816 }
00817 
00818 /*
00819  * Matching function : NULL
00820  */
00821 int
00822 MatchingComponentNull ( char *oid, ComponentSyntaxInfo *csi_attr,
00823                      ComponentSyntaxInfo *csi_assert )
00824 {
00825         MatchingRule* mr;
00826         ComponentNull *a, *b;
00827                                                                           
00828         if( oid ) {
00829                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
00830                 if ( mr )
00831                         return component_value_match( mr, csi_attr , csi_assert );
00832         }
00833         a = ((ComponentNull*)csi_attr);
00834         b = ((ComponentNull*)csi_assert);
00835                                                                           
00836         return (a->value == b->value) ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
00837 }
00838 
00839 /*
00840  * GSER Encoder : NULL
00841  */
00842 int
00843 GEncComponentNull ( GenBuf *b, ComponentNull *in )
00844 {
00845        GAsnNull t = {0};
00846 
00847        if ( !in )
00848               return (-1);
00849        t.value = in->value;
00850        return GEncAsnNullContent ( b, &t );
00851 }
00852 
00853 /*
00854  * GSER Decoder : NULL
00855  */
00856 int
00857 GDecComponentNull ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
00858 {
00859         char* peek_head;
00860         int i, strLen;
00861         void* component_values;
00862         ComponentNull* k, **k2;
00863        GAsnNull result;
00864                                                                           
00865         k = (ComponentNull*) v;
00866                                                                           
00867         if ( mode & DEC_ALLOC_MODE_0 ) {
00868                 k2 = (ComponentNull**) v;
00869                 *k2 = (ComponentNull*) CompAlloc( mem_op, sizeof( ComponentNull ) );
00870               if ( !*k2 ) return LDAP_DECODING_ERROR;
00871                 k = *k2;
00872         }
00873 
00874        if ( GDecAsnNullContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
00875               if ( k ) CompFree ( mem_op, k );
00876               return LDAP_DECODING_ERROR;
00877        }
00878        k->value = result.value;
00879 
00880        k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
00881        if ( !k->comp_desc )  {
00882               if ( k ) CompFree ( mem_op, k );
00883               return LDAP_DECODING_ERROR;
00884        }
00885        k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentNull;
00886        k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentNull;
00887        k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentNull;
00888        k->comp_desc->cd_free = (comp_free_func*)FreeComponentNull;
00889        k->comp_desc->cd_extract_i = NULL;
00890        k->comp_desc->cd_type = ASN_BASIC;
00891        k->comp_desc->cd_type_id = BASICTYPE_NULL;
00892        k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentNull;
00893 
00894        return LDAP_SUCCESS;
00895 }
00896 
00897 /*
00898  * Component BER Decoder : NULL
00899  */
00900 int
00901 BDecComponentNullTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
00902 {
00903        return BDecComponentNull ( mem_op, b, 0, 0, v,bytesDecoded, mode|CALL_TAG_DECODER );
00904 }
00905 
00906 int
00907 BDecComponentNull ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
00908                      AsnLen *bytesDecoded, int mode )
00909 {
00910         char* peek_head;
00911         int i, strLen, rc;
00912         void* component_values;
00913         ComponentNull* k, **k2;
00914        AsnNull result;
00915 
00916         k = (ComponentNull*) v;
00917                                                                          
00918         if ( mode & DEC_ALLOC_MODE_0 ) {
00919                 k2 = (ComponentNull**) v;
00920                 *k2 = (ComponentNull*) CompAlloc( mem_op, sizeof( ComponentNull ) );
00921               if ( !*k2 ) return LDAP_DECODING_ERROR;
00922                 k = *k2;
00923         }
00924 
00925        if ( mode & CALL_TAG_DECODER ){
00926               mode = mode & CALL_CONTENT_DECODER;
00927               rc = BDecAsnNull ( mem_op, b, &result, bytesDecoded );
00928        }
00929        else {
00930               rc = BDecAsnNullContent ( mem_op, b, tagId, len, &result, bytesDecoded);
00931        }
00932        if ( rc < 0 ) {
00933               if ( k ) CompFree ( mem_op, k );
00934               return LDAP_DECODING_ERROR;
00935        }
00936        k->value = result;
00937 
00938        k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
00939        if ( !k->comp_desc )  {
00940               if ( k ) CompFree ( mem_op, k );
00941               return LDAP_DECODING_ERROR;
00942        }
00943        k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentNull;
00944        k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentNull;
00945        k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentNull;
00946        k->comp_desc->cd_free = (comp_free_func*)FreeComponentNull;
00947        k->comp_desc->cd_extract_i = NULL;
00948        k->comp_desc->cd_type = ASN_BASIC;
00949        k->comp_desc->cd_type_id = BASICTYPE_NULL;
00950        k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentNull;
00951        return LDAP_SUCCESS;
00952 }
00953 
00954 /*
00955  * Component BER Decoder : NumericString
00956  */
00957 int
00958 BDecComponentNumericStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
00959        return BDecComponentNumericString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
00960 }
00961 
00962 int
00963 BDecComponentNumericString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
00964 {
00965         char* peek_head;
00966         int i, strLen, rc;
00967         void* component_values;
00968         ComponentNumericString* k, **k2;
00969        NumericString result;
00970 
00971         k = (ComponentNumericString*) v;
00972                                                                           
00973         if ( mode & DEC_ALLOC_MODE_0 ) {
00974                 k2 = (ComponentNumericString**) v;
00975                 *k2 = (ComponentNumericString*) CompAlloc( mem_op, sizeof( ComponentNumericString ) );
00976               if ( !*k2 ) return LDAP_DECODING_ERROR;
00977                 k = *k2;
00978         }
00979 
00980        if ( mode & CALL_TAG_DECODER ){
00981               mode = mode & CALL_CONTENT_DECODER;
00982               rc = BDecNumericString ( mem_op, b, &result, bytesDecoded );
00983        } else {
00984               rc = BDecNumericStringContent ( mem_op, b, tagId, len, &result, bytesDecoded);
00985        }
00986        if ( rc < 0 ) {
00987               if ( k ) CompFree ( mem_op, k );
00988               return LDAP_DECODING_ERROR;
00989        }
00990        k->value = result;
00991 
00992         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
00993        if ( !k->comp_desc )  {
00994               if ( k ) CompFree ( mem_op, k );
00995               return LDAP_DECODING_ERROR;
00996        }
00997        k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentNumericString;
00998         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentNumericString;
00999         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentNumericString;
01000        k->comp_desc->cd_free = (comp_free_func*)FreeComponentNumericString;
01001         k->comp_desc->cd_extract_i = NULL;
01002         k->comp_desc->cd_type = ASN_BASIC;
01003         k->comp_desc->cd_type_id = BASICTYPE_NUMERIC_STR;
01004         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentNumericString;
01005 
01006        return LDAP_SUCCESS;
01007 }
01008 
01009 
01010 /*
01011  * Free function : OCTET STRING
01012  */
01013 void
01014 FreeComponentOcts ( ComponentOcts* v) {
01015        FreeAsnOcts( &v->value );
01016 }
01017 
01018 /*
01019  * Matching function : OCTET STRING
01020  */
01021 int
01022 MatchingComponentOcts ( char* oid, ComponentSyntaxInfo* csi_attr,
01023                      ComponentSyntaxInfo* csi_assert )
01024 {
01025         int rc;
01026         MatchingRule* mr;
01027         ComponentOcts *a, *b;
01028                                                                           
01029         if( oid ) {
01030                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
01031                 if ( mr )
01032                         return component_value_match( mr, csi_attr , csi_assert );
01033         }
01034         a = (ComponentOcts*) csi_attr;
01035         b = (ComponentOcts*) csi_assert;
01036        /* Assume that both of OCTET string has end of string character */
01037        if ( (a->value.octetLen == b->value.octetLen) &&
01038               strncmp ( a->value.octs, b->value.octs, a->value.octetLen ) == 0 )
01039               return LDAP_COMPARE_TRUE;
01040        else
01041               return LDAP_COMPARE_FALSE;
01042 }
01043 
01044 /*
01045  * GSER Encoder : OCTET STRING
01046  */
01047 int
01048 GEncComponentOcts ( GenBuf* b, ComponentOcts *in )
01049 {
01050        GAsnOcts t = {0};
01051        if ( !in || in->value.octetLen <= 0 )
01052               return (-1);
01053 
01054        t.value = in->value;
01055        return GEncAsnOctsContent ( b, &t );
01056 }
01057 
01058 /*
01059  * GSER Decoder : OCTET STRING
01060  */
01061 int
01062 GDecComponentOcts ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
01063 {
01064         char *peek_head, *data;
01065         int i, j, strLen;
01066         void* component_values;
01067         ComponentOcts* k, **k2;
01068        GAsnOcts result;
01069                                                                           
01070         k = (ComponentOcts*) v;
01071                                                                           
01072         if ( mode & DEC_ALLOC_MODE_0 ) {
01073                 k2 = (ComponentOcts**) v;
01074                 *k2 = (ComponentOcts*) CompAlloc( mem_op, sizeof( ComponentOcts ) );
01075               if ( !*k2 ) return LDAP_DECODING_ERROR;
01076                 k = *k2;
01077         }
01078 
01079        if ( GDecAsnOctsContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
01080               if ( k ) CompFree ( mem_op, k );
01081               return LDAP_DECODING_ERROR;
01082        }
01083        k->value = result.value;
01084 
01085        k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
01086        if ( !k->comp_desc )  {
01087               if ( k ) CompFree ( mem_op, k );
01088               return LDAP_DECODING_ERROR;
01089        }
01090        k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentOcts;
01091        k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentOcts;
01092        k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentOcts;
01093        k->comp_desc->cd_free = (comp_free_func*)FreeComponentOcts;
01094        k->comp_desc->cd_extract_i = NULL;
01095        k->comp_desc->cd_type = ASN_BASIC;
01096        k->comp_desc->cd_type_id = BASICTYPE_OCTETSTRING;
01097        k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentOcts;
01098 
01099        return LDAP_SUCCESS;
01100 }
01101 
01102 /*
01103  * Component BER Decoder : OCTET STRING
01104  */
01105 int
01106 BDecComponentOctsTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
01107        return BDecComponentOcts ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
01108 }
01109 
01110 int
01111 BDecComponentOcts ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
01112                      AsnLen *bytesDecoded, int mode )
01113 {
01114         char *peek_head, *data;
01115         int i, strLen, rc;
01116         void* component_values;
01117         ComponentOcts* k, **k2;
01118        AsnOcts result;
01119                                                                           
01120         k = (ComponentOcts*) v;
01121                                                                           
01122         if ( mode & DEC_ALLOC_MODE_0 ) {
01123                 k2 = (ComponentOcts**) v;
01124                 *k2 = (ComponentOcts*) CompAlloc( mem_op, sizeof( ComponentOcts ) );
01125               if ( !*k2 ) return LDAP_DECODING_ERROR;
01126                 k = *k2;
01127         }
01128 
01129        if ( mode & CALL_TAG_DECODER ){
01130               mode = mode & CALL_CONTENT_DECODER;
01131               rc = BDecAsnOcts ( mem_op, b, &result, bytesDecoded );
01132        } else {
01133               rc = BDecAsnOctsContent ( mem_op, b, tagId, len, &result, bytesDecoded );
01134        }
01135        if ( rc < 0 ) {
01136               if ( k ) CompFree ( mem_op, k );
01137               return LDAP_DECODING_ERROR;
01138        }
01139        k->value = result;
01140 
01141         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
01142        if ( !k->comp_desc )  {
01143               if ( k ) CompFree ( mem_op, k );
01144               return LDAP_DECODING_ERROR;
01145        }
01146        k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentOcts;
01147         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentOcts;
01148         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentOcts;
01149        k->comp_desc->cd_free = (comp_free_func*)FreeComponentOcts;
01150         k->comp_desc->cd_extract_i = NULL;
01151         k->comp_desc->cd_type = ASN_BASIC;
01152         k->comp_desc->cd_type_id = BASICTYPE_OCTETSTRING;
01153         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentOcts;
01154        return LDAP_SUCCESS;
01155 }
01156 
01157 /*
01158  * Matching function : OBJECT IDENTIFIER
01159  */
01160 int
01161 MatchingComponentOid ( char *oid, ComponentSyntaxInfo *csi_attr ,
01162                      ComponentSyntaxInfo *csi_assert )
01163 {
01164         int rc;
01165         MatchingRule* mr;
01166         ComponentOid *a, *b;
01167                                                                           
01168         if( oid ) {
01169                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
01170                 if ( mr )
01171                         return component_value_match( mr, csi_attr , csi_assert );
01172         }
01173 
01174         a = (ComponentOid*)csi_attr;
01175         b = (ComponentOid*)csi_assert;
01176        if ( a->value.octetLen != b->value.octetLen )
01177               return LDAP_COMPARE_FALSE;
01178         rc = ( strncmp( a->value.octs, b->value.octs, a->value.octetLen ) == 0 );
01179                                                                           
01180         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
01181 }
01182 
01183 /*
01184  * GSER Encoder : OID
01185  */
01186 GEncComponentOid ( GenBuf *b, ComponentOid *in )
01187 {
01188        GAsnOid t = {0};
01189 
01190        if ( !in || in->value.octetLen <= 0 )
01191               return (-1);
01192        t.value = in->value;
01193        return GEncAsnOidContent( b, (GAsnOcts*)&t );
01194 }
01195 
01196 /*
01197  * GSER Decoder : OID
01198  */
01199 int
01200 GDecAsnDescOidContent ( void* mem_op, GenBuf *b, GAsnOid *result, AsnLen *bytesDecoded ){
01201        AttributeType *ad_type;
01202        struct berval name;
01203        char* peek_head;
01204        int strLen;
01205 
01206        strLen = LocateNextGSERToken ( mem_op, b, &peek_head, GSER_NO_COPY );
01207        name.bv_val = peek_head;
01208        name.bv_len = strLen;
01209 
01210        ad_type = at_bvfind( &name );
01211 
01212        if ( !ad_type )
01213               return LDAP_DECODING_ERROR;
01214 
01215        peek_head = ad_type->sat_atype.at_oid;
01216        strLen = strlen ( peek_head );
01217 
01218        result->value.octs = (char*)EncodeComponentOid ( mem_op, peek_head , &strLen );
01219        result->value.octetLen = strLen;
01220        return LDAP_SUCCESS;
01221 }
01222 
01223 int
01224 IsNumericOid ( char* peek_head , int strLen ) {
01225        int i;
01226        int num_dot;
01227        for ( i = 0, num_dot = 0 ; i < strLen ; i++ ) {
01228               if ( peek_head[i] == '.' ) num_dot++;
01229               else if ( peek_head[i] > '9' || peek_head[i] < '0' )
01230                      return (-1);
01231        }
01232        if ( num_dot )
01233               return (1);
01234        else
01235               return (-1);
01236 }
01237 
01238 int
01239 GDecComponentOid ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
01240 {
01241         char* peek_head;
01242         int i, strLen, rc;
01243         void* component_values;
01244         ComponentOid* k, **k2;
01245        GAsnOid result;
01246                                                                           
01247         k = (ComponentOid*) v;
01248                                                                           
01249         if ( mode & DEC_ALLOC_MODE_0 ) {
01250                 k2 = (ComponentOid**) v;
01251                 *k2 = (ComponentOid*) CompAlloc( mem_op, sizeof( ComponentOid ) );
01252               if ( !*k2 ) return LDAP_DECODING_ERROR;
01253                 k = *k2;
01254         }
01255 
01256        strLen = LocateNextGSERToken ( mem_op, b, &peek_head, GSER_PEEK );
01257        if ( IsNumericOid ( peek_head , strLen ) >= 1 ) {
01258               /* numeric-oid */
01259               if ( GDecAsnOidContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
01260                      if ( k ) CompFree ( mem_op, k );
01261                      return LDAP_DECODING_ERROR;
01262               }
01263        }
01264        else {
01265               /*descr*/
01266               if ( GDecAsnDescOidContent ( mem_op, b, &result, bytesDecoded ) < 0 ){
01267                      if ( k ) CompFree ( mem_op, k );
01268                      return LDAP_DECODING_ERROR;
01269               }
01270        }
01271        k->value = result.value;
01272        k->comp_desc = get_component_description (BASICTYPE_OID);
01273 
01274        return LDAP_SUCCESS;
01275 }
01276 
01277 /*
01278  * Component BER Decoder : OID
01279  */
01280 int
01281 BDecComponentOidTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
01282        return BDecComponentOid ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
01283 }
01284 
01285 int
01286 BDecComponentOid ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
01287                      AsnLen *bytesDecoded, int mode )
01288 {
01289         char* peek_head;
01290         int i, strLen, rc;
01291         void* component_values;
01292         ComponentOid* k, **k2;
01293        AsnOid result;
01294                                                                           
01295         k = (ComponentOid*) v;
01296                                                                           
01297         if ( mode & DEC_ALLOC_MODE_0 ) {
01298                 k2 = (ComponentOid**) v;
01299                 *k2 = (ComponentOid*) CompAlloc( mem_op, sizeof( ComponentOid ) );
01300               if ( !*k2 ) return LDAP_DECODING_ERROR;
01301                 k = *k2;
01302         }
01303        
01304        if ( mode & CALL_TAG_DECODER ){
01305               mode = mode & CALL_CONTENT_DECODER;
01306               rc = BDecAsnOid ( mem_op, b, &result, bytesDecoded );
01307        } else {
01308               rc = BDecAsnOidContent ( mem_op, b, tagId, len, &result, bytesDecoded );
01309        }
01310        if ( rc < 0 ) {
01311               if ( k ) CompFree ( mem_op, k );
01312               return LDAP_DECODING_ERROR;
01313        }
01314        k->value = result;
01315 
01316        k->comp_desc = get_component_description (BASICTYPE_OID);
01317 
01318        return LDAP_SUCCESS;
01319 }
01320 
01321 /*
01322  * Component BER Decoder : PrintiableString
01323  */
01324 
01325 int
01326 BDecComponentPrintableStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
01327 {
01328        return BDecComponentPrintableString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
01329 }
01330 
01331 int
01332 BDecComponentPrintableString( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
01333 {
01334         char* peek_head;
01335         int i, strLen, rc;
01336         void* component_values;
01337         ComponentPrintableString* k, **k2;
01338        AsnOid result;
01339                                                                           
01340         k = (ComponentPrintableString*) v;
01341                                                                           
01342         if ( mode & DEC_ALLOC_MODE_0 ) {
01343                 k2 = (ComponentPrintableString**) v;
01344                 *k2 = (ComponentPrintableString*) CompAlloc( mem_op, sizeof( ComponentPrintableString ) );
01345               if ( !*k2 ) return LDAP_DECODING_ERROR;
01346                 k = *k2;
01347         }
01348 
01349        if ( mode & CALL_TAG_DECODER ) {
01350               mode = mode & CALL_CONTENT_DECODER;
01351               rc = BDecPrintableString ( mem_op, b, &result, bytesDecoded );
01352        } else {
01353               rc = BDecPrintableStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
01354        }
01355        if ( rc < 0 ) {
01356               if ( k ) CompFree ( mem_op, k );
01357               return LDAP_DECODING_ERROR;
01358        }
01359        k->value = result;
01360 
01361        k->comp_desc = get_component_description (BASICTYPE_PRINTABLE_STR);
01362 
01363        return LDAP_SUCCESS;
01364 }
01365 
01366 /*
01367  * Component BER Decoder : TeletexString
01368  */
01369 
01370 int
01371 BDecComponentTeletexStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
01372 {
01373        return BDecComponentTeletexString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
01374 }
01375 
01376 int
01377 BDecComponentTeletexString( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
01378 {
01379         char* peek_head;
01380         int i, strLen, rc;
01381         void* component_values;
01382         ComponentTeletexString* k, **k2;
01383        AsnOid result;
01384                                                                           
01385         k = (ComponentTeletexString*) v;
01386                                                                           
01387         if ( mode & DEC_ALLOC_MODE_0 ) {
01388                 k2 = (ComponentTeletexString**) v;
01389                 *k2 = (ComponentTeletexString*) CompAlloc( mem_op, sizeof( ComponentTeletexString ) );
01390               if ( !*k2 ) return LDAP_DECODING_ERROR;
01391                 k = *k2;
01392         }
01393 
01394        if ( mode & CALL_TAG_DECODER ) {
01395               mode = mode & CALL_CONTENT_DECODER;
01396               rc = BDecTeletexString ( mem_op, b, &result, bytesDecoded );
01397        } else {
01398               rc = BDecTeletexStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
01399        }
01400        if ( rc < 0 ) {
01401               if ( k ) CompFree ( mem_op, k );
01402               return LDAP_DECODING_ERROR;
01403        }
01404        k->value = result;
01405 
01406        k->comp_desc = get_component_description (BASICTYPE_T61_STR);
01407 
01408        return LDAP_SUCCESS;
01409 }
01410 
01411 
01412 /*
01413  * Matching function : Real
01414  */
01415 int
01416 MatchingComponentReal (char* oid, ComponentSyntaxInfo *csi_attr,
01417                      ComponentSyntaxInfo *csi_assert )
01418 {
01419         int rc;
01420         MatchingRule* mr;
01421         ComponentReal *a, *b;
01422                                                                           
01423         if( oid ) {
01424                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
01425                 if ( mr )
01426                         return component_value_match( mr, csi_attr , csi_assert );
01427         }
01428         a = (ComponentReal*)csi_attr;
01429         b = (ComponentReal*)csi_assert;
01430         rc = (a->value == b->value);
01431                                                                           
01432         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
01433 }
01434 
01435 /*
01436  * GSER Encoder : Real
01437  */
01438 int
01439 GEncComponentReal ( GenBuf *b, ComponentReal *in )
01440 {
01441        GAsnReal t = {0};
01442        if ( !in )
01443               return (-1);
01444        t.value = in->value;
01445        return GEncAsnRealContent ( b, &t );
01446 }
01447 
01448 /*
01449  * GSER Decoder : Real
01450  */
01451 int
01452 GDecComponentReal ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
01453 {
01454         char* peek_head;
01455         int i, strLen;
01456         void* component_values;
01457         ComponentReal* k, **k2;
01458        GAsnReal result;
01459                                                                           
01460         k = (ComponentReal*) v;
01461                                                                           
01462         if ( mode & DEC_ALLOC_MODE_0 ) {
01463                 k2 = (ComponentReal**) v;
01464                 *k2 = (ComponentReal*) CompAlloc( mem_op, sizeof( ComponentReal ) );
01465               if ( !*k2 ) return LDAP_DECODING_ERROR;
01466                 k = *k2;
01467         }
01468 
01469        if ( GDecAsnRealContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
01470               if ( k ) CompFree ( mem_op, k );
01471               return LDAP_DECODING_ERROR;
01472        }
01473        k->value = result.value;
01474        k->comp_desc = get_component_description (BASICTYPE_REAL);
01475 
01476         return LDAP_SUCCESS;
01477 }
01478 
01479 /*
01480  * Component BER Decoder : Real
01481  */
01482 int
01483 BDecComponentRealTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
01484        return BDecComponentReal ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
01485 }
01486 
01487 int
01488 BDecComponentReal ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
01489 {
01490         char* peek_head;
01491         int i, strLen, rc;
01492         void* component_values;
01493         ComponentReal* k, **k2;
01494        AsnReal result;
01495                                                                           
01496         k = (ComponentReal*) v;
01497                                                                           
01498         if ( mode & DEC_ALLOC_MODE_0 ) {
01499                 k2 = (ComponentReal**) v;
01500                 *k2 = (ComponentReal*) CompAlloc( mem_op, sizeof( ComponentReal ) );
01501               if ( !*k2 ) return LDAP_DECODING_ERROR;
01502                 k = *k2;
01503         }
01504 
01505        if ( mode & CALL_TAG_DECODER ){
01506               mode = mode & CALL_CONTENT_DECODER;
01507               rc = BDecAsnReal ( mem_op, b, &result, bytesDecoded );
01508        } else {
01509               rc = BDecAsnRealContent ( mem_op, b, tagId, len, &result, bytesDecoded );
01510        }
01511        if ( rc < 0 ) {
01512               if ( k ) CompFree ( mem_op, k );
01513               return LDAP_DECODING_ERROR;
01514        }
01515        k->value = result;
01516        k->comp_desc = get_component_description (BASICTYPE_REAL);
01517 
01518         return LDAP_SUCCESS;
01519 }
01520 
01521 /*
01522  * Matching function : Relative OID
01523  */
01524 int
01525 MatchingComponentRelativeOid ( char* oid, ComponentSyntaxInfo *csi_attr,
01526                                    ComponentSyntaxInfo *csi_assert )
01527 {
01528         int rc;
01529         MatchingRule* mr;
01530         ComponentRelativeOid *a, *b;
01531                                                                           
01532         if( oid ) {
01533                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
01534                 if ( mr )
01535                         return component_value_match( mr, csi_attr , csi_assert );
01536         }
01537 
01538         a = (ComponentRelativeOid*)csi_attr;
01539         b = (ComponentRelativeOid*)csi_assert;
01540 
01541        if ( a->value.octetLen != b->value.octetLen )
01542               return LDAP_COMPARE_FALSE;
01543         rc = ( strncmp( a->value.octs, b->value.octs, a->value.octetLen ) == 0 );
01544                                                                           
01545         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
01546 }
01547 
01548 /*
01549  * GSER Encoder : RELATIVE_OID.
01550  */
01551 int
01552 GEncComponentRelativeOid ( GenBuf *b, ComponentRelativeOid *in )
01553 {
01554        GAsnRelativeOid t = {0};
01555 
01556        if ( !in || in->value.octetLen <= 0 )
01557               return (-1);
01558        t.value = in->value;
01559        return GEncAsnRelativeOidContent ( b , (GAsnOcts*)&t );
01560 }
01561 
01562 /*
01563  * GSER Decoder : RELATIVE_OID.
01564  */
01565 int
01566 GDecComponentRelativeOid ( void* mem_op, GenBuf *b,void *v, AsnLen *bytesDecoded, int mode )
01567 {
01568         char* peek_head;
01569         int i, strLen;
01570         void* component_values;
01571         ComponentRelativeOid* k, **k2;
01572        GAsnRelativeOid result;
01573                                                                           
01574         k = (ComponentRelativeOid*) v;
01575                                                                           
01576         if ( mode & DEC_ALLOC_MODE_0 ) {
01577                 k2 = (ComponentRelativeOid**) v;
01578                 *k2 = (ComponentRelativeOid*) CompAlloc( mem_op, sizeof( ComponentRelativeOid ) );
01579               if ( !*k2 ) return LDAP_DECODING_ERROR;
01580                 k = *k2;
01581         }
01582        
01583        if ( GDecAsnRelativeOidContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
01584               if ( k ) CompFree ( mem_op, k );
01585               return LDAP_DECODING_ERROR;
01586        }
01587        k->value = result.value;
01588        k->comp_desc = get_component_description (BASICTYPE_OID);
01589 
01590        return LDAP_SUCCESS;
01591 }
01592 
01593 /*
01594  * Component BER Decoder : RELATIVE_OID.
01595  */
01596 int
01597 BDecComponentRelativeOidTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
01598        return BDecComponentRelativeOid ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
01599 }
01600 
01601 int
01602 BDecComponentRelativeOid ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
01603 {
01604         char* peek_head;
01605         int i, strLen, rc;
01606         void* component_values;
01607         ComponentRelativeOid* k, **k2;
01608        AsnRelativeOid result;
01609                                                                           
01610         k = (ComponentRelativeOid*) v;
01611                                                                           
01612         if ( mode & DEC_ALLOC_MODE_0 ) {
01613                 k2 = (ComponentRelativeOid**) v;
01614                 *k2 = (ComponentRelativeOid*) CompAlloc( mem_op, sizeof( ComponentRelativeOid ) );
01615               if ( !*k2 ) return LDAP_DECODING_ERROR;
01616                 k = *k2;
01617         }
01618        
01619        if ( mode & CALL_TAG_DECODER ){
01620               mode = mode & CALL_CONTENT_DECODER;
01621               rc = BDecAsnRelativeOid ( mem_op, b, &result, bytesDecoded );
01622        } else {
01623               rc = BDecAsnRelativeOidContent ( mem_op, b, tagId, len, &result, bytesDecoded );
01624        }
01625        if ( rc < 0 ) {
01626               if ( k ) CompFree ( mem_op, k );
01627               return LDAP_DECODING_ERROR;
01628        }
01629        k->value = result;
01630        k->comp_desc = get_component_description (BASICTYPE_OID);
01631 
01632        return LDAP_SUCCESS;
01633 }
01634 
01635 /*
01636  * GSER Encoder : UniversalString
01637  */
01638 int
01639 GEncComponentUniversalString ( GenBuf *b, ComponentUniversalString *in )
01640 {
01641        GUniversalString t = {0};
01642        if ( !in || in->value.octetLen <= 0 )
01643               return (-1);
01644        t.value = in->value;
01645        return GEncUniversalStringContent( b, &t );
01646 }
01647 
01648 /*
01649  * GSER Decoder : UniversalString
01650  */
01651 static int
01652 UTF8toUniversalString( char* octs, int len){
01653        /* Need to be Implemented */
01654        return LDAP_SUCCESS;
01655 }
01656 
01657 int
01658 GDecComponentUniversalString ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
01659 {
01660        if ( GDecComponentUTF8String ( mem_op, b, v, bytesDecoded, mode) < 0 )
01661        UTF8toUniversalString( ((ComponentUniversalString*)v)->value.octs, ((ComponentUniversalString*)v)->value.octetLen );
01662               return LDAP_DECODING_ERROR;
01663 }
01664 
01665 /*
01666  * Component BER Decoder : UniverseString
01667  */
01668 int
01669 BDecComponentUniversalStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
01670        return BDecComponentUniversalString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
01671 }
01672 
01673 int
01674 BDecComponentUniversalString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
01675 {
01676         char* peek_head;
01677         int i, strLen, rc;
01678         void* component_values;
01679         ComponentUniversalString* k, **k2;
01680        UniversalString result;
01681 
01682         k = (ComponentUniversalString*) v;
01683 
01684         if ( mode & DEC_ALLOC_MODE_0 ) {
01685                 k2 = (ComponentUniversalString**) v;
01686                 *k2 = (ComponentUniversalString*) CompAlloc( mem_op, sizeof( ComponentUniversalString ) );
01687               if ( !*k2 ) return LDAP_DECODING_ERROR;
01688                 k = *k2;
01689         }
01690        
01691        if ( mode & CALL_TAG_DECODER ){
01692               mode = mode & CALL_CONTENT_DECODER;
01693               rc = BDecUniversalString ( mem_op, b, &result, bytesDecoded );
01694        } else {
01695               rc = BDecUniversalStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
01696        }
01697        if ( rc < 0 ) {
01698               if ( k ) CompFree ( mem_op, k );
01699               return LDAP_DECODING_ERROR;
01700        }
01701        k->value = result;
01702        k->comp_desc = get_component_description (BASICTYPE_UNIVERSAL_STR);
01703 
01704        return LDAP_SUCCESS;
01705 }
01706 
01707 /*
01708  * Component BER Decoder : VisibleString
01709  */
01710 int
01711 BDecComponentVisibleStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
01712        return BDecComponentVisibleString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
01713 }
01714 
01715 int
01716 BDecComponentVisibleString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
01717 {
01718         char* peek_head;
01719         int i, strLen, rc;
01720         void* component_values;
01721         ComponentVisibleString* k, **k2;
01722        VisibleString result;
01723                                                                           
01724         k = (ComponentVisibleString*) v;
01725                                                                           
01726         if ( mode & DEC_ALLOC_MODE_0 ) {
01727                 k2 = (ComponentVisibleString**) v;
01728                 *k2 = (ComponentVisibleString*) CompAlloc( mem_op, sizeof( ComponentVisibleString ) );
01729               if ( !*k2 ) return LDAP_DECODING_ERROR;
01730                 k = *k2;
01731         }
01732        
01733        if ( mode & CALL_TAG_DECODER ){
01734               mode = mode & CALL_CONTENT_DECODER;
01735               rc = BDecVisibleString ( mem_op, b, &result, bytesDecoded );
01736        } else {
01737               rc = BDecVisibleStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
01738        }
01739        k->value = result;
01740        k->comp_desc = get_component_description (BASICTYPE_VISIBLE_STR);
01741 
01742        return LDAP_SUCCESS;
01743 }
01744 
01745 /*
01746  * Routines for handling an ANY DEFINED Type
01747  */
01748 
01749 /* Check if the <select> type CR and the OID of the given ANY type */
01750 int
01751 CheckSelectTypeCorrect ( void* mem_op, ComponentAnyInfo* cai, struct berval* select ) {
01752        int strLen;
01753        AttributeType* ad_type;
01754        char* oid;
01755        char* result;
01756 
01757        if ( IsNumericOid ( select->bv_val , select->bv_len ) ) {
01758               oid = select->bv_val;
01759               strLen = select->bv_len;
01760        } else {
01761               ad_type = at_bvfind( select );
01762 
01763               if ( !ad_type )
01764                      return LDAP_DECODING_ERROR;
01765 
01766               oid = ad_type->sat_atype.at_oid;
01767               strLen = strlen ( oid );
01768        }
01769        result = EncodeComponentOid ( mem_op, oid , &strLen );
01770        if ( !result || strLen <= 0 ) return (-1);
01771 
01772        if ( cai->oid.octetLen == strLen &&
01773               strncmp ( cai->oid.octs, result, strLen ) == 0 )
01774               return (1);
01775        else
01776               return (-1);
01777 }
01778 
01779 int
01780 SetAnyTypeByComponentOid ( ComponentAny *v, ComponentOid *id ) {
01781        Hash hash;
01782        void *anyInfo;
01783 
01784        /* use encoded oid as hash string */
01785        hash = MakeHash (id->value.octs, id->value.octetLen);
01786        if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
01787               v->cai = (ComponentAnyInfo*) anyInfo;
01788        else
01789               v->cai = NULL;
01790 
01791        if ( !v->cai ) {
01792        /*
01793         * If not found, the data considered as octet chunk
01794         * Yet-to-be-Implemented
01795         */
01796        }
01797        return LDAP_SUCCESS;
01798 }
01799 
01800 void
01801 SetAnyTypeByComponentInt( ComponentAny *v, ComponentInt id) {
01802        Hash hash;
01803        void *anyInfo;
01804 
01805        hash = MakeHash ((char*)&id, sizeof (id));
01806        if (CheckForAndReturnValue (anyIntHashTblG, hash, &anyInfo))
01807               v->cai = (ComponentAnyInfo*) anyInfo;
01808        else
01809               v->cai = NULL;
01810 }
01811 
01812 int
01813 GEncComponentAny ( GenBuf *b, ComponentAny *in )
01814 {
01815        if ( in->cai != NULL  && in->cai->Encode != NULL )
01816               return in->cai->Encode(b, &in->value );
01817        else
01818               return (-1);
01819 }
01820 
01821 int
01822 BEncComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode)
01823 {
01824         ComponentAny *k, **k2;
01825                                                                           
01826         k = (ComponentAny*) result;
01827 
01828        if ( !k ) return (-1);
01829                                                                           
01830         if ( mode & DEC_ALLOC_MODE_0 ) {
01831                 k2 = (ComponentAny**) result;
01832                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
01833               if ( !*k2 ) return LDAP_DECODING_ERROR;
01834                 k = *k2;
01835         }
01836        
01837        if ((result->cai != NULL) && (result->cai->BER_Decode != NULL)) {
01838               result->value = (void*) CompAlloc ( mem_op, result->cai->size );
01839               if ( !result->value ) return 0;
01840               result->cai->BER_Decode ( mem_op, b, result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_1);
01841 
01842               k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
01843               if ( !k->comp_desc )  {
01844                      if ( k ) CompFree ( mem_op, k );
01845                      return LDAP_DECODING_ERROR;
01846               }
01847               k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
01848               k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
01849               k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
01850               k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
01851               k->comp_desc->cd_extract_i = NULL;
01852               k->comp_desc->cd_type = ASN_BASIC;
01853               k->comp_desc->cd_type_id = BASICTYPE_ANY;
01854               k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
01855               return LDAP_SUCCESS;
01856        }
01857        else {
01858               Asn1Error ("ERROR - Component ANY Decode routine is NULL\n");
01859               return 0;
01860        }
01861 }
01862 
01863 int
01864 BDecComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode) {
01865        int rc;
01866         ComponentAny *k, **k2;
01867                                                                           
01868         k = (ComponentAny*) result;
01869 
01870        if ( !k ) return (-1);
01871                                                                           
01872         if ( mode & DEC_ALLOC_MODE_0 ) {
01873                 k2 = (ComponentAny**) result;
01874                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
01875               if ( !*k2 ) return LDAP_DECODING_ERROR;
01876                 k = *k2;
01877         }
01878        
01879        if ((result->cai != NULL) && (result->cai->BER_Decode != NULL)) {
01880               result->cai->BER_Decode ( mem_op, b, (ComponentSyntaxInfo*)&result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_0 );
01881 
01882               k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
01883               if ( !k->comp_desc )  {
01884                      if ( k ) CompFree ( mem_op, k );
01885                      return LDAP_DECODING_ERROR;
01886               }
01887               k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
01888               k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
01889               k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
01890               k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
01891               k->comp_desc->cd_extract_i = NULL;
01892               k->comp_desc->cd_type = ASN_BASIC;
01893               k->comp_desc->cd_type_id = BASICTYPE_ANY;
01894               k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
01895               return LDAP_SUCCESS;
01896        }
01897        else {
01898               Asn1Error ("ERROR - Component ANY Decode routine is NULL\n");
01899               return 0;
01900        }
01901 }
01902 
01903 int
01904 GDecComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode) {
01905         ComponentAny *k, **k2;
01906                                                                           
01907         k = (ComponentAny*) result;
01908                                                                           
01909         if ( mode & DEC_ALLOC_MODE_0 ) {
01910                 k2 = (ComponentAny**) result;
01911                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
01912               if ( !*k2 ) return LDAP_DECODING_ERROR;
01913                 k = *k2;
01914         }
01915        if ((result->cai != NULL) && (result->cai->GSER_Decode != NULL)) {
01916               result->value = (void*) CompAlloc ( mem_op, result->cai->size );
01917               if ( !result->value ) return 0;
01918               result->cai->GSER_Decode ( mem_op, b, result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_1);
01919               k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
01920               if ( !k->comp_desc )  {
01921                      if ( k ) CompFree ( mem_op, k );
01922                      return LDAP_DECODING_ERROR;
01923               }
01924               k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
01925               k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
01926               k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
01927               k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
01928               k->comp_desc->cd_type = ASN_BASIC;
01929               k->comp_desc->cd_extract_i = NULL;
01930               k->comp_desc->cd_type_id = BASICTYPE_ANY;
01931               k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
01932               return LDAP_SUCCESS;
01933        }
01934        else {
01935               Asn1Error ("ERROR - ANY Decode routine is NULL\n");
01936               return 0;
01937        }
01938 }
01939 
01940 int
01941 MatchingComponentAny (char* oid, ComponentAny *result, ComponentAny *result2) {
01942        void *comp1, *comp2;
01943 
01944        if ( result->comp_desc->cd_type_id == BASICTYPE_ANY )
01945               comp1 = result->value;
01946        else
01947               comp1 = result;
01948 
01949        if ( result2->comp_desc->cd_type_id == BASICTYPE_ANY )
01950               comp2 = result2->value;
01951        else
01952               comp2 = result2;
01953               
01954        if ((result->cai != NULL) && (result->cai->Match != NULL)) {
01955               if ( result->comp_desc->cd_type_id == BASICTYPE_ANY )
01956                      return result->cai->Match(oid, comp1, comp2 );
01957               else if ( result2->comp_desc->cd_type_id == BASICTYPE_ANY )
01958                      return result2->cai->Match(oid, comp1, comp2);
01959               else 
01960                      return LDAP_INVALID_SYNTAX;
01961        }
01962        else {
01963               Asn1Error ("ERROR - ANY Matching routine is NULL\n");
01964               return LDAP_INVALID_SYNTAX;
01965        }
01966 }
01967 
01968 void*
01969 ExtractingComponentAny ( void* mem_op, ComponentReference* cr,  ComponentAny *result ) {
01970        if ((result->cai != NULL) && (result->cai->Extract != NULL)) {
01971               return (void*) result->cai->Extract( mem_op, cr , result->value );
01972        }
01973        else {
01974               Asn1Error ("ERROR - ANY Extracting routine is NULL\n");
01975               return (void*)NULL;
01976        }
01977 }
01978 
01979 void
01980 FreeComponentAny (ComponentAny* any) {
01981        if ( any->cai != NULL && any->cai->Free != NULL ) {
01982               any->cai->Free( any->value );
01983               free ( ((ComponentSyntaxInfo*)any->value)->csi_comp_desc );
01984               free ( any->value );
01985        }
01986        else
01987               Asn1Error ("ERROR - ANY Free routine is NULL\n");
01988 }
01989 
01990 void
01991 InstallAnyByComponentInt (int anyId, ComponentInt intId, unsigned int size,
01992                      EncodeFcn encode, gser_decoder_func* G_decode,
01993                      ber_tag_decoder_func* B_decode, ExtractFcn extract,
01994                      MatchFcn match, FreeFcn free,
01995                      PrintFcn print)
01996 {
01997        ComponentAnyInfo *a;
01998        Hash h;
01999 
02000        a = (ComponentAnyInfo*) malloc(sizeof (ComponentAnyInfo));
02001        a->anyId = anyId;
02002        a->oid.octs = NULL;
02003        a->oid.octetLen = 0;
02004        a->intId = intId;
02005        a->size = size;
02006        a->Encode = encode;
02007        a->GSER_Decode = G_decode;
02008        a->BER_Decode = B_decode;
02009        a->Match = match;
02010        a->Extract = extract;
02011        a->Free = free;
02012        a->Print = print;
02013 
02014        if (anyIntHashTblG == NULL)
02015               anyIntHashTblG = InitHash();
02016 
02017        h = MakeHash ((char*)&intId, sizeof (intId));
02018 
02019        if(anyIntHashTblG != NULL)
02020               Insert(anyIntHashTblG, a, h);
02021 }
02022 
02023 
02024 /*
02025  * OID and its corresponding decoder can be registerd with this func.
02026  * If contained types constrained by <select> are used,
02027  * their OID and decoder MUST be registered, otherwise it will return no entry.
02028  * An open type(ANY type) also need be registered.
02029  */
02030 void
02031 InstallOidDecoderMapping ( char* ch_oid, EncodeFcn encode, gser_decoder_func* G_decode, ber_tag_decoder_func* B_decode, ExtractFcn extract, MatchFcn match ) {
02032        AsnOid oid;
02033        int strLen;
02034        void* mem_op;
02035 
02036        strLen = strlen( ch_oid );
02037        if( strLen <= 0 ) return;
02038        mem_op = comp_nibble_memory_allocator ( 128, 16 );
02039        oid.octs = EncodeComponentOid ( mem_op, ch_oid, &strLen );
02040        oid.octetLen = strLen;
02041        if( strLen <= 0 ) return;
02042        
02043 
02044        InstallAnyByComponentOid ( 0, &oid, 0, encode, G_decode, B_decode,
02045                                           extract, match, NULL, NULL);
02046        comp_nibble_memory_free(mem_op);
02047 }
02048 
02049 /*
02050  * Look up Oid-decoder mapping table by berval have either
02051  * oid or description
02052  */
02053 OidDecoderMapping*
02054 RetrieveOidDecoderMappingbyBV( struct berval* in ) {
02055        if ( IsNumericOid ( in->bv_val, in->bv_len ) )
02056               return RetrieveOidDecoderMappingbyOid( in->bv_val, in->bv_len );
02057        else
02058               return RetrieveOidDecoderMappingbyDesc( in->bv_val, in->bv_len );
02059 }
02060 
02061 /*
02062  * Look up Oid-decoder mapping table by dotted OID
02063  */
02064 OidDecoderMapping*
02065 RetrieveOidDecoderMappingbyOid( char* ch_oid, int oid_len ) {
02066        Hash hash;
02067        void *anyInfo;
02068        AsnOid oid;
02069        int strLen;
02070        void* mem_op;
02071 
02072        mem_op = comp_nibble_memory_allocator ( 128, 16 );
02073        oid.octs = EncodeComponentOid ( mem_op, ch_oid, &oid_len);
02074        oid.octetLen = oid_len;
02075        if( oid_len <= 0 ) {
02076               comp_nibble_memory_free( mem_op );
02077               return NULL;
02078        }
02079        
02080        /* use encoded oid as hash string */
02081        hash = MakeHash ( oid.octs, oid.octetLen);
02082        comp_nibble_memory_free( mem_op );
02083        if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
02084               return (OidDecoderMapping*) anyInfo;
02085        else
02086               return (OidDecoderMapping*) NULL;
02087 
02088 }
02089 
02090 /*
02091  * Look up Oid-decoder mapping table by description
02092  */
02093 OidDecoderMapping*
02094 RetrieveOidDecoderMappingbyDesc( char* desc, int desc_len ) {
02095        Hash hash;
02096        void *anyInfo;
02097        AsnOid oid;
02098        AttributeType* ad_type;
02099        struct berval bv;
02100        void* mem_op;
02101 
02102        bv.bv_val = desc;
02103        bv.bv_len = desc_len;
02104        ad_type = at_bvfind( &bv );
02105 
02106        oid.octs = ad_type->sat_atype.at_oid;
02107        oid.octetLen = strlen ( oid.octs );
02108 
02109        if ( !ad_type )
02110               return (OidDecoderMapping*) NULL;
02111 
02112        mem_op = comp_nibble_memory_allocator ( 128, 16 );
02113 
02114        oid.octs = EncodeComponentOid ( mem_op, oid.octs , (int*)&oid.octetLen );
02115        if( oid.octetLen <= 0 ) {
02116               comp_nibble_memory_free( mem_op );
02117               return (OidDecoderMapping*) NULL;
02118        }
02119        
02120        /* use encoded oid as hash string */
02121        hash = MakeHash ( oid.octs, oid.octetLen);
02122        comp_nibble_memory_free( mem_op );
02123        if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
02124               return (OidDecoderMapping*) anyInfo;
02125        else
02126               return (OidDecoderMapping*) NULL;
02127 
02128 }
02129 void
02130 InstallAnyByComponentOid (int anyId, AsnOid *oid, unsigned int size,
02131                      EncodeFcn encode, gser_decoder_func* G_decode,
02132                      ber_tag_decoder_func* B_decode, ExtractFcn extract,
02133                       MatchFcn match, FreeFcn free, PrintFcn print)
02134 {
02135        ComponentAnyInfo *a;
02136        Hash h;
02137 
02138        a = (ComponentAnyInfo*) malloc (sizeof (ComponentAnyInfo));
02139        a->anyId = anyId;
02140        if ( oid ) {
02141               a->oid.octs = malloc( oid->octetLen );
02142               memcpy ( a->oid.octs, oid->octs, oid->octetLen );
02143               a->oid.octetLen = oid->octetLen;
02144        }
02145        a->size = size;
02146        a->Encode = encode;
02147        a->GSER_Decode = G_decode;
02148        a->BER_Decode = B_decode;
02149        a->Match = match;
02150        a->Extract = extract;
02151        a->Free = free;
02152        a->Print = print;
02153 
02154        h = MakeHash (oid->octs, oid->octetLen);
02155 
02156        if (anyOidHashTblG == NULL)
02157               anyOidHashTblG = InitHash();
02158 
02159        if(anyOidHashTblG != NULL)
02160               Insert(anyOidHashTblG, a, h);
02161 }
02162 
02163 int
02164 BDecComponentTop  (
02165 ber_decoder_func *decoder _AND_
02166 void* mem_op _AND_
02167 GenBuf *b _AND_
02168 AsnTag tag _AND_
02169 AsnLen elmtLen _AND_
02170 void **v _AND_
02171 AsnLen *bytesDecoded _AND_
02172 int mode) {
02173        tag = BDecTag ( b, bytesDecoded );
02174        elmtLen = BDecLen ( b, bytesDecoded );
02175        if ( elmtLen <= 0 ) return (-1);
02176        if ( tag != MAKE_TAG_ID (UNIV, CONS, SEQ_TAG_CODE) ) {
02177               return (-1);
02178        }
02179               
02180        return (*decoder)( mem_op, b, tag, elmtLen, (ComponentSyntaxInfo*)v,(int*)bytesDecoded, mode );
02181 }
02182 
02183 /*
02184  * ASN.1 specification of a distinguished name
02185  * DistinguishedName ::= RDNSequence
02186  * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
02187  * RelativeDistinguishedName ::= SET SIZE(1..MAX) OF AttributeTypeandValue
02188  * AttributeTypeandValue ::= SEQUENCE {
02189  *     type   AttributeType
02190  *     value  AttributeValue
02191  * }
02192  * When dnMatch/rdnMatch is used in a component assertion value
02193  * the component in DistinguishedName/RelativeDistinguishedName
02194  * need to be converted to the LDAP encodings in RFC2253
02195  * in order to be matched against the assertion value
02196  * If allComponentMatch is used, the assertion value may be
02197  * decoded into the Internal Representation(Component Tree)
02198  * by the corresponding GSER or BER decoder
02199  * Following routine converts a component tree(DistinguishedName) into
02200  * LDAP encodings in RFC2253
02201  * Example)
02202  * IR : ComponentRDNSequence
02203  * GSER : { { type cn, value sang },{ type o, value ibm}, {type c, value us} }
02204  * LDAP Encodings : cn=sang,o=ibm,c=us 
02205  */
02206 
02207 increment_bv_mem_by_size ( struct berval* in, int size ) {
02208        int new_size = in->bv_len + size;
02209        in->bv_val = realloc( in->bv_val, new_size );
02210        in->bv_len = new_size;
02211 }
02212 
02213 int
02214 ConvertBER2Desc( char* in, int size, struct berval* out, int* pos ) {
02215        int desc_size;
02216        char* desc_ptr;
02217        unsigned int firstArcNum;
02218        unsigned int arcNum;
02219        int i, rc, start_pos = *pos;
02220        char buf[MAX_OID_LEN];
02221        AttributeType *at;
02222        struct berval bv_name;
02223 
02224        /*convert BER oid to desc*/
02225        for ( i = 0, arcNum = 0; (i < size) && (in[i] & 0x80 ); i++ )
02226               arcNum = (arcNum << 7) + (in[i] & 0x7f);
02227        arcNum = (arcNum << 7) + (in[i] & 0x7f);
02228        i++;
02229        firstArcNum = (unsigned short)(arcNum/40);
02230        if ( firstArcNum > 2 )
02231               firstArcNum = 2;
02232        
02233        arcNum = arcNum - (firstArcNum * 40 );
02234 
02235        rc = intToAscii ( arcNum, buf );
02236 
02237        /*check if the buffer can store the first/second arc and two dots*/
02238        if ( out->bv_len < *pos + 2 + 1 + rc )
02239               increment_bv_mem_by_size ( out, INCREMENT_SIZE );
02240 
02241        if ( firstArcNum == 1)
02242               out->bv_val[*pos] = '1';
02243        else
02244               out->bv_val[*pos] = '2';
02245        (*pos)++;
02246        out->bv_val[*pos] = '.';
02247        (*pos)++;
02248 
02249        memcpy( out->bv_val + *pos, buf, rc );
02250        *pos += rc;
02251        out->bv_val[*pos] = '.';
02252        (*pos)++;
02253 
02254        for ( ; i < size ; ) {
02255               for ( arcNum=0; (i < size) && (in[i] & 0x80) ; i++ )
02256                      arcNum = (arcNum << 7) + (in[i] & 0x7f);
02257               arcNum = (arcNum << 7) + (in[i] & 0x7f);
02258               i++;
02259 
02260               rc = intToAscii ( arcNum, buf );
02261 
02262               if ( out->bv_len < *pos + rc + 1 )
02263                      increment_bv_mem_by_size ( out, INCREMENT_SIZE );
02264 
02265               memcpy( out->bv_val + *pos, buf, rc );
02266               *pos += rc;
02267               out->bv_val[*pos] = '.';
02268               (*pos)++;
02269        }
02270        (*pos)--;/*remove the last '.'*/
02271 
02272        /*
02273         * lookup OID database to locate desc
02274         * then overwrite OID with desc in *out
02275         * If failed to look up desc, OID form is used
02276         */
02277        bv_name.bv_val = out->bv_val + start_pos;
02278        bv_name.bv_len = *pos - start_pos;
02279        at = at_bvfind( &bv_name );
02280        if ( !at )
02281               return LDAP_SUCCESS;
02282        desc_size = at->sat_cname.bv_len;
02283        memcpy( out->bv_val + start_pos, at->sat_cname.bv_val, desc_size );
02284        *pos = start_pos + desc_size;
02285        return LDAP_SUCCESS;
02286 }
02287 
02288 int
02289 ConvertComponentAttributeTypeAndValue2RFC2253 ( irAttributeTypeAndValue* in, struct berval* out, int *pos ) {
02290        int rc;
02291        int value_size = ((ComponentUTF8String*)in->value.value)->value.octetLen;
02292        char* value_ptr =  ((ComponentUTF8String*)in->value.value)->value.octs;
02293 
02294        rc = ConvertBER2Desc( in->type.value.octs, in->type.value.octetLen, out, pos );
02295        if ( rc != LDAP_SUCCESS ) return rc;
02296        if ( out->bv_len < *pos + 1/*for '='*/  )
02297               increment_bv_mem_by_size ( out, INCREMENT_SIZE );
02298        /*Between type and value, put '='*/
02299        out->bv_val[*pos] = '=';
02300        (*pos)++;
02301 
02302        /*Assume it is string*/            
02303        if ( out->bv_len < *pos + value_size )
02304               increment_bv_mem_by_size ( out, INCREMENT_SIZE );
02305        memcpy( out->bv_val + *pos, value_ptr, value_size );
02306        out->bv_len += value_size;
02307        *pos += value_size;
02308        
02309        return LDAP_SUCCESS;
02310 }
02311 
02312 int
02313 ConvertRelativeDistinguishedName2RFC2253 ( irRelativeDistinguishedName* in, struct berval *out , int* pos) {
02314        irAttributeTypeAndValue* attr_typeNvalue;
02315        int rc;
02316 
02317 
02318        FOR_EACH_LIST_ELMT( attr_typeNvalue, &in->comp_list)
02319        {
02320               rc = ConvertComponentAttributeTypeAndValue2RFC2253( attr_typeNvalue, out, pos );
02321               if ( rc != LDAP_SUCCESS ) return LDAP_INVALID_SYNTAX;
02322 
02323               if ( out->bv_len < *pos + 1/*for '+'*/  )
02324                      increment_bv_mem_by_size ( out, INCREMENT_SIZE );
02325               /*between multivalued RDNs, put comma*/
02326               out->bv_val[(*pos)++] = '+';
02327        }
02328        (*pos)--;/*remove the last '+'*/
02329        return LDAP_SUCCESS;
02330 }
02331 
02332 int 
02333 ConvertRDN2RFC2253 ( irRelativeDistinguishedName* in, struct berval *out ) {
02334        int rc, pos = 0;
02335        out->bv_val = (char*)malloc( INITIAL_DN_SIZE );
02336        out->bv_len = INITIAL_DN_SIZE;
02337 
02338        rc = ConvertRelativeDistinguishedName2RFC2253 ( in, out , &pos);
02339        if ( rc != LDAP_SUCCESS ) return rc;
02340        out->bv_val[pos] = '\0';
02341        out->bv_len = pos;
02342        return LDAP_SUCCESS;
02343 }
02344 
02345 int
02346 ConvertRDNSequence2RFC2253( irRDNSequence *in, struct berval* out ) {
02347        irRelativeDistinguishedName* rdn_seq;
02348        AsnList* seq = &in->comp_list;
02349        int pos = 0, rc ;
02350 
02351        out->bv_val = (char*)malloc( INITIAL_DN_SIZE );
02352        out->bv_len = INITIAL_DN_SIZE;
02353 
02354        FOR_EACH_LIST_ELMT( rdn_seq, seq )
02355        {
02356               rc = ConvertRelativeDistinguishedName2RFC2253( rdn_seq, out, &pos );
02357               if ( rc != LDAP_SUCCESS ) return LDAP_INVALID_SYNTAX;
02358 
02359               if ( out->bv_len < pos + 1/*for ','*/ )
02360                      increment_bv_mem_by_size ( out, INCREMENT_SIZE );
02361               /*Between RDN, put comma*/
02362               out->bv_val[pos++] = ',';
02363        }
02364        pos--;/*remove the last '+'*/
02365        out->bv_val[pos] = '\0';
02366        out->bv_len =pos;
02367        return LDAP_SUCCESS;
02368 }
02369 
02370 #endif