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