forked from openenclave/openenclave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.c
124 lines (96 loc) · 2.61 KB
/
files.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.
#include <openenclave/host.h>
#include <openenclave/internal/files.h>
#include <openenclave/internal/raise.h>
#include <openenclave/internal/safecrt.h>
#include <openenclave/internal/safemath.h>
#include <openenclave/internal/trace.h>
#include <openenclave/internal/utils.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "fopen.h"
bool __oe_file_exists(const char* path)
{
struct stat st;
return stat(path, &st) == 0 ? true : false;
}
oe_result_t __oe_load_file(
const char* path,
size_t extra_bytes,
void** data,
size_t* size)
{
oe_result_t result = OE_UNEXPECTED;
FILE* is = NULL;
if (data)
*data = NULL;
if (size)
*size = 0;
/* Check parameters */
if (!path || !data || !size)
OE_RAISE(OE_INVALID_PARAMETER);
/* Get size of this file */
{
struct stat st;
if (stat(path, &st) != 0)
OE_RAISE(OE_NOT_FOUND);
*size = (size_t)st.st_size;
}
/* Check for integer overflow */
size_t total_size;
OE_CHECK(oe_safe_add_sizet(*size, extra_bytes, &total_size));
/* Allocate memory */
if (!(*data = malloc(total_size)))
OE_RAISE(OE_OUT_OF_MEMORY);
/* Open the file */
if (oe_fopen(&is, path, "rb") != 0)
OE_RAISE(OE_NOT_FOUND);
/* Read file into memory */
if (fread(*data, 1, *size, is) != *size)
OE_RAISE(OE_READ_FAILED);
/* Zero-fill any extra bytes */
if (extra_bytes)
memset((unsigned char*)*data + *size, 0, extra_bytes);
result = OE_OK;
done:
if (result != OE_OK)
{
if (data && *data)
{
free(*data);
*data = NULL;
}
if (size)
*size = 0;
}
if (is)
fclose(is);
return result;
}
oe_result_t __oe_load_pages(const char* path, oe_page_t** pages, size_t* npages)
{
oe_result_t result = OE_UNEXPECTED;
void* data = NULL;
size_t size;
/* Reject invalid parameters */
if (!path || !pages || !npages)
OE_RAISE(OE_INVALID_PARAMETER);
/* Load the file into memory with zero extra bytes */
OE_CHECK(__oe_load_file(path, 0, &data, &size));
/* Fail if file size is not a multiple of the page size */
if (size % OE_PAGE_SIZE)
OE_RAISE(OE_FAILURE);
/* Set the output parameters */
*pages = ((oe_page_t*)data);
*npages = size / OE_PAGE_SIZE;
result = OE_OK;
done:
if (result != OE_OK)
free(data);
return result;
}