| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include <unistd.h> |
| 14 |
|
| 15 |
#include "host_common.h" |
| 16 |
|
| 17 |
char* StrCopy(char* dest, const char* src, int dest_size) |
| 18 |
{ |
| 19 |
strncpy(dest, src, dest_size); |
| 20 |
dest[dest_size - 1] = '\0'; |
| 21 |
return dest; |
| 22 |
} |
| 23 |
|
| 24 |
uint8_t* ReadFile(const char* filename, uint64_t* sizeptr) |
| 25 |
{ |
| 26 |
FILE* f; |
| 27 |
uint8_t* buf; |
| 28 |
long size; |
| 29 |
|
| 30 |
f = fopen(filename, "rb"); |
| 31 |
if (!f) { |
| 32 |
fprintf(stderr, "Unable to open file %s\n", filename); |
| 33 |
return NULL; |
| 34 |
} |
| 35 |
|
| 36 |
fseek(f, 0, SEEK_END); |
| 37 |
size = ftell(f); |
| 38 |
if (size < 0) { |
| 39 |
fclose(f); |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
rewind(f); |
| 43 |
|
| 44 |
buf = malloc(size); |
| 45 |
if (!buf) { |
| 46 |
fclose(f); |
| 47 |
return NULL; |
| 48 |
} |
| 49 |
|
| 50 |
if (1 != fread(buf, size, 1, f)) { |
| 51 |
fprintf(stderr, "Unable to read from file %s\n", filename); |
| 52 |
fclose(f); |
| 53 |
free(buf); |
| 54 |
return NULL; |
| 55 |
} |
| 56 |
|
| 57 |
fclose(f); |
| 58 |
if (sizeptr) |
| 59 |
*sizeptr = size; |
| 60 |
return buf; |
| 61 |
} |
| 62 |
|
| 63 |
char* ReadFileFirstLine(char* dest, int size, const char* filename) |
| 64 |
{ |
| 65 |
char* got; |
| 66 |
FILE* f; |
| 67 |
|
| 68 |
f = fopen(filename, "rt"); |
| 69 |
if (!f) |
| 70 |
return NULL; |
| 71 |
|
| 72 |
got = fgets(dest, size, f); |
| 73 |
fclose(f); |
| 74 |
|
| 75 |
|
| 76 |
if (got) |
| 77 |
dest[strcspn(dest, "\n")] = 0; |
| 78 |
return got; |
| 79 |
} |
| 80 |
|
| 81 |
int ReadFileInt(const char* filename, unsigned* value) |
| 82 |
{ |
| 83 |
char buf[64]; |
| 84 |
char* e = NULL; |
| 85 |
|
| 86 |
if (!ReadFileFirstLine(buf, sizeof(buf), filename)) |
| 87 |
return -1; |
| 88 |
|
| 89 |
|
| 90 |
*value = (unsigned)strtoul(buf, &e, 0); |
| 91 |
if (e == buf) |
| 92 |
return -1; |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
int ReadFileBit(const char* filename, int bitmask) |
| 98 |
{ |
| 99 |
unsigned value; |
| 100 |
if (ReadFileInt(filename, &value) < 0) |
| 101 |
return -1; |
| 102 |
else return (value & bitmask ? 1 : 0); |
| 103 |
} |
| 104 |
|
| 105 |
vb2_error_t WriteFile(const char* filename, const void *data, uint64_t size) |
| 106 |
{ |
| 107 |
FILE *f = fopen(filename, "wb"); |
| 108 |
if (!f) { |
| 109 |
fprintf(stderr, "Unable to open file %s\n", filename); |
| 110 |
return 1; |
| 111 |
} |
| 112 |
|
| 113 |
if (1 != fwrite(data, size, 1, f)) { |
| 114 |
fprintf(stderr, "Unable to write to file %s\n", filename); |
| 115 |
fclose(f); |
| 116 |
unlink(filename); |
| 117 |
return 1; |
| 118 |
} |
| 119 |
|
| 120 |
fclose(f); |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
bool parse_hex(uint8_t *val, const char *str) |
| 125 |
{ |
| 126 |
uint8_t v = 0; |
| 127 |
char c; |
| 128 |
int digit; |
| 129 |
|
| 130 |
for (digit = 0; digit < 2; digit++) { |
| 131 |
c = *str; |
| 132 |
if (!c) |
| 133 |
return false; |
| 134 |
if (!isxdigit(c)) |
| 135 |
return false; |
| 136 |
c = tolower(c); |
| 137 |
if (c >= '0' && c <= '9') |
| 138 |
v += c - '0'; |
| 139 |
else |
| 140 |
v += 10 + c - 'a'; |
| 141 |
if (!digit) |
| 142 |
v <<= 4; |
| 143 |
str++; |
| 144 |
} |
| 145 |
|
| 146 |
*val = v; |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
bool parse_hash(uint8_t *buf, size_t len, const char *str) |
| 151 |
{ |
| 152 |
const char *s = str; |
| 153 |
int i; |
| 154 |
|
| 155 |
for (i = 0; i < len; i++) { |
| 156 |
|
| 157 |
while (*s && isspace(*s)) |
| 158 |
s++; |
| 159 |
if (!*s) |
| 160 |
break; |
| 161 |
if (!parse_hex(buf, s)) |
| 162 |
break; |
| 163 |
|
| 164 |
|
| 165 |
s += 2; |
| 166 |
buf++; |
| 167 |
} |
| 168 |
|
| 169 |
if (i != len || *s) |
| 170 |
return false; |
| 171 |
return true; |
| 172 |
} |