lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


----- Original Message -----
From: Luiz Henrique de Figueiredo
Date: 4/26/2010 6:12 AM
-- using LHF's previous version of lmd5, the one before it was married to openssl :D
My version of lmd5 has this useful function. It calculates the MD5 of an entire file and does so from C code. This means the garbage collector isn't pegged heavily during a read of a large file.

/**
**/
static int Lupdatefile(lua_State *L)
{
    MD5_CTX *c=Pget(L,1);
    const char *fileName=luaL_checkstring(L,2);
    const size_t BLOCK_SIZE = 32768;
    unsigned char* buffer;
    size_t fileLen;
    size_t bytesToDo;
    FILE* file = fopen(fileName, "rb");
    if (!file)
        return 0;
    buffer = malloc(BLOCK_SIZE);
    fseek(file, 0, SEEK_END);
    fileLen = ftell(file);
    fseek(file, 0, SEEK_SET);
    bytesToDo = fileLen;
    while (bytesToDo > 0)
    {
size_t bytesToRead = bytesToDo < BLOCK_SIZE ? bytesToDo : BLOCK_SIZE;

        fread(buffer, bytesToRead, 1, file);
        bytesToDo -= bytesToRead;
        MD5Update(c, buffer, bytesToRead);
    }

    fseek(file, 0, SEEK_SET);
    fclose(file);

    free(buffer);
    return 0;
}


-Josh