Advertisement
andruhovski

Practic 01

Oct 30th, 2019
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.41 KB | None | 0 0
  1. // Practic1Lab1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. // ossp01.cpp : This file contains the 'main' function. Program execution begins and ends there.
  6. //
  7.  
  8. #include "pch.h"
  9. struct BPB {
  10.     WORD sectorSize;
  11.     BYTE clusterSize;
  12.     WORD reservedSectors;
  13.     BYTE fatCount;
  14.     WORD rootSize;
  15.     WORD totalSectors;
  16.     BYTE media;
  17.     WORD fatSize;
  18.     WORD sectors;
  19.     WORD heads;
  20.     WORD hiddenLow;
  21.     WORD hiddenHigh;
  22.     BYTE largeTotalSectors[4];
  23. };
  24.  
  25. struct bootRecord {
  26.     BYTE jmpCommand[3];
  27.     BYTE vendorName[8];
  28.     BPB extendedBlock;
  29.     BYTE drivePhysicalNumber;
  30.     BYTE rezerved;
  31.     BYTE symbolBrackets;
  32.     BYTE volumeSerialNumber[4];
  33.     BYTE volumeLabel[11];
  34.     BYTE identifierFAT[8];
  35.     BYTE bootCode[450];
  36. } boot;
  37.  
  38. typedef struct root_Entries
  39. {
  40.     BYTE short_FileName[11];
  41.     BYTE fileAttributes;
  42.     BYTE reserved;
  43.     BYTE createTime_ms;
  44.     WORD createTime;
  45.     WORD createDate;
  46.     WORD accessedDate;
  47.     WORD clusterNumber_High;
  48.     WORD modifiedTime;
  49.     WORD modifiedDate;
  50.     WORD firstClusterAddress_FAT12;
  51.     DWORD sizeofFile;
  52. } root;
  53.  
  54. void ScanBootSector();
  55. void ScanRoot();
  56. void ScanFat();
  57.  
  58. WORD FAT[2880];
  59. BYTE Buffer[512 * 9];
  60. WORD* Val;
  61. HANDLE hDisk;
  62.  
  63. int main()
  64. {
  65.     hDisk = CreateFile(TEXT("C:\\Tmp\\Dos6.22.img"),    // drive to open
  66.         GENERIC_READ,                     // Access mode
  67.         FILE_SHARE_READ,                  // Share Mode
  68.         NULL,                             // Security Descriptor
  69.         OPEN_EXISTING,                    // How to create
  70.         0,                                // File attributes
  71.         NULL);                            // Handle to template
  72.     if (hDisk != NULL)
  73.     {
  74.         ScanBootSector();
  75.         ScanRoot();
  76.         ScanFat();
  77.         int cluster = 2619;
  78.         int count = 1;
  79.         while (FAT[cluster] != 0xfff)
  80.         {
  81.             printf("%6x\t", FAT[cluster]);
  82.             cluster = FAT[cluster];
  83.             count++;
  84.         }
  85.         printf("\n%d", count);
  86.     }
  87.     CloseHandle(hDisk);
  88.  
  89.     return 0;
  90. }
  91.  
  92. void ScanFat() {
  93.     DWORD dwBytesRead;
  94.     WORD fatElement;
  95.     int j = 0;
  96.     SetFilePointer(hDisk, 512, NULL, FILE_BEGIN);
  97.     ReadFile(hDisk, Buffer, 512 * 9, &dwBytesRead, NULL);
  98.     if (Buffer[0] != boot.extendedBlock.media) {
  99.         printf("Error 1 \n");
  100.         return;
  101.     }
  102.     //FAT table entries are packed so that two cluster entries occupy three bytes with the following general format :
  103.     //yz Zx XY
  104.     //  where
  105.     //  xyz is the one pointer entry and
  106.     //  XYZ is the second pointer entry.
  107.     //  E.g., bytes :
  108.     //  2d e0 02
  109.     //  refer to clusters 0x02d (45) and 0x02e (46)
  110.    
  111.     for (int i = 0, j = 0; i < 512 * 9; i += 3, j += 2) {      
  112.         FAT[j] = (Buffer[i] + (Buffer[i + 1] << 8)) & 0x0FFF;
  113.         FAT[j+1] = (Buffer[i + 1] + (Buffer[i + 2] << 8)) >> 4;
  114.     }  
  115. }
  116.  
  117. void ScanRoot() {
  118.     DWORD dwFilePointer;
  119.     DWORD dwBytesRead;
  120.     root stRoot;
  121.     BYTE byteRoot[512];
  122.     memset(&byteRoot, 0, 512);
  123.     dwFilePointer = SetFilePointer(hDisk, (512 * 19), NULL, FILE_BEGIN);
  124.     if (dwFilePointer == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
  125.     {
  126.         // Deal with failure
  127.         // . . .
  128.  
  129.     } // End of error handler
  130.     int iSector = 19;
  131.     BOOL bNoEntry = FALSE;
  132.     // Iterate through root directory sectors
  133.     do
  134.     {
  135.         if (!ReadFile(hDisk, byteRoot, 512, &dwBytesRead, NULL))
  136.         {
  137.             printf("Error in Reading Root Entry.\n");
  138.         }
  139.         else
  140.         {
  141.             BYTE *pByteRoot = byteRoot;
  142.             // Iterate through 32 byte entries
  143.             for (int i = 0; i < (512 / 32); i++)
  144.             {
  145.                 ZeroMemory(&stRoot, 32);
  146.                 memcpy(&stRoot, pByteRoot, 32);
  147.  
  148.                 // No entry beyond this used.
  149.                 if (stRoot.short_FileName[0] == 0x00)
  150.                 {
  151.                     // Stop iteration
  152.                     bNoEntry = TRUE;
  153.                     break;
  154.                 }
  155.                 else
  156.                 {
  157.                     // This if loops checks for file deletion
  158.                     // status
  159.                     if (stRoot.short_FileName[0] == 0xE5)
  160.                     {
  161.                         printf("First character of file after deletion :0x%x\n",
  162.                             stRoot.short_FileName[0]);
  163.                         printf("File status: Deleted.\n");
  164.                     }
  165.  
  166.  
  167.                     puts("File Name            :");
  168.                     for (size_t idx = 0; idx < 8; idx++)
  169.                     {
  170.                         putchar(stRoot.short_FileName[idx]);
  171.                     }
  172.                     putchar('\n');
  173.                     if (stRoot.fileAttributes & 0x01)
  174.                         printf("File Attribute       : Read Only File\n");
  175.                     if (stRoot.fileAttributes & 0x02)
  176.                         printf("File Attribute    : Hidden File\n");
  177.                     if (stRoot.fileAttributes & 0x04)
  178.                         printf("File Attribute    : System File\n");
  179.                     if (stRoot.fileAttributes & 0x08)
  180.                         printf("File Attribute    : Volume Label\n");
  181.                     if (stRoot.fileAttributes & 0x0f)
  182.                         printf("File Attribute    : Long File Name\n");
  183.                     if (stRoot.fileAttributes & 0x10)
  184.                         printf("File Attribute    : Directory\n");
  185.                     if (stRoot.fileAttributes & 0x20)
  186.                         printf("File Attribute    : Archive\n");
  187.  
  188.                     WORD nYear = (stRoot.createDate >> 9);
  189.                     WORD nMonth = (stRoot.createDate << 7);
  190.                     nMonth = nMonth >> 12;
  191.                     WORD nDay = (stRoot.createDate << 11);
  192.                     nDay = nDay >> 11;
  193.  
  194.                     //printf("Create Date    : %d/%d/%d\n", nDay, nMonth, (nYear + 1980));
  195.  
  196.                     nYear = (stRoot.modifiedDate >> 9);
  197.                     nMonth = (stRoot.modifiedDate << 7);
  198.                     nMonth = nMonth >> 12;
  199.                     nDay = (stRoot.modifiedDate << 11);
  200.                     nDay = nDay >> 11;
  201.  
  202.                     printf("Modification Date    : %d/%d/%d\n", nDay, nMonth, (nYear + 1980));
  203.  
  204.                     nYear = (stRoot.accessedDate >> 9);
  205.                     nMonth = (stRoot.accessedDate << 7);
  206.                     nMonth = nMonth >> 12;
  207.                     nDay = (stRoot.accessedDate << 11);
  208.                     nDay = nDay >> 11;
  209.  
  210.                     //printf("Accessed Date        : %d/%d/%d\n", nDay, nMonth, (nYear + 1980));
  211.  
  212.                     printf("Start Cluster Address: %d\n",
  213.                         stRoot.firstClusterAddress_FAT12);
  214.                     printf("File Size            : %d bytes\n",
  215.                         stRoot.sizeofFile);
  216.                     pByteRoot += 32;
  217.                 }    // End of else
  218.             }
  219.  
  220.             if (bNoEntry)
  221.                 break;
  222.             else
  223.             {
  224.                 iSector += 1;
  225.             }
  226.         }
  227.     } while (iSector <= 33);
  228.  
  229. }
  230.  
  231. void ScanBootSector()
  232. {
  233.  
  234.     DWORD dwBytesRead;
  235.     char str[128];
  236.     unsigned short num1, num2;
  237.     bool check;
  238.  
  239.     check = ReadFile(hDisk, (void *)&boot, sizeof(bootRecord), &dwBytesRead, NULL);
  240.     if (!check) {
  241.         printf("Error %d", GetLastError());
  242.     }
  243.     strncpy_s(str, (char *)boot.vendorName, 8);
  244.     str[8] = '\0';
  245.     printf("Vendor Name: %s\n", str);
  246.  
  247.     if (boot.drivePhysicalNumber >= 0x80)
  248.         puts("Hard disk");
  249.     else
  250.         puts("Floppy disk");
  251.     if (boot.symbolBrackets == '(')
  252.         puts("Extended block present.");
  253.     else
  254.         puts("Extended block absent.");
  255.     memcpy(&num1, boot.volumeSerialNumber, 2);
  256.     memcpy(&num2, boot.volumeSerialNumber + 2, 2);
  257.     printf("Serial num=%X-%X\n", num1, num2);
  258.  
  259.     strncpy_s(str, (char *)boot.volumeLabel, 11);
  260.     str[11] = '\0';
  261.     printf("Label: %s\n", str);
  262.  
  263.     strncpy_s(str, (char *)boot.identifierFAT, 8);
  264.     str[8] = '\0';
  265.     printf("Ident FAT: %s\n", str);
  266.  
  267.     //printf("%d", sizeof(struct param_bios));
  268.     /*printf("Byte in sector = %d\n", boot.extendedBlock.sectorSize);
  269.     printf("Sector in cluster = %d\n", boot.extendedBlock.clusterSize);
  270.     printf("Reserved sector = %d\n", boot.extendedBlock.reservedSectors);
  271.     printf("FAT Count = %d\n", boot.extendedBlock.fatCount);
  272.     printf("root size = %d\n", boot.extendedBlock.rootSize);
  273.     printf("Total sector = %d\n", boot.extendedBlock.totalSectors);
  274.     printf("Media = %d\n", boot.extendedBlock.media);
  275.     printf("Fat size = %d\n", boot.extendedBlock.fatSize);
  276.     printf("Sectors = %d\n", boot.extendedBlock.sectors);
  277.     printf("Heads = %d\n", boot.extendedBlock.heads);
  278.     printf("hidden Low = %d\n", boot.extendedBlock.hiddenLow);
  279.     printf("hidden High  = %d\n", boot.extendedBlock.hiddenHigh);
  280.     printf("largeTotal Sectors = %d\n", boot.extendedBlock.largeTotalSectors[4]);*/
  281.        
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement