2012-09-20 07:51:34 +04:00
|
|
|
/**
|
|
|
|
* xrdp: A Remote Desktop Protocol server.
|
|
|
|
*
|
2014-03-02 11:26:40 +04:00
|
|
|
* Copyright (C) Jay Sorg 2004-2014
|
2012-09-20 07:51:34 +04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
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"
|
2021-02-04 13:11:54 +03:00
|
|
|
#include "log.h"
|
2005-06-30 05:01:19 +04:00
|
|
|
|
2021-05-19 13:34:41 +03:00
|
|
|
/* Check the config_ac.h file is included so we know whether to enable the
|
|
|
|
* development macros
|
|
|
|
*/
|
|
|
|
#ifndef CONFIG_AC_H
|
|
|
|
# error config_ac.h not visible in parse.h
|
|
|
|
#endif
|
|
|
|
|
2005-03-01 04:14:37 +03:00
|
|
|
#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
|
|
|
|
{
|
2014-07-26 08:18:40 +04:00
|
|
|
char *p;
|
|
|
|
char *end;
|
|
|
|
char *data;
|
|
|
|
int size;
|
2015-07-06 09:14:46 +03:00
|
|
|
int pad0;
|
2014-07-26 08:18:40 +04:00
|
|
|
/* offsets of various headers */
|
|
|
|
char *iso_hdr;
|
|
|
|
char *mcs_hdr;
|
|
|
|
char *sec_hdr;
|
|
|
|
char *rdp_hdr;
|
|
|
|
char *channel_hdr;
|
2015-07-06 09:14:46 +03:00
|
|
|
/* other */
|
2014-07-26 08:18:40 +04:00
|
|
|
char *next_packet;
|
2015-07-06 09:14:46 +03:00
|
|
|
struct stream *next;
|
|
|
|
int *source;
|
2005-01-07 03:56:38 +03:00
|
|
|
};
|
|
|
|
|
2021-05-19 13:34:41 +03:00
|
|
|
/** Check arguments to stream primitives
|
|
|
|
*
|
|
|
|
* This adds a function call overhead to every stream primitive and is
|
|
|
|
* intended for development only
|
|
|
|
*
|
|
|
|
* @param s stream
|
|
|
|
* @param n Bytes being requested for input/output
|
|
|
|
* @param is_out (0=input, !0=output)
|
|
|
|
* @param file __file__for caller
|
|
|
|
* @param line __line__ for caller
|
|
|
|
*
|
|
|
|
* On any kind of violation a message is output and the program is
|
|
|
|
* aborted.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
parser_stream_overflow_check(const struct stream *s, int n, int is_out,
|
|
|
|
const char *file, int line);
|
|
|
|
|
2021-06-02 17:52:49 +03:00
|
|
|
#ifdef USE_DEVEL_STREAMCHECK
|
2021-05-19 13:34:41 +03:00
|
|
|
# define S_CHECK_REM(s,n) \
|
|
|
|
parser_stream_overflow_check((s), (n), 0, __FILE__, __LINE__)
|
|
|
|
# define S_CHECK_REM_OUT(s,n) \
|
|
|
|
parser_stream_overflow_check((s), (n), 1, __FILE__, __LINE__)
|
|
|
|
#else
|
|
|
|
# define S_CHECK_REM(s,n)
|
|
|
|
# define S_CHECK_REM_OUT(s,n)
|
|
|
|
#endif
|
|
|
|
|
2023-09-19 13:36:13 +03:00
|
|
|
/******************************************************************************/
|
|
|
|
/**
|
|
|
|
* Copies a UTF-8 string to a stream as little-endian UTF-16
|
|
|
|
*
|
|
|
|
* @param s Stream
|
|
|
|
* @param v UTF-8 string
|
|
|
|
* @param vn Length of UTF-8 string.
|
|
|
|
* @param file Caller location (from __FILE__)
|
|
|
|
* @param line Caller location (from __LINE__)
|
|
|
|
*
|
|
|
|
* Caller is expected to check there is room for the result in s
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
out_utf8_as_utf16_le_proc(struct stream *s, const char *v,
|
|
|
|
unsigned int vn,
|
|
|
|
const char *file, int line);
|
|
|
|
|
|
|
|
#define out_utf8_as_utf16_le(s,v,vn) \
|
|
|
|
out_utf8_as_utf16_le_proc((s), (v), (vn), __FILE__, __LINE__)
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/**
|
|
|
|
* Copies a fixed-size little-endian UTF-16 string from a stream as UTF-8
|
|
|
|
*
|
|
|
|
* @param s Stream
|
|
|
|
* @param n Number of 16-bit words to copy
|
|
|
|
* @param v Pointer to result buffer
|
|
|
|
* @param vn Max size of result buffer
|
|
|
|
*
|
|
|
|
* @return number of characters which would be written to v, INCLUDING
|
|
|
|
* an additional terminator. This can be used to check for a buffer
|
|
|
|
* overflow. A terminator is added whether or not the input
|
|
|
|
* includes one.
|
|
|
|
*
|
|
|
|
* Output is unconditionally NULL-terminated.
|
|
|
|
* Input is not checked for NULLs - these are copied verbatim
|
|
|
|
*/
|
|
|
|
unsigned int
|
|
|
|
in_utf16_le_fixed_as_utf8_proc(struct stream *s, unsigned int n,
|
|
|
|
char *v, unsigned int vn,
|
|
|
|
const char *file, int line);
|
|
|
|
|
|
|
|
#define in_utf16_le_fixed_as_utf8(s,n,v,vn) \
|
|
|
|
in_utf16_le_fixed_as_utf8_proc((s), (n), (v), (vn), __FILE__, __LINE__)
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/**
|
|
|
|
* Returns the size of the buffer needed to store a fixed-size
|
|
|
|
* little-endian UTF-16 string in a stream as a UTF-8 string
|
|
|
|
*
|
|
|
|
* @param s Stream
|
|
|
|
* @param n Number of 16-bit words to consider
|
|
|
|
* @return number of characters needed to store the UTF-8 string. This
|
|
|
|
* includes a terminator, which is written whether the parsed
|
|
|
|
* string includes one or not.
|
|
|
|
* @post Stream position is not moved between start and end of this call
|
|
|
|
*/
|
|
|
|
unsigned int
|
|
|
|
in_utf16_le_fixed_as_utf8_length(struct stream *s, unsigned int n);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/**
|
|
|
|
* Copies a terminated little-endian UTF-16 string from a stream as UTF-8
|
|
|
|
*
|
|
|
|
* @param s Stream
|
|
|
|
* @param v Pointer to result buffer
|
|
|
|
* @param vn Max size of result buffer
|
|
|
|
*
|
|
|
|
* @return number of characters which would be written to v, INCLUDING
|
|
|
|
* the terminator. This can be used to check for a buffer overflow.
|
|
|
|
*
|
|
|
|
* Output is unconditionally NULL-terminated.
|
|
|
|
* Input processing stops when a NULL is encountered, or the end of the buffer
|
|
|
|
* is reached.
|
|
|
|
*/
|
|
|
|
unsigned int
|
|
|
|
in_utf16_le_terminated_as_utf8(struct stream *s,
|
|
|
|
char *v, unsigned int vn);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/**
|
|
|
|
* Returns the size of the buffer needed to store a terminated
|
|
|
|
* little-endian UTF-16 string in a stream as a terminated UTF-8 string
|
|
|
|
*
|
|
|
|
* @param s Stream
|
|
|
|
* @return number of characters needed to store the UTF-8 string,
|
|
|
|
* including the terminator
|
|
|
|
* @post Stream position is not moved between start and end of this call
|
|
|
|
*
|
|
|
|
* Input processing stops when a NULL is encountered, or the end of the buffer
|
|
|
|
* is reached.
|
|
|
|
*/
|
|
|
|
unsigned int
|
|
|
|
in_utf16_le_terminated_as_utf8_length(struct stream *s);
|
|
|
|
|
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
|
|
|
|
2021-02-04 13:11:54 +03:00
|
|
|
/******************************************************************************/
|
|
|
|
/**
|
2021-03-06 18:45:27 +03:00
|
|
|
* @returns true if there are at least n bytes remaining in the stream,
|
2021-02-04 13:11:54 +03:00
|
|
|
* else false and logs an error message
|
|
|
|
*/
|
|
|
|
#define s_check_rem_and_log(s, n, msg_prefix) \
|
|
|
|
( s_check_rem((s), (n)) ? \
|
|
|
|
1 : \
|
|
|
|
LOG(LOG_LEVEL_ERROR, \
|
|
|
|
"%s Not enough bytes in the stream: expected %d, remaining %d", \
|
|
|
|
(msg_prefix), (n), s_rem(s)) \
|
2021-03-06 18:45:27 +03:00
|
|
|
&& 0 )
|
2021-02-04 13:11:54 +03:00
|
|
|
|
2013-09-11 03:00:30 +04:00
|
|
|
/******************************************************************************/
|
|
|
|
#define s_check_rem_out(s, n) ((s)->p + (n) <= (s)->data + (s)->size)
|
|
|
|
|
2021-02-04 13:11:54 +03:00
|
|
|
/******************************************************************************/
|
|
|
|
/**
|
2021-03-06 18:45:27 +03:00
|
|
|
* @returns true if there are at least n bytes remaining in the stream,
|
2021-02-04 13:11:54 +03:00
|
|
|
* else false and logs an error message
|
|
|
|
*/
|
|
|
|
#define s_check_rem_out_and_log(s, n, msg_prefix) \
|
|
|
|
( s_check_rem_out((s), (n)) ? \
|
|
|
|
1 : \
|
|
|
|
LOG(LOG_LEVEL_ERROR, \
|
|
|
|
"%s Not enough bytes in the stream: expected %d, remaining %d", \
|
|
|
|
(msg_prefix), (n), s_rem_out(s)) \
|
2021-03-06 18:45:27 +03:00
|
|
|
&& 0 )
|
|
|
|
|
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
|
|
|
|
2021-02-04 13:11:54 +03:00
|
|
|
/******************************************************************************/
|
|
|
|
/**
|
2021-03-06 18:45:27 +03:00
|
|
|
* @returns true if there are exactly 0 bytes remaining in the stream,
|
2021-02-04 13:11:54 +03:00
|
|
|
* else false and logs an error message
|
|
|
|
*/
|
|
|
|
#define s_check_end_and_log(s, msg_prefix) \
|
|
|
|
( s_check_end((s)) ? \
|
|
|
|
1 : \
|
|
|
|
LOG(LOG_LEVEL_ERROR, \
|
|
|
|
"%s Expected to be at the end of the stream, " \
|
|
|
|
"but there are %d bytes remaining", \
|
|
|
|
(msg_prefix), s_rem(s)) \
|
2021-03-06 18:45:27 +03:00
|
|
|
&& 0 )
|
2021-02-04 13:11:54 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
#define s_rem(s) ((int) ((s)->end - (s)->p))
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
#define s_rem_out(s) ((int) ((s)->data + (s)->size - (s)->p))
|
|
|
|
|
2005-01-07 03:56:38 +03:00
|
|
|
/******************************************************************************/
|
|
|
|
#define make_stream(s) \
|
2014-07-26 08:18:40 +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 \
|
2014-07-26 08:18:40 +04:00
|
|
|
{ \
|
2021-03-06 18:45:27 +03:00
|
|
|
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)
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2010-08-10 03:02:06 +04:00
|
|
|
#define free_stream(s) do \
|
2014-07-26 08:18:40 +04:00
|
|
|
{ \
|
2021-03-06 18:45:27 +03:00
|
|
|
if ((s) != 0) \
|
|
|
|
{ \
|
|
|
|
g_free((s)->data); \
|
|
|
|
} \
|
|
|
|
g_free((s)); \
|
|
|
|
} 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 \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
|
|
|
(s)->h = (s)->p; \
|
|
|
|
(s)->p += (n); \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
#define s_pop_layer(s, h) \
|
2014-07-26 08:18:40 +04:00
|
|
|
(s)->p = (s)->h
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
#define s_mark_end(s) \
|
2014-07-26 08:18:40 +04:00
|
|
|
(s)->end = (s)->p
|
2005-01-07 03:56:38 +03:00
|
|
|
|
2010-08-10 03:02:06 +04:00
|
|
|
#define in_sint8(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 1); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(v) = *((signed char*)((s)->p)); \
|
|
|
|
(s)->p++; \
|
|
|
|
} 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 \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 1); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(v) = *((unsigned char*)((s)->p)); \
|
|
|
|
(s)->p++; \
|
|
|
|
} while (0)
|
2014-02-08 15:34:01 +04:00
|
|
|
/******************************************************************************/
|
2021-05-19 13:34:41 +03:00
|
|
|
#define in_uint8_peek(s, v) do \
|
|
|
|
{ \
|
|
|
|
S_CHECK_REM((s), 1); \
|
|
|
|
v = *s->p; \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
/******************************************************************************/
|
2005-03-01 04:14:37 +03:00
|
|
|
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
|
2010-08-10 03:02:06 +04:00
|
|
|
#define in_sint16_le(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 2); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(v) = (signed short) \
|
|
|
|
( \
|
|
|
|
(*((unsigned char*)((s)->p + 0)) << 0) | \
|
|
|
|
(*((unsigned char*)((s)->p + 1)) << 8) \
|
|
|
|
); \
|
|
|
|
(s)->p += 2; \
|
|
|
|
} 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 \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 2); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(v) = *((signed short*)((s)->p)); \
|
|
|
|
(s)->p += 2; \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2005-03-01 04:14:37 +03:00
|
|
|
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
|
2010-08-10 03:02:06 +04:00
|
|
|
#define in_uint16_le(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 2); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(v) = (unsigned short) \
|
|
|
|
( \
|
|
|
|
(*((unsigned char*)((s)->p + 0)) << 0) | \
|
|
|
|
(*((unsigned char*)((s)->p + 1)) << 8) \
|
|
|
|
); \
|
|
|
|
(s)->p += 2; \
|
|
|
|
} 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 \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 2); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(v) = *((unsigned short*)((s)->p)); \
|
|
|
|
(s)->p += 2; \
|
|
|
|
} 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 \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 2); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(v) = *((unsigned char*)((s)->p)); \
|
|
|
|
(s)->p++; \
|
|
|
|
(v) <<= 8; \
|
|
|
|
(v) |= *((unsigned char*)((s)->p)); \
|
|
|
|
(s)->p++; \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2005-03-01 04:14:37 +03:00
|
|
|
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
|
2010-08-10 03:02:06 +04:00
|
|
|
#define in_uint32_le(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 4); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(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)
|
2005-01-07 03:56:38 +03:00
|
|
|
#else
|
2010-08-10 03:02:06 +04:00
|
|
|
#define in_uint32_le(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 4); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(v) = *((unsigned int*)((s)->p)); \
|
|
|
|
(s)->p += 4; \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
#endif
|
|
|
|
|
2013-08-06 07:14:20 +04:00
|
|
|
/******************************************************************************/
|
|
|
|
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
|
|
|
|
#define in_uint64_le(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 8); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(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)
|
2013-08-06 07:14:20 +04:00
|
|
|
#else
|
|
|
|
#define in_uint64_le(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 8); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(v) = *((tui64*)((s)->p)); \
|
|
|
|
(s)->p += 8; \
|
|
|
|
} while (0)
|
2013-08-06 07:14:20 +04:00
|
|
|
#endif
|
|
|
|
|
2005-01-07 03:56:38 +03:00
|
|
|
/******************************************************************************/
|
2010-08-10 03:02:06 +04:00
|
|
|
#define in_uint32_be(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), 4); \
|
2021-03-06 18:45:27 +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++; \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2010-08-10 03:02:06 +04:00
|
|
|
#define out_uint8(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), 1); \
|
2021-03-06 18:45:27 +03:00
|
|
|
*((s)->p) = (unsigned char)(v); \
|
|
|
|
(s)->p++; \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2005-03-01 04:14:37 +03:00
|
|
|
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
|
2010-08-10 03:02:06 +04:00
|
|
|
#define out_uint16_le(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), 2); \
|
2021-03-06 18:45:27 +03:00
|
|
|
*((s)->p) = (unsigned char)((v) >> 0); \
|
|
|
|
(s)->p++; \
|
|
|
|
*((s)->p) = (unsigned char)((v) >> 8); \
|
|
|
|
(s)->p++; \
|
|
|
|
} 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 \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), 2); \
|
2021-03-06 18:45:27 +03:00
|
|
|
*((unsigned short*)((s)->p)) = (unsigned short)(v); \
|
|
|
|
(s)->p += 2; \
|
|
|
|
} 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 \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), 2); \
|
2021-03-06 18:45:27 +03:00
|
|
|
*((s)->p) = (unsigned char)((v) >> 8); \
|
|
|
|
(s)->p++; \
|
|
|
|
*((s)->p) = (unsigned char)((v) >> 0); \
|
|
|
|
(s)->p++; \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2005-03-01 04:14:37 +03:00
|
|
|
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
|
2010-08-10 03:02:06 +04:00
|
|
|
#define out_uint32_le(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), 4); \
|
2021-03-06 18:45:27 +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++; \
|
|
|
|
} 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 \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), 4); \
|
2021-03-06 18:45:27 +03:00
|
|
|
*((unsigned int*)((s)->p)) = (v); \
|
|
|
|
(s)->p += 4; \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
#endif
|
|
|
|
|
2013-08-19 21:47:26 +04:00
|
|
|
/******************************************************************************/
|
|
|
|
#if defined(B_ENDIAN) || defined(NEED_ALIGN)
|
|
|
|
#define out_uint64_le(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), 8); \
|
2021-03-06 18:45:27 +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++; \
|
|
|
|
*((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)
|
2013-08-19 21:47:26 +04:00
|
|
|
#else
|
|
|
|
#define out_uint64_le(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), 8); \
|
2021-03-06 18:45:27 +03:00
|
|
|
*((tui64*)((s)->p)) = (v); \
|
|
|
|
(s)->p += 8; \
|
|
|
|
} while (0)
|
2013-08-19 21:47:26 +04:00
|
|
|
#endif
|
|
|
|
|
2005-01-07 03:56:38 +03:00
|
|
|
/******************************************************************************/
|
2010-08-10 03:02:06 +04:00
|
|
|
#define out_uint32_be(s, v) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), 4); \
|
2021-03-06 18:45:27 +03:00
|
|
|
*((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)
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2010-08-10 03:02:06 +04:00
|
|
|
#define in_uint8p(s, v, n) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), (n)); \
|
2021-03-06 18:45:27 +03:00
|
|
|
(v) = (s)->p; \
|
|
|
|
(s)->p += (n); \
|
|
|
|
} 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 \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM((s), (n)); \
|
2021-03-06 18:45:27 +03:00
|
|
|
g_memcpy((v), (s)->p, (n)); \
|
|
|
|
(s)->p += (n); \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2021-05-19 13:34:41 +03:00
|
|
|
#define in_uint8s(s, n) do \
|
|
|
|
{ \
|
|
|
|
S_CHECK_REM((s), (n)); \
|
|
|
|
(s)->p += (n); \
|
|
|
|
} while (0);
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2010-08-10 03:02:06 +04:00
|
|
|
#define out_uint8p(s, v, n) do \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), (n)); \
|
2021-03-06 18:45:27 +03:00
|
|
|
g_memcpy((s)->p, (v), (n)); \
|
|
|
|
(s)->p += (n); \
|
|
|
|
} while (0)
|
2005-01-07 03:56:38 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
#define out_uint8a(s, v, n) \
|
2014-07-26 08:18:40 +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 \
|
2021-03-06 18:45:27 +03:00
|
|
|
{ \
|
2021-05-19 13:34:41 +03:00
|
|
|
S_CHECK_REM_OUT((s), (n)); \
|
2021-03-06 18:45:27 +03:00
|
|
|
g_memset((s)->p, 0, (n)); \
|
|
|
|
(s)->p += (n); \
|
|
|
|
} while (0)
|
2005-06-28 07:04:36 +04:00
|
|
|
|
2013-03-09 03:37:30 +04:00
|
|
|
/*
|
|
|
|
* @brief allocate a new stream
|
|
|
|
*
|
|
|
|
* @param _s opaque handle to the new stream
|
|
|
|
* @param _l length of new stream
|
|
|
|
******************************************************************************/
|
2013-05-03 13:07:02 +04:00
|
|
|
#define xstream_new(_s, _l) \
|
2021-03-06 18:45:27 +03:00
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
make_stream((_s)); \
|
|
|
|
init_stream((_s), (_l)); \
|
|
|
|
} while (0)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief release a previously allocated stream
|
|
|
|
*
|
|
|
|
* @param _s opaque handle returned by stream_new()
|
|
|
|
*****************************************************************************/
|
2013-05-03 13:07:02 +04:00
|
|
|
#define xstream_free(_s) free_stream(_s)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
2021-09-02 19:08:53 +03:00
|
|
|
#define xstream_skip_u8(_s, _n) in_uint8s(_s, _n)
|
|
|
|
|
2013-05-03 13:07:02 +04:00
|
|
|
#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)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
2013-05-03 13:07:02 +04:00
|
|
|
#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
|
2013-03-09 03:37:30 +04:00
|
|
|
|
2013-05-03 13:07:02 +04:00
|
|
|
#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)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
2013-05-03 13:07:02 +04:00
|
|
|
#define xstream_wr_s8(_s, _var) TODO
|
|
|
|
#define xstream_wr_s16_le(_s, _var) TODO
|
|
|
|
#define xstream_wr_s32_le(_s, _var) TODO
|
2013-03-09 03:37:30 +04:00
|
|
|
|
2013-07-19 22:10:55 +04:00
|
|
|
#define xstream_rd_u64_le(_s, _v) \
|
2021-03-06 18:45:27 +03:00
|
|
|
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)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
2013-07-19 22:10:55 +04:00
|
|
|
#define xstream_wr_u64_le(_s, _v) \
|
2021-03-06 18:45:27 +03:00
|
|
|
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)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
|
|
|
/* copy data into stream */
|
2013-05-16 04:06:05 +04:00
|
|
|
#define xstream_copyin(_s, _dest, _len) \
|
2021-03-06 18:45:27 +03:00
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
g_memcpy((_s)->p, (_dest), (_len)); \
|
|
|
|
(_s)->p += (_len); \
|
|
|
|
} while (0)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
|
|
|
/* copy data out of stream */
|
2013-05-16 04:06:05 +04:00
|
|
|
#define xstream_copyout(_dest, _s, _len) \
|
2021-03-06 18:45:27 +03:00
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
g_memcpy((_dest), (_s)->p, (_len)); \
|
|
|
|
(_s)->p += (_len); \
|
|
|
|
} while (0)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
2013-05-03 13:07:02 +04:00
|
|
|
#define xstream_rd_string(_dest, _s, _len) \
|
2021-03-06 18:45:27 +03:00
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
g_memcpy((_dest), (_s)->p, (_len)); \
|
|
|
|
(_s)->p += (_len); \
|
|
|
|
} while (0)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
2013-07-19 22:10:55 +04:00
|
|
|
#define xstream_wr_string(_s, _src, _len) \
|
2021-03-06 18:45:27 +03:00
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
g_memcpy((_s)->p, (_src), (_len)); \
|
|
|
|
(_s)->p += (_len); \
|
|
|
|
} while (0)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
2013-05-03 13:07:02 +04:00
|
|
|
#define xstream_len(_s) (int) ((_s)->p - (_s)->data)
|
|
|
|
#define xstream_seek(_s, _len) (_s)->p += (_len)
|
2013-03-09 03:37:30 +04:00
|
|
|
|
2005-06-28 07:04:36 +04:00
|
|
|
#endif
|