From 84706d7ea436740e047beab11a95b2c4a4302280 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Sun, 4 Jan 2015 11:29:51 -0800 Subject: [PATCH] Update NufxLib snapshot to v3.0.0d2 This integrates the latest NufxLib sources, and updates CiderPress to work with the API changes. --- app/AboutDialog.cpp | 2 +- app/DiskArchive.cpp | 2 +- app/GenericArchive.cpp | 2 +- app/NufxArchive.cpp | 45 +- diskimg/DiskImg.h | 2 +- diskimg/Global.cpp | 5 +- diskimg/ImageWrapper.cpp | 6 +- mdc/Main.cpp | 2 +- nufxlib/.gitignore | 1 + nufxlib/Archive.c | 320 +++++++-------- nufxlib/ArchiveIO.c | 232 +++++------ nufxlib/Bzip2.c | 69 ++-- nufxlib/ChangeLog.txt | 11 + nufxlib/Charset.c | 553 +++++++++++++++++++++++++ nufxlib/Compress.c | 67 ++- nufxlib/Crc16.c | 9 +- nufxlib/Debug.c | 95 ++--- nufxlib/Deferred.c | 433 ++++++++++---------- nufxlib/Deflate.c | 54 ++- nufxlib/Entry.c | 298 ++++++-------- nufxlib/Expand.c | 48 ++- nufxlib/FileIO.c | 637 +++++++++++++---------------- nufxlib/Funnel.c | 251 ++++++------ nufxlib/Lzc.c | 86 ++-- nufxlib/Lzw.c | 291 +++++++------ nufxlib/Makefile.in | 47 ++- nufxlib/Makefile.msc | 17 +- nufxlib/MiscStuff.c | 15 +- nufxlib/MiscStuff.h | 14 +- nufxlib/MiscUtils.c | 69 ++-- nufxlib/NufxLib.h | 358 +++++++++------- nufxlib/NufxLibPriv.h | 259 ++++++------ nufxlib/README.txt | 51 ++- nufxlib/Record.c | 702 +++++++++++++++----------------- nufxlib/SourceSink.c | 291 ++++++------- nufxlib/Squeeze.c | 99 ++--- nufxlib/SunOS4.h | 46 --- nufxlib/SysDefs.h | 28 +- nufxlib/Thread.c | 324 ++++++++------- nufxlib/Value.c | 51 ++- nufxlib/Version.c | 15 +- nufxlib/config.h.in | 12 - nufxlib/configure | 49 --- nufxlib/configure.in | 10 - nufxlib/nufxlib.def | 2 + nufxlib/nufxlib.vcxproj | 1 + nufxlib/nufxlib.vcxproj.filters | 3 + nufxlib/samples/Common.h | 8 +- nufxlib/samples/Exerciser.c | 425 +++++++++---------- nufxlib/samples/ImgConv.c | 159 ++++---- nufxlib/samples/Launder.c | 113 +++-- nufxlib/samples/Makefile.in | 56 ++- nufxlib/samples/TestBasic.c | 300 +++++++------- nufxlib/samples/TestExtract.c | 167 ++++---- nufxlib/samples/TestNames.c | 580 ++++++++++++++++++++++++++ nufxlib/samples/TestSimple.c | 34 +- nufxlib/samples/TestTwirl.c | 150 ++++--- 57 files changed, 4317 insertions(+), 3659 deletions(-) create mode 100644 nufxlib/Charset.c delete mode 100644 nufxlib/SunOS4.h create mode 100644 nufxlib/samples/TestNames.c diff --git a/app/AboutDialog.cpp b/app/AboutDialog.cpp index 969842b..0fd806f 100644 --- a/app/AboutDialog.cpp +++ b/app/AboutDialog.cpp @@ -32,7 +32,7 @@ static const WCHAR kVersionExtra[] = BOOL AboutDialog::OnInitDialog(void) { NuError nerr; - long major, minor, bug; + int32_t major, minor, bug; CString newVersion, tmpStr; CStatic* pStatic; //CString versionFmt; diff --git a/app/DiskArchive.cpp b/app/DiskArchive.cpp index d180d7c..7e435af 100644 --- a/app/DiskArchive.cpp +++ b/app/DiskArchive.cpp @@ -359,7 +359,7 @@ bool DiskEntry::GetFeatureFlag(Feature feature) const { CString result(""); DIError dierr; - long major, minor, bug; + int32_t major, minor, bug; LOGI("Initializing DiskImg library"); diff --git a/app/GenericArchive.cpp b/app/GenericArchive.cpp index 60dec50..ab64716 100644 --- a/app/GenericArchive.cpp +++ b/app/GenericArchive.cpp @@ -996,7 +996,7 @@ GenericArchive::FileDetails::operator const NuFileDetails() const fOrigNameA = origName; details.origName = fOrigNameA; fStorageNameA = storageName; - details.storageName = fStorageNameA; + details.storageNameMOR = fStorageNameA; //details.fileSysID = fileSysID; details.fileSysInfo = fileSysInfo; details.access = access; diff --git a/app/NufxArchive.cpp b/app/NufxArchive.cpp index 4e749f3..a00e9fa 100644 --- a/app/NufxArchive.cpp +++ b/app/NufxArchive.cpp @@ -404,7 +404,7 @@ void NufxEntry::AnalyzeRecord(const NuRecord* pRecord) { NuError nerr; CString result(""); - long major, minor, bug; + int32_t major, minor, bug; nerr = NuGetVersion(&major, &minor, &bug, NULL, NULL); if (nerr != kNuErrNone) { @@ -501,15 +501,15 @@ NuResult NufxArchive::NufxErrorMsgHandler(NuArchive*, void* vErrorMessage) oldName = newName = NULL; if (pProgress->operation == kNuOpAdd) { - oldName = pProgress->origPathname; - newName = pProgress->pathname; + oldName = pProgress->origPathnameUNI; + newName = pProgress->pathnameUNI; if (pThis->fProgressAsRecompress) oldName = "-"; } else if (pProgress->operation == kNuOpTest) { - oldName = pProgress->pathname; + oldName = pProgress->pathnameUNI; } else if (pProgress->operation == kNuOpExtract) { if (pThis->fProgressAsRecompress) { - oldName = pProgress->origPathname; + oldName = pProgress->origPathnameUNI; newName = "-"; } } @@ -796,7 +796,7 @@ NuResult NufxArchive::ContentFunc(NuArchive* pArchive, void* vpRecord) pNewEntry = new NufxEntry(pArchive); - CStringW filenameW(pRecord->filename); + CStringW filenameW(pRecord->filenameMOR); pNewEntry->SetPathName(filenameW); pNewEntry->SetFssep(NuGetSepFromSysInfo(pRecord->recFileSysInfo)); pNewEntry->SetFileType(pRecord->recFileType); @@ -942,7 +942,7 @@ bool NufxArchive::BulkAdd(ActionProgressDialog* pActionProgress, } /* actually do the work */ - long statusFlags; + uint32_t statusFlags; nerr = NuFlush(fpArchive, &statusFlags); if (nerr != kNuErrNone) { if (nerr != kNuErrAborted) { @@ -1055,7 +1055,7 @@ bool NufxArchive::AddDisk(ActionProgressDialog* pActionProgress, origNameA = fileName; // narrowing conversion storageNameA = pathProp.fStoredPathName; details.origName = origNameA; - details.storageName = storageNameA; + details.storageNameMOR = storageNameA; details.fileSysID = kNuFileSysUnknown; details.fileSysInfo = PathProposal::kDefaultStoredFssep; @@ -1125,7 +1125,7 @@ bool NufxArchive::AddDisk(ActionProgressDialog* pActionProgress, pSource = NULL; /* NufxLib owns it now */ /* actually do the work */ - long statusFlags; + uint32_t statusFlags; nerr = NuFlush(fpArchive, &statusFlags); if (nerr != kNuErrNone) { if (nerr != kNuErrAborted) { @@ -1298,7 +1298,7 @@ NuResult NufxArchive::HandleReplaceExisting(const NuErrorStatus* pErrorStatus) NuResult result = kNuOK; ASSERT(pErrorStatus != NULL); - ASSERT(pErrorStatus->pathname != NULL); + ASSERT(pErrorStatus->pathnameUNI != NULL); ASSERT(pErrorStatus->canOverwrite); ASSERT(pErrorStatus->canSkip); @@ -1307,13 +1307,14 @@ NuResult NufxArchive::HandleReplaceExisting(const NuErrorStatus* pErrorStatus) /* no firm policy, ask the user */ ConfirmOverwriteDialog confOvwr; - PathName path(pErrorStatus->pathname); + PathName path(pErrorStatus->pathnameUNI); - confOvwr.fExistingFile = pErrorStatus->pRecord->filename; + confOvwr.fExistingFile = pErrorStatus->pRecord->filenameMOR; confOvwr.fExistingFileModWhen = DateTimeToSeconds(&pErrorStatus->pRecord->recModWhen); if (pErrorStatus->origPathname != NULL) { - confOvwr.fNewFileSource = pErrorStatus->origPathname; + // TODO: use wchar_t instead for origPathname + confOvwr.fNewFileSource = (char*) pErrorStatus->origPathname; PathName checkPath(confOvwr.fNewFileSource); confOvwr.fNewFileModWhen = checkPath.GetModWhen(); } else { @@ -1354,7 +1355,7 @@ NuResult NufxArchive::HandleAddNotFound(const NuErrorStatus* pErrorStatus) CString errMsg; errMsg.Format(L"Failed while adding '%hs': file no longer exists.", - pErrorStatus->pathname); + pErrorStatus->pathnameUNI); ShowFailureMsg(fpMsgWnd, errMsg, IDS_FAILED); return kNuAbort; @@ -1450,7 +1451,7 @@ bool NufxArchive::DeleteSelection(CWnd* pMsgWnd, SelectionSet* pSelSet) } /* actually do the delete */ - long statusFlags; + uint32_t statusFlags; nerr = NuFlush(fpArchive, &statusFlags); if (nerr != kNuErrNone) { errMsg.Format(L"Unable to delete all files: %hs.", NuStrError(nerr)); @@ -1546,7 +1547,7 @@ bool NufxArchive::RenameSelection(CWnd* pMsgWnd, SelectionSet* pSelSet) { CWaitCursor waitc; - long statusFlags; + uint32_t statusFlags; nerr = NuFlush(fpArchive, &statusFlags); if (nerr != kNuErrNone) { errMsg.Format(L"Unable to rename all files: %hs.", @@ -1693,7 +1694,7 @@ bool NufxArchive::RecompressSelection(CWnd* pMsgWnd, SelectionSet* pSelSet, /* if we're sitting on too much, push it out */ if (sizeInMemory > kMaxSizeInMemory) { /* flush anything pending */ - long statusFlags; + uint32_t statusFlags; nerr = NuFlush(fpArchive, &statusFlags); if (nerr != kNuErrNone) { if (nerr != kNuErrAborted) { @@ -1727,7 +1728,7 @@ bool NufxArchive::RecompressSelection(CWnd* pMsgWnd, SelectionSet* pSelSet, /* flush anything pending */ - long statusFlags; + uint32_t statusFlags; nerr = NuFlush(fpArchive, &statusFlags); if (nerr != kNuErrNone) { if (nerr != kNuErrAborted) { @@ -2185,7 +2186,7 @@ void NufxArchive::XferFinish(CWnd* pMsgWnd) LOGI(" NufxArchive::XferFinish"); /* actually do the work */ - long statusFlags; + uint32_t statusFlags; nerr = NuFlush(fpArchive, &statusFlags); if (nerr != kNuErrNone) { if (nerr != kNuErrAborted) { @@ -2345,7 +2346,7 @@ bool NufxArchive::SetComment(CWnd* pMsgWnd, GenericEntry* pGenericEntry, pSource = NULL; // nufxlib owns it now /* flush changes */ - long statusFlags; + uint32_t statusFlags; nerr = NuFlush(fpArchive, &statusFlags); if (nerr != kNuErrNone) { errMsg.Format(L"Unable to flush comment changes: %hs.", @@ -2387,7 +2388,7 @@ bool NufxArchive::DeleteComment(CWnd* pMsgWnd, GenericEntry* pGenericEntry) } /* flush changes */ - long statusFlags; + uint32_t statusFlags; nerr = NuFlush(fpArchive, &statusFlags); if (nerr != kNuErrNone) { errMsg.Format(L"Unable to flush comment deletion: %hs.", @@ -2447,7 +2448,7 @@ bool NufxArchive::SetProps(CWnd* pMsgWnd, GenericEntry* pEntry, return false; } - long statusFlags; + uint32_t statusFlags; nerr = NuFlush(fpArchive, &statusFlags); if (nerr != kNuErrNone) { LOGI("ERROR: NuFlush failed: %hs", NuStrError(nerr)); diff --git a/diskimg/DiskImg.h b/diskimg/DiskImg.h index 75ca78e..398a74e 100644 --- a/diskimg/DiskImg.h +++ b/diskimg/DiskImg.h @@ -198,7 +198,7 @@ public: static DIError AppCleanup(void); // return the DiskImg version number - static void GetVersion(long* pMajor, long* pMinor, long* pBug); + static void GetVersion(int32_t* pMajor, int32_t* pMinor, int32_t* pBug); static bool GetAppInitCalled(void) { return fAppInitCalled; } static bool GetHasSPTI(void); diff --git a/diskimg/Global.cpp b/diskimg/Global.cpp index 0fcd9a3..101d892 100644 --- a/diskimg/Global.cpp +++ b/diskimg/Global.cpp @@ -24,7 +24,7 @@ const char* DiskImgLib::kASPIDev = "ASPI:"; /*static*/ DIError Global::AppInit(void) { NuError nerr; - long major, minor, bug; + int32_t major, minor, bug; if (fAppInitCalled) { LOGW("DiskImg AppInit already called"); @@ -123,7 +123,8 @@ const char* DiskImgLib::kASPIDev = "ASPI:"; /* * Return current library versions. */ -/*static*/ void Global::GetVersion(long* pMajor, long* pMinor, long* pBug) +/*static*/ void Global::GetVersion(int32_t* pMajor, int32_t* pMinor, + int32_t* pBug) { if (pMajor != NULL) *pMajor = kDiskImgVersionMajor; diff --git a/diskimg/ImageWrapper.cpp b/diskimg/ImageWrapper.cpp index e61be7c..89853a5 100644 --- a/diskimg/ImageWrapper.cpp +++ b/diskimg/ImageWrapper.cpp @@ -683,9 +683,9 @@ DIError WrapperNuFX::Flush(GenericFD* pWrapperGFD, GenericFD* pDataGFD, memset(&fileDetails, 0, sizeof(fileDetails)); fileDetails.threadID = kNuThreadIDDiskImage; if (fStorageName != NULL) - fileDetails.storageName = fStorageName; + fileDetails.storageNameMOR = fStorageName; // TODO else - fileDetails.storageName = "NEW.DISK"; + fileDetails.storageNameMOR = "NEW.DISK"; fileDetails.fileSysID = kNuFileSysUnknown; fileDetails.fileSysInfo = kDefaultStorageFssep; fileDetails.storageType = 512; @@ -738,7 +738,7 @@ DIError WrapperNuFX::Flush(GenericFD* pWrapperGFD, GenericFD* pDataGFD, /* * Flush changes (does the actual compression). */ - long status; + uint32_t status; nerr = NuFlush(fpArchive, &status); if (nerr != kNuErrNone) { LOGI(" NuFX flush failed (nerr=%d, status=%ld)", nerr, status); diff --git a/mdc/Main.cpp b/mdc/Main.cpp index b200122..797457a 100644 --- a/mdc/Main.cpp +++ b/mdc/Main.cpp @@ -195,7 +195,7 @@ void MainWindow::ScanFiles(void) goto bail; } - long major, minor, bug; + int32_t major, minor, bug; DiskImgLib::Global::GetVersion(&major, &minor, &bug); fprintf(scanOpts.outfp, "MDC for Windows v%d.%d.%d (DiskImg library v%ld.%ld.%ld)\n", kAppMajorVersion, kAppMinorVersion, kAppBugVersion, diff --git a/nufxlib/.gitignore b/nufxlib/.gitignore index 78ae93c..0f68fbd 100644 --- a/nufxlib/.gitignore +++ b/nufxlib/.gitignore @@ -11,5 +11,6 @@ samples/imgconv samples/launder samples/test-basic samples/test-extract +samples/test-names samples/test-simple samples/test-twirl diff --git a/nufxlib/Archive.c b/nufxlib/Archive.c index 4dd544c..7c97cb3 100644 --- a/nufxlib/Archive.c +++ b/nufxlib/Archive.c @@ -16,13 +16,13 @@ #endif /* master header identification */ -static const uchar kNuMasterID[kNufileIDLen] = +static const uint8_t kNuMasterID[kNufileIDLen] = { 0x4e, 0xf5, 0x46, 0xe9, 0x6c, 0xe5 }; /* other identification; can be no longer than kNufileIDLen */ -static const uchar kNuBinary2ID[] = +static const uint8_t kNuBinary2ID[] = { 0x0a, 0x47, 0x4c }; -static const uchar kNuSHKSEAID[] = +static const uint8_t kNuSHKSEAID[] = { 0xa2, 0x2e, 0x00 }; /* @@ -53,19 +53,16 @@ static void Nu_CloseAndFree(NuArchive* pArchive); /* * Allocate and initialize a new NuArchive structure. */ -static NuError -Nu_NuArchiveNew(NuArchive** ppArchive) +static NuError Nu_NuArchiveNew(NuArchive** ppArchive) { - Assert(ppArchive != nil); + Assert(ppArchive != NULL); /* validate some assumptions we make throughout the code */ Assert(sizeof(int) >= 2); - Assert(sizeof(ushort) >= 2); - Assert(sizeof(ulong) >= 4); Assert(sizeof(void*) >= sizeof(NuArchive*)); - *ppArchive = Nu_Calloc(nil, sizeof(**ppArchive)); - if (*ppArchive == nil) + *ppArchive = Nu_Calloc(NULL, sizeof(**ppArchive)); + if (*ppArchive == NULL) return kNuErrMalloc; (*ppArchive)->structMagic = kNuArchiveStructMagic; @@ -107,10 +104,9 @@ Nu_NuArchiveNew(NuArchive** ppArchive) /* * Free up a NuArchive structure and its contents. */ -static NuError -Nu_NuArchiveFree(NuArchive* pArchive) +static NuError Nu_NuArchiveFree(NuArchive* pArchive) { - Assert(pArchive != nil); + Assert(pArchive != NULL); Assert(pArchive->structMagic == kNuArchiveStructMagic); (void) Nu_RecordSet_FreeAllRecords(pArchive, &pArchive->origRecordSet); @@ -118,15 +114,15 @@ Nu_NuArchiveFree(NuArchive* pArchive) (void) Nu_RecordSet_FreeAllRecords(pArchive, &pArchive->copyRecordSet); (void) Nu_RecordSet_FreeAllRecords(pArchive, &pArchive->newRecordSet); - Nu_Free(nil, pArchive->archivePathname); - Nu_Free(nil, pArchive->tmpPathname); - Nu_Free(nil, pArchive->compBuf); - Nu_Free(nil, pArchive->lzwCompressState); - Nu_Free(nil, pArchive->lzwExpandState); + Nu_Free(NULL, pArchive->archivePathnameUNI); + Nu_Free(NULL, pArchive->tmpPathnameUNI); + Nu_Free(NULL, pArchive->compBuf); + Nu_Free(NULL, pArchive->lzwCompressState); + Nu_Free(NULL, pArchive->lzwExpandState); /* mark it as deceased to prevent further use, then free it */ pArchive->structMagic = kNuArchiveStructMagic ^ 0xffffffff; - Nu_Free(nil, pArchive); + Nu_Free(NULL, pArchive); return kNuErrNone; } @@ -135,13 +131,12 @@ Nu_NuArchiveFree(NuArchive* pArchive) /* * Copy a NuMasterHeader struct. */ -void -Nu_MasterHeaderCopy(NuArchive* pArchive, NuMasterHeader* pDstHeader, +void Nu_MasterHeaderCopy(NuArchive* pArchive, NuMasterHeader* pDstHeader, const NuMasterHeader* pSrcHeader) { - Assert(pArchive != nil); - Assert(pDstHeader != nil); - Assert(pSrcHeader != nil); + Assert(pArchive != NULL); + Assert(pDstHeader != NULL); + Assert(pSrcHeader != NULL); *pDstHeader = *pSrcHeader; } @@ -149,10 +144,10 @@ Nu_MasterHeaderCopy(NuArchive* pArchive, NuMasterHeader* pDstHeader, /* * Get a pointer to the archive master header (this is an API call). */ -NuError -Nu_GetMasterHeader(NuArchive* pArchive, const NuMasterHeader** ppMasterHeader) +NuError Nu_GetMasterHeader(NuArchive* pArchive, + const NuMasterHeader** ppMasterHeader) { - if (ppMasterHeader == nil) + if (ppMasterHeader == NULL) return kNuErrInvalidArg; *ppMasterHeader = &pArchive->masterHeader; @@ -164,16 +159,15 @@ Nu_GetMasterHeader(NuArchive* pArchive, const NuMasterHeader** ppMasterHeader) /* * Allocate the general-purpose compression buffer, if needed. */ -NuError -Nu_AllocCompressionBufferIFN(NuArchive* pArchive) +NuError Nu_AllocCompressionBufferIFN(NuArchive* pArchive) { - Assert(pArchive != nil); + Assert(pArchive != NULL); - if (pArchive->compBuf != nil) + if (pArchive->compBuf != NULL) return kNuErrNone; pArchive->compBuf = Nu_Malloc(pArchive, kNuGenCompBufSize); - if (pArchive->compBuf == nil) + if (pArchive->compBuf == NULL) return kNuErrMalloc; return kNuErrNone; @@ -183,8 +177,7 @@ Nu_AllocCompressionBufferIFN(NuArchive* pArchive) /* * Return a unique value. */ -NuRecordIdx -Nu_GetNextRecordIdx(NuArchive* pArchive) +NuRecordIdx Nu_GetNextRecordIdx(NuArchive* pArchive) { return pArchive->nextRecordIdx++; } @@ -192,8 +185,7 @@ Nu_GetNextRecordIdx(NuArchive* pArchive) /* * Return a unique value. */ -NuThreadIdx -Nu_GetNextThreadIdx(NuArchive* pArchive) +NuThreadIdx Nu_GetNextThreadIdx(NuArchive* pArchive) { return pArchive->nextRecordIdx++; /* just use the record counter */ } @@ -208,8 +200,7 @@ Nu_GetNextThreadIdx(NuArchive* pArchive) /* * Copy the wrapper from the archive file to the temp file. */ -NuError -Nu_CopyWrapperToTemp(NuArchive* pArchive) +NuError Nu_CopyWrapperToTemp(NuArchive* pArchive) { NuError err; @@ -247,13 +238,12 @@ bail: * guess some of the SEA weirdness stems from some far-sighted support * for multiple archives within a single SEA wrapper. */ -NuError -Nu_UpdateWrapper(NuArchive* pArchive, FILE* fp) +NuError Nu_UpdateWrapper(NuArchive* pArchive, FILE* fp) { NuError err = kNuErrNone; Boolean hasBinary2, hasSea; - uchar identBuf[kNufileIDLen]; - ulong archiveLen, archiveLen512; + uint8_t identBuf[kNufileIDLen]; + uint32_t archiveLen, archiveLen512; Assert(pArchive->newMasterHeader.isValid); /* need new crc and len */ @@ -306,20 +296,20 @@ Nu_UpdateWrapper(NuArchive* pArchive, FILE* fp) err = Nu_FSeek(fp, kNuBNYFileSizeLo - kNufileIDLen, SEEK_CUR); BailError(err); - Nu_WriteTwo(pArchive, fp, (ushort)(archiveLen512 & 0xffff)); + Nu_WriteTwo(pArchive, fp, (uint16_t)(archiveLen512 & 0xffff)); err = Nu_FSeek(fp, kNuBNYFileSizeHi - (kNuBNYFileSizeLo+2), SEEK_CUR); BailError(err); - Nu_WriteTwo(pArchive, fp, (ushort)(archiveLen512 >> 16)); + Nu_WriteTwo(pArchive, fp, (uint16_t)(archiveLen512 >> 16)); err = Nu_FSeek(fp, kNuBNYEOFLo - (kNuBNYFileSizeHi+2), SEEK_CUR); BailError(err); - Nu_WriteTwo(pArchive, fp, (ushort)(archiveLen & 0xffff)); - Nu_WriteOne(pArchive, fp, (uchar)((archiveLen >> 16) & 0xff)); + Nu_WriteTwo(pArchive, fp, (uint16_t)(archiveLen & 0xffff)); + Nu_WriteOne(pArchive, fp, (uint8_t)((archiveLen >> 16) & 0xff)); err = Nu_FSeek(fp, kNuBNYEOFHi - (kNuBNYEOFLo+3), SEEK_CUR); BailError(err); - Nu_WriteOne(pArchive, fp, (uchar)(archiveLen >> 24)); + Nu_WriteOne(pArchive, fp, (uint8_t)(archiveLen >> 24)); err = Nu_FSeek(fp, kNuBNYDiskSpace - (kNuBNYEOFHi+1), SEEK_CUR); BailError(err); @@ -358,11 +348,11 @@ Nu_UpdateWrapper(NuArchive* pArchive, FILE* fp) err = Nu_FSeek(fp, kNuSEALength1 - (kNuSEAFunkySize+4), SEEK_CUR); BailError(err); - Nu_WriteTwo(pArchive, fp, (ushort)archiveLen); + Nu_WriteTwo(pArchive, fp, (uint16_t)archiveLen); err = Nu_FSeek(fp, kNuSEALength2 - (kNuSEALength1+2), SEEK_CUR); BailError(err); - Nu_WriteTwo(pArchive, fp, (ushort)archiveLen); + Nu_WriteTwo(pArchive, fp, (uint16_t)archiveLen); /* seek past end of SEA wrapper */ err = Nu_FSeek(fp, kNuSEAOffset - (kNuSEALength2+2), SEEK_CUR); @@ -399,8 +389,7 @@ bail: * require additional disk space to be used, assuming a filesystem block * size of at least 128 bytes. */ -NuError -Nu_AdjustWrapperPadding(NuArchive* pArchive, FILE* fp) +NuError Nu_AdjustWrapperPadding(NuArchive* pArchive, FILE* fp) { NuError err = kNuErrNone; Boolean hasBinary2, hasSea; @@ -495,17 +484,16 @@ bail: * * On exit, the stream will be positioned just past the master header. */ -static NuError -Nu_ReadMasterHeader(NuArchive* pArchive) +static NuError Nu_ReadMasterHeader(NuArchive* pArchive) { NuError err; - ushort crc; + uint16_t crc; FILE* fp; NuMasterHeader* pHeader; Boolean isBinary2 = false; Boolean isSea = false; - Assert(pArchive != nil); + Assert(pArchive != NULL); fp = pArchive->archiveFp; /* saves typing */ pHeader = &pArchive->masterHeader; @@ -640,7 +628,7 @@ retry: /* compare the CRC */ if (!pArchive->valIgnoreCRC && crc != pHeader->mhMasterCRC) { - if (!Nu_ShouldIgnoreBadCRC(pArchive, nil, kNuErrBadMHCRC)) { + if (!Nu_ShouldIgnoreBadCRC(pArchive, NULL, kNuErrBadMHCRC)) { err = kNuErrBadMHCRC; Nu_ReportError(NU_BLOB, err, "Stored MH CRC=0x%04x, calc=0x%04x", pHeader->mhMasterCRC, crc); @@ -669,7 +657,7 @@ retry: if (pHeader->mhMasterEOF == kNuMasterHeaderSize) { err = kNuErrNoRecords; Nu_ReportError(NU_BLOB, err, - "Master EOF is %ld, archive is probably truncated", + "Master EOF is %u, archive is probably truncated", pHeader->mhMasterEOF); goto bail; } @@ -712,12 +700,11 @@ bail: * Prepare the NuArchive and NuMasterHeader structures for use with a * newly-created archive. */ -static void -Nu_InitNewArchive(NuArchive* pArchive) +static void Nu_InitNewArchive(NuArchive* pArchive) { NuMasterHeader* pHeader; - Assert(pArchive != nil); + Assert(pArchive != NULL); pHeader = &pArchive->masterHeader; @@ -741,14 +728,13 @@ Nu_InitNewArchive(NuArchive* pArchive) /* * Open an archive in streaming read-only mode. */ -NuError -Nu_StreamOpenRO(FILE* infp, NuArchive** ppArchive) +NuError Nu_StreamOpenRO(FILE* infp, NuArchive** ppArchive) { NuError err; - NuArchive* pArchive = nil; + NuArchive* pArchive = NULL; - Assert(infp != nil); - Assert(ppArchive != nil); + Assert(infp != NULL); + Assert(ppArchive != NULL); err = Nu_NuArchiveNew(ppArchive); if (err != kNuErrNone) @@ -757,16 +743,16 @@ Nu_StreamOpenRO(FILE* infp, NuArchive** ppArchive) pArchive->openMode = kNuOpenStreamingRO; pArchive->archiveFp = infp; - pArchive->archivePathname = strdup("(stream)"); + pArchive->archivePathnameUNI = strdup("(stream)"); err = Nu_ReadMasterHeader(pArchive); BailError(err); bail: if (err != kNuErrNone) { - if (pArchive != nil) + if (pArchive != NULL) (void) Nu_NuArchiveFree(pArchive); - *ppArchive = nil; + *ppArchive = NULL; } return err; } @@ -775,21 +761,24 @@ bail: /* * Open an archive in non-streaming read-only mode. */ -NuError -Nu_OpenRO(const char* archivePathname, NuArchive** ppArchive) +NuError Nu_OpenRO(const UNICHAR* archivePathnameUNI, NuArchive** ppArchive) { NuError err; - NuArchive* pArchive = nil; - FILE* fp = nil; + NuArchive* pArchive = NULL; + FILE* fp = NULL; - if (archivePathname == nil || !strlen(archivePathname) || ppArchive == nil) + if (archivePathnameUNI == NULL || !strlen(archivePathnameUNI) || + ppArchive == NULL) + { return kNuErrInvalidArg; + } - *ppArchive = nil; + *ppArchive = NULL; - fp = fopen(archivePathname, kNuFileOpenReadOnly); - if (fp == nil) { - Nu_ReportError(NU_BLOB, errno, "Unable to open '%s'", archivePathname); + fp = fopen(archivePathnameUNI, kNuFileOpenReadOnly); + if (fp == NULL) { + Nu_ReportError(NU_BLOB, errno, "Unable to open '%s'", + archivePathnameUNI); err = kNuErrFileOpen; goto bail; } @@ -801,19 +790,19 @@ Nu_OpenRO(const char* archivePathname, NuArchive** ppArchive) pArchive->openMode = kNuOpenRO; pArchive->archiveFp = fp; - fp = nil; - pArchive->archivePathname = strdup(archivePathname); + fp = NULL; + pArchive->archivePathnameUNI = strdup(archivePathnameUNI); err = Nu_ReadMasterHeader(pArchive); BailError(err); bail: if (err != kNuErrNone) { - if (pArchive != nil) { + if (pArchive != NULL) { (void) Nu_CloseAndFree(pArchive); - *ppArchive = nil; + *ppArchive = NULL; } - if (fp != nil) + if (fp != NULL) fclose(fp); } return err; @@ -822,15 +811,15 @@ bail: /* * Open a temp file. If "fileName" contains six Xs ("XXXXXX"), it will - * be treated as a mktemp-style template, and modified before use. + * be treated as a mktemp-style template, and modified before use (so + * pass a copy of the string in). * * Thought for the day: consider using Win32 SetFileAttributes() to make * temp files hidden. We will need to un-hide it before rolling it over. */ -static NuError -Nu_OpenTempFile(char* fileName, FILE** pFp) +static NuError Nu_OpenTempFile(UNICHAR* fileNameUNI, FILE** pFp) { - NuArchive* pArchive = nil; /* dummy for NU_BLOB */ + NuArchive* pArchive = NULL; /* dummy for NU_BLOB */ NuError err = kNuErrNone; int len; @@ -843,25 +832,25 @@ Nu_OpenTempFile(char* fileName, FILE** pFp) * to complain about mktemp, since it's generally a bad way to do * things. */ - len = strlen(fileName); - if (len > 6 && strcmp(fileName + len - 6, "XXXXXX") == 0) { + len = strlen(fileNameUNI); + if (len > 6 && strcmp(fileNameUNI + len - 6, "XXXXXX") == 0) { #if defined(HAVE_MKSTEMP) && defined(HAVE_FDOPEN) int fd; DBUG(("+++ Using mkstemp\n")); /* this modifies the template *and* opens the file */ - fd = mkstemp(fileName); + fd = mkstemp(fileNameUNI); if (fd < 0) { err = errno ? errno : kNuErrFileOpen; Nu_ReportError(NU_BLOB, kNuErrNone, "mkstemp failed on '%s'", - fileName); + fileNameUNI); goto bail; } - DBUG(("--- Fd-opening temp file '%s'\n", fileName)); + DBUG(("--- Fd-opening temp file '%s'\n", fileNameUNI)); *pFp = fdopen(fd, kNuFileOpenReadWriteCreat); - if (*pFp == nil) { + if (*pFp == NULL) { close(fd); err = errno ? errno : kNuErrFileOpen; goto bail; @@ -874,10 +863,10 @@ Nu_OpenTempFile(char* fileName, FILE** pFp) char* result; DBUG(("+++ Using mktemp\n")); - result = mktemp(fileName); - if (result == nil) { + result = mktemp(fileNameUNI); + if (result == NULL) { Nu_ReportError(NU_BLOB, kNuErrNone, "mktemp failed on '%s'", - fileName); + fileNameUNI); err = kNuErrInternal; goto bail; } @@ -886,34 +875,33 @@ Nu_OpenTempFile(char* fileName, FILE** pFp) #endif } - DBUG(("--- Opening temp file '%s'\n", fileName)); + DBUG(("--- Opening temp file '%s'\n", fileNameUNI)); #if defined(HAVE_FDOPEN) { int fd; - fd = open(fileName, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600); + fd = open(fileNameUNI, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600); if (fd < 0) { err = errno ? errno : kNuErrFileOpen; goto bail; } *pFp = fdopen(fd, kNuFileOpenReadWriteCreat); - if (*pFp == nil) { + if (*pFp == NULL) { close(fd); err = errno ? errno : kNuErrFileOpen; goto bail; } } #else - /* (not sure how portable "access" is... I think it's POSIX) */ - if (access(fileName, F_OK) == 0) { + if (access(fileNameUNI, F_OK) == 0) { err = kNuErrFileExists; goto bail; } - *pFp = fopen(fileName, kNuFileOpenReadWriteCreat); - if (*pFp == nil) { + *pFp = fopen(fileNameUNI, kNuFileOpenReadWriteCreat); + if (*pFp == NULL) { err = errno ? errno : kNuErrFileOpen; goto bail; } @@ -928,26 +916,25 @@ bail: * Open an archive in read-write mode, optionally creating it if it doesn't * exist. */ -NuError -Nu_OpenRW(const char* archivePathname, const char* tmpPathname, ulong flags, - NuArchive** ppArchive) +NuError Nu_OpenRW(const UNICHAR* archivePathnameUNI, + const UNICHAR* tmpPathnameUNI, uint32_t flags, NuArchive** ppArchive) { NuError err; - FILE* fp = nil; - FILE* tmpFp = nil; - NuArchive* pArchive = nil; - char* tmpPathDup = nil; + FILE* fp = NULL; + FILE* tmpFp = NULL; + NuArchive* pArchive = NULL; + char* tmpPathDup = NULL; Boolean archiveExists; Boolean newlyCreated; - if (archivePathname == nil || !strlen(archivePathname) || - tmpPathname == nil || !strlen(tmpPathname) || ppArchive == nil || - (flags & ~(kNuOpenCreat|kNuOpenExcl)) != 0) + if (archivePathnameUNI == NULL || !strlen(archivePathnameUNI) || + tmpPathnameUNI == NULL || !strlen(tmpPathnameUNI) || + ppArchive == NULL || (flags & ~(kNuOpenCreat|kNuOpenExcl)) != 0) { return kNuErrInvalidArg; } - archiveExists = (access(archivePathname, F_OK) == 0); + archiveExists = (access(archivePathnameUNI, F_OK) == 0); /* * Open or create archive file. @@ -955,27 +942,30 @@ Nu_OpenRW(const char* archivePathname, const char* tmpPathname, ulong flags, if (archiveExists) { if ((flags & kNuOpenCreat) && (flags & kNuOpenExcl)) { err = kNuErrFileExists; - Nu_ReportError(NU_BLOB, err, "File '%s' exists", archivePathname); + Nu_ReportError(NU_BLOB, err, "File '%s' exists", + archivePathnameUNI); goto bail; } - fp = fopen(archivePathname, kNuFileOpenReadWrite); + fp = fopen(archivePathnameUNI, kNuFileOpenReadWrite); newlyCreated = false; } else { if (!(flags & kNuOpenCreat)) { err = kNuErrFileNotFound; - Nu_ReportError(NU_BLOB, err, "File '%s' not found",archivePathname); + Nu_ReportError(NU_BLOB, err, "File '%s' not found", + archivePathnameUNI); goto bail; } - fp = fopen(archivePathname, kNuFileOpenReadWriteCreat); + fp = fopen(archivePathnameUNI, kNuFileOpenReadWriteCreat); newlyCreated = true; } - if (fp == nil) { + if (fp == NULL) { if (errno == EACCES) err = kNuErrFileAccessDenied; else err = kNuErrFileOpen; - Nu_ReportError(NU_BLOB, errno, "Unable to open '%s'", archivePathname); + Nu_ReportError(NU_BLOB, errno, "Unable to open '%s'", + archivePathnameUNI); goto bail; } @@ -985,7 +975,7 @@ Nu_OpenRW(const char* archivePathname, const char* tmpPathname, ulong flags, if (archiveExists && !newlyCreated) { long length; - err = Nu_GetFileLength(nil, fp, &length); + err = Nu_GetFileLength(NULL, fp, &length); BailError(err); if (!length) { @@ -1003,12 +993,12 @@ Nu_OpenRW(const char* archivePathname, const char* tmpPathname, ulong flags, * So, create a temp file whether we think we need one or not. Won't * do any harm, and might save us some troubles later. */ - tmpPathDup = strdup(tmpPathname); + tmpPathDup = strdup(tmpPathnameUNI); BailNil(tmpPathDup); err = Nu_OpenTempFile(tmpPathDup, &tmpFp); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, "Failed opening temp file '%s'", - tmpPathname); + tmpPathnameUNI); goto bail; } @@ -1019,13 +1009,13 @@ Nu_OpenRW(const char* archivePathname, const char* tmpPathname, ulong flags, pArchive->openMode = kNuOpenRW; pArchive->newlyCreated = newlyCreated; - pArchive->archivePathname = strdup(archivePathname); + pArchive->archivePathnameUNI = strdup(archivePathnameUNI); pArchive->archiveFp = fp; - fp = nil; + fp = NULL; pArchive->tmpFp = tmpFp; - tmpFp = nil; - pArchive->tmpPathname = tmpPathDup; - tmpPathDup = nil; + tmpFp = NULL; + pArchive->tmpPathnameUNI = tmpPathDup; + tmpPathDup = NULL; if (archiveExists && !newlyCreated) { err = Nu_ReadMasterHeader(pArchive); @@ -1036,15 +1026,15 @@ Nu_OpenRW(const char* archivePathname, const char* tmpPathname, ulong flags, bail: if (err != kNuErrNone) { - if (pArchive != nil) { + if (pArchive != NULL) { (void) Nu_CloseAndFree(pArchive); - *ppArchive = nil; + *ppArchive = NULL; } - if (fp != nil) + if (fp != NULL) fclose(fp); - if (tmpFp != nil) + if (tmpFp != NULL) fclose(tmpFp); - if (tmpPathDup != nil) + if (tmpPathDup != NULL) Nu_Free(pArchive, tmpPathDup); } return err; @@ -1060,17 +1050,16 @@ bail: /* * Write the NuFX master header at the current offset. */ -NuError -Nu_WriteMasterHeader(NuArchive* pArchive, FILE* fp, +NuError Nu_WriteMasterHeader(NuArchive* pArchive, FILE* fp, NuMasterHeader* pHeader) { NuError err; long crcOffset; - ushort crc; + uint16_t crc; - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pHeader != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pHeader != NULL); Assert(pHeader->isValid); Assert(pHeader->mhMasterVersion == kNuOurMHVersion); @@ -1122,23 +1111,22 @@ bail: * If it's a brand-new archive, and we didn't add anything to it, then we * want to remove the stub archive file. */ -static void -Nu_CloseAndFree(NuArchive* pArchive) +static void Nu_CloseAndFree(NuArchive* pArchive) { - if (pArchive->archiveFp != nil) { + if (pArchive->archiveFp != NULL) { DBUG(("--- Closing archive\n")); fclose(pArchive->archiveFp); - pArchive->archiveFp = nil; + pArchive->archiveFp = NULL; } - if (pArchive->tmpFp != nil) { + if (pArchive->tmpFp != NULL) { DBUG(("--- Closing and removing temp file\n")); fclose(pArchive->tmpFp); - pArchive->tmpFp = nil; - Assert(pArchive->tmpPathname != nil); - if (remove(pArchive->tmpPathname) != 0) { + pArchive->tmpFp = NULL; + Assert(pArchive->tmpPathnameUNI != NULL); + if (remove(pArchive->tmpPathnameUNI) != 0) { Nu_ReportError(NU_BLOB, errno, "Unable to remove temp file '%s'", - pArchive->tmpPathname); + pArchive->tmpPathnameUNI); /* keep going */ } } @@ -1146,9 +1134,9 @@ Nu_CloseAndFree(NuArchive* pArchive) if (pArchive->newlyCreated && Nu_RecordSet_IsEmpty(&pArchive->origRecordSet)) { DBUG(("--- Newly-created archive unmodified; removing it\n")); - if (remove(pArchive->archivePathname) != 0) { + if (remove(pArchive->archivePathnameUNI) != 0) { Nu_ReportError(NU_BLOB, errno, "Unable to remove archive file '%s'", - pArchive->archivePathname); + pArchive->archivePathnameUNI); } } @@ -1158,13 +1146,12 @@ Nu_CloseAndFree(NuArchive* pArchive) /* * Flush pending changes to the archive, then close it. */ -NuError -Nu_Close(NuArchive* pArchive) +NuError Nu_Close(NuArchive* pArchive) { NuError err = kNuErrNone; - long flushStatus; + uint32_t flushStatus; - Assert(pArchive != nil); + Assert(pArchive != NULL); if (!Nu_IsReadOnly(pArchive)) err = Nu_Flush(pArchive, &flushStatus); @@ -1190,29 +1177,28 @@ Nu_Close(NuArchive* pArchive) /* * Delete the archive file, which should already have been closed. */ -NuError -Nu_DeleteArchiveFile(NuArchive* pArchive) +NuError Nu_DeleteArchiveFile(NuArchive* pArchive) { - Assert(pArchive != nil); - Assert(pArchive->archiveFp == nil); - Assert(pArchive->archivePathname != nil); + Assert(pArchive != NULL); + Assert(pArchive->archiveFp == NULL); + Assert(pArchive->archivePathnameUNI != NULL); - return Nu_DeleteFile(pArchive->archivePathname); + return Nu_DeleteFile(pArchive->archivePathnameUNI); } /* * Rename the temp file on top of the original archive. The temp file * should be closed, and the archive file should be deleted. */ -NuError -Nu_RenameTempToArchive(NuArchive* pArchive) +NuError Nu_RenameTempToArchive(NuArchive* pArchive) { - Assert(pArchive != nil); - Assert(pArchive->archiveFp == nil); - Assert(pArchive->tmpFp == nil); - Assert(pArchive->archivePathname != nil); - Assert(pArchive->tmpPathname != nil); + Assert(pArchive != NULL); + Assert(pArchive->archiveFp == NULL); + Assert(pArchive->tmpFp == NULL); + Assert(pArchive->archivePathnameUNI != NULL); + Assert(pArchive->tmpPathnameUNI != NULL); - return Nu_RenameFile(pArchive->tmpPathname, pArchive->archivePathname); + return Nu_RenameFile(pArchive->tmpPathnameUNI, + pArchive->archivePathnameUNI); } diff --git a/nufxlib/ArchiveIO.c b/nufxlib/ArchiveIO.c index 5f71486..893584b 100644 --- a/nufxlib/ArchiveIO.c +++ b/nufxlib/ArchiveIO.c @@ -26,45 +26,41 @@ /* * Read one byte, optionally computing a CRC. */ -uchar -Nu_ReadOneC(NuArchive* pArchive, FILE* fp, ushort* pCrc) +uint8_t Nu_ReadOneC(NuArchive* pArchive, FILE* fp, uint16_t* pCrc) { int ic; - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); ic = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); - return (uchar) ic; + return (uint8_t) ic; } -uchar -Nu_ReadOne(NuArchive* pArchive, FILE* fp) +uint8_t Nu_ReadOne(NuArchive* pArchive, FILE* fp) { - ushort dummyCrc CLEAN_INIT; + uint16_t dummyCrc CLEAN_INIT; return Nu_ReadOneC(pArchive, fp, &dummyCrc); } /* * Write one byte, optionally computing a CRC. */ -void -Nu_WriteOneC(NuArchive* pArchive, FILE* fp, uchar val, ushort* pCrc) +void Nu_WriteOneC(NuArchive* pArchive, FILE* fp, uint8_t val, uint16_t* pCrc) { - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); putc(val, fp); } -void -Nu_WriteOne(NuArchive* pArchive, FILE* fp, uchar val) +void Nu_WriteOne(NuArchive* pArchive, FILE* fp, uint8_t val) { - ushort dummyCrc CLEAN_INIT; + uint16_t dummyCrc CLEAN_INIT; Nu_WriteOneC(pArchive, fp, val, &dummyCrc); } @@ -72,27 +68,25 @@ Nu_WriteOne(NuArchive* pArchive, FILE* fp, uchar val) /* * Read two little-endian bytes, optionally computing a CRC. */ -ushort -Nu_ReadTwoC(NuArchive* pArchive, FILE* fp, ushort* pCrc) +uint16_t Nu_ReadTwoC(NuArchive* pArchive, FILE* fp, uint16_t* pCrc) { int ic1, ic2; - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); ic1 = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic1, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic1, *pCrc); ic2 = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic2, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic2, *pCrc); return ic1 | ic2 << 8; } -ushort -Nu_ReadTwo(NuArchive* pArchive, FILE* fp) +uint16_t Nu_ReadTwo(NuArchive* pArchive, FILE* fp) { - ushort dummyCrc CLEAN_INIT; + uint16_t dummyCrc CLEAN_INIT; return Nu_ReadTwoC(pArchive, fp, &dummyCrc); } @@ -100,28 +94,26 @@ Nu_ReadTwo(NuArchive* pArchive, FILE* fp) /* * Write two little-endian bytes, optionally computing a CRC. */ -void -Nu_WriteTwoC(NuArchive* pArchive, FILE* fp, ushort val, ushort* pCrc) +void Nu_WriteTwoC(NuArchive* pArchive, FILE* fp, uint16_t val, uint16_t* pCrc) { int ic1, ic2; - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); ic1 = val & 0xff; - *pCrc = Nu_UpdateCRC16((uchar)ic1, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic1, *pCrc); ic2 = val >> 8; - *pCrc = Nu_UpdateCRC16((uchar)ic2, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic2, *pCrc); putc(ic1, fp); putc(ic2, fp); } -void -Nu_WriteTwo(NuArchive* pArchive, FILE* fp, ushort val) +void Nu_WriteTwo(NuArchive* pArchive, FILE* fp, uint16_t val) { - ushort dummyCrc CLEAN_INIT; + uint16_t dummyCrc CLEAN_INIT; Nu_WriteTwoC(pArchive, fp, val, &dummyCrc); } @@ -129,31 +121,29 @@ Nu_WriteTwo(NuArchive* pArchive, FILE* fp, ushort val) /* * Read four little-endian bytes, optionally computing a CRC. */ -ulong -Nu_ReadFourC(NuArchive* pArchive, FILE* fp, ushort* pCrc) +uint32_t Nu_ReadFourC(NuArchive* pArchive, FILE* fp, uint16_t* pCrc) { int ic1, ic2, ic3, ic4; - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); ic1 = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic1, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic1, *pCrc); ic2 = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic2, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic2, *pCrc); ic3 = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic3, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic3, *pCrc); ic4 = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic4, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic4, *pCrc); - return ic1 | ic2 << 8 | (ulong)ic3 << 16 | (ulong)ic4 << 24; + return ic1 | ic2 << 8 | (uint32_t)ic3 << 16 | (uint32_t)ic4 << 24; } -ulong -Nu_ReadFour(NuArchive* pArchive, FILE* fp) +uint32_t Nu_ReadFour(NuArchive* pArchive, FILE* fp) { - ushort dummyCrc CLEAN_INIT; + uint16_t dummyCrc CLEAN_INIT; return Nu_ReadFourC(pArchive, fp, &dummyCrc); } @@ -161,23 +151,22 @@ Nu_ReadFour(NuArchive* pArchive, FILE* fp) /* * Write four little-endian bytes, optionally computing a CRC. */ -void -Nu_WriteFourC(NuArchive* pArchive, FILE* fp, ulong val, ushort* pCrc) +void Nu_WriteFourC(NuArchive* pArchive, FILE* fp, uint32_t val, uint16_t* pCrc) { int ic1, ic2, ic3, ic4; - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); ic1 = val & 0xff; - *pCrc = Nu_UpdateCRC16((uchar)ic1, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic1, *pCrc); ic2 = (val >> 8) & 0xff; - *pCrc = Nu_UpdateCRC16((uchar)ic2, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic2, *pCrc); ic3 = (val >> 16) & 0xff; - *pCrc = Nu_UpdateCRC16((uchar)ic3, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic3, *pCrc); ic4 = val >> 24; - *pCrc = Nu_UpdateCRC16((uchar)ic4, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic4, *pCrc); putc(ic1, fp); putc(ic2, fp); @@ -185,10 +174,9 @@ Nu_WriteFourC(NuArchive* pArchive, FILE* fp, ulong val, ushort* pCrc) putc(ic4, fp); } -void -Nu_WriteFour(NuArchive* pArchive, FILE* fp, ulong val) +void Nu_WriteFour(NuArchive* pArchive, FILE* fp, uint32_t val) { - ushort dummyCrc CLEAN_INIT; + uint16_t dummyCrc CLEAN_INIT; Nu_WriteFourC(pArchive, fp, val, &dummyCrc); } @@ -200,48 +188,46 @@ Nu_WriteFour(NuArchive* pArchive, FILE* fp, ulong val) * and GS/ShrinkIt. It's easy enough to deal with, and I figure the less * messing-with, the better. */ -NuDateTime -Nu_ReadDateTimeC(NuArchive* pArchive, FILE* fp, ushort* pCrc) +NuDateTime Nu_ReadDateTimeC(NuArchive* pArchive, FILE* fp, uint16_t* pCrc) { NuDateTime temp; int ic; - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); ic = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); temp.second = ic; ic = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); temp.minute = ic; ic = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); temp.hour = ic; ic = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); temp.year = ic; ic = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); temp.day = ic; ic = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); temp.month = ic; ic = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); temp.extra = ic; ic = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); temp.weekDay = ic; return temp; } -NuDateTime -Nu_ReadDateTime(NuArchive* pArchive, FILE* fp, ushort* pCrc) +NuDateTime Nu_ReadDateTime(NuArchive* pArchive, FILE* fp, uint16_t* pCrc) { - ushort dummyCrc CLEAN_INIT; + uint16_t dummyCrc CLEAN_INIT; return Nu_ReadDateTimeC(pArchive, fp, &dummyCrc); } @@ -249,46 +235,44 @@ Nu_ReadDateTime(NuArchive* pArchive, FILE* fp, ushort* pCrc) /* * Write an 8-byte NuFX Date/Time structure. */ -void -Nu_WriteDateTimeC(NuArchive* pArchive, FILE* fp, NuDateTime dateTime, - ushort* pCrc) +void Nu_WriteDateTimeC(NuArchive* pArchive, FILE* fp, NuDateTime dateTime, + uint16_t* pCrc) { int ic; - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); ic = dateTime.second; - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); putc(ic, fp); ic = dateTime.minute; - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); putc(ic, fp); ic = dateTime.hour; - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); putc(ic, fp); ic = dateTime.year; - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); putc(ic, fp); ic = dateTime.day; - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); putc(ic, fp); ic = dateTime.month; - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); putc(ic, fp); ic = dateTime.extra; - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); putc(ic, fp); ic = dateTime.weekDay; - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); putc(ic, fp); } -void -Nu_WriteDateTime(NuArchive* pArchive, FILE* fp, NuDateTime dateTime) +void Nu_WriteDateTime(NuArchive* pArchive, FILE* fp, NuDateTime dateTime) { - ushort dummyCrc CLEAN_INIT; + uint16_t dummyCrc CLEAN_INIT; Nu_WriteDateTimeC(pArchive, fp, dateTime, &dummyCrc); } @@ -296,30 +280,28 @@ Nu_WriteDateTime(NuArchive* pArchive, FILE* fp, NuDateTime dateTime) /* * Read N bytes from the stream, optionally computing a CRC. */ -void -Nu_ReadBytesC(NuArchive* pArchive, FILE* fp, void* vbuffer, long count, - ushort* pCrc) +void Nu_ReadBytesC(NuArchive* pArchive, FILE* fp, void* vbuffer, long count, + uint16_t* pCrc) { - uchar* buffer = vbuffer; + uint8_t* buffer = vbuffer; int ic; - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pCrc != nil); - Assert(buffer != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); + Assert(buffer != NULL); Assert(count > 0); while (count--) { ic = getc(fp); - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); *buffer++ = ic; } } -void -Nu_ReadBytes(NuArchive* pArchive, FILE* fp, void* vbuffer, long count) +void Nu_ReadBytes(NuArchive* pArchive, FILE* fp, void* vbuffer, long count) { - ushort dummyCrc CLEAN_INIT; + uint16_t dummyCrc CLEAN_INIT; Nu_ReadBytesC(pArchive, fp, vbuffer, count, &dummyCrc); } @@ -327,30 +309,29 @@ Nu_ReadBytes(NuArchive* pArchive, FILE* fp, void* vbuffer, long count) /* * Write N bytes to the stream, optionally computing a CRC. */ -void -Nu_WriteBytesC(NuArchive* pArchive, FILE* fp, const void* vbuffer, long count, - ushort* pCrc) +void Nu_WriteBytesC(NuArchive* pArchive, FILE* fp, const void* vbuffer, + long count, uint16_t* pCrc) { - const uchar* buffer = vbuffer; + const uint8_t* buffer = vbuffer; int ic; - Assert(pArchive != nil); - Assert(fp != nil); - Assert(pCrc != nil); - Assert(buffer != nil); + Assert(pArchive != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); + Assert(buffer != NULL); Assert(count > 0); while (count--) { ic = *buffer++; - *pCrc = Nu_UpdateCRC16((uchar)ic, *pCrc); + *pCrc = Nu_UpdateCRC16((uint8_t)ic, *pCrc); putc(ic, fp); } } -void -Nu_WriteBytes(NuArchive* pArchive, FILE* fp, const void* vbuffer, long count) +void Nu_WriteBytes(NuArchive* pArchive, FILE* fp, const void* vbuffer, + long count) { - ushort dummyCrc CLEAN_INIT; + uint16_t dummyCrc CLEAN_INIT; Nu_WriteBytesC(pArchive, fp, vbuffer, count, &dummyCrc); } @@ -365,8 +346,7 @@ Nu_WriteBytes(NuArchive* pArchive, FILE* fp, const void* vbuffer, long count) * Determine whether the stream completed the last set of operations * successfully. */ -NuError -Nu_HeaderIOFailed(NuArchive* pArchive, FILE* fp) +NuError Nu_HeaderIOFailed(NuArchive* pArchive, FILE* fp) { if (feof(fp) || ferror(fp)) return kNuErrFile; @@ -381,8 +361,7 @@ Nu_HeaderIOFailed(NuArchive* pArchive, FILE* fp) * * The values for "ptrname" are the same as for fseek(). */ -NuError -Nu_SeekArchive(NuArchive* pArchive, FILE* fp, long offset, int ptrname) +NuError Nu_SeekArchive(NuArchive* pArchive, FILE* fp, long offset, int ptrname) { if (Nu_IsStreaming(pArchive)) { Assert(ptrname == SEEK_CUR); @@ -408,10 +387,9 @@ Nu_SeekArchive(NuArchive* pArchive, FILE* fp, long offset, int ptrname) * * Note that rewind(3S) resets the error indication, but this doesn't. */ -NuError -Nu_RewindArchive(NuArchive* pArchive) +NuError Nu_RewindArchive(NuArchive* pArchive) { - Assert(pArchive != nil); + Assert(pArchive != NULL); Assert(!Nu_IsStreaming(pArchive)); if (Nu_SeekArchive(pArchive, pArchive->archiveFp, diff --git a/nufxlib/Bzip2.c b/nufxlib/Bzip2.c index 8fde0b6..6515e21 100644 --- a/nufxlib/Bzip2.c +++ b/nufxlib/Bzip2.c @@ -25,13 +25,11 @@ /* * Alloc and free functions provided to libbz2. */ -static void* -Nu_bzalloc(void* opaque, int items, int size) +static void* Nu_bzalloc(void* opaque, int items, int size) { return Nu_Malloc(opaque, items * size); } -static void -Nu_bzfree(void* opaque, void* address) +static void Nu_bzfree(void* opaque, void* address) { Nu_Free(opaque, address); } @@ -46,21 +44,20 @@ Nu_bzfree(void* opaque, void* address) /* * Compress "srcLen" bytes from "pStraw" to "fp". */ -NuError -Nu_CompressBzip2(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc) +NuError Nu_CompressBzip2(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc) { NuError err = kNuErrNone; bz_stream bzstream; int bzerr; - uchar* outbuf = nil; + uint8_t* outbuf = NULL; - Assert(pArchive != nil); - Assert(pStraw != nil); - Assert(fp != nil); + Assert(pArchive != NULL); + Assert(pStraw != NULL); + Assert(fp != NULL); Assert(srcLen > 0); - Assert(pDstLen != nil); - Assert(pCrc != nil); + Assert(pDstLen != NULL); + Assert(pCrc != NULL); err = Nu_AllocCompressionBufferIFN(pArchive); if (err != kNuErrNone) @@ -76,7 +73,7 @@ Nu_CompressBzip2(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, bzstream.bzalloc = Nu_bzalloc; bzstream.bzfree = Nu_bzfree; bzstream.opaque = pArchive; - bzstream.next_in = nil; + bzstream.next_in = NULL; bzstream.avail_in = 0; bzstream.next_out = outbuf; bzstream.avail_out = kNuGenCompBufSize; @@ -98,7 +95,7 @@ Nu_CompressBzip2(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, * Loop while we have data. */ do { - ulong getSize; + uint32_t getSize; int action; /* should be able to read a full buffer every time */ @@ -139,8 +136,8 @@ Nu_CompressBzip2(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, (bzerr == BZ_STREAM_END && bzstream.avail_out != kNuGenCompBufSize)) { DBUG(("+++ writing %d bytes\n", - (uchar*)bzstream.next_out - outbuf)); - err = Nu_FWrite(fp, outbuf, (uchar*)bzstream.next_out - outbuf); + (uint8_t*)bzstream.next_out - outbuf)); + err = Nu_FWrite(fp, outbuf, (uint8_t*)bzstream.next_out - outbuf); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, "fwrite failed in bzip2"); goto bz_bail; @@ -158,8 +155,8 @@ bz_bail: BZ2_bzCompressEnd(&bzstream); /* free up any allocated structures */ bail: - if (outbuf != nil) - free(outbuf); + if (outbuf != NULL) + Nu_Free(NULL, outbuf); return err; } @@ -173,20 +170,19 @@ bail: /* * Expand from "infp" to "pFunnel". */ -NuError -Nu_ExpandBzip2(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pCrc) +NuError Nu_ExpandBzip2(NuArchive* pArchive, const NuRecord* pRecord, + const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, uint16_t* pCrc) { NuError err = kNuErrNone; bz_stream bzstream; int bzerr; - ulong compRemaining; - uchar* outbuf; + uint32_t compRemaining; + uint8_t* outbuf; - Assert(pArchive != nil); - Assert(pThread != nil); - Assert(infp != nil); - Assert(pFunnel != nil); + Assert(pArchive != NULL); + Assert(pThread != NULL); + Assert(infp != NULL); + Assert(pFunnel != NULL); err = Nu_AllocCompressionBufferIFN(pArchive); if (err != kNuErrNone) @@ -204,7 +200,7 @@ Nu_ExpandBzip2(NuArchive* pArchive, const NuRecord* pRecord, bzstream.bzalloc = Nu_bzalloc; bzstream.bzfree = Nu_bzfree; bzstream.opaque = pArchive; - bzstream.next_in = nil; + bzstream.next_in = NULL; bzstream.avail_in = 0; bzstream.next_out = outbuf; bzstream.avail_out = kNuGenCompBufSize; @@ -226,7 +222,7 @@ Nu_ExpandBzip2(NuArchive* pArchive, const NuRecord* pRecord, * Loop while we have data. */ do { - ulong getSize; + uint32_t getSize; /* read as much as we can */ if (bzstream.avail_in == 0) { @@ -258,17 +254,18 @@ Nu_ExpandBzip2(NuArchive* pArchive, const NuRecord* pRecord, /* write every time there's anything (buffer will usually be full) */ if (bzstream.avail_out != kNuGenCompBufSize) { - DBUG(("+++ writing %d bytes\n",(uchar*)bzstream.next_out - outbuf)); + DBUG(("+++ writing %d bytes\n", + (uint8_t*) bzstream.next_out - outbuf)); err = Nu_FunnelWrite(pArchive, pFunnel, outbuf, - (uchar*)bzstream.next_out - outbuf); + (uint8_t*)bzstream.next_out - outbuf); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, "write failed in bzip2"); goto bz_bail; } - if (pCrc != nil) + if (pCrc != NULL) *pCrc = Nu_CalcCRC16(*pCrc, outbuf, - (uchar*) bzstream.next_out - outbuf); + (uint8_t*) bzstream.next_out - outbuf); bzstream.next_out = outbuf; bzstream.avail_out = kNuGenCompBufSize; @@ -291,8 +288,8 @@ bz_bail: BZ2_bzDecompressEnd(&bzstream); /* free up any allocated structures */ bail: - if (outbuf != nil) - free(outbuf); + if (outbuf != NULL) + Nu_Free(NULL, outbuf); return err; } diff --git a/nufxlib/ChangeLog.txt b/nufxlib/ChangeLog.txt index b932e41..ca1c148 100644 --- a/nufxlib/ChangeLog.txt +++ b/nufxlib/ChangeLog.txt @@ -1,3 +1,14 @@ +2015/01/03 fadden + - Mac OS X: replace Carbon FinderInfo calls with BSD xattr. + - Mac OS X: fix resource fork naming. + - Mac OS X: disable use of native resource forks. + +2015/01/02 fadden + - Distinguish Unicode and Mac OS Roman strings. + +2014/12/22 fadden + - Source code cleanup. + 2014/10/30 ***** v2.2.2 shipped ***** 2014/10/28 fadden diff --git a/nufxlib/Charset.c b/nufxlib/Charset.c new file mode 100644 index 0000000..9ab10c7 --- /dev/null +++ b/nufxlib/Charset.c @@ -0,0 +1,553 @@ +/* + * NuFX archive manipulation library + * Copyright (C) 2014 by Andy McFadden, All Rights Reserved. + * This is free software; you can redistribute it and/or modify it under the + * terms of the BSD License, see the file COPYING-LIB. + * + * Miscellaneous NufxLib utility functions. + */ +#include "NufxLibPriv.h" + +/* + * Convert Mac OS Roman to Unicode. Mapping comes from: + * + * http://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/ROMAN.TXT + * + * We use the "Control Pictures" block for the control characters + * (0x00-0x1f, 0x7f --> 0x2400-0x241f, 0x2421). This is a bit nicer + * than embedding control characters in filenames. + */ +static const uint16_t gMORToUnicode[256] = { + /*0x00*/ 0x2400, // [control] NULL + /*0x01*/ 0x2401, // [control] START OF HEADING + /*0x02*/ 0x2402, // [control] START OF TEXT + /*0x03*/ 0x2403, // [control] END OF TEXT + /*0x04*/ 0x2404, // [control] END OF TRANSMISSION + /*0x05*/ 0x2405, // [control] ENQUIRY + /*0x06*/ 0x2406, // [control] ACKNOWLEDGE + /*0x07*/ 0x2407, // [control] BELL + /*0x08*/ 0x2408, // [control] BACKSPACE + /*0x09*/ 0x2409, // [control] HORIZONTAL TABULATION + /*0x0a*/ 0x240a, // [control] LINE FEED + /*0x0b*/ 0x240b, // [control] VERTICAL TABULATION + /*0x0c*/ 0x240c, // [control] FORM FEED + /*0x0d*/ 0x240d, // [control] CARRIAGE RETURN + /*0x0e*/ 0x240e, // [control] SHIFT OUT + /*0x0f*/ 0x240f, // [control] SHIFT IN + /*0x10*/ 0x2410, // [control] DATA LINK ESCAPE + /*0x11*/ 0x2411, // [control] DEVICE CONTROL ONE + /*0x12*/ 0x2412, // [control] DEVICE CONTROL TWO + /*0x13*/ 0x2413, // [control] DEVICE CONTROL THREE + /*0x14*/ 0x2414, // [control] DEVICE CONTROL FOUR + /*0x15*/ 0x2415, // [control] NEGATIVE ACKNOWLEDGE + /*0x16*/ 0x2416, // [control] SYNCHRONOUS IDLE + /*0x17*/ 0x2417, // [control] END OF TRANSMISSION BLOCK + /*0x18*/ 0x2418, // [control] CANCEL + /*0x19*/ 0x2419, // [control] END OF MEDIUM + /*0x1a*/ 0x241a, // [control] SUBSTITUTE + /*0x1b*/ 0x241b, // [control] ESCAPE + /*0x1c*/ 0x241c, // [control] FILE SEPARATOR + /*0x1d*/ 0x241d, // [control] GROUP SEPARATOR + /*0x1e*/ 0x241e, // [control] RECORD SEPARATOR + /*0x1f*/ 0x241f, // [control] UNIT SEPARATOR + /*0x20*/ 0x0020, // SPACE + /*0x21*/ 0x0021, // EXCLAMATION MARK + /*0x22*/ 0x0022, // QUOTATION MARK + /*0x23*/ 0x0023, // NUMBER SIGN + /*0x24*/ 0x0024, // DOLLAR SIGN + /*0x25*/ 0x0025, // PERCENT SIGN + /*0x26*/ 0x0026, // AMPERSAND + /*0x27*/ 0x0027, // APOSTROPHE + /*0x28*/ 0x0028, // LEFT PARENTHESIS + /*0x29*/ 0x0029, // RIGHT PARENTHESIS + /*0x2A*/ 0x002A, // ASTERISK + /*0x2B*/ 0x002B, // PLUS SIGN + /*0x2C*/ 0x002C, // COMMA + /*0x2D*/ 0x002D, // HYPHEN-MINUS + /*0x2E*/ 0x002E, // FULL STOP + /*0x2F*/ 0x002F, // SOLIDUS + /*0x30*/ 0x0030, // DIGIT ZERO + /*0x31*/ 0x0031, // DIGIT ONE + /*0x32*/ 0x0032, // DIGIT TWO + /*0x33*/ 0x0033, // DIGIT THREE + /*0x34*/ 0x0034, // DIGIT FOUR + /*0x35*/ 0x0035, // DIGIT FIVE + /*0x36*/ 0x0036, // DIGIT SIX + /*0x37*/ 0x0037, // DIGIT SEVEN + /*0x38*/ 0x0038, // DIGIT EIGHT + /*0x39*/ 0x0039, // DIGIT NINE + /*0x3A*/ 0x003A, // COLON + /*0x3B*/ 0x003B, // SEMICOLON + /*0x3C*/ 0x003C, // LESS-THAN SIGN + /*0x3D*/ 0x003D, // EQUALS SIGN + /*0x3E*/ 0x003E, // GREATER-THAN SIGN + /*0x3F*/ 0x003F, // QUESTION MARK + /*0x40*/ 0x0040, // COMMERCIAL AT + /*0x41*/ 0x0041, // LATIN CAPITAL LETTER A + /*0x42*/ 0x0042, // LATIN CAPITAL LETTER B + /*0x43*/ 0x0043, // LATIN CAPITAL LETTER C + /*0x44*/ 0x0044, // LATIN CAPITAL LETTER D + /*0x45*/ 0x0045, // LATIN CAPITAL LETTER E + /*0x46*/ 0x0046, // LATIN CAPITAL LETTER F + /*0x47*/ 0x0047, // LATIN CAPITAL LETTER G + /*0x48*/ 0x0048, // LATIN CAPITAL LETTER H + /*0x49*/ 0x0049, // LATIN CAPITAL LETTER I + /*0x4A*/ 0x004A, // LATIN CAPITAL LETTER J + /*0x4B*/ 0x004B, // LATIN CAPITAL LETTER K + /*0x4C*/ 0x004C, // LATIN CAPITAL LETTER L + /*0x4D*/ 0x004D, // LATIN CAPITAL LETTER M + /*0x4E*/ 0x004E, // LATIN CAPITAL LETTER N + /*0x4F*/ 0x004F, // LATIN CAPITAL LETTER O + /*0x50*/ 0x0050, // LATIN CAPITAL LETTER P + /*0x51*/ 0x0051, // LATIN CAPITAL LETTER Q + /*0x52*/ 0x0052, // LATIN CAPITAL LETTER R + /*0x53*/ 0x0053, // LATIN CAPITAL LETTER S + /*0x54*/ 0x0054, // LATIN CAPITAL LETTER T + /*0x55*/ 0x0055, // LATIN CAPITAL LETTER U + /*0x56*/ 0x0056, // LATIN CAPITAL LETTER V + /*0x57*/ 0x0057, // LATIN CAPITAL LETTER W + /*0x58*/ 0x0058, // LATIN CAPITAL LETTER X + /*0x59*/ 0x0059, // LATIN CAPITAL LETTER Y + /*0x5A*/ 0x005A, // LATIN CAPITAL LETTER Z + /*0x5B*/ 0x005B, // LEFT SQUARE BRACKET + /*0x5C*/ 0x005C, // REVERSE SOLIDUS + /*0x5D*/ 0x005D, // RIGHT SQUARE BRACKET + /*0x5E*/ 0x005E, // CIRCUMFLEX ACCENT + /*0x5F*/ 0x005F, // LOW LINE + /*0x60*/ 0x0060, // GRAVE ACCENT + /*0x61*/ 0x0061, // LATIN SMALL LETTER A + /*0x62*/ 0x0062, // LATIN SMALL LETTER B + /*0x63*/ 0x0063, // LATIN SMALL LETTER C + /*0x64*/ 0x0064, // LATIN SMALL LETTER D + /*0x65*/ 0x0065, // LATIN SMALL LETTER E + /*0x66*/ 0x0066, // LATIN SMALL LETTER F + /*0x67*/ 0x0067, // LATIN SMALL LETTER G + /*0x68*/ 0x0068, // LATIN SMALL LETTER H + /*0x69*/ 0x0069, // LATIN SMALL LETTER I + /*0x6A*/ 0x006A, // LATIN SMALL LETTER J + /*0x6B*/ 0x006B, // LATIN SMALL LETTER K + /*0x6C*/ 0x006C, // LATIN SMALL LETTER L + /*0x6D*/ 0x006D, // LATIN SMALL LETTER M + /*0x6E*/ 0x006E, // LATIN SMALL LETTER N + /*0x6F*/ 0x006F, // LATIN SMALL LETTER O + /*0x70*/ 0x0070, // LATIN SMALL LETTER P + /*0x71*/ 0x0071, // LATIN SMALL LETTER Q + /*0x72*/ 0x0072, // LATIN SMALL LETTER R + /*0x73*/ 0x0073, // LATIN SMALL LETTER S + /*0x74*/ 0x0074, // LATIN SMALL LETTER T + /*0x75*/ 0x0075, // LATIN SMALL LETTER U + /*0x76*/ 0x0076, // LATIN SMALL LETTER V + /*0x77*/ 0x0077, // LATIN SMALL LETTER W + /*0x78*/ 0x0078, // LATIN SMALL LETTER X + /*0x79*/ 0x0079, // LATIN SMALL LETTER Y + /*0x7A*/ 0x007A, // LATIN SMALL LETTER Z + /*0x7B*/ 0x007B, // LEFT CURLY BRACKET + /*0x7C*/ 0x007C, // VERTICAL LINE + /*0x7D*/ 0x007D, // RIGHT CURLY BRACKET + /*0x7E*/ 0x007E, // TILDE + /*0x7f*/ 0x2421, // [control] DELETE + /*0x80*/ 0x00C4, // LATIN CAPITAL LETTER A WITH DIAERESIS + /*0x81*/ 0x00C5, // LATIN CAPITAL LETTER A WITH RING ABOVE + /*0x82*/ 0x00C7, // LATIN CAPITAL LETTER C WITH CEDILLA + /*0x83*/ 0x00C9, // LATIN CAPITAL LETTER E WITH ACUTE + /*0x84*/ 0x00D1, // LATIN CAPITAL LETTER N WITH TILDE + /*0x85*/ 0x00D6, // LATIN CAPITAL LETTER O WITH DIAERESIS + /*0x86*/ 0x00DC, // LATIN CAPITAL LETTER U WITH DIAERESIS + /*0x87*/ 0x00E1, // LATIN SMALL LETTER A WITH ACUTE + /*0x88*/ 0x00E0, // LATIN SMALL LETTER A WITH GRAVE + /*0x89*/ 0x00E2, // LATIN SMALL LETTER A WITH CIRCUMFLEX + /*0x8A*/ 0x00E4, // LATIN SMALL LETTER A WITH DIAERESIS + /*0x8B*/ 0x00E3, // LATIN SMALL LETTER A WITH TILDE + /*0x8C*/ 0x00E5, // LATIN SMALL LETTER A WITH RING ABOVE + /*0x8D*/ 0x00E7, // LATIN SMALL LETTER C WITH CEDILLA + /*0x8E*/ 0x00E9, // LATIN SMALL LETTER E WITH ACUTE + /*0x8F*/ 0x00E8, // LATIN SMALL LETTER E WITH GRAVE + /*0x90*/ 0x00EA, // LATIN SMALL LETTER E WITH CIRCUMFLEX + /*0x91*/ 0x00EB, // LATIN SMALL LETTER E WITH DIAERESIS + /*0x92*/ 0x00ED, // LATIN SMALL LETTER I WITH ACUTE + /*0x93*/ 0x00EC, // LATIN SMALL LETTER I WITH GRAVE + /*0x94*/ 0x00EE, // LATIN SMALL LETTER I WITH CIRCUMFLEX + /*0x95*/ 0x00EF, // LATIN SMALL LETTER I WITH DIAERESIS + /*0x96*/ 0x00F1, // LATIN SMALL LETTER N WITH TILDE + /*0x97*/ 0x00F3, // LATIN SMALL LETTER O WITH ACUTE + /*0x98*/ 0x00F2, // LATIN SMALL LETTER O WITH GRAVE + /*0x99*/ 0x00F4, // LATIN SMALL LETTER O WITH CIRCUMFLEX + /*0x9A*/ 0x00F6, // LATIN SMALL LETTER O WITH DIAERESIS + /*0x9B*/ 0x00F5, // LATIN SMALL LETTER O WITH TILDE + /*0x9C*/ 0x00FA, // LATIN SMALL LETTER U WITH ACUTE + /*0x9D*/ 0x00F9, // LATIN SMALL LETTER U WITH GRAVE + /*0x9E*/ 0x00FB, // LATIN SMALL LETTER U WITH CIRCUMFLEX + /*0x9F*/ 0x00FC, // LATIN SMALL LETTER U WITH DIAERESIS + /*0xA0*/ 0x2020, // DAGGER + /*0xA1*/ 0x00B0, // DEGREE SIGN + /*0xA2*/ 0x00A2, // CENT SIGN + /*0xA3*/ 0x00A3, // POUND SIGN + /*0xA4*/ 0x00A7, // SECTION SIGN + /*0xA5*/ 0x2022, // BULLET + /*0xA6*/ 0x00B6, // PILCROW SIGN + /*0xA7*/ 0x00DF, // LATIN SMALL LETTER SHARP S + /*0xA8*/ 0x00AE, // REGISTERED SIGN + /*0xA9*/ 0x00A9, // COPYRIGHT SIGN + /*0xAA*/ 0x2122, // TRADE MARK SIGN + /*0xAB*/ 0x00B4, // ACUTE ACCENT + /*0xAC*/ 0x00A8, // DIAERESIS + /*0xAD*/ 0x2260, // NOT EQUAL TO + /*0xAE*/ 0x00C6, // LATIN CAPITAL LETTER AE + /*0xAF*/ 0x00D8, // LATIN CAPITAL LETTER O WITH STROKE + /*0xB0*/ 0x221E, // INFINITY + /*0xB1*/ 0x00B1, // PLUS-MINUS SIGN + /*0xB2*/ 0x2264, // LESS-THAN OR EQUAL TO + /*0xB3*/ 0x2265, // GREATER-THAN OR EQUAL TO + /*0xB4*/ 0x00A5, // YEN SIGN + /*0xB5*/ 0x00B5, // MICRO SIGN + /*0xB6*/ 0x2202, // PARTIAL DIFFERENTIAL + /*0xB7*/ 0x2211, // N-ARY SUMMATION + /*0xB8*/ 0x220F, // N-ARY PRODUCT + /*0xB9*/ 0x03C0, // GREEK SMALL LETTER PI + /*0xBA*/ 0x222B, // INTEGRAL + /*0xBB*/ 0x00AA, // FEMININE ORDINAL INDICATOR + /*0xBC*/ 0x00BA, // MASCULINE ORDINAL INDICATOR + /*0xBD*/ 0x03A9, // GREEK CAPITAL LETTER OMEGA + /*0xBE*/ 0x00E6, // LATIN SMALL LETTER AE + /*0xBF*/ 0x00F8, // LATIN SMALL LETTER O WITH STROKE + /*0xC0*/ 0x00BF, // INVERTED QUESTION MARK + /*0xC1*/ 0x00A1, // INVERTED EXCLAMATION MARK + /*0xC2*/ 0x00AC, // NOT SIGN + /*0xC3*/ 0x221A, // SQUARE ROOT + /*0xC4*/ 0x0192, // LATIN SMALL LETTER F WITH HOOK + /*0xC5*/ 0x2248, // ALMOST EQUAL TO + /*0xC6*/ 0x2206, // INCREMENT + /*0xC7*/ 0x00AB, // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK + /*0xC8*/ 0x00BB, // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK + /*0xC9*/ 0x2026, // HORIZONTAL ELLIPSIS + /*0xCA*/ 0x00A0, // NO-BREAK SPACE + /*0xCB*/ 0x00C0, // LATIN CAPITAL LETTER A WITH GRAVE + /*0xCC*/ 0x00C3, // LATIN CAPITAL LETTER A WITH TILDE + /*0xCD*/ 0x00D5, // LATIN CAPITAL LETTER O WITH TILDE + /*0xCE*/ 0x0152, // LATIN CAPITAL LIGATURE OE + /*0xCF*/ 0x0153, // LATIN SMALL LIGATURE OE + /*0xD0*/ 0x2013, // EN DASH + /*0xD1*/ 0x2014, // EM DASH + /*0xD2*/ 0x201C, // LEFT DOUBLE QUOTATION MARK + /*0xD3*/ 0x201D, // RIGHT DOUBLE QUOTATION MARK + /*0xD4*/ 0x2018, // LEFT SINGLE QUOTATION MARK + /*0xD5*/ 0x2019, // RIGHT SINGLE QUOTATION MARK + /*0xD6*/ 0x00F7, // DIVISION SIGN + /*0xD7*/ 0x25CA, // LOZENGE + /*0xD8*/ 0x00FF, // LATIN SMALL LETTER Y WITH DIAERESIS + /*0xD9*/ 0x0178, // LATIN CAPITAL LETTER Y WITH DIAERESIS + /*0xDA*/ 0x2044, // FRACTION SLASH + /*0xDB*/ 0x00A4, // CURRENCY SIGN (was EURO SIGN) + /*0xDC*/ 0x2039, // SINGLE LEFT-POINTING ANGLE QUOTATION MARK + /*0xDD*/ 0x203A, // SINGLE RIGHT-POINTING ANGLE QUOTATION MARK + /*0xDE*/ 0xFB01, // LATIN SMALL LIGATURE FI + /*0xDF*/ 0xFB02, // LATIN SMALL LIGATURE FL + /*0xE0*/ 0x2021, // DOUBLE DAGGER + /*0xE1*/ 0x00B7, // MIDDLE DOT + /*0xE2*/ 0x201A, // SINGLE LOW-9 QUOTATION MARK + /*0xE3*/ 0x201E, // DOUBLE LOW-9 QUOTATION MARK + /*0xE4*/ 0x2030, // PER MILLE SIGN + /*0xE5*/ 0x00C2, // LATIN CAPITAL LETTER A WITH CIRCUMFLEX + /*0xE6*/ 0x00CA, // LATIN CAPITAL LETTER E WITH CIRCUMFLEX + /*0xE7*/ 0x00C1, // LATIN CAPITAL LETTER A WITH ACUTE + /*0xE8*/ 0x00CB, // LATIN CAPITAL LETTER E WITH DIAERESIS + /*0xE9*/ 0x00C8, // LATIN CAPITAL LETTER E WITH GRAVE + /*0xEA*/ 0x00CD, // LATIN CAPITAL LETTER I WITH ACUTE + /*0xEB*/ 0x00CE, // LATIN CAPITAL LETTER I WITH CIRCUMFLEX + /*0xEC*/ 0x00CF, // LATIN CAPITAL LETTER I WITH DIAERESIS + /*0xED*/ 0x00CC, // LATIN CAPITAL LETTER I WITH GRAVE + /*0xEE*/ 0x00D3, // LATIN CAPITAL LETTER O WITH ACUTE + /*0xEF*/ 0x00D4, // LATIN CAPITAL LETTER O WITH CIRCUMFLEX + /*0xF0*/ 0xF8FF, // Apple logo + /*0xF1*/ 0x00D2, // LATIN CAPITAL LETTER O WITH GRAVE + /*0xF2*/ 0x00DA, // LATIN CAPITAL LETTER U WITH ACUTE + /*0xF3*/ 0x00DB, // LATIN CAPITAL LETTER U WITH CIRCUMFLEX + /*0xF4*/ 0x00D9, // LATIN CAPITAL LETTER U WITH GRAVE + /*0xF5*/ 0x0131, // LATIN SMALL LETTER DOTLESS I + /*0xF6*/ 0x02C6, // MODIFIER LETTER CIRCUMFLEX ACCENT + /*0xF7*/ 0x02DC, // SMALL TILDE + /*0xF8*/ 0x00AF, // MACRON + /*0xF9*/ 0x02D8, // BREVE + /*0xFA*/ 0x02D9, // DOT ABOVE + /*0xFB*/ 0x02DA, // RING ABOVE + /*0xFC*/ 0x00B8, // CEDILLA + /*0xFD*/ 0x02DD, // DOUBLE ACUTE ACCENT + /*0xFE*/ 0x02DB, // OGONEK + /*0xFF*/ 0x02C7 // CARON +}; + +/* + * Static table, populated on first use. Provides the inverse map. + * + * An entry with 0x00 indicates no conversion. That's incorrect for + * the entry for '\0', but since we're operating on null-terminated + * strings that's never valid anyway. (It's possible for a filename + * to contain 0x2400, but that would translate to 0x00, which we don't + * allow; so it makes more sense to treat it as illegal.) + */ +static uint8_t gUnicodeToMOR[65536] = { 0xff /*indicates not initialized*/ }; + +static void Nu_GenerateUnicodeToMOR(void) +{ + memset(gUnicodeToMOR, 0, sizeof(gUnicodeToMOR)); + + int i; + for (i = 0; i < 256; i++) { + int codePoint = gMORToUnicode[i]; + Assert(codePoint >= 0 && codePoint < 65536); + gUnicodeToMOR[codePoint] = i; + } +} + + +/* + * Converts stringMOR to Unicode, storing the output in bufUNI until it's + * full. Null termination is guaranteed. If the buffer size is zero or + * bufUNI is NULL, no string data is returned. + * + * Returns the number of bytes required to represent stringMOR in Unicode. + */ +size_t Nu_ConvertMORToUNI(const char* stringMOR, UNICHAR* bufUNI, + size_t bufSize) +{ + Assert(stringMOR != 0); + +#ifdef _WIN32 + /* place-holder if we're not using UTF-16 yet */ + Assert(sizeof(UNICHAR) == 1); + size_t morLen = strlen(stringMOR) + 1; + if (bufUNI != NULL && bufSize != 0) { + size_t copyLen = morLen < bufSize ? morLen : bufSize; + memcpy(bufUNI, stringMOR, copyLen); + bufUNI[bufSize-1] = '\0'; + } + return morLen; +#else + /* + * Convert Mac OS Roman to UTF-8. We only output full code points, + * so if only the first byte of a UTF-8 sequence will fit we just + * stop early. + */ + size_t uniLen = 0; + Boolean doOutput = (bufUNI != NULL); + + while (*stringMOR != '\0') { + // ASCII values just "convert" to themselves in this table + uint16_t us = gMORToUnicode[(uint8_t)*stringMOR]; + if (us < 0x80) { + // single byte, no conversion + if (uniLen+1 >= bufSize) { + doOutput = false; + } + if (doOutput) { + bufUNI[uniLen] = (char) us; + } + uniLen++; + } else if (us < 0x7ff) { + // two bytes + if (uniLen+2 >= bufSize) { + doOutput = false; + } + if (doOutput) { + bufUNI[uniLen] = (us >> 6) | 0xc0; + bufUNI[uniLen+1] = (us & 0x3f) | 0x80; + } + uniLen += 2; + } else { + // three bytes + if (uniLen+3 >= bufSize) { + doOutput = false; + } + if (doOutput) { + bufUNI[uniLen] = (us >> 12) | 0xe0; + bufUNI[uniLen+1] = ((us >> 6) & 0x3f) | 0x80; + bufUNI[uniLen+2] = (us & 0x3f) | 0x80; + } + uniLen += 3; + } + + stringMOR++; + } + + // null-terminate + if (doOutput && uniLen < bufSize) { + bufUNI[uniLen] = '\0'; + } + uniLen++; + + return uniLen; +#endif +} + +/* + * Decode a single Unicode code point from a UTF-8 string. This will + * consume 1 to 4 bytes. If an error is detected, only one byte is + * consumed, and the code point value will be 0xDCnn (invalid). + * + * cf. http://en.wikipedia.org/wiki/UTF-8#Sample_code + */ +static uint32_t Nu_DecodeUTF8(const char** pStr) +{ + const uint8_t* str = (const uint8_t*) *pStr; + uint32_t codePoint; + uint32_t uc1, uc2, uc3, uc4; + uc1 = *str++; + + if (uc1 < 0x80) { + // single byte + codePoint = uc1; + } else if (uc1 < 0xc2) { + // illegal: continuation or overlong 2-byte sequence + goto fail; + } else if (uc1 < 0xe0) { + // 2-byte sequence + uc2 = *str++; + if ((uc2 & 0xc0) != 0x80) { + goto fail; // not a continuation + } + codePoint = (uc1 << 6) + uc2 - 0x3080; + } else if (uc1 < 0xf0) { + // 3-byte sequence */ + uc2 = *str++; + if ((uc2 & 0xc0) != 0x80) { + goto fail; // not a continuation + } + if (uc1 == 0xe0 && uc2 < 0xa0) { + goto fail; // overlong + } + uc3 = *str++; + if ((uc3 & 0xc0) != 0x80) { + goto fail; // not a continuation + } + codePoint = (uc1 << 12) + (uc2 << 6) + uc3 - 0xE2080; + } else if (uc1 < 0xf5) { + uc2 = *str++; + if ((uc2 & 0xc0) != 0x80) { + goto fail; // not a continuation + } + if (uc1 == 0xf0 && uc2 < 0x90) { + goto fail; // overlong + } + if (uc1 == 0xf4 && uc2 >= 0x90) { + goto fail; // U+10FFFF + } + uc3 = *str++; + if ((uc3 & 0xc0) != 0x80) { + goto fail; // not a continuation + } + uc4 = *str++; + if ((uc4 & 0xc0) != 0x80) { + goto fail; // not a continuation + } + codePoint = (uc1 << 18) + (uc2 << 12) + (uc3 << 6) + uc4 - 0x3C82080; + } else { + // illegal: > U+10FFFF + goto fail; + } + + *pStr = (const UNICHAR*) str; + return codePoint; + +fail: + (*pStr)++; // advance one char only + return 0xdc00 | uc1; +} + +/* + * Converts stringUNI to Mac OS Roman, storing the output in bufMOR + * until it's full. Null termination is guaranteed. If the buffer + * size is zero or bufMOR is NULL, no string data is returned. + * + * Returns the number of bytes required to represent stringUNI in MOR. + */ +size_t Nu_ConvertUNIToMOR(const UNICHAR* stringUNI, char* bufMOR, + size_t bufSize) +{ + Assert(stringUNI != 0); + +#ifdef _WIN32 + /* + * Place-holder if we're not using UTF-16 yet. This doesn't pass + * tests that check for behavior with non-MOR Unicode values. + */ + Assert(sizeof(UNICHAR) == 1); + size_t uniLen = strlen(stringUNI) + 1; + if (bufMOR != NULL && bufSize != 0) { + size_t copyLen = uniLen < bufSize ? uniLen : bufSize; + memcpy(bufMOR, stringUNI, copyLen); + bufMOR[bufSize-1] = '\0'; + } + return uniLen; +#else + /* + * Convert UTF-8 to Mac OS Roman. If the code point doesn't have + * a valid conversion (either because it's not in the table, or the + * UTF-8 code is damaged) we just insert an ASCII '?'. + */ + if (gUnicodeToMOR[0] == 0xff) { + Nu_GenerateUnicodeToMOR(); + Assert(gUnicodeToMOR[0] != 0xff); + } + + uint32_t codePoint; + size_t morLen = 0; + Boolean doOutput = (bufMOR != NULL); + + while (*stringUNI != '\0') { + codePoint = Nu_DecodeUTF8(&stringUNI); + char mc; + + if (codePoint < 0x80) { + mc = (char) codePoint; + } else if (codePoint < 0xffff) { + // UTF-8 errors come back as 0xDCnn, which has no mapping in table + mc = gUnicodeToMOR[codePoint]; + if (mc == 0x00) { + mc = '?'; + } + } else { + // non-BMP code point + mc = '?'; + } + if (morLen+1 >= bufSize) { + doOutput = false; + } + if (doOutput) { + bufMOR[morLen] = mc; + } + morLen++; + } + + // null-terminate + if (doOutput && morLen < bufSize) { + bufMOR[morLen] = '\0'; + } + morLen++; + + return morLen; +#endif +} + +/* + * Utility function that wraps NuConvertMORToUTF8, allocating a new + * buffer to hold the converted string. The caller must free the result. + * + * Returns NULL if stringMOR is NULL or the conversion fails. + */ +UNICHAR* Nu_CopyMORToUNI(const char* stringMOR) +{ + size_t uniLen; + UNICHAR* uniBuf; + + if (stringMOR == NULL) { + return NULL; + } + + uniLen = Nu_ConvertMORToUNI(stringMOR, NULL, 0); + if (uniLen == (size_t) -1) { + return NULL; + } + uniBuf = (UNICHAR*) Nu_Malloc(NULL, uniLen); + Nu_ConvertMORToUNI(stringMOR, uniBuf, uniLen); + return uniBuf; +} diff --git a/nufxlib/Compress.c b/nufxlib/Compress.c index c8f4cc3..d6bfc6a 100644 --- a/nufxlib/Compress.c +++ b/nufxlib/Compress.c @@ -15,17 +15,16 @@ /* * "Compress" an uncompressed thread. */ -static NuError -Nu_CompressUncompressed(NuArchive* pArchive, NuStraw* pStraw, - FILE* fp, ulong srcLen, ulong* pDstLen, ushort *pCrc) +static NuError Nu_CompressUncompressed(NuArchive* pArchive, NuStraw* pStraw, + FILE* fp, uint32_t srcLen, uint32_t* pDstLen, uint16_t *pCrc) { NuError err = kNuErrNone; - /*uchar* buffer = nil;*/ - ulong count, getsize; + /*uint8_t* buffer = NULL;*/ + uint32_t count, getsize; - Assert(pArchive != nil); - Assert(pStraw != nil); - Assert(fp != nil); + Assert(pArchive != NULL); + Assert(pStraw != NULL); + Assert(fp != NULL); Assert(srcLen > 0); *pDstLen = srcLen; /* get this over with */ @@ -33,7 +32,7 @@ Nu_CompressUncompressed(NuArchive* pArchive, NuStraw* pStraw, err = Nu_AllocCompressionBufferIFN(pArchive); BailError(err); - if (pCrc != nil) + if (pCrc != NULL) *pCrc = kNuInitialThreadCRC; count = srcLen; @@ -42,7 +41,7 @@ Nu_CompressUncompressed(NuArchive* pArchive, NuStraw* pStraw, err = Nu_StrawRead(pArchive, pStraw, pArchive->compBuf, getsize); BailError(err); - if (pCrc != nil) + if (pCrc != NULL) *pCrc = Nu_CalcCRC16(*pCrc, pArchive->compBuf, getsize); err = Nu_FWrite(fp, pArchive->compBuf, getsize); BailError(err); @@ -87,24 +86,23 @@ bail: * On exit, the output file will be positioned after the last byte of the * output. (For a pre-sized buffer, this may not be the desired result.) */ -NuError -Nu_CompressToArchive(NuArchive* pArchive, NuDataSource* pDataSource, +NuError Nu_CompressToArchive(NuArchive* pArchive, NuDataSource* pDataSource, NuThreadID threadID, NuThreadFormat sourceFormat, NuThreadFormat targetFormat, NuProgressData* pProgressData, FILE* dstFp, NuThread* pThread) { NuError err; long origOffset; - NuStraw* pStraw = nil; - NuDataSink* pDataSink = nil; - ulong srcLen = 0, dstLen = 0; - ushort threadCrc; + NuStraw* pStraw = NULL; + NuDataSink* pDataSink = NULL; + uint32_t srcLen = 0, dstLen = 0; + uint16_t threadCrc; - Assert(pArchive != nil); - Assert(pDataSource != nil); - /* okay if pProgressData is nil */ - Assert(dstFp != nil); - Assert(pThread != nil); + Assert(pArchive != NULL); + Assert(pDataSource != NULL); + /* okay if pProgressData is NULL */ + Assert(dstFp != NULL); + Assert(pThread != NULL); /* remember file offset, so we can back up if compression fails */ err = Nu_FTell(dstFp, &origOffset); @@ -116,7 +114,7 @@ Nu_CompressToArchive(NuArchive* pArchive, NuDataSource* pDataSource, pThread->thThreadClass = NuThreadIDGetClass(threadID); pThread->thThreadKind = NuThreadIDGetKind(threadID); - pThread->actualThreadEOF = (ulong)-1; + pThread->actualThreadEOF = (uint32_t)-1; /* nuThreadIdx and fileOffset should already be set */ /* @@ -168,7 +166,7 @@ Nu_CompressToArchive(NuArchive* pArchive, NuDataSource* pDataSource, if (pArchive->valMimicSHK && srcLen < kNuSHKLZWThreshold) targetFormat = kNuThreadFormatUncompressed; - if (pProgressData != nil) { + if (pProgressData != NULL) { if (targetFormat != kNuThreadFormatUncompressed) Nu_StrawSetProgressState(pStraw, kNuProgressCompressing); else @@ -245,7 +243,7 @@ Nu_CompressToArchive(NuArchive* pArchive, NuDataSource* pDataSource, BailError(err); err = Nu_StrawRewind(pArchive, pStraw); BailError(err); - if (pProgressData != nil) + if (pProgressData != NULL) Nu_StrawSetProgressState(pStraw, kNuProgressStoring); err = Nu_ProgressDataCompressPrep(pArchive, pStraw, kNuThreadFormatUncompressed, srcLen); @@ -262,7 +260,7 @@ Nu_CompressToArchive(NuArchive* pArchive, NuDataSource* pDataSource, * computed a CRC on the entire file (i.e. didn't stop early * when it noticed the output was larger than the input). If * this is always the case, then we can change "&threadCrc" - * a few lines back to "nil" and avoid re-computing the CRC. + * a few lines back to "NULL" and avoid re-computing the CRC. * If this is not always the case, remove this assert. */ Assert(threadCrc == pThread->thThreadCRC); @@ -276,14 +274,14 @@ Nu_CompressToArchive(NuArchive* pArchive, NuDataSource* pDataSource, /* * Copy the already-compressed input. */ - if (pProgressData != nil) + if (pProgressData != NULL) Nu_StrawSetProgressState(pStraw, kNuProgressCopying); err = Nu_ProgressDataCompressPrep(pArchive, pStraw, kNuThreadFormatUncompressed, srcLen); BailError(err); err = Nu_CompressUncompressed(pArchive, pStraw, dstFp, srcLen, - &dstLen, nil); + &dstLen, NULL); BailError(err); pThread->thThreadEOF = Nu_DataSourceGetOtherLen(pDataSource); @@ -298,7 +296,7 @@ done: srcLen, dstLen, pThread->actualThreadEOF)); /* make sure we send a final "success" progress message at 100% */ - if (pProgressData != nil) { + if (pProgressData != NULL) { (void) Nu_StrawSetProgressState(pStraw, kNuProgressDone); err = Nu_StrawSendProgressUpdate(pArchive, pStraw); BailError(err); @@ -322,14 +320,13 @@ bail: * will copy the data, and then continue writing zeros to fill out the rest * of the pre-sized buffer. */ -NuError -Nu_CopyPresizedToArchive(NuArchive* pArchive, NuDataSource* pDataSource, +NuError Nu_CopyPresizedToArchive(NuArchive* pArchive, NuDataSource* pDataSource, NuThreadID threadID, FILE* dstFp, NuThread* pThread, char** ppSavedCopy) { NuError err = kNuErrNone; - NuStraw* pStraw = nil; - ulong srcLen, bufferLen; - ulong count, getsize; + NuStraw* pStraw = NULL; + uint32_t srcLen, bufferLen; + uint32_t count, getsize; srcLen = Nu_DataSourceGetDataLen(pDataSource); bufferLen = Nu_DataSourceGetOtherLen(pDataSource); @@ -355,7 +352,7 @@ Nu_CopyPresizedToArchive(NuArchive* pArchive, NuDataSource* pDataSource, * is a convenient way to deal with the dataSource, even though we * don't have a progress updater. */ - err = Nu_StrawNew(pArchive, pDataSource, nil, &pStraw); + err = Nu_StrawNew(pArchive, pDataSource, NULL, &pStraw); BailError(err); count = srcLen; @@ -370,7 +367,7 @@ Nu_CopyPresizedToArchive(NuArchive* pArchive, NuDataSource* pDataSource, err = Nu_FWrite(dstFp, pArchive->compBuf, getsize); BailError(err); - if (ppSavedCopy != nil && *ppSavedCopy == nil) { + if (ppSavedCopy != NULL && *ppSavedCopy == NULL) { /* * Grab a copy of the filename for our own use. This assumes * that the filename fits in kNuGenCompBufSize, which is a diff --git a/nufxlib/Crc16.c b/nufxlib/Crc16.c index 15a0769..1f92397 100644 --- a/nufxlib/Crc16.c +++ b/nufxlib/Crc16.c @@ -7,7 +7,6 @@ * Compute 16-bit CRCs. Depending on the hardware, the table version * might be slower than the loop computation. */ -#define __Crc16_c__ 1 #include "NufxLibPriv.h" #define CRC_TAB @@ -30,7 +29,7 @@ /* crctab calculated by Mark G. Mendel, Network Systems Corporation */ -const ushort gNuCrc16Table[256] = { +const uint16_t gNuCrc16Table[256] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, @@ -83,13 +82,11 @@ const ushort gNuCrc16Table[256] = { * Depending on CPU architecture, one may be dramatically faster than * the other. */ -ushort -Nu_CalcCRC16(ushort seed, const uchar* ptr, int count) +uint16_t Nu_CalcCRC16(uint16_t seed, const uint8_t* ptr, int count) { - ushort CRC = seed; + uint16_t CRC = seed; #ifndef CRC_TAB int x; - Assert(sizeof(ushort) == 2); /* I think this is assumed */ #endif do { diff --git a/nufxlib/Debug.c b/nufxlib/Debug.c index eeb1daa..8a704f9 100644 --- a/nufxlib/Debug.c +++ b/nufxlib/Debug.c @@ -65,8 +65,7 @@ static const char* gFileSysIDs[] = { * * Returns "buffer" for the benefit of printf() calls. */ -static char* -Nu_DebugDumpDate(const NuDateTime* pDateTime, char* buffer) +static char* Nu_DebugDumpDate(const NuDateTime* pDateTime, char* buffer) { char* cp; @@ -118,8 +117,7 @@ bail: * * The result will be 2x the size of the original, +1 for a null byte. */ -static void -ConvertToHexStr(const uchar* inBuf, int inLen, char* outBuf) +static void ConvertToHexStr(const uint8_t* inBuf, int inLen, char* outBuf) { while (inLen--) { *outBuf++ = HexConv((*inBuf >> 4) & 0x0f); @@ -133,14 +131,13 @@ ConvertToHexStr(const uchar* inBuf, int inLen, char* outBuf) /* * Dump everything we know about pThread. */ -void -Nu_DebugDumpThread(const NuThread* pThread) +void Nu_DebugDumpThread(const NuThread* pThread) { static const char* kInd = " "; NuThreadID threadID; const char* descr; - Assert(pThread != nil); + Assert(pThread != NULL); printf("%sThreadClass: 0x%04x (%s)\n", kInd, pThread->thThreadClass, @@ -164,9 +161,9 @@ Nu_DebugDumpThread(const NuThread* pThread) printf("%sThreadKind: 0x%04x (%s)\n", kInd, pThread->thThreadKind, descr); - printf("%sThreadCRC: 0x%04x ThreadEOF: %lu CompThreadEOF: %lu\n", kInd, + printf("%sThreadCRC: 0x%04x ThreadEOF: %u CompThreadEOF: %u\n", kInd, pThread->thThreadCRC, pThread->thThreadEOF, pThread->thCompThreadEOF); - printf("%s*File data offset: %ld actualThreadEOF: %ld\n", kInd, + printf("%s*File data offset: %ld actualThreadEOF: %d\n", kInd, pThread->fileOffset, pThread->actualThreadEOF); } @@ -177,8 +174,7 @@ Nu_DebugDumpThread(const NuThread* pThread) * set. Pass in the "orig" copy in "pRecord", and optionally pass in the * "copy" set in "pXrefRecord" to glean data from both. */ -static void -Nu_DebugDumpRecord(NuArchive* pArchive, const NuRecord* pRecord, +static void Nu_DebugDumpRecord(NuArchive* pArchive, const NuRecord* pRecord, const NuRecord* pXrefRecord, Boolean isDeleted) { NuError err; /* dummy */ @@ -186,22 +182,24 @@ Nu_DebugDumpRecord(NuArchive* pArchive, const NuRecord* pRecord, char dateBuf[kNuDateOutputLen]; const NuThreadMod* pThreadMod; const NuThread* pThread; - ulong idx; + uint32_t idx; - Assert(pRecord != nil); + Assert(pRecord != NULL); /*printf("PTR: pRecord=0x%08lx pXrefRecord=0x%08lx\n", (long) pRecord, (long) pXrefRecord);*/ - printf("%s%s%sFilename: '%s' (idx=%lu)\n", kInd, + UNICHAR* filenameUNI = Nu_CopyMORToUNI(pRecord->filenameMOR); + printf("%s%s%sFilename: '%s' (idx=%u)\n", kInd, isDeleted ? "[DEL] " : "", - pXrefRecord != nil && pXrefRecord->pThreadMods != nil ? "[MOD] " : "", - pRecord->filename == nil ? "" : pRecord->filename, + pXrefRecord != NULL && pXrefRecord->pThreadMods != NULL ? "[MOD] " : "", + filenameUNI == NULL ? "" : filenameUNI, pRecord->recordIdx); + free(filenameUNI); printf("%sHeaderID: '%.4s' VersionNumber: 0x%04x HeaderCRC: 0x%04x\n", kInd, pRecord->recNufxID, pRecord->recVersionNumber, pRecord->recHeaderCRC); - printf("%sAttribCount: %u TotalThreads: %lu\n", kInd, + printf("%sAttribCount: %u TotalThreads: %u\n", kInd, pRecord->recAttribCount, pRecord->recTotalThreads); printf("%sFileSysID: %u (%s) FileSysInfo: 0x%04x ('%c')\n", kInd, pRecord->recFileSysID, @@ -209,7 +207,7 @@ Nu_DebugDumpRecord(NuArchive* pArchive, const NuRecord* pRecord, pRecord->recFileSysInfo, NuGetSepFromSysInfo(pRecord->recFileSysInfo)); /* do something fancy for ProDOS? */ - printf("%sFileType: 0x%08lx ExtraType: 0x%08lx Access: 0x%08lx\n", kInd, + printf("%sFileType: 0x%08x ExtraType: 0x%08x Access: 0x%08x\n", kInd, pRecord->recFileType, pRecord->recExtraType, pRecord->recAccess); printf("%sCreateWhen: %s\n", kInd, Nu_DebugDumpDate(&pRecord->recCreateWhen, dateBuf)); @@ -223,13 +221,13 @@ Nu_DebugDumpRecord(NuArchive* pArchive, const NuRecord* pRecord, if (pRecord->recOptionSize) { char* outBuf = Nu_Malloc(pArchive, pRecord->recOptionSize * 2 +1); BailAlloc(outBuf); - Assert(pRecord->recOptionList != nil); + Assert(pRecord->recOptionList != NULL); ConvertToHexStr(pRecord->recOptionList, pRecord->recOptionSize, outBuf); printf("%sOptionList: [%s]\n", kInd, outBuf); Nu_Free(pArchive, outBuf); } - printf("%s*ExtraCount: %ld RecFileOffset: %ld RecHeaderLength: %ld\n", + printf("%s*ExtraCount: %d RecFileOffset: %ld RecHeaderLength: %d\n", kInd, pRecord->extraCount, pRecord->fileOffset, pRecord->recHeaderLength); @@ -238,34 +236,34 @@ Nu_DebugDumpRecord(NuArchive* pArchive, const NuRecord* pRecord, isFake = (idx >= pRecord->recTotalThreads - pRecord->fakeThreads); pThread = Nu_GetThread(pRecord, idx); - Assert(pThread != nil); + Assert(pThread != NULL); - printf("%s--Thread #%lu (idx=%lu)%s\n", kInd, idx, pThread->threadIdx, + printf("%s--Thread #%u (idx=%u)%s\n", kInd, idx, pThread->threadIdx, isFake ? " [FAKE]" : ""); Nu_DebugDumpThread(pThread); } - if (pXrefRecord != nil) + if (pXrefRecord != NULL) pThreadMod = pXrefRecord->pThreadMods; else pThreadMod = pRecord->pThreadMods; /* probably empty */ - if (pThreadMod != nil) + if (pThreadMod != NULL) printf("%s*ThreadMods -----\n", kInd); - while (pThreadMod != nil) { + while (pThreadMod != NULL) { switch (pThreadMod->entry.kind) { case kNuThreadModAdd: - printf("%s *-ThreadMod ADD 0x%08lx 0x%04x (sourceType=%d)\n", kInd, + printf("%s *-ThreadMod ADD 0x%08x 0x%04x (sourceType=%d)\n", kInd, pThreadMod->entry.add.threadID, pThreadMod->entry.add.threadFormat, Nu_DataSourceGetType(pThreadMod->entry.add.pDataSource)); break; case kNuThreadModUpdate: - printf("%s *-ThreadMod UPDATE %6ld\n", kInd, + printf("%s *-ThreadMod UPDATE %6d\n", kInd, pThreadMod->entry.update.threadIdx); break; case kNuThreadModDelete: - printf("%s *-ThreadMod DELETE %6ld\n", kInd, + printf("%s *-ThreadMod DELETE %6d\n", kInd, pThreadMod->entry.delete.threadIdx); break; case kNuThreadModUnknown: @@ -280,7 +278,7 @@ Nu_DebugDumpRecord(NuArchive* pArchive, const NuRecord* pRecord, /*printf("%s*TotalLength: %ld TotalCompLength: %ld\n", kInd, pRecord->totalLength, pRecord->totalCompLength);*/ - printf("%s*TotalCompLength: %ld\n", kInd, pRecord->totalCompLength); + printf("%s*TotalCompLength: %u\n", kInd, pRecord->totalCompLength); printf("\n"); bail: @@ -290,9 +288,8 @@ bail: /* * Dump the records in a RecordSet. */ -static void -Nu_DebugDumpRecordSet(NuArchive* pArchive, const NuRecordSet* pRecordSet, - const NuRecordSet* pXrefSet) +static void Nu_DebugDumpRecordSet(NuArchive* pArchive, + const NuRecordSet* pRecordSet, const NuRecordSet* pXrefSet) { const NuRecord* pRecord; const NuRecord* pXrefRecord; @@ -300,8 +297,8 @@ Nu_DebugDumpRecordSet(NuArchive* pArchive, const NuRecordSet* pRecordSet, long count; doXref = false; - pXrefRecord = nil; - if (pXrefSet != nil && Nu_RecordSet_GetLoaded(pXrefSet)) { + pXrefRecord = NULL; + if (pXrefSet != NULL && Nu_RecordSet_GetLoaded(pXrefSet)) { pXrefRecord = Nu_RecordSet_GetListHead(pXrefSet); doXref = true; } @@ -309,18 +306,18 @@ Nu_DebugDumpRecordSet(NuArchive* pArchive, const NuRecordSet* pRecordSet, /* dump every record, if we've loaded them */ count = Nu_RecordSet_GetNumRecords(pRecordSet); pRecord = Nu_RecordSet_GetListHead(pRecordSet); - if (pRecord != nil) { + if (pRecord != NULL) { Assert(count != 0); while (count--) { - Assert(pRecord != nil); + Assert(pRecord != NULL); - if (pXrefRecord != nil && + if (pXrefRecord != NULL && pRecord->recordIdx == pXrefRecord->recordIdx) { Nu_DebugDumpRecord(pArchive, pRecord, pXrefRecord, false); pXrefRecord = pXrefRecord->pNext; } else { - Nu_DebugDumpRecord(pArchive, pRecord, nil, doXref); + Nu_DebugDumpRecord(pArchive, pRecord, NULL, doXref); } pRecord = pRecord->pNext; } @@ -332,22 +329,21 @@ Nu_DebugDumpRecordSet(NuArchive* pArchive, const NuRecordSet* pRecordSet, /* * Dump the master header block. */ -static void -Nu_DebugDumpMH(const NuMasterHeader* pMasterHeader) +static void Nu_DebugDumpMH(const NuMasterHeader* pMasterHeader) { static const char* kInd = " "; char dateBuf1[kNuDateOutputLen]; - Assert(pMasterHeader != nil); + Assert(pMasterHeader != NULL); - printf("%sNufileID: '%.6s' MasterCRC: 0x%04x TotalRecords: %lu\n", kInd, + printf("%sNufileID: '%.6s' MasterCRC: 0x%04x TotalRecords: %u\n", kInd, pMasterHeader->mhNufileID, pMasterHeader->mhMasterCRC, pMasterHeader->mhTotalRecords); printf("%sArchiveCreateWhen: %s\n", kInd, Nu_DebugDumpDate(&pMasterHeader->mhArchiveCreateWhen, dateBuf1)); printf("%sArchiveModWhen: %s\n", kInd, Nu_DebugDumpDate(&pMasterHeader->mhArchiveModWhen, dateBuf1)); - printf("%sMasterVersion: %u MasterEOF: %lu\n", kInd, + printf("%sMasterVersion: %u MasterEOF: %u\n", kInd, pMasterHeader->mhMasterVersion, pMasterHeader->mhMasterEOF); } @@ -359,20 +355,19 @@ Nu_DebugDumpMH(const NuMasterHeader* pMasterHeader) * the archive, then this won't be very interesting. This will never * show any records for streaming-mode archives. */ -void -Nu_DebugDumpAll(NuArchive* pArchive) +void Nu_DebugDumpAll(NuArchive* pArchive) { - Assert(pArchive != nil); + Assert(pArchive != NULL); - printf("*Archive pathname: '%s'\n", pArchive->archivePathname); + printf("*Archive pathname: '%s'\n", pArchive->archivePathnameUNI); printf("*Archive type: %d\n", pArchive->archiveType); printf("*Header offset: %ld (junk offset=%ld)\n", pArchive->headerOffset, pArchive->junkOffset); - printf("*Num records: %ld orig, %ld copy, %ld new\n", + printf("*Num records: %u orig, %u copy, %u new\n", Nu_RecordSet_GetNumRecords(&pArchive->origRecordSet), Nu_RecordSet_GetNumRecords(&pArchive->copyRecordSet), Nu_RecordSet_GetNumRecords(&pArchive->newRecordSet)); - printf("*NuRecordIdx seed: %lu NuRecordIdx next: %lu\n", + printf("*NuRecordIdx seed: %u NuRecordIdx next: %u\n", pArchive->recordIdxSeed, pArchive->nextRecordIdx); /* master header */ @@ -382,7 +377,7 @@ Nu_DebugDumpAll(NuArchive* pArchive) Nu_DebugDumpRecordSet(pArchive, &pArchive->origRecordSet, &pArchive->copyRecordSet); printf(" *NEW record set:\n"); - Nu_DebugDumpRecordSet(pArchive, &pArchive->newRecordSet, nil); + Nu_DebugDumpRecordSet(pArchive, &pArchive->newRecordSet, NULL); if (!Nu_RecordSet_GetLoaded(&pArchive->origRecordSet) && !Nu_RecordSet_GetLoaded(&pArchive->newRecordSet)) diff --git a/nufxlib/Deferred.c b/nufxlib/Deferred.c index fface90..c5f293d 100644 --- a/nufxlib/Deferred.c +++ b/nufxlib/Deferred.c @@ -23,16 +23,15 @@ * NOTE: threadFormat is how you want the data to be compressed. The * threadFormat passed to DataSource describes the source data. */ -NuError -Nu_ThreadModAdd_New(NuArchive* pArchive, NuThreadID threadID, +NuError Nu_ThreadModAdd_New(NuArchive* pArchive, NuThreadID threadID, NuThreadFormat threadFormat, NuDataSource* pDataSource, NuThreadMod** ppThreadMod) { - Assert(ppThreadMod != nil); - Assert(pDataSource != nil); + Assert(ppThreadMod != NULL); + Assert(pDataSource != NULL); *ppThreadMod = Nu_Calloc(pArchive, sizeof(**ppThreadMod)); - if (*ppThreadMod == nil) + if (*ppThreadMod == NULL) return kNuErrMalloc; (*ppThreadMod)->entry.kind = kNuThreadModAdd; @@ -53,15 +52,14 @@ Nu_ThreadModAdd_New(NuArchive* pArchive, NuThreadID threadID, * * Caller is allowed to dispose of the data source. */ -NuError -Nu_ThreadModUpdate_New(NuArchive* pArchive, NuThreadIdx threadIdx, +NuError Nu_ThreadModUpdate_New(NuArchive* pArchive, NuThreadIdx threadIdx, NuDataSource* pDataSource, NuThreadMod** ppThreadMod) { - Assert(ppThreadMod != nil); - Assert(pDataSource != nil); + Assert(ppThreadMod != NULL); + Assert(pDataSource != NULL); *ppThreadMod = Nu_Calloc(pArchive, sizeof(**ppThreadMod)); - if (*ppThreadMod == nil) + if (*ppThreadMod == NULL) return kNuErrMalloc; (*ppThreadMod)->entry.kind = kNuThreadModUpdate; @@ -78,14 +76,13 @@ Nu_ThreadModUpdate_New(NuArchive* pArchive, NuThreadIdx threadIdx, * The "threadID" argument is really only needed for filename threads. We * use it when trying to track how many filename threads we really have. */ -NuError -Nu_ThreadModDelete_New(NuArchive* pArchive, NuThreadIdx threadIdx, +NuError Nu_ThreadModDelete_New(NuArchive* pArchive, NuThreadIdx threadIdx, NuThreadID threadID, NuThreadMod** ppThreadMod) { - Assert(ppThreadMod != nil); + Assert(ppThreadMod != NULL); *ppThreadMod = Nu_Calloc(pArchive, sizeof(**ppThreadMod)); - if (*ppThreadMod == nil) + if (*ppThreadMod == NULL) return kNuErrMalloc; (*ppThreadMod)->entry.kind = kNuThreadModDelete; @@ -99,10 +96,9 @@ Nu_ThreadModDelete_New(NuArchive* pArchive, NuThreadIdx threadIdx, /* * Free a single NuThreadMod. */ -void -Nu_ThreadModFree(NuArchive* pArchive, NuThreadMod* pThreadMod) +void Nu_ThreadModFree(NuArchive* pArchive, NuThreadMod* pThreadMod) { - if (pThreadMod == nil) + if (pThreadMod == NULL) return; switch (pThreadMod->entry.kind) { @@ -131,13 +127,13 @@ Nu_ThreadModFree(NuArchive* pArchive, NuThreadMod* pThreadMod) * be more than three or four threads per record, so the extra search * isn't costly.) * - * Returns "nil" if nothing found. + * Returns "NULL" if nothing found. */ -NuThreadMod* -Nu_ThreadMod_FindByThreadIdx(const NuRecord* pRecord, NuThreadIdx threadIdx) +NuThreadMod* Nu_ThreadMod_FindByThreadIdx(const NuRecord* pRecord, + NuThreadIdx threadIdx) { NuThreadMod* pThreadMod; - NuThreadMod* pMatch = nil; + NuThreadMod* pMatch = NULL; pThreadMod = pRecord->pThreadMods; while (pThreadMod) { @@ -148,13 +144,13 @@ Nu_ThreadMod_FindByThreadIdx(const NuRecord* pRecord, NuThreadIdx threadIdx) break; case kNuThreadModUpdate: if (pThreadMod->entry.update.threadIdx == threadIdx) { - Assert(pMatch == nil); + Assert(pMatch == NULL); pMatch = pThreadMod; } break; case kNuThreadModDelete: if (pThreadMod->entry.delete.threadIdx == threadIdx) { - Assert(pMatch == nil); + Assert(pMatch == NULL); pMatch = pThreadMod; } break; @@ -178,17 +174,16 @@ Nu_ThreadMod_FindByThreadIdx(const NuRecord* pRecord, NuThreadIdx threadIdx) /* * Search for an "add" ThreadMod, by threadID. */ -NuError -Nu_ThreadModAdd_FindByThreadID(const NuRecord* pRecord, NuThreadID threadID, - NuThreadMod** ppThreadMod) +NuError Nu_ThreadModAdd_FindByThreadID(const NuRecord* pRecord, + NuThreadID threadID, NuThreadMod** ppThreadMod) { NuThreadMod* pThreadMod; - Assert(pRecord != nil); - Assert(ppThreadMod != nil); + Assert(pRecord != NULL); + Assert(ppThreadMod != NULL); pThreadMod = pRecord->pThreadMods; - while (pThreadMod != nil) { + while (pThreadMod != NULL) { if (pThreadMod->entry.kind != kNuThreadModAdd) continue; @@ -207,26 +202,25 @@ Nu_ThreadModAdd_FindByThreadID(const NuRecord* pRecord, NuThreadID threadID, /* * Free up the list of NuThreadMods in this record. */ -void -Nu_FreeThreadMods(NuArchive* pArchive, NuRecord* pRecord) +void Nu_FreeThreadMods(NuArchive* pArchive, NuRecord* pRecord) { NuThreadMod* pThreadMod; NuThreadMod* pNext; - Assert(pRecord != nil); + Assert(pRecord != NULL); pThreadMod = pRecord->pThreadMods; - if (pThreadMod == nil) + if (pThreadMod == NULL) return; - while (pThreadMod != nil) { + while (pThreadMod != NULL) { pNext = pThreadMod->pNext; Nu_ThreadModFree(pArchive, pThreadMod); pThreadMod = pNext; } - pRecord->pThreadMods = nil; + pRecord->pThreadMods = NULL; } @@ -246,9 +240,8 @@ typedef struct { /* * Allocate and initialize a NuNewThreads struct. */ -static NuError -Nu_NewThreads_New(NuArchive* pArchive, NuNewThreads** ppNewThreads, - long numThreads) +static NuError Nu_NewThreads_New(NuArchive* pArchive, + NuNewThreads** ppNewThreads, long numThreads) { NuError err = kNuErrNone; @@ -256,7 +249,8 @@ Nu_NewThreads_New(NuArchive* pArchive, NuNewThreads** ppNewThreads, BailAlloc(*ppNewThreads); (*ppNewThreads)->numThreads = numThreads; (*ppNewThreads)->nextSlot = 0; - (*ppNewThreads)->pThreads = Nu_Malloc(pArchive, numThreads * sizeof(NuThread)); + (*ppNewThreads)->pThreads = + Nu_Malloc(pArchive, numThreads * sizeof(NuThread)); BailAlloc((*ppNewThreads)->pThreads); bail: @@ -266,10 +260,9 @@ bail: /* * Free a NuNewThreads struct. */ -static void -Nu_NewThreads_Free(NuArchive* pArchive, NuNewThreads* pNewThreads) +static void Nu_NewThreads_Free(NuArchive* pArchive, NuNewThreads* pNewThreads) { - if (pNewThreads != nil) { + if (pNewThreads != NULL) { Nu_Free(pArchive, pNewThreads->pThreads); Nu_Free(pArchive, pNewThreads); } @@ -278,8 +271,7 @@ Nu_NewThreads_Free(NuArchive* pArchive, NuNewThreads* pNewThreads) /* * Returns true if "pNewThreads" has room for another entry, false otherwise. */ -static Boolean -Nu_NewThreads_HasRoom(const NuNewThreads* pNewThreads) +static Boolean Nu_NewThreads_HasRoom(const NuNewThreads* pNewThreads) { if (pNewThreads->nextSlot < pNewThreads->numThreads) return true; @@ -293,8 +285,8 @@ Nu_NewThreads_HasRoom(const NuNewThreads* pNewThreads) * * The "next slot" marker is automatically advanced. */ -static NuThread* -Nu_NewThreads_GetNext(NuNewThreads* pNewThreads, NuArchive* pArchive) +static NuThread* Nu_NewThreads_GetNext(NuNewThreads* pNewThreads, + NuArchive* pArchive) { NuThread* pThread; @@ -313,10 +305,9 @@ Nu_NewThreads_GetNext(NuNewThreads* pNewThreads, NuArchive* pArchive) /* * Return the #of threads we're meant to hold. */ -static int -Nu_NewThreads_GetNumThreads(const NuNewThreads* pNewThreads) +static int Nu_NewThreads_GetNumThreads(const NuNewThreads* pNewThreads) { - Assert(pNewThreads != nil); + Assert(pNewThreads != NULL); return pNewThreads->numThreads; } @@ -324,14 +315,13 @@ Nu_NewThreads_GetNumThreads(const NuNewThreads* pNewThreads) /* * Total up the compressed EOFs of all threads. */ -static ulong -Nu_NewThreads_TotalCompThreadEOF(NuNewThreads* pNewThreads) +static uint32_t Nu_NewThreads_TotalCompThreadEOF(NuNewThreads* pNewThreads) { - ulong compThreadEOF; + uint32_t compThreadEOF; int i; /* we should be all full up at this point; if not, we have a bug */ - Assert(pNewThreads != nil); + Assert(pNewThreads != NULL); Assert(pNewThreads->numThreads == pNewThreads->nextSlot); compThreadEOF = 0; @@ -347,12 +337,11 @@ Nu_NewThreads_TotalCompThreadEOF(NuNewThreads* pNewThreads) * to the thread array, and then nukes our copy of the pointer. This * allows us to transfer ownership of the storage to the caller. */ -static NuThread* -Nu_NewThreads_DonateThreads(NuNewThreads* pNewThreads) +static NuThread* Nu_NewThreads_DonateThreads(NuNewThreads* pNewThreads) { NuThread* pThreads = pNewThreads->pThreads; - pNewThreads->pThreads = nil; + pNewThreads->pThreads = NULL; return pThreads; } @@ -369,8 +358,7 @@ Nu_NewThreads_DonateThreads(NuNewThreads* pNewThreads) * * Pass in the record from the *copy* set, not the original. */ -static NuError -Nu_CopyArchiveRecord(NuArchive* pArchive, NuRecord* pRecord) +static NuError Nu_CopyArchiveRecord(NuArchive* pArchive, NuRecord* pRecord) { NuError err = kNuErrNone; long offsetAdjust; @@ -401,7 +389,7 @@ Nu_CopyArchiveRecord(NuArchive* pArchive, NuRecord* pRecord) } Assert(outputOffset + pRecord->recHeaderLength + pRecord->totalCompLength == - (ulong)ftell(pArchive->tmpFp)); + (uint32_t)ftell(pArchive->tmpFp)); Assert(pRecord->fileOffset == outputOffset); bail: @@ -414,9 +402,8 @@ bail: * * Returns -1 on error. */ -static NuError -Nu_CountEventualThreads(const NuRecord* pRecord, long* pTotalThreads, - long* pFilenameThreads) +static NuError Nu_CountEventualThreads(const NuRecord* pRecord, + long* pTotalThreads, long* pFilenameThreads) { const NuThreadMod* pThreadMod; const NuThread* pThread; @@ -433,7 +420,7 @@ Nu_CountEventualThreads(const NuRecord* pRecord, long* pTotalThreads, numFilenameThreads = 0; pThreadMod = pRecord->pThreadMods; - while (pThreadMod != nil) { + while (pThreadMod != NULL) { switch (pThreadMod->entry.kind) { case kNuThreadModAdd: numThreads++; @@ -463,7 +450,7 @@ Nu_CountEventualThreads(const NuRecord* pRecord, long* pTotalThreads, */ for (idx = 0; idx < (long)pRecord->recTotalThreads; idx++) { pThread = Nu_GetThread(pRecord, idx); - Assert(pThread != nil); + Assert(pThread != NULL); if (NuGetThreadID(pThread) == kNuThreadIDFilename) numFilenameThreads++; @@ -497,18 +484,17 @@ Nu_CountEventualThreads(const NuRecord* pRecord, long* pTotalThreads, * we ignore, are marked "used" during processing, so we don't need * to be terribly bright here. */ -static Boolean -Nu_VerifyAllTouched(NuArchive* pArchive, const NuRecord* pRecord) +static Boolean Nu_VerifyAllTouched(NuArchive* pArchive, const NuRecord* pRecord) { const NuThreadMod* pThreadMod; const NuThread* pThread; long idx; - Assert(pArchive != nil); - Assert(pRecord != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); pThreadMod = pRecord->pThreadMods; - while (pThreadMod != nil) { + while (pThreadMod != NULL) { Assert(pThreadMod->entry.generic.used == false || pThreadMod->entry.generic.used == true); if (!pThreadMod->entry.generic.used) @@ -518,7 +504,7 @@ Nu_VerifyAllTouched(NuArchive* pArchive, const NuRecord* pRecord) for (idx = 0; idx < (long)pRecord->recTotalThreads; idx++) { pThread = Nu_GetThread(pRecord, idx); - Assert(pThread != nil); + Assert(pThread != NULL); Assert(pThread->used == false || pThread->used == true); if (!pThread->used) @@ -536,17 +522,17 @@ Nu_VerifyAllTouched(NuArchive* pArchive, const NuRecord* pRecord) * This call should only be made after an "add" or "update" threadMod has * successfully completed. * - * "newName" must be allocated storage. + * "newName" must be allocated storage, Mac OS Roman charset. */ -static void -Nu_SetNewThreadFilename(NuArchive* pArchive, NuRecord* pRecord, char* newName) +static void Nu_SetNewThreadFilename(NuArchive* pArchive, NuRecord* pRecord, + char* newNameMOR) { - Assert(pRecord != nil); - Assert(newName != nil); + Assert(pRecord != NULL); + Assert(newNameMOR != NULL); - Nu_Free(pArchive, pRecord->threadFilename); - pRecord->threadFilename = newName; - pRecord->filename = pRecord->threadFilename; + Nu_Free(pArchive, pRecord->threadFilenameMOR); + pRecord->threadFilenameMOR = newNameMOR; + pRecord->filenameMOR = pRecord->threadFilenameMOR; } /* @@ -556,8 +542,8 @@ Nu_SetNewThreadFilename(NuArchive* pArchive, NuRecord* pRecord, char* newName) * appropriate values, we'll set them on their behalf, so long as * the uncompressed size is a multiple of 512. */ -static NuError -Nu_UpdateDiskImageFields(NuArchive* pArchive, NuRecord* pRecord, long sourceLen) +static NuError Nu_UpdateDiskImageFields(NuArchive* pArchive, NuRecord* pRecord, + long sourceLen) { NuError err = kNuErrNone; long actualLen; @@ -599,14 +585,13 @@ Nu_UpdateDiskImageFields(NuArchive* pArchive, NuRecord* pRecord, long sourceLen) * * Possible side-effects on "pRecord": threadFilename may be updated. */ -static NuError -Nu_ConstructArchiveUpdate(NuArchive* pArchive, FILE* fp, NuRecord* pRecord, - NuThread* pThread, const NuThreadMod* pThreadMod) +static NuError Nu_ConstructArchiveUpdate(NuArchive* pArchive, FILE* fp, + NuRecord* pRecord, NuThread* pThread, const NuThreadMod* pThreadMod) { NuError err; - NuDataSource* pDataSource = nil; - ulong sourceLen; - ulong threadBufSize; + NuDataSource* pDataSource = NULL; + uint32_t sourceLen; + uint32_t threadBufSize; /* * We're going to copy the data out of the data source. Because @@ -621,7 +606,7 @@ Nu_ConstructArchiveUpdate(NuArchive* pArchive, FILE* fp, NuRecord* pRecord, Assert(pThread->thCompThreadEOF >= pThread->thThreadEOF); threadBufSize = pThread->thCompThreadEOF; pDataSource = pThreadMod->entry.update.pDataSource; - Assert(pDataSource != nil); + Assert(pDataSource != NULL); err = Nu_DataSourcePrepareInput(pArchive, pDataSource); if (err == kNuErrSkipped) { @@ -645,7 +630,7 @@ Nu_ConstructArchiveUpdate(NuArchive* pArchive, FILE* fp, NuRecord* pRecord, sourceLen = Nu_DataSourceGetDataLen(pDataSource); if (sourceLen > pThread->thCompThreadEOF) { err = kNuErrPreSizeOverflow; - Nu_ReportError(NU_BLOB, err, "can't fit %ld bytes into %ld-byte buffer", + Nu_ReportError(NU_BLOB, err, "can't fit %u bytes into %u-byte buffer", sourceLen, pThread->thCompThreadEOF); goto bail; } @@ -662,24 +647,24 @@ Nu_ConstructArchiveUpdate(NuArchive* pArchive, FILE* fp, NuRecord* pRecord, if (NuGetThreadID(pThread) == kNuThreadIDFilename) { /* special handling for filename updates */ - char* savedCopy = nil; + char* savedCopyMOR = NULL; err = Nu_CopyPresizedToArchive(pArchive, pDataSource, - NuGetThreadID(pThread), fp, pThread, &savedCopy); + NuGetThreadID(pThread), fp, pThread, &savedCopyMOR); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, "thread update failed"); goto bail; } - Nu_SetNewThreadFilename(pArchive, pRecord, savedCopy); + Nu_SetNewThreadFilename(pArchive, pRecord, savedCopyMOR); } else { err = Nu_CopyPresizedToArchive(pArchive, pDataSource, - NuGetThreadID(pThread), fp, pThread, nil); + NuGetThreadID(pThread), fp, pThread, NULL); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, "thread update failed"); goto bail; } } - Assert((ulong)ftell(fp) == pThread->fileOffset + threadBufSize); + Assert((uint32_t)ftell(fp) == pThread->fileOffset + threadBufSize); skip_update: Nu_DataSourceUnPrepareInput(pArchive, pDataSource); @@ -705,8 +690,7 @@ bail: * Possible side-effects on "pRecord": disk image fields may be revised * (storage type, extra type), and threadFilename may be updated. */ -static NuError -Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, +static NuError Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, NuThreadID threadID, Boolean doKeepFirstOnly, NuNewThreads* pNewThreads, FILE* dstFp) { @@ -716,6 +700,7 @@ Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, NuProgressData* pProgressData; NuThreadMod* pThreadMod; NuThread* pNewThread; + UNICHAR* pathnameUNIStorage = NULL; Boolean foundOne = false; /* @@ -723,7 +708,7 @@ Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, * matching by wildcards, but don't re-use "used" entries. */ pThreadMod = pRecord->pThreadMods; - while (pThreadMod != nil) { + while (pThreadMod != NULL) { if (pThreadMod->entry.kind == kNuThreadModAdd && !pThreadMod->entry.generic.used && (pThreadMod->entry.add.threadID == threadID || @@ -747,7 +732,7 @@ Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, } /* if this is a data thread, prepare the progress message */ - pProgressData = nil; + pProgressData = NULL; if (NuThreadIDGetClass(pThreadMod->entry.add.threadID) == kNuThreadClassData) { @@ -763,17 +748,19 @@ Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, * Do something different here for data sinks with * filenames attached. ++ATM 2003/02/17] */ + pathnameUNIStorage = Nu_CopyMORToUNI(pRecord->filenameMOR); if (Nu_DataSourceGetType(pThreadMod->entry.add.pDataSource) == kNuDataSourceFromFile) { /* use on-disk filename */ err = Nu_ProgressDataInit_Compress(pArchive, &progressData, pRecord, Nu_DataSourceFile_GetPathname( - pThreadMod->entry.add.pDataSource)); + pThreadMod->entry.add.pDataSource), + pathnameUNIStorage); } else { /* use archive filename for both */ err = Nu_ProgressDataInit_Compress(pArchive, &progressData, - pRecord, pRecord->filename); + pRecord, pathnameUNIStorage, pathnameUNIStorage); } BailError(err); @@ -804,7 +791,7 @@ Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, if (pThreadMod->entry.add.threadID == kNuThreadIDDiskImage) { const NuDataSource* pDataSource = pThreadMod->entry.add.pDataSource; - ulong uncompLen; + uint32_t uncompLen; if (Nu_DataSourceGetThreadFormat(pDataSource) == kNuThreadFormatUncompressed) @@ -836,14 +823,14 @@ Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, if (pThreadMod->entry.add.threadID == kNuThreadIDFilename) { /* filenames are special */ - char* savedCopy = nil; + char* savedCopyMOR = NULL; Assert(pThreadMod->entry.add.threadFormat == kNuThreadFormatUncompressed); err = Nu_CopyPresizedToArchive(pArchive, pThreadMod->entry.add.pDataSource, pThreadMod->entry.add.threadID, - dstFp, pNewThread, &savedCopy); + dstFp, pNewThread, &savedCopyMOR); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, "fn thread add failed"); goto bail; @@ -854,7 +841,7 @@ Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, just clear it, because we've already made space for the record header, and didn't include the filename in it. */ - Nu_SetNewThreadFilename(pArchive, pRecord, savedCopy); + Nu_SetNewThreadFilename(pArchive, pRecord, savedCopyMOR); } else if (pThreadMod->entry.add.isPresized) { /* don't compress, just copy */ @@ -863,7 +850,7 @@ Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, err = Nu_CopyPresizedToArchive(pArchive, pThreadMod->entry.add.pDataSource, pThreadMod->entry.add.threadID, - dstFp, pNewThread, nil); + dstFp, pNewThread, NULL); /* fall through with err */ } else { @@ -890,6 +877,7 @@ Nu_HandleAddThreadMods(NuArchive* pArchive, NuRecord* pRecord, } bail: + Nu_Free(pArchive, pathnameUNIStorage); return err; } @@ -911,9 +899,9 @@ bail: * * "pRecord" must be from the "copy" data set. */ -static NuError -Nu_ConstructArchiveThreads(NuArchive* pArchive, NuRecord* pRecord, - NuThreadID threadID, Boolean doKeepFirstOnly, NuNewThreads* pNewThreads) +static NuError Nu_ConstructArchiveThreads(NuArchive* pArchive, + NuRecord* pRecord, NuThreadID threadID, Boolean doKeepFirstOnly, + NuNewThreads* pNewThreads) { NuError err = kNuErrNone; NuThread* pThread; @@ -929,7 +917,7 @@ Nu_ConstructArchiveThreads(NuArchive* pArchive, NuRecord* pRecord, */ for (idx = 0; idx < (int)pRecord->recTotalThreads; idx++) { pThread = Nu_GetThread(pRecord, idx); - Assert(pThread != nil); + Assert(pThread != NULL); DBUG(("+++ THREAD #%d (used=%d)\n", idx, pThread->used)); if (threadID == kNuThreadIDWildcard || @@ -944,7 +932,7 @@ Nu_ConstructArchiveThreads(NuArchive* pArchive, NuRecord* pRecord, pThreadMod = Nu_ThreadMod_FindByThreadIdx(pRecord, pThread->threadIdx); - if (pThreadMod != nil) { + if (pThreadMod != NULL) { /* * The thread has a related ThreadMod. Deal with it. */ @@ -1056,18 +1044,17 @@ bail: * kNuErrSkipped, which should cause the caller to simply copy the * previous record. */ -static NuError -Nu_ConstructArchiveRecord(NuArchive* pArchive, NuRecord* pRecord) +static NuError Nu_ConstructArchiveRecord(NuArchive* pArchive, NuRecord* pRecord) { NuError err; - NuNewThreads* pNewThreads = nil; + NuNewThreads* pNewThreads = NULL; long threadDisp; long initialOffset, finalOffset; long numThreads, numFilenameThreads; int newHeaderSize; - Assert(pArchive != nil); - Assert(pRecord != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); DBUG(("--- Reconstructing '%s'\n", pRecord->filename)); @@ -1098,24 +1085,25 @@ Nu_ConstructArchiveRecord(NuArchive* pArchive, NuRecord* pRecord) /* * Handle filename deletion. */ - if (!numFilenameThreads && pRecord->threadFilename) { + if (!numFilenameThreads && pRecord->threadFilenameMOR != NULL) { /* looks like a previously existing filename thread got removed */ - DBUG(("--- Dropping thread filename '%s'\n", pRecord->threadFilename)); - if (pRecord->filename == pRecord->threadFilename) - pRecord->filename = nil; /* don't point at freed memory! */ - Nu_Free(pArchive, pRecord->threadFilename); - pRecord->threadFilename = nil; + DBUG(("--- Dropping thread filename '%s'\n", + pRecord->threadFilenameMOR)); + if (pRecord->filenameMOR == pRecord->threadFilenameMOR) + pRecord->filenameMOR = NULL; /* don't point at freed memory! */ + Nu_Free(pArchive, pRecord->threadFilenameMOR); + pRecord->threadFilenameMOR = NULL; /* I don't think this is possible, but check it anyway */ - if (pRecord->filename == nil && pRecord->recFilename != nil && + if (pRecord->filenameMOR == NULL && pRecord->recFilenameMOR != NULL && !pRecord->dropRecFilename) { DBUG(("--- HEY, how did this happen?\n")); - pRecord->filename = pRecord->recFilename; + pRecord->filenameMOR = pRecord->recFilenameMOR; } } - if (pRecord->filename == nil) - pRecord->filename = kNuDefaultRecordName; + if (pRecord->filenameMOR == NULL) + pRecord->filenameMOR = kNuDefaultRecordName; /* * Make a hole, including the header filename if we're not dropping it. @@ -1261,19 +1249,19 @@ bail: * not the entire operation, we rewind the temp file to the initial * position and return kNuErrSkipped. */ -static NuError -Nu_ConstructNewRecord(NuArchive* pArchive, NuRecord* pRecord, FILE* fp) +static NuError Nu_ConstructNewRecord(NuArchive* pArchive, NuRecord* pRecord, + FILE* fp) { NuError err; - NuNewThreads* pNewThreads = nil; + NuNewThreads* pNewThreads = NULL; NuThreadMod* pThreadMod; long threadDisp; long initialOffset, finalOffset; long numThreadMods, numFilenameThreads; int newHeaderSize; - Assert(pArchive != nil); - Assert(pRecord != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); DBUG(("--- Constructing '%s'\n", pRecord->filename)); @@ -1287,7 +1275,7 @@ Nu_ConstructNewRecord(NuArchive* pArchive, NuRecord* pRecord, FILE* fp) * make ourselves useful by counting up the number of eventual * threads, and verify that there is exactly one filename thread. */ - Assert(pRecord->pThreads == nil); + Assert(pRecord->pThreads == NULL); numThreadMods = 0; numFilenameThreads = 0; @@ -1312,21 +1300,21 @@ Nu_ConstructNewRecord(NuArchive* pArchive, NuRecord* pRecord, FILE* fp) * records when the application doesn't explicitly add a thread. */ if (!numFilenameThreads) { - NuDataSource* pTmpDataSource = nil; - NuThreadMod* pNewThreadMod = nil; + NuDataSource* pTmpDataSource = NULL; + NuThreadMod* pNewThreadMod = NULL; int len, maxLen; /* * Generally speaking, the "add file" call should set the * filename. If somehow it didn't, assign a default. */ - if (pRecord->filename == nil) { - pRecord->newFilename = strdup(kNuDefaultRecordName); - pRecord->filename = pRecord->newFilename; + if (pRecord->filenameMOR == NULL) { + pRecord->newFilenameMOR = strdup(kNuDefaultRecordName); + pRecord->filenameMOR = pRecord->newFilenameMOR; } DBUG(("--- No filename thread found, adding one ('%s')\n", - pRecord->filename)); + pRecord->filenameMOR)); /* * Create a trivial data source for the filename. The size of @@ -1335,12 +1323,12 @@ Nu_ConstructNewRecord(NuArchive* pArchive, NuRecord* pRecord, FILE* fp) * (If we're really serious about renaming it, maybe we should * leave some extra space on the end...?) */ - len = strlen(pRecord->filename); + len = strlen(pRecord->filenameMOR); maxLen = len > kNuDefaultFilenameThreadSize ? len : kNuDefaultFilenameThreadSize; err = Nu_DataSourceBuffer_New(kNuThreadFormatUncompressed, - maxLen, (const uchar*)pRecord->filename, 0, - strlen(pRecord->filename), nil, &pTmpDataSource); + maxLen, (const uint8_t*)pRecord->filenameMOR, 0, + strlen(pRecord->filenameMOR), NULL, &pTmpDataSource); BailError(err); /* put in a new "add" threadMod (which copies the data source) */ @@ -1351,7 +1339,7 @@ Nu_ConstructNewRecord(NuArchive* pArchive, NuRecord* pRecord, FILE* fp) /* add it to the list */ Nu_RecordAddThreadMod(pRecord, pNewThreadMod); - pNewThreadMod = nil; + pNewThreadMod = NULL; numFilenameThreads++; numThreadMods++; @@ -1431,7 +1419,7 @@ Nu_ConstructNewRecord(NuArchive* pArchive, NuRecord* pRecord, FILE* fp) /* * Install pNewThreads as the thread list. */ - Assert(pRecord->pThreads == nil && pRecord->recTotalThreads == 0); + Assert(pRecord->pThreads == NULL && pRecord->recTotalThreads == 0); pRecord->pThreads = Nu_NewThreads_DonateThreads(pNewThreads); pRecord->recTotalThreads = Nu_NewThreads_GetNumThreads(pNewThreads); @@ -1490,8 +1478,7 @@ bail: * The position of pArchive->archiveFp on entry and on exit is not * defined. */ -static NuError -Nu_UpdateRecordInOriginal(NuArchive* pArchive, NuRecord* pRecord) +static NuError Nu_UpdateRecordInOriginal(NuArchive* pArchive, NuRecord* pRecord) { NuError err = kNuErrNone; NuThread* pThread; @@ -1501,7 +1488,7 @@ Nu_UpdateRecordInOriginal(NuArchive* pArchive, NuRecord* pRecord) * Loop through all threadMods. */ pThreadMod = pRecord->pThreadMods; - while (pThreadMod != nil) { + while (pThreadMod != NULL) { Assert(pThreadMod->entry.kind == kNuThreadModUpdate); /* find the thread associated with this threadMod */ @@ -1547,7 +1534,7 @@ Nu_UpdateRecordInOriginal(NuArchive* pArchive, NuRecord* pRecord) DBUG(("--- record header wrote %ld bytes\n", pArchive->currentOffset - pRecord->fileOffset)); pThread = pRecord->pThreads; - if (pThread != nil && pArchive->currentOffset != pThread->fileOffset) { + if (pThread != NULL && pArchive->currentOffset != pThread->fileOffset) { /* guess what, we just trashed the archive */ err = kNuErrDamaged; Nu_ReportError(NU_BLOB, err, @@ -1583,8 +1570,7 @@ bail: * * On exit, pArchive->tmpFp will point at the archive EOF. */ -static NuError -Nu_CreateTempFromOriginal(NuArchive* pArchive) +static NuError Nu_CreateTempFromOriginal(NuArchive* pArchive) { NuError err = kNuErrNone; NuRecord* pRecord; @@ -1631,8 +1617,8 @@ Nu_CreateTempFromOriginal(NuArchive* pArchive) * record header, but since all we do is copy the data anyway, * it's not much slower. */ - while (pRecord != nil) { - if (!pRecord->dirtyHeader && pRecord->pThreadMods == nil) { + while (pRecord != NULL) { + if (!pRecord->dirtyHeader && pRecord->pThreadMods == NULL) { err = Nu_CopyArchiveRecord(pArchive, pRecord); BailError(err); } else { @@ -1671,8 +1657,7 @@ bail: * * On exit, pArchive->archiveFp will point at the archive EOF. */ -static NuError -Nu_UpdateInOriginal(NuArchive* pArchive) +static NuError Nu_UpdateInOriginal(NuArchive* pArchive) { NuError err = kNuErrNone; NuRecord* pRecord; @@ -1690,8 +1675,8 @@ Nu_UpdateInOriginal(NuArchive* pArchive) * Run through and process all the updates. */ pRecord = Nu_RecordSet_GetListHead(&pArchive->copyRecordSet); - while (pRecord != nil) { - if (pRecord->dirtyHeader || pRecord->pThreadMods != nil) { + while (pRecord != NULL) { + if (pRecord->dirtyHeader || pRecord->pThreadMods != NULL) { err = Nu_UpdateRecordInOriginal(pArchive, pRecord); BailError(err); } @@ -1717,14 +1702,13 @@ bail: * * On completion, "fp" will point at the end of the archive. */ -static NuError -Nu_CreateNewRecords(NuArchive* pArchive, FILE* fp) +static NuError Nu_CreateNewRecords(NuArchive* pArchive, FILE* fp) { NuError err = kNuErrNone; NuRecord* pRecord; pRecord = Nu_RecordSet_GetListHead(&pArchive->newRecordSet); - while (pRecord != nil) { + while (pRecord != NULL) { err = Nu_ConstructNewRecord(pArchive, pRecord, fp); if (err == kNuErrSkipped) { /* @@ -1770,8 +1754,7 @@ bail: * At present, a "dirtyHeader" flag is not of itself cause to rebuild * the archive, so we don't test for it here. */ -static Boolean -Nu_NoHeavyUpdates(NuArchive* pArchive) +static Boolean Nu_NoHeavyUpdates(NuArchive* pArchive) { const NuRecord* pRecord; long count; @@ -1801,10 +1784,10 @@ Nu_NoHeavyUpdates(NuArchive* pArchive) while (count--) { const NuThreadMod* pThreadMod; - Assert(pRecord != nil); + Assert(pRecord != NULL); pThreadMod = pRecord->pThreadMods; - while (pThreadMod != nil) { + while (pThreadMod != NULL) { /* the only acceptable kind is "update" */ if (pThreadMod->entry.kind != kNuThreadModUpdate) return false; @@ -1828,31 +1811,31 @@ Nu_NoHeavyUpdates(NuArchive* pArchive) * it from the number of threads, skipping on if the record has any * "add" thread mods. */ -static NuError -Nu_PurgeEmptyRecords(NuArchive* pArchive, NuRecordSet* pRecordSet) +static NuError Nu_PurgeEmptyRecords(NuArchive* pArchive, + NuRecordSet* pRecordSet) { NuError err = kNuErrNone; NuRecord* pRecord; NuRecord** ppRecord; - Assert(pArchive != nil); - Assert(pRecordSet != nil); + Assert(pArchive != NULL); + Assert(pRecordSet != NULL); if (Nu_RecordSet_IsEmpty(pRecordSet)) return kNuErrNone; ppRecord = Nu_RecordSet_GetListHeadPtr(pRecordSet); - Assert(ppRecord != nil); - Assert(*ppRecord != nil); + Assert(ppRecord != NULL); + Assert(*ppRecord != NULL); /* maintain a pointer to the pointer, so we can delete easily */ - while (*ppRecord != nil) { + while (*ppRecord != NULL) { pRecord = *ppRecord; if (Nu_RecordIsEmpty(pArchive, pRecord)) { DBUG(("--- Purging empty record %06ld '%s' (0x%08lx-->0x%08lx)\n", pRecord->recordIdx, pRecord->filename, - (ulong)ppRecord, (ulong)pRecord)); + (uint32_t)ppRecord, (uint32_t)pRecord)); err = Nu_RecordSet_DeleteRecordPtr(pArchive, pRecordSet, ppRecord); BailError(err); /* pRecord is now invalid, and *ppRecord has been updated */ @@ -1873,8 +1856,8 @@ bail: * Pass in a correctly positioned "fp" and the total length of the archive * file. */ -static NuError -Nu_UpdateMasterHeader(NuArchive* pArchive, FILE* fp, long archiveEOF) +static NuError Nu_UpdateMasterHeader(NuArchive* pArchive, FILE* fp, + long archiveEOF) { NuError err; long numRecords; @@ -1914,8 +1897,7 @@ bail: /* * Reset the temp file to a known (empty) state. */ -static NuError -Nu_ResetTempFile(NuArchive* pArchive) +static NuError Nu_ResetTempFile(NuArchive* pArchive) { NuError err = kNuErrNone; @@ -1923,11 +1905,11 @@ Nu_ResetTempFile(NuArchive* pArchive) if (Nu_IsReadOnly(pArchive)) return kNuErrNone; /* or kNuErrArchiveRO? */ - Assert(pArchive != nil); - Assert(pArchive->tmpPathname != nil); + Assert(pArchive != NULL); + Assert(pArchive->tmpPathnameUNI != NULL); #if 0 /* keep the temp file around for examination */ -if (pArchive->tmpFp != nil) { +if (pArchive->tmpFp != NULL) { DBUG(("--- NOT Resetting temp file\n")); fflush(pArchive->tmpFp); goto bail; @@ -1937,12 +1919,15 @@ if (pArchive->tmpFp != nil) { DBUG(("--- Resetting temp file\n")); /* if we renamed the temp over the original, we need to open a new temp */ - if (pArchive->tmpFp == nil) { - pArchive->tmpFp = fopen(pArchive->tmpPathname, kNuFileOpenReadWriteCreat); - if (pArchive->tmpFp == nil) { + if (pArchive->tmpFp == NULL) { + // as in Nu_OpenTempFile, skip the wchar conversion for the temp + // file name, which we lazily assume to be ASCII + pArchive->tmpFp = fopen(pArchive->tmpPathnameUNI, + kNuFileOpenReadWriteCreat); + if (pArchive->tmpFp == NULL) { err = errno ? errno : kNuErrFileOpen; Nu_ReportError(NU_BLOB, errno, "Unable to open temp file '%s'", - pArchive->tmpPathname); + pArchive->tmpPathnameUNI); goto bail; } } else { @@ -1956,19 +1941,20 @@ if (pArchive->tmpFp != nil) { /* do it the hard way if we don't have ftruncate or equivalent */ err = kNuErrNone; fclose(pArchive->tmpFp); - pArchive->tmpFp = fopen(pArchive->tmpPathname, kNuFileOpenWriteTrunc); - if (pArchive->tmpFp == nil) { + pArchive->tmpFp = fopen(pArchive->tmpPathnameUNI, + kNuFileOpenWriteTrunc); + if (pArchive->tmpFp == NULL) { err = errno ? errno : kNuErrFileOpen; Nu_ReportError(NU_BLOB, err, "failed truncating tmp file"); goto bail; } fclose(pArchive->tmpFp); - pArchive->tmpFp = - fopen(pArchive->tmpPathname, kNuFileOpenReadWriteCreat); - if (pArchive->tmpFp == nil) { + pArchive->tmpFp = fopen(pArchive->tmpPathnameUNI, + kNuFileOpenReadWriteCreat); + if (pArchive->tmpFp == NULL) { err = errno ? errno : kNuErrFileOpen; Nu_ReportError(NU_BLOB, err, "Unable to open temp file '%s'", - pArchive->tmpPathname); + pArchive->tmpPathnameUNI); goto bail; } } @@ -1984,14 +1970,13 @@ bail: * don't even exist. This is done as we are cleaning up the record sets * after a successful (or aborted) update. */ -static NuError -Nu_RecordResetUsedFlags(NuArchive* pArchive, NuRecord* pRecord) +static NuError Nu_RecordResetUsedFlags(NuArchive* pArchive, NuRecord* pRecord) { NuThread* pThread; long idx; - Assert(pArchive != nil); - Assert(pRecord != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); /* these should already be clear */ if (pRecord->pThreadMods) { @@ -2002,7 +1987,7 @@ Nu_RecordResetUsedFlags(NuArchive* pArchive, NuRecord* pRecord) /* these might still be set */ for (idx = 0; idx < (long)pRecord->recTotalThreads; idx++) { pThread = Nu_GetThread(pRecord, idx); - Assert(pThread != nil); + Assert(pThread != NULL); pThread->used = false; } @@ -2016,14 +2001,13 @@ Nu_RecordResetUsedFlags(NuArchive* pArchive, NuRecord* pRecord) /* * Invoke Nu_RecordResetUsedFlags on all records in a record set. */ -static NuError -Nu_ResetUsedFlags(NuArchive* pArchive, NuRecordSet* pRecordSet) +static NuError Nu_ResetUsedFlags(NuArchive* pArchive, NuRecordSet* pRecordSet) { NuError err = kNuErrNone; NuRecord* pRecord; pRecord = Nu_RecordSet_GetListHead(pRecordSet); - while (pRecord != nil) { + while (pRecord != NULL) { err = Nu_RecordResetUsedFlags(pArchive, pRecord); if (err != kNuErrNone) { Assert(0); @@ -2040,8 +2024,7 @@ Nu_ResetUsedFlags(NuArchive* pArchive, NuRecordSet* pRecordSet) /* * If nothing in the "copy" set has actually been disturbed, throw it out. */ -static void -Nu_ResetCopySetIfUntouched(NuArchive* pArchive) +static void Nu_ResetCopySetIfUntouched(NuArchive* pArchive) { const NuRecord* pRecord; @@ -2054,8 +2037,8 @@ Nu_ResetCopySetIfUntouched(NuArchive* pArchive) /* do we have any thread mods or dirty record headers? */ pRecord = Nu_RecordSet_GetListHead(&pArchive->copyRecordSet); - while (pRecord != nil) { - if (pRecord->pThreadMods != nil || pRecord->dirtyHeader) + while (pRecord != NULL) { + if (pRecord->pThreadMods != NULL || pRecord->dirtyHeader) return; pRecord = pRecord->pNext; @@ -2071,14 +2054,13 @@ Nu_ResetCopySetIfUntouched(NuArchive* pArchive) * GSHK always adds a comment to the first new record added to an archive. * Imitate this behavior. */ -static NuError -Nu_AddCommentToFirstNewRecord(NuArchive* pArchive) +static NuError Nu_AddCommentToFirstNewRecord(NuArchive* pArchive) { NuError err = kNuErrNone; NuRecord* pRecord; - NuThreadMod* pThreadMod = nil; - NuThreadMod* pExistingThreadMod = nil; - NuDataSource* pDataSource = nil; + NuThreadMod* pThreadMod = NULL; + NuThreadMod* pExistingThreadMod = NULL; + NuDataSource* pDataSource = NULL; /* if there aren't any records there, skip this */ if (Nu_RecordSet_IsEmpty(&pArchive->newRecordSet)) @@ -2100,20 +2082,20 @@ Nu_AddCommentToFirstNewRecord(NuArchive* pArchive) /* create a new data source with nothing in it */ err = Nu_DataSourceBuffer_New(kNuThreadFormatUncompressed, - kNuDefaultCommentSize, nil, 0, 0, nil, &pDataSource); + kNuDefaultCommentSize, NULL, 0, 0, NULL, &pDataSource); BailError(err); - Assert(pDataSource != nil); + Assert(pDataSource != NULL); /* create a new ThreadMod */ err = Nu_ThreadModAdd_New(pArchive, kNuThreadIDComment, kNuThreadFormatUncompressed, pDataSource, &pThreadMod); BailError(err); - Assert(pThreadMod != nil); - /*pDataSource = nil;*/ /* ThreadModAdd_New makes a copy */ + Assert(pThreadMod != NULL); + /*pDataSource = NULL;*/ /* ThreadModAdd_New makes a copy */ /* add the thread mod to the record */ Nu_RecordAddThreadMod(pRecord, pThreadMod); - pThreadMod = nil; /* don't free on exit */ + pThreadMod = NULL; /* don't free on exit */ bail: Nu_ThreadModFree(pArchive, pThreadMod); @@ -2137,8 +2119,7 @@ bail: * If the things this function is doing aren't making any sense at all, * read "NOTES.txt" for an introduction. */ -NuError -Nu_Flush(NuArchive* pArchive, long* pStatusFlags) +NuError Nu_Flush(NuArchive* pArchive, uint32_t* pStatusFlags) { NuError err = kNuErrNone; Boolean canAbort = true; @@ -2148,7 +2129,7 @@ Nu_Flush(NuArchive* pArchive, long* pStatusFlags) DBUG(("--- FLUSH\n")); - if (pStatusFlags == nil) + if (pStatusFlags == NULL) return kNuErrInvalidArg; /* these do get set on error, so clear them no matter what */ *pStatusFlags = 0; @@ -2410,42 +2391,43 @@ Nu_Flush(NuArchive* pArchive, long* pStatusFlags) *pStatusFlags |= kNuFlushSucceeded; /* temp file is fully valid */ fclose(pArchive->archiveFp); - pArchive->archiveFp = nil; + pArchive->archiveFp = NULL; err = Nu_DeleteArchiveFile(pArchive); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, "unable to remove original archive"); Nu_ReportError(NU_BLOB, kNuErrNone, "New data is in '%s'", - pArchive->tmpPathname); + pArchive->tmpPathnameUNI); *pStatusFlags |= kNuFlushInaccessible; goto bail_reopen; /* must re-open archiveFp */ } fclose(pArchive->tmpFp); - pArchive->tmpFp = nil; + pArchive->tmpFp = NULL; err = Nu_RenameTempToArchive(pArchive); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, "unable to rename temp file"); Nu_ReportError(NU_BLOB, kNuErrNone, - "NOTE: only copy of archive is in '%s'", pArchive->tmpPathname); + "NOTE: only copy of archive is in '%s'", + pArchive->tmpPathnameUNI); /* maintain Entry.c semantics (and keep them from removing temp) */ - Nu_Free(pArchive, pArchive->archivePathname); - pArchive->archivePathname = nil; - Nu_Free(pArchive, pArchive->tmpPathname); - pArchive->tmpPathname = nil; + Nu_Free(pArchive, pArchive->archivePathnameUNI); + pArchive->archivePathnameUNI = NULL; + Nu_Free(pArchive, pArchive->tmpPathnameUNI); + pArchive->tmpPathnameUNI = NULL; /* bail will put us into read-only mode, which is what we want */ goto bail; } bail_reopen: - pArchive->archiveFp = fopen(pArchive->archivePathname, + pArchive->archiveFp = fopen(pArchive->archivePathnameUNI, kNuFileOpenReadWrite); - if (pArchive->archiveFp == nil) { + if (pArchive->archiveFp == NULL) { err = errno ? errno : -1; Nu_ReportError(NU_BLOB, err, "unable to reopen archive file '%s' after rename", - pArchive->archivePathname); + pArchive->archivePathnameUNI); *pStatusFlags |= kNuFlushInaccessible; goto bail; /* the Entry.c funcs will obstruct further use */ } @@ -2545,8 +2527,8 @@ bail: /* last-minute sanity check */ Assert(pArchive->origRecordSet.numRecords == 0 || - (pArchive->origRecordSet.nuRecordHead != nil && - pArchive->origRecordSet.nuRecordTail != nil)); + (pArchive->origRecordSet.nuRecordHead != NULL && + pArchive->origRecordSet.nuRecordTail != NULL)); return err; } @@ -2555,10 +2537,9 @@ bail: /* * Abort any pending changes. */ -NuError -Nu_Abort(NuArchive* pArchive) +NuError Nu_Abort(NuArchive* pArchive) { - Assert(pArchive != nil); + Assert(pArchive != NULL); if (Nu_IsReadOnly(pArchive)) return kNuErrArchiveRO; diff --git a/nufxlib/Deflate.c b/nufxlib/Deflate.c index c5050ec..6a0e979 100644 --- a/nufxlib/Deflate.c +++ b/nufxlib/Deflate.c @@ -25,13 +25,11 @@ /* * Alloc and free functions provided to zlib. */ -static voidpf -Nu_zalloc(voidpf opaque, uInt items, uInt size) +static voidpf Nu_zalloc(voidpf opaque, uInt items, uInt size) { return Nu_Malloc(opaque, items * size); } -static void -Nu_zfree(voidpf opaque, voidpf address) +static void Nu_zfree(voidpf opaque, voidpf address) { Nu_Free(opaque, address); } @@ -46,21 +44,20 @@ Nu_zfree(voidpf opaque, voidpf address) /* * Compress "srcLen" bytes from "pStraw" to "fp". */ -NuError -Nu_CompressDeflate(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc) +NuError Nu_CompressDeflate(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc) { NuError err = kNuErrNone; z_stream zstream; int zerr; - Bytef* outbuf = nil; + Bytef* outbuf = NULL; - Assert(pArchive != nil); - Assert(pStraw != nil); - Assert(fp != nil); + Assert(pArchive != NULL); + Assert(pStraw != NULL); + Assert(fp != NULL); Assert(srcLen > 0); - Assert(pDstLen != nil); - Assert(pCrc != nil); + Assert(pDstLen != NULL); + Assert(pCrc != NULL); err = Nu_AllocCompressionBufferIFN(pArchive); if (err != kNuErrNone) @@ -76,7 +73,7 @@ Nu_CompressDeflate(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, zstream.zalloc = Nu_zalloc; zstream.zfree = Nu_zfree; zstream.opaque = pArchive; - zstream.next_in = nil; + zstream.next_in = NULL; zstream.avail_in = 0; zstream.next_out = outbuf; zstream.avail_out = kNuGenCompBufSize; @@ -100,7 +97,7 @@ Nu_CompressDeflate(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, * Loop while we have data. */ do { - ulong getSize; + uint32_t getSize; int flush; /* should be able to read a full buffer every time */ @@ -159,7 +156,7 @@ z_bail: deflateEnd(&zstream); /* free up any allocated structures */ bail: - if (outbuf != nil) + if (outbuf != NULL) free(outbuf); return err; } @@ -174,20 +171,19 @@ bail: /* * Expand from "infp" to "pFunnel". */ -NuError -Nu_ExpandDeflate(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pCrc) +NuError Nu_ExpandDeflate(NuArchive* pArchive, const NuRecord* pRecord, + const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, uint16_t* pCrc) { NuError err = kNuErrNone; z_stream zstream; int zerr; - ulong compRemaining; + uint32_t compRemaining; Bytef* outbuf; - Assert(pArchive != nil); - Assert(pThread != nil); - Assert(infp != nil); - Assert(pFunnel != nil); + Assert(pArchive != NULL); + Assert(pThread != NULL); + Assert(infp != NULL); + Assert(pFunnel != NULL); err = Nu_AllocCompressionBufferIFN(pArchive); if (err != kNuErrNone) @@ -205,7 +201,7 @@ Nu_ExpandDeflate(NuArchive* pArchive, const NuRecord* pRecord, zstream.zalloc = Nu_zalloc; zstream.zfree = Nu_zfree; zstream.opaque = pArchive; - zstream.next_in = nil; + zstream.next_in = NULL; zstream.avail_in = 0; zstream.next_out = outbuf; zstream.avail_out = kNuGenCompBufSize; @@ -229,7 +225,7 @@ Nu_ExpandDeflate(NuArchive* pArchive, const NuRecord* pRecord, * Loop while we have data. */ do { - ulong getSize; + uint32_t getSize; /* read as much as we can */ if (zstream.avail_in == 0) { @@ -269,7 +265,7 @@ Nu_ExpandDeflate(NuArchive* pArchive, const NuRecord* pRecord, goto z_bail; } - if (pCrc != nil) + if (pCrc != NULL) *pCrc = Nu_CalcCRC16(*pCrc, outbuf, zstream.next_out - outbuf); zstream.next_out = outbuf; @@ -282,7 +278,7 @@ Nu_ExpandDeflate(NuArchive* pArchive, const NuRecord* pRecord, if (zstream.total_out != pThread->actualThreadEOF) { err = kNuErrBadData; Nu_ReportError(NU_BLOB, err, - "size mismatch on inflated file (%ld vs %ld)", + "size mismatch on inflated file (%ld vs %u)", zstream.total_out, pThread->actualThreadEOF); goto z_bail; } @@ -291,7 +287,7 @@ z_bail: inflateEnd(&zstream); /* free up any allocated structures */ bail: - if (outbuf != nil) + if (outbuf != NULL) free(outbuf); return err; } diff --git a/nufxlib/Entry.c b/nufxlib/Entry.c index f068e30..e9f0fd3 100644 --- a/nufxlib/Entry.c +++ b/nufxlib/Entry.c @@ -24,8 +24,7 @@ * it does not follow all sorts of crazy semaphore semantics. If you * have the need, go ahead and fix it. */ -static inline void -Nu_SetBusy(NuArchive* pArchive) +static inline void Nu_SetBusy(NuArchive* pArchive) { pArchive->busy = true; } @@ -33,8 +32,7 @@ Nu_SetBusy(NuArchive* pArchive) /* * Clear the busy flag. */ -static inline void -Nu_ClearBusy(NuArchive* pArchive) +static inline void Nu_ClearBusy(NuArchive* pArchive) { pArchive->busy = false; } @@ -45,13 +43,11 @@ Nu_ClearBusy(NuArchive* pArchive) * can be made during callback functions when the archive isn't fully * consistent. */ -static NuError -Nu_PartiallyValidateNuArchive(const NuArchive* pArchive) +static NuError Nu_PartiallyValidateNuArchive(const NuArchive* pArchive) { - if (pArchive == nil) + if (pArchive == NULL) return kNuErrInvalidArg; - pArchive = pArchive; if (pArchive->structMagic != kNuArchiveStructMagic) return kNuErrBadStruct; @@ -61,8 +57,7 @@ Nu_PartiallyValidateNuArchive(const NuArchive* pArchive) /* * Validate the NuArchive* argument passed in to us. */ -static NuError -Nu_ValidateNuArchive(const NuArchive* pArchive) +static NuError Nu_ValidateNuArchive(const NuArchive* pArchive) { NuError err; @@ -77,19 +72,19 @@ Nu_ValidateNuArchive(const NuArchive* pArchive) /* make sure the TOC state is consistent */ if (pArchive->haveToc) { if (pArchive->masterHeader.mhTotalRecords != 0) - Assert(Nu_RecordSet_GetListHead(&pArchive->origRecordSet) != nil); + Assert(Nu_RecordSet_GetListHead(&pArchive->origRecordSet) != NULL); Assert(Nu_RecordSet_GetNumRecords(&pArchive->origRecordSet) == pArchive->masterHeader.mhTotalRecords); } else { - Assert(Nu_RecordSet_GetListHead(&pArchive->origRecordSet) == nil); + Assert(Nu_RecordSet_GetListHead(&pArchive->origRecordSet) == NULL); } /* make sure we have open files to work with */ - Assert(pArchive->archivePathname == nil || pArchive->archiveFp != nil); - if (pArchive->archivePathname != nil && pArchive->archiveFp == nil) + Assert(pArchive->archivePathnameUNI == NULL || pArchive->archiveFp != NULL); + if (pArchive->archivePathnameUNI != NULL && pArchive->archiveFp == NULL) return kNuErrInternal; - Assert(pArchive->tmpPathname == nil || pArchive->tmpFp != nil); - if (pArchive->tmpPathname != nil && pArchive->tmpFp == nil) + Assert(pArchive->tmpPathnameUNI == NULL || pArchive->tmpFp != NULL); + if (pArchive->tmpPathnameUNI != NULL && pArchive->tmpFp == NULL) return kNuErrInternal; /* further validations */ @@ -104,12 +99,11 @@ Nu_ValidateNuArchive(const NuArchive* pArchive) * =========================================================================== */ -NUFXLIB_API NuError -NuStreamOpenRO(FILE* infp, NuArchive** ppArchive) +NUFXLIB_API NuError NuStreamOpenRO(FILE* infp, NuArchive** ppArchive) { NuError err; - if (infp == nil || ppArchive == nil) + if (infp == NULL || ppArchive == NULL) return kNuErrInvalidArg; err = Nu_StreamOpenRO(infp, (NuArchive**) ppArchive); @@ -117,8 +111,7 @@ NuStreamOpenRO(FILE* infp, NuArchive** ppArchive) return err; } -NUFXLIB_API NuError -NuContents(NuArchive* pArchive, NuCallback contentFunc) +NUFXLIB_API NuError NuContents(NuArchive* pArchive, NuCallback contentFunc) { NuError err; @@ -134,8 +127,7 @@ NuContents(NuArchive* pArchive, NuCallback contentFunc) return err; } -NUFXLIB_API NuError -NuExtract(NuArchive* pArchive) +NUFXLIB_API NuError NuExtract(NuArchive* pArchive) { NuError err; @@ -151,8 +143,7 @@ NuExtract(NuArchive* pArchive) return err; } -NUFXLIB_API NuError -NuTest(NuArchive* pArchive) +NUFXLIB_API NuError NuTest(NuArchive* pArchive) { NuError err; @@ -168,8 +159,7 @@ NuTest(NuArchive* pArchive) return err; } -NUFXLIB_API NuError -NuTestRecord(NuArchive* pArchive, NuRecordIdx recordIdx) +NUFXLIB_API NuError NuTestRecord(NuArchive* pArchive, NuRecordIdx recordIdx) { NuError err; @@ -189,18 +179,17 @@ NuTestRecord(NuArchive* pArchive, NuRecordIdx recordIdx) * =========================================================================== */ -NUFXLIB_API NuError -NuOpenRO(const char* filename, NuArchive** ppArchive) +NUFXLIB_API NuError NuOpenRO(const UNICHAR* archivePathnameUNI, + NuArchive** ppArchive) { NuError err; - err = Nu_OpenRO(filename, (NuArchive**) ppArchive); + err = Nu_OpenRO(archivePathnameUNI, (NuArchive**) ppArchive); return err; } -NUFXLIB_API NuError -NuExtractRecord(NuArchive* pArchive, NuRecordIdx recordIdx) +NUFXLIB_API NuError NuExtractRecord(NuArchive* pArchive, NuRecordIdx recordIdx) { NuError err; @@ -213,8 +202,7 @@ NuExtractRecord(NuArchive* pArchive, NuRecordIdx recordIdx) return err; } -NUFXLIB_API NuError -NuExtractThread(NuArchive* pArchive, NuThreadIdx threadIdx, +NUFXLIB_API NuError NuExtractThread(NuArchive* pArchive, NuThreadIdx threadIdx, NuDataSink* pDataSink) { NuError err; @@ -228,8 +216,7 @@ NuExtractThread(NuArchive* pArchive, NuThreadIdx threadIdx, return err; } -NUFXLIB_API NuError -NuGetRecord(NuArchive* pArchive, NuRecordIdx recordIdx, +NUFXLIB_API NuError NuGetRecord(NuArchive* pArchive, NuRecordIdx recordIdx, const NuRecord** ppRecord) { NuError err; @@ -243,23 +230,21 @@ NuGetRecord(NuArchive* pArchive, NuRecordIdx recordIdx, return err; } -NUFXLIB_API NuError -NuGetRecordIdxByName(NuArchive* pArchive, const char* name, - NuRecordIdx* pRecordIdx) +NUFXLIB_API NuError NuGetRecordIdxByName(NuArchive* pArchive, + const char* nameMOR, NuRecordIdx* pRecordIdx) { NuError err; if ((err = Nu_ValidateNuArchive(pArchive)) == kNuErrNone) { Nu_SetBusy(pArchive); - err = Nu_GetRecordIdxByName(pArchive, name, pRecordIdx); + err = Nu_GetRecordIdxByName(pArchive, nameMOR, pRecordIdx); Nu_ClearBusy(pArchive); } return err; } -NUFXLIB_API NuError -NuGetRecordIdxByPosition(NuArchive* pArchive, unsigned long position, +NUFXLIB_API NuError NuGetRecordIdxByPosition(NuArchive* pArchive, uint32_t position, NuRecordIdx* pRecordIdx) { NuError err; @@ -280,20 +265,18 @@ NuGetRecordIdxByPosition(NuArchive* pArchive, unsigned long position, * =========================================================================== */ -NUFXLIB_API NuError -NuOpenRW(const char* archivePathname, const char* tmpPathname, - unsigned long flags, NuArchive** ppArchive) +NUFXLIB_API NuError NuOpenRW(const UNICHAR* archivePathnameUNI, + const UNICHAR* tmpPathnameUNI, uint32_t flags, NuArchive** ppArchive) { NuError err; - err = Nu_OpenRW(archivePathname, tmpPathname, flags, + err = Nu_OpenRW(archivePathnameUNI, tmpPathnameUNI, flags, (NuArchive**) ppArchive); return err; } -NUFXLIB_API NuError -NuFlush(NuArchive* pArchive, long* pStatusFlags) +NUFXLIB_API NuError NuFlush(NuArchive* pArchive, uint32_t* pStatusFlags) { NuError err; @@ -306,8 +289,7 @@ NuFlush(NuArchive* pArchive, long* pStatusFlags) return err; } -NUFXLIB_API NuError -NuAbort(NuArchive* pArchive) +NUFXLIB_API NuError NuAbort(NuArchive* pArchive) { NuError err; @@ -320,24 +302,22 @@ NuAbort(NuArchive* pArchive) return err; } -NUFXLIB_API NuError -NuAddRecord(NuArchive* pArchive, const NuFileDetails* pFileDetails, - NuRecordIdx* pRecordIdx) +NUFXLIB_API NuError NuAddRecord(NuArchive* pArchive, + const NuFileDetails* pFileDetails, NuRecordIdx* pRecordIdx) { NuError err; if ((err = Nu_ValidateNuArchive(pArchive)) == kNuErrNone) { Nu_SetBusy(pArchive); - err = Nu_AddRecord(pArchive, pFileDetails, pRecordIdx, nil); + err = Nu_AddRecord(pArchive, pFileDetails, pRecordIdx, NULL); Nu_ClearBusy(pArchive); } return err; } -NUFXLIB_API NuError -NuAddThread(NuArchive* pArchive, NuRecordIdx recordIdx, NuThreadID threadID, - NuDataSource* pDataSource, NuThreadIdx* pThreadIdx) +NUFXLIB_API NuError NuAddThread(NuArchive* pArchive, NuRecordIdx recordIdx, + NuThreadID threadID, NuDataSource* pDataSource, NuThreadIdx* pThreadIdx) { NuError err; @@ -351,8 +331,7 @@ NuAddThread(NuArchive* pArchive, NuRecordIdx recordIdx, NuThreadID threadID, return err; } -NUFXLIB_API NuError -NuAddFile(NuArchive* pArchive, const char* pathname, +NUFXLIB_API NuError NuAddFile(NuArchive* pArchive, const UNICHAR* pathnameUNI, const NuFileDetails* pFileDetails, short isFromRsrcFork, NuRecordIdx* pRecordIdx) { @@ -360,7 +339,7 @@ NuAddFile(NuArchive* pArchive, const char* pathname, if ((err = Nu_ValidateNuArchive(pArchive)) == kNuErrNone) { Nu_SetBusy(pArchive); - err = Nu_AddFile(pArchive, pathname, pFileDetails, + err = Nu_AddFile(pArchive, pathnameUNI, pFileDetails, (Boolean)(isFromRsrcFork != 0), pRecordIdx); Nu_ClearBusy(pArchive); } @@ -368,15 +347,14 @@ NuAddFile(NuArchive* pArchive, const char* pathname, return err; } -NUFXLIB_API NuError -NuRename(NuArchive* pArchive, NuRecordIdx recordIdx, const char* pathname, - char fssep) +NUFXLIB_API NuError NuRename(NuArchive* pArchive, NuRecordIdx recordIdx, + const char* pathnameMOR, char fssep) { NuError err; if ((err = Nu_ValidateNuArchive(pArchive)) == kNuErrNone) { Nu_SetBusy(pArchive); - err = Nu_Rename(pArchive, recordIdx, pathname, fssep); + err = Nu_Rename(pArchive, recordIdx, pathnameMOR, fssep); Nu_ClearBusy(pArchive); } @@ -384,8 +362,7 @@ NuRename(NuArchive* pArchive, NuRecordIdx recordIdx, const char* pathname, } -NUFXLIB_API NuError -NuSetRecordAttr(NuArchive* pArchive, NuRecordIdx recordIdx, +NUFXLIB_API NuError NuSetRecordAttr(NuArchive* pArchive, NuRecordIdx recordIdx, const NuRecordAttr* pRecordAttr) { NuError err; @@ -399,9 +376,8 @@ NuSetRecordAttr(NuArchive* pArchive, NuRecordIdx recordIdx, return err; } -NUFXLIB_API NuError -NuUpdatePresizedThread(NuArchive* pArchive, NuThreadIdx threadIdx, - NuDataSource* pDataSource, long* pMaxLen) +NUFXLIB_API NuError NuUpdatePresizedThread(NuArchive* pArchive, + NuThreadIdx threadIdx, NuDataSource* pDataSource, int32_t* pMaxLen) { NuError err; @@ -415,8 +391,7 @@ NuUpdatePresizedThread(NuArchive* pArchive, NuThreadIdx threadIdx, return err; } -NUFXLIB_API NuError -NuDelete(NuArchive* pArchive) +NUFXLIB_API NuError NuDelete(NuArchive* pArchive) { NuError err; @@ -429,8 +404,7 @@ NuDelete(NuArchive* pArchive) return err; } -NUFXLIB_API NuError -NuDeleteRecord(NuArchive* pArchive, NuRecordIdx recordIdx) +NUFXLIB_API NuError NuDeleteRecord(NuArchive* pArchive, NuRecordIdx recordIdx) { NuError err; @@ -443,8 +417,7 @@ NuDeleteRecord(NuArchive* pArchive, NuRecordIdx recordIdx) return err; } -NUFXLIB_API NuError -NuDeleteThread(NuArchive* pArchive, NuThreadIdx threadIdx) +NUFXLIB_API NuError NuDeleteThread(NuArchive* pArchive, NuThreadIdx threadIdx) { NuError err; @@ -464,8 +437,7 @@ NuDeleteThread(NuArchive* pArchive, NuThreadIdx threadIdx) * =========================================================================== */ -NUFXLIB_API NuError -NuClose(NuArchive* pArchive) +NUFXLIB_API NuError NuClose(NuArchive* pArchive) { NuError err; @@ -480,8 +452,8 @@ NuClose(NuArchive* pArchive) return err; } -NUFXLIB_API NuError -NuGetMasterHeader(NuArchive* pArchive, const NuMasterHeader** ppMasterHeader) +NUFXLIB_API NuError NuGetMasterHeader(NuArchive* pArchive, + const NuMasterHeader** ppMasterHeader) { NuError err; @@ -491,12 +463,11 @@ NuGetMasterHeader(NuArchive* pArchive, const NuMasterHeader** ppMasterHeader) return err; } -NUFXLIB_API NuError -NuGetExtraData(NuArchive* pArchive, void** ppData) +NUFXLIB_API NuError NuGetExtraData(NuArchive* pArchive, void** ppData) { NuError err; - if (ppData == nil) + if (ppData == NULL) return kNuErrInvalidArg; if ((err = Nu_PartiallyValidateNuArchive(pArchive)) == kNuErrNone) *ppData = pArchive->extraData; @@ -504,8 +475,7 @@ NuGetExtraData(NuArchive* pArchive, void** ppData) return err; } -NUFXLIB_API NuError -NuSetExtraData(NuArchive* pArchive, void* pData) +NUFXLIB_API NuError NuSetExtraData(NuArchive* pArchive, void* pData) { NuError err; @@ -515,8 +485,8 @@ NuSetExtraData(NuArchive* pArchive, void* pData) return err; } -NUFXLIB_API NuError -NuGetValue(NuArchive* pArchive, NuValueID ident, NuValue* pValue) +NUFXLIB_API NuError NuGetValue(NuArchive* pArchive, NuValueID ident, + NuValue* pValue) { NuError err; @@ -526,8 +496,8 @@ NuGetValue(NuArchive* pArchive, NuValueID ident, NuValue* pValue) return err; } -NUFXLIB_API NuError -NuSetValue(NuArchive* pArchive, NuValueID ident, NuValue value) +NUFXLIB_API NuError NuSetValue(NuArchive* pArchive, NuValueID ident, + NuValue value) { NuError err; @@ -537,8 +507,8 @@ NuSetValue(NuArchive* pArchive, NuValueID ident, NuValue value) return err; } -NUFXLIB_API NuError -NuGetAttr(NuArchive* pArchive, NuAttrID ident, NuAttr* pAttr) +NUFXLIB_API NuError NuGetAttr(NuArchive* pArchive, NuAttrID ident, + NuAttr* pAttr) { NuError err; @@ -548,8 +518,7 @@ NuGetAttr(NuArchive* pArchive, NuAttrID ident, NuAttr* pAttr) return err; } -NUFXLIB_API NuError -NuDebugDumpArchive(NuArchive* pArchive) +NUFXLIB_API NuError NuDebugDumpArchive(NuArchive* pArchive) { #if defined(DEBUG_MSGS) /* skip validation checks for this one */ @@ -568,82 +537,75 @@ NuDebugDumpArchive(NuArchive* pArchive) * =========================================================================== */ -NUFXLIB_API NuError -NuCreateDataSourceForFile(NuThreadFormat threadFormat, - unsigned long otherLen, const char* pathname, short isFromRsrcFork, +NUFXLIB_API NuError NuCreateDataSourceForFile(NuThreadFormat threadFormat, + uint32_t otherLen, const UNICHAR* pathnameUNI, short isFromRsrcFork, NuDataSource** ppDataSource) { return Nu_DataSourceFile_New(threadFormat, otherLen, - pathname, (Boolean)(isFromRsrcFork != 0), ppDataSource); + pathnameUNI, (Boolean)(isFromRsrcFork != 0), ppDataSource); } -NUFXLIB_API NuError -NuCreateDataSourceForFP(NuThreadFormat threadFormat, - unsigned long otherLen, FILE* fp, long offset, long length, +NUFXLIB_API NuError NuCreateDataSourceForFP(NuThreadFormat threadFormat, + uint32_t otherLen, FILE* fp, long offset, long length, NuCallback fcloseFunc, NuDataSource** ppDataSource) { return Nu_DataSourceFP_New(threadFormat, otherLen, fp, offset, length, fcloseFunc, ppDataSource); } -NUFXLIB_API NuError -NuCreateDataSourceForBuffer(NuThreadFormat threadFormat, - unsigned long otherLen, const unsigned char* buffer, long offset, +NUFXLIB_API NuError NuCreateDataSourceForBuffer(NuThreadFormat threadFormat, + uint32_t otherLen, const uint8_t* buffer, long offset, long length, NuCallback freeFunc, NuDataSource** ppDataSource) { return Nu_DataSourceBuffer_New(threadFormat, otherLen, buffer, offset, length, freeFunc, ppDataSource); } -NUFXLIB_API NuError -NuFreeDataSource(NuDataSource* pDataSource) +NUFXLIB_API NuError NuFreeDataSource(NuDataSource* pDataSource) { return Nu_DataSourceFree(pDataSource); } -NUFXLIB_API NuError -NuDataSourceSetRawCrc(NuDataSource* pDataSource, unsigned short crc) +NUFXLIB_API NuError NuDataSourceSetRawCrc(NuDataSource* pDataSource, + uint16_t crc) { - if (pDataSource == nil) + if (pDataSource == NULL) return kNuErrInvalidArg; Nu_DataSourceSetRawCrc(pDataSource, crc); return kNuErrNone; } -NUFXLIB_API NuError -NuCreateDataSinkForFile(short doExpand, NuValue convertEOL, - const char* pathname, char fssep, NuDataSink** ppDataSink) +NUFXLIB_API NuError NuCreateDataSinkForFile(short doExpand, NuValue convertEOL, + const UNICHAR* pathnameUNI, UNICHAR fssep, NuDataSink** ppDataSink) { - return Nu_DataSinkFile_New((Boolean)(doExpand != 0), convertEOL, pathname, - fssep, ppDataSink); + return Nu_DataSinkFile_New((Boolean)(doExpand != 0), convertEOL, + pathnameUNI, fssep, ppDataSink); } -NUFXLIB_API NuError -NuCreateDataSinkForFP(short doExpand, NuValue convertEOL, FILE* fp, - NuDataSink** ppDataSink) +NUFXLIB_API NuError NuCreateDataSinkForFP(short doExpand, NuValue convertEOL, + FILE* fp, NuDataSink** ppDataSink) { return Nu_DataSinkFP_New((Boolean)(doExpand != 0), convertEOL, fp, ppDataSink); } -NUFXLIB_API NuError -NuCreateDataSinkForBuffer(short doExpand, NuValue convertEOL, - unsigned char* buffer, unsigned long bufLen, NuDataSink** ppDataSink) +NUFXLIB_API NuError NuCreateDataSinkForBuffer(short doExpand, + NuValue convertEOL, uint8_t* buffer, uint32_t bufLen, + NuDataSink** ppDataSink) { return Nu_DataSinkBuffer_New((Boolean)(doExpand != 0), convertEOL, buffer, bufLen, ppDataSink); } -NUFXLIB_API NuError -NuFreeDataSink(NuDataSink* pDataSink) +NUFXLIB_API NuError NuFreeDataSink(NuDataSink* pDataSink) { return Nu_DataSinkFree(pDataSink); } -NUFXLIB_API NuError -NuDataSinkGetOutCount(NuDataSink* pDataSink, ulong* pOutCount) +NUFXLIB_API NuError NuDataSinkGetOutCount(NuDataSink* pDataSink, + uint32_t* pOutCount) { - if (pDataSink == nil || pOutCount == nil) + if (pDataSink == NULL || pOutCount == NULL) return kNuErrInvalidArg; *pOutCount = Nu_DataSinkGetOutCount(pDataSink); @@ -657,22 +619,19 @@ NuDataSinkGetOutCount(NuDataSink* pDataSink, ulong* pOutCount) * =========================================================================== */ -NUFXLIB_API const char* -NuStrError(NuError err) +NUFXLIB_API const char* NuStrError(NuError err) { return Nu_StrError(err); } -NUFXLIB_API NuError -NuGetVersion(long* pMajorVersion, long* pMinorVersion, long* pBugVersion, - const char** ppBuildDate, const char** ppBuildFlags) +NUFXLIB_API NuError NuGetVersion(int32_t* pMajorVersion, int32_t* pMinorVersion, + int32_t* pBugVersion, const char** ppBuildDate, const char** ppBuildFlags) { return Nu_GetVersion(pMajorVersion, pMinorVersion, pBugVersion, ppBuildDate, ppBuildFlags); } -NUFXLIB_API NuError -NuTestFeature(NuFeature feature) +NUFXLIB_API NuError NuTestFeature(NuFeature feature) { NuError err = kNuErrUnsupFeature; @@ -710,8 +669,8 @@ NuTestFeature(NuFeature feature) return err; } -NUFXLIB_API void -NuRecordCopyAttr(NuRecordAttr* pRecordAttr, const NuRecord* pRecord) +NUFXLIB_API void NuRecordCopyAttr(NuRecordAttr* pRecordAttr, + const NuRecord* pRecord) { pRecordAttr->fileSysID = pRecord->recFileSysID; /*pRecordAttr->fileSysInfo = pRecord->recFileSysInfo;*/ @@ -723,16 +682,16 @@ NuRecordCopyAttr(NuRecordAttr* pRecordAttr, const NuRecord* pRecord) pRecordAttr->archiveWhen = pRecord->recArchiveWhen; } -NUFXLIB_API NuError -NuRecordCopyThreads(const NuRecord* pNuRecord, NuThread** ppThreads) +NUFXLIB_API NuError NuRecordCopyThreads(const NuRecord* pNuRecord, + NuThread** ppThreads) { - if (pNuRecord == nil || ppThreads == nil) + if (pNuRecord == NULL || ppThreads == NULL) return kNuErrInvalidArg; - Assert(pNuRecord->pThreads != nil); + Assert(pNuRecord->pThreads != NULL); - *ppThreads = Nu_Malloc(nil, pNuRecord->recTotalThreads * sizeof(NuThread)); - if (*ppThreads == nil) + *ppThreads = Nu_Malloc(NULL, pNuRecord->recTotalThreads * sizeof(NuThread)); + if (*ppThreads == NULL) return kNuErrMalloc; memcpy(*ppThreads, pNuRecord->pThreads, @@ -741,29 +700,39 @@ NuRecordCopyThreads(const NuRecord* pNuRecord, NuThread** ppThreads) return kNuErrNone; } -NUFXLIB_API unsigned long -NuRecordGetNumThreads(const NuRecord* pNuRecord) +NUFXLIB_API uint32_t NuRecordGetNumThreads(const NuRecord* pNuRecord) { - if (pNuRecord == nil) + if (pNuRecord == NULL) return -1; return pNuRecord->recTotalThreads; } -NUFXLIB_API const NuThread* -NuThreadGetByIdx(const NuThread* pNuThread, long idx) +NUFXLIB_API const NuThread* NuThreadGetByIdx(const NuThread* pNuThread, + int32_t idx) { - if (pNuThread == nil) - return nil; + if (pNuThread == NULL) + return NULL; return &pNuThread[idx]; /* can't range-check here */ } -NUFXLIB_API short -NuIsPresizedThreadID(NuThreadID threadID) +NUFXLIB_API short NuIsPresizedThreadID(NuThreadID threadID) { return Nu_IsPresizedThreadID(threadID); } +NUFXLIB_API size_t NuConvertMORToUNI(const char* stringMOR, + UNICHAR* bufUNI, size_t bufSize) +{ + return Nu_ConvertMORToUNI(stringMOR, bufUNI, bufSize); +} + +NUFXLIB_API size_t NuConvertUNIToMOR(const UNICHAR* stringUNI, + char* bufMOR, size_t bufSize) +{ + return Nu_ConvertUNIToMOR(stringUNI, bufMOR, bufSize); +} + /* * =========================================================================== @@ -771,13 +740,13 @@ NuIsPresizedThreadID(NuThreadID threadID) * =========================================================================== */ -NUFXLIB_API NuCallback -NuSetSelectionFilter(NuArchive* pArchive, NuCallback filterFunc) +NUFXLIB_API NuCallback NuSetSelectionFilter(NuArchive* pArchive, + NuCallback filterFunc) { NuError err; NuCallback oldFunc = kNuInvalidCallback; - /*Assert(!((ulong)filterFunc % 4));*/ + /*Assert(!((uint32_t)filterFunc % 4));*/ if ((err = Nu_ValidateNuArchive(pArchive)) == kNuErrNone) { oldFunc = pArchive->selectionFilterFunc; @@ -787,13 +756,13 @@ NuSetSelectionFilter(NuArchive* pArchive, NuCallback filterFunc) return oldFunc; } -NUFXLIB_API NuCallback -NuSetOutputPathnameFilter(NuArchive* pArchive, NuCallback filterFunc) +NUFXLIB_API NuCallback NuSetOutputPathnameFilter(NuArchive* pArchive, + NuCallback filterFunc) { NuError err; NuCallback oldFunc = kNuInvalidCallback; - /*Assert(!((ulong)filterFunc % 4));*/ + /*Assert(!((uint32_t)filterFunc % 4));*/ if ((err = Nu_ValidateNuArchive(pArchive)) == kNuErrNone) { oldFunc = pArchive->outputPathnameFunc; @@ -803,13 +772,13 @@ NuSetOutputPathnameFilter(NuArchive* pArchive, NuCallback filterFunc) return oldFunc; } -NUFXLIB_API NuCallback -NuSetProgressUpdater(NuArchive* pArchive, NuCallback updateFunc) +NUFXLIB_API NuCallback NuSetProgressUpdater(NuArchive* pArchive, + NuCallback updateFunc) { NuError err; NuCallback oldFunc = kNuInvalidCallback; - /*Assert(!((ulong)updateFunc % 4));*/ + /*Assert(!((uint32_t)updateFunc % 4));*/ if ((err = Nu_ValidateNuArchive(pArchive)) == kNuErrNone) { oldFunc = pArchive->progressUpdaterFunc; @@ -819,13 +788,13 @@ NuSetProgressUpdater(NuArchive* pArchive, NuCallback updateFunc) return oldFunc; } -NUFXLIB_API NuCallback -NuSetErrorHandler(NuArchive* pArchive, NuCallback errorFunc) +NUFXLIB_API NuCallback NuSetErrorHandler(NuArchive* pArchive, + NuCallback errorFunc) { NuError err; NuCallback oldFunc = kNuInvalidCallback; - /*Assert(!((ulong)errorFunc % 4));*/ + /*Assert(!((uint32_t)errorFunc % 4));*/ if ((err = Nu_ValidateNuArchive(pArchive)) == kNuErrNone) { oldFunc = pArchive->errorHandlerFunc; @@ -835,13 +804,13 @@ NuSetErrorHandler(NuArchive* pArchive, NuCallback errorFunc) return oldFunc; } -NUFXLIB_API NuCallback -NuSetErrorMessageHandler(NuArchive* pArchive, NuCallback messageHandlerFunc) +NUFXLIB_API NuCallback NuSetErrorMessageHandler(NuArchive* pArchive, + NuCallback messageHandlerFunc) { NuError err; NuCallback oldFunc = kNuInvalidCallback; - /*Assert(!((ulong)messageHandlerFunc % 4));*/ + /*Assert(!((uint32_t)messageHandlerFunc % 4));*/ if ((err = Nu_ValidateNuArchive(pArchive)) == kNuErrNone) { oldFunc = pArchive->messageHandlerFunc; @@ -851,11 +820,10 @@ NuSetErrorMessageHandler(NuArchive* pArchive, NuCallback messageHandlerFunc) return oldFunc; } -NUFXLIB_API NuCallback -NuSetGlobalErrorMessageHandler(NuCallback messageHandlerFunc) +NUFXLIB_API NuCallback NuSetGlobalErrorMessageHandler(NuCallback messageHandlerFunc) { NuCallback oldFunc = kNuInvalidCallback; - /*Assert(!((ulong)messageHandlerFunc % 4));*/ + /*Assert(!((uint32_t)messageHandlerFunc % 4));*/ oldFunc = gNuGlobalErrorMessageHandler; gNuGlobalErrorMessageHandler = messageHandlerFunc; diff --git a/nufxlib/Expand.c b/nufxlib/Expand.c index ecf901b..76b9d2b 100644 --- a/nufxlib/Expand.c +++ b/nufxlib/Expand.c @@ -12,18 +12,18 @@ /* * "Expand" an uncompressed thread. */ -static NuError -Nu_ExpandUncompressed(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pCrc) +static NuError Nu_ExpandUncompressed(NuArchive* pArchive, + const NuRecord* pRecord, const NuThread* pThread, FILE* infp, + NuFunnel* pFunnel, uint16_t* pCrc) { NuError err; - /*uchar* buffer = nil;*/ - ulong count, getsize; + /*uint8_t* buffer = NULL;*/ + uint32_t count, getsize; - Assert(pArchive != nil); - Assert(pThread != nil); - Assert(infp != nil); - Assert(pFunnel != nil); + Assert(pArchive != NULL); + Assert(pThread != NULL); + Assert(infp != NULL); + Assert(pFunnel != NULL); /* doesn't have to be same size as funnel, but it's not a bad idea */ /*buffer = Nu_Malloc(pArchive, kNuFunnelBufSize);*/ @@ -43,7 +43,7 @@ Nu_ExpandUncompressed(NuArchive* pArchive, const NuRecord* pRecord, err = Nu_FRead(infp, pArchive->compBuf, getsize); BailError(err); - if (pCrc != nil) + if (pCrc != NULL) *pCrc = Nu_CalcCRC16(*pCrc, pArchive->compBuf, getsize); err = Nu_FunnelWrite(pArchive, pFunnel, pArchive->compBuf, getsize); BailError(err); @@ -63,18 +63,17 @@ bail: * Copy the "raw" data out of the thread. Unlike the preceeding function, * this reads up to "thCompThreadEOF", and doesn't even try to compute a CRC. */ -static NuError -Nu_ExpandRaw(NuArchive* pArchive, const NuThread* pThread, FILE* infp, - NuFunnel* pFunnel) +static NuError Nu_ExpandRaw(NuArchive* pArchive, const NuThread* pThread, + FILE* infp, NuFunnel* pFunnel) { NuError err; - /*uchar* buffer = nil;*/ - ulong count, getsize; + /*uint8_t* buffer = NULL;*/ + uint32_t count, getsize; - Assert(pArchive != nil); - Assert(pThread != nil); - Assert(infp != nil); - Assert(pFunnel != nil); + Assert(pArchive != NULL); + Assert(pThread != NULL); + Assert(infp != NULL); + Assert(pFunnel != NULL); /* doesn't have to be same size as funnel, but it's not a bad idea */ /*buffer = Nu_Malloc(pArchive, kNuFunnelBufSize);*/ @@ -108,13 +107,12 @@ bail: * Expand a thread from "infp" to "pFunnel", using the compression * and stream length specified by "pThread". */ -NuError -Nu_ExpandStream(NuArchive* pArchive, const NuRecord* pRecord, +NuError Nu_ExpandStream(NuArchive* pArchive, const NuRecord* pRecord, const NuThread* pThread, FILE* infp, NuFunnel* pFunnel) { NuError err = kNuErrNone; - ushort calcCrc; - ushort* pCalcCrc; + uint16_t calcCrc; + uint16_t* pCalcCrc; if (!pThread->thThreadEOF && !pThread->thCompThreadEOF) { /* somebody stored an empty file! */ @@ -134,7 +132,7 @@ Nu_ExpandStream(NuArchive* pArchive, const NuRecord* pRecord, * unfortunately, unprotected before v3. */ calcCrc = kNuInitialThreadCRC; - pCalcCrc = nil; + pCalcCrc = NULL; if (Nu_ThreadHasCRC(pRecord->recVersionNumber, NuGetThreadID(pThread)) && !pArchive->valIgnoreCRC) { @@ -205,7 +203,7 @@ Nu_ExpandStream(NuArchive* pArchive, const NuRecord* pRecord, /* * If we have a CRC to check, check it. */ - if (pCalcCrc != nil) { + if (pCalcCrc != NULL) { if (calcCrc != pThread->thThreadCRC) { if (!Nu_ShouldIgnoreBadCRC(pArchive, pRecord, kNuErrBadThreadCRC)) { err = kNuErrBadDataCRC; diff --git a/nufxlib/FileIO.c b/nufxlib/FileIO.c index aa5cc46..c4c11db 100644 --- a/nufxlib/FileIO.c +++ b/nufxlib/FileIO.c @@ -19,7 +19,7 @@ #include "NufxLibPriv.h" #ifdef MAC_LIKE -# include +# include #endif /* @@ -81,14 +81,13 @@ * * If the conversion is invalid, "*pWhen" is set to zero. */ -static void -Nu_DateTimeToGMTSeconds(const NuDateTime* pDateTime, time_t* pWhen) +static void Nu_DateTimeToGMTSeconds(const NuDateTime* pDateTime, time_t* pWhen) { struct tm tmbuf; time_t when; - Assert(pDateTime != nil); - Assert(pWhen != nil); + Assert(pDateTime != NULL); + Assert(pWhen != NULL); tmbuf.tm_sec = pDateTime->second; tmbuf.tm_min = pDateTime->minute; @@ -119,13 +118,12 @@ Nu_DateTimeToGMTSeconds(const NuDateTime* pDateTime, time_t* pWhen) /* * Convert from GMT seconds since 1970 to local time in a NuDateTime struct. */ -static void -Nu_GMTSecondsToDateTime(const time_t* pWhen, NuDateTime *pDateTime) +static void Nu_GMTSecondsToDateTime(const time_t* pWhen, NuDateTime *pDateTime) { struct tm* ptm; - Assert(pWhen != nil); - Assert(pDateTime != nil); + Assert(pWhen != NULL); + Assert(pDateTime != NULL); #if defined(HAVE_LOCALTIME_R) && defined(USE_REENTRANT_CALLS) struct tm res; @@ -149,14 +147,13 @@ Nu_GMTSecondsToDateTime(const time_t* pWhen, NuDateTime *pDateTime) /* * Fill in the current time. */ -void -Nu_SetCurrentDateTime(NuDateTime* pDateTime) +void Nu_SetCurrentDateTime(NuDateTime* pDateTime) { - Assert(pDateTime != nil); + Assert(pDateTime != NULL); #if defined(UNIX_LIKE) || defined(WINDOWS_LIKE) { - time_t now = time(nil); + time_t now = time(NULL); Nu_GMTSecondsToDateTime(&now, pDateTime); } #else @@ -173,8 +170,7 @@ Nu_SetCurrentDateTime(NuDateTime* pDateTime) * to time in seconds, and compare them that way. However, I don't want * to rely on that function too heavily, so we just compare fields. */ -Boolean -Nu_IsOlder(const NuDateTime* pWhen1, const NuDateTime* pWhen2) +Boolean Nu_IsOlder(const NuDateTime* pWhen1, const NuDateTime* pWhen2) { long result, year1, year2; @@ -218,12 +214,10 @@ typedef struct NuFileInfo { Boolean isRegularFile; /* is this a regular file? */ Boolean isDirectory; /* is this a directory? */ + Boolean isForked; /* does file have a non-empty resource fork? */ - ulong dataEof; - ulong rsrcEof; + uint32_t dataEof; - ulong fileType; - ulong auxType; NuDateTime modWhen; mode_t unixMode; /* UNIX-style permissions */ } NuFileInfo; @@ -235,8 +229,7 @@ typedef struct NuFileInfo { /* * Determine whether the record has both data and resource forks. */ -static Boolean -Nu_IsForkedFile(NuArchive* pArchive, const NuRecord* pRecord) +static Boolean Nu_IsForkedFile(NuArchive* pArchive, const NuRecord* pRecord) { const NuThread* pThread; NuThreadID threadID; @@ -247,7 +240,7 @@ Nu_IsForkedFile(NuArchive* pArchive, const NuRecord* pRecord) for (i = 0; i < (int)pRecord->recTotalThreads; i++) { pThread = Nu_GetThread(pRecord, i); - Assert(pThread != nil); + Assert(pThread != NULL); threadID = NuMakeThreadID(pThread->thThreadClass,pThread->thThreadKind); if (threadID == kNuThreadIDDataFork) @@ -263,18 +256,109 @@ Nu_IsForkedFile(NuArchive* pArchive, const NuRecord* pRecord) } +#if defined(MAC_LIKE) +# if defined(HAS_RESOURCE_FORKS) +/* + * String to append to the filename to access the resource fork. + * + * This appears to be the correct way to access the resource fork, since + * at least OS X 10.1. Up until 10.7 ("Lion", July 2011), you could also + * access the fork with "/rsrc". + */ +static const char kMacRsrcPath[] = "/..namedfork/rsrc"; + +/* + * Generates the resource fork pathname from the file path. + * + * The caller must free the string returned. + */ +static UNICHAR* GetResourcePath(const UNICHAR* pathnameUNI) +{ + Assert(pathnameUNI != NULL); + + // sizeof(kMacRsrcPath) includes the string and the terminating null byte + const size_t bufLen = + strlen(pathnameUNI) * sizeof(UNICHAR) + sizeof(kMacRsrcPath); + char* buf; + + buf = (char*) malloc(bufLen); + snprintf(buf, bufLen, "%s%s", pathnameUNI, kMacRsrcPath); + return buf; +} +# endif /*HAS_RESOURCE_FORKS*/ + +/* + * Due to historical reasons, the XATTR_FINDERINFO_NAME (defined to be + * ``com.apple.FinderInfo'') extended attribute must be 32 bytes; see the + * ATTR_CMN_FNDRINFO section in getattrlist(2). + * + * The FinderInfo block is the concatenation of a FileInfo structure + * and an ExtendedFileInfo (or ExtendedFolderInfo) structure -- see + * ATTR_CMN_FNDRINFO in getattrlist(2). + * + * All we're really interested in is the file type and creator code, + * which are stored big-endian in the first 8 bytes. + */ +static const int kFinderInfoSize = 32; + +/* + * Set the file type and creator type. + */ +static NuError Nu_SetFinderInfo(NuArchive* pArchive, const NuRecord* pRecord, + const UNICHAR* pathnameUNI) +{ + uint8_t fiBuf[kFinderInfoSize]; + + size_t actual = getxattr(pathnameUNI, XATTR_FINDERINFO_NAME, + fiBuf, sizeof(fiBuf), 0, 0); + if (actual == (size_t) -1 && errno == ENOATTR) { + // doesn't yet have Finder info + memset(fiBuf, 0, sizeof(fiBuf)); + } else if (actual != kFinderInfoSize) { + Nu_ReportError(NU_BLOB, errno, + "Finder info on '%s' returned %d", pathnameUNI, (int) actual); + return kNuErrFile; + } + + /* build type and creator from 8-bit type and 16-bit aux type */ + uint32_t fileType, creator; + fileType = ('p' << 24) | ((pRecord->recFileType & 0xff) << 16) | + (pRecord->recExtraType & 0xffff); + creator = 'pdos'; + + fiBuf[0] = fileType >> 24; + fiBuf[1] = fileType >> 16; + fiBuf[2] = fileType >> 8; + fiBuf[3] = fileType; + fiBuf[4] = creator >> 24; + fiBuf[5] = creator >> 16; + fiBuf[6] = creator >> 8; + fiBuf[7] = creator; + + if (setxattr(pathnameUNI, XATTR_FINDERINFO_NAME, fiBuf, sizeof(fiBuf), + 0, 0) != 0) + { + Nu_ReportError(NU_BLOB, errno, + "Unable to set Finder info on '%s'", pathnameUNI); + return kNuErrFile; + } + + return kNuErrNone; +} +#endif /*MAC_LIKE*/ + + /* * Get the file info into a NuFileInfo struct. Fields which are * inappropriate for the current system are set to default values. */ -static NuError -Nu_GetFileInfo(NuArchive* pArchive, const char* pathname, +static NuError Nu_GetFileInfo(NuArchive* pArchive, const UNICHAR* pathnameUNI, NuFileInfo* pFileInfo) { NuError err = kNuErrNone; - Assert(pArchive != nil); - Assert(pathname != nil); - Assert(pFileInfo != nil); + Assert(pArchive != NULL); + Assert(pathnameUNI != NULL); + Assert(pFileInfo != NULL); pFileInfo->isValid = false; @@ -283,7 +367,7 @@ Nu_GetFileInfo(NuArchive* pArchive, const char* pathname, struct stat sbuf; int cc; - cc = stat(pathname, &sbuf); + cc = stat(pathnameUNI, &sbuf); if (cc) { if (errno == ENOENT) err = kNuErrFileNotFound; @@ -301,99 +385,28 @@ Nu_GetFileInfo(NuArchive* pArchive, const char* pathname, /* BUG: should check for 32-bit overflow from 64-bit off_t */ pFileInfo->dataEof = sbuf.st_size; - pFileInfo->rsrcEof = 0; - pFileInfo->fileType = kDefaultFileType; - pFileInfo->auxType = kDefaultAuxType; -# if defined(MAC_LIKE) + pFileInfo->isForked = false; +# if defined(MAC_LIKE) && defined(HAS_RESOURCE_FORKS) if (!pFileInfo->isDirectory) { - char path[4096]; + /* + * Check for the presence of a resource fork. You can check + * these from a terminal with "ls -l@" -- look for the + * "com.apple.ResourceFork" attribute. + * + * We can either use getxattr() and check for the presence of + * the attribute, or get the file length with stat(). I + * don't know if xattr has always worked with resource forks, + * so we'll stick with stat for now. + */ + UNICHAR* rsrcPath = GetResourcePath(pathnameUNI); + struct stat res_sbuf; - OSErr result; - OSType fileType, creator; - FSCatalogInfo catalogInfo; - FSRef ref; - unsigned long proType, proAux; - - strcpy(path, pathname); - strcat(path, "/rsrc"); - cc = stat(path, &res_sbuf); - if (cc) { - if (!errno) { - pFileInfo->rsrcEof = res_sbuf.st_size; - } - } - - result = FSPathMakeRef(pathname, &ref, NULL); - if (!result) { - result = FSGetCatalogInfo(&ref, kFSCatInfoFinderInfo, &catalogInfo, - NULL, NULL, NULL); - if (!result) { - fileType = ((FileInfo *) &catalogInfo.finderInfo)->fileType; - creator = ((FileInfo *) &catalogInfo.finderInfo)->fileCreator; - - /* This actually is probably more efficient than a weird table, for - so few values */ - - switch(creator) { - case 'pdos': - if (fileType == 'PSYS') { - proType = 0xFF; - proAux = 0x0000; - } else if (fileType == 'PS16') { - proType = 0xB3; - proAux = 0x0000; - } else { - if (((fileType >> 24) & 0xFF) == 'p') { - proType = (fileType >> 16) & 0xFF; - proAux = fileType & 0xFFFF; - } else { - proType = 0x00; - proAux = 0x0000; - } - } - break; - case 'dCpy': - if (fileType == 'dImg') { - proType = 0xE0; - proAux = 0x0005; - } else { - proType = 0x00; - proAux = 0x0000; - } - break; - default: - switch(fileType) { - case 'BINA': - proType = 0x06; - proAux = 0x0000; - break; - case 'TEXT': - proType = 0x04; - proAux = 0x0000; - break; - case 'MIDI': - proType = 0xD7; - proAux = 0x0000; - break; - case 'AIFF': - proType = 0xD8; - proAux = 0x0000; - break; - case 'AIFC': - proType = 0xD8; - proAux = 0x0001; - break; - default: - proType = 0x00; - proAux = 0x0000; - break; - } - break; - } - pFileInfo->fileType = proType; - pFileInfo->auxType = proAux; - } + + if (stat(rsrcPath, &res_sbuf) == 0) { + pFileInfo->isForked = (res_sbuf.st_size != 0); } + + free(rsrcPath); } # endif Nu_GMTSecondsToDateTime(&sbuf.st_mtime, &pFileInfo->modWhen); @@ -417,68 +430,44 @@ bail: * file with data and resource forks, we only claim it exists if it has * nonzero length. */ -static NuError -Nu_FileForkExists(NuArchive* pArchive, const char* pathname, - Boolean isForkedFile, Boolean checkRsrcFork, Boolean* pExists, - NuFileInfo* pFileInfo) +static NuError Nu_FileForkExists(NuArchive* pArchive, + const UNICHAR* pathnameUNI, Boolean isForkedFile, Boolean checkRsrcFork, + Boolean* pExists, NuFileInfo* pFileInfo) { NuError err = kNuErrNone; - Assert(pArchive != nil); - Assert(pathname != nil); + Assert(pArchive != NULL); + Assert(pathnameUNI != NULL); Assert(checkRsrcFork == true || checkRsrcFork == false); - Assert(pExists != nil); - Assert(pFileInfo != nil); + Assert(pExists != NULL); + Assert(pFileInfo != NULL); -#if defined(MAC_LIKE) - /* - * On Mac OS X, we do much like on Unix, but we do need to look for - * a resource fork. - */ - *pExists = true; - if (!checkRsrcFork) { - /* - * Check the data fork. - */ - Assert(pArchive->lastFileCreated == nil); - err = Nu_GetFileInfo(pArchive, pathname, pFileInfo); - if (err == kNuErrFileNotFound) { - err = kNuErrNone; - *pExists = false; - } -/* DBUG(("Data fork %s: %d (err %d)\n", pathname, *pExists, err));*/ - } else { - /* - * Check the resource fork. - */ - char path[4096]; - strncpy(path, pathname, 4089); - strcat(path, "/rsrc"); - err = Nu_GetFileInfo(pArchive, path, pFileInfo); - if (err == kNuErrFileNotFound) { - err = kNuErrNone; - *pExists = false; - } else if (!err && !pFileInfo->rsrcEof) { - err = kNuErrNone; - *pExists = false; - } -/* DBUG(("Rsrc fork %s: %d (err %d)\n", path, *pExists, err));*/ - } - -#elif defined(UNIX_LIKE) || defined(WINDOWS_LIKE) +#if defined(UNIX_LIKE) || defined(WINDOWS_LIKE) +# if !defined(MAC_LIKE) /* * On Unix and Windows we ignore "isForkedFile" and "checkRsrcFork". * The file must not exist at all. */ - Assert(pArchive->lastFileCreated == nil); + Assert(pArchive->lastFileCreatedUNI == NULL); +# endif *pExists = true; - err = Nu_GetFileInfo(pArchive, pathname, pFileInfo); + err = Nu_GetFileInfo(pArchive, pathnameUNI, pFileInfo); if (err == kNuErrFileNotFound) { err = kNuErrNone; *pExists = false; } +# if defined(MAC_LIKE) + /* + * On Mac OS X, we'll use the resource fork, but we may not want to + * overwrite existing data. + */ + if (*pExists && checkRsrcFork) { + *pExists = pFileInfo->isForked; + } +# endif + #elif defined(__ORCAC__) /* * If the file doesn't exist, great. If it does, and "lastFileCreated" @@ -509,15 +498,14 @@ Nu_FileForkExists(NuArchive* pArchive, const char* pathname, /* * Set the dates on a file according to what's in the record. */ -static NuError -Nu_SetFileDates(NuArchive* pArchive, const NuRecord* pRecord, - const char* pathname) +static NuError Nu_SetFileDates(NuArchive* pArchive, const NuRecord* pRecord, + const UNICHAR* pathnameUNI) { NuError err = kNuErrNone; - Assert(pArchive != nil); - Assert(pRecord != nil); - Assert(pathname != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); + Assert(pathnameUNI != NULL); #if defined(UNIX_LIKE) || defined(WINDOWS_LIKE) { @@ -529,9 +517,9 @@ Nu_SetFileDates(NuArchive* pArchive, const NuRecord* pRecord, /* only do it if the NuDateTime was valid */ if (utbuf.modtime) { - if (utime(pathname, &utbuf) < 0) { + if (utime(pathnameUNI, &utbuf) < 0) { Nu_ReportError(NU_BLOB, errno, - "Unable to set time stamp on '%s'", pathname); + "Unable to set time stamp on '%s'", pathnameUNI); err = kNuErrFileSetDate; goto bail; } @@ -563,8 +551,7 @@ bail: * possible variations. For our purposes, we treat all files as unlocked * unless they match the classic "locked" bit pattern. */ -static Boolean -Nu_IsRecordLocked(const NuRecord* pRecord) +static Boolean Nu_IsRecordLocked(const NuRecord* pRecord) { if (pRecord->recAccess == 0x21L || pRecord->recAccess == 0x01L) return true; @@ -578,15 +565,14 @@ Nu_IsRecordLocked(const NuRecord* pRecord) * This assumes that the file is currently writable, so we only need * to do something if the original file was "locked". */ -static NuError -Nu_SetFileAccess(NuArchive* pArchive, const NuRecord* pRecord, - const char* pathname) +static NuError Nu_SetFileAccess(NuArchive* pArchive, const NuRecord* pRecord, + const UNICHAR* pathnameUNI) { NuError err = kNuErrNone; - Assert(pArchive != nil); - Assert(pRecord != nil); - Assert(pathname != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); + Assert(pathnameUNI != NULL); #if defined(UNIX_LIKE) || defined(WINDOWS_LIKE) /* only need to do something if the file was "locked" */ @@ -598,9 +584,10 @@ Nu_SetFileAccess(NuArchive* pArchive, const NuRecord* pRecord, umask(mask); //DBUG(("+++ chmod '%s' %03o (mask=%03o)\n", pathname, // (S_IRUSR | S_IRGRP | S_IROTH) & ~mask, mask)); - if (chmod(pathname, (S_IRUSR | S_IRGRP | S_IROTH) & ~mask) < 0) { + if (chmod(pathnameUNI, (S_IRUSR | S_IRGRP | S_IROTH) & ~mask) < 0) { Nu_ReportError(NU_BLOB, errno, - "unable to set access for '%s' to %03o", pathname, (int) mask); + "unable to set access for '%s' to %03o", pathnameUNI, + (int) mask); err = kNuErrFileSetAccess; goto bail; } @@ -626,20 +613,19 @@ bail: * * Generally this just involves ensuring that the file is writable. If * this is a convenient place to truncate it, we should do that too. + * + * 20150103: we don't seem to be doing the truncation here, so prepRsrc + * is unused. */ -static NuError -Nu_PrepareForWriting(NuArchive* pArchive, const char* pathname, - Boolean prepRsrc, NuFileInfo* pFileInfo) +static NuError Nu_PrepareForWriting(NuArchive* pArchive, + const UNICHAR* pathnameUNI, Boolean prepRsrc, NuFileInfo* pFileInfo) { NuError err = kNuErrNone; -#if defined(MAC_LIKE) - char path[4096]; -#endif - Assert(pArchive != nil); - Assert(pathname != nil); + Assert(pArchive != NULL); + Assert(pathnameUNI != NULL); Assert(prepRsrc == true || prepRsrc == false); - Assert(pFileInfo != nil); + Assert(pFileInfo != NULL); Assert(pFileInfo->isValid == true); @@ -648,18 +634,11 @@ Nu_PrepareForWriting(NuArchive* pArchive, const char* pathname, return kNuErrNotRegularFile; #if defined(UNIX_LIKE) || defined(WINDOWS_LIKE) -# if defined(MAC_LIKE) - if (prepRsrc) { - strcpy(path, pathname); - strcat(path, "/rsrc"); - pathname = path; - } -# endif if (!(pFileInfo->unixMode & S_IWUSR)) { /* make it writable by owner, plus whatever it was before */ - if (chmod(pathname, S_IWUSR | pFileInfo->unixMode) < 0) { + if (chmod(pathnameUNI, S_IWUSR | pFileInfo->unixMode) < 0) { Nu_ReportError(NU_BLOB, errno, - "unable to set access for '%s'", pathname); + "unable to set access for '%s'", pathnameUNI); err = kNuErrFileSetAccess; goto bail; } @@ -679,13 +658,12 @@ bail: /* * Invoke the system-dependent directory creation function. */ -static NuError -Nu_Mkdir(NuArchive* pArchive, const char* dir) +static NuError Nu_Mkdir(NuArchive* pArchive, const char* dir) { NuError err = kNuErrNone; - Assert(pArchive != nil); - Assert(dir != nil); + Assert(pArchive != NULL); + Assert(dir != NULL); #if defined(UNIX_LIKE) if (mkdir(dir, S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH) < 0) { @@ -715,28 +693,27 @@ bail: * subdirectory level doesn't exist either, cut down the pathname and * recurse. */ -static NuError -Nu_CreateSubdirIFN(NuArchive* pArchive, const char* pathStart, - const char* pathEnd, char fssep) +static NuError Nu_CreateSubdirIFN(NuArchive* pArchive, + const UNICHAR* pathStartUNI, const char* pathEnd, char fssep) { NuError err = kNuErrNone; NuFileInfo fileInfo; - char* tmpBuf = nil; + char* tmpBuf = NULL; - Assert(pArchive != nil); - Assert(pathStart != nil); - Assert(pathEnd != nil); + Assert(pArchive != NULL); + Assert(pathStartUNI != NULL); + Assert(pathEnd != NULL); Assert(fssep != '\0'); /* pathStart might have whole path, but we only want up to "pathEnd" */ - tmpBuf = strdup(pathStart); - tmpBuf[pathEnd - pathStart +1] = '\0'; + tmpBuf = strdup(pathStartUNI); + tmpBuf[pathEnd - pathStartUNI +1] = '\0'; err = Nu_GetFileInfo(pArchive, tmpBuf, &fileInfo); if (err == kNuErrFileNotFound) { /* dir doesn't exist; move up a level and check parent */ pathEnd = strrchr(tmpBuf, fssep); - if (pathEnd != nil) { + if (pathEnd != NULL) { pathEnd--; Assert(pathEnd >= tmpBuf); err = Nu_CreateSubdirIFN(pArchive, tmpBuf, pathEnd, fssep); @@ -770,27 +747,28 @@ bail: * If "pathname" is just a filename, or the set of directories matches * the last directory we created, we don't do anything. */ -static NuError -Nu_CreatePathIFN(NuArchive* pArchive, const char* pathname, char fssep) +static NuError Nu_CreatePathIFN(NuArchive* pArchive, const UNICHAR* pathnameUNI, + UNICHAR fssep) { NuError err = kNuErrNone; const char* pathStart; const char* pathEnd; - Assert(pArchive != nil); - Assert(pathname != nil); + Assert(pArchive != NULL); + Assert(pathnameUNI != NULL); Assert(fssep != '\0'); - pathStart = pathname; - -#if !defined(MAC_LIKE) /* On the Mac, if it's a full path, treat it like one */ - if (pathname[0] == fssep) + pathStart = pathnameUNI; + +#if !defined(MAC_LIKE) /* On the Mac, if it's a full path, treat it like one */ + // 20150103: not sure what use case this is for + if (pathnameUNI[0] == fssep) pathStart++; #endif /* NOTE: not expecting names like "foo/bar/ack/", with terminating fssep */ pathEnd = strrchr(pathStart, fssep); - if (pathEnd == nil) { + if (pathEnd == NULL) { /* no subdirectory components found */ goto bail; } @@ -805,8 +783,9 @@ Nu_CreatePathIFN(NuArchive* pArchive, const char* pathname, char fssep) * this is meant solely as an optimization to avoid extra stat() calls, * so we want to use the most restrictive case. */ - if (pArchive->lastDirCreated && - strncmp(pathStart, pArchive->lastDirCreated, pathEnd - pathStart +1) == 0) + if (pArchive->lastDirCreatedUNI && + strncmp(pathStart, pArchive->lastDirCreatedUNI, + pathEnd - pathStart +1) == 0) { /* we created this one recently, don't do it again */ goto bail; @@ -828,20 +807,21 @@ bail: /* * Open the file for writing, possibly truncating it. */ -static NuError -Nu_OpenFileForWrite(NuArchive* pArchive, const char* pathname, - Boolean openRsrc, FILE** pFp) +static NuError Nu_OpenFileForWrite(NuArchive* pArchive, + const UNICHAR* pathnameUNI, Boolean openRsrc, FILE** pFp) { -#if defined(MAC_LIKE) - char path[4096]; +#if defined(MAC_LIKE) && defined(HAS_RESOURCE_FORKS) if (openRsrc) { - strcpy(path, pathname); - strcat(path, "/rsrc"); - pathname = path; + UNICHAR* rsrcPath = GetResourcePath(pathnameUNI); + *pFp = fopen(rsrcPath, kNuFileOpenWriteTrunc); + free(rsrcPath); + } else { + *pFp = fopen(pathnameUNI, kNuFileOpenWriteTrunc); } +#else + *pFp = fopen(pathnameUNI, kNuFileOpenWriteTrunc); #endif - *pFp = fopen(pathname, kNuFileOpenWriteTrunc); - if (*pFp == nil) + if (*pFp == NULL) return errno ? errno : -1; return kNuErrNone; } @@ -856,9 +836,8 @@ Nu_OpenFileForWrite(NuArchive* pArchive, const char* pathname, * "freshen" option that requires us to only update files that are * older than what we have. */ -NuError -Nu_OpenOutputFile(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, const char* newPathname, char newFssep, +NuError Nu_OpenOutputFile(NuArchive* pArchive, const NuRecord* pRecord, + const NuThread* pThread, const UNICHAR* newPathnameUNI, UNICHAR newFssep, FILE** pFp) { NuError err = kNuErrNone; @@ -867,20 +846,20 @@ Nu_OpenOutputFile(NuArchive* pArchive, const NuRecord* pRecord, NuErrorStatus errorStatus; NuResult result; - Assert(pArchive != nil); - Assert(pRecord != nil); - Assert(pThread != nil); - Assert(newPathname != nil); - Assert(pFp != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); + Assert(pThread != NULL); + Assert(newPathnameUNI != NULL); + Assert(pFp != NULL); /* set up some defaults, in case something goes wrong */ errorStatus.operation = kNuOpExtract; errorStatus.err = kNuErrInternal; errorStatus.sysErr = 0; - errorStatus.message = nil; + errorStatus.message = NULL; errorStatus.pRecord = pRecord; - errorStatus.pathname = newPathname; - errorStatus.origPathname = nil; + errorStatus.pathnameUNI = newPathnameUNI; + errorStatus.origPathname = NULL; errorStatus.filenameSeparator = newFssep; /*errorStatus.origArchiveTouched = false;*/ errorStatus.canAbort = true; @@ -906,7 +885,7 @@ Nu_OpenOutputFile(NuArchive* pArchive, const NuRecord* pRecord, * empty, this will *not* set "exists". */ fileInfo.isValid = false; - err = Nu_FileForkExists(pArchive, newPathname, isForkedFile, + err = Nu_FileForkExists(pArchive, newPathnameUNI, isForkedFile, extractingRsrc, &exists, &fileInfo); BailError(err); @@ -926,7 +905,7 @@ Nu_OpenOutputFile(NuArchive* pArchive, const NuRecord* pRecord, if ((pArchive->valOnlyUpdateOlder) && !Nu_IsOlder(&fileInfo.modWhen, &pRecord->recModWhen)) { - if (pArchive->errorHandlerFunc != nil) { + if (pArchive->errorHandlerFunc != NULL) { errorStatus.err = kNuErrNotNewer; result = (*pArchive->errorHandlerFunc)(pArchive, &errorStatus); @@ -962,7 +941,7 @@ Nu_OpenOutputFile(NuArchive* pArchive, const NuRecord* pRecord, * and extracting to a different file. */ if (pArchive->valHandleExisting == kNuMaybeOverwrite) { - if (pArchive->errorHandlerFunc != nil) { + if (pArchive->errorHandlerFunc != NULL) { errorStatus.err = kNuErrFileExists; result = (*pArchive->errorHandlerFunc)(pArchive, &errorStatus); @@ -1004,8 +983,8 @@ Nu_OpenOutputFile(NuArchive* pArchive, const NuRecord* pRecord, * return kNuSkip. */ if (pArchive->valHandleExisting == kNuMustOverwrite) { - DBUG(("+++ can't freshen nonexistent file '%s'\n", newPathname)); - if (pArchive->errorHandlerFunc != nil) { + DBUG(("+++ can't freshen nonexistent file '%s'\n", newPathnameUNI)); + if (pArchive->errorHandlerFunc != NULL) { errorStatus.err = kNuErrDuplicateNotFound; /* give them a chance to rename */ @@ -1055,23 +1034,23 @@ Nu_OpenOutputFile(NuArchive* pArchive, const NuRecord* pRecord, */ if (exists) { Assert(fileInfo.isValid == true); - err = Nu_PrepareForWriting(pArchive, newPathname, extractingRsrc, + err = Nu_PrepareForWriting(pArchive, newPathnameUNI, extractingRsrc, &fileInfo); BailError(err); } else if (!fileInfo.isValid) { - err = Nu_CreatePathIFN(pArchive, newPathname, newFssep); + err = Nu_CreatePathIFN(pArchive, newPathnameUNI, newFssep); BailError(err); } /* * Open sesame. */ - err = Nu_OpenFileForWrite(pArchive, newPathname, extractingRsrc, pFp); + err = Nu_OpenFileForWrite(pArchive, newPathnameUNI, extractingRsrc, pFp); BailError(err); #if defined(HAS_RESOURCE_FORKS) - pArchive->lastFileCreated = newPathname; + pArchive->lastFileCreatedUNI = newPathnameUNI; #endif bail: @@ -1080,7 +1059,7 @@ bail: err != kNuErrFileExists) { Nu_ReportError(NU_BLOB, err, "Unable to open '%s'%s", - newPathname, extractingRsrc ? " (rsrc fork)" : ""); + newPathnameUNI, extractingRsrc ? " (rsrc fork)" : ""); } } return err; @@ -1104,51 +1083,27 @@ bail: * when writing a rsrc fork to a file with just a data fork. This isn't * quite right, but it's close enough. */ -NuError -Nu_CloseOutputFile(NuArchive* pArchive, const NuRecord* pRecord, FILE* fp, - const char* pathname) +NuError Nu_CloseOutputFile(NuArchive* pArchive, const NuRecord* pRecord, + FILE* fp, const UNICHAR* pathnameUNI) { NuError err; - Assert(pArchive != nil); - Assert(pRecord != nil); - Assert(fp != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); + Assert(fp != NULL); fclose(fp); - err = Nu_SetFileDates(pArchive, pRecord, pathname); + err = Nu_SetFileDates(pArchive, pRecord, pathnameUNI); BailError(err); - err = Nu_SetFileAccess(pArchive, pRecord, pathname); + err = Nu_SetFileAccess(pArchive, pRecord, pathnameUNI); BailError(err); -#ifdef MAC_LIKE - OSErr result; - OSType fileType; - FSCatalogInfo catalogInfo; - FSRef ref; - - result = FSPathMakeRef(pathname, &ref, NULL); - BailError(result); - - result = FSGetCatalogInfo(&ref, kFSCatInfoNodeFlags|kFSCatInfoFinderInfo, &catalogInfo, - NULL, NULL, NULL); - if (result) { - BailError(kNuErrFileStat); - } - - /* Build the type and creator */ - - fileType = 0x70000000; - fileType |= (pRecord->recFileType & 0xFF) << 16; - fileType |= (pRecord->recExtraType & 0xFFFF); - - /* Set the type and creator */ - - ((FileInfo *) &catalogInfo.finderInfo)->fileType = fileType; - ((FileInfo *) &catalogInfo.finderInfo)->fileCreator = 'pdos'; - result = FSSetCatalogInfo(&ref, kFSCatInfoFinderInfo, &catalogInfo); - BailError(result); +#if defined(MAC_LIKE) + /* could also do this earlier and pass the fd for fsetxattr */ + err = Nu_SetFinderInfo(pArchive, pRecord, pathnameUNI); + BailError(err); #endif bail: @@ -1164,12 +1119,11 @@ bail: /* * Open the file for reading, in "binary" mode when necessary. */ -static NuError -Nu_OpenFileForRead(NuArchive* pArchive, const char* pathname, - Boolean openRsrc, FILE** pFp) +static NuError Nu_OpenFileForRead(NuArchive* pArchive, + const UNICHAR* pathnameUNI, Boolean openRsrc, FILE** pFp) { - *pFp = fopen(pathname, kNuFileOpenReadOnly); - if (*pFp == nil) + *pFp = fopen(pathnameUNI, kNuFileOpenReadOnly); + if (*pFp == NULL) return errno ? errno : -1; return kNuErrNone; } @@ -1181,8 +1135,7 @@ Nu_OpenFileForRead(NuArchive* pArchive, const char* pathname, * If the file can't be found, we give the application an opportunity to * skip the absent file, retry, or abort the whole thing. */ -NuError -Nu_OpenInputFile(NuArchive* pArchive, const char* pathname, +NuError Nu_OpenInputFile(NuArchive* pArchive, const UNICHAR* pathnameUNI, Boolean openRsrc, FILE** pFp) { NuError err = kNuErrNone; @@ -1190,16 +1143,15 @@ Nu_OpenInputFile(NuArchive* pArchive, const char* pathname, NuErrorStatus errorStatus; NuResult result; - Assert(pArchive != nil); - Assert(pathname != nil); - Assert(pFp != nil); + Assert(pArchive != NULL); + Assert(pathnameUNI != NULL); + Assert(pFp != NULL); -#if defined(MAC_LIKE) - char path[4096]; +#if defined(MAC_LIKE) && defined(HAS_RESOURCE_FORKS) + UNICHAR* rsrcPath = NULL; if (openRsrc) { - strcpy(path, pathname); - strcat(path, "/rsrc"); - pathname = path; + rsrcPath = GetResourcePath(pathnameUNI); + pathnameUNI = rsrcPath; } #endif @@ -1207,21 +1159,21 @@ retry: /* * Open sesame. */ - err = Nu_OpenFileForRead(pArchive, pathname, openRsrc, pFp); + err = Nu_OpenFileForRead(pArchive, pathnameUNI, openRsrc, pFp); if (err == kNuErrNone) /* success! */ goto bail; if (err == ENOENT) openErr = kNuErrFileNotFound; - if (pArchive->errorHandlerFunc != nil) { + if (pArchive->errorHandlerFunc != NULL) { errorStatus.operation = kNuOpAdd; errorStatus.err = openErr; errorStatus.sysErr = 0; - errorStatus.message = nil; - errorStatus.pRecord = nil; - errorStatus.pathname = pathname; - errorStatus.origPathname = nil; + errorStatus.message = NULL; + errorStatus.pRecord = NULL; + errorStatus.pathnameUNI = pathnameUNI; + errorStatus.origPathname = NULL; errorStatus.filenameSeparator = '\0'; /*errorStatus.origArchiveTouched = false;*/ errorStatus.canAbort = true; @@ -1258,15 +1210,18 @@ retry: bail: if (err == kNuErrNone) { - Assert(*pFp != nil); + Assert(*pFp != NULL); } else { if (err != kNuErrSkipped && err != kNuErrRename && err != kNuErrFileExists) { Nu_ReportError(NU_BLOB, err, "Unable to open '%s'%s", - pathname, openRsrc ? " (rsrc fork)" : ""); + pathnameUNI, openRsrc ? " (rsrc fork)" : ""); } } +#if defined(MAC_LIKE) && defined(HAS_RESOURCE_FORKS) + free(rsrcPath); +#endif return err; } @@ -1280,15 +1235,14 @@ bail: /* * Delete a file. */ -NuError -Nu_DeleteFile(const char* pathname) +NuError Nu_DeleteFile(const UNICHAR* pathnameUNI) { #if defined(UNIX_LIKE) || defined(WINDOWS_LIKE) int cc; - DBUG(("--- Deleting '%s'\n", pathname)); + DBUG(("--- Deleting '%s'\n", pathnameUNI)); - cc = unlink(pathname); + cc = unlink(pathnameUNI); if (cc < 0) return errno ? errno : -1; else @@ -1301,15 +1255,14 @@ Nu_DeleteFile(const char* pathname) /* * Rename a file from "fromPath" to "toPath". */ -NuError -Nu_RenameFile(const char* fromPath, const char* toPath) +NuError Nu_RenameFile(const UNICHAR* fromPathUNI, const UNICHAR* toPathUNI) { #if defined(UNIX_LIKE) || defined(WINDOWS_LIKE) int cc; - DBUG(("--- Renaming '%s' to '%s'\n", fromPath, toPath)); + DBUG(("--- Renaming '%s' to '%s'\n", fromPathUNI, toPathUNI)); - cc = rename(fromPath, toPath); + cc = rename(fromPathUNI, toPathUNI); if (cc < 0) return errno ? errno : -1; else @@ -1329,11 +1282,10 @@ Nu_RenameFile(const char* fromPath, const char* toPath) /* * Wrapper for ftell(). */ -NuError -Nu_FTell(FILE* fp, long* pOffset) +NuError Nu_FTell(FILE* fp, long* pOffset) { - Assert(fp != nil); - Assert(pOffset != nil); + Assert(fp != NULL); + Assert(pOffset != NULL); errno = 0; *pOffset = ftell(fp); @@ -1347,10 +1299,9 @@ Nu_FTell(FILE* fp, long* pOffset) /* * Wrapper for fseek(). */ -NuError -Nu_FSeek(FILE* fp, long offset, int ptrname) +NuError Nu_FSeek(FILE* fp, long offset, int ptrname) { - Assert(fp != nil); + Assert(fp != NULL); Assert(ptrname == SEEK_SET || ptrname == SEEK_CUR || ptrname == SEEK_END); errno = 0; @@ -1366,8 +1317,7 @@ Nu_FSeek(FILE* fp, long offset, int ptrname) * Wrapper for fread(). Note the arguments resemble read(2) rather than the * slightly silly ones used by fread(3S). */ -NuError -Nu_FRead(FILE* fp, void* buf, size_t nbyte) +NuError Nu_FRead(FILE* fp, void* buf, size_t nbyte) { size_t result; @@ -1382,8 +1332,7 @@ Nu_FRead(FILE* fp, void* buf, size_t nbyte) * Wrapper for fwrite(). Note the arguments resemble write(2) rather than the * slightly silly ones used by fwrite(3S). */ -NuError -Nu_FWrite(FILE* fp, const void* buf, size_t nbyte) +NuError Nu_FWrite(FILE* fp, const void* buf, size_t nbyte) { size_t result; @@ -1403,15 +1352,15 @@ Nu_FWrite(FILE* fp, const void* buf, size_t nbyte) /* * Copy a section from one file to another. */ -NuError -Nu_CopyFileSection(NuArchive* pArchive, FILE* dstFp, FILE* srcFp, long length) +NuError Nu_CopyFileSection(NuArchive* pArchive, FILE* dstFp, FILE* srcFp, + long length) { NuError err; long readLen; - Assert(pArchive != nil); - Assert(dstFp != nil); - Assert(srcFp != nil); + Assert(pArchive != NULL); + Assert(dstFp != NULL); + Assert(srcFp != NULL); Assert(length >= 0); /* can be == 0, e.g. empty data fork from HFS */ /* nice big buffer, for speed... could use getc/putc for simplicity */ @@ -1450,16 +1399,15 @@ bail: * Only useful for files < 2GB in size. * * (pArchive is only used for BailError message reporting, so it's okay - * to call here with a nil pointer if the archive isn't open yet.) + * to call here with a NULL pointer if the archive isn't open yet.) */ -NuError -Nu_GetFileLength(NuArchive* pArchive, FILE* fp, long* pLength) +NuError Nu_GetFileLength(NuArchive* pArchive, FILE* fp, long* pLength) { NuError err; long oldpos; - Assert(fp != nil); - Assert(pLength != nil); + Assert(fp != NULL); + Assert(pLength != NULL); err = Nu_FTell(fp, &oldpos); BailError(err); @@ -1482,8 +1430,7 @@ bail: * Truncate an open file. This differs from ftruncate() in that it takes * a FILE* instead of an fd, and the length is a long instead of off_t. */ -NuError -Nu_TruncateOpenFile(FILE* fp, long length) +NuError Nu_TruncateOpenFile(FILE* fp, long length) { #if defined(HAVE_FTRUNCATE) if (ftruncate(fileno(fp), length) < 0) diff --git a/nufxlib/Funnel.c b/nufxlib/Funnel.c index 4762d96..58a0797 100644 --- a/nufxlib/Funnel.c +++ b/nufxlib/Funnel.c @@ -22,38 +22,36 @@ * The same structure will be used when expanding all threads in a given * record. */ -NuError -Nu_ProgressDataInit_Compress(NuArchive* pArchive, NuProgressData* pProgressData, - const NuRecord* pRecord, const char* origPathname) +NuError Nu_ProgressDataInit_Compress(NuArchive* pArchive, + NuProgressData* pProgressData, const NuRecord* pRecord, + const UNICHAR* origPathnameUNI, const UNICHAR* pathnameUNI) { const char* cp; - Assert(pProgressData != nil); - Assert(pArchive != nil); - Assert(pRecord != nil); - Assert(origPathname != nil); + Assert(pProgressData != NULL); + Assert(pArchive != NULL); + Assert(pRecord != NULL); + Assert(origPathnameUNI != NULL); + Assert(pathnameUNI != NULL); pProgressData->pRecord = pRecord; - pProgressData->origPathname = origPathname; - pProgressData->pathname = pRecord->filename; - cp = strrchr(pRecord->filename, - NuGetSepFromSysInfo(pRecord->recFileSysInfo)); - if (cp == nil || *(cp+1) == '\0') - pProgressData->filename = pProgressData->pathname; + pProgressData->origPathnameUNI = origPathnameUNI; + pProgressData->pathnameUNI = pathnameUNI; + cp = strrchr(pathnameUNI, NuGetSepFromSysInfo(pRecord->recFileSysInfo)); + if (cp == NULL || *(cp+1) == '\0') + pProgressData->filenameUNI = pProgressData->pathnameUNI; else - pProgressData->filename = cp+1; + pProgressData->filenameUNI = cp+1; pProgressData->operation = kNuOpAdd; pProgressData->state = kNuProgressPreparing; - /*pProgressData->compressedLength = 0;*/ - /*pProgressData->compressedProgress = 0;*/ pProgressData->uncompressedLength = 0; pProgressData->uncompressedProgress = 0; pProgressData->compress.threadFormat = (NuThreadFormat)-1; - /* ya know... if this is nil, none of the above matters much */ + /* ya know... if this is NULL, none of the above matters much */ pProgressData->progressFunc = pArchive->progressUpdaterFunc; return kNuErrNone; @@ -67,31 +65,32 @@ Nu_ProgressDataInit_Compress(NuArchive* pArchive, NuProgressData* pProgressData, * The same structure will be used when expanding all threads in a given * record. */ -NuError -Nu_ProgressDataInit_Expand(NuArchive* pArchive, NuProgressData* pProgressData, - const NuRecord* pRecord, const char* newPathname, char newFssep, - NuValue convertEOL) +NuError Nu_ProgressDataInit_Expand(NuArchive* pArchive, + NuProgressData* pProgressData, const NuRecord* pRecord, + const UNICHAR* newPathnameUNI, UNICHAR newFssep, + const UNICHAR* origPathnameUNI, NuValue convertEOL) { const NuThread* pThreadIter; const char* cp; int i; - Assert(pProgressData != nil); - Assert(pArchive != nil); - Assert(pRecord != nil); - Assert(newPathname != nil); + Assert(pProgressData != NULL); + Assert(pArchive != NULL); + Assert(pRecord != NULL); + Assert(newPathnameUNI != NULL); + Assert(origPathnameUNI != NULL); Assert(newFssep != 0); pProgressData->pRecord = pRecord; - pProgressData->expand.pThread = nil; + pProgressData->expand.pThread = NULL; - pProgressData->origPathname = pRecord->filename; - pProgressData->pathname = newPathname; - cp = strrchr(newPathname, newFssep); - if (cp == nil || *(cp+1) == '\0') - pProgressData->filename = newPathname; + pProgressData->origPathnameUNI = origPathnameUNI; + pProgressData->pathnameUNI = newPathnameUNI; + cp = strrchr(newPathnameUNI, newFssep); + if (cp == NULL || *(cp+1) == '\0') + pProgressData->filenameUNI = newPathnameUNI; else - pProgressData->filename = cp+1; + pProgressData->filenameUNI = cp+1; pProgressData->expand.convertEOL = convertEOL; @@ -111,12 +110,10 @@ Nu_ProgressDataInit_Expand(NuArchive* pArchive, NuProgressData* pProgressData, if (pArchive->testMode) pProgressData->operation = kNuOpTest; pProgressData->state = kNuProgressPreparing; - /*pProgressData->expand.compressedLength = 0;*/ - /*pProgressData->expand.compressedProgress = 0;*/ pProgressData->uncompressedLength = 0; pProgressData->uncompressedProgress = 0; - /* ya know... if this is nil, none of the above matters much */ + /* ya know... if this is NULL, none of the above matters much */ pProgressData->progressFunc = pArchive->progressUpdaterFunc; return kNuErrNone; @@ -126,18 +123,17 @@ Nu_ProgressDataInit_Expand(NuArchive* pArchive, NuProgressData* pProgressData, /* * Do the setup on a ProgressData prior to compressing a thread. */ -NuError -Nu_ProgressDataCompressPrep(NuArchive* pArchive, NuStraw* pStraw, - NuThreadFormat threadFormat, ulong sourceLen) +NuError Nu_ProgressDataCompressPrep(NuArchive* pArchive, NuStraw* pStraw, + NuThreadFormat threadFormat, uint32_t sourceLen) { NuProgressData* pProgressData; - Assert(pArchive != nil); - Assert(pStraw != nil); + Assert(pArchive != NULL); + Assert(pStraw != NULL); Assert(sourceLen < 32767*65536); pProgressData = pStraw->pProgress; - if (pProgressData == nil) + if (pProgressData == NULL) return kNuErrNone; pProgressData->uncompressedLength = sourceLen; @@ -151,18 +147,17 @@ Nu_ProgressDataCompressPrep(NuArchive* pArchive, NuStraw* pStraw, * * "pThread" is the thread being expanded. */ -NuError -Nu_ProgressDataExpandPrep(NuArchive* pArchive, NuFunnel* pFunnel, +NuError Nu_ProgressDataExpandPrep(NuArchive* pArchive, NuFunnel* pFunnel, const NuThread* pThread) { NuProgressData* pProgressData; - Assert(pArchive != nil); - Assert(pFunnel != nil); - Assert(pThread != nil); + Assert(pArchive != NULL); + Assert(pFunnel != NULL); + Assert(pThread != NULL); pProgressData = pFunnel->pProgress; - if (pProgressData == nil) + if (pProgressData == NULL) return kNuErrNone; /*pProgressData->compressedLength = pThread->thCompThreadEOF;*/ @@ -175,10 +170,9 @@ Nu_ProgressDataExpandPrep(NuArchive* pArchive, NuFunnel* pFunnel, /* * Compute a completion percentage. */ -static int -Nu_ComputePercent(ulong total, ulong progress) +static int Nu_ComputePercent(uint32_t total, uint32_t progress) { - ulong perc; + uint32_t perc; if (!total) return 0; @@ -200,15 +194,14 @@ Nu_ComputePercent(ulong total, ulong progress) * Send the initial progress message, before the output file is opened * (when extracting) or the input file is opened (when adding). */ -NuError -Nu_SendInitialProgress(NuArchive* pArchive, NuProgressData* pProgress) +NuError Nu_SendInitialProgress(NuArchive* pArchive, NuProgressData* pProgress) { NuResult result; - Assert(pArchive != nil); - Assert(pProgress != nil); + Assert(pArchive != NULL); + Assert(pProgress != NULL); - if (pProgress->progressFunc == nil) + if (pProgress->progressFunc == NULL) return kNuErrNone; pProgress->percentComplete = Nu_ComputePercent( @@ -234,15 +227,15 @@ Nu_SendInitialProgress(NuArchive* pArchive, NuProgressData* pProgress) /* * Allocate and initialize a Funnel. */ -NuError -Nu_FunnelNew(NuArchive* pArchive, NuDataSink* pDataSink, NuValue convertEOL, - NuValue convertEOLTo, NuProgressData* pProgress, NuFunnel** ppFunnel) +NuError Nu_FunnelNew(NuArchive* pArchive, NuDataSink* pDataSink, + NuValue convertEOL, NuValue convertEOLTo, NuProgressData* pProgress, + NuFunnel** ppFunnel) { NuError err = kNuErrNone; - NuFunnel* pFunnel = nil; + NuFunnel* pFunnel = NULL; - Assert(ppFunnel != nil); - Assert(pDataSink != nil); + Assert(ppFunnel != NULL); + Assert(pDataSink != NULL); Assert(convertEOL == kNuConvertOff || convertEOL == kNuConvertOn || convertEOL == kNuConvertAuto); @@ -278,10 +271,9 @@ bail: * The data should already have been written; it's not the duty of a * "free" function to flush data out. */ -NuError -Nu_FunnelFree(NuArchive* pArchive, NuFunnel* pFunnel) +NuError Nu_FunnelFree(NuArchive* pArchive, NuFunnel* pFunnel) { - if (pFunnel == nil) + if (pFunnel == NULL) return kNuErrNone; #ifdef DEBUG_MSGS @@ -304,10 +296,9 @@ Nu_FunnelFree(NuArchive* pArchive, NuFunnel* pFunnel) * allows us to bail out as soon as it's apparent that compression is * failing and is actually resulting in a larger file. */ -void -Nu_FunnelSetMaxOutput(NuFunnel* pFunnel, ulong maxBytes) +void Nu_FunnelSetMaxOutput(NuFunnel* pFunnel, uint32_t maxBytes) { - Assert(pFunnel != nil); + Assert(pFunnel != NULL); Assert(maxBytes > 0); pFunnel->outMax = maxBytes; @@ -324,13 +315,12 @@ Nu_FunnelSetMaxOutput(NuFunnel* pFunnel, ulong maxBytes) * character must have its high bit set, except for spaces (0x20). * (The exception is courtesy Glen Bredon's "Merlin".) */ -static Boolean -Nu_CheckHighASCII(const NuFunnel* pFunnel, const unsigned char* buffer, - unsigned long count) +static Boolean Nu_CheckHighASCII(const NuFunnel* pFunnel, const uint8_t* buffer, + uint32_t count) { Boolean isHighASCII; - Assert(buffer != nil); + Assert(buffer != NULL); Assert(count != 0); Assert(pFunnel->checkStripHighASCII); @@ -405,12 +395,12 @@ static const char gNuIsBinary[256] = { * Returns kConvEOLOff or kConvEOLOn, and sets pFunnel->doStripHighASCII * if pFunnel->CheckStripHighASCII is set. */ -static NuValue -Nu_DetermineConversion(NuFunnel* pFunnel, const uchar* buffer, ulong count) +static NuValue Nu_DetermineConversion(NuFunnel* pFunnel, const uint8_t* buffer, + uint32_t count) { - ulong bufCount, numBinary, numLF, numCR; + uint32_t bufCount, numBinary, numLF, numCR; Boolean isHighASCII; - uchar val; + uint8_t val; if (count < kNuMinConvThreshold) return kNuConvertOff; @@ -484,12 +474,12 @@ Nu_DetermineConversion(NuFunnel* pFunnel, const uchar* buffer, ulong count) * This is either a Funnel function or a DataSink function, depending on * your perspective. */ -static inline void -Nu_FunnelPutBlock(NuFunnel* pFunnel, const uchar* buf, ulong len) +static inline void Nu_FunnelPutBlock(NuFunnel* pFunnel, const uint8_t* buf, + uint32_t len) { - Assert(pFunnel != nil); - Assert(pFunnel->pDataSink != nil); - Assert(buf != nil); + Assert(pFunnel != NULL); + Assert(pFunnel->pDataSink != NULL); + Assert(buf != NULL); Assert(len > 0); #if 0 @@ -511,10 +501,9 @@ Nu_FunnelPutBlock(NuFunnel* pFunnel, const uchar* buf, ulong len) /* * Output the EOL marker requested for this system. */ -static inline void -Nu_PutEOL(NuFunnel* pFunnel) +static inline void Nu_PutEOL(NuFunnel* pFunnel) { - uchar ch; + uint8_t ch; if (pFunnel->convertEOLTo == kNuEOLCR) { ch = kNuCharCR; @@ -540,11 +529,11 @@ Nu_PutEOL(NuFunnel* pFunnel) * that looks like an EOL mark and convert it. Doesn't matter if it's * CR, LF, or CRLF; all three get converted to whatever the system uses. */ -static NuError -Nu_FunnelWriteConvert(NuFunnel* pFunnel, const uchar* buffer, ulong count) +static NuError Nu_FunnelWriteConvert(NuFunnel* pFunnel, const uint8_t* buffer, + uint32_t count) { NuError err = kNuErrNone; - ulong progressCount = count; + uint32_t progressCount = count; /*if (pFunnel->outMaxExceeded) return kNuErrOutMax;*/ @@ -566,7 +555,7 @@ Nu_FunnelWriteConvert(NuFunnel* pFunnel, const uchar* buffer, ulong count) pFunnel->convertEOL = kNuConvertOff; } /* put it where the progress meter can see it */ - if (pFunnel->pProgress != nil) + if (pFunnel->pProgress != NULL) pFunnel->pProgress->expand.convertEOL = pFunnel->convertEOL; } else if (pFunnel->convertEOL == kNuConvertOn) { if (pFunnel->checkStripHighASCII) { @@ -589,7 +578,7 @@ Nu_FunnelWriteConvert(NuFunnel* pFunnel, const uchar* buffer, ulong count) } else { /* do the EOL conversion and optional high-bit stripping */ Boolean lastCR = pFunnel->lastCR; /* make local copy */ - uchar uch; + uint8_t uch; int mask; if (pFunnel->doStripHighASCII) @@ -627,7 +616,7 @@ Nu_FunnelWriteConvert(NuFunnel* pFunnel, const uchar* buffer, ulong count) err = Nu_DataSinkGetError(pFunnel->pDataSink); /* update progress counter with pre-LFCR count */ - if (err == kNuErrNone && pFunnel->pProgress != nil) + if (err == kNuErrNone && pFunnel->pProgress != NULL) pFunnel->pProgress->uncompressedProgress += progressCount; return err; @@ -637,8 +626,7 @@ Nu_FunnelWriteConvert(NuFunnel* pFunnel, const uchar* buffer, ulong count) /* * Flush any data currently in the funnel. */ -NuError -Nu_FunnelFlush(NuArchive* pArchive, NuFunnel* pFunnel) +NuError Nu_FunnelFlush(NuArchive* pArchive, NuFunnel* pFunnel) { NuError err = kNuErrNone; @@ -661,9 +649,8 @@ bail: * Write a bunch of bytes into a funnel. They will be held in the buffer * if they fit, or flushed out the bottom if not. */ -NuError -Nu_FunnelWrite(NuArchive* pArchive, NuFunnel* pFunnel, const uchar* buffer, - ulong count) +NuError Nu_FunnelWrite(NuArchive* pArchive, NuFunnel* pFunnel, + const uint8_t* buffer, uint32_t count) { NuError err = kNuErrNone; @@ -714,12 +701,11 @@ bail: /* * Set the Funnel's progress state. */ -NuError -Nu_FunnelSetProgressState(NuFunnel* pFunnel, NuProgressState state) +NuError Nu_FunnelSetProgressState(NuFunnel* pFunnel, NuProgressState state) { - Assert(pFunnel != nil); + Assert(pFunnel != NULL); - if (pFunnel->pProgress == nil) + if (pFunnel->pProgress == NULL) return kNuErrNone; pFunnel->pProgress->state = state; @@ -731,20 +717,19 @@ Nu_FunnelSetProgressState(NuFunnel* pFunnel, NuProgressState state) /* * Send a progress update to the application, if they're interested. */ -NuError -Nu_FunnelSendProgressUpdate(NuArchive* pArchive, NuFunnel* pFunnel) +NuError Nu_FunnelSendProgressUpdate(NuArchive* pArchive, NuFunnel* pFunnel) { NuProgressData* pProgress; - Assert(pArchive != nil); - Assert(pFunnel != nil); + Assert(pArchive != NULL); + Assert(pFunnel != NULL); pProgress = pFunnel->pProgress; - if (pProgress == nil) + if (pProgress == NULL) return kNuErrNone; /* no progress meter attached */ /* don't continue if they're not accepting progress messages */ - if (pProgress->progressFunc == nil) + if (pProgress->progressFunc == NULL) return kNuErrNone; /* other than the choice of arguments, it's pretty much the same story */ @@ -755,11 +740,10 @@ Nu_FunnelSendProgressUpdate(NuArchive* pArchive, NuFunnel* pFunnel) /* * Pull the "doExpand" parameter out of the data source. */ -Boolean -Nu_FunnelGetDoExpand(NuFunnel* pFunnel) +Boolean Nu_FunnelGetDoExpand(NuFunnel* pFunnel) { - Assert(pFunnel != nil); - Assert(pFunnel->pDataSink != nil); + Assert(pFunnel != NULL); + Assert(pFunnel->pDataSink != NULL); return Nu_DataSinkGetDoExpand(pFunnel->pDataSink); } @@ -774,15 +758,14 @@ Nu_FunnelGetDoExpand(NuFunnel* pFunnel) /* * Allocate and initialize a Straw. */ -NuError -Nu_StrawNew(NuArchive* pArchive, NuDataSource* pDataSource, +NuError Nu_StrawNew(NuArchive* pArchive, NuDataSource* pDataSource, NuProgressData* pProgress, NuStraw** ppStraw) { NuError err = kNuErrNone; - NuStraw* pStraw = nil; + NuStraw* pStraw = NULL; - Assert(ppStraw != nil); - Assert(pDataSource != nil); + Assert(ppStraw != NULL); + Assert(pDataSource != NULL); pStraw = Nu_Calloc(pArchive, sizeof(*pStraw)); BailAlloc(pStraw); @@ -802,10 +785,9 @@ bail: /* * Free a Straw. */ -NuError -Nu_StrawFree(NuArchive* pArchive, NuStraw* pStraw) +NuError Nu_StrawFree(NuArchive* pArchive, NuStraw* pStraw) { - if (pStraw == nil) + if (pStraw == NULL) return kNuErrNone; /* we don't own the data source or progress meter */ @@ -818,11 +800,10 @@ Nu_StrawFree(NuArchive* pArchive, NuStraw* pStraw) /* * Set the Straw's progress state. */ -NuError -Nu_StrawSetProgressState(NuStraw* pStraw, NuProgressState state) +NuError Nu_StrawSetProgressState(NuStraw* pStraw, NuProgressState state) { - Assert(pStraw != nil); - Assert(pStraw->pProgress != nil); + Assert(pStraw != NULL); + Assert(pStraw->pProgress != NULL); pStraw->pProgress->state = state; @@ -832,20 +813,19 @@ Nu_StrawSetProgressState(NuStraw* pStraw, NuProgressState state) /* * Send a progress update to the application, if they're interested. */ -NuError -Nu_StrawSendProgressUpdate(NuArchive* pArchive, NuStraw* pStraw) +NuError Nu_StrawSendProgressUpdate(NuArchive* pArchive, NuStraw* pStraw) { NuProgressData* pProgress; - Assert(pArchive != nil); - Assert(pStraw != nil); + Assert(pArchive != NULL); + Assert(pStraw != NULL); pProgress = pStraw->pProgress; - if (pProgress == nil) + if (pProgress == NULL) return kNuErrNone; /* no progress meter attached */ /* don't continue if they're not accepting progress messages */ - if (pProgress->progressFunc == nil) + if (pProgress->progressFunc == NULL) return kNuErrNone; /* other than the choice of arguments, it's pretty much the same story */ @@ -856,14 +836,14 @@ Nu_StrawSendProgressUpdate(NuArchive* pArchive, NuStraw* pStraw) /* * Read data from a straw. */ -NuError -Nu_StrawRead(NuArchive* pArchive, NuStraw* pStraw, uchar* buffer, long len) +NuError Nu_StrawRead(NuArchive* pArchive, NuStraw* pStraw, uint8_t* buffer, + long len) { NuError err; - Assert(pArchive != nil); - Assert(pStraw != nil); - Assert(buffer != nil); + Assert(pArchive != NULL); + Assert(pStraw != NULL); + Assert(buffer != NULL); Assert(len > 0); /* @@ -887,7 +867,7 @@ Nu_StrawRead(NuArchive* pArchive, NuStraw* pStraw, uchar* buffer, long len) * on the previous call. (This assumes that whatever they asked for * last time has already been fully processed.) */ - if (pStraw->pProgress != nil) { + if (pStraw->pProgress != NULL) { pStraw->pProgress->uncompressedProgress = pStraw->lastProgress; pStraw->lastProgress += len; @@ -911,11 +891,10 @@ bail: * Rewind a straw. This rewinds the underlying data source, and resets * some progress counters. */ -NuError -Nu_StrawRewind(NuArchive* pArchive, NuStraw* pStraw) +NuError Nu_StrawRewind(NuArchive* pArchive, NuStraw* pStraw) { - Assert(pStraw != nil); - Assert(pStraw->pDataSource != nil); + Assert(pStraw != NULL); + Assert(pStraw->pDataSource != NULL); pStraw->lastProgress = 0; pStraw->lastDisplayed = 0; diff --git a/nufxlib/Lzc.c b/nufxlib/Lzc.c index ded5168..5798ba3 100644 --- a/nufxlib/Lzc.c +++ b/nufxlib/Lzc.c @@ -25,10 +25,10 @@ /* * Selected definitions from compress.h. */ -typedef unsigned short CODE; -typedef unsigned char UCHAR; -typedef unsigned int INTCODE; -typedef unsigned int HASH; +typedef uint16_t CODE; +typedef uint8_t UCHAR; +typedef uint32_t INTCODE; +typedef uint32_t HASH; typedef int FLAG; #ifndef FALSE /* let's get some sense to this */ @@ -80,7 +80,7 @@ static UCHAR gNu_magic_header[] = { 0x1F,0x9D }; * Normally in COMPUSI.UNI. */ static inline ALLOCTYPE FAR * -Nu_LZC_emalloc(NuArchive* pArchive, unsigned int x, int y) +Nu_LZC_emalloc(NuArchive* pArchive, uint32_t x, int y) { return Nu_Malloc(pArchive, x*y); } @@ -153,7 +153,7 @@ Nu_LZC_efree(NuArchive* pArchive, ALLOCTYPE FAR * ptr) typedef struct LZCState { NuArchive* pArchive; int doCalcCRC; - ushort crc; + uint16_t crc; /* compression */ NuStraw* pStraw; @@ -163,7 +163,7 @@ typedef struct LZCState { /* expansion */ FILE* infp; NuFunnel* pFunnel; - ushort* pCrc; + uint16_t* pCrc; long compRemaining; @@ -251,18 +251,18 @@ static CONST INTCODE gNu_mc[] = { #ifdef __STDC__ #ifdef DEBUG_LZC #define allocx(type, ptr, size) \ - (((ptr) = (type FAR *) Nu_LZC_emalloc(pArchive, (unsigned int)(size),sizeof(type))) == NULLPTR(type) \ + (((ptr) = (type FAR *) Nu_LZC_emalloc(pArchive, (uint32_t)(size),sizeof(type))) == NULLPTR(type) \ ? (DBUG(("%s: "#ptr" -- ", "LZC")), NOMEM) : OK \ ) #else #define allocx(type,ptr,size) \ - (((ptr) = (type FAR *) Nu_LZC_emalloc(pArchive, (unsigned int)(size),sizeof(type))) == NULLPTR(type) \ + (((ptr) = (type FAR *) Nu_LZC_emalloc(pArchive, (uint32_t)(size),sizeof(type))) == NULLPTR(type) \ ? NOMEM : OK \ ) #endif #else #define allocx(type,ptr,size) \ - (((ptr) = (type FAR *) Nu_LZC_emalloc(pArchive, (unsigned int)(size),sizeof(type))) == NULLPTR(type) \ + (((ptr) = (type FAR *) Nu_LZC_emalloc(pArchive, (uint32_t)(size),sizeof(type))) == NULLPTR(type) \ ? NOMEM : OK \ ) #endif @@ -300,8 +300,8 @@ static CONST INTCODE gNu_mc[] = { #endif -static int -Nu_LZC_alloc_tables(LZCState* pLzcState, INTCODE newmaxcode, HASH newhashsize) +static int Nu_LZC_alloc_tables(LZCState* pLzcState, INTCODE newmaxcode, + HASH newhashsize) { NuArchive* pArchive = pLzcState->pArchive; /*static INTCODE oldmaxcode = 0;*/ @@ -421,8 +421,7 @@ Nu_LZC_alloc_tables(LZCState* pLzcState, INTCODE newmaxcode, HASH newhashsize) * =========================================================================== */ -static void -Nu_prratio(long int num, long int den) +static void Nu_prratio(long int num, long int den) { register int q; /* Doesn't need to be long */ @@ -443,8 +442,7 @@ Nu_prratio(long int num, long int den) /* table clear for block compress */ /* this is for adaptive reset present in version 4.0 joe release */ /* DjG, sets it up and returns TRUE to compress and FALSE to not compress */ -static int -Nu_LZC_cl_block(LZCState* pLzcState) +static int Nu_LZC_cl_block(LZCState* pLzcState) { register long int rat; @@ -486,8 +484,7 @@ Nu_LZC_cl_block(LZCState* pLzcState) static CONST UCHAR gNu_rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; -static void -Nu_LZC_putcode(LZCState* pLzcState, INTCODE code, register int bits) +static void Nu_LZC_putcode(LZCState* pLzcState, INTCODE code, register int bits) { /*static int oldbits = 0;*/ /*static UCHAR outbuf[MAXBITS];*/ @@ -554,11 +551,10 @@ Nu_LZC_putcode(LZCState* pLzcState, INTCODE code, register int bits) * * Returns kNuLZCEOF as the value when we're out of data. */ -static NuError -Nu_LZCGetcCRC(LZCState* pLzcState, int* pSym) +static NuError Nu_LZCGetcCRC(LZCState* pLzcState, int* pSym) { NuError err; - uchar c; + uint8_t c; if (!pLzcState->uncompRemaining) { *pSym = kNuLZCEOF; @@ -579,15 +575,14 @@ Nu_LZCGetcCRC(LZCState* pLzcState, int* pSym) /* * compress stdin to stdout */ -static void -Nu_LZC_compress(LZCState* pLzcState, ulong* pDstLen) +static void Nu_LZC_compress(LZCState* pLzcState, uint32_t* pDstLen) { int c,adjbits; register HASH hash; register INTCODE code; HASH hashf[256]; - Assert(pLzcState->outfp != nil); + Assert(pLzcState->outfp != NULL); pLzcState->maxcode = Maxcode(pLzcState->maxbits); pLzcState->hashsize = Hashsize(pLzcState->maxbits); @@ -758,9 +753,8 @@ Nu_LZC_compress(LZCState* pLzcState, ulong* pDstLen) /* * NufxLib interface to LZC compression. */ -static NuError -Nu_CompressLZC(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc, int maxbits) +static NuError Nu_CompressLZC(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc, int maxbits) { NuError err = kNuErrNone; LZCState lzcState; @@ -771,7 +765,7 @@ Nu_CompressLZC(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, lzcState.outfp = fp; lzcState.uncompRemaining = srcLen; - if (pCrc == nil) { + if (pCrc == NULL) { lzcState.doCalcCRC = false; } else { lzcState.doCalcCRC = true; @@ -800,22 +794,20 @@ Nu_CompressLZC(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, #endif free_array(char,lzcState.sfx, 256); - if (pCrc != nil) + if (pCrc != NULL) *pCrc = lzcState.crc; return err; } -NuError -Nu_CompressLZC12(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc) +NuError Nu_CompressLZC12(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc) { return Nu_CompressLZC(pArchive, pStraw, fp, srcLen, pDstLen, pCrc, 12); } -NuError -Nu_CompressLZC16(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc) +NuError Nu_CompressLZC16(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc) { return Nu_CompressLZC(pArchive, pStraw, fp, srcLen, pDstLen, pCrc, 16); } @@ -833,22 +825,20 @@ Nu_CompressLZC16(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, * * Returns kNuLZCEOF as the value when we're out of data. */ -static NuError -Nu_LZCPutcCRC(LZCState* pLzcState, char c) +static NuError Nu_LZCPutcCRC(LZCState* pLzcState, char c) { NuError err; err = Nu_FunnelWrite(pLzcState->pArchive, pLzcState->pFunnel, - (uchar*) &c, 1); + (uint8_t*) &c, 1); if (pLzcState->doCalcCRC) - pLzcState->crc = Nu_CalcCRC16(pLzcState->crc, (uchar*) &c, 1); + pLzcState->crc = Nu_CalcCRC16(pLzcState->crc, (uint8_t*) &c, 1); return err; } -static int -Nu_LZC_nextcode(LZCState* pLzcState, INTCODE* codeptr) +static int Nu_LZC_nextcode(LZCState* pLzcState, INTCODE* codeptr) /* Get the next code from input and put it in *codeptr. * Return (TRUE) on success, or return (FALSE) on end-of-file. * Adapted from COMPRESS V4.0. @@ -898,8 +888,7 @@ Nu_LZC_nextcode(LZCState* pLzcState, INTCODE* codeptr) return (TRUE); } -static void -Nu_LZC_decompress(LZCState* pLzcState, ulong compressedLen) +static void Nu_LZC_decompress(LZCState* pLzcState, uint32_t compressedLen) { NuArchive* pArchive = pLzcState->pArchive; register int i; @@ -911,7 +900,7 @@ Nu_LZC_decompress(LZCState* pLzcState, ulong compressedLen) /*static*/ int maxtoklen = MAXTOKLEN; int flags; - Assert(pLzcState->infp != nil); + Assert(pLzcState->infp != NULL); pLzcState->exit_stat = OK; @@ -1060,9 +1049,8 @@ Nu_LZC_decompress(LZCState* pLzcState, ulong compressedLen) /* * NufxLib interface to LZC expansion. */ -NuError -Nu_ExpandLZC(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pCrc) +NuError Nu_ExpandLZC(NuArchive* pArchive, const NuRecord* pRecord, + const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, uint16_t* pCrc) { NuError err = kNuErrNone; LZCState lzcState; @@ -1072,7 +1060,7 @@ Nu_ExpandLZC(NuArchive* pArchive, const NuRecord* pRecord, lzcState.infp = infp; lzcState.pFunnel = pFunnel; - if (pCrc == nil) { + if (pCrc == NULL) { lzcState.doCalcCRC = false; } else { lzcState.doCalcCRC = true; @@ -1098,7 +1086,7 @@ Nu_ExpandLZC(NuArchive* pArchive, const NuRecord* pRecord, #endif free_array(char,lzcState.sfx, 256); - if (pCrc != nil) + if (pCrc != NULL) *pCrc = lzcState.crc; return err; } diff --git a/nufxlib/Lzw.c b/nufxlib/Lzw.c index 14934a9..69ff120 100644 --- a/nufxlib/Lzw.c +++ b/nufxlib/Lzw.c @@ -119,17 +119,17 @@ static const int gNuBitMask[] = { typedef struct LZWCompressState { NuArchive* pArchive; - ushort entry[kNuLZWHashSize]; /* uint or ushort */ - ushort prefix[kNuLZWMaxCode+1]; /* uint or ushort */ - uchar suffix[kNuLZWMaxCode+1]; + uint16_t entry[kNuLZWHashSize]; /* uint or ushort */ + uint16_t prefix[kNuLZWMaxCode+1]; /* uint or ushort */ + uint8_t suffix[kNuLZWMaxCode+1]; - ushort hashFunc[kNuLZWHashFuncTblSize]; /* uint or ushort */ + uint16_t hashFunc[kNuLZWHashFuncTblSize]; /* uint or ushort */ - uchar inputBuf[kNuLZWBlockSize]; /* 4K of raw input */ - uchar rleBuf[kNuLZWBlockSize*2 + kNuSafetyPadding]; - uchar lzwBuf[(kNuLZWBlockSize * 3) / 2 + kNuSafetyPadding]; + uint8_t inputBuf[kNuLZWBlockSize]; /* 4K of raw input */ + uint8_t rleBuf[kNuLZWBlockSize*2 + kNuSafetyPadding]; + uint8_t lzwBuf[(kNuLZWBlockSize * 3) / 2 + kNuSafetyPadding]; - ushort chunkCrc; /* CRC for LZW/1 */ + uint16_t chunkCrc; /* CRC for LZW/1 */ /* LZW/2 state variables */ int nextFree; @@ -146,15 +146,14 @@ typedef struct LZWCompressState { * the hash function. This way we don't have to re-create it for * every file, or store it statically in the binary. */ -static NuError -Nu_AllocLZWCompressState(NuArchive* pArchive) +static NuError Nu_AllocLZWCompressState(NuArchive* pArchive) { NuError err; LZWCompressState* lzwState; int ic; - Assert(pArchive != nil); - Assert(pArchive->lzwCompressState == nil); + Assert(pArchive != NULL); + Assert(pArchive->lzwCompressState == NULL); /* allocate the general-purpose compression buffer, if needed */ err = Nu_AllocCompressionBufferIFN(pArchive); @@ -162,7 +161,7 @@ Nu_AllocLZWCompressState(NuArchive* pArchive) return err; pArchive->lzwCompressState = Nu_Malloc(pArchive, sizeof(LZWCompressState)); - if (pArchive->lzwCompressState == nil) + if (pArchive->lzwCompressState == NULL) return kNuErrMalloc; /* @@ -191,13 +190,12 @@ Nu_AllocLZWCompressState(NuArchive* pArchive) * The RLE format is " ", where count is zero-based * (i.e. for three bytes we encode "2", allowing us to express 1-256). */ -static NuError -Nu_CompressBlockRLE(LZWCompressState* lzwState, int* pRLESize) +static NuError Nu_CompressBlockRLE(LZWCompressState* lzwState, int* pRLESize) { - const uchar* inPtr = lzwState->inputBuf; - const uchar* endPtr = inPtr + kNuLZWBlockSize; - uchar* outPtr = lzwState->rleBuf; - uchar matchChar; + const uint8_t* inPtr = lzwState->inputBuf; + const uint8_t* endPtr = inPtr + kNuLZWBlockSize; + uint8_t* outPtr = lzwState->rleBuf; + uint8_t matchChar; int matchCount; while (inPtr < endPtr) { @@ -258,10 +256,9 @@ Nu_CompressBlockRLE(LZWCompressState* lzwState, int* pRLESize) /* * Clear the LZW table. Also resets the LZW/2 state. */ -static void -Nu_ClearLZWTable(LZWCompressState* lzwState) +static void Nu_ClearLZWTable(LZWCompressState* lzwState) { - Assert(lzwState != nil); + Assert(lzwState != NULL); /*DBUG_LZW(("### clear table\n"));*/ @@ -296,11 +293,11 @@ Nu_ClearLZWTable(LZWCompressState* lzwState) * * (Turning this into a macro might speed things up.) */ -static inline void -Nu_LZWPutCode(uchar** pOutBuf, ulong prefixCode, int codeBits, int* pAtBit) +static inline void Nu_LZWPutCode(uint8_t** pOutBuf, uint32_t prefixCode, + int codeBits, int* pAtBit) { int atBit = *pAtBit; - uchar* outBuf = *pOutBuf; + uint8_t* outBuf = *pOutBuf; /*DBUG_LZW(("### PUT: prefixCode=0x%04lx, codeBits=%d, atBit=%d\n", prefixCode, codeBits, atBit));*/ @@ -313,20 +310,20 @@ Nu_LZWPutCode(uchar** pOutBuf, ulong prefixCode, int codeBits, int* pAtBit) /* merge it with the buffer contents (if necessary) and write lo bits */ outBuf--; - *outBuf = (uchar)((*outBuf & gNuBitMask[atBit]) | prefixCode); + *outBuf = (uint8_t)((*outBuf & gNuBitMask[atBit]) | prefixCode); outBuf++; } else { /* nothing to merge with; write lo byte at next posn and advance */ - *outBuf++ = (uchar)prefixCode; + *outBuf++ = (uint8_t)prefixCode; } /* codes are at least 9 bits, so we know we have to write one more */ - *outBuf++ = (uchar)(prefixCode >> 8); + *outBuf++ = (uint8_t)(prefixCode >> 8); /* in some cases, we may have to write yet another */ atBit += codeBits; if (atBit > 16) - *outBuf++ = (uchar)(prefixCode >> 16); + *outBuf++ = (uint8_t)(prefixCode >> 16); *pAtBit = atBit & 0x07; *pOutBuf = outBuf; @@ -353,23 +350,22 @@ Nu_LZWPutCode(uchar** pOutBuf, ulong prefixCode, int codeBits, int* pAtBit) * "resetFix" logic in the expansion functions. Code 0x0101 is essentially * lost in this situation. */ -static NuError -Nu_CompressLZWBlock(LZWCompressState* lzwState, const uchar* inputBuf, - int inputCount, int* pOutputCount) +static NuError Nu_CompressLZWBlock(LZWCompressState* lzwState, + const uint8_t* inputBuf, int inputCount, int* pOutputCount) { int nextFree, ic, atBit, codeBits; int hash, hashDelta; int prefixCode, code, highCode; - const uchar* inputEnd = inputBuf + inputCount; + const uint8_t* inputEnd = inputBuf + inputCount; /* local copies of lzwState members, for speed */ - const ushort* pHashFunc = lzwState->hashFunc; - ushort* pEntry = lzwState->entry; - ushort* pPrefix = lzwState->prefix; - uchar* pSuffix = lzwState->suffix; - uchar* outBuf = lzwState->lzwBuf; + const uint16_t* pHashFunc = lzwState->hashFunc; + uint16_t* pEntry = lzwState->entry; + uint16_t* pPrefix = lzwState->prefix; + uint8_t* pSuffix = lzwState->suffix; + uint8_t* outBuf = lzwState->lzwBuf; - Assert(lzwState != nil); - Assert(inputBuf != nil); + Assert(lzwState != NULL); + Assert(inputBuf != NULL); Assert(inputCount > 0 && inputCount <= kNuLZWBlockSize); /* make sure nobody has been messing with the types */ Assert(sizeof(pHashFunc[0]) == sizeof(lzwState->hashFunc[0])); @@ -555,35 +551,34 @@ Nu_CompressLZWBlock(LZWCompressState* lzwState, const uchar* inputBuf, * * On exit, the output file will be positioned past the last byte written. */ -static NuError -Nu_CompressLZW(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pThreadCrc, Boolean isType2) +static NuError Nu_CompressLZW(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pThreadCrc, Boolean isType2) { NuError err = kNuErrNone; LZWCompressState* lzwState; long initialOffset; - const uchar* lzwInputBuf; - uint blockSize, rleSize, lzwSize; + const uint8_t* lzwInputBuf; + uint32_t blockSize, rleSize, lzwSize; long compressedLen; Boolean keepLzw; - Assert(pArchive != nil); - Assert(pStraw != nil); - Assert(fp != nil); + Assert(pArchive != NULL); + Assert(pStraw != NULL); + Assert(fp != NULL); Assert(srcLen > 0); - Assert(pDstLen != nil); - Assert(pThreadCrc != nil); + Assert(pDstLen != NULL); + Assert(pThreadCrc != NULL); Assert(isType2 == true || isType2 == false); /* * Do some initialization and set-up. */ - if (pArchive->lzwCompressState == nil) { + if (pArchive->lzwCompressState == NULL) { err = Nu_AllocLZWCompressState(pArchive); BailError(err); } - Assert(pArchive->lzwCompressState != nil); - Assert(pArchive->compBuf != nil); + Assert(pArchive->lzwCompressState != NULL); + Assert(pArchive->compBuf != NULL); lzwState = pArchive->lzwCompressState; lzwState->pArchive = pArchive; @@ -782,9 +777,8 @@ bail: /* * Compress ShrinkIt-style "LZW/1". */ -NuError -Nu_CompressLZW1(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc) +NuError Nu_CompressLZW1(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc) { return Nu_CompressLZW(pArchive, pStraw, fp, srcLen, pDstLen, pCrc, false); } @@ -792,9 +786,8 @@ Nu_CompressLZW1(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, /* * Compress ShrinkIt-style "LZW/2". */ -NuError -Nu_CompressLZW2(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc) +NuError Nu_CompressLZW2(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc) { return Nu_CompressLZW(pArchive, pStraw, fp, srcLen, pDstLen, pCrc, true); } @@ -813,21 +806,21 @@ Nu_CompressLZW2(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, /* * Static tables useful for bit manipulation. */ -static const uint gNuMaskTable[17] = { +static const uint32_t gNuMaskTable[17] = { 0x0000, 0x01ff, 0x03ff, 0x03ff, 0x07ff, 0x07ff, 0x07ff, 0x07ff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff }; /* convert high byte of "entry" into a bit width */ -static const uint gNuBitWidth[17] = { +static const uint32_t gNuBitWidth[17] = { 8,9,10,10,11,11,11,11,12,12,12,12,12,12,12,12,12 }; /* entry in the trie */ typedef struct TableEntry { - uchar ch; - uint prefix; + uint8_t ch; + uint32_t prefix; } TableEntry; /* @@ -839,38 +832,38 @@ typedef struct LZWExpandState { NuArchive* pArchive; TableEntry trie[4096-256]; /* holds from 9 bits to 12 bits */ - uchar stack[kNuLZWBlockSize]; + uint8_t stack[kNuLZWBlockSize]; - uint entry; /* 16-bit index into table */ - uint oldcode; /* carryover state for LZW/2 */ - uint incode; /* carryover state for LZW/2 */ - uint finalc; /* carryover state for LZW/2 */ + // some of these don't need to be 32 bits; they were "uint" before + uint32_t entry; /* 16-bit index into table */ + uint32_t oldcode; /* carryover state for LZW/2 */ + uint32_t incode; /* carryover state for LZW/2 */ + uint32_t finalc; /* carryover state for LZW/2 */ Boolean resetFix; /* work around an LZW/2 bug */ - ushort chunkCrc; /* CRC we calculate for LZW/1 */ - ushort fileCrc; /* CRC stored with file */ + uint16_t chunkCrc; /* CRC we calculate for LZW/1 */ + uint16_t fileCrc; /* CRC stored with file */ - uchar diskVol; /* disk volume # */ - uchar rleEscape; /* RLE escape char, usually 0xdb */ + uint8_t diskVol; /* disk volume # */ + uint8_t rleEscape; /* RLE escape char, usually 0xdb */ - ulong dataInBuffer; /* #of bytes in compBuf */ - uchar* dataPtr; /* current data offset */ + uint32_t dataInBuffer; /* #of bytes in compBuf */ + uint8_t* dataPtr; /* current data offset */ - uchar lzwOutBuf[kNuLZWBlockSize + kNuSafetyPadding]; - uchar rleOutBuf[kNuLZWBlockSize + kNuSafetyPadding]; + uint8_t lzwOutBuf[kNuLZWBlockSize + kNuSafetyPadding]; + uint8_t rleOutBuf[kNuLZWBlockSize + kNuSafetyPadding]; } LZWExpandState; /* * Allocate some "reusable" state for LZW expansion. */ -static NuError -Nu_AllocLZWExpandState(NuArchive* pArchive) +static NuError Nu_AllocLZWExpandState(NuArchive* pArchive) { NuError err; - Assert(pArchive != nil); - Assert(pArchive->lzwExpandState == nil); + Assert(pArchive != NULL); + Assert(pArchive->lzwExpandState == NULL); /* allocate the general-purpose compression buffer, if needed */ err = Nu_AllocCompressionBufferIFN(pArchive); @@ -878,7 +871,7 @@ Nu_AllocLZWExpandState(NuArchive* pArchive) return err; pArchive->lzwExpandState = Nu_Malloc(pArchive, sizeof(LZWExpandState)); - if (pArchive->lzwExpandState == nil) + if (pArchive->lzwExpandState == NULL) return kNuErrMalloc; return kNuErrNone; } @@ -896,8 +889,8 @@ Nu_AllocLZWExpandState(NuArchive* pArchive) ( Nu_LZWPopCheck(lzwState, stackPtr), *(--stackPtr) ) # define Nu_LZWStackEmpty() ( stackPtr == lzwState->stack ) -static inline void -Nu_LZWPushCheck(uchar uch, const LZWExpandState* lzwState,const uchar* stackPtr) +static inline void Nu_LZWPushCheck(uint8_t uch, const LZWExpandState* lzwState, + const uint8_t* stackPtr) { if (stackPtr >= lzwState->stack + sizeof(lzwState->stack)) { Nu_ReportError(lzwState->NU_BLOB, kNuErrBadData, "stack overflow"); @@ -905,8 +898,8 @@ Nu_LZWPushCheck(uchar uch, const LZWExpandState* lzwState,const uchar* stackPtr) } } -static inline void -Nu_LZWPopCheck(const LZWExpandState* lzwState, const uchar* stackPtr) +static inline void Nu_LZWPopCheck(const LZWExpandState* lzwState, + const uint8_t* stackPtr) { if (stackPtr == lzwState->stack) { Nu_ReportError(lzwState->NU_BLOB, kNuErrBadData, "stack underflow"); @@ -927,13 +920,11 @@ Nu_LZWPopCheck(const LZWExpandState* lzwState, const uchar* stackPtr) * * (Turning this into a macro might speed things up.) */ -static inline uint -Nu_LZWGetCode(const uchar** pInBuf, uint entry, int* pAtBit, uint* pLastByte) +static inline uint32_t Nu_LZWGetCode(const uint8_t** pInBuf, uint32_t entry, + int* pAtBit, uint32_t* pLastByte) { - uint numBits, startBit, lastBit; - ulong value; - - Assert(sizeof(uint) >= 2); + uint32_t numBits, startBit, lastBit; + uint32_t value; numBits = (entry +1) >> 8; /* bit-width of next code */ startBit = *pAtBit; @@ -952,7 +943,7 @@ Nu_LZWGetCode(const uchar** pInBuf, uint entry, int* pAtBit, uint* pLastByte) /* need two more bytes */ value |= *(*pInBuf)++ << 8; *pLastByte = *(*pInBuf)++; - value |= (ulong) *pLastByte << 16; + value |= (uint32_t) *pLastByte << 16; } else { /* only need one more byte */ *pLastByte = *(*pInBuf)++; @@ -977,20 +968,19 @@ Nu_LZWGetCode(const uchar** pInBuf, uint entry, int* pAtBit, uint* pLastByte) * * Reads from lzwState->dataPtr, writes to lzwState->lzwOutBuf. */ -static NuError -Nu_ExpandLZW1(LZWExpandState* lzwState, uint expectedLen) +static NuError Nu_ExpandLZW1(LZWExpandState* lzwState, uint32_t expectedLen) { NuError err = kNuErrNone; TableEntry* tablePtr; int atBit; - uint entry, oldcode, incode, ptr; - uint lastByte, finalc; - const uchar* inbuf; - uchar* outbuf; - uchar* outbufend; - uchar* stackPtr; + uint32_t entry, oldcode, incode, ptr; + uint32_t lastByte, finalc; + const uint8_t* inbuf; + uint8_t* outbuf; + uint8_t* outbufend; + uint8_t* stackPtr; - Assert(lzwState != nil); + Assert(lzwState != NULL); Assert(expectedLen > 0 && expectedLen <= kNuLZWBlockSize); inbuf = lzwState->dataPtr; @@ -1024,7 +1014,7 @@ Nu_ExpandLZW1(LZWExpandState* lzwState, uint expectedLen) err = kNuErrBadData; return err; } - Nu_LZWPush((uchar)finalc); + Nu_LZWPush((uint8_t)finalc); ptr = oldcode; } @@ -1062,7 +1052,7 @@ bail: /* adjust input buffer */ lzwState->dataInBuffer -= (inbuf - lzwState->dataPtr); Assert(lzwState->dataInBuffer < 32767*65536); - lzwState->dataPtr = (uchar*)inbuf; + lzwState->dataPtr = (uint8_t*)inbuf; return err; } @@ -1077,24 +1067,23 @@ bail: * In some cases, "expectedInputUsed" will be -1 to indicate that the * value is not known. */ -static NuError -Nu_ExpandLZW2(LZWExpandState* lzwState, uint expectedLen, - uint expectedInputUsed) +static NuError Nu_ExpandLZW2(LZWExpandState* lzwState, uint32_t expectedLen, + uint32_t expectedInputUsed) { NuError err = kNuErrNone; TableEntry* tablePtr; int atBit; - uint entry, oldcode, incode, ptr; - uint lastByte, finalc; - const uchar* inbuf; - const uchar* inbufend; - uchar* outbuf; - uchar* outbufend; - uchar* stackPtr; + uint32_t entry, oldcode, incode, ptr; + uint32_t lastByte, finalc; + const uint8_t* inbuf; + const uint8_t* inbufend; + uint8_t* outbuf; + uint8_t* outbufend; + uint8_t* stackPtr; /*DBUG_LZW(("### LZW/2 block start (compIn=%d, rleOut=%d, entry=0x%04x)\n", expectedInputUsed, expectedLen, lzwState->entry));*/ - Assert(lzwState != nil); + Assert(lzwState != NULL); Assert(expectedLen > 0 && expectedLen <= kNuLZWBlockSize); inbuf = lzwState->dataPtr; @@ -1171,7 +1160,7 @@ main_loop: err = kNuErrBadData; return err; } - Nu_LZWPush((uchar)finalc); + Nu_LZWPush((uint8_t)finalc); ptr = oldcode; } @@ -1203,7 +1192,7 @@ main_loop: bail: /*DBUG_LZW(("### end of block\n"));*/ - if (expectedInputUsed != (unsigned int) -1 && inbuf != inbufend) { + if (expectedInputUsed != (uint32_t) -1 && inbuf != inbufend) { /* data was corrupted; if we keep going this will get worse */ DBUG(("--- inbuf != inbufend in ExpandLZW2 (diff=%d)\n", inbufend - inbuf)); @@ -1215,7 +1204,7 @@ bail: /* adjust input buffer */ lzwState->dataInBuffer -= (inbuf - lzwState->dataPtr); Assert(lzwState->dataInBuffer < 32767*65536); - lzwState->dataPtr = (uchar*)inbuf; + lzwState->dataPtr = (uint8_t*)inbuf; /* save off local copies of stuff */ lzwState->entry = entry; @@ -1230,15 +1219,14 @@ bail: /* * Expands a chunk of RLEd data into 4K of output. */ -static NuError -Nu_ExpandRLE(LZWExpandState* lzwState, const uchar* inbuf, - uint expectedInputUsed) +static NuError Nu_ExpandRLE(LZWExpandState* lzwState, const uint8_t* inbuf, + uint32_t expectedInputUsed) { NuError err = kNuErrNone; - uchar *outbuf; - uchar *outbufend; - const uchar *inbufend; - uchar uch, rleEscape; + uint8_t *outbuf; + uint8_t *outbufend; + const uint8_t *inbufend; + uint8_t uch, rleEscape; int count; outbuf = lzwState->rleOutBuf; @@ -1284,8 +1272,7 @@ bail: /* * Utility function to get a byte from the input buffer. */ -static inline uchar -Nu_GetHeaderByte(LZWExpandState* lzwState) +static inline uint8_t Nu_GetHeaderByte(LZWExpandState* lzwState) { lzwState->dataInBuffer--; Assert(lzwState->dataInBuffer > 0); @@ -1298,33 +1285,33 @@ Nu_GetHeaderByte(LZWExpandState* lzwState) * This manages the input data buffer, passing chunks of compressed data * into the appropriate expansion function. * - * Pass in nil for "pThreadCrc" if no thread CRC is desired. Otherwise, + * Pass in NULL for "pThreadCrc" if no thread CRC is desired. Otherwise, * "*pThreadCrc" should already be set to its initial value. On exit it * will contain the CRC of the uncompressed data. */ -NuError -Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pThreadCrc) +NuError Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, + const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, + uint16_t* pThreadCrc) { NuError err = kNuErrNone; Boolean isType2; LZWExpandState* lzwState; - ulong compRemaining, uncompRemaining, minSize; + uint32_t compRemaining, uncompRemaining, minSize; - Assert(pArchive != nil); - Assert(pThread != nil); - Assert(infp != nil); - Assert(pFunnel != nil); + Assert(pArchive != NULL); + Assert(pThread != NULL); + Assert(infp != NULL); + Assert(pFunnel != NULL); /* * Do some initialization and set-up. */ - if (pArchive->lzwExpandState == nil) { + if (pArchive->lzwExpandState == NULL) { err = Nu_AllocLZWExpandState(pArchive); BailError(err); } - Assert(pArchive->lzwExpandState != nil); - Assert(pArchive->compBuf != nil); + Assert(pArchive->lzwExpandState != NULL); + Assert(pArchive->compBuf != NULL); lzwState = pArchive->lzwExpandState; lzwState->pArchive = pArchive; @@ -1368,7 +1355,7 @@ Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, compRemaining -= 2; lzwState->dataInBuffer = 0; - lzwState->dataPtr = nil; + lzwState->dataPtr = NULL; /* reset pointers */ lzwState->entry = kNuLZWFirstCode; /* 0x0101 */ @@ -1396,11 +1383,11 @@ Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, while (uncompRemaining) { Boolean rleUsed; Boolean lzwUsed; - ulong getSize; - uint rleLen; /* length after RLE; 4096 if no RLE */ - uint lzwLen = 0; /* type 2 only */ - uint writeLen, inCount; - const uchar* writeBuf; + uint32_t getSize; + uint32_t rleLen; /* length after RLE; 4096 if no RLE */ + uint32_t lzwLen = 0; /* type 2 only */ + uint32_t writeLen, inCount; + const uint8_t* writeBuf; /* if we're low, and there's more data available, read more */ if (lzwState->dataInBuffer < kNuLZWDesiredChunk && compRemaining) { @@ -1409,7 +1396,7 @@ Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, * the buffer. */ if (lzwState->dataInBuffer) { - Assert(lzwState->dataPtr != nil); + Assert(lzwState->dataPtr != NULL); Assert(pArchive->compBuf != lzwState->dataPtr); memmove(pArchive->compBuf, lzwState->dataPtr, lzwState->dataInBuffer); @@ -1429,7 +1416,7 @@ Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, getSize); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, - "failed reading compressed data (%ld bytes)", getSize); + "failed reading compressed data (%u bytes)", getSize); goto bail; } lzwState->dataInBuffer += getSize; @@ -1476,7 +1463,7 @@ Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, writeLen = kNuLZWBlockSize; #ifndef NDEBUG - writeBuf = nil; + writeBuf = NULL; #endif /* @@ -1491,7 +1478,7 @@ Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, } else { if (pRecord->isBadMac || pArchive->valIgnoreLZW2Len) { /* might be big-endian, might be okay; just ignore it */ - lzwLen = (unsigned int) -1; + lzwLen = (uint32_t) -1; } else if (lzwState->dataInBuffer < lzwLen) { /* rare -- GSHK will do this if you don't let it finish */ err = kNuErrBufferUnderrun; @@ -1537,7 +1524,7 @@ Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, lzwState->resetFix = false; } - Assert(writeBuf != nil); + Assert(writeBuf != NULL); /* * Compute the CRC of the uncompressed data, and write it. For @@ -1547,7 +1534,7 @@ Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, * See commentary in the compression code for why we have to * compute two CRCs for LZW/1. */ - if (pThreadCrc != nil) { + if (pThreadCrc != NULL) { *pThreadCrc = Nu_CalcCRC16(*pThreadCrc, writeBuf, writeLen); } if (!isType2) { @@ -1590,7 +1577,7 @@ Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, DBUG(("--- Found %ld bytes following compressed data (compRem=%ld)\n", lzwState->dataInBuffer, compRemaining)); if (lzwState->dataInBuffer > 32) { - Nu_ReportError(NU_BLOB, kNuErrNone, "(Warning) lots of fluff (%ld)", + Nu_ReportError(NU_BLOB, kNuErrNone, "(Warning) lots of fluff (%u)", lzwState->dataInBuffer); } } @@ -1602,7 +1589,7 @@ Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, if (compRemaining) { err = kNuErrBadData; Nu_ReportError(NU_BLOB, err, - "not all compressed data was used (%ld/%ld)", + "not all compressed data was used (%u/%u)", compRemaining, lzwState->dataInBuffer); goto bail; } diff --git a/nufxlib/Makefile.in b/nufxlib/Makefile.in index c02d0ec..66c2ae4 100644 --- a/nufxlib/Makefile.in +++ b/nufxlib/Makefile.in @@ -12,6 +12,9 @@ # # The shared library support currently leaves much to be desired. # +# If you build with -DDEBUG_MSGS, nulib2 will be able to use the hidden +# 'g' command, which generates a verbose archive dump for debugging. +# # NufxLib install location. prefix = @prefix@ @@ -33,12 +36,12 @@ OPT = @CFLAGS@ GCC_FLAGS = -Wall -Wwrite-strings -Wstrict-prototypes -Wpointer-arith -Wshadow CFLAGS = @BUILD_FLAGS@ -I. @DEFS@ -DOPTFLAGSTR="\"$(OPT)\"" -SRCS = Archive.c ArchiveIO.c Bzip2.c Compress.c Crc16.c Debug.c \ - Deferred.c Deflate.c Entry.c Expand.c FileIO.c Funnel.c \ +SRCS = Archive.c ArchiveIO.c Bzip2.c Charset.c Compress.c Crc16.c \ + Debug.c Deferred.c Deflate.c Entry.c Expand.c FileIO.c Funnel.c \ Lzc.c Lzw.c MiscStuff.c MiscUtils.c Record.c SourceSink.c \ Squeeze.c Thread.c Value.c Version.c -OBJS = Archive.o ArchiveIO.o Bzip2.o Compress.o Crc16.o Debug.o \ - Deferred.o Deflate.o Entry.o Expand.o FileIO.o Funnel.o \ +OBJS = Archive.o ArchiveIO.o Bzip2.o Charset.o Compress.o Crc16.o \ + Debug.o Deferred.o Deflate.o Entry.o Expand.o FileIO.o Funnel.o \ Lzc.o Lzw.o MiscStuff.o MiscUtils.o Record.o SourceSink.o \ Squeeze.o Thread.o Value.o Version.o @@ -79,7 +82,7 @@ $(STATIC_PRODUCT): $(OBJS) $(AR) $@ $(OBJS) @RANLIB@ $@ -# BUG: we probably want -fPIC -D_REENTRANT on the compile lines for this. +# BUG: we need -fPIC, maybe -D_REENTRANT when compiling for this. # BUG: for Linux we may want -Wl,-soname,libnufx.so.1 on the link line. $(SHARED_PRODUCT): $(OBJS) -rm -f $(STATIC_PRODUCT) $(SHARED_PRODUCT) @@ -97,7 +100,6 @@ tags:: distclean: clean (cd samples; make distclean) - -rm -f Version.c -rm -f Makefile Makefile.bak -rm -f config.log config.cache config.status config.h -rm -f tags @@ -111,12 +113,29 @@ baktar: @gzip -9 nufxlib.tar @mv -i nufxlib.tar.gz /home/fadden/BAK/ -depend: - makedepend -- $(CFLAGS) -I/usr/local/include -- $(SRCS) - @(cd samples; unset CFLAGS OBJS; @SET_MAKE@ $(MAKE) depend) - -# catch OPTFLAGSTR updates -Version.o: Makefile - -# DO NOT DELETE THIS LINE -- make depend depends on it. +# dependency info +COMMON_HDRS = NufxLibPriv.h NufxLib.h MiscStuff.h SysDefs.h +Archive.o: Archive.c $(COMMON_HDRS) +ArchiveIO.o: ArchiveIO.c $(COMMON_HDRS) +Bzip2.o: Bzip2.c $(COMMON_HDRS) +Charset.o: Charset.c $(COMMON_HDRS) +Compress.o: Compress.c $(COMMON_HDRS) +Crc16.o: Crc16.c $(COMMON_HDRS) +Debug.o: Debug.c $(COMMON_HDRS) +Deferred.o: Deferred.c $(COMMON_HDRS) +Deflate.o: Deflate.c $(COMMON_HDRS) +Entry.o: Entry.c $(COMMON_HDRS) +Expand.o: Expand.c $(COMMON_HDRS) +FileIO.o: FileIO.c $(COMMON_HDRS) +Funnel.o: Funnel.c $(COMMON_HDRS) +Lzc.o: Lzc.c $(COMMON_HDRS) +Lzw.o: Lzw.c $(COMMON_HDRS) +MiscStuff.o: MiscStuff.c $(COMMON_HDRS) +MiscUtils.o: MiscUtils.c $(COMMON_HDRS) +Record.o: Record.c $(COMMON_HDRS) +SourceSink.o: SourceSink.c $(COMMON_HDRS) +Squeeze.o: Squeeze.c $(COMMON_HDRS) +Thread.o: Thread.c $(COMMON_HDRS) +Value.o: Value.c $(COMMON_HDRS) +Version.o: Version.c $(COMMON_HDRS) Makefile diff --git a/nufxlib/Makefile.msc b/nufxlib/Makefile.msc index aafb34c..8c2bb90 100644 --- a/nufxlib/Makefile.msc +++ b/nufxlib/Makefile.msc @@ -65,16 +65,16 @@ LDFLAGS = $(LDFLAGS) zlib.lib # object files -OBJS = Archive.obj ArchiveIO.obj Bzip2.obj Compress.obj Crc16.obj Debug.obj \ - Deferred.obj Deflate.obj Entry.obj Expand.obj FileIO.obj Funnel.obj \ - Lzc.obj Lzw.obj MiscStuff.obj MiscUtils.obj Record.obj SourceSink.obj \ - Squeeze.obj Thread.obj Value.obj Version.obj +OBJS = Archive.obj ArchiveIO.obj Bzip2.obj Charset.obj Compress.obj \ + Crc16.obj Debug.obj Deferred.obj Deflate.obj Entry.obj Expand.obj \ + FileIO.obj Funnel.obj Lzc.obj Lzw.obj MiscStuff.obj MiscUtils.obj \ + Record.obj SourceSink.obj Squeeze.obj Thread.obj Value.obj Version.obj # build targets -- static library, dynamic library, and test programs all: $(STATICLIB) $(SHAREDLIB) $(IMPLIB) \ - exerciser.exe imgconv.exe launder.exe test-basic.exe \ - test-basic-d.exe test-extract.exe test-simple.exe test-twirl.exe + exerciser.exe imgconv.exe launder.exe test-basic.exe test-basic-d.exe \ + test-extract.exe test-names.exe test-simple.exe test-twirl.exe clean: -del *.obj *.pdb *.exp @@ -107,6 +107,9 @@ test-basic-d.exe: TestBasic.obj $(IMPLIB) test-extract.exe: TestExtract.obj $(STATICLIB) $(LD) $(LDFLAGS) -out:$@ TestExtract.obj $(STATICLIB) +test-names.exe: TestNames.obj $(STATICLIB) + $(LD) $(LDFLAGS) -out:$@ TestNames.obj $(STATICLIB) + test-simple.exe: TestSimple.obj $(STATICLIB) $(LD) $(LDFLAGS) -out:$@ TestSimple.obj $(STATICLIB) @@ -125,6 +128,7 @@ COMMON_HDRS = NufxLibPriv.h NufxLib.h MiscStuff.h SysDefs.h Archive.obj: Archive.c $(COMMON_HDRS) ArchiveIO.obj: ArchiveIO.c $(COMMON_HDRS) Bzip2.obj: Bzip2.c $(COMMON_HDRS) +Charset.obj: Charset.c $(COMMON_HDRS) Compress.obj: Compress.c $(COMMON_HDRS) Crc16.obj: Crc16.c $(COMMON_HDRS) Debug.obj: Debug.c $(COMMON_HDRS) @@ -150,6 +154,7 @@ ImgConv.obj: samples/ImgConv.c $(COMMON_HDRS) Launder.obj: samples/Launder.c $(COMMON_HDRS) TestBasic.obj: samples/TestBasic.c $(COMMON_HDRS) TestExtract.obj: samples/TestExtract.c $(COMMON_HDRS) +TestNames.obj: samples/TestNames.c $(COMMON_HDRS) TestSimple.obj: samples/TestSimple.c $(COMMON_HDRS) TestTwirl.obj: samples/TestTwirl.c $(COMMON_HDRS) diff --git a/nufxlib/MiscStuff.c b/nufxlib/MiscStuff.c index 4de6ab7..ff92c2a 100644 --- a/nufxlib/MiscStuff.c +++ b/nufxlib/MiscStuff.c @@ -16,8 +16,7 @@ * Return a pointer to the appropriate string in the system table, or NULL * if the value is out of bounds. */ -const char* -Nu_strerror(int errnum) +const char* Nu_strerror(int errnum) { extern int sys_nerr; extern char *sys_errlist[]; @@ -38,8 +37,7 @@ Nu_strerror(int errnum) * from BSD, is available in the PGP 2.6.2 distribution, but this should * suffice for those few systems that don't have memmove. */ -void* -Nu_memmove(void* dst, const void* src, size_t n) +void* Nu_memmove(void* dst, const void* src, size_t n) { void* retval = dst; char* srcp = (char*)src; @@ -80,8 +78,7 @@ Nu_memmove(void* dst, const void* src, size_t n) * For our purposes here, strtol does all we need it to. Someday * we should replace this with a "real" version. */ -unsigned long -Nu_strtoul(const char *nptr, char **endptr, int base) +unsigned long Nu_strtoul(const char *nptr, char **endptr, int base) { return strtol(nptr, endptr, base); } @@ -91,8 +88,7 @@ Nu_strtoul(const char *nptr, char **endptr, int base) /* * Compare two strings, case-insensitive. */ -int -Nu_strcasecmp(const char *str1, const char *str2) +int Nu_strcasecmp(const char *str1, const char *str2) { while (*str1 && *str2 && toupper(*str1) == toupper(*str2)) str1++, str2++; @@ -105,8 +101,7 @@ Nu_strcasecmp(const char *str1, const char *str2) /* * Compare two strings, case-insensitive, stopping after "n" chars. */ -int -Nu_strncasecmp(const char *str1, const char *str2, size_t n) +int Nu_strncasecmp(const char *str1, const char *str2, size_t n) { while (n && *str1 && *str2 && toupper(*str1) == toupper(*str2)) str1++, str2++, n--; diff --git a/nufxlib/MiscStuff.h b/nufxlib/MiscStuff.h index 2e8a376..493985c 100644 --- a/nufxlib/MiscStuff.h +++ b/nufxlib/MiscStuff.h @@ -6,8 +6,8 @@ * Misc stuff (shared between nufxlib and nulib2). This is a collection * of miscellaneous types and macros that I find generally useful. */ -#ifndef __MiscStuff__ -#define __MiscStuff__ +#ifndef NUFXLIB_MISCSTUFF_H +#define NUFXLIB_MISCSTUFF_H #define VALGRIND /* assume we're using it */ @@ -42,11 +42,7 @@ int Nu_strncasecmp(const char *s1, const char *s2, size_t n); * Misc types. */ -#include - -#define nil NULL /* I can't seem to stop typing 'nil' now */ - -typedef uchar Boolean; +typedef unsigned char Boolean; #define false (0) #define true (!false) @@ -63,7 +59,7 @@ typedef uchar Boolean; (x) <= '9' ? (x) - '0' : toupper(x) +10 - 'A' ) /* convert number from 0-15 to hex digit */ -#define HexConv(x) ( ((uint)(x)) <= 15 ? \ +#define HexConv(x) ( ((unsigned int)(x)) <= 15 ? \ ( (x) <= 9 ? (x) + '0' : (x) -10 + 'A') : -1 ) @@ -106,4 +102,4 @@ typedef uchar Boolean; #define kInvalidPtr ((void*)0xa3a3a3a3) -#endif /*__MiscStuff__*/ +#endif /*NUFXLIB_MISCSTUFF_H*/ diff --git a/nufxlib/MiscUtils.c b/nufxlib/MiscUtils.c index 9d6a7c6..a3efd53 100644 --- a/nufxlib/MiscUtils.c +++ b/nufxlib/MiscUtils.c @@ -6,13 +6,12 @@ * * Miscellaneous NufxLib utility functions. */ -#define __MiscUtils_c__ #include "NufxLibPriv.h" /* * Big fat hairy global. Unfortunately this is unavoidable. */ -NuCallback gNuGlobalErrorMessageHandler = nil; +NuCallback gNuGlobalErrorMessageHandler = NULL; static const char* kNufxLibName = "nufxlib"; @@ -21,8 +20,7 @@ static const char* kNufxLibName = "nufxlib"; /* * strerror() equivalent for NufxLib errors. */ -const char* -Nu_StrError(NuError err) +const char* Nu_StrError(NuError err) { /* * BUG: this should be set up as per-thread storage in an MT environment. @@ -32,7 +30,7 @@ Nu_StrError(NuError err) * to return this. * * An easier solution, should this present a problem for someone, would - * be to have the function return nil or "unknown error" when the + * be to have the function return NULL or "unknown error" when the * error value isn't recognized. I'd recommend leaving it as-is for * debug builds, though, as it's helpful to know *which* error is not * recognized. @@ -200,15 +198,15 @@ Nu_StrError(NuError err) * Similar to perror(), but takes the error as an argument, and knows * about NufxLib errors as well as system errors. * - * Depending on the compiler, "file", "line", and "function" may be nil/zero. + * Depending on the compiler, "file", "line", and "function" may be NULL/zero. * - * Calling here with "pArchive"==nil is allowed, but should only be done + * Calling here with "pArchive"==NULL is allowed, but should only be done * if the archive is inaccessible (perhaps because it failed to open). We - * can't invoke the error message callback if the pointer is nil. + * can't invoke the error message callback if the pointer is NULL. */ -void -Nu_ReportError(NuArchive* pArchive, const char* file, int line, - const char* function, Boolean isDebug, NuError err, const char* format, ...) +void Nu_ReportError(NuArchive* pArchive, const char* file, int line, + const char* function, Boolean isDebug, NuError err, + const UNICHAR* format, ...) { NuErrorMessage errorMessage; const char* msg; @@ -219,7 +217,7 @@ Nu_ReportError(NuArchive* pArchive, const char* file, int line, int cc; #endif - Assert(format != nil); + Assert(format != NULL); va_start(args, format); @@ -247,28 +245,28 @@ Nu_ReportError(NuArchive* pArchive, const char* file, int line, strcpy(buf+count, ": "); count += 2; - msg = nil; + msg = NULL; if (err >= 0) msg = strerror(err); - if (msg == nil) + if (msg == NULL) msg = Nu_StrError(err); #if defined(HAVE_SNPRINTF) && defined(SNPRINTF_DECLARED) - if (msg == nil) + if (msg == NULL) snprintf(buf+count, sizeof(buf) - count, "(unknown err=%d)", err); else snprintf(buf+count, sizeof(buf) - count, "%s", msg); #else #ifdef SPRINTF_RETURNS_INT - if (msg == nil) + if (msg == NULL) cc = sprintf(buf+count, "(unknown err=%d)", err); else cc = sprintf(buf+count, "%s", msg); Assert(cc > 0); count += cc; #else - if (msg == nil) + if (msg == NULL) sprintf(buf+count, "(unknown err=%d)", err); else sprintf(buf+count, "%s", msg); @@ -284,8 +282,8 @@ Nu_ReportError(NuArchive* pArchive, const char* file, int line, Assert(count <= kNuHeftyBufSize); #endif - if ((pArchive != nil && pArchive->messageHandlerFunc == nil) || - (pArchive == nil && gNuGlobalErrorMessageHandler == nil)) + if ((pArchive != NULL && pArchive->messageHandlerFunc == NULL) || + (pArchive == NULL && gNuGlobalErrorMessageHandler == NULL)) { if (isDebug) { fprintf(stderr, "%s: [%s:%d %s] %s\n", kNufxLibName, @@ -301,7 +299,7 @@ Nu_ReportError(NuArchive* pArchive, const char* file, int line, errorMessage.line = line; errorMessage.function = function; - if (pArchive == nil) + if (pArchive == NULL) (void) (*gNuGlobalErrorMessageHandler)(pArchive, &errorMessage); else (void) (*pArchive->messageHandlerFunc)(pArchive, &errorMessage); @@ -322,48 +320,46 @@ bail: */ #ifndef USE_DMALLOC -void* -Nu_Malloc(NuArchive* pArchive, size_t size) +void* Nu_Malloc(NuArchive* pArchive, size_t size) { void* _result; Assert(size > 0); _result = malloc(size); - if (_result == nil) { - Nu_ReportError(NU_BLOB, kNuErrMalloc, "malloc(%u) failed", (uint) size); + if (_result == NULL) { + Nu_ReportError(NU_BLOB, kNuErrMalloc, + "malloc(%u) failed", (unsigned int) size); DebugAbort(); /* leave a core dump if we're built for it */ } DebugFill(_result, size); return _result; } -void* -Nu_Calloc(NuArchive* pArchive, size_t size) +void* Nu_Calloc(NuArchive* pArchive, size_t size) { void* _cresult = Nu_Malloc(pArchive, size); memset(_cresult, 0, size); return _cresult; } -void* -Nu_Realloc(NuArchive* pArchive, void* ptr, size_t size) +void* Nu_Realloc(NuArchive* pArchive, void* ptr, size_t size) { void* _result; - Assert(ptr != nil); /* disallow this usage */ + Assert(ptr != NULL); /* disallow this usage */ Assert(size > 0); /* disallow this usage */ _result = realloc(ptr, size); - if (_result == nil) { - Nu_ReportError(NU_BLOB, kNuErrMalloc, "realloc(%u) failed",(uint) size); + if (_result == NULL) { + Nu_ReportError(NU_BLOB, kNuErrMalloc, + "realloc(%u) failed", (unsigned int) size); DebugAbort(); /* leave a core dump if we're built for it */ } return _result; } -void -Nu_Free(NuArchive* pArchive, void* ptr) +void Nu_Free(NuArchive* pArchive, void* ptr) { - if (ptr != nil) + if (ptr != NULL) free(ptr); } #endif @@ -372,11 +368,10 @@ Nu_Free(NuArchive* pArchive, void* ptr) * If somebody internal wants to set doClose on a buffer DataSource * (looks like "Rename" does), we need to supply a "free" callback. */ -NuResult -Nu_InternalFreeCallback(NuArchive* pArchive, void* args) +NuResult Nu_InternalFreeCallback(NuArchive* pArchive, void* args) { DBUG(("+++ internal free callback 0x%08lx\n", (long) args)); - Nu_Free(nil, args); + Nu_Free(NULL, args); return kNuOK; } diff --git a/nufxlib/NufxLib.h b/nufxlib/NufxLib.h index 0dc4dbc..57c70ad 100644 --- a/nufxlib/NufxLib.h +++ b/nufxlib/NufxLib.h @@ -6,10 +6,11 @@ * * External interface (types, defines, and function prototypes). */ -#ifndef __NufxLib__ -#define __NufxLib__ +#ifndef NUFXLIB_NUFXLIB_H +#define NUFXLIB_NUFXLIB_H #include +#include #ifdef __cplusplus @@ -31,9 +32,9 @@ extern "C" { * The "bug" version can usually be ignored, since it represents minor * fixes. Unless, of course, your code depends upon that fix. */ -#define kNuVersionMajor 2 -#define kNuVersionMinor 2 -#define kNuVersionBug 2 +#define kNuVersionMajor 3 +#define kNuVersionMinor 0 +#define kNuVersionBug 0 /* @@ -42,6 +43,33 @@ extern "C" { * =========================================================================== */ +/* + * Unicode character type. For Linux and Mac OS X, filenames use "narrow" + * characters and UTF-8 encoding, which allows them to use standard file I/O + * functions like fopen(). Windows uses UTF-16, which requires a different + * character type and an alternative set of I/O functions like _wfopen(). + * + * The idea is that NufxLib API functions will operate on filenames with + * the OS dominant method, so on Windows the API accepts UTF-16. This + * definition is a bit like Windows TCHAR, but it's dependent on the OS, not + * on whether _MBCS or _UNICODE is defined. + * + * The app can include "Unichar.h" to get definitions for functions that + * switch between narrow and wide functions (e.g. "unistrlen()" becomes + * strlen() or wcslen() as appropriate). + * + * We switch based on _WIN32, because we're not really switching on + * filename-character size; the key issue is all the pesky wide I/O calls. + */ +#if defined(_WIN32) +// TODO: complete this +//# include +//# define UNICHAR wchar_t +# define UNICHAR char +#else +# define UNICHAR char +#endif + /* * Error values returned from functions. * @@ -148,27 +176,27 @@ typedef enum NuResult { * NuRecordIdxs are assigned to records in an archive. You may assume that * the values are unique, but that is all. */ -typedef unsigned long NuRecordIdx; +typedef uint32_t NuRecordIdx; /* * NuThreadIdxs are assigned to threads within a record. Again, you may * assume that the values are unique within a record, but that is all. */ -typedef unsigned long NuThreadIdx; +typedef uint32_t NuThreadIdx; /* * Thread ID, a combination of thread_class and thread_kind. Standard * values have explicit identifiers. */ -typedef unsigned long NuThreadID; +typedef uint32_t NuThreadID; #define NuMakeThreadID(class, kind) /* construct a NuThreadID */ \ - ((unsigned long)(class) << 16 | (unsigned long)(kind)) + ((uint32_t)(class) << 16 | (uint32_t)(kind)) #define NuGetThreadID(pThread) /* pull NuThreadID out of NuThread */ \ (NuMakeThreadID((pThread)->thThreadClass, (pThread)->thThreadKind)) #define NuThreadIDGetClass(threadID) /* get threadClass from NuThreadID */ \ - ((unsigned short) ((unsigned long)(threadID) >> 16)) + ((uint16_t) ((uint32_t)(threadID) >> 16)) #define NuThreadIDGetKind(threadID) /* get threadKind from NuThreadID */ \ - ((unsigned short) ((threadID) & 0xffff)) + ((uint16_t) ((threadID) & 0xffff)) #define kNuThreadClassMessage 0x0000 #define kNuThreadClassControl 0x0001 #define kNuThreadClassData 0x0002 @@ -198,10 +226,10 @@ typedef enum NuThreadFormat { /* extract the filesystem separator char from the "file_sys_info" field */ #define NuGetSepFromSysInfo(sysInfo) \ - ((char) ((sysInfo) & 0xff)) + ((UNICHAR) ((sysInfo) & 0xff)) /* return a file_sys_info with a replaced filesystem separator */ #define NuSetSepInSysInfo(sysInfo, newSep) \ - ((unsigned short) (((sysInfo) & 0xff00) | ((newSep) & 0xff)) ) + ((uint16_t) (((sysInfo) & 0xff00) | ((newSep) & 0xff)) ) /* GS/OS-defined file system identifiers; sadly, UNIX is not among them */ typedef enum NuFileSysID { @@ -273,7 +301,7 @@ typedef enum NuValueID { kNuValueIgnoreLZW2Len = 14, kNuValueHandleBadMac = 15 } NuValueID; -typedef unsigned long NuValue; +typedef uint32_t NuValue; /* * Enumerated values for things you pass in a NuValue. @@ -322,7 +350,7 @@ typedef enum NuAttrID { kNuAttrHeaderOffset = 3, kNuAttrJunkOffset = 4, } NuAttrID; -typedef unsigned long NuAttr; +typedef uint32_t NuAttr; /* * Archive types. @@ -381,14 +409,14 @@ typedef union NuDataSink NuDataSink; /* dummy def for internal struct */ * NuFX Date/Time structure; same as TimeRec from IIgs "misctool.h". */ typedef struct NuDateTime { - unsigned char second; /* 0-59 */ - unsigned char minute; /* 0-59 */ - unsigned char hour; /* 0-23 */ - unsigned char year; /* year - 1900 */ - unsigned char day; /* 0-30 */ - unsigned char month; /* 0-11 */ - unsigned char extra; /* (must be zero) */ - unsigned char weekDay; /* 1-7 (1=sunday) */ + uint8_t second; /* 0-59 */ + uint8_t minute; /* 0-59 */ + uint8_t hour; /* 0-23 */ + uint8_t year; /* year - 1900 */ + uint8_t day; /* 0-30 */ + uint8_t month; /* 0-11 */ + uint8_t extra; /* (must be zero) */ + uint8_t weekDay; /* 1-7 (1=sunday) */ } NuDateTime; /* @@ -399,26 +427,30 @@ typedef struct NuDateTime { */ typedef struct NuThread { /* from the archive */ - unsigned short thThreadClass; - NuThreadFormat thThreadFormat; - unsigned short thThreadKind; - unsigned short thThreadCRC; /* comp or uncomp data; see rec vers */ - unsigned long thThreadEOF; - unsigned long thCompThreadEOF; + uint16_t thThreadClass; + NuThreadFormat thThreadFormat; + uint16_t thThreadKind; + uint16_t thThreadCRC; /* comp or uncomp data; see rec vers */ + uint32_t thThreadEOF; + uint32_t thCompThreadEOF; /* extra goodies */ - NuThreadIdx threadIdx; - unsigned long actualThreadEOF; /* disk images might be off */ - long fileOffset; /* fseek offset to data in shk */ + NuThreadIdx threadIdx; + uint32_t actualThreadEOF; /* disk images might be off */ + long fileOffset; /* fseek offset to data in shk */ /* internal use only */ - unsigned short used; /* mark as uninteresting */ + uint16_t used; /* mark as uninteresting */ } NuThread; /* * NuFX "record" definition. * * (Note to developers: update Nu_AddRecord if this changes.) + * + * The filenames are in Mac OS Roman format. It's arguable whether MOR + * strings should be part of the interface at all. However, the API + * pre-dates the inclusion of Unicode support, and I'm leaving it alone. */ #define kNufxIDLen 4 /* len of 'NuFX' with funky MSBs */ #define kNuReasonableAttribCount 256 @@ -428,52 +460,52 @@ typedef struct NuThread { #define kNuOurRecordVersion 3 /* what we write */ typedef struct NuRecord { /* version 0+ */ - unsigned char recNufxID[kNufxIDLen]; - unsigned short recHeaderCRC; - unsigned short recAttribCount; - unsigned short recVersionNumber; - unsigned long recTotalThreads; - NuFileSysID recFileSysID; - unsigned short recFileSysInfo; - unsigned long recAccess; - unsigned long recFileType; - unsigned long recExtraType; - unsigned short recStorageType; /* NuStorage*,file_sys_block_size */ - NuDateTime recCreateWhen; - NuDateTime recModWhen; - NuDateTime recArchiveWhen; + uint8_t recNufxID[kNufxIDLen]; + uint16_t recHeaderCRC; + uint16_t recAttribCount; + uint16_t recVersionNumber; + uint32_t recTotalThreads; + NuFileSysID recFileSysID; + uint16_t recFileSysInfo; + uint32_t recAccess; + uint32_t recFileType; + uint32_t recExtraType; + uint16_t recStorageType; /* NuStorage*,file_sys_block_size */ + NuDateTime recCreateWhen; + NuDateTime recModWhen; + NuDateTime recArchiveWhen; /* option lists only in version 1+ */ - unsigned short recOptionSize; - unsigned char* recOptionList; /* NULL if v0 or recOptionSize==0 */ + uint16_t recOptionSize; + uint8_t* recOptionList; /* NULL if v0 or recOptionSize==0 */ /* data specified by recAttribCount, not accounted for by option list */ - long extraCount; - unsigned char* extraBytes; + int32_t extraCount; + uint8_t* extraBytes; - unsigned short recFilenameLength; /* usually zero */ - char* recFilename; /* doubles as disk volume_name */ + uint16_t recFilenameLength; /* usually zero */ + char* recFilenameMOR; /* doubles as disk volume_name */ /* extra goodies; "dirtyHeader" does not apply to anything below */ - NuRecordIdx recordIdx; /* session-unique record index */ - char* threadFilename; /* extracted from filename thread */ - char* newFilename; /* memorized during "add file" call */ - const char* filename; /* points at recFilen or threadFilen */ - unsigned long recHeaderLength; /* size of rec hdr, incl thread hdrs */ - unsigned long totalCompLength; /* total len of data in archive file */ - long fakeThreads; /* used by "MaskDataless" */ - int isBadMac; /* malformed "bad mac" header */ + NuRecordIdx recordIdx; /* session-unique record index */ + char* threadFilenameMOR; /* extracted from filename thread */ + char* newFilenameMOR; /* memorized during "add file" call */ + const char* filenameMOR; /* points at recFilen or threadFilen */ + uint32_t recHeaderLength; /* size of rec hdr, incl thread hdrs */ + uint32_t totalCompLength; /* total len of data in archive file */ + uint32_t fakeThreads; /* used by "MaskDataless" */ + int isBadMac; /* malformed "bad mac" header */ - long fileOffset; /* file offset of record header */ + long fileOffset; /* file offset of record header */ /* use provided interface to access this */ - struct NuThread* pThreads; /* ptr to thread array */ + struct NuThread* pThreads; /* ptr to thread array */ /* private -- things the application shouldn't look at */ - struct NuRecord* pNext; /* used internally */ - NuThreadMod* pThreadMods; /* used internally */ - short dirtyHeader; /* set in "copy" when hdr fields uptd */ - short dropRecFilename; /* if set, we're dropping this name */ + struct NuRecord* pNext; /* used internally */ + NuThreadMod* pThreadMods; /* used internally */ + short dirtyHeader; /* set in "copy" when hdr fields uptd */ + short dropRecFilename; /* if set, we're dropping this name */ } NuRecord; /* @@ -488,18 +520,18 @@ typedef struct NuRecord { #define kNuMaxMHVersion 2 /* max we can handle */ #define kNuOurMHVersion 2 /* what we write */ typedef struct NuMasterHeader { - unsigned char mhNufileID[kNufileIDLen]; - unsigned short mhMasterCRC; - unsigned long mhTotalRecords; - NuDateTime mhArchiveCreateWhen; - NuDateTime mhArchiveModWhen; - unsigned short mhMasterVersion; - unsigned char mhReserved1[kNufileMasterReserved1Len]; - unsigned long mhMasterEOF; - unsigned char mhReserved2[kNufileMasterReserved2Len]; + uint8_t mhNufileID[kNufileIDLen]; + uint16_t mhMasterCRC; + uint32_t mhTotalRecords; + NuDateTime mhArchiveCreateWhen; + NuDateTime mhArchiveModWhen; + uint16_t mhMasterVersion; + uint8_t mhReserved1[kNufileMasterReserved1Len]; + uint32_t mhMasterEOF; + uint8_t mhReserved2[kNufileMasterReserved2Len]; /* private -- internal use only */ - short isValid; + short isValid; } NuMasterHeader; @@ -514,35 +546,40 @@ typedef struct NuMasterHeader { * a small subset of the full record. */ typedef struct NuRecordAttr { - NuFileSysID fileSysID; - /*unsigned short fileSysInfo;*/ - unsigned long access; - unsigned long fileType; - unsigned long extraType; - NuDateTime createWhen; - NuDateTime modWhen; - NuDateTime archiveWhen; + NuFileSysID fileSysID; + /*uint16_t fileSysInfo;*/ + uint32_t access; + uint32_t fileType; + uint32_t extraType; + NuDateTime createWhen; + NuDateTime modWhen; + NuDateTime archiveWhen; } NuRecordAttr; /* * Some additional details about a file. + * + * Ideally (from an API cleanliness perspective) the storage name would + * be passed around as UTF-8 and converted internally. Passing it as + * MOR required fewer changes to the library, and allows us to avoid + * having to deal with illegal characters. */ typedef struct NuFileDetails { /* used during AddFile call */ - NuThreadID threadID; /* data, rsrc, disk img? */ - const char* origName; + NuThreadID threadID; /* data, rsrc, disk img? */ + const void* origName; /* arbitrary pointer, usually a string */ /* these go straight into the NuRecord */ - const char* storageName; - NuFileSysID fileSysID; - unsigned short fileSysInfo; - unsigned long access; - unsigned long fileType; - unsigned long extraType; - unsigned short storageType; /* use Unknown, or disk block size */ - NuDateTime createWhen; - NuDateTime modWhen; - NuDateTime archiveWhen; + const char* storageNameMOR; + NuFileSysID fileSysID; + uint16_t fileSysInfo; + uint32_t access; + uint32_t fileType; + uint32_t extraType; + uint16_t storageType; /* use Unknown, or disk block size */ + NuDateTime createWhen; + NuDateTime modWhen; + NuDateTime archiveWhen; } NuFileDetails; @@ -550,23 +587,23 @@ typedef struct NuFileDetails { * Passed into the SelectionFilter callback. */ typedef struct NuSelectionProposal { - const NuRecord* pRecord; - const NuThread* pThread; + const NuRecord* pRecord; + const NuThread* pThread; } NuSelectionProposal; /* * Passed into the OutputPathnameFilter callback. */ typedef struct NuPathnameProposal { - const char* pathname; - char filenameSeparator; - const NuRecord* pRecord; - const NuThread* pThread; + const UNICHAR* pathnameUNI; + char filenameSeparator; + const NuRecord* pRecord; + const NuThread* pThread; - const char* newPathname; - unsigned char newFilenameSeparator; - /*NuThreadID newStorage;*/ - NuDataSink* newDataSink; + const UNICHAR* newPathnameUNI; + uint8_t newFilenameSeparator; + /*NuThreadID newStorage;*/ + NuDataSink* newDataSink; } NuPathnameProposal; @@ -598,7 +635,8 @@ typedef enum NuProgressState { } NuProgressState; /* - * Passed into the ProgressUpdater callback. + * Passed into the ProgressUpdater callback. All pointers become + * invalid when the callback returns. * * [ Thought for the day: add an optional flag that causes us to only * call the progressFunc when the "percentComplete" changes by more @@ -606,36 +644,36 @@ typedef enum NuProgressState { */ typedef struct NuProgressData { /* what are we doing */ - NuOperation operation; + NuOperation operation; /* what specifically are we doing */ - NuProgressState state; + NuProgressState state; /* how far along are we */ - short percentComplete; /* 0-100 */ + short percentComplete; /* 0-100 */ /* original pathname (in archive for expand, on disk for compress) */ - const char* origPathname; + const UNICHAR* origPathnameUNI; /* processed pathname (PathnameFilter for expand, in-record for compress) */ - const char* pathname; - /* basename of "pathname" */ - const char* filename; + const UNICHAR* pathnameUNI; + /* basename of "pathname" (for convenience) */ + const UNICHAR* filenameUNI; /* pointer to the record we're expanding from */ - const NuRecord* pRecord; + const NuRecord* pRecord; - unsigned long uncompressedLength; /* size of uncompressed data */ - unsigned long uncompressedProgress; /* #of bytes in/out */ + uint32_t uncompressedLength; /* size of uncompressed data */ + uint32_t uncompressedProgress; /* #of bytes in/out */ struct { - NuThreadFormat threadFormat; /* compression being applied */ + NuThreadFormat threadFormat; /* compression being applied */ } compress; struct { - unsigned long totalCompressedLength; /* all "data" threads */ - unsigned long totalUncompressedLength; + uint32_t totalCompressedLength; /* all "data" threads */ + uint32_t totalUncompressedLength; - /*unsigned long compressedLength; * size of compressed data */ - /*unsigned long compressedProgress; * #of compressed bytes in/out*/ - const NuThread* pThread; /* thread we're working on */ - NuValue convertEOL; /* set if LF/CR conv is on */ + /*uint32_t compressedLength; * size of compressed data */ + /*uint32_t compressedProgress; * #of compressed bytes in/out*/ + const NuThread* pThread; /* thread we're working on */ + NuValue convertEOL; /* set if LF/CR conv is on */ } expand; /* pay no attention */ @@ -649,11 +687,11 @@ typedef struct NuErrorStatus { NuOperation operation; /* were we adding, extracting, ?? */ NuError err; /* library error code */ int sysErr; /* system error code, if applicable */ - const char* message; /* (optional) message to user */ + const UNICHAR* message; /* (optional) message to user */ const NuRecord* pRecord; /* relevant record, if any */ - const char* pathname; /* problematic pathname, if any */ - const char* origPathname; /* original pathname, if any */ - char filenameSeparator; /* fssep for pathname, if any */ + const UNICHAR* pathnameUNI; /* problematic pathname, if any */ + const void* origPathname; /* original pathname ref, if any */ + UNICHAR filenameSeparator; /* fssep for pathname, if any */ /*char origArchiveTouched;*/ char canAbort; /* give option to abort */ @@ -669,14 +707,14 @@ typedef struct NuErrorStatus { * Error message callback gets one of these. */ typedef struct NuErrorMessage { - const char* message; /* the message itself */ + const char* message; /* the message itself (UTF-8) */ NuError err; /* relevant error code (may be none) */ short isDebug; /* set for debug-only messages */ /* these identify where the message originated if lib built w/debug set */ - const char* file; /* source file */ + const char* file; /* source file (UTF-8) */ int line; /* line number */ - const char* function; /* function name (might be nil) */ + const char* function; /* function name (might be NULL) */ } NuErrorMessage; @@ -727,37 +765,38 @@ NUFXLIB_API NuError NuExtract(NuArchive* pArchive); NUFXLIB_API NuError NuTest(NuArchive* pArchive); /* strictly non-streaming read-only interfaces */ -NUFXLIB_API NuError NuOpenRO(const char* archivePathname,NuArchive** ppArchive); +NUFXLIB_API NuError NuOpenRO(const UNICHAR* archivePathnameUNI, + NuArchive** ppArchive); NUFXLIB_API NuError NuExtractRecord(NuArchive* pArchive, NuRecordIdx recordIdx); NUFXLIB_API NuError NuExtractThread(NuArchive* pArchive, NuThreadIdx threadIdx, NuDataSink* pDataSink); NUFXLIB_API NuError NuTestRecord(NuArchive* pArchive, NuRecordIdx recordIdx); NUFXLIB_API NuError NuGetRecord(NuArchive* pArchive, NuRecordIdx recordIdx, const NuRecord** ppRecord); -NUFXLIB_API NuError NuGetRecordIdxByName(NuArchive* pArchive, const char* name, - NuRecordIdx* pRecordIdx); +NUFXLIB_API NuError NuGetRecordIdxByName(NuArchive* pArchive, + const char* nameMOR, NuRecordIdx* pRecordIdx); NUFXLIB_API NuError NuGetRecordIdxByPosition(NuArchive* pArchive, - unsigned long position, NuRecordIdx* pRecordIdx); + uint32_t position, NuRecordIdx* pRecordIdx); /* read/write interfaces */ -NUFXLIB_API NuError NuOpenRW(const char* archivePathname, - const char* tempPathname, unsigned long flags, +NUFXLIB_API NuError NuOpenRW(const UNICHAR* archivePathnameUNI, + const UNICHAR* tempPathnameUNI, uint32_t flags, NuArchive** ppArchive); -NUFXLIB_API NuError NuFlush(NuArchive* pArchive, long* pStatusFlags); +NUFXLIB_API NuError NuFlush(NuArchive* pArchive, uint32_t* pStatusFlags); NUFXLIB_API NuError NuAddRecord(NuArchive* pArchive, const NuFileDetails* pFileDetails, NuRecordIdx* pRecordIdx); NUFXLIB_API NuError NuAddThread(NuArchive* pArchive, NuRecordIdx recordIdx, NuThreadID threadID, NuDataSource* pDataSource, NuThreadIdx* pThreadIdx); -NUFXLIB_API NuError NuAddFile(NuArchive* pArchive, const char* pathname, +NUFXLIB_API NuError NuAddFile(NuArchive* pArchive, const UNICHAR* pathnameUNI, const NuFileDetails* pFileDetails, short fromRsrcFork, NuRecordIdx* pRecordIdx); NUFXLIB_API NuError NuRename(NuArchive* pArchive, NuRecordIdx recordIdx, - const char* pathname, char fssep); + const char* pathnameMOR, UNICHAR fssep); NUFXLIB_API NuError NuSetRecordAttr(NuArchive* pArchive, NuRecordIdx recordIdx, const NuRecordAttr* pRecordAttr); NUFXLIB_API NuError NuUpdatePresizedThread(NuArchive* pArchive, - NuThreadIdx threadIdx, NuDataSource* pDataSource, long* pMaxLen); + NuThreadIdx threadIdx, NuDataSource* pDataSource, int32_t* pMaxLen); NUFXLIB_API NuError NuDelete(NuArchive* pArchive); NUFXLIB_API NuError NuDeleteRecord(NuArchive* pArchive, NuRecordIdx recordIdx); NUFXLIB_API NuError NuDeleteThread(NuArchive* pArchive, NuThreadIdx threadIdx); @@ -779,31 +818,31 @@ NUFXLIB_API NuError NuDebugDumpArchive(NuArchive* pArchive); /* sources and sinks */ NUFXLIB_API NuError NuCreateDataSourceForFile(NuThreadFormat threadFormat, - unsigned long otherLen, const char* pathname, + uint32_t otherLen, const UNICHAR* pathnameUNI, short isFromRsrcFork, NuDataSource** ppDataSource); NUFXLIB_API NuError NuCreateDataSourceForFP(NuThreadFormat threadFormat, - unsigned long otherLen, FILE* fp, long offset, long length, + uint32_t otherLen, FILE* fp, long offset, long length, NuCallback closeFunc, NuDataSource** ppDataSource); NUFXLIB_API NuError NuCreateDataSourceForBuffer(NuThreadFormat threadFormat, - unsigned long otherLen, const unsigned char* buffer, long offset, + uint32_t otherLen, const uint8_t* buffer, long offset, long length, NuCallback freeFunc, NuDataSource** ppDataSource); NUFXLIB_API NuError NuFreeDataSource(NuDataSource* pDataSource); NUFXLIB_API NuError NuDataSourceSetRawCrc(NuDataSource* pDataSource, - unsigned short crc); + uint16_t crc); NUFXLIB_API NuError NuCreateDataSinkForFile(short doExpand, NuValue convertEOL, - const char* pathname, char fssep, NuDataSink** ppDataSink); + const UNICHAR* pathnameUNI, UNICHAR fssep, NuDataSink** ppDataSink); NUFXLIB_API NuError NuCreateDataSinkForFP(short doExpand, NuValue convertEOL, FILE* fp, NuDataSink** ppDataSink); NUFXLIB_API NuError NuCreateDataSinkForBuffer(short doExpand, - NuValue convertEOL, unsigned char* buffer, unsigned long bufLen, + NuValue convertEOL, uint8_t* buffer, uint32_t bufLen, NuDataSink** ppDataSink); NUFXLIB_API NuError NuFreeDataSink(NuDataSink* pDataSink); NUFXLIB_API NuError NuDataSinkGetOutCount(NuDataSink* pDataSink, - unsigned long* pOutCount); + uint32_t* pOutCount); /* miscellaneous non-archive operations */ -NUFXLIB_API NuError NuGetVersion(long* pMajorVersion, long* pMinorVersion, - long* pBugVersion, const char** ppBuildDate, +NUFXLIB_API NuError NuGetVersion(int32_t* pMajorVersion, int32_t* pMinorVersion, + int32_t* pBugVersion, const char** ppBuildDate, const char** ppBuildFlags); NUFXLIB_API const char* NuStrError(NuError err); NUFXLIB_API NuError NuTestFeature(NuFeature feature); @@ -811,13 +850,18 @@ NUFXLIB_API void NuRecordCopyAttr(NuRecordAttr* pRecordAttr, const NuRecord* pRecord); NUFXLIB_API NuError NuRecordCopyThreads(const NuRecord* pRecord, NuThread** ppThreads); -NUFXLIB_API unsigned long NuRecordGetNumThreads(const NuRecord* pRecord); -NUFXLIB_API const NuThread* NuThreadGetByIdx(const NuThread* pThread, long idx); +NUFXLIB_API uint32_t NuRecordGetNumThreads(const NuRecord* pRecord); +NUFXLIB_API const NuThread* NuThreadGetByIdx(const NuThread* pThread, + int32_t idx); NUFXLIB_API short NuIsPresizedThreadID(NuThreadID threadID); +NUFXLIB_API size_t NuConvertMORToUNI(const char* stringMOR, + UNICHAR* bufUNI, size_t bufSize); +NUFXLIB_API size_t NuConvertUNIToMOR(const UNICHAR* stringUNI, + char* bufMOR, size_t bufSize); -#define NuGetThread(pRecord, idx) ( (const NuThread*) \ - ((unsigned long) (idx) < (unsigned long) (pRecord)->recTotalThreads ? \ - &(pRecord)->pThreads[(idx)] : NULL) \ +#define NuGetThread(pRecord, idx) ( (const NuThread*) \ + ((uint32_t) (idx) < (pRecord)->recTotalThreads ? \ + &(pRecord)->pThreads[(idx)] : NULL) \ ) @@ -840,4 +884,4 @@ NUFXLIB_API NuCallback NuSetGlobalErrorMessageHandler(NuCallback messageHandlerF } #endif -#endif /*__NufxLib__*/ +#endif /*NUFXLIB_NUFXLIB_H*/ diff --git a/nufxlib/NufxLibPriv.h b/nufxlib/NufxLibPriv.h index 3e59030..dd3df95 100644 --- a/nufxlib/NufxLibPriv.h +++ b/nufxlib/NufxLibPriv.h @@ -6,8 +6,8 @@ * * Global internal declarations and definitions. */ -#ifndef __NufxLibPriv__ -#define __NufxLibPriv__ +#ifndef NUFXLIB_NUFXLIBPRIV_H +#define NUFXLIB_NUFXLIBPRIV_H /* include files that everybody needs */ #include "SysDefs.h" @@ -86,7 +86,7 @@ typedef enum NuOpenMode { */ typedef struct NuRecordSet { Boolean loaded; - ulong numRecords; + uint32_t numRecords; NuRecord* nuRecordHead; NuRecord* nuRecordTail; } NuRecordSet; @@ -95,12 +95,12 @@ typedef struct NuRecordSet { * Archive state. */ struct NuArchive { - ulong structMagic; + uint32_t structMagic; Boolean busy; NuOpenMode openMode; Boolean newlyCreated; - char* archivePathname; /* pathname or "(stream)" */ + UNICHAR* archivePathnameUNI; /* pathname or "(stream)" */ FILE* archiveFp; NuArchiveType archiveType; @@ -108,7 +108,7 @@ struct NuArchive { long junkOffset; /* skip past leading junk */ long headerOffset; /* adjustment for BXY/SEA/BSE */ - char* tmpPathname; /* temp file, for writes */ + UNICHAR* tmpPathnameUNI; /* temp file, for writes */ FILE* tmpFp; /* used during initial processing; helps avoid ftell() calls */ @@ -118,9 +118,9 @@ struct NuArchive { Boolean testMode; /* clumsy way of remembering name used for other fork in forked file */ - const char* lastFileCreated; + const UNICHAR* lastFileCreatedUNI; /* clumsy way to avoid trying to create the same subdir several times */ - const char* lastDirCreated; + const UNICHAR* lastDirCreatedUNI; /* master header from the archive */ NuMasterHeader masterHeader; /* original */ @@ -135,7 +135,7 @@ struct NuArchive { NuRecordSet newRecordSet; /* newly-added records */ /* state for compression functions */ - uchar* compBuf; /* large general-purpose buffer */ + uint8_t* compBuf; /* large general-purpose buffer */ void* lzwCompressState; /* state for LZW/1 and LZW/2 */ void* lzwExpandState; /* state for LZW/1 and LZW/2 */ @@ -169,7 +169,7 @@ struct NuArchive { #define kNuArchiveStructMagic 0xc0edbabe -#define kNuDefaultRecordName "UNKNOWN" +#define kNuDefaultRecordName "UNKNOWN" /* use ASCII charset */ /* @@ -256,7 +256,7 @@ struct NuThreadMod { */ typedef struct NuFunnel { /* data storage */ - uchar* buffer; /* kNuFunnelBufSize worth of storage */ + uint8_t* buffer; /* kNuFunnelBufSize worth of storage */ long bufCount; /* #of bytes in buffer */ /* text conversion; if "auto", on first flush we convert to "on" or "off" */ @@ -270,10 +270,10 @@ typedef struct NuFunnel { Boolean isFirstWrite; /* cleared on first write */ #if 0 - ulong inCount; /* total #of bytes in the top */ - ulong outCount; /* total #of bytes out the bottom */ + uint32_t inCount; /* total #of bytes in the top */ + uint32_t outCount; /* total #of bytes out the bottom */ - ulong outMax; /* flag an err when outCount exceeds this */ + uint32_t outMax; /* flag an err when outCount exceeds this */ Boolean outMaxExceeded; /* in fact, it's this flag */ #endif @@ -302,11 +302,11 @@ typedef struct NuStraw { NuDataSource* pDataSource; /* progress update fields */ - ulong lastProgress; - ulong lastDisplayed; + uint32_t lastProgress; + uint32_t lastDisplayed; } NuStraw; -/*NuError Nu_CopyStreamToStream(FILE* outfp, FILE* infp, ulong count);*/ +/*NuError Nu_CopyStreamToStream(FILE* outfp, FILE* infp, uint32_t count);*/ /* @@ -329,10 +329,10 @@ typedef enum NuDataSourceType { typedef struct NuDataSourceCommon { NuDataSourceType sourceType; NuThreadFormat threadFormat; /* is it already compressed? */ - ushort rawCrc; /* crc for already-compressed data*/ + uint16_t rawCrc; /* crc for already-compressed data*/ /*Boolean doClose; \* close on completion? */ - ulong dataLen; /* length of data (var for buf) */ - ulong otherLen; /* uncomp len or preset buf size */ + uint32_t dataLen; /* length of data (var for buf) */ + uint32_t otherLen; /* uncomp len or preset buf size */ int refCount; /* so we can copy structs */ } NuDataSourceCommon; @@ -343,7 +343,7 @@ union NuDataSource { struct { NuDataSourceCommon common; - char* pathname; + UNICHAR* pathnameUNI; Boolean fromRsrcFork; /* temp storage; only valid when processing in library */ @@ -360,7 +360,7 @@ union NuDataSource { struct { NuDataSourceCommon common; - const uchar* buffer; /* non-const if doClose=true */ + const uint8_t* buffer; /* non-const if doClose=true */ long offset; /* starting offset */ long curOffset; /* current offset */ @@ -387,7 +387,7 @@ typedef struct NuDataSinkCommon { NuDataSinkType sinkType; Boolean doExpand; /* expand file? */ NuValue convertEOL; /* convert EOL? (req "expand") */ - ulong outCount; + uint32_t outCount; } NuDataSinkCommon; union NuDataSink { @@ -397,10 +397,10 @@ union NuDataSink { struct { NuDataSinkCommon common; - char* pathname; /* file to open */ - char fssep; + UNICHAR* pathnameUNI; /* file to open */ + UNICHAR fssep; - /* temp storage; must be nil except when processing in library */ + /* temp storage; must be NULL except when processing in library */ FILE* fp; } toFile; @@ -411,8 +411,8 @@ union NuDataSink { struct { NuDataSinkCommon common; - uchar* buffer; - ulong bufLen; /* max amount of data "buffer" holds */ + uint8_t* buffer; + uint32_t bufLen; /* max amount of data "buffer" holds */ NuError stickyErr; } toBuffer; }; @@ -469,13 +469,13 @@ union NuDataSink { goto bail; \ } #define BailNil(val) { \ - if ((val) == nil) { \ + if ((val) == NULL) { \ err = kNuErrUnexpectedNil; \ BailError(err); \ } \ } #define BailAlloc(val) { \ - if ((val) == nil) { \ + if ((val) == NULL) { \ err = kNuErrMalloc; \ BailError(err); \ } \ @@ -498,9 +498,9 @@ NuError Nu_UpdateWrapper(NuArchive* pArchive, FILE* fp); NuError Nu_AdjustWrapperPadding(NuArchive* pArchive, FILE* fp); NuError Nu_AllocCompressionBufferIFN(NuArchive* pArchive); NuError Nu_StreamOpenRO(FILE* infp, NuArchive** ppArchive); -NuError Nu_OpenRO(const char* filename, NuArchive** ppArchive); -NuError Nu_OpenRW(const char* archivePathname, const char* tempPathname, - ulong flags, NuArchive** ppArchive); +NuError Nu_OpenRO(const UNICHAR* archivePathnameUNI, NuArchive** ppArchive); +NuError Nu_OpenRW(const UNICHAR* archivePathnameUNI, + const UNICHAR* tempPathnameUNI, uint32_t flags, NuArchive** ppArchive); NuError Nu_WriteMasterHeader(NuArchive* pArchive, FILE* fp, NuMasterHeader* pMasterHeader); NuError Nu_Close(NuArchive* pArchive); @@ -509,28 +509,28 @@ NuError Nu_RenameTempToArchive(NuArchive* pArchive); NuError Nu_DeleteArchiveFile(NuArchive* pArchive); /* ArchiveIO.c */ -uchar Nu_ReadOneC(NuArchive* pArchive, FILE* fp, ushort* pCrc); -uchar Nu_ReadOne(NuArchive* pArchive, FILE* fp); -void Nu_WriteOneC(NuArchive* pArchive, FILE* fp, uchar val, ushort* pCrc); -void Nu_WriteOne(NuArchive* pArchive, FILE* fp, uchar val); -ushort Nu_ReadTwoC(NuArchive* pArchive, FILE* fp, ushort* pCrc); -ushort Nu_ReadTwo(NuArchive* pArchive, FILE* fp); -void Nu_WriteTwoC(NuArchive* pArchive, FILE* fp, ushort val, ushort* pCrc); -void Nu_WriteTwo(NuArchive* pArchive, FILE* fp, ushort val); -ulong Nu_ReadFourC(NuArchive* pArchive, FILE* fp, ushort* pCrc); -ulong Nu_ReadFour(NuArchive* pArchive, FILE* fp); -void Nu_WriteFourC(NuArchive* pArchive, FILE* fp, ulong val, ushort* pCrc); -void Nu_WriteFour(NuArchive* pArchive, FILE* fp, ulong val); -NuDateTime Nu_ReadDateTimeC(NuArchive* pArchive, FILE* fp, ushort* pCrc); -NuDateTime Nu_ReadDateTime(NuArchive* pArchive, FILE* fp, ushort* pCrc); +uint8_t Nu_ReadOneC(NuArchive* pArchive, FILE* fp, uint16_t* pCrc); +uint8_t Nu_ReadOne(NuArchive* pArchive, FILE* fp); +void Nu_WriteOneC(NuArchive* pArchive, FILE* fp, uint8_t val, uint16_t* pCrc); +void Nu_WriteOne(NuArchive* pArchive, FILE* fp, uint8_t val); +uint16_t Nu_ReadTwoC(NuArchive* pArchive, FILE* fp, uint16_t* pCrc); +uint16_t Nu_ReadTwo(NuArchive* pArchive, FILE* fp); +void Nu_WriteTwoC(NuArchive* pArchive, FILE* fp, uint16_t val, uint16_t* pCrc); +void Nu_WriteTwo(NuArchive* pArchive, FILE* fp, uint16_t val); +uint32_t Nu_ReadFourC(NuArchive* pArchive, FILE* fp, uint16_t* pCrc); +uint32_t Nu_ReadFour(NuArchive* pArchive, FILE* fp); +void Nu_WriteFourC(NuArchive* pArchive, FILE* fp, uint32_t val, uint16_t* pCrc); +void Nu_WriteFour(NuArchive* pArchive, FILE* fp, uint32_t val); +NuDateTime Nu_ReadDateTimeC(NuArchive* pArchive, FILE* fp, uint16_t* pCrc); +NuDateTime Nu_ReadDateTime(NuArchive* pArchive, FILE* fp, uint16_t* pCrc); void Nu_WriteDateTimeC(NuArchive* pArchive, FILE* fp, NuDateTime dateTime, - ushort* pCrc); + uint16_t* pCrc); void Nu_WriteDateTime(NuArchive* pArchive, FILE* fp, NuDateTime dateTime); void Nu_ReadBytesC(NuArchive* pArchive, FILE* fp, void* vbuffer, long count, - ushort* pCrc); + uint16_t* pCrc); void Nu_ReadBytes(NuArchive* pArchive, FILE* fp, void* vbuffer, long count); void Nu_WriteBytesC(NuArchive* pArchive, FILE* fp, const void* vbuffer, - long count, ushort* pCrc); + long count, uint16_t* pCrc); void Nu_WriteBytes(NuArchive* pArchive, FILE* fp, const void* vbuffer, long count); NuError Nu_HeaderIOFailed(NuArchive* pArchive, FILE* fp); @@ -540,9 +540,16 @@ NuError Nu_RewindArchive(NuArchive* pArchive); /* Bzip2.c */ NuError Nu_CompressBzip2(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc); + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc); NuError Nu_ExpandBzip2(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pCrc); + const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, uint16_t* pCrc); + +/* Charset.c */ +size_t Nu_ConvertMORToUNI(const char* stringMOR, UNICHAR* bufUNI, + size_t bufSize); +UNICHAR* Nu_CopyMORToUNI(const char* stringMOR); +size_t Nu_ConvertUNIToMOR(const UNICHAR* stringUNI, char* bufMOR, + size_t bufSize); /* Compress.c */ NuError Nu_CompressToArchive(NuArchive* pArchive, NuDataSource* pDataSource, @@ -554,22 +561,17 @@ NuError Nu_CopyPresizedToArchive(NuArchive* pArchive, NuThread* pThread, char** ppSavedCopy); /* Crc16.c */ -extern const ushort gNuCrc16Table[256]; -ushort Nu_CalcCRC16(ushort seed, const uchar* ptr, int count); -#ifdef __Crc16_c__ /* just doing "static inline" warns def-but-not-used */ - #define CRC_INLINE /**/ -#else - #define CRC_INLINE extern inline -#endif -#if defined(inline) && !defined(__Crc16_c__) /* somebody ovrd inline def? */ -ushort Nu_UpdateCRC16(uchar val, ushort crc); -#else -CRC_INLINE ushort -Nu_UpdateCRC16(uchar val, ushort crc) -{ - return (gNuCrc16Table[((crc >> 8) & 0xFF) ^ val] ^ (crc << 8)) & 0xFFFF; -} -#endif +extern const uint16_t gNuCrc16Table[256]; +uint16_t Nu_CalcCRC16(uint16_t seed, const uint8_t* ptr, int count); +/* + * Update the CRC-16. + * + * _val (uint8_t) is the byte to add to the CRC. It's evaluated once. + * _crc (uint16_t) is the previous CRC. It's evaluated twice. + * Returns the updated CRC as a uint16_t. + */ +#define Nu_UpdateCRC16(_val, _crc) \ + (gNuCrc16Table[(((_crc) >> 8) & 0xff) ^ (_val)] ^ ((_crc) << 8)) /* Debug.c */ #if defined(DEBUG_MSGS) || !defined(NDEBUG) @@ -591,13 +593,13 @@ NuError Nu_ThreadModAdd_FindByThreadID(const NuRecord* pRecord, void Nu_FreeThreadMods(NuArchive* pArchive, NuRecord* pRecord); NuThreadMod* Nu_ThreadMod_FindByThreadIdx(const NuRecord* pRecord, NuThreadIdx threadIdx); -NuError Nu_Flush(NuArchive* pArchive, long* pStatusFlags); +NuError Nu_Flush(NuArchive* pArchive, uint32_t* pStatusFlags); /* Deflate.c */ NuError Nu_CompressDeflate(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc); + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc); NuError Nu_ExpandDeflate(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pCrc); + const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, uint16_t* pCrc); /* Expand.c */ NuError Nu_ExpandStream(NuArchive* pArchive, const NuRecord* pRecord, @@ -607,14 +609,14 @@ NuError Nu_ExpandStream(NuArchive* pArchive, const NuRecord* pRecord, void Nu_SetCurrentDateTime(NuDateTime* pDateTime); Boolean Nu_IsOlder(const NuDateTime* pWhen1, const NuDateTime* pWhen2); NuError Nu_OpenOutputFile(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, const char* newPathname, char newFssep, + const NuThread* pThread, const UNICHAR* newPathnameUNI, UNICHAR newFssep, FILE** pFp); NuError Nu_CloseOutputFile(NuArchive* pArchive, const NuRecord* pRecord, - FILE* fp, const char* pathname); -NuError Nu_OpenInputFile(NuArchive* pArchive, const char* pathname, + FILE* fp, const UNICHAR* pathnameUNI); +NuError Nu_OpenInputFile(NuArchive* pArchive, const UNICHAR* pathnameUNI, Boolean openRsrc, FILE** pFp); -NuError Nu_DeleteFile(const char* pathname); -NuError Nu_RenameFile(const char* fromPath, const char* toPath); +NuError Nu_DeleteFile(const UNICHAR* pathnameUNI); +NuError Nu_RenameFile(const UNICHAR* fromPathUNI, const UNICHAR* toPathUNI); NuError Nu_FTell(FILE* fp, long* pOffset); NuError Nu_FSeek(FILE* fp, long offset, int ptrname); NuError Nu_FRead(FILE* fp, void* buf, size_t nbyte); @@ -627,22 +629,23 @@ NuError Nu_TruncateOpenFile(FILE* fp, long length); /* Funnel.c */ NuError Nu_ProgressDataInit_Compress(NuArchive* pArchive, NuProgressData* pProgressData, const NuRecord* pRecord, - const char* origPathname); + const UNICHAR* origPathnameUNI, const UNICHAR* pathnameUNI); NuError Nu_ProgressDataInit_Expand(NuArchive* pArchive, NuProgressData* pProgressData, const NuRecord* pRecord, - const char* newPathname, char newFssep, NuValue convertEOL); + const UNICHAR* newPathnameUNI, UNICHAR newFssep, + const UNICHAR* origPathnameUNI, NuValue convertEOL); NuError Nu_SendInitialProgress(NuArchive* pArchive, NuProgressData* pProgress); NuError Nu_FunnelNew(NuArchive* pArchive, NuDataSink* pDataSink, NuValue convertEOL, NuValue convertEOLTo, NuProgressData* pProgress, NuFunnel** ppFunnel); NuError Nu_FunnelFree(NuArchive* pArchive, NuFunnel* pFunnel); -/*void Nu_FunnelSetMaxOutput(NuFunnel* pFunnel, ulong maxBytes);*/ +/*void Nu_FunnelSetMaxOutput(NuFunnel* pFunnel, uint32_t maxBytes);*/ NuError Nu_FunnelWrite(NuArchive* pArchive, NuFunnel* pFunnel, - const uchar* buffer, ulong count); + const uint8_t* buffer, uint32_t count); NuError Nu_FunnelFlush(NuArchive* pArchive, NuFunnel* pFunnel); NuError Nu_ProgressDataCompressPrep(NuArchive* pArchive, NuStraw* pStraw, - NuThreadFormat threadFormat, ulong sourceLen); + NuThreadFormat threadFormat, uint32_t sourceLen); NuError Nu_ProgressDataExpandPrep(NuArchive* pArchive, NuFunnel* pFunnel, const NuThread* pThread); NuError Nu_FunnelSetProgressState(NuFunnel* pFunnel, NuProgressState state); @@ -654,32 +657,34 @@ NuError Nu_StrawNew(NuArchive* pArchive, NuDataSource* pDataSource, NuError Nu_StrawFree(NuArchive* pArchive, NuStraw* pStraw); NuError Nu_StrawSetProgressState(NuStraw* pStraw, NuProgressState state); NuError Nu_StrawSendProgressUpdate(NuArchive* pArchive, NuStraw* pStraw); -NuError Nu_StrawRead(NuArchive* pArchive, NuStraw* pStraw, uchar* buffer, +NuError Nu_StrawRead(NuArchive* pArchive, NuStraw* pStraw, uint8_t* buffer, long len); NuError Nu_StrawRewind(NuArchive* pArchive, NuStraw* pStraw); /* Lzc.c */ NuError Nu_CompressLZC12(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc); + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc); NuError Nu_CompressLZC16(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc); + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc); NuError Nu_ExpandLZC(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pCrc); + const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, uint16_t* pCrc); /* Lzw.c */ NuError Nu_CompressLZW1(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc); + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc); NuError Nu_CompressLZW2(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc); + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc); NuError Nu_ExpandLZW(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pThreadCrc); + const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, + uint16_t* pThreadCrc); /* MiscUtils.c */ /*extern const char* kNufxLibName;*/ extern NuCallback gNuGlobalErrorMessageHandler; const char* Nu_StrError(NuError err); void Nu_ReportError(NuArchive* pArchive, const char* file, int line, - const char* function, Boolean isDebug, NuError err, const char* format, ...) + const char* function, Boolean isDebug, NuError err, + const UNICHAR* format, ...) #if defined(__GNUC__) __attribute__ ((format(printf, 7, 8))) #endif @@ -688,7 +693,7 @@ void Nu_ReportError(NuArchive* pArchive, const char* file, int line, # define Nu_Malloc(archive, size) malloc(size) # define Nu_Calloc(archive, size) calloc(1, size) # define Nu_Realloc(archive, ptr, size) realloc(ptr, size) -# define Nu_Free(archive, ptr) (ptr != nil ? free(ptr) : (void)0) +# define Nu_Free(archive, ptr) (ptr != NULL ? free(ptr) : (void)0) #else void* Nu_Malloc(NuArchive* pArchive, size_t size); void* Nu_Calloc(NuArchive* pArchive, size_t size); @@ -701,8 +706,8 @@ NuResult Nu_InternalFreeCallback(NuArchive* pArchive, void* args); void Nu_RecordAddThreadMod(NuRecord* pRecord, NuThreadMod* pThreadMod); Boolean Nu_RecordIsEmpty(NuArchive* pArchive, const NuRecord* pRecord); Boolean Nu_RecordSet_GetLoaded(const NuRecordSet* pRecordSet); -ulong Nu_RecordSet_GetNumRecords(const NuRecordSet* pRecordSet); -void Nu_RecordSet_SetNumRecords(NuRecordSet* pRecordSet, ulong val); +uint32_t Nu_RecordSet_GetNumRecords(const NuRecordSet* pRecordSet); +void Nu_RecordSet_SetNumRecords(NuRecordSet* pRecordSet, uint32_t val); void Nu_RecordSet_IncNumRecords(NuRecordSet* pRecordSet); NuRecord* Nu_RecordSet_GetListHead(const NuRecordSet* pRecordSet); NuRecord** Nu_RecordSet_GetListHeadPtr(NuRecordSet* pRecordSet); @@ -738,19 +743,19 @@ NuError Nu_Test(NuArchive* pArchive); NuError Nu_TestRecord(NuArchive* pArchive, NuRecordIdx recIdx); NuError Nu_GetRecord(NuArchive* pArchive, NuRecordIdx recordIdx, const NuRecord** ppRecord); -NuError Nu_GetRecordIdxByName(NuArchive* pArchive, const char* name, +NuError Nu_GetRecordIdxByName(NuArchive* pArchive, const char* nameMOR, NuRecordIdx* pRecordIdx); -NuError Nu_GetRecordIdxByPosition(NuArchive* pArchive, ulong position, +NuError Nu_GetRecordIdxByPosition(NuArchive* pArchive, uint32_t position, NuRecordIdx* pRecordIdx); NuError Nu_FindRecordForWriteByIdx(NuArchive* pArchive, NuRecordIdx recIdx, NuRecord** ppFoundRecord); -NuError Nu_AddFile(NuArchive* pArchive, const char* pathname, +NuError Nu_AddFile(NuArchive* pArchive, const UNICHAR* pathnameUNI, const NuFileDetails* pFileDetails, Boolean fromRsrcFork, NuRecordIdx* pRecordIdx); NuError Nu_AddRecord(NuArchive* pArchive, const NuFileDetails* pFileDetails, NuRecordIdx* pRecordIdx, NuRecord** ppRecord); NuError Nu_Rename(NuArchive* pArchive, NuRecordIdx recIdx, - const char* pathname, char fssep); + const char* pathnameMOR, char fssepMOR); NuError Nu_SetRecordAttr(NuArchive* pArchive, NuRecordIdx recordIdx, const NuRecordAttr* pRecordAttr); NuError Nu_Delete(NuArchive* pArchive); @@ -758,88 +763,74 @@ NuError Nu_DeleteRecord(NuArchive* pArchive, NuRecordIdx rec); /* SourceSink.c */ NuError Nu_DataSourceFile_New(NuThreadFormat threadFormat, - ulong otherLen, const char* pathname, Boolean isFromRsrcFork, + uint32_t otherLen, const UNICHAR* pathnameUNI, Boolean isFromRsrcFork, NuDataSource** ppDataSource); NuError Nu_DataSourceFP_New(NuThreadFormat threadFormat, - ulong otherLen, FILE* fp, long offset, long length, + uint32_t otherLen, FILE* fp, long offset, long length, NuCallback fcloseFunc, NuDataSource** ppDataSource); NuError Nu_DataSourceBuffer_New(NuThreadFormat threadFormat, - ulong otherLen, const uchar* buffer, long offset, long length, + uint32_t otherLen, const uint8_t* buffer, long offset, long length, NuCallback freeFunc, NuDataSource** ppDataSource); NuDataSource* Nu_DataSourceCopy(NuDataSource* pDataSource); NuError Nu_DataSourceFree(NuDataSource* pDataSource); NuDataSourceType Nu_DataSourceGetType(const NuDataSource* pDataSource); NuThreadFormat Nu_DataSourceGetThreadFormat(const NuDataSource* pDataSource); -ulong Nu_DataSourceGetDataLen(const NuDataSource* pDataSource); -ulong Nu_DataSourceGetOtherLen(const NuDataSource* pDataSource); +uint32_t Nu_DataSourceGetDataLen(const NuDataSource* pDataSource); +uint32_t Nu_DataSourceGetOtherLen(const NuDataSource* pDataSource); void Nu_DataSourceSetOtherLen(NuDataSource* pDataSource, long otherLen); -ushort Nu_DataSourceGetRawCrc(const NuDataSource* pDataSource); -void Nu_DataSourceSetRawCrc(NuDataSource* pDataSource, ushort crc); +uint16_t Nu_DataSourceGetRawCrc(const NuDataSource* pDataSource); +void Nu_DataSourceSetRawCrc(NuDataSource* pDataSource, uint16_t crc); NuError Nu_DataSourcePrepareInput(NuArchive* pArchive, NuDataSource* pDataSource); void Nu_DataSourceUnPrepareInput(NuArchive* pArchive, NuDataSource* pDataSource); const char* Nu_DataSourceFile_GetPathname(NuDataSource* pDataSource); -NuError Nu_DataSourceGetBlock(NuDataSource* pDataSource, uchar* buf, ulong len); +NuError Nu_DataSourceGetBlock(NuDataSource* pDataSource, uint8_t* buf, + uint32_t len); NuError Nu_DataSourceRewind(NuDataSource* pDataSource); NuError Nu_DataSinkFile_New(Boolean doExpand, NuValue convertEOL, - const char* pathname, char fssep, NuDataSink** ppDataSink); + const UNICHAR* pathnameUNI, UNICHAR fssep, NuDataSink** ppDataSink); NuError Nu_DataSinkFP_New(Boolean doExpand, NuValue convertEOL, FILE* fp, NuDataSink** ppDataSink); NuError Nu_DataSinkBuffer_New(Boolean doExpand, NuValue convertEOL, - uchar* buffer, ulong bufLen, NuDataSink** ppDataSink); + uint8_t* buffer, uint32_t bufLen, NuDataSink** ppDataSink); NuError Nu_DataSinkVoid_New(Boolean doExpand, NuValue convertEOL, NuDataSink** ppDataSink); NuError Nu_DataSinkFree(NuDataSink* pDataSink); NuDataSinkType Nu_DataSinkGetType(const NuDataSink* pDataSink); Boolean Nu_DataSinkGetDoExpand(const NuDataSink* pDataSink); NuValue Nu_DataSinkGetConvertEOL(const NuDataSink* pDataSink); -ulong Nu_DataSinkGetOutCount(const NuDataSink* pDataSink); +uint32_t Nu_DataSinkGetOutCount(const NuDataSink* pDataSink); const char* Nu_DataSinkFile_GetPathname(const NuDataSink* pDataSink); -char Nu_DataSinkFile_GetFssep(const NuDataSink* pDataSink); +UNICHAR Nu_DataSinkFile_GetFssep(const NuDataSink* pDataSink); FILE* Nu_DataSinkFile_GetFP(const NuDataSink* pDataSink); void Nu_DataSinkFile_SetFP(NuDataSink* pDataSink, FILE* fp); void Nu_DataSinkFile_Close(NuDataSink* pDataSink); -NuError Nu_DataSinkPutBlock(NuDataSink* pDataSink, const uchar* buf, ulong len); +NuError Nu_DataSinkPutBlock(NuDataSink* pDataSink, const uint8_t* buf, + uint32_t len); NuError Nu_DataSinkGetError(NuDataSink* pDataSink); /* Squeeze.c */ NuError Nu_CompressHuffmanSQ(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc); + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc); NuError Nu_ExpandHuffmanSQ(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pCrc); + const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, uint16_t* pCrc); /* Thread.c */ -#ifdef __Thread_c__ - #define THREAD_INLINE /**/ -#else - #define THREAD_INLINE extern inline -#endif -#if defined(inline) && !defined(__Thread_c__) /* somebody ovrd inline def? */ NuThread* Nu_GetThread(const NuRecord* pRecord, int idx); -#else -THREAD_INLINE NuThread* -Nu_GetThread(const NuRecord* pRecord, int idx) -{ - if (idx >= (int)pRecord->recTotalThreads) - return nil; - else - return &pRecord->pThreads[idx]; -} -#endif void Nu_StripHiIfAllSet(char* str); Boolean Nu_IsPresizedThreadID(NuThreadID threadID); Boolean Nu_IsCompressibleThreadID(NuThreadID threadID); -Boolean Nu_ThreadHasCRC(long recordVersion, NuThreadID threadID); +Boolean Nu_ThreadHasCRC(uint16_t recordVersion, NuThreadID threadID); NuError Nu_FindThreadByIdx(const NuRecord* pRecord, NuThreadIdx thread, NuThread** ppThread); NuError Nu_FindThreadByID(const NuRecord* pRecord, NuThreadID threadID, NuThread** ppThread); void Nu_CopyThreadContents(NuThread* pDstThread, const NuThread* pSrcThread); NuError Nu_ReadThreadHeaders(NuArchive* pArchive, NuRecord* pRecord, - ushort* pCrc); + uint16_t* pCrc); NuError Nu_WriteThreadHeaders(NuArchive* pArchive, NuRecord* pRecord, FILE* fp, - ushort* pCrc); + uint16_t* pCrc); NuError Nu_ComputeThreadData(NuArchive* pArchive, NuRecord* pRecord); NuError Nu_ScanThreads(NuArchive* pArchive, NuRecord* pRecord,long numThreads); NuError Nu_ExtractThreadBulk(NuArchive* pArchive, const NuRecord* pRecord, @@ -853,7 +844,7 @@ NuError Nu_OkayToAddThread(NuArchive* pArchive, const NuRecord* pRecord, NuError Nu_AddThread(NuArchive* pArchive, NuRecordIdx rec, NuThreadID threadID, NuDataSource* pDataSource, NuThreadIdx* pThreadIdx); NuError Nu_UpdatePresizedThread(NuArchive* pArchive, NuThreadIdx threadIdx, - NuDataSource* pDataSource, long* pMaxLen); + NuDataSource* pDataSource, int32_t* pMaxLen); NuError Nu_DeleteThread(NuArchive* pArchive, NuThreadIdx threadIdx); /* Value.c */ @@ -864,7 +855,7 @@ NuThreadFormat Nu_ConvertCompressValToFormat(NuArchive* pArchive, NuValue compValue); /* Version.c */ -NuError Nu_GetVersion(long* pMajorVersion, long* pMinorVersion, - long* pBugVersion, const char** ppBuildDate, const char** ppBuildFlags); +NuError Nu_GetVersion(int32_t* pMajorVersion, int32_t* pMinorVersion, + int32_t* pBugVersion, const char** ppBuildDate, const char** ppBuildFlags); -#endif /*__NufxLibPriv__*/ +#endif /*NUFXLIB_NUFXLIBPRIV_H*/ diff --git a/nufxlib/README.txt b/nufxlib/README.txt index 37c7c49..3e246ee 100644 --- a/nufxlib/README.txt +++ b/nufxlib/README.txt @@ -1,4 +1,4 @@ -NufxLib README, updated 2004/03/18 +NufxLib README, updated 2014/12/23 http://www.nulib.com/ See "COPYING-LIB" for distribution restrictions. @@ -42,11 +42,8 @@ for @CFLAGS@ is "-g -O2". (Implicitly sets DEBUG_MSGS.) Spray lots of debugging output. If you want to do benchmarks, use "-O2 -DNDEBUG". The recommended -configuration during testing is "-g -O2 -DDEBUG_MSGS", so that verbose -debug output is available when errors occur. - -The flags are stuffed into Version.c, so the application program can -examine and display the flags that were used to build the library. +configuration is "-g -O2 -DDEBUG_MSGS", so that verbose debug output is +available when errors occur. BeOS @@ -67,14 +64,10 @@ If you're using BeOS/PPC, it will also do: Mac OS X ======== -This works just like the UNIX version, with the exception that when you link -against nufxlib, your project must also link against the Carbon framework. -This can be done in ProjectBuilder by using the Add Framework option in the -Project menu, or by adding "-framework Carbon" to the gcc command line. +This works just like the UNIX version, but includes support for resource +forks and Finder file/aux types. -You'll see some warnings due to some namespace collisions between nufxlib and -Carbon, but everything will work fine. Carbon is used to provide support for -file types and resource forks. +Tested with Xcode v5.1.1 and Mac OS 10.8.5. Win32 @@ -83,29 +76,24 @@ Win32 If you're using an environment that supports "configure" scripts, such as DJGPP, follow the UNIX instructions. -NufxLib has been tested with Microsoft Visual C++ 6.0. To build NufxLib, -start up a DOS shell and run vcvars32.bat to set your environment. Run: +NufxLib has been tested with Microsoft Visual C++ 12 (Visual Studio 2013). +To build NufxLib, run the "Visual Studio 2013 x86 Native Tools Command +Prompt" shortcut to get a shell. Change to the nufxlib directory, then: + nmake -f makefile.msc -to build with debugging info, or - nmake -f makefile.msc nodebug=1 -to build optimized. -See the makefile for comments about including zlib or libbz2. These -need to be enabled at compile time and linked into the sample apps. +When the build finishes, run "test-basic.exe" to confirm things are working. -Once the library has been built, "cd samples" and run the same command there. -When it finishes, run "test-basic.exe". +If you want to have zlib support enabled, you will need to have zlib.h, +zconf.h, and zlib.lib copied into the directory. See "makefile.msc" for +more details. -If you want to build NufxLib as a DLL, use "makefile.dll" instead. -If you're using zlib or libbz2, these will need to be linked into the DLL. -The makefile currently assumes that you will want to use zlib.dll. +The makefile builds NufxLib as a static library and as a DLL. Other Notes =========== -All of the source code is now formatted with spaces instead of tabs. - If you want to use the library in a multithreaded application, you should define "USE_REENTRANT_CALLS" to tell it to use reentrant versions of certain library calls. This defines _REENTRANT, which causes Solaris to @@ -113,12 +101,19 @@ add the appropriate goodies. (Seems to me you'd always want this on, but for some reason Solaris makes you take an extra step, so I'm not going to define it by default.) +Originally, NufxLib / NuLib2 were intended to be usable natively on the +Apple IIgs, so some of the design decisions were influenced by the need +to minimize memory usage (e.g. being able to get a directory listing +without holding the entire directory in memory) and interact with GS/OS +(forked files have a single filename, files have type/auxtype). The IIgs +port was never started. + Legalese ======== NufxLib, a NuFX archive manipulation library. -Copyright (C) 2000-2007 by Andy McFadden, All Rights Reserved. +Copyright (C) 2000-2014 by Andy McFadden, All Rights Reserved. See COPYING for license. diff --git a/nufxlib/Record.c b/nufxlib/Record.c index 300cf2d..d77899c 100644 --- a/nufxlib/Record.c +++ b/nufxlib/Record.c @@ -12,7 +12,7 @@ /* * Local constants. */ -static const uchar kNufxID[kNufxIDLen] = { 0x4e, 0xf5, 0x46, 0xd8 }; +static const uint8_t kNufxID[kNufxIDLen] = { 0x4e, 0xf5, 0x46, 0xd8 }; /* @@ -26,21 +26,20 @@ static const uchar kNufxID[kNufxIDLen] = { 0x4e, 0xf5, 0x46, 0xd8 }; * things that a Nu_FreeRecordContents call will check, so that we don't * end up trying to free garbage. No need to memset() the whole thing. */ -static NuError -Nu_InitRecordContents(NuArchive* pArchive, NuRecord* pRecord) +static NuError Nu_InitRecordContents(NuArchive* pArchive, NuRecord* pRecord) { - Assert(pRecord != nil); + Assert(pRecord != NULL); DebugFill(pRecord, sizeof(*pRecord)); - pRecord->recOptionList = nil; - pRecord->extraBytes = nil; - pRecord->recFilename = nil; - pRecord->threadFilename = nil; - pRecord->newFilename = nil; - pRecord->pThreads = nil; - pRecord->pNext = nil; - pRecord->pThreadMods = nil; + pRecord->recOptionList = NULL; + pRecord->extraBytes = NULL; + pRecord->recFilenameMOR = NULL; + pRecord->threadFilenameMOR = NULL; + pRecord->newFilenameMOR = NULL; + pRecord->pThreads = NULL; + pRecord->pNext = NULL; + pRecord->pThreadMods = NULL; pRecord->dirtyHeader = false; pRecord->dropRecFilename = false; pRecord->isBadMac = false; @@ -51,13 +50,12 @@ Nu_InitRecordContents(NuArchive* pArchive, NuRecord* pRecord) /* * Allocate and initialize a new NuRecord struct. */ -static NuError -Nu_RecordNew(NuArchive* pArchive, NuRecord** ppRecord) +static NuError Nu_RecordNew(NuArchive* pArchive, NuRecord** ppRecord) { - Assert(ppRecord != nil); + Assert(ppRecord != NULL); *ppRecord = Nu_Malloc(pArchive, sizeof(**ppRecord)); - if (*ppRecord == nil) + if (*ppRecord == NULL) return kNuErrMalloc; return Nu_InitRecordContents(pArchive, *ppRecord); @@ -67,16 +65,15 @@ Nu_RecordNew(NuArchive* pArchive, NuRecord** ppRecord) * Free anything allocated within a record. Doesn't try to free the record * itself. */ -static NuError -Nu_FreeRecordContents(NuArchive* pArchive, NuRecord* pRecord) +static NuError Nu_FreeRecordContents(NuArchive* pArchive, NuRecord* pRecord) { - Assert(pRecord != nil); + Assert(pRecord != NULL); Nu_Free(pArchive, pRecord->recOptionList); Nu_Free(pArchive, pRecord->extraBytes); - Nu_Free(pArchive, pRecord->recFilename); - Nu_Free(pArchive, pRecord->threadFilename); - Nu_Free(pArchive, pRecord->newFilename); + Nu_Free(pArchive, pRecord->recFilenameMOR); + Nu_Free(pArchive, pRecord->threadFilenameMOR); + Nu_Free(pArchive, pRecord->newFilenameMOR); Nu_Free(pArchive, pRecord->pThreads); /* don't Free(pRecord->pNext)! */ Nu_FreeThreadMods(pArchive, pRecord); @@ -89,10 +86,9 @@ Nu_FreeRecordContents(NuArchive* pArchive, NuRecord* pRecord) /* * Free up a NuRecord struct. */ -static NuError -Nu_RecordFree(NuArchive* pArchive, NuRecord* pRecord) +static NuError Nu_RecordFree(NuArchive* pArchive, NuRecord* pRecord) { - if (pRecord == nil) + if (pRecord == NULL) return kNuErrNone; (void) Nu_FreeRecordContents(pArchive, pRecord); @@ -105,23 +101,23 @@ Nu_RecordFree(NuArchive* pArchive, NuRecord* pRecord) * Copy a field comprised of a buffer and a length from one structure to * another. It is assumed that the length value has already been copied. */ -static NuError -CopySizedField(NuArchive* pArchive, void* vppDst, const void* vpSrc, uint len) +static NuError CopySizedField(NuArchive* pArchive, void* vppDst, + const void* vpSrc, uint32_t len) { NuError err = kNuErrNone; - uchar** ppDst = vppDst; - const uchar* pSrc = vpSrc; + uint8_t** ppDst = vppDst; + const uint8_t* pSrc = vpSrc; - Assert(ppDst != nil); + Assert(ppDst != NULL); if (len) { - Assert(pSrc != nil); + Assert(pSrc != NULL); *ppDst = Nu_Malloc(pArchive, len); BailAlloc(*ppDst); memcpy(*ppDst, pSrc, len); } else { - Assert(pSrc == nil); - *ppDst = nil; + Assert(pSrc == NULL); + *ppDst = NULL; } bail: @@ -131,8 +127,8 @@ bail: /* * Make a copy of a record. */ -static NuError -Nu_RecordCopy(NuArchive* pArchive, NuRecord** ppDst, const NuRecord* pSrc) +static NuError Nu_RecordCopy(NuArchive* pArchive, NuRecord** ppDst, + const NuRecord* pSrc) { NuError err; NuRecord* pDst; @@ -147,29 +143,29 @@ Nu_RecordCopy(NuArchive* pArchive, NuRecord** ppDst, const NuRecord* pSrc) pSrc->recOptionSize); CopySizedField(pArchive, &pDst->extraBytes, pSrc->extraBytes, pSrc->extraCount); - CopySizedField(pArchive, &pDst->recFilename, pSrc->recFilename, + CopySizedField(pArchive, &pDst->recFilenameMOR, pSrc->recFilenameMOR, pSrc->recFilenameLength == 0 ? 0 : pSrc->recFilenameLength+1); - CopySizedField(pArchive, &pDst->threadFilename, pSrc->threadFilename, - pSrc->threadFilename == nil ? 0 : strlen(pSrc->threadFilename) +1); - CopySizedField(pArchive, &pDst->newFilename, pSrc->newFilename, - pSrc->newFilename == nil ? 0 : strlen(pSrc->newFilename) +1); + CopySizedField(pArchive, &pDst->threadFilenameMOR, pSrc->threadFilenameMOR, + pSrc->threadFilenameMOR == NULL ? 0 : strlen(pSrc->threadFilenameMOR) +1); + CopySizedField(pArchive, &pDst->newFilenameMOR, pSrc->newFilenameMOR, + pSrc->newFilenameMOR == NULL ? 0 : strlen(pSrc->newFilenameMOR) +1); CopySizedField(pArchive, &pDst->pThreads, pSrc->pThreads, pSrc->recTotalThreads * sizeof(*pDst->pThreads)); /* now figure out what the filename is supposed to point at */ - if (pSrc->filename == pSrc->threadFilename) - pDst->filename = pDst->threadFilename; - else if (pSrc->filename == pSrc->recFilename) - pDst->filename = pDst->recFilename; - else if (pSrc->filename == pSrc->newFilename) - pDst->filename = pDst->newFilename; + if (pSrc->filenameMOR == pSrc->threadFilenameMOR) + pDst->filenameMOR = pDst->threadFilenameMOR; + else if (pSrc->filenameMOR == pSrc->recFilenameMOR) + pDst->filenameMOR = pDst->recFilenameMOR; + else if (pSrc->filenameMOR == pSrc->newFilenameMOR) + pDst->filenameMOR = pDst->newFilenameMOR; else - pDst->filename = pSrc->filename; /* probably static kDefault value */ + pDst->filenameMOR = pSrc->filenameMOR; /* probably static kDefault value */ - pDst->pNext = nil; + pDst->pNext = NULL; /* these only hold for copy from orig... may need to remove */ - Assert(pSrc->pThreadMods == nil); + Assert(pSrc->pThreadMods == NULL); Assert(!pSrc->dirtyHeader); bail: @@ -189,25 +185,24 @@ bail: * acceptable. We could do simple optimizations, like only preserving * ordering for "add" threadMods, but even that seems silly. */ -void -Nu_RecordAddThreadMod(NuRecord* pRecord, NuThreadMod* pThreadMod) +void Nu_RecordAddThreadMod(NuRecord* pRecord, NuThreadMod* pThreadMod) { NuThreadMod* pScanThreadMod; - Assert(pRecord != nil); - Assert(pThreadMod != nil); + Assert(pRecord != NULL); + Assert(pThreadMod != NULL); - if (pRecord->pThreadMods == nil) { + if (pRecord->pThreadMods == NULL) { pRecord->pThreadMods = pThreadMod; } else { pScanThreadMod = pRecord->pThreadMods; - while (pScanThreadMod->pNext != nil) + while (pScanThreadMod->pNext != NULL) pScanThreadMod = pScanThreadMod->pNext; pScanThreadMod->pNext = pThreadMod; } - pThreadMod->pNext = nil; + pThreadMod->pNext = NULL; } @@ -224,18 +219,17 @@ Nu_RecordAddThreadMod(NuRecord* pRecord, NuThreadMod* pThreadMod) * threads, subtract the number of deletes, and return "true" if the net * result is zero. */ -Boolean -Nu_RecordIsEmpty(NuArchive* pArchive, const NuRecord* pRecord) +Boolean Nu_RecordIsEmpty(NuArchive* pArchive, const NuRecord* pRecord) { const NuThreadMod* pThreadMod; int numThreads; - Assert(pRecord != nil); + Assert(pRecord != NULL); numThreads = pRecord->recTotalThreads; pThreadMod = pRecord->pThreadMods; - while (pThreadMod != nil) { + while (pThreadMod != NULL) { switch (pThreadMod->entry.kind) { case kNuThreadModAdd: case kNuThreadModUpdate: @@ -275,51 +269,43 @@ Nu_RecordIsEmpty(NuArchive* pArchive, const NuRecord* pRecord) * Trivial getters and setters */ -Boolean -Nu_RecordSet_GetLoaded(const NuRecordSet* pRecordSet) +Boolean Nu_RecordSet_GetLoaded(const NuRecordSet* pRecordSet) { - Assert(pRecordSet != nil); + Assert(pRecordSet != NULL); return pRecordSet->loaded; } -void -Nu_RecordSet_SetLoaded(NuRecordSet* pRecordSet, Boolean val) +void Nu_RecordSet_SetLoaded(NuRecordSet* pRecordSet, Boolean val) { pRecordSet->loaded = val; } -ulong -Nu_RecordSet_GetNumRecords(const NuRecordSet* pRecordSet) +uint32_t Nu_RecordSet_GetNumRecords(const NuRecordSet* pRecordSet) { return pRecordSet->numRecords; } -void -Nu_RecordSet_SetNumRecords(NuRecordSet* pRecordSet, ulong val) +void Nu_RecordSet_SetNumRecords(NuRecordSet* pRecordSet, uint32_t val) { pRecordSet->numRecords = val; } -void -Nu_RecordSet_IncNumRecords(NuRecordSet* pRecordSet) +void Nu_RecordSet_IncNumRecords(NuRecordSet* pRecordSet) { pRecordSet->numRecords++; } -NuRecord* -Nu_RecordSet_GetListHead(const NuRecordSet* pRecordSet) +NuRecord* Nu_RecordSet_GetListHead(const NuRecordSet* pRecordSet) { return pRecordSet->nuRecordHead; } -NuRecord** -Nu_RecordSet_GetListHeadPtr(NuRecordSet* pRecordSet) +NuRecord** Nu_RecordSet_GetListHeadPtr(NuRecordSet* pRecordSet) { return &pRecordSet->nuRecordHead; } -NuRecord* -Nu_RecordSet_GetListTail(const NuRecordSet* pRecordSet) +NuRecord* Nu_RecordSet_GetListTail(const NuRecordSet* pRecordSet) { return pRecordSet->nuRecordTail; } @@ -329,8 +315,7 @@ Nu_RecordSet_GetListTail(const NuRecordSet* pRecordSet) * Returns "true" if the record set has no records or hasn't ever been * used. */ -Boolean -Nu_RecordSet_IsEmpty(const NuRecordSet* pRecordSet) +Boolean Nu_RecordSet_IsEmpty(const NuRecordSet* pRecordSet) { if (!pRecordSet->loaded || pRecordSet->numRecords == 0) return true; @@ -341,23 +326,23 @@ Nu_RecordSet_IsEmpty(const NuRecordSet* pRecordSet) /* * Free the list of records, and reset the record sets to initial state. */ -NuError -Nu_RecordSet_FreeAllRecords(NuArchive* pArchive, NuRecordSet* pRecordSet) +NuError Nu_RecordSet_FreeAllRecords(NuArchive* pArchive, + NuRecordSet* pRecordSet) { NuError err = kNuErrNone; NuRecord* pRecord; NuRecord* pNextRecord; if (!pRecordSet->loaded) { - Assert(pRecordSet->nuRecordHead == nil); - Assert(pRecordSet->nuRecordTail == nil); + Assert(pRecordSet->nuRecordHead == NULL); + Assert(pRecordSet->nuRecordTail == NULL); Assert(pRecordSet->numRecords == 0); return kNuErrNone; } DBUG(("+++ FreeAllRecords\n")); pRecord = pRecordSet->nuRecordHead; - while (pRecord != nil) { + while (pRecord != NULL) { pNextRecord = pRecord->pNext; err = Nu_RecordFree(pArchive, pRecord); @@ -366,7 +351,7 @@ Nu_RecordSet_FreeAllRecords(NuArchive* pArchive, NuRecordSet* pRecordSet) pRecord = pNextRecord; } - pRecordSet->nuRecordHead = pRecordSet->nuRecordTail = nil; + pRecordSet->nuRecordHead = pRecordSet->nuRecordTail = NULL; pRecordSet->numRecords = 0; pRecordSet->loaded = false; @@ -378,23 +363,23 @@ bail: /* * Add a new record to the end of the list. */ -static NuError -Nu_RecordSet_AddRecord(NuRecordSet* pRecordSet, NuRecord* pRecord) +static NuError Nu_RecordSet_AddRecord(NuRecordSet* pRecordSet, + NuRecord* pRecord) { - Assert(pRecordSet != nil); - Assert(pRecord != nil); + Assert(pRecordSet != NULL); + Assert(pRecord != NULL); - /* if one is nil, both must be nil */ - Assert(pRecordSet->nuRecordHead == nil || pRecordSet->nuRecordTail != nil); - Assert(pRecordSet->nuRecordTail == nil || pRecordSet->nuRecordHead != nil); + /* if one is NULL, both must be NULL */ + Assert(pRecordSet->nuRecordHead == NULL || pRecordSet->nuRecordTail != NULL); + Assert(pRecordSet->nuRecordTail == NULL || pRecordSet->nuRecordHead != NULL); - if (pRecordSet->nuRecordHead == nil) { + if (pRecordSet->nuRecordHead == NULL) { /* empty list */ pRecordSet->nuRecordHead = pRecordSet->nuRecordTail = pRecord; pRecordSet->loaded = true; Assert(!pRecordSet->numRecords); } else { - pRecord->pNext = nil; + pRecord->pNext = NULL; pRecordSet->nuRecordTail->pNext = pRecord; pRecordSet->nuRecordTail = pRecord; } @@ -413,16 +398,15 @@ Nu_RecordSet_AddRecord(NuRecordSet* pRecordSet, NuRecord* pRecord) * (Should have a "heavy assert" mode where we verify that "ppRecord" * actually has something to do with pRecordSet.) */ -NuError -Nu_RecordSet_DeleteRecordPtr(NuArchive* pArchive, NuRecordSet* pRecordSet, - NuRecord** ppRecord) +NuError Nu_RecordSet_DeleteRecordPtr(NuArchive* pArchive, + NuRecordSet* pRecordSet, NuRecord** ppRecord) { NuError err; NuRecord* pRecord; - Assert(pRecordSet != nil); - Assert(ppRecord != nil); - Assert(*ppRecord != nil); + Assert(pRecordSet != NULL); + Assert(ppRecord != NULL); + Assert(*ppRecord != NULL); /* save a copy of the record we're freeing */ pRecord = *ppRecord; @@ -433,21 +417,21 @@ Nu_RecordSet_DeleteRecordPtr(NuArchive* pArchive, NuRecordSet* pRecordSet, /* if we're deleting the tail, we have to find the "new" last entry */ if (pRecord == pRecordSet->nuRecordTail) { - if (pRecordSet->nuRecordHead == nil) { + if (pRecordSet->nuRecordHead == NULL) { /* this was the last entry; we're done */ - pRecordSet->nuRecordTail = nil; + pRecordSet->nuRecordTail = NULL; } else { /* walk through the list... delete bottom-up will be slow! */ pRecordSet->nuRecordTail = pRecordSet->nuRecordHead; - while (pRecordSet->nuRecordTail->pNext != nil) + while (pRecordSet->nuRecordTail->pNext != NULL) pRecordSet->nuRecordTail = pRecordSet->nuRecordTail->pNext; } } if (pRecordSet->numRecords) - Assert(pRecordSet->nuRecordHead!=nil && pRecordSet->nuRecordTail!=nil); + Assert(pRecordSet->nuRecordHead!=NULL && pRecordSet->nuRecordTail!=NULL); else - Assert(pRecordSet->nuRecordHead==nil && pRecordSet->nuRecordTail==nil); + Assert(pRecordSet->nuRecordHead==NULL && pRecordSet->nuRecordTail==NULL); err = Nu_RecordFree(pArchive, pRecord); return err; @@ -456,16 +440,15 @@ Nu_RecordSet_DeleteRecordPtr(NuArchive* pArchive, NuRecordSet* pRecordSet, /* * Delete a record from the record set. */ -NuError -Nu_RecordSet_DeleteRecord(NuArchive* pArchive, NuRecordSet* pRecordSet, +NuError Nu_RecordSet_DeleteRecord(NuArchive* pArchive, NuRecordSet* pRecordSet, NuRecord* pRecord) { NuError err; NuRecord** ppRecord; ppRecord = Nu_RecordSet_GetListHeadPtr(pRecordSet); - Assert(ppRecord != nil); - Assert(*ppRecord != nil); + Assert(ppRecord != NULL); + Assert(*ppRecord != NULL); /* look for the record, so we can update his neighbors */ /* (this also ensures that the record really is in the set we think it is)*/ @@ -490,16 +473,15 @@ bail: * Make a clone of a record set. This is used to create the "copy" record * set out of the "orig" set. */ -NuError -Nu_RecordSet_Clone(NuArchive* pArchive, NuRecordSet* pDstSet, +NuError Nu_RecordSet_Clone(NuArchive* pArchive, NuRecordSet* pDstSet, const NuRecordSet* pSrcSet) { NuError err = kNuErrNone; const NuRecord* pSrcRecord; NuRecord* pDstRecord; - Assert(pDstSet != nil); - Assert(pSrcSet != nil); + Assert(pDstSet != NULL); + Assert(pSrcSet != NULL); Assert(Nu_RecordSet_GetLoaded(pDstSet) == false); Assert(Nu_RecordSet_GetLoaded(pSrcSet) == true); @@ -509,7 +491,7 @@ Nu_RecordSet_Clone(NuArchive* pArchive, NuRecordSet* pDstSet, /* copy each record over */ pSrcRecord = pSrcSet->nuRecordHead; - while (pSrcRecord != nil) { + while (pSrcRecord != NULL) { err = Nu_RecordCopy(pArchive, &pDstRecord, pSrcRecord); BailError(err); err = Nu_RecordSet_AddRecord(pDstSet, pDstRecord); @@ -533,23 +515,22 @@ bail: * * On completion, "pSrcSet" will be empty and "unloaded". */ -NuError -Nu_RecordSet_MoveAllRecords(NuArchive* pArchive, NuRecordSet* pDstSet, +NuError Nu_RecordSet_MoveAllRecords(NuArchive* pArchive, NuRecordSet* pDstSet, NuRecordSet* pSrcSet) { NuError err = kNuErrNone; - Assert(pDstSet != nil); - Assert(pSrcSet != nil); + Assert(pDstSet != NULL); + Assert(pSrcSet != NULL); /* move records over */ if (Nu_RecordSet_GetNumRecords(pSrcSet)) { Assert(pSrcSet->loaded); - Assert(pSrcSet->nuRecordHead != nil); - Assert(pSrcSet->nuRecordTail != nil); - if (pDstSet->nuRecordHead == nil) { + Assert(pSrcSet->nuRecordHead != NULL); + Assert(pSrcSet->nuRecordTail != NULL); + if (pDstSet->nuRecordHead == NULL) { /* empty dst list */ - Assert(pDstSet->nuRecordTail == nil); + Assert(pDstSet->nuRecordTail == NULL); pDstSet->nuRecordHead = pSrcSet->nuRecordHead; pDstSet->nuRecordTail = pSrcSet->nuRecordTail; pDstSet->numRecords = pSrcSet->numRecords; @@ -557,22 +538,22 @@ Nu_RecordSet_MoveAllRecords(NuArchive* pArchive, NuRecordSet* pDstSet, } else { /* append to dst list */ Assert(pDstSet->loaded); - Assert(pDstSet->nuRecordTail != nil); + Assert(pDstSet->nuRecordTail != NULL); pDstSet->nuRecordTail->pNext = pSrcSet->nuRecordHead; pDstSet->nuRecordTail = pSrcSet->nuRecordTail; pDstSet->numRecords += pSrcSet->numRecords; } } else { /* no records in src set */ - Assert(pSrcSet->nuRecordHead == nil); - Assert(pSrcSet->nuRecordTail == nil); + Assert(pSrcSet->nuRecordHead == NULL); + Assert(pSrcSet->nuRecordTail == NULL); if (pSrcSet->loaded) pDstSet->loaded = true; } /* nuke all pointers in original list */ - pSrcSet->nuRecordHead = pSrcSet->nuRecordTail = nil; + pSrcSet->nuRecordHead = pSrcSet->nuRecordTail = NULL; pSrcSet->numRecords = 0; pSrcSet->loaded = false; @@ -583,14 +564,13 @@ Nu_RecordSet_MoveAllRecords(NuArchive* pArchive, NuRecordSet* pDstSet, /* * Find a record in the list by index. */ -NuError -Nu_RecordSet_FindByIdx(const NuRecordSet* pRecordSet, NuRecordIdx recIdx, - NuRecord** ppRecord) +NuError Nu_RecordSet_FindByIdx(const NuRecordSet* pRecordSet, + NuRecordIdx recIdx, NuRecord** ppRecord) { NuRecord* pRecord; pRecord = pRecordSet->nuRecordHead; - while (pRecord != nil) { + while (pRecord != NULL) { if (pRecord->recordIdx == recIdx) { *ppRecord = pRecord; return kNuErrNone; @@ -606,15 +586,14 @@ Nu_RecordSet_FindByIdx(const NuRecordSet* pRecordSet, NuRecordIdx recIdx, /* * Search for a specific thread in all records in the specified record set. */ -NuError -Nu_RecordSet_FindByThreadIdx(NuRecordSet* pRecordSet, NuThreadIdx threadIdx, - NuRecord** ppRecord, NuThread** ppThread) +NuError Nu_RecordSet_FindByThreadIdx(NuRecordSet* pRecordSet, + NuThreadIdx threadIdx, NuRecord** ppRecord, NuThread** ppThread) { NuError err = kNuErrThreadIdxNotFound; NuRecord* pRecord; pRecord = Nu_RecordSet_GetListHead(pRecordSet); - while (pRecord != nil) { + while (pRecord != NULL) { err = Nu_FindThreadByIdx(pRecord, threadIdx, ppThread); if (err == kNuErrNone) { *ppRecord = pRecord; @@ -623,7 +602,7 @@ Nu_RecordSet_FindByThreadIdx(NuRecordSet* pRecordSet, NuThreadIdx threadIdx, pRecord = pRecord->pNext; } - Assert(err != kNuErrNone || (*ppRecord != nil && *ppThread != nil)); + Assert(err != kNuErrNone || (*ppRecord != NULL && *ppThread != NULL)); return err; } @@ -650,13 +629,12 @@ Nu_RecordSet_FindByThreadIdx(NuRecordSet* pRecordSet, NuThreadIdx threadIdx, * string pointed to by name1 is greater than, equal to, or less than * the string pointed to by s2, respectively (i.e. same as strcmp). */ -static int -Nu_CompareRecordNames(const char* name1, const char* name2) +static int Nu_CompareRecordNames(const char* name1MOR, const char* name2MOR) { #ifdef NU_CASE_SENSITIVE - return strcmp(name1, name2); + return strcmp(name1MOR, name2MOR); #else - return strcasecmp(name1, name2); + return strcasecmp(name1MOR, name2MOR); #endif } @@ -664,20 +642,19 @@ Nu_CompareRecordNames(const char* name1, const char* name2) /* * Find a record in the list by storageName. */ -static NuError -Nu_RecordSet_FindByName(const NuRecordSet* pRecordSet, const char* name, - NuRecord** ppRecord) +static NuError Nu_RecordSet_FindByName(const NuRecordSet* pRecordSet, + const char* nameMOR, NuRecord** ppRecord) { NuRecord* pRecord; - Assert(pRecordSet != nil); + Assert(pRecordSet != NULL); Assert(pRecordSet->loaded); - Assert(name != nil); - Assert(ppRecord != nil); + Assert(nameMOR != NULL); + Assert(ppRecord != NULL); pRecord = pRecordSet->nuRecordHead; - while (pRecord != nil) { - if (Nu_CompareRecordNames(pRecord->filename, name) == 0) { + while (pRecord != NULL) { + if (Nu_CompareRecordNames(pRecord->filenameMOR, nameMOR) == 0) { *ppRecord = pRecord; return kNuErrNone; } @@ -696,27 +673,26 @@ Nu_RecordSet_FindByName(const NuRecordSet* pRecordSet, const char* name, * up scanning the entire list and keeping the last match. If this * causes a notable reduction in efficiency we'll have to fix this. */ -static NuError -Nu_RecordSet_ReverseFindByName(const NuRecordSet* pRecordSet, const char* name, - NuRecord** ppRecord) +static NuError Nu_RecordSet_ReverseFindByName(const NuRecordSet* pRecordSet, + const char* nameMOR, NuRecord** ppRecord) { NuRecord* pRecord; - NuRecord* pFoundRecord = nil; + NuRecord* pFoundRecord = NULL; - Assert(pRecordSet != nil); + Assert(pRecordSet != NULL); Assert(pRecordSet->loaded); - Assert(name != nil); - Assert(ppRecord != nil); + Assert(nameMOR != NULL); + Assert(ppRecord != NULL); pRecord = pRecordSet->nuRecordHead; - while (pRecord != nil) { - if (Nu_CompareRecordNames(pRecord->filename, name) == 0) + while (pRecord != NULL) { + if (Nu_CompareRecordNames(pRecord->filenameMOR, nameMOR) == 0) pFoundRecord = pRecord; pRecord = pRecord->pNext; } - if (pFoundRecord != nil) { + if (pFoundRecord != NULL) { *ppRecord = pFoundRecord; return kNuErrNone; } @@ -734,20 +710,19 @@ Nu_RecordSet_ReverseFindByName(const NuRecordSet* pRecordSet, const char* name, * * "ppNewRecord" will get a pointer to the newly-created clone. */ -NuError -Nu_RecordSet_ReplaceRecord(NuArchive* pArchive, NuRecordSet* pBadSet, +NuError Nu_RecordSet_ReplaceRecord(NuArchive* pArchive, NuRecordSet* pBadSet, NuRecord* pBadRecord, NuRecordSet* pGoodSet, NuRecord** ppNewRecord) { NuError err; NuRecord* pGoodRecord; NuRecord* pSiblingRecord; - NuRecord* pNewRecord = nil; + NuRecord* pNewRecord = NULL; - Assert(pArchive != nil); - Assert(pBadSet != nil); - Assert(pBadRecord != nil); - Assert(pGoodSet != nil); - Assert(ppNewRecord != nil); + Assert(pArchive != NULL); + Assert(pBadSet != NULL); + Assert(pBadRecord != NULL); + Assert(pGoodSet != NULL); + Assert(ppNewRecord != NULL); /* * Find a record in "pGoodSet" that has the same record index as @@ -775,10 +750,10 @@ Nu_RecordSet_ReplaceRecord(NuArchive* pArchive, NuRecordSet* pBadSet, else { /* find the record that points to pBadRecord */ pSiblingRecord = pBadSet->nuRecordHead; - while (pSiblingRecord->pNext != pBadRecord && pSiblingRecord != nil) + while (pSiblingRecord->pNext != pBadRecord && pSiblingRecord != NULL) pSiblingRecord = pSiblingRecord->pNext; - if (pSiblingRecord == nil) { + if (pSiblingRecord == NULL) { /* looks like "pBadRecord" wasn't part of "pBadSet" after all */ Assert(0); err = kNuErrInternal; @@ -792,10 +767,10 @@ Nu_RecordSet_ReplaceRecord(NuArchive* pArchive, NuRecordSet* pBadSet, BailError(err); *ppNewRecord = pNewRecord; - pNewRecord = nil; /* don't free */ + pNewRecord = NULL; /* don't free */ bail: - if (pNewRecord != nil) + if (pNewRecord != NULL) Nu_RecordFree(pArchive, pNewRecord); return err; } @@ -811,26 +786,28 @@ bail: * Ask the user if it's okay to ignore a bad CRC. If we can't ask the * user, return "false". */ -Boolean -Nu_ShouldIgnoreBadCRC(NuArchive* pArchive, const NuRecord* pRecord, NuError err) +Boolean Nu_ShouldIgnoreBadCRC(NuArchive* pArchive, const NuRecord* pRecord, + NuError err) { NuErrorStatus errorStatus; NuResult result; Boolean retval = false; + UNICHAR* pathnameUNI = NULL; Assert(pArchive->valIgnoreCRC == false); - if (pArchive->errorHandlerFunc != nil) { + if (pArchive->errorHandlerFunc != NULL) { errorStatus.operation = kNuOpTest; /* mostly accurate */ errorStatus.err = err; errorStatus.sysErr = 0; - errorStatus.message = nil; + errorStatus.message = NULL; errorStatus.pRecord = pRecord; - errorStatus.pathname = nil; - errorStatus.origPathname = nil; + errorStatus.pathnameUNI = NULL; + errorStatus.origPathname = NULL; errorStatus.filenameSeparator = 0; - if (pRecord != nil) { - errorStatus.pathname = pRecord->filename; + if (pRecord != NULL) { + pathnameUNI = Nu_CopyMORToUNI(pRecord->filenameMOR); + errorStatus.pathnameUNI = pathnameUNI; errorStatus.filenameSeparator = NuGetSepFromSysInfo(pRecord->recFileSysInfo); } @@ -862,6 +839,7 @@ Nu_ShouldIgnoreBadCRC(NuArchive* pArchive, const NuRecord* pRecord, NuError err) } bail: + Nu_Free(pArchive, pathnameUNI); return retval; } @@ -872,25 +850,24 @@ bail: * * Pass in a NuRecord structure that will hold the data we read. */ -static NuError -Nu_ReadRecordHeader(NuArchive* pArchive, NuRecord* pRecord) +static NuError Nu_ReadRecordHeader(NuArchive* pArchive, NuRecord* pRecord) { NuError err = kNuErrNone; - ushort crc; + uint16_t crc; FILE* fp; int bytesRead; - Assert(pArchive != nil); - Assert(pRecord != nil); - Assert(pRecord->pThreads == nil); - Assert(pRecord->pNext == nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); + Assert(pRecord->pThreads == NULL); + Assert(pRecord->pNext == NULL); fp = pArchive->archiveFp; pRecord->recordIdx = Nu_GetNextRecordIdx(pArchive); /* points to whichever filename storage we like best */ - pRecord->filename = nil; + pRecord->filenameMOR = NULL; pRecord->fileOffset = pArchive->currentOffset; (void) Nu_ReadBytes(pArchive, fp, pRecord->recNufxID, kNufxIDLen); @@ -941,7 +918,7 @@ Nu_ReadRecordHeader(NuArchive* pArchive, NuRecord* pRecord) } if (pRecord->recTotalThreads > kNuReasonableTotalThreads) { err = kNuErrBadRecord; - Nu_ReportError(NU_BLOB, err, "Unreasonable number of threads (%lu)", + Nu_ReportError(NU_BLOB, err, "Unreasonable number of threads (%u)", pRecord->recTotalThreads); goto bail; } @@ -988,7 +965,7 @@ Nu_ReadRecordHeader(NuArchive* pArchive, NuRecord* pRecord) } } else { pRecord->recOptionSize = 0; - pRecord->recOptionList = nil; + pRecord->recOptionList = NULL; } /* last two bytes are the filename len; all else is "extra" */ @@ -1020,18 +997,19 @@ Nu_ReadRecordHeader(NuArchive* pArchive, NuRecord* pRecord) goto bail; } if (pRecord->recFilenameLength) { - pRecord->recFilename = Nu_Malloc(pArchive, pRecord->recFilenameLength +1); - BailAlloc(pRecord->recFilename); - (void) Nu_ReadBytesC(pArchive, fp, pRecord->recFilename, + pRecord->recFilenameMOR = + Nu_Malloc(pArchive, pRecord->recFilenameLength +1); + BailAlloc(pRecord->recFilenameMOR); + (void) Nu_ReadBytesC(pArchive, fp, pRecord->recFilenameMOR, pRecord->recFilenameLength, &crc); - pRecord->recFilename[pRecord->recFilenameLength] = '\0'; + pRecord->recFilenameMOR[pRecord->recFilenameLength] = '\0'; bytesRead += pRecord->recFilenameLength; - Nu_StripHiIfAllSet(pRecord->recFilename); + Nu_StripHiIfAllSet(pRecord->recFilenameMOR); /* use the in-header one */ - pRecord->filename = pRecord->recFilename; + pRecord->filenameMOR = pRecord->recFilenameMOR; } /* @@ -1056,7 +1034,7 @@ Nu_ReadRecordHeader(NuArchive* pArchive, NuRecord* pRecord) Nu_ReportError(NU_BLOB, err, "Stored RH CRC=0x%04x, calc=0x%04x", pRecord->recHeaderCRC, crc); Nu_ReportError(NU_BLOB_DEBUG, kNuErrNone, - "--- Problematic record is id=%ld", pRecord->recordIdx); + "--- Problematic record is id=%u", pRecord->recordIdx); goto bail; } } @@ -1113,8 +1091,7 @@ bail: * NOTE: for data files (types 1, 2, and 3), the actual value may not match * up what ProDOS would use, because this doesn't test for sparseness. */ -static void -Nu_UpdateStorageType(NuArchive* pArchive, NuRecord* pRecord) +static void Nu_UpdateStorageType(NuArchive* pArchive, NuRecord* pRecord) { NuError err; NuThread* pThread; @@ -1167,17 +1144,16 @@ bail: * The position of the file pointer on exit is undefined. The position * past the end of the record will be stored in pArchive->currentOffset. */ -NuError -Nu_WriteRecordHeader(NuArchive* pArchive, NuRecord* pRecord, FILE* fp) +NuError Nu_WriteRecordHeader(NuArchive* pArchive, NuRecord* pRecord, FILE* fp) { NuError err = kNuErrNone; - ushort crc; + uint16_t crc; long crcOffset; int bytesWritten; - Assert(pArchive != nil); - Assert(pRecord != nil); - Assert(fp != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); + Assert(fp != NULL); /* * Before we get started, let's make sure the storageType makes sense @@ -1199,7 +1175,7 @@ Nu_WriteRecordHeader(NuArchive* pArchive, NuRecord* pRecord, FILE* fp) Nu_WriteTwoC(pArchive, fp, pRecord->recAttribCount, &crc); Nu_WriteTwoC(pArchive, fp, pRecord->recVersionNumber, &crc); Nu_WriteFourC(pArchive, fp, pRecord->recTotalThreads, &crc); - Nu_WriteTwoC(pArchive, fp, (ushort)pRecord->recFileSysID, &crc); + Nu_WriteTwoC(pArchive, fp, (uint16_t)pRecord->recFileSysID, &crc); Nu_WriteTwoC(pArchive, fp, pRecord->recFileSysInfo, &crc); Nu_WriteFourC(pArchive, fp, pRecord->recAccess, &crc); Nu_WriteFourC(pArchive, fp, pRecord->recFileType, &crc); @@ -1254,7 +1230,7 @@ Nu_WriteRecordHeader(NuArchive* pArchive, NuRecord* pRecord, FILE* fp) if (pRecord->recFilenameLength && !pRecord->dropRecFilename) { Nu_WriteTwoC(pArchive, fp, pRecord->recFilenameLength, &crc); bytesWritten += 2; - Nu_WriteBytesC(pArchive, fp, pRecord->recFilename, + Nu_WriteBytesC(pArchive, fp, pRecord->recFilenameMOR, pRecord->recFilenameLength, &crc); } else { Nu_WriteTwoC(pArchive, fp, 0, &crc); @@ -1312,17 +1288,16 @@ bail: * Prepare for a "walk" through the records. This is useful for the * "read the TOC as you go" method of archive use. */ -static NuError -Nu_RecordWalkPrepare(NuArchive* pArchive, NuRecord** ppRecord) +static NuError Nu_RecordWalkPrepare(NuArchive* pArchive, NuRecord** ppRecord) { NuError err = kNuErrNone; - Assert(pArchive != nil); - Assert(ppRecord != nil); + Assert(pArchive != NULL); + Assert(ppRecord != NULL); DBUG(("--- walk prep\n")); - *ppRecord = nil; + *ppRecord = NULL; if (!pArchive->haveToc) { /* might have tried and aborted earlier, rewind to start of records */ @@ -1345,23 +1320,22 @@ bail: * we read it from the archive file, and add it to the TOC being * constructed. */ -static NuError -Nu_RecordWalkGetNext(NuArchive* pArchive, NuRecord** ppRecord) +static NuError Nu_RecordWalkGetNext(NuArchive* pArchive, NuRecord** ppRecord) { NuError err = kNuErrNone; - Assert(pArchive != nil); - Assert(ppRecord != nil); + Assert(pArchive != NULL); + Assert(ppRecord != NULL); /*DBUG(("--- walk toc=%d\n", pArchive->haveToc));*/ if (pArchive->haveToc) { - if (*ppRecord == nil) + if (*ppRecord == NULL) *ppRecord = Nu_RecordSet_GetListHead(&pArchive->origRecordSet); else *ppRecord = (*ppRecord)->pNext; } else { - *ppRecord = nil; /* so we don't try to free it on exit */ + *ppRecord = NULL; /* so we don't try to free it on exit */ /* allocate and fill in a new record */ err = Nu_RecordNew(pArchive, ppRecord); @@ -1373,7 +1347,7 @@ Nu_RecordWalkGetNext(NuArchive* pArchive, NuRecord** ppRecord) err = Nu_ScanThreads(pArchive, *ppRecord, (*ppRecord)->recTotalThreads); BailError(err); - DBUG(("--- Found record '%s'\n", (*ppRecord)->filename)); + DBUG(("--- Found record '%s'\n", (*ppRecord)->filenameMOR)); /* add to list */ err = Nu_RecordSet_AddRecord(&pArchive->origRecordSet, *ppRecord); @@ -1384,7 +1358,7 @@ bail: if (err != kNuErrNone && !pArchive->haveToc) { /* on failure, free whatever we allocated */ Nu_RecordFree(pArchive, *ppRecord); - *ppRecord = nil; + *ppRecord = NULL; } return err; } @@ -1394,8 +1368,7 @@ bail: * full table of contents. On an unsuccessful walk, blow away the TOC * if we don't have all of it. */ -static NuError -Nu_RecordWalkFinish(NuArchive* pArchive, NuError walkErr) +static NuError Nu_RecordWalkFinish(NuArchive* pArchive, NuError walkErr) { if (pArchive->haveToc) return kNuErrNone; @@ -1418,14 +1391,13 @@ Nu_RecordWalkFinish(NuArchive* pArchive, NuError walkErr) * * Uses the "record walk" functions, because they're there. */ -NuError -Nu_GetTOCIfNeeded(NuArchive* pArchive) +NuError Nu_GetTOCIfNeeded(NuArchive* pArchive) { NuError err = kNuErrNone; NuRecord* pRecord; - ulong count; + uint32_t count; - Assert(pArchive != nil); + Assert(pArchive != NULL); if (pArchive->haveToc) goto bail; @@ -1458,15 +1430,14 @@ bail: * Run through the entire archive, pulling out the header bits, skipping * over the data bits, and calling "contentFunc" for each record. */ -NuError -Nu_StreamContents(NuArchive* pArchive, NuCallback contentFunc) +NuError Nu_StreamContents(NuArchive* pArchive, NuCallback contentFunc) { NuError err = kNuErrNone; NuRecord tmpRecord; NuResult result; - ulong count; + uint32_t count; - if (contentFunc == nil) { + if (contentFunc == NULL) { err = kNuErrInvalidArg; goto bail; } @@ -1516,13 +1487,13 @@ bail: * to recreate the original we need to re-add the resource fork so * NufxLib knows to make it an extended file. */ -static NuError -Nu_FakeZeroExtract(NuArchive* pArchive, NuRecord* pRecord, int threadKind) +static NuError Nu_FakeZeroExtract(NuArchive* pArchive, NuRecord* pRecord, + int threadKind) { NuError err; NuThread fakeThread; - Assert(pRecord != nil); + Assert(pRecord != NULL); DBUG(("--- found empty record, creating zero-byte file (kind=0x%04x)\n", threadKind)); @@ -1549,17 +1520,16 @@ Nu_FakeZeroExtract(NuArchive* pArchive, NuRecord* pRecord, int threadKind) /* * Run through the entire archive, extracting the contents. */ -NuError -Nu_StreamExtract(NuArchive* pArchive) +NuError Nu_StreamExtract(NuArchive* pArchive) { NuError err = kNuErrNone; NuRecord tmpRecord; Boolean hasInterestingThread; - ulong count; + uint32_t count; long idx; /* reset this just to be safe */ - pArchive->lastDirCreated = nil; + pArchive->lastDirCreatedUNI = NULL; Nu_InitRecordContents(pArchive, &tmpRecord); count = pArchive->masterHeader.mhTotalRecords; @@ -1593,7 +1563,7 @@ Nu_StreamExtract(NuArchive* pArchive) BailError(err); } else idx = 0; - if (tmpRecord.filename == nil) { + if (tmpRecord.filenameMOR == NULL) { Nu_ReportError(NU_BLOB, kNuErrNone, "Couldn't find filename in record"); err = kNuErrBadRecord; @@ -1606,7 +1576,7 @@ Nu_StreamExtract(NuArchive* pArchive) hasInterestingThread = false; /* extract all relevant (remaining) threads */ - pArchive->lastFileCreated = nil; + pArchive->lastFileCreatedUNI = NULL; for ( ; idx < (long)tmpRecord.recTotalThreads; idx++) { const NuThread* pThread = Nu_GetThread(&tmpRecord, idx); @@ -1662,8 +1632,7 @@ bail: * Test the contents of an archive. Works just like extraction, but we * don't store anything. */ -NuError -Nu_StreamTest(NuArchive* pArchive) +NuError Nu_StreamTest(NuArchive* pArchive) { NuError err; @@ -1686,15 +1655,14 @@ Nu_StreamTest(NuArchive* pArchive) * This only walks through the "orig" list, so it does not reflect the * results of un-flushed changes. */ -NuError -Nu_Contents(NuArchive* pArchive, NuCallback contentFunc) +NuError Nu_Contents(NuArchive* pArchive, NuCallback contentFunc) { NuError err = kNuErrNone; NuRecord* pRecord; NuResult result; - ulong count; + uint32_t count; - if (contentFunc == nil) { + if (contentFunc == NULL) { err = kNuErrInvalidArg; goto bail; } @@ -1707,7 +1675,7 @@ Nu_Contents(NuArchive* pArchive, NuCallback contentFunc) err = Nu_RecordWalkGetNext(pArchive, &pRecord); BailError(err); - Assert(pRecord->filename != nil); + Assert(pRecord->filenameMOR != NULL); result = (*contentFunc)(pArchive, pRecord); if (result == kNuAbort) { err = kNuErrAborted; @@ -1727,19 +1695,18 @@ bail: * * This assumes random access, so it can't be used in streaming mode. */ -static NuError -Nu_ExtractRecordByPtr(NuArchive* pArchive, NuRecord* pRecord) +static NuError Nu_ExtractRecordByPtr(NuArchive* pArchive, NuRecord* pRecord) { NuError err = kNuErrNone; Boolean hasInterestingThread; - ulong idx; + uint32_t idx; Assert(!Nu_IsStreaming(pArchive)); /* we don't skip things we don't read */ - Assert(pRecord != nil); + Assert(pRecord != NULL); /* extract all relevant threads */ hasInterestingThread = false; - pArchive->lastFileCreated = nil; + pArchive->lastFileCreatedUNI = NULL; for (idx = 0; idx < pRecord->recTotalThreads; idx++) { const NuThread* pThread = Nu_GetThread(pRecord, idx); @@ -1759,7 +1726,7 @@ Nu_ExtractRecordByPtr(NuArchive* pArchive, NuRecord* pRecord) } DBUG(("IGNORING 0x%08lx from '%s'\n", NuMakeThreadID(pThread->thThreadClass, pThread->thThreadKind), - pRecord->filename)); + pRecord->filenameMOR)); } } @@ -1796,16 +1763,15 @@ bail: /* * Extract a big buncha files. */ -NuError -Nu_Extract(NuArchive* pArchive) +NuError Nu_Extract(NuArchive* pArchive) { NuError err; - NuRecord* pRecord = nil; - ulong count; + NuRecord* pRecord = NULL; + uint32_t count; long offset; /* reset this just to be safe */ - pArchive->lastDirCreated = nil; + pArchive->lastDirCreatedUNI = NULL; err = Nu_RecordWalkPrepare(pArchive, &pRecord); BailError(err); @@ -1842,8 +1808,7 @@ bail: /* * Extract a single record. */ -NuError -Nu_ExtractRecord(NuArchive* pArchive, NuRecordIdx recIdx) +NuError Nu_ExtractRecord(NuArchive* pArchive, NuRecordIdx recIdx) { NuError err; NuRecord* pRecord; @@ -1856,7 +1821,7 @@ Nu_ExtractRecord(NuArchive* pArchive, NuRecordIdx recIdx) /* find the correct record by index */ err = Nu_RecordSet_FindByIdx(&pArchive->origRecordSet, recIdx, &pRecord); BailError(err); - Assert(pRecord != nil); + Assert(pRecord != NULL); /* extract whatever looks promising */ err = Nu_ExtractRecordByPtr(pArchive, pRecord); @@ -1871,8 +1836,7 @@ bail: * Test the contents of an archive. Works just like extraction, but we * don't store anything. */ -NuError -Nu_Test(NuArchive* pArchive) +NuError Nu_Test(NuArchive* pArchive) { NuError err; @@ -1885,8 +1849,7 @@ Nu_Test(NuArchive* pArchive) /* * Test a single record. */ -NuError -Nu_TestRecord(NuArchive* pArchive, NuRecordIdx recIdx) +NuError Nu_TestRecord(NuArchive* pArchive, NuRecordIdx recIdx) { NuError err; NuRecord* pRecord; @@ -1899,7 +1862,7 @@ Nu_TestRecord(NuArchive* pArchive, NuRecordIdx recIdx) /* find the correct record by index */ err = Nu_RecordSet_FindByIdx(&pArchive->origRecordSet, recIdx, &pRecord); BailError(err); - Assert(pRecord != nil); + Assert(pRecord != NULL); /* extract whatever looks promising */ pArchive->testMode = true; @@ -1919,13 +1882,12 @@ bail: * for records that have been deleted. It will not reflect changes * made by previous "write" calls, not even SetRecordAttr. */ -NuError -Nu_GetRecord(NuArchive* pArchive, NuRecordIdx recordIdx, +NuError Nu_GetRecord(NuArchive* pArchive, NuRecordIdx recordIdx, const NuRecord** ppRecord) { NuError err; - if (recordIdx == 0 || ppRecord == nil) + if (recordIdx == 0 || ppRecord == NULL) return kNuErrInvalidArg; if (Nu_IsStreaming(pArchive)) @@ -1936,7 +1898,7 @@ Nu_GetRecord(NuArchive* pArchive, NuRecordIdx recordIdx, err = Nu_RecordSet_FindByIdx(&pArchive->origRecordSet, recordIdx, (NuRecord**)ppRecord); if (err == kNuErrNone) { - Assert(*ppRecord != nil); + Assert(*ppRecord != NULL); } /* fall through with error */ @@ -1947,14 +1909,13 @@ bail: /* * Find the recordIdx of a record by storage name. */ -NuError -Nu_GetRecordIdxByName(NuArchive* pArchive, const char* name, +NuError Nu_GetRecordIdxByName(NuArchive* pArchive, const char* nameMOR, NuRecordIdx* pRecordIdx) { NuError err; - NuRecord* pRecord = nil; + NuRecord* pRecord = NULL; - if (pRecordIdx == nil) + if (pRecordIdx == NULL) return kNuErrInvalidArg; if (Nu_IsStreaming(pArchive)) @@ -1962,9 +1923,9 @@ Nu_GetRecordIdxByName(NuArchive* pArchive, const char* name, err = Nu_GetTOCIfNeeded(pArchive); BailError(err); - err = Nu_RecordSet_FindByName(&pArchive->origRecordSet, name, &pRecord); + err = Nu_RecordSet_FindByName(&pArchive->origRecordSet, nameMOR, &pRecord); if (err == kNuErrNone) { - Assert(pRecord != nil); + Assert(pRecord != NULL); *pRecordIdx = pRecord->recordIdx; } /* fall through with error */ @@ -1976,14 +1937,13 @@ bail: /* * Find the recordIdx of a record by zero-based position. */ -NuError -Nu_GetRecordIdxByPosition(NuArchive* pArchive, ulong position, +NuError Nu_GetRecordIdxByPosition(NuArchive* pArchive, uint32_t position, NuRecordIdx* pRecordIdx) { NuError err; const NuRecord* pRecord; - if (pRecordIdx == nil) + if (pRecordIdx == NULL) return kNuErrInvalidArg; if (Nu_IsStreaming(pArchive)) @@ -1998,7 +1958,7 @@ Nu_GetRecordIdxByPosition(NuArchive* pArchive, ulong position, pRecord = Nu_RecordSet_GetListHead(&pArchive->origRecordSet); while (position--) { - Assert(pRecord->pNext != nil); + Assert(pRecord->pNext != NULL); pRecord = pRecord->pNext; } @@ -2028,14 +1988,13 @@ bail: * The record returned will always be from the "copy" set. An error result * is returned if the record isn't found. */ -NuError -Nu_FindRecordForWriteByIdx(NuArchive* pArchive, NuRecordIdx recIdx, +NuError Nu_FindRecordForWriteByIdx(NuArchive* pArchive, NuRecordIdx recIdx, NuRecord** ppFoundRecord) { NuError err; - Assert(pArchive != nil); - Assert(ppFoundRecord != nil); + Assert(pArchive != NULL); + Assert(ppFoundRecord != NULL); if (Nu_RecordSet_GetLoaded(&pArchive->copyRecordSet)) { err = Nu_RecordSet_FindByIdx(&pArchive->copyRecordSet, recIdx, @@ -2044,7 +2003,7 @@ Nu_FindRecordForWriteByIdx(NuArchive* pArchive, NuRecordIdx recIdx, Assert(Nu_RecordSet_GetLoaded(&pArchive->origRecordSet)); err = Nu_RecordSet_FindByIdx(&pArchive->origRecordSet, recIdx, ppFoundRecord); - *ppFoundRecord = nil; /* can't delete from here */ + *ppFoundRecord = NULL; /* can't delete from here */ } BailErrorQuiet(err); @@ -2052,13 +2011,13 @@ Nu_FindRecordForWriteByIdx(NuArchive* pArchive, NuRecordIdx recIdx, * The record exists. If we were looking in the "orig" set, we have * to create a "copy" set and return it from there. */ - if (*ppFoundRecord == nil) { + if (*ppFoundRecord == NULL) { err = Nu_RecordSet_Clone(pArchive, &pArchive->copyRecordSet, &pArchive->origRecordSet); BailError(err); err = Nu_RecordSet_FindByIdx(&pArchive->copyRecordSet, recIdx, ppFoundRecord); - Assert(err == kNuErrNone && *ppFoundRecord != nil); /* must succeed */ + Assert(err == kNuErrNone && *ppFoundRecord != NULL); /* must succeed */ BailError(err); } @@ -2079,9 +2038,9 @@ bail: * passed in, then the record will be deleted from the "copy" set (which * will be created only if necessary). */ -static NuError -Nu_HandleAddDuplicateRecord(NuArchive* pArchive, NuRecordSet* pRecordSet, - NuRecord* pRecord, const NuFileDetails* pFileDetails) +static NuError Nu_HandleAddDuplicateRecord(NuArchive* pArchive, + NuRecordSet* pRecordSet, NuRecord* pRecord, + const NuFileDetails* pFileDetails) { NuError err = kNuErrNone; NuErrorStatus errorStatus; @@ -2089,8 +2048,8 @@ Nu_HandleAddDuplicateRecord(NuArchive* pArchive, NuRecordSet* pRecordSet, Assert(pRecordSet == &pArchive->origRecordSet || pRecordSet == &pArchive->copyRecordSet); - Assert(pRecord != nil); - Assert(pFileDetails != nil); + Assert(pRecord != NULL); + Assert(pFileDetails != NULL); Assert(pArchive->valAllowDuplicates == false); /* @@ -2117,13 +2076,15 @@ Nu_HandleAddDuplicateRecord(NuArchive* pArchive, NuRecordSet* pRecordSet, */ switch (pArchive->valHandleExisting) { case kNuMaybeOverwrite: - if (pArchive->errorHandlerFunc != nil) { + if (pArchive->errorHandlerFunc != NULL) { errorStatus.operation = kNuOpAdd; errorStatus.err = kNuErrRecordExists; errorStatus.sysErr = 0; - errorStatus.message = nil; + errorStatus.message = NULL; errorStatus.pRecord = pRecord; - errorStatus.pathname = pFileDetails->storageName; + UNICHAR* pathnameUNI = + Nu_CopyMORToUNI(pFileDetails->storageNameMOR); + errorStatus.pathnameUNI = pathnameUNI; errorStatus.origPathname = pFileDetails->origName; errorStatus.filenameSeparator = NuGetSepFromSysInfo(pFileDetails->fileSysInfo); @@ -2136,6 +2097,7 @@ Nu_HandleAddDuplicateRecord(NuArchive* pArchive, NuRecordSet* pRecordSet, errorStatus.canOverwrite = true; result = (*pArchive->errorHandlerFunc)(pArchive, &errorStatus); + Nu_Free(pArchive, pathnameUNI); switch (result) { case kNuAbort: @@ -2191,7 +2153,7 @@ Nu_HandleAddDuplicateRecord(NuArchive* pArchive, NuRecordSet* pRecordSet, err = Nu_RecordSet_FindByIdx(&pArchive->copyRecordSet, pRecord->recordIdx, &pRecord); - Assert(err == kNuErrNone && pRecord != nil); /* must succeed */ + Assert(err == kNuErrNone && pRecord != NULL); /* must succeed */ BailError(err); } @@ -2215,21 +2177,20 @@ bail: * * On success, the NuRecordIdx of the newly-created record will be placed * in "*pRecordIdx", and the NuThreadIdx of the filename thread will be - * placed in "*pThreadIdx". If "*ppNewRecord" is non-nil, it gets a pointer + * placed in "*pThreadIdx". If "*ppNewRecord" is non-NULL, it gets a pointer * to the newly-created record (this isn't part of the external interface). */ -NuError -Nu_AddRecord(NuArchive* pArchive, const NuFileDetails* pFileDetails, +NuError Nu_AddRecord(NuArchive* pArchive, const NuFileDetails* pFileDetails, NuRecordIdx* pRecordIdx, NuRecord** ppNewRecord) { NuError err; - NuRecord* pNewRecord = nil; + NuRecord* pNewRecord = NULL; - if (pFileDetails == nil || pFileDetails->storageName == nil || - pFileDetails->storageName[0] == '\0' || + if (pFileDetails == NULL || pFileDetails->storageNameMOR == NULL || + pFileDetails->storageNameMOR[0] == '\0' || NuGetSepFromSysInfo(pFileDetails->fileSysInfo) == 0) - /* pRecordIdx may be nil */ - /* ppNewRecord may be nil */ + /* pRecordIdx may be NULL */ + /* ppNewRecord may be NULL */ { err = kNuErrInvalidArg; goto bail; @@ -2241,7 +2202,7 @@ Nu_AddRecord(NuArchive* pArchive, const NuFileDetails* pFileDetails, BailError(err); /* NuFX spec forbids leading fssep chars */ - if (pFileDetails->storageName[0] == + if (pFileDetails->storageNameMOR[0] == NuGetSepFromSysInfo(pFileDetails->fileSysInfo)) { err = kNuErrLeadingFssep; @@ -2265,12 +2226,12 @@ Nu_AddRecord(NuArchive* pArchive, const NuFileDetails* pFileDetails, if (!Nu_RecordSet_GetLoaded(pRecordSet)) pRecordSet = &pArchive->origRecordSet; Assert(Nu_RecordSet_GetLoaded(pRecordSet)); - err = Nu_RecordSet_FindByName(pRecordSet, pFileDetails->storageName, + err = Nu_RecordSet_FindByName(pRecordSet, pFileDetails->storageNameMOR, &pFoundRecord); if (err == kNuErrNone) { /* handle the existing record */ DBUG(("--- Duplicate record found (%06ld) '%s'\n", - pFoundRecord->recordIdx, pFoundRecord->filename)); + pFoundRecord->recordIdx, pFoundRecord->filenameMOR)); err = Nu_HandleAddDuplicateRecord(pArchive, pRecordSet, pFoundRecord, pFileDetails); if (err != kNuErrNone) { @@ -2290,7 +2251,7 @@ Nu_AddRecord(NuArchive* pArchive, const NuFileDetails* pFileDetails, if (Nu_RecordSet_GetLoaded(&pArchive->newRecordSet)) { err = Nu_RecordSet_FindByName(&pArchive->newRecordSet, - pFileDetails->storageName, &pFoundRecord); + pFileDetails->storageNameMOR, &pFoundRecord); if (err == kNuErrNone) { /* we can't delete from the "new" list, so return an error */ err = kNuErrRecordExists; @@ -2327,9 +2288,9 @@ Nu_AddRecord(NuArchive* pArchive, const NuFileDetails* pFileDetails, pNewRecord->recFilenameLength = 0; pNewRecord->recordIdx = Nu_GetNextRecordIdx(pArchive); - pNewRecord->threadFilename = nil; - pNewRecord->newFilename = strdup(pFileDetails->storageName); - pNewRecord->filename = pNewRecord->newFilename; + pNewRecord->threadFilenameMOR = NULL; + pNewRecord->newFilenameMOR = strdup(pFileDetails->storageNameMOR); + pNewRecord->filenameMOR = pNewRecord->newFilenameMOR; pNewRecord->recHeaderLength = -1; pNewRecord->totalCompLength = 0; pNewRecord->fakeThreads = 0; @@ -2342,9 +2303,9 @@ Nu_AddRecord(NuArchive* pArchive, const NuFileDetails* pFileDetails, BailError(err); /* return values */ - if (pRecordIdx != nil) + if (pRecordIdx != NULL) *pRecordIdx = pNewRecord->recordIdx; - if (ppNewRecord != nil) + if (ppNewRecord != NULL) *ppNewRecord = pNewRecord; bail: @@ -2358,20 +2319,19 @@ bail: * The caller should have already verified that there isn't another * "add file" thread mod with the same ThreadID. */ -static NuError -Nu_AddFileThreadMod(NuArchive* pArchive, NuRecord* pRecord, - const char* pathname, const NuFileDetails* pFileDetails, +static NuError Nu_AddFileThreadMod(NuArchive* pArchive, NuRecord* pRecord, + const UNICHAR* pathnameUNI, const NuFileDetails* pFileDetails, Boolean fromRsrcFork) { NuError err; NuThreadFormat threadFormat; - NuDataSource* pDataSource = nil; - NuThreadMod* pThreadMod = nil; + NuDataSource* pDataSource = NULL; + NuThreadMod* pThreadMod = NULL; - Assert(pArchive != nil); - Assert(pRecord != nil); - Assert(pathname != nil); - Assert(pFileDetails != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); + Assert(pathnameUNI != NULL); + Assert(pFileDetails != NULL); Assert(fromRsrcFork == true || fromRsrcFork == false); if (Nu_IsReadOnly(pArchive)) @@ -2386,24 +2346,24 @@ Nu_AddFileThreadMod(NuArchive* pArchive, NuRecord* pRecord, /* create a data source for this file, which is assumed uncompressed */ err = Nu_DataSourceFile_New(kNuThreadFormatUncompressed, 0, - pathname, fromRsrcFork, &pDataSource); + pathnameUNI, fromRsrcFork, &pDataSource); BailError(err); /* create a new ThreadMod */ err = Nu_ThreadModAdd_New(pArchive, pFileDetails->threadID, threadFormat, pDataSource, &pThreadMod); BailError(err); - Assert(pThreadMod != nil); - /*pDataSource = nil;*/ /* ThreadModAdd_New makes a copy */ + Assert(pThreadMod != NULL); + /*pDataSource = NULL;*/ /* ThreadModAdd_New makes a copy */ /* add the thread mod to the record */ Nu_RecordAddThreadMod(pRecord, pThreadMod); - pThreadMod = nil; /* don't free on exit */ + pThreadMod = NULL; /* don't free on exit */ bail: - if (pDataSource != nil) + if (pDataSource != NULL) Nu_DataSourceFree(pDataSource); - if (pThreadMod != nil) + if (pThreadMod != NULL) Nu_ThreadModFree(pArchive, pThreadMod); return err; } @@ -2417,10 +2377,9 @@ bail: * "fromRsrcFork" tells us how to open the source file, not what type * of thread the file should be stored as. * - * If "pRecordIdx" is non-nil, it will receive the newly assigned recordID. + * If "pRecordIdx" is non-NULL, it will receive the newly assigned recordID. */ -NuError -Nu_AddFile(NuArchive* pArchive, const char* pathname, +NuError Nu_AddFile(NuArchive* pArchive, const UNICHAR* pathnameUNI, const NuFileDetails* pFileDetails, Boolean fromRsrcFork, NuRecordIdx* pRecordIdx) { @@ -2428,7 +2387,7 @@ Nu_AddFile(NuArchive* pArchive, const char* pathname, NuRecordIdx recordIdx = 0; NuRecord* pRecord; - if (pathname == nil || pFileDetails == nil || + if (pathnameUNI == NULL || pFileDetails == NULL || !(fromRsrcFork == true || fromRsrcFork == false)) { return kNuErrInvalidArg; @@ -2439,20 +2398,20 @@ Nu_AddFile(NuArchive* pArchive, const char* pathname, err = Nu_GetTOCIfNeeded(pArchive); BailError(err); - if (pFileDetails->storageName == nil) { + if (pFileDetails->storageNameMOR == NULL) { err = kNuErrInvalidArg; Nu_ReportError(NU_BLOB, err, "Must specify storageName"); goto bail; } - if (pFileDetails->storageName[0] == + if (pFileDetails->storageNameMOR[0] == NuGetSepFromSysInfo(pFileDetails->fileSysInfo)) { err = kNuErrLeadingFssep; goto bail; } - DBUG(("+++ ADDING '%s' (%s) 0x%02lx 0x%04lx threadID=0x%08lx\n", pathname, - pFileDetails->storageName, pFileDetails->fileType, + DBUG(("+++ ADDING '%s' (%s) 0x%02lx 0x%04lx threadID=0x%08lx\n", + pathnameUNI, pFileDetails->storageName, pFileDetails->fileType, pFileDetails->extraType, pFileDetails->threadID)); /* @@ -2471,7 +2430,7 @@ Nu_AddFile(NuArchive* pArchive, const char* pathname, NuRecord* pNewRecord; err = Nu_RecordSet_ReverseFindByName(&pArchive->newRecordSet, - pFileDetails->storageName, &pNewRecord); + pFileDetails->storageNameMOR, &pNewRecord); if (err == kNuErrNone) { /* is it okay to add it here? */ err = Nu_OkayToAddThread(pArchive, pNewRecord, @@ -2481,7 +2440,7 @@ Nu_AddFile(NuArchive* pArchive, const char* pathname, /* okay to add it to this record */ DBUG((" attaching to existing record %06ld\n", pNewRecord->recordIdx)); - err = Nu_AddFileThreadMod(pArchive, pNewRecord, pathname, + err = Nu_AddFileThreadMod(pArchive, pNewRecord, pathnameUNI, pFileDetails, fromRsrcFork); BailError(err); recordIdx = pNewRecord->recordIdx; @@ -2525,12 +2484,12 @@ Nu_AddFile(NuArchive* pArchive, const char* pathname, /* * Got the record, now add a data file thread. */ - err = Nu_AddFileThreadMod(pArchive, pRecord, pathname, pFileDetails, + err = Nu_AddFileThreadMod(pArchive, pRecord, pathnameUNI, pFileDetails, fromRsrcFork); BailError(err); bail: - if (err == kNuErrNone && pRecordIdx != nil) + if (err == kNuErrNone && pRecordIdx != NULL) *pRecordIdx = recordIdx; return err; @@ -2561,23 +2520,25 @@ bail: * We might also want to screen out trailing fssep chars, though the NuFX * spec doesn't say they're illegal. */ -NuError -Nu_Rename(NuArchive* pArchive, NuRecordIdx recIdx, const char* pathname, - char fssep) +NuError Nu_Rename(NuArchive* pArchive, NuRecordIdx recIdx, + const char* pathnameMOR, char fssepMOR) { NuError err; NuRecord* pRecord; NuThread* pFilenameThread; const NuThreadMod* pThreadMod; - NuThreadMod* pNewThreadMod = nil; - NuDataSource* pDataSource = nil; + NuThreadMod* pNewThreadMod = NULL; + NuDataSource* pDataSource = NULL; long requiredCapacity, existingCapacity, newCapacity; Boolean doDelete, doAdd, doUpdate; - if (recIdx == 0 || pathname == nil || pathname[0] == '\0' || fssep == '\0') + if (recIdx == 0 || pathnameMOR == NULL || pathnameMOR[0] == '\0' || + fssepMOR == '\0') + { return kNuErrInvalidArg; + } - if (pathname[0] == fssep) { + if (pathnameMOR[0] == fssepMOR) { err = kNuErrLeadingFssep; Nu_ReportError(NU_BLOB, err, "rename path"); goto bail; @@ -2591,19 +2552,19 @@ Nu_Rename(NuArchive* pArchive, NuRecordIdx recIdx, const char* pathname, /* find the record in the "copy" set */ err = Nu_FindRecordForWriteByIdx(pArchive, recIdx, &pRecord); BailError(err); - Assert(pRecord != nil); + Assert(pRecord != NULL); /* look for a filename thread */ err = Nu_FindThreadByID(pRecord, kNuThreadIDFilename, &pFilenameThread); if (err != kNuErrNone) - pFilenameThread = nil; + pFilenameThread = NULL; else if (err == kNuErrNone && pRecord->pThreadMods) { /* found a thread, check to see if it has been deleted (or modifed) */ - Assert(pFilenameThread != nil); + Assert(pFilenameThread != NULL); pThreadMod = Nu_ThreadMod_FindByThreadIdx(pRecord, pFilenameThread->threadIdx); - if (pThreadMod != nil) { + if (pThreadMod != NULL) { DBUG(("--- tried to modify threadIdx %ld, which has already been\n", pFilenameThread->threadIdx)); err = kNuErrModThreadChange; @@ -2616,9 +2577,9 @@ Nu_Rename(NuArchive* pArchive, NuRecordIdx recIdx, const char* pathname, */ doDelete = doAdd = doUpdate = false; newCapacity = existingCapacity = 0; - requiredCapacity = strlen(pathname); + requiredCapacity = strlen(pathnameMOR); - if (pFilenameThread != nil) { + if (pFilenameThread != NULL) { existingCapacity = pFilenameThread->thCompThreadEOF; if (existingCapacity >= requiredCapacity) { doUpdate = true; @@ -2644,7 +2605,7 @@ Nu_Rename(NuArchive* pArchive, NuRecordIdx recIdx, const char* pathname, if (doAdd || doUpdate) { Assert(newCapacity); err = Nu_DataSourceBuffer_New(kNuThreadFormatUncompressed, - newCapacity, (const uchar*)strdup(pathname), 0, + newCapacity, (const uint8_t*)strdup(pathnameMOR), 0, requiredCapacity /*(strlen)*/, Nu_InternalFreeCallback, &pDataSource); BailError(err); @@ -2655,44 +2616,44 @@ Nu_Rename(NuArchive* pArchive, NuRecordIdx recIdx, const char* pathname, kNuThreadIDFilename, &pNewThreadMod); BailError(err); Nu_RecordAddThreadMod(pRecord, pNewThreadMod); - pNewThreadMod = nil; /* successful, don't free */ + pNewThreadMod = NULL; /* successful, don't free */ } if (doAdd) { err = Nu_ThreadModAdd_New(pArchive, kNuThreadIDFilename, kNuThreadFormatUncompressed, pDataSource, &pNewThreadMod); BailError(err); - /*pDataSource = nil;*/ /* ThreadModAdd_New makes a copy */ + /*pDataSource = NULL;*/ /* ThreadModAdd_New makes a copy */ Nu_RecordAddThreadMod(pRecord, pNewThreadMod); - pNewThreadMod = nil; /* successful, don't free */ + pNewThreadMod = NULL; /* successful, don't free */ } if (doUpdate) { err = Nu_ThreadModUpdate_New(pArchive, pFilenameThread->threadIdx, pDataSource, &pNewThreadMod); BailError(err); - /*pDataSource = nil;*/ /* ThreadModAdd_New makes a copy */ + /*pDataSource = NULL;*/ /* ThreadModAdd_New makes a copy */ Nu_RecordAddThreadMod(pRecord, pNewThreadMod); - pNewThreadMod = nil; /* successful, don't free */ + pNewThreadMod = NULL; /* successful, don't free */ } DBUG(("--- renaming '%s' to '%s' with delete=%d add=%d update=%d\n", - pRecord->filename, pathname, doDelete, doAdd, doUpdate)); + pRecord->filenameMOR, pathnameMOR, doDelete, doAdd, doUpdate)); /* * Update the fssep, if necessary. (This is slightly silly -- we * have to rewrite the record header anyway since we're changing * threads around.) */ - if (NuGetSepFromSysInfo(pRecord->recFileSysInfo) != fssep) { + if (NuGetSepFromSysInfo(pRecord->recFileSysInfo) != fssepMOR) { DBUG(("--- and updating the fssep\n")); pRecord->recFileSysInfo = NuSetSepInSysInfo(pRecord->recFileSysInfo, - fssep); + fssepMOR); pRecord->dirtyHeader = true; } /* if we had a header filename, mark it for oblivion */ - if (pFilenameThread == nil) { + if (pFilenameThread == NULL) { DBUG(("+++ rename gonna drop the filename\n")); pRecord->dropRecFilename = true; } @@ -2707,14 +2668,13 @@ bail: /* * Update a record's attributes with the contents of pRecordAttr. */ -NuError -Nu_SetRecordAttr(NuArchive* pArchive, NuRecordIdx recordIdx, +NuError Nu_SetRecordAttr(NuArchive* pArchive, NuRecordIdx recordIdx, const NuRecordAttr* pRecordAttr) { NuError err; NuRecord* pRecord; - if (pRecordAttr == nil) + if (pRecordAttr == NULL) return kNuErrInvalidArg; if (Nu_IsReadOnly(pArchive)) @@ -2726,7 +2686,7 @@ Nu_SetRecordAttr(NuArchive* pArchive, NuRecordIdx recordIdx, err = Nu_FindRecordForWriteByIdx(pArchive, recordIdx, &pRecord); BailError(err); - Assert(pRecord != nil); + Assert(pRecord != NULL); pRecord->recFileSysID = pRecordAttr->fileSysID; /*pRecord->recFileSysInfo = pRecordAttr->fileSysInfo;*/ pRecord->recAccess = pRecordAttr->access; @@ -2745,8 +2705,7 @@ bail: /* * Bulk-delete several records, using the selection filter callback. */ -NuError -Nu_Delete(NuArchive* pArchive) +NuError Nu_Delete(NuArchive* pArchive) { NuError err; NuSelectionProposal selProposal; @@ -2775,7 +2734,7 @@ Nu_Delete(NuArchive* pArchive) * have already been deleted, we might as well use this set. */ pNextRecord = Nu_RecordSet_GetListHead(&pArchive->copyRecordSet); - while (pNextRecord != nil) { + while (pNextRecord != NULL) { pRecord = pNextRecord; pNextRecord = pRecord->pNext; @@ -2784,7 +2743,7 @@ Nu_Delete(NuArchive* pArchive) * isn't allowed. There's no point in showing the record to the * user. */ - if (pRecord->pThreadMods != nil) { + if (pRecord->pThreadMods != NULL) { DBUG(("+++ Skipping delete on a modified record\n")); continue; } @@ -2794,7 +2753,7 @@ Nu_Delete(NuArchive* pArchive) * to select which files will be deleted, or abort the entire * operation. */ - if (pArchive->selectionFilterFunc != nil) { + if (pArchive->selectionFilterFunc != NULL) { selProposal.pRecord = pRecord; selProposal.pThread = pRecord->pThreads; /* doesn't matter */ result = (*pArchive->selectionFilterFunc)(pArchive, &selProposal); @@ -2810,7 +2769,7 @@ Nu_Delete(NuArchive* pArchive) /* * Do we want to allow this? (Same test as for DeleteRecord.) */ - if (pRecord->pThreadMods != nil || pRecord->dirtyHeader) { + if (pRecord->pThreadMods != NULL || pRecord->dirtyHeader) { DBUG(("--- Tried to delete a modified record\n")); err = kNuErrModRecChange; goto bail; @@ -2828,8 +2787,7 @@ bail: /* * Delete an entire record. */ -NuError -Nu_DeleteRecord(NuArchive* pArchive, NuRecordIdx recIdx) +NuError Nu_DeleteRecord(NuArchive* pArchive, NuRecordIdx recIdx) { NuError err; NuRecord* pRecord; @@ -2852,7 +2810,7 @@ Nu_DeleteRecord(NuArchive* pArchive, NuRecordIdx recIdx) * record's filetype). This isn't necessary for correct operation, * but again it maintains the semantics. */ - if (pRecord->pThreadMods != nil || pRecord->dirtyHeader) { + if (pRecord->pThreadMods != NULL || pRecord->dirtyHeader) { DBUG(("--- Tried to delete a modified record\n")); err = kNuErrModRecChange; goto bail; diff --git a/nufxlib/SourceSink.c b/nufxlib/SourceSink.c index 1c13778..e5d6313 100644 --- a/nufxlib/SourceSink.c +++ b/nufxlib/SourceSink.c @@ -19,13 +19,12 @@ /* * Allocate a new DataSource structure. */ -static NuError -Nu_DataSourceNew(NuDataSource** ppDataSource) +static NuError Nu_DataSourceNew(NuDataSource** ppDataSource) { - Assert(ppDataSource != nil); + Assert(ppDataSource != NULL); - *ppDataSource = Nu_Malloc(nil, sizeof(**ppDataSource)); - if (*ppDataSource == nil) + *ppDataSource = Nu_Malloc(NULL, sizeof(**ppDataSource)); + if (*ppDataSource == NULL) return kNuErrMalloc; (*ppDataSource)->sourceType = kNuDataSourceUnknown; @@ -47,10 +46,9 @@ Nu_DataSourceNew(NuDataSource** ppDataSource) * needed in the first place.) Buffer sources are a little scary since * they include a "curOffset" value. * - * Returns nil on error. + * Returns NULL on error. */ -NuDataSource* -Nu_DataSourceCopy(NuDataSource* pDataSource) +NuDataSource* Nu_DataSourceCopy(NuDataSource* pDataSource) { Assert(pDataSource->common.refCount >= 1); pDataSource->common.refCount++; @@ -59,18 +57,18 @@ Nu_DataSourceCopy(NuDataSource* pDataSource) #if 0 /* we used to copy them -- very bad idea */ NuDataSource* pNewDataSource; - Assert(pDataSource != nil); + Assert(pDataSource != NULL); if (Nu_DataSourceNew(&pNewDataSource) != kNuErrNone) - return nil; - Assert(pNewDataSource != nil); + return NULL; + Assert(pNewDataSource != NULL); /* this gets most of it */ memcpy(pNewDataSource, pDataSource, sizeof(*pNewDataSource)); /* copy anything we're sure to free up */ if (pDataSource->sourceType == kNuDataSourceFromFile) { - Assert(pDataSource->fromFile.fp == nil); /* does this matter? */ + Assert(pDataSource->fromFile.fp == NULL); /* does this matter? */ pNewDataSource->fromFile.pathname = strdup(pDataSource->fromFile.pathname); } @@ -89,10 +87,9 @@ Nu_DataSourceCopy(NuDataSource* pDataSource) /* * Free a data source structure, and any type-specific elements. */ -NuError -Nu_DataSourceFree(NuDataSource* pDataSource) +NuError Nu_DataSourceFree(NuDataSource* pDataSource) { - if (pDataSource == nil) + if (pDataSource == NULL) return kNuErrNone; Assert(pDataSource->common.refCount > 0); @@ -103,25 +100,25 @@ Nu_DataSourceFree(NuDataSource* pDataSource) switch (pDataSource->sourceType) { case kNuDataSourceFromFile: - Nu_Free(nil, pDataSource->fromFile.pathname); - if (pDataSource->fromFile.fp != nil) { + Nu_Free(NULL, pDataSource->fromFile.pathnameUNI); + if (pDataSource->fromFile.fp != NULL) { fclose(pDataSource->fromFile.fp); - pDataSource->fromFile.fp = nil; + pDataSource->fromFile.fp = NULL; } break; case kNuDataSourceFromFP: - if (pDataSource->fromFP.fcloseFunc != nil && - pDataSource->fromFP.fp != nil) + if (pDataSource->fromFP.fcloseFunc != NULL && + pDataSource->fromFP.fp != NULL) { - (*pDataSource->fromFP.fcloseFunc)(nil, pDataSource->fromFP.fp); - pDataSource->fromFP.fp = nil; + (*pDataSource->fromFP.fcloseFunc)(NULL, pDataSource->fromFP.fp); + pDataSource->fromFP.fp = NULL; } break; case kNuDataSourceFromBuffer: - if (pDataSource->fromBuffer.freeFunc != nil) { - (*pDataSource->fromBuffer.freeFunc)(nil, + if (pDataSource->fromBuffer.freeFunc != NULL) { + (*pDataSource->fromBuffer.freeFunc)(NULL, (void*)pDataSource->fromBuffer.buffer); - pDataSource->fromBuffer.buffer = nil; + pDataSource->fromBuffer.buffer = NULL; } break; case kNuDataSourceUnknown: @@ -131,7 +128,7 @@ Nu_DataSourceFree(NuDataSource* pDataSource) return kNuErrInternal; } - Nu_Free(nil, pDataSource); + Nu_Free(NULL, pDataSource); return kNuErrNone; } @@ -139,15 +136,15 @@ Nu_DataSourceFree(NuDataSource* pDataSource) /* * Create a data source for an unopened file. */ -NuError -Nu_DataSourceFile_New(NuThreadFormat threadFormat, ulong otherLen, - const char* pathname, Boolean isFromRsrcFork, NuDataSource** ppDataSource) +NuError Nu_DataSourceFile_New(NuThreadFormat threadFormat, uint32_t otherLen, + const UNICHAR* pathnameUNI, Boolean isFromRsrcFork, + NuDataSource** ppDataSource) { NuError err; - if (pathname == nil || + if (pathnameUNI == NULL || !(isFromRsrcFork == true || isFromRsrcFork == false) || - ppDataSource == nil) + ppDataSource == NULL) { return kNuErrInvalidArg; } @@ -162,9 +159,9 @@ Nu_DataSourceFile_New(NuThreadFormat threadFormat, ulong otherLen, (*ppDataSource)->common.otherLen = otherLen; (*ppDataSource)->common.refCount = 1; - (*ppDataSource)->fromFile.pathname = strdup(pathname); + (*ppDataSource)->fromFile.pathnameUNI = strdup(pathnameUNI); (*ppDataSource)->fromFile.fromRsrcFork = isFromRsrcFork; - (*ppDataSource)->fromFile.fp = nil; /* to be filled in later */ + (*ppDataSource)->fromFile.fp = NULL; /* to be filled in later */ bail: return err; @@ -175,20 +172,19 @@ bail: * Create a data source for an open file at a specific offset. The FILE* * must be seekable. */ -NuError -Nu_DataSourceFP_New(NuThreadFormat threadFormat, ulong otherLen, +NuError Nu_DataSourceFP_New(NuThreadFormat threadFormat, uint32_t otherLen, FILE* fp, long offset, long length, NuCallback fcloseFunc, NuDataSource** ppDataSource) { NuError err; - if (fp == nil || offset < 0 || length < 0 || - ppDataSource == nil) + if (fp == NULL || offset < 0 || length < 0 || + ppDataSource == NULL) { return kNuErrInvalidArg; } - if (otherLen && otherLen < (ulong)length) { + if (otherLen && otherLen < (uint32_t)length) { DBUG(("--- rejecting FP len=%ld other=%ld\n", length, otherLen)); err = kNuErrPreSizeOverflow; goto bail; @@ -216,28 +212,27 @@ bail: /* * Create a data source for a buffer. * - * We allow "buffer" to be nil so long as "offset" and "length" are also - * nil. This is useful for creating empty pre-sized buffers, such as + * We allow "buffer" to be NULL so long as "offset" and "length" are also + * NULL. This is useful for creating empty pre-sized buffers, such as * blank comment fields. */ -NuError -Nu_DataSourceBuffer_New(NuThreadFormat threadFormat, ulong otherLen, - const uchar* buffer, long offset, long length, NuCallback freeFunc, +NuError Nu_DataSourceBuffer_New(NuThreadFormat threadFormat, uint32_t otherLen, + const uint8_t* buffer, long offset, long length, NuCallback freeFunc, NuDataSource** ppDataSource) { NuError err; - if (offset < 0 || length < 0 || ppDataSource == nil) + if (offset < 0 || length < 0 || ppDataSource == NULL) return kNuErrInvalidArg; - if (buffer == nil && (offset != 0 || length != 0)) + if (buffer == NULL && (offset != 0 || length != 0)) return kNuErrInvalidArg; - if (buffer == nil) { + if (buffer == NULL) { DBUG(("+++ zeroing freeFunc for empty-buffer DataSource\n")); - freeFunc = nil; + freeFunc = NULL; } - if (otherLen && otherLen < (ulong)length) { + if (otherLen && otherLen < (uint32_t)length) { DBUG(("--- rejecting buffer len=%ld other=%ld\n", length, otherLen)); err = kNuErrPreSizeOverflow; goto bail; @@ -267,34 +262,31 @@ bail: /* * Get the type of a NuDataSource. */ -NuDataSourceType -Nu_DataSourceGetType(const NuDataSource* pDataSource) +NuDataSourceType Nu_DataSourceGetType(const NuDataSource* pDataSource) { - Assert(pDataSource != nil); + Assert(pDataSource != NULL); return pDataSource->sourceType; } /* * Get the threadFormat for a data source. */ -NuThreadFormat -Nu_DataSourceGetThreadFormat(const NuDataSource* pDataSource) +NuThreadFormat Nu_DataSourceGetThreadFormat(const NuDataSource* pDataSource) { - Assert(pDataSource != nil); + Assert(pDataSource != NULL); return pDataSource->common.threadFormat; } /* * Get "dataLen" from a dataSource. */ -ulong -Nu_DataSourceGetDataLen(const NuDataSource* pDataSource) +uint32_t Nu_DataSourceGetDataLen(const NuDataSource* pDataSource) { - Assert(pDataSource != nil); + Assert(pDataSource != NULL); if (pDataSource->sourceType == kNuDataSourceFromFile) { /* dataLen can only be valid if file has been opened */ - Assert(pDataSource->fromFile.fp != nil); + Assert(pDataSource->fromFile.fp != NULL); } return pDataSource->common.dataLen; @@ -303,20 +295,18 @@ Nu_DataSourceGetDataLen(const NuDataSource* pDataSource) /* * Get "otherLen" from a dataSource. */ -ulong -Nu_DataSourceGetOtherLen(const NuDataSource* pDataSource) +uint32_t Nu_DataSourceGetOtherLen(const NuDataSource* pDataSource) { - Assert(pDataSource != nil); + Assert(pDataSource != NULL); return pDataSource->common.otherLen; } /* * Change the "otherLen" value. */ -void -Nu_DataSourceSetOtherLen(NuDataSource* pDataSource, long otherLen) +void Nu_DataSourceSetOtherLen(NuDataSource* pDataSource, long otherLen) { - Assert(pDataSource != nil && otherLen > 0); + Assert(pDataSource != NULL && otherLen > 0); pDataSource->common.otherLen = otherLen; } @@ -324,10 +314,9 @@ Nu_DataSourceSetOtherLen(NuDataSource* pDataSource, long otherLen) /* * Get the "raw CRC" value. */ -ushort -Nu_DataSourceGetRawCrc(const NuDataSource* pDataSource) +uint16_t Nu_DataSourceGetRawCrc(const NuDataSource* pDataSource) { - Assert(pDataSource != nil); + Assert(pDataSource != NULL); return pDataSource->common.rawCrc; } @@ -335,10 +324,9 @@ Nu_DataSourceGetRawCrc(const NuDataSource* pDataSource) * Set the "raw CRC" value. You would want to do this if the input was * already-compressed data, and you wanted to propagate the thread CRC. */ -void -Nu_DataSourceSetRawCrc(NuDataSource* pDataSource, ushort crc) +void Nu_DataSourceSetRawCrc(NuDataSource* pDataSource, uint16_t crc) { - Assert(pDataSource != nil); + Assert(pDataSource != NULL); pDataSource->common.rawCrc = crc; } @@ -346,11 +334,11 @@ Nu_DataSourceSetRawCrc(NuDataSource* pDataSource, ushort crc) /* * Prepare a data source for action. */ -NuError -Nu_DataSourcePrepareInput(NuArchive* pArchive, NuDataSource* pDataSource) +NuError Nu_DataSourcePrepareInput(NuArchive* pArchive, + NuDataSource* pDataSource) { NuError err = kNuErrNone; - FILE* fileFp = nil; + FILE* fileFp = NULL; /* * Doesn't apply to buffer sources. @@ -372,11 +360,11 @@ Nu_DataSourcePrepareInput(NuArchive* pArchive, NuDataSource* pDataSource) * We're adding from a file on disk. Open it. */ err = Nu_OpenInputFile(pArchive, - pDataSource->fromFile.pathname, pDataSource->fromFile.fromRsrcFork, - &fileFp); + pDataSource->fromFile.pathnameUNI, + pDataSource->fromFile.fromRsrcFork, &fileFp); BailError(err); - Assert(fileFp != nil); + Assert(fileFp != NULL); pDataSource->fromFile.fp = fileFp; err = Nu_GetFileLength(pArchive, fileFp, (long*)&pDataSource->common.dataLen); @@ -403,49 +391,47 @@ bail: * call will take care of this eventually -- but for normal operation on * a large number of files, it's vital. */ -void -Nu_DataSourceUnPrepareInput(NuArchive* pArchive, NuDataSource* pDataSource) +void Nu_DataSourceUnPrepareInput(NuArchive* pArchive, NuDataSource* pDataSource) { if (Nu_DataSourceGetType(pDataSource) != kNuDataSourceFromFile) return; - if (pDataSource->fromFile.fp != nil) { + if (pDataSource->fromFile.fp != NULL) { fclose(pDataSource->fromFile.fp); - pDataSource->fromFile.fp = nil; + pDataSource->fromFile.fp = NULL; pDataSource->common.dataLen = 0; } } /* - * Get the pathname from a "from-file" dataSource. + * Get the pathname from a "from-file" dataSource. Returned string is UTF-8. */ -const char* -Nu_DataSourceFile_GetPathname(NuDataSource* pDataSource) +const char* Nu_DataSourceFile_GetPathname(NuDataSource* pDataSource) { - Assert(pDataSource != nil); + Assert(pDataSource != NULL); Assert(pDataSource->sourceType == kNuDataSourceFromFile); - Assert(pDataSource->fromFile.pathname != nil); + Assert(pDataSource->fromFile.pathnameUNI != NULL); - return pDataSource->fromFile.pathname; + return pDataSource->fromFile.pathnameUNI; } /* * Read a block of data from a dataSource. */ -NuError -Nu_DataSourceGetBlock(NuDataSource* pDataSource, uchar* buf, ulong len) +NuError Nu_DataSourceGetBlock(NuDataSource* pDataSource, uint8_t* buf, + uint32_t len) { NuError err; - Assert(pDataSource != nil); - Assert(buf != nil); + Assert(pDataSource != NULL); + Assert(buf != NULL); Assert(len > 0); switch (pDataSource->sourceType) { case kNuDataSourceFromFile: - Assert(pDataSource->fromFile.fp != nil); + Assert(pDataSource->fromFile.fp != NULL); err = Nu_FRead(pDataSource->fromFile.fp, buf, len); if (feof(pDataSource->fromFile.fp)) Nu_ReportError(NU_NILBLOB, err, "EOF hit unexpectedly"); @@ -479,16 +465,15 @@ Nu_DataSourceGetBlock(NuDataSource* pDataSource, uchar* buf, ulong len) /* * Rewind a data source to the start of its input. */ -NuError -Nu_DataSourceRewind(NuDataSource* pDataSource) +NuError Nu_DataSourceRewind(NuDataSource* pDataSource) { NuError err; - Assert(pDataSource != nil); + Assert(pDataSource != NULL); switch (pDataSource->sourceType) { case kNuDataSourceFromFile: - Assert(pDataSource->fromFile.fp != nil); + Assert(pDataSource->fromFile.fp != NULL); err = Nu_FSeek(pDataSource->fromFile.fp, 0, SEEK_SET); break; /* fall through with error */ case kNuDataSourceFromFP: @@ -518,13 +503,12 @@ Nu_DataSourceRewind(NuDataSource* pDataSource) /* * Allocate a new DataSink structure. */ -static NuError -Nu_DataSinkNew(NuDataSink** ppDataSink) +static NuError Nu_DataSinkNew(NuDataSink** ppDataSink) { - Assert(ppDataSink != nil); + Assert(ppDataSink != NULL); - *ppDataSink = Nu_Malloc(nil, sizeof(**ppDataSink)); - if (*ppDataSink == nil) + *ppDataSink = Nu_Malloc(NULL, sizeof(**ppDataSink)); + if (*ppDataSink == NULL) return kNuErrMalloc; (*ppDataSink)->sinkType = kNuDataSinkUnknown; @@ -536,16 +520,15 @@ Nu_DataSinkNew(NuDataSink** ppDataSink) /* * Free a data sink structure, and any type-specific elements. */ -NuError -Nu_DataSinkFree(NuDataSink* pDataSink) +NuError Nu_DataSinkFree(NuDataSink* pDataSink) { - if (pDataSink == nil) + if (pDataSink == NULL) return kNuErrNone; switch (pDataSink->sinkType) { case kNuDataSinkToFile: Nu_DataSinkFile_Close(pDataSink); - Nu_Free(nil, pDataSink->toFile.pathname); + Nu_Free(NULL, pDataSink->toFile.pathnameUNI); break; case kNuDataSinkToFP: break; @@ -560,7 +543,7 @@ Nu_DataSinkFree(NuDataSink* pDataSink) return kNuErrInternal; } - Nu_Free(nil, pDataSink); + Nu_Free(NULL, pDataSink); return kNuErrNone; } @@ -568,18 +551,17 @@ Nu_DataSinkFree(NuDataSink* pDataSink) /* * Create a data sink for an unopened file. */ -NuError -Nu_DataSinkFile_New(Boolean doExpand, NuValue convertEOL, const char* pathname, - char fssep, NuDataSink** ppDataSink) +NuError Nu_DataSinkFile_New(Boolean doExpand, NuValue convertEOL, + const UNICHAR* pathnameUNI, UNICHAR fssep, NuDataSink** ppDataSink) { NuError err; if ((doExpand != true && doExpand != false) || (convertEOL != kNuConvertOff && convertEOL != kNuConvertOn && convertEOL != kNuConvertAuto) || - pathname == nil || + pathnameUNI == NULL || fssep == 0 || - ppDataSink == nil) + ppDataSink == NULL) { return kNuErrInvalidArg; } @@ -594,10 +576,10 @@ Nu_DataSinkFile_New(Boolean doExpand, NuValue convertEOL, const char* pathname, else (*ppDataSink)->common.convertEOL = kNuConvertOff; (*ppDataSink)->common.outCount = 0; - (*ppDataSink)->toFile.pathname = strdup(pathname); + (*ppDataSink)->toFile.pathnameUNI = strdup(pathnameUNI); (*ppDataSink)->toFile.fssep = fssep; - (*ppDataSink)->toFile.fp = nil; + (*ppDataSink)->toFile.fp = NULL; bail: return err; @@ -607,8 +589,7 @@ bail: /* * Create a data sink based on a file pointer. */ -NuError -Nu_DataSinkFP_New(Boolean doExpand, NuValue convertEOL, FILE* fp, +NuError Nu_DataSinkFP_New(Boolean doExpand, NuValue convertEOL, FILE* fp, NuDataSink** ppDataSink) { NuError err; @@ -616,8 +597,8 @@ Nu_DataSinkFP_New(Boolean doExpand, NuValue convertEOL, FILE* fp, if ((doExpand != true && doExpand != false) || (convertEOL != kNuConvertOff && convertEOL != kNuConvertOn && convertEOL != kNuConvertAuto) || - fp == nil || - ppDataSink == nil) + fp == NULL || + ppDataSink == NULL) { return kNuErrInvalidArg; } @@ -642,18 +623,17 @@ bail: /* * Create a data sink for a buffer in memory. */ -NuError -Nu_DataSinkBuffer_New(Boolean doExpand, NuValue convertEOL, uchar* buffer, - ulong bufLen, NuDataSink** ppDataSink) +NuError Nu_DataSinkBuffer_New(Boolean doExpand, NuValue convertEOL, + uint8_t* buffer, uint32_t bufLen, NuDataSink** ppDataSink) { NuError err; if ((doExpand != true && doExpand != false) || (convertEOL != kNuConvertOff && convertEOL != kNuConvertOn && convertEOL != kNuConvertAuto) || - buffer == nil || + buffer == NULL || bufLen == 0 || - ppDataSink == nil) + ppDataSink == NULL) { return kNuErrInvalidArg; } @@ -681,14 +661,13 @@ bail: /* * Create a data sink that goes nowhere. */ -NuError -Nu_DataSinkVoid_New(Boolean doExpand, NuValue convertEOL, +NuError Nu_DataSinkVoid_New(Boolean doExpand, NuValue convertEOL, NuDataSink** ppDataSink) { NuError err; Assert(doExpand == true || doExpand == false); - Assert(ppDataSink != nil); + Assert(ppDataSink != NULL); err = Nu_DataSinkNew(ppDataSink); BailErrorQuiet(err); @@ -706,10 +685,9 @@ bail: /* * Get the type of a NuDataSink. */ -NuDataSinkType -Nu_DataSinkGetType(const NuDataSink* pDataSink) +NuDataSinkType Nu_DataSinkGetType(const NuDataSink* pDataSink) { - Assert(pDataSink != nil); + Assert(pDataSink != NULL); return pDataSink->sinkType; } @@ -717,8 +695,7 @@ Nu_DataSinkGetType(const NuDataSink* pDataSink) /* * Return the "doExpand" parameter from any kind of sink. */ -Boolean -Nu_DataSinkGetDoExpand(const NuDataSink* pDataSink) +Boolean Nu_DataSinkGetDoExpand(const NuDataSink* pDataSink) { return pDataSink->common.doExpand; } @@ -726,8 +703,7 @@ Nu_DataSinkGetDoExpand(const NuDataSink* pDataSink) /* * Return the "convertEOL" parameter from any kind of sink. */ -NuValue -Nu_DataSinkGetConvertEOL(const NuDataSink* pDataSink) +NuValue Nu_DataSinkGetConvertEOL(const NuDataSink* pDataSink) { return pDataSink->common.convertEOL; } @@ -735,32 +711,29 @@ Nu_DataSinkGetConvertEOL(const NuDataSink* pDataSink) /* * Return the #of bytes written to the sink. */ -ulong -Nu_DataSinkGetOutCount(const NuDataSink* pDataSink) +uint32_t Nu_DataSinkGetOutCount(const NuDataSink* pDataSink) { return pDataSink->common.outCount; } /* - * Get "pathname" from a to-file sink. + * Get "pathname" from a to-file sink. Returned string is UTF-8. */ -const char* -Nu_DataSinkFile_GetPathname(const NuDataSink* pDataSink) +const char* Nu_DataSinkFile_GetPathname(const NuDataSink* pDataSink) { - Assert(pDataSink != nil); + Assert(pDataSink != NULL); Assert(pDataSink->sinkType == kNuDataSinkToFile); - return pDataSink->toFile.pathname; + return pDataSink->toFile.pathnameUNI; } /* * Get "fssep" from a to-file sink. */ -char -Nu_DataSinkFile_GetFssep(const NuDataSink* pDataSink) +UNICHAR Nu_DataSinkFile_GetFssep(const NuDataSink* pDataSink) { - Assert(pDataSink != nil); + Assert(pDataSink != NULL); Assert(pDataSink->sinkType == kNuDataSinkToFile); return pDataSink->toFile.fssep; @@ -769,10 +742,9 @@ Nu_DataSinkFile_GetFssep(const NuDataSink* pDataSink) /* * Get the "fp" for a file sink. */ -FILE* -Nu_DataSinkFile_GetFP(const NuDataSink* pDataSink) +FILE* Nu_DataSinkFile_GetFP(const NuDataSink* pDataSink) { - Assert(pDataSink != nil); + Assert(pDataSink != NULL); Assert(pDataSink->sinkType == kNuDataSinkToFile); return pDataSink->toFile.fp; @@ -781,10 +753,9 @@ Nu_DataSinkFile_GetFP(const NuDataSink* pDataSink) /* * Set the "fp" for a file sink. */ -void -Nu_DataSinkFile_SetFP(NuDataSink* pDataSink, FILE* fp) +void Nu_DataSinkFile_SetFP(NuDataSink* pDataSink, FILE* fp) { - Assert(pDataSink != nil); + Assert(pDataSink != NULL); Assert(pDataSink->sinkType == kNuDataSinkToFile); pDataSink->toFile.fp = fp; @@ -793,14 +764,13 @@ Nu_DataSinkFile_SetFP(NuDataSink* pDataSink, FILE* fp) /* * Close a to-file sink. */ -void -Nu_DataSinkFile_Close(NuDataSink* pDataSink) +void Nu_DataSinkFile_Close(NuDataSink* pDataSink) { - Assert(pDataSink != nil); + Assert(pDataSink != NULL); - if (pDataSink->toFile.fp != nil) { + if (pDataSink->toFile.fp != NULL) { fclose(pDataSink->toFile.fp); - pDataSink->toFile.fp = nil; + pDataSink->toFile.fp = NULL; } } @@ -808,24 +778,24 @@ Nu_DataSinkFile_Close(NuDataSink* pDataSink) /* * Write a block of data to a DataSink. */ -NuError -Nu_DataSinkPutBlock(NuDataSink* pDataSink, const uchar* buf, ulong len) +NuError Nu_DataSinkPutBlock(NuDataSink* pDataSink, const uint8_t* buf, + uint32_t len) { NuError err; - Assert(pDataSink != nil); - Assert(buf != nil); + Assert(pDataSink != NULL); + Assert(buf != NULL); Assert(len > 0); switch (pDataSink->sinkType) { case kNuDataSinkToFile: - Assert(pDataSink->toFile.fp != nil); + Assert(pDataSink->toFile.fp != NULL); err = Nu_FWrite(pDataSink->toFile.fp, buf, len); if (err != kNuErrNone) return err; break; case kNuDataSinkToFP: - Assert(pDataSink->toFP.fp != nil); + Assert(pDataSink->toFP.fp != NULL); err = Nu_FWrite(pDataSink->toFP.fp, buf, len); if (err != kNuErrNone) return err; @@ -856,12 +826,11 @@ Nu_DataSinkPutBlock(NuDataSink* pDataSink, const uchar* buf, ulong len) /* * Figure out if one of our earlier writes has failed. */ -NuError -Nu_DataSinkGetError(NuDataSink* pDataSink) +NuError Nu_DataSinkGetError(NuDataSink* pDataSink) { NuError err = kNuErrNone; - Assert(pDataSink != nil); + Assert(pDataSink != NULL); switch (pDataSink->sinkType) { case kNuDataSinkToFile: diff --git a/nufxlib/Squeeze.c b/nufxlib/Squeeze.c index f28b3c4..a4c339f 100644 --- a/nufxlib/Squeeze.c +++ b/nufxlib/Squeeze.c @@ -77,13 +77,13 @@ typedef struct EncTreeNode { typedef struct SQState { NuArchive* pArchive; int doCalcCRC; /* boolean; if set, compute CRC on input */ - ushort crc; + uint16_t crc; NuStraw* pStraw; long uncompRemaining; #ifdef FULL_SQ_HEADER - ushort checksum; + uint16_t checksum; #endif /* @@ -98,12 +98,12 @@ typedef struct SQState { */ EncTreeNode node[kNuSQNumNodes]; - int treeHead; /* index to head node of final tree */ + int treeHead; /* index to head node of final tree */ /* encoding table */ int codeLen[kNuSQNumVals]; /* number of bits in code for symbol N */ - ushort code[kNuSQNumVals]; /* bits for symbol N (first bit in lsb) */ - ushort tmpCode; /* temporary code value */ + uint16_t code[kNuSQNumVals]; /* bits for symbol N (first bit in lsb) */ + uint16_t tmpCode; /* temporary code value */ } SQState; @@ -117,11 +117,10 @@ typedef struct SQState { * * Returns kNuSQEOFToken as the value when we're out of data. */ -static NuError -Nu_SQGetcCRC(SQState* pSqState, int* pSym) +static NuError Nu_SQGetcCRC(SQState* pSqState, int* pSym) { NuError err; - uchar c; + uint8_t c; if (!pSqState->uncompRemaining) { *pSym = kNuSQEOFToken; @@ -148,8 +147,7 @@ Nu_SQGetcCRC(SQState* pSqState, int* pSym) * * Returns kNuSQEOFToken in "*pSum" when we reach the end of the input. */ -static NuError -Nu_SQGetcRLE(SQState* pSqState, int* pSym) +static NuError Nu_SQGetcRLE(SQState* pSqState, int* pSym) { NuError err = kNuErrNone; int likeCount, newSym; @@ -275,8 +273,7 @@ bail: /* * Return the greater of two integers. */ -static int -Nu_SQMax(int a, int b) +static int Nu_SQMax(int a, int b) { if (a > b) return a; @@ -289,8 +286,7 @@ Nu_SQMax(int a, int b) * Priority is given to weight, then depth. "a" and "b" are heaps, * so we only need to look at the root element. */ -static int -Nu_SQCmpTrees(SQState* pSqState, int a, int b) +static int Nu_SQCmpTrees(SQState* pSqState, int a, int b) { if (pSqState->node[a].weight > pSqState->node[b].weight) return true; @@ -312,8 +308,7 @@ Nu_SQCmpTrees(SQState* pSqState, int a, int b) /* * Recursively make a heap from a heap with a new top. */ -static void -Nu_SQHeapAdjust(SQState* pSqState, int list[], int top, int bottom) +static void Nu_SQHeapAdjust(SQState* pSqState, int list[], int top, int bottom) { int k, temp; @@ -337,8 +332,7 @@ Nu_SQHeapAdjust(SQState* pSqState, int list[], int top, int bottom) /* * Create a heap. */ -static void -Nu_SQHeap(SQState* pSqState, int list[], int length) +static void Nu_SQHeap(SQState* pSqState, int list[], int length) { int i; @@ -365,8 +359,7 @@ Nu_SQHeap(SQState* pSqState, int list[], int length) * moving the last element over the top element and * reheaping the shorter list. */ -static void -Nu_SQBuildTree(SQState* pSqState, int list[], int len) +static void Nu_SQBuildTree(SQState* pSqState, int list[], int len) { int freenode; /* next free node in tree */ EncTreeNode* frnp; /* free node pointer */ @@ -422,8 +415,7 @@ Nu_SQBuildTree(SQState* pSqState, int list[], int len) * * Returns zero on success, nonzero if codes are too long. */ -static int -Nu_SQBuildEncTable(SQState* pSqState, int level, int root) +static int Nu_SQBuildEncTable(SQState* pSqState, int level, int root) { int l, r; @@ -437,7 +429,7 @@ Nu_SQBuildEncTable(SQState* pSqState, int level, int root) */ pSqState->codeLen[root] = level; pSqState->code[root] = - pSqState->tmpCode & (((ushort)~0) >> (16 - level)); + pSqState->tmpCode & (((uint16_t)~0) >> (16 - level)); return (level > 16) ? -1 : 0; } else { if (l != kNuSQNoChild) { @@ -470,12 +462,11 @@ Nu_SQBuildEncTable(SQState* pSqState, int level, int root) * the codes will fit in an unsigned integer. Rescaling is * used if necessary to limit the code length. */ -static void -Nu_SQScale(SQState* pSqState, int ceiling) +static void Nu_SQScale(SQState* pSqState, int ceiling) { int i; int wt, ovflw, divisor; - ushort sum; + uint16_t sum; int increased; /* flag */ do { @@ -510,8 +501,7 @@ Nu_SQScale(SQState* pSqState, int ceiling) * Build a frequency table from the post-RLE input stream, then generate * an encoding tree from the results. */ -static NuError -Nu_SQComputeHuffTree(SQState* pSqState) +static NuError Nu_SQComputeHuffTree(SQState* pSqState) { NuError err = kNuErrNone; int btreeList[kNuSQNumVals]; /* list of intermediate binary trees */ @@ -610,12 +600,12 @@ bail: * Compress data from input to output, using the values in the "code" * and "codeLen" arrays. */ -static NuError -Nu_SQCompressInput(SQState* pSqState, FILE* fp, long* pCompressedLen) +static NuError Nu_SQCompressInput(SQState* pSqState, FILE* fp, + long* pCompressedLen) { NuError err = kNuErrNone; int sym = kNuSQEOFToken-1; - unsigned long bits, code; /* must hold at least 23 bits */ + uint32_t bits, code; /* must hold at least 23 bits */ int codeLen, gotbits; long compressedLen; @@ -661,11 +651,10 @@ bail: /* * Write a 16-bit value in little-endian order. */ -static NuError -Nu_SQWriteShort(FILE* outfp, short val) +static NuError Nu_SQWriteShort(FILE* outfp, short val) { NuError err; - uchar tmpc; + uint8_t tmpc; tmpc = val & 0xff; err = Nu_FWrite(outfp, &tmpc, 1); @@ -689,9 +678,8 @@ bail: * it an empty file. "xsq" works fine, creating an empty tree that * "xusq" unpacks. */ -NuError -Nu_CompressHuffmanSQ(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, - ulong srcLen, ulong* pDstLen, ushort* pCrc) +NuError Nu_CompressHuffmanSQ(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, + uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc) { NuError err = kNuErrNone; SQState sqState; @@ -704,7 +692,7 @@ Nu_CompressHuffmanSQ(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, sqState.pArchive = pArchive; sqState.crc = 0; - if (pCrc == nil) { + if (pCrc == NULL) { sqState.doCalcCRC = false; } else { sqState.doCalcCRC = true; @@ -727,7 +715,7 @@ Nu_CompressHuffmanSQ(NuArchive* pArchive, NuStraw* pStraw, FILE* fp, err = Nu_SQComputeHuffTree(&sqState); BailError(err); - if (pCrc != nil) + if (pCrc != NULL) *pCrc = sqState.crc; /* @@ -822,8 +810,8 @@ bail: * State during uncompression. */ typedef struct USQState { - ulong dataInBuffer; - uchar* dataPtr; + uint32_t dataInBuffer; + uint8_t* dataPtr; int bitPosn; int bits; @@ -842,8 +830,7 @@ typedef struct USQState { /* * Decode the next symbol from the Huffman stream. */ -static NuError -Nu_USQDecodeHuffSymbol(USQState* pUsqState, int* pVal) +static NuError Nu_USQDecodeHuffSymbol(USQState* pUsqState, int* pVal) { short val = 0; int bits, bitPosn; @@ -879,8 +866,7 @@ Nu_USQDecodeHuffSymbol(USQState* pUsqState, int* pVal) /* * Read two bytes of signed data out of the buffer. */ -static inline NuError -Nu_USQReadShort(USQState* pUsqState, short* pShort) +static inline NuError Nu_USQReadShort(USQState* pUsqState, short* pShort) { if (pUsqState->dataInBuffer < 2) return kNuErrBufferUnderrun; @@ -898,24 +884,23 @@ Nu_USQReadShort(USQState* pUsqState, short* pShort) * Because we have a stop symbol, knowing the uncompressed length of * the file is not essential. */ -NuError -Nu_ExpandHuffmanSQ(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, ushort* pCrc) +NuError Nu_ExpandHuffmanSQ(NuArchive* pArchive, const NuRecord* pRecord, + const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, uint16_t* pCrc) { NuError err = kNuErrNone; USQState usqState; - ulong compRemaining, getSize; + uint32_t compRemaining, getSize; #ifdef FULL_SQ_HEADER - ushort magic, fileChecksum, checksum; + uint16_t magic, fileChecksum, checksum; #endif short nodeCount; int i, inrep; - uchar lastc = 0; + uint8_t lastc = 0; err = Nu_AllocCompressionBufferIFN(pArchive); if (err != kNuErrNone) return err; - Assert(pArchive->compBuf != nil); + Assert(pArchive->compBuf != NULL); usqState.dataInBuffer = 0; usqState.dataPtr = pArchive->compBuf; @@ -945,7 +930,7 @@ Nu_ExpandHuffmanSQ(NuArchive* pArchive, const NuRecord* pRecord, err = Nu_FRead(infp, usqState.dataPtr, getSize); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, - "failed reading compressed data (%ld bytes)", getSize); + "failed reading compressed data (%u bytes)", getSize); goto bail; } usqState.dataInBuffer += getSize; @@ -1046,7 +1031,7 @@ Nu_ExpandHuffmanSQ(NuArchive* pArchive, const NuRecord* pRecord, getSize); if (err != kNuErrNone) { Nu_ReportError(NU_BLOB, err, - "failed reading compressed data (%ld bytes)", getSize); + "failed reading compressed data (%u bytes)", getSize); goto bail; } usqState.dataInBuffer += getSize; @@ -1080,7 +1065,7 @@ Nu_ExpandHuffmanSQ(NuArchive* pArchive, const NuRecord* pRecord, val = 2; } while (--val) { - if (pCrc != nil) + if (pCrc != NULL) *pCrc = Nu_CalcCRC16(*pCrc, &lastc, 1); err = Nu_FunnelWrite(pArchive, pFunnel, &lastc, 1); #ifdef FULL_SQ_HEADER @@ -1095,7 +1080,7 @@ Nu_ExpandHuffmanSQ(NuArchive* pArchive, const NuRecord* pRecord, inrep = true; } else { lastc = val; - if (pCrc != nil) + if (pCrc != NULL) *pCrc = Nu_CalcCRC16(*pCrc, &lastc, 1); err = Nu_FunnelWrite(pArchive, pFunnel, &lastc, 1); #ifdef FULL_SQ_HEADER @@ -1135,7 +1120,7 @@ Nu_ExpandHuffmanSQ(NuArchive* pArchive, const NuRecord* pRecord, if (usqState.dataInBuffer > 1) { DBUG(("--- Found %ld bytes following compressed data (compRem=%ld)\n", usqState.dataInBuffer, compRemaining)); - Nu_ReportError(NU_BLOB, kNuErrNone, "(Warning) unexpected fluff (%ld)", + Nu_ReportError(NU_BLOB, kNuErrNone, "(Warning) unexpected fluff (%u)", usqState.dataInBuffer); } diff --git a/nufxlib/SunOS4.h b/nufxlib/SunOS4.h deleted file mode 100644 index 8db6562..0000000 --- a/nufxlib/SunOS4.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2000-2007 by Andy McFadden, All Rights Reserved. - * This is free software; you can redistribute it and/or modify it under the - * terms of the BSD License, see the file COPYING-LIB. - * - * This file was adapted from Devin Reade's "sunos4.h" in NuLib 3.2.5. - * It is provided for compilation under SunOS 4.x, when an ANSI compiler - * (such as gcc) is used. The system header files aren't quite sufficient - * to eliminate hordes of warnings. - */ -#ifndef __SunOS4__ -#define __SunOS4__ - -#ifdef __GNUC__ -extern int _flsbuf(int, FILE*); -extern int _filbuf(FILE*); -#endif - -extern void bcopy(char*, char*, int); -extern int fclose(FILE*); -extern int fflush(FILE*); -extern int fprintf(FILE*, const char*, ...); -extern int fread(char*, int, int, FILE *); -extern int fseek(FILE*, long, int); -extern int ftruncate(int, off_t); -extern int fwrite(const char*, int, int, FILE*); -extern char* mktemp(char *template); -extern time_t mktime(struct tm*); -extern int perror(const char*); -extern int printf(const char*, ...); -extern int remove(const char*); -extern int rename(const char*, const char*); -extern int tolower(int); -extern int setvbuf(FILE*, char*, int, int); -extern int sscanf(char*, const char*, ...); -extern int strcasecmp(const char*, const char*); -extern int strncasecmp(const char*, const char*, size_t); -extern long strtol(const char *, char **, int); -extern int system(const char*); -extern time_t timelocal(struct tm*); -extern time_t time(time_t*); -extern int toupper(int); -extern int vfprintf(FILE*, const char *, va_list); -extern char* vsprintf(char *str, const char *format, va_list ap); - -#endif /*__SunOS4__*/ diff --git a/nufxlib/SysDefs.h b/nufxlib/SysDefs.h index ae87530..b6ff2b1 100644 --- a/nufxlib/SysDefs.h +++ b/nufxlib/SysDefs.h @@ -6,8 +6,8 @@ * * External type definitions and function prototypes. */ -#ifndef __SysDefs__ -#define __SysDefs__ +#ifndef NUFXLIB_SYSDEFS_H +#define NUFXLIB_SYSDEFS_H #ifdef HAVE_CONFIG_H # include @@ -18,7 +18,6 @@ #endif /* these should exist everywhere */ -#include #include #include #include @@ -55,10 +54,6 @@ # define SNPRINTF_DECLARED # define VSNPRINTF_DECLARED # define SPRINTF_RETURNS_INT -# define uchar unsigned char -# define ushort unsigned short -# define uint unsigned int -# define ulong unsigned long # define inline /*Visual C++6.0 can't inline ".c" files*/ # define mode_t int # define ENABLE_SQ @@ -74,6 +69,7 @@ # define HAVE_CHSIZE # define snprintf _snprintf # define vsnprintf _vsnprintf + #endif #ifdef HAVE_MALLOC_H @@ -123,26 +119,18 @@ # endif #endif -/* resource forks on UFS filesystem under Mac OS X are a kluge */ -/*#ifdef MAC*/ -/*# define HAS_RESOURCE_FORKS*/ -/*#endif*/ - -#if defined(__ORCAC__) || defined(MAC_LIKE) -# define HAS_RESOURCE_FORKS -#endif +/* not currently using filesystem resource forks */ +//#if defined(__ORCAC__) || defined(MAC_LIKE) +//# define HAS_RESOURCE_FORKS +//#endif /* __FUNCTION__ was missing from BeOS __MWERKS__, and might be gcc-only */ #ifdef __GNUC__ # define HAS__FUNCTION__ #endif -#if defined(__sun__) && !defined(__SVR4) -# include "SunOS4.h" -#endif - #if defined(__linux__) # define HAS_MALLOC_CHECK_ #endif -#endif /*__SysDefs__*/ +#endif /*NUFXLIB_SYSDEFS_H*/ diff --git a/nufxlib/Thread.c b/nufxlib/Thread.c index 99480f2..d8e2683 100644 --- a/nufxlib/Thread.c +++ b/nufxlib/Thread.c @@ -6,7 +6,6 @@ * * Thread-level operations. */ -#define __Thread_c__ 1 #include "NufxLibPriv.h" @@ -16,6 +15,17 @@ * =========================================================================== */ +/* + * Returns thread N, or NULL if the index is invalid. + */ +NuThread* Nu_GetThread(const NuRecord* pRecord, int idx) +{ + if (idx >= (int)pRecord->recTotalThreads) + return NULL; + else + return &pRecord->pThreads[idx]; +} + /* * ShrinkIt v3.0.0 had a bug where the filename thread would get created * with the high bits set. We want to undo that without stomping on @@ -26,16 +36,15 @@ * This high-bit-ism was also done for disk archives by most older versions * of ShrinkIt. */ -void -Nu_StripHiIfAllSet(char* str) +void Nu_StripHiIfAllSet(char* str) { - uchar* cp; + uint8_t* cp; - for (cp = (uchar*)str; *cp != '\0'; cp++) + for (cp = (uint8_t*)str; *cp != '\0'; cp++) if (!(*cp & 0x80)) return; - for (cp = (uchar*)str; *cp != '\0'; cp++) + for (cp = (uint8_t*)str; *cp != '\0'; cp++) *cp &= 0x7f; } @@ -44,8 +53,7 @@ Nu_StripHiIfAllSet(char* str) * Decide if a thread is pre-sized (i.e. has a fixed maximum size with a * lesser amount of uncompressed data within) based on the threadID. */ -Boolean -Nu_IsPresizedThreadID(NuThreadID threadID) +Boolean Nu_IsPresizedThreadID(NuThreadID threadID) { if (threadID == kNuThreadIDFilename || threadID == kNuThreadIDComment) return true; @@ -58,8 +66,7 @@ Nu_IsPresizedThreadID(NuThreadID threadID) * Return an indication of whether the type of thread specified by ThreadID * should ever be compressed. Right now, that's only data-class threads. */ -Boolean -Nu_IsCompressibleThreadID(NuThreadID threadID) +Boolean Nu_IsCompressibleThreadID(NuThreadID threadID) { if (NuThreadIDGetClass(threadID) == kNuThreadClassData) return true; @@ -72,8 +79,7 @@ Nu_IsCompressibleThreadID(NuThreadID threadID) * Decide if the thread has a CRC, based on the record version and the * threadID. */ -Boolean -Nu_ThreadHasCRC(long recordVersion, NuThreadID threadID) +Boolean Nu_ThreadHasCRC(uint16_t recordVersion, NuThreadID threadID) { return recordVersion >= 3 && NuThreadIDGetClass(threadID) == kNuThreadClassData; @@ -83,8 +89,7 @@ Nu_ThreadHasCRC(long recordVersion, NuThreadID threadID) /* * Search through a given NuRecord for the specified thread. */ -NuError -Nu_FindThreadByIdx(const NuRecord* pRecord, NuThreadIdx thread, +NuError Nu_FindThreadByIdx(const NuRecord* pRecord, NuThreadIdx thread, NuThread** ppThread) { NuThread* pThread; @@ -92,7 +97,7 @@ Nu_FindThreadByIdx(const NuRecord* pRecord, NuThreadIdx thread, for (idx = 0; idx < (int)pRecord->recTotalThreads; idx++) { pThread = Nu_GetThread(pRecord, idx); - Assert(pThread != nil); + Assert(pThread != NULL); if (pThread->threadIdx == thread) { *ppThread = pThread; @@ -108,8 +113,7 @@ Nu_FindThreadByIdx(const NuRecord* pRecord, NuThreadIdx thread, * Search through a given NuRecord for the first thread with a matching * threadID. */ -NuError -Nu_FindThreadByID(const NuRecord* pRecord, NuThreadID threadID, +NuError Nu_FindThreadByID(const NuRecord* pRecord, NuThreadID threadID, NuThread** ppThread) { NuThread* pThread; @@ -117,7 +121,7 @@ Nu_FindThreadByID(const NuRecord* pRecord, NuThreadID threadID, for (idx = 0; idx < (int)pRecord->recTotalThreads; idx++) { pThread = Nu_GetThread(pRecord, idx); - Assert(pThread != nil); + Assert(pThread != NULL); if (NuGetThreadID(pThread) == threadID) { *ppThread = pThread; @@ -132,11 +136,10 @@ Nu_FindThreadByID(const NuRecord* pRecord, NuThreadID threadID, /* * Copy the contents of a NuThread. */ -void -Nu_CopyThreadContents(NuThread* pDstThread, const NuThread* pSrcThread) +void Nu_CopyThreadContents(NuThread* pDstThread, const NuThread* pSrcThread) { - Assert(pDstThread != nil); - Assert(pSrcThread != nil); + Assert(pDstThread != NULL); + Assert(pSrcThread != NULL); memcpy(pDstThread, pSrcThread, sizeof(*pDstThread)); } @@ -151,14 +154,14 @@ Nu_CopyThreadContents(NuThread* pDstThread, const NuThread* pSrcThread) /* * Read a single thread header from the archive. */ -static NuError -Nu_ReadThreadHeader(NuArchive* pArchive, NuThread* pThread, ushort* pCrc) +static NuError Nu_ReadThreadHeader(NuArchive* pArchive, NuThread* pThread, + uint16_t* pCrc) { FILE* fp; - Assert(pArchive != nil); - Assert(pThread != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(pThread != NULL); + Assert(pCrc != NULL); fp = pArchive->archiveFp; @@ -184,17 +187,17 @@ Nu_ReadThreadHeader(NuArchive* pArchive, NuThread* pThread, ushort* pCrc) * have used a linked list like NuLib, but that doesn't really provide any * benefit for us, and adds complexity. */ -NuError -Nu_ReadThreadHeaders(NuArchive* pArchive, NuRecord* pRecord, ushort* pCrc) +NuError Nu_ReadThreadHeaders(NuArchive* pArchive, NuRecord* pRecord, + uint16_t* pCrc) { NuError err = kNuErrNone; NuThread* pThread; long count; Boolean hasData = false; - Assert(pArchive != nil); - Assert(pRecord != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); + Assert(pCrc != NULL); if (!pRecord->recTotalThreads) { /* not sure if this is reasonable, but we can handle it */ @@ -306,17 +309,16 @@ bail: /* * Write a single thread header to the archive. */ -static NuError -Nu_WriteThreadHeader(NuArchive* pArchive, const NuThread* pThread, FILE* fp, - ushort* pCrc) +static NuError Nu_WriteThreadHeader(NuArchive* pArchive, + const NuThread* pThread, FILE* fp, uint16_t* pCrc) { - Assert(pArchive != nil); - Assert(pThread != nil); - Assert(fp != nil); - Assert(pCrc != nil); + Assert(pArchive != NULL); + Assert(pThread != NULL); + Assert(fp != NULL); + Assert(pCrc != NULL); Nu_WriteTwoC(pArchive, fp, pThread->thThreadClass, pCrc); - Nu_WriteTwoC(pArchive, fp, (ushort)pThread->thThreadFormat, pCrc); + Nu_WriteTwoC(pArchive, fp, (uint16_t)pThread->thThreadFormat, pCrc); Nu_WriteTwoC(pArchive, fp, pThread->thThreadKind, pCrc); Nu_WriteTwoC(pArchive, fp, pThread->thThreadCRC, pCrc); Nu_WriteFourC(pArchive, fp, pThread->thThreadEOF, pCrc); @@ -332,9 +334,8 @@ Nu_WriteThreadHeader(NuArchive* pArchive, const NuThread* pThread, FILE* fp, * effect, we promote all threads to "real" status. We update the * "fake" count in pRecord accordingly. */ -NuError -Nu_WriteThreadHeaders(NuArchive* pArchive, NuRecord* pRecord, FILE* fp, - ushort* pCrc) +NuError Nu_WriteThreadHeaders(NuArchive* pArchive, NuRecord* pRecord, FILE* fp, + uint16_t* pCrc) { NuError err = kNuErrNone; NuThread* pThread; @@ -342,7 +343,7 @@ Nu_WriteThreadHeaders(NuArchive* pArchive, NuRecord* pRecord, FILE* fp, for (idx = 0; idx < (int)pRecord->recTotalThreads; idx++) { pThread = Nu_GetThread(pRecord, idx); - Assert(pThread != nil); + Assert(pThread != NULL); err = Nu_WriteThreadHeader(pArchive, pThread, fp, pCrc); BailError(err); @@ -366,14 +367,13 @@ bail: * Requires that the pArchive->currentOffset be set to the offset * immediately after the last of the thread headers. */ -NuError -Nu_ComputeThreadData(NuArchive* pArchive, NuRecord* pRecord) +NuError Nu_ComputeThreadData(NuArchive* pArchive, NuRecord* pRecord) { NuThread* pThread; long fileOffset, count; - Assert(pArchive != nil); - Assert(pRecord != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); /*pRecord->totalLength = 0;*/ pRecord->totalCompLength = 0; @@ -407,15 +407,14 @@ Nu_ComputeThreadData(NuArchive* pArchive, NuRecord* pRecord) * show to the application. (Someday I'll get AndyN for putting me * through this...) */ -NuError -Nu_ScanThreads(NuArchive* pArchive, NuRecord* pRecord, long numThreads) +NuError Nu_ScanThreads(NuArchive* pArchive, NuRecord* pRecord, long numThreads) { NuError err = kNuErrNone; NuThread* pThread; FILE* fp; - Assert(pArchive != nil); - Assert(pRecord != nil); + Assert(pArchive != NULL); + Assert(pRecord != NULL); fp = pArchive->archiveFp; @@ -423,23 +422,23 @@ Nu_ScanThreads(NuArchive* pArchive, NuRecord* pRecord, long numThreads) pThread = pRecord->pThreads; while (numThreads--) { - if (pRecord->threadFilename == nil && + if (pRecord->threadFilenameMOR == NULL && NuMakeThreadID(pThread->thThreadClass, pThread->thThreadKind) == kNuThreadIDFilename) { /* it's the first filename thread, read the whole thing */ if (pThread->thCompThreadEOF > kNuReasonableFilenameLen) { err = kNuErrBadRecord; - Nu_ReportError(NU_BLOB, err, "Bad thread filename len (%lu)", + Nu_ReportError(NU_BLOB, err, "Bad thread filename len (%u)", pThread->thCompThreadEOF); goto bail; } - pRecord->threadFilename = Nu_Malloc(pArchive, + pRecord->threadFilenameMOR = Nu_Malloc(pArchive, pThread->thCompThreadEOF +1); - BailAlloc(pRecord->threadFilename); + BailAlloc(pRecord->threadFilenameMOR); /* note there is no CRC on a filename thread */ - (void) Nu_ReadBytes(pArchive, fp, pRecord->threadFilename, + (void) Nu_ReadBytes(pArchive, fp, pRecord->threadFilenameMOR, pThread->thCompThreadEOF); if ((err = Nu_HeaderIOFailed(pArchive, fp)) != kNuErrNone) { Nu_ReportError(NU_BLOB, err, "Failed reading filename thread"); @@ -447,15 +446,15 @@ Nu_ScanThreads(NuArchive* pArchive, NuRecord* pRecord, long numThreads) } /* null-terminate on the actual len, not the buffer len */ - pRecord->threadFilename[pThread->thThreadEOF] = '\0'; + pRecord->threadFilenameMOR[pThread->thThreadEOF] = '\0'; - Nu_StripHiIfAllSet(pRecord->threadFilename); + Nu_StripHiIfAllSet(pRecord->threadFilenameMOR); /* prefer this one over the record one, but only one should exist */ - if (pRecord->filename != nil) { + if (pRecord->filenameMOR != NULL) { DBUG(("--- HEY: got record filename and thread filename\n")); } - pRecord->filename = pRecord->threadFilename; + pRecord->filenameMOR = pRecord->threadFilenameMOR; } else { /* not a filename (or not first filename), skip past it */ @@ -473,9 +472,9 @@ Nu_ScanThreads(NuArchive* pArchive, NuRecord* pRecord, long numThreads) * end up with a disk image that had no name attached. This will tend * to confuse things, so we go ahead and give it a name. */ - if (pRecord->filename == nil) { + if (pRecord->filenameMOR == NULL) { DBUG(("+++ no filename found, using default record name\n")); - pRecord->filename = kNuDefaultRecordName; + pRecord->filenameMOR = kNuDefaultRecordName; } pArchive->currentOffset += pRecord->totalCompLength; @@ -494,8 +493,7 @@ bail: * assumes that the file pointer is set to the start of the thread's data * already. */ -NuError -Nu_SkipThread(NuArchive* pArchive, const NuRecord* pRecord, +NuError Nu_SkipThread(NuArchive* pArchive, const NuRecord* pRecord, const NuThread* pThread) { NuError err; @@ -522,12 +520,12 @@ Nu_SkipThread(NuArchive* pArchive, const NuRecord* pRecord, * If the archive is a stream, the stream must be positioned at the * start of pThread's data. If not, it will be seeked first. */ -static NuError -Nu_ExtractThreadToDataSink(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, NuProgressData* pProgress, NuDataSink* pDataSink) +static NuError Nu_ExtractThreadToDataSink(NuArchive* pArchive, + const NuRecord* pRecord, const NuThread* pThread, + NuProgressData* pProgress, NuDataSink* pDataSink) { NuError err; - NuFunnel* pFunnel = nil; + NuFunnel* pFunnel = NULL; /* if it's not a stream, seek to the appropriate spot in the file */ if (!Nu_IsStreaming(pArchive)) { @@ -576,9 +574,8 @@ bail: * filters for every thread, which means we can reject specific kinds * of forks and/or give them different names. This is a good thing. */ -static NuError -Nu_ExtractThreadCommon(NuArchive* pArchive, const NuRecord* pRecord, - const NuThread* pThread, NuDataSink* pDataSink) +static NuError Nu_ExtractThreadCommon(NuArchive* pArchive, + const NuRecord* pRecord, const NuThread* pThread, NuDataSink* pDataSink) { NuError err = kNuErrNone; NuSelectionProposal selProposal; @@ -586,18 +583,19 @@ Nu_ExtractThreadCommon(NuArchive* pArchive, const NuRecord* pRecord, NuProgressData progressData; NuProgressData* pProgressData; NuDataSink* pOrigDataSink; - char* newPathStorage = nil; - const char* newPathname; + UNICHAR* newPathStorageUNI = NULL; + UNICHAR* recFilenameStorageUNI = NULL; + const UNICHAR* newPathnameUNI; NuResult result; - uchar newFssep; + uint8_t newFssep; Boolean doFreeSink = false; - Assert(pRecord != nil); - Assert(pThread != nil); - Assert(pDataSink != nil); + Assert(pRecord != NULL); + Assert(pThread != NULL); + Assert(pDataSink != NULL); memset(&progressData, 0, sizeof(progressData)); - pProgressData = nil; + pProgressData = NULL; /* * If we're just trying to verify the archive contents, create a @@ -619,7 +617,7 @@ Nu_ExtractThreadCommon(NuArchive* pArchive, const NuRecord* pRecord, * use by the "bulk" extract, not the per-thread extract, but it * still applies if they so desire. */ - if (pArchive->selectionFilterFunc != nil) { + if (pArchive->selectionFilterFunc != NULL) { selProposal.pRecord = pRecord; selProposal.pThread = pThread; result = (*pArchive->selectionFilterFunc)(pArchive, &selProposal); @@ -632,9 +630,11 @@ Nu_ExtractThreadCommon(NuArchive* pArchive, const NuRecord* pRecord, } } - newPathname = nil; + newPathnameUNI = NULL; newFssep = 0; + recFilenameStorageUNI = Nu_CopyMORToUNI(pRecord->filenameMOR); + retry_name: if (Nu_DataSinkGetType(pDataSink) == kNuDataSinkToFile) { /* @@ -645,21 +645,21 @@ retry_name: * Start by resetting everything to defaults, in case this isn't * our first time through the "rename" loop. */ - newPathname = Nu_DataSinkFile_GetPathname(pDataSink); + newPathnameUNI = Nu_DataSinkFile_GetPathname(pDataSink); newFssep = Nu_DataSinkFile_GetFssep(pDataSink); pDataSink = pOrigDataSink; /* if they don't have a pathname func defined, we just use default */ - if (pArchive->outputPathnameFunc != nil) { - pathProposal.pathname = pRecord->filename; + if (pArchive->outputPathnameFunc != NULL) { + pathProposal.pathnameUNI = recFilenameStorageUNI; pathProposal.filenameSeparator = NuGetSepFromSysInfo(pRecord->recFileSysInfo); pathProposal.pRecord = pRecord; pathProposal.pThread = pThread; - pathProposal.newPathname = nil; + pathProposal.newPathnameUNI = NULL; pathProposal.newFilenameSeparator = '\0'; /*pathProposal.newStorage = (NuThreadID)-1;*/ - pathProposal.newDataSink = nil; + pathProposal.newDataSink = NULL; result = (*pArchive->outputPathnameFunc)(pArchive, &pathProposal); @@ -671,35 +671,38 @@ retry_name: } /* we don't own this string, so make a copy */ - if (pathProposal.newPathname != nil) { - newPathStorage = strdup(pathProposal.newPathname); - newPathname = newPathStorage; - } else - newPathname = nil; + if (pathProposal.newPathnameUNI != NULL) { + Nu_Free(pArchive, newPathStorageUNI); + newPathStorageUNI = strdup(pathProposal.newPathnameUNI); + newPathnameUNI = newPathStorageUNI; + } else { + newPathnameUNI = NULL; + } if (pathProposal.newFilenameSeparator != '\0') newFssep = pathProposal.newFilenameSeparator; /* if they want to send this somewhere else, let them */ - if (pathProposal.newDataSink != nil) + if (pathProposal.newDataSink != NULL) pDataSink = pathProposal.newDataSink; } /* at least one of these must be set */ - Assert(!(newPathname == nil && pathProposal.newDataSink == nil)); + Assert(!(newPathnameUNI == NULL && pathProposal.newDataSink == NULL)); } /* * Prepare the progress data if this is a data thread. */ - if (newPathname == nil) { + if (newPathnameUNI == NULL) { /* using a data sink; get the pathname out of the record */ - newPathname = pRecord->filename; + newPathnameUNI = recFilenameStorageUNI; newFssep = NuGetSepFromSysInfo(pRecord->recFileSysInfo); } if (pThread->thThreadClass == kNuThreadClassData) { pProgressData = &progressData; err = Nu_ProgressDataInit_Expand(pArchive, pProgressData, pRecord, - newPathname, newFssep, Nu_DataSinkGetConvertEOL(pOrigDataSink)); + newPathnameUNI, newFssep, recFilenameStorageUNI, + Nu_DataSinkGetConvertEOL(pOrigDataSink)); BailError(err); /* send initial progress so they see the right name if "open" fails */ @@ -713,23 +716,23 @@ retry_name: * We're extracting to a file. Open it, creating it if necessary and * allowed. */ - FILE* fileFp = nil; + FILE* fileFp = NULL; - err = Nu_OpenOutputFile(pArchive, pRecord, pThread, newPathname, + err = Nu_OpenOutputFile(pArchive, pRecord, pThread, newPathnameUNI, newFssep, &fileFp); if (err == kNuErrRename) { /* they want to rename; the OutputPathname callback handles this */ - Nu_Free(pArchive, newPathStorage); - newPathStorage = nil; + Nu_Free(pArchive, newPathStorageUNI); + newPathStorageUNI = NULL; /* reset these just to be careful */ - newPathname = nil; - fileFp = nil; + newPathnameUNI = NULL; + fileFp = NULL; goto retry_name; } else if (err != kNuErrNone) { goto bail; } - Assert(fileFp != nil); + Assert(fileFp != NULL); (void) Nu_DataSinkFile_SetFP(pDataSink, fileFp); DBUG(("+++ EXTRACTING 0x%08lx from '%s' at offset %0ld to '%s'\n", @@ -752,13 +755,13 @@ retry_name: * permissions as appropriate. */ err = Nu_CloseOutputFile(pArchive, pRecord, - Nu_DataSinkFile_GetFP(pDataSink), newPathname); - Nu_DataSinkFile_SetFP(pDataSink, nil); + Nu_DataSinkFile_GetFP(pDataSink), newPathnameUNI); + Nu_DataSinkFile_SetFP(pDataSink, NULL); BailError(err); } bail: - if (err != kNuErrNone && pProgressData != nil) { + if (err != kNuErrNone && pProgressData != NULL) { /* send a final progress message, indicating failure */ if (err == kNuErrSkipped) pProgressData->state = kNuProgressSkipped; @@ -773,7 +776,8 @@ bail: if (Nu_DataSinkGetType(pDataSink) == kNuDataSinkToFile) Nu_DataSinkFile_Close(pDataSink); - Nu_Free(pArchive, newPathStorage); + Nu_Free(pArchive, newPathStorageUNI); + Nu_Free(pArchive, recFilenameStorageUNI); if (doFreeSink) Nu_DataSinkFree(pDataSink); @@ -785,12 +789,12 @@ bail: * * Streaming archives must be properly positioned. */ -NuError -Nu_ExtractThreadBulk(NuArchive* pArchive, const NuRecord* pRecord, +NuError Nu_ExtractThreadBulk(NuArchive* pArchive, const NuRecord* pRecord, const NuThread* pThread) { NuError err; - NuDataSink* pDataSink = nil; + NuDataSink* pDataSink = NULL; + UNICHAR* recFilenameStorageUNI = NULL; NuValue eolConv; /* @@ -806,7 +810,8 @@ Nu_ExtractThreadBulk(NuArchive* pArchive, const NuRecord* pRecord, eolConv = pArchive->valConvertExtractedEOL; if (NuGetThreadID(pThread) == kNuThreadIDDiskImage) eolConv = kNuConvertOff; - err = Nu_DataSinkFile_New(true, eolConv, pRecord->filename, + recFilenameStorageUNI = Nu_CopyMORToUNI(pRecord->filenameMOR); + err = Nu_DataSinkFile_New(true, eolConv, recFilenameStorageUNI, NuGetSepFromSysInfo(pRecord->recFileSysInfo), &pDataSink); BailError(err); @@ -814,11 +819,12 @@ Nu_ExtractThreadBulk(NuArchive* pArchive, const NuRecord* pRecord, BailError(err); bail: - if (pDataSink != nil) { + if (pDataSink != NULL) { NuError err2 = Nu_DataSinkFree(pDataSink); if (err == kNuErrNone) err = err2; } + Nu_Free(pArchive, recFilenameStorageUNI); return err; } @@ -827,8 +833,7 @@ bail: /* * Extract a thread, given the IDs and a data sink. */ -NuError -Nu_ExtractThread(NuArchive* pArchive, NuThreadIdx threadIdx, +NuError Nu_ExtractThread(NuArchive* pArchive, NuThreadIdx threadIdx, NuDataSink* pDataSink) { NuError err; @@ -837,7 +842,7 @@ Nu_ExtractThread(NuArchive* pArchive, NuThreadIdx threadIdx, if (Nu_IsStreaming(pArchive)) return kNuErrUsage; - if (threadIdx == 0 || pDataSink == nil) + if (threadIdx == 0 || pDataSink == NULL) return kNuErrInvalidArg; err = Nu_GetTOCIfNeeded(pArchive); BailError(err); @@ -846,7 +851,7 @@ Nu_ExtractThread(NuArchive* pArchive, NuThreadIdx threadIdx, err = Nu_RecordSet_FindByThreadIdx(&pArchive->origRecordSet, threadIdx, &pRecord, &pThread); BailError(err); - Assert(pRecord != nil); + Assert(pRecord != NULL); /* extract away */ err = Nu_ExtractThreadCommon(pArchive, pRecord, pThread, pDataSink); @@ -872,9 +877,8 @@ bail: * * If a matching threadID is found, this returns an error. */ -static NuError -Nu_FindNoFutureThread(NuArchive* pArchive, const NuRecord* pRecord, - NuThreadID threadID) +static NuError Nu_FindNoFutureThread(NuArchive* pArchive, + const NuRecord* pRecord, NuThreadID threadID) { NuError err = kNuErrNone; const NuThread* pThread; @@ -886,13 +890,13 @@ Nu_FindNoFutureThread(NuArchive* pArchive, const NuRecord* pRecord, */ for (idx = 0; idx < (int)pRecord->recTotalThreads; idx++) { pThread = Nu_GetThread(pRecord, idx); - Assert(pThread != nil); + Assert(pThread != NULL); if (NuGetThreadID(pThread) == threadID) { /* found a match, see if it has been deleted */ pThreadMod = Nu_ThreadMod_FindByThreadIdx(pRecord, pThread->threadIdx); - if (pThreadMod != nil && + if (pThreadMod != NULL && pThreadMod->entry.kind == kNuThreadModDelete) { /* it's deleted, ignore it */ @@ -908,7 +912,7 @@ Nu_FindNoFutureThread(NuArchive* pArchive, const NuRecord* pRecord, * Now look for "add" threadMods with a matching threadID. */ pThreadMod = pRecord->pThreadMods; - while (pThreadMod != nil) { + while (pThreadMod != NULL) { if (pThreadMod->entry.kind == kNuThreadModAdd && pThreadMod->entry.add.threadID == threadID) { @@ -927,9 +931,8 @@ bail: /* * Like Nu_FindNoFutureThread, but tests against a whole class. */ -static NuError -Nu_FindNoFutureThreadClass(NuArchive* pArchive, const NuRecord* pRecord, - long threadClass) +static NuError Nu_FindNoFutureThreadClass(NuArchive* pArchive, + const NuRecord* pRecord, long threadClass) { NuError err = kNuErrNone; const NuThread* pThread; @@ -941,13 +944,13 @@ Nu_FindNoFutureThreadClass(NuArchive* pArchive, const NuRecord* pRecord, */ for (idx = 0; idx < (int)pRecord->recTotalThreads; idx++) { pThread = Nu_GetThread(pRecord, idx); - Assert(pThread != nil); + Assert(pThread != NULL); if (pThread->thThreadClass == threadClass) { /* found a match, see if it has been deleted */ pThreadMod = Nu_ThreadMod_FindByThreadIdx(pRecord, pThread->threadIdx); - if (pThreadMod != nil && + if (pThreadMod != NULL && pThreadMod->entry.kind == kNuThreadModDelete) { /* it's deleted, ignore it */ @@ -963,7 +966,7 @@ Nu_FindNoFutureThreadClass(NuArchive* pArchive, const NuRecord* pRecord, * Now look for "add" threadMods with a matching threadClass. */ pThreadMod = pRecord->pThreadMods; - while (pThreadMod != nil) { + while (pThreadMod != NULL) { if (pThreadMod->entry.kind == kNuThreadModAdd && NuThreadIDGetClass(pThreadMod->entry.add.threadID) == threadClass) { @@ -988,9 +991,8 @@ bail: * The record and thread returned will always be from the "copy" set. An * error result is returned if the record and thread aren't found. */ -static NuError -Nu_FindThreadForWriteByIdx(NuArchive* pArchive, NuThreadIdx threadIdx, - NuRecord** ppFoundRecord, NuThread** ppFoundThread) +static NuError Nu_FindThreadForWriteByIdx(NuArchive* pArchive, + NuThreadIdx threadIdx, NuRecord** ppFoundRecord, NuThread** ppFoundThread) { NuError err; @@ -1001,7 +1003,7 @@ Nu_FindThreadForWriteByIdx(NuArchive* pArchive, NuThreadIdx threadIdx, Assert(Nu_RecordSet_GetLoaded(&pArchive->origRecordSet)); err = Nu_RecordSet_FindByThreadIdx(&pArchive->origRecordSet, threadIdx, ppFoundRecord, ppFoundThread); - *ppFoundThread = nil; /* can't delete from here, wipe ptr */ + *ppFoundThread = NULL; /* can't delete from here, wipe ptr */ } BailError(err); @@ -1009,13 +1011,13 @@ Nu_FindThreadForWriteByIdx(NuArchive* pArchive, NuThreadIdx threadIdx, * The thread exists. If we were looking in the "orig" set, we have * to create a "copy" set, and delete it from that. */ - if (*ppFoundThread == nil) { + if (*ppFoundThread == NULL) { err = Nu_RecordSet_Clone(pArchive, &pArchive->copyRecordSet, &pArchive->origRecordSet); BailError(err); err = Nu_RecordSet_FindByThreadIdx(&pArchive->copyRecordSet, threadIdx, ppFoundRecord, ppFoundThread); - Assert(err == kNuErrNone && *ppFoundThread != nil); /* must succeed */ + Assert(err == kNuErrNone && *ppFoundThread != NULL); /* must succeed */ BailError(err); } @@ -1029,8 +1031,7 @@ bail: * * Returns with an error (kNuErrThreadAdd) if it's not okay. */ -NuError -Nu_OkayToAddThread(NuArchive* pArchive, const NuRecord* pRecord, +NuError Nu_OkayToAddThread(NuArchive* pArchive, const NuRecord* pRecord, NuThreadID threadID) { NuError err = kNuErrNone; @@ -1094,17 +1095,16 @@ bail: * On success, the NuThreadIdx of the newly-created record will be placed * in "*pThreadIdx", and "pDataSource" will be owned by NufxLib. */ -NuError -Nu_AddThread(NuArchive* pArchive, NuRecordIdx recIdx, NuThreadID threadID, - NuDataSource* pDataSource, NuThreadIdx* pThreadIdx) +NuError Nu_AddThread(NuArchive* pArchive, NuRecordIdx recIdx, + NuThreadID threadID, NuDataSource* pDataSource, NuThreadIdx* pThreadIdx) { NuError err; NuRecord* pRecord; - NuThreadMod* pThreadMod = nil; + NuThreadMod* pThreadMod = NULL; NuThreadFormat threadFormat; - /* okay for pThreadIdx to be nil */ - if (recIdx == 0 || pDataSource == nil) + /* okay for pThreadIdx to be NULL */ + if (recIdx == 0 || pDataSource == NULL) return kNuErrInvalidArg; if (Nu_IsReadOnly(pArchive)) @@ -1123,7 +1123,7 @@ Nu_AddThread(NuArchive* pArchive, NuRecordIdx recIdx, NuThreadID threadID, err = Nu_RecordSet_FindByIdx(&pArchive->newRecordSet, recIdx, &pRecord); } BailError(err); - Assert(pRecord != nil); + Assert(pRecord != NULL); /* * Do some tests, looking for specific types of threads that conflict @@ -1153,13 +1153,13 @@ Nu_AddThread(NuArchive* pArchive, NuRecordIdx recIdx, NuThreadID threadID, err = Nu_ThreadModAdd_New(pArchive, threadID, threadFormat, pDataSource, &pThreadMod); BailError(err); - Assert(pThreadMod != nil); + Assert(pThreadMod != NULL); /* add the thread mod to the record */ Nu_RecordAddThreadMod(pRecord, pThreadMod); - if (pThreadIdx != nil) + if (pThreadIdx != NULL) *pThreadIdx = pThreadMod->entry.add.threadIdx; - pThreadMod = nil; /* successful, don't free */ + pThreadMod = NULL; /* successful, don't free */ /* * If we've got a header filename and we're adding a filename thread, @@ -1172,9 +1172,9 @@ Nu_AddThread(NuArchive* pArchive, NuRecordIdx recIdx, NuThreadID threadID, } bail: - if (pThreadMod != nil) + if (pThreadMod != NULL) Nu_ThreadModFree(pArchive, pThreadMod); - if (err == kNuErrNone && pDataSource != nil) { + if (err == kNuErrNone && pDataSource != NULL) { /* on success, we have ownership of the data source. ThreadMod made its own copy, so get rid of this one */ Nu_DataSourceFree(pDataSource); @@ -1194,16 +1194,15 @@ bail: * You aren't allowed to update threads that have been deleted. Updating * newly-added threads isn't possible, since they aren't really threads yet. */ -NuError -Nu_UpdatePresizedThread(NuArchive* pArchive, NuThreadIdx threadIdx, - NuDataSource* pDataSource, long* pMaxLen) +NuError Nu_UpdatePresizedThread(NuArchive* pArchive, NuThreadIdx threadIdx, + NuDataSource* pDataSource, int32_t* pMaxLen) { NuError err; - NuThreadMod* pThreadMod = nil; + NuThreadMod* pThreadMod = NULL; NuRecord* pFoundRecord; NuThread* pFoundThread; - if (pDataSource == nil) { + if (pDataSource == NULL) { err = kNuErrInvalidArg; goto bail; } @@ -1239,14 +1238,14 @@ Nu_UpdatePresizedThread(NuArchive* pArchive, NuThreadIdx threadIdx, goto bail; } - if (pMaxLen != nil) + if (pMaxLen != NULL) *pMaxLen = pFoundThread->thCompThreadEOF; /* * Check to see if somebody is trying to delete this, or has already * updated it. */ - if (Nu_ThreadMod_FindByThreadIdx(pFoundRecord, threadIdx) != nil) { + if (Nu_ThreadMod_FindByThreadIdx(pFoundRecord, threadIdx) != NULL) { DBUG(("--- Tried to modify a deleted or modified thread\n")); err = kNuErrModThreadChange; goto bail; @@ -1269,7 +1268,7 @@ Nu_UpdatePresizedThread(NuArchive* pArchive, NuThreadIdx threadIdx, Nu_DataSourceGetOtherLen(pDataSource)) { err = kNuErrPreSizeOverflow; - Nu_ReportError(NU_BLOB, err, "can't put %ld bytes into %ld", + Nu_ReportError(NU_BLOB, err, "can't put %u bytes into %u", Nu_DataSourceGetOtherLen(pDataSource), pFoundThread->thCompThreadEOF); goto bail; @@ -1281,7 +1280,7 @@ Nu_UpdatePresizedThread(NuArchive* pArchive, NuThreadIdx threadIdx, Nu_DataSourceGetOtherLen(pDataSource) > kNuReasonableFilenameLen)) { err = kNuErrInvalidFilename; - Nu_ReportError(NU_BLOB, err, "invalid filename (%ld bytes)", + Nu_ReportError(NU_BLOB, err, "invalid filename (%u bytes)", Nu_DataSourceGetOtherLen(pDataSource)); goto bail; } @@ -1294,7 +1293,7 @@ Nu_UpdatePresizedThread(NuArchive* pArchive, NuThreadIdx threadIdx, Assert(pFoundThread->thThreadFormat == kNuThreadFormatUncompressed); err = Nu_ThreadModUpdate_New(pArchive, threadIdx, pDataSource, &pThreadMod); BailError(err); - Assert(pThreadMod != nil); + Assert(pThreadMod != NULL); /* add the thread mod to the record */ Nu_RecordAddThreadMod(pFoundRecord, pThreadMod); @@ -1322,11 +1321,10 @@ bail: * later on. Besides, it's sort of handy to hang on to the filename for * as long as possible. */ -NuError -Nu_DeleteThread(NuArchive* pArchive, NuThreadIdx threadIdx) +NuError Nu_DeleteThread(NuArchive* pArchive, NuThreadIdx threadIdx) { NuError err; - NuThreadMod* pThreadMod = nil; + NuThreadMod* pThreadMod = NULL; NuRecord* pFoundRecord; NuThread* pFoundThread; @@ -1348,7 +1346,7 @@ Nu_DeleteThread(NuArchive* pArchive, NuThreadIdx threadIdx) * allowed. Deletion of threads from deleted records can't happen, * because deleted records are completely removed from the "copy" set. */ - if (Nu_ThreadMod_FindByThreadIdx(pFoundRecord, threadIdx) != nil) { + if (Nu_ThreadMod_FindByThreadIdx(pFoundRecord, threadIdx) != NULL) { DBUG(("--- Tried to delete a deleted or modified thread\n")); err = kNuErrModThreadChange; goto bail; @@ -1361,7 +1359,7 @@ Nu_DeleteThread(NuArchive* pArchive, NuThreadIdx threadIdx) NuGetThreadID(pFoundThread), &pThreadMod); BailError(err); Nu_RecordAddThreadMod(pFoundRecord, pThreadMod); - pThreadMod = nil; /* successful, don't free */ + pThreadMod = NULL; /* successful, don't free */ bail: Nu_ThreadModFree(pArchive, pThreadMod); diff --git a/nufxlib/Value.c b/nufxlib/Value.c index d6465f7..c344b01 100644 --- a/nufxlib/Value.c +++ b/nufxlib/Value.c @@ -14,12 +14,11 @@ /* * Get a configurable parameter. */ -NuError -Nu_GetValue(NuArchive* pArchive, NuValueID ident, NuValue* pValue) +NuError Nu_GetValue(NuArchive* pArchive, NuValueID ident, NuValue* pValue) { NuError err = kNuErrNone; - if (pValue == nil) + if (pValue == NULL) return kNuErrInvalidArg; switch (ident) { @@ -82,8 +81,7 @@ bail: /* * Set a configurable parameter. */ -NuError -Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) +NuError Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) { NuError err = kNuErrInvalidArg; @@ -91,7 +89,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueAllowDuplicates: if (value != true && value != false) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueAllowDuplicates value %ld", value); + "Invalid kNuValueAllowDuplicates value %u", value); goto bail; } pArchive->valAllowDuplicates = value; @@ -99,7 +97,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueConvertExtractedEOL: if (value < kNuConvertOff || value > kNuConvertAuto) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueConvertExtractedEOL value %ld", value); + "Invalid kNuValueConvertExtractedEOL value %u", value); goto bail; } pArchive->valConvertExtractedEOL = value; @@ -107,7 +105,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueDataCompression: if (value < kNuCompressNone || value > kNuCompressBzip2) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueDataCompression value %ld", value); + "Invalid kNuValueDataCompression value %u", value); goto bail; } pArchive->valDataCompression = value; @@ -115,7 +113,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueDiscardWrapper: if (value != true && value != false) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueDiscardWrapper value %ld", value); + "Invalid kNuValueDiscardWrapper value %u", value); goto bail; } pArchive->valDiscardWrapper = value; @@ -123,7 +121,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueEOL: if (value < kNuEOLUnknown || value > kNuEOLCRLF) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueEOL value %ld", value); + "Invalid kNuValueEOL value %u", value); goto bail; } pArchive->valEOL = value; @@ -131,7 +129,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueHandleExisting: if (value < kNuMaybeOverwrite || value > kNuMustOverwrite) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueHandleExisting value %ld", value); + "Invalid kNuValueHandleExisting value %u", value); goto bail; } pArchive->valHandleExisting = value; @@ -139,7 +137,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueIgnoreCRC: if (value != true && value != false) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueIgnoreCRC value %ld", value); + "Invalid kNuValueIgnoreCRC value %u", value); goto bail; } pArchive->valIgnoreCRC = value; @@ -147,7 +145,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueMaskDataless: if (value != true && value != false) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueMaskDataless value %ld", value); + "Invalid kNuValueMaskDataless value %u", value); goto bail; } pArchive->valMaskDataless = value; @@ -155,7 +153,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueMimicSHK: if (value != true && value != false) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueMimicSHK value %ld", value); + "Invalid kNuValueMimicSHK value %u", value); goto bail; } pArchive->valMimicSHK = value; @@ -163,7 +161,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueModifyOrig: if (value != true && value != false) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueModifyOrig value %ld", value); + "Invalid kNuValueModifyOrig value %u", value); goto bail; } pArchive->valModifyOrig = value; @@ -171,7 +169,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueOnlyUpdateOlder: if (value != true && value != false) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueOnlyUpdateOlder value %ld", value); + "Invalid kNuValueOnlyUpdateOlder value %u", value); goto bail; } pArchive->valOnlyUpdateOlder = value; @@ -179,7 +177,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueStripHighASCII: if (value != true && value != false) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueStripHighASCII value %ld", value); + "Invalid kNuValueStripHighASCII value %u", value); goto bail; } pArchive->valStripHighASCII = value; @@ -187,7 +185,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueJunkSkipMax: if (value > kMaxJunkSkipMax) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueJunkSkipMax value %ld", value); + "Invalid kNuValueJunkSkipMax value %u", value); goto bail; } pArchive->valJunkSkipMax = value; @@ -195,7 +193,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueIgnoreLZW2Len: if (value != true && value != false) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueIgnoreLZW2Len value %ld", value); + "Invalid kNuValueIgnoreLZW2Len value %u", value); goto bail; } pArchive->valIgnoreLZW2Len = value; @@ -203,7 +201,7 @@ Nu_SetValue(NuArchive* pArchive, NuValueID ident, NuValue value) case kNuValueHandleBadMac: if (value != true && value != false) { Nu_ReportError(NU_BLOB, err, - "Invalid kNuValueHandleBadMac value %ld", value); + "Invalid kNuValueHandleBadMac value %u", value); goto bail; } pArchive->valHandleBadMac = value; @@ -225,11 +223,10 @@ bail: * pry into pArchive to get at (like the archive type) or get the master * header (like the number of records). */ -NuError -Nu_GetAttr(NuArchive* pArchive, NuAttrID ident, NuAttr* pAttr) +NuError Nu_GetAttr(NuArchive* pArchive, NuAttrID ident, NuAttr* pAttr) { NuError err = kNuErrNone; - if (pAttr == nil) + if (pAttr == NULL) return kNuErrInvalidArg; switch (ident) { @@ -260,8 +257,8 @@ bail: * * Unsupported compression types cause a warning to be flagged. */ -NuThreadFormat -Nu_ConvertCompressValToFormat(NuArchive* pArchive, NuValue compValue) +NuThreadFormat Nu_ConvertCompressValToFormat(NuArchive* pArchive, + NuValue compValue) { NuThreadFormat threadFormat; Boolean unsup = false; @@ -312,14 +309,14 @@ Nu_ConvertCompressValToFormat(NuArchive* pArchive, NuValue compValue) default: Nu_ReportError(NU_BLOB, kNuErrInvalidArg, - "Unknown compress value %ld", compValue); + "Unknown compress value %u", compValue); Assert(false); return kNuThreadFormatUncompressed; } if (unsup) { Nu_ReportError(NU_BLOB, kNuErrNone, - "Unsupported compression 0x%04x requested (%ld), storing", + "Unsupported compression 0x%04x requested (%u), storing", threadFormat, compValue); return kNuThreadFormatUncompressed; } diff --git a/nufxlib/Version.c b/nufxlib/Version.c index 7eec7c2..9c3396c 100644 --- a/nufxlib/Version.c +++ b/nufxlib/Version.c @@ -23,19 +23,18 @@ static const char gNuBuildFlags[] = "-"; /* * Return the version number, date built, and build flags. */ -NuError -Nu_GetVersion(long* pMajorVersion, long* pMinorVersion, long* pBugVersion, - const char** ppBuildDate, const char** ppBuildFlags) +NuError Nu_GetVersion(int32_t* pMajorVersion, int32_t* pMinorVersion, + int32_t* pBugVersion, const char** ppBuildDate, const char** ppBuildFlags) { - if (pMajorVersion != nil) + if (pMajorVersion != NULL) *pMajorVersion = kNuVersionMajor; - if (pMinorVersion != nil) + if (pMinorVersion != NULL) *pMinorVersion = kNuVersionMinor; - if (pBugVersion != nil) + if (pBugVersion != NULL) *pBugVersion = kNuVersionBug; - if (ppBuildDate != nil) + if (ppBuildDate != NULL) *ppBuildDate = gNuBuildDate; - if (ppBuildFlags != nil) + if (ppBuildFlags != NULL) *ppBuildFlags = gNuBuildFlags; return kNuErrNone; } diff --git a/nufxlib/config.h.in b/nufxlib/config.h.in index ec6d300..dd294e2 100644 --- a/nufxlib/config.h.in +++ b/nufxlib/config.h.in @@ -27,18 +27,6 @@ /* Define if your declares struct tm. */ #undef TM_IN_SYS_TIME -/* Define to `unsigned char' if doesn't define. */ -#undef uchar - -/* Define to `unsigned short' if doesn't define. */ -#undef ushort - -/* Define to `unsigned int' if doesn't define. */ -#undef uint - -/* Define to `unsigned long' if doesn't define. */ -#undef ulong - /* Define to `int' if doesn't define. */ #undef mode_t diff --git a/nufxlib/configure b/nufxlib/configure index f029fde..f459119 100755 --- a/nufxlib/configure +++ b/nufxlib/configure @@ -3879,50 +3879,6 @@ $as_echo "#define TM_IN_SYS_TIME 1" >>confdefs.h fi -ac_fn_c_check_type "$LINENO" "uchar" "ac_cv_type_uchar" "$ac_includes_default" -if test "x$ac_cv_type_uchar" = xyes; then : - -else - -cat >>confdefs.h <<_ACEOF -#define uchar unsigned char -_ACEOF - -fi - -ac_fn_c_check_type "$LINENO" "ushort" "ac_cv_type_ushort" "$ac_includes_default" -if test "x$ac_cv_type_ushort" = xyes; then : - -else - -cat >>confdefs.h <<_ACEOF -#define ushort unsigned short -_ACEOF - -fi - -ac_fn_c_check_type "$LINENO" "uint" "ac_cv_type_uint" "$ac_includes_default" -if test "x$ac_cv_type_uint" = xyes; then : - -else - -cat >>confdefs.h <<_ACEOF -#define uint unsigned int -_ACEOF - -fi - -ac_fn_c_check_type "$LINENO" "ulong" "ac_cv_type_ulong" "$ac_includes_default" -if test "x$ac_cv_type_ulong" = xyes; then : - -else - -cat >>confdefs.h <<_ACEOF -#define ulong unsigned long -_ACEOF - -fi - for ac_func in fdopen ftruncate memmove mkdir mkstemp mktime timelocal \ localtime_r snprintf strcasecmp strncasecmp strtoul strerror vsnprintf @@ -4014,11 +3970,6 @@ elif test "$host_os" = "beos"; then SHARE_FLAGS='-nostartfiles -Xlinker -soname="$@"' fi -if test "$host_vendor" = "apple" -a ${host_os:0:6} = "darwin"; then - echo "checking for Mac OS X... yes, adding -framework Carbon" - LIBS="$LIBS -framework Carbon" -fi - diff --git a/nufxlib/configure.in b/nufxlib/configure.in index 2a0cb03..46df1a4 100644 --- a/nufxlib/configure.in +++ b/nufxlib/configure.in @@ -29,10 +29,6 @@ AC_TYPE_MODE_T AC_TYPE_OFF_T AC_TYPE_SIZE_T AC_STRUCT_TM -AC_CHECK_TYPE(uchar, unsigned char) -AC_CHECK_TYPE(ushort, unsigned short) -AC_CHECK_TYPE(uint, unsigned int) -AC_CHECK_TYPE(ulong, unsigned long) dnl Checks for library functions. AC_CHECK_FUNCS(fdopen ftruncate memmove mkdir mkstemp mktime timelocal \ @@ -94,12 +90,6 @@ elif test "$host_os" = "beos"; then SHARE_FLAGS='-nostartfiles -Xlinker -soname="$@"' fi -dnl Mac OS X (powerpc-apple-darwin6.6) needs an extra flag. -if test "$host_vendor" = "apple" -a ${host_os:0:6} = "darwin"; then - echo "checking for Mac OS X... yes, adding -framework Carbon" - LIBS="$LIBS -framework Carbon" -fi - AC_SUBST(BUILD_FLAGS) AC_SUBST(SHARE_FLAGS) diff --git a/nufxlib/nufxlib.def b/nufxlib/nufxlib.def index 0fc2673..ad33e3e 100644 --- a/nufxlib/nufxlib.def +++ b/nufxlib/nufxlib.def @@ -16,6 +16,8 @@ EXPORTS NuAddThread NuClose NuContents + NuConvertMORToUNI + NuConvertUNIToMOR NuCreateDataSinkForBuffer NuCreateDataSinkForFP NuCreateDataSinkForFile diff --git a/nufxlib/nufxlib.vcxproj b/nufxlib/nufxlib.vcxproj index 02c1378..d6b4d3c 100644 --- a/nufxlib/nufxlib.vcxproj +++ b/nufxlib/nufxlib.vcxproj @@ -85,6 +85,7 @@ + diff --git a/nufxlib/nufxlib.vcxproj.filters b/nufxlib/nufxlib.vcxproj.filters index 3408ddb..816c720 100644 --- a/nufxlib/nufxlib.vcxproj.filters +++ b/nufxlib/nufxlib.vcxproj.filters @@ -95,5 +95,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/nufxlib/samples/Common.h b/nufxlib/samples/Common.h index 0f377b8..2391747 100644 --- a/nufxlib/samples/Common.h +++ b/nufxlib/samples/Common.h @@ -6,8 +6,8 @@ * * Common functions for NuLib tests. */ -#ifndef __Common__ -#define __Common__ +#ifndef NUFXLIB_SAMPLES_COMMON_H +#define NUFXLIB_SAMPLES_COMMON_H #include "SysDefs.h" /* might as well draft off the autoconf */ #include "NufxLib.h" @@ -16,8 +16,6 @@ # include "dmalloc.h" #endif -#define nil NULL /* this is seriously habit-forming */ - #define NELEM(x) (sizeof(x) / sizeof((x)[0])) #ifndef __cplusplus @@ -65,4 +63,4 @@ # define PATH_SEP '/' #endif -#endif /*__Common__*/ +#endif /*NUFXLIB_SAMPLES_COMMON_H*/ diff --git a/nufxlib/samples/Exerciser.c b/nufxlib/samples/Exerciser.c index e3be72d..18b72dc 100644 --- a/nufxlib/samples/Exerciser.c +++ b/nufxlib/samples/Exerciser.c @@ -44,81 +44,76 @@ typedef struct ExerciserState { } ExerciserState; -ExerciserState* -ExerciserState_New(void) +ExerciserState* ExerciserState_New(void) { ExerciserState* pExerState; pExerState = (ExerciserState*) malloc(sizeof(*pExerState)); - if (pExerState == nil) - return nil; + if (pExerState == NULL) + return NULL; - pExerState->pArchive = nil; - pExerState->archivePath = nil; - pExerState->archiveFile = nil; + pExerState->pArchive = NULL; + pExerState->archivePath = NULL; + pExerState->archiveFile = NULL; return pExerState; } -void -ExerciserState_Free(ExerciserState* pExerState) +void ExerciserState_Free(ExerciserState* pExerState) { - if (pExerState == nil) + if (pExerState == NULL) return; - if (pExerState->pArchive != nil) { + if (pExerState->pArchive != NULL) { printf("Exerciser: aborting open archive\n"); (void) NuAbort(pExerState->pArchive); (void) NuClose(pExerState->pArchive); } - if (pExerState->archivePath != nil) + if (pExerState->archivePath != NULL) free(pExerState->archivePath); free(pExerState); } -inline NuArchive* -ExerciserState_GetNuArchive(const ExerciserState* pExerState) +inline NuArchive* ExerciserState_GetNuArchive(const ExerciserState* pExerState) { return pExerState->pArchive; } -inline void -ExerciserState_SetNuArchive(ExerciserState* pExerState, NuArchive* newArchive) +inline void ExerciserState_SetNuArchive(ExerciserState* pExerState, + NuArchive* newArchive) { pExerState->pArchive = newArchive; } -inline char* -ExerciserState_GetArchivePath(const ExerciserState* pExerState) +inline char* ExerciserState_GetArchivePath(const ExerciserState* pExerState) { return pExerState->archivePath; } -inline void -ExerciserState_SetArchivePath(ExerciserState* pExerState, char* newPath) +inline void ExerciserState_SetArchivePath(ExerciserState* pExerState, + char* newPath) { - if (pExerState->archivePath != nil) + if (pExerState->archivePath != NULL) free(pExerState->archivePath); - if (newPath == nil) { - pExerState->archivePath = nil; - pExerState->archiveFile = nil; + if (newPath == NULL) { + pExerState->archivePath = NULL; + pExerState->archiveFile = NULL; } else { pExerState->archivePath = strdup(newPath); pExerState->archiveFile = strrchr(newPath, kFssep); - if (pExerState->archiveFile != nil) + if (pExerState->archiveFile != NULL) pExerState->archiveFile++; - if (pExerState->archiveFile == nil || *pExerState->archiveFile == '\0') + if (pExerState->archiveFile == NULL || *pExerState->archiveFile == '\0') pExerState->archiveFile = pExerState->archivePath; } } -inline const char* -ExerciserState_GetArchiveFile(const ExerciserState* pExerState) +inline const char* ExerciserState_GetArchiveFile(const ExerciserState* pExerState) { - if (pExerState->archiveFile == nil) + if (pExerState->archiveFile == NULL) return "[no archive open]"; else return pExerState->archiveFile; @@ -134,16 +129,15 @@ ExerciserState_GetArchiveFile(const ExerciserState* pExerState) /* * NuContents callback function. Print the contents of an individual record. */ -NuResult -PrintEntry(NuArchive* pArchive, void* vpRecord) +NuResult PrintEntry(NuArchive* pArchive, void* vpRecord) { const NuRecord* pRecord = (const NuRecord*) vpRecord; int idx; (void)pArchive; /* shut up, gcc */ - printf("RecordIdx %ld: '%s'\n", - pRecord->recordIdx, pRecord->filename); + printf("RecordIdx %u: '%s'\n", + pRecord->recordIdx, pRecord->filenameMOR); for (idx = 0; idx < (int) pRecord->recTotalThreads; idx++) { const NuThread* pThread; @@ -151,7 +145,7 @@ PrintEntry(NuArchive* pArchive, void* vpRecord) const char* threadLabel; pThread = NuGetThread(pRecord, idx); - assert(pThread != nil); + assert(pThread != NULL); threadID = NuGetThreadID(pThread); switch (NuThreadIDGetClass(threadID)) { @@ -198,7 +192,7 @@ PrintEntry(NuArchive* pArchive, void* vpRecord) break; } - printf(" ThreadIdx %ld - 0x%08lx (%s)\n", pThread->threadIdx, + printf(" ThreadIdx %u - 0x%08x (%s)\n", pThread->threadIdx, threadID, threadLabel); } @@ -211,13 +205,12 @@ PrintEntry(NuArchive* pArchive, void* vpRecord) /* * Get a line of input, stripping the '\n' off the end. */ -static NuError -GetLine(const char* prompt, char* buffer, int bufferSize) +static NuError GetLine(const char* prompt, char* buffer, int bufferSize) { printf("%s> ", prompt); fflush(stdout); - if (fgets(buffer, bufferSize, stdin) == nil) + if (fgets(buffer, bufferSize, stdin) == NULL) return kNuErrGeneric; if (buffer[strlen(buffer)-1] == '\n') @@ -230,16 +223,15 @@ GetLine(const char* prompt, char* buffer, int bufferSize) /* * Selection filter for mass "extract" and "delete" operations. */ -NuResult -SelectionFilter(NuArchive* pArchive, void* vselFilt) +NuResult SelectionFilter(NuArchive* pArchive, void* vselFilt) { const NuSelectionProposal* selProposal = (NuSelectionProposal*) vselFilt; char buffer[8]; - printf("%s (N/y)? ", selProposal->pRecord->filename); + printf("%s (N/y)? ", selProposal->pRecord->filenameMOR); fflush(stdout); - if (fgets(buffer, sizeof(buffer), stdin) == nil) + if (fgets(buffer, sizeof(buffer), stdin) == NULL) return kNuAbort; if (tolower(buffer[0]) == 'y') @@ -252,8 +244,7 @@ SelectionFilter(NuArchive* pArchive, void* vselFilt) /* * General-purpose error handler. */ -NuResult -ErrorHandler(NuArchive* pArchive, void* vErrorStatus) +NuResult ErrorHandler(NuArchive* pArchive, void* vErrorStatus) { const NuErrorStatus* pErrorStatus = (const NuErrorStatus*) vErrorStatus; char buffer[8]; @@ -262,8 +253,8 @@ ErrorHandler(NuArchive* pArchive, void* vErrorStatus) printf("Exerciser: error handler op=%d err=%d sysErr=%d message='%s'\n" "\tfilename='%s' '%c'(0x%02x)\n", pErrorStatus->operation, pErrorStatus->err, pErrorStatus->sysErr, - pErrorStatus->message == nil ? "(nil)" : pErrorStatus->message, - pErrorStatus->pathname, pErrorStatus->filenameSeparator, + pErrorStatus->message == NULL ? "(NULL)" : pErrorStatus->message, + pErrorStatus->pathnameUNI, pErrorStatus->filenameSeparator, pErrorStatus->filenameSeparator); printf("\tValid options are:"); if (pErrorStatus->canAbort) @@ -283,7 +274,7 @@ ErrorHandler(NuArchive* pArchive, void* vErrorStatus) printf("Return what (a/r/i/s/e/o)? "); fflush(stdout); - if (fgets(buffer, sizeof(buffer), stdin) == nil) { + if (fgets(buffer, sizeof(buffer), stdin) == NULL) { printf("Returning kNuSkip\n"); } else switch (buffer[0]) { case 'a': result = kNuAbort; break; @@ -303,8 +294,7 @@ ErrorHandler(NuArchive* pArchive, void* vErrorStatus) /* * This gets called when a buffer DataSource is no longer needed. */ -NuResult -FreeCallback(NuArchive* pArchive, void* args) +NuResult FreeCallback(NuArchive* pArchive, void* args) { free(args); return kNuOK; @@ -335,8 +325,7 @@ GenericFunc(ExerciserState* pState, int argc, char** argv) /* * Do nothing. Useful when the user just hits on a blank line. */ -static NuError -NothingFunc(ExerciserState* pState, int argc, char** argv) +static NuError NothingFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ return kNuErrNone; @@ -348,8 +337,7 @@ NothingFunc(ExerciserState* pState, int argc, char** argv) * Do nothing. This is used as a trigger for quitting the program. In * practice, we catch this earlier, and won't actually call here. */ -static NuError -QuitFunc(ExerciserState* pState, int argc, char** argv) +static NuError QuitFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ assert(0); @@ -361,11 +349,10 @@ QuitFunc(ExerciserState* pState, int argc, char** argv) /* * ab - abort current changes */ -static NuError -AbortFunc(ExerciserState* pState, int argc, char** argv) +static NuError AbortFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 1); return NuAbort(ExerciserState_GetNuArchive(pState)); @@ -374,44 +361,42 @@ AbortFunc(ExerciserState* pState, int argc, char** argv) /* * af - add file to archive */ -static NuError -AddFileFunc(ExerciserState* pState, int argc, char** argv) +static NuError AddFileFunc(ExerciserState* pState, int argc, char** argv) { NuFileDetails nuFileDetails; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); memset(&nuFileDetails, 0, sizeof(nuFileDetails)); nuFileDetails.threadID = kNuThreadIDDataFork; - nuFileDetails.storageName = argv[1]; + nuFileDetails.storageNameMOR = argv[1]; nuFileDetails.fileSysID = kNuFileSysUnknown; nuFileDetails.fileSysInfo = (short) kFssep; nuFileDetails.access = kUnlocked; /* fileType, extraType, storageType, dates */ return NuAddFile(ExerciserState_GetNuArchive(pState), argv[1], - &nuFileDetails, false, nil); + &nuFileDetails, false, NULL); } /* * ar - add an empty record */ -static NuError -AddRecordFunc(ExerciserState* pState, int argc, char** argv) +static NuError AddRecordFunc(ExerciserState* pState, int argc, char** argv) { NuError err; NuRecordIdx recordIdx; NuFileDetails nuFileDetails; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); memset(&nuFileDetails, 0, sizeof(nuFileDetails)); nuFileDetails.threadID = 0; /* irrelevant */ - nuFileDetails.storageName = argv[1]; + nuFileDetails.storageNameMOR = argv[1]; nuFileDetails.fileSysID = kNuFileSysUnknown; nuFileDetails.fileSysInfo = (short) kFssep; nuFileDetails.access = kUnlocked; @@ -420,31 +405,30 @@ AddRecordFunc(ExerciserState* pState, int argc, char** argv) err = NuAddRecord(ExerciserState_GetNuArchive(pState), &nuFileDetails, &recordIdx); if (err == kNuErrNone) - printf("Exerciser: success, new recordIdx=%ld\n", recordIdx); + printf("Exerciser: success, new recordIdx=%u\n", recordIdx); return err; } /* * at - add thread to record */ -static NuError -AddThreadFunc(ExerciserState* pState, int argc, char** argv) +static NuError AddThreadFunc(ExerciserState* pState, int argc, char** argv) { NuError err; - NuDataSource* pDataSource = nil; - char* lineBuf = nil; + NuDataSource* pDataSource = NULL; + char* lineBuf = NULL; long ourLen, maxLen; NuThreadID threadID; NuThreadIdx threadIdx; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 3); lineBuf = (char*)malloc(kNiceLineLen); - assert(lineBuf != nil); + assert(lineBuf != NULL); - threadID = strtol(argv[2], nil, 0); + threadID = strtol(argv[2], NULL, 0); if (NuThreadIDGetClass(threadID) == kNuThreadClassData) { /* load data from a file on disk */ maxLen = 0; @@ -470,7 +454,7 @@ AddThreadFunc(ExerciserState* pState, int argc, char** argv) err = GetLine("Enter max buffer size", lineBuf, kNiceLineLen); if (err != kNuErrNone) goto bail; - maxLen = strtol(lineBuf, nil, 0); + maxLen = strtol(lineBuf, NULL, 0); if (maxLen <= 0) { fprintf(stderr, "Bad length\n"); err = kNuErrInvalidArg; @@ -487,28 +471,28 @@ AddThreadFunc(ExerciserState* pState, int argc, char** argv) /* create a data source from the buffer */ err = NuCreateDataSourceForBuffer(kNuThreadFormatUncompressed, - maxLen, (unsigned char*)lineBuf, 0, ourLen, FreeCallback, + maxLen, (uint8_t*)lineBuf, 0, ourLen, FreeCallback, &pDataSource); if (err != kNuErrNone) { fprintf(stderr, "Exerciser: buffer data source create failed (err=%d)\n", err); goto bail; } - lineBuf = nil; /* now owned by the library */ + lineBuf = NULL; /* now owned by the library */ } err = NuAddThread(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0), threadID, pDataSource, &threadIdx); + strtol(argv[1], NULL, 0), threadID, pDataSource, &threadIdx); if (err == kNuErrNone) { - pDataSource = nil; /* library owns it now */ - printf("Exerciser: success; function returned threadIdx=%ld\n", + pDataSource = NULL; /* library owns it now */ + printf("Exerciser: success; function returned threadIdx=%u\n", threadIdx); } bail: NuFreeDataSource(pDataSource); - if (lineBuf != nil) + if (lineBuf != NULL) free(lineBuf); return err; } @@ -516,19 +500,18 @@ bail: /* * cl - close archive */ -static NuError -CloseFunc(ExerciserState* pState, int argc, char** argv) +static NuError CloseFunc(ExerciserState* pState, int argc, char** argv) { NuError err; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 1); err = NuClose(ExerciserState_GetNuArchive(pState)); if (err == kNuErrNone) { - ExerciserState_SetNuArchive(pState, nil); - ExerciserState_SetArchivePath(pState, nil); + ExerciserState_SetNuArchive(pState, NULL); + ExerciserState_SetArchivePath(pState, NULL); } return err; @@ -537,11 +520,10 @@ CloseFunc(ExerciserState* pState, int argc, char** argv) /* * d - delete all records (selection-filtered) */ -static NuError -DeleteFunc(ExerciserState* pState, int argc, char** argv) +static NuError DeleteFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 1); NuSetSelectionFilter(ExerciserState_GetNuArchive(pState), SelectionFilter); @@ -552,39 +534,36 @@ DeleteFunc(ExerciserState* pState, int argc, char** argv) /* * dr - delete record */ -static NuError -DeleteRecordFunc(ExerciserState* pState, int argc, char** argv) +static NuError DeleteRecordFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); return NuDeleteRecord(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0)); + strtol(argv[1], NULL, 0)); } /* * dt - delete thread */ -static NuError -DeleteThreadFunc(ExerciserState* pState, int argc, char** argv) +static NuError DeleteThreadFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); return NuDeleteThread(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0)); + strtol(argv[1], NULL, 0)); } /* * e - extract all files (selection-filtered) */ -static NuError -ExtractFunc(ExerciserState* pState, int argc, char** argv) +static NuError ExtractFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 1); NuSetSelectionFilter(ExerciserState_GetNuArchive(pState), SelectionFilter); @@ -595,28 +574,26 @@ ExtractFunc(ExerciserState* pState, int argc, char** argv) /* * er - extract record */ -static NuError -ExtractRecordFunc(ExerciserState* pState, int argc, char** argv) +static NuError ExtractRecordFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); return NuExtractRecord(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0)); + strtol(argv[1], NULL, 0)); } /* * et - extract thread */ -static NuError -ExtractThreadFunc(ExerciserState* pState, int argc, char** argv) +static NuError ExtractThreadFunc(ExerciserState* pState, int argc, char** argv) { NuError err; - NuDataSink* pDataSink = nil; + NuDataSink* pDataSink = NULL; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 3); err = NuCreateDataSinkForFile(true, kNuConvertOff, argv[2], kFssep, @@ -627,7 +604,7 @@ ExtractThreadFunc(ExerciserState* pState, int argc, char** argv) } err = NuExtractThread(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0), pDataSink); + strtol(argv[1], NULL, 0), pDataSink); /* fall through with err */ bail: @@ -638,19 +615,18 @@ bail: /* * fl - flush changes to archive */ -static NuError -FlushFunc(ExerciserState* pState, int argc, char** argv) +static NuError FlushFunc(ExerciserState* pState, int argc, char** argv) { NuError err; - long flushStatus; + uint32_t flushStatus; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 1); err = NuFlush(ExerciserState_GetNuArchive(pState), &flushStatus); if (err != kNuErrNone) - printf("Exerciser: flush failed, status flags=0x%04lx\n", flushStatus); + printf("Exerciser: flush failed, status flags=0x%04x\n", flushStatus); return err; } @@ -660,40 +636,39 @@ FlushFunc(ExerciserState* pState, int argc, char** argv) * Currently takes numeric arguments. We could be nice and accept the * things like "IgnoreCRC" for kNuValueIgnoreCRC, but not yet. */ -static NuError -GetValueFunc(ExerciserState* pState, int argc, char** argv) +static NuError GetValueFunc(ExerciserState* pState, int argc, char** argv) { NuError err; NuValue value; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); err = NuGetValue(ExerciserState_GetNuArchive(pState), - (NuValueID) strtol(argv[1], nil, 0), &value); + (NuValueID) strtol(argv[1], NULL, 0), &value); if (err == kNuErrNone) - printf(" --> %ld\n", value); + printf(" --> %u\n", value); return err; } /* * gmh - get master header */ -static NuError -GetMasterHeaderFunc(ExerciserState* pState, int argc, char** argv) +static NuError GetMasterHeaderFunc(ExerciserState* pState, int argc, + char** argv) { NuError err; const NuMasterHeader* pMasterHeader; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 1); err = NuGetMasterHeader(ExerciserState_GetNuArchive(pState), &pMasterHeader); if (err == kNuErrNone) { - printf("Exerciser: success (version=%u, totalRecords=%lu, EOF=%lu)\n", + printf("Exerciser: success (version=%u, totalRecords=%u, EOF=%u)\n", pMasterHeader->mhMasterVersion, pMasterHeader->mhTotalRecords, pMasterHeader->mhMasterEOF); } @@ -703,26 +678,25 @@ GetMasterHeaderFunc(ExerciserState* pState, int argc, char** argv) /* * gr - get record attributes */ -static NuError -GetRecordFunc(ExerciserState* pState, int argc, char** argv) +static NuError GetRecordFunc(ExerciserState* pState, int argc, char** argv) { NuError err; const NuRecord* pRecord; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); err = NuGetRecord(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0), &pRecord); + strtol(argv[1], NULL, 0), &pRecord); if (err == kNuErrNone) { printf("Exerciser: success, call returned:\n"); printf("\tfileSysID : %d\n", pRecord->recFileSysID); printf("\tfileSysInfo : 0x%04x ('%c')\n", pRecord->recFileSysInfo, NuGetSepFromSysInfo(pRecord->recFileSysInfo)); - printf("\taccess : 0x%02lx\n", pRecord->recAccess); - printf("\tfileType : 0x%04lx\n", pRecord->recFileType); - printf("\textraType : 0x%04lx\n", pRecord->recExtraType); + printf("\taccess : 0x%02x\n", pRecord->recAccess); + printf("\tfileType : 0x%04x\n", pRecord->recFileType); + printf("\textraType : 0x%04x\n", pRecord->recExtraType); printf("\tcreateWhen : ...\n"); printf("\tmodWhen : ...\n"); /* too lazy */ printf("\tarchiveWhen : ...\n"); @@ -733,53 +707,53 @@ GetRecordFunc(ExerciserState* pState, int argc, char** argv) /* * grin - get record idx by name */ -static NuError -GetRecordIdxByNameFunc(ExerciserState* pState, int argc, char** argv) +static NuError GetRecordIdxByNameFunc(ExerciserState* pState, int argc, + char** argv) { NuError err; NuRecordIdx recIdx; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); err = NuGetRecordIdxByName(ExerciserState_GetNuArchive(pState), argv[1], &recIdx); if (err == kNuErrNone) - printf("Exerciser: success, returned recordIdx=%ld\n", recIdx); + printf("Exerciser: success, returned recordIdx=%u\n", recIdx); return err; } /* * grip - get record idx by position */ -static NuError -GetRecordIdxByPositionFunc(ExerciserState* pState, int argc, char** argv) +static NuError GetRecordIdxByPositionFunc(ExerciserState* pState, int argc, + char** argv) { NuError err; NuRecordIdx recIdx; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); err = NuGetRecordIdxByPosition(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0), &recIdx); + strtol(argv[1], NULL, 0), &recIdx); if (err == kNuErrNone) - printf("Exerciser: success, returned recordIdx=%ld\n", recIdx); + printf("Exerciser: success, returned recordIdx=%u\n", recIdx); return err; } /* * ocrw - open/create read-write */ -static NuError -OpenCreateReadWriteFunc(ExerciserState* pState, int argc, char** argv) +static NuError OpenCreateReadWriteFunc(ExerciserState* pState, int argc, + char** argv) { NuError err; NuArchive* pArchive; - assert(ExerciserState_GetNuArchive(pState) == nil); + assert(ExerciserState_GetNuArchive(pState) == NULL); assert(argc == 2); err = NuOpenRW(argv[1], kTempFile, kNuOpenCreat|kNuOpenExcl, &pArchive); @@ -794,13 +768,12 @@ OpenCreateReadWriteFunc(ExerciserState* pState, int argc, char** argv) /* * oro - open read-only */ -static NuError -OpenReadOnlyFunc(ExerciserState* pState, int argc, char** argv) +static NuError OpenReadOnlyFunc(ExerciserState* pState, int argc, char** argv) { NuError err; NuArchive* pArchive; - assert(ExerciserState_GetNuArchive(pState) == nil); + assert(ExerciserState_GetNuArchive(pState) == NULL); assert(argc == 2); err = NuOpenRO(argv[1], &pArchive); @@ -815,17 +788,17 @@ OpenReadOnlyFunc(ExerciserState* pState, int argc, char** argv) /* * ors - open streaming read-only */ -static NuError -OpenStreamingReadOnlyFunc(ExerciserState* pState, int argc, char** argv) +static NuError OpenStreamingReadOnlyFunc(ExerciserState* pState, int argc, + char** argv) { NuError err; NuArchive* pArchive; - FILE* fp = nil; + FILE* fp = NULL; - assert(ExerciserState_GetNuArchive(pState) == nil); + assert(ExerciserState_GetNuArchive(pState) == NULL); assert(argc == 2); - if ((fp = fopen(argv[1], kNuFileOpenReadOnly)) == nil) { + if ((fp = fopen(argv[1], kNuFileOpenReadOnly)) == NULL) { err = errno ? (NuError)errno : kNuErrGeneric; fprintf(stderr, "Exerciser: unable to open '%s'\n", argv[1]); } else { @@ -833,11 +806,11 @@ OpenStreamingReadOnlyFunc(ExerciserState* pState, int argc, char** argv) if (err == kNuErrNone) { ExerciserState_SetNuArchive(pState, pArchive); ExerciserState_SetArchivePath(pState, argv[1]); - fp = nil; + fp = NULL; } } - if (fp != nil) + if (fp != NULL) fclose(fp); return err; @@ -846,13 +819,12 @@ OpenStreamingReadOnlyFunc(ExerciserState* pState, int argc, char** argv) /* * orw - open read-write */ -static NuError -OpenReadWriteFunc(ExerciserState* pState, int argc, char** argv) +static NuError OpenReadWriteFunc(ExerciserState* pState, int argc, char** argv) { NuError err; NuArchive* pArchive; - assert(ExerciserState_GetNuArchive(pState) == nil); + assert(ExerciserState_GetNuArchive(pState) == NULL); assert(argc == 2); err = NuOpenRW(argv[1], kTempFile, 0, &pArchive); @@ -867,11 +839,10 @@ OpenReadWriteFunc(ExerciserState* pState, int argc, char** argv) /* * p - print */ -static NuError -PrintFunc(ExerciserState* pState, int argc, char** argv) +static NuError PrintFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 1); return NuContents(ExerciserState_GetNuArchive(pState), PrintEntry); @@ -880,11 +851,10 @@ PrintFunc(ExerciserState* pState, int argc, char** argv) /* * pd - print debug */ -static NuError -PrintDebugFunc(ExerciserState* pState, int argc, char** argv) +static NuError PrintDebugFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 1); return NuDebugDumpArchive(ExerciserState_GetNuArchive(pState)); @@ -893,15 +863,14 @@ PrintDebugFunc(ExerciserState* pState, int argc, char** argv) /* * re - rename record */ -static NuError -RenameFunc(ExerciserState* pState, int argc, char** argv) +static NuError RenameFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 4); return NuRename(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0), argv[2], argv[3][0]); + strtol(argv[1], NULL, 0), argv[2], argv[3][0]); } /* @@ -909,11 +878,11 @@ RenameFunc(ExerciserState* pState, int argc, char** argv) * * Use an error handler callback. */ -static NuError -SetErrorCallbackFunc(ExerciserState* pState, int argc, char** argv) +static NuError SetErrorCallbackFunc(ExerciserState* pState, int argc, + char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 1); NuSetErrorHandler(ExerciserState_GetNuArchive(pState), ErrorHandler); @@ -925,15 +894,14 @@ SetErrorCallbackFunc(ExerciserState* pState, int argc, char** argv) * * Currently takes numeric arguments. */ -static NuError -SetValueFunc(ExerciserState* pState, int argc, char** argv) +static NuError SetValueFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 3); return NuSetValue(ExerciserState_GetNuArchive(pState), - (NuValueID) strtol(argv[1], nil, 0), strtol(argv[2], nil, 0)); + (NuValueID) strtol(argv[1], NULL, 0), strtol(argv[2], NULL, 0)); } /* @@ -944,38 +912,36 @@ SetValueFunc(ExerciserState* pState, int argc, char** argv) * rigid notion of how many arguments each function should have, so * you'd need to list all of them every time. */ -static NuError -SetRecordAttrFunc(ExerciserState* pState, int argc, char** argv) +static NuError SetRecordAttrFunc(ExerciserState* pState, int argc, char** argv) { NuError err; const NuRecord* pRecord; NuRecordAttr recordAttr; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 4); err = NuGetRecord(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0), &pRecord); + strtol(argv[1], NULL, 0), &pRecord); if (err != kNuErrNone) return err; printf("Exerciser: NuGetRecord succeeded, calling NuSetRecordAttr\n"); NuRecordCopyAttr(&recordAttr, pRecord); - recordAttr.fileType = strtol(argv[2], nil, 0); - recordAttr.extraType = strtol(argv[3], nil, 0); + recordAttr.fileType = strtol(argv[2], NULL, 0); + recordAttr.extraType = strtol(argv[3], NULL, 0); /*recordAttr.fileSysInfo = ':';*/ return NuSetRecordAttr(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0), &recordAttr); + strtol(argv[1], NULL, 0), &recordAttr); } /* * t - test archive */ -static NuError -TestFunc(ExerciserState* pState, int argc, char** argv) +static NuError TestFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 1); return NuTest(ExerciserState_GetNuArchive(pState)); @@ -984,34 +950,34 @@ TestFunc(ExerciserState* pState, int argc, char** argv) /* * tr - test record */ -static NuError -TestRecordFunc(ExerciserState* pState, int argc, char** argv) +static NuError TestRecordFunc(ExerciserState* pState, int argc, char** argv) { (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); return NuTestRecord(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0)); + strtol(argv[1], NULL, 0)); } /* * upt - update pre-sized thread */ -static NuError -UpdatePresizedThreadFunc(ExerciserState* pState, int argc, char** argv) +static NuError UpdatePresizedThreadFunc(ExerciserState* pState, int argc, + char** argv) { NuError err; - NuDataSource* pDataSource = nil; - char* lineBuf = nil; - long ourLen, maxLen; + NuDataSource* pDataSource = NULL; + char* lineBuf = NULL; + long ourLen; + int32_t maxLen; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ - assert(ExerciserState_GetNuArchive(pState) != nil); + assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); lineBuf = (char*)malloc(kNiceLineLen); - assert(lineBuf != nil); + assert(lineBuf != NULL); err = GetLine("Enter data for thread", lineBuf, kNiceLineLen); if (err != kNuErrNone) goto bail; @@ -1020,23 +986,23 @@ UpdatePresizedThreadFunc(ExerciserState* pState, int argc, char** argv) /* use "ourLen" for both buffer len and data len */ err = NuCreateDataSourceForBuffer(kNuThreadFormatUncompressed, - ourLen, (unsigned char*)lineBuf, 0, ourLen, FreeCallback, + ourLen, (uint8_t*)lineBuf, 0, ourLen, FreeCallback, &pDataSource); if (err != kNuErrNone) { fprintf(stderr, "Exerciser: data source create failed (err=%d)\n", err); goto bail; } - lineBuf = nil; /* now owned by the library */ + lineBuf = NULL; /* now owned by the library */ err = NuUpdatePresizedThread(ExerciserState_GetNuArchive(pState), - strtol(argv[1], nil, 0), pDataSource, &maxLen); + strtol(argv[1], NULL, 0), pDataSource, &maxLen); if (err == kNuErrNone) - printf("Exerciser: success; function returned maxLen=%ld\n", maxLen); + printf("Exerciser: success; function returned maxLen=%d\n", maxLen); bail: NuFreeDataSource(pDataSource); - if (lineBuf != nil) + if (lineBuf != NULL) free(lineBuf); return err; } @@ -1056,7 +1022,7 @@ static const struct { CommandFunc func; int expectedArgCount; const char* argumentList; - unsigned long flags; + uint32_t flags; const char* description; } gCommandTable[] = { { "--- exerciser commands ---", HelpFunc, 0, "", 0, @@ -1138,8 +1104,7 @@ static const struct { /* * Display a summary of available commands. */ -static NuError -HelpFunc(ExerciserState* pState, int argc, char** argv) +static NuError HelpFunc(ExerciserState* pState, int argc, char** argv) { int i; @@ -1171,9 +1136,8 @@ static const char* kWhitespace = " \t\n"; * "lineBuf" will be mangled. On success, "pFunc", "pArgc", and "pArgv" * will receive the results. */ -static NuError -ParseLine(char* lineBuf, ExerciserState* pState, CommandFunc* pFunc, int* pArgc, - char*** pArgv) +static NuError ParseLine(char* lineBuf, ExerciserState* pState, + CommandFunc* pFunc, int* pArgc, char*** pArgv) { NuError err = kNuErrSyntax; char* command; @@ -1185,22 +1149,22 @@ ParseLine(char* lineBuf, ExerciserState* pState, CommandFunc* pFunc, int* pArgc, */ command = strtok(lineBuf, kWhitespace); - if (command == nil) { + if (command == NULL) { /* no command; the user probably just hit "enter" on a blank line */ *pFunc = NothingFunc; *pArgc = 0; - *pArgv = nil; + *pArgv = NULL; err = kNuErrNone; goto bail; } - /* no real need to be flexible; add 1 for command and one for nil */ + /* no real need to be flexible; add 1 for command and one for NULL */ *pArgv = (char**) malloc(sizeof(char*) * (kMaxArgs+2)); (*pArgv)[0] = command; *pArgc = 1; - cp = strtok(nil, kWhitespace); - while (cp != nil) { + cp = strtok(NULL, kWhitespace); + while (cp != NULL) { if (*pArgc >= kMaxArgs+1) { printf("ERROR: too many arguments\n"); goto bail; @@ -1208,10 +1172,10 @@ ParseLine(char* lineBuf, ExerciserState* pState, CommandFunc* pFunc, int* pArgc, (*pArgv)[*pArgc] = cp; (*pArgc)++; - cp = strtok(nil, kWhitespace); + cp = strtok(NULL, kWhitespace); } assert(*pArgc < kMaxArgs+2); - (*pArgv)[*pArgc] = nil; + (*pArgv)[*pArgc] = NULL; /* * Look up the command. @@ -1237,13 +1201,13 @@ ParseLine(char* lineBuf, ExerciserState* pState, CommandFunc* pFunc, int* pArgc, } if (gCommandTable[i].flags & kFlagArchiveReq) { - if (ExerciserState_GetNuArchive(pState) == nil) { + if (ExerciserState_GetNuArchive(pState) == NULL) { printf("ERROR: must have an archive open\n"); goto bail; } } if (gCommandTable[i].flags & kFlagNoArchiveReq) { - if (ExerciserState_GetNuArchive(pState) != nil) { + if (ExerciserState_GetNuArchive(pState) != NULL) { printf("ERROR: an archive is already open\n"); goto bail; } @@ -1259,41 +1223,39 @@ bail: } - /* * Interpret commands, do clever things. */ -static NuError -CommandLoop(void) +static NuError CommandLoop(void) { NuError err = kNuErrNone; ExerciserState* pState = ExerciserState_New(); CommandFunc func; char lineBuf[128]; int argc; - char** argv = nil; + char** argv = NULL; while (1) { printf("\nEnter command (%s)> ", ExerciserState_GetArchiveFile(pState)); fflush(stdout); - if (fgets(lineBuf, sizeof(lineBuf), stdin) == nil) { + if (fgets(lineBuf, sizeof(lineBuf), stdin) == NULL) { printf("\n"); break; } - if (argv != nil) { + if (argv != NULL) { free(argv); - argv = nil; + argv = NULL; } - func = nil; /* sanity check */ + func = NULL; /* sanity check */ err = ParseLine(lineBuf, pState, &func, &argc, &argv); if (err != kNuErrNone) continue; - assert(func != nil); + assert(func != NULL); if (func == QuitFunc) break; @@ -1304,13 +1266,13 @@ CommandLoop(void) else if (err > 0) printf("Exerciser: received error %d\n", err); - if (argv != nil) { + if (argv != NULL) { free(argv); - argv = nil; + argv = NULL; } } - if (ExerciserState_GetNuArchive(pState) != nil) { + if (ExerciserState_GetNuArchive(pState) != NULL) { /* ought to query the archive before saying something like this... */ printf("Exerciser: aborting any un-flushed changes in archive %s\n", ExerciserState_GetArchivePath(pState)); @@ -1318,12 +1280,12 @@ CommandLoop(void) err = NuClose(ExerciserState_GetNuArchive(pState)); if (err != kNuErrNone) printf("Exerciser: got error %d closing archive\n", err); - ExerciserState_SetNuArchive(pState, nil); + ExerciserState_SetNuArchive(pState, NULL); } - if (pState != nil) + if (pState != NULL) ExerciserState_Free(pState); - if (argv != nil) + if (argv != NULL) free(argv); return kNuErrNone; } @@ -1334,17 +1296,16 @@ CommandLoop(void) * * We don't currently take any arguments, so this is pretty straightforward. */ -int -main(void) +int main(void) { NuError result; - long majorVersion, minorVersion, bugVersion; + int32_t majorVersion, minorVersion, bugVersion; const char* nufxLibDate; const char* nufxLibFlags; (void) NuGetVersion(&majorVersion, &minorVersion, &bugVersion, &nufxLibDate, &nufxLibFlags); - printf("NufxLib exerciser, linked with NufxLib v%ld.%ld.%ld [%s]\n\n", + printf("NufxLib exerciser, linked with NufxLib v%d.%d.%d [%s]\n\n", majorVersion, minorVersion, bugVersion, nufxLibFlags); printf("Use 'h' or '?' for help, 'q' to quit.\n"); @@ -1371,7 +1332,7 @@ main(void) */ { char* debugSet = getenv("MALLOC_CHECK_"); - if (debugSet == nil) + if (debugSet == NULL) printf("WARNING: MALLOC_CHECK_ not enabled\n\n"); } #endif diff --git a/nufxlib/samples/ImgConv.c b/nufxlib/samples/ImgConv.c index a697b60..620b827 100644 --- a/nufxlib/samples/ImgConv.c +++ b/nufxlib/samples/ImgConv.c @@ -51,35 +51,33 @@ strcasecmp(const char *str1, const char *str2) typedef struct ImgHeader { char magic[4]; char creator[4]; - short headerLen; - short version; - long imageFormat; - unsigned long flags; - long numBlocks; - long dataOffset; - long dataLen; - long cmntOffset; - long cmntLen; - long creatorOffset; - long creatorLen; - long spare[4]; + uint16_t headerLen; + uint16_t version; + uint32_t imageFormat; + uint32_t flags; + uint32_t numBlocks; + uint32_t dataOffset; + uint32_t dataLen; + uint32_t cmntOffset; + uint32_t cmntLen; + uint32_t creatorOffset; + uint32_t creatorLen; + uint32_t spare[4]; } ImgHeader; /* * Read a two-byte little-endian value. */ -void -ReadShortLE(FILE* fp, short* pBuf) +void ReadShortLE(FILE* fp, uint16_t* pBuf) { *pBuf = getc(fp); - *pBuf += (short) getc(fp) << 8; + *pBuf += (uint16_t) getc(fp) << 8; } /* * Write a two-byte little-endian value. */ -void -WriteShortLE(FILE* fp, unsigned short val) +void WriteShortLE(FILE* fp, uint16_t val) { putc(val, fp); putc(val >> 8, fp); @@ -88,20 +86,18 @@ WriteShortLE(FILE* fp, unsigned short val) /* * Read a four-byte little-endian value. */ -void -ReadLongLE(FILE* fp, long* pBuf) +void ReadLongLE(FILE* fp, uint32_t* pBuf) { *pBuf = getc(fp); - *pBuf += (long) getc(fp) << 8; - *pBuf += (long) getc(fp) << 16; - *pBuf += (long) getc(fp) << 24; + *pBuf += (uint32_t) getc(fp) << 8; + *pBuf += (uint32_t) getc(fp) << 16; + *pBuf += (uint32_t) getc(fp) << 24; } /* * Write a four-byte little-endian value. */ -void -WriteLongLE(FILE* fp, unsigned long val) +void WriteLongLE(FILE* fp, uint32_t val) { putc(val, fp); putc(val >> 8, fp); @@ -112,8 +108,7 @@ WriteLongLE(FILE* fp, unsigned long val) /* * Read the header from a 2IMG file. */ -int -ReadImgHeader(FILE* fp, ImgHeader* pHeader) +int ReadImgHeader(FILE* fp, ImgHeader* pHeader) { size_t ignored; ignored = fread(pHeader->magic, 4, 1, fp); @@ -121,7 +116,7 @@ ReadImgHeader(FILE* fp, ImgHeader* pHeader) ReadShortLE(fp, &pHeader->headerLen); ReadShortLE(fp, &pHeader->version); ReadLongLE(fp, &pHeader->imageFormat); - ReadLongLE(fp, (long*)&pHeader->flags); + ReadLongLE(fp, &pHeader->flags); ReadLongLE(fp, &pHeader->numBlocks); ReadLongLE(fp, &pHeader->dataOffset); ReadLongLE(fp, &pHeader->dataLen); @@ -154,8 +149,7 @@ ReadImgHeader(FILE* fp, ImgHeader* pHeader) /* * Write the header to a 2IMG file. */ -int -WriteImgHeader(FILE* fp, ImgHeader* pHeader) +int WriteImgHeader(FILE* fp, ImgHeader* pHeader) { fwrite(pHeader->magic, 4, 1, fp); fwrite(pHeader->creator, 4, 1, fp); @@ -185,23 +179,22 @@ WriteImgHeader(FILE* fp, ImgHeader* pHeader) /* * Dump the contents of an ImgHeader. */ -void -DumpImgHeader(ImgHeader* pHeader) +void DumpImgHeader(ImgHeader* pHeader) { printf("--- header contents:\n"); printf("\tmagic = '%.4s'\n", pHeader->magic); printf("\tcreator = '%.4s'\n", pHeader->creator); printf("\theaderLen = %d\n", pHeader->headerLen); printf("\tversion = %d\n", pHeader->version); - printf("\timageFormat = %ld\n", pHeader->imageFormat); - printf("\tflags = 0x%08lx\n", pHeader->flags); - printf("\tnumBlocks = %ld\n", pHeader->numBlocks); - printf("\tdataOffset = %ld\n", pHeader->dataOffset); - printf("\tdataLen = %ld\n", pHeader->dataLen); - printf("\tcmntOffset = %ld\n", pHeader->cmntOffset); - printf("\tcmntLen = %ld\n", pHeader->cmntLen); - printf("\tcreatorOffset = %ld\n", pHeader->creatorOffset); - printf("\tcreatorLen = %ld\n", pHeader->creatorLen); + printf("\timageFormat = %u\n", pHeader->imageFormat); + printf("\tflags = 0x%08x\n", pHeader->flags); + printf("\tnumBlocks = %u\n", pHeader->numBlocks); + printf("\tdataOffset = %u\n", pHeader->dataOffset); + printf("\tdataLen = %u\n", pHeader->dataLen); + printf("\tcmntOffset = %u\n", pHeader->cmntOffset); + printf("\tcmntLen = %u\n", pHeader->cmntLen); + printf("\tcreatorOffset = %u\n", pHeader->creatorOffset); + printf("\tcreatorLen = %u\n", pHeader->creatorLen); printf("\n"); } @@ -217,8 +210,7 @@ typedef enum ArchiveKind { kKindUnknown, kKindShk, kKindImg } ArchiveKind; /* * This gets called when a buffer DataSource is no longer needed. */ -NuResult -FreeCallback(NuArchive* pArchive, void* args) +NuResult FreeCallback(NuArchive* pArchive, void* args) { free(args); return kNuOK; @@ -227,8 +219,7 @@ FreeCallback(NuArchive* pArchive, void* args) /* * This gets called when an "FP" DataSource is no longer needed. */ -NuResult -FcloseCallback(NuArchive* pArchive, void* args) +NuResult FcloseCallback(NuArchive* pArchive, void* args) { fclose((FILE*) args); return kNuOK; @@ -242,8 +233,7 @@ FcloseCallback(NuArchive* pArchive, void* args) * of NufxLib. We could just as easily not set it and call fclose() * ourselves, because the structure of this program is pretty simple. */ -NuError -CreateProdosSource(const ImgHeader* pHeader, FILE* fp, +NuError CreateProdosSource(const ImgHeader* pHeader, FILE* fp, NuDataSource** ppDataSource) { return NuCreateDataSourceForFP(kNuThreadFormatUncompressed, 0, fp, @@ -254,17 +244,16 @@ CreateProdosSource(const ImgHeader* pHeader, FILE* fp, * Create a data source for a DOS-ordered image. This is a little harder, * since we have to reorder the blocks into ProDOS ordering for ShrinkIt. */ -NuError -CreateDosSource(const ImgHeader* pHeader, FILE* fp, +NuError CreateDosSource(const ImgHeader* pHeader, FILE* fp, NuDataSource** ppDataSource) { NuError err; - char* diskBuffer = nil; + char* diskBuffer = NULL; long offset; if (pHeader->dataLen % 4096) { fprintf(stderr, - "ERROR: image size must be multiple of 4096 (%ld isn't)\n", + "ERROR: image size must be multiple of 4096 (%u isn't)\n", pHeader->dataLen); err = kNuErrGeneric; goto bail; @@ -277,8 +266,8 @@ CreateDosSource(const ImgHeader* pHeader, FILE* fp, } diskBuffer = malloc(pHeader->dataLen); - if (diskBuffer == nil) { - fprintf(stderr, "ERROR: malloc(%ld) failed\n", pHeader->dataLen); + if (diskBuffer == NULL) { + fprintf(stderr, "ERROR: malloc(%u) failed\n", pHeader->dataLen); err = kNuErrMalloc; goto bail; } @@ -319,13 +308,13 @@ CreateDosSource(const ImgHeader* pHeader, FILE* fp, * "true", so NufxLib will free the buffer for us. */ err = NuCreateDataSourceForBuffer(kNuThreadFormatUncompressed, 0, - (const unsigned char*) diskBuffer, 0, pHeader->dataLen, + (const uint8_t*) diskBuffer, 0, pHeader->dataLen, FreeCallback, ppDataSource); if (err == kNuErrNone) - diskBuffer = nil; + diskBuffer = NULL; bail: - if (diskBuffer != nil) + if (diskBuffer != NULL) free(diskBuffer); return err; } @@ -338,18 +327,17 @@ bail: * This requires opening up the 2IMG file, verifying that it's okay, and * then creating a new disk image record and thread. */ -int -ConvertFromImgToShk(const char* srcName, const char* dstName) +int ConvertFromImgToShk(const char* srcName, const char* dstName) { NuError err; - NuArchive* pArchive = nil; - NuDataSource* pDataSource = nil; + NuArchive* pArchive = NULL; + NuDataSource* pDataSource = NULL; NuRecordIdx recordIdx; NuFileDetails fileDetails; ImgHeader header; - FILE* fp = nil; - long flushStatus; - char* storageName = nil; + FILE* fp = NULL; + uint32_t flushStatus; + char* storageName = NULL; char* cp; printf("Converting 2IMG file '%s' to ShrinkIt archive '%s'\n\n", @@ -394,10 +382,10 @@ ConvertFromImgToShk(const char* srcName, const char* dstName) /* create the name that will be stored in the archive */ storageName = strdup(dstName); cp = strrchr(storageName, '.'); - if (cp != nil) + if (cp != NULL) *cp = '\0'; cp = strrchr(storageName, kLocalFssep); - if (cp != nil && *(cp+1) != '\0') + if (cp != NULL && *(cp+1) != '\0') cp++; else cp = storageName; @@ -410,7 +398,7 @@ ConvertFromImgToShk(const char* srcName, const char* dstName) /* set up the contents of the NuFX Record */ memset(&fileDetails, 0, sizeof(fileDetails)); - fileDetails.storageName = cp; + fileDetails.storageNameMOR = cp; fileDetails.fileSysID = kNuFileSysUnknown; /* DOS? ProDOS? */ fileDetails.fileSysInfo = kLocalFssep; fileDetails.access = kNuAccessUnlocked; @@ -433,11 +421,11 @@ ConvertFromImgToShk(const char* srcName, const char* dstName) switch (header.imageFormat) { case kImageFormatDOS: err = CreateDosSource(&header, fp, &pDataSource); - fp = nil; + fp = NULL; break; case kImageFormatProDOS: err = CreateProdosSource(&header, fp, &pDataSource); - fp = nil; + fp = NULL; break; default: fprintf(stderr, "How the heck did I get here?"); @@ -451,17 +439,17 @@ ConvertFromImgToShk(const char* srcName, const char* dstName) /* add a disk image thread */ err = NuAddThread(pArchive, recordIdx, kNuThreadIDDiskImage, pDataSource, - nil); + NULL); if (err != kNuErrNone) { fprintf(stderr, "ERROR: unable to create thread (err=%d)\n", err); goto bail; } - pDataSource = nil; /* library owns it now */ + pDataSource = NULL; /* library owns it now */ /* nothing happens until we Flush */ err = NuFlush(pArchive, &flushStatus); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: flush failed (err=%d, status=0x%04lx)\n", + fprintf(stderr, "ERROR: flush failed (err=%d, status=0x%04x)\n", err, flushStatus); goto bail; } @@ -470,17 +458,17 @@ ConvertFromImgToShk(const char* srcName, const char* dstName) fprintf(stderr, "ERROR: close failed (err=%d)\n", err); goto bail; } - pArchive = nil; + pArchive = NULL; bail: - if (pArchive != nil) { + if (pArchive != NULL) { (void)NuAbort(pArchive); (void)NuClose(pArchive); } NuFreeDataSource(pDataSource); - if (storageName != nil) + if (storageName != NULL) free(storageName); - if (fp != nil) + if (fp != NULL) fclose(fp); return (err == kNuErrNone) ? 0 : -1; } @@ -492,17 +480,16 @@ bail: * This takes a simple-minded approach and assumes that the first record * in the archive has the disk image in it. If it doesn't, we give up. */ -int -ConvertFromShkToImg(const char* srcName, const char* dstName) +int ConvertFromShkToImg(const char* srcName, const char* dstName) { NuError err; - NuArchive* pArchive = nil; - NuDataSink* pDataSink = nil; + NuArchive* pArchive = NULL; + NuDataSink* pDataSink = NULL; NuRecordIdx recordIdx; const NuRecord* pRecord; - const NuThread* pThread = nil; + const NuThread* pThread = NULL; ImgHeader header; - FILE* fp = nil; + FILE* fp = NULL; int idx; printf("Converting ShrinkIt archive '%s' to 2IMG file '%s'\n\n", @@ -592,10 +579,10 @@ ConvertFromShkToImg(const char* srcName, const char* dstName) } bail: - if (pArchive != nil) + if (pArchive != NULL) NuClose(pArchive); NuFreeDataSink(pDataSink); - if (fp != nil) + if (fp != NULL) fclose(fp); return (err == kNuErrNone) ? 0 : -1; } @@ -604,13 +591,12 @@ bail: /* * Figure out what kind of archive this is by looking at the filename. */ -ArchiveKind -DetermineKind(const char* filename) +ArchiveKind DetermineKind(const char* filename) { const char* dot; dot = strrchr(filename, '.'); - if (dot == nil) + if (dot == NULL) return kKindUnknown; if (strcasecmp(dot, ".shk") == 0 || strcasecmp(dot, ".sdk") == 0) @@ -626,8 +612,7 @@ DetermineKind(const char* filename) /* * Figure out what we want to do. */ -int -main(int argc, char** argv) +int main(int argc, char** argv) { ArchiveKind kind; int cc; diff --git a/nufxlib/samples/Launder.c b/nufxlib/samples/Launder.c index bc0f525..fba4913 100644 --- a/nufxlib/samples/Launder.c +++ b/nufxlib/samples/Launder.c @@ -47,8 +47,7 @@ char gSentRecordWarning = false; /* * This gets called when a buffer DataSource is no longer needed. */ -NuResult -FreeCallback(NuArchive* pArchive, void* args) +NuResult FreeCallback(NuArchive* pArchive, void* args) { free(args); return kNuOK; @@ -60,14 +59,13 @@ FreeCallback(NuArchive* pArchive, void* args) * This assumes the library is configured for compression (it defaults * to LZW/2, so this is a reasonable assumption). */ -NuError -CopyThreadRecompressed(NuArchive* pInArchive, NuArchive* pOutArchive, +NuError CopyThreadRecompressed(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, const NuThread* pThread, long newRecordIdx) { NuError err = kNuErrNone; - NuDataSource* pDataSource = nil; - NuDataSink* pDataSink = nil; - uchar* buffer = nil; + NuDataSource* pDataSource = NULL; + NuDataSink* pDataSink = NULL; + uint8_t* buffer = NULL; /* * Allocate a buffer large enough to hold all the uncompressed data, and @@ -77,7 +75,7 @@ CopyThreadRecompressed(NuArchive* pInArchive, NuArchive* pOutArchive, */ if (pThread->actualThreadEOF) { buffer = malloc(pThread->actualThreadEOF); - if (buffer == nil) { + if (buffer == NULL) { err = kNuErrMalloc; goto bail; } @@ -95,7 +93,7 @@ CopyThreadRecompressed(NuArchive* pInArchive, NuArchive* pOutArchive, */ err = NuExtractThread(pInArchive, pThread->threadIdx, pDataSink); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: unable to extract thread %ld (err=%d)\n", + fprintf(stderr, "ERROR: unable to extract thread %u (err=%d)\n", pThread->threadIdx, err); goto bail; } @@ -111,7 +109,7 @@ CopyThreadRecompressed(NuArchive* pInArchive, NuArchive* pOutArchive, * We always use "actualThreadEOF" because "thThreadEOF" is broken * for disk archives created by certain versions of ShrinkIt. * - * It's okay to pass in a nil value for "buffer", so long as the + * It's okay to pass in a NULL value for "buffer", so long as the * amount of data in the buffer is also zero. The library will do * the right thing. */ @@ -134,25 +132,25 @@ CopyThreadRecompressed(NuArchive* pInArchive, NuArchive* pOutArchive, goto bail; } } - buffer = nil; /* doClose was set, so it's owned by the data source */ + buffer = NULL; /* doClose was set, so it's owned by the data source */ /* * Schedule the data for addition to the record. */ err = NuAddThread(pOutArchive, newRecordIdx, NuGetThreadID(pThread), - pDataSource, nil); + pDataSource, NULL); if (err != kNuErrNone) { fprintf(stderr, "ERROR: unable to add thread (err=%d)\n", err); goto bail; } - pDataSource = nil; /* library owns it now */ + pDataSource = NULL; /* library owns it now */ bail: - if (pDataSource != nil) + if (pDataSource != NULL) NuFreeDataSource(pDataSource); - if (pDataSink != nil) + if (pDataSink != NULL) NuFreeDataSink(pDataSink); - if (buffer != nil) + if (buffer != NULL) free(buffer); return err; } @@ -174,14 +172,13 @@ bail: * reliable but extracts a little more than we need on pre-sized * threads (filenames, comments). */ -NuError -CopyThreadUncompressed(NuArchive* pInArchive, NuArchive* pOutArchive, +NuError CopyThreadUncompressed(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, const NuThread* pThread, long newRecordIdx) { NuError err = kNuErrNone; - NuDataSource* pDataSource = nil; - NuDataSink* pDataSink = nil; - uchar* buffer = nil; + NuDataSource* pDataSource = NULL; + NuDataSink* pDataSink = NULL; + uint8_t* buffer = NULL; /* * If we have some data files that were left uncompressed, perhaps @@ -207,7 +204,7 @@ CopyThreadUncompressed(NuArchive* pInArchive, NuArchive* pOutArchive, * wrap a data sink around it. */ buffer = malloc(pThread->thCompThreadEOF); - if (buffer == nil) { + if (buffer == NULL) { err = kNuErrMalloc; goto bail; } @@ -225,7 +222,7 @@ CopyThreadUncompressed(NuArchive* pInArchive, NuArchive* pOutArchive, */ err = NuExtractThread(pInArchive, pThread->threadIdx, pDataSink); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: unable to extract thread %ld (err=%d)\n", + fprintf(stderr, "ERROR: unable to extract thread %u (err=%d)\n", pThread->threadIdx, err); goto bail; } @@ -265,7 +262,7 @@ CopyThreadUncompressed(NuArchive* pInArchive, NuArchive* pOutArchive, goto bail; } } - buffer = nil; /* doClose was set, so it's owned by the data source */ + buffer = NULL; /* doClose was set, so it's owned by the data source */ /* yes, this is a kluge... sigh */ err = NuDataSourceSetRawCrc(pDataSource, pThread->thThreadCRC); @@ -281,19 +278,19 @@ CopyThreadUncompressed(NuArchive* pInArchive, NuArchive* pOutArchive, * "doClose" on our copy, so we are free to dispose of pDataSource. */ err = NuAddThread(pOutArchive, newRecordIdx, NuGetThreadID(pThread), - pDataSource, nil); + pDataSource, NULL); if (err != kNuErrNone) { fprintf(stderr, "ERROR: unable to add thread (err=%d)\n", err); goto bail; } - pDataSource = nil; /* library owns it now */ + pDataSource = NULL; /* library owns it now */ bail: - if (pDataSource != nil) + if (pDataSource != NULL) NuFreeDataSource(pDataSource); - if (pDataSink != nil) + if (pDataSink != NULL) NuFreeDataSink(pDataSink); - if (buffer != nil) + if (buffer != NULL) free(buffer); return err; } @@ -305,8 +302,7 @@ bail: * Depending on "flags", this will either copy it raw or uncompress and * recompress. */ -NuError -CopyThread(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, +NuError CopyThread(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, const NuThread* pThread, long newRecordIdx) { if (flags & kFlagCopyOnly) { @@ -327,8 +323,7 @@ CopyThread(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, * of which will not usually have any effect since NufxLib imposes a * specific thread ordering on most common types) depending on "flags". */ -NuError -CopyRecord(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, +NuError CopyRecord(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, NuRecordIdx recordIdx) { NuError err = kNuErrNone; @@ -344,7 +339,7 @@ CopyRecord(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, */ err = NuGetRecord(pInArchive, recordIdx, &pRecord); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: unable to get recordIdx %ld\n", recordIdx); + fprintf(stderr, "ERROR: unable to get recordIdx %u\n", recordIdx); goto bail; } @@ -362,7 +357,7 @@ CopyRecord(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, numThreads = NuRecordGetNumThreads(pRecord); if (!numThreads) { - fprintf(stderr, "WARNING: recordIdx=%ld was empty\n", recordIdx); + fprintf(stderr, "WARNING: recordIdx=%u was empty\n", recordIdx); goto bail; } @@ -370,7 +365,7 @@ CopyRecord(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, * Create a new record that looks just like the original. */ memset(&fileDetails, 0, sizeof(fileDetails)); - fileDetails.storageName = pRecord->filename; + fileDetails.storageNameMOR = pRecord->filenameMOR; fileDetails.fileSysID = pRecord->recFileSysID; fileDetails.fileSysInfo = pRecord->recFileSysInfo; fileDetails.access = pRecord->recAccess; @@ -393,7 +388,7 @@ CopyRecord(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, if (flags & kFlagReverseThreads) { for (idx = numThreads-1; idx >= 0; idx--) { pThread = NuGetThread(pRecord, idx); - assert(pThread != nil); + assert(pThread != NULL); err = CopyThread(pInArchive, pOutArchive, flags, pThread, newRecordIdx); @@ -403,7 +398,7 @@ CopyRecord(NuArchive* pInArchive, NuArchive* pOutArchive, long flags, } else { for (idx = 0; idx < numThreads; idx++) { pThread = NuGetThread(pRecord, idx); - assert(pThread != nil); + assert(pThread != NULL); err = CopyThread(pInArchive, pOutArchive, flags, pThread, newRecordIdx); @@ -422,16 +417,15 @@ bail: * * Returns 0 on success, nonzero on failure. */ -int -LaunderArchive(const char* inFile, const char* outFile, NuValue compressMethod, - long flags) +int LaunderArchive(const char* inFile, const char* outFile, + NuValue compressMethod, long flags) { NuError err = kNuErrNone; - NuArchive* pInArchive = nil; - NuArchive* pOutArchive = nil; + NuArchive* pInArchive = NULL; + NuArchive* pOutArchive = NULL; const NuMasterHeader* pMasterHeader; NuRecordIdx recordIdx; - long idx, flushStatus; + uint32_t idx, flushStatus; err = NuOpenRO(inFile, &pInArchive); if (err != kNuErrNone) { @@ -487,10 +481,10 @@ LaunderArchive(const char* inFile, const char* outFile, NuValue compressMethod, /* * Iterate through the set of records. */ - for (idx = 0; idx < (int)pMasterHeader->mhTotalRecords; idx++) { + for (idx = 0; idx < pMasterHeader->mhTotalRecords; idx++) { err = NuGetRecordIdxByPosition(pInArchive, idx, &recordIdx); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: couldn't get record #%ld (err=%d)\n", + fprintf(stderr, "ERROR: couldn't get record #%u (err=%d)\n", idx, err); goto bail; } @@ -525,7 +519,7 @@ LaunderArchive(const char* inFile, const char* outFile, NuValue compressMethod, err = NuFlush(pOutArchive, &flushStatus); if (err != kNuErrNone) { fprintf(stderr, - "ERROR: flush failed (err=%d, status=0x%04lx)\n", + "ERROR: flush failed (err=%d, status=0x%04x)\n", err, flushStatus); goto bail; } @@ -535,15 +529,15 @@ LaunderArchive(const char* inFile, const char* outFile, NuValue compressMethod, /* first and only flush if frequent-flushing wasn't enabled */ err = NuFlush(pOutArchive, &flushStatus); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: flush failed (err=%d, status=0x%04lx)\n", + fprintf(stderr, "ERROR: flush failed (err=%d, status=0x%04x)\n", err, flushStatus); goto bail; } bail: - if (pInArchive != nil) + if (pInArchive != NULL) NuClose(pInArchive); - if (pOutArchive != nil) { + if (pOutArchive != NULL) { if (err != kNuErrNone) NuAbort(pOutArchive); NuClose(pOutArchive); /* flush pending changes and close */ @@ -559,12 +553,11 @@ bail: * does everything we need here. */ int myoptind = 0; -char* myoptarg = nil; -const char* curchar = nil; +char* myoptarg = NULL; +const char* curchar = NULL; int skipnext = false; -int -mygetopt(int argc, char** argv, const char* optstr) +int mygetopt(int argc, char** argv, const char* optstr) { if (!myoptind) { myoptind = 1; @@ -609,8 +602,7 @@ mygetopt(int argc, char** argv, const char* optstr) /* * Print usage info. */ -void -Usage(const char* argv0) +void Usage(const char* argv0) { fprintf(stderr, "Usage: %s [-crfat] [-m method] infile.shk outfile.shk\n", argv0); @@ -627,19 +619,18 @@ Usage(const char* argv0) /* * Grab the name of an archive to read. */ -int -main(int argc, char** argv) +int main(int argc, char** argv) { NuValue compressMethod = kNuCompressLZW2; - long major, minor, bug; + int32_t major, minor, bug; const char* pBuildDate; long flags = 0; int errorFlag; int ic; int cc; - (void) NuGetVersion(&major, &minor, &bug, &pBuildDate, nil); - printf("Using NuFX lib %ld.%ld.%ld built on or after %s\n", + (void) NuGetVersion(&major, &minor, &bug, &pBuildDate, NULL); + printf("Using NuFX lib %d.%d.%d built on or after %s\n", major, minor, bug, pBuildDate); errorFlag = false; diff --git a/nufxlib/samples/Makefile.in b/nufxlib/samples/Makefile.in index 6590a08..7f436bf 100644 --- a/nufxlib/samples/Makefile.in +++ b/nufxlib/samples/Makefile.in @@ -28,49 +28,35 @@ ALL_SRCS = Exerciser.c ImgConv.c Launder.c TestBasic.c \ NUFXLIB = -L.. -lnufx -PRODUCTS = exerciser imgconv launder test-basic test-extract test-simple \ - test-twirl - -#ifdef PURIFY_BUILD -# PURIFY = purify -# CFLAGS += -DPURIFY -#endif -#ifdef QUANTIFY_BUILD -# QUANTIFY = quantify -# CFLAGS += -DQUANTIFY -#endif +PRODUCTS = exerciser imgconv launder test-basic test-extract test-names \ + test-simple test-twirl all: $(PRODUCTS) @true -#quantify: -# -rm -f $(PRODUCT) -# @$(MAKE) QUANTIFY_BUILD=1 -# -#purify: -# -rm -f $(PRODUCT) -# @$(MAKE) PURIFY_BUILD=1 - exerciser: Exerciser.o $(LIB_PRODUCT) - $(PURIFY) $(QUANTIFY) $(CC) -o $@ Exerciser.o $(NUFXLIB) @LIBS@ + $(CC) -o $@ Exerciser.o $(NUFXLIB) @LIBS@ imgconv: ImgConv.o $(LIB_PRODUCT) - $(PURIFY) $(QUANTIFY) $(CC) -o $@ ImgConv.o $(NUFXLIB) @LIBS@ + $(CC) -o $@ ImgConv.o $(NUFXLIB) @LIBS@ launder: Launder.o $(LIB_PRODUCT) - $(PURIFY) $(QUANTIFY) $(CC) -o $@ Launder.o $(NUFXLIB) @LIBS@ + $(CC) -o $@ Launder.o $(NUFXLIB) @LIBS@ test-basic: TestBasic.o $(LIB_PRODUCT) - $(PURIFY) $(QUANTIFY) $(CC) -o $@ TestBasic.o $(NUFXLIB) @LIBS@ - -test-simple: TestSimple.o $(LIB_PRODUCT) - $(PURIFY) $(QUANTIFY) $(CC) -o $@ TestSimple.o $(NUFXLIB) @LIBS@ + $(CC) -o $@ TestBasic.o $(NUFXLIB) @LIBS@ test-extract: TestExtract.o $(LIB_PRODUCT) - $(PURIFY) $(QUANTIFY) $(CC) -o $@ TestExtract.o $(NUFXLIB) @LIBS@ + $(CC) -o $@ TestExtract.o $(NUFXLIB) @LIBS@ + +test-names: TestNames.o $(LIB_PRODUCT) + $(CC) -o $@ TestNames.o $(NUFXLIB) @LIBS@ + +test-simple: TestSimple.o $(LIB_PRODUCT) + $(CC) -o $@ TestSimple.o $(NUFXLIB) @LIBS@ test-twirl: TestTwirl.o $(LIB_PRODUCT) - $(PURIFY) $(QUANTIFY) $(CC) -o $@ TestTwirl.o $(NUFXLIB) @LIBS@ + $(CC) -o $@ TestTwirl.o $(NUFXLIB) @LIBS@ tags:: ctags --totals -R ../* @@ -84,8 +70,12 @@ distclean: clean -rm -f tags -rm -f Makefile Makefile.bak -depend: - makedepend -- $(CFLAGS) -I/usr/local/include -- $(ALL_SRCS) - -# DO NOT DELETE THIS LINE -- make depend depends on it. - +COMMON_HDRS = ../NufxLibPriv.h ../NufxLib.h ../MiscStuff.h ../SysDefs.h +Exerciser.o: Exerciser.c $(COMMON_HDRS) +ImgConv.o: ImgConv.c $(COMMON_HDRS) +Launder.o: Launder.c $(COMMON_HDRS) +TestBasic.o: TestBasic.c $(COMMON_HDRS) +TestExtract.o: TestExtract.c $(COMMON_HDRS) +TestNames.o: TestNames.c $(COMMON_HDRS) +TestSimple.o: TestSimple.c $(COMMON_HDRS) +TestTwirl.o: TestTwirl.c $(COMMON_HDRS) diff --git a/nufxlib/samples/TestBasic.c b/nufxlib/samples/TestBasic.c index 44c85c3..2559608 100644 --- a/nufxlib/samples/TestBasic.c +++ b/nufxlib/samples/TestBasic.c @@ -15,6 +15,8 @@ #define kTestTempFile "nlbt.tmp" #define kNumEntries 3 /* how many records are we going to add? */ + +/* stick to ASCII characters for these -- not doing conversions just yet */ #define kTestEntryBytes "bytes" #define kTestEntryBytesUPPER "BYTES" #define kTestEntryEnglish "English" @@ -39,12 +41,11 @@ char gSuppressError = false; /* * Get a single character of input from the user. */ -static char -TGetReplyChar(char defaultReply) +static char TGetReplyChar(char defaultReply) { char tmpBuf[32]; - if (fgets(tmpBuf, sizeof(tmpBuf), stdin) == nil) + if (fgets(tmpBuf, sizeof(tmpBuf), stdin) == NULL) return defaultReply; if (tmpBuf[0] == '\n' || tmpBuf[0] == '\r') return defaultReply; @@ -52,14 +53,13 @@ TGetReplyChar(char defaultReply) return tmpBuf[0]; } -NuError -AddSimpleRecord(NuArchive* pArchive, const char* filename, +NuError AddSimpleRecord(NuArchive* pArchive, const char* filenameMOR, NuRecordIdx* pRecordIdx) { NuFileDetails fileDetails; memset(&fileDetails, 0, sizeof(fileDetails)); - fileDetails.storageName = filename; + fileDetails.storageNameMOR = filenameMOR; fileDetails.fileSysInfo = kLocalFssep; fileDetails.access = kNuAccessUnlocked; @@ -70,8 +70,7 @@ AddSimpleRecord(NuArchive* pArchive, const char* filename, /* * Display error messages... or not. */ -NuResult -ErrorMessageHandler(NuArchive* pArchive, void* vErrorMessage) +NuResult ErrorMessageHandler(NuArchive* pArchive, void* vErrorMessage) { const NuErrorMessage* pErrorMessage = (const NuErrorMessage*) vErrorMessage; @@ -80,12 +79,12 @@ ErrorMessageHandler(NuArchive* pArchive, void* vErrorMessage) if (pErrorMessage->isDebug) { fprintf(stderr, "%sNufxLib says: [%s:%d %s] %s\n", - pArchive == nil ? "GLOBAL>" : "", + pArchive == NULL ? "GLOBAL>" : "", pErrorMessage->file, pErrorMessage->line, pErrorMessage->function, pErrorMessage->message); } else { fprintf(stderr, "%sNufxLib says: %s\n", - pArchive == nil ? "GLOBAL>" : "", + pArchive == NULL ? "GLOBAL>" : "", pErrorMessage->message); } @@ -95,13 +94,37 @@ ErrorMessageHandler(NuArchive* pArchive, void* vErrorMessage) /* * This gets called when a buffer DataSource is no longer needed. */ -NuResult -FreeCallback(NuArchive* pArchive, void* args) +NuResult FreeCallback(NuArchive* pArchive, void* args) { free(args); return kNuOK; } +/* + * If the test file currently exists, ask the user if it's okay to remove + * it. + * + * Returns 0 if the file was successfully removed, -1 if the file could not + * be removed (because the unlink failed, or the user refused). + */ +int RemoveTestFile(const char* title, const char* fileName) +{ + char answer; + + if (access(fileName, F_OK) == 0) { + printf("%s '%s' exists, remove (y/n)? ", title, fileName); + fflush(stdout); + answer = TGetReplyChar('n'); + if (tolower(answer) != 'y') + return -1; + if (unlink(fileName) < 0) { + perror("unlink"); + return -1; + } + } + return 0; +} + /* * =========================================================================== @@ -113,21 +136,20 @@ FreeCallback(NuArchive* pArchive, void* args) * Make sure the flags that control how we open the file work right, * and verify that we handle existing zero-byte archive files correctly. */ -int -Test_OpenFlags(void) +int Test_OpenFlags(void) { NuError err; - FILE* fp = nil; - NuArchive* pArchive = nil; + FILE* fp = NULL; + NuArchive* pArchive = NULL; printf("... open zero-byte existing\n"); fp = fopen(kTestArchive, kNuFileOpenWriteTrunc); - if (fp == nil) { + if (fp == NULL) { perror("fopen kTestArchive"); goto failed; } fclose(fp); - fp = nil; + fp = NULL; FAIL_OK; err = NuOpenRW(kTestArchive, kTestTempFile, kNuOpenCreat|kNuOpenExcl, @@ -149,7 +171,7 @@ Test_OpenFlags(void) fprintf(stderr, "ERROR: close failed\n"); goto failed; } - pArchive = nil; + pArchive = NULL; if (access(kTestArchive, F_OK) == 0) { fprintf(stderr, "ERROR: archive should have been removed but wasn't\n"); @@ -159,7 +181,7 @@ Test_OpenFlags(void) return 0; failed: - if (pArchive != nil) { + if (pArchive != NULL) { NuAbort(pArchive); NuClose(pArchive); } @@ -170,14 +192,13 @@ failed: /* * Add some files to the archive. These will be used by later tests. */ -int -Test_AddStuff(NuArchive* pArchive) +int Test_AddStuff(NuArchive* pArchive) { NuError err; - uchar* buf = nil; - NuDataSource* pDataSource = nil; + uint8_t* buf = NULL; + NuDataSource* pDataSource = NULL; NuRecordIdx recordIdx; - long status; + uint32_t status; int i; static const char* testMsg = "This is a nice test message that has linefeeds in it so we can\n" @@ -185,20 +206,20 @@ Test_AddStuff(NuArchive* pArchive) "all. It's certainly nice to know that everything works the way\n" "it's supposed to, which I suppose is why we have this nifty test\n" "program available. It sure would be nice if everybody tested\n" - "there code, but where would Microsoft be without endless upgrades\n" + "their code, but where would Microsoft be without endless upgrades\n" "and service packs? Bugs are what America was built on, and\n" "anybody who says otherwise is a pinko commie lowlife. Verily.\n"; printf("... add 'bytes' record\n"); buf = malloc(131072); - if (buf == nil) + if (buf == NULL) goto failed; for (i = 0; i < 131072; i++) *(buf+i) = i & 0xff; FAIL_OK; err = NuCreateDataSourceForBuffer(kNuThreadFormatUncompressed, - 0, nil, 0, 131072, FreeCallback, &pDataSource); + 0, NULL, 0, 131072, FreeCallback, &pDataSource); FAIL_BAD; if (err == kNuErrNone) { fprintf(stderr, "ERROR: that should've failed!\n"); @@ -215,7 +236,7 @@ Test_AddStuff(NuArchive* pArchive) "ERROR: 'bytes' data source create failed (err=%d)\n", err); goto failed; } - buf = nil; /* now owned by library */ + buf = NULL; /* now owned by library */ err = AddSimpleRecord(pArchive, kTestEntryBytes, &recordIdx); if (err != kNuErrNone) { @@ -224,12 +245,12 @@ Test_AddStuff(NuArchive* pArchive) } err = NuAddThread(pArchive, recordIdx, kNuThreadIDDataFork, pDataSource, - nil); + NULL); if (err != kNuErrNone) { fprintf(stderr, "ERROR: 'bytes' thread add failed (err=%d)\n", err); goto failed; } - pDataSource = nil; /* now owned by library */ + pDataSource = NULL; /* now owned by library */ /* @@ -237,7 +258,7 @@ Test_AddStuff(NuArchive* pArchive) */ printf("... add 'English' record\n"); err = NuCreateDataSourceForBuffer(kNuThreadFormatUncompressed, - 0, (const uchar*)testMsg, 0, strlen(testMsg), nil, &pDataSource); + 0, (const uint8_t*)testMsg, 0, strlen(testMsg), NULL, &pDataSource); if (err != kNuErrNone) { fprintf(stderr, "ERROR: 'English' source create failed (err=%d)\n", err); @@ -246,7 +267,7 @@ Test_AddStuff(NuArchive* pArchive) FAIL_OK; err = NuAddThread(pArchive, recordIdx, kNuThreadIDDataFork, pDataSource, - nil); + NULL); FAIL_BAD; if (err == kNuErrNone) { fprintf(stderr, "ERROR: 'English' add should've conflicted!\n"); @@ -268,12 +289,12 @@ Test_AddStuff(NuArchive* pArchive) } err = NuAddThread(pArchive, recordIdx, kNuThreadIDDataFork, pDataSource, - nil); + NULL); if (err != kNuErrNone) { fprintf(stderr, "ERROR: 'English' thread add failed (err=%d)\n", err); goto failed; } - pDataSource = nil; /* now owned by library */ + pDataSource = NULL; /* now owned by library */ /* @@ -281,7 +302,7 @@ Test_AddStuff(NuArchive* pArchive) */ printf("... add 'long' record\n"); err = NuCreateDataSourceForBuffer(kNuThreadFormatUncompressed, - 0, nil, 0, 0, nil, &pDataSource); + 0, NULL, 0, 0, NULL, &pDataSource); if (err != kNuErrNone) { fprintf(stderr, "ERROR: 'English' source create failed (err=%d)\n", err); @@ -295,12 +316,12 @@ Test_AddStuff(NuArchive* pArchive) } err = NuAddThread(pArchive, recordIdx, kNuThreadIDRsrcFork, pDataSource, - nil); + NULL); if (err != kNuErrNone) { fprintf(stderr, "ERROR: 'long' thread add failed (err=%d)\n", err); goto failed; } - pDataSource = nil; /* now owned by library */ + pDataSource = NULL; /* now owned by library */ /* @@ -308,7 +329,7 @@ Test_AddStuff(NuArchive* pArchive) */ err = NuFlush(pArchive, &status); if (err != kNuErrNone) { - fprintf(stderr,"ERROR: couldn't flush after add (err=%d, status=%ld)\n", + fprintf(stderr, "ERROR: couldn't flush after add (err=%d, status=%u)\n", err, status); goto failed; } @@ -318,16 +339,16 @@ Test_AddStuff(NuArchive* pArchive) */ err = NuFlush(pArchive, &status); if (err != kNuErrNone) { - fprintf(stderr,"ERROR: second add flush failed (err=%d, status=%ld)\n", + fprintf(stderr, "ERROR: second add flush failed (err=%d, status=%u)\n", err, status); goto failed; } return 0; failed: - if (pDataSource != nil) + if (pDataSource != NULL) NuFreeDataSource(pDataSource); - if (buf != nil) + if (buf != NULL) free(buf); return -1; } @@ -336,19 +357,18 @@ failed: /* * Make sure that what we're seeing makes sense. */ -NuResult -TestContentsCallback(NuArchive* pArchive, void* vpRecord) +NuResult TestContentsCallback(NuArchive* pArchive, void* vpRecord) { const NuRecord* pRecord = (NuRecord*) vpRecord; - if (strcmp(pRecord->filename, kTestEntryBytes) == 0 || - strcmp(pRecord->filename, kTestEntryEnglish) == 0 || - strcmp(pRecord->filename, kTestEntryLong) == 0) + if (strcmp(pRecord->filenameMOR, kTestEntryBytes) == 0 || + strcmp(pRecord->filenameMOR, kTestEntryEnglish) == 0 || + strcmp(pRecord->filenameMOR, kTestEntryLong) == 0) { return kNuOK; } - fprintf(stderr, "ERROR: found mystery entry '%s'\n", pRecord->filename); + fprintf(stderr, "ERROR: found mystery entry '%s'\n", pRecord->filenameMOR); return kNuAbort; } @@ -356,8 +376,7 @@ TestContentsCallback(NuArchive* pArchive, void* vpRecord) /* * Verify that the contents look about right. */ -int -Test_Contents(NuArchive* pArchive) +int Test_Contents(NuArchive* pArchive) { NuError err; long posn; @@ -386,21 +405,21 @@ Test_Contents(NuArchive* pArchive) err = NuGetRecord(pArchive, recordIdx, &pRecord); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: couldn't get record index %ld (err=%d)\n", + fprintf(stderr, "ERROR: couldn't get record index %u (err=%d)\n", recordIdx, err); goto failed; } - assert(pRecord != nil); + assert(pRecord != NULL); switch (posn) { case 0: - cc = strcmp(pRecord->filename, kTestEntryBytes); + cc = strcmp(pRecord->filenameMOR, kTestEntryBytes); break; case 1: - cc = strcmp(pRecord->filename, kTestEntryEnglish); + cc = strcmp(pRecord->filenameMOR, kTestEntryEnglish); break; case 2: - cc = strcmp(pRecord->filename, kTestEntryLong); + cc = strcmp(pRecord->filenameMOR, kTestEntryLong); if (!cc) cc = !(pRecord->recStorageType == kNuStorageExtended); break; @@ -411,8 +430,8 @@ Test_Contents(NuArchive* pArchive) } if (cc) { - fprintf(stderr, "ERROR: got '%s' for %ld (%ld), not expected\n", - pRecord->filename, posn, recordIdx); + fprintf(stderr, "ERROR: got '%s' for %ld (%u), not expected\n", + pRecord->filenameMOR, posn, recordIdx); goto failed; } } @@ -437,17 +456,16 @@ failed: /* * Selection callback filter for "test". This gets called once per record. */ -NuResult -VerifySelectionCallback(NuArchive* pArchive, void* vpProposal) +NuResult VerifySelectionCallback(NuArchive* pArchive, void* vpProposal) { NuError err; const NuSelectionProposal* pProposal = vpProposal; long count; - if (pProposal->pRecord == nil || pProposal->pThread == nil || - pProposal->pRecord->filename == nil) + if (pProposal->pRecord == NULL || pProposal->pThread == NULL || + pProposal->pRecord->filenameMOR == NULL) { - fprintf(stderr, "ERROR: unexpected nil in proposal\n"); + fprintf(stderr, "ERROR: unexpected NULL in proposal\n"); goto failed; } @@ -473,8 +491,7 @@ failed: /* * Verify the archive contents. */ -int -Test_Verify(NuArchive* pArchive) +int Test_Verify(NuArchive* pArchive) { NuError err; long count; @@ -520,15 +537,14 @@ failed: /* * Extract stuff. */ -int -Test_Extract(NuArchive* pArchive) +int Test_Extract(NuArchive* pArchive) { NuError err; NuRecordIdx recordIdx; const NuRecord* pRecord; const NuThread* pThread; - NuDataSink* pDataSink = nil; - uchar* buf = nil; + NuDataSink* pDataSink = NULL; + uint8_t* buf = NULL; printf("... extracting files\n"); @@ -549,24 +565,24 @@ Test_Extract(NuArchive* pArchive) } err = NuGetRecord(pArchive, recordIdx, &pRecord); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: couldn't get record index %ld (err=%d)\n", + fprintf(stderr, "ERROR: couldn't get record index %u (err=%d)\n", recordIdx, err); goto failed; } - assert(pRecord != nil); + assert(pRecord != NULL); /* we're not using ShrinkIt compat mode, so there should not be a comment */ pThread = NuGetThread(pRecord, 1); - assert(pThread != nil); + assert(pThread != NULL); if (NuGetThreadID(pThread) != kNuThreadIDDataFork) { - fprintf(stderr, "ERROR: 'bytes' had unexpected threadID 0x%08lx\n", + fprintf(stderr, "ERROR: 'bytes' had unexpected threadID 0x%08x\n", NuGetThreadID(pThread)); goto failed; } buf = malloc(pThread->actualThreadEOF); - if (buf == nil) { - fprintf(stderr, "ERROR: malloc(%ld) failed\n",pThread->actualThreadEOF); + if (buf == NULL) { + fprintf(stderr, "ERROR: malloc(%u) failed\n",pThread->actualThreadEOF); goto failed; } @@ -587,7 +603,7 @@ Test_Extract(NuArchive* pArchive) goto failed; } NuFreeDataSink(pDataSink); - pDataSink = nil; + pDataSink = NULL; /* * Try to extract with "on" conversion, which should fail because the @@ -608,7 +624,7 @@ Test_Extract(NuArchive* pArchive) goto failed; } NuFreeDataSink(pDataSink); - pDataSink = nil; + pDataSink = NULL; /* * Try to extract with "auto" conversion, which should conclude that @@ -628,12 +644,12 @@ Test_Extract(NuArchive* pArchive) goto failed; } NuFreeDataSink(pDataSink); - pDataSink = nil; + pDataSink = NULL; free(buf); - buf = nil; + buf = NULL; @@ -648,24 +664,24 @@ Test_Extract(NuArchive* pArchive) } err = NuGetRecord(pArchive, recordIdx, &pRecord); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: couldn't get record index %ld (err=%d)\n", + fprintf(stderr, "ERROR: couldn't get record index %u (err=%d)\n", recordIdx, err); goto failed; } - assert(pRecord != nil); + assert(pRecord != NULL); /* we're not using ShrinkIt compat mode, so there should not be a comment */ pThread = NuGetThread(pRecord, 1); - assert(pThread != nil); + assert(pThread != NULL); if (NuGetThreadID(pThread) != kNuThreadIDDataFork) { - fprintf(stderr, "ERROR: 'English' had unexpected threadID 0x%08lx\n", + fprintf(stderr, "ERROR: 'English' had unexpected threadID 0x%08x\n", NuGetThreadID(pThread)); goto failed; } buf = malloc(pThread->actualThreadEOF); - if (buf == nil) { - fprintf(stderr, "ERROR: malloc(%ld) failed\n",pThread->actualThreadEOF); + if (buf == NULL) { + fprintf(stderr, "ERROR: malloc(%u) failed\n", pThread->actualThreadEOF); goto failed; } @@ -686,7 +702,7 @@ Test_Extract(NuArchive* pArchive) goto failed; } NuFreeDataSink(pDataSink); - pDataSink = nil; + pDataSink = NULL; /* * Try to extract with "auto" conversion, which should fail because the @@ -707,12 +723,12 @@ Test_Extract(NuArchive* pArchive) goto failed; } NuFreeDataSink(pDataSink); - pDataSink = nil; + pDataSink = NULL; /*Free(buf);*/ - /*buf = nil;*/ + /*buf = NULL;*/ @@ -727,17 +743,17 @@ Test_Extract(NuArchive* pArchive) } err = NuGetRecord(pArchive, recordIdx, &pRecord); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: couldn't get record index %ld (err=%d)\n", + fprintf(stderr, "ERROR: couldn't get record index %u (err=%d)\n", recordIdx, err); goto failed; } - assert(pRecord != nil); + assert(pRecord != NULL); /* we're not using ShrinkIt compat mode, so there should not be a comment */ pThread = NuGetThread(pRecord, 1); - assert(pThread != nil); + assert(pThread != NULL); if (NuGetThreadID(pThread) != kNuThreadIDRsrcFork) { - fprintf(stderr, "ERROR: 'Long' had unexpected threadID 0x%08lx\n", + fprintf(stderr, "ERROR: 'Long' had unexpected threadID 0x%08x\n", NuGetThreadID(pThread)); goto failed; } @@ -759,20 +775,20 @@ Test_Extract(NuArchive* pArchive) goto failed; } NuFreeDataSink(pDataSink); - pDataSink = nil; + pDataSink = NULL; free(buf); - buf = nil; + buf = NULL; return 0; failed: - if (buf != nil) + if (buf != NULL) free(buf); - if (pDataSink != nil) + if (pDataSink != NULL) (void) NuFreeDataSink(pDataSink); return -1; } @@ -780,13 +796,12 @@ failed: /* * Delete the first and last records. Does *not* flush the archive. */ -int -Test_Delete(NuArchive* pArchive) +int Test_Delete(NuArchive* pArchive) { NuError err; NuRecordIdx recordIdx; const NuRecord* pRecord; - const NuThread* pThread = nil; + const NuThread* pThread = NULL; long count; int idx; @@ -802,33 +817,33 @@ Test_Delete(NuArchive* pArchive) } err = NuGetRecord(pArchive, recordIdx, &pRecord); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: couldn't get record index %ld (err=%d)\n", + fprintf(stderr, "ERROR: couldn't get record index %u (err=%d)\n", recordIdx, err); goto failed; } - assert(pRecord != nil); + assert(pRecord != NULL); assert(pRecord->recTotalThreads > 0); for (idx = 0; idx < (int)pRecord->recTotalThreads; idx++) { pThread = NuGetThread(pRecord, idx); - assert(pThread != nil); + assert(pThread != NULL); err = NuDeleteThread(pArchive, pThread->threadIdx); if (err != kNuErrNone) { fprintf(stderr, - "ERROR: couldn't delete thread #%d (%ld) (err=%d)\n", + "ERROR: couldn't delete thread #%d (%u) (err=%d)\n", idx, recordIdx, err); goto failed; } } /* try to re-delete the same thread */ - assert(pThread != nil); + assert(pThread != NULL); FAIL_OK; err = NuDeleteThread(pArchive, pThread->threadIdx); FAIL_BAD; if (err == kNuErrNone) { - fprintf(stderr, "ERROR: allowed to re-delete thread (%ld) (err=%d)\n", + fprintf(stderr, "ERROR: allowed to re-delete thread (%u) (err=%d)\n", recordIdx, err); goto failed; } @@ -839,7 +854,7 @@ Test_Delete(NuArchive* pArchive) FAIL_BAD; if (err == kNuErrNone) { fprintf(stderr, - "ERROR: able to delete modified record (%ld) (err=%d)\n", + "ERROR: able to delete modified record (%u) (err=%d)\n", recordIdx, err); goto failed; } @@ -847,7 +862,8 @@ Test_Delete(NuArchive* pArchive) /* * Make sure the attr hasn't been updated yet. */ - err = NuGetAttr(pArchive, kNuAttrNumRecords, (unsigned long*) &count); + count = 0; + err = NuGetAttr(pArchive, kNuAttrNumRecords, (uint32_t*) &count); if (count != kNumEntries) { fprintf(stderr, "ERROR: kNuAttrNumRecords %ld vs %d\n", count, kNumEntries); @@ -864,19 +880,19 @@ Test_Delete(NuArchive* pArchive) } err = NuGetRecord(pArchive, recordIdx, &pRecord); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: couldn't get record index %ld (err=%d)\n", + fprintf(stderr, "ERROR: couldn't get record index %u (err=%d)\n", recordIdx, err); goto failed; } - assert(pRecord != nil); + assert(pRecord != NULL); /* grab the first thread before we whack the record */ pThread = NuGetThread(pRecord, 0); - assert(pThread != nil); + assert(pThread != NULL); err = NuDeleteRecord(pArchive, recordIdx); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: unable to delete record #%d (%ld) (err=%d)\n", + fprintf(stderr, "ERROR: unable to delete record #%d (%u) (err=%d)\n", kNumEntries-1, recordIdx, err); goto failed; } @@ -887,7 +903,7 @@ Test_Delete(NuArchive* pArchive) FAIL_BAD; if (err == kNuErrNone) { fprintf(stderr, - "ERROR: allowed to delete from deleted (%ld) (err=%d)\n", + "ERROR: allowed to delete from deleted (%u) (err=%d)\n", pThread->threadIdx, err); goto failed; } @@ -901,8 +917,7 @@ failed: /* * Verify that the count in the master header has been updated. */ -int -Test_MasterCount(NuArchive* pArchive, long expected) +int Test_MasterCount(NuArchive* pArchive, long expected) { NuError err; const NuMasterHeader* pMasterHeader; @@ -915,8 +930,8 @@ Test_MasterCount(NuArchive* pArchive, long expected) goto failed; } - if (pMasterHeader->mhTotalRecords != (ulong)expected) { - fprintf(stderr, "ERROR: unexpected MH count (%ld vs %ld)\n", + if (pMasterHeader->mhTotalRecords != (uint32_t)expected) { + fprintf(stderr, "ERROR: unexpected MH count (%u vs %ld)\n", pMasterHeader->mhTotalRecords, expected); goto failed; } @@ -932,41 +947,21 @@ failed: * * Returns 0 on success, -1 on error. */ -int -DoTests(void) +int DoTests(void) { NuError err; - NuArchive* pArchive = nil; - long status; + NuArchive* pArchive = NULL; + uint32_t status; int cc, result = 0; - char answer; /* * Make sure we're starting with a clean slate. */ - if (access(kTestArchive, F_OK) == 0) { - printf("Test archive '%s' exists, remove (y/n)? ", kTestArchive); - fflush(stdout); - answer = TGetReplyChar('n'); - if (tolower(answer) != 'y') - goto failed; - cc = unlink(kTestArchive); - if (cc < 0) { - perror("unlink kTestArchive"); - goto failed; - } + if (RemoveTestFile("Test archive", kTestArchive) < 0) { + goto failed; } - if (access(kTestTempFile, F_OK) == 0) { - printf("Test temp file '%s' exists, remove (y/n)? ", kTestTempFile); - fflush(stdout); - answer = TGetReplyChar('n'); - if (tolower(answer) != 'y') - goto failed; - cc = unlink(kTestTempFile); - if (cc < 0) { - perror("unlink kTestTempFile"); - goto failed; - } + if (RemoveTestFile("Test temp file", kTestTempFile) < 0) { + goto failed; } /* @@ -1013,7 +1008,7 @@ DoTests(void) fprintf(stderr, "ERROR: mid NuClose failed (err=%d)\n", err); goto failed; } - pArchive = nil; + pArchive = NULL; err = NuOpenRO(kTestArchive, &pArchive); if (err != kNuErrNone) { @@ -1028,14 +1023,14 @@ DoTests(void) } /* - * Make sure the contents are still what we expect. + * Make sure the TOC (i.e. list of files) is still what we expect. */ printf("... checking contents\n"); if (Test_Contents(pArchive) != 0) goto failed; /* - * Verify the archive contents. + * Verify the archive data. */ if (Test_Verify(pArchive) != 0) goto failed; @@ -1055,7 +1050,7 @@ DoTests(void) fprintf(stderr, "ERROR: late NuClose failed (err=%d)\n", err); goto failed; } - pArchive = nil; + pArchive = NULL; err = NuOpenRW(kTestArchive, kTestTempFile, 0, &pArchive); if (err != kNuErrNone) { @@ -1107,7 +1102,7 @@ DoTests(void) */ err = NuFlush(pArchive, &status); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: flush failed (err=%d, status=%ld)\n", + fprintf(stderr, "ERROR: flush failed (err=%d, status=%d)\n", err, status); goto failed; } @@ -1122,7 +1117,7 @@ DoTests(void) * That's all, folks... */ NuClose(pArchive); - pArchive = nil; + pArchive = NULL; printf("... removing '%s'\n", kTestArchive); cc = unlink(kTestArchive); @@ -1133,7 +1128,7 @@ DoTests(void) leave: - if (pArchive != nil) { + if (pArchive != NULL) { NuAbort(pArchive); NuClose(pArchive); } @@ -1148,16 +1143,15 @@ failed: /* * Crank away. */ -int -main(void) +int main(void) { - long major, minor, bug; + int32_t major, minor, bug; const char* pBuildDate; const char* pBuildFlags; int cc; (void) NuGetVersion(&major, &minor, &bug, &pBuildDate, &pBuildFlags); - printf("Using NuFX library v%ld.%ld.%ld, built on or after\n" + printf("Using NuFX library v%d.%d.%d, built on or after\n" " %s with [%s]\n\n", major, minor, bug, pBuildDate, pBuildFlags); diff --git a/nufxlib/samples/TestExtract.c b/nufxlib/samples/TestExtract.c index 302c631..4e8bfb2 100644 --- a/nufxlib/samples/TestExtract.c +++ b/nufxlib/samples/TestExtract.c @@ -37,7 +37,7 @@ * Track an archive record. */ typedef struct ArchiveRecord { - char* filename; + char* filenameMOR; NuRecordIdx recordIdx; long numThreads; @@ -50,25 +50,24 @@ typedef struct ArchiveRecord { /* * Alloc a new ArchiveRecord. */ -ArchiveRecord* -ArchiveRecord_New(const NuRecord* pRecord) +ArchiveRecord* ArchiveRecord_New(const NuRecord* pRecord) { - ArchiveRecord* pArcRec = nil; + ArchiveRecord* pArcRec = NULL; pArcRec = malloc(sizeof(*pArcRec)); - if (pArcRec == nil) - return nil; + if (pArcRec == NULL) + return NULL; - if (pRecord->filename == nil) - pArcRec->filename = strdup(""); + if (pRecord->filenameMOR == NULL) + pArcRec->filenameMOR = strdup(""); else - pArcRec->filename = strdup((char*)pRecord->filename); + pArcRec->filenameMOR = strdup(pRecord->filenameMOR); pArcRec->recordIdx = pRecord->recordIdx; pArcRec->numThreads = NuRecordGetNumThreads(pRecord); (void) NuRecordCopyThreads(pRecord, &pArcRec->pThreads); - pArcRec->pNext = nil; + pArcRec->pNext = NULL; return pArcRec; } @@ -76,15 +75,14 @@ ArchiveRecord_New(const NuRecord* pRecord) /* * Free up an ArchiveRecord. */ -void -ArchiveRecord_Free(ArchiveRecord* pArcRec) +void ArchiveRecord_Free(ArchiveRecord* pArcRec) { - if (pArcRec == nil) + if (pArcRec == NULL) return; - if (pArcRec->filename != nil) - free(pArcRec->filename); - if (pArcRec->pThreads != nil) + if (pArcRec->filenameMOR != NULL) + free(pArcRec->filenameMOR); + if (pArcRec->pThreads != NULL) free(pArcRec->pThreads); free(pArcRec); } @@ -92,8 +90,8 @@ ArchiveRecord_Free(ArchiveRecord* pArcRec) /* * Find a thread with a matching NuThreadID. */ -const NuThread* -ArchiveRecord_FindThreadByID(const ArchiveRecord* pArcRec, NuThreadID threadID) +const NuThread* ArchiveRecord_FindThreadByID(const ArchiveRecord* pArcRec, + NuThreadID threadID) { const NuThread* pThread; int i; @@ -104,44 +102,38 @@ ArchiveRecord_FindThreadByID(const ArchiveRecord* pArcRec, NuThreadID threadID) return pThread; } - return nil; + return NULL; } -const char* -ArchiveRecord_GetFilename(const ArchiveRecord* pArcRec) +const char* ArchiveRecord_GetFilename(const ArchiveRecord* pArcRec) { - return pArcRec->filename; + return pArcRec->filenameMOR; } -NuRecordIdx -ArchiveRecord_GetRecordIdx(const ArchiveRecord* pArcRec) +NuRecordIdx ArchiveRecord_GetRecordIdx(const ArchiveRecord* pArcRec) { return pArcRec->recordIdx; } -long -ArchiveRecord_GetNumThreads(const ArchiveRecord* pArcRec) +long ArchiveRecord_GetNumThreads(const ArchiveRecord* pArcRec) { return pArcRec->numThreads; } -const NuThread* -ArchiveRecord_GetThread(const ArchiveRecord* pArcRec, int idx) +const NuThread* ArchiveRecord_GetThread(const ArchiveRecord* pArcRec, int idx) { if (idx < 0 || idx >= pArcRec->numThreads) - return nil; + return NULL; return NuThreadGetByIdx(pArcRec->pThreads, idx); } -void -ArchiveRecord_SetNext(ArchiveRecord* pArcRec, ArchiveRecord* pNextRec) +void ArchiveRecord_SetNext(ArchiveRecord* pArcRec, ArchiveRecord* pNextRec) { pArcRec->pNext = pNextRec; } -ArchiveRecord* -ArchiveRecord_GetNext(const ArchiveRecord* pArcRec) +ArchiveRecord* ArchiveRecord_GetNext(const ArchiveRecord* pArcRec) { return pArcRec->pNext; } @@ -163,31 +155,29 @@ typedef struct ArchiveData { } ArchiveData; -ArchiveData* -ArchiveData_New(void) +ArchiveData* ArchiveData_New(void) { ArchiveData* pArcData; pArcData = malloc(sizeof(*pArcData)); - if (pArcData == nil) - return nil; + if (pArcData == NULL) + return NULL; pArcData->numRecords = 0; - pArcData->pRecordHead = pArcData->pRecordTail = nil; + pArcData->pRecordHead = pArcData->pRecordTail = NULL; return pArcData; } -void -ArchiveData_Free(ArchiveData* pArcData) +void ArchiveData_Free(ArchiveData* pArcData) { ArchiveRecord* pNext; - if (pArcData == nil) + if (pArcData == NULL) return; printf("*** Deleting %ld records!\n", pArcData->numRecords); - while (pArcData->pRecordHead != nil) { + while (pArcData->pRecordHead != NULL) { pNext = ArchiveRecord_GetNext(pArcData->pRecordHead); ArchiveRecord_Free(pArcData->pRecordHead); pArcData->pRecordHead = pNext; @@ -197,22 +187,20 @@ ArchiveData_Free(ArchiveData* pArcData) } -ArchiveRecord* -ArchiveData_GetRecordHead(const ArchiveData* pArcData) +ArchiveRecord* ArchiveData_GetRecordHead(const ArchiveData* pArcData) { return pArcData->pRecordHead; } /* add an ArchiveRecord to the list pointed at by ArchiveData */ -void -ArchiveData_AddRecord(ArchiveData* pArcData, ArchiveRecord* pRecord) +void ArchiveData_AddRecord(ArchiveData* pArcData, ArchiveRecord* pRecord) { - assert(pRecord != nil); - assert((pArcData->pRecordHead == nil && pArcData->pRecordTail == nil) || - (pArcData->pRecordHead != nil && pArcData->pRecordTail != nil)); + assert(pRecord != NULL); + assert((pArcData->pRecordHead == NULL && pArcData->pRecordTail == NULL) || + (pArcData->pRecordHead != NULL && pArcData->pRecordTail != NULL)); - if (pArcData->pRecordHead == nil) { + if (pArcData->pRecordHead == NULL) { /* first */ pArcData->pRecordHead = pArcData->pRecordTail = pRecord; } else { @@ -225,24 +213,23 @@ ArchiveData_AddRecord(ArchiveData* pArcData, ArchiveRecord* pRecord) } /* dump the contents of the ArchiveData to stdout */ -void -ArchiveData_DumpContents(const ArchiveData* pArcData) +void ArchiveData_DumpContents(const ArchiveData* pArcData) { ArchiveRecord* pArcRec; pArcRec = pArcData->pRecordHead; - while (pArcRec != nil) { + while (pArcRec != NULL) { const NuThread* pThread; int i, count; - printf("%5ld '%s'\n", + printf("%5u '%s'\n", ArchiveRecord_GetRecordIdx(pArcRec), ArchiveRecord_GetFilename(pArcRec)); count = ArchiveRecord_GetNumThreads(pArcRec); for (i = 0; i < count; i++) { pThread = ArchiveRecord_GetThread(pArcRec, i); - printf(" %5ld 0x%04x 0x%04x\n", pThread->threadIdx, + printf(" %5u 0x%04x 0x%04x\n", pThread->threadIdx, pThread->thThreadClass, pThread->thThreadKind); } @@ -260,18 +247,18 @@ ArchiveData_DumpContents(const ArchiveData* pArcData) /* * Callback function to collect archive information. */ -NuResult -GatherContents(NuArchive* pArchive, void* vpRecord) +NuResult GatherContents(NuArchive* pArchive, void* vpRecord) { NuRecord* pRecord = (NuRecord*) vpRecord; - ArchiveData* pArchiveData = nil; + ArchiveData* pArchiveData = NULL; ArchiveRecord* pArchiveRecord = ArchiveRecord_New(pRecord); NuGetExtraData(pArchive, (void**)&pArchiveData); - assert(pArchiveData != nil); + assert(pArchiveData != NULL); printf("*** Filename = '%s'\n", - pRecord->filename == nil ? "":(const char*)pRecord->filename); + pRecord->filenameMOR == NULL ? + "" : pRecord->filenameMOR); ArchiveData_AddRecord(pArchiveData, pArchiveRecord); @@ -282,8 +269,7 @@ GatherContents(NuArchive* pArchive, void* vpRecord) /* * Copy the filename thread from every record to "pDataSink". */ -NuError -ReadAllFilenameThreads(NuArchive* pArchive, ArchiveData* pArchiveData, +NuError ReadAllFilenameThreads(NuArchive* pArchive, ArchiveData* pArchiveData, NuDataSink* pDataSink) { NuError err = kNuErrNone; @@ -291,10 +277,10 @@ ReadAllFilenameThreads(NuArchive* pArchive, ArchiveData* pArchiveData, const NuThread* pThread; pArchiveRecord = ArchiveData_GetRecordHead(pArchiveData); - while (pArchiveRecord != nil) { + while (pArchiveRecord != NULL) { pThread = ArchiveRecord_FindThreadByID(pArchiveRecord, kNuThreadIDFilename); - if (pThread != nil) { + if (pThread != NULL) { err = NuExtractThread(pArchive, pThread->threadIdx, pDataSink); if (err != kNuErrNone) { fprintf(stderr, "*** Extract failed (%d)\n", err); @@ -310,11 +296,10 @@ bail: /* extract every filename thread into a single file, overwriting each time */ -NuError -ExtractToFile(NuArchive* pArchive, ArchiveData* pArchiveData) +NuError ExtractToFile(NuArchive* pArchive, ArchiveData* pArchiveData) { NuError err; - NuDataSink* pDataSink = nil; + NuDataSink* pDataSink = NULL; err = NuCreateDataSinkForFile(true, kNuConvertOff, "out.file", PATH_SEP, &pDataSink); @@ -337,14 +322,13 @@ bail: } /* extract every filename thread into a FILE*, appending */ -NuError -ExtractToFP(NuArchive* pArchive, ArchiveData* pArchiveData) +NuError ExtractToFP(NuArchive* pArchive, ArchiveData* pArchiveData) { NuError err; - FILE* fp = nil; - NuDataSink* pDataSink = nil; + FILE* fp = NULL; + NuDataSink* pDataSink = NULL; - if ((fp = fopen("out.fp", kNuFileOpenWriteTrunc)) == nil) + if ((fp = fopen("out.fp", kNuFileOpenWriteTrunc)) == NULL) return kNuErrFileOpen; err = NuCreateDataSinkForFP(true, kNuConvertOff, fp, &pDataSink); @@ -357,7 +341,7 @@ ExtractToFP(NuArchive* pArchive, ArchiveData* pArchiveData) bail: (void) NuFreeDataSink(pDataSink); - if (fp != nil) + if (fp != NULL) fclose(fp); if (err == kNuErrNone) printf("*** FP write complete\n"); @@ -365,13 +349,12 @@ bail: } /* extract every filename thread into a buffer, advancing as we go */ -NuError -ExtractToBuffer(NuArchive* pArchive, ArchiveData* pArchiveData) +NuError ExtractToBuffer(NuArchive* pArchive, ArchiveData* pArchiveData) { NuError err; - unsigned char buffer[kHappySize]; - NuDataSink* pDataSink = nil; - unsigned long count; + uint8_t buffer[kHappySize]; + NuDataSink* pDataSink = NULL; + uint32_t count; err = NuCreateDataSinkForBuffer(true, kNuConvertOff, buffer, kHappySize, &pDataSink); @@ -389,9 +372,9 @@ ExtractToBuffer(NuArchive* pArchive, ArchiveData* pArchiveData) (void) NuDataSinkGetOutCount(pDataSink, &count); if (count > 0) { FILE* fp; - if ((fp = fopen("out.buf", kNuFileOpenWriteTrunc)) != nil) { + if ((fp = fopen("out.buf", kNuFileOpenWriteTrunc)) != NULL) { - printf("*** Writing %ld bytes\n", count); + printf("*** Writing %u bytes\n", count); if (fwrite(buffer, count, 1, fp) != 1) err = kNuErrFileWrite; fclose(fp); @@ -409,14 +392,13 @@ bail: /* * Do file stuff. */ -int -DoFileStuff(const char* filename) +int DoFileStuff(const UNICHAR* filenameUNI) { NuError err; - NuArchive* pArchive = nil; + NuArchive* pArchive = NULL; ArchiveData* pArchiveData = ArchiveData_New(); - err = NuOpenRO(filename, &pArchive); + err = NuOpenRO(filenameUNI, &pArchive); if (err != kNuErrNone) goto bail; @@ -444,7 +426,7 @@ bail: if (err != kNuErrNone) fprintf(stderr, "*** ERROR: got error %d\n", err); - if (pArchive != nil) { + if (pArchive != NULL) { NuError err2 = NuClose(pArchive); if (err == kNuErrNone && err2 != kNuErrNone) err = err2; @@ -459,21 +441,20 @@ bail: /* * Grab the name of an archive to read. If no name was provided, use stdin. */ -int -main(int argc, char** argv) +int main(int argc, char** argv) { - long major, minor, bug; + int32_t major, minor, bug; const char* pBuildDate; - FILE* infp = nil; + FILE* infp = NULL; int cc; - (void) NuGetVersion(&major, &minor, &bug, &pBuildDate, nil); - printf("Using NuFX lib %ld.%ld.%ld built on or after %s\n", + (void) NuGetVersion(&major, &minor, &bug, &pBuildDate, NULL); + printf("Using NuFX lib %d.%d.%d built on or after %s\n", major, minor, bug, pBuildDate); if (argc == 2) { infp = fopen(argv[1], kNuFileOpenReadOnly); - if (infp == nil) { + if (infp == NULL) { perror("fopen failed"); exit(1); } @@ -484,7 +465,7 @@ main(int argc, char** argv) cc = DoFileStuff(argv[1]); - if (infp != nil) + if (infp != NULL) fclose(infp); exit(cc != 0); diff --git a/nufxlib/samples/TestNames.c b/nufxlib/samples/TestNames.c new file mode 100644 index 0000000..61ea479 --- /dev/null +++ b/nufxlib/samples/TestNames.c @@ -0,0 +1,580 @@ +/* + * NuFX archive manipulation library + * Copyright (C) 2000-2007 by Andy McFadden, All Rights Reserved. + * This is free software; you can redistribute it and/or modify it under the + * terms of the BSD License, see the file COPYING.LIB. + * + * Test local and storage names with Unicode and Mac OS Roman content. + * + * On Windows, opening files with fancy filenames requires UTF-16 and + * special functions. On Linux and Mac OS X we're just writing UTF-8 data, + * so they don't really need to do anything special other than be 8-bit + * clean. NufxLib functions take UTF-8 strings, so on Windows we define + * everything in UTF-16 and convert to UTF-8. (We need the UTF-16 form so + * we can use "wide" I/O functions to confirm that the file was created + * with the correct name.) + * + * To see files with the correct appearance with "ls", you may need to + * do something like: + * + * % LC_ALL=en_US.UTF-8 ls + * + * (Many users set LC_ALL=POSIX to avoid GNU grep slowdowns and altered + * sort ordering in ls.) + */ +#include +#include +#include "NufxLib.h" +#include "Common.h" + +/* + * Test filenames. + * + * The local filename (kTestArchive) contains non-MOR Unicode values + * (two Japanese characters that Google Translate claims form the verb + * "shrink"). The temp file name is similar. + * + * The entry name uses a mix of simple ASCII, CP1252 MOR, and + * non-CP1252 MOR characters: fl ligature, double dagger, copyright symbol, + * Apple logo (the latter of which doesn't have a glyph on Windows or Linux). + * All of the characters have MOR translations. + */ +#ifdef USE_UTF16 +const UNICHAR kTestArchive[] = L"nlTest\u7e2e\u3080.shk"; +const UNICHAR kTestEntryName[] = L"nl-test\u2013\ufb01_\u2021_\u00a9\uf8ff!"; +const UNICHAR kTestTempFile[] = L"nlTest\4e00\u6642\u30d5\u30a1\u30a4\u30eb.tmp"; +#else +const UNICHAR kTestArchive[] = "nlTest\xe7\xb8\xae\xe3\x82\x80.shk"; +const UNICHAR kTestEntryName[] = "nl-test\xe2\x80\x93\xef\xac\x81_\xe2\x80\xa1_" + "\xc2\xa9\xef\xa3\xbf!"; +const UNICHAR kTestTempFile[] = "nlTest\xe4\xb8\x80\xe6\x99\x82\xe3\x83\x95" + "\xe3\x82\xa1\xe3\x82\xa4\xe3\x83\xab.tmp"; +#endif + +const UNICHAR kLocalFssep = '|'; + + +/* + * =========================================================================== + * Helper functions + * =========================================================================== + */ + +/* + * Get a single character of input from the user. + */ +static char TGetReplyChar(char defaultReply) +{ + char tmpBuf[32]; + + if (fgets(tmpBuf, sizeof(tmpBuf), stdin) == NULL) + return defaultReply; + if (tmpBuf[0] == '\n' || tmpBuf[0] == '\r') + return defaultReply; + + return tmpBuf[0]; +} + +NuError AddSimpleRecord(NuArchive* pArchive, const char* fileNameMOR, + NuRecordIdx* pRecordIdx) +{ + NuFileDetails fileDetails; + + memset(&fileDetails, 0, sizeof(fileDetails)); + fileDetails.storageNameMOR = fileNameMOR; + fileDetails.fileSysInfo = kLocalFssep; + fileDetails.access = kNuAccessUnlocked; + + return NuAddRecord(pArchive, &fileDetails, pRecordIdx); +} + +/* + * Display error messages... or not. + */ +NuResult ErrorMessageHandler(NuArchive* pArchive, void* vErrorMessage) +{ + const NuErrorMessage* pErrorMessage = (const NuErrorMessage*) vErrorMessage; + + //if (gSuppressError) + // return kNuOK; + + if (pErrorMessage->isDebug) { + fprintf(stderr, "%sNufxLib says: [%s:%d %s] %s\n", + pArchive == NULL ? "GLOBAL>" : "", + pErrorMessage->file, pErrorMessage->line, pErrorMessage->function, + pErrorMessage->message); + } else { + fprintf(stderr, "%sNufxLib says: %s\n", + pArchive == NULL ? "GLOBAL>" : "", + pErrorMessage->message); + } + + return kNuOK; +} + +#ifdef USE_UTF16 +TODO - use _waccess, _wunlink, etc. +#else +int RemoveTestFile(const char* title, const char* fileName) +{ + char answer; + + if (access(fileName, F_OK) == 0) { + printf("%s '%s' exists, remove (y/n)? ", title, fileName); + fflush(stdout); + answer = TGetReplyChar('n'); + if (tolower(answer) != 'y') + return -1; + if (unlink(fileName) < 0) { + perror("unlink"); + return -1; + } + } + return 0; +} +#endif + +/* + * Utility function that wraps NuConvertUNIToMOR, allocating a new + * buffer to hold the converted string. The caller must free the result. + */ +char* CopyUNIToMOR(const UNICHAR* stringUNI) +{ + size_t morLen; + char* morBuf; + + morLen = NuConvertUNIToMOR(stringUNI, NULL, 0); + if (morLen == (size_t) -1) { + return NULL; + } + morBuf = (char*) malloc(morLen); + (void) NuConvertUNIToMOR(stringUNI, morBuf, morLen); + return morBuf; +} + +/* + * Utility function that wraps NuConvertMORToUNI, allocating a new + * buffer to hold the converted string. The caller must free the result. + */ +UNICHAR* CopyMORToUNI(const char* stringMOR) +{ + size_t uniLen; + char* uniBuf; + + uniLen = NuConvertMORToUNI(stringMOR, NULL, 0); + if (uniLen == (size_t) -1) { + return NULL; + } + uniBuf = (UNICHAR*) malloc(uniLen); + (void) NuConvertMORToUNI(stringMOR, uniBuf, uniLen); + return uniBuf; +} + + +/* + * =========================================================================== + * Tests + * =========================================================================== + */ + +void DumpMorString(const char* str) +{ + printf("(%d) ", (int) strlen(str)); + while (*str != '\0') { + if (*str >= 0x20 && *str < 0x7f) { + putchar(*str); + } else { + printf("\\x%02x", (uint8_t) *str); + } + str++; + } + putchar('\n'); +} + +void DumpUnicharString(const UNICHAR* str) +{ + printf("(%d) ", (int) strlen(str)); + while (*str != '\0') { + if (*str >= 0x20 && *str < 0x7f) { + putchar(*str); + } else { + if (sizeof(UNICHAR) == 1) { + printf("\\x%02x", (uint8_t) *str); + } else { + printf("\\u%04x", (uint16_t) *str); + } + } + str++; + } + putchar('\n'); +} + +/* + * Some basic string conversion unit tests. + * + * TODO: test with short buffer, make sure we don't get partial code + * points when converting to Unicode + */ +int TestStringConversion(void) +{ + static const char kMORTest[] = "test\xe0\xe9\xed\xf3\xfa#\xf0\xb0"; + + size_t outLen; + char morBuf[512]; + UNICHAR uniBuf[512]; + + // convert test string to Unicode + memset(uniBuf, 0xcc, sizeof(uniBuf)); + //printf("MOR: "); DumpMorString(kMORTest); + + outLen = NuConvertMORToUNI(kMORTest, NULL, 0); + //printf("outLen is %u\n", (unsigned int) outLen); + if (NuConvertMORToUNI(kMORTest, uniBuf, sizeof(uniBuf)) != outLen) { + fprintf(stderr, "Inconsistent MORToUNI len\n"); + return -1; + } + //printf("UNI: "); DumpUnicharString(uniBuf); + if (strlen(uniBuf) + 1 != outLen) { + fprintf(stderr, "Expected length != actual length\n"); + return -1; + } + + // convert Unicode back to MOR + memset(morBuf, 0xcc, sizeof(morBuf)); + + outLen = NuConvertUNIToMOR(uniBuf, NULL, 0); + //printf("outLen is %u\n", (unsigned int) outLen); + if (NuConvertUNIToMOR(uniBuf, morBuf, sizeof(morBuf)) != outLen) { + fprintf(stderr, "Inconsistent UNIToMOR len\n"); + return -1; + } + //printf("MOR: "); DumpMorString(morBuf); + if (strlen(morBuf) + 1 != outLen) { + fprintf(stderr, "Expected length != actual length\n"); + return -1; + } + + // check vs. original + if (strcmp(kMORTest, morBuf) != 0) { + fprintf(stderr, "Test string corrupted by double conversion\n"); + return -1; + } + +#ifdef USE_UTF16 + static const UNICHAR kNonMorUniStr[] = L"nlTest\u7e2e\u3080.shk"; + static const UNICHAR kBadUniStr[] = L"nlTest\u7e2e\x30"; +#else + static const UNICHAR kNonMorUniStr[] = "nlTest\xe7\xb8\xae\xe3\x82\x80.shk"; + static const UNICHAR kBadUniStr[] = "nlTest\x81\xe7"; +#endif + static const char kNonMorExpected[] = "nlTest??.shk"; + static const char kBadExpected[] = "nlTest??"; + + NuConvertUNIToMOR(kNonMorUniStr, morBuf, sizeof(morBuf)); + if (strcmp(morBuf, kNonMorExpected) != 0) { + fprintf(stderr, "Non-MOR string conversion failed\n"); + return -1; + } + + NuConvertUNIToMOR(kBadUniStr, morBuf, sizeof(morBuf)); + if (strcmp(morBuf, kBadExpected) != 0) { + fprintf(stderr, "Bad UNI string conversion failed\n"); + return -1; + } + + printf("... string conversion tests successful\n"); + + return 0; +} + +/* + * Create a new entry and give it a trivial data fork. + */ +int AddTestEntry(NuArchive* pArchive, const char* entryNameMOR) +{ + NuDataSource* pDataSource = NULL; + NuRecordIdx recordIdx; + static const char* kTestMsg = "Hello, world!\n"; + uint32_t status; + NuError err; + + /* + * Add our test entry. + */ + err = AddSimpleRecord(pArchive, entryNameMOR, &recordIdx); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: add record failed (err=%d)\n", err); + goto failed; + } + + err = NuCreateDataSourceForBuffer(kNuThreadFormatUncompressed, + 0, (const uint8_t*)kTestMsg, 0, strlen(kTestMsg), NULL, + &pDataSource); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: source create failed (err=%d)\n", err); + goto failed; + } + + err = NuAddThread(pArchive, recordIdx, kNuThreadIDDataFork, pDataSource, + NULL); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: thread add failed (err=%d)\n", err); + goto failed; + } + pDataSource = NULL; /* now owned by library */ + + /* + * Flush changes. + */ + err = NuFlush(pArchive, &status); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: couldn't flush after add (err=%d, status=%u)\n", + err, status); + goto failed; + } + + return 0; +failed: + if (pDataSource != NULL) + NuFreeDataSource(pDataSource); + return -1; +} + +/* + * Extract the file we created. + */ +int TestExtract(NuArchive* pArchive, const char* entryNameMOR) +{ + const NuRecord* pRecord; + NuRecordIdx recordIdx; + NuError err; + + err = NuGetRecordIdxByName(pArchive, entryNameMOR, &recordIdx); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: couldn't find '%s' (err=%d)\n", + entryNameMOR, err); + return -1; + } + err = NuGetRecord(pArchive, recordIdx, &pRecord); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: couldn't get record index %u (err=%d)\n", + recordIdx, err); + return -1; + } + assert(pRecord != NULL); + + const NuThread* pThread = NULL; + uint32_t idx; + for (idx = 0; idx < NuRecordGetNumThreads(pRecord); idx++) { + pThread = NuGetThread(pRecord, idx); + + if (NuGetThreadID(pThread) == kNuThreadIDDataFork) + break; + } + if (pThread == NULL) { + fprintf(stderr, "ERROR: no data thread?\n"); + return -1; + } + + /* + * Prepare the output file. + */ + UNICHAR* entryNameUNI = CopyMORToUNI(entryNameMOR); + NuDataSink* pDataSink = NULL; + err = NuCreateDataSinkForFile(true, kNuConvertOff, entryNameUNI, + kLocalFssep, &pDataSink); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: unable to create data sink for file (err=%d)\n", + err); + free(entryNameUNI); + return -1; + } + + err = NuExtractThread(pArchive, pThread->threadIdx, pDataSink); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: extract failed (err=%d)\n", err); + (void) NuFreeDataSink(pDataSink); + free(entryNameUNI); + return -1; + } + + (void) NuFreeDataSink(pDataSink); + + printf("... confirming extraction of '%s'\n", entryNameUNI); + if (access(entryNameUNI, F_OK) != 0) { + fprintf(stderr, "ERROR: unable to read '%s' (err=%d)\n", + entryNameUNI, errno); + free(entryNameUNI); + return -1; + } + + if (unlink(entryNameUNI) < 0) { + perror("unlink test entry"); + free(entryNameUNI); + return -1; + } + + free(entryNameUNI); + return 0; +} + +/* + * Run some tests. + * + * Returns 0 on success, -1 on error. + */ +int DoTests(void) +{ + NuError err; + NuArchive* pArchive = NULL; + char* testEntryNameMOR = NULL; + int result = 0; + + if (TestStringConversion() < 0) { + goto failed; + } + + /* + * Make sure we're starting with a clean slate. + */ + if (RemoveTestFile("Test archive", kTestArchive) < 0) { + goto failed; + } + if (RemoveTestFile("Test temp file", kTestTempFile) < 0) { + goto failed; + } + if (RemoveTestFile("Test entry", kTestEntryName) < 0) { + goto failed; + } + + testEntryNameMOR = CopyUNIToMOR(kTestEntryName); + + /* + * Create a new archive to play with. + */ + err = NuOpenRW(kTestArchive, kTestTempFile, kNuOpenCreat|kNuOpenExcl, + &pArchive); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: NuOpenRW failed (err=%d)\n", err); + goto failed; + } + if (NuSetErrorMessageHandler(pArchive, ErrorMessageHandler) == + kNuInvalidCallback) + { + fprintf(stderr, "ERROR: couldn't set message handler\n"); + goto failed; + } + + /* + * Add a single entry. + */ + if (AddTestEntry(pArchive, testEntryNameMOR) != 0) { + goto failed; + } + + printf("... checking presence of '%s' and '%s'\n", + kTestArchive, kTestTempFile); + + if (access(kTestTempFile, F_OK) != 0) { + /* in theory, NufxLib doesn't need to use the temp file we provide, + so this test isn't entirely sound */ + fprintf(stderr, "ERROR: did not find %s (err=%d)\n", + kTestTempFile, err); + goto failed; + } + + /* + * Close it and confirm that the file has the expected name. + */ + err = NuClose(pArchive); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: mid NuClose failed (err=%d)\n", err); + goto failed; + } + pArchive = NULL; + + if (access(kTestArchive, F_OK) != 0) { + fprintf(stderr, "ERROR: did not find %s (err=%d)\n", kTestArchive, err); + goto failed; + } + + /* + * Reopen it read-only. + */ + printf("... reopening archive read-only\n"); + err = NuOpenRO(kTestArchive, &pArchive); + if (err != kNuErrNone) { + fprintf(stderr, "ERROR: NuOpenRO failed (err=%d)\n", err); + goto failed; + } + if (NuSetErrorMessageHandler(pArchive, ErrorMessageHandler) == + kNuInvalidCallback) + { + fprintf(stderr, "ERROR: couldn't set message handler\n"); + goto failed; + } + + /* + * Extract the file. + */ + if (TestExtract(pArchive, testEntryNameMOR) < 0) { + goto failed; + } + + /* + * That's all, folks... + */ + NuClose(pArchive); + pArchive = NULL; + + printf("... removing '%s'\n", kTestArchive); + if (unlink(kTestArchive) < 0) { + perror("unlink kTestArchive"); + goto failed; + } + + +leave: + if (pArchive != NULL) { + NuAbort(pArchive); + NuClose(pArchive); + } + free(testEntryNameMOR); + return result; + +failed: + result = -1; + goto leave; +} + + +/* + * Start here. + */ +int main(void) +{ + int32_t major, minor, bug; + const char* pBuildDate; + const char* pBuildFlags; + int cc; + + (void) NuGetVersion(&major, &minor, &bug, &pBuildDate, &pBuildFlags); + printf("Using NuFX library v%d.%d.%d, built on or after\n" + " %s with [%s]\n\n", + major, minor, bug, pBuildDate, pBuildFlags); + + if (NuSetGlobalErrorMessageHandler(ErrorMessageHandler) == + kNuInvalidCallback) + { + fprintf(stderr, "ERROR: can't set the global message handler"); + exit(1); + } + + printf("... starting tests\n"); + + cc = DoTests(); + + printf("... tests ended, %s\n", cc == 0 ? "SUCCESS" : "FAILURE"); + exit(cc != 0); +} + diff --git a/nufxlib/samples/TestSimple.c b/nufxlib/samples/TestSimple.c index 45e51df..cc77e8b 100644 --- a/nufxlib/samples/TestSimple.c +++ b/nufxlib/samples/TestSimple.c @@ -21,12 +21,20 @@ * header, a filename thread, or a default value ("UNKNOWN", stuffed in * when a record has no filename at all). */ -NuResult -ShowContents(NuArchive* pArchive, void* vpRecord) +NuResult ShowContents(NuArchive* pArchive, void* vpRecord) { const NuRecord* pRecord = (NuRecord*) vpRecord; - printf("*** Filename = '%s'\n", pRecord->filename); + size_t bufLen = NuConvertMORToUNI(pRecord->filenameMOR, NULL, 0); + if (bufLen == (size_t) -1) { + fprintf(stderr, "GLITCH: unable to convert '%s'\n", + pRecord->filenameMOR); + } else { + UNICHAR* buf = (UNICHAR*) malloc(bufLen); + NuConvertMORToUNI(pRecord->filenameMOR, buf, bufLen); + printf("*** Filename = '%s'\n", buf); + free(buf); + } return kNuOK; } @@ -38,11 +46,10 @@ ShowContents(NuArchive* pArchive, void* vpRecord) * If we're not interested in handling an archive on stdin, we could just * pass the filename in here and use NuOpenRO instead. */ -int -DoStreamStuff(FILE* fp) +int DoStreamStuff(FILE* fp) { NuError err; - NuArchive* pArchive = nil; + NuArchive* pArchive = NULL; err = NuStreamOpenRO(fp, &pArchive); if (err != kNuErrNone) { @@ -59,7 +66,7 @@ DoStreamStuff(FILE* fp) } bail: - if (pArchive != nil) { + if (pArchive != NULL) { NuError err2 = NuClose(pArchive); if (err == kNuErrNone) err = err2; @@ -72,16 +79,15 @@ bail: /* * Grab the name of an archive to read. If "-" was given, use stdin. */ -int -main(int argc, char** argv) +int main(int argc, char** argv) { - long major, minor, bug; + int32_t major, minor, bug; const char* pBuildDate; - FILE* infp = nil; + FILE* infp = NULL; int cc; - (void) NuGetVersion(&major, &minor, &bug, &pBuildDate, nil); - printf("Using NuFX lib %ld.%ld.%ld built on or after %s\n", + (void) NuGetVersion(&major, &minor, &bug, &pBuildDate, NULL); + printf("Using NuFX lib %d.%d.%d built on or after %s\n", major, minor, bug, pBuildDate); if (argc != 2) { @@ -93,7 +99,7 @@ main(int argc, char** argv) infp = stdin; else { infp = fopen(argv[1], kNuFileOpenReadOnly); - if (infp == nil) { + if (infp == NULL) { fprintf(stderr, "ERROR: unable to open '%s'\n", argv[1]); exit(1); } diff --git a/nufxlib/samples/TestTwirl.c b/nufxlib/samples/TestTwirl.c index be996a1..8e8c34e 100644 --- a/nufxlib/samples/TestTwirl.c +++ b/nufxlib/samples/TestTwirl.c @@ -29,16 +29,15 @@ const int kMaxHeldLen = 1024 * 1024; * A list of CRCs. */ typedef struct CRCList { - int numEntries; - unsigned short* entries; + int numEntries; + uint16_t* entries; } CRCList; /* * Returns true if the compression type is supported, false otherwise. */ -int -CompressionSupported(NuValue compression) +int CompressionSupported(NuValue compression) { int result; @@ -76,8 +75,7 @@ CompressionSupported(NuValue compression) /* * This gets called when a buffer DataSource is no longer needed. */ -NuResult -FreeCallback(NuArchive* pArchive, void* args) +NuResult FreeCallback(NuArchive* pArchive, void* args) { free(args); return kNuOK; @@ -87,8 +85,7 @@ FreeCallback(NuArchive* pArchive, void* args) /* * Dump a CRC list. */ -void -DumpCRCs(const CRCList* pCRCList) +void DumpCRCs(const CRCList* pCRCList) { int i; @@ -101,10 +98,9 @@ DumpCRCs(const CRCList* pCRCList) /* * Free a CRC list. */ -void -FreeCRCs(CRCList* pCRCList) +void FreeCRCs(CRCList* pCRCList) { - if (pCRCList == nil) + if (pCRCList == NULL) return; free(pCRCList->entries); @@ -117,21 +113,20 @@ FreeCRCs(CRCList* pCRCList) * We assume there are at most two data threads (e.g. data fork and rsrc * fork) in a record. * - * Returns the list on success, nil on failure. + * Returns the list on success, NULL on failure. */ -CRCList* -GatherCRCs(NuArchive* pArchive) +CRCList* GatherCRCs(NuArchive* pArchive) { NuError err = kNuErrNone; const NuMasterHeader* pMasterHeader; - CRCList* pCRCList = nil; - unsigned short* pEntries = nil; + CRCList* pCRCList = NULL; + uint16_t* pEntries = NULL; long recCount, maxCRCs; long recIdx, crcIdx; int i; pCRCList = malloc(sizeof(*pCRCList)); - if (pCRCList == nil) { + if (pCRCList == NULL) { fprintf(stderr, "ERROR: couldn't alloc CRC list\n"); err = kNuErrGeneric; goto bail; @@ -148,7 +143,7 @@ GatherCRCs(NuArchive* pArchive) maxCRCs = recCount * 2; pEntries = malloc(sizeof(*pEntries) * maxCRCs); - if (pEntries == nil) { + if (pEntries == NULL) { fprintf(stderr, "ERROR: unable to alloc CRC list (%ld entries)\n", maxCRCs); err = kNuErrGeneric; @@ -178,12 +173,12 @@ GatherCRCs(NuArchive* pArchive) err = NuGetRecord(pArchive, recordIdx, &pRecord); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: unable to get recordIdx %ld\n", recordIdx); + fprintf(stderr, "ERROR: unable to get recordIdx %u\n", recordIdx); goto bail; } if (NuRecordGetNumThreads(pRecord) == 0) { - fprintf(stderr, "ERROR: not expecting empty record (%ld)!\n", + fprintf(stderr, "ERROR: not expecting empty record (%u)!\n", recordIdx); err = kNuErrGeneric; goto bail; @@ -211,7 +206,7 @@ GatherCRCs(NuArchive* pArchive) bail: if (err != kNuErrNone) { FreeCRCs(pCRCList); - pCRCList = nil; + pCRCList = NULL; } return pCRCList; } @@ -228,16 +223,15 @@ bail: * * Returns 0 on success, nonzero on failure. */ -int -CompareCRCs(NuArchive* pArchive, const CRCList* pOldCRCList) +int CompareCRCs(NuArchive* pArchive, const CRCList* pOldCRCList) { - CRCList* pNewCRCList = nil; + CRCList* pNewCRCList = NULL; int result = -1; int badCrc = 0; int i; pNewCRCList = GatherCRCs(pArchive); - if (pNewCRCList == nil) { + if (pNewCRCList == NULL) { fprintf(stderr, "ERROR: unable to gather new list\n"); goto bail; } @@ -274,18 +268,17 @@ bail: * * All of this good stuff gets queued up until the next NuFlush call. */ -NuError -RecompressThread(NuArchive* pArchive, const NuRecord* pRecord, +NuError RecompressThread(NuArchive* pArchive, const NuRecord* pRecord, const NuThread* pThread) { NuError err = kNuErrNone; - NuDataSource* pDataSource = nil; - NuDataSink* pDataSink = nil; - unsigned char* buf = nil; + NuDataSource* pDataSource = NULL; + NuDataSink* pDataSink = NULL; + uint8_t* buf = NULL; if (pThread->actualThreadEOF == 0) { buf = malloc(1); - if (buf == nil) { + if (buf == NULL) { fprintf(stderr, "ERROR: failed allocating trivial buffer\n"); err = kNuErrGeneric; goto bail; @@ -295,8 +288,8 @@ RecompressThread(NuArchive* pArchive, const NuRecord* pRecord, * Create a buffer and data sink to hold the data. */ buf = malloc(pThread->actualThreadEOF); - if (buf == nil) { - fprintf(stderr, "ERROR: failed allocating %ld bytes\n", + if (buf == NULL) { + fprintf(stderr, "ERROR: failed allocating %u bytes\n", pThread->actualThreadEOF); err = kNuErrGeneric; goto bail; @@ -314,8 +307,8 @@ RecompressThread(NuArchive* pArchive, const NuRecord* pRecord, */ err = NuExtractThread(pArchive, pThread->threadIdx, pDataSink); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: failed extracting thread %ld in '%s': %s\n", - pThread->threadIdx, pRecord->filename, NuStrError(err)); + fprintf(stderr, "ERROR: failed extracting thread %u in '%s': %s\n", + pThread->threadIdx, pRecord->filenameMOR, NuStrError(err)); goto bail; } } @@ -325,7 +318,7 @@ RecompressThread(NuArchive* pArchive, const NuRecord* pRecord, */ err = NuDeleteThread(pArchive, pThread->threadIdx); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: unable to delete thread %ld\n", + fprintf(stderr, "ERROR: unable to delete thread %u\n", pThread->threadIdx); goto bail; } @@ -340,19 +333,19 @@ RecompressThread(NuArchive* pArchive, const NuRecord* pRecord, fprintf(stderr, "ERROR: unable to create data source (err=%d)\n", err); goto bail; } - buf = nil; + buf = NULL; /* * Create replacement thread. */ err = NuAddThread(pArchive, pRecord->recordIdx, NuGetThreadID(pThread), - pDataSource, nil); + pDataSource, NULL); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: unable to add new thread ID=0x%08lx (err=%d)\n", + fprintf(stderr, "ERROR: unable to add new thread ID=0x%08x (err=%d)\n", NuGetThreadID(pThread), err); goto bail; } - pDataSource = nil; /* now owned by NufxLib */ + pDataSource = NULL; /* now owned by NufxLib */ bail: NuFreeDataSink(pDataSink); @@ -367,21 +360,20 @@ bail: * The amount of data we're holding in memory as a result of the * recompression is placed in "*pLen". */ -NuError -RecompressRecord(NuArchive* pArchive, NuRecordIdx recordIdx, long* pLen) +NuError RecompressRecord(NuArchive* pArchive, NuRecordIdx recordIdx, long* pLen) { NuError err = kNuErrNone; const NuRecord* pRecord; const NuThread* pThread; int i; - printf(" Recompressing %ld\n", recordIdx); + printf(" Recompressing %u\n", recordIdx); *pLen = 0; err = NuGetRecord(pArchive, recordIdx, &pRecord); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: unable to get record %ld (err=%d)\n", + fprintf(stderr, "ERROR: unable to get record %u (err=%d)\n", recordIdx, err); goto bail; } @@ -393,8 +385,8 @@ RecompressRecord(NuArchive* pArchive, NuRecordIdx recordIdx, long* pLen) NuGetThreadID(pThread));*/ err = RecompressThread(pArchive, pRecord, pThread); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: failed recompressing thread %ld " - " in record %ld (err=%d)\n", + fprintf(stderr, "ERROR: failed recompressing thread %u " + " in record %u (err=%d)\n", pThread->threadIdx, pRecord->recordIdx, err); goto bail; } @@ -412,23 +404,22 @@ bail: /* * Recompress every data thread in the archive. */ -NuError -RecompressArchive(NuArchive* pArchive, NuValue compression) +NuError RecompressArchive(NuArchive* pArchive, NuValue compression) { NuError err = kNuErrNone; - NuRecordIdx* pIndices = nil; + NuRecordIdx* pIndices = NULL; NuAttr countAttr; long heldLen; long idx; err = NuSetValue(pArchive, kNuValueDataCompression, compression); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: unable to set compression to %ld (err=%d)\n", + fprintf(stderr, "ERROR: unable to set compression to %u (err=%d)\n", compression, err); goto bail; } - printf("Recompressing threads with compression type %ld\n", compression); + printf("Recompressing threads with compression type %u\n", compression); err = NuGetAttr(pArchive, kNuAttrNumRecords, &countAttr); if (err != kNuErrNone) { @@ -446,8 +437,8 @@ RecompressArchive(NuArchive* pArchive, NuValue compression) * record to "disappear" during processing, we will know about it. */ pIndices = malloc(countAttr * sizeof(*pIndices)); - if (pIndices == nil) { - fprintf(stderr, "ERROR: malloc on %ld indices failed\n", countAttr); + if (pIndices == NULL) { + fprintf(stderr, "ERROR: malloc on %u indices failed\n", countAttr); err = kNuErrGeneric; goto bail; } @@ -470,7 +461,7 @@ RecompressArchive(NuArchive* pArchive, NuValue compression) err = RecompressRecord(pArchive, pIndices[idx], &recHeldLen); if (err != kNuErrNone) { - fprintf(stderr, "ERROR: failed recompressing record %ld (err=%d)\n", + fprintf(stderr, "ERROR: failed recompressing record %u (err=%d)\n", pIndices[idx], err); goto bail; } @@ -478,7 +469,7 @@ RecompressArchive(NuArchive* pArchive, NuValue compression) heldLen += recHeldLen; if (heldLen > kMaxHeldLen) { - long statusFlags; + uint32_t statusFlags; printf(" (flush)\n"); err = NuFlush(pArchive, &statusFlags); @@ -500,12 +491,11 @@ bail: /* * Initiate the twirling. */ -int -TwirlArchive(const char* filename) +int TwirlArchive(const char* filename) { NuError err = kNuErrNone; - NuArchive* pArchive = nil; - CRCList* pCRCList = nil; + NuArchive* pArchive = NULL; + CRCList* pCRCList = NULL; int compression; int cc; @@ -534,7 +524,7 @@ TwirlArchive(const char* filename) } pCRCList = GatherCRCs(pArchive); - if (pCRCList == nil) { + if (pCRCList == NULL) { fprintf(stderr, "ERROR: unable to get CRC list\n"); goto bail; } @@ -545,7 +535,7 @@ TwirlArchive(const char* filename) for (compression = kNuCompressNone; compression <= kNuCompressBzip2; compression++) { - long statusFlags; + uint32_t statusFlags; if (!CompressionSupported(compression)) continue; @@ -571,7 +561,7 @@ TwirlArchive(const char* filename) for (compression = kNuCompressBzip2; compression >= kNuCompressNone; compression--) { - long statusFlags; + uint32_t statusFlags; if (!CompressionSupported(compression)) continue; @@ -599,7 +589,7 @@ TwirlArchive(const char* filename) bail: FreeCRCs(pCRCList); - if (pArchive != nil) { + if (pArchive != NULL) { NuAbort(pArchive); NuClose(pArchive); } @@ -611,22 +601,21 @@ bail: /* * Copy from the current offset in "srcfp" to a new file called * "outFileName". Returns a writable file descriptor for the new file - * on success, or nil on error. + * on success, or NULL on error. * * (Note "CopyFile()" exists under Win32.) */ -FILE* -MyCopyFile(const char* outFileName, FILE* srcfp) +FILE* MyCopyFile(const char* outFileName, FILE* srcfp) { char buf[24576]; FILE* outfp; size_t count; outfp = fopen(outFileName, kNuFileOpenWriteTrunc); - if (outfp == nil) { + if (outfp == NULL) { fprintf(stderr, "ERROR: unable to open '%s' (err=%d)\n", outFileName, errno); - return nil; + return NULL; } while (!feof(srcfp)) { @@ -636,14 +625,14 @@ MyCopyFile(const char* outFileName, FILE* srcfp) if (fwrite(buf, 1, count, outfp) != count) { fprintf(stderr, "ERROR: failed writing outfp (err=%d)\n", errno); fclose(outfp); - return nil; + return NULL; } } if (ferror(srcfp)) { fprintf(stderr, "ERROR: failed reading srcfp (err=%d)\n", errno); fclose(outfp); - return nil; + return NULL; } return outfp; @@ -652,26 +641,25 @@ MyCopyFile(const char* outFileName, FILE* srcfp) /* * Let's get started. */ -int -main(int argc, char** argv) +int main(int argc, char** argv) { - long major, minor, bug; + int32_t major, minor, bug; const char* pBuildDate; - FILE* srcfp = nil; - FILE* infp = nil; + FILE* srcfp = NULL; + FILE* infp = NULL; int cc; /* don't buffer output */ - setvbuf(stdout, nil, _IONBF, 0); - setvbuf(stderr, nil, _IONBF, 0); + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); - (void) NuGetVersion(&major, &minor, &bug, &pBuildDate, nil); - printf("Using NuFX lib %ld.%ld.%ld built on or after %s\n\n", + (void) NuGetVersion(&major, &minor, &bug, &pBuildDate, NULL); + printf("Using NuFX lib %d.%d.%d built on or after %s\n\n", major, minor, bug, pBuildDate); if (argc == 2) { srcfp = fopen(argv[1], kNuFileOpenReadOnly); - if (srcfp == nil) { + if (srcfp == NULL) { perror("fopen failed"); exit(1); } @@ -683,7 +671,7 @@ main(int argc, char** argv) printf("Copying '%s' to '%s'\n", argv[1], kWorkFileName); infp = MyCopyFile(kWorkFileName, srcfp); - if (infp == nil) { + if (infp == NULL) { fprintf(stderr, "Copy failed, bailing.\n"); exit(1); }