xref: /freebsd/sys/contrib/openzfs/module/lua/lzio.h (revision c03c5b1c80914ec656fbee84539355d1fad68bf9)
1*eda14cbcSMatt Macy /*
2*eda14cbcSMatt Macy ** $Id: lzio.h,v 1.26.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 #ifndef lzio_h
9*eda14cbcSMatt Macy #define lzio_h
10*eda14cbcSMatt Macy 
11*eda14cbcSMatt Macy #include <sys/lua/lua.h>
12*eda14cbcSMatt Macy 
13*eda14cbcSMatt Macy #include "lmem.h"
14*eda14cbcSMatt Macy 
15*eda14cbcSMatt Macy 
16*eda14cbcSMatt Macy #define EOZ	(-1)			/* end of stream */
17*eda14cbcSMatt Macy 
18*eda14cbcSMatt Macy typedef struct Zio ZIO;
19*eda14cbcSMatt Macy 
20*eda14cbcSMatt Macy #define zgetc(z)  (((z)->n--)>0 ?  cast_uchar(*(z)->p++) : luaZ_fill(z))
21*eda14cbcSMatt Macy 
22*eda14cbcSMatt Macy 
23*eda14cbcSMatt Macy typedef struct Mbuffer {
24*eda14cbcSMatt Macy   char *buffer;
25*eda14cbcSMatt Macy   size_t n;
26*eda14cbcSMatt Macy   size_t buffsize;
27*eda14cbcSMatt Macy } Mbuffer;
28*eda14cbcSMatt Macy 
29*eda14cbcSMatt Macy #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
30*eda14cbcSMatt Macy 
31*eda14cbcSMatt Macy #define luaZ_buffer(buff)	((buff)->buffer)
32*eda14cbcSMatt Macy #define luaZ_sizebuffer(buff)	((buff)->buffsize)
33*eda14cbcSMatt Macy #define luaZ_bufflen(buff)	((buff)->n)
34*eda14cbcSMatt Macy 
35*eda14cbcSMatt Macy #define luaZ_resetbuffer(buff) ((buff)->n = 0)
36*eda14cbcSMatt Macy 
37*eda14cbcSMatt Macy 
38*eda14cbcSMatt Macy #define luaZ_resizebuffer(L, buff, size) \
39*eda14cbcSMatt Macy 	(luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
40*eda14cbcSMatt Macy 	(buff)->buffsize = size)
41*eda14cbcSMatt Macy 
42*eda14cbcSMatt Macy #define luaZ_freebuffer(L, buff)	luaZ_resizebuffer(L, buff, 0)
43*eda14cbcSMatt Macy 
44*eda14cbcSMatt Macy 
45*eda14cbcSMatt Macy LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
46*eda14cbcSMatt Macy LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
47*eda14cbcSMatt Macy                                         void *data);
48*eda14cbcSMatt Macy LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n);	/* read next n bytes */
49*eda14cbcSMatt Macy 
50*eda14cbcSMatt Macy 
51*eda14cbcSMatt Macy 
52*eda14cbcSMatt Macy /* --------- Private Part ------------------ */
53*eda14cbcSMatt Macy 
54*eda14cbcSMatt Macy struct Zio {
55*eda14cbcSMatt Macy   size_t n;			/* bytes still unread */
56*eda14cbcSMatt Macy   const char *p;		/* current position in buffer */
57*eda14cbcSMatt Macy   lua_Reader reader;		/* reader function */
58*eda14cbcSMatt Macy   void* data;			/* additional data */
59*eda14cbcSMatt Macy   lua_State *L;			/* Lua state (for reader) */
60*eda14cbcSMatt Macy };
61*eda14cbcSMatt Macy 
62*eda14cbcSMatt Macy 
63*eda14cbcSMatt Macy LUAI_FUNC int luaZ_fill (ZIO *z);
64*eda14cbcSMatt Macy 
65*eda14cbcSMatt Macy #endif
66