Port to new grub_pci_iterate signature as of 2013-01-13, removing nested functions.
[grub-extras.git] / lzio.c
blob3ccba193cc8fa013f323da85b89750e09827b884
1 /*
2 ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
3 ** a generic input stream interface
4 ** See Copyright Notice in lua.h
5 */
7 #if 0
8 #include <string.h>
9 #endif
11 #define lzio_c
12 #define LUA_CORE
14 #include "lua.h"
16 #include "llimits.h"
17 #include "lmem.h"
18 #include "lstate.h"
19 #include "lzio.h"
22 int luaZ_fill (ZIO *z) {
23 size_t size;
24 lua_State *L = z->L;
25 const char *buff;
26 lua_unlock(L);
27 buff = z->reader(L, z->data, &size);
28 lua_lock(L);
29 if (buff == NULL || size == 0) return EOZ;
30 z->n = size - 1;
31 z->p = buff;
32 return char2int(*(z->p++));
36 int luaZ_lookahead (ZIO *z) {
37 if (z->n == 0) {
38 if (luaZ_fill(z) == EOZ)
39 return EOZ;
40 else {
41 z->n++; /* luaZ_fill removed first byte; put back it */
42 z->p--;
45 return char2int(*z->p);
49 void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
50 z->L = L;
51 z->reader = reader;
52 z->data = data;
53 z->n = 0;
54 z->p = NULL;
58 /* --------------------------------------------------------------- read --- */
59 size_t luaZ_read (ZIO *z, void *b, size_t n) {
60 while (n) {
61 size_t m;
62 if (luaZ_lookahead(z) == EOZ)
63 return n; /* return number of missing bytes */
64 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
65 memcpy(b, z->p, m);
66 z->n -= m;
67 z->p += m;
68 b = (char *)b + m;
69 n -= m;
71 return 0;
74 /* ------------------------------------------------------------------------ */
75 char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
76 if (n > buff->buffsize) {
77 if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
78 luaZ_resizebuffer(L, buff, n);
80 return buff->buffer;