Coverage Report

Created: 2025-07-18 06:59

/src/testdir/build/lua-master/source/lzio.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** $Id: lzio.c $
3
** Buffered streams
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lzio_c
8
#define LUA_CORE
9
10
#include "lprefix.h"
11
12
13
#include <string.h>
14
15
#include "lua.h"
16
17
#include "lapi.h"
18
#include "llimits.h"
19
#include "lmem.h"
20
#include "lstate.h"
21
#include "lzio.h"
22
23
24
579
int luaZ_fill (ZIO *z) {
25
579
  size_t size;
26
579
  lua_State *L = z->L;
27
579
  const char *buff;
28
579
  lua_unlock(L);
29
579
  buff = z->reader(L, z->data, &size);
30
579
  lua_lock(L);
31
579
  if (buff == NULL || size == 0)
32
157
    return EOZ;
33
422
  z->n = size - 1;  /* discount char being returned */
34
422
  z->p = buff;
35
422
  return cast_uchar(*(z->p++));
36
579
}
37
38
39
422
void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
40
422
  z->L = L;
41
422
  z->reader = reader;
42
422
  z->data = data;
43
422
  z->n = 0;
44
422
  z->p = NULL;
45
422
}
46
47
48
/* --------------------------------------------------------------- read --- */
49
50
0
static int checkbuffer (ZIO *z) {
51
0
  if (z->n == 0) {  /* no bytes in buffer? */
52
0
    if (luaZ_fill(z) == EOZ)  /* try to read more */
53
0
      return 0;  /* no more input */
54
0
    else {
55
0
      z->n++;  /* luaZ_fill consumed first byte; put it back */
56
0
      z->p--;
57
0
    }
58
0
  }
59
0
  return 1;  /* now buffer has something */
60
0
}
61
62
63
0
size_t luaZ_read (ZIO *z, void *b, size_t n) {
64
0
  while (n) {
65
0
    size_t m;
66
0
    if (!checkbuffer(z))
67
0
      return n;  /* no more input; return number of missing bytes */
68
0
    m = (n <= z->n) ? n : z->n;  /* min. between n and z->n */
69
0
    memcpy(b, z->p, m);
70
0
    z->n -= m;
71
0
    z->p += m;
72
0
    b = (char *)b + m;
73
0
    n -= m;
74
0
  }
75
0
  return 0;
76
0
}
77
78
79
0
const void *luaZ_getaddr (ZIO* z, size_t n) {
80
0
  const void *res;
81
0
  if (!checkbuffer(z))
82
0
    return NULL;  /* no more input */
83
0
  if (z->n < n)  /* not enough bytes? */
84
0
    return NULL;  /* block not whole; cannot give an address */
85
0
  res = z->p;  /* get block address */
86
0
  z->n -= n;  /* consume these bytes */
87
0
  z->p += n;
88
0
  return res;
89
0
}