xref: /third_party/ffmpeg/ffbuild/bin2c.c (revision cabdff1a)
1/*
2 * This file is part of FFmpeg.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23#include <string.h>
24#include <stdio.h>
25
26int main(int argc, char **argv)
27{
28    const char *name;
29    FILE *input, *output;
30    unsigned int length = 0;
31    unsigned char data;
32
33    if (argc < 3 || argc > 4)
34        return 1;
35
36    input = fopen(argv[1], "rb");
37    if (!input)
38        return -1;
39
40    output = fopen(argv[2], "wb");
41    if (!output)
42        return -1;
43
44    if (argc == 4) {
45        name = argv[3];
46    } else {
47        size_t arglen = strlen(argv[1]);
48        name = argv[1];
49
50        for (int i = 0; i < arglen; i++) {
51            if (argv[1][i] == '.')
52                argv[1][i] = '_';
53            else if (argv[1][i] == '/')
54                name = &argv[1][i+1];
55        }
56    }
57
58    fprintf(output, "const unsigned char ff_%s_data[] = { ", name);
59
60    while (fread(&data, 1, 1, input) > 0) {
61        fprintf(output, "0x%02x, ", data);
62        length++;
63    }
64
65    fprintf(output, "0x00 };\n");
66    fprintf(output, "const unsigned int ff_%s_len = %u;\n", name, length);
67
68    fclose(output);
69
70    if (ferror(input) || !feof(input))
71        return -1;
72
73    fclose(input);
74
75    return 0;
76}
77