1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "utils.h"
17 
18 #include <string.h>
19 #include "log.h"
20 
21 #define ERROR_STR_LENGTH 0
22 
HcfIsStrValid(const char * str,uint32_t maxLen)23 bool HcfIsStrValid(const char *str, uint32_t maxLen)
24 {
25     if (str == NULL) {
26         LOGE("input string is NULL ptr");
27         return false;
28     }
29     // One byte must be reserved for the terminator.
30     if (strnlen(str, maxLen) >= maxLen) {
31         LOGE("input string is beyond max length");
32         return false;
33     }
34     return true;
35 }
36 
HcfIsBlobValid(const HcfBlob * blob)37 bool HcfIsBlobValid(const HcfBlob *blob)
38 {
39     return ((blob != NULL) && (blob->data != NULL) && (blob->len > 0));
40 }
41 
HcfIsClassMatch(const HcfObjectBase * obj,const char * className)42 bool HcfIsClassMatch(const HcfObjectBase *obj, const char *className)
43 {
44     if ((obj == NULL) || (obj->getClass() == NULL) || (className == NULL)) {
45         return false;
46     }
47     if (strcmp(obj->getClass(), className) == 0) {
48         return true;
49     } else {
50         LOGE("class is not match. expect class: %s, input class: %s", className, obj->getClass());
51         return false;
52     }
53 }
54 
HcfStrlen(const char * str)55 size_t HcfStrlen(const char *str)
56 {
57     if (str == NULL) {
58         LOGE("str is null");
59         return ERROR_STR_LENGTH;
60     }
61     return strlen(str);
62 }
63