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