replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / tool / svrdbeditor_src / svrdbeditorcommon.c
1 /* *****************************************************************
2  *
3  * Copyright 2017 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * *****************************************************************/
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "utlist.h"
25 #include "oic_malloc.h"
26 #include "oic_string.h"
27
28 #include "srmresourcestrings.h"
29 #include "securevirtualresourcetypes.h"
30 #include "srmutility.h"
31
32 #include "svrdbeditorcommon.h"
33
34 #define STR_UUID_LENGTH (UUID_LENGTH * 2 + 4 + 1) // length + dash length  + '\0'
35 #define STR_UUID_ZERO "0"
36
37 void PrintUuid(const OicUuid_t *uuid)
38 {
39     char *strUuid = NULL;
40     if (OC_STACK_OK == ConvertUuidToStr(uuid, &strUuid))
41     {
42         PRINT_DATA("%s\n", strUuid);
43         OICFree(strUuid);
44     }
45     else
46     {
47         PRINT_ERR("Can not convert UUID to String");
48     }
49 }
50
51 void PrintIntArray(const int *array, size_t length)
52 {
53     for (size_t i = 0; i < length; i++)
54     {
55         PRINT_DATA("%d ", array[i]);
56     }
57     PRINT_NL();
58 }
59
60 void PrintStringArray(const char **array, size_t length)
61 {
62     for (size_t i = 0; i < length; i++)
63     {
64         PRINT_DATA("%s ", array[i]);
65     }
66     PRINT_NL();
67 }
68
69 void PrintInt(int value)
70 {
71     PRINT_DATA("%d\n", value);
72 }
73
74 void PrintString(const char *text)
75 {
76     PRINT_DATA("%s\n", text);
77 }
78
79 void PrintBuffer(const uint8_t *buf, size_t bufLen)
80 {
81     size_t i = 0;
82
83     for (i = 0; i < bufLen; i++)
84     {
85         if (0 == (i + 1) % 20 || bufLen - 1 == i)
86         {
87             PRINT_DATA("%02X \n", buf[i]);
88         }
89         else
90         {
91             PRINT_DATA("%02X ", buf[i]);
92         }
93     }
94 }
95
96 int InputNumber(const char *infoText)
97 {
98     int inputValue = 0;
99
100     PRINT_PROG("%s", infoText);
101     for (int ret = 0; 1 != ret; )
102     {
103         ret = scanf("%d", &inputValue);
104         for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
105         // '0x20<=code' is character region
106     }
107
108     return inputValue;
109 }
110
111 char *InputString(const char *infoText)
112 {
113     char tmpStr[SVR_DB_PATH_LENGTH] = {0};
114
115     PRINT_PROG("%s", infoText);
116     for (int ret = 0; 1 != ret; )
117     {
118         ret = scanf("%1024s", tmpStr);
119         for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
120         // '0x20<=code' is character region
121     }
122
123     return OICStrdup(tmpStr);
124 }
125
126 int InputUuid(OicUuid_t *uuid)
127 {
128     OCStackResult ocResult = OC_STACK_ERROR;
129     char strUuid[STR_UUID_LENGTH] = {0};
130     size_t strLen = 0;
131
132     if (NULL == uuid)
133     {
134         PRINT_ERR("Invalid parameter");
135         return -1;
136     }
137
138     if (NULL == fgets(strUuid, STR_UUID_LENGTH, stdin))
139     {
140         PRINT_ERR("Failed fgets");
141         return -1;
142     }
143     strLen = strlen(strUuid);
144     if ('\n' == strUuid[strLen - 1])
145     {
146         strUuid[strLen - 1] = '\0';
147     }
148     else
149     {
150         for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
151     }
152
153     if (0 == strncmp(strUuid, STR_UUID_ZERO, sizeof(STR_UUID_ZERO)))
154     {
155         memset(uuid->id, 0x00, sizeof(uuid->id));
156     }
157     else if (0 == strncmp(strUuid, (char *)WILDCARD_SUBJECT_ID.id, sizeof(WILDCARD_SUBJECT_ID.id)))
158     {
159         memset(uuid->id, 0x00, sizeof(uuid->id));
160         memcpy(uuid->id, WILDCARD_SUBJECT_ID.id, WILDCARD_SUBJECT_ID_LEN);
161     }
162     else
163     {
164         ocResult = ConvertStrToUuid(strUuid, uuid);
165         if (OC_STACK_OK != ocResult)
166         {
167             PRINT_ERR("Failed ConvertStrToUuid");
168             return -1;
169         }
170     }
171
172     return 0;
173 }
174