1 |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Protocol services - parsing layer |
4 |
Copyright (C) Matthew Chapman 1999-2000 |
5 |
|
6 |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation; either version 2 of the License, or |
9 |
(at your option) any later version. |
10 |
|
11 |
This program is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU General Public License |
17 |
along with this program; if not, write to the Free Software |
18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#include "includes.h" |
22 |
|
23 |
/* Parse a 8-bit integer */ |
24 |
BOOL prs_io_uint8(STREAM s, uint8 *i) |
25 |
{ |
26 |
if (s->offset + 1 > s->end) |
27 |
{ |
28 |
ERROR("Parse past end of buffer\n"); |
29 |
return False; |
30 |
} |
31 |
|
32 |
if (s->marshall) |
33 |
s->data[s->offset] = *i; |
34 |
else |
35 |
*i = s->data[s->offset]; |
36 |
|
37 |
s->offset++; |
38 |
return True; |
39 |
} |
40 |
|
41 |
/* Parse a sequence of 8-bit integers */ |
42 |
BOOL prs_io_uint8s(STREAM s, uint8 *p, unsigned int length) |
43 |
{ |
44 |
if (s->offset + length > s->end) |
45 |
{ |
46 |
ERROR("Parse past end of buffer\n"); |
47 |
return False; |
48 |
} |
49 |
|
50 |
if (s->marshall) |
51 |
memcpy(s->data + s->offset, p, length); |
52 |
else |
53 |
memcpy(p, s->data + s->offset, length); |
54 |
|
55 |
s->offset += length; |
56 |
return True; |
57 |
} |
58 |
|
59 |
/* Parse a 16-bit integer, most significant bytes first */ |
60 |
BOOL msb_io_uint16(STREAM s, uint16 *i) |
61 |
{ |
62 |
int offset = s->offset; |
63 |
|
64 |
if (offset + 2 > s->end) |
65 |
{ |
66 |
ERROR("Parse past end of buffer\n"); |
67 |
return False; |
68 |
} |
69 |
|
70 |
if (s->marshall) { |
71 |
s->data[offset] = (uint8)(*i >> 8); |
72 |
s->data[offset+1] = (uint8)(*i); |
73 |
} else { |
74 |
*i = (s->data[offset] << 8) + (s->data[offset+1]); |
75 |
} |
76 |
|
77 |
s->offset+=2; |
78 |
return True; |
79 |
} |
80 |
|
81 |
/* Parse a 16-bit integer, least significant bytes first */ |
82 |
BOOL lsb_io_uint16(STREAM s, uint16 *i) |
83 |
{ |
84 |
int offset = s->offset; |
85 |
|
86 |
if (offset + 2 > s->end) |
87 |
{ |
88 |
ERROR("Parse past end of buffer\n"); |
89 |
return False; |
90 |
} |
91 |
|
92 |
if (s->marshall) { |
93 |
s->data[offset] = (uint8)(*i); |
94 |
s->data[offset+1] = (uint8)(*i >> 8); |
95 |
} else { |
96 |
*i = (s->data[offset]) + (s->data[offset+1] << 8); |
97 |
} |
98 |
|
99 |
s->offset += 2; |
100 |
return True; |
101 |
} |
102 |
|
103 |
/* Parse a 32-bit integer, least significant bytes first */ |
104 |
BOOL lsb_io_uint32(STREAM s, uint32 *i) |
105 |
{ |
106 |
int offset = s->offset; |
107 |
|
108 |
if (offset + 4 > s->end) |
109 |
{ |
110 |
ERROR("Parse past end of buffer\n"); |
111 |
return False; |
112 |
} |
113 |
|
114 |
if (s->marshall) { |
115 |
s->data[offset] = (uint8)(*i); |
116 |
s->data[offset+1] = (uint8)(*i >> 8); |
117 |
s->data[offset+2] = (uint8)(*i >> 16); |
118 |
s->data[offset+3] = (uint8)(*i >> 24); |
119 |
} else { |
120 |
*i = (s->data[offset]) + (s->data[offset+1] << 8) |
121 |
+ (s->data[offset+2] << 16) + (s->data[offset+3] << 24); |
122 |
} |
123 |
|
124 |
s->offset += 4; |
125 |
return True; |
126 |
} |