BOOL GetCluster(PVOLINFO pVolInfo, PFINDINFO pFindInfo, USHORT usClusterIndex) { USHORT usIndex; if (usClusterIndex >= pFindInfo->usTotalClusters) return FALSE; if (!pFindInfo->pInfo->rgClusters[usClusterIndex]) { for (usIndex = pFindInfo->usClusterIndex; usIndex < usClusterIndex; usIndex++) { pFindInfo->pInfo->rgClusters[usIndex + 1] = GetNextCluster( pVolInfo, pFindInfo->pInfo->rgClusters[usIndex]); if (!pFindInfo->pInfo->rgClusters[usIndex + 1]) pFindInfo->pInfo->rgClusters[usIndex + 1] = FAT_EOF; if (pFindInfo->pInfo->rgClusters[usIndex + 1] == FAT_EOF) return FALSE; } } if (pFindInfo->pInfo->rgClusters[usClusterIndex] == FAT_EOF) return FALSE; if (ReadCluster( pVolInfo, pFindInfo->pInfo->rgClusters[usClusterIndex], pFindInfo->pInfo->pDirEntries, 0)) return FALSE; pFindInfo->usClusterIndex = usClusterIndex; return TRUE; }
void DirectoryRead(Directory* dir) { char* buffer = new char[bpb->bytesPerSector]; FatEntry fatEntry; fatEntry.buffer = new char[bpb->bytesPerSector]; fatEntry.cluster = dir->cluster; fatEntry.sector = GetFATSector(fatEntry.cluster); ReadSector(fatEntry.buffer, fatEntry.sector); Sector* sector; while (ReadCluster(&fatEntry, buffer)) { // Allocate a new sector sector = new Sector; sector->buffer = new char[512]; sector->sector = ClusterToSector(fatEntry.cluster); memcpy(sector->buffer, buffer, 512); // Add to list directory.sectors[directory.count] = sector; directory.count++; } // Allocate a new sector sector = new Sector; sector->buffer = new char[512]; sector->sector = ClusterToSector(fatEntry.cluster); memcpy(sector->buffer, buffer, 512); // Add to list directory.sectors[directory.count] = sector; directory.count++; delete[] buffer; delete[] fatEntry.buffer; }
exp bool CopyOut(const char* source, const char* dest) { // Convert the filename into DOS8.3 format char* dosName = new char[12]; ToDos83Name(source, dosName); // Find the file on the disk DirectoryEntry* entry = FindEntry(dosName, &directory); if (!entry) { LastError("CopyOut", "File doesn't exist"); return false; } if (!(entry->attrib & ATTRIB_ARCHIVE)) { LastError("CopyOut", "This is not a file"); return false; } // Update the entry UpdateEntry(entry, 0, 0); std::ofstream outHandle(dest, std::ios::out | std::ios::binary); if (!outHandle.is_open()) return false; // Set up a FatEntry FatEntry fatEntry; fatEntry.cluster = GetClusterFromEntry(entry); fatEntry.sector = GetFATSector(fatEntry.cluster); fatEntry.buffer = new char[bpb->bytesPerSector]; ReadSector(fatEntry.buffer, fatEntry.sector); char* sectorData = new char[bpb->bytesPerSector]; // Read Data while (ReadCluster(&fatEntry, sectorData)) outHandle.write(sectorData, 512); if (!(entry->size % bpb->bytesPerSector)) outHandle.write(sectorData, 512); else outHandle.write(sectorData, entry->size % bpb->bytesPerSector); delete[] fatEntry.buffer; delete[] sectorData; delete[] dosName; outHandle.close(); return true; }
BOOL DumpDirectory(PDRIVEINFO pDrive, ULONG ulDirCluster, PSZ pszPath) { static BYTE szLongName[512]; int iIndex; PDIRENTRY pDir; PBYTE pbCluster; PBYTE pbPath; USHORT usClusters; ULONG ulCluster; ULONG ulBytesNeeded; BYTE _huge * p; PDIRENTRY pEnd; BYTE bCheckSum, bCheck; ULONG ulClustersNeeded; ULONG ulClustersUsed; ULONG ulEntries; static BYTE szShortName[13]; if (!ulDirCluster) { printf("ERROR: Cluster for %s is 0!\n", pszPath); return TRUE; } if (ulDirCluster == pDrive->bpb.RootDirStrtClus) { PULONG pulCluster; ReadFATSector(pDrive, 0); pulCluster = (PULONG)pDrive->pbFATSector; printf("MEDIA BYTES in FAT: %8.8lX\n", *pulCluster); pulCluster = (PULONG)pDrive->pbFATSector + 1; printf("DiskStatus: %8.8lX\n", *pulCluster); // vDumpSector(pDrive->pbFATSector); } //pDrive->ulTotalDirs++; pbPath = malloc(512); usClusters = GetClusterCount(pDrive, ulDirCluster); ulTotalClusters += usClusters; if (fDetailed > 2) printf("\n\nDirectory of %s (%u clusters)\n\n", pszPath, usClusters); ulBytesNeeded = (ULONG)pDrive->bpb.SectorsPerCluster * (ULONG)pDrive->bpb.BytesPerSector * usClusters; pbCluster = calloc(usClusters, pDrive->bpb.SectorsPerCluster * pDrive->bpb.BytesPerSector); if (!pbCluster) { printf("ERROR:Not enough memory!\n"); return FALSE; } ulCluster = ulDirCluster; p = pbCluster; while (ulCluster != FAT_EOF) { ReadCluster(pDrive, ulCluster); memcpy(p, pDrive->pbCluster, pDrive->bpb.SectorsPerCluster * pDrive->bpb.BytesPerSector); ulCluster = GetNextCluster(pDrive, ulCluster); p += pDrive->bpb.SectorsPerCluster * pDrive->bpb.BytesPerSector; } memset(szLongName, 0, sizeof szLongName); pDir = (PDIRENTRY)pbCluster; pEnd = (PDIRENTRY)(pbCluster + ulBytesNeeded - sizeof (DIRENTRY)); ulEntries = 0; bCheck = 0; while (pDir <= pEnd) { if (fDetailed > 3) { ULONG ulOffset = (PBYTE)pDir - pbCluster; if (ulOffset && !(ulOffset % 4096)) printf("-------- NEXT CLUSTER ----------\n"); } if (pDir->bFileName[0] && pDir->bFileName[0] != 0xE5) { if (pDir->bAttr == FILE_LONGNAME) { if (fDetailed > 3) { printf("(longname)\n"); vDumpDirEntry(pDir); } if (strlen(szLongName) && bCheck != pDir->bReserved) { printf("A lost long filename was found: %s\n", szLongName); memset(szLongName, 0, sizeof szLongName); } bCheck = pDir->bReserved; fGetLongName(pDir, szLongName, sizeof szLongName); } else { bCheckSum = 0; for (iIndex = 0; iIndex < 11; iIndex++) { if (bCheckSum & 0x01) { bCheckSum >>=1; bCheckSum |= 0x80; } else bCheckSum >>=1; bCheckSum += pDir->bFileName[iIndex]; } if (strlen(szLongName) && bCheck != bCheckSum) { printf("The longname %s does not belong to %s\\%s\n", szLongName, pszPath, MakeName(pDir, szShortName, sizeof szShortName)); memset(szLongName, 0, sizeof szLongName); } if (fDetailed > 2) { printf("%-8.8s.%-3.3s %2.2X ", pDir->bFileName, pDir->bExtention, pDir->bAttr); if (pDir->bAttr & FILE_DIRECTORY) printf("<DIR> "); else printf("%10lu ", pDir->ulFileSize); printf("%8.8lX ", MAKEP(pDir->wClusterHigh, pDir->wCluster)); if (pDir->fEAS) printf("%2.2X ", pDir->fEAS); else printf(" "); printf("%s\n", szLongName); } if (!(pDir->bAttr & FILE_DIRECTORY)) { ulClustersNeeded = pDir->ulFileSize / pDrive->usClusterSize + (pDir->ulFileSize % pDrive->usClusterSize ? 1:0); ulClustersUsed = GetClusterCount(pDrive,(ULONG)pDir->wClusterHigh * 0x10000 + pDir->wCluster); ulTotalClusters += ulClustersUsed; if (ulClustersNeeded != ulClustersUsed) { printf("File allocation error detected for %s\\%s\n", pszPath, MakeName(pDir, szShortName, sizeof szShortName)); printf("%lu clusters needed, %lu clusters allocated\n", ulClustersNeeded, ulClustersUsed); } } memset(szLongName, 0, sizeof szLongName); } ulEntries++; }
USHORT usReadEAS(PVOLINFO pVolInfo, ULONG ulDirCluster, PSZ pszFileName, PFEALIST * ppFEAL, BOOL fCreate) { PFEALIST pFEAL; ULONG ulCluster; PBYTE pszEAName; PBYTE pRead; USHORT rc; USHORT usClustersUsed; *ppFEAL = NULL; rc = GetEASName(pVolInfo, ulDirCluster, pszFileName, &pszEAName); if (rc) return rc; ulCluster = FindPathCluster(pVolInfo, ulDirCluster, pszEAName, NULL, NULL); free(pszEAName); if ((ulCluster && ulCluster != FAT_EOF) || fCreate) { pFEAL = gdtAlloc(MAX_EA_SIZE, FALSE); if (!pFEAL) return ERROR_NOT_ENOUGH_MEMORY; memset(pFEAL, 0, (size_t) MAX_EA_SIZE); pFEAL->cbList = sizeof (ULONG); } else pFEAL = NULL; if (!ulCluster || ulCluster == FAT_EOF) { *ppFEAL = pFEAL; return 0; } pRead = (PBYTE)pFEAL; if (f32Parms.fMessageActive & LOG_EAS) Message("usReadEAS: Reading (1) cluster %lu", ulCluster); rc = ReadCluster(pVolInfo, ulCluster, pRead, 0); if (rc) { freeseg(pFEAL); return rc; } if (pFEAL->cbList > MAX_EA_SIZE) { freeseg(pFEAL); return ERROR_EAS_DIDNT_FIT; } usClustersUsed = (USHORT)(pFEAL->cbList / pVolInfo->usClusterSize); if (pFEAL->cbList % pVolInfo->usClusterSize) usClustersUsed++; /* vreemd: zonder deze Messages lijkt deze routine mis te gaan. Optimalisatie? */ if (f32Parms.fMessageActive & LOG_EAS) Message("usReadEAS: %u clusters used", usClustersUsed); usClustersUsed--; pRead += pVolInfo->usClusterSize; while (usClustersUsed) { ulCluster = GetNextCluster(pVolInfo, ulCluster); if (!ulCluster) ulCluster = FAT_EOF; if (ulCluster == FAT_EOF) { freeseg(pFEAL); return ERROR_EA_FILE_CORRUPT; } /* vreemd: zonder deze Messages lijkt deze routine mis te gaan. Optimalisatie? */ if (f32Parms.fMessageActive & LOG_EAS) Message("usReadEAS: Reading (2) cluster %lu", ulCluster); rc = ReadCluster(pVolInfo, ulCluster, pRead, 0); if (rc) { freeseg(pFEAL); return rc; } usClustersUsed--; pRead += pVolInfo->usClusterSize; } *ppFEAL = pFEAL; return 0; }