summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lzio.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lzio.c')
-rw-r--r--apps/plugins/lua/lzio.c34
1 files changed, 20 insertions, 14 deletions
diff --git a/apps/plugins/lua/lzio.c b/apps/plugins/lua/lzio.c
index 20efea9830..293edd59b0 100644
--- a/apps/plugins/lua/lzio.c
+++ b/apps/plugins/lua/lzio.c
@@ -1,6 +1,6 @@
1/* 1/*
2** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $ 2** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
3** Buffered streams 3** a generic input stream interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
@@ -25,11 +25,23 @@ int luaZ_fill (ZIO *z) {
25 lua_unlock(L); 25 lua_unlock(L);
26 buff = z->reader(L, z->data, &size); 26 buff = z->reader(L, z->data, &size);
27 lua_lock(L); 27 lua_lock(L);
28 if (buff == NULL || size == 0) 28 if (buff == NULL || size == 0) return EOZ;
29 return EOZ; 29 z->n = size - 1;
30 z->n = size - 1; /* discount char being returned */
31 z->p = buff; 30 z->p = buff;
32 return cast_uchar(*(z->p++)); 31 return char2int(*(z->p++));
32}
33
34
35int luaZ_lookahead (ZIO *z) {
36 if (z->n == 0) {
37 if (luaZ_fill(z) == EOZ)
38 return EOZ;
39 else {
40 z->n++; /* luaZ_fill removed first byte; put back it */
41 z->p--;
42 }
43 }
44 return char2int(*z->p);
33} 45}
34 46
35 47
@@ -46,14 +58,8 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
46size_t luaZ_read (ZIO *z, void *b, size_t n) { 58size_t luaZ_read (ZIO *z, void *b, size_t n) {
47 while (n) { 59 while (n) {
48 size_t m; 60 size_t m;
49 if (z->n == 0) { /* no bytes in buffer? */ 61 if (luaZ_lookahead(z) == EOZ)
50 if (luaZ_fill(z) == EOZ) /* try to read more */ 62 return n; /* return number of missing bytes */
51 return n; /* no more input; return number of missing bytes */
52 else {
53 z->n++; /* luaZ_fill consumed first byte; put it back */
54 z->p--;
55 }
56 }
57 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 63 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
58 memcpy(b, z->p, m); 64 memcpy(b, z->p, m);
59 z->n -= m; 65 z->n -= m;