⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 read_apis.cpp

📁 linux下开发的针对所有磁盘的数据恢复的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/** The Sleuth Kit **** Brian Carrier [carrier <at> sleuthkit [dot] org]* Copyright (c) 2008 Brian Carrier.  All Rights reserved** This software is distributed under the Common Public License 1.0*//* * this is a test file for The Sleuth Kit.  It tests the various * read API functions.  It uses "file_walk" for specific files and then * verifies the data passed to the callback using the fs_file_read() * function, fs_read() function, and img_read() function.  Note that * not all files can be tested with the latter options because the file * could be sparse or compressed.  */#include "tsk3/tsk_tools_i.h"static TSK_FS_FILE *s_file2;static TSK_OFF_T s_off;static char *s_buf;static char *s_root;/* Callback that is used to do the testing */static TSK_WALK_RET_ENUMfw_action1(TSK_FS_FILE * a_fs_file, TSK_OFF_T a_off, TSK_DADDR_T a_addr,    char *a_buf, size_t a_size, TSK_FS_BLOCK_FLAG_ENUM a_flags,    void *a_ptr){    TSK_OFF_T tmp_off;    ssize_t cnt;    size_t tmp_len;    TSK_FS_INFO *fs = a_fs_file->fs_info;    // verify teh offset passed is what we expected    if (a_off != s_off) {        fprintf(stderr,            "offset passed in callback (%" PRIuOFF            ") diff from internal off (%" PRIuOFF ")\n", a_off, s_off);    }    /* The first set of tests is for the file_read API.  We seek     * to a "random" place to move around any caches, adn then read     * from the same offset that this call is from.  We compare     * the buffers. */    // pick a random place and length    tmp_off = (s_off * 4 + 1372) % s_file2->meta->size;    if (s_file2->meta->size - tmp_off > fs->block_size)        tmp_len = fs->block_size;    else        tmp_len = s_file2->meta->size - tmp_off;    cnt =        tsk_fs_file_read(s_file2, tmp_off, s_buf, tmp_len,        (TSK_FS_FILE_READ_FLAG_ENUM) 0);    if (cnt != (ssize_t) tmp_len) {        fprintf(stderr,            "Error reading random offset %" PRIuOFF " in file sized %"            PRIuOFF " (%zd vs %zd)\n", tmp_off, s_file2->meta->size, cnt,            tmp_len);        tsk_error_print(stderr);        return TSK_WALK_ERROR;    }    // now read from the real offset and compare with what we were passed    if (a_size > fs->block_size)        tmp_len = fs->block_size;    else        tmp_len = a_size;    cnt =        tsk_fs_file_read(s_file2, s_off, s_buf, tmp_len,        (TSK_FS_FILE_READ_FLAG_ENUM) 0);    if (cnt != (ssize_t) tmp_len) {        fprintf(stderr,            "Error reading file offset %" PRIuOFF " in file sized %"            PRIuOFF "\n", s_off, s_file2->meta->size);        tsk_error_print(stderr);        return TSK_WALK_ERROR;    }    if (memcmp(s_buf, a_buf, a_size)) {        fprintf(stderr,            "Buffers at offset %" PRIuOFF " in file %" PRIuINUM            " are different\n", s_off, s_file2->meta->addr);        return TSK_WALK_ERROR;    }    s_off += a_size;    /* IF the block we were passed is RAW (not BAD, resident, compressed etc.,     * then read using the fs_read() API      */    if (a_flags & TSK_FS_BLOCK_FLAG_RAW) {        tmp_off = (a_addr * 42 + 82) % fs->last_block;        cnt = tsk_fs_read_block(fs, tmp_off, s_buf, fs->block_size);        if (cnt != (ssize_t) fs->block_size) {            fprintf(stderr,                "Error reading random block %" PRIuOFF " in file system\n",                tmp_off);            tsk_error_print(stderr);            return TSK_WALK_ERROR;        }        cnt = tsk_fs_read_block(fs, a_addr, s_buf, fs->block_size);        if (cnt != (ssize_t) fs->block_size) {            fprintf(stderr, "Error reading block %" PRIuOFF "\n", a_addr);            tsk_error_print(stderr);            return TSK_WALK_ERROR;        }        // compare        if (memcmp(s_buf, a_buf, a_size)) {            fprintf(stderr,                "Buffers at block addr %" PRIuOFF " in file %" PRIuINUM                " are different\n", a_addr, s_file2->meta->addr);            return TSK_WALK_ERROR;        }        /* Now we also read using the img_read() API, just because we can */        cnt = tsk_fs_read_block(fs, tmp_off, s_buf, fs->block_size);        if (cnt != (ssize_t) fs->block_size) {            fprintf(stderr,                "Error reading random block %" PRIuOFF " in file system\n",                tmp_off);            tsk_error_print(stderr);            return TSK_WALK_ERROR;        }        // get the offset into the image        tmp_off = a_addr * fs->block_size + fs->offset;        cnt = tsk_img_read(fs->img_info, tmp_off, s_buf, fs->block_size);        if (cnt != (ssize_t) fs->block_size) {            fprintf(stderr,                "Error reading image offset %" PRIuOFF " in image\n",                tmp_off);            tsk_error_print(stderr);            return TSK_WALK_ERROR;        }        // compare        if (memcmp(s_buf, a_buf, a_size)) {            fprintf(stderr,                "Buffers at image offset  %" PRIuOFF " in file %" PRIuINUM                " are different\n", tmp_off, s_file2->meta->addr);            return TSK_WALK_ERROR;        }    }    return TSK_WALK_CONT;}inttestfile(TSK_FS_INFO * a_fs, TSK_INUM_T a_inum){    TSK_FS_FILE *file1 = NULL;    if ((s_buf = (char *) malloc(a_fs->block_size)) == NULL) {        fprintf(stderr, "Error allocating  memory\n");        return 1;    }    file1 = tsk_fs_file_open_meta(a_fs, NULL, a_inum);    if (file1 == NULL) {        fprintf(stderr, "Error opening inode %" PRIuINUM "\n", a_inum);        return 1;    }    s_file2 = tsk_fs_file_open_meta(a_fs, NULL, a_inum);    if (s_file2 == NULL) {        fprintf(stderr, "Error opening inode %" PRIuINUM "\n", a_inum);        return 1;    }    s_off = 0;    if (tsk_fs_file_walk(file1, (TSK_FS_FILE_WALK_FLAG_ENUM) 0,            fw_action1, NULL)) {        fprintf(stderr, "Error walking file inode: %"PRIuINUM"\n", a_inum);        tsk_error_print(stderr);        tsk_error_reset();        return 1;    }    free(s_buf);    tsk_fs_file_close(file1);    tsk_fs_file_close(s_file2);    return 0;}inttest_fat12(){    TSK_FS_INFO *fs;    TSK_IMG_INFO *img;    char *tname = "fat12.dd";    char fname[512];    snprintf(fname, 512, "%s/fat12.dd", s_root);    if ((img =            tsk_img_open_sing((const TSK_TCHAR *) fname,                (TSK_IMG_TYPE_ENUM) 0)) == NULL) {        fprintf(stderr, "Error opening %s image\n", tname);        tsk_error_print(stderr);        tsk_error_reset();        return 1;    }    if ((fs = tsk_fs_open_img(img, 0, (TSK_FS_TYPE_ENUM) 0)) == NULL) {        fprintf(stderr, "Error opening %s image\n", tname);        tsk_error_print(stderr);        tsk_error_reset();        return 1;    }    if (testfile(fs, 33)) {        fprintf(stderr, "%s failure\n", tname);        return 1;    }    tsk_fs_close(fs);    tsk_img_close(img);    return 0;}/* This test checks the SLACK flags and verifies * that we read data from the slack space */inttest_fat_slack(){    TSK_FS_INFO *fs;    TSK_IMG_INFO *img;    char *tname = "fat-img-kw";    char fname[512];    TSK_FS_FILE *file1;    char buf[512];    ssize_t retval;    snprintf(fname, 512, "%s/fat-img-kw.dd", s_root);    if ((img =            tsk_img_open_sing((const TSK_TCHAR *) fname,                (TSK_IMG_TYPE_ENUM) 0)) == NULL) {        fprintf(stderr, "Error opening %s image\n", tname);        tsk_error_print(stderr);        tsk_error_reset();        return 1;    }    if ((fs = tsk_fs_open_img(img, 0, (TSK_FS_TYPE_ENUM) 0)) == NULL) {        fprintf(stderr, "Error opening %s image\n", tname);        tsk_error_print(stderr);        tsk_error_reset();        return 1;    }    // file4.dat    file1 = tsk_fs_file_open_meta(fs, NULL, 10);    if (file1 == NULL) {        fprintf(stderr, "Error opening file4.dat (%s)\n", tname);        return 1;    }    // verify expected size    if (file1->meta->size != 631) {        fprintf(stderr,            "Error: file4.dat not expected size (%" PRIuOFF ") (%s)\n",            file1->meta->size, tname);        return 1;    }    // try to read all of last sector with/out Slack set    retval =        tsk_fs_file_read(file1, 512, buf, 512,        (TSK_FS_FILE_READ_FLAG_ENUM) 0);    if (retval == -1) {        fprintf(stderr,            "Error reading file4.dat to end w/out slack flag\n");        tsk_error_print(stderr);        tsk_error_reset();        return 1;    }    if (retval != 119) {        fprintf(stderr,            "Unexpected return value from reading file4.dat to end w/out slack flag.\n");        fprintf(stderr, "Expected: 119.  Got: %zd\n", retval);        return 1;    }    retval =        tsk_fs_file_read(file1, 512, buf, 512,        TSK_FS_FILE_READ_FLAG_SLACK);    if (retval == -1) {        fprintf(stderr, "Error reading file4.dat to end w/slack flag\n");        tsk_error_print(stderr);        tsk_error_reset();        return 1;    }    if (retval != 512) {        fprintf(stderr,            "Unexpected return value from reading file4.dat  w/slack flag.\n");        fprintf(stderr, "Expected: 512.  Got: %zd\n", retval);        return 1;    }    // verify the term in the slack space    if (memcmp("3slack3", &buf[385], 7) != 0) {        fprintf(stderr,            "slack string not found in file4.dat slack space: %x %x %x %x %x %x %x\n",            buf[385], buf[386], buf[387], buf[388], buf[389], buf[390],            buf[391]);        return 1;    }    tsk_fs_close(fs);    tsk_img_close(img);    return 0;}/* This test checks the RECOVER flags  */inttest_fat_recover(){    TSK_FS_INFO *fs;    TSK_IMG_INFO *img;    char *tname = "fe_test_1.img-FAT";    char fname[512];    TSK_FS_FILE *file1;    TSK_FS_FILE *file2;    char buf[512];    ssize_t retval;    snprintf(fname, 512, "%s/fe_test_1.img", s_root);    if ((img =            tsk_img_open_sing((const TSK_TCHAR *) fname,                (TSK_IMG_TYPE_ENUM) 0)) == NULL) {        fprintf(stderr, "Error opening %s image\n", tname);        tsk_error_print(stderr);        tsk_error_reset();        return 1;    }    if ((fs =            tsk_fs_open_img(img, 41126400,                (TSK_FS_TYPE_ENUM) 0)) == NULL) {        fprintf(stderr, "Error opening %s image\n", tname);        tsk_error_print(stderr);        tsk_error_reset();        return 1;    }    // fragmented.html    char *fname2 = "fragmented.html";    file1 = tsk_fs_file_open_meta(fs, NULL, 1162);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -