Skip to content

Commit

Permalink
[mdns-avahi] simplify TXT data conversion to AvahiStringList
Browse files Browse the repository at this point in the history
This commit updates `TxtDataToAvahiStringList()` to directly
use the encoded TXT data entries.
  • Loading branch information
abtink committed Sep 6, 2023
1 parent d3314fb commit 53271eb
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/mdns/mdns_avahi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,47 +798,40 @@ otbrError PublisherAvahi::TxtDataToAvahiStringList(const TxtData &aTxtData,
size_t used = 0;
AvahiStringList *last = nullptr;
AvahiStringList *curr = aBuffer;
TxtList txtList;

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 53271eb

Please sign in to comment.