Commit dbd17971 authored by Oleksandr Dmytrenko's avatar Oleksandr Dmytrenko Committed by Randeep

[IOT-2912] svr db editor

svr db editor refactoring

Bug: https://jira.iotivity.org/browse/IOT-2912
Change-Id: Id08cb65a06c296619bf81814bf4ca9490688e2c8
Signed-off-by: default avatarOleksandr Dmytrenko <o.dmytrenko@samsung.com>
parent bf1ea55f
......@@ -1635,22 +1635,19 @@ static cJSON_bool print_object(const cJSON * const item, const size_t depth, con
}
/* Get Array size/item / object item. */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
CJSON_PUBLIC(size_t) cJSON_GetArraySize(const cJSON *array)
{
cJSON *c = array->child;
size_t i = 0;
while(c)
while(c && i+1)
{
i++;
c = c->next;
}
/* FIXME: Can overflow here. Cannot be fixed without breaking the API */
return (int)i;
return (size_t)i;
}
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item)
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, size_t item)
{
cJSON *c = array ? array->child : NULL;
while (c && item > 0)
......
......@@ -139,9 +139,9 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, cons
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
CJSON_PUBLIC(size_t) cJSON_GetArraySize(const cJSON *array);
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item);
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, size_t item);
/* Get item "string" from object. Case insensitive. */
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
......
......@@ -109,11 +109,6 @@ static OCStackResult ReadBufferFromFile(const char *fileName, uint8_t **buffer,
ret = OC_STACK_OK;
*bufferSize = bytesRead + 1;
}
if (0 != fclose(fp))
{
OIC_LOG_V(ERROR, TAG, "Failed to close file \"%s\"", fileName);
ret = OC_STACK_ERROR;
}
}
else
{
......@@ -126,6 +121,13 @@ exit:
*bufferSize = 0;
OICFreeAndSetToNull((void**)buffer);
}
if(fp)
{
if (0 != fclose(fp))
{
OIC_LOG_V(ERROR, TAG, "Failed to close file \"%s\"", fileName);
}
}
return ret;
}
......@@ -594,8 +596,8 @@ OicSecAcl_t *JSONToAclBin(OicSecAclVersion_t *aclVersion, const char *jsonStr)
if (cJSON_Array == jsonAclArray->type)
{
int numAcl = cJSON_GetArraySize(jsonAclArray);
int idx = 0;
size_t numAcl = cJSON_GetArraySize(jsonAclArray);
size_t idx = 0;
VERIFY_SUCCESS(TAG, numAcl > 0, INFO);
do
......@@ -916,16 +918,14 @@ OicSecDoxm_t *JSONToDoxmBin(const char *jsonStr)
OicSecDoxm_t *doxm = NULL;
cJSON *jsonDoxm = NULL;
cJSON *jsonObj = NULL;
cJSON *jsonRoot = cJSON_Parse(jsonStr);
VERIFY_NOT_NULL(TAG, jsonRoot, ERROR);
jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
VERIFY_NOT_NULL(TAG, jsonDoxm, ERROR);
cJSON *jsonRoot = NULL;
doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(OicSecDoxm_t));
VERIFY_NOT_NULL(TAG, doxm, ERROR);
jsonRoot = cJSON_Parse(jsonStr);
VERIFY_NOT_NULL(TAG, jsonRoot, ERROR);
//Oxm -- not Mandatory
jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXMS_NAME);
if (jsonObj && cJSON_Array == jsonObj->type)
......@@ -1039,7 +1039,10 @@ OicSecDoxm_t *JSONToDoxmBin(const char *jsonStr)
ret = OC_STACK_OK;
exit:
cJSON_Delete(jsonRoot);
if (NULL != jsonRoot)
{
cJSON_Delete(jsonRoot);
}
if (OC_STACK_OK != ret)
{
DeleteDoxmBinData(doxm);
......@@ -1192,9 +1195,9 @@ static OicSecCred_t *JSONToCredBinWithRowner(const char *jsonStr,OicUuid_t *rown
if (cJSON_Array == jsonCredArray->type)
{
int numCred = cJSON_GetArraySize(jsonCredArray);
size_t numCred = cJSON_GetArraySize(jsonCredArray);
VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
int idx = 0;
size_t idx = 0;
do
{
cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
......@@ -1208,6 +1211,7 @@ static OicSecCred_t *JSONToCredBinWithRowner(const char *jsonStr,OicUuid_t *rown
else
{
cred = (OicSecCred_t *)OICCalloc(1, sizeof(OicSecCred_t));
VERIFY_NOT_NULL(TAG, cred, ERROR);
OicSecCred_t *temp = headCred;
while (temp->next)
{
......@@ -1261,9 +1265,10 @@ static OicSecCred_t *JSONToCredBinWithRowner(const char *jsonStr,OicUuid_t *rown
char tmp[3];
char *buf = (char *)OICCalloc(1, jsonObjLen/2);
VERIFY_NOT_NULL(TAG, buf, ERROR);
for(size_t i = 0, p = 0 ; i < jsonObjLen; i+=2, ++p)
{
sprintf(tmp, "%c%c", jsonPriv->valuestring[i], jsonPriv->valuestring[i+1]);
snprintf(tmp, 2, "%c%c", jsonPriv->valuestring[i], jsonPriv->valuestring[i+1]);
buf[p] = (char)strtol(tmp, NULL, 16);
}
cred->privateData.len = jsonObjLen/2;
......@@ -1289,9 +1294,10 @@ static OicSecCred_t *JSONToCredBinWithRowner(const char *jsonStr,OicUuid_t *rown
char tmp[3];
char *buf = (char *)OICCalloc(1, jsonObjLen/2);
VERIFY_NOT_NULL(TAG, buf, ERROR);
for(size_t i = 0, p = 0 ; i < jsonObjLen; i+=2, ++p)
{
sprintf(tmp, "%c%c", jsonPub->valuestring[i], jsonPub->valuestring[i+1]);
snprintf(tmp, 2, "%c%c", jsonPub->valuestring[i], jsonPub->valuestring[i+1]);
buf[p] = (char)strtol(tmp, NULL, 16);
}
cred->publicData.len = jsonObjLen/2;
......@@ -1317,9 +1323,10 @@ static OicSecCred_t *JSONToCredBinWithRowner(const char *jsonStr,OicUuid_t *rown
ret = (jsonObjLen % 2 == 0) ? ret : OC_STACK_ERROR;
char tmp[3];
char *buf = (char *)OICCalloc(1, jsonObjLen/2);
VERIFY_NOT_NULL(TAG, buf, ERROR);
for(size_t i = 0, p = 0; i < jsonObjLen; i+=2, ++p)
{
sprintf(tmp, "%c%c", jsonOpt->valuestring[i], jsonOpt->valuestring[i+1]);
snprintf(tmp, 2, "%c%c", jsonOpt->valuestring[i], jsonOpt->valuestring[i+1]);
buf[p] = (char)strtol(tmp, NULL, 16);
}
cred->optionalData.len = jsonObjLen/2;
......
......@@ -388,8 +388,8 @@ static int InputResources(OicSecRsrc_t *resources)
size_t i = 0;
char *href = NULL;
char **types = NULL;
size_t typeLen = 0;
char **interfaces = NULL;
size_t typeLen = 0;
size_t interfaceLen = 0;
if (NULL == resources)
......@@ -408,45 +408,42 @@ static int InputResources(OicSecRsrc_t *resources)
// input types
PRINT_PROG("\tInput the number of resource type for %s (MAX : %d): ", href,
SVR_MAX_ENTITY);
typeLen = InputNumber("");
if (SVR_MAX_ENTITY < typeLen)
typeLen = InputSize("");
if (SVR_MAX_ENTITY < typeLen || 0 == typeLen)
{
PRINT_ERR("Invalid number");
PRINT_ERR("Invalid number: %zu", typeLen);
OICFree(href);
return -1;
}
if (0 < typeLen)
types = (char **)OICCalloc(typeLen, sizeof(char *));
if (NULL == types)
{
types = (char **)OICCalloc(typeLen, sizeof(char *));
if (NULL == types)
{
PRINT_ERR("Failed to allocate memory");
OICFree(href);
return -1;
}
PRINT_ERR("Failed to allocate memory");
OICFree(href);
return -1;
}
for (i = 0; i < typeLen; i++)
for (i = 0; i < typeLen; i++)
{
PRINT_PROG("\tInput the resource type name #%zu : ", i + 1);
types[i] = InputString("");
if (NULL == types[i] )
{
PRINT_PROG("\tInput the resource type name #%zu : ", i + 1);
types[i] = InputString("");
if (NULL == types[i] )
PRINT_ERR("Failed InputString");
OICFree(href);
for (size_t j = 0; j < i; j++)
{
PRINT_ERR("Failed InputString");
OICFree(href);
for (size_t j = 0; j < i; j++)
{
OICFree(types[j]);
}
OICFree(types);
return -1;
OICFree(types[j]);
}
OICFree(types);
return -1;
}
}
// input interfaces
PRINT_PROG("\tInput the number of interface for %s (MAX : %d): ", href, SVR_MAX_ENTITY);
interfaceLen = InputNumber("");
interfaceLen = InputSize("");
if (SVR_MAX_ENTITY < interfaceLen)
{
PRINT_ERR("Invalid number");
......@@ -608,7 +605,7 @@ int InputAceData(OicSecAce_t *ace)
return ret;
}
PRINT_PROG("\tInput the number of resource for this access : ");
numOfRsrc = InputNumber("");
numOfRsrc = InputSize("");
if (SVR_MAX_ENTITY < numOfRsrc)
{
PRINT_ERR("Invalid number");
......@@ -696,7 +693,7 @@ static int ModifyAce(OicSecAce_t *ace)
break;
case ACE_REMOVE_RESOURCE:
sizeOfRsrc = PrintResourceList(ace->resources);
rsrcIdx = InputNumber("\tInput the number of resource to remove : ");
rsrcIdx = InputSize("\tInput the number of resource to remove : ");
if (0 == rsrcIdx || sizeOfRsrc < rsrcIdx)
{
PRINT_ERR("Invalid number");
......@@ -715,7 +712,7 @@ static int ModifyAce(OicSecAce_t *ace)
case ACE_EDIT_RESOURCE:
sizeOfRsrc = PrintResourceList(ace->resources);
PRINT_PROG("\tInput the number of resource to edit : ");
rsrcIdx = InputNumber("");
rsrcIdx = InputSize("");
if (0 == rsrcIdx || sizeOfRsrc < rsrcIdx)
{
PRINT_ERR("Invalid number");
......@@ -775,7 +772,7 @@ static int ModifyAcl(void)
PRINT_ERR("empty ace");
return -1;
}
aclIdx = InputNumber("\tPlease input the number of ACE : ");
aclIdx = InputSize("\tPlease input the number of ACE : ");
if (0 == aclIdx || numOfAce < aclIdx)
{
PRINT_ERR("Wrong number of ACE.");
......@@ -888,7 +885,7 @@ void HandleAclOperation(const SubOperationType_t cmd)
return;
}
aclIdx = InputNumber("\tPlease input the number of ACE : ");
aclIdx = InputSize("\tPlease input the number of ACE : ");
if (0 == aclIdx || aclIdx > numOfAce)
{
......
......@@ -93,6 +93,20 @@ void PrintBuffer(const uint8_t *buf, size_t bufLen)
}
}
size_t InputSize(const char *infoText)
{
size_t inputValue = 0;
PRINT_PROG("%s", infoText);
for (int ret = 0; 1 != ret; )
{
ret = scanf("%zu", &inputValue);
while ('\n' != getchar());
}
return inputValue;
}
int InputNumber(const char *infoText)
{
int inputValue = 0;
......
......@@ -69,6 +69,7 @@ void PrintString(const char *text);
void PrintBuffer(const uint8_t *buf, size_t bufLen);
int InputNumber(const char *infoText);
size_t InputSize(const char *infoText);
char *InputString(const char *infoText);
int InputUuid(OicUuid_t *uuid);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment