forked from JonathanSeals/CBPatcher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CBPUtils.c
58 lines (44 loc) · 1.32 KB
/
CBPUtils.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
//
// CBPUtils.c
// CBPatcher
//
// Created by JonathanSeals on 11/18/18.
// Copyright © 2018 JonathanSeals. All rights reserved.
//
#include "CBPUtils.h"
/* Get a buffer from a file name. Returns -1 on failure, 0 on success */
int openFile(char *fileName, size_t *fileSize, void **outBuf) {
/* Try to open the file */
FILE *fd = fopen(fileName, "r");
/* Make sure it opens successfully */
if (!fd) {
printf("Error opening %s\n", fileName);
return -1;
}
/* Seek to end of file descriptor */
fseek(fd, 0, SEEK_END);
/* Get the current position */
*fileSize = ftell(fd);
/* Rewind */
fseek(fd, 0, SEEK_SET);
/* File must be at least 0x800 bytes, and at most 100MB */
if (*fileSize < 0x800 || *fileSize > (100*1000*1000)) {
printf("Suspicious file length %zu, refusing to malloc\n", *fileSize);
fclose(fd);
return -1;
}
/* Allocate the buffer */
*outBuf = (void*)malloc(*fileSize);
if (!*outBuf) {
printf("Error allocating file buffer\n");
fclose(fd);
return -1;
}
/* Zero the buffer */
bzero(*outBuf, *fileSize);
/* Read in the file */
fread(*outBuf, *fileSize, 1, fd);
/* Close the file descriptor */
fclose(fd);
return 0;
}