• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(HITLS_BSL_SAL_LINUX) && defined(HITLS_BSL_SAL_FILE)
18 
19 #include <stdio.h>
20 #include <termios.h>
21 #include "bsl_errno.h"
22 #include "bsl_sal.h"
23 
SAL_FileOpen(bsl_sal_file_handle * stream,const char * path,const char * mode)24 int32_t SAL_FileOpen(bsl_sal_file_handle *stream, const char *path, const char *mode)
25 {
26     bsl_sal_file_handle temp = NULL;
27 
28     if (path == NULL || path[0] == 0 || mode == NULL || stream == NULL) {
29         return BSL_NULL_INPUT;
30     }
31 
32     temp = (bsl_sal_file_handle)fopen(path, mode);
33     if (temp == NULL) {
34         return BSL_SAL_ERR_FILE_OPEN;
35     }
36 
37     (*stream) = temp;
38     return BSL_SUCCESS;
39 }
40 
SAL_FileRead(bsl_sal_file_handle stream,void * buffer,size_t size,size_t num,size_t * len)41 int32_t SAL_FileRead(bsl_sal_file_handle stream, void *buffer, size_t size, size_t num, size_t *len)
42 {
43     if (stream == NULL || buffer == NULL || len == NULL) {
44         return BSL_NULL_INPUT;
45     }
46     *len = fread(buffer, size, num, stream);
47     if (*len != num) {
48         return feof(stream) != 0 ? BSL_SUCCESS : BSL_SAL_ERR_FILE_READ;
49     }
50     return BSL_SUCCESS;
51 }
52 
SAL_FileWrite(bsl_sal_file_handle stream,const void * buffer,size_t size,size_t num)53 int32_t SAL_FileWrite(bsl_sal_file_handle stream, const void *buffer, size_t size, size_t num)
54 {
55     if (stream == NULL || buffer == NULL) {
56         return BSL_NULL_INPUT;
57     }
58     size_t ret = fwrite(buffer, size, num, stream);
59 
60     return ret == num ? BSL_SUCCESS : BSL_SAL_ERR_FILE_WRITE;
61 }
62 
SAL_FileClose(bsl_sal_file_handle stream)63 void SAL_FileClose(bsl_sal_file_handle stream)
64 {
65     (void)fclose(stream);
66 }
67 
SAL_FileLength(const char * path,size_t * len)68 int32_t SAL_FileLength(const char *path, size_t *len)
69 {
70     int32_t ret;
71     long tmp;
72     bsl_sal_file_handle stream = NULL;
73 
74     if (path == NULL || len == NULL) {
75         return BSL_NULL_INPUT;
76     }
77 
78     ret = BSL_SAL_FileOpen(&stream, path, "rb");
79     if (ret != BSL_SUCCESS) {
80         return BSL_SAL_ERR_FILE_LENGTH;
81     }
82 
83     ret = fseek(stream, 0, SEEK_END);
84     if (ret != BSL_SUCCESS) {
85         BSL_SAL_FileClose(stream);
86         return BSL_SAL_ERR_FILE_LENGTH;
87     }
88 
89     tmp = ftell(stream);
90     if (tmp < 0) {
91         BSL_SAL_FileClose(stream);
92         return BSL_SAL_ERR_FILE_LENGTH;
93     }
94 
95     *len = (size_t)tmp;
96 
97     BSL_SAL_FileClose(stream);
98 
99     return BSL_SUCCESS;
100 }
101 
102 #endif
103