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