Jump to content

Any help in converting this to C++?


FaridAgl
 Share

Recommended Posts

#include <FileConstants.au3>

Func IsOriginalFile($sFilePath)
    Local $hFile = FileOpen($sFilePath, BitOR($FO_READ, $FO_BINARY))
    If ($hFile = -1) Then Return False

    If (Not FileSetPos($hFile, -8, $FILE_END)) Then
        FileClose($hFile)
        Return False
    EndIf

    Local $bSignature = FileRead($hFile, 8)
    If ((@error) Or (@extended <> 8)) Then
        FileClose($hFile)
        Return False
    EndIf

    FileClose($hFile)

    Local Const $SIGNATURE = Binary(0xEFBEADDEEFBEADDE)

    Return (BitXOR($SIGNATURE, $bSignature) = 0)
EndFunc

It opens a file, reads its last 8 bytes and checks to make sure its last 8 bytes are same as the $SIGNATURE.

Any help is welcomed.

Thanks

Link to comment
Share on other sites

I can not do it in C++, but I can in C

#include <stdio.h>
#include <stdlib.h>
int IsOriginalFile(const char* sFilePath);
int main()
{
    const char *sFilename = "C:\\main.c";
    if(IsOriginalFile(sFilename))
    {
        printf("File is right\n");
    }
    else
    {
        printf("File is wrong\n");
    }
    return 0;
}

int IsOriginalFile(const char* sFilePath)
{
    int i, iBytesToRead = 8;
    FILE *hFile;
    hFile = fopen(sFilePath, "rb");
    if (hFile == NULL)
    {
        return 0;
    }
    if(fseek(hFile, -iBytesToRead, SEEK_END))
    {
        fclose (hFile);
        return 0;
    }
    char * buffer;
    buffer = (char*)malloc(sizeof(char)* iBytesToRead);
    char bufcmp[] = {0xEF,0xBE,0xAD,0xDE,0xEF,0xBE,0xAD,0xDE};
    if(fread(buffer, 1, iBytesToRead, hFile) != iBytesToRead)
    {
        free (buffer);
        fclose (hFile);
        printf("Test\n");
        return 0;
    }
    fclose (hFile);
    for(i = 0; i < iBytesToRead; i++)
    {
        if(buffer[i] != bufcmp[i])
        {
            free (buffer);
            return 0;
        }
    }
    free (buffer);
    return 1;
}
Hope this helps.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Actually this is great.

Thanks a lot.

Here is my modified version of your code:

BOOL IsOriginalFile(LPCSTR szFilePath)
{
    FILE *hFile;
    if (fopen_s(&hFile, szFilePath, "rb") != 0)
    {
        return FALSE;
    }

    if (fseek(hFile, -8, SEEK_END) != 0)
    {
        fclose(hFile);
        return FALSE;
    }

    LPSTR szBuffer = (LPSTR)VirtualAlloc(NULL, 8, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
    if (fread(szBuffer, 1, 8, hFile) != 8)
    {
        VirtualFree(szBuffer, NULL, MEM_RELEASE);
        fclose(hFile);
        return FALSE;
    }

    fclose(hFile);

    CHAR szSignature[8] = {0x01, 0x03, 0x07, 0x00, 0x00, 0x05, 0x01, 0x02};

    for (int i = 0; i < 8; i++)
    {
        if (szSignature[i] != szBuffer[i])
        {
            VirtualFree(szBuffer, NULL, MEM_RELEASE);
            return FALSE;
        }
    }

    VirtualFree(szBuffer, NULL, MEM_RELEASE);
    return TRUE;
}
Edited by D4RKON3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...