#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "hlnk.h"

int hiperiso_create_hlnk(uint32_t disksig, uint64_t partoffset, const char *path, hiperiso_hlnk *hlnk)
{
    uint32_t crc;
    hiperiso_guid guid = HIPERISO_GUID;

    memcpy(&(hlnk->guid), &guid, sizeof(hiperiso_guid));
    hlnk->disk_signature = disksig;
    hlnk->part_offset = partoffset;

#ifdef WIN32
    strcpy_s(hlnk->filepath, sizeof(hlnk->filepath) - 1, path);
#else
    strncpy(hlnk->filepath, path, sizeof(hlnk->filepath) - 1);
#endif

    crc = hiperiso_getcrc32c(0, hlnk, sizeof(hiperiso_hlnk));
    hlnk->crc32 = crc;

    return 0;
}


int CheckHlnkData(hiperiso_hlnk *hlnk)
{
    uint32_t readcrc, calccrc;
    hiperiso_guid guid = HIPERISO_GUID;

    if (memcmp(&hlnk->guid, &guid, sizeof(guid)))
    {
        return 0;
    }

    readcrc = hlnk->crc32;
    hlnk->crc32 = 0;
    calccrc = hiperiso_getcrc32c(0, hlnk, sizeof(hiperiso_hlnk));

    if (readcrc != calccrc)
    {
        return 0;
    }

    return 1;
}

int IsSupportedImgSuffix(char *suffix)
{
    int i = 0;
    const char *suffixs[] =
    {
        ".iso", ".img", ".wim", ".efi", ".vhd", ".vhdx", ".dat", ".hiso", NULL
    };

    if (!suffix)
    {
        return 0;
    }

    while (suffixs[i])
    {

#ifdef WIN32
        if (_stricmp(suffixs[i], suffix) == 0)
#else
        if (strcasecmp(suffixs[i], suffix) == 0)
#endif
        {
            return 1;
        }

        i++;
    }

    return 0;
}
