summaryrefslogtreecommitdiffstats
path: root/xrdp/parse.h
blob: 05d0e4b31de344dbc53af53a44626f9d93cc165a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
   rdesktop: A Remote Desktop Protocol client.
   Parsing primitives
   Copyright (C) Matthew Chapman 1999-2002

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/* modified for xrdp */
/* this is a super fast stream method, you bet */
/* all little endian for now */

/* parser state */
struct stream
{
  char* p;
  char* end;
  char* data;
  int size;
  /* offsets of various headers */
  char* iso_hdr;
  char* mcs_hdr;
  char* sec_hdr;
  char* rdp_hdr;
  char* channel_hdr;
  char* next_packet;
};

#define s_check(s) (s->p <= s->end)

#define s_check_rem(s, n) (s->p + n <= s->end)

#define s_check_end(s) (s->p == s->end)

#define make_stream(s) \
{ \
  s = (struct stream*)g_malloc(sizeof(struct stream), 1); \
} \

#define init_stream(s, v) \
{ \
  if (v > s->size) \
  { \
    g_free(s->data); \
    s->data = (char*)g_malloc(v, 0); \
  } \
  s->p = s->data; \
  s->end = s->data; \
  s->size = v; \
  s->next_packet = 0; \
}

#define free_stream(s) \
{ \
  if (s != 0) \
    g_free(s->data); \
  g_free(s); \
} \

#define s_push_layer(s, h, n) \
{ \
  s->h = s->p; \
  s->p += n; \
}

#define s_pop_layer(s, h) \
{ \
  s->p = s->h; \
}

#define s_mark_end(s) \
{ \
  s->end = s->p; \
}

#define in_uint8(s, v) \
{ \
  v = *((unsigned char*)(s->p)); \
  s->p++; \
}

#define in_sint16_le(s, v) \
{ \
  v = *((signed short*)(s->p)); \
  s->p += 2; \
}

#define in_uint16_le(s, v) \
{ \
  v = *((unsigned short*)(s->p)); \
  s->p += 2; \
}

#define in_uint16_be(s, v) \
{ \
  v = *((unsigned char*)(s->p)); \
  s->p++; \
  v = v << 8; \
  v = v | *((unsigned char*)(s->p)); \
  s->p++; \
}

#define in_uint32_le(s, v) \
{ \
  v = *(unsigned long*)(s->p); \
  s->p += 4; \
}

#define in_uint32_be(s, v) \
{ \
  v = *((unsigned char*)(s->p)); \
  s->p++; \
  v = v << 8; \
  v = v | *((unsigned char*)(s->p)); \
  s->p++; \
  v = v << 8; \
  v = v | *((unsigned char*)(s->p)); \
  s->p++; \
  v = v << 8; \
  v = v | *((unsigned char*)(s->p)); \
  s->p++; \
}

#define out_uint8(s, v) \
{ \
  *((unsigned char*)(s->p)) = (unsigned char)(v); \
  s->p++; \
}

#define out_uint16_le(s, v) \
{ \
  *((unsigned short*)(s->p)) = (unsigned short)(v); \
  s->p += 2; \
}

#define out_uint16_be(s, v) \
{ \
  *((unsigned char*)(s->p)) = (unsigned char)((v) >> 8); \
  s->p++; \
  *((unsigned char*)(s->p)) = (unsigned char)(v); \
  s->p++; \
}

#define out_uint32_le(s, v) \
{ \
  *((unsigned long*)(s->p)) = (v); \
  s->p += 4; \
}

#define out_uint32_be(s, v) \
{ \
  *((unsigned char*)(s->p)) = (unsigned char)((v) >> 24); \
  s->p++; \
  *((unsigned char*)(s->p)) = (unsigned char)((v) >> 16); \
  s->p++; \
  *((unsigned char*)(s->p)) = (unsigned char)((v) >> 8); \
  s->p++; \
  *((unsigned char*)(s->p)) = (unsigned char)(v); \
  s->p++; \
}

#define in_uint8p(s, v, n) \
{ \
  v = s->p; \
  s->p += n; \
}

#define in_uint8a(s, v, n) \
{ \
  g_memcpy(v, s->p, n); \
  s->p += n; \
}

#define in_uint8s(s, n) \
{ \
  s->p += n; \
}

#define out_uint8p(s, v, n) \
{ \
  g_memcpy(s->p, v, n); \
  s->p += n; \
}

#define out_uint8a(s, v, n) \
{ \
  out_uint8p(s, v, n); \
}

#define out_uint8s(s, n) \
{ \
  g_memset(s->p, 0, n); \
  s->p += n; \
}