首页 > 解决方案 > cs50 恢复,恢复的图像不匹配

问题描述

在 pset4 恢复中,我成功恢复了 49 个而不是 50 个 JPG,虽然它们看起来不错,但 check50 告诉我图像不匹配::) recover.c 存在。:) recover.c 编译。:) 处理缺少取证图像 :( 正确恢复 000.jpg 恢复图像不匹配 :( 正确恢复中间图像 恢复图像不匹配 :( 正确恢复 049.jpg 049.jpg 未找到

我的代码是:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef uint8_t BYTE;

// Declare a function
int find_start(FILE *ptr);
int find_jpeg(FILE *ptr);

int main(int argc, char *argv[])
{
    // Accept only one command-line argument
    if (argc != 2)
    {
        printf("Enter the name of the forensic image\n");
        return 1;
    }
    char *name = argv[1];
    FILE *pointer = fopen(name, "r");
    // Inform the reader if the file can't be opened
    if (pointer == NULL)
    {
        printf("File cannot be opened\n");
        return 1;
    }
    int q = 0;
    // Find the signature for the beginning of the first jpeg
    int d1 = find_start(pointer);
    // Loop
    while (q < 50)
    {
        if (q == 0)
        {
            fseek(pointer, 508, SEEK_CUR);
        }
        int e = find_jpeg(pointer);
        fseek(pointer, -(e + 512), SEEK_CUR);
        BYTE array[e + 512];
        fread(array, 1, e + 512, pointer);
        // Transfer the jpeg from the array to a new file
        char new_name[8];
        int o = sprintf(new_name, "%i%i%i.jpg", q / 100, q / 10 - (q / 100) * 100, q - (q / 100) * 100 - (q / 10) * 10);
        FILE *out = fopen(new_name, "w");
        fwrite(array, 1, e + 512, out);
        q++;
        fclose(out);
    }
    // Close all files
    fclose(pointer);
    return 0;
}

// Define the function for finding the beginning of the first jpeg
int find_start(FILE *ptr)
{
    BYTE i, j, k, l;
    int x;
    int *count = &x;
    *count = 0;
    do
    {
        do
        {
            do
            {
                do
                {
                    i = fgetc(ptr);
                    *count = *count + 1;
                }
                while (i != 0xff);
                j = fgetc(ptr);
            }
            while (j != 0xd8);
            k = fgetc(ptr);
            *count = *count + 1;
        }
        while (k != 0xff);
        l = fgetc(ptr);
        *count = *count + 1;
    }
    while (l < 224 || l > 240);
    return x;
}

int find_jpeg(FILE *ptr)
{
    BYTE i, j, k, l;
    int x;
    int *count = &x;
    *count = 0;
    do
    {
        i = fgetc(ptr);
        j = fgetc(ptr);
        k = fgetc(ptr);
        l = fgetc(ptr);
        fseek(ptr, 508, SEEK_CUR);
        *count = *count + 1;
    }
    while (i != 0xff ||  j != 0xd8 || k != 0xff || l < 224 || l > 240);
    int a = x * 512;
    return a;
}

我知道它很长而且不优雅,但否则有什么问题?

标签: cjpegcs50recover

解决方案


推荐阅读