#include <stdio.h>
#include <fcntl.h>      /* Needed only for _O_RDWR definition */
#include <stdlib.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#include <share.h>
#else
#include <unistd.h>
#endif
#include <glib.h>
#include <memory.h>
#include <stdio.h>
#include <gmodule.h>

void (*ftd_plugin_auth_decode_init) (const char *filename) = 0;
unsigned char * (*ftd_plugin_auth_decode) (const char *buffer) = 0;

int main(int argc, char **argv)
{
    int fp = open("testfile", O_RDONLY);
    unsigned char *res;
    GModule *handle;

    GString *buf = g_string_new("");

#ifdef WIN32
    SetErrorMode(SEM_FAILCRITICALERRORS);
#endif

    handle = g_module_open(argv[1], (GModuleFlags) 0);

#ifdef WIN32
    SetErrorMode(0);
#endif

    if (handle == NULL) {
        return 1;
    }
    g_module_symbol(handle, "ftd_plugin_auth_decode_init", (gpointer *) &ftd_plugin_auth_decode_init);
    g_module_symbol(handle, "ftd_plugin_auth_decode", (gpointer *) &ftd_plugin_auth_decode);
    if (ftd_plugin_auth_decode_init == NULL || ftd_plugin_auth_decode == NULL) {
        return 1;
    }

    ftd_plugin_auth_decode_init("test");

    while (1) {
        char tmp[1024];
        int len;
        memset(tmp, 0, sizeof(tmp));
        if ((len = read(fp, tmp, sizeof(tmp))) > 0) {
            char tmp2[1025];
            memcpy(tmp2, tmp, sizeof(tmp));
            tmp2[1024] = 0;

            buf = g_string_append(buf, tmp2);
        } else {
            break;
        }
    }

    close(fp);

    res = ftd_plugin_auth_decode(buf->str);
    g_string_free(buf, TRUE);
    g_free(res);

    return 0;
}

