Skip to content

Commit

Permalink
[jsontlv] Add a prefix for octet strings encoded as base64 (#23889)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and pull[bot] committed Jul 8, 2023
1 parent 647b9f1 commit 243606f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/lib/support/jsontlv/TlvJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ struct KeyContext
//
static constexpr uint16_t kMaxStringLen = 1280;

constexpr const char kBase64Header[] = "base64:";
constexpr size_t kBase64HeaderLen = ArraySize(kBase64Header) - 1;

namespace chip {

/*
Expand Down Expand Up @@ -152,10 +155,15 @@ CHIP_ERROR TlvToJson(TLV::TLVReader & reader, KeyContext context, Json::Value &
VerifyOrReturnError(span.size() < kMaxStringLen, CHIP_ERROR_INVALID_TLV_ELEMENT);

Platform::ScopedMemoryBuffer<char> byteString;
byteString.Alloc(BASE64_ENCODED_LEN(span.size()) + 1);
byteString.Alloc(kBase64HeaderLen + BASE64_ENCODED_LEN(span.size()) + 1);
VerifyOrReturnError(byteString.Get() != nullptr, CHIP_ERROR_NO_MEMORY);

auto encodedLen = Base64Encode(span.data(), span.size(), byteString.Get());
auto encodedLen = Base64Encode(span.data(), span.size(), byteString.Get() + kBase64HeaderLen);
if (encodedLen)
{
memcpy(byteString.Get(), kBase64Header, kBase64HeaderLen);
encodedLen += kBase64HeaderLen;
}
byteString.Get()[encodedLen] = '\0';

InsertKeyValue(parent, context, byteString.Get());
Expand Down
8 changes: 4 additions & 4 deletions src/lib/support/tests/TestTlvToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void TestConverter(nlTestSuite * inSuite, void * inContext)
ByteSpan byteSpan(byteBuf);
EncodeAndValidate(byteSpan,
"{\n"
" \"value\" : \"AQIDBP/+mYjdzQ==\"\n"
" \"value\" : \"base64:AQIDBP/+mYjdzQ==\"\n"
"}\n");

DataModel::Nullable<uint8_t> nullValue;
Expand All @@ -170,7 +170,7 @@ void TestConverter(nlTestSuite * inSuite, void * inContext)
" \"0\" : 20,\n"
" \"1\" : true,\n"
" \"2\" : 0,\n"
" \"3\" : \"AQIDBP/+mYjdzQ==\",\n"
" \"3\" : \"base64:AQIDBP/+mYjdzQ==\",\n"
" \"4\" : \"hello\",\n"
" \"5\" : 0,\n"
" \"6\" : 1.0,\n"
Expand Down Expand Up @@ -200,7 +200,7 @@ void TestConverter(nlTestSuite * inSuite, void * inContext)
" \"0\" : 20,\n"
" \"1\" : true,\n"
" \"2\" : 0,\n"
" \"3\" : \"AQIDBP/+mYjdzQ==\",\n"
" \"3\" : \"base64:AQIDBP/+mYjdzQ==\",\n"
" \"4\" : \"hello\",\n"
" \"5\" : 0,\n"
" \"6\" : 1.0,\n"
Expand All @@ -210,7 +210,7 @@ void TestConverter(nlTestSuite * inSuite, void * inContext)
" \"0\" : 20,\n"
" \"1\" : true,\n"
" \"2\" : 0,\n"
" \"3\" : \"AQIDBP/+mYjdzQ==\",\n"
" \"3\" : \"base64:AQIDBP/+mYjdzQ==\",\n"
" \"4\" : \"hello\",\n"
" \"5\" : 0,\n"
" \"6\" : 1.0,\n"
Expand Down

0 comments on commit 243606f

Please sign in to comment.