diff options
Diffstat (limited to 'lzio.c')
-rw-r--r-- | lzio.c | 36 |
1 files changed, 28 insertions, 8 deletions
@@ -45,17 +45,25 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { | |||
45 | 45 | ||
46 | 46 | ||
47 | /* --------------------------------------------------------------- read --- */ | 47 | /* --------------------------------------------------------------- read --- */ |
48 | |||
49 | static int checkbuffer (ZIO *z) { | ||
50 | if (z->n == 0) { /* no bytes in buffer? */ | ||
51 | if (luaZ_fill(z) == EOZ) /* try to read more */ | ||
52 | return 0; /* no more input */ | ||
53 | else { | ||
54 | z->n++; /* luaZ_fill consumed first byte; put it back */ | ||
55 | z->p--; | ||
56 | } | ||
57 | } | ||
58 | return 1; /* now buffer has something */ | ||
59 | } | ||
60 | |||
61 | |||
48 | size_t luaZ_read (ZIO *z, void *b, size_t n) { | 62 | size_t luaZ_read (ZIO *z, void *b, size_t n) { |
49 | while (n) { | 63 | while (n) { |
50 | size_t m; | 64 | size_t m; |
51 | if (z->n == 0) { /* no bytes in buffer? */ | 65 | if (!checkbuffer(z)) |
52 | if (luaZ_fill(z) == EOZ) /* try to read more */ | 66 | return n; /* no more input; return number of missing bytes */ |
53 | return n; /* no more input; return number of missing bytes */ | ||
54 | else { | ||
55 | z->n++; /* luaZ_fill consumed first byte; put it back */ | ||
56 | z->p--; | ||
57 | } | ||
58 | } | ||
59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ | 67 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ |
60 | memcpy(b, z->p, m); | 68 | memcpy(b, z->p, m); |
61 | z->n -= m; | 69 | z->n -= m; |
@@ -66,3 +74,15 @@ size_t luaZ_read (ZIO *z, void *b, size_t n) { | |||
66 | return 0; | 74 | return 0; |
67 | } | 75 | } |
68 | 76 | ||
77 | |||
78 | const void *luaZ_getaddr (ZIO* z, size_t n) { | ||
79 | const void *res; | ||
80 | if (!checkbuffer(z)) | ||
81 | return NULL; /* no more input */ | ||
82 | if (z->n < n) /* not enough bytes? */ | ||
83 | return NULL; /* block not whole; cannot give an address */ | ||
84 | res = z->p; /* get block address */ | ||
85 | z->n -= n; /* consume these bytes */ | ||
86 | z->p += n; | ||
87 | return res; | ||
88 | } | ||