LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
bin2s.c
Go to the documentation of this file.
1/*---------------------------------------------------------------------------------
2
3 bin2s: convert a binary file to a gcc asm module
4 for gfx/foo.bin it'll write foo_bin (an array of char)
5 foo_bin_end, and foo_bin_len (an unsigned int)
6 for 4bit.chr it'll write _4bit_chr, _4bit_chr_end, and
7 _4bit_chr_len
8
9
10 Copyright 2003 - 2005 Damian Yerrick
11
12 Permission is hereby granted, free of charge, to any person obtaining
13 a copy of this software and associated documentation files (the
14 "Software"), to deal in the Software without restriction, including
15 without limitation the rights to use, copy, modify, merge, publish,
16 distribute, sublicense, and/or sell copies of the Software, and to
17 permit persons to whom the Software is furnished to do so, subject to
18 the following conditions:
19
20 The above copyright notice and this permission notice shall be
21 included in all copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
28 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
29 OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 IN THE SOFTWARE.
31
32---------------------------------------------------------------------------------*/
33
34
35
36/*
37.align
38.global SomeLabel_len
39.int 1234
40.global SomeLabel
41.byte blah,blah,blah,blah...
42*/
43
44#include <stdio.h>
45#include <stdlib.h>
46#include <ctype.h>
47
48/*---------------------------------------------------------------------------------
49 Print the closest valid C identifier to a given word.
50---------------------------------------------------------------------------------*/
51void strnident(FILE *fout, const char *src) {
52//---------------------------------------------------------------------------------
53 char got_first = 0;
54
55 while(*src != 0) {
56
57 int s = *src++;
58
59 /* initial digit */
60 if(isdigit(s) && !got_first)
61 fputc('_', fout); /* stick a '_' before an initial digit */
62
63 /* convert only out-of-range characters */
64 if(!isalpha(s) && !isdigit(s) && (s != '_')) {
65 if(s == '-' || s == '.' || s == '/') s = '_';
66 else
67 s = 0;
68 }
69
70 if(s) {
71 fputc(s, fout);
72 got_first = 1;
73 }
74 }
75}
76
77
78//---------------------------------------------------------------------------------
79int main(int argc, char **argv) {
80//---------------------------------------------------------------------------------
81 FILE *fin;
82 size_t filelen;
83 int linelen;
84 int arg;
85 int alignment = 4;
86 if(argc < 2) {
87 fputs( "bin2s - convert binary files to assembly language\n"
88 "typical usage: bin2s foo.bin bar.bin baz.bin > foo.s\n", stderr);
89 return 1;
90 }
91
92 for(arg = 1; arg < argc; arg++) {
93
94 if (argv[arg][0] == '-')
95 {
96 switch (argv[arg][1])
97 {
98 case 'a':
99
100 alignment = (argc > arg) ? strtoul(argv[++arg], 0, 0) : 0;
101
102 if ( alignment == 0 ) alignment =4;
103 break;
104 }
105 continue;
106 }
107
108
109 fin = fopen(argv[arg], "rb");
110
111 if(!fin) {
112 fputs("bin2s: could not open ", stderr);
113 perror(argv[arg]);
114 return 1;
115 }
116
117 fseek(fin, 0, SEEK_END);
118 filelen = ftell(fin);
119 rewind(fin);
120
121 if(filelen == 0) {
122 fclose(fin);
123 fprintf(stderr, "bin2s: warning: skipping empty file %s\n", argv[arg]);
124 continue;
125 }
126
127 char *ptr = argv[arg];
128 char chr;
129 char *filename = NULL;
130
131 while ( (chr=*ptr) ) {
132
133 if ( chr == '\\' || chr == '/') {
134
135 filename = ptr;
136 }
137
138 ptr++;
139 }
140
141 if ( NULL != filename ) {
142 filename++;
143 } else {
144 filename = argv[arg];
145 }
146
147 /*---------------------------------------------------------------------------------
148 Generate the prolog for each included file. It has two purposes:
149
150 1. provide length info, and
151 2. align to user defined boundary, default is 32bit
152
153 ---------------------------------------------------------------------------------*/
154 fprintf( stdout, "/* Generated by BIN2S - please don't edit directly */\n"
155 "\t.section .rodata\n"
156 "\t.balign %d\n"
157 "\t.global ", alignment);
158 strnident(stdout, filename);
159 fputs("_size\n", stdout);
160 fputs("\t.global ", stdout);
161 strnident(stdout, filename);
162 fputs("\n", stdout);
163 strnident(stdout, filename);
164 fputs(":\n\t.byte ", stdout);
165
166 linelen = 0;
167
168 int count = filelen;
169
170 while(count > 0) {
171 unsigned char c = fgetc(fin);
172
173 printf("%3u", (unsigned int)c);
174 count--;
175
176 /* don't put a comma after the last item */
177 if(count) {
178
179 /* break after every 16th number */
180 if(++linelen >= 16) {
181 linelen = 0;
182 fputs("\n\t.byte ", stdout);
183 } else {
184 fputc(',', stdout);
185 }
186 }
187 }
188
189 fputs("\n\n\t.global ", stdout);
190 strnident(stdout, filename);
191 fputs("_end\n", stdout);
192 strnident(stdout, filename);
193 fputs("_end:\n\n", stdout);
194 fputs("\t.align\n",stdout);
195 strnident(stdout, filename);
196 fprintf( stdout, "_size: .int %lu\n", (unsigned long)filelen);
197
198 fclose(fin);
199 }
200
201 return 0;
202}
int main(int argc, char **argv)
Definition: bin2s.c:79
void strnident(FILE *fout, const char *src)
Definition: bin2s.c:51
#define NULL
Definition: def.h:47
#define isdigit(c)
Definition: ip_addr.c:116
u32 ptr
Definition: iso9660.c:536
u8 c
Definition: xenos_edid.h:7