/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 "lapi.h" |
18 | | #include "llimits.h" |
19 | | #include "lmem.h" |
20 | | #include "lstate.h" |
21 | | #include "lzio.h" |
22 | | |
23 | | |
24 | 13.8M | int luaZ_fill (ZIO *z) { |
25 | 13.8M | size_t size; |
26 | 13.8M | lua_State *L = z->L; |
27 | 13.8M | const char *buff; |
28 | 13.8M | lua_unlock(L); |
29 | 13.8M | buff = z->reader(L, z->data, &size); |
30 | 13.8M | lua_lock(L); |
31 | 13.8M | if (buff == NULL || size == 0) |
32 | 5.48M | return EOZ; |
33 | 8.40M | z->n = size - 1; /* discount char being returned */ |
34 | 8.40M | z->p = buff; |
35 | 8.40M | return cast_uchar(*(z->p++)); |
36 | 13.8M | } |
37 | | |
38 | | |
39 | 9.93M | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { |
40 | 9.93M | z->L = L; |
41 | 9.93M | z->reader = reader; |
42 | 9.93M | z->data = data; |
43 | 9.93M | z->n = 0; |
44 | 9.93M | z->p = NULL; |
45 | 9.93M | } |
46 | | |
47 | | |
48 | | /* --------------------------------------------------------------- read --- */ |
49 | | |
50 | 2.51M | static int checkbuffer (ZIO *z) { |
51 | 2.51M | if (z->n == 0) { /* no bytes in buffer? */ |
52 | 19.8k | if (luaZ_fill(z) == EOZ) /* try to read more */ |
53 | 19.8k | return 0; /* no more input */ |
54 | 72 | else { |
55 | 72 | z->n++; /* luaZ_fill consumed first byte; put it back */ |
56 | 72 | z->p--; |
57 | 72 | } |
58 | 19.8k | } |
59 | 2.49M | return 1; /* now buffer has something */ |
60 | 2.51M | } |
61 | | |
62 | | |
63 | 2.55M | size_t luaZ_read (ZIO *z, void *b, size_t n) { |
64 | 5.04M | while (n) { |
65 | 2.51M | size_t m; |
66 | 2.51M | if (!checkbuffer(z)) |
67 | 19.8k | return n; /* no more input; return number of missing bytes */ |
68 | 2.49M | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ |
69 | 2.49M | memcpy(b, z->p, m); |
70 | 2.49M | z->n -= m; |
71 | 2.49M | z->p += m; |
72 | 2.49M | b = (char *)b + m; |
73 | 2.49M | n -= m; |
74 | 2.49M | } |
75 | 2.53M | return 0; |
76 | 2.55M | } |
77 | | |
78 | | |
79 | 0 | const void *luaZ_getaddr (ZIO* z, size_t n) { |
80 | 0 | const void *res; |
81 | 0 | if (!checkbuffer(z)) |
82 | 0 | return NULL; /* no more input */ |
83 | 0 | if (z->n < n) /* not enough bytes? */ |
84 | 0 | return NULL; /* block not whole; cannot give an address */ |
85 | 0 | res = z->p; /* get block address */ |
86 | 0 | z->n -= n; /* consume these bytes */ |
87 | 0 | z->p += n; |
88 | 0 | return res; |
89 | 0 | } |