1*eda14cbcSMatt Macy /*
2*eda14cbcSMatt Macy ** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $
3*eda14cbcSMatt Macy ** Buffered streams
4*eda14cbcSMatt Macy ** See Copyright Notice in lua.h
5*eda14cbcSMatt Macy */
6*eda14cbcSMatt Macy
7*eda14cbcSMatt Macy
8*eda14cbcSMatt Macy #define lzio_c
9*eda14cbcSMatt Macy #define LUA_CORE
10*eda14cbcSMatt Macy
11*eda14cbcSMatt Macy #include <sys/lua/lua.h>
12*eda14cbcSMatt Macy
13*eda14cbcSMatt Macy #include "llimits.h"
14*eda14cbcSMatt Macy #include "lmem.h"
15*eda14cbcSMatt Macy #include "lstate.h"
16*eda14cbcSMatt Macy #include "lzio.h"
17*eda14cbcSMatt Macy
18*eda14cbcSMatt Macy
luaZ_fill(ZIO * z)19*eda14cbcSMatt Macy int luaZ_fill (ZIO *z) {
20*eda14cbcSMatt Macy size_t size;
21*eda14cbcSMatt Macy lua_State *L = z->L;
22*eda14cbcSMatt Macy const char *buff;
23*eda14cbcSMatt Macy lua_unlock(L);
24*eda14cbcSMatt Macy buff = z->reader(L, z->data, &size);
25*eda14cbcSMatt Macy lua_lock(L);
26*eda14cbcSMatt Macy if (buff == NULL || size == 0)
27*eda14cbcSMatt Macy return EOZ;
28*eda14cbcSMatt Macy z->n = size - 1; /* discount char being returned */
29*eda14cbcSMatt Macy z->p = buff;
30*eda14cbcSMatt Macy return cast_uchar(*(z->p++));
31*eda14cbcSMatt Macy }
32*eda14cbcSMatt Macy
33*eda14cbcSMatt Macy
luaZ_init(lua_State * L,ZIO * z,lua_Reader reader,void * data)34*eda14cbcSMatt Macy void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
35*eda14cbcSMatt Macy z->L = L;
36*eda14cbcSMatt Macy z->reader = reader;
37*eda14cbcSMatt Macy z->data = data;
38*eda14cbcSMatt Macy z->n = 0;
39*eda14cbcSMatt Macy z->p = NULL;
40*eda14cbcSMatt Macy }
41*eda14cbcSMatt Macy
42*eda14cbcSMatt Macy
43*eda14cbcSMatt Macy /* --------------------------------------------------------------- read --- */
luaZ_read(ZIO * z,void * b,size_t n)44*eda14cbcSMatt Macy size_t luaZ_read (ZIO *z, void *b, size_t n) {
45*eda14cbcSMatt Macy while (n) {
46*eda14cbcSMatt Macy size_t m;
47*eda14cbcSMatt Macy if (z->n == 0) { /* no bytes in buffer? */
48*eda14cbcSMatt Macy if (luaZ_fill(z) == EOZ) /* try to read more */
49*eda14cbcSMatt Macy return n; /* no more input; return number of missing bytes */
50*eda14cbcSMatt Macy else {
51*eda14cbcSMatt Macy z->n++; /* luaZ_fill consumed first byte; put it back */
52*eda14cbcSMatt Macy z->p--;
53*eda14cbcSMatt Macy }
54*eda14cbcSMatt Macy }
55*eda14cbcSMatt Macy m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
56*eda14cbcSMatt Macy memcpy(b, z->p, m);
57*eda14cbcSMatt Macy z->n -= m;
58*eda14cbcSMatt Macy z->p += m;
59*eda14cbcSMatt Macy b = (char *)b + m;
60*eda14cbcSMatt Macy n -= m;
61*eda14cbcSMatt Macy }
62*eda14cbcSMatt Macy return 0;
63*eda14cbcSMatt Macy }
64*eda14cbcSMatt Macy
65*eda14cbcSMatt Macy /* ------------------------------------------------------------------------ */
luaZ_openspace(lua_State * L,Mbuffer * buff,size_t n)66*eda14cbcSMatt Macy char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
67*eda14cbcSMatt Macy if (n > buff->buffsize) {
68*eda14cbcSMatt Macy if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
69*eda14cbcSMatt Macy luaZ_resizebuffer(L, buff, n);
70*eda14cbcSMatt Macy }
71*eda14cbcSMatt Macy return buff->buffer;
72*eda14cbcSMatt Macy }
73