Jump to content
Tuts 4 You

[C++] appendoverlay


mrexodia

Recommended Posts

#include <stdio.h>
#include <vector>
#include <string>
#include <windows.h>

bool readFileData(const std::string & fileName, std::vector<unsigned char> & data)
{
    data.clear();
    HANDLE hFile = CreateFileA(fileName.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
    bool result=false;
    if(hFile != INVALID_HANDLE_VALUE)
    {
        unsigned int fileSize = GetFileSize(hFile, 0);
        unsigned char* dataPtr = new unsigned char[fileSize];
        DWORD read=0;
        if(ReadFile(hFile, dataPtr, fileSize, &read, 0))
            result = true;
        data.assign(dataPtr, dataPtr+fileSize);
        delete[] dataPtr;
        CloseHandle(hFile);
    }
    return result;
}

bool writeFile(const std::string & fileName, const std::vector<unsigned char> & data)
{
    HANDLE hFile = CreateFileA(fileName.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
    bool result=false;
    if(hFile != INVALID_HANDLE_VALUE)
    {
        SetEndOfFile(hFile);
        DWORD written=0;
        if(WriteFile(hFile, data.data(), data.size(), &written, 0))
            result=true;
        CloseHandle(hFile);
    }
    return result;
}

int main(int argc, char* argv[])
{
    if(argc < 4)
    {
        puts("usage: appendoverlay file overlay outfile");
        return -1;
    }
    std::vector<unsigned char> fileData;
    if(!readFileData(argv[1], fileData))
    {
        puts("failed to read file!");
        return -1;
    }
    printf("file size: %d\n", fileData.size());
    std::vector<unsigned char> overlayData;
    if(!readFileData(argv[2], overlayData))
    {
        puts("failed to read overlay!");
        return -1;
    }
    printf("overlay size: %d\n", fileData.size());
    fileData.insert(fileData.end(), overlayData.begin(), overlayData.end());
    printf("out size: %d\n", fileData.size());
    if(!writeFile(argv[3], fileData))
    {
        puts("failed to write outfile!");
        return -1;
    }
    puts("all done!");
}

This is a small tool that I wrote, might be useful for some people...

  • Like 1
Link to comment

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
×
×
  • Create New...