开发者

Read data from a disk in c++

I'm trying to read some data that lies outside of the partition table, I can successfully read the raw data, but it appears to be encoded in Unicode (UTF-8) or something. There is an application that has been written to read this data and display it properly so I know it can be done.

This data is called "Image Safe Data" and it is something Novell ZenWorks places on the disk at the sixth sector (0x05).

T开发者_运维技巧he raw data looks like this:

ZISD♂   Æ☻      ☺   └¿ ├└¿ ☺    ñ3åG    ╓ï╝Y    ≡   ♣   §   ╗ç9%¥⌂+0Kâ¬ê


  :☺               @   f 8 b 3 4 6 6 2 9 3 b 3 2 b b f d c 8 c b d 6 2 2 1 2 1 0 d
2 1 ♫   M H S T R E E ▬   F H I F L 0 0 0 5 9 3     L   \ \ F H 0 1 F S N T H 0
9 \ A P P S \ i m g s \ C X P P N 6 7 1 0 B . z m g $   t r i n i t y - h e a l
 t h . o r g ▬   F H I F L 0 0 0 5 9 3 H   Z N W

I wrote the following code to read the data directly from disk in C++:

#include <iostream>
#include <windows.h>


void main() {
    DWORD nRead;
    char buf[512];

    HANDLE hDisk = CreateFile("\\\\.\\PhysicalDrive0", 
        GENERIC_READ, FILE_SHARE_READ,        
        NULL, OPEN_EXISTING, 0, NULL);

    SetFilePointer(hDisk, 0xA00, 0, FILE_BEGIN);
    ReadFile(hDisk, buf, 512, &nRead, NULL);
    for (int currentpos=0;currentpos < 512;currentpos++) {
        std::cout << buf[currentpos];
    }
    CloseHandle(hDisk);
}

I'm a novice in C++. I need to find a way to output this data so that it can be read by a script if possible. It looks to be delimited by inconsistent characters.


When you print it you're interpreting the data as a char. Some of the values are in the a-zA-Z "normal, printable" range and so print as you'd expect. Others, like '0' are special and unprintable directly. You probably want to print the bytes as hex, not characters. To do that:

std::cout << std::hex << std::setw(2) << std::setfill('0') << unsigned(buf[currentpos]) << " ";

This tells the ostream to print two digits of hex, using 0 if there's less and then forces the char to be printed as an unsigned int.

(If you're interested in learning more about C++ you could also use std::copy and std::ostream_iterator to avoid writing the loop yourself)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜