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