1*0a6a1f1dSLionel Sambuc /* $NetBSD: lzio.c,v 1.3 2015/02/02 14:03:05 lneto Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc ** Id: lzio.c,v 1.36 2014/11/02 19:19:04 roberto Exp
5*0a6a1f1dSLionel Sambuc ** Buffered streams
611be35a1SLionel Sambuc ** See Copyright Notice in lua.h
711be35a1SLionel Sambuc */
811be35a1SLionel Sambuc
911be35a1SLionel Sambuc #define lzio_c
1011be35a1SLionel Sambuc #define LUA_CORE
1111be35a1SLionel Sambuc
12*0a6a1f1dSLionel Sambuc #include "lprefix.h"
13*0a6a1f1dSLionel Sambuc
14*0a6a1f1dSLionel Sambuc
15*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
16*0a6a1f1dSLionel Sambuc #include <string.h>
17*0a6a1f1dSLionel Sambuc #endif
18*0a6a1f1dSLionel Sambuc
1911be35a1SLionel Sambuc #include "lua.h"
2011be35a1SLionel Sambuc
2111be35a1SLionel Sambuc #include "llimits.h"
2211be35a1SLionel Sambuc #include "lmem.h"
2311be35a1SLionel Sambuc #include "lstate.h"
2411be35a1SLionel Sambuc #include "lzio.h"
2511be35a1SLionel Sambuc
2611be35a1SLionel Sambuc
luaZ_fill(ZIO * z)2711be35a1SLionel Sambuc int luaZ_fill (ZIO *z) {
2811be35a1SLionel Sambuc size_t size;
2911be35a1SLionel Sambuc lua_State *L = z->L;
3011be35a1SLionel Sambuc const char *buff;
3111be35a1SLionel Sambuc lua_unlock(L);
3211be35a1SLionel Sambuc buff = z->reader(L, z->data, &size);
3311be35a1SLionel Sambuc lua_lock(L);
34*0a6a1f1dSLionel Sambuc if (buff == NULL || size == 0)
3511be35a1SLionel Sambuc return EOZ;
36*0a6a1f1dSLionel Sambuc z->n = size - 1; /* discount char being returned */
37*0a6a1f1dSLionel Sambuc z->p = buff;
38*0a6a1f1dSLionel Sambuc return cast_uchar(*(z->p++));
3911be35a1SLionel Sambuc }
4011be35a1SLionel Sambuc
4111be35a1SLionel Sambuc
luaZ_init(lua_State * L,ZIO * z,lua_Reader reader,void * data)4211be35a1SLionel Sambuc void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
4311be35a1SLionel Sambuc z->L = L;
4411be35a1SLionel Sambuc z->reader = reader;
4511be35a1SLionel Sambuc z->data = data;
4611be35a1SLionel Sambuc z->n = 0;
4711be35a1SLionel Sambuc z->p = NULL;
4811be35a1SLionel Sambuc }
4911be35a1SLionel Sambuc
5011be35a1SLionel Sambuc
5111be35a1SLionel Sambuc /* --------------------------------------------------------------- read --- */
luaZ_read(ZIO * z,void * b,size_t n)5211be35a1SLionel Sambuc size_t luaZ_read (ZIO *z, void *b, size_t n) {
5311be35a1SLionel Sambuc while (n) {
5411be35a1SLionel Sambuc size_t m;
55*0a6a1f1dSLionel Sambuc if (z->n == 0) { /* no bytes in buffer? */
56*0a6a1f1dSLionel Sambuc if (luaZ_fill(z) == EOZ) /* try to read more */
57*0a6a1f1dSLionel Sambuc return n; /* no more input; return number of missing bytes */
58*0a6a1f1dSLionel Sambuc else {
59*0a6a1f1dSLionel Sambuc z->n++; /* luaZ_fill consumed first byte; put it back */
60*0a6a1f1dSLionel Sambuc z->p--;
61*0a6a1f1dSLionel Sambuc }
62*0a6a1f1dSLionel Sambuc }
6311be35a1SLionel Sambuc m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
6411be35a1SLionel Sambuc memcpy(b, z->p, m);
6511be35a1SLionel Sambuc z->n -= m;
6611be35a1SLionel Sambuc z->p += m;
6711be35a1SLionel Sambuc b = (char *)b + m;
6811be35a1SLionel Sambuc n -= m;
6911be35a1SLionel Sambuc }
7011be35a1SLionel Sambuc return 0;
7111be35a1SLionel Sambuc }
7211be35a1SLionel Sambuc
7311be35a1SLionel Sambuc /* ------------------------------------------------------------------------ */
luaZ_openspace(lua_State * L,Mbuffer * buff,size_t n)7411be35a1SLionel Sambuc char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
7511be35a1SLionel Sambuc if (n > buff->buffsize) {
7611be35a1SLionel Sambuc if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
7711be35a1SLionel Sambuc luaZ_resizebuffer(L, buff, n);
7811be35a1SLionel Sambuc }
7911be35a1SLionel Sambuc return buff->buffer;
8011be35a1SLionel Sambuc }
8111be35a1SLionel Sambuc
8211be35a1SLionel Sambuc
83