summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/test-tools/binarydump-tool/binarydump.c
blob: 050de6dfa4c7b9121fe9d701fb8e524da0cdf9cf (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2019 Intel Corporation. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

static unsigned char *
read_file_to_buffer(const char *filename, int *ret_size)
{
    unsigned char *buffer;
    FILE *file;
    int file_size, read_size;

    if (!(file = fopen(filename, "r")))
        return NULL;

    fseek(file, 0, SEEK_END);
    file_size = ftell(file);
    fseek(file, 0, SEEK_SET);

    if (!(buffer = malloc(file_size))) {
        fclose(file);
        return NULL;
    }

    read_size = fread(buffer, 1, file_size, file);
    fclose(file);

    if (read_size < file_size) {
        free(buffer);
        return NULL;
    }

    *ret_size = file_size;

    return buffer;
}

static int
print_help()
{
    printf("Usage: binarydump -o <file> -n <name> input_file\n");
    printf("Options:\n");
    printf("  -o <file>      Place the output into <file>\n");
    printf("  -n <name>      The name of array <file>\n");

    return -1;
}

static bool
bin_file_dump(const unsigned char *file, int size, const char *bin_file_output,
              const char *array_name)
{
    unsigned i = 0;
    const unsigned char *p = file, *p_end = file + size;
    FILE *file_output = fopen(bin_file_output, "wb+");

    if (!file_output)
        return false;

    fprintf(file_output, "\nunsigned char __aligned(4) %s[] = {\n  ",
            array_name);

    while (p < p_end) {
        fprintf(file_output, "0x%02X", *p++);

        if (p == p_end)
            break;

        fprintf(file_output, ",");

        if ((++i % 12) != 0)
            fprintf(file_output, " ");
        else
            fprintf(file_output, "\n  ");
    }

    fprintf(file_output, "\n};\n");

    fclose(file_output);
    return true;
}

int
main(int argc, char *argv[])
{
    unsigned char *file;
    int size;
    bool ret;
    const char *bin_file_input, *array_file_output = NULL, *array_name = NULL;

    for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
        if (!strcmp(argv[0], "-o")) {
            ++argv;
            if (--argc == 0)
                return print_help();
            array_file_output = *argv;
        }
        else if (!strcmp(argv[0], "-n")) {
            ++argv;
            if (--argc == 0)
                return print_help();
            array_name = *argv;
        }
        else
            return print_help();
    }

    if (!array_file_output || !array_name)
        return print_help();

    bin_file_input = *argv;

    if (!(file = read_file_to_buffer(bin_file_input, &size)))
        return -1;

    ret = bin_file_dump(file, size, array_file_output, array_name);

    free(file);

    return ret ? 0 : -1;
}