/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 | 141M | int luaZ_fill (ZIO *z) { |
24 | 141M | size_t size; |
25 | 141M | lua_State *L = z->L; |
26 | 141M | const char *buff; |
27 | 141M | lua_unlock(L); |
28 | 141M | buff = z->reader(L, z->data, &size); |
29 | 141M | lua_lock(L); |
30 | 141M | if (buff == NULL || size == 0) |
31 | 67.2k | return EOZ; |
32 | 141M | z->n = size - 1; /* discount char being returned */ |
33 | 141M | z->p = buff; |
34 | 141M | return cast_uchar(*(z->p++)); |
35 | 141M | } |
36 | | |
37 | | |
38 | 205k | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { |
39 | 205k | z->L = L; |
40 | 205k | z->reader = reader; |
41 | 205k | z->data = data; |
42 | 205k | z->n = 0; |
43 | 205k | z->p = NULL; |
44 | 205k | } |
45 | | |
46 | | |
47 | | /* --------------------------------------------------------------- read --- */ |
48 | 7.57k | size_t luaZ_read (ZIO *z, void *b, size_t n) { |
49 | 15.1k | while (n) { |
50 | 8.55k | size_t m; |
51 | 8.55k | if (z->n == 0) { /* no bytes in buffer? */ |
52 | 978 | if (luaZ_fill(z) == EOZ) /* try to read more */ |
53 | 978 | 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 | 978 | } |
59 | 7.57k | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ |
60 | 7.57k | memcpy(b, z->p, m); |
61 | 7.57k | z->n -= m; |
62 | 7.57k | z->p += m; |
63 | 7.57k | b = (char *)b + m; |
64 | 7.57k | n -= m; |
65 | 7.57k | } |
66 | 6.59k | return 0; |
67 | 7.57k | } |
68 | | |