diff --git a/Disk.cpp b/Disk.cpp index 497d760..d72996e 100644 --- a/Disk.cpp +++ b/Disk.cpp @@ -7,8 +7,7 @@ */ #include "Disk.h" -#include "DiskCopy42.h" -#include "UniversalDiskImage.h" + #include "common.h" @@ -24,6 +23,7 @@ #include #include +#include struct ucmp { @@ -36,6 +36,8 @@ struct ucmp using std::set; using std::vector; +using namespace LittleEndian; + typedef set uset; Disk::Disk() @@ -61,111 +63,6 @@ DiskPointer Disk::OpenFile(Device::BlockDevicePointer device) return disk; } -#if 0 -Disk *Disk::OpenFile(const char *file, unsigned flags) -{ - int fd; - struct stat st; - size_t size; - unsigned blocks; - - unsigned offset; - - - void *map; - Disk *d = NULL; - - - - fd = open(file, O_RDONLY); - if (fd >= 0) - { - - if (fstat(fd, &st) == 0) - { - size = st.st_size; - - // raw disk images must be a blocksize multiple and <= 32 Meg. - - if ( (size & 0x1ff) == 0 - && size > 0 - && size <= 32 * 1024 * 1024 - ) - { - blocks = size >> 9; - offset = 0; - } - else { - - // check for disk copy4.2 / universal disk image. - uint8_t buffer[1024]; - - if (read(fd, buffer, 1024) != 1024) - { - close(fd); - return NULL; - } - - bool ok = false; - do { - - DiskCopy42 dc; - - if (dc.Load(buffer) - && size == 84 + dc.data_size + dc.tag_size - && (dc.data_size & 0x1ff) == 0) - { - offset = 84; - blocks = dc.data_size >> 9; - ok = true; - flags |= P8_DC42; - break; - } - - UniversalDiskImage udi; - - if (udi.Load(buffer) - //&& udi.version == 1 - && udi.image_format == UDI_FORMAT_PRODOS_ORDER) - { - - blocks = udi.data_blocks; - offset = udi.data_offset; - ok = true; - flags |= P8_2MG; - break; - } - - } while (false); - - if (!ok) - { - close(fd); - return NULL; - } - lseek(fd, 0, SEEK_SET); - } - - - map = mmap(NULL, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); - if (map != (void *)-1) - { - d = new Disk(); - d->_size = size; - d->_data = (uint8_t *)map; - d->_blocks = blocks; - d->_offset = offset; - d->_flags = flags; - } - - } - close(fd); - } - - return d; -} -#endif - // load the mini entry into the regular entry. int Disk::Normalize(FileEntry &f, unsigned fork, ExtendedEntry *ee) { @@ -447,8 +344,8 @@ int Disk::ReadVolume(VolumeEntry *volume, std::vector *files) if (ok < 0) return ok; - prev = load16(&buffer[0x00]); - next = load16(&buffer[0x02]); + prev = Read16(&buffer[0x00]); + next = Read16(&buffer[0x02]); VolumeEntry v; v.Load(buffer + 0x04); @@ -494,8 +391,8 @@ int Disk::ReadVolume(VolumeEntry *volume, std::vector *files) if (ok < 0) return ok; block = next; - prev = load16(&buffer[0x00]); - next = load16(&buffer[0x02]); + prev = Read16(&buffer[0x00]); + next = Read16(&buffer[0x02]); index = 0; } @@ -524,8 +421,8 @@ int Disk::ReadDirectory(unsigned block, SubdirEntry *dir, std::vector if (ok < 0) return ok; - prev = load16(&buffer[0x00]); - next = load16(&buffer[0x02]); + prev = Read16(&buffer[0x00]); + next = Read16(&buffer[0x02]); SubdirEntry v; v.Load(buffer + 0x04); @@ -574,8 +471,8 @@ int Disk::ReadDirectory(unsigned block, SubdirEntry *dir, std::vector block = next; - prev = load16(&buffer[0x00]); - next = load16(&buffer[0x02]); + prev = Read16(&buffer[0x00]); + next = Read16(&buffer[0x02]); index = 0; } diff --git a/ProDOS/File.cpp b/ProDOS/File.cpp index 087dcf7..985d8a4 100644 --- a/ProDOS/File.cpp +++ b/ProDOS/File.cpp @@ -8,6 +8,7 @@ #include #include +#include #include "common.h" #include @@ -15,6 +16,7 @@ #include #include +using namespace LittleEndian; bool FileEntry::Load(const void *data) @@ -31,18 +33,18 @@ bool FileEntry::Load(const void *data) file_type = cp[0x10]; - key_pointer = load16(&cp[0x11]); + key_pointer = Read16(&cp[0x11]); - blocks_used = load16(&cp[0x13]); + blocks_used = Read16(&cp[0x13]); - eof = load24(&cp[0x15]); + eof = Read24(&cp[0x15]); - creation = ProDOS::DateTime(load16(&cp[0x18]), load16(&cp[0x1a])); + creation = ProDOS::DateTime(Read16(&cp[0x18]), Read16(&cp[0x1a])); //version = cp[0x1c]; //min_version = cp[0x1d]; - unsigned xcase = load16(&cp[0x1c]); + unsigned xcase = Read16(&cp[0x1c]); if (xcase & 0x8000) { // gsos technote #8 @@ -59,11 +61,11 @@ bool FileEntry::Load(const void *data) access = cp[0x1e]; - aux_type = load16(&cp[0x1f]); + aux_type = Read16(&cp[0x1f]); - last_mod = ProDOS::DateTime(load16(&cp[0x21]), load16(&cp[0x23])); + last_mod = ProDOS::DateTime(Read16(&cp[0x21]), Read16(&cp[0x23])); - header_pointer = load16(&cp[0x25]); + header_pointer = Read16(&cp[0x25]); return true; } @@ -79,16 +81,16 @@ bool ExtendedEntry::Load(const void *data) // offset 0 - mini entry for data fork dataFork.storage_type = cp[0x00] & 0x0f; - dataFork.key_block = load16(&cp[0x01]); - dataFork.blocks_used = load16(&cp[0x03]); - dataFork.eof = load24(&cp[0x05]); + dataFork.key_block = Read16(&cp[0x01]); + dataFork.blocks_used = Read16(&cp[0x03]); + dataFork.eof = Read24(&cp[0x05]); // offset 256 - mini entry for resource fork. resourceFork.storage_type = cp[256 + 0x00] & 0x0f; - resourceFork.key_block = load16(&cp[256 + 0x01]); - resourceFork.blocks_used = load16(&cp[256 + 0x03]); - resourceFork.eof = load24(&cp[256 + 0x05]); + resourceFork.key_block = Read16(&cp[256 + 0x01]); + resourceFork.blocks_used = Read16(&cp[256 + 0x03]); + resourceFork.eof = Read24(&cp[256 + 0x05]); // xFInfo may be missing. bzero(FInfo, sizeof(FInfo)); @@ -135,15 +137,15 @@ bool VolumeEntry::Load(const void *data) // 0x14--0x1b reserved - creation = ProDOS::DateTime(load16(&cp[0x18]), load16(&cp[0x1a])); - last_mod = ProDOS::DateTime(load16(&cp[0x12]), load16(&cp[0x14])); + creation = ProDOS::DateTime(Read16(&cp[0x18]), Read16(&cp[0x1a])); + last_mod = ProDOS::DateTime(Read16(&cp[0x12]), Read16(&cp[0x14])); if (last_mod == 0) last_mod = creation; //version = cp[0x1c]; //min_version = cp[0x1d]; - unsigned xcase = load16(&cp[0x16]); + unsigned xcase = Read16(&cp[0x16]); if (xcase & 0x8000) { // gsos technote #8 @@ -162,11 +164,11 @@ bool VolumeEntry::Load(const void *data) entries_per_block = cp[0x20]; - file_count = load16(&cp[0x21]); + file_count = Read16(&cp[0x21]); - bit_map_pointer = load16(&cp[0x23]); + bit_map_pointer = Read16(&cp[0x23]); - total_blocks = load16(&cp[0x25]); + total_blocks = Read16(&cp[0x25]); return true; } @@ -192,7 +194,7 @@ bool SubdirEntry::Load(const void *data) // 0x145-0x1b reserved - creation = ProDOS::DateTime(load16(&cp[0x18]), load16(&cp[0x1a])); + creation = ProDOS::DateTime(Read16(&cp[0x18]), Read16(&cp[0x1a])); //version = cp[0x1c]; //min_version = cp[0x1d]; @@ -216,9 +218,9 @@ bool SubdirEntry::Load(const void *data) entries_per_block = cp[0x20]; - file_count = load16(&cp[0x21]); + file_count = Read16(&cp[0x21]); - parent_pointer = load16(&cp[0x23]); + parent_pointer = Read16(&cp[0x23]); parent_entry = cp[0x25]; diff --git a/common.h b/common.h index 39774f2..615bfbc 100644 --- a/common.h +++ b/common.h @@ -13,36 +13,6 @@ #define BLOCK_SIZE 512 -// little endian. - -inline unsigned load16(const uint8_t *cp) -{ - return (cp[1] << 8 ) | cp[0]; -} - -inline unsigned load24(const uint8_t *cp) -{ - return (cp[2] << 16 ) | (cp[1] << 8) | (cp[0]); -} - -inline unsigned load32(const uint8_t *cp) -{ - return (cp[3] << 24) | (cp[2] << 16 ) | (cp[1] << 8) | (cp[0]); -} - - -// big endian format. -inline unsigned load16_be(const uint8_t *cp) -{ - return (cp[0] << 8) | (cp[1]); -} - -inline unsigned load32_be(const uint8_t *cp) -{ - return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | (cp[3]); -} - - #endif