summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2009-12-17 23:54:48 +0100
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2009-12-17 23:54:48 +0100
commit8635cc606ba24f71383dead4ad9a34ba0275dc73 (patch)
tree1952e9f7357df9dbdf64e49a5ee0c89cafde67e6
parent121c51168f06aa253b78d0f1bdfd0dbc2fed1cbe (diff)
Fixing adler32 & crc32, and adding block-reading system for them, instead of reading the whole file.
-rw-r--r--lib/LuaHandle.cc40
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;
}