replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / provisioning / examples / cloudWrapper.cpp
index 493ff47..fefd85a 100644 (file)
@@ -1,7 +1,26 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * *****************************************************************/
 
 #include "logger.h"
 #include "occloudprovisioning.h"
-#include "OCCloudProvisioning.h"
+#include "OCCloudProvisioning.hpp"
 #include "oic_malloc.h"
 #include "oic_string.h"
 #include "utils.h"
 
 using namespace OC;
 
+/**
+ * Skip special characters from stdin
+ * */
+static void skipSpecialCharacters()
+{
+    for( ; 0x20<=getchar(); );  // for removing overflow garbages
+                                // '0x20<=code' is character region
+}
+
 static bool readOptional(const char* description)
 {
     if (NULL == description)
@@ -39,12 +67,17 @@ static bool readOptional(const char* description)
     }
 
     printf("Do you want to Enter %s (y/n):\n", description);
-    char choice = 0;
+    char temp, choice = 0;
 
     while(1)
     {
-        scanf("%c", &choice);
-        getchar();
+        for (int ret = 0; 1 != ret; )
+        {
+            ret = scanf("%c", &temp);
+            skipSpecialCharacters();
+        }
+
+        choice = temp;
 
         switch (choice)
         {
@@ -66,11 +99,25 @@ static bool readOptional(const char* description)
  */
 void readString(char* item, int length, const char* description, const char* example)
 {
+    char *input = (char*)OICCalloc(length, sizeof(char));
+    if (NULL == input)
+    {
+        OIC_LOG(INFO, TAG, "input is NULL");
+        return;
+    }
+
     printf("Enter %s (f.e. %s):\n", description, example);
     char temp[8] = { 0 };
     snprintf(temp, sizeof(temp), "%%%ds", length - 1);
-    scanf(temp, item);
-    getchar();
+
+    for (int ret = 0; 1 != ret; )
+    {
+        ret = scanf(temp, input);
+        skipSpecialCharacters();
+    }
+
+    strncpy(item, input, length);
+    OICFree(input);
 }
 
 /**
@@ -98,9 +145,17 @@ static void readOptionalString(char* item, int length, const char* description,
  */
 void readInteger(int* item, const char* description, const char* example)
 {
+    int temp;
+
     printf("Enter %s (f.e. %s):\n", description, example);
-    scanf("%d", item);
-    getchar();
+
+    for (int ret = 0; 1 != ret; )
+    {
+        ret = scanf("%d", &temp);
+        skipSpecialCharacters();
+    }
+
+    *item = temp;
 }
 
 /**
@@ -122,7 +177,7 @@ static void readStringArray(stringArray_t *list, int length, const char* descrip
 
     char **item = NULL;
 
-    if (0 == count)
+    if (0 >= count)
     {
         return;
     }
@@ -147,7 +202,7 @@ static void readStringArray(stringArray_t *list, int length, const char* descrip
         readString(item[i], length, hint, example);
     }
     list->array  = item;
-    list->length = count;
+    list->length = (size_t)count;
     return;
 
 no_memory:
@@ -238,7 +293,7 @@ static int ReadFile(const char *name, OCByteString *crl)
     }
 
     crl->bytes = buffer;
-    crl->len   = length;
+    crl->len   = (size_t)length;
 
     result = 0;
 exit:
@@ -336,14 +391,20 @@ OCStackResult OCWrapperAclIndividualUpdateAce(const OCDevAddr *endPoint, OCCloud
 {
     OCStackResult result = OC_STACK_NO_MEMORY;
     int i = 0, j = 0;
+    cloudAce_t *aces = NULL;
 
     char aclid[MAX_ID_LENGTH] = { 0 };
     readString(aclid, sizeof(aclid), "ace id", ACL_ID_EXAMPLE);
 
     int acllist_count = 0;
     readInteger(&acllist_count, "acl list count", "1");
+    if (0 >= acllist_count)
+    {
+        OIC_LOG(ERROR, TAG, "Wrong number of aclList");
+        goto exit;
+    }
 
-    cloudAce_t *aces = (cloudAce_t*)OICCalloc(acllist_count, sizeof(cloudAce_t));
+    aces = (cloudAce_t*)OICCalloc(acllist_count, sizeof(cloudAce_t));
     if (!aces)
     {
         OIC_LOG(ERROR, TAG, "Can't allocate memory for aces");
@@ -418,10 +479,10 @@ exit:
                     OicSecRsrc_t *res = &ace->resources[l];
                     OICFree(res->href);
 
-                    stringArray_t rt = {.array = res->types, .length = res->typeLen};
+                    stringArray_t rt { res->types, res->typeLen};
                     clearStringArray(&rt);
 
-                    stringArray_t _if = {.array = res->interfaces, .length = res->interfaceLen};
+                    stringArray_t _if {res->interfaces, res->interfaceLen};
                     clearStringArray(&_if);
                 }
             }