static char* OCStringLLJoin(OCStringLL* val) { OCStringLL* temp = val; size_t size = strlen(temp->value); while (temp->next) { ++size; temp = temp->next; size += strlen(temp->value); } char* joinedStr = (char*)OICCalloc(sizeof(char), size + 1); if (!joinedStr) { return NULL; } OICStrcat(joinedStr, size + 1, val->value); while (val->next) { val = val->next; OICStrcat(joinedStr, size + 1, " "); OICStrcat(joinedStr, size + 1, val->value); } return joinedStr; }
OCEntityHandlerResult getDoubleValueFromString (const char *str, double *outDouble) { size_t hexOutValSize = strlen(HexPrepend) + strlen(str) + 1; char * hexOutVal = (char *) OICCalloc(1, hexOutValSize); if(!hexOutVal) { return OC_EH_ERROR; } OICStrcpy(hexOutVal, hexOutValSize, HexPrepend); OICStrcat(hexOutVal, hexOutValSize, str); char *endPtr = NULL; errno = 0; double value = strtod(hexOutVal, &endPtr); if(errno != 0 || *endPtr != 0 || value == HUGE_VALF || value == HUGE_VALL) { OICFree(hexOutVal); return OC_EH_ERROR; } OICFree(hexOutVal); *outDouble = value; return OC_EH_OK; }
// Tests a cat where the Destination is zero length TEST(StringTests, StrcatZeroDestination) { char target[0]; char source[] = "12345"; char *result = OICStrcat(target, sizeof(target), source); EXPECT_EQ(target, result); }
// Tests a normal cat where the target has zero room TEST(StringTests, StrcatZeroRoom) { char target[10] = "Original1"; char source[] = "12345"; char *result = OICStrcat(target, sizeof(target), source); EXPECT_EQ(target, result); EXPECT_EQ(sizeof(target) - 1, strlen(target)); EXPECT_STREQ("Original1", target); }
// Tests a normal cat where the target has exactly enough room TEST(StringTests, StrcatExactSize) { char target[10] = "Orig"; memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig")); char source[] = "12345"; char *result = OICStrcat(target, sizeof(target), source); EXPECT_EQ(target, result); EXPECT_EQ(sizeof(target) - 1, strlen(target)); EXPECT_STREQ("Orig12345", target); }
// Tests a normal cat where the target has insufficient room TEST(StringTests, StrcatInsufficientRoom) { char target[10]; memset(target, SENTINEL_VALUE, sizeof(target)); target[0] = '\0'; char source[] = "1234567890123456"; char *result = OICStrcat(target, sizeof(target), source); EXPECT_EQ(target, result); EXPECT_EQ(sizeof(target) - 1, strlen(target)); EXPECT_STREQ("123456789", target); }
// tests a normal cat where the target has exactly enough room, // except it is of strlen 0 TEST(StringTests, StrcatExactSizeEmptySourceString) { char target[10]; memset(target, SENTINEL_VALUE, sizeof(target)); target[0] = '\0'; char source[] = "123456789"; char *result = OICStrcat(target, sizeof(target), source); EXPECT_EQ(target, result); EXPECT_EQ(sizeof(target) - 1, strlen(target)); EXPECT_STREQ(source, target); }
// Tests a cat where the Destination is zero length // Tests with what is in reality an oversized buffer to ensure that // the buffer isn't over-written TEST(StringTests, StrcatZeroDestinationSentinel) { char target[0 + 5]; memset(target, SENTINEL_VALUE, sizeof(target)); char source[] = "123456789"; char *result = OICStrcat(target, sizeof(target) - 5, source); EXPECT_EQ(target, result); for(size_t i = 0; i < sizeof(target); ++i) { EXPECT_EQ(SENTINEL_VALUE, result[i]); } }
// Tests a cat where the source is zero length TEST(StringTests, StrcatZeroSource) { char target[10] = "Orig"; memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig")); char source[] = ""; char *result = OICStrcat(target, sizeof(target), source); EXPECT_EQ(target, result); EXPECT_EQ(sizeof("Orig") - 1, strlen(target)); EXPECT_STREQ("Orig", target); for(size_t i = sizeof("Orig"); i < sizeof(target); ++i) { EXPECT_EQ(SENTINEL_VALUE, result[i]); } }
// Tests a normal cat where the target has zero room // Tests with what is in reality an oversized buffer to ensure that // the buffer isn't over-written TEST(StringTests, StrcatZeroRoomSentinel) { char target[10 + 5] = "Original1"; memset(target + sizeof("Original1"), SENTINEL_VALUE, sizeof(target) - sizeof("Original1")); char source[] = "12345"; char *result = OICStrcat(target, sizeof(target) - 5, source); EXPECT_EQ(target, result); EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target)); EXPECT_STREQ("Original1", target); for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i) { EXPECT_EQ(SENTINEL_VALUE, result[i]); } }
// tests a normal cat where the target has extra room TEST(StringTests, StrcatExtraRoom) { char target[10] = "Orig"; memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig")); char source[] = "12"; char *result = OICStrcat(target, sizeof(target), source); EXPECT_EQ(target, result); EXPECT_EQ(static_cast<size_t>(6), strlen(target)); EXPECT_STREQ("Orig12", target); for(size_t i = sizeof("Orig12"); i < sizeof(target); ++i) { EXPECT_EQ(SENTINEL_VALUE, result[i]); } }
// Tests a normal cat where the target has insufficient room // Tests with what is in reality an oversized buffer to ensure that // the buffer isn't over-written TEST(StringTests, StrcatInsufficientRoomSentinel) { char target[10 + 5]; memset(target, SENTINEL_VALUE, sizeof(target)); target[0]= '\0'; char source[] = "1234567890123456"; char *result = OICStrcat(target, sizeof(target) - 5, source); EXPECT_EQ(target, result); EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target)); EXPECT_STREQ("123456789", target); for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i) { EXPECT_EQ(SENTINEL_VALUE, result[i]); } }
// tests a normal cat where the target has exactly enough room, // except it is of strlen 0 // Tests with what is in reality an oversized buffer to ensure that // the buffer isn't over-written TEST(StringTests, StrcatExactSizeEmptySourceStringSentinel) { char target[10 + 5]; memset(target, SENTINEL_VALUE, sizeof(target)); target[0] = '\0'; char source[] = "123456789"; char *result = OICStrcat(target, sizeof(target) + 5, source); EXPECT_EQ(target, result); EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target)); EXPECT_STREQ(source, target); for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i) { EXPECT_EQ(SENTINEL_VALUE, result[i]); } }
coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL_RET(info, TAG, "info", NULL); VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL); coap_pdu_t *pdu = NULL; // RESET have to use only 4byte (empty message) // and ACKNOWLEDGE can use empty message when code is empty. if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type)) { OIC_LOG(DEBUG, TAG, "code is empty"); if (!(pdu = CAGeneratePDUImpl((code_t) code, NULL, info, endpoint))) { OIC_LOG(ERROR, TAG, "pdu NULL"); return NULL; } } else { coap_list_t *optlist = NULL; if (CA_MSG_ACKNOWLEDGE != info->type) { const char *uri = info->resourceUri; if (NULL == uri) { OIC_LOG(ERROR, TAG, "uri NULL"); return NULL; } uint32_t length = strlen(uri); if (CA_MAX_URI_LENGTH < length) { OIC_LOG(ERROR, TAG, "URI len err"); return NULL; } uint32_t uriLength = length + sizeof(COAP_URI_HEADER); char *coapUri = (char *) OICCalloc(1, uriLength); if (NULL == coapUri) { OIC_LOG(ERROR, TAG, "out of memory"); return NULL; } OICStrcat(coapUri, uriLength, COAP_URI_HEADER); OICStrcat(coapUri, uriLength, uri); // parsing options in URI CAResult_t res = CAParseURI(coapUri, &optlist); if (CA_STATUS_OK != res) { if (optlist) { coap_delete_list(optlist); } OICFree(coapUri); return NULL; } OICFree(coapUri); } // parsing options in HeadOption CAResult_t ret = CAParseHeadOption(code, info, &optlist); if (CA_STATUS_OK != ret) { coap_delete_list(optlist); return NULL; } pdu = CAGeneratePDUImpl((code_t) code, optlist, info, endpoint); if (NULL == pdu) { OIC_LOG(ERROR, TAG, "pdu NULL"); coap_delete_list(optlist); return NULL; } // free option list coap_delete_list(optlist); } // pdu print method : coap_show_pdu(pdu); OIC_LOG(DEBUG, TAG, "OUT"); return pdu; }
OCStackResult buildURI(char ** output, const char * prefix, const char * nodeId, const char * endpointId, const char * clusterId) { if(!output || !prefix || !nodeId || !endpointId || !clusterId) { return OC_STACK_INVALID_PARAM; } const char LEN_SEPARATOR[] = "/"; size_t lenSeparatorSize = sizeof(LEN_SEPARATOR) - 1; size_t newUriSize = strlen(prefix) + lenSeparatorSize + strlen(nodeId) + lenSeparatorSize + strlen(endpointId) + lenSeparatorSize + strlen(clusterId) + 1; // NULL Terminator *output = (char *) OICCalloc(1, newUriSize); if (!*output) { OC_LOG (ERROR, TAG, "Out of memory"); return OC_STACK_NO_MEMORY; } char * temp = OICStrcpy(*output, newUriSize, prefix); if(temp != *output) { goto exit; } temp = OICStrcat(*output, newUriSize, LEN_SEPARATOR); if(temp != *output) { goto exit; } temp = OICStrcat(*output, newUriSize, nodeId); if(temp != *output) { goto exit; } temp = OICStrcat(*output, newUriSize, LEN_SEPARATOR); if(temp != *output) { goto exit; } temp = OICStrcat(*output, newUriSize, endpointId); if(temp != *output) { goto exit; } temp = OICStrcat(*output, newUriSize, LEN_SEPARATOR); if(temp != *output) { goto exit; } temp = OICStrcat(*output, newUriSize, clusterId); if(temp != *output) { goto exit; } return OC_STACK_OK; exit: OICFree(*output); *output = NULL; return OC_STACK_NO_MEMORY; }
coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint, coap_list_t **optlist, coap_transport_t *transport) { VERIFY_NON_NULL_RET(info, TAG, "info", NULL); VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL); VERIFY_NON_NULL_RET(optlist, TAG, "optlist", NULL); OIC_LOG_V(DEBUG, TAG, "generate pdu for [%d]adapter, [%d]flags", endpoint->adapter, endpoint->flags); coap_pdu_t *pdu = NULL; // RESET have to use only 4byte (empty message) // and ACKNOWLEDGE can use empty message when code is empty. if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type)) { if (CA_EMPTY != code) { OIC_LOG(ERROR, TAG, "reset is not empty message"); return NULL; } if (info->payloadSize > 0 || info->payload || info->token || info->tokenLength > 0) { OIC_LOG(ERROR, TAG, "Empty message has unnecessary data after messageID"); return NULL; } OIC_LOG(DEBUG, TAG, "code is empty"); if (!(pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, NULL, transport))) { OIC_LOG(ERROR, TAG, "pdu NULL"); return NULL; } } else { if (info->resourceUri) { OIC_LOG_V(DEBUG, TAG, "uri : %s", info->resourceUri); uint32_t length = strlen(info->resourceUri); if (CA_MAX_URI_LENGTH < length) { OIC_LOG(ERROR, TAG, "URI len err"); return NULL; } uint32_t uriLength = length + sizeof(COAP_URI_HEADER); char *coapUri = (char *) OICCalloc(1, uriLength); if (NULL == coapUri) { OIC_LOG(ERROR, TAG, "out of memory"); return NULL; } OICStrcat(coapUri, uriLength, COAP_URI_HEADER); OICStrcat(coapUri, uriLength, info->resourceUri); // parsing options in URI CAResult_t res = CAParseURI(coapUri, optlist); if (CA_STATUS_OK != res) { OICFree(coapUri); return NULL; } OICFree(coapUri); } // parsing options in HeadOption CAResult_t ret = CAParseHeadOption(code, info, optlist); if (CA_STATUS_OK != ret) { return NULL; } pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, *optlist, transport); if (NULL == pdu) { OIC_LOG(ERROR, TAG, "pdu NULL"); return NULL; } } // pdu print method : coap_show_pdu(pdu); return pdu; }