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