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