Rename buffer util functions with sc_ prefix

This commit is contained in:
Romain Vimont 2022-02-12 09:12:46 +01:00
parent 044acc2259
commit 5c62f3419d
5 changed files with 37 additions and 37 deletions

View file

@ -63,17 +63,17 @@ static const char *const copy_key_labels[] = {
static void static void
write_position(uint8_t *buf, const struct sc_position *position) { write_position(uint8_t *buf, const struct sc_position *position) {
buffer_write32be(&buf[0], position->point.x); sc_write32be(&buf[0], position->point.x);
buffer_write32be(&buf[4], position->point.y); sc_write32be(&buf[4], position->point.y);
buffer_write16be(&buf[8], position->screen_size.width); sc_write16be(&buf[8], position->screen_size.width);
buffer_write16be(&buf[10], position->screen_size.height); sc_write16be(&buf[10], position->screen_size.height);
} }
// write length (4 bytes) + string (non null-terminated) // write length (4 bytes) + string (non null-terminated)
static size_t static size_t
write_string(const char *utf8, size_t max_len, unsigned char *buf) { write_string(const char *utf8, size_t max_len, unsigned char *buf) {
size_t len = sc_str_utf8_truncation_index(utf8, max_len); size_t len = sc_str_utf8_truncation_index(utf8, max_len);
buffer_write32be(buf, len); sc_write32be(buf, len);
memcpy(&buf[4], utf8, len); memcpy(&buf[4], utf8, len);
return 4 + len; return 4 + len;
} }
@ -94,9 +94,9 @@ sc_control_msg_serialize(const struct sc_control_msg *msg, unsigned char *buf) {
switch (msg->type) { switch (msg->type) {
case SC_CONTROL_MSG_TYPE_INJECT_KEYCODE: case SC_CONTROL_MSG_TYPE_INJECT_KEYCODE:
buf[1] = msg->inject_keycode.action; buf[1] = msg->inject_keycode.action;
buffer_write32be(&buf[2], msg->inject_keycode.keycode); sc_write32be(&buf[2], msg->inject_keycode.keycode);
buffer_write32be(&buf[6], msg->inject_keycode.repeat); sc_write32be(&buf[6], msg->inject_keycode.repeat);
buffer_write32be(&buf[10], msg->inject_keycode.metastate); sc_write32be(&buf[10], msg->inject_keycode.metastate);
return 14; return 14;
case SC_CONTROL_MSG_TYPE_INJECT_TEXT: { case SC_CONTROL_MSG_TYPE_INJECT_TEXT: {
size_t len = size_t len =
@ -106,20 +106,20 @@ sc_control_msg_serialize(const struct sc_control_msg *msg, unsigned char *buf) {
} }
case SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT: case SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT:
buf[1] = msg->inject_touch_event.action; buf[1] = msg->inject_touch_event.action;
buffer_write64be(&buf[2], msg->inject_touch_event.pointer_id); sc_write64be(&buf[2], msg->inject_touch_event.pointer_id);
write_position(&buf[10], &msg->inject_touch_event.position); write_position(&buf[10], &msg->inject_touch_event.position);
uint16_t pressure = uint16_t pressure =
to_fixed_point_16(msg->inject_touch_event.pressure); to_fixed_point_16(msg->inject_touch_event.pressure);
buffer_write16be(&buf[22], pressure); sc_write16be(&buf[22], pressure);
buffer_write32be(&buf[24], msg->inject_touch_event.buttons); sc_write32be(&buf[24], msg->inject_touch_event.buttons);
return 28; return 28;
case SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT: case SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
write_position(&buf[1], &msg->inject_scroll_event.position); write_position(&buf[1], &msg->inject_scroll_event.position);
buffer_write32be(&buf[13], sc_write32be(&buf[13],
(uint32_t) msg->inject_scroll_event.hscroll); (uint32_t) msg->inject_scroll_event.hscroll);
buffer_write32be(&buf[17], sc_write32be(&buf[17],
(uint32_t) msg->inject_scroll_event.vscroll); (uint32_t) msg->inject_scroll_event.vscroll);
buffer_write32be(&buf[21], msg->inject_scroll_event.buttons); sc_write32be(&buf[21], msg->inject_scroll_event.buttons);
return 25; return 25;
case SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON: case SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
buf[1] = msg->inject_keycode.action; buf[1] = msg->inject_keycode.action;
@ -128,7 +128,7 @@ sc_control_msg_serialize(const struct sc_control_msg *msg, unsigned char *buf) {
buf[1] = msg->get_clipboard.copy_key; buf[1] = msg->get_clipboard.copy_key;
return 2; return 2;
case SC_CONTROL_MSG_TYPE_SET_CLIPBOARD: case SC_CONTROL_MSG_TYPE_SET_CLIPBOARD:
buffer_write64be(&buf[1], msg->set_clipboard.sequence); sc_write64be(&buf[1], msg->set_clipboard.sequence);
buf[9] = !!msg->set_clipboard.paste; buf[9] = !!msg->set_clipboard.paste;
size_t len = write_string(msg->set_clipboard.text, size_t len = write_string(msg->set_clipboard.text,
SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH, SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,

View file

@ -46,8 +46,8 @@ sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
return false; return false;
} }
uint64_t pts_flags = buffer_read64be(header); uint64_t pts_flags = sc_read64be(header);
uint32_t len = buffer_read32be(&header[8]); uint32_t len = sc_read32be(&header[8]);
assert(len); assert(len);
if (av_new_packet(packet, len)) { if (av_new_packet(packet, len)) {

View file

@ -18,7 +18,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
msg->type = buf[0]; msg->type = buf[0];
switch (msg->type) { switch (msg->type) {
case DEVICE_MSG_TYPE_CLIPBOARD: { case DEVICE_MSG_TYPE_CLIPBOARD: {
size_t clipboard_len = buffer_read32be(&buf[1]); size_t clipboard_len = sc_read32be(&buf[1]);
if (clipboard_len > len - 5) { if (clipboard_len > len - 5) {
return 0; // not available return 0; // not available
} }
@ -36,7 +36,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
return 5 + clipboard_len; return 5 + clipboard_len;
} }
case DEVICE_MSG_TYPE_ACK_CLIPBOARD: { case DEVICE_MSG_TYPE_ACK_CLIPBOARD: {
uint64_t sequence = buffer_read64be(&buf[1]); uint64_t sequence = sc_read64be(&buf[1]);
msg->ack_clipboard.sequence = sequence; msg->ack_clipboard.sequence = sequence;
return 9; return 9;
} }

View file

@ -1,5 +1,5 @@
#ifndef BUFFER_UTIL_H #ifndef SC_BUFFER_UTIL_H
#define BUFFER_UTIL_H #define SC_BUFFER_UTIL_H
#include "common.h" #include "common.h"
@ -7,13 +7,13 @@
#include <stdint.h> #include <stdint.h>
static inline void static inline void
buffer_write16be(uint8_t *buf, uint16_t value) { sc_write16be(uint8_t *buf, uint16_t value) {
buf[0] = value >> 8; buf[0] = value >> 8;
buf[1] = value; buf[1] = value;
} }
static inline void static inline void
buffer_write32be(uint8_t *buf, uint32_t value) { sc_write32be(uint8_t *buf, uint32_t value) {
buf[0] = value >> 24; buf[0] = value >> 24;
buf[1] = value >> 16; buf[1] = value >> 16;
buf[2] = value >> 8; buf[2] = value >> 8;
@ -21,25 +21,25 @@ buffer_write32be(uint8_t *buf, uint32_t value) {
} }
static inline void static inline void
buffer_write64be(uint8_t *buf, uint64_t value) { sc_write64be(uint8_t *buf, uint64_t value) {
buffer_write32be(buf, value >> 32); sc_write32be(buf, value >> 32);
buffer_write32be(&buf[4], (uint32_t) value); sc_write32be(&buf[4], (uint32_t) value);
} }
static inline uint16_t static inline uint16_t
buffer_read16be(const uint8_t *buf) { sc_read16be(const uint8_t *buf) {
return (buf[0] << 8) | buf[1]; return (buf[0] << 8) | buf[1];
} }
static inline uint32_t static inline uint32_t
buffer_read32be(const uint8_t *buf) { sc_read32be(const uint8_t *buf) {
return ((uint32_t) buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; return ((uint32_t) buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
} }
static inline uint64_t static inline uint64_t
buffer_read64be(const uint8_t *buf) { sc_read64be(const uint8_t *buf) {
uint32_t msb = buffer_read32be(buf); uint32_t msb = sc_read32be(buf);
uint32_t lsb = buffer_read32be(&buf[4]); uint32_t lsb = sc_read32be(&buf[4]);
return ((uint64_t) msb << 32) | lsb; return ((uint64_t) msb << 32) | lsb;
} }

View file

@ -8,7 +8,7 @@ static void test_buffer_write16be(void) {
uint16_t val = 0xABCD; uint16_t val = 0xABCD;
uint8_t buf[2]; uint8_t buf[2];
buffer_write16be(buf, val); sc_write16be(buf, val);
assert(buf[0] == 0xAB); assert(buf[0] == 0xAB);
assert(buf[1] == 0xCD); assert(buf[1] == 0xCD);
@ -18,7 +18,7 @@ static void test_buffer_write32be(void) {
uint32_t val = 0xABCD1234; uint32_t val = 0xABCD1234;
uint8_t buf[4]; uint8_t buf[4];
buffer_write32be(buf, val); sc_write32be(buf, val);
assert(buf[0] == 0xAB); assert(buf[0] == 0xAB);
assert(buf[1] == 0xCD); assert(buf[1] == 0xCD);
@ -30,7 +30,7 @@ static void test_buffer_write64be(void) {
uint64_t val = 0xABCD1234567890EF; uint64_t val = 0xABCD1234567890EF;
uint8_t buf[8]; uint8_t buf[8];
buffer_write64be(buf, val); sc_write64be(buf, val);
assert(buf[0] == 0xAB); assert(buf[0] == 0xAB);
assert(buf[1] == 0xCD); assert(buf[1] == 0xCD);
@ -45,7 +45,7 @@ static void test_buffer_write64be(void) {
static void test_buffer_read16be(void) { static void test_buffer_read16be(void) {
uint8_t buf[2] = {0xAB, 0xCD}; uint8_t buf[2] = {0xAB, 0xCD};
uint16_t val = buffer_read16be(buf); uint16_t val = sc_read16be(buf);
assert(val == 0xABCD); assert(val == 0xABCD);
} }
@ -53,7 +53,7 @@ static void test_buffer_read16be(void) {
static void test_buffer_read32be(void) { static void test_buffer_read32be(void) {
uint8_t buf[4] = {0xAB, 0xCD, 0x12, 0x34}; uint8_t buf[4] = {0xAB, 0xCD, 0x12, 0x34};
uint32_t val = buffer_read32be(buf); uint32_t val = sc_read32be(buf);
assert(val == 0xABCD1234); assert(val == 0xABCD1234);
} }
@ -62,7 +62,7 @@ static void test_buffer_read64be(void) {
uint8_t buf[8] = {0xAB, 0xCD, 0x12, 0x34, uint8_t buf[8] = {0xAB, 0xCD, 0x12, 0x34,
0x56, 0x78, 0x90, 0xEF}; 0x56, 0x78, 0x90, 0xEF};
uint64_t val = buffer_read64be(buf); uint64_t val = sc_read64be(buf);
assert(val == 0xABCD1234567890EF); assert(val == 0xABCD1234567890EF);
} }