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