diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2009-12-17 23:43:20 +0100 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2009-12-17 23:43:20 +0100 |
commit | 121c51168f06aa253b78d0f1bdfd0dbc2fed1cbe (patch) | |
tree | 00630dc3022f4865f08d1388f6fa70960d780cb0 | |
parent | 4db9139bfedf1af9a097b247c5814c5b1058e669 (diff) |
Adding readfile, adler32 and crc32
-rw-r--r-- | lib/LuaHandle.cc | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc index 3172ec8..4456e13 100644 --- a/lib/LuaHandle.cc +++ b/lib/LuaHandle.cc @@ -38,6 +38,7 @@ class sLuaHandle : public Base { static int readU32(lua_State * L); static int readFloat(lua_State * L); static int readDouble(lua_State * L); + static int readfile(lua_State * L); static int write(lua_State * L); static int writestring(lua_State * L); static int writeU8(lua_State * L); @@ -81,6 +82,8 @@ class sLuaHandle : public Base { #endif static int archive(lua_State * L); static int user2table(lua_State * L); + static int adler32(lua_State * L); + static int crc32(lua_State * L); private: static int read(lua_State * L, int); static int write(lua_State * L, int); @@ -248,6 +251,82 @@ int sLuaHandle::readstring(lua_State * __L) { return 1; } +int sLuaHandle::readfile(lua_State * __L) { + Lua * L = Lua::find(__L); + int n = L->gettop(); + Handle * h; + Byte * buf; + + if (n != 1) { + L->error("Incorrect arguments to method `Headle::readfile'"); + } + + h = L->recast<Handle>(); + if (h->GetSize() < 0) return 0; + + h->seek(0); + + buf = (Byte *) malloc(h->GetSize()); + h->read(buf, h->GetSize()); + + L->push((char *) buf, h->GetSize()); + free(buf); + + return 1; +} + +int sLuaHandle::adler32(lua_State * __L) { + Lua * L = Lua::find(__L); + int n = L->gettop(); + Handle * h; + Byte * buf; + + if (n != 1) { + L->error("Incorrect arguments to function `adler32'"); + } + + h = L->recast<Handle>(); + if (h->GetSize() < 0) return 0; + + h->seek(0); + + buf = (Byte *) malloc(h->GetSize()); + Uint32 adler = ::adler32(0L, Z_NULL, 0); + + adler = ::adler32(adler, buf, h->GetSize()); + + L->push((lua_Number) adler); + free(buf); + + return 1; +} + +int sLuaHandle::crc32(lua_State * __L) { + Lua * L = Lua::find(__L); + int n = L->gettop(); + Handle * h; + Byte * buf; + + if (n != 1) { + L->error("Incorrect arguments to function `crc32'"); + } + + h = L->recast<Handle>(); + if (h->GetSize() < 0) return 0; + + h->seek(0); + + buf = (Byte *) malloc(h->GetSize()); + Uint32 crc = ::crc32(0L, Z_NULL, 0); + + crc = ::crc32(crc, buf, h->GetSize()); + + L->push((lua_Number) crc); + free(buf); + + return 1; +} + int sLuaHandle::readU8(lua_State * L) { return read(L, U8); } @@ -928,6 +1007,7 @@ void LuaHandle::pushmembers(Lua * L) { pushit(L, "readstring", sLuaHandle::readstring); pushit(L, "writestring", sLuaHandle::writestring); + pushit(L, "readfile", sLuaHandle::readfile); pushit(L, "readU8", sLuaHandle::readU8); pushit(L, "readU16", sLuaHandle::readU16); @@ -998,6 +1078,9 @@ void LuaHandle::pushconstruct(Lua * L) { L->declarefunc("Archive", sLuaHandle::archive); L->declarefunc("user2table", sLuaHandle::user2table); + + L->declarefunc("adler32", sLuaHandle::adler32); + L->declarefunc("crc32", sLuaHandle::crc32); } HandleLua::HandleLua(Lua * L, int i) : Handle(-1), L(L) { |