2007-03-27 17:47:10 +00:00
|
|
|
/*
|
|
|
|
* CiderPress
|
|
|
|
* Copyright (C) 2007 by faddenSoft, LLC. All Rights Reserved.
|
|
|
|
* See the file LICENSE for distribution terms.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* DiskImgLib global utility functions.
|
|
|
|
*/
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "DiskImgPriv.h"
|
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
#define kFilenameExtDelim '.' /* separates extension from filename */
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get values from a memory buffer.
|
|
|
|
*/
|
2014-11-21 21:18:20 +00:00
|
|
|
uint16_t DiskImgLib::GetShortLE(const uint8_t* ptr)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-21 21:18:20 +00:00
|
|
|
return *ptr | (uint16_t) *(ptr+1) << 8;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 21:18:20 +00:00
|
|
|
uint32_t DiskImgLib::GetLongLE(const uint8_t* ptr)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
return *ptr |
|
2014-11-21 21:18:20 +00:00
|
|
|
(uint32_t) *(ptr+1) << 8 |
|
|
|
|
(uint32_t) *(ptr+2) << 16 |
|
|
|
|
(uint32_t) *(ptr+3) << 24;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 21:18:20 +00:00
|
|
|
uint16_t DiskImgLib::GetShortBE(const uint8_t* ptr)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-21 21:18:20 +00:00
|
|
|
return *(ptr+1) | (uint16_t) *ptr << 8;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 21:18:20 +00:00
|
|
|
uint32_t DiskImgLib::GetLongBE(const uint8_t* ptr)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
return *(ptr+3) |
|
2014-11-21 21:18:20 +00:00
|
|
|
(uint32_t) *(ptr+2) << 8 |
|
|
|
|
(uint32_t) *(ptr+1) << 16 |
|
|
|
|
(uint32_t) *ptr << 24;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 21:18:20 +00:00
|
|
|
uint32_t DiskImgLib::Get24BE(const uint8_t* ptr)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
return *(ptr+2) |
|
2014-11-21 21:18:20 +00:00
|
|
|
(uint32_t) *(ptr+1) << 8 |
|
|
|
|
(uint32_t) *ptr << 16;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 20:56:19 +00:00
|
|
|
void DiskImgLib::PutShortLE(uint8_t* ptr, uint16_t val)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-21 21:18:20 +00:00
|
|
|
*ptr++ = (uint8_t) val;
|
2007-03-27 17:47:10 +00:00
|
|
|
*ptr = val >> 8;
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:56:19 +00:00
|
|
|
void DiskImgLib::PutLongLE(uint8_t* ptr, uint32_t val)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-21 21:18:20 +00:00
|
|
|
*ptr++ = (uint8_t) val;
|
|
|
|
*ptr++ = (uint8_t) (val >> 8);
|
|
|
|
*ptr++ = (uint8_t) (val >> 16);
|
|
|
|
*ptr = (uint8_t) (val >> 24);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 20:56:19 +00:00
|
|
|
void DiskImgLib::PutShortBE(uint8_t* ptr, uint16_t val)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
|
|
|
*ptr++ = val >> 8;
|
2014-11-21 21:18:20 +00:00
|
|
|
*ptr = (uint8_t) val;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 20:56:19 +00:00
|
|
|
void DiskImgLib::PutLongBE(uint8_t* ptr, uint32_t val)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-21 21:18:20 +00:00
|
|
|
*ptr++ = (uint8_t) (val >> 24);
|
|
|
|
*ptr++ = (uint8_t) (val >> 16);
|
|
|
|
*ptr++ = (uint8_t) (val >> 8);
|
|
|
|
*ptr = (uint8_t) val;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a two-byte little-endian value.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskImgLib::ReadShortLE(GenericFD* pGFD, uint16_t* pBuf)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
2014-11-21 21:18:20 +00:00
|
|
|
uint8_t val[2];
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = pGFD->Read(&val[0], 1);
|
|
|
|
if (dierr == kDIErrNone)
|
|
|
|
dierr = pGFD->Read(&val[1], 1);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
*pBuf = val[0] | (short) val[1] << 8;
|
2014-11-04 00:26:53 +00:00
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a four-byte little-endian value.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskImgLib::ReadLongLE(GenericFD* pGFD, uint32_t* pBuf)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
2014-11-21 21:18:20 +00:00
|
|
|
uint8_t val[4];
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
dierr = pGFD->Read(&val[0], 1);
|
|
|
|
if (dierr == kDIErrNone)
|
|
|
|
dierr = pGFD->Read(&val[1], 1);
|
|
|
|
if (dierr == kDIErrNone)
|
|
|
|
dierr = pGFD->Read(&val[2], 1);
|
|
|
|
if (dierr == kDIErrNone)
|
|
|
|
dierr = pGFD->Read(&val[3], 1);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-21 21:18:20 +00:00
|
|
|
*pBuf = val[0] | (uint32_t)val[1] << 8 |
|
|
|
|
(uint32_t)val[2] << 16 | (uint32_t)val[3] << 24;
|
2014-11-04 00:26:53 +00:00
|
|
|
return dierr;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a two-byte little-endian value.
|
|
|
|
*/
|
2014-11-21 21:18:20 +00:00
|
|
|
DIError DiskImgLib::WriteShortLE(FILE* fp, uint16_t val)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
|
|
|
putc(val, fp);
|
|
|
|
putc(val >> 8, fp);
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a four-byte little-endian value.
|
|
|
|
*/
|
2014-11-21 21:18:20 +00:00
|
|
|
DIError DiskImgLib::WriteLongLE(FILE* fp, uint32_t val)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
|
|
|
putc(val, fp);
|
|
|
|
putc(val >> 8, fp);
|
|
|
|
putc(val >> 16, fp);
|
|
|
|
putc(val >> 24, fp);
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrNone;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a two-byte little-endian value.
|
|
|
|
*/
|
2014-11-21 21:18:20 +00:00
|
|
|
DIError DiskImgLib::WriteShortLE(GenericFD* pGFD, uint16_t val)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-21 21:18:20 +00:00
|
|
|
uint8_t buf;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-21 21:18:20 +00:00
|
|
|
buf = (uint8_t) val;
|
2007-03-27 17:47:10 +00:00
|
|
|
pGFD->Write(&buf, 1);
|
|
|
|
buf = val >> 8;
|
|
|
|
return pGFD->Write(&buf, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a four-byte little-endian value.
|
|
|
|
*/
|
2014-11-21 21:18:20 +00:00
|
|
|
DIError DiskImgLib::WriteLongLE(GenericFD* pGFD, uint32_t val)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-24 20:56:19 +00:00
|
|
|
uint8_t buf;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-21 21:18:20 +00:00
|
|
|
buf = (uint8_t) val;
|
2007-03-27 17:47:10 +00:00
|
|
|
pGFD->Write(&buf, 1);
|
2014-11-21 21:18:20 +00:00
|
|
|
buf = (uint8_t) (val >> 8);
|
2007-03-27 17:47:10 +00:00
|
|
|
pGFD->Write(&buf, 1);
|
2014-11-21 21:18:20 +00:00
|
|
|
buf = (uint8_t) (val >> 16);
|
2007-03-27 17:47:10 +00:00
|
|
|
pGFD->Write(&buf, 1);
|
2014-11-21 21:18:20 +00:00
|
|
|
buf = (uint8_t) (val >> 24);
|
2007-03-27 17:47:10 +00:00
|
|
|
return pGFD->Write(&buf, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a two-byte big-endian value.
|
|
|
|
*/
|
2014-11-21 21:18:20 +00:00
|
|
|
DIError DiskImgLib::WriteShortBE(GenericFD* pGFD, uint16_t val)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-24 20:56:19 +00:00
|
|
|
uint8_t buf;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
buf = val >> 8;
|
|
|
|
pGFD->Write(&buf, 1);
|
2014-11-21 21:18:20 +00:00
|
|
|
buf = (uint8_t) val;
|
2007-03-27 17:47:10 +00:00
|
|
|
return pGFD->Write(&buf, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a four-byte big-endian value.
|
|
|
|
*/
|
2014-11-21 21:18:20 +00:00
|
|
|
DIError DiskImgLib::WriteLongBE(GenericFD* pGFD, uint32_t val)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-24 20:56:19 +00:00
|
|
|
uint8_t buf;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-24 20:56:19 +00:00
|
|
|
buf = (uint8_t) (val >> 24);
|
2007-03-27 17:47:10 +00:00
|
|
|
pGFD->Write(&buf, 1);
|
2014-11-24 20:56:19 +00:00
|
|
|
buf = (uint8_t) (val >> 16);
|
2007-03-27 17:47:10 +00:00
|
|
|
pGFD->Write(&buf, 1);
|
2014-11-24 20:56:19 +00:00
|
|
|
buf = (uint8_t) (val >> 8);
|
2007-03-27 17:47:10 +00:00
|
|
|
pGFD->Write(&buf, 1);
|
2014-11-24 20:56:19 +00:00
|
|
|
buf = (uint8_t) val;
|
2007-03-27 17:47:10 +00:00
|
|
|
return pGFD->Write(&buf, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the filename component of a local pathname. Uses the fssep passed
|
|
|
|
* in. If the fssep is '\0' (as is the case for DOS 3.3), then the entire
|
|
|
|
* pathname is returned.
|
|
|
|
*
|
2014-11-18 05:13:13 +00:00
|
|
|
* Always returns a pointer to a string; never returns NULL.
|
2007-03-27 17:47:10 +00:00
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
const char* DiskImgLib::FilenameOnly(const char* pathname, char fssep)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
const char* retstr;
|
|
|
|
const char* pSlash;
|
2014-11-18 05:13:13 +00:00
|
|
|
char* tmpStr = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
assert(pathname != NULL);
|
2014-11-04 00:26:53 +00:00
|
|
|
if (fssep == '\0') {
|
|
|
|
retstr = pathname;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
pSlash = strrchr(pathname, fssep);
|
2014-11-18 05:13:13 +00:00
|
|
|
if (pSlash == NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
retstr = pathname; /* whole thing is the filename */
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
pSlash++;
|
|
|
|
if (*pSlash == '\0') {
|
|
|
|
if (strlen(pathname) < 2) {
|
|
|
|
retstr = pathname; /* the pathname is just "/"? Whatever */
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* some bonehead put an fssep on the very end; back up before it */
|
|
|
|
/* (not efficient, but this should be rare, and I'm feeling lazy) */
|
|
|
|
tmpStr = strdup(pathname);
|
|
|
|
tmpStr[strlen(pathname)-1] = '\0';
|
|
|
|
pSlash = strrchr(tmpStr, fssep);
|
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
if (pSlash == NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
retstr = pathname; /* just a filename with a '/' after it */
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
pSlash++;
|
|
|
|
if (*pSlash == '\0') {
|
|
|
|
retstr = pathname; /* I give up! */
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
retstr = pathname + (pSlash - tmpStr);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
retstr = pSlash;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
bail:
|
2014-11-04 00:26:53 +00:00
|
|
|
free(tmpStr);
|
|
|
|
return retstr;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the filename extension found in a full pathname.
|
|
|
|
*
|
|
|
|
* An extension is the stuff following the last '.' in the filename. If
|
|
|
|
* there is nothing following the last '.', then there is no extension.
|
|
|
|
*
|
2014-11-18 05:13:13 +00:00
|
|
|
* Returns a pointer to the '.' preceding the extension, or NULL if no
|
2007-03-27 17:47:10 +00:00
|
|
|
* extension was found.
|
|
|
|
*
|
|
|
|
* We guarantee that there is at least one character after the '.'.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
const char* DiskImgLib::FindExtension(const char* pathname, char fssep)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
const char* pFilename;
|
|
|
|
const char* pExt;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have to isolate the filename so that we don't get excited
|
|
|
|
* about "/foo.bar/file".
|
|
|
|
*/
|
|
|
|
pFilename = FilenameOnly(pathname, fssep);
|
2014-11-18 05:13:13 +00:00
|
|
|
assert(pFilename != NULL);
|
2014-11-04 00:26:53 +00:00
|
|
|
pExt = strrchr(pFilename, kFilenameExtDelim);
|
|
|
|
|
|
|
|
/* also check for "/blah/foo.", which doesn't count */
|
2014-11-18 05:13:13 +00:00
|
|
|
if (pExt != NULL && *(pExt+1) != '\0')
|
2014-11-04 00:26:53 +00:00
|
|
|
return pExt;
|
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
return NULL;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like strcpy(), but allocate with new[] instead.
|
|
|
|
*
|
2014-11-18 05:13:13 +00:00
|
|
|
* If "str" is NULL, or "new" fails, this returns NULL.
|
2014-11-18 01:54:34 +00:00
|
|
|
*
|
|
|
|
* TODO: should be "StrdupNew()"
|
2007-03-27 17:47:10 +00:00
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
char* DiskImgLib::StrcpyNew(const char* str)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
char* newStr;
|
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
newStr = new char[strlen(str)+1];
|
2014-11-18 05:13:13 +00:00
|
|
|
if (newStr != NULL)
|
2014-11-04 00:26:53 +00:00
|
|
|
strcpy(newStr, str);
|
|
|
|
return newStr;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
/*
|
|
|
|
* Convert the value from GetLastError() to its DIError counterpart.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
DIError DiskImgLib::LastErrorToDIError(void)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DWORD lastErr = ::GetLastError();
|
|
|
|
|
|
|
|
switch (lastErr) {
|
|
|
|
case ERROR_FILE_NOT_FOUND: return kDIErrFileNotFound; // 2
|
|
|
|
case ERROR_ACCESS_DENIED: return kDIErrAccessDenied; // 5
|
|
|
|
case ERROR_WRITE_PROTECT: return kDIErrWriteProtected; // 19
|
|
|
|
case ERROR_SECTOR_NOT_FOUND: return kDIErrGeneric; // 27
|
|
|
|
case ERROR_SHARING_VIOLATION: return kDIErrSharingViolation; // 32
|
|
|
|
case ERROR_HANDLE_EOF: return kDIErrEOF; // 38
|
|
|
|
case ERROR_INVALID_PARAMETER: return kDIErrInvalidArg; // 87
|
|
|
|
case ERROR_SEM_TIMEOUT: return kDIErrGenericIO; // 121
|
|
|
|
// ERROR_SEM_TIMEOUT seen read bad blocks from floptical under Win2K
|
|
|
|
|
|
|
|
case ERROR_INVALID_HANDLE: // 6
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("HEY: got ERROR_INVALID_HANDLE!");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrInternal;
|
|
|
|
case ERROR_NEGATIVE_SEEK: // 131
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("HEY: got ERROR_NEGATIVE_SEEK!");
|
2014-11-04 00:26:53 +00:00
|
|
|
return kDIErrInternal;
|
|
|
|
default:
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("LastErrorToDIError: not converting 0x%08lx (%ld)",
|
2014-11-04 00:26:53 +00:00
|
|
|
lastErr, lastErr);
|
|
|
|
return kDIErrGeneric;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns "true" if we're running on Win9x (Win95, Win98, WinME), "false"
|
|
|
|
* if not (could be WinNT/2K/XP or even Win31 with Win32s).
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
bool DiskImgLib::IsWin9x(void)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2015-01-09 02:04:09 +00:00
|
|
|
return false;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
#endif
|