1 | /*
|
---|
2 | rdesktop: A Remote Desktop Protocol client.
|
---|
3 | Parsing primitives
|
---|
4 | Copyright (C) Matthew Chapman 1999-2008
|
---|
5 |
|
---|
6 | This program is free software: you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation, either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /*
|
---|
21 | * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
22 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
23 | * the General Public License version 2 (GPLv2) at this time for any software where
|
---|
24 | * a choice of GPL license versions is made available with the language indicating
|
---|
25 | * that GPLv2 or any later version may be used, or where a choice of which version
|
---|
26 | * of the GPL is applied is otherwise unspecified.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /* Parser state */
|
---|
30 | typedef struct stream
|
---|
31 | {
|
---|
32 | unsigned char *p;
|
---|
33 | unsigned char *end;
|
---|
34 | unsigned char *data;
|
---|
35 | unsigned int size;
|
---|
36 |
|
---|
37 | /* Offsets of various headers */
|
---|
38 | unsigned char *iso_hdr;
|
---|
39 | unsigned char *mcs_hdr;
|
---|
40 | unsigned char *sec_hdr;
|
---|
41 | unsigned char *rdp_hdr;
|
---|
42 | unsigned char *channel_hdr;
|
---|
43 |
|
---|
44 | }
|
---|
45 | *STREAM;
|
---|
46 |
|
---|
47 | #define s_push_layer(s,h,n) { (s)->h = (s)->p; (s)->p += n; }
|
---|
48 | #define s_pop_layer(s,h) (s)->p = (s)->h;
|
---|
49 | #define s_mark_end(s) (s)->end = (s)->p;
|
---|
50 | #define s_check(s) ((s)->p <= (s)->end)
|
---|
51 | #define s_check_rem(s,n) ((s)->p + n <= (s)->end)
|
---|
52 | #define s_check_end(s) ((s)->p == (s)->end)
|
---|
53 |
|
---|
54 | #if defined(L_ENDIAN) && !defined(NEED_ALIGN)
|
---|
55 | #define in_uint16_le(s,v) { v = *(uint16 *)((s)->p); (s)->p += 2; }
|
---|
56 | #define in_uint32_le(s,v) { v = *(uint32 *)((s)->p); (s)->p += 4; }
|
---|
57 | #define out_uint16_le(s,v) { *(uint16 *)((s)->p) = v; (s)->p += 2; }
|
---|
58 | #define out_uint32_le(s,v) { *(uint32 *)((s)->p) = v; (s)->p += 4; }
|
---|
59 |
|
---|
60 | #else
|
---|
61 | #define in_uint16_le(s,v) { v = *((s)->p++); v += *((s)->p++) << 8; }
|
---|
62 | #define in_uint32_le(s,v) { in_uint16_le(s,v) \
|
---|
63 | v += *((s)->p++) << 16; v += *((s)->p++) << 24; }
|
---|
64 | #define out_uint16_le(s,v) { *((s)->p++) = (v) & 0xff; *((s)->p++) = ((v) >> 8) & 0xff; }
|
---|
65 | #define out_uint32_le(s,v) { out_uint16_le(s, (v) & 0xffff); out_uint16_le(s, ((v) >> 16) & 0xffff); }
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #if defined(B_ENDIAN) && !defined(NEED_ALIGN)
|
---|
69 | #define in_uint16_be(s,v) { v = *(uint16 *)((s)->p); (s)->p += 2; }
|
---|
70 | #define in_uint32_be(s,v) { v = *(uint32 *)((s)->p); (s)->p += 4; }
|
---|
71 | #define out_uint16_be(s,v) { *(uint16 *)((s)->p) = v; (s)->p += 2; }
|
---|
72 | #define out_uint32_be(s,v) { *(uint32 *)((s)->p) = v; (s)->p += 4; }
|
---|
73 |
|
---|
74 | #define B_ENDIAN_PREFERRED
|
---|
75 | #define in_uint16(s,v) in_uint16_be(s,v)
|
---|
76 | #define in_uint32(s,v) in_uint32_be(s,v)
|
---|
77 | #define out_uint16(s,v) out_uint16_be(s,v)
|
---|
78 | #define out_uint32(s,v) out_uint32_be(s,v)
|
---|
79 |
|
---|
80 | #else
|
---|
81 | #define in_uint16_be(s,v) { v = *((s)->p++); next_be(s,v); }
|
---|
82 | #define in_uint32_be(s,v) { in_uint16_be(s,v); next_be(s,v); next_be(s,v); }
|
---|
83 | #define out_uint16_be(s,v) { *((s)->p++) = ((v) >> 8) & 0xff; *((s)->p++) = (v) & 0xff; }
|
---|
84 | #define out_uint32_be(s,v) { out_uint16_be(s, ((v) >> 16) & 0xffff); out_uint16_be(s, (v) & 0xffff); }
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | #ifndef B_ENDIAN_PREFERRED
|
---|
88 | #define in_uint16(s,v) in_uint16_le(s,v)
|
---|
89 | #define in_uint32(s,v) in_uint32_le(s,v)
|
---|
90 | #define out_uint16(s,v) out_uint16_le(s,v)
|
---|
91 | #define out_uint32(s,v) out_uint32_le(s,v)
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | #define in_uint8(s,v) v = *((s)->p++);
|
---|
95 | #define in_uint8p(s,v,n) { v = (s)->p; (s)->p += n; }
|
---|
96 | #define in_uint8a(s,v,n) { memcpy(v,(s)->p,n); (s)->p += n; }
|
---|
97 | #define in_uint8s(s,n) (s)->p += n;
|
---|
98 | #define out_uint8(s,v) *((s)->p++) = v;
|
---|
99 | #define out_uint8p(s,v,n) { memcpy((s)->p,v,n); (s)->p += n; }
|
---|
100 | #define out_uint8a(s,v,n) out_uint8p(s,v,n);
|
---|
101 | #define out_uint8s(s,n) { memset((s)->p,0,n); (s)->p += n; }
|
---|
102 |
|
---|
103 | #define next_be(s,v) v = ((v) << 8) + *((s)->p++);
|
---|