FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / utils / zenutils / source / firmware_extract / main.cpp
blob1f31cc86ebb7b8f5dd3614e795679108306d646d
1 /* zenutils - Utilities for working with creative firmwares.
2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <iostream>
20 #include <ctime>
21 #include <getpot/GetPot>
22 #include <utils.h>
23 #include <firmware.h>
26 static const char VERSION[] = "0.1";
29 void print_version()
31 std::cout
32 << "firmware_extract - Extracts files from a Creative firmware."
33 << std::endl
34 << "Version " << VERSION << std::endl
35 << "Copyright (c) 2007 Rasmus Ry" << std::endl;
38 void print_help()
40 print_version();
41 std::cout
42 << "Usage: firmware_extract [command] [options]" << std::endl
43 << std::endl
44 << " Commands:" << std::endl
45 << " -h,--help" << std::endl
46 << " prints this message." << std::endl
47 << " -f,--firmware [file]" << std::endl
48 << " specifies the firmware arhive file name." << std::endl
49 << std::endl
50 << " Options:" << std::endl
51 << " -V,--verbose" << std::endl
52 << " prints verbose messages." << std::endl
53 << " -p,--prefix [prefix]" << std::endl
54 << " specifies a file name prefix for the extracted files." << std::endl
55 << std::endl
60 struct save_entry_functor
62 save_entry_functor(const std::string& fileprefix)
63 : _fileprefix(fileprefix) {}
65 bool operator()(const zen::firmware_entry& entry)
67 std::string filename = _fileprefix + entry.get_content_name();
68 std::ofstream ofs;
69 ofs.open(filename.c_str(), std::ios::binary);
70 if (!ofs)
71 false;
73 size_t off = entry.get_content_offset();
74 std::streamsize size = entry.get_bytes().size() - off;
75 ofs.write((const char*)&entry.get_bytes()[off], size);
77 return ofs.good();
80 const std::string& _fileprefix;
81 }; //struct save_entry_functor
83 struct print_entry_functor
85 print_entry_functor(std::ostream& os, const std::string& fileprefix)
86 : _os(os), _fileprefix(fileprefix), num(0) {}
88 bool operator()(const zen::firmware_entry& entry)
90 std::string filename = _fileprefix + entry.get_content_name();
91 if (!num)
92 _os << "[./" << num++ << "]" << std::endl;
93 else
94 _os << "[../" << num++ << "]" << std::endl;
95 _os << "tag = " << entry.get_name() << std::endl;
97 if (entry.get_content_offset())
98 _os << "name = " << entry.get_content_name() << std::endl;
100 _os << "file = \'" << shared::double_quote(filename) << "\'"
101 << std::endl;
103 return _os.good();
106 std::ostream& _os;
107 const std::string& _fileprefix;
108 int num;
109 }; //struct print_entry_functor
112 int process_arguments(int argc, char* argv[])
114 //--------------------------------------------------------------------
115 // Parse input variables.
116 //--------------------------------------------------------------------
118 GetPot cl(argc, argv);
119 if (cl.size() == 1 || cl.search(2, "-h", "--help"))
121 print_help();
122 return 1;
125 std::string firmwarename;
126 if (cl.search("-f") || cl.search("--firmware"))
127 firmwarename = cl.next("");
128 if (firmwarename.empty())
130 std::cerr << "Firmware archive must be specified." << std::endl;
131 return 2;
134 bool verbose = false;
135 if (cl.search("-V") || cl.search("--verbose"))
136 verbose = true;
138 std::string prefixname = shared::remove_extension(firmwarename) + "_";
139 if (cl.search("-p") || cl.search("--prefix"))
140 prefixname = cl.next(prefixname.c_str());
143 //--------------------------------------------------------------------
144 // Read the firmware archive.
145 //--------------------------------------------------------------------
147 if (verbose)
148 std::cout << "[*] Reading firmware archive..." << std::endl;
150 zen::firmware_archive archive(false);
151 std::ifstream ifs;
152 ifs.open(firmwarename.c_str(), std::ios::binary);
153 if (!ifs)
155 std::cerr << "Failed to open the firmware archive." << std::endl;
156 return 3;
159 if (!archive.read(ifs))
161 std::cerr << "Failed to read the firmware archive." << std::endl;
162 return 4;
166 //--------------------------------------------------------------------
167 // Generate a make file for the extracted firmware archive.
168 //--------------------------------------------------------------------
170 // Get make filename for the given input file.
171 std::string makefile = shared::replace_extension(firmwarename, ".mk");
173 if (verbose)
174 std::cout << "[*] Producing make file..." << std::endl;
177 // Produce make file for the given input file.
178 std::ofstream ofs;
179 ofs.open(makefile.c_str(), std::ios::binary);
180 if (!ofs)
182 std::cerr << "Failed to create firmware archive make file."
183 << std::endl;
184 return 5;
187 time_t timeval = time(NULL);
188 ofs << "# Make file generated at: " << ctime(&timeval);
189 ofs << "endian = " << (archive.is_big_endian() ? "big" : "little")
190 << std::endl;
191 ofs << "signed = " << (archive.is_signed() ? "true" : "false")
192 << std::endl;
194 ofs << "[children]" << std::endl;
195 ofs << "count = " << archive.get_children().size() << std::endl;
197 std::for_each(archive.get_children().begin(),
198 archive.get_children().end(),
199 print_entry_functor(ofs, prefixname));
201 ofs << "[neighbours]" << std::endl;
202 ofs << "count = " << archive.get_neighbours().size() << std::endl;
203 std::for_each(archive.get_neighbours().begin(),
204 archive.get_neighbours().end(),
205 print_entry_functor(ofs, prefixname));
208 //--------------------------------------------------------------------
209 // Save firmware entries.
210 //--------------------------------------------------------------------
212 if (verbose)
213 std::cout << "[*] Saving firmware entries..." << std::endl;
215 std::for_each(archive.get_children().begin(),
216 archive.get_children().end(),
217 save_entry_functor(prefixname));
219 std::for_each(archive.get_neighbours().begin(),
220 archive.get_neighbours().end(),
221 save_entry_functor(prefixname));
223 return 0;
226 int main(int argc, char* argv[])
230 return process_arguments(argc, argv);
232 catch (const std::exception& xcpt)
234 std::cerr << "Exception caught: " << xcpt.what() << std::endl;
235 return -1;
237 catch (...)
239 std::cerr << "Unknown exception caught." << std::endl;
240 return -2;
242 return -3;