Commit 840fcb3d authored by Derek Konigsberg's avatar Derek Konigsberg

Replace usage of strndup as it is not available everywhere

Implemented internal utility functions for converting between a
null terminated string and a protobuf binary data structure.
parent 32426261
......@@ -477,6 +477,29 @@ int axolotl_constant_memcmp(const void *s1, const void *s2, size_t n)
return result;
}
void axolotl_str_serialize_protobuf(ProtobufCBinaryData *buffer, const char *str)
{
assert(buffer);
assert(str);
buffer->data = (uint8_t *)str;
buffer->len = strlen(str);
}
char *axolotl_str_deserialize_protobuf(ProtobufCBinaryData *buffer)
{
assert(buffer);
char *str = malloc(buffer->len + 1);
if(!str) {
return 0;
}
memcpy(str, buffer->data, buffer->len);
str[buffer->len] = '\0';
return str;
}
/*------------------------------------------------------------------------*/
int axolotl_store_context_create(axolotl_store_context **context, axolotl_context *global_context)
......
......@@ -76,4 +76,7 @@ int sender_key_state_serialize_prepare(sender_key_state *state, Textsecure__Send
void sender_key_state_serialize_prepare_free(Textsecure__SenderKeyStateStructure *state_structure);
int sender_key_state_deserialize_protobuf(sender_key_state **state, Textsecure__SenderKeyStateStructure *state_structure, axolotl_context *global_context);
void axolotl_str_serialize_protobuf(ProtobufCBinaryData *buffer, const char *str);
char *axolotl_str_deserialize_protobuf(ProtobufCBinaryData *buffer);
#endif /* AXOLOTL_INTERNAL_H */
......@@ -418,8 +418,7 @@ int scannable_fingerprint_serialize(axolotl_buffer **buffer, const scannable_fin
combined_fingerprint.has_version = 1;
if(scannable->local_stable_identifier && scannable->local_identity_key) {
local_fingerprint.identifier.data = (uint8_t *)scannable->local_stable_identifier;
local_fingerprint.identifier.len = strlen(scannable->local_stable_identifier);
axolotl_str_serialize_protobuf(&local_fingerprint.identifier, scannable->local_stable_identifier);
local_fingerprint.has_identifier = 1;
result = ec_public_key_serialize_protobuf(&local_fingerprint.publickey, scannable->local_identity_key);
......@@ -432,8 +431,7 @@ int scannable_fingerprint_serialize(axolotl_buffer **buffer, const scannable_fin
}
if(scannable->remote_stable_identifier && scannable->remote_identity_key) {
remote_fingerprint.identifier.data = (uint8_t *)scannable->remote_stable_identifier;
remote_fingerprint.identifier.len = strlen(scannable->remote_stable_identifier);
axolotl_str_serialize_protobuf(&remote_fingerprint.identifier, scannable->remote_stable_identifier);
remote_fingerprint.has_identifier = 1;
result = ec_public_key_serialize_protobuf(&remote_fingerprint.publickey, scannable->remote_identity_key);
......@@ -497,9 +495,7 @@ int scannable_fingerprint_deserialize(scannable_fingerprint **scannable, const u
if(combined_fingerprint->localfingerprint) {
if(combined_fingerprint->localfingerprint->has_identifier) {
local_stable_identifier = strndup(
(char *)combined_fingerprint->localfingerprint->identifier.data,
combined_fingerprint->localfingerprint->identifier.len);
local_stable_identifier = axolotl_str_deserialize_protobuf(&combined_fingerprint->localfingerprint->identifier);
if(!local_stable_identifier) {
result = AX_ERR_NOMEM;
goto complete;
......@@ -518,9 +514,7 @@ int scannable_fingerprint_deserialize(scannable_fingerprint **scannable, const u
if(combined_fingerprint->remotefingerprint) {
if(combined_fingerprint->remotefingerprint->has_identifier) {
remote_stable_identifier = strndup(
(char *)combined_fingerprint->remotefingerprint->identifier.data,
combined_fingerprint->remotefingerprint->identifier.len);
remote_stable_identifier = axolotl_str_deserialize_protobuf(&combined_fingerprint->remotefingerprint->identifier);
if(!remote_stable_identifier) {
result = AX_ERR_NOMEM;
goto complete;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment