Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-utils / rail.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Remote Applications Integrated Locally (RAIL) Utils
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <freerdp/types.h>
21 #include <freerdp/utils/memory.h>
22
23 #include <freerdp/utils/rail.h>
24 #include <freerdp/rail.h>
25
26 void rail_unicode_string_alloc(UNICODE_STRING* unicode_string, uint16 cbString)
27 {
28         unicode_string->length = cbString;
29         unicode_string->string = xzalloc(cbString);
30 }
31
32 void rail_unicode_string_free(UNICODE_STRING* unicode_string)
33 {
34         unicode_string->length = 0;
35
36         if (unicode_string->string != NULL)
37                 xfree(unicode_string->string);
38 }
39
40 void rail_read_unicode_string(STREAM* s, UNICODE_STRING* unicode_string)
41 {
42         stream_read_uint16(s, unicode_string->length); /* cbString (2 bytes) */
43
44         if (unicode_string->string == NULL)
45                 unicode_string->string = (uint8*) xmalloc(unicode_string->length);
46         else
47                 unicode_string->string = (uint8*) xrealloc(unicode_string->string, unicode_string->length);
48
49         stream_read(s, unicode_string->string, unicode_string->length);
50 }
51
52 void rail_write_unicode_string(STREAM* s, UNICODE_STRING* unicode_string)
53 {
54         stream_check_size(s, 2 + unicode_string->length);
55         stream_write_uint16(s, unicode_string->length); /* cbString (2 bytes) */
56         stream_write(s, unicode_string->string, unicode_string->length); /* string */
57 }
58
59 void rail_write_unicode_string_value(STREAM* s, UNICODE_STRING* unicode_string)
60 {
61         if (unicode_string->length > 0)
62         {
63                 stream_check_size(s, unicode_string->length);
64                 stream_write(s, unicode_string->string, unicode_string->length); /* string */
65         }
66 }
67
68 void rail_read_rectangle_16(STREAM* s, RECTANGLE_16* rectangle_16)
69 {
70         stream_read_uint16(s, rectangle_16->left); /* left (2 bytes) */
71         stream_read_uint16(s, rectangle_16->top); /* top (2 bytes) */
72         stream_read_uint16(s, rectangle_16->right); /* right (2 bytes) */
73         stream_read_uint16(s, rectangle_16->bottom); /* bottom (2 bytes) */
74 }
75
76 void rail_write_rectangle_16(STREAM* s, RECTANGLE_16* rectangle_16)
77 {
78         stream_write_uint16(s, rectangle_16->left); /* left (2 bytes) */
79         stream_write_uint16(s, rectangle_16->top); /* top (2 bytes) */
80         stream_write_uint16(s, rectangle_16->right); /* right (2 bytes) */
81         stream_write_uint16(s, rectangle_16->bottom); /* bottom (2 bytes) */
82 }
83
84 void* rail_clone_order(uint32 event_type, void* order)
85 {
86         struct
87         {
88                 uint32 type;
89                 uint32 size;
90         } ordersize_table[] =
91         {
92                 {RDP_EVENT_TYPE_RAIL_CHANNEL_GET_SYSPARAMS, sizeof(RAIL_SYSPARAM_ORDER)},
93                 {RDP_EVENT_TYPE_RAIL_CHANNEL_EXEC_RESULTS, sizeof(RAIL_EXEC_RESULT_ORDER)},
94                 {RDP_EVENT_TYPE_RAIL_CHANNEL_SERVER_SYSPARAM, sizeof(RAIL_SYSPARAM_ORDER)},
95                 {RDP_EVENT_TYPE_RAIL_CHANNEL_SERVER_MINMAXINFO, sizeof(RAIL_MINMAXINFO_ORDER)},
96                 {RDP_EVENT_TYPE_RAIL_CHANNEL_SERVER_LOCALMOVESIZE, sizeof(RAIL_LOCALMOVESIZE_ORDER)},
97                 {RDP_EVENT_TYPE_RAIL_CHANNEL_APPID_RESP, sizeof(RAIL_GET_APPID_RESP_ORDER)},
98                 {RDP_EVENT_TYPE_RAIL_CHANNEL_LANGBARINFO, sizeof(RAIL_LANGBAR_INFO_ORDER)},
99                 {RDP_EVENT_TYPE_RAIL_CLIENT_SET_SYSPARAMS, sizeof(RAIL_SYSPARAM_ORDER)},
100                 {RDP_EVENT_TYPE_RAIL_CLIENT_EXEC_REMOTE_APP, sizeof(RDP_PLUGIN_DATA)},
101                 {RDP_EVENT_TYPE_RAIL_CLIENT_ACTIVATE, sizeof(RAIL_ACTIVATE_ORDER)},
102                 {RDP_EVENT_TYPE_RAIL_CLIENT_SYSMENU, sizeof(RAIL_SYSMENU_ORDER)},
103                 {RDP_EVENT_TYPE_RAIL_CLIENT_SYSCOMMAND, sizeof(RAIL_SYSCOMMAND_ORDER)},
104                 {RDP_EVENT_TYPE_RAIL_CLIENT_NOTIFY_EVENT, sizeof(RAIL_NOTIFY_EVENT_ORDER)},
105                 {RDP_EVENT_TYPE_RAIL_CLIENT_WINDOW_MOVE, sizeof(RAIL_WINDOW_MOVE_ORDER)},
106                 {RDP_EVENT_TYPE_RAIL_CLIENT_APPID_REQ, sizeof(RAIL_GET_APPID_REQ_ORDER)},
107                 {RDP_EVENT_TYPE_RAIL_CLIENT_LANGBARINFO, sizeof(RAIL_LANGBAR_INFO_ORDER)},
108         };
109         size_t i = 0;
110         size_t order_size = 0;
111         void*  new_order = NULL;
112
113         for (i = 0; i < RAIL_ARRAY_SIZE(ordersize_table); i++)
114         {
115                 if (event_type == ordersize_table[i].type)
116                 {
117                         order_size = ordersize_table[i].size;
118                         break;
119                 }
120         }
121
122         // Event type not found.
123         if (order_size == 0) return NULL;
124
125         new_order = xmalloc(order_size);
126         memcpy(new_order, order, order_size);
127
128         //printf("rail_clone_order: type=%d order=%p\n", event_type, new_order);
129
130         // Create copy of variable data for some orders
131         if ((event_type == RDP_EVENT_TYPE_RAIL_CHANNEL_GET_SYSPARAMS) ||
132                 (event_type == RDP_EVENT_TYPE_RAIL_CLIENT_SET_SYSPARAMS))
133         {
134                 RAIL_SYSPARAM_ORDER* new_sysparam = (RAIL_SYSPARAM_ORDER*)new_order;
135                 RAIL_SYSPARAM_ORDER* old_sysparam = (RAIL_SYSPARAM_ORDER*)order;
136
137                 rail_unicode_string_alloc(&new_sysparam->highContrast.colorScheme,
138                         old_sysparam->highContrast.colorScheme.length);
139
140                 memcpy(new_sysparam->highContrast.colorScheme.string,
141                         old_sysparam->highContrast.colorScheme.string,
142                         old_sysparam->highContrast.colorScheme.length);
143         }
144
145         if (event_type == RDP_EVENT_TYPE_RAIL_CHANNEL_EXEC_RESULTS)
146         {
147                 RAIL_EXEC_RESULT_ORDER* new_exec_result = (RAIL_EXEC_RESULT_ORDER*)new_order;
148                 RAIL_EXEC_RESULT_ORDER* old_exec_result = (RAIL_EXEC_RESULT_ORDER*)order;
149
150                 rail_unicode_string_alloc(&new_exec_result->exeOrFile,
151                                 old_exec_result->exeOrFile.length);
152
153                 memcpy(new_exec_result->exeOrFile.string,
154                         old_exec_result->exeOrFile.string,
155                         old_exec_result->exeOrFile.length);
156         }
157
158         if (event_type == RDP_EVENT_TYPE_RAIL_CHANNEL_APPID_RESP)
159         {
160                 RAIL_GET_APPID_RESP_ORDER* new_app_resp = (RAIL_GET_APPID_RESP_ORDER*)new_order;
161
162                 new_app_resp->applicationId.string = &new_app_resp->applicationIdBuffer[0];
163         }
164
165         return new_order;
166 }
167
168 void rail_free_cloned_order(uint32 event_type, void* order)
169 {
170         //printf("rail_free_cloned_order: type=%d order=%p\n", event_type, order);
171         if ((event_type == RDP_EVENT_TYPE_RAIL_CHANNEL_GET_SYSPARAMS) ||
172                 (event_type == RDP_EVENT_TYPE_RAIL_CLIENT_SET_SYSPARAMS))
173         {
174                 RAIL_SYSPARAM_ORDER* sysparam = (RAIL_SYSPARAM_ORDER*)order;
175                 rail_unicode_string_free(&sysparam->highContrast.colorScheme);
176         }
177
178         if (event_type == RDP_EVENT_TYPE_RAIL_CHANNEL_EXEC_RESULTS)
179         {
180                 RAIL_EXEC_RESULT_ORDER* exec_result = (RAIL_EXEC_RESULT_ORDER*)order;
181                 rail_unicode_string_free(&exec_result->exeOrFile);
182         }
183         xfree(order);
184 }