Debian Codegraph @

suite/benchmark/load_bin.inc

raw
1 // From https://github.com/athre0z/disas-bench
2
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <time.h>
7
8
9 int read_file_data(uint8_t** buf, const char* filename, size_t file_offset, size_t bin_len)
10 {
11 FILE* f = fopen(filename, "rb");
12 if (!f) return 0;
13
14 // Seek to code section
15 if (fseek(f, (long)file_offset, SEEK_SET))
16 {
17 fclose(f);
18 return 0;
19 }
20
21 uint8_t* code = malloc(bin_len);
22 if (!code)
23 {
24 fclose(f);
25 return 0;
26 }
27
28 if (fread(code, 1, bin_len, f) != bin_len)
29 {
30 fclose(f);
31 free(code);
32 return 0;
33 }
34
35 *buf = code;
36
37 return 1;
38 }
39
40 int read_file(int argc, char *argv[], uint8_t **buf, size_t *bin_len, size_t *loop_count)
41 {
42 if (argc != 5)
43 {
44 fputs("Expected args: <loop-count> <code-offset> <code-len> <filename>\n", stderr);
45 return 0;
46 }
47
48 *loop_count = (size_t)strtoull(argv[1], NULL, 0);
49 size_t file_offset = (size_t)strtoull(argv[2], NULL, 0);
50 *bin_len = (size_t)strtoull(argv[3], NULL, 0);
51 const char *filename = argv[4];
52
53 if (!read_file_data(buf, filename, file_offset, *bin_len))
54 {
55 fprintf(stderr, "Couldn't read `%s`\n", filename);
56 return 0;
57 }
58
59 return 1;
60 }