-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.c
68 lines (62 loc) · 1.32 KB
/
buffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <string.h>
#include "buffer.h"
#include "limits.h"
#include "memory.h"
#include "state.h"
int luaZ_fill(ZIO *z) {
lua_State *L = z->L;
lua_unlock(L);
// Enter the userspace, so we unlock first.
size_t size;
const char *buff = z->reader(L, z->ud, &size);
lua_lock(L);
if (buff == nullptr || size == 0) {
return EOZ;
}
z->n = size - 1;
z->p = buff;
return (int)(uint8_t)*z->p++;
}
int luaZ_lookahead(ZIO *z) {
if (z->n == 0) {
if (luaZ_fill(z) == EOZ) {
return EOZ;
}
// luaZ_fill removed first byte, put it back.
z->n++;
z->p--;
}
return (int)(uint8_t)*z->p;
}
void luaZ_init(lua_State *L, ZIO *z, lua_Reader reader, void *ud) {
z->L = L;
z->reader = reader;
z->ud = ud;
z->n = 0;
z->p = nullptr;
}
size_t luaZ_read(ZIO *z, void *b, size_t n) {
uint8_t *bytes = b;
while (n) {
if (luaZ_lookahead(z) == EOZ) {
// Return number of missing bytes.
return n;
}
size_t m = n <= z->n ? n : z->n; /* min. between n and z->n */
memcpy(bytes, z->p, m);
z->n -= m;
z->p += m;
bytes += m;
n -= m;
}
return 0;
}
char *luaZ_reserve(lua_State *L, StringBuilder *buff, size_t n) {
if (n > buff->size) {
if (n < LUA_MIN_BUF_SIZE) {
n = LUA_MIN_BUF_SIZE;
}
StringBuilder_resize(L, buff, n);
}
return buff->str;
}