summaryrefslogtreecommitdiffstatshomepage
path: root/src/tools/image_handler.cpp
blob: 14976516e1ce43ec75d85faf777f01e8fcb73aa0 (plain) (blame)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert

// Image generic handler class and helpers

#include "image_handler.h"

#include "formats/all.h"
#include "formats/fsblk_vec.h"
#include "formats/fs_unformatted.h"

#include "ioprocs.h"
#include "ioprocsfill.h"
#include "ioprocsvec.h"
#include "multibyte.h"
#include "strformat.h"

#include <algorithm>


// Format enumeration

namespace {
	struct enumerator : public mame_formats_enumerator {
		formats_table *m_table;
		std::string m_category;

		enumerator(formats_table *table) : mame_formats_enumerator(), m_table(table), m_category("None") {}

		virtual ~enumerator() = default;
		virtual void add(const cassette_image::Format *const *formats) {}

		virtual void category(const char *name) {
			m_category = name;
		}

		virtual void add(const floppy_image_format_t &format) {
			m_table->floppy_format_infos.emplace_back(std::make_unique<floppy_format_info>(&format, m_category));
		}

		virtual void add(const fs::manager_t &fs) {
			m_table->filesystem_formats.emplace_back(std::make_unique<filesystem_format>(&fs, m_category));
		}
	};

	struct fs_enum : public fs::manager_t::floppy_enumerator {
		filesystem_format *m_format;
		fs_enum(filesystem_format *format, u32 form_factor, const std::vector<u32> &variants) : fs::manager_t::floppy_enumerator(form_factor, variants), m_format(format) {}

		virtual void add_format(const floppy_image_format_t &type, u32 image_size, const char *name, const char *description) override {
			m_format->m_floppy = true;
			m_format->m_floppy_create.emplace_back(std::make_unique<floppy_create_info>(m_format->m_manager, &type, image_size, name, description));
		}

		virtual void add_raw(const char *name, u32 key, const char *description) override {
			m_format->m_floppy_raw = true;
			m_format->m_floppy_create.emplace_back(std::make_unique<floppy_create_info>(name, key, description));
		}
	};
}

void formats_table::init()
{
	std::vector<uint32_t> variants;

	enumerator en(this);
	mame_formats_full_list(en);

	for(auto &f : filesystem_formats) {
		fs_enum fen(f.get(), floppy_image::FF_UNKNOWN, variants);
		f->m_manager->enumerate_f(fen);
	}

	for(auto &f : floppy_format_infos) {
		auto *ff = f.get();
		std::string key = ff->m_format->name();
		auto i = floppy_format_info_by_key.find(key);
		if(i != floppy_format_info_by_key.end()) {
			fprintf(stderr, "Collision on floppy format name %s between \"%s\" and \"%s\".\n",
					ff->m_format->name(),
					ff->m_format->description(),
					i->second->m_format->description());
			exit(1);
		}

		floppy_format_info_by_key[key] = ff;
		floppy_format_info_by_category[ff->m_category].push_back(ff);
	}

	for(auto &f : filesystem_formats) {
		auto *ff = f.get();
		std::string key = ff->m_manager->name();
		auto i = filesystem_format_by_key.find(key);
		if(i != filesystem_format_by_key.end()) {
			fprintf(stderr, "Collision on filesystem name %s between \"%s\" and \"%s\".\n",
					ff->m_manager->name(),
					ff->m_manager->description(),
					i->second->m_manager->description());
			exit(1);
		}

		filesystem_format_by_key[key] = ff;
		filesystem_format_by_category[ff->m_category].push_back(ff);

		for(auto &f2 : ff->m_floppy_create) {
			auto *ff2 = f2.get();
			key = ff2->m_name;
			auto i = floppy_create_info_by_key.find(key);
			if(i != floppy_create_info_by_key.end()) {
				fprintf(stderr, "Collision on floppy create name %s between \"%s\" and \"%s\".\n",
						ff2->m_name,
						ff2->m_description,
						i->second->m_description);
				exit(1);
			}

			floppy_create_info_by_key[key] = ff2;
		}
	}
}

const floppy_format_info *formats_table::find_floppy_format_info_by_key(const std::string &key) const
{
	auto i = floppy_format_info_by_key.find(key);
	return i == floppy_format_info_by_key.end() ? nullptr : i->second;
}

const filesystem_format *formats_table::find_filesystem_format_by_key(const std::string &key) const
{
	auto i = filesystem_format_by_key.find(key);
	return i == filesystem_format_by_key.end() ? nullptr : i->second;
}

const floppy_create_info *formats_table::find_floppy_create_info_by_key(const std::string &key) const
{
	auto i = floppy_create_info_by_key.find(key);
	return i == floppy_create_info_by_key.end() ? nullptr : i->second;
}


// Image handling

std::vector<u8> image_handler::fload(std::string path)
{
	auto fi = fopen(path.c_str(), "rb");
	if(!fi) {
		perror(util::string_format("Error opening %s for reading", path).c_str());
		exit(1);
	}
	fseek(fi, 0, SEEK_END);
	long size = ftell(fi);
	std::vector<u8> filedata(size);
	fseek(fi, 0, SEEK_SET);
	fread(filedata.data(), filedata.size(), 1, fi);
	fclose(fi);

	return filedata;
}

std::vector<u8> image_handler::fload_rsrc(std::string path)
{
	auto filedata = fload(path);
	const u8 *head = filedata.data();

	if(get_u32be(head+0x00) == 0x00051607 &&
	   get_u32be(head+0x04) == 0x00020000) {
		u16 nent = get_u16be(head+0x18);
		for(u16 i=0; i != nent; i++) {
			const u8 *e = head + 12*i;
			if(get_u32be(e+0) == 2) {
				u32 start = get_u32be(e+4);
				u32 len = get_u32be(e+8);
				filedata.erase(filedata.begin(), filedata.begin() + start);
				filedata.erase(filedata.begin() + len, filedata.end());
				return filedata;
			}
		}
	}
	filedata.clear();
	return filedata;
}

void image_handler::fsave(std::string path, const std::vector<u8> &data)
{
	auto fo = fopen(path.c_str(), "wb");
	if(!fo) {
		perror(util::string_format("Error opening %s for writing", path).c_str());
		exit(1);
	}

	fwrite(data.data(), data.size(), 1, fo);
	fclose(fo);
}

void image_handler::fsave_rsrc(std::string path, const std::vector<u8> &data)
{
	u8 head[0x2a];

	put_u32be(head+0x00, 0x00051607);   // Magic
	put_u32be(head+0x04, 0x00020000);   // Version
	memset(head+0x08, 0, 16);           // Filler
	put_u16be(head+0x18, 1);            // Number of entries
	put_u32be(head+0x1a, 2);            // Resource fork
	put_u32be(head+0x22, 0x2a);         // Offset in the file
	put_u32be(head+0x26, data.size());  // Length

	auto fo = fopen(path.c_str(), "wb");
	if(!fo) {
		perror(util::string_format("Error opening %s for writing", path).c_str());
		exit(1);
	}

	fwrite(head, sizeof(head), 1, fo);
	fwrite(data.data(), data.size(), 1, fo);
	fclose(fo);
}

image_handler::image_handler() : m_floppy_image(84, 2, floppy_image::FF_UNKNOWN)
{
}

void image_handler::set_on_disk_path(std::string path)
{
	m_on_disk_path = path;
}

std::vector<std::pair<u8, const floppy_format_info *>> image_handler::identify(const formats_table &formats)
{
	std::vector<std::pair<u8, const floppy_format_info *>> res;
	std::vector<uint32_t> variants;

	FILE *f = fopen(m_on_disk_path.c_str(), "rb");
	if (!f) {
		std::string msg = util::string_format("Error opening %s for reading", m_on_disk_path);
		perror(msg.c_str());
		return res;
	}

	auto io = util::stdio_read(f, 0xff);

	for(const auto &e : formats.floppy_format_info_by_key) {
		u8 score = e.second->m_format->identify(*io, floppy_image::FF_UNKNOWN, variants);
		if(score && e.second->m_format->extension_matches(m_on_disk_path.c_str()))
			score |= floppy_image_format_t::FIFID_EXT;
		if(score)
			res.emplace_back(std::make_pair(score, e.second));
	}

	// Sort results by decreasing score
	std::stable_sort(res.begin(), res.end(), [](const auto &a, const auto &b) { return a.first > b.first; });

	return res;
}

bool image_handler::floppy_load(const floppy_format_info &format)
{
	std::vector<uint32_t> variants;
	FILE *f = fopen(m_on_disk_path.c_str(), "rb");
	if (!f) {
		std::string msg = util::string_format("Error opening %s for reading", m_on_disk_path);
		perror(msg.c_str());
		return true;
	}

	auto io = util::stdio_read(f, 0xff);

	return !format.m_format->load(*io, floppy_image::FF_UNKNOWN, variants, m_floppy_image);
}

bool image_handler::floppy_save(const floppy_format_info &format) const
{
	std::vector<uint32_t> variants;
	FILE *f = fopen(m_on_disk_path.c_str(), "wb");
	if (!f) {
		auto msg = util::string_format("Error opening %s for writing", m_on_disk_path);
		perror(msg.c_str());
		return true;
	}

	auto io = util::stdio_read_write(f, 0xff);

	return !format.m_format->save(*io, variants, m_floppy_image);
}

void image_handler::floppy_create(const floppy_create_info &format, fs::meta_data meta)
{
	if(format.m_type) {
		std::vector<uint32_t> variants;
		std::vector<u8> img(format.m_image_size);
		fs::fsblk_vec_t blockdev(img);
		auto fs = format.m_manager->mount(blockdev);
		fs->format(meta);

		auto io = util::ram_read(img.data(), img.size(), 0xff);
		format.m_type->load(*io, floppy_image::FF_UNKNOWN, variants, m_floppy_image);
	} else {
		fs::unformatted_image::format(format.m_key, &m_floppy_image);
	}
}

bool image_handler::floppy_mount_fs(const filesystem_format &format)
{
	// Create a restricted copy of the format list based on the known form factor and variant
	filesystem_format fmtcopy(format.m_manager, format.m_category);
	std::vector<u32> const var(1, m_floppy_image.get_variant());
	fs_enum fen(&fmtcopy, m_floppy_image.get_form_factor(), var);
	format.m_manager->enumerate_f(fen);

	m_floppy_fs_converter = nullptr;
	for(const auto &ci : fmtcopy.m_floppy_create) {
		if(ci->m_type != m_floppy_fs_converter) {
			std::vector<uint32_t> variants;
			m_floppy_fs_converter = ci->m_type;
			m_sector_image.clear();
			util::random_read_write_fill_wrapper<util::vector_read_write_adapter<u8>, 0xff> io(m_sector_image);
			m_floppy_fs_converter->save(io, variants, m_floppy_image);
		}

		if(ci->m_image_size == m_sector_image.size())
			goto success;
	}
	m_floppy_fs_converter = nullptr;
	m_sector_image.clear();
	return true;

 success:
	m_fsblk.reset(new fs::fsblk_vec_t(m_sector_image));
	m_fsm = format.m_manager;
	m_fs = m_fsm->mount(*m_fsblk);
	return false;
}

bool image_handler::hd_mount_fs(const filesystem_format &format)
{
	// Should use the chd mechanisms, one thing at a time...

	m_sector_image = fload(m_on_disk_path);
	m_fsblk.reset(new fs::fsblk_vec_t(m_sector_image));
	m_fsm = format.m_manager;
	m_fs = m_fsm->mount(*m_fsblk);
	return false;
}

void image_handler::fs_to_floppy()
{
	std::vector<uint32_t> variants;
	auto io = util::ram_read(m_sector_image.data(), m_sector_image.size(), 0xff);
	m_floppy_fs_converter->load(*io, floppy_image::FF_UNKNOWN, variants, m_floppy_image);
}

std::vector<std::string> image_handler::path_split(std::string path) const
{
	std::string opath = path;
	std::vector<std::string> rpath;
	if(m_fsm->has_subdirectories()) {
		std::string element;
		char sep = m_fsm->directory_separator();
		for(char c : opath) {
			if(c == sep) {
				if(!element.empty()) {
					rpath.push_back(element);
					element.clear();
				}
			} else
				element += c;
		}
		if(!element.empty())
			rpath.push_back(element);

	} else
		rpath.push_back(opath);

	return rpath;
}

bool image_handler::fexists(std::string path)
{
	auto f = fopen(path.c_str(), "rb");
	if(f != nullptr) {
		fclose(f);
		return true;
	}
	return false;
}


std::string image_handler::path_make_rsrc(std::string path)
{
	auto p = path.end();
	while(p != path.begin() && p[-1] != '/')
		p--;
	std::string rpath(path.begin(), p);
	rpath += "._";
	rpath += std::string(p, path.end());
	return rpath;
}