xref: /netbsd-src/external/bsd/openldap/dist/contrib/slapd-modules/comp_match/componentlib.c (revision 3816d47b2c42fcd6e549e3407f842a5b1a1d23ad)
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  * Component BER Decoder : TeletexString
1368  */
1369 
1370 int
1371 BDecComponentTeletexStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1372 {
1373 	return BDecComponentTeletexString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1374 }
1375 
1376 int
1377 BDecComponentTeletexString( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1378 {
1379         char* peek_head;
1380         int i, strLen, rc;
1381         void* component_values;
1382         ComponentTeletexString* k, **k2;
1383 	AsnOid result;
1384 
1385         k = (ComponentTeletexString*) v;
1386 
1387         if ( mode & DEC_ALLOC_MODE_0 ) {
1388                 k2 = (ComponentTeletexString**) v;
1389                 *k2 = (ComponentTeletexString*) CompAlloc( mem_op, sizeof( ComponentTeletexString ) );
1390 		if ( !*k2 ) return LDAP_DECODING_ERROR;
1391                 k = *k2;
1392         }
1393 
1394 	if ( mode & CALL_TAG_DECODER ) {
1395 		mode = mode & CALL_CONTENT_DECODER;
1396 		rc = BDecTeletexString ( mem_op, b, &result, bytesDecoded );
1397 	} else {
1398 		rc = BDecTeletexStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1399 	}
1400 	if ( rc < 0 ) {
1401 		if ( k ) CompFree ( mem_op, k );
1402 		return LDAP_DECODING_ERROR;
1403 	}
1404 	k->value = result;
1405 
1406 	k->comp_desc = get_component_description (BASICTYPE_T61_STR);
1407 
1408 	return LDAP_SUCCESS;
1409 }
1410 
1411 
1412 /*
1413  * Matching function : Real
1414  */
1415 int
1416 MatchingComponentReal (char* oid, ComponentSyntaxInfo *csi_attr,
1417 			ComponentSyntaxInfo *csi_assert )
1418 {
1419         int rc;
1420         MatchingRule* mr;
1421         ComponentReal *a, *b;
1422 
1423         if( oid ) {
1424                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
1425                 if ( mr )
1426                         return component_value_match( mr, csi_attr , csi_assert );
1427         }
1428         a = (ComponentReal*)csi_attr;
1429         b = (ComponentReal*)csi_assert;
1430         rc = (a->value == b->value);
1431 
1432         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
1433 }
1434 
1435 /*
1436  * GSER Encoder : Real
1437  */
1438 int
1439 GEncComponentReal ( GenBuf *b, ComponentReal *in )
1440 {
1441 	GAsnReal t = {0};
1442 	if ( !in )
1443 		return (-1);
1444 	t.value = in->value;
1445 	return GEncAsnRealContent ( b, &t );
1446 }
1447 
1448 /*
1449  * GSER Decoder : Real
1450  */
1451 int
1452 GDecComponentReal ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1453 {
1454         char* peek_head;
1455         int i, strLen;
1456         void* component_values;
1457         ComponentReal* k, **k2;
1458 	GAsnReal result;
1459 
1460         k = (ComponentReal*) v;
1461 
1462         if ( mode & DEC_ALLOC_MODE_0 ) {
1463                 k2 = (ComponentReal**) v;
1464                 *k2 = (ComponentReal*) CompAlloc( mem_op, sizeof( ComponentReal ) );
1465 		if ( !*k2 ) return LDAP_DECODING_ERROR;
1466                 k = *k2;
1467         }
1468 
1469 	if ( GDecAsnRealContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
1470 		if ( k ) CompFree ( mem_op, k );
1471 		return LDAP_DECODING_ERROR;
1472 	}
1473 	k->value = result.value;
1474 	k->comp_desc = get_component_description (BASICTYPE_REAL);
1475 
1476         return LDAP_SUCCESS;
1477 }
1478 
1479 /*
1480  * Component BER Decoder : Real
1481  */
1482 int
1483 BDecComponentRealTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1484 	return BDecComponentReal ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1485 }
1486 
1487 int
1488 BDecComponentReal ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1489 {
1490         char* peek_head;
1491         int i, strLen, rc;
1492         void* component_values;
1493         ComponentReal* k, **k2;
1494 	AsnReal result;
1495 
1496         k = (ComponentReal*) v;
1497 
1498         if ( mode & DEC_ALLOC_MODE_0 ) {
1499                 k2 = (ComponentReal**) v;
1500                 *k2 = (ComponentReal*) CompAlloc( mem_op, sizeof( ComponentReal ) );
1501 		if ( !*k2 ) return LDAP_DECODING_ERROR;
1502                 k = *k2;
1503         }
1504 
1505 	if ( mode & CALL_TAG_DECODER ){
1506 		mode = mode & CALL_CONTENT_DECODER;
1507 		rc = BDecAsnReal ( mem_op, b, &result, bytesDecoded );
1508 	} else {
1509 		rc = BDecAsnRealContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1510 	}
1511 	if ( rc < 0 ) {
1512 		if ( k ) CompFree ( mem_op, k );
1513 		return LDAP_DECODING_ERROR;
1514 	}
1515 	k->value = result;
1516 	k->comp_desc = get_component_description (BASICTYPE_REAL);
1517 
1518         return LDAP_SUCCESS;
1519 }
1520 
1521 /*
1522  * Matching function : Relative OID
1523  */
1524 int
1525 MatchingComponentRelativeOid ( char* oid, ComponentSyntaxInfo *csi_attr,
1526 					ComponentSyntaxInfo *csi_assert )
1527 {
1528         int rc;
1529         MatchingRule* mr;
1530         ComponentRelativeOid *a, *b;
1531 
1532         if( oid ) {
1533                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
1534                 if ( mr )
1535                         return component_value_match( mr, csi_attr , csi_assert );
1536         }
1537 
1538         a = (ComponentRelativeOid*)csi_attr;
1539         b = (ComponentRelativeOid*)csi_assert;
1540 
1541 	if ( a->value.octetLen != b->value.octetLen )
1542 		return LDAP_COMPARE_FALSE;
1543         rc = ( strncmp( a->value.octs, b->value.octs, a->value.octetLen ) == 0 );
1544 
1545         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
1546 }
1547 
1548 /*
1549  * GSER Encoder : RELATIVE_OID.
1550  */
1551 int
1552 GEncComponentRelativeOid ( GenBuf *b, ComponentRelativeOid *in )
1553 {
1554 	GAsnRelativeOid t = {0};
1555 
1556 	if ( !in || in->value.octetLen <= 0 )
1557 		return (-1);
1558 	t.value = in->value;
1559 	return GEncAsnRelativeOidContent ( b , (GAsnOcts*)&t );
1560 }
1561 
1562 /*
1563  * GSER Decoder : RELATIVE_OID.
1564  */
1565 int
1566 GDecComponentRelativeOid ( void* mem_op, GenBuf *b,void *v, AsnLen *bytesDecoded, int mode )
1567 {
1568         char* peek_head;
1569         int i, strLen;
1570         void* component_values;
1571         ComponentRelativeOid* k, **k2;
1572 	GAsnRelativeOid result;
1573 
1574         k = (ComponentRelativeOid*) v;
1575 
1576         if ( mode & DEC_ALLOC_MODE_0 ) {
1577                 k2 = (ComponentRelativeOid**) v;
1578                 *k2 = (ComponentRelativeOid*) CompAlloc( mem_op, sizeof( ComponentRelativeOid ) );
1579 		if ( !*k2 ) return LDAP_DECODING_ERROR;
1580                 k = *k2;
1581         }
1582 
1583 	if ( GDecAsnRelativeOidContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
1584 		if ( k ) CompFree ( mem_op, k );
1585 		return LDAP_DECODING_ERROR;
1586 	}
1587 	k->value = result.value;
1588 	k->comp_desc = get_component_description (BASICTYPE_OID);
1589 
1590 	return LDAP_SUCCESS;
1591 }
1592 
1593 /*
1594  * Component BER Decoder : RELATIVE_OID.
1595  */
1596 int
1597 BDecComponentRelativeOidTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1598 	return BDecComponentRelativeOid ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1599 }
1600 
1601 int
1602 BDecComponentRelativeOid ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1603 {
1604         char* peek_head;
1605         int i, strLen, rc;
1606         void* component_values;
1607         ComponentRelativeOid* k, **k2;
1608 	AsnRelativeOid result;
1609 
1610         k = (ComponentRelativeOid*) v;
1611 
1612         if ( mode & DEC_ALLOC_MODE_0 ) {
1613                 k2 = (ComponentRelativeOid**) v;
1614                 *k2 = (ComponentRelativeOid*) CompAlloc( mem_op, sizeof( ComponentRelativeOid ) );
1615 		if ( !*k2 ) return LDAP_DECODING_ERROR;
1616                 k = *k2;
1617         }
1618 
1619 	if ( mode & CALL_TAG_DECODER ){
1620 		mode = mode & CALL_CONTENT_DECODER;
1621 		rc = BDecAsnRelativeOid ( mem_op, b, &result, bytesDecoded );
1622 	} else {
1623 		rc = BDecAsnRelativeOidContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1624 	}
1625 	if ( rc < 0 ) {
1626 		if ( k ) CompFree ( mem_op, k );
1627 		return LDAP_DECODING_ERROR;
1628 	}
1629 	k->value = result;
1630 	k->comp_desc = get_component_description (BASICTYPE_OID);
1631 
1632 	return LDAP_SUCCESS;
1633 }
1634 
1635 /*
1636  * GSER Encoder : UniversalString
1637  */
1638 int
1639 GEncComponentUniversalString ( GenBuf *b, ComponentUniversalString *in )
1640 {
1641 	GUniversalString t = {0};
1642 	if ( !in || in->value.octetLen <= 0 )
1643 		return (-1);
1644 	t.value = in->value;
1645 	return GEncUniversalStringContent( b, &t );
1646 }
1647 
1648 /*
1649  * GSER Decoder : UniversalString
1650  */
1651 static int
1652 UTF8toUniversalString( char* octs, int len){
1653 	/* Need to be Implemented */
1654 	return LDAP_SUCCESS;
1655 }
1656 
1657 int
1658 GDecComponentUniversalString ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1659 {
1660 	if ( GDecComponentUTF8String ( mem_op, b, v, bytesDecoded, mode) < 0 )
1661 	UTF8toUniversalString( ((ComponentUniversalString*)v)->value.octs, ((ComponentUniversalString*)v)->value.octetLen );
1662 		return LDAP_DECODING_ERROR;
1663 }
1664 
1665 /*
1666  * Component BER Decoder : UniverseString
1667  */
1668 int
1669 BDecComponentUniversalStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1670 	return BDecComponentUniversalString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1671 }
1672 
1673 int
1674 BDecComponentUniversalString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1675 {
1676         char* peek_head;
1677         int i, strLen, rc;
1678         void* component_values;
1679         ComponentUniversalString* k, **k2;
1680 	UniversalString result;
1681 
1682         k = (ComponentUniversalString*) v;
1683 
1684         if ( mode & DEC_ALLOC_MODE_0 ) {
1685                 k2 = (ComponentUniversalString**) v;
1686                 *k2 = (ComponentUniversalString*) CompAlloc( mem_op, sizeof( ComponentUniversalString ) );
1687 		if ( !*k2 ) return LDAP_DECODING_ERROR;
1688                 k = *k2;
1689         }
1690 
1691 	if ( mode & CALL_TAG_DECODER ){
1692 		mode = mode & CALL_CONTENT_DECODER;
1693 		rc = BDecUniversalString ( mem_op, b, &result, bytesDecoded );
1694 	} else {
1695 		rc = BDecUniversalStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1696 	}
1697 	if ( rc < 0 ) {
1698 		if ( k ) CompFree ( mem_op, k );
1699 		return LDAP_DECODING_ERROR;
1700 	}
1701 	k->value = result;
1702 	k->comp_desc = get_component_description (BASICTYPE_UNIVERSAL_STR);
1703 
1704 	return LDAP_SUCCESS;
1705 }
1706 
1707 /*
1708  * Component BER Decoder : VisibleString
1709  */
1710 int
1711 BDecComponentVisibleStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1712 	return BDecComponentVisibleString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1713 }
1714 
1715 int
1716 BDecComponentVisibleString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1717 {
1718         char* peek_head;
1719         int i, strLen, rc;
1720         void* component_values;
1721         ComponentVisibleString* k, **k2;
1722 	VisibleString result;
1723 
1724         k = (ComponentVisibleString*) v;
1725 
1726         if ( mode & DEC_ALLOC_MODE_0 ) {
1727                 k2 = (ComponentVisibleString**) v;
1728                 *k2 = (ComponentVisibleString*) CompAlloc( mem_op, sizeof( ComponentVisibleString ) );
1729 		if ( !*k2 ) return LDAP_DECODING_ERROR;
1730                 k = *k2;
1731         }
1732 
1733 	if ( mode & CALL_TAG_DECODER ){
1734 		mode = mode & CALL_CONTENT_DECODER;
1735 		rc = BDecVisibleString ( mem_op, b, &result, bytesDecoded );
1736 	} else {
1737 		rc = BDecVisibleStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1738 	}
1739 	k->value = result;
1740 	k->comp_desc = get_component_description (BASICTYPE_VISIBLE_STR);
1741 
1742 	return LDAP_SUCCESS;
1743 }
1744 
1745 /*
1746  * Routines for handling an ANY DEFINED Type
1747  */
1748 
1749 /* Check if the <select> type CR and the OID of the given ANY type */
1750 int
1751 CheckSelectTypeCorrect ( void* mem_op, ComponentAnyInfo* cai, struct berval* select ) {
1752 	int strLen;
1753 	AttributeType* ad_type;
1754 	char* oid;
1755 	char* result;
1756 
1757 	if ( IsNumericOid ( select->bv_val , select->bv_len ) ) {
1758 		oid = select->bv_val;
1759 		strLen = select->bv_len;
1760 	} else {
1761 		ad_type = at_bvfind( select );
1762 
1763 		if ( !ad_type )
1764 			return LDAP_DECODING_ERROR;
1765 
1766 		oid = ad_type->sat_atype.at_oid;
1767 		strLen = strlen ( oid );
1768 	}
1769 	result = EncodeComponentOid ( mem_op, oid , &strLen );
1770 	if ( !result || strLen <= 0 ) return (-1);
1771 
1772 	if ( cai->oid.octetLen == strLen &&
1773 		strncmp ( cai->oid.octs, result, strLen ) == 0 )
1774 		return (1);
1775 	else
1776 		return (-1);
1777 }
1778 
1779 int
1780 SetAnyTypeByComponentOid ( ComponentAny *v, ComponentOid *id ) {
1781 	Hash hash;
1782 	void *anyInfo;
1783 
1784 	/* use encoded oid as hash string */
1785 	hash = MakeHash (id->value.octs, id->value.octetLen);
1786 	if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
1787 		v->cai = (ComponentAnyInfo*) anyInfo;
1788 	else
1789 		v->cai = NULL;
1790 
1791 	if ( !v->cai ) {
1792 	/*
1793 	 * If not found, the data considered as octet chunk
1794 	 * Yet-to-be-Implemented
1795 	 */
1796 	}
1797 	return LDAP_SUCCESS;
1798 }
1799 
1800 void
1801 SetAnyTypeByComponentInt( ComponentAny *v, ComponentInt id) {
1802 	Hash hash;
1803 	void *anyInfo;
1804 
1805 	hash = MakeHash ((char*)&id, sizeof (id));
1806 	if (CheckForAndReturnValue (anyIntHashTblG, hash, &anyInfo))
1807 		v->cai = (ComponentAnyInfo*) anyInfo;
1808 	else
1809 		v->cai = NULL;
1810 }
1811 
1812 int
1813 GEncComponentAny ( GenBuf *b, ComponentAny *in )
1814 {
1815 	if ( in->cai != NULL  && in->cai->Encode != NULL )
1816 		return in->cai->Encode(b, &in->value );
1817 	else
1818 		return (-1);
1819 }
1820 
1821 int
1822 BEncComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode)
1823 {
1824         ComponentAny *k, **k2;
1825 
1826         k = (ComponentAny*) result;
1827 
1828 	if ( !k ) return (-1);
1829 
1830         if ( mode & DEC_ALLOC_MODE_0 ) {
1831                 k2 = (ComponentAny**) result;
1832                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
1833 		if ( !*k2 ) return LDAP_DECODING_ERROR;
1834                 k = *k2;
1835         }
1836 
1837 	if ((result->cai != NULL) && (result->cai->BER_Decode != NULL)) {
1838 		result->value = (void*) CompAlloc ( mem_op, result->cai->size );
1839 		if ( !result->value ) return 0;
1840 		result->cai->BER_Decode ( mem_op, b, result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_1);
1841 
1842 		k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1843 		if ( !k->comp_desc )  {
1844 			if ( k ) CompFree ( mem_op, k );
1845 			return LDAP_DECODING_ERROR;
1846 		}
1847 		k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
1848 		k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
1849 		k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
1850 		k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
1851 		k->comp_desc->cd_extract_i = NULL;
1852 		k->comp_desc->cd_type = ASN_BASIC;
1853 		k->comp_desc->cd_type_id = BASICTYPE_ANY;
1854 		k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
1855 		return LDAP_SUCCESS;
1856 	}
1857 	else {
1858 		Asn1Error ("ERROR - Component ANY Decode routine is NULL\n");
1859 		return 0;
1860 	}
1861 }
1862 
1863 int
1864 BDecComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode) {
1865 	int rc;
1866         ComponentAny *k, **k2;
1867 
1868         k = (ComponentAny*) result;
1869 
1870 	if ( !k ) return (-1);
1871 
1872         if ( mode & DEC_ALLOC_MODE_0 ) {
1873                 k2 = (ComponentAny**) result;
1874                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
1875 		if ( !*k2 ) return LDAP_DECODING_ERROR;
1876                 k = *k2;
1877         }
1878 
1879 	if ((result->cai != NULL) && (result->cai->BER_Decode != NULL)) {
1880 		result->cai->BER_Decode ( mem_op, b, (ComponentSyntaxInfo*)&result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_0 );
1881 
1882 		k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1883 		if ( !k->comp_desc )  {
1884 			if ( k ) CompFree ( mem_op, k );
1885 			return LDAP_DECODING_ERROR;
1886 		}
1887 		k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
1888 		k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
1889 		k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
1890 		k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
1891 		k->comp_desc->cd_extract_i = NULL;
1892 		k->comp_desc->cd_type = ASN_BASIC;
1893 		k->comp_desc->cd_type_id = BASICTYPE_ANY;
1894 		k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
1895 		return LDAP_SUCCESS;
1896 	}
1897 	else {
1898 		Asn1Error ("ERROR - Component ANY Decode routine is NULL\n");
1899 		return 0;
1900 	}
1901 }
1902 
1903 int
1904 GDecComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode) {
1905         ComponentAny *k, **k2;
1906 
1907         k = (ComponentAny*) result;
1908 
1909         if ( mode & DEC_ALLOC_MODE_0 ) {
1910                 k2 = (ComponentAny**) result;
1911                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
1912 		if ( !*k2 ) return LDAP_DECODING_ERROR;
1913                 k = *k2;
1914         }
1915 	if ((result->cai != NULL) && (result->cai->GSER_Decode != NULL)) {
1916 		result->value = (void*) CompAlloc ( mem_op, result->cai->size );
1917 		if ( !result->value ) return 0;
1918 		result->cai->GSER_Decode ( mem_op, b, result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_1);
1919 		k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1920 		if ( !k->comp_desc )  {
1921 			if ( k ) CompFree ( mem_op, k );
1922 			return LDAP_DECODING_ERROR;
1923 		}
1924 		k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
1925 		k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
1926 		k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
1927 		k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
1928 		k->comp_desc->cd_type = ASN_BASIC;
1929 		k->comp_desc->cd_extract_i = NULL;
1930 		k->comp_desc->cd_type_id = BASICTYPE_ANY;
1931 		k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
1932 		return LDAP_SUCCESS;
1933 	}
1934 	else {
1935 		Asn1Error ("ERROR - ANY Decode routine is NULL\n");
1936 		return 0;
1937 	}
1938 }
1939 
1940 int
1941 MatchingComponentAny (char* oid, ComponentAny *result, ComponentAny *result2) {
1942 	void *comp1, *comp2;
1943 
1944 	if ( result->comp_desc->cd_type_id == BASICTYPE_ANY )
1945 		comp1 = result->value;
1946 	else
1947 		comp1 = result;
1948 
1949 	if ( result2->comp_desc->cd_type_id == BASICTYPE_ANY )
1950 		comp2 = result2->value;
1951 	else
1952 		comp2 = result2;
1953 
1954 	if ((result->cai != NULL) && (result->cai->Match != NULL)) {
1955 		if ( result->comp_desc->cd_type_id == BASICTYPE_ANY )
1956 			return result->cai->Match(oid, comp1, comp2 );
1957 		else if ( result2->comp_desc->cd_type_id == BASICTYPE_ANY )
1958 			return result2->cai->Match(oid, comp1, comp2);
1959 		else
1960 			return LDAP_INVALID_SYNTAX;
1961 	}
1962 	else {
1963 		Asn1Error ("ERROR - ANY Matching routine is NULL\n");
1964 		return LDAP_INVALID_SYNTAX;
1965 	}
1966 }
1967 
1968 void*
1969 ExtractingComponentAny ( void* mem_op, ComponentReference* cr,  ComponentAny *result ) {
1970 	if ((result->cai != NULL) && (result->cai->Extract != NULL)) {
1971 		return (void*) result->cai->Extract( mem_op, cr , result->value );
1972 	}
1973 	else {
1974 		Asn1Error ("ERROR - ANY Extracting routine is NULL\n");
1975 		return (void*)NULL;
1976 	}
1977 }
1978 
1979 void
1980 FreeComponentAny (ComponentAny* any) {
1981 	if ( any->cai != NULL && any->cai->Free != NULL ) {
1982 		any->cai->Free( any->value );
1983 		free ( ((ComponentSyntaxInfo*)any->value)->csi_comp_desc );
1984 		free ( any->value );
1985 	}
1986 	else
1987 		Asn1Error ("ERROR - ANY Free routine is NULL\n");
1988 }
1989 
1990 void
1991 InstallAnyByComponentInt (int anyId, ComponentInt intId, unsigned int size,
1992 			EncodeFcn encode, gser_decoder_func* G_decode,
1993 			ber_tag_decoder_func* B_decode, ExtractFcn extract,
1994 			MatchFcn match, FreeFcn free,
1995 			PrintFcn print)
1996 {
1997 	ComponentAnyInfo *a;
1998 	Hash h;
1999 
2000 	a = (ComponentAnyInfo*) malloc(sizeof (ComponentAnyInfo));
2001 	a->anyId = anyId;
2002 	a->oid.octs = NULL;
2003 	a->oid.octetLen = 0;
2004 	a->intId = intId;
2005 	a->size = size;
2006 	a->Encode = encode;
2007 	a->GSER_Decode = G_decode;
2008 	a->BER_Decode = B_decode;
2009 	a->Match = match;
2010 	a->Extract = extract;
2011 	a->Free = free;
2012 	a->Print = print;
2013 
2014 	if (anyIntHashTblG == NULL)
2015 		anyIntHashTblG = InitHash();
2016 
2017 	h = MakeHash ((char*)&intId, sizeof (intId));
2018 
2019 	if(anyIntHashTblG != NULL)
2020 		Insert(anyIntHashTblG, a, h);
2021 }
2022 
2023 
2024 /*
2025  * OID and its corresponding decoder can be registerd with this func.
2026  * If contained types constrained by <select> are used,
2027  * their OID and decoder MUST be registered, otherwise it will return no entry.
2028  * An open type(ANY type) also need be registered.
2029  */
2030 void
2031 InstallOidDecoderMapping ( char* ch_oid, EncodeFcn encode, gser_decoder_func* G_decode, ber_tag_decoder_func* B_decode, ExtractFcn extract, MatchFcn match ) {
2032 	AsnOid oid;
2033 	int strLen;
2034 	void* mem_op;
2035 
2036 	strLen = strlen( ch_oid );
2037 	if( strLen <= 0 ) return;
2038 	mem_op = comp_nibble_memory_allocator ( 128, 16 );
2039 	oid.octs = EncodeComponentOid ( mem_op, ch_oid, &strLen );
2040 	oid.octetLen = strLen;
2041 	if( strLen <= 0 ) return;
2042 
2043 
2044 	InstallAnyByComponentOid ( 0, &oid, 0, encode, G_decode, B_decode,
2045 						extract, match, NULL, NULL);
2046 	comp_nibble_memory_free(mem_op);
2047 }
2048 
2049 /*
2050  * Look up Oid-decoder mapping table by berval have either
2051  * oid or description
2052  */
2053 OidDecoderMapping*
2054 RetrieveOidDecoderMappingbyBV( struct berval* in ) {
2055 	if ( IsNumericOid ( in->bv_val, in->bv_len ) )
2056 		return RetrieveOidDecoderMappingbyOid( in->bv_val, in->bv_len );
2057 	else
2058 		return RetrieveOidDecoderMappingbyDesc( in->bv_val, in->bv_len );
2059 }
2060 
2061 /*
2062  * Look up Oid-decoder mapping table by dotted OID
2063  */
2064 OidDecoderMapping*
2065 RetrieveOidDecoderMappingbyOid( char* ch_oid, int oid_len ) {
2066 	Hash hash;
2067 	void *anyInfo;
2068 	AsnOid oid;
2069 	int strLen;
2070 	void* mem_op;
2071 
2072 	mem_op = comp_nibble_memory_allocator ( 128, 16 );
2073 	oid.octs = EncodeComponentOid ( mem_op, ch_oid, &oid_len);
2074 	oid.octetLen = oid_len;
2075 	if( oid_len <= 0 ) {
2076 		comp_nibble_memory_free( mem_op );
2077 		return NULL;
2078 	}
2079 
2080 	/* use encoded oid as hash string */
2081 	hash = MakeHash ( oid.octs, oid.octetLen);
2082 	comp_nibble_memory_free( mem_op );
2083 	if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
2084 		return (OidDecoderMapping*) anyInfo;
2085 	else
2086 		return (OidDecoderMapping*) NULL;
2087 
2088 }
2089 
2090 /*
2091  * Look up Oid-decoder mapping table by description
2092  */
2093 OidDecoderMapping*
2094 RetrieveOidDecoderMappingbyDesc( char* desc, int desc_len ) {
2095 	Hash hash;
2096 	void *anyInfo;
2097 	AsnOid oid;
2098 	AttributeType* ad_type;
2099 	struct berval bv;
2100 	void* mem_op;
2101 
2102 	bv.bv_val = desc;
2103 	bv.bv_len = desc_len;
2104 	ad_type = at_bvfind( &bv );
2105 
2106 	oid.octs = ad_type->sat_atype.at_oid;
2107 	oid.octetLen = strlen ( oid.octs );
2108 
2109 	if ( !ad_type )
2110 		return (OidDecoderMapping*) NULL;
2111 
2112 	mem_op = comp_nibble_memory_allocator ( 128, 16 );
2113 
2114 	oid.octs = EncodeComponentOid ( mem_op, oid.octs , (int*)&oid.octetLen );
2115 	if( oid.octetLen <= 0 ) {
2116 		comp_nibble_memory_free( mem_op );
2117 		return (OidDecoderMapping*) NULL;
2118 	}
2119 
2120 	/* use encoded oid as hash string */
2121 	hash = MakeHash ( oid.octs, oid.octetLen);
2122 	comp_nibble_memory_free( mem_op );
2123 	if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
2124 		return (OidDecoderMapping*) anyInfo;
2125 	else
2126 		return (OidDecoderMapping*) NULL;
2127 
2128 }
2129 void
2130 InstallAnyByComponentOid (int anyId, AsnOid *oid, unsigned int size,
2131 			EncodeFcn encode, gser_decoder_func* G_decode,
2132 			ber_tag_decoder_func* B_decode, ExtractFcn extract,
2133 			 MatchFcn match, FreeFcn free, PrintFcn print)
2134 {
2135 	ComponentAnyInfo *a;
2136 	Hash h;
2137 
2138 	a = (ComponentAnyInfo*) malloc (sizeof (ComponentAnyInfo));
2139 	a->anyId = anyId;
2140 	if ( oid ) {
2141 		a->oid.octs = malloc( oid->octetLen );
2142 		memcpy ( a->oid.octs, oid->octs, oid->octetLen );
2143 		a->oid.octetLen = oid->octetLen;
2144 	}
2145 	a->size = size;
2146 	a->Encode = encode;
2147 	a->GSER_Decode = G_decode;
2148 	a->BER_Decode = B_decode;
2149 	a->Match = match;
2150 	a->Extract = extract;
2151 	a->Free = free;
2152 	a->Print = print;
2153 
2154 	h = MakeHash (oid->octs, oid->octetLen);
2155 
2156 	if (anyOidHashTblG == NULL)
2157 		anyOidHashTblG = InitHash();
2158 
2159 	if(anyOidHashTblG != NULL)
2160 		Insert(anyOidHashTblG, a, h);
2161 }
2162 
2163 int
2164 BDecComponentTop  (
2165 ber_decoder_func *decoder _AND_
2166 void* mem_op _AND_
2167 GenBuf *b _AND_
2168 AsnTag tag _AND_
2169 AsnLen elmtLen _AND_
2170 void **v _AND_
2171 AsnLen *bytesDecoded _AND_
2172 int mode) {
2173 	tag = BDecTag ( b, bytesDecoded );
2174 	elmtLen = BDecLen ( b, bytesDecoded );
2175 	if ( elmtLen <= 0 ) return (-1);
2176 	if ( tag != MAKE_TAG_ID (UNIV, CONS, SEQ_TAG_CODE) ) {
2177 		return (-1);
2178 	}
2179 
2180 	return (*decoder)( mem_op, b, tag, elmtLen, (ComponentSyntaxInfo*)v,(int*)bytesDecoded, mode );
2181 }
2182 
2183 /*
2184  * ASN.1 specification of a distinguished name
2185  * DistinguishedName ::= RDNSequence
2186  * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
2187  * RelativeDistinguishedName ::= SET SIZE(1..MAX) OF AttributeTypeandValue
2188  * AttributeTypeandValue ::= SEQUENCE {
2189  * 	type	AttributeType
2190  *	value	AttributeValue
2191  * }
2192  * When dnMatch/rdnMatch is used in a component assertion value
2193  * the component in DistinguishedName/RelativeDistinguishedName
2194  * need to be converted to the LDAP encodings in RFC2253
2195  * in order to be matched against the assertion value
2196  * If allComponentMatch is used, the assertion value may be
2197  * decoded into the Internal Representation(Component Tree)
2198  * by the corresponding GSER or BER decoder
2199  * Following routine converts a component tree(DistinguishedName) into
2200  * LDAP encodings in RFC2253
2201  * Example)
2202  * IR : ComponentRDNSequence
2203  * GSER : { { type cn, value sang },{ type o, value ibm}, {type c, value us} }
2204  * LDAP Encodings : cn=sang,o=ibm,c=us
2205  */
2206 
2207 increment_bv_mem_by_size ( struct berval* in, int size ) {
2208 	int new_size = in->bv_len + size;
2209 	in->bv_val = realloc( in->bv_val, new_size );
2210 	in->bv_len = new_size;
2211 }
2212 
2213 int
2214 ConvertBER2Desc( char* in, int size, struct berval* out, int* pos ) {
2215 	int desc_size;
2216 	char* desc_ptr;
2217 	unsigned int firstArcNum;
2218 	unsigned int arcNum;
2219 	int i, rc, start_pos = *pos;
2220 	char buf[MAX_OID_LEN];
2221 	AttributeType *at;
2222 	struct berval bv_name;
2223 
2224 	/*convert BER oid to desc*/
2225 	for ( i = 0, arcNum = 0; (i < size) && (in[i] & 0x80 ); i++ )
2226 		arcNum = (arcNum << 7) + (in[i] & 0x7f);
2227 	arcNum = (arcNum << 7) + (in[i] & 0x7f);
2228 	i++;
2229 	firstArcNum = (unsigned short)(arcNum/40);
2230 	if ( firstArcNum > 2 )
2231 		firstArcNum = 2;
2232 
2233 	arcNum = arcNum - (firstArcNum * 40 );
2234 
2235 	rc = intToAscii ( arcNum, buf );
2236 
2237 	/*check if the buffer can store the first/second arc and two dots*/
2238 	if ( out->bv_len < *pos + 2 + 1 + rc )
2239 		increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2240 
2241 	if ( firstArcNum == 1)
2242 		out->bv_val[*pos] = '1';
2243 	else
2244 		out->bv_val[*pos] = '2';
2245 	(*pos)++;
2246 	out->bv_val[*pos] = '.';
2247 	(*pos)++;
2248 
2249 	memcpy( out->bv_val + *pos, buf, rc );
2250 	*pos += rc;
2251 	out->bv_val[*pos] = '.';
2252 	(*pos)++;
2253 
2254 	for ( ; i < size ; ) {
2255 		for ( arcNum=0; (i < size) && (in[i] & 0x80) ; i++ )
2256 			arcNum = (arcNum << 7) + (in[i] & 0x7f);
2257 		arcNum = (arcNum << 7) + (in[i] & 0x7f);
2258 		i++;
2259 
2260 		rc = intToAscii ( arcNum, buf );
2261 
2262 		if ( out->bv_len < *pos + rc + 1 )
2263 			increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2264 
2265 		memcpy( out->bv_val + *pos, buf, rc );
2266 		*pos += rc;
2267 		out->bv_val[*pos] = '.';
2268 		(*pos)++;
2269 	}
2270 	(*pos)--;/*remove the last '.'*/
2271 
2272 	/*
2273 	 * lookup OID database to locate desc
2274 	 * then overwrite OID with desc in *out
2275 	 * If failed to look up desc, OID form is used
2276 	 */
2277 	bv_name.bv_val = out->bv_val + start_pos;
2278 	bv_name.bv_len = *pos - start_pos;
2279 	at = at_bvfind( &bv_name );
2280 	if ( !at )
2281 		return LDAP_SUCCESS;
2282 	desc_size = at->sat_cname.bv_len;
2283 	memcpy( out->bv_val + start_pos, at->sat_cname.bv_val, desc_size );
2284 	*pos = start_pos + desc_size;
2285 	return LDAP_SUCCESS;
2286 }
2287 
2288 int
2289 ConvertComponentAttributeTypeAndValue2RFC2253 ( irAttributeTypeAndValue* in, struct berval* out, int *pos ) {
2290 	int rc;
2291 	int value_size = ((ComponentUTF8String*)in->value.value)->value.octetLen;
2292 	char* value_ptr =  ((ComponentUTF8String*)in->value.value)->value.octs;
2293 
2294 	rc = ConvertBER2Desc( in->type.value.octs, in->type.value.octetLen, out, pos );
2295 	if ( rc != LDAP_SUCCESS ) return rc;
2296 	if ( out->bv_len < *pos + 1/*for '='*/  )
2297 		increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2298 	/*Between type and value, put '='*/
2299 	out->bv_val[*pos] = '=';
2300 	(*pos)++;
2301 
2302 	/*Assume it is string*/
2303 	if ( out->bv_len < *pos + value_size )
2304 		increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2305 	memcpy( out->bv_val + *pos, value_ptr, value_size );
2306 	out->bv_len += value_size;
2307 	*pos += value_size;
2308 
2309 	return LDAP_SUCCESS;
2310 }
2311 
2312 int
2313 ConvertRelativeDistinguishedName2RFC2253 ( irRelativeDistinguishedName* in, struct berval *out , int* pos) {
2314 	irAttributeTypeAndValue* attr_typeNvalue;
2315 	int rc;
2316 
2317 
2318 	FOR_EACH_LIST_ELMT( attr_typeNvalue, &in->comp_list)
2319 	{
2320 		rc = ConvertComponentAttributeTypeAndValue2RFC2253( attr_typeNvalue, out, pos );
2321 		if ( rc != LDAP_SUCCESS ) return LDAP_INVALID_SYNTAX;
2322 
2323 		if ( out->bv_len < *pos + 1/*for '+'*/  )
2324 			increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2325 		/*between multivalued RDNs, put comma*/
2326 		out->bv_val[(*pos)++] = '+';
2327 	}
2328 	(*pos)--;/*remove the last '+'*/
2329 	return LDAP_SUCCESS;
2330 }
2331 
2332 int
2333 ConvertRDN2RFC2253 ( irRelativeDistinguishedName* in, struct berval *out ) {
2334 	int rc, pos = 0;
2335 	out->bv_val = (char*)malloc( INITIAL_DN_SIZE );
2336 	out->bv_len = INITIAL_DN_SIZE;
2337 
2338 	rc = ConvertRelativeDistinguishedName2RFC2253 ( in, out , &pos);
2339 	if ( rc != LDAP_SUCCESS ) return rc;
2340 	out->bv_val[pos] = '\0';
2341 	out->bv_len = pos;
2342 	return LDAP_SUCCESS;
2343 }
2344 
2345 int
2346 ConvertRDNSequence2RFC2253( irRDNSequence *in, struct berval* out ) {
2347 	irRelativeDistinguishedName* rdn_seq;
2348 	AsnList* seq = &in->comp_list;
2349 	int pos = 0, rc ;
2350 
2351 	out->bv_val = (char*)malloc( INITIAL_DN_SIZE );
2352 	out->bv_len = INITIAL_DN_SIZE;
2353 
2354 	FOR_EACH_LIST_ELMT( rdn_seq, seq )
2355 	{
2356 		rc = ConvertRelativeDistinguishedName2RFC2253( rdn_seq, out, &pos );
2357 		if ( rc != LDAP_SUCCESS ) return LDAP_INVALID_SYNTAX;
2358 
2359 		if ( out->bv_len < pos + 1/*for ','*/ )
2360 			increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2361 		/*Between RDN, put comma*/
2362 		out->bv_val[pos++] = ',';
2363 	}
2364 	pos--;/*remove the last '+'*/
2365 	out->bv_val[pos] = '\0';
2366 	out->bv_len =pos;
2367 	return LDAP_SUCCESS;
2368 }
2369 
2370 #endif
2371