// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! A disk out of whatever file somebody named. //! //! There are four things a file holding an Apple II disk can be, and which one //! it is is not the caller's business. It can be a bare dump of the sectors, in //! which case its name is the only thing saying which way round they are. It //! can be a 2IMG, which says so itself and is believed. It can be a recording //! of the surface — a `.nib` — which is not sectors at all but the nibbles a //! drive's head saw, and which is how a copy-protected disk survives. Or it can //! be a ShrinkIt archive, which is not a disk image either but an archive that //! may have one inside it, and usually does when somebody hands you one. //! //! Every place in either front end that takes a path takes all three, and this //! is the one door they go through. The only thing a caller has to know is //! that a ShrinkIt archive has to be unpacked somewhere, so what comes back //! may own memory, and says whether it does. const std = @import("std"); const disk2 = @import("disk2.zig"); const wozfmt = @import("woz.zig"); const shrinkit = @import("shrinkit"); const twomg = @import("twomg.zig"); pub const Error = twomg.Error || shrinkit.nufx.Error || wozfmt.Error || std.mem.Allocator.Error; /// A disk, and whatever had to be allocated to get at it. pub const Opened = struct { image: twomg.Image, /// The bytes this allocated, which the caller frees when it has finished /// with the disk. Nothing when the file was already a disk image and what /// came back points into the caller's own buffer. owned: ?[]u8 = null, pub fn deinit(self: Opened, gpa: std.mem.Allocator) void { if (self.owned) |bytes| gpa.free(bytes); } }; /// Opens whatever the file turns out to be. /// /// `bytes` is the file, and stays borrowed for as long as what comes back is /// used, unless the file was an archive — in which case the disk is somewhere /// else and `owned` says so. pub fn open(gpa: std.mem.Allocator, bytes: []u8, path: []const u8) Error!Opened { if (wozfmt.looksLike(bytes)) { // Checked here rather than taken on trust: what comes back points into // the caller's bytes, so anything wrong with them is wrong with the // disk, and a WOZ says outright whether it survived the journey. _ = try wozfmt.open(bytes); return .{ .image = .{ .data = bytes, .format = .woz, .write_protected = false, .volume_number = null, .blocks = 0, } }; } if (!shrinkit.nufx.looksLike(bytes)) { var image = try twomg.openOrBare(bytes, path); // A bare file the size of a drive's own idea of a disk is a recording // of the surface rather than a description of the sectors on it. Only // the size says so — a `.nib` has no header — and no other kind of // Apple II disk file is that size, so the size is enough. if (image.data.len == disk2.Floppy.nibble_size) image.format = .nibble; return .{ .image = image }; } const disk = try shrinkit.nufx.diskAlloc(gpa, bytes); errdefer gpa.free(disk); return .{ .owned = disk, .image = .{ .data = disk, // A disk in a NuFX archive is blocks, in order, whatever // filesystem is on it: ShrinkIt read the drive a block at a time // and wrote down what it got. So a DOS 3.3 disk out of an archive // is in ProDOS order the same way a `.po` of one is, and the // sector skewing is the filesystem's problem rather than this // one's. .format = .prodos, // Nothing in a disk thread says the disk was write protected. The // record has an access byte, but it is the archived *file's* // access rather than the disk's, and reading it as the disk's // would lock most disks that came out of an archive. .write_protected = false, .volume_number = null, .blocks = @intCast(disk.len / 512), }, }; } /// What went wrong, in words for somebody who named a file rather than for a /// programmer. /// /// Worth having because the interesting failure here is not a broken file: it /// is an archive that is perfectly sound and simply has no disk in it, which /// is what most `.SHK` files are. pub fn explain(err: anyerror) []const u8 { return switch (err) { error.NoDisk => "that archive holds files rather than a disk", error.UnsupportedFormat => "that archive is squeezed in a way this cannot read", error.BadCrc => "that archive is damaged: what came out does not match its checksum", error.Corrupt => "that archive does not make sense partway through", error.NotTwoImg, error.NotAnArchive => "that file is not a disk image", error.Truncated => "that file stops in the middle", error.NotAWoz => "that file is not a disk image", error.Mangled => "that WOZ has been through something that thought it was text", error.NibbleImage => "that is a recording of a disk's surface, and only a 5.25-inch drive takes one", else => @errorName(err), }; } const testing = std.testing; test "a bare image is opened as itself and owns nothing" { var bare: [1024]u8 = @splat(0); bare[0] = 0x99; const opened = try open(testing.allocator, &bare, "somewhere/disk.po"); defer opened.deinit(testing.allocator); try testing.expectEqual(@as(?[]u8, null), opened.owned); try testing.expectEqual(twomg.Format.prodos, opened.image.format); try testing.expectEqual(@as(u8, 0x99), opened.image.data[0]); // And it is still the caller's own bytes, so writing to the disk writes to // the file's buffer. opened.image.data[0] = 0x42; try testing.expectEqual(@as(u8, 0x42), bare[0]); } test "a ShrinkIt archive is unpacked, and what it gives back is owned" { // An archive of one stored disk image, which is the shape a .SDK has. var buffer: [1024]u8 = undefined; const disk = "a disk, as far as this is concerned"; @memset(&buffer, 0); @memcpy(buffer[0..6], &shrinkit.nufx.magic); std.mem.writeInt(u32, buffer[8..12], 1, .little); const rec = shrinkit.nufx.master_header_size; @memcpy(buffer[rec..][0..4], &shrinkit.nufx.record_magic); std.mem.writeInt(u16, buffer[rec + 6 ..][0..2], 60, .little); std.mem.writeInt(u16, buffer[rec + 8 ..][0..2], 3, .little); std.mem.writeInt(u32, buffer[rec + 0x0a ..][0..4], 1, .little); const th = rec + 60; std.mem.writeInt(u16, buffer[th..][0..2], @intFromEnum(shrinkit.nufx.Class.data), .little); std.mem.writeInt(u16, buffer[th + 4 ..][0..2], @intFromEnum(shrinkit.nufx.Kind.disk_image), .little); std.mem.writeInt(u32, buffer[th + 8 ..][0..4], @intCast(disk.len), .little); std.mem.writeInt(u32, buffer[th + 12 ..][0..4], @intCast(disk.len), .little); const at = th + shrinkit.nufx.thread_header_size; @memcpy(buffer[at..][0..disk.len], disk); const file = buffer[0 .. at + disk.len]; const opened = try open(testing.allocator, file, "somewhere/game.sdk"); defer opened.deinit(testing.allocator); try testing.expect(opened.owned != null); try testing.expectEqualStrings(disk, opened.image.data); // Blocks in order, whatever the file was called: a `.sdk` name would // otherwise be read as DOS order, which no disk in an archive is. try testing.expectEqual(twomg.Format.prodos, opened.image.format); try testing.expectEqual(@as(?disk2.Order, .prodos), opened.image.order()); } test "a file the size of a recorded surface is taken as one" { // Only the size says so: a `.nib` has no header, and no other kind of // Apple II disk file is 232,960 bytes. const bytes = try testing.allocator.alloc(u8, disk2.Floppy.nibble_size); defer testing.allocator.free(bytes); @memset(bytes, 0xff); const opened = try open(testing.allocator, bytes, "somewhere/protected.nib"); defer opened.deinit(testing.allocator); try testing.expect(opened.image.isNibble()); // And it is neither sector order, because it is not sectors. try testing.expectEqual(@as(?disk2.Order, null), opened.image.order()); // A disk that is not that size is still read the old way, name and all. const ordinary = try testing.allocator.alloc(u8, disk2.image_size); defer testing.allocator.free(ordinary); @memset(ordinary, 0); const plain = try open(testing.allocator, ordinary, "somewhere/disk.po"); defer plain.deinit(testing.allocator); try testing.expect(!plain.image.isNibble()); try testing.expectEqual(@as(?disk2.Order, .prodos), plain.image.order()); } test "an archive with no disk in it says so in words" { try testing.expectEqualStrings( "that archive holds files rather than a disk", explain(error.NoDisk), ); try testing.expectEqualStrings("that file stops in the middle", explain(error.Truncated)); }