Coverage Report

Created: 2025-08-29 06:37

/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
12.1M
int luaZ_fill (ZIO *z) {
25
12.1M
  size_t size;
26
12.1M
  lua_State *L = z->L;
27
12.1M
  const char *buff;
28
12.1M
  lua_unlock(L);
29
12.1M
  buff = z->reader(L, z->data, &size);
30
12.1M
  lua_lock(L);
31
12.1M
  if (buff == NULL || size == 0)
32
5.65M
    return EOZ;
33
6.46M
  z->n = size - 1;  /* discount char being returned */
34
6.46M
  z->p = buff;
35
6.46M
  return cast_uchar(*(z->p++));
36
12.1M
}
37
38
39
8.16M
void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
40
8.16M
  z->L = L;
41
8.16M
  z->reader = reader;
42
8.16M
  z->data = data;
43
8.16M
  z->n = 0;
44
8.16M
  z->p = NULL;
45
8.16M
}
46
47
48
/* --------------------------------------------------------------- read --- */
49
50
117k
static int checkbuffer (ZIO *z) {
51
117k
  if (z->n == 0) {  /* no bytes in buffer? */
52
8.99k
    if (luaZ_fill(z) == EOZ)  /* try to read more */
53
8.96k
      return 0;  /* no more input */
54
35
    else {
55
35
      z->n++;  /* luaZ_fill consumed first byte; put it back */
56
35
      z->p--;
57
35
    }
58
8.99k
  }
59
108k
  return 1;  /* now buffer has something */
60
117k
}
61
62
63
109k
size_t luaZ_read (ZIO *z, void *b, size_t n) {
64
217k
  while (n) {
65
117k
    size_t m;
66
117k
    if (!checkbuffer(z))
67
8.96k
      return n;  /* no more input; return number of missing bytes */
68
108k
    m = (n <= z->n) ? n : z->n;  /* min. between n and z->n */
69
108k
    memcpy(b, z->p, m);
70
108k
    z->n -= m;
71
108k
    z->p += m;
72
108k
    b = (char *)b + m;
73
108k
    n -= m;
74
108k
  }
75
100k
  return 0;
76
109k
}
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
}