1*c2c66affSColin Finck /* 2*c2c66affSColin Finck rdesktop: A Remote Desktop Protocol client. 3*c2c66affSColin Finck Parsing primitives 4*c2c66affSColin Finck Copyright (C) Matthew Chapman 1999-2008 5*c2c66affSColin Finck Copyright 2012 Henrik Andersson <hean01@cendio.se> for Cendio AB 6*c2c66affSColin Finck 7*c2c66affSColin Finck This program is free software: you can redistribute it and/or modify 8*c2c66affSColin Finck it under the terms of the GNU General Public License as published by 9*c2c66affSColin Finck the Free Software Foundation, either version 3 of the License, or 10*c2c66affSColin Finck (at your option) any later version. 11*c2c66affSColin Finck 12*c2c66affSColin Finck This program is distributed in the hope that it will be useful, 13*c2c66affSColin Finck but WITHOUT ANY WARRANTY; without even the implied warranty of 14*c2c66affSColin Finck MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*c2c66affSColin Finck GNU General Public License for more details. 16*c2c66affSColin Finck 17*c2c66affSColin Finck You should have received a copy of the GNU General Public License 18*c2c66affSColin Finck along with this program. If not, see <http://www.gnu.org/licenses/>. 19*c2c66affSColin Finck */ 20*c2c66affSColin Finck 21*c2c66affSColin Finck /* Parser state */ 22*c2c66affSColin Finck typedef struct stream 23*c2c66affSColin Finck { 24*c2c66affSColin Finck unsigned char *p; 25*c2c66affSColin Finck unsigned char *end; 26*c2c66affSColin Finck unsigned char *data; 27*c2c66affSColin Finck unsigned int size; 28*c2c66affSColin Finck 29*c2c66affSColin Finck /* Offsets of various headers */ 30*c2c66affSColin Finck unsigned char *iso_hdr; 31*c2c66affSColin Finck unsigned char *mcs_hdr; 32*c2c66affSColin Finck unsigned char *sec_hdr; 33*c2c66affSColin Finck unsigned char *rdp_hdr; 34*c2c66affSColin Finck unsigned char *channel_hdr; 35*c2c66affSColin Finck 36*c2c66affSColin Finck } 37*c2c66affSColin Finck *STREAM; 38*c2c66affSColin Finck 39*c2c66affSColin Finck #define s_push_layer(s,h,n) { (s)->h = (s)->p; (s)->p += n; } 40*c2c66affSColin Finck #define s_pop_layer(s,h) (s)->p = (s)->h; 41*c2c66affSColin Finck #define s_mark_end(s) (s)->end = (s)->p; 42*c2c66affSColin Finck #define s_check(s) ((s)->p <= (s)->end) 43*c2c66affSColin Finck #define s_check_rem(s,n) ((s)->p + n <= (s)->end) 44*c2c66affSColin Finck #define s_check_end(s) ((s)->p == (s)->end) 45*c2c66affSColin Finck #define s_length(s) ((s)->end - (s)->data) 46*c2c66affSColin Finck #define s_reset(s) ((s)->end = (s)->p = (s)->data) 47*c2c66affSColin Finck 48*c2c66affSColin Finck #if defined(L_ENDIAN) && !defined(NEED_ALIGN) 49*c2c66affSColin Finck #define in_uint16_le(s,v) { v = *(uint16 *)((s)->p); (s)->p += 2; } 50*c2c66affSColin Finck #define in_uint32_le(s,v) { v = *(uint32 *)((s)->p); (s)->p += 4; } 51*c2c66affSColin Finck #define out_uint16_le(s,v) { *(uint16 *)((s)->p) = v; (s)->p += 2; } 52*c2c66affSColin Finck #define out_uint32_le(s,v) { *(uint32 *)((s)->p) = v; (s)->p += 4; } 53*c2c66affSColin Finck 54*c2c66affSColin Finck #else 55*c2c66affSColin Finck #define in_uint16_le(s,v) { v = *((s)->p++); v += *((s)->p++) << 8; } 56*c2c66affSColin Finck #define in_uint32_le(s,v) { in_uint16_le(s,v) \ 57*c2c66affSColin Finck v += *((s)->p++) << 16; v += *((s)->p++) << 24; } 58*c2c66affSColin Finck #define out_uint16_le(s,v) { *((s)->p++) = (v) & 0xff; *((s)->p++) = ((v) >> 8) & 0xff; } 59*c2c66affSColin Finck #define out_uint32_le(s,v) { out_uint16_le(s, (v) & 0xffff); out_uint16_le(s, ((v) >> 16) & 0xffff); } 60*c2c66affSColin Finck #endif 61*c2c66affSColin Finck 62*c2c66affSColin Finck #if defined(B_ENDIAN) && !defined(NEED_ALIGN) 63*c2c66affSColin Finck #define in_uint16_be(s,v) { v = *(uint16 *)((s)->p); (s)->p += 2; } 64*c2c66affSColin Finck #define in_uint32_be(s,v) { v = *(uint32 *)((s)->p); (s)->p += 4; } 65*c2c66affSColin Finck #define out_uint16_be(s,v) { *(uint16 *)((s)->p) = v; (s)->p += 2; } 66*c2c66affSColin Finck #define out_uint32_be(s,v) { *(uint32 *)((s)->p) = v; (s)->p += 4; } 67*c2c66affSColin Finck 68*c2c66affSColin Finck #define B_ENDIAN_PREFERRED 69*c2c66affSColin Finck #define in_uint16(s,v) in_uint16_be(s,v) 70*c2c66affSColin Finck #define in_uint32(s,v) in_uint32_be(s,v) 71*c2c66affSColin Finck #define out_uint16(s,v) out_uint16_be(s,v) 72*c2c66affSColin Finck #define out_uint32(s,v) out_uint32_be(s,v) 73*c2c66affSColin Finck 74*c2c66affSColin Finck #else 75*c2c66affSColin Finck #define in_uint16_be(s,v) { v = *((s)->p++); next_be(s,v); } 76*c2c66affSColin Finck #define in_uint32_be(s,v) { in_uint16_be(s,v); next_be(s,v); next_be(s,v); } 77*c2c66affSColin Finck #define out_uint16_be(s,v) { *((s)->p++) = ((v) >> 8) & 0xff; *((s)->p++) = (v) & 0xff; } 78*c2c66affSColin Finck #define out_uint32_be(s,v) { out_uint16_be(s, ((v) >> 16) & 0xffff); out_uint16_be(s, (v) & 0xffff); } 79*c2c66affSColin Finck #endif 80*c2c66affSColin Finck 81*c2c66affSColin Finck #ifndef B_ENDIAN_PREFERRED 82*c2c66affSColin Finck #define in_uint16(s,v) in_uint16_le(s,v) 83*c2c66affSColin Finck #define in_uint32(s,v) in_uint32_le(s,v) 84*c2c66affSColin Finck #define out_uint16(s,v) out_uint16_le(s,v) 85*c2c66affSColin Finck #define out_uint32(s,v) out_uint32_le(s,v) 86*c2c66affSColin Finck #endif 87*c2c66affSColin Finck 88*c2c66affSColin Finck #define in_uint8(s,v) v = *((s)->p++); 89*c2c66affSColin Finck #define in_uint8p(s,v,n) { v = (s)->p; (s)->p += n; } 90*c2c66affSColin Finck #define in_uint8a(s,v,n) { memcpy(v,(s)->p,n); (s)->p += n; } 91*c2c66affSColin Finck #define in_uint8s(s,n) (s)->p += n; 92*c2c66affSColin Finck #define out_uint8(s,v) *((s)->p++) = v; 93*c2c66affSColin Finck #define out_uint8p(s,v,n) { memcpy((s)->p,v,n); (s)->p += n; } 94*c2c66affSColin Finck #define out_uint8a(s,v,n) out_uint8p(s,v,n); 95*c2c66affSColin Finck #define out_uint8s(s,n) { memset((s)->p,0,n); (s)->p += n; } 96*c2c66affSColin Finck 97*c2c66affSColin Finck #define next_be(s,v) v = ((v) << 8) + *((s)->p++); 98