From 8635cc606ba24f71383dead4ad9a34ba0275dc73 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Thu, 17 Dec 2009 23:54:48 +0100 Subject: Fixing adler32 & crc32, and adding block-reading system for them, instead of reading the whole file. --- lib/LuaHandle.cc | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc index 4456e13..cef3135 100644 --- a/lib/LuaHandle.cc +++ b/lib/LuaHandle.cc @@ -275,28 +275,37 @@ int sLuaHandle::readfile(lua_State * __L) { return 1; } +#define MAXCHUNKSIZE 65536 + int sLuaHandle::adler32(lua_State * __L) { Lua * L = Lua::find(__L); int n = L->gettop(); Handle * h; Byte * buf; + ssize_t tg, blk; if (n != 1) { L->error("Incorrect arguments to function `adler32'"); } h = L->recast(); - if (h->GetSize() < 0) return 0; + tg = h->GetSize(); + if (tg < 0) return 0; h->seek(0); - buf = (Byte *) malloc(h->GetSize()); + buf = (Byte *) malloc(MAXCHUNKSIZE); Uint32 adler = ::adler32(0L, Z_NULL, 0); - - adler = ::adler32(adler, buf, h->GetSize()); - - L->push((lua_Number) adler); + + while (tg > 0) { + blk = MIN(MAXCHUNKSIZE, tg); + h->read(buf, blk); + tg -= blk; + adler = ::adler32(adler, buf, blk); + } free(buf); + + L->push((lua_Number) adler); return 1; } @@ -306,23 +315,30 @@ int sLuaHandle::crc32(lua_State * __L) { int n = L->gettop(); Handle * h; Byte * buf; + ssize_t tg, blk; if (n != 1) { L->error("Incorrect arguments to function `crc32'"); } h = L->recast(); - if (h->GetSize() < 0) return 0; + tg = h->GetSize(); + if (tg < 0) return 0; h->seek(0); - buf = (Byte *) malloc(h->GetSize()); + buf = (Byte *) malloc(MAXCHUNKSIZE); Uint32 crc = ::crc32(0L, Z_NULL, 0); - - crc = ::crc32(crc, buf, h->GetSize()); - - L->push((lua_Number) crc); + + while (tg > 0) { + blk = MIN(MAXCHUNKSIZE, tg); + h->read(buf, blk); + tg -= blk; + crc = ::crc32(crc, buf, blk); + } free(buf); + + L->push((lua_Number) crc); return 1; } -- cgit v1.2.3