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,139 +736,165 @@ void ImageInitialize () {
|
|||||||
|
|
||||||
//===========================================================================
|
//===========================================================================
|
||||||
int ImageOpen (LPCTSTR imagefilename,
|
int ImageOpen (LPCTSTR imagefilename,
|
||||||
HIMAGE *imagehandle,
|
HIMAGE *hDiskImage_,
|
||||||
BOOL *writeprotected,
|
BOOL *pWriteProtected_,
|
||||||
BOOL createifnecessary) {
|
BOOL bCreateIfNecessary)
|
||||||
if (!(imagefilename && imagehandle && writeprotected && workbuffer))
|
{
|
||||||
return -1;
|
if (! (imagefilename && hDiskImage_ && pWriteProtected_ && workbuffer))
|
||||||
|
return IMAGE_ERROR_BAD_POINTER; // HACK: MAGIC # -1
|
||||||
|
|
||||||
// TRY TO OPEN THE IMAGE FILE
|
// TRY TO OPEN THE IMAGE FILE
|
||||||
HANDLE file = INVALID_HANDLE_VALUE;
|
HANDLE file = INVALID_HANDLE_VALUE;
|
||||||
if (!*writeprotected)
|
|
||||||
file = CreateFile(imagefilename,
|
if (! *pWriteProtected_)
|
||||||
|
file = CreateFile(imagefilename,
|
||||||
GENERIC_READ | GENERIC_WRITE,
|
GENERIC_READ | GENERIC_WRITE,
|
||||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||||
(LPSECURITY_ATTRIBUTES)NULL,
|
(LPSECURITY_ATTRIBUTES)NULL,
|
||||||
OPEN_EXISTING,
|
OPEN_EXISTING,
|
||||||
FILE_ATTRIBUTE_NORMAL,
|
FILE_ATTRIBUTE_NORMAL,
|
||||||
NULL);
|
NULL);
|
||||||
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;
|
|
||||||
}
|
|
||||||
if ((file == INVALID_HANDLE_VALUE) && createifnecessary)
|
|
||||||
file = CreateFile(imagefilename,
|
|
||||||
GENERIC_READ | GENERIC_WRITE,
|
|
||||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
||||||
(LPSECURITY_ATTRIBUTES)NULL,
|
|
||||||
CREATE_NEW,
|
|
||||||
FILE_ATTRIBUTE_NORMAL,
|
|
||||||
NULL);
|
|
||||||
|
|
||||||
// IF WE AREN'T ABLE TO OPEN THE FILE, RETURN
|
// File may have read-only attribute set, so try to open as read-only.
|
||||||
if (file == INVALID_HANDLE_VALUE)
|
if (file == INVALID_HANDLE_VALUE)
|
||||||
return 1;
|
{
|
||||||
|
file = CreateFile(
|
||||||
|
imagefilename,
|
||||||
|
GENERIC_READ,
|
||||||
|
FILE_SHARE_READ,
|
||||||
|
(LPSECURITY_ATTRIBUTES)NULL,
|
||||||
|
OPEN_EXISTING,
|
||||||
|
FILE_ATTRIBUTE_NORMAL,
|
||||||
|
NULL );
|
||||||
|
|
||||||
|
if (file != INVALID_HANDLE_VALUE)
|
||||||
|
*pWriteProtected_ = 1;
|
||||||
|
}
|
||||||
|
|
||||||
// DETERMINE THE FILE'S EXTENSION AND CONVERT IT TO LOWERCASE
|
if ((file == INVALID_HANDLE_VALUE) && bCreateIfNecessary)
|
||||||
LPCTSTR imagefileext = imagefilename;
|
file = CreateFile(
|
||||||
if (_tcsrchr(imagefileext,TEXT('\\')))
|
imagefilename,
|
||||||
imagefileext = _tcsrchr(imagefileext,TEXT('\\'))+1;
|
GENERIC_READ | GENERIC_WRITE,
|
||||||
if (_tcsrchr(imagefileext,TEXT('.')))
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||||
imagefileext = _tcsrchr(imagefileext,TEXT('.'));
|
(LPSECURITY_ATTRIBUTES)NULL,
|
||||||
TCHAR ext[_MAX_EXT];
|
CREATE_NEW,
|
||||||
_tcsncpy(ext,imagefileext,_MAX_EXT);
|
FILE_ATTRIBUTE_NORMAL,
|
||||||
CharLowerBuff(ext,_tcslen(ext));
|
NULL );
|
||||||
|
|
||||||
DWORD size = GetFileSize(file,NULL);
|
// IF WE AREN'T ABLE TO OPEN THE FILE, RETURN
|
||||||
LPBYTE view = NULL;
|
if (file == INVALID_HANDLE_VALUE)
|
||||||
LPBYTE imageptr = NULL;
|
return IMAGE_ERROR_UNABLE_TO_OPEN; // HACK: MAGIC # 1
|
||||||
DWORD format = 0xFFFFFFFF;
|
|
||||||
if (size > 0) {
|
|
||||||
|
|
||||||
// MAP THE FILE INTO MEMORY FOR USE BY THE DETECTION FUNCTIONS
|
// DETERMINE THE FILE'S EXTENSION AND CONVERT IT TO LOWERCASE
|
||||||
HANDLE mapping = CreateFileMapping(file,
|
LPCTSTR imagefileext = imagefilename;
|
||||||
(LPSECURITY_ATTRIBUTES)NULL,
|
if (_tcsrchr(imagefileext,TEXT('\\')))
|
||||||
PAGE_READONLY,
|
imagefileext = _tcsrchr(imagefileext,TEXT('\\'))+1;
|
||||||
0,0,NULL);
|
if (_tcsrchr(imagefileext,TEXT('.')))
|
||||||
view = (LPBYTE)MapViewOfFile(mapping,FILE_MAP_READ,0,0,0);
|
imagefileext = _tcsrchr(imagefileext,TEXT('.'));
|
||||||
imageptr = view;
|
TCHAR ext[_MAX_EXT];
|
||||||
if (imageptr) {
|
_tcsncpy(ext,imagefileext,_MAX_EXT);
|
||||||
|
CharLowerBuff(ext,_tcslen(ext));
|
||||||
|
|
||||||
// DETERMINE WHETHER THE FILE HAS A 128-BYTE MACBINARY HEADER
|
DWORD size = GetFileSize(file,NULL);
|
||||||
if ((size > 128) &&
|
LPBYTE view = NULL;
|
||||||
(!*imageptr) &&
|
LPBYTE pImage = NULL;
|
||||||
(*(imageptr+1) < 120) &&
|
|
||||||
(!*(imageptr+*(imageptr+1)+2)) &&
|
|
||||||
(*(imageptr+0x7A) == 0x81) &&
|
|
||||||
(*(imageptr+0x7B) == 0x81)) {
|
|
||||||
imageptr += 128;
|
|
||||||
size -= 128;
|
|
||||||
}
|
|
||||||
|
|
||||||
// CALL THE DETECTION FUNCTIONS IN ORDER, LOOKING FOR A MATCH
|
const DWORD UNKNOWN_FORMAT = 0xFFFFFFFF;
|
||||||
DWORD possibleformat = 0xFFFFFFFF;
|
DWORD format = UNKNOWN_FORMAT;
|
||||||
int loop = 0;
|
|
||||||
while ((loop < IMAGETYPES) && (format == 0xFFFFFFFF)) {
|
if (size > 0)
|
||||||
if (*ext && _tcsstr(imagetype[loop].rejectexts,ext))
|
{
|
||||||
++loop;
|
// MAP THE FILE INTO MEMORY FOR USE BY THE DETECTION FUNCTIONS
|
||||||
else {
|
HANDLE mapping = CreateFileMapping(
|
||||||
DWORD result = imagetype[loop].detect(imageptr,size);
|
file,
|
||||||
if (result == 2)
|
(LPSECURITY_ATTRIBUTES)NULL,
|
||||||
format = loop;
|
PAGE_READONLY,
|
||||||
else if ((result == 1) && (possibleformat == 0xFFFFFFFF))
|
0,0,NULL );
|
||||||
possibleformat = loop++;
|
|
||||||
else
|
|
||||||
++loop;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (format == 0xFFFFFFFF)
|
|
||||||
format = possibleformat;
|
|
||||||
|
|
||||||
// CLOSE THE MEMORY MAPPING
|
view = (LPBYTE)MapViewOfFile(mapping,FILE_MAP_READ,0,0,0);
|
||||||
UnmapViewOfFile(view);
|
pImage = view;
|
||||||
}
|
|
||||||
CloseHandle(mapping);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
|
|
||||||
// WE CREATE ONLY DOS ORDER (DO) OR 6656-NIBBLE (NIB) FORMAT FILES
|
if (pImage)
|
||||||
for (int loop = 1; loop <= 4; loop += 3)
|
{
|
||||||
if (*ext && _tcsstr(imagetype[loop].createexts,ext)) {
|
// DETERMINE WHETHER THE FILE HAS A 128-BYTE MACBINARY HEADER
|
||||||
format = loop;
|
if ((size > 128) &&
|
||||||
break;
|
(!*pImage) &&
|
||||||
}
|
(*(pImage+1) < 120) &&
|
||||||
}
|
(!*(pImage+*(pImage+1)+2)) &&
|
||||||
|
(*(pImage+0x7A) == 0x81) &&
|
||||||
|
(*(pImage+0x7B) == 0x81))
|
||||||
|
{
|
||||||
|
pImage += 128;
|
||||||
|
size -= 128;
|
||||||
|
}
|
||||||
|
|
||||||
// IF THE FILE DOES MATCH A KNOWN FORMAT...
|
// CALL THE DETECTION FUNCTIONS IN ORDER, LOOKING FOR A MATCH
|
||||||
if (format != 0xFFFFFFFF) {
|
DWORD possibleformat = UNKNOWN_FORMAT; // 0xFFFFFFFF;
|
||||||
|
int loop = 0;
|
||||||
|
while ((loop < IMAGETYPES) && (format == UNKNOWN_FORMAT)) // 0xFFFFFFFF)) {
|
||||||
|
{
|
||||||
|
if (*ext && _tcsstr(imagetype[loop].rejectexts,ext))
|
||||||
|
++loop;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DWORD result = imagetype[loop].detect(pImage,size);
|
||||||
|
if (result == 2)
|
||||||
|
format = loop;
|
||||||
|
else if ((result == 1) && (possibleformat == UNKNOWN_FORMAT)) // 0xFFFFFFFF))
|
||||||
|
possibleformat = loop++;
|
||||||
|
else
|
||||||
|
++loop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CREATE A RECORD FOR THE FILE, AND RETURN AN IMAGE HANDLE
|
if (format == UNKNOWN_FORMAT) // 0xFFFFFFFF)
|
||||||
*imagehandle = (HIMAGE)VirtualAlloc(NULL,sizeof(imageinforec),MEM_COMMIT,PAGE_READWRITE);
|
format = possibleformat;
|
||||||
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;
|
|
||||||
for (int track = 0; track < TRACKS; track++)
|
|
||||||
((imageinfoptr)*imagehandle)->validtrack[track] = (size > 0);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CloseHandle(file);
|
// CLOSE THE MEMORY MAPPING
|
||||||
if (!(size > 0))
|
UnmapViewOfFile(view);
|
||||||
DeleteFile(imagefilename);
|
}
|
||||||
return 2;
|
CloseHandle(mapping);
|
||||||
|
}
|
||||||
|
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))
|
||||||
|
{
|
||||||
|
format = loop;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IF THE FILE DOES MATCH A KNOWN FORMAT...
|
||||||
|
if (format != UNKNOWN_FORMAT)
|
||||||
|
{
|
||||||
|
// CREATE A RECORD FOR THE FILE, AND RETURN AN IMAGE HANDLE
|
||||||
|
*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)*hDiskImage_)->validtrack[track] = (size > 0);
|
||||||
|
|
||||||
|
return IMAGE_ERROR_NONE; // HACK: MAGIC # 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CloseHandle(file);
|
||||||
|
if (!(size > 0))
|
||||||
|
DeleteFile(imagefilename);
|
||||||
|
|
||||||
|
return IMAGE_ERROR_BAD_SIZE; // HACK: MAGIC # 2
|
||||||
}
|
}
|
||||||
|
|
||||||
//===========================================================================
|
//===========================================================================
|
||||||
|
@ -8,6 +8,17 @@ BOOL ImageBoot (HIMAGE);
|
|||||||
void ImageClose (HIMAGE);
|
void ImageClose (HIMAGE);
|
||||||
void ImageDestroy ();
|
void ImageDestroy ();
|
||||||
void ImageInitialize ();
|
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 ImageReadTrack (HIMAGE,int,int,LPBYTE,int *);
|
||||||
void ImageWriteTrack (HIMAGE,int,int,LPBYTE,int);
|
void ImageWriteTrack (HIMAGE,int,int,LPBYTE,int);
|
||||||
|
Loading…
Reference in New Issue
Block a user