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