• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef ALG_UTILS_H
17 #define ALG_UTILS_H
18 
19 #include <stdint.h>
20 #include <sys/stat.h>
21 
22 #define MAX_MALLOC_SIZE (1024 * 500) /* 500K */
23 #define CLIB_MAX_MALLOC_SIZE 40960 /* 40K */
24 #define MAX_POLICY_DATA_LEN (100 * 1024)
25 
26 typedef uint32_t HcBool;
27 #define HC_TRUE 1
28 #define HC_FALSE 0
29 
30 namespace OHOS {
31 namespace Security {
32 namespace DlpPermission {
33 
34 typedef struct {
35     uint32_t dataSize;
36     uint8_t *value;
37 } BlobData;
38 
39 typedef struct {
40     char *data;
41     unsigned int beginPos;
42     unsigned int endPos;
43     unsigned int length;
44     unsigned int allocUnit;
45 } HcParcel;
46 
47 typedef union {
48     void *pfd;
49     int fd;
50 } FileHandle;
51 
52 #define MODE_FILE_READ 0
53 #define MODE_FILE_WRITE 1
54 #define USER_R_W_FILE_PERMISSION 066
55 
56 #define DLP_FREE_PTR(ptr) \
57 { \
58     HcFree(ptr); \
59     (ptr) = NULL; \
60 }
61 
62 void* HcMalloc(uint32_t size, char val);
63 void HcFree(void* addr);
64 uint32_t HcStrlen(const char *str);
65 void* ClibMalloc(uint32_t size, char val);
66 
67 bool IsBlobDataValid(const BlobData *blob);
68 void FreeBlobData(BlobData *data);
69 
70 HcParcel CreateParcel(uint32_t size, uint32_t allocUnit);
71 void DeleteParcel(HcParcel *parcel);
72 uint32_t GetParcelDataSize(const HcParcel *parcel);
73 const char *GetParcelData(const HcParcel *parcel);
74 HcBool ParcelRead(HcParcel *parcel, void *dst, uint32_t dataSize);
75 HcBool ParcelWrite(HcParcel *parcel, const void *src, uint32_t dataSize);
76 
77 int HcFileOpen(const char *path, int mode, FileHandle *file, mode_t permissionMode);
78 int HcFileSize(FileHandle file);
79 int HcFileRead(FileHandle file, void *dst, int dstSize);
80 int HcFileWrite(FileHandle file, const void *src, int srcSize);
81 void HcFileClose(FileHandle file);
82 bool HcIsFileExist(const char *path);
83 
84 }  // namespace DlpPermission
85 }  // namespace Security
86 }  // namespace OHOS
87 #endif  // ALG_UTILS_H