/src/testdir/build/lua-master/source/lzio.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lzio.c $ |
3 | | ** Buffered streams |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lzio_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <string.h> |
14 | | |
15 | | #include "lua.h" |
16 | | |
17 | | #include "llimits.h" |
18 | | #include "lmem.h" |
19 | | #include "lstate.h" |
20 | | #include "lzio.h" |
21 | | |
22 | | |
23 | 507 | int luaZ_fill (ZIO *z) { |
24 | 507 | size_t size; |
25 | 507 | lua_State *L = z->L; |
26 | 507 | const char *buff; |
27 | 507 | lua_unlock(L); |
28 | 507 | buff = z->reader(L, z->data, &size); |
29 | 507 | lua_lock(L); |
30 | 507 | if (buff == NULL || size == 0) |
31 | 117 | return EOZ; |
32 | 390 | z->n = size - 1; /* discount char being returned */ |
33 | 390 | z->p = buff; |
34 | 390 | return cast_uchar(*(z->p++)); |
35 | 507 | } |
36 | | |
37 | | |
38 | 390 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { |
39 | 390 | z->L = L; |
40 | 390 | z->reader = reader; |
41 | 390 | z->data = data; |
42 | 390 | z->n = 0; |
43 | 390 | z->p = NULL; |
44 | 390 | } |
45 | | |
46 | | |
47 | | /* --------------------------------------------------------------- read --- */ |
48 | 0 | size_t luaZ_read (ZIO *z, void *b, size_t n) { |
49 | 0 | while (n) { |
50 | 0 | size_t m; |
51 | 0 | if (z->n == 0) { /* no bytes in buffer? */ |
52 | 0 | if (luaZ_fill(z) == EOZ) /* try to read more */ |
53 | 0 | return n; /* no more input; return number of missing bytes */ |
54 | 0 | else { |
55 | 0 | z->n++; /* luaZ_fill consumed first byte; put it back */ |
56 | 0 | z->p--; |
57 | 0 | } |
58 | 0 | } |
59 | 0 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ |
60 | 0 | memcpy(b, z->p, m); |
61 | 0 | z->n -= m; |
62 | 0 | z->p += m; |
63 | 0 | b = (char *)b + m; |
64 | 0 | n -= m; |
65 | 0 | } |
66 | 0 | return 0; |
67 | 0 | } |
68 | | |