Changeset View
Changeset View
Standalone View
Standalone View
head/lib/libsecureboot/readfile.c
Show All 22 Lines | |||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <libsecureboot.h> | #include <libsecureboot.h> | ||||
unsigned char * | unsigned char * | ||||
read_file(const char *path, size_t *len) | read_fd(int fd, size_t len) | ||||
{ | { | ||||
int fd, m, n, x; | int m, n, x; | ||||
struct stat st; | |||||
unsigned char *buf; | unsigned char *buf; | ||||
if (len) | buf = malloc(len + 1); | ||||
*len = 0; | for (x = 0, m = len; m > 0; ) { | ||||
if ((fd = open(path, O_RDONLY)) < 0) | |||||
return (NULL); | |||||
fstat(fd, &st); | |||||
if (len) | |||||
*len = st.st_size; | |||||
buf = malloc(st.st_size + 1); | |||||
for (x = 0, m = st.st_size; m > 0; ) { | |||||
n = read(fd, &buf[x], m); | n = read(fd, &buf[x], m); | ||||
if (n < 0) | if (n < 0) | ||||
break; | break; | ||||
if (n > 0) { | if (n > 0) { | ||||
m -= n; | m -= n; | ||||
x += n; | x += n; | ||||
} | } | ||||
} | } | ||||
close(fd); | |||||
if (m == 0) { | if (m == 0) { | ||||
buf[st.st_size] = '\0'; | buf[len] = '\0'; | ||||
return (buf); | return (buf); | ||||
} | } | ||||
free(buf); | free(buf); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
unsigned char * | |||||
read_file(const char *path, size_t *len) | |||||
{ | |||||
struct stat st; | |||||
unsigned char *ucp; | |||||
int fd; | |||||
if (len) | |||||
*len = 0; | |||||
if ((fd = open(path, O_RDONLY)) < 0) | |||||
return (NULL); | |||||
fstat(fd, &st); | |||||
ucp = read_fd(fd, st.st_size); | |||||
close(fd); | |||||
if (len != NULL && ucp != NULL) | |||||
*len = st.st_size; | |||||
return (ucp); | |||||
} | |||||