-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
initial MPRIS support #5255
base: master
Are you sure you want to change the base?
initial MPRIS support #5255
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,20 @@ | |
#include "util/binary.h" | ||
#include "util/log.h" | ||
|
||
static int read_message(uint8_t **target, const uint8_t *src, const uint16_t size) { | ||
uint8_t *data = malloc(size + 1); | ||
if (!data) { | ||
LOG_OOM(); | ||
return -1; | ||
} | ||
if (size) { | ||
data[size] = '\0'; | ||
memcpy(data, src, size); | ||
} | ||
*target = data; | ||
return 0; | ||
} | ||
|
||
ssize_t | ||
sc_device_msg_deserialize(const uint8_t *buf, size_t len, | ||
struct sc_device_msg *msg) { | ||
|
@@ -25,17 +39,10 @@ sc_device_msg_deserialize(const uint8_t *buf, size_t len, | |
if (clipboard_len > len - 5) { | ||
return 0; // no complete message | ||
} | ||
char *text = malloc(clipboard_len + 1); | ||
if (!text) { | ||
LOG_OOM(); | ||
if (read_message((uint8_t **)&msg->clipboard.text, &buf[5], clipboard_len) == -1) { | ||
return -1; | ||
} | ||
if (clipboard_len) { | ||
memcpy(text, &buf[5], clipboard_len); | ||
} | ||
text[clipboard_len] = '\0'; | ||
|
||
msg->clipboard.text = text; | ||
return 5 + clipboard_len; | ||
} | ||
case DEVICE_MSG_TYPE_ACK_CLIPBOARD: { | ||
|
@@ -56,21 +63,43 @@ sc_device_msg_deserialize(const uint8_t *buf, size_t len, | |
if (size < len - 5) { | ||
return 0; // not available | ||
} | ||
uint8_t *data = malloc(size); | ||
if (!data) { | ||
LOG_OOM(); | ||
|
||
msg->uhid_output.id = id; | ||
msg->uhid_output.size = size; | ||
if (read_message(&msg->uhid_output.data, &buf[5], size) == -1) { | ||
return -1; | ||
} | ||
if (size) { | ||
memcpy(data, &buf[5], size); | ||
|
||
return 5 + size; | ||
case DEVICE_MSG_TYPE_MEDIA_UPDATE: { | ||
if (len < 5) { | ||
// at least id + size | ||
return 0; // not available | ||
} | ||
uint16_t id = sc_read16be(&buf[1]); | ||
size_t size = sc_read16be(&buf[3]); | ||
if (size < len - 5) { | ||
return 0; // not available | ||
} | ||
|
||
msg->uhid_output.id = id; | ||
msg->uhid_output.size = size; | ||
msg->uhid_output.data = data; | ||
msg->media_update.id = id; | ||
msg->media_update.size = size; | ||
if (read_message(&msg->media_update.data, &buf[5], size) == -1) { | ||
return -1; | ||
} | ||
|
||
return 5 + size; | ||
} | ||
case DEVICE_MSG_TYPE_MEDIA_REMOVE: { | ||
if (len < 3) { | ||
// at least id | ||
return 0; // not available | ||
} | ||
uint16_t id = sc_read16be(&buf[1]); | ||
msg->media_remove.id = id; | ||
return 3; | ||
} | ||
} | ||
Comment on lines
+101
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it intentional that these closing brackets don't match their indentation level? The first one closes the case of (I'm not even sure if the |
||
default: | ||
LOGW("Unknown device message type: %d", (int) msg->type); | ||
return -1; // error, we cannot recover | ||
|
@@ -86,6 +115,9 @@ sc_device_msg_destroy(struct sc_device_msg *msg) { | |
case DEVICE_MSG_TYPE_UHID_OUTPUT: | ||
free(msg->uhid_output.data); | ||
break; | ||
case DEVICE_MSG_TYPE_MEDIA_UPDATE: | ||
free(msg->media_update.data); | ||
break; | ||
default: | ||
// nothing to do | ||
break; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this leave
target
pointing at uninitialized memory ifsize == 0
? Below, the null terminator write was done unconditionally. Putting this here would result in: