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