mirror of
https://github.com/AppleWin/AppleWin.git
synced 2025-01-09 05:32:05 +00:00
Cleaned up ImageOpen()
This commit is contained in:
parent
7935b18c15
commit
bdf8dfdfe7
@ -736,15 +736,17 @@ void ImageInitialize () {
|
||||
|
||||
//===========================================================================
|
||||
int ImageOpen (LPCTSTR imagefilename,
|
||||
HIMAGE *imagehandle,
|
||||
BOOL *writeprotected,
|
||||
BOOL createifnecessary) {
|
||||
if (!(imagefilename && imagehandle && writeprotected && workbuffer))
|
||||
return -1;
|
||||
HIMAGE *hDiskImage_,
|
||||
BOOL *pWriteProtected_,
|
||||
BOOL bCreateIfNecessary)
|
||||
{
|
||||
if (! (imagefilename && hDiskImage_ && pWriteProtected_ && workbuffer))
|
||||
return IMAGE_ERROR_BAD_POINTER; // HACK: MAGIC # -1
|
||||
|
||||
// TRY TO OPEN THE IMAGE FILE
|
||||
HANDLE file = INVALID_HANDLE_VALUE;
|
||||
if (!*writeprotected)
|
||||
|
||||
if (! *pWriteProtected_)
|
||||
file = CreateFile(imagefilename,
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
@ -752,19 +754,26 @@ int ImageOpen (LPCTSTR imagefilename,
|
||||
OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL,
|
||||
NULL);
|
||||
if (file == INVALID_HANDLE_VALUE) {
|
||||
file = CreateFile(imagefilename,
|
||||
|
||||
// File may have read-only attribute set, so try to open as read-only.
|
||||
if (file == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
file = CreateFile(
|
||||
imagefilename,
|
||||
GENERIC_READ,
|
||||
FILE_SHARE_READ,
|
||||
(LPSECURITY_ATTRIBUTES)NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL,
|
||||
NULL );
|
||||
|
||||
if (file != INVALID_HANDLE_VALUE)
|
||||
*writeprotected = 1;
|
||||
*pWriteProtected_ = 1;
|
||||
}
|
||||
if ((file == INVALID_HANDLE_VALUE) && createifnecessary)
|
||||
file = CreateFile(imagefilename,
|
||||
|
||||
if ((file == INVALID_HANDLE_VALUE) && bCreateIfNecessary)
|
||||
file = CreateFile(
|
||||
imagefilename,
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
(LPSECURITY_ATTRIBUTES)NULL,
|
||||
@ -774,7 +783,7 @@ int ImageOpen (LPCTSTR imagefilename,
|
||||
|
||||
// IF WE AREN'T ABLE TO OPEN THE FILE, RETURN
|
||||
if (file == INVALID_HANDLE_VALUE)
|
||||
return 1;
|
||||
return IMAGE_ERROR_UNABLE_TO_OPEN; // HACK: MAGIC # 1
|
||||
|
||||
// DETERMINE THE FILE'S EXTENSION AND CONVERT IT TO LOWERCASE
|
||||
LPCTSTR imagefileext = imagefilename;
|
||||
@ -788,47 +797,57 @@ int ImageOpen (LPCTSTR imagefilename,
|
||||
|
||||
DWORD size = GetFileSize(file,NULL);
|
||||
LPBYTE view = NULL;
|
||||
LPBYTE imageptr = NULL;
|
||||
DWORD format = 0xFFFFFFFF;
|
||||
if (size > 0) {
|
||||
LPBYTE pImage = NULL;
|
||||
|
||||
const DWORD UNKNOWN_FORMAT = 0xFFFFFFFF;
|
||||
DWORD format = UNKNOWN_FORMAT;
|
||||
|
||||
if (size > 0)
|
||||
{
|
||||
// MAP THE FILE INTO MEMORY FOR USE BY THE DETECTION FUNCTIONS
|
||||
HANDLE mapping = CreateFileMapping(file,
|
||||
HANDLE mapping = CreateFileMapping(
|
||||
file,
|
||||
(LPSECURITY_ATTRIBUTES)NULL,
|
||||
PAGE_READONLY,
|
||||
0,0,NULL );
|
||||
view = (LPBYTE)MapViewOfFile(mapping,FILE_MAP_READ,0,0,0);
|
||||
imageptr = view;
|
||||
if (imageptr) {
|
||||
|
||||
view = (LPBYTE)MapViewOfFile(mapping,FILE_MAP_READ,0,0,0);
|
||||
pImage = view;
|
||||
|
||||
if (pImage)
|
||||
{
|
||||
// DETERMINE WHETHER THE FILE HAS A 128-BYTE MACBINARY HEADER
|
||||
if ((size > 128) &&
|
||||
(!*imageptr) &&
|
||||
(*(imageptr+1) < 120) &&
|
||||
(!*(imageptr+*(imageptr+1)+2)) &&
|
||||
(*(imageptr+0x7A) == 0x81) &&
|
||||
(*(imageptr+0x7B) == 0x81)) {
|
||||
imageptr += 128;
|
||||
(!*pImage) &&
|
||||
(*(pImage+1) < 120) &&
|
||||
(!*(pImage+*(pImage+1)+2)) &&
|
||||
(*(pImage+0x7A) == 0x81) &&
|
||||
(*(pImage+0x7B) == 0x81))
|
||||
{
|
||||
pImage += 128;
|
||||
size -= 128;
|
||||
}
|
||||
|
||||
// CALL THE DETECTION FUNCTIONS IN ORDER, LOOKING FOR A MATCH
|
||||
DWORD possibleformat = 0xFFFFFFFF;
|
||||
DWORD possibleformat = UNKNOWN_FORMAT; // 0xFFFFFFFF;
|
||||
int loop = 0;
|
||||
while ((loop < IMAGETYPES) && (format == 0xFFFFFFFF)) {
|
||||
while ((loop < IMAGETYPES) && (format == UNKNOWN_FORMAT)) // 0xFFFFFFFF)) {
|
||||
{
|
||||
if (*ext && _tcsstr(imagetype[loop].rejectexts,ext))
|
||||
++loop;
|
||||
else {
|
||||
DWORD result = imagetype[loop].detect(imageptr,size);
|
||||
else
|
||||
{
|
||||
DWORD result = imagetype[loop].detect(pImage,size);
|
||||
if (result == 2)
|
||||
format = loop;
|
||||
else if ((result == 1) && (possibleformat == 0xFFFFFFFF))
|
||||
else if ((result == 1) && (possibleformat == UNKNOWN_FORMAT)) // 0xFFFFFFFF))
|
||||
possibleformat = loop++;
|
||||
else
|
||||
++loop;
|
||||
}
|
||||
}
|
||||
if (format == 0xFFFFFFFF)
|
||||
|
||||
if (format == UNKNOWN_FORMAT) // 0xFFFFFFFF)
|
||||
format = possibleformat;
|
||||
|
||||
// CLOSE THE MEMORY MAPPING
|
||||
@ -838,37 +857,44 @@ int ImageOpen (LPCTSTR imagefilename,
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// WE CREATE ONLY DOS ORDER (DO) OR 6656-NIBBLE (NIB) FORMAT FILES
|
||||
for (int loop = 1; loop <= 4; loop += 3)
|
||||
if (*ext && _tcsstr(imagetype[loop].createexts,ext)) {
|
||||
{
|
||||
if (*ext && _tcsstr(imagetype[loop].createexts,ext))
|
||||
{
|
||||
format = loop;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// IF THE FILE DOES MATCH A KNOWN FORMAT...
|
||||
if (format != 0xFFFFFFFF) {
|
||||
|
||||
if (format != UNKNOWN_FORMAT)
|
||||
{
|
||||
// CREATE A RECORD FOR THE FILE, AND RETURN AN IMAGE HANDLE
|
||||
*imagehandle = (HIMAGE)VirtualAlloc(NULL,sizeof(imageinforec),MEM_COMMIT,PAGE_READWRITE);
|
||||
if (*imagehandle) {
|
||||
ZeroMemory(*imagehandle,sizeof(imageinforec));
|
||||
_tcsncpy(((imageinfoptr)*imagehandle)->filename,imagefilename,MAX_PATH);
|
||||
((imageinfoptr)*imagehandle)->format = format;
|
||||
((imageinfoptr)*imagehandle)->file = file;
|
||||
((imageinfoptr)*imagehandle)->offset = imageptr-view;
|
||||
((imageinfoptr)*imagehandle)->writeprotected = *writeprotected;
|
||||
*hDiskImage_ = (HIMAGE)VirtualAlloc(NULL,sizeof(imageinforec),MEM_COMMIT,PAGE_READWRITE);
|
||||
if (*hDiskImage_)
|
||||
{
|
||||
ZeroMemory(*hDiskImage_,sizeof(imageinforec));
|
||||
_tcsncpy(((imageinfoptr)*hDiskImage_)->filename,imagefilename,MAX_PATH);
|
||||
((imageinfoptr)*hDiskImage_)->format = format;
|
||||
((imageinfoptr)*hDiskImage_)->file = file;
|
||||
((imageinfoptr)*hDiskImage_)->offset = pImage-view;
|
||||
((imageinfoptr)*hDiskImage_)->writeprotected = *pWriteProtected_;
|
||||
|
||||
for (int track = 0; track < TRACKS; track++)
|
||||
((imageinfoptr)*imagehandle)->validtrack[track] = (size > 0);
|
||||
return 0;
|
||||
((imageinfoptr)*hDiskImage_)->validtrack[track] = (size > 0);
|
||||
|
||||
return IMAGE_ERROR_NONE; // HACK: MAGIC # 0
|
||||
}
|
||||
}
|
||||
|
||||
CloseHandle(file);
|
||||
if (!(size > 0))
|
||||
DeleteFile(imagefilename);
|
||||
return 2;
|
||||
|
||||
return IMAGE_ERROR_BAD_SIZE; // HACK: MAGIC # 2
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
|
@ -8,6 +8,17 @@ BOOL ImageBoot (HIMAGE);
|
||||
void ImageClose (HIMAGE);
|
||||
void ImageDestroy ();
|
||||
void ImageInitialize ();
|
||||
int ImageOpen (LPCTSTR,HIMAGE *,BOOL *,BOOL);
|
||||
|
||||
enum ImageError_e
|
||||
{
|
||||
IMAGE_ERROR_BAD_POINTER =-1,
|
||||
IMAGE_ERROR_NONE = 0,
|
||||
IMAGE_ERROR_UNABLE_TO_OPEN = 1,
|
||||
IMAGE_ERROR_BAD_SIZE = 2
|
||||
};
|
||||
|
||||
//int ImageOpen (LPCTSTR,HIMAGE *,BOOL *,BOOL);
|
||||
int ImageOpen (LPCTSTR imagefilename, HIMAGE *hDiskImage_, BOOL *pWriteProtected_, BOOL bCreateIfNecessary );
|
||||
|
||||
void ImageReadTrack (HIMAGE,int,int,LPBYTE,int *);
|
||||
void ImageWriteTrack (HIMAGE,int,int,LPBYTE,int);
|
||||
|
Loading…
Reference in New Issue
Block a user