improve string handling

This commit is contained in:
hippoz 2022-12-26 21:13:09 +02:00
parent 3ea788d1a7
commit dd62b8570a
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
3 changed files with 41 additions and 58 deletions

View file

@ -12,9 +12,9 @@
char *string_dup(const char *s)
{
size_t len = strlen(s) + 1;
char *p = malloc(len);
return p ? memcpy(p, s, len) : NULL;
size_t len = strlen(s) + 1;
char *p = malloc(len);
return p ? memcpy(p, s, len) : NULL;
}
@ -266,7 +266,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
printf("assigned unique name '%s' to connection %d\n", s->names[unique_name_index].name, i);
} else if (strcmp(member, "RequestName") == 0) {
char *name = TRYPTR(wire_get_string(&ctx, NULL));
char *name = TRYPTR(wire_get_string(&ctx));
int name_len = strlen(name);
if (name_len < 1 || name_len > 256) {
return -1;
@ -293,7 +293,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
printf("client '%s' (index=%d) now owns name '%s'\n", s->names[client->unique_name_index].name, i, name_str);
}
} else if (strcmp(member, "GetNameOwner") == 0) {
char *name = TRYPTR(wire_get_string(&ctx, NULL));
char *name = TRYPTR(wire_get_string(&ctx));
int name_len = strlen(name);
if (name_len < 1 || name_len > 256) {
return -1;
@ -311,7 +311,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
TRYPTR(wire_set_string(&reply_ctx, s->names[target->unique_name_index].name));
} _reply_end()
} else if (strcmp(member, "NameHasOwner") == 0) {
char *name = TRYPTR(wire_get_string(&ctx, NULL));
char *name = TRYPTR(wire_get_string(&ctx));
int name_len = strlen(name);
if (name_len < 1 || name_len > 256) {
return -1;
@ -351,7 +351,7 @@ int jb_server_client_process_message(struct jb_server *s, int i, uint8_t *data,
} else if (strcmp(member, "StartServiceByName") == 0) {
// TODO: stub (does nothing and always returns success)
char *name = TRYPTR(wire_get_string(&ctx, NULL));
char *name = TRYPTR(wire_get_string(&ctx));
int name_len = strlen(name);
if (name_len < 1 || name_len > 256) {
return -1;
@ -487,7 +487,6 @@ int jb_server_turn(struct jb_server *s)
fprintf(stderr, "jb_server_turn: error: got POLLERR for sock_fd\n");
return -1;
}
fprintf(stderr, "jb_server_turn: warn: got POLLERR for fds[%d], removing...\n", i);
jb_server_client_remove(s, i);
continue;
}

60
wire.c
View file

@ -48,11 +48,13 @@ _WIRE_DEF_TYPE(i32, int32_t, 4);
_WIRE_DEF_TYPE(u32, uint32_t, 4);
_WIRE_DEF_TYPE(boolean, uint32_t, 4);
char *wire_get_string(wire_context_t *c, uint32_t *out_len)
char *wire_get_string_impl(wire_context_t *c, bool as_signature)
{
uint32_t len = *(uint32_t*)TRYPTR_NIL(wire_get_u32(c));
if (out_len) {
*out_len = len;
uint32_t len = 0;
if (as_signature) {
len = *(uint8_t*)TRYPTR_NIL(wire_get_u8(c));
} else {
len = *(uint32_t*)TRYPTR_NIL(wire_get_u32(c));
}
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
@ -67,49 +69,15 @@ char *wire_get_string(wire_context_t *c, uint32_t *out_len)
return str;
}
char *wire_set_string(wire_context_t *c, const char *str)
char *wire_set_string_impl(wire_context_t *c, const char *str, bool as_signature)
{
uint32_t len = strlen(str);
TRYPTR_NIL(wire_set_u32(c, len));
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
if (new_cursor >= c->data_len) {
return NULL;
if (as_signature) {
TRYPTR_NIL(wire_set_u8(c, len));
} else {
TRYPTR_NIL(wire_set_u32(c, len));
}
// TODO?
char *dest = (char*)&c->data[c->byte_cursor];
memcpy(dest, str, len + 1);
c->byte_cursor = new_cursor;
return dest;
}
char *wire_get_signature(wire_context_t *c, uint8_t *out_len)
{
uint8_t len = *(uint8_t*)TRYPTR_NIL(wire_get_u8(c));
if (out_len) {
*out_len = len;
}
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
if (new_cursor >= c->data_len) {
return NULL;
}
char *str = (char *)&c->data[c->byte_cursor];
c->byte_cursor = new_cursor;
return str;
}
char *wire_set_signature(wire_context_t *c, const char *str)
{
uint8_t len = strlen(str);
TRYPTR_NIL(wire_set_u8(c, len));
uint32_t new_cursor = c->byte_cursor + len + 1; /* + 1 because of the extra null termination that strings have */
if (new_cursor >= c->data_len) {
return NULL;
@ -146,19 +114,19 @@ int wire_parse_message(wire_context_t *c, wire_message_t *msg)
while (c->byte_cursor < header_fields_end) {
uint8_t field_code = *(uint8_t*)TRYPTR(wire_get_u8(c));
TRYPTR(wire_get_signature(c, NULL));
TRYPTR(wire_get_signature(c));
switch (field_code) {
case DBUS_HEADER_FIELD_PATH: /* through */
case DBUS_HEADER_FIELD_INTERFACE: /* through */
case DBUS_HEADER_FIELD_MEMBER: /* through */
case DBUS_HEADER_FIELD_DESTINATION: {
char *str = TRYPTR(wire_get_string(c, NULL));
char *str = TRYPTR(wire_get_string(c));
WIRE_VERBOSE("field: %s\n", str);
msg->fields[field_code].t.str = str;
} break;
case DBUS_HEADER_FIELD_SIGNATURE: {
char *str = TRYPTR(wire_get_signature(c, NULL));
char *str = TRYPTR(wire_get_signature(c));
WIRE_VERBOSE("field: %s\n", str);
msg->fields[field_code].t.str = str;
} break;

24
wire.h
View file

@ -122,13 +122,29 @@ _WIRE_DECL_TYPE(i32, int32_t, 4);
_WIRE_DECL_TYPE(u32, uint32_t, 4);
_WIRE_DECL_TYPE(boolean, uint32_t, 4);
char *wire_get_string(wire_context_t *c, uint32_t *out_len);
char *wire_set_string(wire_context_t *c, const char *str);
char *wire_get_signature(wire_context_t *c, uint8_t *out_len);
char *wire_set_signature(wire_context_t *c, const char *str);
char *wire_get_string_impl(wire_context_t *c, bool as_signature);
char *wire_set_string_impl(wire_context_t *c, const char *str, bool as_signature);
int wire_parse_message(wire_context_t *c, wire_message_t *msg);
int wire_compose_reply(wire_context_t *c, wire_message_t *msg, const char *signature, uint32_t **out_body_length);
int wire_compose_error(wire_context_t *c, wire_message_t *msg, const char *error_name);
int wire_compose_unicast_reply(wire_context_t *c, wire_context_t *msg_c, wire_message_t *msg, char *sender_unique_name);
static inline char *wire_get_signature(wire_context_t *c) {
return wire_get_string_impl(c, true);
}
static inline char *wire_set_signature(wire_context_t *c, const char *str) {
return wire_set_string_impl(c, str, true);
}
static inline char *wire_get_string(wire_context_t *c) {
return wire_get_string_impl(c, false);
}
static inline char *wire_set_string(wire_context_t *c, const char *str) {
return wire_set_string_impl(c, str, false);
}
#endif // _JITTERBUG__WIRE_H