1 /*
2 * Copyright (c) 2023 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 "cf_log.h"
20
IsStrValid(const char * str,uint32_t maxLen)21 bool IsStrValid(const char *str, uint32_t maxLen)
22 {
23 if (str == NULL) {
24 LOGE("input string is NULL ptr");
25 return false;
26 }
27 // One byte must be reserved for the terminator.
28 if (strnlen(str, maxLen) >= maxLen) {
29 LOGE("input string is beyond max length");
30 return false;
31 }
32 return true;
33 }
34
IsBlobValid(const CfBlob * blob)35 bool IsBlobValid(const CfBlob *blob)
36 {
37 return ((blob != NULL) && (blob->data != NULL) && (blob->size > 0));
38 }
39
IsClassMatch(const CfObjectBase * obj,const char * className)40 bool IsClassMatch(const CfObjectBase *obj, const char *className)
41 {
42 if ((obj == NULL) || (obj->getClass() == NULL) || (className == NULL)) {
43 return false;
44 }
45 if (strcmp(obj->getClass(), className) == 0) {
46 return true;
47 } else {
48 LOGE("class is not match. expect class: %s, input class: %s", className, obj->getClass());
49 return false;
50 }
51 }
52
IsPubKeyClassMatch(const HcfObjectBase * obj,const char * className)53 bool IsPubKeyClassMatch(const HcfObjectBase *obj, const char *className)
54 {
55 if ((obj == NULL) || (obj->getClass() == NULL) || (className == NULL)) {
56 return false;
57 }
58 if (strcmp(obj->getClass(), className) == 0) {
59 return true;
60 } else {
61 LOGE("class is not match. expect class: %s, input class: %s", className, obj->getClass());
62 return false;
63 }
64 }