1 /*
2  * Copyright (c) 2023-2024 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 
20 #include "cf_log.h"
21 #include "cf_memory.h"
22 
23 #define MIN_URL_LEN 5
24 #define HTTP_URL_LEN 7
25 #define HTTPS_URL_LEN 8
26 
CfIsStrValid(const char * str,uint32_t maxLen)27 bool CfIsStrValid(const char *str, uint32_t maxLen)
28 {
29     if (str == NULL) {
30         LOGE("input string is NULL ptr");
31         return false;
32     }
33     // One byte must be reserved for the terminator.
34     if (strnlen(str, maxLen) >= maxLen) {
35         LOGE("input string is beyond max length");
36         return false;
37     }
38     return true;
39 }
40 
CfIsBlobValid(const CfBlob * blob)41 bool CfIsBlobValid(const CfBlob *blob)
42 {
43     return ((blob != NULL) && (blob->data != NULL) && (blob->size > 0));
44 }
45 
CfIsClassMatch(const CfObjectBase * obj,const char * className)46 bool CfIsClassMatch(const CfObjectBase *obj, const char *className)
47 {
48     if ((obj == NULL) || (obj->getClass() == NULL) || (className == NULL)) {
49         return false;
50     }
51     if (strcmp(obj->getClass(), className) == 0) {
52         return true;
53     } else {
54         LOGE("class is not match. expect class: %s, input class: %s", className, obj->getClass());
55         return false;
56     }
57 }
58 
CfIsPubKeyClassMatch(const HcfObjectBase * obj,const char * className)59 bool CfIsPubKeyClassMatch(const HcfObjectBase *obj, const char *className)
60 {
61     if ((obj == NULL) || (obj->getClass() == NULL) || (className == NULL)) {
62         return false;
63     }
64     if (strcmp(obj->getClass(), className) == 0) {
65         return true;
66     } else {
67         LOGE("class is not match. expect class: %s, input class: %s", className, obj->getClass());
68         return false;
69     }
70 }
71 
CfIsUrlValid(const char * url)72 bool CfIsUrlValid(const char *url)
73 {
74     if (url == NULL) {
75         return false;
76     }
77     if (strlen(url) < MIN_URL_LEN) {
78         return false;
79     }
80     if (strncmp(url, "http://", HTTP_URL_LEN) != 0 && strncmp(url, "https://", HTTPS_URL_LEN) != 0) {
81         return false;
82     }
83 
84     int httpHeaderLen = (strncmp(url, "http://", HTTP_URL_LEN) == 0) ? HTTP_URL_LEN : HTTPS_URL_LEN;
85     const char *startDomain = url + httpHeaderLen;
86     const char *endDomain = startDomain;
87     while (*endDomain != '\0' && *endDomain != '/') {
88         endDomain++;
89     }
90     if (endDomain == startDomain) {
91         return false;
92     }
93 
94     if (*endDomain == '\0') {
95         return true;
96     } else {
97         const char *startPath = endDomain;
98         while (*startPath == '/') {
99             startPath++;
100         }
101         if (*startPath == '\0') {
102             return false;
103         } else {
104             return true;
105         }
106     }
107 }
108 
CfIsHttp(const char * url)109 bool CfIsHttp(const char *url)
110 {
111     if (url != NULL && strncmp(url, "http://", strlen("http://")) == 0) {
112         return true;
113     } else {
114         return false;
115     }
116 }