| 1 |
|
| 2 |
|
| 3 |
#include "data.h" |
| 4 |
|
| 5 |
#include <ctype.h> |
| 6 |
#include <limits.h> |
| 7 |
#include <stdint.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "utils.h" |
| 13 |
|
| 14 |
void print_data(const uint8_t data[], size_t data_size, enum data_type type) |
| 15 |
{ |
| 16 |
if (data_size == 0) |
| 17 |
return; |
| 18 |
|
| 19 |
switch (type) { |
| 20 |
case DATA_TYPE_BOOL: |
| 21 |
bool value = false; |
| 22 |
for (size_t i = 0; i < data_size; ++i) { |
| 23 |
if (data[i] != 0) { |
| 24 |
value = true; |
| 25 |
break; |
| 26 |
} |
| 27 |
} |
| 28 |
printf("%s\n", value ? "true" : "false"); |
| 29 |
break; |
| 30 |
case DATA_TYPE_UINT8: |
| 31 |
if (data_size != 1) { |
| 32 |
fprintf(stderr, |
| 33 |
"warning: expected size of 1, got %zu\n", |
| 34 |
data_size); |
| 35 |
} |
| 36 |
|
| 37 |
if (data_size >= 1) |
| 38 |
printf("%u\n", *(uint8_t *)data); |
| 39 |
break; |
| 40 |
case DATA_TYPE_UINT16: |
| 41 |
if (data_size != 2) { |
| 42 |
fprintf(stderr, |
| 43 |
"warning: expected size of 2, got %zu\n", |
| 44 |
data_size); |
| 45 |
} |
| 46 |
|
| 47 |
if (data_size >= 2) |
| 48 |
printf("%u\n", *(uint16_t *)data); |
| 49 |
break; |
| 50 |
case DATA_TYPE_UINT32: |
| 51 |
if (data_size != 4) { |
| 52 |
fprintf(stderr, |
| 53 |
"warning: expected size of 4, got %zu\n", |
| 54 |
data_size); |
| 55 |
} |
| 56 |
|
| 57 |
if (data_size >= 4) |
| 58 |
printf("%u\n", *(uint32_t *)data); |
| 59 |
break; |
| 60 |
case DATA_TYPE_UINT64: |
| 61 |
if (data_size != 8) { |
| 62 |
fprintf(stderr, |
| 63 |
"warning: expected size of 8, got %zu\n", |
| 64 |
data_size); |
| 65 |
} |
| 66 |
|
| 67 |
if (data_size >= 8) |
| 68 |
printf("%llu\n", (unsigned long long)*(uint64_t *)data); |
| 69 |
break; |
| 70 |
case DATA_TYPE_ASCII: |
| 71 |
for (size_t i = 0; i < data_size; ++i) { |
| 72 |
char c = data[i]; |
| 73 |
if (isprint(c)) |
| 74 |
printf("%c", c); |
| 75 |
} |
| 76 |
printf("\n"); |
| 77 |
break; |
| 78 |
case DATA_TYPE_UNICODE: |
| 79 |
char *chars = to_chars((const CHAR16 *)data, data_size); |
| 80 |
printf("%s\n", chars); |
| 81 |
free(chars); |
| 82 |
break; |
| 83 |
case DATA_TYPE_FILE: |
| 84 |
fprintf(stderr, "File data type is input only\n"); |
| 85 |
break; |
| 86 |
case DATA_TYPE_RAW: |
| 87 |
fwrite(data, 1, data_size, stdout); |
| 88 |
break; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
static uint64_t parse_uint(const char source[], |
| 93 |
const char type[], |
| 94 |
unsigned long long max, |
| 95 |
bool *failed) |
| 96 |
{ |
| 97 |
char *end; |
| 98 |
unsigned long long uint = strtoull(source, &end, 0); |
| 99 |
if (*end != '\0') { |
| 100 |
fprintf(stderr, "Trailing characters in \"%s\": %s\n", |
| 101 |
source, end); |
| 102 |
*failed = true; |
| 103 |
return 0; |
| 104 |
} |
| 105 |
if (uint > max) { |
| 106 |
fprintf(stderr, "Invalid %s value: %llu\n", type, uint); |
| 107 |
*failed = true; |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
*failed = false; |
| 112 |
return uint; |
| 113 |
} |
| 114 |
|
| 115 |
void *make_data(const char source[], size_t *data_size, enum data_type type) |
| 116 |
{ |
| 117 |
switch (type) { |
| 118 |
void *data; |
| 119 |
bool boolean; |
| 120 |
uint64_t uint; |
| 121 |
struct mem_range_t file; |
| 122 |
bool failed; |
| 123 |
|
| 124 |
case DATA_TYPE_BOOL: |
| 125 |
if (str_eq(source, "true")) { |
| 126 |
boolean = true; |
| 127 |
} else if (str_eq(source, "false")) { |
| 128 |
boolean = false; |
| 129 |
} else { |
| 130 |
fprintf(stderr, "Invalid boolean value: \"%s\"\n", |
| 131 |
source); |
| 132 |
return NULL; |
| 133 |
} |
| 134 |
|
| 135 |
*data_size = 1; |
| 136 |
data = xmalloc(*data_size); |
| 137 |
*(uint8_t *)data = boolean; |
| 138 |
return data; |
| 139 |
case DATA_TYPE_UINT8: |
| 140 |
uint = parse_uint(source, "uint8", UINT8_MAX, &failed); |
| 141 |
if (failed) |
| 142 |
return NULL; |
| 143 |
|
| 144 |
*data_size = 1; |
| 145 |
data = xmalloc(*data_size); |
| 146 |
*(uint8_t *)data = uint; |
| 147 |
return data; |
| 148 |
case DATA_TYPE_UINT16: |
| 149 |
uint = parse_uint(source, "uint16", UINT16_MAX, &failed); |
| 150 |
if (failed) |
| 151 |
return NULL; |
| 152 |
|
| 153 |
*data_size = 2; |
| 154 |
data = xmalloc(*data_size); |
| 155 |
*(uint16_t *)data = uint; |
| 156 |
return data; |
| 157 |
case DATA_TYPE_UINT32: |
| 158 |
uint = parse_uint(source, "uint32", UINT32_MAX, &failed); |
| 159 |
if (failed) |
| 160 |
return NULL; |
| 161 |
|
| 162 |
*data_size = 4; |
| 163 |
data = xmalloc(*data_size); |
| 164 |
*(uint32_t *)data = uint; |
| 165 |
return data; |
| 166 |
case DATA_TYPE_UINT64: |
| 167 |
uint = parse_uint(source, "uint64", UINT64_MAX, &failed); |
| 168 |
if (failed) |
| 169 |
return NULL; |
| 170 |
|
| 171 |
*data_size = 8; |
| 172 |
data = xmalloc(*data_size); |
| 173 |
*(uint64_t *)data = uint; |
| 174 |
return data; |
| 175 |
case DATA_TYPE_ASCII: |
| 176 |
*data_size = strlen(source) + 1; |
| 177 |
return strdup(source); |
| 178 |
case DATA_TYPE_UNICODE: |
| 179 |
return to_uchars(source, data_size); |
| 180 |
case DATA_TYPE_FILE: |
| 181 |
file = map_file(source, false); |
| 182 |
if (file.start == NULL || file.length == 0) |
| 183 |
return NULL; |
| 184 |
|
| 185 |
*data_size = file.length; |
| 186 |
data = xmalloc(*data_size); |
| 187 |
memcpy(data, file.start, *data_size); |
| 188 |
unmap_file(file); |
| 189 |
return data; |
| 190 |
case DATA_TYPE_RAW: |
| 191 |
fprintf(stderr, "Raw data type is output only\n"); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
bool parse_data_type(const char str[], enum data_type *type) |
| 199 |
{ |
| 200 |
if (str_eq(str, "bool")) |
| 201 |
*type = DATA_TYPE_BOOL; |
| 202 |
else if (str_eq(str, "uint8")) |
| 203 |
*type = DATA_TYPE_UINT8; |
| 204 |
else if (str_eq(str, "uint16")) |
| 205 |
*type = DATA_TYPE_UINT16; |
| 206 |
else if (str_eq(str, "uint32")) |
| 207 |
*type = DATA_TYPE_UINT32; |
| 208 |
else if (str_eq(str, "uint64")) |
| 209 |
*type = DATA_TYPE_UINT64; |
| 210 |
else if (str_eq(str, "ascii")) |
| 211 |
*type = DATA_TYPE_ASCII; |
| 212 |
else if (str_eq(str, "unicode")) |
| 213 |
*type = DATA_TYPE_UNICODE; |
| 214 |
else if (str_eq(str, "file")) |
| 215 |
*type = DATA_TYPE_FILE; |
| 216 |
else if (str_eq(str, "raw")) |
| 217 |
*type = DATA_TYPE_RAW; |
| 218 |
else |
| 219 |
return false; |
| 220 |
|
| 221 |
return true; |
| 222 |
} |