2021-03-08 23:59:02 +00:00
|
|
|
//
|
|
|
|
// Media.m
|
|
|
|
// Ample
|
|
|
|
//
|
|
|
|
// Created by Kelvin Sherlock on 3/7/2021.
|
|
|
|
// Copyright © 2021 Kelvin Sherlock. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import "Media.h"
|
|
|
|
|
|
|
|
const Media EmptyMedia = { 0 };
|
|
|
|
|
|
|
|
struct Media MediaFromDictionary(NSDictionary *dict) {
|
|
|
|
|
|
|
|
Media m = { 0 };
|
|
|
|
|
|
|
|
#define _(name) m.name = [[dict objectForKey: @ # name] unsignedIntValue]
|
|
|
|
_(cass);
|
|
|
|
_(cdrom);
|
|
|
|
_(hard);
|
|
|
|
_(floppy_3_5);
|
|
|
|
_(floppy_5_25);
|
|
|
|
_(pseudo_disk);
|
2021-06-13 16:29:23 +00:00
|
|
|
_(bitbanger);
|
2021-07-11 03:33:20 +00:00
|
|
|
_(midiin);
|
|
|
|
_(midiout);
|
|
|
|
_(picture);
|
2021-03-08 23:59:02 +00:00
|
|
|
|
|
|
|
return m;
|
|
|
|
#undef _
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaAdd(Media *dest, const Media *src) {
|
|
|
|
|
|
|
|
if (!src || !dest) return;
|
|
|
|
|
2022-02-24 01:56:21 +00:00
|
|
|
// could merge from src media but not currently set there.
|
|
|
|
|
|
|
|
unsigned count;
|
|
|
|
unsigned flops = dest->floppy_5_25 + dest->floppy_3_5;
|
|
|
|
|
|
|
|
|
2021-03-08 23:59:02 +00:00
|
|
|
#define _(name) dest->name += src->name;
|
|
|
|
_(cass);
|
|
|
|
_(cdrom);
|
|
|
|
_(hard);
|
|
|
|
_(floppy_3_5);
|
|
|
|
_(floppy_5_25);
|
|
|
|
_(pseudo_disk);
|
2021-06-13 16:29:23 +00:00
|
|
|
_(bitbanger);
|
2021-07-11 03:33:20 +00:00
|
|
|
_(midiin);
|
|
|
|
_(midiout);
|
|
|
|
_(picture);
|
2021-03-08 23:59:02 +00:00
|
|
|
#undef _
|
2022-02-24 01:56:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
if ((count = src->floppy_5_25)) {
|
|
|
|
uint64_t bits = (1 << count) - 1;
|
|
|
|
//dest->floppy_mask <<= count;
|
|
|
|
bits <<= flops;
|
|
|
|
dest->floppy_mask |= bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-08 23:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL MediaEqual(const Media *lhs, const Media *rhs) {
|
|
|
|
if (lhs == rhs) return YES;
|
|
|
|
if (!lhs || !rhs) return NO;
|
|
|
|
|
|
|
|
|
|
|
|
#define _(name) if (lhs->name != rhs->name) return NO;
|
|
|
|
_(cass);
|
|
|
|
_(cdrom);
|
|
|
|
_(hard);
|
|
|
|
_(floppy_3_5);
|
|
|
|
_(floppy_5_25);
|
|
|
|
_(pseudo_disk);
|
2021-06-13 16:29:23 +00:00
|
|
|
_(bitbanger);
|
2021-07-11 03:33:20 +00:00
|
|
|
_(midiin);
|
|
|
|
_(midiout);
|
|
|
|
_(picture);
|
|
|
|
|
2022-02-24 01:56:21 +00:00
|
|
|
_(floppy_mask);
|
|
|
|
|
2021-03-08 23:59:02 +00:00
|
|
|
return YES;
|
|
|
|
#undef _
|
|
|
|
}
|
2021-06-18 03:43:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2021-07-09 02:09:33 +00:00
|
|
|
|
2021-06-18 03:43:31 +00:00
|
|
|
|
|
|
|
#define _x2(a,b) (a | (b << 8))
|
|
|
|
#define _x3(a,b,c) (a | (b << 8) | (c << 16))
|
|
|
|
#define _x4(a,b,c, d) (a | (b << 8) | (c << 16) | (d << 24))
|
|
|
|
static unsigned hash(const char *cp) {
|
|
|
|
unsigned rv = 0;
|
|
|
|
int i, shift;
|
|
|
|
if (!cp) return 0;
|
|
|
|
for (i = 0, shift = 0; i < 4; ++i, shift += 8) {
|
2021-07-10 05:15:34 +00:00
|
|
|
unsigned c = cp[i];
|
2021-06-18 03:43:31 +00:00
|
|
|
if (!c) break;
|
|
|
|
c = tolower(c);
|
|
|
|
rv |= (c << shift);
|
|
|
|
}
|
2021-07-10 05:15:34 +00:00
|
|
|
//if (i > 4) return 0; // .image is 5....
|
2021-06-18 03:43:31 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *extname(const char *path) {
|
|
|
|
|
|
|
|
const char *rv = NULL;
|
|
|
|
if (!path) return path;
|
|
|
|
for(unsigned i = 0; ; ++i) {
|
|
|
|
unsigned c = path[i];
|
|
|
|
if (c == 0) break;
|
|
|
|
if (c == '/') rv = NULL;
|
|
|
|
if (c == '.') rv = path + i + 1;
|
|
|
|
}
|
|
|
|
if (rv && !*rv) rv = NULL;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2021-07-09 02:09:33 +00:00
|
|
|
/*
|
|
|
|
* MAME cares deeply about file extensions. LIST NOT EXHAUSTIVE
|
|
|
|
*
|
|
|
|
* Hard Disk: .chd .hd .hdv .2mg .hdi
|
|
|
|
* CD-ROM: .chd .cue .toc .nrg .gdi .iso .cdr
|
|
|
|
* 5.25: .dsk .do .po .edd .woz .nib .mfi .dfi .rti
|
|
|
|
* 3.5: .dc42 .woz .2mg .mfi .dfi .hfe .mfm .td0 .imd
|
|
|
|
.d77 .d88 .1dd .cqm .cqi .dsk .ima .img .ufi
|
|
|
|
.360 .ipf [also .po and .hdv]
|
|
|
|
* Midi in: .mid
|
|
|
|
* Midi out: .mid
|
|
|
|
* Picture: .png
|
|
|
|
* Cassette: .wav
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool is_raw_525(size_t disk_size) {
|
|
|
|
if (disk_size & 255) return NO;
|
|
|
|
size_t blocks = disk_size >> 8;
|
2021-06-18 03:43:31 +00:00
|
|
|
|
2021-07-09 02:09:33 +00:00
|
|
|
return blocks == 560 || blocks == 640;
|
|
|
|
}
|
2021-06-18 03:43:31 +00:00
|
|
|
|
2021-07-09 02:09:33 +00:00
|
|
|
static BOOL is_raw_35(size_t disk_size) {
|
|
|
|
if (disk_size & 511) return NO;
|
|
|
|
size_t blocks = disk_size >> 9;
|
2021-06-18 03:43:31 +00:00
|
|
|
|
2021-07-09 02:09:33 +00:00
|
|
|
return blocks == 800 || blocks == 1600 || blocks == 1440 || blocks == 2880;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MediaType is_woz(const uint8_t *buffer, size_t file_size) {
|
2021-06-18 03:43:31 +00:00
|
|
|
|
|
|
|
/* woz 1/2 ? */
|
|
|
|
if (!memcmp(buffer, "WOZ1\xff\x0a\x0d\x0a", 8) || !memcmp(buffer, "WOZ2\xff\x0a\x0d\x0a", 8)) {
|
|
|
|
|
|
|
|
if (!memcmp(buffer + 12, "INFO", 4)) {
|
|
|
|
unsigned type = buffer[21]; // 1 = 5.25, 2 = 3.5
|
|
|
|
if (type == 1) return MediaType_5_25;
|
|
|
|
if (type == 2) return MediaType_3_5;
|
|
|
|
}
|
2021-07-09 02:09:33 +00:00
|
|
|
return MediaTypeUnknown;
|
2021-06-18 03:43:31 +00:00
|
|
|
}
|
2021-07-10 05:15:34 +00:00
|
|
|
return MediaTypeError;
|
2021-07-09 02:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static MediaType is_dc42(const uint8_t *buffer, size_t file_size) {
|
|
|
|
|
|
|
|
if (buffer[0x52] == 0x01 && buffer[0x53] == 0x00 && buffer[0] >= 1 && buffer[0] <= 0x3f) {
|
|
|
|
int dsize = OSReadBigInt32(buffer, 0x40);
|
|
|
|
int tsize = OSReadBigInt32(buffer, 0x44);
|
|
|
|
if (dsize + tsize + 0x54 == file_size) {
|
|
|
|
if (is_raw_35(dsize)) return MediaType_3_5;
|
|
|
|
return MediaType_HardDisk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-10 05:15:34 +00:00
|
|
|
return MediaTypeError;
|
2021-07-09 02:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static MediaType is_2img(const uint8_t *buffer, size_t file_size) {
|
|
|
|
|
2021-06-18 03:43:31 +00:00
|
|
|
if (!memcmp(buffer, "2IMG", 4)) {
|
|
|
|
int format = OSReadLittleInt32(buffer, 0x0c); // 0 - dos order, 1 = prodos order, 2 = nib data
|
|
|
|
int blocks = OSReadLittleInt32(buffer, 0x14); // prodos only.
|
|
|
|
//int bytes = OSReadLittleInt32(buffer, 0x1c);
|
|
|
|
|
|
|
|
if (format == 2 || format == 0) return MediaType_5_25; // nib and dos order
|
2021-07-09 02:09:33 +00:00
|
|
|
if (is_raw_525(blocks * 512)) return MediaType_5_25;
|
|
|
|
if (is_raw_35(blocks * 512)) return MediaType_3_5;
|
|
|
|
return MediaType_HardDisk; //
|
|
|
|
//return MediaTypeUnknown;
|
2021-06-18 03:43:31 +00:00
|
|
|
}
|
2021-07-09 02:09:33 +00:00
|
|
|
|
2021-07-10 05:15:34 +00:00
|
|
|
return MediaTypeError;
|
2021-07-09 02:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static MediaType is_chd(const uint8_t *buffer, size_t file_size) {
|
2021-06-18 03:43:31 +00:00
|
|
|
if (!memcmp(buffer, "MComprHD", 8)) {
|
|
|
|
static int offsets[] = { 0, 0, 0, 28, 28, 32}; // offset for logival bytes.
|
|
|
|
int version = OSReadBigInt32(buffer, 12);
|
|
|
|
|
|
|
|
if (version >= 3 && version <= 5) {
|
|
|
|
long bytes = OSReadBigInt64(buffer, offsets[version]);
|
2021-07-09 02:09:33 +00:00
|
|
|
if (is_raw_525(bytes)) return MediaType_5_25;
|
|
|
|
if (is_raw_35(bytes)) return MediaType_3_5;
|
|
|
|
return MediaType_HardDisk; // iso?
|
2021-06-18 03:43:31 +00:00
|
|
|
}
|
2021-07-09 02:09:33 +00:00
|
|
|
return MediaTypeUnknown;
|
2021-06-18 03:43:31 +00:00
|
|
|
}
|
2021-07-10 05:15:34 +00:00
|
|
|
return MediaTypeError;
|
2021-07-09 02:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-07-10 05:15:34 +00:00
|
|
|
MediaType ClassifyMediaFile(id file) {
|
2021-06-18 03:43:31 +00:00
|
|
|
|
2021-07-09 02:09:33 +00:00
|
|
|
struct stat st;
|
|
|
|
ssize_t size;
|
|
|
|
unsigned char buffer[128];
|
|
|
|
int fd;
|
2021-07-10 05:15:34 +00:00
|
|
|
const char *path = [file fileSystemRepresentation]; // or URL
|
2021-07-09 02:09:33 +00:00
|
|
|
const char *ext = extname(path);
|
|
|
|
unsigned ext_hash = hash(ext);
|
|
|
|
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
2021-07-10 05:15:34 +00:00
|
|
|
if (fd < 0) return MediaTypeError;
|
2021-07-09 02:09:33 +00:00
|
|
|
fstat(fd, &st);
|
2021-06-18 03:43:31 +00:00
|
|
|
|
2021-07-09 02:09:33 +00:00
|
|
|
size = read(fd, buffer, sizeof(buffer));
|
|
|
|
close(fd);
|
2021-07-10 05:15:34 +00:00
|
|
|
if (size <= 0) return MediaTypeError;
|
2021-06-18 03:43:31 +00:00
|
|
|
|
2021-07-09 02:09:33 +00:00
|
|
|
// 13 sector support ? not on an event 512 block boundary.
|
|
|
|
// = 116480 bytes.
|
|
|
|
|
2021-06-18 03:43:31 +00:00
|
|
|
switch(ext_hash) {
|
2021-07-09 02:09:33 +00:00
|
|
|
case _x3('w', 'o', 'z'):
|
|
|
|
return is_woz(buffer, st.st_size);
|
|
|
|
|
|
|
|
case _x3('2', 'm', 'g'):
|
|
|
|
case _x4('2', 'i', 'm', 'g'):
|
|
|
|
return is_2img(buffer, st.st_size);
|
|
|
|
|
|
|
|
case _x3('c', 'h', 'd'):
|
|
|
|
return is_chd(buffer, st.st_size);
|
|
|
|
|
|
|
|
// dc42 - dc,dc42,dsk,img,image
|
|
|
|
case _x2('d', 'c'): // n.b. as of .233, allowed for mac, not for apple2
|
|
|
|
case _x4('d', 'c', '4', '2'):
|
|
|
|
return is_dc42(buffer, st.st_size);
|
|
|
|
|
|
|
|
// dsk, image, img may also be raw or disk copy.
|
|
|
|
case _x3('d', 's', 'k'):
|
|
|
|
case _x3('i', 'm', 'g'):
|
|
|
|
case _x4('i', 'm', 'a', 'g'):
|
|
|
|
|
|
|
|
if (is_raw_525(st.st_size)) return MediaType_5_25;
|
|
|
|
if (is_raw_35(st.st_size)) return MediaType_3_5;
|
|
|
|
return is_dc42(buffer, st.st_size);
|
|
|
|
|
|
|
|
case _x2('d', 'o'):
|
|
|
|
case _x2('p', 'o'):
|
|
|
|
if (is_raw_525(st.st_size)) return MediaType_5_25;
|
|
|
|
if (is_raw_35(st.st_size)) return MediaType_3_5;
|
|
|
|
return MediaTypeUnknown;
|
|
|
|
|
|
|
|
// hdv - 3.5 or hard drive.
|
|
|
|
case _x3('h', 'd', 'v'):
|
2021-07-10 05:15:34 +00:00
|
|
|
case _x3('r', 'a', 'w'):
|
2021-07-09 02:09:33 +00:00
|
|
|
if (is_raw_35(st.st_size)) return MediaType_3_5;
|
|
|
|
if ((st.st_size & 511) == 0) return MediaType_HardDisk;
|
|
|
|
return MediaTypeUnknown;
|
|
|
|
|
|
|
|
case _x3('n', 'i', 'b'):
|
|
|
|
return MediaType_5_25;
|
|
|
|
|
|
|
|
case _x3('w', 'a', 'v'):
|
|
|
|
return MediaType_Cassette;
|
|
|
|
|
|
|
|
// cd-rom
|
2021-06-18 03:43:31 +00:00
|
|
|
case _x3('i', 's', 'o'):
|
|
|
|
case _x3('c', 'u', 'e'):
|
|
|
|
case _x3('c', 'd', 'r'):
|
2021-07-10 05:15:34 +00:00
|
|
|
return MediaType_CDROM;
|
2021-06-18 03:43:31 +00:00
|
|
|
|
2021-07-09 02:09:33 +00:00
|
|
|
case _x3('p', 'n', 'g'):
|
|
|
|
return MediaType_Picture;
|
|
|
|
|
|
|
|
case _x3('m', 'i', 'd'):
|
|
|
|
return MediaType_MIDI;
|
|
|
|
|
2021-06-18 03:43:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 02:09:33 +00:00
|
|
|
return MediaTypeUnknown;
|
|
|
|
|
2021-06-18 03:43:31 +00:00
|
|
|
}
|