diff options
Diffstat (limited to 'apps/plugins/lua/lzio.c')
-rw-r--r-- | apps/plugins/lua/lzio.c | 34 |
1 files changed, 14 insertions, 20 deletions
diff --git a/apps/plugins/lua/lzio.c b/apps/plugins/lua/lzio.c index 293edd59b0..20efea9830 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.31.1.1 2007/12/27 13:02:25 roberto Exp $ | 2 | ** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $ |
3 | ** a generic input stream interface | 3 | ** Buffered streams |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
6 | 6 | ||
@@ -25,23 +25,11 @@ 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) return EOZ; | 28 | if (buff == NULL || size == 0) |
29 | z->n = size - 1; | 29 | return EOZ; |
30 | z->n = size - 1; /* discount char being returned */ | ||
30 | z->p = buff; | 31 | z->p = buff; |
31 | return char2int(*(z->p++)); | 32 | return cast_uchar(*(z->p++)); |
32 | } | ||
33 | |||
34 | |||
35 | int 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); | ||
45 | } | 33 | } |
46 | 34 | ||
47 | 35 | ||
@@ -58,8 +46,14 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { | |||
58 | size_t luaZ_read (ZIO *z, void *b, size_t n) { | 46 | size_t luaZ_read (ZIO *z, void *b, size_t n) { |
59 | while (n) { | 47 | while (n) { |
60 | size_t m; | 48 | size_t m; |
61 | if (luaZ_lookahead(z) == EOZ) | 49 | if (z->n == 0) { /* no bytes in buffer? */ |
62 | return n; /* return number of missing bytes */ | 50 | if (luaZ_fill(z) == EOZ) /* try to read more */ |
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 | } | ||
63 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ | 57 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ |
64 | memcpy(b, z->p, m); | 58 | memcpy(b, z->p, m); |
65 | z->n -= m; | 59 | z->n -= m; |