FreeRDP
Loading...
Searching...
No Matches
rdpei_common.c
1
21#include <freerdp/config.h>
22
23#include <winpr/crt.h>
24#include <winpr/cast.h>
25#include <winpr/stream.h>
26
27#include "rdpei_common.h"
28
29#include <freerdp/log.h>
30
31#define TAG FREERDP_TAG("channels.rdpei.common")
32
33BOOL rdpei_read_2byte_unsigned(wStream* s, UINT16* value)
34{
35 BYTE byte = 0;
36
37 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
38 return FALSE;
39
40 Stream_Read_UINT8(s, byte);
41
42 if (byte & 0x80)
43 {
44 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
45 return FALSE;
46
47 const INT32 ibyte = ((byte & 0x7F) << 8);
48 *value = WINPR_ASSERTING_INT_CAST(UINT16, ibyte);
49 Stream_Read_UINT8(s, byte);
50 *value |= byte;
51 }
52 else
53 {
54 *value = (byte & 0x7F);
55 }
56
57 return TRUE;
58}
59
60BOOL rdpei_write_2byte_unsigned(wStream* s, UINT16 value)
61{
62 BYTE byte = 0;
63
64 if (!Stream_EnsureRemainingCapacity(s, 2))
65 return FALSE;
66
67 if (value > 0x7FFF)
68 return FALSE;
69
70 if (value >= 0x7F)
71 {
72 byte = ((value & 0x7F00) >> 8);
73 Stream_Write_UINT8(s, byte | 0x80);
74 byte = (value & 0xFF);
75 Stream_Write_UINT8(s, byte);
76 }
77 else
78 {
79 byte = (value & 0x7F);
80 Stream_Write_UINT8(s, byte);
81 }
82
83 return TRUE;
84}
85
86BOOL rdpei_read_2byte_signed(wStream* s, INT16* value)
87{
88 BYTE byte = 0;
89 BOOL negative = 0;
90
91 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
92 return FALSE;
93
94 Stream_Read_UINT8(s, byte);
95
96 negative = (byte & 0x40) ? TRUE : FALSE;
97
98 const BYTE val = (byte & 0x3F);
99
100 if (byte & 0x80)
101 {
102 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
103 return FALSE;
104
105 Stream_Read_UINT8(s, byte);
106 *value = (INT16)((val << 8) | byte);
107 }
108 else
109 *value = val;
110
111 if (negative)
112 *value *= -1;
113
114 return TRUE;
115}
116
117BOOL rdpei_write_2byte_signed(wStream* s, INT16 value)
118{
119 BYTE byte = 0;
120 BOOL negative = FALSE;
121
122 if (!Stream_EnsureRemainingCapacity(s, 2))
123 return FALSE;
124
125 if (value < 0)
126 {
127 negative = TRUE;
128 value *= -1;
129 }
130
131 if (value > 0x3FFF)
132 return FALSE;
133
134 if (value >= 0x3F)
135 {
136 byte = ((value & 0x3F00) >> 8);
137
138 if (negative)
139 byte |= 0x40;
140
141 Stream_Write_UINT8(s, byte | 0x80);
142 byte = (value & 0xFF);
143 Stream_Write_UINT8(s, byte);
144 }
145 else
146 {
147 byte = (value & 0x3F);
148
149 if (negative)
150 byte |= 0x40;
151
152 Stream_Write_UINT8(s, byte);
153 }
154
155 return TRUE;
156}
157
158BOOL rdpei_read_4byte_unsigned(wStream* s, UINT32* value)
159{
160 BYTE byte = 0;
161 BYTE count = 0;
162
163 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
164 return FALSE;
165
166 Stream_Read_UINT8(s, byte);
167
168 count = (byte & 0xC0) >> 6;
169
170 if (!Stream_CheckAndLogRequiredLength(TAG, s, count))
171 return FALSE;
172
173 switch (count)
174 {
175 case 0:
176 *value = (byte & 0x3F);
177 break;
178
179 case 1:
180 *value = ((byte & 0x3F) << 8) & 0xFF00;
181 Stream_Read_UINT8(s, byte);
182 *value |= byte;
183 break;
184
185 case 2:
186 *value = ((byte & 0x3F) << 16) & 0xFF0000;
187 Stream_Read_UINT8(s, byte);
188 *value |= ((byte << 8) & 0xFF00);
189 Stream_Read_UINT8(s, byte);
190 *value |= byte;
191 break;
192
193 case 3:
194 *value = ((UINT32)(byte & 0x3F) << 24) & 0xFF000000;
195 Stream_Read_UINT8(s, byte);
196 *value |= ((UINT32)(byte << 16) & 0xFF0000);
197 Stream_Read_UINT8(s, byte);
198 *value |= ((UINT32)(byte << 8) & 0xFF00);
199 Stream_Read_UINT8(s, byte);
200 *value |= byte;
201 break;
202
203 default:
204 break;
205 }
206
207 return TRUE;
208}
209
210BOOL rdpei_write_4byte_unsigned(wStream* s, UINT32 value)
211{
212 BYTE byte = 0;
213
214 if (!Stream_EnsureRemainingCapacity(s, 4))
215 return FALSE;
216
217 if (value <= 0x3FUL)
218 {
219 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, value));
220 }
221 else if (value <= 0x3FFFUL)
222 {
223 byte = (value >> 8) & 0x3F;
224 Stream_Write_UINT8(s, byte | 0x40);
225 byte = (value & 0xFF);
226 Stream_Write_UINT8(s, byte);
227 }
228 else if (value <= 0x3FFFFFUL)
229 {
230 byte = (value >> 16) & 0x3F;
231 Stream_Write_UINT8(s, byte | 0x80);
232 byte = (value >> 8) & 0xFF;
233 Stream_Write_UINT8(s, byte);
234 byte = (value & 0xFF);
235 Stream_Write_UINT8(s, byte);
236 }
237 else if (value <= 0x3FFFFFFFUL)
238 {
239 byte = (value >> 24) & 0x3F;
240 Stream_Write_UINT8(s, byte | 0xC0);
241 byte = (value >> 16) & 0xFF;
242 Stream_Write_UINT8(s, byte);
243 byte = (value >> 8) & 0xFF;
244 Stream_Write_UINT8(s, byte);
245 byte = (value & 0xFF);
246 Stream_Write_UINT8(s, byte);
247 }
248 else
249 {
250 return FALSE;
251 }
252
253 return TRUE;
254}
255
256BOOL rdpei_read_4byte_signed(wStream* s, INT32* value)
257{
258 BYTE byte = 0;
259 BYTE count = 0;
260 BOOL negative = 0;
261
262 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
263 return FALSE;
264
265 Stream_Read_UINT8(s, byte);
266
267 count = (byte & 0xC0) >> 6;
268 negative = (byte & 0x20) ? TRUE : FALSE;
269
270 if (!Stream_CheckAndLogRequiredLength(TAG, s, count))
271 return FALSE;
272
273 switch (count)
274 {
275 case 0:
276 *value = (byte & 0x1F);
277 break;
278
279 case 1:
280 *value = (byte & 0x1F) << 8;
281 Stream_Read_UINT8(s, byte);
282 *value |= byte;
283 break;
284
285 case 2:
286 *value = (byte & 0x1F) << 16;
287 Stream_Read_UINT8(s, byte);
288 *value |= (byte << 8);
289 Stream_Read_UINT8(s, byte);
290 *value |= byte;
291 break;
292
293 case 3:
294 *value = (byte & 0x1F) << 24;
295 Stream_Read_UINT8(s, byte);
296 *value |= (byte << 16);
297 Stream_Read_UINT8(s, byte);
298 *value |= (byte << 8);
299 Stream_Read_UINT8(s, byte);
300 *value |= byte;
301 break;
302
303 default:
304 break;
305 }
306
307 if (negative)
308 *value *= -1;
309
310 return TRUE;
311}
312
313BOOL rdpei_write_4byte_signed(wStream* s, INT32 value)
314{
315 BYTE byte = 0;
316 BOOL negative = FALSE;
317
318 if (!Stream_EnsureRemainingCapacity(s, 4))
319 return FALSE;
320
321 if (value < 0)
322 {
323 negative = TRUE;
324 value *= -1;
325 }
326
327 if (value <= 0x1FL)
328 {
329 byte = value & 0x1F;
330
331 if (negative)
332 byte |= 0x20;
333
334 Stream_Write_UINT8(s, byte);
335 }
336 else if (value <= 0x1FFFL)
337 {
338 byte = (value >> 8) & 0x1F;
339
340 if (negative)
341 byte |= 0x20;
342
343 Stream_Write_UINT8(s, byte | 0x40);
344 byte = (value & 0xFF);
345 Stream_Write_UINT8(s, byte);
346 }
347 else if (value <= 0x1FFFFFL)
348 {
349 byte = (value >> 16) & 0x1F;
350
351 if (negative)
352 byte |= 0x20;
353
354 Stream_Write_UINT8(s, byte | 0x80);
355 byte = (value >> 8) & 0xFF;
356 Stream_Write_UINT8(s, byte);
357 byte = (value & 0xFF);
358 Stream_Write_UINT8(s, byte);
359 }
360 else if (value <= 0x1FFFFFFFL)
361 {
362 byte = (value >> 24) & 0x1F;
363
364 if (negative)
365 byte |= 0x20;
366
367 Stream_Write_UINT8(s, byte | 0xC0);
368 byte = (value >> 16) & 0xFF;
369 Stream_Write_UINT8(s, byte);
370 byte = (value >> 8) & 0xFF;
371 Stream_Write_UINT8(s, byte);
372 byte = (value & 0xFF);
373 Stream_Write_UINT8(s, byte);
374 }
375 else
376 {
377 return FALSE;
378 }
379
380 return TRUE;
381}
382
383BOOL rdpei_read_8byte_unsigned(wStream* s, UINT64* value)
384{
385 UINT64 byte = 0;
386 BYTE count = 0;
387
388 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
389 return FALSE;
390
391 Stream_Read_UINT8(s, byte);
392
393 count = (byte & 0xE0) >> 5;
394
395 if (!Stream_CheckAndLogRequiredLength(TAG, s, count))
396 return FALSE;
397
398 switch (count)
399 {
400 case 0:
401 *value = (byte & 0x1F);
402 break;
403
404 case 1:
405 *value = (byte & 0x1FU) << 8U;
406 Stream_Read_UINT8(s, byte);
407 *value |= byte;
408 break;
409
410 case 2:
411 *value = (byte & 0x1FU) << 16U;
412 Stream_Read_UINT8(s, byte);
413 *value |= (byte << 8U);
414 Stream_Read_UINT8(s, byte);
415 *value |= byte;
416 break;
417
418 case 3:
419 *value = (byte & 0x1FU) << 24U;
420 Stream_Read_UINT8(s, byte);
421 *value |= (byte << 16U);
422 Stream_Read_UINT8(s, byte);
423 *value |= (byte << 8U);
424 Stream_Read_UINT8(s, byte);
425 *value |= byte;
426 break;
427
428 case 4:
429 *value = ((byte & 0x1FU)) << 32U;
430 Stream_Read_UINT8(s, byte);
431 *value |= (byte << 24U);
432 Stream_Read_UINT8(s, byte);
433 *value |= (byte << 16U);
434 Stream_Read_UINT8(s, byte);
435 *value |= (byte << 8U);
436 Stream_Read_UINT8(s, byte);
437 *value |= byte;
438 break;
439
440 case 5:
441 *value = ((byte & 0x1FU)) << 40U;
442 Stream_Read_UINT8(s, byte);
443 *value |= ((byte) << 32U);
444 Stream_Read_UINT8(s, byte);
445 *value |= (byte << 24U);
446 Stream_Read_UINT8(s, byte);
447 *value |= (byte << 16U);
448 Stream_Read_UINT8(s, byte);
449 *value |= (byte << 8U);
450 Stream_Read_UINT8(s, byte);
451 *value |= byte;
452 break;
453
454 case 6:
455 *value = ((byte & 0x1FU)) << 48U;
456 Stream_Read_UINT8(s, byte);
457 *value |= ((byte) << 40U);
458 Stream_Read_UINT8(s, byte);
459 *value |= ((byte) << 32U);
460 Stream_Read_UINT8(s, byte);
461 *value |= (byte << 24U);
462 Stream_Read_UINT8(s, byte);
463 *value |= (byte << 16U);
464 Stream_Read_UINT8(s, byte);
465 *value |= (byte << 8U);
466 Stream_Read_UINT8(s, byte);
467 *value |= byte;
468 break;
469
470 case 7:
471 *value = ((byte & 0x1FU)) << 56U;
472 Stream_Read_UINT8(s, byte);
473 *value |= ((byte) << 48U);
474 Stream_Read_UINT8(s, byte);
475 *value |= ((byte) << 40U);
476 Stream_Read_UINT8(s, byte);
477 *value |= ((byte) << 32U);
478 Stream_Read_UINT8(s, byte);
479 *value |= (byte << 24U);
480 Stream_Read_UINT8(s, byte);
481 *value |= (byte << 16U);
482 Stream_Read_UINT8(s, byte);
483 *value |= (byte << 8U);
484 Stream_Read_UINT8(s, byte);
485 *value |= byte;
486 break;
487
488 default:
489 break;
490 }
491
492 return TRUE;
493}
494
495BOOL rdpei_write_8byte_unsigned(wStream* s, UINT64 value)
496{
497 BYTE byte = 0;
498
499 if (!Stream_EnsureRemainingCapacity(s, 8))
500 return FALSE;
501
502 if (value <= 0x1FULL)
503 {
504 byte = value & 0x1F;
505 Stream_Write_UINT8(s, byte);
506 }
507 else if (value <= 0x1FFFULL)
508 {
509 byte = (value >> 8) & 0x1F;
510 byte |= (1 << 5);
511 Stream_Write_UINT8(s, byte);
512 byte = (value & 0xFF);
513 Stream_Write_UINT8(s, byte);
514 }
515 else if (value <= 0x1FFFFFULL)
516 {
517 byte = (value >> 16) & 0x1F;
518 byte |= (2 << 5);
519 Stream_Write_UINT8(s, byte);
520 byte = (value >> 8) & 0xFF;
521 Stream_Write_UINT8(s, byte);
522 byte = (value & 0xFF);
523 Stream_Write_UINT8(s, byte);
524 }
525 else if (value <= 0x1FFFFFFFULL)
526 {
527 byte = (value >> 24) & 0x1F;
528 byte |= (3 << 5);
529 Stream_Write_UINT8(s, byte);
530 byte = (value >> 16) & 0xFF;
531 Stream_Write_UINT8(s, byte);
532 byte = (value >> 8) & 0xFF;
533 Stream_Write_UINT8(s, byte);
534 byte = (value & 0xFF);
535 Stream_Write_UINT8(s, byte);
536 }
537 else if (value <= 0x1FFFFFFFFFULL)
538 {
539 byte = (value >> 32) & 0x1F;
540 byte |= (4 << 5);
541 Stream_Write_UINT8(s, byte);
542 byte = (value >> 24) & 0x1F;
543 Stream_Write_UINT8(s, byte);
544 byte = (value >> 16) & 0xFF;
545 Stream_Write_UINT8(s, byte);
546 byte = (value >> 8) & 0xFF;
547 Stream_Write_UINT8(s, byte);
548 byte = (value & 0xFF);
549 Stream_Write_UINT8(s, byte);
550 }
551 else if (value <= 0x1FFFFFFFFFFFULL)
552 {
553 byte = (value >> 40) & 0x1F;
554 byte |= (5 << 5);
555 Stream_Write_UINT8(s, byte);
556 byte = (value >> 32) & 0x1F;
557 Stream_Write_UINT8(s, byte);
558 byte = (value >> 24) & 0x1F;
559 Stream_Write_UINT8(s, byte);
560 byte = (value >> 16) & 0xFF;
561 Stream_Write_UINT8(s, byte);
562 byte = (value >> 8) & 0xFF;
563 Stream_Write_UINT8(s, byte);
564 byte = (value & 0xFF);
565 Stream_Write_UINT8(s, byte);
566 }
567 else if (value <= 0x1FFFFFFFFFFFFFULL)
568 {
569 byte = (value >> 48) & 0x1F;
570 byte |= (6 << 5);
571 Stream_Write_UINT8(s, byte);
572 byte = (value >> 40) & 0x1F;
573 Stream_Write_UINT8(s, byte);
574 byte = (value >> 32) & 0x1F;
575 Stream_Write_UINT8(s, byte);
576 byte = (value >> 24) & 0x1F;
577 Stream_Write_UINT8(s, byte);
578 byte = (value >> 16) & 0xFF;
579 Stream_Write_UINT8(s, byte);
580 byte = (value >> 8) & 0xFF;
581 Stream_Write_UINT8(s, byte);
582 byte = (value & 0xFF);
583 Stream_Write_UINT8(s, byte);
584 }
585 else if (value <= 0x1FFFFFFFFFFFFFFFULL)
586 {
587 byte = (value >> 56) & 0x1F;
588 byte |= (7 << 5);
589 Stream_Write_UINT8(s, byte);
590 byte = (value >> 48) & 0x1F;
591 Stream_Write_UINT8(s, byte);
592 byte = (value >> 40) & 0x1F;
593 Stream_Write_UINT8(s, byte);
594 byte = (value >> 32) & 0x1F;
595 Stream_Write_UINT8(s, byte);
596 byte = (value >> 24) & 0x1F;
597 Stream_Write_UINT8(s, byte);
598 byte = (value >> 16) & 0xFF;
599 Stream_Write_UINT8(s, byte);
600 byte = (value >> 8) & 0xFF;
601 Stream_Write_UINT8(s, byte);
602 byte = (value & 0xFF);
603 Stream_Write_UINT8(s, byte);
604 }
605 else
606 {
607 return FALSE;
608 }
609
610 return TRUE;
611}
612
613void touch_event_reset(RDPINPUT_TOUCH_EVENT* event)
614{
615 for (UINT16 i = 0; i < event->frameCount; i++)
616 touch_frame_reset(&event->frames[i]);
617
618 free(event->frames);
619 event->frames = NULL;
620 event->frameCount = 0;
621}
622
623void touch_frame_reset(RDPINPUT_TOUCH_FRAME* frame)
624{
625 free(frame->contacts);
626 frame->contacts = NULL;
627 frame->contactCount = 0;
628}
629
630void pen_event_reset(RDPINPUT_PEN_EVENT* event)
631{
632 for (UINT16 i = 0; i < event->frameCount; i++)
633 pen_frame_reset(&event->frames[i]);
634
635 free(event->frames);
636 event->frames = NULL;
637 event->frameCount = 0;
638}
639
640void pen_frame_reset(RDPINPUT_PEN_FRAME* frame)
641{
642 free(frame->contacts);
643 frame->contacts = NULL;
644 frame->contactCount = 0;
645}
a touch event with some frames
a touch event with some frames
a frame containing contact points