summaryrefslogtreecommitdiffstats
path: root/common/parse.h
blob: 34170a41bda899d76715a24665a40fa754867f63 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/**
 * xrdp: A Remote Desktop Protocol server.
 *
 * Copyright (C) Jay Sorg 2004-2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Parsing structs and macros
 *
 * based on parse.h from rdesktop
 * this is a super fast stream method, you bet
 * needed functions g_malloc, g_free, g_memset, g_memcpy
 */

#if !defined(PARSE_H)
#define PARSE_H

#include "arch.h"

#if defined(L_ENDIAN)
#elif defined(B_ENDIAN)
#else
#error Unknown endianness.
#endif

/* 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_rem_out(s, n) ((s)->p + (n) <= (s)->data + (s)->size)

/******************************************************************************/
#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) do \
{ \
  if ((v) > (s)->size) \
  { \
    g_free((s)->data); \
    (s)->data = (char*)g_malloc((v), 0); \
    (s)->size = (v); \
  } \
  (s)->p = (s)->data; \
  (s)->end = (s)->data; \
  (s)->next_packet = 0; \
} while (0)

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

/******************************************************************************/
#define s_push_layer(s, h, n) do \
{ \
  (s)->h = (s)->p; \
  (s)->p += (n); \
} while (0)

/******************************************************************************/
#define s_pop_layer(s, h) \
  (s)->p = (s)->h

/******************************************************************************/
#define s_mark_end(s) \
  (s)->end = (s)->p

#define in_sint8(s, v) do \
{ \
  (v) = *((signed char*)((s)->p)); \
  (s)->p++; \
} while (0)

/******************************************************************************/
#define in_uint8(s, v) do \
{ \
  (v) = *((unsigned char*)((s)->p)); \
  (s)->p++; \
} while (0)
/******************************************************************************/
#define in_uint8_peek(s, v) do { v = *s->p; } while (0)
/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
#define in_sint16_le(s, v) do \
{ \
  (v) = (signed short) \
    ( \
      (*((unsigned char*)((s)->p + 0)) << 0) | \
      (*((unsigned char*)((s)->p + 1)) << 8) \
    ); \
  (s)->p += 2; \
} while (0)
#else
#define in_sint16_le(s, v) do \
{ \
  (v) = *((signed short*)((s)->p)); \
  (s)->p += 2; \
} while (0)
#endif

/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
#define in_uint16_le(s, v) do \
{ \
  (v) = (unsigned short) \
    ( \
      (*((unsigned char*)((s)->p + 0)) << 0) | \
      (*((unsigned char*)((s)->p + 1)) << 8) \
    ); \
  (s)->p += 2; \
} while (0)
#else
#define in_uint16_le(s, v) do \
{ \
  (v) = *((unsigned short*)((s)->p)); \
  (s)->p += 2; \
} while (0)
#endif

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

/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
#define in_uint32_le(s, v) do \
{ \
  (v) = (unsigned int) \
    ( \
      (*((unsigned char*)((s)->p + 0)) << 0) | \
      (*((unsigned char*)((s)->p + 1)) << 8) | \
      (*((unsigned char*)((s)->p + 2)) << 16) | \
      (*((unsigned char*)((s)->p + 3)) << 24) \
    ); \
  (s)->p += 4; \
} while (0)
#else
#define in_uint32_le(s, v) do \
{ \
  (v) = *((unsigned int*)((s)->p)); \
  (s)->p += 4; \
} while (0)
#endif

/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
#define in_uint64_le(s, v) do \
{ \
  (v) = (tui64) \
    ( \
      (((tui64)(*((unsigned char*)((s)->p + 0)))) << 0) | \
      (((tui64)(*((unsigned char*)((s)->p + 1)))) << 8) | \
      (((tui64)(*((unsigned char*)((s)->p + 2)))) << 16) | \
      (((tui64)(*((unsigned char*)((s)->p + 3)))) << 24) | \
      (((tui64)(*((unsigned char*)((s)->p + 4)))) << 32) | \
      (((tui64)(*((unsigned char*)((s)->p + 5)))) << 40) | \
      (((tui64)(*((unsigned char*)((s)->p + 6)))) << 48) | \
      (((tui64)(*((unsigned char*)((s)->p + 7)))) << 56) \
    ); \
  (s)->p += 8; \
} while (0)
#else
#define in_uint64_le(s, v) do \
{ \
  (v) = *((tui64*)((s)->p)); \
  (s)->p += 8; \
} while (0)
#endif

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

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

/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
#define out_uint16_le(s, v) do \
{ \
  *((s)->p) = (unsigned char)((v) >> 0); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 8); \
  (s)->p++; \
} while (0)
#else
#define out_uint16_le(s, v) do \
{ \
  *((unsigned short*)((s)->p)) = (unsigned short)(v); \
  (s)->p += 2; \
} while (0)
#endif

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

/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
#define out_uint32_le(s, v) do \
{ \
  *((s)->p) = (unsigned char)((v) >> 0); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 8); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 16); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 24); \
  (s)->p++; \
} while (0)
#else
#define out_uint32_le(s, v) do \
{ \
  *((unsigned int*)((s)->p)) = (v); \
  (s)->p += 4; \
} while (0)
#endif

/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
#define out_uint64_le(s, v) do \
{ \
  *((s)->p) = (unsigned char)((v) >> 0); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 8); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 16); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 24); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 32); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 40); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 48); \
  (s)->p++; \
  *((s)->p) = (unsigned char)((v) >> 56); \
  (s)->p++; \
} while (0)
#else
#define out_uint64_le(s, v) do \
{ \
  *((tui64*)((s)->p)) = (v); \
  (s)->p += 8; \
} while (0)
#endif

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

/******************************************************************************/
#define in_uint8p(s, v, n) do \
{ \
  (v) = (s)->p; \
  (s)->p += (n); \
} while (0)

/******************************************************************************/
#define in_uint8a(s, v, n) do \
{ \
  g_memcpy((v), (s)->p, (n)); \
  (s)->p += (n); \
} while (0)

/******************************************************************************/
#define in_uint8s(s, n) \
  (s)->p += (n)

/******************************************************************************/
#define out_uint8p(s, v, n) do \
{ \
  g_memcpy((s)->p, (v), (n)); \
  (s)->p += (n); \
} while (0)

/******************************************************************************/
#define out_uint8a(s, v, n) \
  out_uint8p((s), (v), (n))

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

/*
 * @brief allocate a new stream
 *
 * @param _s opaque handle to the new stream
 * @param _l length of new stream
 ******************************************************************************/
#define xstream_new(_s, _l)   \
do                           \
{                            \
    make_stream((_s));       \
    init_stream((_s), (_l)); \
} while (0)

/**
 * @brief release a previously allocated stream
 *
 * @param _s opaque handle returned by stream_new()
 *****************************************************************************/
#define xstream_free(_s) free_stream(_s)

#define xstream_rd_u8(_s, _var)       in_uint8(_s, _var)
#define xstream_rd_u16_le(_s, _var)   in_uint16_le(_s, _var)
#define xstream_rd_u32_le(_s, _var)   in_uint32_le(_s, _var)

#define xstream_rd_s8_le(_s, _var)    in_sint8(_s, _var)
#define xstream_rd_s16_le(_s, _var)   in_sint16_le(_s, _var)
#define xstream_rd_s32_le(_s, _var)   TODO

#define xstream_wr_u8(_s, _var)       out_uint8(_s, _var)
#define xstream_wr_u16_le(_s, _var)   out_uint16_le(_s, _var)
#define xstream_wr_u32_le(_s, _var)   out_uint32_le(_s, _var)

#define xstream_wr_s8(_s, _var)       TODO
#define xstream_wr_s16_le(_s, _var)   TODO
#define xstream_wr_s32_le(_s, _var)   TODO

#define xstream_rd_u64_le(_s, _v)                             \
do                                                            \
{                                                             \
    _v =                                                      \
    (tui64)(*((unsigned char *)_s->p)) |                      \
    (((tui64) (*(((unsigned char *)_s->p) + 1))) << 8)  |     \
    (((tui64) (*(((unsigned char *)_s->p) + 2))) << 16) |     \
    (((tui64) (*(((unsigned char *)_s->p) + 3))) << 24) |     \
    (((tui64) (*(((unsigned char *)_s->p) + 4))) << 32) |     \
    (((tui64) (*(((unsigned char *)_s->p) + 5))) << 40) |     \
    (((tui64) (*(((unsigned char *)_s->p) + 6))) << 48) |     \
    (((tui64) (*(((unsigned char *)_s->p) + 7))) << 56);      \
    _s->p += 8;                                               \
} while (0)

#define xstream_wr_u64_le(_s, _v)                                           \
do                                                                          \
{                                                                           \
    *(((unsigned char *) _s->p) + 0) = (unsigned char) ((_v >>  0) & 0xff); \
    *(((unsigned char *) _s->p) + 1) = (unsigned char) ((_v >>  8) & 0xff); \
    *(((unsigned char *) _s->p) + 2) = (unsigned char) ((_v >> 16) & 0xff); \
    *(((unsigned char *) _s->p) + 3) = (unsigned char) ((_v >> 24) & 0xff); \
    *(((unsigned char *) _s->p) + 4) = (unsigned char) ((_v >> 32) & 0xff); \
    *(((unsigned char *) _s->p) + 5) = (unsigned char) ((_v >> 40) & 0xff); \
    *(((unsigned char *) _s->p) + 6) = (unsigned char) ((_v >> 48) & 0xff); \
    *(((unsigned char *) _s->p) + 7) = (unsigned char) ((_v >> 56) & 0xff); \
    _s->p += 8;                                                             \
} while (0)

/* copy data into stream */
#define xstream_copyin(_s, _dest, _len)   \
do                                        \
{                                         \
    g_memcpy((_s)->p, (_dest), (_len));   \
    (_s)->p += (_len);                    \
} while (0)

/* copy data out of stream */
#define xstream_copyout(_dest, _s, _len)  \
do                                        \
{                                         \
    g_memcpy((_dest), (_s)->p, (_len));   \
    (_s)->p += (_len);                    \
} while (0)

#define xstream_rd_string(_dest, _s, _len) \
do                                         \
{                                          \
    g_memcpy((_dest), (_s)->p, (_len));    \
    (_s)->p += (_len);                     \
} while (0)

#define xstream_wr_string(_s, _src, _len) \
do                                        \
{                                         \
    g_memcpy((_s)->p, (_src), (_len));    \
    (_s)->p += (_len);                    \
} while (0)

#define xstream_len(_s)               (int) ((_s)->p - (_s)->data)
#define xstream_seek(_s, _len)        (_s)->p += (_len)

#endif