• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "init_utils.h"
16 
17 #include <string.h>
18 
19 #include "fscrypt_log.h"
20 #include "securec.h"
21 
22 #define MAX_FILE_LEN 102400    // max init.cfg size 100KB
23 
ReadFileToBuf(const char * configFile)24 char *ReadFileToBuf(const char *configFile)
25 {
26     char *buffer = NULL;
27     FILE *fd = NULL;
28     struct stat fileStat = {0};
29     FSCRYPT_CHECK_RETURN_VALUE(configFile != NULL && *configFile != '\0', NULL);
30     do {
31         if (stat(configFile, &fileStat) != 0 ||
32             fileStat.st_size <= 0 || fileStat.st_size > MAX_FILE_LEN) {
33             LOGE("Unexpected config file \" %{public}s \", check if it exist. if exist, check file size", configFile);
34             break;
35         }
36         fd = fopen(configFile, "r");
37         if (fd == NULL) {
38             LOGE("Open %{public}s failed. err = %{public}d", configFile, errno);
39             break;
40         }
41         buffer = (char*)malloc((size_t)(fileStat.st_size + 1));
42         if (buffer == NULL) {
43             LOGE("Failed to allocate memory for config file, err = %{public}d", errno);
44             break;
45         }
46 
47         if (fread(buffer, fileStat.st_size, 1, fd) != 1) {
48             free(buffer);
49             buffer = NULL;
50             LOGE("Failed to read config file, err = %{public}d", errno);
51             break;
52         }
53         buffer[fileStat.st_size] = '\0';
54     } while (0);
55 
56     if (fd != NULL) {
57         (void)fclose(fd);
58         fd = NULL;
59     }
60     return buffer;
61 }
62 
SplitString(char * srcPtr,const char * del,char ** dstPtr,int maxNum)63 int SplitString(char *srcPtr, const char *del, char **dstPtr, int maxNum)
64 {
65     FSCRYPT_CHECK_RETURN_VALUE(srcPtr != NULL && dstPtr != NULL && del != NULL, -1);
66     char *buf = NULL;
67     dstPtr[0] = strtok_r(srcPtr, del, &buf);
68     int counter = 0;
69     while ((counter < maxNum) && (dstPtr[counter] != NULL)) {
70         counter++;
71         if (counter >= maxNum) {
72             break;
73         }
74         dstPtr[counter] = strtok_r(NULL, del, &buf);
75     }
76     return counter;
77 }
78 
FreeStringVector(char ** vector,int count)79 void FreeStringVector(char **vector, int count)
80 {
81     if (vector != NULL) {
82         for (int i = 0; i < count; i++) {
83             if (vector[i] != NULL) {
84                 free(vector[i]);
85                 vector[i] = NULL;
86             }
87         }
88         free(vector);
89         vector = NULL;
90     }
91 }
92 
SplitStringExt(char * buffer,const char * del,int * returnCount,int maxItemCount)93 char **SplitStringExt(char *buffer, const char *del, int *returnCount, int maxItemCount)
94 {
95     FSCRYPT_CHECK_RETURN_VALUE((maxItemCount >= 0) && (buffer != NULL) && (del != NULL) && (returnCount != NULL), NULL);
96     // Why is this number?
97     // Now we use this function to split a string with a given delimiter
98     // We do not know how many sub-strings out there after splitting.
99     // 50 is just a guess value.
100     const int defaultItemCounts = 50;
101     int itemCounts = maxItemCount;
102 
103     if (maxItemCount > defaultItemCounts) {
104         itemCounts = defaultItemCounts;
105     }
106     char **items = (char **)malloc(sizeof(char*) * itemCounts);
107     FSCRYPT_ERROR_CHECK(items != NULL, return NULL, "No enough memory to store items");
108     char *rest = NULL;
109     char *p = strtok_r(buffer, del, &rest);
110     int count = 0;
111     while (p != NULL) {
112         if (count > itemCounts - 1) {
113             itemCounts += (itemCounts / 2) + 1; // 2 Request to increase the original memory by half.
114             LOGD("Too many items,expand size");
115 
116             char **expand = (char **)malloc(sizeof(char*) * itemCounts);
117             FSCRYPT_ERROR_CHECK(expand != NULL, FreeStringVector(items, count);
118                 return NULL, "Failed to expand memory");
119             int ret = memcpy_s(expand, sizeof(char *) * itemCounts, items, sizeof(char *) * count);
120             if (ret != 0) {
121                 FreeStringVector(items, count);
122                 FreeStringVector(expand, itemCounts);
123                 LOGD("Too many items,expand size");
124                 return NULL;
125             }
126             free(items);
127             items = expand;
128         }
129         size_t len = strlen(p);
130         items[count] = (char *)malloc(len + 1);
131         FSCRYPT_CHECK(items[count] != NULL, FreeStringVector(items, count);
132             return NULL);
133         if (strncpy_s(items[count], len + 1, p, len) != EOK) {
134             LOGE("Copy string failed");
135             FreeStringVector(items, count);
136             return NULL;
137         }
138         items[count][len] = '\0';
139         count++;
140         p = strtok_r(NULL, del, &rest);
141     }
142     *returnCount = count;
143     return items;
144 }