]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/comp_match/componentlib.c
Bug fix: invalid return value
[openldap] / contrib / slapd-modules / comp_match / componentlib.c
1 /* Copyright 2004 IBM Corporation
2  * All rights reserved.
3  * Redisribution and use in source and binary forms, with or without
4  * modification, are permitted only as authorizd by the OpenLADP
5  * Public License.
6  */
7 /* ACKNOWLEDGEMENTS
8  * This work originally developed by Sang Seok Lim
9  * 2004/06/18   03:20:00        slim@OpenLDAP.org
10  */
11
12 #include "portable.h"
13 #include <ac/string.h>
14 #include <ac/socket.h>
15 #include <ldap_pvt.h>
16 #include "lutil.h"
17 #include <ldap.h>
18 #include "slap.h"
19 #include "component.h"
20
21 #include "componentlib.h"
22 #include "asn.h"
23 #include <asn-gser.h>
24 #include <stdlib.h>
25
26 #include <string.h>
27
28 #ifndef SLAPD_COMP_MATCH
29 #define SLAPD_COMP_MATCH SLAPD_MOD_DYNAMIC
30 #endif
31
32 #ifdef SLAPD_COMP_MATCH
33 /*
34  * Matching function : BIT STRING
35  */
36 int
37 MatchingComponentBits ( char* oid, ComponentSyntaxInfo *csi_attr,
38                         ComponentSyntaxInfo *csi_assert )
39 {
40         int rc;
41         MatchingRule* mr;
42         ComponentBits *a, *b;
43                                                                           
44         if ( oid ) {
45                 mr = retrieve_matching_rule(oid, (AsnTypeId)csi_attr->csi_comp_desc->cd_type_id );
46                 if ( mr )
47                         return component_value_match( mr, csi_attr , csi_assert );
48         }
49         a = ((ComponentBits*)csi_attr);
50         b = ((ComponentBits*)csi_assert);
51         rc = ( a->value.bitLen == b->value.bitLen && 
52                 strncmp( a->value.bits,b->value.bits,a->value.bitLen ) == 0 );
53         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
54 }
55
56 /*
57  * Free function: BIT STRING
58  */
59 void
60 FreeComponentBits ( ComponentBits* v ) {
61         FreeAsnBits( &v->value );
62 }
63
64 /*
65  * GSER Encoder : BIT STRING
66  */
67 int
68 GEncComponentBits ( GenBuf *b, ComponentBits *in )
69 {
70         GAsnBits bits = {0};
71
72         bits.value = in->value;
73         if ( !in )
74                 return (-1);
75         return GEncAsnBitsContent ( b, &bits);
76 }
77
78
79 /*
80  * GSER Decoder : BIT STRING
81  */
82 int
83 GDecComponentBits ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
84 {
85         char* peek_head;
86         int i, strLen;
87         void* component_values;
88         ComponentBits* k, **k2;
89         GAsnBits result;
90
91         k = (ComponentBits*) v;
92                                                                           
93         if ( mode & DEC_ALLOC_MODE_0 ) {
94                 k2 = (ComponentBits**) v;
95                 *k2 = (ComponentBits*) CompAlloc( mem_op, sizeof( ComponentBits ) );
96                 if ( !*k2 ) return LDAP_DECODING_ERROR;
97                 k = *k2;
98         }
99         
100         if ( GDecAsnBitsContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
101                 if ( k ) CompFree( mem_op,  k );
102                 return LDAP_DECODING_ERROR;
103         }
104         k->value = result.value;
105         k->comp_desc = get_component_description (BASICTYPE_BITSTRING);
106
107         return LDAP_SUCCESS;
108 }
109
110 /*
111  * Component BER Decoder : BIT STRING
112  */
113 int
114 BDecComponentBitsTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
115         return BDecComponentBits ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
116 }
117
118 int
119 BDecComponentBits ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
120                         AsnLen *bytesDecoded, int mode )
121 {
122         char* peek_head;
123         int i, strLen, rc;
124         void* component_values;
125         ComponentBits* k, **k2;
126         AsnBits result;
127                                                                           
128         k = (ComponentBits*) v;
129                                                                           
130         if ( mode & DEC_ALLOC_MODE_0 ) {
131                 k2 = (ComponentBits**) v;
132                 *k2 = (ComponentBits*) CompAlloc( mem_op, sizeof( ComponentBits ) );
133                 if ( !*k2 ) return LDAP_DECODING_ERROR;
134                 k = *k2;
135         }
136         
137         if ( mode & CALL_TAG_DECODER ){
138                 mode = mode & CALL_CONTENT_DECODER;
139                 rc = BDecAsnBits ( mem_op, b, &result, bytesDecoded );
140         } else {
141                 rc = BDecAsnBitsContent ( mem_op, b, tagId, len, &result, bytesDecoded );
142         }
143
144         if ( rc < 0 ) {
145                 if ( k ) CompFree( mem_op,  k );
146                 return LDAP_DECODING_ERROR;
147         }
148
149         k->value = result;
150         k->comp_desc = get_component_description (BASICTYPE_BITSTRING);
151  
152         return LDAP_SUCCESS;
153 }
154
155 /*
156  * Component GSER BMPString Encoder
157  */
158 int
159 GEncComponentBMPString ( GenBuf *b, ComponentBMPString *in )
160 {
161         GBMPString t = {0};
162
163         if ( !in || in->value.octetLen <= 0 )
164                 return (-1);
165         t.value = in->value;
166         return GEncBMPStringContent ( b, &t );
167 }
168
169 /*
170  * Component GSER BMPString Decoder
171  */
172 int
173 GDecComponentBMPString ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode)
174 {
175         char* peek_head;
176         int i, strLen;
177         void* component_values;
178         ComponentBMPString* k, **k2;
179         GBMPString result;
180                                                                           
181         k = (ComponentBMPString*) v;
182                                                                           
183         if ( mode & DEC_ALLOC_MODE_0 ) {
184                 k2 = (ComponentBMPString**) v;
185                 *k2 = (ComponentBMPString*) CompAlloc( mem_op, sizeof( ComponentBMPString ) );
186                 if ( !*k2 ) return LDAP_DECODING_ERROR;
187                 k = *k2;
188         }
189
190         *bytesDecoded = 0;
191
192         if ( GDecBMPStringContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
193                 if ( k ) CompFree( mem_op, k );
194                 return LDAP_DECODING_ERROR;
195         }
196
197         k->value = result.value;
198         k->comp_desc = get_component_description (BASICTYPE_BMP_STR);
199  
200         return LDAP_SUCCESS;
201
202 }
203
204 /*
205  * Component BER BMPString Decoder
206  */
207 int
208 BDecComponentBMPStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
209         return BDecComponentBMPString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
210 }
211
212 int
213 BDecComponentBMPString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
214                         AsnLen *bytesDecoded, int mode )
215 {
216         char* peek_head;
217         int i, strLen, rc;
218         void* component_values;
219         ComponentBMPString* k, **k2;
220         BMPString result;
221                                                                           
222         k = (ComponentBMPString*) v;
223                                                                           
224         if ( mode & DEC_ALLOC_MODE_0 ) {
225                 k2 = (ComponentBMPString**) v;
226                 *k2 = (ComponentBMPString*) CompAlloc( mem_op, sizeof( ComponentBMPString ) );
227                 if ( !*k2 ) return LDAP_DECODING_ERROR;
228                 k = *k2;
229         }
230
231         if ( mode & CALL_TAG_DECODER ){
232                 mode = mode & CALL_CONTENT_DECODER;
233                 rc = BDecBMPString ( mem_op, b, &result, bytesDecoded );
234         } else {
235                 rc = BDecBMPStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
236         }
237
238         if ( rc < 0 ) {
239                 if ( k ) CompFree( mem_op, k );
240                 return LDAP_DECODING_ERROR;
241         }
242
243         k->value = result;
244         k->comp_desc = get_component_description (BASICTYPE_BMP_STR);
245  
246         return LDAP_SUCCESS;
247
248 }
249
250 /*
251  * Component GSER Encoder : UTF8 String
252  */
253 int
254 GEncComponentUTF8String ( GenBuf *b, ComponentUTF8String *in )
255 {
256         GUTF8String t = {0};
257         if ( !in || in->value.octetLen <= 0 )
258                 return (-1);
259         t.value = in->value;
260         return GEncUTF8StringContent ( b, &t );
261 }
262
263 /*
264  * Component GSER Decoder :  UTF8 String
265  */
266 int
267 GDecComponentUTF8String ( void* mem_op, GenBuf *b, void *v,
268                                 AsnLen *bytesDecoded, int mode) {
269         char* peek_head;
270         int i, strLen;
271         void* component_values;
272         ComponentUTF8String* k, **k2;
273         GUTF8String result;
274                                                                           
275         k = (ComponentUTF8String*) v;
276                                                                           
277         if ( mode & DEC_ALLOC_MODE_0 ) {
278                 k2 = (ComponentUTF8String**) v;
279                 *k2 = (ComponentUTF8String*)CompAlloc( mem_op, sizeof( ComponentUTF8String ) );
280                 if ( !*k2 ) return LDAP_DECODING_ERROR;
281                 k = *k2;
282         }
283
284         *bytesDecoded = 0;
285
286         if ( GDecUTF8StringContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
287                 if ( k ) CompFree( mem_op,  k );
288                 return LDAP_DECODING_ERROR;
289         }
290         
291         k->value = result.value;
292         k->comp_desc = get_component_description (BASICTYPE_UTF8_STR);
293  
294         return LDAP_SUCCESS;
295 }
296
297 /*
298  * Component BER Decoder : UTF8String
299  */
300 int
301 BDecComponentUTF8StringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
302         return BDecComponentUTF8String ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
303 }
304
305 int
306 BDecComponentUTF8String ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len,
307                                 void *v, AsnLen *bytesDecoded, int mode )
308 {
309         char* peek_head;
310         int i, strLen, rc;
311         void* component_values;
312         ComponentUTF8String* k, **k2;
313         UTF8String result;
314                                                                           
315         k = (ComponentUTF8String*) v;
316                                                                           
317         if ( mode & DEC_ALLOC_MODE_0 ) {
318                 k2 = (ComponentUTF8String**) v;
319                 *k2 = (ComponentUTF8String*) CompAlloc( mem_op, sizeof( ComponentUTF8String ) );
320                 if ( !*k2 ) return LDAP_DECODING_ERROR;
321                 k = *k2;
322         }
323         
324         if ( mode & CALL_TAG_DECODER ){
325                 mode = mode & CALL_CONTENT_DECODER;
326                 rc = BDecUTF8String ( mem_op, b, &result, bytesDecoded );
327         } else {
328                 rc = BDecUTF8StringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
329         }
330         if ( rc < 0 ) {
331                 if ( k ) CompFree( mem_op,  k );
332                 return LDAP_DECODING_ERROR;
333         }
334
335         k->value = result;
336         k->comp_desc = get_component_description (BASICTYPE_UTF8_STR);
337
338         return LDAP_SUCCESS;
339 }
340
341 /*
342  * Component GSER Encoder :  Teletex String
343  */
344 int
345 GEncComponentTeletexString ( GenBuf *b, ComponentTeletexString *in )
346 {
347         GTeletexString t = {0};
348
349         if ( !in || in->value.octetLen <= 0 )
350                 return (-1);
351         t.value = in->value;
352         return GEncTeletexStringContent ( b, &t );
353 }
354
355 /*
356  * Component GSER Decoder :  Teletex String
357  */
358 int
359 GDecComponentTeletexString  ( void* mem_op, GenBuf *b, void *v,
360                                         AsnLen *bytesDecoded, int mode) {
361         char* peek_head;
362         int i, strLen;
363         void* component_values;
364         ComponentTeletexString* k, **k2;
365         GTeletexString result;
366                                                                           
367         k = (ComponentTeletexString*) v;
368                                                                           
369         if ( mode & DEC_ALLOC_MODE_0 ) {
370                 k2 = (ComponentTeletexString**) v;
371                 *k2 = (ComponentTeletexString*)CompAlloc( mem_op, sizeof( ComponentTeletexString ) );
372                 if ( !*k2 ) return LDAP_DECODING_ERROR;
373                 k = *k2;
374         }
375
376         *bytesDecoded = 0;
377
378         if ( GDecTeletexStringContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
379                 if ( k ) CompFree( mem_op,  k );
380                 return LDAP_DECODING_ERROR;
381         }
382
383         k->value = result.value;
384         k->comp_desc = get_component_description (BASICTYPE_VIDEOTEX_STR);
385  
386         return LDAP_SUCCESS;
387 }
388
389
390 /*
391  * Matching function : BOOLEAN
392  */
393 int
394 MatchingComponentBool(char* oid, ComponentSyntaxInfo* csi_attr,
395                         ComponentSyntaxInfo* csi_assert )
396 {
397         MatchingRule* mr;
398         ComponentBool *a, *b;
399                                                                           
400         if( oid ) {
401                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
402                 if ( mr )
403                         return component_value_match( mr, csi_attr , csi_assert );
404         }
405
406         a = ((ComponentBool*)csi_attr);
407         b = ((ComponentBool*)csi_assert);
408
409         return (a->value == b->value) ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
410 }
411
412 /*
413  * GSER Encoder : BOOLEAN
414  */
415 int
416 GEncComponentBool ( GenBuf *b, ComponentBool *in )
417 {
418         GAsnBool t = {0};
419
420         if ( !in )
421                 return (-1);
422         t.value = in->value;
423         return GEncAsnBoolContent ( b, &t );
424 }
425
426 /*
427  * GSER Decoder : BOOLEAN
428  */
429 int
430 GDecComponentBool ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
431 {
432         char* peek_head;
433         int i, strLen;
434         ComponentBool* k, **k2;
435         GAsnBool result;
436                                                                           
437         k = (ComponentBool*) v;
438                                                                           
439         if ( mode & DEC_ALLOC_MODE_0 ) {
440                 k2 = (ComponentBool**) v;
441                 *k2 = (ComponentBool*) CompAlloc( mem_op, sizeof( ComponentBool ) );
442                 if ( !*k2 ) return LDAP_DECODING_ERROR;
443                 k = *k2;
444         }
445
446         if ( GDecAsnBoolContent( mem_op, b, &result, bytesDecoded ) < 0 ) {
447                 if ( k ) CompFree ( mem_op, k );
448                 return LDAP_DECODING_ERROR;
449         }
450
451         k->value = result.value;
452         k->comp_desc = get_component_description (BASICTYPE_BOOLEAN);
453  
454         return LDAP_SUCCESS;
455 }
456
457 /*
458  * Component BER Decoder : BOOLEAN
459  */
460 int
461 BDecComponentBoolTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
462         return BDecComponentBool ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
463 }
464
465 int
466 BDecComponentBool ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
467                         AsnLen *bytesDecoded, int mode )
468 {
469         char* peek_head;
470         int i, strLen, rc;
471         ComponentBool* k, **k2;
472         AsnBool result;
473                                                                           
474         k = (ComponentBool*) v;
475                                                                           
476         if ( mode & DEC_ALLOC_MODE_0 ) {
477                 k2 = (ComponentBool**) v;
478                 *k2 = (ComponentBool*) CompAlloc( mem_op, sizeof( ComponentBool ) );
479                 if ( !*k2 ) return LDAP_DECODING_ERROR;
480                 k = *k2;
481         }
482
483         if ( mode & CALL_TAG_DECODER ){
484                 mode = mode & CALL_CONTENT_DECODER;
485                 rc = BDecAsnBool ( mem_op, b, &result, bytesDecoded );
486         } else {
487                 rc = BDecAsnBoolContent( mem_op, b, tagId, len, &result, bytesDecoded );
488         }
489         if ( rc < 0 ) {
490                 if ( k ) CompFree ( mem_op, k );
491                 return LDAP_DECODING_ERROR;
492         }
493
494         k->value = result;
495         k->comp_desc = get_component_description (BASICTYPE_BOOLEAN);
496
497         return LDAP_SUCCESS;
498 }
499
500 /*
501  * Matching function : ENUMERATE
502  */
503 int
504 MatchingComponentEnum ( char* oid, ComponentSyntaxInfo *csi_attr,
505                         ComponentSyntaxInfo *csi_assert )
506 {
507         int rc;
508         MatchingRule* mr;
509         ComponentEnum *a, *b;
510                                                                           
511         if( oid ) {
512                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
513                 if ( mr )
514                         return component_value_match( mr, csi_attr , csi_assert );
515         }
516         a = ((ComponentEnum*)csi_attr);
517         b = ((ComponentEnum*)csi_assert);
518         rc = (a->value == b->value);
519                                                                           
520         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
521 }
522
523 /*
524  * GSER Encoder : ENUMERATE
525  */
526 int
527 GEncComponentEnum ( GenBuf *b, ComponentEnum *in )
528 {
529         GAsnEnum t = {0};
530
531         if ( !in )
532                 return (-1);
533         t.value = in->value;
534         return GEncAsnEnumContent ( b, &t );
535 }
536
537 /*
538  * GSER Decoder : ENUMERATE
539  */
540 int
541 GDecComponentEnum ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
542 {
543         char* peek_head;
544         int i, strLen;
545         void* component_values;
546         ComponentEnum* k, **k2;
547         GAsnEnum result;
548                                                                           
549         k = (ComponentEnum*) v;
550                                                                           
551         if ( mode & DEC_ALLOC_MODE_0 ) {
552                 k2 = (ComponentEnum**) v;
553                 *k2 = (ComponentEnum*) CompAlloc( mem_op, sizeof( ComponentEnum ) );
554                 if ( !*k2 ) return LDAP_DECODING_ERROR;
555                 k = *k2;
556         }
557
558         if ( GDecAsnEnumContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
559                 if ( k ) CompFree ( mem_op, k );
560                 return LDAP_DECODING_ERROR;
561         }
562
563         k->value_identifier.bv_val = result.value_identifier;
564         k->value_identifier.bv_len = result.len;
565
566         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
567         if ( !k->comp_desc )  {
568                 if ( k ) CompFree ( mem_op, k );
569                 return LDAP_DECODING_ERROR;
570         }
571         k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentEnum;
572         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentEnum;
573         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentEnum;
574         k->comp_desc->cd_free = (comp_free_func*)NULL;
575         k->comp_desc->cd_extract_i = NULL;
576         k->comp_desc->cd_type = ASN_BASIC;
577         k->comp_desc->cd_type_id = BASICTYPE_ENUMERATED;
578         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentEnum;
579
580         return LDAP_SUCCESS;
581 }
582
583 /*
584  * Component BER Decoder : ENUMERATE
585  */
586 int
587 BDecComponentEnumTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
588         return BDecComponentEnum ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
589 }
590
591 int
592 BDecComponentEnum ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
593                         AsnLen *bytesDecoded, int mode )
594 {
595         char* peek_head;
596         int i, strLen, rc;
597         void* component_values;
598         ComponentEnum* k, **k2;
599         AsnEnum result;
600                                                                           
601         k = (ComponentEnum*) v;
602                                                                           
603         if ( mode & DEC_ALLOC_MODE_0 ) {
604                 k2 = (ComponentEnum**) v;
605                 *k2 = (ComponentEnum*) CompAlloc( mem_op, sizeof( ComponentEnum ) );
606                 if ( k ) return LDAP_DECODING_ERROR;
607                 k = *k2;
608         }
609
610         if ( mode & CALL_TAG_DECODER ){
611                 mode = mode & CALL_CONTENT_DECODER;
612                 rc = BDecAsnEnum ( mem_op, b, &result, bytesDecoded );
613         } else {
614                 rc = BDecAsnEnumContent ( mem_op, b, tagId, len, &result, bytesDecoded );
615         }
616         if ( rc < 0 ) {
617                 if ( k ) CompFree ( mem_op, k );
618                 return LDAP_DECODING_ERROR;
619         }
620
621         k->value = result;
622
623         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
624         if ( !k->comp_desc )  {
625                 if ( k  ) CompFree ( mem_op, k );
626                 return LDAP_DECODING_ERROR;
627         }
628         k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentEnum;
629         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentEnum;
630         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentEnum;
631         k->comp_desc->cd_free = (comp_free_func*)NULL;
632         k->comp_desc->cd_extract_i = NULL;
633         k->comp_desc->cd_type = ASN_BASIC;
634         k->comp_desc->cd_type_id = BASICTYPE_ENUMERATED;
635         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentEnum;
636
637         return LDAP_SUCCESS;
638 }
639
640 /*
641  * Component GSER Encoder : IA5String
642  */
643 int
644 GEncComponentIA5Stirng ( GenBuf *b, ComponentIA5String* in )
645 {
646         GIA5String t = {0};
647         t.value = in->value;
648         if ( !in || in->value.octetLen <= 0 ) return (-1);
649         return GEncIA5StringContent( b, &t );
650 }
651
652 /*
653  * Component BER Decoder : IA5String
654  */
655 int
656 BDecComponentIA5StringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
657         return BDecComponentIA5String ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
658 }
659
660 int
661 BDecComponentIA5String ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
662                         AsnLen *bytesDecoded, int mode )
663 {
664         char* peek_head;
665         int i, strLen, rc;
666         void* component_values;
667         ComponentIA5String* k, **k2;
668         IA5String result;
669                                                                           
670         k = (ComponentIA5String*) v;
671                                                                           
672         if ( mode & DEC_ALLOC_MODE_0 ) {
673                 k2 = (ComponentIA5String**) v;
674                 *k2 = (ComponentIA5String*) CompAlloc( mem_op, sizeof( ComponentIA5String ) );
675                 if ( !*k2 ) return LDAP_DECODING_ERROR;
676                 k = *k2;
677         }
678
679         if ( mode & CALL_TAG_DECODER ){
680                 mode = mode & CALL_CONTENT_DECODER;
681                 rc = BDecIA5String ( mem_op, b, &result, bytesDecoded );
682         } else {
683                 rc = BDecIA5StringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
684         }
685         if ( rc < 0 ) {
686                 if ( k ) CompFree ( mem_op, k );
687                 return LDAP_DECODING_ERROR;
688         }
689
690         k->value = result;
691
692         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
693         if ( !k->comp_desc )  {
694                 if ( k ) CompFree ( mem_op, k );
695                 return LDAP_DECODING_ERROR;
696         }
697         k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentIA5String;
698         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentIA5String;
699         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentIA5String;
700         k->comp_desc->cd_free = (comp_free_func*)FreeComponentIA5String;
701         k->comp_desc->cd_extract_i = NULL;
702         k->comp_desc->cd_type = ASN_BASIC;
703         k->comp_desc->cd_type_id = BASICTYPE_IA5_STR;
704         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentIA5String;
705
706         return LDAP_SUCCESS;
707 }
708
709 /*
710  * Matching function : INTEGER
711  */
712 int
713 MatchingComponentInt(char* oid, ComponentSyntaxInfo* csi_attr,
714                         ComponentSyntaxInfo* csi_assert )
715 {
716         MatchingRule* mr;
717         ComponentInt *a, *b;
718                                                                           
719         if( oid ) {
720                 /* check if this ASN type's matching rule is overrided */
721                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
722                 /* if existing function is overrided, call the overriding
723 function*/
724                 if ( mr )
725                         return component_value_match( mr, csi_attr , csi_assert );
726         }
727         a = ((ComponentInt*)csi_attr);
728         b = ((ComponentInt*)csi_assert);
729                                                                           
730         return ( a->value == b->value ) ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
731 }
732
733 /*
734  * GSER Encoder : INTEGER
735  */
736 int
737 GEncComponentInt ( GenBuf *b, ComponentInt* in )
738 {
739         GAsnInt t = {0};
740
741         if ( !in )
742                 return (-1);
743         t.value = in->value;
744         return GEncAsnIntContent ( b, &t );
745 }
746
747 /*
748  * GSER Decoder : INTEGER 
749  */
750 int
751 GDecComponentInt( void* mem_op, GenBuf * b, void *v, AsnLen *bytesDecoded, int mode)
752 {
753         char* peek_head;
754         int i, strLen;
755         void* component_values;
756         ComponentInt* k, **k2;
757         GAsnInt result;
758                                                                           
759         k = (ComponentInt*) v;
760                                                                           
761         if ( mode & DEC_ALLOC_MODE_0 ) {
762                 k2 = (ComponentInt**) v;
763                 *k2 = (ComponentInt*) CompAlloc( mem_op, sizeof( ComponentInt ) );
764                 if ( !*k2 ) return LDAP_DECODING_ERROR;
765                 k = *k2;
766         }
767
768         if ( GDecAsnIntContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
769                 if ( k ) CompFree ( mem_op, k );
770                 return LDAP_DECODING_ERROR;
771         }
772         k->value = result.value;
773         k->comp_desc = get_component_description (BASICTYPE_INTEGER );
774
775         return LDAP_SUCCESS;
776 }
777
778 /*
779  * Component BER Decoder : INTEGER 
780  */
781 int
782 BDecComponentIntTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
783         return BDecComponentInt ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
784 }
785
786 int
787 BDecComponentInt ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
788                         AsnLen *bytesDecoded, int mode )
789 {
790         char* peek_head;
791         int i, strLen, rc;
792         void* component_values;
793         ComponentInt* k, **k2;
794         AsnInt result;
795                                                                           
796         k = (ComponentInt*) v;
797                                                                           
798         if ( mode & DEC_ALLOC_MODE_0 ) {
799                 k2 = (ComponentInt**) v;
800                 *k2 = (ComponentInt*) CompAlloc( mem_op, sizeof( ComponentInt ) );
801                 if ( !*k2 ) return LDAP_DECODING_ERROR;
802                 k = *k2;
803         }
804
805         if ( mode & CALL_TAG_DECODER ){
806                 mode = mode & CALL_CONTENT_DECODER;
807                 rc = BDecAsnInt ( mem_op, b, &result, bytesDecoded );
808         } else {
809                 rc = BDecAsnIntContent ( mem_op, b, tagId, len, &result, bytesDecoded );
810         }
811         k->value = result;
812
813         k->comp_desc = get_component_description (BASICTYPE_INTEGER );
814         
815         return LDAP_SUCCESS;
816 }
817
818 /*
819  * Matching function : NULL
820  */
821 int
822 MatchingComponentNull ( char *oid, ComponentSyntaxInfo *csi_attr,
823                         ComponentSyntaxInfo *csi_assert )
824 {
825         MatchingRule* mr;
826         ComponentNull *a, *b;
827                                                                           
828         if( oid ) {
829                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
830                 if ( mr )
831                         return component_value_match( mr, csi_attr , csi_assert );
832         }
833         a = ((ComponentNull*)csi_attr);
834         b = ((ComponentNull*)csi_assert);
835                                                                           
836         return (a->value == b->value) ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
837 }
838
839 /*
840  * GSER Encoder : NULL
841  */
842 int
843 GEncComponentNull ( GenBuf *b, ComponentNull *in )
844 {
845         GAsnNull t = {0};
846
847         if ( !in )
848                 return (-1);
849         t.value = in->value;
850         return GEncAsnNullContent ( b, &t );
851 }
852
853 /*
854  * GSER Decoder : NULL
855  */
856 int
857 GDecComponentNull ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
858 {
859         char* peek_head;
860         int i, strLen;
861         void* component_values;
862         ComponentNull* k, **k2;
863         GAsnNull result;
864                                                                           
865         k = (ComponentNull*) v;
866                                                                           
867         if ( mode & DEC_ALLOC_MODE_0 ) {
868                 k2 = (ComponentNull**) v;
869                 *k2 = (ComponentNull*) CompAlloc( mem_op, sizeof( ComponentNull ) );
870                 if ( !*k2 ) return LDAP_DECODING_ERROR;
871                 k = *k2;
872         }
873
874         if ( GDecAsnNullContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
875                 if ( k ) CompFree ( mem_op, k );
876                 return LDAP_DECODING_ERROR;
877         }
878         k->value = result.value;
879
880         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
881         if ( !k->comp_desc )  {
882                 if ( k ) CompFree ( mem_op, k );
883                 return LDAP_DECODING_ERROR;
884         }
885         k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentNull;
886         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentNull;
887         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentNull;
888         k->comp_desc->cd_free = (comp_free_func*)FreeComponentNull;
889         k->comp_desc->cd_extract_i = NULL;
890         k->comp_desc->cd_type = ASN_BASIC;
891         k->comp_desc->cd_type_id = BASICTYPE_NULL;
892         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentNull;
893
894         return LDAP_SUCCESS;
895 }
896
897 /*
898  * Component BER Decoder : NULL
899  */
900 int
901 BDecComponentNullTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
902 {
903         return BDecComponentNull ( mem_op, b, 0, 0, v,bytesDecoded, mode|CALL_TAG_DECODER );
904 }
905
906 int
907 BDecComponentNull ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
908                         AsnLen *bytesDecoded, int mode )
909 {
910         char* peek_head;
911         int i, strLen, rc;
912         void* component_values;
913         ComponentNull* k, **k2;
914         AsnNull result;
915
916         k = (ComponentNull*) v;
917                                                                          
918         if ( mode & DEC_ALLOC_MODE_0 ) {
919                 k2 = (ComponentNull**) v;
920                 *k2 = (ComponentNull*) CompAlloc( mem_op, sizeof( ComponentNull ) );
921                 if ( !*k2 ) return LDAP_DECODING_ERROR;
922                 k = *k2;
923         }
924
925         if ( mode & CALL_TAG_DECODER ){
926                 mode = mode & CALL_CONTENT_DECODER;
927                 rc = BDecAsnNull ( mem_op, b, &result, bytesDecoded );
928         }
929         else {
930                 rc = BDecAsnNullContent ( mem_op, b, tagId, len, &result, bytesDecoded);
931         }
932         if ( rc < 0 ) {
933                 if ( k ) CompFree ( mem_op, k );
934                 return LDAP_DECODING_ERROR;
935         }
936         k->value = result;
937
938         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
939         if ( !k->comp_desc )  {
940                 if ( k ) CompFree ( mem_op, k );
941                 return LDAP_DECODING_ERROR;
942         }
943         k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentNull;
944         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentNull;
945         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentNull;
946         k->comp_desc->cd_free = (comp_free_func*)FreeComponentNull;
947         k->comp_desc->cd_extract_i = NULL;
948         k->comp_desc->cd_type = ASN_BASIC;
949         k->comp_desc->cd_type_id = BASICTYPE_NULL;
950         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentNull;
951         return LDAP_SUCCESS;
952 }
953
954 /*
955  * Component BER Decoder : NumericString
956  */
957 int
958 BDecComponentNumericStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
959         return BDecComponentNumericString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
960 }
961
962 int
963 BDecComponentNumericString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
964 {
965         char* peek_head;
966         int i, strLen, rc;
967         void* component_values;
968         ComponentNumericString* k, **k2;
969         NumericString result;
970
971         k = (ComponentNumericString*) v;
972                                                                           
973         if ( mode & DEC_ALLOC_MODE_0 ) {
974                 k2 = (ComponentNumericString**) v;
975                 *k2 = (ComponentNumericString*) CompAlloc( mem_op, sizeof( ComponentNumericString ) );
976                 if ( !*k2 ) return LDAP_DECODING_ERROR;
977                 k = *k2;
978         }
979
980         if ( mode & CALL_TAG_DECODER ){
981                 mode = mode & CALL_CONTENT_DECODER;
982                 rc = BDecNumericString ( mem_op, b, &result, bytesDecoded );
983         } else {
984                 rc = BDecNumericStringContent ( mem_op, b, tagId, len, &result, bytesDecoded);
985         }
986         if ( rc < 0 ) {
987                 if ( k ) CompFree ( mem_op, k );
988                 return LDAP_DECODING_ERROR;
989         }
990         k->value = result;
991
992         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
993         if ( !k->comp_desc )  {
994                 if ( k ) CompFree ( mem_op, k );
995                 return LDAP_DECODING_ERROR;
996         }
997         k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentNumericString;
998         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentNumericString;
999         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentNumericString;
1000         k->comp_desc->cd_free = (comp_free_func*)FreeComponentNumericString;
1001         k->comp_desc->cd_extract_i = NULL;
1002         k->comp_desc->cd_type = ASN_BASIC;
1003         k->comp_desc->cd_type_id = BASICTYPE_NUMERIC_STR;
1004         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentNumericString;
1005
1006         return LDAP_SUCCESS;
1007 }
1008
1009
1010 /*
1011  * Free function : OCTET STRING
1012  */
1013 void
1014 FreeComponentOcts ( ComponentOcts* v) {
1015         FreeAsnOcts( &v->value );
1016 }
1017
1018 /*
1019  * Matching function : OCTET STRING
1020  */
1021 int
1022 MatchingComponentOcts ( char* oid, ComponentSyntaxInfo* csi_attr,
1023                         ComponentSyntaxInfo* csi_assert )
1024 {
1025         int rc;
1026         MatchingRule* mr;
1027         ComponentOcts *a, *b;
1028                                                                           
1029         if( oid ) {
1030                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
1031                 if ( mr )
1032                         return component_value_match( mr, csi_attr , csi_assert );
1033         }
1034         a = (ComponentOcts*) csi_attr;
1035         b = (ComponentOcts*) csi_assert;
1036         /* Assume that both of OCTET string has end of string character */
1037         if ( (a->value.octetLen == b->value.octetLen) &&
1038                 strncmp ( a->value.octs, b->value.octs, a->value.octetLen ) == 0 )
1039                 return LDAP_COMPARE_TRUE;
1040         else
1041                 return LDAP_COMPARE_FALSE;
1042 }
1043
1044 /*
1045  * GSER Encoder : OCTET STRING
1046  */
1047 int
1048 GEncComponentOcts ( GenBuf* b, ComponentOcts *in )
1049 {
1050         GAsnOcts t = {0};
1051         if ( !in || in->value.octetLen <= 0 )
1052                 return (-1);
1053
1054         t.value = in->value;
1055         return GEncAsnOctsContent ( b, &t );
1056 }
1057
1058 /*
1059  * GSER Decoder : OCTET STRING
1060  */
1061 int
1062 GDecComponentOcts ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1063 {
1064         char *peek_head, *data;
1065         int i, j, strLen;
1066         void* component_values;
1067         ComponentOcts* k, **k2;
1068         GAsnOcts result;
1069                                                                           
1070         k = (ComponentOcts*) v;
1071                                                                           
1072         if ( mode & DEC_ALLOC_MODE_0 ) {
1073                 k2 = (ComponentOcts**) v;
1074                 *k2 = (ComponentOcts*) CompAlloc( mem_op, sizeof( ComponentOcts ) );
1075                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1076                 k = *k2;
1077         }
1078
1079         if ( GDecAsnOctsContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
1080                 if ( k ) CompFree ( mem_op, k );
1081                 return LDAP_DECODING_ERROR;
1082         }
1083         k->value = result.value;
1084
1085         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1086         if ( !k->comp_desc )  {
1087                 if ( k ) CompFree ( mem_op, k );
1088                 return LDAP_DECODING_ERROR;
1089         }
1090         k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentOcts;
1091         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentOcts;
1092         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentOcts;
1093         k->comp_desc->cd_free = (comp_free_func*)FreeComponentOcts;
1094         k->comp_desc->cd_extract_i = NULL;
1095         k->comp_desc->cd_type = ASN_BASIC;
1096         k->comp_desc->cd_type_id = BASICTYPE_OCTETSTRING;
1097         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentOcts;
1098
1099         return LDAP_SUCCESS;
1100 }
1101
1102 /*
1103  * Component BER Decoder : OCTET STRING
1104  */
1105 int
1106 BDecComponentOctsTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1107         return BDecComponentOcts ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1108 }
1109
1110 int
1111 BDecComponentOcts ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
1112                         AsnLen *bytesDecoded, int mode )
1113 {
1114         char *peek_head, *data;
1115         int i, strLen, rc;
1116         void* component_values;
1117         ComponentOcts* k, **k2;
1118         AsnOcts result;
1119                                                                           
1120         k = (ComponentOcts*) v;
1121                                                                           
1122         if ( mode & DEC_ALLOC_MODE_0 ) {
1123                 k2 = (ComponentOcts**) v;
1124                 *k2 = (ComponentOcts*) CompAlloc( mem_op, sizeof( ComponentOcts ) );
1125                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1126                 k = *k2;
1127         }
1128
1129         if ( mode & CALL_TAG_DECODER ){
1130                 mode = mode & CALL_CONTENT_DECODER;
1131                 rc = BDecAsnOcts ( mem_op, b, &result, bytesDecoded );
1132         } else {
1133                 rc = BDecAsnOctsContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1134         }
1135         if ( rc < 0 ) {
1136                 if ( k ) CompFree ( mem_op, k );
1137                 return LDAP_DECODING_ERROR;
1138         }
1139         k->value = result;
1140
1141         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1142         if ( !k->comp_desc )  {
1143                 if ( k ) CompFree ( mem_op, k );
1144                 return LDAP_DECODING_ERROR;
1145         }
1146         k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentOcts;
1147         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentOcts;
1148         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentOcts;
1149         k->comp_desc->cd_free = (comp_free_func*)FreeComponentOcts;
1150         k->comp_desc->cd_extract_i = NULL;
1151         k->comp_desc->cd_type = ASN_BASIC;
1152         k->comp_desc->cd_type_id = BASICTYPE_OCTETSTRING;
1153         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentOcts;
1154         return LDAP_SUCCESS;
1155 }
1156
1157 /*
1158  * Matching function : OBJECT IDENTIFIER
1159  */
1160 int
1161 MatchingComponentOid ( char *oid, ComponentSyntaxInfo *csi_attr ,
1162                         ComponentSyntaxInfo *csi_assert )
1163 {
1164         int rc;
1165         MatchingRule* mr;
1166         ComponentOid *a, *b;
1167                                                                           
1168         if( oid ) {
1169                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
1170                 if ( mr )
1171                         return component_value_match( mr, csi_attr , csi_assert );
1172         }
1173
1174         a = (ComponentOid*)csi_attr;
1175         b = (ComponentOid*)csi_assert;
1176         if ( a->value.octetLen != b->value.octetLen )
1177                 return LDAP_COMPARE_FALSE;
1178         rc = ( strncmp( a->value.octs, b->value.octs, a->value.octetLen ) == 0 );
1179                                                                           
1180         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
1181 }
1182
1183 /*
1184  * GSER Encoder : OID
1185  */
1186 GEncComponentOid ( GenBuf *b, ComponentOid *in )
1187 {
1188         GAsnOid t = {0};
1189
1190         if ( !in || in->value.octetLen <= 0 )
1191                 return (-1);
1192         t.value = in->value;
1193         return GEncAsnOidContent( b, (GAsnOcts*)&t );
1194 }
1195
1196 /*
1197  * GSER Decoder : OID
1198  */
1199 int
1200 GDecAsnDescOidContent ( void* mem_op, GenBuf *b, GAsnOid *result, AsnLen *bytesDecoded ){
1201         AttributeType *ad_type;
1202         struct berval name;
1203         char* peek_head;
1204         int strLen;
1205
1206         strLen = LocateNextGSERToken ( mem_op, b, &peek_head, GSER_NO_COPY );
1207         name.bv_val = peek_head;
1208         name.bv_len = strLen;
1209
1210         ad_type = at_bvfind( &name );
1211
1212         if ( !ad_type )
1213                 return LDAP_DECODING_ERROR;
1214
1215         peek_head = ad_type->sat_atype.at_oid;
1216         strLen = strlen ( peek_head );
1217
1218         result->value.octs = (char*)EncodeComponentOid ( mem_op, peek_head , &strLen );
1219         result->value.octetLen = strLen;
1220         return LDAP_SUCCESS;
1221 }
1222
1223 int
1224 IsNumericOid ( char* peek_head , int strLen ) {
1225         int i;
1226         int num_dot;
1227         for ( i = 0, num_dot = 0 ; i < strLen ; i++ ) {
1228                 if ( peek_head[i] == '.' ) num_dot++;
1229                 else if ( peek_head[i] > '9' || peek_head[i] < '0' )
1230                         return (-1);
1231         }
1232         if ( num_dot )
1233                 return (1);
1234         else
1235                 return (-1);
1236 }
1237
1238 int
1239 GDecComponentOid ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1240 {
1241         char* peek_head;
1242         int i, strLen, rc;
1243         void* component_values;
1244         ComponentOid* k, **k2;
1245         GAsnOid result;
1246                                                                           
1247         k = (ComponentOid*) v;
1248                                                                           
1249         if ( mode & DEC_ALLOC_MODE_0 ) {
1250                 k2 = (ComponentOid**) v;
1251                 *k2 = (ComponentOid*) CompAlloc( mem_op, sizeof( ComponentOid ) );
1252                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1253                 k = *k2;
1254         }
1255
1256         strLen = LocateNextGSERToken ( mem_op, b, &peek_head, GSER_PEEK );
1257         if ( IsNumericOid ( peek_head , strLen ) >= 1 ) {
1258                 /* numeric-oid */
1259                 if ( GDecAsnOidContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
1260                         if ( k ) CompFree ( mem_op, k );
1261                         return LDAP_DECODING_ERROR;
1262                 }
1263         }
1264         else {
1265                 /*descr*/
1266                 if ( GDecAsnDescOidContent ( mem_op, b, &result, bytesDecoded ) < 0 ){
1267                         if ( k ) CompFree ( mem_op, k );
1268                         return LDAP_DECODING_ERROR;
1269                 }
1270         }
1271         k->value = result.value;
1272         k->comp_desc = get_component_description (BASICTYPE_OID);
1273
1274         return LDAP_SUCCESS;
1275 }
1276
1277 /*
1278  * Component BER Decoder : OID
1279  */
1280 int
1281 BDecComponentOidTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1282         return BDecComponentOid ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1283 }
1284
1285 int
1286 BDecComponentOid ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
1287                         AsnLen *bytesDecoded, int mode )
1288 {
1289         char* peek_head;
1290         int i, strLen, rc;
1291         void* component_values;
1292         ComponentOid* k, **k2;
1293         AsnOid result;
1294                                                                           
1295         k = (ComponentOid*) v;
1296                                                                           
1297         if ( mode & DEC_ALLOC_MODE_0 ) {
1298                 k2 = (ComponentOid**) v;
1299                 *k2 = (ComponentOid*) CompAlloc( mem_op, sizeof( ComponentOid ) );
1300                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1301                 k = *k2;
1302         }
1303         
1304         if ( mode & CALL_TAG_DECODER ){
1305                 mode = mode & CALL_CONTENT_DECODER;
1306                 rc = BDecAsnOid ( mem_op, b, &result, bytesDecoded );
1307         } else {
1308                 rc = BDecAsnOidContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1309         }
1310         if ( rc < 0 ) {
1311                 if ( k ) CompFree ( mem_op, k );
1312                 return LDAP_DECODING_ERROR;
1313         }
1314         k->value = result;
1315
1316         k->comp_desc = get_component_description (BASICTYPE_OID);
1317
1318         return LDAP_SUCCESS;
1319 }
1320
1321 /*
1322  * Component BER Decoder : PrintiableString
1323  */
1324
1325 int
1326 BDecComponentPrintableStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1327 {
1328         return BDecComponentPrintableString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1329 }
1330
1331 int
1332 BDecComponentPrintableString( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1333 {
1334         char* peek_head;
1335         int i, strLen, rc;
1336         void* component_values;
1337         ComponentPrintableString* k, **k2;
1338         AsnOid result;
1339                                                                           
1340         k = (ComponentPrintableString*) v;
1341                                                                           
1342         if ( mode & DEC_ALLOC_MODE_0 ) {
1343                 k2 = (ComponentPrintableString**) v;
1344                 *k2 = (ComponentPrintableString*) CompAlloc( mem_op, sizeof( ComponentPrintableString ) );
1345                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1346                 k = *k2;
1347         }
1348
1349         if ( mode & CALL_TAG_DECODER ) {
1350                 mode = mode & CALL_CONTENT_DECODER;
1351                 rc = BDecPrintableString ( mem_op, b, &result, bytesDecoded );
1352         } else {
1353                 rc = BDecPrintableStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1354         }
1355         if ( rc < 0 ) {
1356                 if ( k ) CompFree ( mem_op, k );
1357                 return LDAP_DECODING_ERROR;
1358         }
1359         k->value = result;
1360
1361         k->comp_desc = get_component_description (BASICTYPE_PRINTABLE_STR);
1362
1363         return LDAP_SUCCESS;
1364 }
1365
1366 /*
1367  * Matching function : Real
1368  */
1369 int
1370 MatchingComponentReal (char* oid, ComponentSyntaxInfo *csi_attr,
1371                         ComponentSyntaxInfo *csi_assert )
1372 {
1373         int rc;
1374         MatchingRule* mr;
1375         ComponentReal *a, *b;
1376                                                                           
1377         if( oid ) {
1378                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
1379                 if ( mr )
1380                         return component_value_match( mr, csi_attr , csi_assert );
1381         }
1382         a = (ComponentReal*)csi_attr;
1383         b = (ComponentReal*)csi_assert;
1384         rc = (a->value == b->value);
1385                                                                           
1386         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
1387 }
1388
1389 /*
1390  * GSER Encoder : Real
1391  */
1392 int
1393 GEncComponentReal ( GenBuf *b, ComponentReal *in )
1394 {
1395         GAsnReal t = {0};
1396         if ( !in )
1397                 return (-1);
1398         t.value = in->value;
1399         return GEncAsnRealContent ( b, &t );
1400 }
1401
1402 /*
1403  * GSER Decoder : Real
1404  */
1405 int
1406 GDecComponentReal ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1407 {
1408         char* peek_head;
1409         int i, strLen;
1410         void* component_values;
1411         ComponentReal* k, **k2;
1412         GAsnReal result;
1413                                                                           
1414         k = (ComponentReal*) v;
1415                                                                           
1416         if ( mode & DEC_ALLOC_MODE_0 ) {
1417                 k2 = (ComponentReal**) v;
1418                 *k2 = (ComponentReal*) CompAlloc( mem_op, sizeof( ComponentReal ) );
1419                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1420                 k = *k2;
1421         }
1422
1423         if ( GDecAsnRealContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
1424                 if ( k ) CompFree ( mem_op, k );
1425                 return LDAP_DECODING_ERROR;
1426         }
1427         k->value = result.value;
1428         k->comp_desc = get_component_description (BASICTYPE_REAL);
1429
1430         return LDAP_SUCCESS;
1431 }
1432
1433 /*
1434  * Component BER Decoder : Real
1435  */
1436 int
1437 BDecComponentRealTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1438         return BDecComponentReal ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1439 }
1440
1441 int
1442 BDecComponentReal ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1443 {
1444         char* peek_head;
1445         int i, strLen, rc;
1446         void* component_values;
1447         ComponentReal* k, **k2;
1448         AsnReal result;
1449                                                                           
1450         k = (ComponentReal*) v;
1451                                                                           
1452         if ( mode & DEC_ALLOC_MODE_0 ) {
1453                 k2 = (ComponentReal**) v;
1454                 *k2 = (ComponentReal*) CompAlloc( mem_op, sizeof( ComponentReal ) );
1455                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1456                 k = *k2;
1457         }
1458
1459         if ( mode & CALL_TAG_DECODER ){
1460                 mode = mode & CALL_CONTENT_DECODER;
1461                 rc = BDecAsnReal ( mem_op, b, &result, bytesDecoded );
1462         } else {
1463                 rc = BDecAsnRealContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1464         }
1465         if ( rc < 0 ) {
1466                 if ( k ) CompFree ( mem_op, k );
1467                 return LDAP_DECODING_ERROR;
1468         }
1469         k->value = result;
1470         k->comp_desc = get_component_description (BASICTYPE_REAL);
1471
1472         return LDAP_SUCCESS;
1473 }
1474
1475 /*
1476  * Matching function : Relative OID
1477  */
1478 int
1479 MatchingComponentRelativeOid ( char* oid, ComponentSyntaxInfo *csi_attr,
1480                                         ComponentSyntaxInfo *csi_assert )
1481 {
1482         int rc;
1483         MatchingRule* mr;
1484         ComponentRelativeOid *a, *b;
1485                                                                           
1486         if( oid ) {
1487                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
1488                 if ( mr )
1489                         return component_value_match( mr, csi_attr , csi_assert );
1490         }
1491
1492         a = (ComponentRelativeOid*)csi_attr;
1493         b = (ComponentRelativeOid*)csi_assert;
1494
1495         if ( a->value.octetLen != b->value.octetLen )
1496                 return LDAP_COMPARE_FALSE;
1497         rc = ( strncmp( a->value.octs, b->value.octs, a->value.octetLen ) == 0 );
1498                                                                           
1499         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
1500 }
1501
1502 /*
1503  * GSER Encoder : RELATIVE_OID.
1504  */
1505 int
1506 GEncComponentRelativeOid ( GenBuf *b, ComponentRelativeOid *in )
1507 {
1508         GAsnRelativeOid t = {0};
1509
1510         if ( !in || in->value.octetLen <= 0 )
1511                 return (-1);
1512         t.value = in->value;
1513         return GEncAsnRelativeOidContent ( b , (GAsnOcts*)&t );
1514 }
1515
1516 /*
1517  * GSER Decoder : RELATIVE_OID.
1518  */
1519 int
1520 GDecComponentRelativeOid ( void* mem_op, GenBuf *b,void *v, AsnLen *bytesDecoded, int mode )
1521 {
1522         char* peek_head;
1523         int i, strLen;
1524         void* component_values;
1525         ComponentRelativeOid* k, **k2;
1526         GAsnRelativeOid result;
1527                                                                           
1528         k = (ComponentRelativeOid*) v;
1529                                                                           
1530         if ( mode & DEC_ALLOC_MODE_0 ) {
1531                 k2 = (ComponentRelativeOid**) v;
1532                 *k2 = (ComponentRelativeOid*) CompAlloc( mem_op, sizeof( ComponentRelativeOid ) );
1533                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1534                 k = *k2;
1535         }
1536         
1537         if ( GDecAsnRelativeOidContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
1538                 if ( k ) CompFree ( mem_op, k );
1539                 return LDAP_DECODING_ERROR;
1540         }
1541         k->value = result.value;
1542         k->comp_desc = get_component_description (BASICTYPE_OID);
1543
1544         return LDAP_SUCCESS;
1545 }
1546
1547 /*
1548  * Component BER Decoder : RELATIVE_OID.
1549  */
1550 int
1551 BDecComponentRelativeOidTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1552         return BDecComponentRelativeOid ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1553 }
1554
1555 int
1556 BDecComponentRelativeOid ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1557 {
1558         char* peek_head;
1559         int i, strLen, rc;
1560         void* component_values;
1561         ComponentRelativeOid* k, **k2;
1562         AsnRelativeOid result;
1563                                                                           
1564         k = (ComponentRelativeOid*) v;
1565                                                                           
1566         if ( mode & DEC_ALLOC_MODE_0 ) {
1567                 k2 = (ComponentRelativeOid**) v;
1568                 *k2 = (ComponentRelativeOid*) CompAlloc( mem_op, sizeof( ComponentRelativeOid ) );
1569                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1570                 k = *k2;
1571         }
1572         
1573         if ( mode & CALL_TAG_DECODER ){
1574                 mode = mode & CALL_CONTENT_DECODER;
1575                 rc = BDecAsnRelativeOid ( mem_op, b, &result, bytesDecoded );
1576         } else {
1577                 rc = BDecAsnRelativeOidContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1578         }
1579         if ( rc < 0 ) {
1580                 if ( k ) CompFree ( mem_op, k );
1581                 return LDAP_DECODING_ERROR;
1582         }
1583         k->value = result;
1584         k->comp_desc = get_component_description (BASICTYPE_OID);
1585
1586         return LDAP_SUCCESS;
1587 }
1588
1589 /*
1590  * GSER Encoder : UniversalString
1591  */
1592 int
1593 GEncComponentUniversalString ( GenBuf *b, ComponentUniversalString *in )
1594 {
1595         GUniversalString t = {0};
1596         if ( !in || in->value.octetLen <= 0 )
1597                 return (-1);
1598         t.value = in->value;
1599         return GEncUniversalStringContent( b, &t );
1600 }
1601
1602 /*
1603  * GSER Decoder : UniversalString
1604  */
1605 static int
1606 UTF8toUniversalString( char* octs, int len){
1607         /* Need to be Implemented */
1608         return LDAP_SUCCESS;
1609 }
1610
1611 int
1612 GDecComponentUniversalString ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1613 {
1614         if ( GDecComponentUTF8String ( mem_op, b, v, bytesDecoded, mode) < 0 )
1615         UTF8toUniversalString( ((ComponentUniversalString*)v)->value.octs, ((ComponentUniversalString*)v)->value.octetLen );
1616                 return LDAP_DECODING_ERROR;
1617 }
1618
1619 /*
1620  * Component BER Decoder : UniverseString
1621  */
1622 int
1623 BDecComponentUniversalStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1624         return BDecComponentUniversalString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1625 }
1626
1627 int
1628 BDecComponentUniversalString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1629 {
1630         char* peek_head;
1631         int i, strLen, rc;
1632         void* component_values;
1633         ComponentUniversalString* k, **k2;
1634         UniversalString result;
1635
1636         k = (ComponentUniversalString*) v;
1637
1638         if ( mode & DEC_ALLOC_MODE_0 ) {
1639                 k2 = (ComponentUniversalString**) v;
1640                 *k2 = (ComponentUniversalString*) CompAlloc( mem_op, sizeof( ComponentUniversalString ) );
1641                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1642                 k = *k2;
1643         }
1644         
1645         if ( mode & CALL_TAG_DECODER ){
1646                 mode = mode & CALL_CONTENT_DECODER;
1647                 rc = BDecUniversalString ( mem_op, b, &result, bytesDecoded );
1648         } else {
1649                 rc = BDecUniversalStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1650         }
1651         if ( rc < 0 ) {
1652                 if ( k ) CompFree ( mem_op, k );
1653                 return LDAP_DECODING_ERROR;
1654         }
1655         k->value = result;
1656         k->comp_desc = get_component_description (BASICTYPE_UNIVERSAL_STR);
1657
1658         return LDAP_SUCCESS;
1659 }
1660
1661 /*
1662  * Component BER Decoder : VisibleString
1663  */
1664 int
1665 BDecComponentVisibleStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1666         return BDecComponentVisibleString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1667 }
1668
1669 int
1670 BDecComponentVisibleString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1671 {
1672         char* peek_head;
1673         int i, strLen, rc;
1674         void* component_values;
1675         ComponentVisibleString* k, **k2;
1676         VisibleString result;
1677                                                                           
1678         k = (ComponentVisibleString*) v;
1679                                                                           
1680         if ( mode & DEC_ALLOC_MODE_0 ) {
1681                 k2 = (ComponentVisibleString**) v;
1682                 *k2 = (ComponentVisibleString*) CompAlloc( mem_op, sizeof( ComponentVisibleString ) );
1683                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1684                 k = *k2;
1685         }
1686         
1687         if ( mode & CALL_TAG_DECODER ){
1688                 mode = mode & CALL_CONTENT_DECODER;
1689                 rc = BDecVisibleString ( mem_op, b, &result, bytesDecoded );
1690         } else {
1691                 rc = BDecVisibleStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1692         }
1693         k->value = result;
1694         k->comp_desc = get_component_description (BASICTYPE_VISIBLE_STR);
1695
1696         return LDAP_SUCCESS;
1697 }
1698
1699 /*
1700  * Routines for handling an ANY DEFINED Type
1701  */
1702
1703 /* Check if the <select> type CR and the OID of the given ANY type */
1704 int
1705 CheckSelectTypeCorrect ( void* mem_op, ComponentAnyInfo* cai, struct berval* select ) {
1706         int strLen;
1707         AttributeType* ad_type;
1708         char* oid;
1709         char* result;
1710
1711         if ( IsNumericOid ( select->bv_val , select->bv_len ) ) {
1712                 oid = select->bv_val;
1713                 strLen = select->bv_len;
1714         } else {
1715                 ad_type = at_bvfind( select );
1716
1717                 if ( !ad_type )
1718                         return LDAP_DECODING_ERROR;
1719
1720                 oid = ad_type->sat_atype.at_oid;
1721                 strLen = strlen ( oid );
1722         }
1723         result = EncodeComponentOid ( mem_op, oid , &strLen );
1724         if ( !result || strLen <= 0 ) return (-1);
1725
1726         if ( cai->oid.octetLen == strLen &&
1727                 strncmp ( cai->oid.octs, result, strLen ) == 0 )
1728                 return (1);
1729         else
1730                 return (-1);
1731 }
1732
1733 int
1734 SetAnyTypeByComponentOid ( ComponentAny *v, ComponentOid *id ) {
1735         Hash hash;
1736         void *anyInfo;
1737
1738         /* use encoded oid as hash string */
1739         hash = MakeHash (id->value.octs, id->value.octetLen);
1740         if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
1741                 v->cai = (ComponentAnyInfo*) anyInfo;
1742         else
1743                 v->cai = NULL;
1744
1745         if ( !v->cai ) {
1746         /*
1747          * If not found, the data considered as octet chunk
1748          * Yet-to-be-Implemented
1749          */
1750         }
1751         return LDAP_SUCCESS;
1752 }
1753
1754 void
1755 SetAnyTypeByComponentInt( ComponentAny *v, ComponentInt id) {
1756         Hash hash;
1757         void *anyInfo;
1758
1759         hash = MakeHash ((char*)&id, sizeof (id));
1760         if (CheckForAndReturnValue (anyIntHashTblG, hash, &anyInfo))
1761                 v->cai = (ComponentAnyInfo*) anyInfo;
1762         else
1763                 v->cai = NULL;
1764 }
1765
1766 int
1767 GEncComponentAny ( GenBuf *b, ComponentAny *in )
1768 {
1769         if ( in->cai != NULL  && in->cai->Encode != NULL )
1770                 return in->cai->Encode(b, &in->value );
1771         else
1772                 return (-1);
1773 }
1774
1775 int
1776 BEncComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode)
1777 {
1778         ComponentAny *k, **k2;
1779                                                                           
1780         k = (ComponentAny*) result;
1781
1782         if ( !k ) return (-1);
1783                                                                           
1784         if ( mode & DEC_ALLOC_MODE_0 ) {
1785                 k2 = (ComponentAny**) result;
1786                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
1787                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1788                 k = *k2;
1789         }
1790         
1791         if ((result->cai != NULL) && (result->cai->BER_Decode != NULL)) {
1792                 result->value = (void*) CompAlloc ( mem_op, result->cai->size );
1793                 if ( !result->value ) return 0;
1794                 result->cai->BER_Decode ( mem_op, b, result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_1);
1795
1796                 k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1797                 if ( !k->comp_desc )  {
1798                         if ( k ) CompFree ( mem_op, k );
1799                         return LDAP_DECODING_ERROR;
1800                 }
1801                 k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
1802                 k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
1803                 k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
1804                 k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
1805                 k->comp_desc->cd_extract_i = NULL;
1806                 k->comp_desc->cd_type = ASN_BASIC;
1807                 k->comp_desc->cd_type_id = BASICTYPE_ANY;
1808                 k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
1809                 return LDAP_SUCCESS;
1810         }
1811         else {
1812                 Asn1Error ("ERROR - Component ANY Decode routine is NULL\n");
1813                 return 0;
1814         }
1815 }
1816
1817 int
1818 BDecComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode) {
1819         int rc;
1820         ComponentAny *k, **k2;
1821                                                                           
1822         k = (ComponentAny*) result;
1823
1824         if ( !k ) return (-1);
1825                                                                           
1826         if ( mode & DEC_ALLOC_MODE_0 ) {
1827                 k2 = (ComponentAny**) result;
1828                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
1829                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1830                 k = *k2;
1831         }
1832         
1833         if ((result->cai != NULL) && (result->cai->BER_Decode != NULL)) {
1834                 result->cai->BER_Decode ( mem_op, b, (ComponentSyntaxInfo*)&result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_0 );
1835
1836                 k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1837                 if ( !k->comp_desc )  {
1838                         if ( k ) CompFree ( mem_op, k );
1839                         return LDAP_DECODING_ERROR;
1840                 }
1841                 k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
1842                 k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
1843                 k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
1844                 k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
1845                 k->comp_desc->cd_extract_i = NULL;
1846                 k->comp_desc->cd_type = ASN_BASIC;
1847                 k->comp_desc->cd_type_id = BASICTYPE_ANY;
1848                 k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
1849                 return LDAP_SUCCESS;
1850         }
1851         else {
1852                 Asn1Error ("ERROR - Component ANY Decode routine is NULL\n");
1853                 return 0;
1854         }
1855 }
1856
1857 int
1858 GDecComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode) {
1859         ComponentAny *k, **k2;
1860                                                                           
1861         k = (ComponentAny*) result;
1862                                                                           
1863         if ( mode & DEC_ALLOC_MODE_0 ) {
1864                 k2 = (ComponentAny**) result;
1865                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
1866                 if ( !*k2 ) return LDAP_DECODING_ERROR;
1867                 k = *k2;
1868         }
1869         if ((result->cai != NULL) && (result->cai->GSER_Decode != NULL)) {
1870                 result->value = (void*) CompAlloc ( mem_op, result->cai->size );
1871                 if ( !result->value ) return 0;
1872                 result->cai->GSER_Decode ( mem_op, b, result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_1);
1873                 k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1874                 if ( !k->comp_desc )  {
1875                         if ( k ) CompFree ( mem_op, k );
1876                         return LDAP_DECODING_ERROR;
1877                 }
1878                 k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
1879                 k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
1880                 k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
1881                 k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
1882                 k->comp_desc->cd_type = ASN_BASIC;
1883                 k->comp_desc->cd_extract_i = NULL;
1884                 k->comp_desc->cd_type_id = BASICTYPE_ANY;
1885                 k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
1886                 return LDAP_SUCCESS;
1887         }
1888         else {
1889                 Asn1Error ("ERROR - ANY Decode routine is NULL\n");
1890                 return 0;
1891         }
1892 }
1893
1894 int
1895 MatchingComponentAny (char* oid, ComponentAny *result, ComponentAny *result2) {
1896         void *comp1, *comp2;
1897
1898         if ( result->comp_desc->cd_type_id == BASICTYPE_ANY )
1899                 comp1 = result->value;
1900         else
1901                 comp1 = result;
1902
1903         if ( result2->comp_desc->cd_type_id == BASICTYPE_ANY )
1904                 comp2 = result2->value;
1905         else
1906                 comp2 = result2;
1907                 
1908         if ((result->cai != NULL) && (result->cai->Match != NULL)) {
1909                 if ( result->comp_desc->cd_type_id == BASICTYPE_ANY )
1910                         return result->cai->Match(oid, comp1, comp2 );
1911                 else if ( result2->comp_desc->cd_type_id == BASICTYPE_ANY )
1912                         return result2->cai->Match(oid, comp1, comp2);
1913                 else 
1914                         return LDAP_INVALID_SYNTAX;
1915         }
1916         else {
1917                 Asn1Error ("ERROR - ANY Matching routine is NULL\n");
1918                 return LDAP_INVALID_SYNTAX;
1919         }
1920 }
1921
1922 void*
1923 ExtractingComponentAny ( void* mem_op, ComponentReference* cr,  ComponentAny *result ) {
1924         if ((result->cai != NULL) && (result->cai->Extract != NULL)) {
1925                 return (void*) result->cai->Extract( mem_op, cr , result->value );
1926         }
1927         else {
1928                 Asn1Error ("ERROR - ANY Extracting routine is NULL\n");
1929                 return (void*)NULL;
1930         }
1931 }
1932
1933 void
1934 FreeComponentAny (ComponentAny* any) {
1935         if ( any->cai != NULL && any->cai->Free != NULL ) {
1936                 any->cai->Free( any->value );
1937                 free ( ((ComponentSyntaxInfo*)any->value)->csi_comp_desc );
1938                 free ( any->value );
1939         }
1940         else
1941                 Asn1Error ("ERROR - ANY Free routine is NULL\n");
1942 }
1943
1944 void
1945 InstallAnyByComponentInt (int anyId, ComponentInt intId, unsigned int size,
1946                         EncodeFcn encode, gser_decoder_func* G_decode,
1947                         ber_tag_decoder_func* B_decode, ExtractFcn extract,
1948                         MatchFcn match, FreeFcn free,
1949                         PrintFcn print)
1950 {
1951         ComponentAnyInfo *a;
1952         Hash h;
1953
1954         a = (ComponentAnyInfo*) malloc(sizeof (ComponentAnyInfo));
1955         a->anyId = anyId;
1956         a->oid.octs = NULL;
1957         a->oid.octetLen = 0;
1958         a->intId = intId;
1959         a->size = size;
1960         a->Encode = encode;
1961         a->GSER_Decode = G_decode;
1962         a->BER_Decode = B_decode;
1963         a->Match = match;
1964         a->Extract = extract;
1965         a->Free = free;
1966         a->Print = print;
1967
1968         if (anyIntHashTblG == NULL)
1969                 anyIntHashTblG = InitHash();
1970
1971         h = MakeHash ((char*)&intId, sizeof (intId));
1972
1973         if(anyIntHashTblG != NULL)
1974                 Insert(anyIntHashTblG, a, h);
1975 }
1976
1977
1978 /*
1979  * OID and its corresponding decoder can be registerd with this func.
1980  * If contained types constrained by <select> are used,
1981  * their OID and decoder MUST be registered, otherwise it will return no entry.
1982  * An open type(ANY type) also need be registered.
1983  */
1984 void
1985 InstallOidDecoderMapping ( char* ch_oid, EncodeFcn encode, gser_decoder_func* G_decode, ber_tag_decoder_func* B_decode, ExtractFcn extract, MatchFcn match ) {
1986         AsnOid oid;
1987         int strLen;
1988         void* mem_op;
1989
1990         strLen = strlen( ch_oid );
1991         if( strLen <= 0 ) return;
1992         mem_op = comp_nibble_memory_allocator ( 128, 16 );
1993         oid.octs = EncodeComponentOid ( mem_op, ch_oid, &strLen );
1994         oid.octetLen = strLen;
1995         if( strLen <= 0 ) return;
1996         
1997
1998         InstallAnyByComponentOid ( 0, &oid, 0, encode, G_decode, B_decode,
1999                                                 extract, match, NULL, NULL);
2000         comp_nibble_memory_free(mem_op);
2001 }
2002
2003 /*
2004  * Look up Oid-decoder mapping table by berval have either
2005  * oid or description
2006  */
2007 OidDecoderMapping*
2008 RetrieveOidDecoderMappingbyBV( struct berval* in ) {
2009         if ( IsNumericOid ( in->bv_val, in->bv_len ) )
2010                 return RetrieveOidDecoderMappingbyOid( in->bv_val, in->bv_len );
2011         else
2012                 return RetrieveOidDecoderMappingbyDesc( in->bv_val, in->bv_len );
2013 }
2014
2015 /*
2016  * Look up Oid-decoder mapping table by dotted OID
2017  */
2018 OidDecoderMapping*
2019 RetrieveOidDecoderMappingbyOid( char* ch_oid, int oid_len ) {
2020         Hash hash;
2021         void *anyInfo;
2022         AsnOid oid;
2023         int strLen;
2024         void* mem_op;
2025
2026         mem_op = comp_nibble_memory_allocator ( 128, 16 );
2027         oid.octs = EncodeComponentOid ( mem_op, ch_oid, &oid_len);
2028         oid.octetLen = oid_len;
2029         if( oid_len <= 0 ) {
2030                 comp_nibble_memory_free( mem_op );
2031                 return NULL;
2032         }
2033         
2034         /* use encoded oid as hash string */
2035         hash = MakeHash ( oid.octs, oid.octetLen);
2036         comp_nibble_memory_free( mem_op );
2037         if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
2038                 return (OidDecoderMapping*) anyInfo;
2039         else
2040                 return (OidDecoderMapping*) NULL;
2041
2042 }
2043
2044 /*
2045  * Look up Oid-decoder mapping table by description
2046  */
2047 OidDecoderMapping*
2048 RetrieveOidDecoderMappingbyDesc( char* desc, int desc_len ) {
2049         Hash hash;
2050         void *anyInfo;
2051         AsnOid oid;
2052         AttributeType* ad_type;
2053         struct berval bv;
2054         void* mem_op;
2055
2056         bv.bv_val = desc;
2057         bv.bv_len = desc_len;
2058         ad_type = at_bvfind( &bv );
2059
2060         oid.octs = ad_type->sat_atype.at_oid;
2061         oid.octetLen = strlen ( oid.octs );
2062
2063         if ( !ad_type )
2064                 return (OidDecoderMapping*) NULL;
2065
2066         mem_op = comp_nibble_memory_allocator ( 128, 16 );
2067
2068         oid.octs = EncodeComponentOid ( mem_op, oid.octs , (int*)&oid.octetLen );
2069         if( oid.octetLen <= 0 ) {
2070                 comp_nibble_memory_free( mem_op );
2071                 return (OidDecoderMapping*) NULL;
2072         }
2073         
2074         /* use encoded oid as hash string */
2075         hash = MakeHash ( oid.octs, oid.octetLen);
2076         comp_nibble_memory_free( mem_op );
2077         if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
2078                 return (OidDecoderMapping*) anyInfo;
2079         else
2080                 return (OidDecoderMapping*) NULL;
2081
2082 }
2083 void
2084 InstallAnyByComponentOid (int anyId, AsnOid *oid, unsigned int size,
2085                         EncodeFcn encode, gser_decoder_func* G_decode,
2086                         ber_tag_decoder_func* B_decode, ExtractFcn extract,
2087                          MatchFcn match, FreeFcn free, PrintFcn print)
2088 {
2089         ComponentAnyInfo *a;
2090         Hash h;
2091
2092         a = (ComponentAnyInfo*) malloc (sizeof (ComponentAnyInfo));
2093         a->anyId = anyId;
2094         if ( oid ) {
2095                 a->oid.octs = malloc( oid->octetLen );
2096                 memcpy ( a->oid.octs, oid->octs, oid->octetLen );
2097                 a->oid.octetLen = oid->octetLen;
2098         }
2099         a->size = size;
2100         a->Encode = encode;
2101         a->GSER_Decode = G_decode;
2102         a->BER_Decode = B_decode;
2103         a->Match = match;
2104         a->Extract = extract;
2105         a->Free = free;
2106         a->Print = print;
2107
2108         h = MakeHash (oid->octs, oid->octetLen);
2109
2110         if (anyOidHashTblG == NULL)
2111                 anyOidHashTblG = InitHash();
2112
2113         if(anyOidHashTblG != NULL)
2114                 Insert(anyOidHashTblG, a, h);
2115 }
2116
2117 int
2118 BDecComponentTop  (
2119 ber_decoder_func *decoder _AND_
2120 void* mem_op _AND_
2121 GenBuf *b _AND_
2122 AsnTag tag _AND_
2123 AsnLen elmtLen _AND_
2124 void **v _AND_
2125 AsnLen *bytesDecoded _AND_
2126 int mode) {
2127         tag = BDecTag ( b, bytesDecoded );
2128         elmtLen = BDecLen ( b, bytesDecoded );
2129         if ( elmtLen <= 0 ) return (-1);
2130         if ( tag != MAKE_TAG_ID (UNIV, CONS, SEQ_TAG_CODE) ) {
2131                 return (-1);
2132         }
2133                 
2134         return (*decoder)( mem_op, b, tag, elmtLen, (ComponentSyntaxInfo*)v,(int*)bytesDecoded, mode );
2135 }
2136
2137 /*
2138  * ASN.1 specification of a distinguished name
2139  * DistinguishedName ::= RDNSequence
2140  * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
2141  * RelativeDistinguishedName ::= SET SIZE(1..MAX) OF AttributeTypeandValue
2142  * AttributeTypeandValue ::= SEQUENCE {
2143  *      type    AttributeType
2144  *      value   AttributeValue
2145  * }
2146  * When dnMatch/rdnMatch is used in a component assertion value
2147  * the component in DistinguishedName/RelativeDistinguishedName
2148  * need to be converted to the LDAP encodings in RFC2253
2149  * in order to be matched against the assertion value
2150  * If allComponentMatch is used, the assertion value may be
2151  * decoded into the Internal Representation(Component Tree)
2152  * by the corresponding GSER or BER decoder
2153  * Following routine converts a component tree(DistinguishedName) into
2154  * LDAP encodings in RFC2253
2155  * Example)
2156  * IR : ComponentRDNSequence
2157  * GSER : { { type cn, value sang },{ type o, value ibm}, {type c, value us} }
2158  * LDAP Encodings : cn=sang,o=ibm,c=us 
2159  */
2160
2161 increment_bv_mem_by_size ( struct berval* in, int size ) {
2162         int new_size = in->bv_len + size;
2163         in->bv_val = realloc( in->bv_val, new_size );
2164         in->bv_len = new_size;
2165 }
2166
2167 int
2168 ConvertBER2Desc( char* in, int size, struct berval* out, int* pos ) {
2169         int desc_size;
2170         char* desc_ptr;
2171         unsigned int firstArcNum;
2172         unsigned int arcNum;
2173         int i, rc, start_pos = *pos;
2174         char buf[MAX_OID_LEN];
2175         AttributeType *at;
2176         struct berval bv_name;
2177
2178         /*convert BER oid to desc*/
2179         for ( i = 0, arcNum = 0; (i < size) && (in[i] & 0x80 ); i++ )
2180                 arcNum = (arcNum << 7) + (in[i] & 0x7f);
2181         arcNum = (arcNum << 7) + (in[i] & 0x7f);
2182         i++;
2183         firstArcNum = (unsigned short)(arcNum/40);
2184         if ( firstArcNum > 2 )
2185                 firstArcNum = 2;
2186         
2187         arcNum = arcNum - (firstArcNum * 40 );
2188
2189         rc = intToAscii ( arcNum, buf );
2190
2191         /*check if the buffer can store the first/second arc and two dots*/
2192         if ( out->bv_len < *pos + 2 + 1 + rc )
2193                 increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2194
2195         if ( firstArcNum == 1)
2196                 out->bv_val[*pos] = '1';
2197         else
2198                 out->bv_val[*pos] = '2';
2199         (*pos)++;
2200         out->bv_val[*pos] = '.';
2201         (*pos)++;
2202
2203         memcpy( out->bv_val + *pos, buf, rc );
2204         *pos += rc;
2205         out->bv_val[*pos] = '.';
2206         (*pos)++;
2207
2208         for ( ; i < size ; ) {
2209                 for ( arcNum=0; (i < size) && (in[i] & 0x80) ; i++ )
2210                         arcNum = (arcNum << 7) + (in[i] & 0x7f);
2211                 arcNum = (arcNum << 7) + (in[i] & 0x7f);
2212                 i++;
2213
2214                 rc = intToAscii ( arcNum, buf );
2215
2216                 if ( out->bv_len < *pos + rc + 1 )
2217                         increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2218
2219                 memcpy( out->bv_val + *pos, buf, rc );
2220                 *pos += rc;
2221                 out->bv_val[*pos] = '.';
2222                 (*pos)++;
2223         }
2224         (*pos)--;/*remove the last '.'*/
2225
2226         /*
2227          * lookup OID database to locate desc
2228          * then overwrite OID with desc in *out
2229          * If failed to look up desc, OID form is used
2230          */
2231         bv_name.bv_val = out->bv_val + start_pos;
2232         bv_name.bv_len = *pos - start_pos;
2233         at = at_bvfind( &bv_name );
2234         if ( !at )
2235                 return LDAP_SUCCESS;
2236         desc_size = at->sat_cname.bv_len;
2237         memcpy( out->bv_val + start_pos, at->sat_cname.bv_val, desc_size );
2238         *pos = start_pos + desc_size;
2239         return LDAP_SUCCESS;
2240 }
2241
2242 int
2243 ConvertComponentAttributeTypeAndValue2RFC2253 ( irAttributeTypeAndValue* in, struct berval* out, int *pos ) {
2244         int rc;
2245         int value_size = ((ComponentUTF8String*)in->value.value)->value.octetLen;
2246         char* value_ptr =  ((ComponentUTF8String*)in->value.value)->value.octs;
2247
2248         rc = ConvertBER2Desc( in->type.value.octs, in->type.value.octetLen, out, pos );
2249         if ( rc != LDAP_SUCCESS ) return rc;
2250         if ( out->bv_len < *pos + 1/*for '='*/  )
2251                 increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2252         /*Between type and value, put '='*/
2253         out->bv_val[*pos] = '=';
2254         (*pos)++;
2255
2256         /*Assume it is string*/         
2257         if ( out->bv_len < *pos + value_size )
2258                 increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2259         memcpy( out->bv_val + *pos, value_ptr, value_size );
2260         out->bv_len += value_size;
2261         *pos += value_size;
2262         
2263         return LDAP_SUCCESS;
2264 }
2265
2266 int
2267 ConvertRelativeDistinguishedName2RFC2253 ( irRelativeDistinguishedName* in, struct berval *out , int* pos) {
2268         irAttributeTypeAndValue* attr_typeNvalue;
2269         int rc;
2270
2271
2272         FOR_EACH_LIST_ELMT( attr_typeNvalue, &in->comp_list)
2273         {
2274                 rc = ConvertComponentAttributeTypeAndValue2RFC2253( attr_typeNvalue, out, pos );
2275                 if ( rc != LDAP_SUCCESS ) return LDAP_INVALID_SYNTAX;
2276
2277                 if ( out->bv_len < *pos + 1/*for '+'*/  )
2278                         increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2279                 /*between multivalued RDNs, put comma*/
2280                 out->bv_val[(*pos)++] = '+';
2281         }
2282         (*pos)--;/*remove the last '+'*/
2283         return LDAP_SUCCESS;
2284 }
2285
2286 int 
2287 ConvertRDN2RFC2253 ( irRelativeDistinguishedName* in, struct berval *out ) {
2288         int rc, pos = 0;
2289         out->bv_val = (char*)malloc( INITIAL_DN_SIZE );
2290         out->bv_len = INITIAL_DN_SIZE;
2291
2292         rc = ConvertRelativeDistinguishedName2RFC2253 ( in, out , &pos);
2293         if ( rc != LDAP_SUCCESS ) return rc;
2294         out->bv_val[pos] = '\0';
2295         out->bv_len = pos;
2296         return LDAP_SUCCESS;
2297 }
2298
2299 int
2300 ConvertRDNSequence2RFC2253( irRDNSequence *in, struct berval* out ) {
2301         irRelativeDistinguishedName* rdn_seq;
2302         AsnList* seq = &in->comp_list;
2303         int pos = 0, rc ;
2304
2305         out->bv_val = (char*)malloc( INITIAL_DN_SIZE );
2306         out->bv_len = INITIAL_DN_SIZE;
2307
2308         FOR_EACH_LIST_ELMT( rdn_seq, seq )
2309         {
2310                 rc = ConvertRelativeDistinguishedName2RFC2253( rdn_seq, out, &pos );
2311                 if ( rc != LDAP_SUCCESS ) return LDAP_INVALID_SYNTAX;
2312
2313                 if ( out->bv_len < pos + 1/*for ','*/ )
2314                         increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2315                 /*Between RDN, put comma*/
2316                 out->bv_val[pos++] = ',';
2317         }
2318         pos--;/*remove the last '+'*/
2319         out->bv_val[pos] = '\0';
2320         out->bv_len =pos;
2321         return LDAP_SUCCESS;
2322 }
2323
2324 #endif