summaryrefslogtreecommitdiffstatshomepage
path: root/src/tools/image_handler.cpp
blob: abb45de2db3350c19ac9e2dd74ba0a23cbfe28c6 (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// 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"

// Fatalerror implementation

emu_fatalerror::emu_fatalerror(util::format_argument_pack<std::ostream> const &args)
	: emu_fatalerror(0, args)
{
}

emu_fatalerror::emu_fatalerror(int _exitcode, util::format_argument_pack<std::ostream> const &args)
	: m_text(util::string_format(args))
	, m_code(_exitcode)
{
}

// io_generic from vector<u8>

static void ram_closeproc(void *file)
{
	auto f = (iofile_ram *)file;
	delete f;
}

static int ram_seekproc(void *file, int64_t offset, int whence)
{
	auto f = (iofile_ram *)file;
	switch(whence) {
	case SEEK_SET: f->pos = offset; break;
	case SEEK_CUR: f->pos += offset; break;
	case SEEK_END: f->pos = f->data->size() + offset; break;
	}

	if(whence == SEEK_CUR)
		f->pos = std::max<int64_t>(f->pos, 0);
	else
		f->pos = std::clamp<int64_t>(f->pos, 0, f->data->size());
	return 0;
}

static size_t ram_readproc(void *file, void *buffer, size_t length)
{
	auto f = (iofile_ram *)file;
	size_t l = std::min<std::common_type_t<size_t, int64_t> >(length, f->data->size() - f->pos);
	memcpy(buffer, f->data->data() + f->pos, l);
	return l;
}

static size_t ram_writeproc(void *file, const void *buffer, size_t length)
{
	auto f = (iofile_ram *)file;
	size_t l = std::max<std::common_type_t<size_t, int64_t> >(f->pos + length, f->data->size());
	f->data->resize(l);
	memcpy(f->data->data() + f->pos, buffer, length);
	return length;
}

static uint64_t ram_filesizeproc(void *file)
{
	auto f = (iofile_ram *)file;
	return f->data->size();
}


static const io_procs iop_ram = {
	ram_closeproc,
	ram_seekproc,
	ram_readproc,
	ram_writeproc,
	ram_filesizeproc
};

io_generic *ram_open(std::vector<u8> &data)
{
	iofile_ram *f = new iofile_ram;
	f->data = &data;
	f->pos = 0;
	return new io_generic({ &iop_ram, f });
}


// 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(floppy_format_type format) {
			m_table->floppy_format_infos.emplace_back(std::make_unique<floppy_format_info>(format(), m_category));
		}

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

	struct fs_enum : public filesystem_manager_t::floppy_enumerator {
		filesystem_format *m_format;

		fs_enum(filesystem_format *format) : m_format(format) {}

		virtual void add(floppy_format_type 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());
		f->m_manager->enumerate_f(fen, floppy_image::FF_UNKNOWN, variants);
	}

	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)
{
	char msg[4096];
	sprintf(msg, "Error opening %s for reading", path.c_str());
	auto fi = fopen(path.c_str(), "rb");
	if(!fi) {
		perror(msg);
		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(filesystem_t::r32b(head+0x00) == 0x00051607 &&
	   filesystem_t::r32b(head+0x04) == 0x00020000) {
		u16 nent = filesystem_t::r16b(head+0x18);
		for(u16 i=0; i != nent; i++) {
			const u8 *e = head + 12*i;
			if(filesystem_t::r32b(e+0) == 2) {
				u32 start = filesystem_t::r32b(e+4);
				u32 len = filesystem_t::r32b(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)
{
	char msg[4096];
	sprintf(msg, "Error opening %s for writing", path.c_str());
	auto fo = fopen(path.c_str(), "wb");
	if(!fo) {
		perror(msg);
		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];

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

	char msg[4096];
	sprintf(msg, "Error opening %s for writing", path.c_str());
	auto fo = fopen(path.c_str(), "wb");
	if(!fo) {
		perror(msg);
		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;

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

	io_generic io;
	io.file = f;
	io.procs = &stdio_ioprocs_noclose;
	io.filler = 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)
			res.emplace_back(std::make_pair(score, e.second));
	}

	fclose(f);

	return res;
}

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

	io_generic io;
	io.file = f;
	io.procs = &stdio_ioprocs_noclose;
	io.filler = 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)
{
	std::vector<uint32_t> variants;
	std::string msg = util::string_format("Error opening %s for writing", m_on_disk_path);
	FILE *f = fopen(m_on_disk_path.c_str(), "wb");
	if (!f) {
		perror(msg.c_str());
		return true;
	}

	io_generic io;
	io.file = f;
	io.procs = &stdio_ioprocs_noclose;
	io.filler = 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);
		fsblk_vec_t blockdev(img);
		auto fs = format->m_manager->mount(blockdev);
		fs->format(meta);

		auto iog = ram_open(img);
		auto source_format = format->m_type();
		source_format->load(iog, floppy_image::FF_UNKNOWN, variants, &m_floppy_image);
		delete source_format;
		delete iog;

	} else
		fs_unformatted::format(format->m_key, &m_floppy_image);
}

bool image_handler::floppy_mount_fs(const filesystem_format *format)
{
	m_floppy_fs_converter = nullptr;
	for(const auto &ci : format->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();
			auto iog = ram_open(m_sector_image);
			auto load_format = m_floppy_fs_converter();
			load_format->save(iog, variants, &m_floppy_image);
			delete load_format;
			delete iog;
		}

		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 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 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 iog = ram_open(m_sector_image);
	auto format = m_floppy_fs_converter();
	format->load(iog, floppy_image::FF_UNKNOWN, variants, &m_floppy_image);
	delete format;
	delete iog;
}

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;
}