7
7
#include "util/binary.h"
8
8
#include "util/log.h"
9
9
10
+ static int read_message (uint8_t * * target , const uint8_t * src , const uint16_t size ) {
11
+ uint8_t * data = malloc (size + 1 );
12
+ if (!data ) {
13
+ LOG_OOM ();
14
+ return -1 ;
15
+ }
16
+ if (size ) {
17
+ data [size ] = '\0' ;
18
+ memcpy (data , src , size );
19
+ }
20
+ * target = data ;
21
+ return 0 ;
22
+ }
23
+
10
24
ssize_t
11
25
sc_device_msg_deserialize (const uint8_t * buf , size_t len ,
12
26
struct sc_device_msg * msg ) {
@@ -25,17 +39,10 @@ sc_device_msg_deserialize(const uint8_t *buf, size_t len,
25
39
if (clipboard_len > len - 5 ) {
26
40
return 0 ; // no complete message
27
41
}
28
- char * text = malloc (clipboard_len + 1 );
29
- if (!text ) {
30
- LOG_OOM ();
42
+ if (read_message ((uint8_t * * )& msg -> clipboard .text , & buf [5 ], clipboard_len ) == -1 ) {
31
43
return -1 ;
32
44
}
33
- if (clipboard_len ) {
34
- memcpy (text , & buf [5 ], clipboard_len );
35
- }
36
- text [clipboard_len ] = '\0' ;
37
45
38
- msg -> clipboard .text = text ;
39
46
return 5 + clipboard_len ;
40
47
}
41
48
case DEVICE_MSG_TYPE_ACK_CLIPBOARD : {
@@ -56,21 +63,43 @@ sc_device_msg_deserialize(const uint8_t *buf, size_t len,
56
63
if (size < len - 5 ) {
57
64
return 0 ; // not available
58
65
}
59
- uint8_t * data = malloc (size );
60
- if (!data ) {
61
- LOG_OOM ();
66
+
67
+ msg -> uhid_output .id = id ;
68
+ msg -> uhid_output .size = size ;
69
+ if (read_message (& msg -> uhid_output .data , & buf [5 ], size ) == -1 ) {
62
70
return -1 ;
63
71
}
64
- if (size ) {
65
- memcpy (data , & buf [5 ], size );
72
+
73
+ return 5 + size ;
74
+ case DEVICE_MSG_TYPE_MEDIA_UPDATE : {
75
+ if (len < 5 ) {
76
+ // at least id + size
77
+ return 0 ; // not available
78
+ }
79
+ uint16_t id = sc_read16be (& buf [1 ]);
80
+ size_t size = sc_read16be (& buf [3 ]);
81
+ if (size < len - 5 ) {
82
+ return 0 ; // not available
66
83
}
67
84
68
- msg -> uhid_output .id = id ;
69
- msg -> uhid_output .size = size ;
70
- msg -> uhid_output .data = data ;
85
+ msg -> media_update .id = id ;
86
+ msg -> media_update .size = size ;
87
+ if (read_message (& msg -> media_update .data , & buf [5 ], size ) == -1 ) {
88
+ return -1 ;
89
+ }
71
90
72
91
return 5 + size ;
73
92
}
93
+ case DEVICE_MSG_TYPE_MEDIA_REMOVE : {
94
+ if (len < 3 ) {
95
+ // at least id
96
+ return 0 ; // not available
97
+ }
98
+ uint16_t id = sc_read16be (& buf [1 ]);
99
+ msg -> media_remove .id = id ;
100
+ return 3 ;
101
+ }
102
+ }
74
103
default :
75
104
LOGW ("Unknown device message type: %d" , (int ) msg -> type );
76
105
return -1 ; // error, we cannot recover
@@ -86,6 +115,9 @@ sc_device_msg_destroy(struct sc_device_msg *msg) {
86
115
case DEVICE_MSG_TYPE_UHID_OUTPUT :
87
116
free (msg -> uhid_output .data );
88
117
break ;
118
+ case DEVICE_MSG_TYPE_MEDIA_UPDATE :
119
+ free (msg -> media_update .data );
120
+ break ;
89
121
default :
90
122
// nothing to do
91
123
break ;
0 commit comments