summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/formats/fs_prodos.cpp
blob: c812f7ec87b5a546b0838f678f8966b50ffca1c2 (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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert

// Creation of Apple ProDOS floppy images

#include "fs_prodos.h"
#include "ap_dsk35.h"

#include "strformat.h"

#include <stdexcept>

using namespace fs;

namespace fs { const prodos_image PRODOS; }

namespace {
class impl : public filesystem_t {
public:
	impl(fsblk_t &blockdev);
	virtual ~impl() = default;

	virtual void format(const meta_data &meta) override;

	virtual meta_data metadata() override;
	virtual dir_t root() override;

	void drop_root_ref();

	static util::arbitrary_datetime prodos_to_dt(u32 date);
	std::vector<dir_entry> contents(u16 block);

private:
	static const u8 boot[512];

	dir_t m_root;

	class root_dir : public idir_t {
	public:
		root_dir(impl &fs, u16 base_block) : m_fs(fs), m_base_block(base_block) { }
		virtual ~root_dir() = default;

		virtual void drop_weak_references() override;

		virtual meta_data metadata() override;
		virtual std::vector<dir_entry> contents() override;
		virtual file_t file_get(u64 key) override;
		virtual dir_t dir_get(u64 key) override;

	protected:
		impl &m_fs;
		u16 m_base_block;

		std::pair<fsblk_t::block_t, const u8 *> get_entry_ro(u64 key);
		std::pair<fsblk_t::block_t, u8 *> get_entry(u64 key);
	};

	class dir : public root_dir {
	public:
		dir(impl &fs, const u8 *entry, u16 base_block, u16 key, root_dir *parent_dir);
		virtual ~dir() = default;

		virtual meta_data metadata() override;

	protected:
		root_dir *m_parent_dir;
		u16 m_key;
		u8 m_entry[39];
	};

	class file : public ifile_t {
	public:
		file(impl &fs, const u8 *entry, u16 key, root_dir *parent_dir);
		virtual ~file() = default;

		virtual void drop_weak_references() override;

		virtual meta_data metadata() override;
		virtual std::vector<u8> read_all() override;
		virtual std::vector<u8> rsrc_read_all() override;

	private:
		impl &m_fs;
		root_dir *m_parent_dir;
		u16 m_key;
		u8 m_entry[39];

		std::vector<u8> any_read_all(u8 type, u16 block, u32 length);
	};
};
}


const char *prodos_image::name() const
{
	return "prodos";
}

const char *prodos_image::description() const
{
	return "Apple ProDOS";
}

const u8 impl::boot[512] = {
	0x01, 0x38, 0xb0, 0x03, 0x4c, 0x1c, 0x09, 0x78, 0x86, 0x43, 0xc9, 0x03, 0x08, 0x8a, 0x29, 0x70,
	0x4a, 0x4a, 0x4a, 0x4a, 0x09, 0xc0, 0x85, 0x49, 0xa0, 0xff, 0x84, 0x48, 0x28, 0xc8, 0xb1, 0x48,
	0xd0, 0x3a, 0xb0, 0x0e, 0xa9, 0x03, 0x8d, 0x00, 0x08, 0xe6, 0x3d, 0xa5, 0x49, 0x48, 0xa9, 0x5b,
	0x48, 0x60, 0x85, 0x40, 0x85, 0x48, 0xa0, 0x5e, 0xb1, 0x48, 0x99, 0x94, 0x09, 0xc8, 0xc0, 0xeb,
	0xd0, 0xf6, 0xa2, 0x06, 0xbc, 0x32, 0x09, 0xbd, 0x39, 0x09, 0x99, 0xf2, 0x09, 0xbd, 0x40, 0x09,
	0x9d, 0x7f, 0x0a, 0xca, 0x10, 0xee, 0xa9, 0x09, 0x85, 0x49, 0xa9, 0x86, 0xa0, 0x00, 0xc9, 0xf9,
	0xb0, 0x2f, 0x85, 0x48, 0x84, 0x60, 0x84, 0x4a, 0x84, 0x4c, 0x84, 0x4e, 0x84, 0x47, 0xc8, 0x84,
	0x42, 0xc8, 0x84, 0x46, 0xa9, 0x0c, 0x85, 0x61, 0x85, 0x4b, 0x20, 0x27, 0x09, 0xb0, 0x66, 0xe6,
	0x61, 0xe6, 0x61, 0xe6, 0x46, 0xa5, 0x46, 0xc9, 0x06, 0x90, 0xef, 0xad, 0x00, 0x0c, 0x0d, 0x01,
	0x0c, 0xd0, 0x52, 0xa9, 0x04, 0xd0, 0x02, 0xa5, 0x4a, 0x18, 0x6d, 0x23, 0x0c, 0xa8, 0x90, 0x0d,
	0xe6, 0x4b, 0xa5, 0x4b, 0x4a, 0xb0, 0x06, 0xc9, 0x0a, 0xf0, 0x71, 0xa0, 0x04, 0x84, 0x4a, 0xad,
	0x20, 0x09, 0x29, 0x0f, 0xa8, 0xb1, 0x4a, 0xd9, 0x20, 0x09, 0xd0, 0xdb, 0x88, 0x10, 0xf6, 0xa0,
	0x16, 0xb1, 0x4a, 0x4a, 0x6d, 0x1f, 0x09, 0x8d, 0x1f, 0x09, 0xa0, 0x11, 0xb1, 0x4a, 0x85, 0x46,
	0xc8, 0xb1, 0x4a, 0x85, 0x47, 0xa9, 0x00, 0x85, 0x4a, 0xa0, 0x1e, 0x84, 0x4b, 0x84, 0x61, 0xc8,
	0x84, 0x4d, 0x20, 0x27, 0x09, 0xb0, 0x35, 0xe6, 0x61, 0xe6, 0x61, 0xa4, 0x4e, 0xe6, 0x4e, 0xb1,
	0x4a, 0x85, 0x46, 0xb1, 0x4c, 0x85, 0x47, 0x11, 0x4a, 0xd0, 0x18, 0xa2, 0x01, 0xa9, 0x00, 0xa8,
	0x91, 0x60, 0xc8, 0xd0, 0xfb, 0xe6, 0x61, 0xea, 0xea, 0xca, 0x10, 0xf4, 0xce, 0x1f, 0x09, 0xf0,
	0x07, 0xd0, 0xd8, 0xce, 0x1f, 0x09, 0xd0, 0xca, 0x58, 0x4c, 0x00, 0x20, 0x4c, 0x47, 0x09, 0x02,
	0x26, 0x50, 0x52, 0x4f, 0x44, 0x4f, 0x53, 0xa5, 0x60, 0x85, 0x44, 0xa5, 0x61, 0x85, 0x45, 0x6c,
	0x48, 0x00, 0x08, 0x1e, 0x24, 0x3f, 0x45, 0x47, 0x76, 0xf4, 0xd7, 0xd1, 0xb6, 0x4b, 0xb4, 0xac,
	0xa6, 0x2b, 0x18, 0x60, 0x4c, 0xbc, 0x09, 0x20, 0x58, 0xfc, 0xa0, 0x14, 0xb9, 0x58, 0x09, 0x99,
	0xb1, 0x05, 0x88, 0x10, 0xf7, 0x4c, 0x55, 0x09, 0xd5, 0xce, 0xc1, 0xc2, 0xcc, 0xc5, 0xa0, 0xd4,
	0xcf, 0xa0, 0xcc, 0xcf, 0xc1, 0xc4, 0xa0, 0xd0, 0xd2, 0xcf, 0xc4, 0xcf, 0xd3, 0xa5, 0x53, 0x29,
	0x03, 0x2a, 0x05, 0x2b, 0xaa, 0xbd, 0x80, 0xc0, 0xa9, 0x2c, 0xa2, 0x11, 0xca, 0xd0, 0xfd, 0xe9,
	0x01, 0xd0, 0xf7, 0xa6, 0x2b, 0x60, 0xa5, 0x46, 0x29, 0x07, 0xc9, 0x04, 0x29, 0x03, 0x08, 0x0a,
	0x28, 0x2a, 0x85, 0x3d, 0xa5, 0x47, 0x4a, 0xa5, 0x46, 0x6a, 0x4a, 0x4a, 0x85, 0x41, 0x0a, 0x85,
	0x51, 0xa5, 0x45, 0x85, 0x27, 0xa6, 0x2b, 0xbd, 0x89, 0xc0, 0x20, 0xbc, 0x09, 0xe6, 0x27, 0xe6,
	0x3d, 0xe6, 0x3d, 0xb0, 0x03, 0x20, 0xbc, 0x09, 0xbc, 0x88, 0xc0, 0x60, 0xa5, 0x40, 0x0a, 0x85,
	0x53, 0xa9, 0x00, 0x85, 0x54, 0xa5, 0x53, 0x85, 0x50, 0x38, 0xe5, 0x51, 0xf0, 0x14, 0xb0, 0x04,
	0xe6, 0x53, 0x90, 0x02, 0xc6, 0x53, 0x38, 0x20, 0x6d, 0x09, 0xa5, 0x50, 0x18, 0x20, 0x6f, 0x09,
	0xd0, 0xe3, 0xa0, 0x7f, 0x84, 0x52, 0x08, 0x28, 0x38, 0xc6, 0x52, 0xf0, 0xce, 0x18, 0x08, 0x88,
	0xf0, 0xf5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

void prodos_image::enumerate_f(floppy_enumerator &fe, u32 form_factor, const std::vector<u32> &variants) const
{
	if(has(form_factor, variants, floppy_image::FF_35, floppy_image::DSDD))
		fe.add(FLOPPY_APPLE_GCR_FORMAT, 819200, "prodos_800k", "Apple ProDOS 800K");
	if(has(form_factor, variants, floppy_image::FF_35, floppy_image::SSDD))
		fe.add(FLOPPY_APPLE_GCR_FORMAT, 409600, "prodos_400k", "Apple ProDOS 400K");
}

std::unique_ptr<filesystem_t> prodos_image::mount(fsblk_t &blockdev) const
{
	return std::make_unique<impl>(blockdev);
}

bool prodos_image::can_format() const
{
	return true;
}

bool prodos_image::can_read() const
{
	return true;
}

bool prodos_image::can_write() const
{
	return false;
}

bool prodos_image::has_rsrc() const
{
	return true;
}

char prodos_image::directory_separator() const
{
	return '/';
}

std::vector<meta_description> prodos_image::volume_meta_description() const
{
	std::vector<meta_description> res;
	res.emplace_back(meta_description(meta_name::name, meta_type::string, "UNTITLED", false, [](const meta_value &m) { return m.as_string().size() <= 15; }, "Volume name, up to 15 characters"));
	res.emplace_back(meta_description(meta_name::os_version, meta_type::number, 5, false, [](const meta_value &m) { return m.as_number() <= 255; }, "Creator OS version"));
	res.emplace_back(meta_description(meta_name::os_minimum_version, meta_type::number, 5, false, [](const meta_value &m) { return m.as_number() <= 255; }, "Minimum OS version"));

	auto now = util::arbitrary_datetime::now();
	res.emplace_back(meta_description(meta_name::creation_date, meta_type::date, now, false, nullptr, "Creation time"));
	res.emplace_back(meta_description(meta_name::modification_date, meta_type::date, now, false, nullptr, "Modification time"));
	return res;
}

std::vector<meta_description> prodos_image::file_meta_description() const
{
	std::vector<meta_description> res;
	res.emplace_back(meta_description(meta_name::name, meta_type::string, "Empty file", false, [](const meta_value &m) { return m.as_string().size() <= 15; }, "File name, up to 15 characters"));
	res.emplace_back(meta_description(meta_name::length, meta_type::number, 0, true, nullptr, "Size of the file in bytes"));
	res.emplace_back(meta_description(meta_name::rsrc_length, meta_type::number, 0, true, nullptr, "Size of the resource fork in bytes"));
	res.emplace_back(meta_description(meta_name::os_version, meta_type::number, 5, false, [](const meta_value &m) { return m.as_number() <= 255; }, "Creator OS version"));
	res.emplace_back(meta_description(meta_name::os_minimum_version, meta_type::number, 5, false, [](const meta_value &m) { return m.as_number() <= 255; }, "Minimum OS version"));

	auto now = util::arbitrary_datetime::now();
	res.emplace_back(meta_description(meta_name::creation_date, meta_type::date, now, false, nullptr, "Creation time"));
	res.emplace_back(meta_description(meta_name::modification_date, meta_type::date, now, false, nullptr, "Modification time"));
	return res;
}

std::vector<meta_description> prodos_image::directory_meta_description() const
{
	std::vector<meta_description> res;
	res.emplace_back(meta_description(meta_name::name, meta_type::string, "Empty directory", false, [](const meta_value &m) { return m.as_string().size() <= 15; }, "Directory name, up to 15 characters"));
	res.emplace_back(meta_description(meta_name::os_version, meta_type::number, 5, false, [](const meta_value &m) { return m.as_number() <= 255; }, "Creator OS version"));
	res.emplace_back(meta_description(meta_name::os_minimum_version, meta_type::number, 5, false, [](const meta_value &m) { return m.as_number() <= 255; }, "Minimum OS version"));

	auto now = util::arbitrary_datetime::now();
	res.emplace_back(meta_description(meta_name::creation_date, meta_type::date, now, false, nullptr, "Creation time"));
	res.emplace_back(meta_description(meta_name::modification_date, meta_type::date, now, false, nullptr, "Modification time"));
	return res;
}

void impl::format(const meta_data &meta)
{
	std::string volume_name = meta.get_string(meta_name::name, "UNTITLED");
	u32 blocks = m_blockdev.block_count();

	// Maximum usable partition size = 32M - 512 bytes (65535 blocks)
	if(blocks >= 0x10000)
		blocks = 0xffff;

	m_blockdev.get(0).copy(0x000, boot, 0x200);               // Standard ProDOS boot sector as written by a 2gs
	m_blockdev.get(1).fill(0x00);                             // No SOS boot sector

	auto kblk1 = m_blockdev.get(2);                           // key block first block
	auto kblk2 = m_blockdev.get(3);                           // key block second block
	auto kblk3 = m_blockdev.get(4);                           // key block third block
	auto kblk4 = m_blockdev.get(5);                           // key block fourth block

	kblk1.w16l(0x00, 0x0000);                                 // Backwards key block pointer (null)
	kblk1.w16l(0x02, 0x0003);                                 // Forwards key block pointer
	kblk1.w8  (0x04, 0xf0 | volume_name.size());              // Block type (f, key block) and name size
	kblk1.wstr(0x05, volume_name);                            // Volume name, up to 15 characters
	kblk1.w32b(0x16, 0x642a250d);                             // ??? date & time
	kblk1.w16b(0x1a, 0x80ff);                                 // ???
	kblk1.w32b(0x1c, 0x642a250d);                             // Creation date & time
	kblk1.w8  (0x20, 0x05);                                   // ProDOS version (2gs)
	kblk1.w8  (0x21, 0x00);                                   // ProDOS minimum version
	kblk1.w8  (0x22, 0xc3);                                   // Allowed access (destroy, rename, !backup, 3x0, write read)
	kblk1.w8  (0x23, 0x27);                                   // Directory entry length (fixed)
	kblk1.w8  (0x24, 0x0d);                                   // Entries per block (fixed)
	kblk1.w16l(0x25, 0x0000);                                 // Number of file entries in the directory
	kblk1.w16l(0x27, 0x0006);                                 // Bitmap block pointer
	kblk1.w16l(0x29, blocks);                                 // Number of blocks

	kblk2.w16l(0x00, 0x0002);                                 // Backwards block pointer of the second volume block
	kblk2.w16l(0x02, 0x0004);                                 // Forwards block pointer of the second volume block
	kblk3.w16l(0x00, 0x0003);                                 // Backwards block pointer of the third volume block
	kblk3.w16l(0x02, 0x0005);                                 // Forwards block pointer of the third volume block
	kblk4.w16l(0x00, 0x0004);                                 // Backwards block pointer of the fourth volume block
	kblk4.w16l(0x02, 0x0000);                                 // Forwards block pointer of the fourth volume block (null)

	u32 fmap_block_count = (blocks + 4095) / 4096;
	u32 first_free_block = 6 + fmap_block_count;

	// Mark blocks from first_free_block to blocks-1 (the last one) as free
	for(u32 i = 0; i != fmap_block_count; i++) {
		auto fmap = m_blockdev.get(6 + i);
		u8 *fdata = fmap.data();
		u32 start = i ? 0 : first_free_block;
		u32 end = i != fmap_block_count - 1 ? 4095 : (blocks - 1) & 4095;
		end += 1;
		u32 sb = start >> 3;
		u32 si = start & 7;
		u32 eb = end >> 3;
		u32 ei = end & 7;
		if(sb == eb)
			fdata[sb] = (0xff >> si) & ~(0xff >> ei);
		else {
			fdata[sb] = 0xff >> si;
			if(eb != 512)
				fdata[eb] = ~(0xff >> ei);
			if(eb - sb > 1)
				memset(fdata+sb, 0xff, eb-sb-1);
		}
	}
}

impl::impl(fsblk_t &blockdev) : filesystem_t(blockdev, 512), m_root(true)
{
}

util::arbitrary_datetime impl::prodos_to_dt(u32 date)
{
	util::arbitrary_datetime dt;
	dt.second       = 0;
	dt.minute       = ((date >> 16) & 0x3f);
	dt.hour         = ((date >> 24) & 0x1f);
	dt.day_of_month = ((date >> 0) & 0x1f);
	dt.month        = ((date >> 5) & 0x0f) + 1;
	dt.year         = ((date >> 9) & 0x7f) + 1900;
	if (dt.year <= 1949)
		dt.year += 100;

	return dt;
}

meta_data impl::metadata()
{
	meta_data res;
	auto bdir = m_blockdev.get(2);
	int len = bdir.r8(0x04) & 0xf;
	res.set(meta_name::name, bdir.rstr(0x05, len));
	res.set(meta_name::os_version, bdir.r8(0x20));
	res.set(meta_name::os_minimum_version, bdir.r8(0x21));
	res.set(meta_name::creation_date, prodos_to_dt(bdir.r32l(0x1c)));
	res.set(meta_name::modification_date, prodos_to_dt(bdir.r32l(0x16)));
	return res;
}

filesystem_t::dir_t impl::root()
{
	if(!m_root)
		m_root = new root_dir(*this, 2);
	return m_root.strong();
}

void impl::drop_root_ref()
{
	m_root = nullptr;
}


void impl::root_dir::drop_weak_references()
{
	if(m_base_block == 2)
		m_fs.drop_root_ref();
}

meta_data impl::root_dir::metadata()
{
	return meta_data();
}

std::vector<dir_entry> impl::root_dir::contents()
{
	std::vector<dir_entry> res;

	u16 block = m_base_block;
	u32 off = 39 + 4;
	u32 id = 1;
	do {
		auto blk = m_fs.m_blockdev.get(block);
		while(off < 511) {
			u8 type = blk.r8(off);
			auto name = blk.rstr(off+1, type & 0xf);
			type >>= 4;
			if(type == 0xd)
				res.emplace_back(dir_entry(name, dir_entry_type::dir, id));
			else if(type != 0)
				res.emplace_back(dir_entry(name, dir_entry_type::file, id));
			off += 39;
			id ++;
		}
		block = blk.r16l(2);
		if(block >= m_fs.m_blockdev.block_count())
			break;
		off = 4;
	} while(block);
	return res;
}


std::pair<fsblk_t::block_t, const u8 *> impl::root_dir::get_entry_ro(u64 key)
{
	std::pair<fsblk_t::block_t, const u8 *> res;
	res.first = m_fs.m_blockdev.get(m_base_block);
	while(key >= 13) {
		key -= 13;
		u16 block = res.first.r16l(2);
		if(!block || block >= m_fs.m_blockdev.block_count()) {
			res.first = nullptr;
			res.second = nullptr;
			return res;
		}
		res.first = m_fs.m_blockdev.get(block);
	}
	res.second = res.first.rodata() + 4 + 39 * key;
	return res;
}

std::pair<fsblk_t::block_t, u8 *> impl::root_dir::get_entry(u64 key)
{
	std::pair<fsblk_t::block_t, u8 *> res;
	res.first = m_fs.m_blockdev.get(m_base_block);
	while(key > 13) {
		key -= 13;
		u16 block = res.first.r16l(2);
		if(!block || block >= m_fs.m_blockdev.block_count()) {
			res.first = nullptr;
			res.second = nullptr;
			return res;
		}
		res.first = m_fs.m_blockdev.get(block);
	}
	res.second = res.first.data() + 4 + 39 * key;
	return res;
}

filesystem_t::file_t impl::root_dir::file_get(u64 key)
{
	auto [blk, entry] = get_entry_ro(key);
	if(!blk)
		throw std::out_of_range("Out-of-range key on file_get");
	u8 type = entry[0] >> 4;
	if(type == 0 || type == 4 || type > 5)
		throw std::runtime_error(util::string_format("Unhandled file type %x", type));
	return new file(m_fs, entry, key, this);
}

filesystem_t::dir_t impl::root_dir::dir_get(u64 key)
{
	auto [blk, entry] = get_entry_ro(key);
	if(!blk)
		throw std::out_of_range("Out-of-range key on dir_get");
	u8 type = entry[0] >> 4;
	if(type != 0xd)
		throw std::runtime_error(util::string_format("Unhandled directory type %x", type));

	return new dir(m_fs, entry, r16l(entry+0x11), key, this);
}

impl::dir::dir(impl &fs, const u8 *entry, u16 base_block, u16 key, root_dir *parent_dir) : root_dir(fs, base_block), m_parent_dir(parent_dir), m_key(key)
{
	memcpy(m_entry, entry, 39);
	(void)m_key;
	(void)m_parent_dir;
}

impl::file::file(impl &fs, const u8 *entry, u16 key, root_dir *parent_dir) : m_fs(fs), m_parent_dir(parent_dir), m_key(key)
{
	memcpy(m_entry, entry, 39);
	(void)m_key;
	(void)m_parent_dir;
}

void impl::file::drop_weak_references()
{
}

meta_data impl::file::metadata()
{
	meta_data res;
	u8 type = r8(m_entry);
	std::string name = rstr(m_entry+1, type & 0xf);
	type >>= 4;
	res.set(meta_name::name, name);
	if(type == 5) {
		auto rootblk = m_fs.m_blockdev.get(r16l(m_entry+0x11));
		res.set(meta_name::length, rootblk.r24l(0x005));
		res.set(meta_name::rsrc_length, rootblk.r24l(0x105));

	} else if(type >= 1 && type <= 3)
		res.set(meta_name::length, r24l(m_entry + 0x15));

	else
		throw std::runtime_error(util::string_format("impl::file::metadata: Unhandled file type %d", type));

	return res;
}

meta_data impl::dir::metadata()
{
	meta_data res;
	u8 type = r8(m_entry);
	std::string name = rstr(m_entry+1, type & 0xf);
	res.set(meta_name::name, name);

	return res;
}

std::vector<u8> impl::file::any_read_all(u8 type, u16 block, u32 length)
{
	std::vector<u8> data((length + 511) & ~511);
	u32 nb = data.size()/512;
	if(!nb)
		return data;

	u8 *dst = data.data();
	u8 *end = dst + data.size();
	switch(type) {
	case 1:
		memcpy(dst, m_fs.m_blockdev.get(block).rodata(), 512);
		dst += 512;
		break;

	case 2: {
		auto iblk = m_fs.m_blockdev.get(block);
		for(u32 i=0; i != 256 && dst != end; i++) {
			u16 blk = iblk.r8(i) | (iblk.r8(i | 0x100) << 8);
			memcpy(dst, m_fs.m_blockdev.get(blk).rodata(), 512);
			dst += 512;
		}
		break;
	}

	case 3: {
		auto mblk = m_fs.m_blockdev.get(block);
		for(u32 j=0; dst != end; j += 256) {
			u32 idx = j/256;
			auto iblk = m_fs.m_blockdev.get(mblk.r8(idx) | (mblk.r8(idx | 0x100) << 8));
			for(u32 i=0; i != 256 && dst != end; i++) {
				u16 blk = iblk.r8(i) | (iblk.r8(i | 0x100) << 8);
				memcpy(dst, m_fs.m_blockdev.get(blk).rodata(), 512);
				dst += 512;
			}
		}
		break;
	}

	default:
		throw std::runtime_error(util::string_format("impl::file::get_file_blocks: unknown file type %d", type));
	}

	data.resize(length);
	return data;
}

std::vector<u8> impl::file::read_all()
{
	u8 type = r8(m_entry) >> 4;
	if(type >= 1 && type <= 3)
		return any_read_all(type, r16l(m_entry+0x11), r24l(m_entry + 0x15));

	else if(type == 5) {
		auto kblk = m_fs.m_blockdev.get(r16l(m_entry+0x11));
		return any_read_all(kblk.r8(0x000), kblk.r16l(0x001), kblk.r24l(0x005));

	} else
		throw std::runtime_error(util::string_format("impl::file::read_all: Unhandled file type %d", type));
}

std::vector<u8> impl::file::rsrc_read_all()
{
	u8 type = r8(m_entry) >> 4;

	if(type == 5) {
		auto kblk = m_fs.m_blockdev.get(r16l(m_entry+0x11));
		return any_read_all(kblk.r8(0x100), kblk.r16l(0x101), kblk.r24l(0x105));

	} else
		throw std::runtime_error(util::string_format("impl::file::rsrc_blocks: Unhandled file type %d", type));
}