-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin2c.c
More file actions
57 lines (44 loc) · 1.67 KB
/
bin2c.c
File metadata and controls
57 lines (44 loc) · 1.67 KB
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
////////////////////////////////////////////////////////////////////////////
// **** BIN2C **** //
// Binary to C-source converter //
// Copyright (c) 2024 David Bryant. //
// All Rights Reserved. //
// Distributed under the BSD Software License (see license.txt) //
////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef _WIN32
#include <fcntl.h>
#endif
#include "lzwlib.h"
#define BYTES_PER_LINE 16
int main (int argc, char **argv)
{
int num_bytes = 0, alloced_bytes = 0, ch;
unsigned char *buffer = NULL;
#ifdef _WIN32
setmode (fileno (stdin), O_BINARY);
#endif
while ((ch = getchar ()) != EOF) {
if (num_bytes == alloced_bytes)
buffer = realloc (buffer, alloced_bytes += 65536);
buffer [num_bytes++] = ch;
}
printf ("static unsigned char %s [%d] = {\n", argc == 2 ? argv [1] : "array", num_bytes);
for (int i = 0; i < num_bytes; i += BYTES_PER_LINE) {
char string [256] = { 0 };
strcat (string, " ");
for (int j = 0; i + j < num_bytes && j < BYTES_PER_LINE; ++j) {
sprintf (string + strlen (string), "0x%02x", buffer [i+j]);
if (i + j < num_bytes - 1)
strcat (string, ",");
if (i + j < num_bytes - 1 && j < BYTES_PER_LINE - 1)
strcat (string, " ");
}
printf ("%s\n", string);
}
printf ("};\n");
free (buffer);
return 0;
}