aboutsummaryrefslogtreecommitdiff
path: root/blob2c.rst
blob: 5db5cc21f51611c4134c079cec8434261575f552 (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
.. RST source for blob2c(1) man page. Convert with:
..   rst2man.py blob2c.rst > blob2c.1
.. rst2man.py comes from the SBo development/docutils package.

======
blob2c
======

---------------------------------------------------
Create C source and header files from a binary file
---------------------------------------------------

.. include:: manhdr.rst

SYNOPSIS
========

blob2c *blobfile* > *output.c* 2> *output.h*

DESCRIPTION
===========

**blob2c**  prints  a  C  source file to standard output, containing
*an unsigned char* array, initialized to the contents of *blobfile*,
and an *int*, initialized to the length of *blobfile* in bytes.

The name of the array is based on the input filename, with
non-alphanumeric characters replaced by underscores. The name of the
int  is the array name, plus the string **_len**.

Also, a header file containing a pair of extern declarations is
printed to standard error output. This header may be included multiple
times in the  same translation unit, since it contains "guard"
preprocessor code to prevent multiple declarations.

**blob2c** takes no options, and requires exactly one argument  (the
input filename *blobfile*).

Exit  status  is  zero for success and non-zero for failure. Error
messages are printed to standard error output as preprocessor
**#error**  directives, since standard error is expected to be
redirected to a header file. The **#error** directives will cause the
error messages to be printed when the header file is compiled.

Although  it's  distributed  with  the  author's Atari 8-bit
utilities, there's nothing Atari-specific about **blob2c**. It could be
useful for any situation  where you need to include a file's contents
as an array in a C program.

EXAMPLE
=======

::

  $ echo "Hello, World." > hello.bin

  $ blob2c hello.bin >hello.c 2>hello.h

  ## check exit status (0=success)
  $ echo $?
  0

  $ cat hello.h
  /* C header created by blob2c from input file hello.bin */

  #ifndef hello_bin_H
  #define hello_bin_H

  extern unsigned char hello_bin[];
  extern int hello_bin_len;

  #endif  /* hello_bin_H */

  $ cat hello.c

  /* C source created by blob2c from input file hello.bin */

  unsigned char hello_bin[] = {
          /*      0 */  0x48,0x65,0x6c,0x6c,0x6f,0x2c,0x20,0x57, /* Hello, W */
          /*      8 */  0x6f,0x72,0x6c,0x64,0x2e,0x0a            /* orld.. */
  }; /* hello_bin */

  int hello_bin_len = 14;

.. include:: manftr.rst