1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include "hitls_build.h"
17
18 #if defined(HITLS_BSL_SAL_FILE)
19 #include <stdint.h>
20 #include "bsl_sal.h"
21 #include "bsl_errno.h"
22 #include "sal_fileimpl.h"
23
24 static BSL_SAL_FileCallback g_filleCallBack = {0};
25
SAL_FileCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type,void * funcCb)26 int32_t SAL_FileCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb)
27 {
28 if (type > BSL_SAL_FILE_LENGTH_CB_FUNC || type < BSL_SAL_FILE_OPEN_CB_FUNC) {
29 return BSL_SAL_FILE_NO_REG_FUNC;
30 }
31 uint32_t offet = (uint32_t)(type - BSL_SAL_FILE_OPEN_CB_FUNC);
32 ((void **)&g_filleCallBack)[offet] = funcCb;
33 return BSL_SUCCESS;
34 }
35
BSL_SAL_FileOpen(bsl_sal_file_handle * stream,const char * path,const char * mode)36 int32_t BSL_SAL_FileOpen(bsl_sal_file_handle *stream, const char *path, const char *mode)
37 {
38 if (g_filleCallBack.pfFileOpen != NULL && g_filleCallBack.pfFileOpen != BSL_SAL_FileOpen) {
39 return g_filleCallBack.pfFileOpen(stream, path, mode);
40 }
41 #ifdef HITLS_BSL_SAL_LINUX
42 return SAL_FileOpen(stream, path, mode);
43 #else
44 return BSL_SAL_FILE_NO_REG_FUNC;
45 #endif
46 }
47
BSL_SAL_FileRead(bsl_sal_file_handle stream,void * buffer,size_t size,size_t num,size_t * len)48 int32_t BSL_SAL_FileRead(bsl_sal_file_handle stream, void *buffer, size_t size, size_t num, size_t *len)
49 {
50 if (g_filleCallBack.pfFileRead != NULL && g_filleCallBack.pfFileRead != BSL_SAL_FileRead) {
51 return g_filleCallBack.pfFileRead(stream, buffer, size, num, len);
52 }
53 #ifdef HITLS_BSL_SAL_LINUX
54 return SAL_FileRead(stream, buffer, size, num, len);
55 #else
56 return BSL_SAL_FILE_NO_REG_FUNC;
57 #endif
58 }
59
BSL_SAL_FileWrite(bsl_sal_file_handle stream,const void * buffer,size_t size,size_t num)60 int32_t BSL_SAL_FileWrite(bsl_sal_file_handle stream, const void *buffer, size_t size, size_t num)
61 {
62 if (g_filleCallBack.pfFileWrite != NULL && g_filleCallBack.pfFileWrite != BSL_SAL_FileWrite) {
63 return g_filleCallBack.pfFileWrite(stream, buffer, size, num);
64 }
65 #ifdef HITLS_BSL_SAL_LINUX
66 return SAL_FileWrite(stream, buffer, size, num);
67 #else
68 return BSL_SAL_FILE_NO_REG_FUNC;
69 #endif
70 }
71
BSL_SAL_FileClose(bsl_sal_file_handle stream)72 void BSL_SAL_FileClose(bsl_sal_file_handle stream)
73 {
74 if (g_filleCallBack.pfFileClose != NULL && g_filleCallBack.pfFileClose != BSL_SAL_FileClose) {
75 g_filleCallBack.pfFileClose(stream);
76 return;
77 }
78 #ifdef HITLS_BSL_SAL_LINUX
79 SAL_FileClose(stream);
80 #endif
81 }
82
BSL_SAL_FileLength(const char * path,size_t * len)83 int32_t BSL_SAL_FileLength(const char *path, size_t *len)
84 {
85 if (g_filleCallBack.pfFileLength != NULL && g_filleCallBack.pfFileLength != BSL_SAL_FileLength) {
86 return g_filleCallBack.pfFileLength(path, len);
87 }
88 #ifdef HITLS_BSL_SAL_LINUX
89 return SAL_FileLength(path, len);
90 #else
91 return BSL_SAL_FILE_NO_REG_FUNC;
92 #endif
93 }
94
BSL_SAL_ReadFile(const char * path,uint8_t ** buff,uint32_t * len)95 int32_t BSL_SAL_ReadFile(const char *path, uint8_t **buff, uint32_t *len)
96 {
97 size_t readLen;
98 size_t fileLen = 0;
99 int32_t ret = BSL_SAL_FileLength(path, &fileLen);
100 if (ret != BSL_SUCCESS) {
101 return ret;
102 }
103 bsl_sal_file_handle stream = NULL;
104 ret = BSL_SAL_FileOpen(&stream, path, "rb");
105 if (ret != BSL_SUCCESS) {
106 return ret;
107 }
108
109 uint8_t *fileBuff = BSL_SAL_Malloc((uint32_t)fileLen + 1);
110 if (fileBuff == NULL) {
111 BSL_SAL_FileClose(stream);
112 return BSL_MALLOC_FAIL;
113 }
114 do {
115 ret = BSL_SAL_FileRead(stream, fileBuff, 1, fileLen, &readLen);
116 BSL_SAL_FileClose(stream);
117 if (ret != BSL_SUCCESS) {
118 break;
119 }
120 fileBuff[fileLen] = '\0';
121 *buff = fileBuff;
122 *len = (uint32_t)fileLen;
123 return ret;
124 } while (0);
125 BSL_SAL_FREE(fileBuff);
126 return ret;
127 }
128
BSL_SAL_WriteFile(const char * path,const uint8_t * buff,uint32_t len)129 int32_t BSL_SAL_WriteFile(const char *path, const uint8_t *buff, uint32_t len)
130 {
131 bsl_sal_file_handle stream = NULL;
132 int32_t ret = BSL_SAL_FileOpen(&stream, path, "wb");
133 if (ret != BSL_SUCCESS) {
134 return ret;
135 }
136
137 ret = BSL_SAL_FileWrite(stream, buff, 1, len);
138 BSL_SAL_FileClose(stream);
139 return ret;
140 }
141
142 #endif
143