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