diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/LuaHandle.cc | 40 |
1 files 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<Handle>(); - 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<Handle>(); - 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; } |