tools/llvm: Do not build with symbols
[minix3.git] / external / mit / lua / dist / src / lzio.c
blobfe01c61212d99898c3d1a99a05a97035d019f364
1 /* $NetBSD: lzio.c,v 1.1.1.2 2012/03/15 00:08:14 alnsn Exp $ */
3 /*
4 ** $Id: lzio.c,v 1.1.1.2 2012/03/15 00:08:14 alnsn Exp $
5 ** a generic input stream interface
6 ** See Copyright Notice in lua.h
7 */
10 #include <string.h>
12 #define lzio_c
13 #define LUA_CORE
15 #include "lua.h"
17 #include "llimits.h"
18 #include "lmem.h"
19 #include "lstate.h"
20 #include "lzio.h"
23 int luaZ_fill (ZIO *z) {
24 size_t size;
25 lua_State *L = z->L;
26 const char *buff;
27 lua_unlock(L);
28 buff = z->reader(L, z->data, &size);
29 lua_lock(L);
30 if (buff == NULL || size == 0) return EOZ;
31 z->n = size - 1;
32 z->p = buff;
33 return char2int(*(z->p++));
37 int luaZ_lookahead (ZIO *z) {
38 if (z->n == 0) {
39 if (luaZ_fill(z) == EOZ)
40 return EOZ;
41 else {
42 z->n++; /* luaZ_fill removed first byte; put back it */
43 z->p--;
46 return char2int(*z->p);
50 void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
51 z->L = L;
52 z->reader = reader;
53 z->data = data;
54 z->n = 0;
55 z->p = NULL;
59 /* --------------------------------------------------------------- read --- */
60 size_t luaZ_read (ZIO *z, void *b, size_t n) {
61 while (n) {
62 size_t m;
63 if (luaZ_lookahead(z) == EOZ)
64 return n; /* return number of missing bytes */
65 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
66 memcpy(b, z->p, m);
67 z->n -= m;
68 z->p += m;
69 b = (char *)b + m;
70 n -= m;
72 return 0;
75 /* ------------------------------------------------------------------------ */
76 char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
77 if (n > buff->buffsize) {
78 if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
79 luaZ_resizebuffer(L, buff, n);
81 return buff->buffer;