cs50恢复,恢复的图像不匹配

时间:2020-08-17 16:10:04

标签: c jpeg cs50 recover

在pset4恢复中,我已经成功恢复了49个而不是50个JPG,尽管它们看起来还不错,但check50告诉我图像不匹配: :) recovery.c存在。 :) recovery.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;
}

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

0 个答案:

没有答案