Guest User

Untitled

a guest
Jun 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <WinIoCtl.h>
  4. #include <malloc.h>
  5. #include <string.h>
  6.  
  7. WINBASEAPI BOOL WINAPI SetFilePointerEx(
  8.     HANDLE hFile,
  9.     LARGE_INTEGER liDistanceToMove,
  10.     PLARGE_INTEGER lpNewFilePointer,
  11.     DWORD dwMoveMethod
  12.     );
  13. #define INVALID_SET_FILE_POINTER ((DWORD)-1)
  14.  
  15. void MuestraError()
  16. {   DWORD Coderror = 0;
  17.     LPTSTR  TextoError;
  18.     Coderror = GetLastError();
  19.     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, Coderror, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) , (LPTSTR)&TextoError, 0, NULL );
  20.     wprintf(L"\nHa ocurrido el error %d: %s", Coderror, TextoError); //Versión de printf para formato ancho (unicode)
  21. }
  22.  
  23. void pausa(void)
  24. {   BYTE tecla;
  25.     scanf(&tecla); fflush(stdin);
  26. }
  27. void MuestraTipoParticion(BYTE tipoParticion)
  28. {   printf("%02X - ", tipoParticion);
  29.     switch(tipoParticion)
  30.     {   case 0x00: printf("No usada \n"); break;
  31.         case 0x01: printf("FAT12\n"); break;
  32.         case 0x05: printf("Particion extendida\n"); break;
  33.         case 0x06: printf("FAT16 (DOS 3.31) \n"); break;
  34.         case 0x07: printf("NTFS\n"); break;
  35.         case 0x0B: printf("FAT32\n"); break;
  36.         case 0x0C: printf("FAT32 (usando LBA)\n"); break;
  37.         case 0x16: printf("FAT16 Oculta\n"); break;
  38.         case 0x1B: printf("FAT32 Oculta\n"); break;
  39.         case 0x82: printf("Linux swap\n"); break;
  40.         case 0x83: printf("ext2fs (Linux)\n"); break;
  41.         default: printf("Desconocida\n");
  42.     }
  43. }
  44.  
  45. BOOL leeSector(HANDLE Hnd,LARGE_INTEGER PosInicio,LPDWORD BufferSector)
  46. {
  47.     DWORD readBytes;
  48.    
  49.     if(SetFilePointerEx(Hnd, PosInicio, NULL, FILE_BEGIN))
  50.     {
  51.         if(ReadFile(Hnd, BufferSector, 512, &readBytes, NULL))
  52.         {
  53.             return 1;
  54.         }
  55.     }
  56.     return 0;
  57. }      
  58.  
  59.  
  60. void main(void)
  61. {
  62.     HANDLE disp;
  63.     DWORD size;    
  64.     //DWORD* buffer;   
  65.     DWORD readBytes;
  66.     DWORD i;
  67.     DWORD j;
  68.     LARGE_INTEGER index;
  69.     BYTE bufferSector[512];
  70.  
  71.     LARGE_INTEGER indexRoot;
  72.  
  73.     struct _DRIVE_LAYOUT_INFORMATION* buffer;
  74.     struct _PARTITION_INFORMATION partition;
  75.  
  76.     //****************************************
  77.     //*** Apartado 1: Abrir el dispositivo ***
  78.     //****************************************
  79.     disp = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ,
  80.         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  81.     if(disp == INVALID_HANDLE_VALUE)
  82.     {
  83.         MuestraError();
  84.         return;
  85.     }
  86.  
  87.     //****************************************************************************
  88.     //*** Apartado 2: Mostrar la información de las particiones mediante IOCTL ***
  89.     //****************************************************************************
  90.  
  91.     size = sizeof(DRIVE_LAYOUT_INFORMATION) + 16* sizeof(PARTITION_INFORMATION);
  92.     buffer = (DWORD *) malloc(size);
  93.  
  94.     DeviceIoControl(disp, IOCTL_DISK_GET_DRIVE_LAYOUT, NULL, 0, buffer, size, &readBytes, NULL);
  95.  
  96.     for(i=0; i<buffer->PartitionCount; i++)
  97.     {
  98.         partition = buffer->PartitionEntry[i];
  99.         MuestraTipoParticion(partition.PartitionType);
  100.        
  101.         if(partition.PartitionType != 0) //muestra datos
  102.         {
  103.             if(partition.PartitionType == 0x1B)
  104.                 index = partition.StartingOffset;
  105.  
  106.             printf("Partition Number:\t%d\n", partition.PartitionNumber);
  107.             printf("Starting Offset:\t%08X\n", partition.StartingOffset);
  108.             printf("Partition Length:\t%08X\n", partition.PartitionLength);
  109.             printf("Active partition:\t");
  110.             partition.BootIndicator ? printf("true\n") : printf("false\n");
  111.         }
  112.         printf("\n");
  113.     }
  114.  
  115.     //***********************************************************
  116.     //*** Apartado 3: Sector de arranque de una partición FAT ***
  117.     //***********************************************************
  118.  
  119.     leeSector(disp, index, bufferSector);
  120.    
  121.     for(i = 0; i<512; i++)
  122.     {
  123.         printf("0x%02X ", bufferSector[i]);
  124.     }
  125.  
  126.     printf("\n Manufacturer ID: ");
  127.     for(i = 3; i<8 && bufferSector[i]!= '\0'; i++)
  128.     {
  129.         printf("%c", bufferSector[i]);
  130.     }
  131.     printf("\n");
  132.     //******************************************************************************
  133.     //*** Apartado 4: Mostrar los ficheros del primer sector del directorio raíz ***
  134.     //******************************************************************************
  135.     //ByteComienzoDirRaiz = ByteComienzoSectorArranque + (SectoresReservados + NºFats * NºSectoresPorCopiaFAT) * 512
  136.     //indexRoot.QuadPart = index.QuadPart + ((WORD)bufferSector[0x00E] + ((DWORD)bufferSector[0x02c] * bufferSector[0x00D]) + bufferSector[0x010] * (DWORD)bufferSector[0x024]) *512;
  137.     //indexRoot.QuadPart = index.QuadPart + (DWORD)bufferSector[0x02c] * bufferSector[0x00D];
  138.     //indexRoot.QuadPart = ((DWORD)bufferSector[0x02c] -2) * bufferSector[0x00D] + (WORD)bufferSector[0x00E] + bufferSector[0x010] * (DWORD)bufferSector[0x024];
  139.     indexRoot.QuadPart = 8024 + index.QuadPart;
  140.  
  141.     leeSector(disp, indexRoot, bufferSector);
  142.    
  143.     for(i = 0; i< 512; i+=32)
  144.     {
  145.         for(j = 0; j<8 && bufferSector[j+i]!= '\0'; j++)
  146.         {
  147.             printf("%c", bufferSector[j+i]);
  148.         }
  149.         printf(".");
  150.         for(j = 8; j<11 && bufferSector[j+i]!= '\0'; j++)
  151.         {
  152.             printf("%c", bufferSector[j+i]);
  153.         }
  154.         printf("\n");
  155.     }
  156.  
  157.  
  158.     //*****************************************
  159.     //*** Apartado 5: Cerrar el dispositivo ***
  160.     //*****************************************
  161.     if(CloseHandle(disp) == 0)
  162.         MuestraError();
  163.  
  164.     return;
  165. }
Add Comment
Please, Sign In to add comment