xrdp/common/parse.h

311 lines
8.1 KiB
C
Raw Normal View History

2005-01-07 03:56:38 +03:00
/*
2010-08-10 03:02:06 +04:00
Copyright (c) 2004-2010 Jay Sorg
2005-01-07 03:56:38 +03:00
2006-10-12 07:40:58 +04:00
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
2006-03-21 05:05:38 +03:00
2006-10-12 07:40:58 +04:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Parsing structs and macros
based on parse.h from rdesktop
this is a super fast stream method, you bet
2006-03-21 05:05:38 +03:00
needed functions g_malloc, g_free, g_memset, g_memcpy
*/
2005-01-07 03:56:38 +03:00
2005-06-28 07:04:36 +04:00
#if !defined(PARSE_H)
#define PARSE_H
2005-06-30 05:01:19 +04:00
#include "arch.h"
#if defined(L_ENDIAN)
#elif defined(B_ENDIAN)
2005-01-07 03:56:38 +03:00
#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;
};
/******************************************************************************/
2005-03-11 05:23:30 +03:00
#define s_check(s) ((s)->p <= (s)->end)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2005-03-11 05:23:30 +03:00
#define s_check_rem(s, n) ((s)->p + (n) <= (s)->end)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2005-03-11 05:23:30 +03:00
#define s_check_end(s) ((s)->p == (s)->end)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
#define make_stream(s) \
2010-08-10 03:02:06 +04:00
(s) = (struct stream*)g_malloc(sizeof(struct stream), 1)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define init_stream(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
if ((v) > (s)->size) \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
g_free((s)->data); \
(s)->data = (char*)g_malloc((v), 0); \
(s)->size = (v); \
2005-01-07 03:56:38 +03:00
} \
2005-03-11 05:23:30 +03:00
(s)->p = (s)->data; \
(s)->end = (s)->data; \
(s)->next_packet = 0; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define free_stream(s) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
if ((s) != 0) \
2005-02-20 09:06:26 +03:00
{ \
2005-03-11 05:23:30 +03:00
g_free((s)->data); \
2005-02-20 09:06:26 +03:00
} \
2005-03-11 05:23:30 +03:00
g_free((s)); \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define s_push_layer(s, h, n) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(s)->h = (s)->p; \
(s)->p += (n); \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
#define s_pop_layer(s, h) \
2010-08-10 03:02:06 +04:00
(s)->p = (s)->h
2005-01-07 03:56:38 +03:00
/******************************************************************************/
#define s_mark_end(s) \
2010-08-10 03:02:06 +04:00
(s)->end = (s)->p
2005-01-07 03:56:38 +03:00
2006-10-12 07:49:15 +04:00
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define in_sint8(s, v) do \
2006-10-12 07:49:15 +04:00
{ \
(v) = *((signed char*)((s)->p)); \
(s)->p++; \
2010-08-10 03:02:06 +04:00
} while (0)
2006-10-12 07:49:15 +04:00
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define in_uint8(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(v) = *((unsigned char*)((s)->p)); \
(s)->p++; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
2010-08-10 03:02:06 +04:00
#define in_sint16_le(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(v) = (signed short) \
2005-01-07 03:56:38 +03:00
( \
2005-03-11 05:23:30 +03:00
(*((unsigned char*)((s)->p + 0)) << 0) | \
(*((unsigned char*)((s)->p + 1)) << 8) \
2005-01-07 03:56:38 +03:00
); \
2005-03-11 05:23:30 +03:00
(s)->p += 2; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
#else
2010-08-10 03:02:06 +04:00
#define in_sint16_le(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(v) = *((signed short*)((s)->p)); \
(s)->p += 2; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
#endif
/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
2010-08-10 03:02:06 +04:00
#define in_uint16_le(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(v) = (unsigned short) \
2005-01-07 03:56:38 +03:00
( \
2005-03-11 05:23:30 +03:00
(*((unsigned char*)((s)->p + 0)) << 0) | \
(*((unsigned char*)((s)->p + 1)) << 8) \
2005-01-07 03:56:38 +03:00
); \
2005-03-11 05:23:30 +03:00
(s)->p += 2; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
#else
2010-08-10 03:02:06 +04:00
#define in_uint16_le(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(v) = *((unsigned short*)((s)->p)); \
(s)->p += 2; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
#endif
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define in_uint16_be(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(v) = *((unsigned char*)((s)->p)); \
(s)->p++; \
(v) <<= 8; \
(v) |= *((unsigned char*)((s)->p)); \
(s)->p++; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
2010-08-10 03:02:06 +04:00
#define in_uint32_le(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(v) = (unsigned int) \
2005-01-07 03:56:38 +03:00
( \
2005-03-11 05:23:30 +03:00
(*((unsigned char*)((s)->p + 0)) << 0) | \
(*((unsigned char*)((s)->p + 1)) << 8) | \
(*((unsigned char*)((s)->p + 2)) << 16) | \
(*((unsigned char*)((s)->p + 3)) << 24) \
2005-01-07 03:56:38 +03:00
); \
2005-03-11 05:23:30 +03:00
(s)->p += 4; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
#else
2010-08-10 03:02:06 +04:00
#define in_uint32_le(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(v) = *((unsigned int*)((s)->p)); \
(s)->p += 4; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
#endif
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define in_uint32_be(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(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++; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define out_uint8(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
*((s)->p) = (unsigned char)(v); \
(s)->p++; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
2010-08-10 03:02:06 +04:00
#define out_uint16_le(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
*((s)->p) = (unsigned char)((v) >> 0); \
(s)->p++; \
*((s)->p) = (unsigned char)((v) >> 8); \
(s)->p++; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
#else
2010-08-10 03:02:06 +04:00
#define out_uint16_le(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
*((unsigned short*)((s)->p)) = (unsigned short)(v); \
(s)->p += 2; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
#endif
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define out_uint16_be(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
*((s)->p) = (unsigned char)((v) >> 8); \
(s)->p++; \
*((s)->p) = (unsigned char)((v) >> 0); \
(s)->p++; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
2010-08-10 03:02:06 +04:00
#define out_uint32_le(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
*((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++; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
#else
2010-08-10 03:02:06 +04:00
#define out_uint32_le(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
*((unsigned int*)((s)->p)) = (v); \
(s)->p += 4; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
#endif
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define out_uint32_be(s, v) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
*((s)->p) = (unsigned char)((v) >> 24); \
2005-01-07 03:56:38 +03:00
s->p++; \
2005-03-11 05:23:30 +03:00
*((s)->p) = (unsigned char)((v) >> 16); \
2005-01-07 03:56:38 +03:00
s->p++; \
2005-03-11 05:23:30 +03:00
*((s)->p) = (unsigned char)((v) >> 8); \
2005-01-07 03:56:38 +03:00
s->p++; \
2005-03-11 05:23:30 +03:00
*((s)->p) = (unsigned char)(v); \
(s)->p++; \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define in_uint8p(s, v, n) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
(v) = (s)->p; \
(s)->p += (n); \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define in_uint8a(s, v, n) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
g_memcpy((v), (s)->p, (n)); \
(s)->p += (n); \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
#define in_uint8s(s, n) \
2010-08-10 03:02:06 +04:00
(s)->p += (n)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define out_uint8p(s, v, n) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
g_memcpy((s)->p, (v), (n)); \
(s)->p += (n); \
2010-08-10 03:02:06 +04:00
} while (0)
2005-01-07 03:56:38 +03:00
/******************************************************************************/
#define out_uint8a(s, v, n) \
2010-08-10 03:02:06 +04:00
out_uint8p((s), (v), (n))
2005-01-07 03:56:38 +03:00
/******************************************************************************/
2010-08-10 03:02:06 +04:00
#define out_uint8s(s, n) do \
2005-01-07 03:56:38 +03:00
{ \
2005-03-11 05:23:30 +03:00
g_memset((s)->p, 0, (n)); \
(s)->p += (n); \
2010-08-10 03:02:06 +04:00
} while (0)
2005-06-28 07:04:36 +04:00
#endif