📄 mini.c
字号:
CMiniGUIDlg *p2=(CMiniGUIDlg *)p;
Pinfo **pInfo=p2->argu.partitionTable;
short drive=(p2->argu.mainArg ? p2->argu.mainArg->drive : 0);
char *file=(p2->argu.mode==SAVE ? p2->argu.fileStr : NULL);
char mode=p2->argu.mode;
char pause=p2->argu.pause;
char yes=p2->argu.yesTRUE;
short driveNum=1;
unsigned long start=0;
short i,j,k,l,m=0;
char buf[512];
FILE *f=NULL;
/*=fopen(file,"wb+"); */
short int13Return;
char done=0;
char FAT_NTFS_ID[]={0x04,0x06,0x0B,0x0C,0x01,0x0E,
0x14,0x16,0x1B,0x1C,0x11,0x1E,
0x07,0x17,0x00};
unsigned long offsetAllRecord=0;
short lastSpare=0;
int driveNumber=0;
CString report;
int preDrive;
char foundPrimary=0;
int totalPart=0;
int sectorToDo=0;
unsigned long sectorToSave=1;
if(mode==SAVE)
{
if(fopenFile(&f, 0, file, NULL)==-1) goto end;
fwrite(miniTOtag,strlen(miniTOtag),1,f);
}
p2->start=true;
((CProgressCtrl *)p2->GetDlgItem(IDC_PROGRESS1))->SetPos(0);
if(pInfo[0]) preDrive=(drive ? drive : pInfo[0]->hardDrive);
for(i=0;pInfo[i];i++)
{
if(pInfo[i]->hardDrive!=preDrive)
{
if(drive)
{
if(pInfo[i]->hardDrive<preDrive) continue;
else break;
}
foundPrimary=0;
}
if(!pInfo[i]->logicDrive)
{
if(!foundPrimary) foundPrimary=1;
else
{
sectorToDo++;
continue;
}
}
for(j=0;FAT_NTFS_ID[j];j++)
{
if(pInfo[i]->ID==FAT_NTFS_ID[j])
{
totalPart++;
break;
}
}
if(!FAT_NTFS_ID[j]) sectorToDo++;
preDrive=pInfo[i]->hardDrive;
}
sectorToDo+=totalPart*2;
report.Format("总任务是 %lu 个扇区。",sectorToDo);
p2->MessageBox(report);
/*
CString a;
a.Format("%d",totalPart);
p2->MessageBox(a);
*/
/*printf("Press ESC to exit...\n");*/
printf("Working...\n");
for(i=0,l=0;l<2;l++)
if(pInfo[l]->hardDrive==-127 || pInfo[l]->hardDrive==-126) i++;
for(m=0;pInfo[i];driveNum++)
{
for(j=0,i-=1; pInfo[i+(j==0 ? 1 : 0)] && pInfo[i+(j==0 ? 1 : 0)]->hardDrive==driveNum;i++,j++)
{
short k;
char FAT_NTFS=0;
if(drive && driveNum!=drive)
{
/*printf("continue..."); */
continue;
}
if(j==0) start=0;
else
{
/*if(pInfo[i]->logicDrive==0) /*continue; */
for(k=0;FAT_NTFS_ID[k];k++)
if(FAT_NTFS_ID[k]==pInfo[i]->ID) FAT_NTFS=1;
if(!FAT_NTFS && !pInfo[i]->logicDrive) break;
start=pInfo[i]->start-(pInfo[i]->logicDrive ? 63 : 0);
}
{
short doLogicDrive=2;
done=1;
while(1)
{
if(p2->stop==true)
{
((CProgressCtrl *)p2->GetDlgItem(IDC_PROGRESS1))->SetPos(0);
goto end;
}
if(!yes)
{/*
printf("Save drive_%d sector_%-10lu 1 sector. ",driveNum,start);
if(YesNo("Continue","")!=1) goto skip;*/
report.Format("保存 第 %d 硬盘 第 %lu 扇区 一个扇区吗?",driveNum,start);
if(p2->MessageBox(report,NULL,MB_YESNO)!=IDYES) goto skip;
}
int13Return=extInt13(0x42,
driveNum+127,
start,
1,
buf);
if(int13Return!=0)
{
/*printf("Drive %d sector %lu\n",driveNum,start); */
int13Error(0x42,driveNum+127,start,int13Return);
goto skip;
}
if(mode==PRINT)
{
/*
if(printByte(buf,512,0,&offsetAllRecord,pause,&lastSpare)==-1)
goto end;*/
}
else
{
fwrite(&driveNum,2,1,f);
fwrite(&start,4,1,f);
fwrite(§orToSave,2,1,f);
fwrite(buf,512,1,f);
}
m++;
((CProgressCtrl *)p2->GetDlgItem(IDC_PROGRESS1))->SetPos(m*65535/sectorToDo);
if(!yes)
{
report.Format("保存了 第 %d 硬盘 第 %lu 扇区 一个扇区。",driveNum,start);
p2->MessageBox(report);
}
if(mode==SAVE)
printf("Saved drive_%d sector_%-10lu 1 sector\n",driveNum,start);
/*l++; */
skip :
if(j!=0 && FAT_NTFS && pInfo[i]->logicDrive==1) doLogicDrive--;
if(j==0 || !FAT_NTFS || pInfo[i]->logicDrive==0 || doLogicDrive==0) break;
else start=pInfo[i]->start;
/*printf("pInfo[%d] drive=%d part=%d start=%lu\n",i,pInfo[i]->hardDrive,pInfo[i]->number,start);
*/ }
}
}
if(driveNum==drive) break;
}
/*
if(mode==SAVE)
{
if(!done) printf("No found\n");
else printf("Saved %d sectors in all\n",m);
}
*/
end :
if(f) fclose(f);
p2->start=false;
report.Format("总共保存了 %d 个扇区。",m);
p2->MessageBox(report);
return 0;
}
/*void*/UINT restoreComparePrintPartitionTable(/*short drive, char *file, char mode, char pause, char yes)
*/LPVOID p){
CMiniGUIDlg *p2=(CMiniGUIDlg *)p;
short drive=(p2->argu.mainArg ? p2->argu.mainArg->drive : 0);
char *file=p2->argu.fileStr;
char mode=p2->argu.mode;
char pause=p2->argu.pause;
char yes=p2->argu.yesTRUE;
short driveNum;
short currentDrive;
/*short supportExtInt13=0;*/
unsigned long start=0;
short i,j;
unsigned long readf=0,size=0;
char *buf=NULL;
char *diskBuf=NULL;
FILE *f=NULL;/*=fopen(file,"rb"); */
short int13Return;
unsigned long diff=0;
unsigned long oldDiff=0;
/*unsigned long offsetAllRecord=0; */
char done=0;
unsigned long offsetAllRecord=0;
short lastSpare=0;
int totalPart=0;
int sectorToDo=0;
unsigned long sectorToRestore=0;
unsigned long sectorDone=0;
CString report;
char checkFile[32];
if(fopenFile(&f, 1, file, NULL)==-1) goto end;
memset(checkFile,0,32);
fread(checkFile,strlen(miniTOtag),1,f);
if(strnicmp(miniTOtag,checkFile,strlen(miniTOtag))!=0)
{
p2->MessageBox("备份文件的格式无效。");
return -1;
}
p2->start=true;
((CProgressCtrl *)p2->GetDlgItem(IDC_PROGRESS1))->SetPos(0);
/* hDev=CreateFile(device,deviceGeneric,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
if(hDev==INVALID_HANDLE_VALUE)
{ if(hDev!=INVALID_HANDLE_VALUE) CloseHandle(hDev);
return -1;} */
/*
if(!drive)
{
for(i=0;p2->argu.partitionTable[i];i++)
totalPart++;
}
else
totalPart=p2->argu.driveTable[p2->argu.mainArg->index]->part;
*/
driveNum=(drive ? drive : 1);
/*size=filelength(fileno(f));*/
/*
CString si;
si.Format("%l %l",filelength(fileno(f)),ftell(f));
p2->MessageBox(si);
*/
buf=(char *)malloc(65535);
if(mode==COMPARE) diskBuf=(char *)malloc(65535);
while(1)
{
printf("Working...\n");
if(drive)
{
while(1)
{
if(!fread(&driveNum,2,1,f))
{
goto OUT2;
}
if(driveNum!=drive)
{
if(driveNum<drive)
{
fseek(f,4,SEEK_CUR);
fread(§orToRestore,2,1,f);
fseek(f,512*sectorToRestore,SEEK_CUR);
}
else goto OUT2;
}
else break;
}
}
else
{
if(!fread(&driveNum,2,1,f)) goto OUT2;
}
fseek(f,4,SEEK_CUR);
fread(§orToRestore,2,1,f);
fseek(f,512*sectorToRestore,SEEK_CUR);
sectorToDo+=sectorToRestore;
}
OUT2 :
report.Format("总任务是 %lu 个扇区。",sectorToDo);
p2->MessageBox(report);
driveNum=(drive ? drive : 1);
sectorToRestore=0;
fseek(f,strlen(miniTOtag),SEEK_SET);
while(1)
{
/* readf=ftell(f);*/
printf("Working...\n");
/*printf("read:%ld,all:%ld\n",readf,all); */
/* if(readf>=size) break;*/
/*if(feof(f)) {p2->MessageBox("1 eof"); break;}*/
/*
currentDrive=driveNum;
CString dr;
dr.Format("%d ",driveNum);
p2->MessageBox(dr);*/
if(p2->stop==true)
{
((CProgressCtrl *)p2->GetDlgItem(IDC_PROGRESS1))->SetPos(0);
goto end;
}
if(drive)
{
while(1)
{
/* printf("2 driveNum=%d,start=%lu\n",driveNum,start); */
/*printf("pos=%lu ",ftell(f)); */
/* if(size==ftell(f)) {goto end;}*/
/*if(feof(f))
{
goto end;}*/
if(!fread(&driveNum,2,1,f))
/*fread(&driveNum,2,1,f);
if(ferror(f))
{*/
goto end;
/*printf("driveNum=%d,drive=%d\n",driveNum,drive); */
if(driveNum!=drive)
{
/*printf("if(driveNum!=drive)\ndriveNum=%d,drive=%d\n",driveNum,drive); */
if(driveNum<drive)
{
fseek(f,4,SEEK_CUR);
fread(§orToRestore,2,1,f);
fseek(f,512*sectorToRestore,SEEK_CUR);
/*printf("after fseek,%ld\n",ftell(f));*/
}
else goto end;
}
else break;
}
}
else
{
if(!fread(&driveNum,2,1,f))
/*if(ferror(f))*/ goto end;
}
fread(&start,4,1,f);
fread(§orToRestore,2,1,f);
fread(buf,512*sectorToRestore,1,f);
/* printf("driveNum=%d,start=%lu\n",driveNum,start); */
done=1;
if(!yes)
{
/*printf("%s drive_%d sector_%-10lu 1 sector. ",(mode==RESTORE ? "Restore" : "Replace"),driveNum,start);
if(YesNo("Continue","")!=1) */
report.Format("恢复 第 %d 硬盘 第 %lu 扇区 %lu 个扇区吗?",
driveNum,
start,
sectorToRestore);
if(p2->MessageBox((report),NULL,MB_YESNO)!=IDYES) continue;
}
if(mode==RESTORE || mode==COMPARE)
{
/*
if(driveNum!=currentDrive || drive)
{
supportExtInt13=1;
}
/* if(!supportExtInt13) continue;*/
int13Return=extInt13((mode==RESTORE ? 0X43 : 0x42),
driveNum+127,
start,
sectorToRestore,
(mode==RESTORE ? buf : diskBuf));
if(int13Return!=0)
{
printf("Drive(%d) sector(%lu)\n",driveNum,start);
int13Error((mode==RESTORE ? 0X43 : 0x42),driveNum+127,start,int13Return);
continue;
}
}
if(mode==COMPARE)
{
if(compareByte(diskBuf,buf,512*sectorToRestore,pause,&diff,&offsetAllRecord)==-1)
break;
}
if(mode==PRINT)
{
/*
if(printByte(buf,512*sectorToRestore,0,&offsetAllRecord,pause,&lastSpare)==-1)
goto end;*/
}
sectorDone+=sectorToRestore;
((CProgressCtrl *)p2->GetDlgItem(IDC_PROGRESS1))->SetPos(sectorDone*65535/sectorToDo);
if(!yes)
{
report.Format("恢复了 第 %d 硬盘 第 %lu 扇区 %lu 个扇区。",
driveNum,
start,
sectorToRestore);
p2->MessageBox(report);
}
}
end :
if(mode!=PRINT)
{
if(done)
{
printf("%s %lu sectors in all %s\n",
(mode==RESTORE ? "Restored" : "Compared"),
sectorDone,
(mode==COMPARE ? "with file" : ""));
if(mode==COMPARE) printf("%lu differences found\n",diff);
}
else printf("No found\n");
}
p2->start=false;
report.Format("总共恢复了 %lu 个扇区。",sectorDone);
p2->MessageBox(report);
if(f) fclose(f);
if(buf) free(buf);
if(diskBuf) free(diskBuf);
return 0;
}
short getFATinfo(Pinfo **partitionTable,
/*short driveNum,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -