Skip to content

Commit

Permalink
update avahi txt data conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
abtink committed Sep 6, 2023
1 parent 84f6867 commit bbbd0a2
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions src/mdns/mdns_avahi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,48 +798,40 @@ otbrError PublisherAvahi::TxtDataToAvahiStringList(const TxtData &aTxtData,
size_t used = 0;
AvahiStringList *last = nullptr;
AvahiStringList *curr = aBuffer;
TxtList txtList;

// TODO: Change this and simplify this...
SuccessOrExit(error = DecodeTxtData(txtList, aTxtData.data(), aTxtData.size()));
const uint8_t *next;
const uint8_t *data = aTxtData.data();
const uint8_t *dataEnd = aTxtData.data() + aTxtData.size();

aHead = nullptr;
for (const auto &txtEntry : txtList)

while (data < dataEnd)
{
const char *key = txtEntry.mKey.c_str();
size_t keyLength = txtEntry.mKey.length();
const uint8_t *value = txtEntry.mValue.data();
size_t valueLength = txtEntry.mValue.size();
size_t needed = sizeof(AvahiStringList) - sizeof(AvahiStringList::text) + keyLength;
const uint8_t *next;

if (!txtEntry.mIsBooleanAttribute)
uint8_t entryLength = *data++;
size_t needed = sizeof(AvahiStringList) - sizeof(AvahiStringList::text) + entryLength;

if (entryLength == 0)
{
needed += valueLength + 1; // +1 is for `=` character.
continue;
}

VerifyOrExit(data + entryLength <= dataEnd, error = OTBR_ERROR_PARSE);

VerifyOrExit(used + needed <= aBufferSize, error = OTBR_ERROR_INVALID_ARGS);
curr->next = last;
last = curr;
memcpy(curr->text, key, keyLength);

if (!txtEntry.mIsBooleanAttribute)
{
curr->text[keyLength] = '=';
memcpy(curr->text + keyLength + 1, value, valueLength);
curr->size = keyLength + valueLength + 1;
}
else
{
curr->size = keyLength;
}
memcpy(curr->text, data, entryLength);
curr->size = entryLength;

data += entryLength;

next = curr->text + curr->size;
curr = OTBR_ALIGNED(next, AvahiStringList *);
used = static_cast<size_t>(reinterpret_cast<uint8_t *>(curr) - reinterpret_cast<uint8_t *>(aBuffer));
}
SuccessOrExit(error);

aHead = last;

exit:
return error;
}
Expand Down

0 comments on commit bbbd0a2

Please sign in to comment.