a180a41bba1d50822df23fff0099e90b86638b89vboxsync/*
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rdesktop: A Remote Desktop Protocol client.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Parsing primitives
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Copyright (C) Matthew Chapman 1999-2008
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Copyright 2012 Henrik Andersson <hean01@cendio.se> for Cendio AB
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync This program is free software: you can redistribute it and/or modify
a180a41bba1d50822df23fff0099e90b86638b89vboxsync it under the terms of the GNU General Public License as published by
a180a41bba1d50822df23fff0099e90b86638b89vboxsync the Free Software Foundation, either version 3 of the License, or
a180a41bba1d50822df23fff0099e90b86638b89vboxsync (at your option) any later version.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync This program is distributed in the hope that it will be useful,
a180a41bba1d50822df23fff0099e90b86638b89vboxsync but WITHOUT ANY WARRANTY; without even the implied warranty of
a180a41bba1d50822df23fff0099e90b86638b89vboxsync MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a180a41bba1d50822df23fff0099e90b86638b89vboxsync GNU General Public License for more details.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync You should have received a copy of the GNU General Public License
a180a41bba1d50822df23fff0099e90b86638b89vboxsync along with this program. If not, see <http://www.gnu.org/licenses/>.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync*/
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync/*
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * the General Public License version 2 (GPLv2) at this time for any software where
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * a choice of GPL license versions is made available with the language indicating
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * that GPLv2 or any later version may be used, or where a choice of which version
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * of the GPL is applied is otherwise unspecified.
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync */
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Parser state */
a180a41bba1d50822df23fff0099e90b86638b89vboxsynctypedef struct stream
a180a41bba1d50822df23fff0099e90b86638b89vboxsync{
a180a41bba1d50822df23fff0099e90b86638b89vboxsync unsigned char *p;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync unsigned char *end;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync unsigned char *data;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync unsigned int size;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* Offsets of various headers */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync unsigned char *iso_hdr;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync unsigned char *mcs_hdr;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync unsigned char *sec_hdr;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync unsigned char *rdp_hdr;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync unsigned char *channel_hdr;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync}
a180a41bba1d50822df23fff0099e90b86638b89vboxsync *STREAM;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define s_push_layer(s,h,n) { (s)->h = (s)->p; (s)->p += n; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define s_pop_layer(s,h) (s)->p = (s)->h;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define s_mark_end(s) (s)->end = (s)->p;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define s_check(s) ((s)->p <= (s)->end)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define s_check_rem(s,n) ((s)->p + n <= (s)->end)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define s_check_end(s) ((s)->p == (s)->end)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define s_length(s) ((s)->end - (s)->data)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define s_reset(s) ((s)->end = (s)->p = (s)->data)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#if defined(L_ENDIAN) && !defined(NEED_ALIGN)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint16_le(s,v) { v = *(uint16 *)((s)->p); (s)->p += 2; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint32_le(s,v) { v = *(uint32 *)((s)->p); (s)->p += 4; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint16_le(s,v) { *(uint16 *)((s)->p) = v; (s)->p += 2; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint32_le(s,v) { *(uint32 *)((s)->p) = v; (s)->p += 4; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#else
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint16_le(s,v) { v = *((s)->p++); v += *((s)->p++) << 8; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint32_le(s,v) { in_uint16_le(s,v) \
a180a41bba1d50822df23fff0099e90b86638b89vboxsync v += *((s)->p++) << 16; v += *((s)->p++) << 24; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint16_le(s,v) { *((s)->p++) = (v) & 0xff; *((s)->p++) = ((v) >> 8) & 0xff; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint32_le(s,v) { out_uint16_le(s, (v) & 0xffff); out_uint16_le(s, ((v) >> 16) & 0xffff); }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#endif
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#if defined(B_ENDIAN) && !defined(NEED_ALIGN)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint16_be(s,v) { v = *(uint16 *)((s)->p); (s)->p += 2; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint32_be(s,v) { v = *(uint32 *)((s)->p); (s)->p += 4; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint16_be(s,v) { *(uint16 *)((s)->p) = v; (s)->p += 2; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint32_be(s,v) { *(uint32 *)((s)->p) = v; (s)->p += 4; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define B_ENDIAN_PREFERRED
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint16(s,v) in_uint16_be(s,v)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint32(s,v) in_uint32_be(s,v)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint16(s,v) out_uint16_be(s,v)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint32(s,v) out_uint32_be(s,v)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#else
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint16_be(s,v) { v = *((s)->p++); next_be(s,v); }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint32_be(s,v) { in_uint16_be(s,v); next_be(s,v); next_be(s,v); }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint16_be(s,v) { *((s)->p++) = ((v) >> 8) & 0xff; *((s)->p++) = (v) & 0xff; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint32_be(s,v) { out_uint16_be(s, ((v) >> 16) & 0xffff); out_uint16_be(s, (v) & 0xffff); }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#endif
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#ifndef B_ENDIAN_PREFERRED
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint16(s,v) in_uint16_le(s,v)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint32(s,v) in_uint32_le(s,v)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint16(s,v) out_uint16_le(s,v)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint32(s,v) out_uint32_le(s,v)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#endif
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint8(s,v) v = *((s)->p++);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint8p(s,v,n) { v = (s)->p; (s)->p += n; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint8a(s,v,n) { memcpy(v,(s)->p,n); (s)->p += n; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define in_uint8s(s,n) (s)->p += n;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint8(s,v) *((s)->p++) = v;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint8p(s,v,n) { memcpy((s)->p,v,n); (s)->p += n; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint8a(s,v,n) out_uint8p(s,v,n);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define out_uint8s(s,n) { memset((s)->p,0,n); (s)->p += n; }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define next_be(s,v) v = ((v) << 8) + *((s)->p++);