Cleaned up ImageOpen()

This commit is contained in:
mpohoreski 2006-03-09 21:40:16 +00:00
parent 7935b18c15
commit bdf8dfdfe7
2 changed files with 153 additions and 116 deletions

View File

@ -736,15 +736,17 @@ 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)
if (! *pWriteProtected_)
file = CreateFile(imagefilename, file = CreateFile(imagefilename,
GENERIC_READ | GENERIC_WRITE, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
@ -752,19 +754,26 @@ int ImageOpen (LPCTSTR imagefilename,
OPEN_EXISTING, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL,
NULL); 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, GENERIC_READ,
FILE_SHARE_READ, FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES)NULL, (LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_NORMAL,
NULL ); NULL );
if (file != INVALID_HANDLE_VALUE) 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, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL, (LPSECURITY_ATTRIBUTES)NULL,
@ -774,7 +783,7 @@ int ImageOpen (LPCTSTR imagefilename,
// IF WE AREN'T ABLE TO OPEN THE FILE, RETURN // IF WE AREN'T ABLE TO OPEN THE FILE, RETURN
if (file == INVALID_HANDLE_VALUE) 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 // DETERMINE THE FILE'S EXTENSION AND CONVERT IT TO LOWERCASE
LPCTSTR imagefileext = imagefilename; LPCTSTR imagefileext = imagefilename;
@ -788,47 +797,57 @@ int ImageOpen (LPCTSTR imagefilename,
DWORD size = GetFileSize(file,NULL); DWORD size = GetFileSize(file,NULL);
LPBYTE view = NULL; LPBYTE view = NULL;
LPBYTE imageptr = NULL; LPBYTE pImage = NULL;
DWORD format = 0xFFFFFFFF;
if (size > 0) {
const DWORD UNKNOWN_FORMAT = 0xFFFFFFFF;
DWORD format = UNKNOWN_FORMAT;
if (size > 0)
{
// MAP THE FILE INTO MEMORY FOR USE BY THE DETECTION FUNCTIONS // MAP THE FILE INTO MEMORY FOR USE BY THE DETECTION FUNCTIONS
HANDLE mapping = CreateFileMapping(file, HANDLE mapping = CreateFileMapping(
file,
(LPSECURITY_ATTRIBUTES)NULL, (LPSECURITY_ATTRIBUTES)NULL,
PAGE_READONLY, PAGE_READONLY,
0,0,NULL ); 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 // DETERMINE WHETHER THE FILE HAS A 128-BYTE MACBINARY HEADER
if ((size > 128) && if ((size > 128) &&
(!*imageptr) && (!*pImage) &&
(*(imageptr+1) < 120) && (*(pImage+1) < 120) &&
(!*(imageptr+*(imageptr+1)+2)) && (!*(pImage+*(pImage+1)+2)) &&
(*(imageptr+0x7A) == 0x81) && (*(pImage+0x7A) == 0x81) &&
(*(imageptr+0x7B) == 0x81)) { (*(pImage+0x7B) == 0x81))
imageptr += 128; {
pImage += 128;
size -= 128; size -= 128;
} }
// CALL THE DETECTION FUNCTIONS IN ORDER, LOOKING FOR A MATCH // CALL THE DETECTION FUNCTIONS IN ORDER, LOOKING FOR A MATCH
DWORD possibleformat = 0xFFFFFFFF; DWORD possibleformat = UNKNOWN_FORMAT; // 0xFFFFFFFF;
int loop = 0; int loop = 0;
while ((loop < IMAGETYPES) && (format == 0xFFFFFFFF)) { while ((loop < IMAGETYPES) && (format == UNKNOWN_FORMAT)) // 0xFFFFFFFF)) {
{
if (*ext && _tcsstr(imagetype[loop].rejectexts,ext)) if (*ext && _tcsstr(imagetype[loop].rejectexts,ext))
++loop; ++loop;
else { else
DWORD result = imagetype[loop].detect(imageptr,size); {
DWORD result = imagetype[loop].detect(pImage,size);
if (result == 2) if (result == 2)
format = loop; format = loop;
else if ((result == 1) && (possibleformat == 0xFFFFFFFF)) else if ((result == 1) && (possibleformat == UNKNOWN_FORMAT)) // 0xFFFFFFFF))
possibleformat = loop++; possibleformat = loop++;
else else
++loop; ++loop;
} }
} }
if (format == 0xFFFFFFFF)
if (format == UNKNOWN_FORMAT) // 0xFFFFFFFF)
format = possibleformat; format = possibleformat;
// CLOSE THE MEMORY MAPPING // CLOSE THE MEMORY MAPPING
@ -838,37 +857,44 @@ int ImageOpen (LPCTSTR imagefilename,
} }
else else
{ {
// WE CREATE ONLY DOS ORDER (DO) OR 6656-NIBBLE (NIB) FORMAT FILES // WE CREATE ONLY DOS ORDER (DO) OR 6656-NIBBLE (NIB) FORMAT FILES
for (int loop = 1; loop <= 4; loop += 3) for (int loop = 1; loop <= 4; loop += 3)
if (*ext && _tcsstr(imagetype[loop].createexts,ext)) { {
if (*ext && _tcsstr(imagetype[loop].createexts,ext))
{
format = loop; format = loop;
break; break;
} }
}
} }
// IF THE FILE DOES MATCH A KNOWN FORMAT... // 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 // CREATE A RECORD FOR THE FILE, AND RETURN AN IMAGE HANDLE
*imagehandle = (HIMAGE)VirtualAlloc(NULL,sizeof(imageinforec),MEM_COMMIT,PAGE_READWRITE); *hDiskImage_ = (HIMAGE)VirtualAlloc(NULL,sizeof(imageinforec),MEM_COMMIT,PAGE_READWRITE);
if (*imagehandle) { if (*hDiskImage_)
ZeroMemory(*imagehandle,sizeof(imageinforec)); {
_tcsncpy(((imageinfoptr)*imagehandle)->filename,imagefilename,MAX_PATH); ZeroMemory(*hDiskImage_,sizeof(imageinforec));
((imageinfoptr)*imagehandle)->format = format; _tcsncpy(((imageinfoptr)*hDiskImage_)->filename,imagefilename,MAX_PATH);
((imageinfoptr)*imagehandle)->file = file; ((imageinfoptr)*hDiskImage_)->format = format;
((imageinfoptr)*imagehandle)->offset = imageptr-view; ((imageinfoptr)*hDiskImage_)->file = file;
((imageinfoptr)*imagehandle)->writeprotected = *writeprotected; ((imageinfoptr)*hDiskImage_)->offset = pImage-view;
((imageinfoptr)*hDiskImage_)->writeprotected = *pWriteProtected_;
for (int track = 0; track < TRACKS; track++) for (int track = 0; track < TRACKS; track++)
((imageinfoptr)*imagehandle)->validtrack[track] = (size > 0); ((imageinfoptr)*hDiskImage_)->validtrack[track] = (size > 0);
return 0;
return IMAGE_ERROR_NONE; // HACK: MAGIC # 0
} }
} }
CloseHandle(file); CloseHandle(file);
if (!(size > 0)) if (!(size > 0))
DeleteFile(imagefilename); DeleteFile(imagefilename);
return 2;
return IMAGE_ERROR_BAD_SIZE; // HACK: MAGIC # 2
} }
//=========================================================================== //===========================================================================

View File

@ -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);