📄 readdpt.cpp
字号:
//Read Disk Partion Table
#include<dos.h>
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int ReadSector(double Sector_Start,int Sector_Num,char * Buffer);//Read Specified sector
void ReadDPT(char * Buffer,int is_Expand);
void ReadDPTItem(char *Buffer);
int ReadExpandPartion(double Start_Position);
double HexToDec(char * cPtr,int Length);
int DecToHex(double DecNum,char *cPtr,int Length);
struct DiskAddressPacket
{
char PacketSize;
char Reserved;
short int BlockCount;
void far * BufferAd;
unsigned char BlockNum[8];
};
const int DPT_START=446;
const int DPT_LENGTH=16;
static int Main_Expartion_Flag=1;
static double Main_Expartion_Start;
void main()
{
unsigned char Buffer[512];
if(!ReadSector(0,1,Buffer))
{
cout<<"\nSome error have ocuured!";
exit(1);
}
ReadDPT(Buffer+DPT_START,0);
getch();
}
int ReadSector(double Sector_Start,int Sector_Num,char * Buffer)//Read Specified sector
{
union REGS in,out;
struct SREGS sregs;
DiskAddressPacket DAP;
DAP.PacketSize=16;
DAP.Reserved=0;
DAP.BlockCount=Sector_Num;
DAP.BufferAd=MK_FP ((char *)FP_SEG (Buffer),Buffer);
if(!(DecToHex(Sector_Start,DAP.BlockNum,8)))
{
return 0;
}
in.h.ah=0x42;
in.h.dl=0x80;
in.x.si=(int)&DAP;
sregs.ds=FP_SEG(&DAP);
int86x(0x13,&in,&out,&sregs);
if(!out.x.cflag)
return 1;
else
return 0;
}
void ReadDPT(char * Buffer,int is_Expand)
{
static int ExpartNum=0;
if(!is_Expand)
printf("\n ******************Disk DPT****************** ");
for ( int i=0;i<4;i++)
{
if(is_Expand)
{
ExpartNum++;
printf("\nThe %d Expand partion parameter is following",ExpartNum);
}
else
printf("\nThe %d Main partion parameter is following",i+1);
ReadDPTItem(Buffer+i*16);
if(is_Expand&&1==i)
break;
}
}
void ReadDPTItem(char *Buffer)
{
unsigned char PartionType;
double Total_Sectors,Start_Position;
//Read partion state;
printf("\n State Start_Position Partion_Size Partion Type");
cout<<endl;
if(Buffer[0])
cout<<" active";
else
cout<<"not active";
//Read partion start positon
Start_Position=HexToDec(Buffer+8,4);
printf("%12.0f ",Start_Position);
//Read partion size
Total_Sectors=HexToDec(Buffer+12,4);
printf(" %12.0f ",Total_Sectors);
//Read partion type
PartionType=Buffer[4];
printf(" %x",PartionType);
if(0xf==PartionType||0x5==PartionType)//expands partion
{
if(Main_Expartion_Flag)
Main_Expartion_Start=Start_Position;
if(!ReadExpandPartion(Start_Position))
printf("\nRead Expand Partoin error!");
}
}
int ReadExpandPartion(double Start_Position)
{
char Buffer[512];
if(!Main_Expartion_Flag)
{
Start_Position+=Main_Expartion_Start;//if not base expartion shoud add
printf("\n Read Expartion ");
}
Main_Expartion_Flag=0;
if(!ReadSector(Start_Position,1,Buffer))
return 0;
ReadDPT(Buffer+DPT_START,1);
return 1;
}
double HexToDec(char * cPtr,int Length)
{
double DecNum=0;
unsigned char oper1,oper2,power=0;
double temp,powresult;
for(int i=0;i<Length;i++)
{
oper1=cPtr[i];
//oper1=oper1&0x0ff;
oper2=oper1;
oper1=oper1&0x0f;
powresult=pow(16,power);
temp=oper1*powresult;
DecNum+=temp;
power++;
oper2=oper2>>4;
oper2=oper2&0x0f;
powresult=pow(16,power);
temp=oper2*powresult;
DecNum+=temp;
power++;
}
return DecNum;
}
int DecToHex(double DecNum,char *cPtr,int Length)
{
double Shang=DecNum,temp;
int Yushu=0;
int ActualLen=0;
int Flag=1;
int ByteTo;
while (Shang>=0.00001)
{
temp=Shang/16;
temp=(long int)temp;
Yushu=Shang-temp*16;
Shang=temp;
if(!(Flag%2))
{
Yushu<<=4;
Yushu&=0x00f0;
cPtr[ActualLen]&=0x0f;
ByteTo=cPtr[ActualLen];
ByteTo|=Yushu;
cPtr[ActualLen]=(char)ByteTo;
ActualLen++;
}
else
{
cPtr[ActualLen]=Yushu;
}
if(Length==ActualLen)
{
return 0;
}
Flag++;
}
for(int i=ActualLen;i<Length;i++)
{
if(!(Flag%2))
i++;
if(Length==i)
break;
cPtr[i]=0;
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -