Merge remote-tracking branches 'asoc/fix/compress', 'asoc/fix/core', 'asoc/fix/dapm...
[linux] / net / netfilter / nf_conntrack_h323_asn1.c
1 /****************************************************************************
2  * ip_conntrack_helper_h323_asn1.c - BER and PER decoding library for H.323
3  *                                   conntrack/NAT module.
4  *
5  * Copyright (c) 2006 by Jing Min Zhao <zhaojingmin@users.sourceforge.net>
6  *
7  * This source code is licensed under General Public License version 2.
8  *
9  * See ip_conntrack_helper_h323_asn1.h for details.
10  *
11  ****************************************************************************/
12
13 #ifdef __KERNEL__
14 #include <linux/kernel.h>
15 #else
16 #include <stdio.h>
17 #endif
18 #include <linux/netfilter/nf_conntrack_h323_asn1.h>
19
20 /* Trace Flag */
21 #ifndef H323_TRACE
22 #define H323_TRACE 0
23 #endif
24
25 #if H323_TRACE
26 #define TAB_SIZE 4
27 #define IFTHEN(cond, act) if(cond){act;}
28 #ifdef __KERNEL__
29 #define PRINT printk
30 #else
31 #define PRINT printf
32 #endif
33 #define FNAME(name) name,
34 #else
35 #define IFTHEN(cond, act)
36 #define PRINT(fmt, args...)
37 #define FNAME(name)
38 #endif
39
40 /* ASN.1 Types */
41 #define NUL 0
42 #define BOOL 1
43 #define OID 2
44 #define INT 3
45 #define ENUM 4
46 #define BITSTR 5
47 #define NUMSTR 6
48 #define NUMDGT 6
49 #define TBCDSTR 6
50 #define OCTSTR 7
51 #define PRTSTR 7
52 #define IA5STR 7
53 #define GENSTR 7
54 #define BMPSTR 8
55 #define SEQ 9
56 #define SET 9
57 #define SEQOF 10
58 #define SETOF 10
59 #define CHOICE 11
60
61 /* Constraint Types */
62 #define FIXD 0
63 /* #define BITS 1-8 */
64 #define BYTE 9
65 #define WORD 10
66 #define CONS 11
67 #define SEMI 12
68 #define UNCO 13
69
70 /* ASN.1 Type Attributes */
71 #define SKIP 0
72 #define STOP 1
73 #define DECODE 2
74 #define EXT 4
75 #define OPEN 8
76 #define OPT 16
77
78
79 /* ASN.1 Field Structure */
80 typedef struct field_t {
81 #if H323_TRACE
82         char *name;
83 #endif
84         unsigned char type;
85         unsigned char sz;
86         unsigned char lb;
87         unsigned char ub;
88         unsigned short attr;
89         unsigned short offset;
90         const struct field_t *fields;
91 } field_t;
92
93 /* Bit Stream */
94 struct bitstr {
95         unsigned char *buf;
96         unsigned char *beg;
97         unsigned char *end;
98         unsigned char *cur;
99         unsigned int bit;
100 };
101
102 /* Tool Functions */
103 #define INC_BIT(bs) if((++(bs)->bit)>7){(bs)->cur++;(bs)->bit=0;}
104 #define INC_BITS(bs,b) if(((bs)->bit+=(b))>7){(bs)->cur+=(bs)->bit>>3;(bs)->bit&=7;}
105 #define BYTE_ALIGN(bs) if((bs)->bit){(bs)->cur++;(bs)->bit=0;}
106 static unsigned int get_len(struct bitstr *bs);
107 static unsigned int get_bit(struct bitstr *bs);
108 static unsigned int get_bits(struct bitstr *bs, unsigned int b);
109 static unsigned int get_bitmap(struct bitstr *bs, unsigned int b);
110 static unsigned int get_uint(struct bitstr *bs, int b);
111
112 /* Decoder Functions */
113 static int decode_nul(struct bitstr *bs, const struct field_t *f, char *base, int level);
114 static int decode_bool(struct bitstr *bs, const struct field_t *f, char *base, int level);
115 static int decode_oid(struct bitstr *bs, const struct field_t *f, char *base, int level);
116 static int decode_int(struct bitstr *bs, const struct field_t *f, char *base, int level);
117 static int decode_enum(struct bitstr *bs, const struct field_t *f, char *base, int level);
118 static int decode_bitstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
119 static int decode_numstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
120 static int decode_octstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
121 static int decode_bmpstr(struct bitstr *bs, const struct field_t *f, char *base, int level);
122 static int decode_seq(struct bitstr *bs, const struct field_t *f, char *base, int level);
123 static int decode_seqof(struct bitstr *bs, const struct field_t *f, char *base, int level);
124 static int decode_choice(struct bitstr *bs, const struct field_t *f, char *base, int level);
125
126 /* Decoder Functions Vector */
127 typedef int (*decoder_t)(struct bitstr *, const struct field_t *, char *, int);
128 static const decoder_t Decoders[] = {
129         decode_nul,
130         decode_bool,
131         decode_oid,
132         decode_int,
133         decode_enum,
134         decode_bitstr,
135         decode_numstr,
136         decode_octstr,
137         decode_bmpstr,
138         decode_seq,
139         decode_seqof,
140         decode_choice,
141 };
142
143 /****************************************************************************
144  * H.323 Types
145  ****************************************************************************/
146 #include "nf_conntrack_h323_types.c"
147
148 /****************************************************************************
149  * Functions
150  ****************************************************************************/
151 /* Assume bs is aligned && v < 16384 */
152 static unsigned int get_len(struct bitstr *bs)
153 {
154         unsigned int v;
155
156         v = *bs->cur++;
157
158         if (v & 0x80) {
159                 v &= 0x3f;
160                 v <<= 8;
161                 v += *bs->cur++;
162         }
163
164         return v;
165 }
166
167 static int nf_h323_error_boundary(struct bitstr *bs, size_t bytes, size_t bits)
168 {
169         bits += bs->bit;
170         bytes += bits / BITS_PER_BYTE;
171         if (bits % BITS_PER_BYTE > 0)
172                 bytes++;
173
174         if (*bs->cur + bytes > *bs->end)
175                 return 1;
176
177         return 0;
178 }
179
180 /****************************************************************************/
181 static unsigned int get_bit(struct bitstr *bs)
182 {
183         unsigned int b = (*bs->cur) & (0x80 >> bs->bit);
184
185         INC_BIT(bs);
186
187         return b;
188 }
189
190 /****************************************************************************/
191 /* Assume b <= 8 */
192 static unsigned int get_bits(struct bitstr *bs, unsigned int b)
193 {
194         unsigned int v, l;
195
196         v = (*bs->cur) & (0xffU >> bs->bit);
197         l = b + bs->bit;
198
199         if (l < 8) {
200                 v >>= 8 - l;
201                 bs->bit = l;
202         } else if (l == 8) {
203                 bs->cur++;
204                 bs->bit = 0;
205         } else {                /* l > 8 */
206
207                 v <<= 8;
208                 v += *(++bs->cur);
209                 v >>= 16 - l;
210                 bs->bit = l - 8;
211         }
212
213         return v;
214 }
215
216 /****************************************************************************/
217 /* Assume b <= 32 */
218 static unsigned int get_bitmap(struct bitstr *bs, unsigned int b)
219 {
220         unsigned int v, l, shift, bytes;
221
222         if (!b)
223                 return 0;
224
225         l = bs->bit + b;
226
227         if (l < 8) {
228                 v = (unsigned int)(*bs->cur) << (bs->bit + 24);
229                 bs->bit = l;
230         } else if (l == 8) {
231                 v = (unsigned int)(*bs->cur++) << (bs->bit + 24);
232                 bs->bit = 0;
233         } else {
234                 for (bytes = l >> 3, shift = 24, v = 0; bytes;
235                      bytes--, shift -= 8)
236                         v |= (unsigned int)(*bs->cur++) << shift;
237
238                 if (l < 32) {
239                         v |= (unsigned int)(*bs->cur) << shift;
240                         v <<= bs->bit;
241                 } else if (l > 32) {
242                         v <<= bs->bit;
243                         v |= (*bs->cur) >> (8 - bs->bit);
244                 }
245
246                 bs->bit = l & 0x7;
247         }
248
249         v &= 0xffffffff << (32 - b);
250
251         return v;
252 }
253
254 /****************************************************************************
255  * Assume bs is aligned and sizeof(unsigned int) == 4
256  ****************************************************************************/
257 static unsigned int get_uint(struct bitstr *bs, int b)
258 {
259         unsigned int v = 0;
260
261         switch (b) {
262         case 4:
263                 v |= *bs->cur++;
264                 v <<= 8;
265         case 3:
266                 v |= *bs->cur++;
267                 v <<= 8;
268         case 2:
269                 v |= *bs->cur++;
270                 v <<= 8;
271         case 1:
272                 v |= *bs->cur++;
273                 break;
274         }
275         return v;
276 }
277
278 /****************************************************************************/
279 static int decode_nul(struct bitstr *bs, const struct field_t *f,
280                       char *base, int level)
281 {
282         PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
283
284         return H323_ERROR_NONE;
285 }
286
287 /****************************************************************************/
288 static int decode_bool(struct bitstr *bs, const struct field_t *f,
289                        char *base, int level)
290 {
291         PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
292
293         INC_BIT(bs);
294         if (nf_h323_error_boundary(bs, 0, 0))
295                 return H323_ERROR_BOUND;
296         return H323_ERROR_NONE;
297 }
298
299 /****************************************************************************/
300 static int decode_oid(struct bitstr *bs, const struct field_t *f,
301                       char *base, int level)
302 {
303         int len;
304
305         PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
306
307         BYTE_ALIGN(bs);
308         if (nf_h323_error_boundary(bs, 1, 0))
309                 return H323_ERROR_BOUND;
310
311         len = *bs->cur++;
312         bs->cur += len;
313         if (nf_h323_error_boundary(bs, 0, 0))
314                 return H323_ERROR_BOUND;
315
316         return H323_ERROR_NONE;
317 }
318
319 /****************************************************************************/
320 static int decode_int(struct bitstr *bs, const struct field_t *f,
321                       char *base, int level)
322 {
323         unsigned int len;
324
325         PRINT("%*.s%s", level * TAB_SIZE, " ", f->name);
326
327         switch (f->sz) {
328         case BYTE:              /* Range == 256 */
329                 BYTE_ALIGN(bs);
330                 bs->cur++;
331                 break;
332         case WORD:              /* 257 <= Range <= 64K */
333                 BYTE_ALIGN(bs);
334                 bs->cur += 2;
335                 break;
336         case CONS:              /* 64K < Range < 4G */
337                 if (nf_h323_error_boundary(bs, 0, 2))
338                         return H323_ERROR_BOUND;
339                 len = get_bits(bs, 2) + 1;
340                 BYTE_ALIGN(bs);
341                 if (base && (f->attr & DECODE)) {       /* timeToLive */
342                         unsigned int v = get_uint(bs, len) + f->lb;
343                         PRINT(" = %u", v);
344                         *((unsigned int *)(base + f->offset)) = v;
345                 }
346                 bs->cur += len;
347                 break;
348         case UNCO:
349                 BYTE_ALIGN(bs);
350                 if (nf_h323_error_boundary(bs, 2, 0))
351                         return H323_ERROR_BOUND;
352                 len = get_len(bs);
353                 bs->cur += len;
354                 break;
355         default:                /* 2 <= Range <= 255 */
356                 INC_BITS(bs, f->sz);
357                 break;
358         }
359
360         PRINT("\n");
361
362         if (nf_h323_error_boundary(bs, 0, 0))
363                 return H323_ERROR_BOUND;
364         return H323_ERROR_NONE;
365 }
366
367 /****************************************************************************/
368 static int decode_enum(struct bitstr *bs, const struct field_t *f,
369                        char *base, int level)
370 {
371         PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
372
373         if ((f->attr & EXT) && get_bit(bs)) {
374                 INC_BITS(bs, 7);
375         } else {
376                 INC_BITS(bs, f->sz);
377         }
378
379         if (nf_h323_error_boundary(bs, 0, 0))
380                 return H323_ERROR_BOUND;
381         return H323_ERROR_NONE;
382 }
383
384 /****************************************************************************/
385 static int decode_bitstr(struct bitstr *bs, const struct field_t *f,
386                          char *base, int level)
387 {
388         unsigned int len;
389
390         PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
391
392         BYTE_ALIGN(bs);
393         switch (f->sz) {
394         case FIXD:              /* fixed length > 16 */
395                 len = f->lb;
396                 break;
397         case WORD:              /* 2-byte length */
398                 if (nf_h323_error_boundary(bs, 2, 0))
399                         return H323_ERROR_BOUND;
400                 len = (*bs->cur++) << 8;
401                 len += (*bs->cur++) + f->lb;
402                 break;
403         case SEMI:
404                 if (nf_h323_error_boundary(bs, 2, 0))
405                         return H323_ERROR_BOUND;
406                 len = get_len(bs);
407                 break;
408         default:
409                 len = 0;
410                 break;
411         }
412
413         bs->cur += len >> 3;
414         bs->bit = len & 7;
415
416         if (nf_h323_error_boundary(bs, 0, 0))
417                 return H323_ERROR_BOUND;
418         return H323_ERROR_NONE;
419 }
420
421 /****************************************************************************/
422 static int decode_numstr(struct bitstr *bs, const struct field_t *f,
423                          char *base, int level)
424 {
425         unsigned int len;
426
427         PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
428
429         /* 2 <= Range <= 255 */
430         if (nf_h323_error_boundary(bs, 0, f->sz))
431                 return H323_ERROR_BOUND;
432         len = get_bits(bs, f->sz) + f->lb;
433
434         BYTE_ALIGN(bs);
435         INC_BITS(bs, (len << 2));
436
437         if (nf_h323_error_boundary(bs, 0, 0))
438                 return H323_ERROR_BOUND;
439         return H323_ERROR_NONE;
440 }
441
442 /****************************************************************************/
443 static int decode_octstr(struct bitstr *bs, const struct field_t *f,
444                          char *base, int level)
445 {
446         unsigned int len;
447
448         PRINT("%*.s%s", level * TAB_SIZE, " ", f->name);
449
450         switch (f->sz) {
451         case FIXD:              /* Range == 1 */
452                 if (f->lb > 2) {
453                         BYTE_ALIGN(bs);
454                         if (base && (f->attr & DECODE)) {
455                                 /* The IP Address */
456                                 IFTHEN(f->lb == 4,
457                                        PRINT(" = %d.%d.%d.%d:%d",
458                                              bs->cur[0], bs->cur[1],
459                                              bs->cur[2], bs->cur[3],
460                                              bs->cur[4] * 256 + bs->cur[5]));
461                                 *((unsigned int *)(base + f->offset)) =
462                                     bs->cur - bs->buf;
463                         }
464                 }
465                 len = f->lb;
466                 break;
467         case BYTE:              /* Range == 256 */
468                 BYTE_ALIGN(bs);
469                 if (nf_h323_error_boundary(bs, 1, 0))
470                         return H323_ERROR_BOUND;
471                 len = (*bs->cur++) + f->lb;
472                 break;
473         case SEMI:
474                 BYTE_ALIGN(bs);
475                 if (nf_h323_error_boundary(bs, 2, 0))
476                         return H323_ERROR_BOUND;
477                 len = get_len(bs) + f->lb;
478                 break;
479         default:                /* 2 <= Range <= 255 */
480                 if (nf_h323_error_boundary(bs, 0, f->sz))
481                         return H323_ERROR_BOUND;
482                 len = get_bits(bs, f->sz) + f->lb;
483                 BYTE_ALIGN(bs);
484                 break;
485         }
486
487         bs->cur += len;
488
489         PRINT("\n");
490
491         if (nf_h323_error_boundary(bs, 0, 0))
492                 return H323_ERROR_BOUND;
493         return H323_ERROR_NONE;
494 }
495
496 /****************************************************************************/
497 static int decode_bmpstr(struct bitstr *bs, const struct field_t *f,
498                          char *base, int level)
499 {
500         unsigned int len;
501
502         PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
503
504         switch (f->sz) {
505         case BYTE:              /* Range == 256 */
506                 BYTE_ALIGN(bs);
507                 if (nf_h323_error_boundary(bs, 1, 0))
508                         return H323_ERROR_BOUND;
509                 len = (*bs->cur++) + f->lb;
510                 break;
511         default:                /* 2 <= Range <= 255 */
512                 if (nf_h323_error_boundary(bs, 0, f->sz))
513                         return H323_ERROR_BOUND;
514                 len = get_bits(bs, f->sz) + f->lb;
515                 BYTE_ALIGN(bs);
516                 break;
517         }
518
519         bs->cur += len << 1;
520
521         if (nf_h323_error_boundary(bs, 0, 0))
522                 return H323_ERROR_BOUND;
523         return H323_ERROR_NONE;
524 }
525
526 /****************************************************************************/
527 static int decode_seq(struct bitstr *bs, const struct field_t *f,
528                       char *base, int level)
529 {
530         unsigned int ext, bmp, i, opt, len = 0, bmp2, bmp2_len;
531         int err;
532         const struct field_t *son;
533         unsigned char *beg = NULL;
534
535         PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
536
537         /* Decode? */
538         base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
539
540         /* Extensible? */
541         if (nf_h323_error_boundary(bs, 0, 1))
542                 return H323_ERROR_BOUND;
543         ext = (f->attr & EXT) ? get_bit(bs) : 0;
544
545         /* Get fields bitmap */
546         if (nf_h323_error_boundary(bs, 0, f->sz))
547                 return H323_ERROR_BOUND;
548         bmp = get_bitmap(bs, f->sz);
549         if (base)
550                 *(unsigned int *)base = bmp;
551
552         /* Decode the root components */
553         for (i = opt = 0, son = f->fields; i < f->lb; i++, son++) {
554                 if (son->attr & STOP) {
555                         PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
556                               son->name);
557                         return H323_ERROR_STOP;
558                 }
559
560                 if (son->attr & OPT) {  /* Optional component */
561                         if (!((0x80000000U >> (opt++)) & bmp))  /* Not exist */
562                                 continue;
563                 }
564
565                 /* Decode */
566                 if (son->attr & OPEN) { /* Open field */
567                         if (nf_h323_error_boundary(bs, 2, 0))
568                                 return H323_ERROR_BOUND;
569                         len = get_len(bs);
570                         if (nf_h323_error_boundary(bs, len, 0))
571                                 return H323_ERROR_BOUND;
572                         if (!base || !(son->attr & DECODE)) {
573                                 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE,
574                                       " ", son->name);
575                                 bs->cur += len;
576                                 continue;
577                         }
578                         beg = bs->cur;
579
580                         /* Decode */
581                         if ((err = (Decoders[son->type]) (bs, son, base,
582                                                           level + 1)) <
583                             H323_ERROR_NONE)
584                                 return err;
585
586                         bs->cur = beg + len;
587                         bs->bit = 0;
588                 } else if ((err = (Decoders[son->type]) (bs, son, base,
589                                                          level + 1)) <
590                            H323_ERROR_NONE)
591                         return err;
592         }
593
594         /* No extension? */
595         if (!ext)
596                 return H323_ERROR_NONE;
597
598         /* Get the extension bitmap */
599         if (nf_h323_error_boundary(bs, 0, 7))
600                 return H323_ERROR_BOUND;
601         bmp2_len = get_bits(bs, 7) + 1;
602         if (nf_h323_error_boundary(bs, 0, bmp2_len))
603                 return H323_ERROR_BOUND;
604         bmp2 = get_bitmap(bs, bmp2_len);
605         bmp |= bmp2 >> f->sz;
606         if (base)
607                 *(unsigned int *)base = bmp;
608         BYTE_ALIGN(bs);
609
610         /* Decode the extension components */
611         for (opt = 0; opt < bmp2_len; opt++, i++, son++) {
612                 /* Check Range */
613                 if (i >= f->ub) {       /* Newer Version? */
614                         if (nf_h323_error_boundary(bs, 2, 0))
615                                 return H323_ERROR_BOUND;
616                         len = get_len(bs);
617                         if (nf_h323_error_boundary(bs, len, 0))
618                                 return H323_ERROR_BOUND;
619                         bs->cur += len;
620                         continue;
621                 }
622
623                 if (son->attr & STOP) {
624                         PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
625                               son->name);
626                         return H323_ERROR_STOP;
627                 }
628
629                 if (!((0x80000000 >> opt) & bmp2))      /* Not present */
630                         continue;
631
632                 if (nf_h323_error_boundary(bs, 2, 0))
633                         return H323_ERROR_BOUND;
634                 len = get_len(bs);
635                 if (nf_h323_error_boundary(bs, len, 0))
636                         return H323_ERROR_BOUND;
637                 if (!base || !(son->attr & DECODE)) {
638                         PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
639                               son->name);
640                         bs->cur += len;
641                         continue;
642                 }
643                 beg = bs->cur;
644
645                 if ((err = (Decoders[son->type]) (bs, son, base,
646                                                   level + 1)) <
647                     H323_ERROR_NONE)
648                         return err;
649
650                 bs->cur = beg + len;
651                 bs->bit = 0;
652         }
653         return H323_ERROR_NONE;
654 }
655
656 /****************************************************************************/
657 static int decode_seqof(struct bitstr *bs, const struct field_t *f,
658                         char *base, int level)
659 {
660         unsigned int count, effective_count = 0, i, len = 0;
661         int err;
662         const struct field_t *son;
663         unsigned char *beg = NULL;
664
665         PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
666
667         /* Decode? */
668         base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
669
670         /* Decode item count */
671         switch (f->sz) {
672         case BYTE:
673                 BYTE_ALIGN(bs);
674                 if (nf_h323_error_boundary(bs, 1, 0))
675                         return H323_ERROR_BOUND;
676                 count = *bs->cur++;
677                 break;
678         case WORD:
679                 BYTE_ALIGN(bs);
680                 if (nf_h323_error_boundary(bs, 2, 0))
681                         return H323_ERROR_BOUND;
682                 count = *bs->cur++;
683                 count <<= 8;
684                 count += *bs->cur++;
685                 break;
686         case SEMI:
687                 BYTE_ALIGN(bs);
688                 if (nf_h323_error_boundary(bs, 2, 0))
689                         return H323_ERROR_BOUND;
690                 count = get_len(bs);
691                 break;
692         default:
693                 if (nf_h323_error_boundary(bs, 0, f->sz))
694                         return H323_ERROR_BOUND;
695                 count = get_bits(bs, f->sz);
696                 break;
697         }
698         count += f->lb;
699
700         /* Write Count */
701         if (base) {
702                 effective_count = count > f->ub ? f->ub : count;
703                 *(unsigned int *)base = effective_count;
704                 base += sizeof(unsigned int);
705         }
706
707         /* Decode nested field */
708         son = f->fields;
709         if (base)
710                 base -= son->offset;
711         for (i = 0; i < count; i++) {
712                 if (son->attr & OPEN) {
713                         BYTE_ALIGN(bs);
714                         if (nf_h323_error_boundary(bs, 2, 0))
715                                 return H323_ERROR_BOUND;
716                         len = get_len(bs);
717                         if (nf_h323_error_boundary(bs, len, 0))
718                                 return H323_ERROR_BOUND;
719                         if (!base || !(son->attr & DECODE)) {
720                                 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE,
721                                       " ", son->name);
722                                 bs->cur += len;
723                                 continue;
724                         }
725                         beg = bs->cur;
726
727                         if ((err = (Decoders[son->type]) (bs, son,
728                                                           i <
729                                                           effective_count ?
730                                                           base : NULL,
731                                                           level + 1)) <
732                             H323_ERROR_NONE)
733                                 return err;
734
735                         bs->cur = beg + len;
736                         bs->bit = 0;
737                 } else
738                         if ((err = (Decoders[son->type]) (bs, son,
739                                                           i <
740                                                           effective_count ?
741                                                           base : NULL,
742                                                           level + 1)) <
743                             H323_ERROR_NONE)
744                                 return err;
745
746                 if (base)
747                         base += son->offset;
748         }
749
750         return H323_ERROR_NONE;
751 }
752
753
754 /****************************************************************************/
755 static int decode_choice(struct bitstr *bs, const struct field_t *f,
756                          char *base, int level)
757 {
758         unsigned int type, ext, len = 0;
759         int err;
760         const struct field_t *son;
761         unsigned char *beg = NULL;
762
763         PRINT("%*.s%s\n", level * TAB_SIZE, " ", f->name);
764
765         /* Decode? */
766         base = (base && (f->attr & DECODE)) ? base + f->offset : NULL;
767
768         /* Decode the choice index number */
769         if (nf_h323_error_boundary(bs, 0, 1))
770                 return H323_ERROR_BOUND;
771         if ((f->attr & EXT) && get_bit(bs)) {
772                 ext = 1;
773                 if (nf_h323_error_boundary(bs, 0, 7))
774                         return H323_ERROR_BOUND;
775                 type = get_bits(bs, 7) + f->lb;
776         } else {
777                 ext = 0;
778                 if (nf_h323_error_boundary(bs, 0, f->sz))
779                         return H323_ERROR_BOUND;
780                 type = get_bits(bs, f->sz);
781                 if (type >= f->lb)
782                         return H323_ERROR_RANGE;
783         }
784
785         /* Write Type */
786         if (base)
787                 *(unsigned int *)base = type;
788
789         /* Check Range */
790         if (type >= f->ub) {    /* Newer version? */
791                 BYTE_ALIGN(bs);
792                 if (nf_h323_error_boundary(bs, 2, 0))
793                         return H323_ERROR_BOUND;
794                 len = get_len(bs);
795                 if (nf_h323_error_boundary(bs, len, 0))
796                         return H323_ERROR_BOUND;
797                 bs->cur += len;
798                 return H323_ERROR_NONE;
799         }
800
801         /* Transfer to son level */
802         son = &f->fields[type];
803         if (son->attr & STOP) {
804                 PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ", son->name);
805                 return H323_ERROR_STOP;
806         }
807
808         if (ext || (son->attr & OPEN)) {
809                 BYTE_ALIGN(bs);
810                 if (nf_h323_error_boundary(bs, len, 0))
811                         return H323_ERROR_BOUND;
812                 len = get_len(bs);
813                 if (nf_h323_error_boundary(bs, len, 0))
814                         return H323_ERROR_BOUND;
815                 if (!base || !(son->attr & DECODE)) {
816                         PRINT("%*.s%s\n", (level + 1) * TAB_SIZE, " ",
817                               son->name);
818                         bs->cur += len;
819                         return H323_ERROR_NONE;
820                 }
821                 beg = bs->cur;
822
823                 if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) <
824                     H323_ERROR_NONE)
825                         return err;
826
827                 bs->cur = beg + len;
828                 bs->bit = 0;
829         } else if ((err = (Decoders[son->type]) (bs, son, base, level + 1)) <
830                    H323_ERROR_NONE)
831                 return err;
832
833         return H323_ERROR_NONE;
834 }
835
836 /****************************************************************************/
837 int DecodeRasMessage(unsigned char *buf, size_t sz, RasMessage *ras)
838 {
839         static const struct field_t ras_message = {
840                 FNAME("RasMessage") CHOICE, 5, 24, 32, DECODE | EXT,
841                 0, _RasMessage
842         };
843         struct bitstr bs;
844
845         bs.buf = bs.beg = bs.cur = buf;
846         bs.end = buf + sz;
847         bs.bit = 0;
848
849         return decode_choice(&bs, &ras_message, (char *) ras, 0);
850 }
851
852 /****************************************************************************/
853 static int DecodeH323_UserInformation(unsigned char *buf, unsigned char *beg,
854                                       size_t sz, H323_UserInformation *uuie)
855 {
856         static const struct field_t h323_userinformation = {
857                 FNAME("H323-UserInformation") SEQ, 1, 2, 2, DECODE | EXT,
858                 0, _H323_UserInformation
859         };
860         struct bitstr bs;
861
862         bs.buf = buf;
863         bs.beg = bs.cur = beg;
864         bs.end = beg + sz;
865         bs.bit = 0;
866
867         return decode_seq(&bs, &h323_userinformation, (char *) uuie, 0);
868 }
869
870 /****************************************************************************/
871 int DecodeMultimediaSystemControlMessage(unsigned char *buf, size_t sz,
872                                          MultimediaSystemControlMessage *
873                                          mscm)
874 {
875         static const struct field_t multimediasystemcontrolmessage = {
876                 FNAME("MultimediaSystemControlMessage") CHOICE, 2, 4, 4,
877                 DECODE | EXT, 0, _MultimediaSystemControlMessage
878         };
879         struct bitstr bs;
880
881         bs.buf = bs.beg = bs.cur = buf;
882         bs.end = buf + sz;
883         bs.bit = 0;
884
885         return decode_choice(&bs, &multimediasystemcontrolmessage,
886                              (char *) mscm, 0);
887 }
888
889 /****************************************************************************/
890 int DecodeQ931(unsigned char *buf, size_t sz, Q931 *q931)
891 {
892         unsigned char *p = buf;
893         int len;
894
895         if (!p || sz < 1)
896                 return H323_ERROR_BOUND;
897
898         /* Protocol Discriminator */
899         if (*p != 0x08) {
900                 PRINT("Unknown Protocol Discriminator\n");
901                 return H323_ERROR_RANGE;
902         }
903         p++;
904         sz--;
905
906         /* CallReferenceValue */
907         if (sz < 1)
908                 return H323_ERROR_BOUND;
909         len = *p++;
910         sz--;
911         if (sz < len)
912                 return H323_ERROR_BOUND;
913         p += len;
914         sz -= len;
915
916         /* Message Type */
917         if (sz < 2)
918                 return H323_ERROR_BOUND;
919         q931->MessageType = *p++;
920         sz--;
921         PRINT("MessageType = %02X\n", q931->MessageType);
922         if (*p & 0x80) {
923                 p++;
924                 sz--;
925         }
926
927         /* Decode Information Elements */
928         while (sz > 0) {
929                 if (*p == 0x7e) {       /* UserUserIE */
930                         if (sz < 3)
931                                 break;
932                         p++;
933                         len = *p++ << 8;
934                         len |= *p++;
935                         sz -= 3;
936                         if (sz < len)
937                                 break;
938                         p++;
939                         len--;
940                         return DecodeH323_UserInformation(buf, p, len,
941                                                           &q931->UUIE);
942                 }
943                 p++;
944                 sz--;
945                 if (sz < 1)
946                         break;
947                 len = *p++;
948                 sz--;
949                 if (sz < len)
950                         break;
951                 p += len;
952                 sz -= len;
953         }
954
955         PRINT("Q.931 UUIE not found\n");
956
957         return H323_ERROR_BOUND;
958 }