Skip to content

Commit

Permalink
Merge pull request eclipse-wakaama#321 from eclipse/fix_320
Browse files Browse the repository at this point in the history
Fix for eclipse-wakaama#320: only opaque data can be encoded in opaque.
  • Loading branch information
dnav committed Jul 12, 2017
2 parents 69df50f + 8549000 commit 8a90b89
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
24 changes: 21 additions & 3 deletions core/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ static int prv_textSerialize(lwm2m_data_t * dataP,
}

case LWM2M_TYPE_OPAQUE:
{
size_t length;

length = utils_base64GetSize(dataP->value.asBuffer.length);
*bufferP = (uint8_t *)lwm2m_malloc(length);
if (*bufferP == NULL) return 0;
length = utils_base64Encode(dataP->value.asBuffer.buffer, dataP->value.asBuffer.length, *bufferP, length);
if (length == 0)
{
lwm2m_free(*bufferP);
*bufferP = NULL;
return 0;
}
return (int)length;
}

case LWM2M_TYPE_UNDEFINED:
default:
return -1;
Expand Down Expand Up @@ -586,11 +602,13 @@ int lwm2m_data_serialize(lwm2m_uri_t * uriP,
}
}

if (*formatP == LWM2M_CONTENT_TEXT
&& dataP->type == LWM2M_TYPE_OPAQUE)
if (*formatP == LWM2M_CONTENT_OPAQUE
&& dataP->type != LWM2M_TYPE_OPAQUE)
{
*formatP = LWM2M_CONTENT_OPAQUE;
LOG("Opaque format is reserved to opaque resources.");
return -1;
}

LOG_ARG("Final format: %s", STR_MEDIA_TYPE(*formatP));

switch (*formatP)
Expand Down
1 change: 1 addition & 0 deletions core/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ size_t utils_floatToText(double data, uint8_t * string, size_t length);
int utils_textToInt(uint8_t * buffer, int length, int64_t * dataP);
int utils_textToFloat(uint8_t * buffer, int length, double * dataP);
void utils_copyValue(void * dst, const void * src, size_t len);
size_t utils_base64GetSize(size_t dataLen);
size_t utils_base64Encode(uint8_t * dataP, size_t dataLen, uint8_t * bufferP, size_t bufferLen);
#ifdef LWM2M_CLIENT_MODE
lwm2m_server_t * utils_findServer(lwm2m_context_t * contextP, void * fromSessionH);
Expand Down
4 changes: 2 additions & 2 deletions core/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ static void prv_encodeBlock(uint8_t input[3],
output[3] = b64Alphabet[input[2] & 0x3F];
}

static size_t prv_getBase64Size(size_t dataLen)
size_t utils_base64GetSize(size_t dataLen)
{
size_t result_len;

Expand All @@ -502,7 +502,7 @@ size_t utils_base64Encode(uint8_t * dataP,
unsigned int result_index;
size_t result_len;

result_len = prv_getBase64Size(dataLen);
result_len = utils_base64GetSize(dataLen);

if (result_len > bufferLen) return 0;

Expand Down

0 comments on commit 8a90b89

Please sign in to comment.