File: fortopat.c

package info (click to toggle)
libsmi 0.4.8%2Bdfsg2-17
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 13,972 kB
  • sloc: ansic: 49,659; java: 13,722; sh: 9,311; yacc: 8,705; lex: 1,448; javascript: 544; makefile: 347; perl: 117
file content (773 lines) | stat: -rw-r--r-- 20,855 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
 * fortopat.c --
 *
 *      Implementation of format string to xsd pattern conversion.
 *
 * Copyright (c) 2001 J. Schoenwaelder, Technical University of Braunschweig.
 *           (c) 2002 T. Klie, Technical University of Braunschweig.
 *           (c) 2002 F. Strauss, Technical University of Braunschweig.
 *           (c) 2007 T. Klie, Technical University of Braunschweig.
 *	     (c) 2007 J. Schoenwaelder, Jacobs University Bremen.
 *
 * See the file "COPYING" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * @(#) $Id: dump-xsd.c 7372 2007-10-17 15:26:40Z tklie $
 */

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#ifdef HAVE_WIN_H
#include "win.h"
#endif

#include "smi.h"
#include "smidump.h"

#include "fortopat.h"


#ifndef MIN
#define MIN(a,b) ((a)) < ((b)) ? ((a)) : ((b))
#endif


/* definition of commonly used display hints */
#define LIBSMI_DH_TP_IPV4Z_ADDR "1d.1d.1d.1d%4d:2d"
#define LIBSMI_DH_UDP_IPV4 "1d.1d.1d.1d/2d"
#define LIBSMI_DH_IPV4Z_ADDR "1d.1d.1d.1d%4d"
#define LIBSMI_DH_IPV4_ADDR "1d.1d.1d.1d"
#define LIBSMI_DH_LDP_ID "1d.1d.1d.1d:2d"
#define LIBSMI_DH_IPV6_ADDR "2x:2x:2x:2x:2x:2x:2x:2x"
#define LIBSMI_DH_IPV6Z_ADDR "2x:2x:2x:2x:2x:2x:2x:2x%4d"
#define LIBSMI_DH_TP_IPV6_ADDR "0a[2x:2x:2x:2x:2x:2x:2x:2x]0a:2d"
#define LIBSMI_DH_TP_IPV6Z_ADDR "0a[2x:2x:2x:2x:2x:2x:2x:2x%4d]0a:2d"
#define LIBSMI_DH_HEX "1x"
#define LIBSMI_DH_HEX_COLON "1x:"
#define LIBSMI_DH_DOUBLE_HEX_COLON "2x:"
#define LIBSMI_DH_ASCII "255a"
#define LIBSMI_DH_UTF8 "255t"
#define LIBSMI_DH_ASCII_CHAR "1a"
#define LIBSMI_DH_DATETIME "2d-1d-1d,1d.1d.1d,1a1d:1d"
#define LIBSMI_DH_DATETIME2 "2d-1d-1d,1d:1d:1d.1d,1a1d:1d"
#define LIBSMI_DH_IPX_ADDR "4x.1x:1x:1x:1x:1x:1x.2d"


/* list of parts of a display hint */
typedef struct DH {
    unsigned int number;
/*    int repeat;    //  repeat not yet supported */
    char type;
    char separator[3];
/*    char repTerm;  // repeat not yet supported */
    struct DH *next;
} DH;





static int
smiPow(int base, unsigned int exponent)
{
    unsigned int i;
    int ret = 1;
    
    if (exponent == 0) {
	return 1;
    }
    
    for (i = 0; i < exponent; i++) {
	ret *= base;
    }
    return ret;
}



static unsigned int
numDigits(unsigned int val)
{
    int ret = 1;

    for (; val / 10; val = val / 10) {
	ret++;
    }
    
    return ret;
}



static int
getBracketLevel(char *bracketString)
{
    int level = 0;
    char *c = bracketString;

    while(level >= 0 && *c != '\0') {
	switch(*c) {
	case '(':
	    level++;
	    break;
	case ')':
	    level--;
	    break;
	default:
	    break;
	}
	c++;
    }

    return level;
}



/* parse a display hint and create a list of DHs */
static struct DH*
parseDH(const char *hint)
{
    struct DH *iterDH = (struct DH *)malloc( sizeof( struct DH ) );
    struct DH *ret = iterDH;
    struct DH *oldIterDH = iterDH;
    unsigned int pos = 0;
    
    if (!ret) {
	return NULL;
    }
    
    memset(iterDH, 0, sizeof(struct DH));
    
    while( pos < strlen( hint ) ) {

	if( ! iterDH ) {
	    iterDH = (struct DH *)malloc( sizeof( struct DH ) );
	    if( ! iterDH ) return NULL;
	    memset(iterDH, 0, sizeof(struct DH));
	    oldIterDH->next = iterDH;
	    oldIterDH = iterDH;
	}

	switch( hint[ pos ] ) {
	    
	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	case '8':
	case '9': {
	    unsigned int endPos;
	    char *strNum;
	    
	    /* find end of number */
	    for( endPos = pos; isdigit( hint[ endPos ] ); endPos++ );
	    
	    /* parse number */
	    strNum = (char *)malloc( endPos - pos );
	    strncpy( strNum, &hint[ pos ], endPos - pos );
	    strNum[ endPos - pos ] = '\0';
	    iterDH->number = atoi( strNum );
	    free( strNum );
	    
	    /* forward the position pointer */
	    pos = endPos;
	    break;
	}
	
	case 'a':
	case 'b':
	case 'd':
	case 'o':
	case 't':
	case 'x':
	    iterDH->type = hint[ pos++ ];
	    if( isdigit( hint[ pos ] ) || hint[ pos ] == '*' ) {
		iterDH = iterDH->next;
	    }
	    break;
	    
	case '*':
	    /* repeat not supported */
	    return NULL;
	    
	default:
	    if( iterDH->separator ) {
		/* iterDH->repTerm = hint[ pos++ ]; // repeat not supported */
	      pos++;
	    }
	    else {
              iterDH->separator[0] = hint[ pos++ ];
	      if (iterDH->separator[0] == '.' ) {
		iterDH->separator[0] = '\\';
		iterDH->separator[1] = '.';
	      }
	    }
	    if( isdigit( hint[ pos ] ) || hint[ pos ] == '*' ) {
		iterDH = iterDH->next;
	    }
	    break;
	}	
    }
    return ret;
}

static char*
getSimpleCharFacet_XSD(char dhType, unsigned int number, 
		       unsigned int minL, unsigned int maxL)
{
    char *ret;
    char *baseRegExp;
    
    /* determine base regular expression, depending on display hint type */
    switch( dhType ) {
    case 'a':
	baseRegExp = "\\p{IsBasicLatin}";
	break;
    case 't':
	baseRegExp = ".";
	break;
    default:
	/* XXX - fix this to use the normal error reporting mechanism XXX */
	fprintf(stderr, 
		"displayHint.c: Error: unknown display hint type\n");
	return NULL;
    } 
    
    smiAsprintf( &ret, "%s{%d,%d}", baseRegExp, minL, MIN( number, maxL));
    return ret;
}


/* check if the hint is known and (if so) return the XSD translation */
static char*
getKnownDisplayHint_XSD(const char *hint,
			SmiUnsigned32 *lengths, unsigned int numSubranges)
{
    int i;
    char *ret = "";
    
    if( ! strcmp( hint, LIBSMI_DH_TP_IPV4Z_ADDR ) )
	/* this is a simplyfied restriction. 
	 * We could restrict more, 
	 * but then, the pattern will be very hard to read / understand */
	return xstrdup("(\\d{1,3}.){3}\\d{1,3})%\\d{1,10}:\\d{1,5}");
    
    if( ! strcmp( hint, LIBSMI_DH_UDP_IPV4 ) ) 
	return xstrdup("(\\d{1,3}.){3}\\d{1,3}/\\d{1,5}");
    
    if( ! strcmp( hint, LIBSMI_DH_IPV4Z_ADDR ) )
	return xstrdup("(\\d{1,3}.){3}\\d{1,3}%\\d{1,10}");
    
    if( ! strcmp( hint, LIBSMI_DH_IPV4_ADDR ) )
	return xstrdup("(\\d{1,3}.){3}\\d{1,3}");
    
    if( ! strcmp( hint, LIBSMI_DH_LDP_ID ) )
	return xstrdup("(\\d{1,3}.){3}\\d{1,3}:\\d{1,5}");
    
    if( ! strcmp( hint, LIBSMI_DH_IPV6_ADDR ) )
	return xstrdup("([\\dA-Fa-f]{2}:){7}[\\dA-Fa-f]{2}");
    
    if( ! strcmp( hint, LIBSMI_DH_IPV6Z_ADDR ) )
	return xstrdup("([\\dA-Fa-f]{2}:){7}[\\dA-Fa-f]{2}%\\d{1,10}");
    
    if( ! strcmp( hint, LIBSMI_DH_TP_IPV6_ADDR ) )
	return xstrdup("\\[([\\dA-Fa-f]{2}:){7}[\\dA-Fa-f]{2}\\]:\\d{1,5}");
    
    if( ! strcmp( hint, LIBSMI_DH_TP_IPV6Z_ADDR ) )
	return xstrdup("\\[([\\dA-Fa-f]{2}:){7}[\\dA-Fa-f]{2}%\\d{1,10}\\]:\\d{1,5}");
    
    if( ! strcmp( hint, LIBSMI_DH_HEX ) )
	return xstrdup("[0-9A-Fa-f]{2}");
    
    if( ! strcmp( hint, LIBSMI_DH_HEX_COLON ) )
	return xstrdup("[0-9A-Fa-f]{2}:");
    
    if( ! strcmp( hint, LIBSMI_DH_DOUBLE_HEX_COLON ) )
	return xstrdup("[0-9A-Fa-f]{4}:");
    
    if( ! strcmp( hint, LIBSMI_DH_ASCII ) ) {
	switch( numSubranges ) { 		
	case 0: 
	    return getSimpleCharFacet_XSD( 'a', 255, 0, 255 );
	case 1: 			
	    return getSimpleCharFacet_XSD( 'a', 255, lengths[0], lengths[1] );
	default:
	    ret = "("; 
	    for( i=0; i < numSubranges * 2; i+=2 ) {
		if( i ) smiAsprintf(&ret, "%s)|(", ret);
		smiAsprintf(&ret, "%s%s", ret,
			    getSimpleCharFacet_XSD('a', 255, 
						   lengths[i], lengths[i+1]));
		
	    }
	    smiAsprintf(&ret, "%s)", ret);
	    return ret;
	}
    }
    
    if( ! strcmp(hint, LIBSMI_DH_UTF8 )) {
	switch( numSubranges ) { 		
	case 0: 
	    return getSimpleCharFacet_XSD( 't' , 255, 0, 255 );
	case 1: 			
	    return getSimpleCharFacet_XSD( 't' , 255, lengths[0], lengths[1]);
	default:
	    ret = "("; 
	    for( i=0; i < numSubranges * 2; i+=2 ) {
		if( i ) smiAsprintf( &ret, "%s)|(", ret );
		smiAsprintf(&ret, "%s%s", 
			    ret, getSimpleCharFacet_XSD('t', 255,
							lengths[i], 
							lengths[i+1]));
	    }
	    smiAsprintf( &ret, "%s)", ret );
	    return ret;
	}
    }
    
    if( ! strcmp( hint, LIBSMI_DH_ASCII_CHAR ))
	return xstrdup("\\p{IsBasicLatin}{0,1}");
    
    /* we assume here date-time-value, i.e. the number of digits are fixed */
    if( ! strcmp( hint, LIBSMI_DH_DATETIME ) )
	return xstrdup("\\d{4}-\\d{2}-\\d{2},(\\d{2}.){2}\\d{2},\\p{IsBasicLatin}\\d{2}:\\d{2}");
    
    if( ! strcmp( hint, LIBSMI_DH_DATETIME2 ) ) {
	if( numSubranges >1 )
	    /* there are more subranges, 
	     * assume: lengths = 8 | lengths = 11 
	     * thus: last 3 octets (time zone info) is optional */ 
	    return xstrdup("\\d{4}-\\d{2}-\\d{2},(\\d{2}:){2}\\d{2}(,[\\+\\-]\\d{2}:\\d{2}){0,1}");
	return xstrdup("\\d{4}-\\d{2}-\\d{2},(\\d{2}:){2}\\d{2},[\\+\\-]\\d{2}:\\d{2}");
    }
    if( !strcmp( hint, LIBSMI_DH_IPX_ADDR ) )
	return xstrdup("[0-9A-Fa-f]{8}.([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}.\\d{1,4}");
    
    else return NULL;		
}



/* build a regexp pattern from a display hint 
 * 
 * Some documentation: First of all, it is checked whether the display hint 
 * matches againts one of the well known display hints. If so, a direct mapping 
 * is applied. Note that most cases ignore any additional length information 
 * (except DATETIME and ASCII / UTF8).
 * If no match can be found, the display hint is parsed and seperated 
 * into a list of blocks (DH struct). A display hint block consists of 
 * o the number of octets to be used (unsigned int "number"),
 * o a charakter representing the type (char "type"),
 * o the separator (char[3] "separator"),
 * o a pointer to the next display hint block in the list.
 * 
 * Then, it is checked whether it is a "simple" display hint, 
 * i.e. is hint that consists only of a single block and 
 * ASCII / UTF-8 restriction (type 'a' or 't'; as in 100a, 127t).
 * (Maybe this can be extended for other simple hints.)
 * 
 * If all of the above matches fail, the hints blocks are converted 
 * step by step using the these Mappings:
 * o ASCII: \p{IsBasicLatin}
 * o UTF-8: .
 * o Binary: (0|1){8} (8 digits (0 or 1))
 * o Decimal: [0-9]{3} (3 decimal digits)
 * o Octal: [0-7]{3} (3 octal digits)
 * o Hexadecimal: [0-9A-Fa-f]{2} (2 hexadecimal digits)
 * The conversion is not straight forward, 
 * because there may be also (even several) length restrictions 
 * and we have to count (or try to estimate) the used octets. Thus, 
 * it is sometimes necessary to generate multiple patterns 
 * with different length specification.
 * 
 * Unfortunately, the conversion still is quite difficult to understand 
 * although I added some comments. The good news is, that all IETF MIBs 
 * (and all other MIBs coming with libsmi) can be converted "the easy way" 
 * (i.e. without the difficult conversion loop).*/

char* getStrDHType(const char *hint,
		   SmiUnsigned32 *lengths, unsigned int numSubranges)
{
    unsigned int i = 0;
    char *ret = lengths[ i ] ? "(" : "((";
    char *r2 = getKnownDisplayHint_XSD( hint, lengths, numSubranges );
    DH *dh;
    int bl;
    
    r2 = NULL;
  
    if( r2 ) {
    /*	if( lengths[ i ] && lengths[i+1] < 65535 ) {    		
    		fputs( "LENGTH: ", stdout );
    		fprintf( stdout, "min: %d, max: %d ; ", lengths[i], lengths[i+1] );
    		fprintf( stdout, "hint was: %s\n", hint );
    	} 
    	if( numSubranges >  1 ) fputs( "MORE_SUBRANGES\n", stdout );*/
    	return r2;	
    }
    dh = parseDH( hint );
    bl = 0;
    
    if(! dh ) return NULL;
    
    /* check if we have a "simple" display hint (e.g. "100a") */
    if( !dh->next && ( dh->type == 'a' || dh->type == 't' ) ) {
    	switch( numSubranges ) { 		
	case 0: 
	    return getSimpleCharFacet_XSD( dh->type , dh->number, 0, dh->number );
	case 1: 			
	    return getSimpleCharFacet_XSD( dh->type , dh->number, lengths[0], lengths[1]);
	default:
	    ret = "("; 
	    for( i=0; i < numSubranges * 2; i+=2 ) {
		if( i ) smiAsprintf( &ret, "%s)|(", ret );
		smiAsprintf( &ret, "%s%s", 
			     ret, getSimpleCharFacet_XSD(dh->type, dh->number,
							 lengths[i], 
							 lengths[i+1]));
	    }
	    smiAsprintf(&ret, "%s)", ret);
	    return ret;
	}   		
    }

    /* no "easy match was possible, so start the "hard" loop */
    do {
	unsigned int octetsUsed = 0;
	DH *iterDH;
	
	for( iterDH = dh; iterDH; iterDH = iterDH->next ) {
	    char *baseRegexp = NULL;
	    
	    switch( iterDH->type ) {
		
	    case 'a':
		/* ascii character */
		baseRegexp = "(\\p{IsBasicLatin})";
		break;
		
	    case 't':
		/* utf-8 character */
		baseRegexp = ".";
		break;
		
	    case 'b':
		/* binary number */
		baseRegexp = "((0|1){8})";
		break;
		
	    case 'd':
		/* decimal number */
		baseRegexp = "([0-9]{3})";
		break;
		
	    case 'o':
		/* octal number */
		baseRegexp = "([0-7]{3})";
		break;
		
	    case 'x':
		/* hexadecimal number */
		baseRegexp = "([0-9A-Fa-f]{2})";
		break;
		
	    default:
		fputs( "smidump: Warning: unknown type of display-hint",
		       stderr );
	    }	   
	    
	    
	    if( iterDH->number < lengths[ i ] ) {
		/* there are more octets to come */
		if( iterDH->type == 'd' ) {
		    /* decimal number needs to be treated differently */
		    if( iterDH->next ){
			/* we still have another diplay hint block coming */
			smiAsprintf( &ret, "%s(0|[1-9](([0-9]){0,%d}))",
				     ret,
				     numDigits(smiPow(255,
						      iterDH->number ))-1 );
			
			/* adjust number of used digits */
			octetsUsed += iterDH->number;
			
			if( octetsUsed >= lengths[ i + 1 ] ) {
			    /* maximum number of octets used,
			       we must exit the loop */			    
			    break;
			}
			
			/* add separator char */
			if( iterDH->separator ) {
			    smiAsprintf( &ret, "%s%s",ret, iterDH->separator );
			}
		    }
		    else {
			/* no orther display hint coming up. 
			 * we are at the last iteration */		
			smiAsprintf( &ret, "%s((0|[1-9](([0-9]){0,%d})",
				     ret,
				     numDigits( smiPow( 255,
							iterDH->number ) ) - 1 );			
			/* add separator char */
			if( iterDH->separator ) {
			    smiAsprintf( &ret, "%s%s",
					 ret, iterDH->separator );
			}
			if( lengths[ i+1 ] - 1 - octetsUsed ) {
			    /* not all digits for maximum string length (length[i+1 ])
			     * have been used, so we have to add some digits */
			    smiAsprintf(&ret,
					"%s){%u,%u})(0|[1-9](([0-9]){0,%d}))",
					ret, lengths[ i ] - 1 - octetsUsed,
					lengths[ i+1 ] - 1 - octetsUsed,
					numDigits(
					    smiPow( 255, iterDH->number ))- 1 );							
			}
			else {
			    /* maximum number of digets have been used,
			     * so let's terminate the pattern for this round*/
			    smiAsprintf( &ret, "%s)", ret );
			}
			
			/* adjust the used digit counter */
			octetsUsed += iterDH->number;
			
			if( octetsUsed >= lengths[ i + 1 ] ) {
			    /* maximum number of octets used, we must exit the loop */
			    break;
			}
		    }
		}
		else {
		    /* type other than decimal */
		    if( iterDH->next ){
			/* there will be another display hint block */
			smiAsprintf( &ret, "%s(%s{%d})",
				     ret,
				     baseRegexp, iterDH->number );
			
			/* adjust number of used octets */
			octetsUsed += iterDH->number;
			if( octetsUsed >= lengths[ i + 1 ] ) {
			    /* maximum number of octets used,
			       we must exit the loop */			    
			    break;
			}
			
			/* add separator char */
			if( iterDH->separator ) {
			    smiAsprintf( &ret, "%s%s", ret, iterDH->separator );
			}
		    }
		    else {
			/* we are the last display hint block */			
			smiAsprintf( &ret, "%s(%s",
				     ret, baseRegexp );
			
			/* add separator char */
			if( iterDH->separator ) {
			    smiAsprintf( &ret, "%s%s", ret, iterDH->separator );
			}
			
			smiAsprintf( &ret, "(%s){%u,%u})%s",
				     ret, lengths[ i ] - 1, lengths[ i+1 ] - 1,
				     baseRegexp );
			
			/* adjust the number of used octets */
			octetsUsed += iterDH->number;
			if( octetsUsed >= lengths[ i + 1 ] ) {
			    /* maximum number of octets used,
			       we must exit the loop */			    
			    break;
			}
		    }
		}
	    }
	    else {
		/* might be the last one */
		
		if( iterDH->type == 'd' ) {
		    /* decimal number needs to be treated differently */
		    if( iterDH->number < lengths[ i+1 ] ) {
			/* we are not using the maximun number of octets */
			smiAsprintf( &ret, "%s(0|[1-9]([0-9]{0,%d}))",
				     ret,
				     numDigits( smiPow( 255, iterDH->number ) ) );
			
			/* adjust the number of used octets */
			octetsUsed += lengths[ i ];
			if( octetsUsed >= lengths[ i + 1 ] ) {
			    /* the maximum number of octets have been reached,
			       we must exit the loop */
			    break;
			}
			
			/* add separator char */
			if( iterDH->separator ) {
			    smiAsprintf( &ret, "%s%s", ret, iterDH->separator );
			}						
		    }
		    else {
			/* we have used the maximum number of octets */
			smiAsprintf( &ret, "%s(0|[1-9]([0-9]{0,%d})",
				     ret,
				     numDigits( smiPow( 255, lengths[ i+1 ] ) ) );
		    }
		}
		else {
		    /* type is not decimal */
		    smiAsprintf( &ret, "%s(%s",  ret, baseRegexp );
		    if( iterDH->next ) {
			/* there will be another display hint block */
			if( iterDH->separator ) {
			    smiAsprintf( &ret, "%s%s", ret, iterDH->separator );
			}
			if( ! lengths[ i ] && lengths[ i+1 ] == 65535 ) {
			    /* minLength = 0, maxLength = 65535, 
			     * i.e. no restriction at all */
			    smiAsprintf( &ret, "%s)*",ret );
			}
			else{
			    /* we have a different length restriction */
			    smiAsprintf( &ret, "%s){%u,%u}",ret, lengths[ i ],
					 MIN( iterDH->number,
					      lengths[ i + 1] ) - 1 );
			}
			
			/* adjust the number of used octets */
			octetsUsed += lengths[ i ];
			if( octetsUsed >= lengths[ i + 1 ] ) {
			    /* the maximum number of octets have been reached,
			       we must exit the loop */
			    break;
			}						
		    }
		    else {
			/* we are the ast display hint block */
			octetsUsed += lengths[ i ];
			if( iterDH->separator &&
			    octetsUsed < lengths[ i + 1 ] ) {
			    /* we have a separator char and 
			     * still not reached the maximum number of octets */
			    if( ! lengths[ i ] && lengths[ i+1 ] == 65535 ) {
				/* we have no length restriction */
				smiAsprintf( &ret, "%s%s)*%s",
					     ret, iterDH->separator, baseRegexp );
			    }
			    else {
				/* we have a length restriction */
				smiAsprintf( &ret, "%s%s){%u,%u}%s",
					     ret, iterDH->separator,
					     lengths[ i ], lengths[ i + 1] - 1,
					     baseRegexp );
			    }
			}
			else {
			    /* we don't have a separator char or
			     * have used the maximum number of octets */
			    if( ! lengths[ i ] && lengths[ i+1 ] == 65535 ) {
				/* no lengths restriction */
				smiAsprintf( &ret, "%s)*%s",
					     ret, iterDH->separator ); 
				/* TBD: what, if there is no separator ??? */
			    }
			    else {
				/* we have a length restriction */
				smiAsprintf( &ret, "%s){%u,%u}%s",
					     ret, lengths[ i ],
					     lengths[ i + 1],
					     iterDH->separator );
				/* TBD: what, if there is no separator ??? */
			    }			    
			}
		    }
		}
		
		if( octetsUsed >= lengths[ i + 1 ] ) {
		    /* the maximum number of octets have been reached,
		       we must exit the loop */
		    break;
		}
	    }
	}
	/* adjust the "pointer" for the lenghts array */
	i += 2;
	
	if( i < numSubranges  * 2 ) {
	    /* we are not the last subrange, so we have to extend the pattern */
	    smiAsprintf( &ret, "%s)|(", ret );
	}
	else {
	    /* we are the last subrange */
	    smiAsprintf( &ret, "%s)", ret );
	    if( ! lengths[ 0 ] ) {
		smiAsprintf( &ret, "%s){0,1}", ret );
	    }
	}
    } while( i < numSubranges * 2 );
    
    /* check if all brackets have been closed */
    if( getBracketLevel( ret ) ) {
	bl = getBracketLevel( ret );
      	fprintf( stderr, "%d\n", bl );
      	if( bl > 0 ) {
	    /* TODO: add a warning that brackets have been added */
	    for(; bl; bl--) {
		smiAsprintf( &ret, "%s)", ret );
	    }
      	}
      	else {
	    /* TODO: some error handling */
      	}
    }
    return ret;
}

/*
 * Perhaps we should introduce another parameter (flags) which
 * controls whether shortcuts are taken and such things...
 */

extern char*
smiFormatToPattern(const char *format, SmiRange *smiRange)
{
    SmiRange *range = smiRange;
    SmiUnsigned32 *lengths = NULL;
    int num = 0, lp;
    char *pattern;

    for (range = smiRange; range; range = smiGetNextRange(range)) {
	num++;
    }

    /* copy ranges to array (no clue why this is being done) */
    
    if (num) {
	lengths = xmalloc(2 * num * sizeof(SmiUnsigned32));
	for (range = smiRange, lp = 0; range; range = smiGetNextRange(range)) {
	    lengths[lp++] = range->minValue.value.unsigned32;
	    lengths[lp++] = range->maxValue.value.unsigned32;
	}
    } else {
	lengths = xmalloc( 2 * sizeof(SmiUnsigned32));
	lengths[0] = 0;
	lengths[1] = 65535;
    }

    pattern = getStrDHType(format, lengths, num);
    xfree(lengths);

    return pattern;
}