需要制作一个“反病毒”来扫描文件以查找C中的特定病毒签名

时间:2019-05-27 18:31:59

标签: c

从fread结果打印文件时,这两个文件看起来都一样,怎么办? 请不要介意malloc类型转换。这两个文件都以“ rb”类型打开。 count对从两个字符之间的第一个匹配点开始扫描的匹配数进行计数,目的是使计数达到病毒签名所包含的相同字符数。

    char charFromVirus = 0;
    char charFromImage = 0;
    int count = 0;
    int virusSignLength = 0;
    int seekLoc = 0;
    int imageLength = 0;
    int imageLengthCount = 0;
    char* virusSignString = 0;
    char* imageString = 0;
    int startOffset = 0;

    fseek(image, 0, SEEK_END);
    imageLength = ftell(image);

    fseek(virusSign, 0, SEEK_END);
    virusSignLength = ftell(virusSign);

    imageString = (char*)malloc(imageLength + 1);
    fread(imageString, 1, imageLength, image);
    virusSignString = (char*)malloc(virusSignLength + 1);
    fread(virusSignString, 1, virusSignLength, virusSign);

    fseek(virusSign, 0, SEEK_SET);
    fseek(image, 0, SEEK_SET);

    charFromImage = imageString[0];
    charFromVirus = virusSignString[0];

    while (imageLengthCount != imageLength)
    {
        printf("%c %c\n", charFromImage, charFromVirus);
        if (charFromImage == charFromVirus)
        {
            printf("Equal.\n");
            charFromImage = imageString[startOffset + seekLoc];
            charFromVirus = virusSignString[seekLoc];
            count++;
            printf("count: %d\n", count);
            if (count == virusSignLength)
            {
                printf("Found infection.\n");
                free(virusSignString);
                free(imageString);
                return 1;
            }
            seekLoc++;

        }
        else
        {
            printf("Not Equal");
            seekLoc = 0;
            count = 0;
            startOffset++;
            charFromImage = imageString[startOffset];
            charFromVirus = virusSignString[0];
            imageLengthCount++;
        }

    }

    free(virusSignString);
    free(imageString);
    printf("Clear\n");
    return 0;

0 个答案:

没有答案