Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <windows.h>
- #include <WinIoCtl.h>
- #include <malloc.h>
- #include <string.h>
- WINBASEAPI BOOL WINAPI SetFilePointerEx(
- HANDLE hFile,
- LARGE_INTEGER liDistanceToMove,
- PLARGE_INTEGER lpNewFilePointer,
- DWORD dwMoveMethod
- );
- #define INVALID_SET_FILE_POINTER ((DWORD)-1)
- void MuestraError()
- { DWORD Coderror = 0;
- LPTSTR TextoError;
- Coderror = GetLastError();
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, Coderror, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) , (LPTSTR)&TextoError, 0, NULL );
- wprintf(L"\nHa ocurrido el error %d: %s", Coderror, TextoError); //Versión de printf para formato ancho (unicode)
- }
- void pausa(void)
- { BYTE tecla;
- scanf(&tecla); fflush(stdin);
- }
- void MuestraTipoParticion(BYTE tipoParticion)
- { printf("%02X - ", tipoParticion);
- switch(tipoParticion)
- { case 0x00: printf("No usada \n"); break;
- case 0x01: printf("FAT12\n"); break;
- case 0x05: printf("Particion extendida\n"); break;
- case 0x06: printf("FAT16 (DOS 3.31) \n"); break;
- case 0x07: printf("NTFS\n"); break;
- case 0x0B: printf("FAT32\n"); break;
- case 0x0C: printf("FAT32 (usando LBA)\n"); break;
- case 0x16: printf("FAT16 Oculta\n"); break;
- case 0x1B: printf("FAT32 Oculta\n"); break;
- case 0x82: printf("Linux swap\n"); break;
- case 0x83: printf("ext2fs (Linux)\n"); break;
- default: printf("Desconocida\n");
- }
- }
- BOOL leeSector(HANDLE Hnd,LARGE_INTEGER PosInicio,LPDWORD BufferSector)
- {
- DWORD readBytes;
- if(SetFilePointerEx(Hnd, PosInicio, NULL, FILE_BEGIN))
- {
- if(ReadFile(Hnd, BufferSector, 512, &readBytes, NULL))
- {
- return 1;
- }
- }
- return 0;
- }
- void main(void)
- {
- HANDLE disp;
- DWORD size;
- //DWORD* buffer;
- DWORD readBytes;
- DWORD i;
- DWORD j;
- LARGE_INTEGER index;
- BYTE bufferSector[512];
- LARGE_INTEGER indexRoot;
- struct _DRIVE_LAYOUT_INFORMATION* buffer;
- struct _PARTITION_INFORMATION partition;
- //****************************************
- //*** Apartado 1: Abrir el dispositivo ***
- //****************************************
- disp = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ,
- NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if(disp == INVALID_HANDLE_VALUE)
- {
- MuestraError();
- return;
- }
- //****************************************************************************
- //*** Apartado 2: Mostrar la información de las particiones mediante IOCTL ***
- //****************************************************************************
- size = sizeof(DRIVE_LAYOUT_INFORMATION) + 16* sizeof(PARTITION_INFORMATION);
- buffer = (DWORD *) malloc(size);
- DeviceIoControl(disp, IOCTL_DISK_GET_DRIVE_LAYOUT, NULL, 0, buffer, size, &readBytes, NULL);
- for(i=0; i<buffer->PartitionCount; i++)
- {
- partition = buffer->PartitionEntry[i];
- MuestraTipoParticion(partition.PartitionType);
- if(partition.PartitionType != 0) //muestra datos
- {
- if(partition.PartitionType == 0x1B)
- index = partition.StartingOffset;
- printf("Partition Number:\t%d\n", partition.PartitionNumber);
- printf("Starting Offset:\t%08X\n", partition.StartingOffset);
- printf("Partition Length:\t%08X\n", partition.PartitionLength);
- printf("Active partition:\t");
- partition.BootIndicator ? printf("true\n") : printf("false\n");
- }
- printf("\n");
- }
- //***********************************************************
- //*** Apartado 3: Sector de arranque de una partición FAT ***
- //***********************************************************
- leeSector(disp, index, bufferSector);
- for(i = 0; i<512; i++)
- {
- printf("0x%02X ", bufferSector[i]);
- }
- printf("\n Manufacturer ID: ");
- for(i = 3; i<8 && bufferSector[i]!= '\0'; i++)
- {
- printf("%c", bufferSector[i]);
- }
- printf("\n");
- //******************************************************************************
- //*** Apartado 4: Mostrar los ficheros del primer sector del directorio raíz ***
- //******************************************************************************
- //ByteComienzoDirRaiz = ByteComienzoSectorArranque + (SectoresReservados + NºFats * NºSectoresPorCopiaFAT) * 512
- //indexRoot.QuadPart = index.QuadPart + ((WORD)bufferSector[0x00E] + ((DWORD)bufferSector[0x02c] * bufferSector[0x00D]) + bufferSector[0x010] * (DWORD)bufferSector[0x024]) *512;
- //indexRoot.QuadPart = index.QuadPart + (DWORD)bufferSector[0x02c] * bufferSector[0x00D];
- //indexRoot.QuadPart = ((DWORD)bufferSector[0x02c] -2) * bufferSector[0x00D] + (WORD)bufferSector[0x00E] + bufferSector[0x010] * (DWORD)bufferSector[0x024];
- indexRoot.QuadPart = 8024 + index.QuadPart;
- leeSector(disp, indexRoot, bufferSector);
- for(i = 0; i< 512; i+=32)
- {
- for(j = 0; j<8 && bufferSector[j+i]!= '\0'; j++)
- {
- printf("%c", bufferSector[j+i]);
- }
- printf(".");
- for(j = 8; j<11 && bufferSector[j+i]!= '\0'; j++)
- {
- printf("%c", bufferSector[j+i]);
- }
- printf("\n");
- }
- //*****************************************
- //*** Apartado 5: Cerrar el dispositivo ***
- //*****************************************
- if(CloseHandle(disp) == 0)
- MuestraError();
- return;
- }
Add Comment
Please, Sign In to add comment