This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
/
savebuffer.c
162 lines (137 loc) · 3.07 KB
/
savebuffer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* savebuffer.c
* Luabins save buffer
* See copyright notice in luabins.h
*/
#include <string.h> /* memcpy() */
#include "luaheaders.h"
#include "saveload.h"
#include "savebuffer.h"
#if 0
#define SPAM(a) printf a
#else
#define SPAM(a) (void)0
#endif
/* TODO: Test this with custom allocator! */
void lbsSB_init(
luabins_SaveBuffer * sb,
lua_Alloc alloc_fn,
void * alloc_ud,
size_t block_size
)
{
sb->alloc_fn = alloc_fn;
sb->alloc_ud = alloc_ud;
sb->block_size = block_size;
sb->buffer = NULL;
sb->buf_size = 0UL;
sb->end = 0UL;
}
/*
* Ensures that there is at least delta size available in buffer.
* New size is aligned by blockSize increments
* Returns non-zero if resize failed.
* If you pre-sized the buffer, subsequent writes up to the new size
* are guaranteed to not fail.
*/
int lbsSB_grow(luabins_SaveBuffer * sb, size_t delta)
{
size_t newsize = sb->end + delta;
SPAM(("end %lu delta %lu newsize %lu\n", sb->end, delta, newsize));
if (newsize > sb->buf_size)
{
/* Pad to the next block boundary */
size_t remainder = (newsize % sb->block_size);
if (remainder > 0)
{
newsize += sb->block_size - remainder;
}
SPAM(("growing from %lu to %lu\n", sb->buf_size, newsize));
sb->buffer = (unsigned char *)sb->alloc_fn(
sb->alloc_ud,
sb->buffer,
sb->buf_size,
newsize
);
if (sb->buffer == NULL)
{
sb->buf_size = 0UL;
sb->end = 0;
return LUABINS_ETOOLONG;
}
sb->buf_size = newsize;
}
return LUABINS_ESUCCESS;
}
/*
* Returns non-zero if write failed.
* Allocates buffer as needed.
*/
int lbsSB_write(
luabins_SaveBuffer * sb,
const unsigned char * bytes,
size_t length
)
{
int result = lbsSB_grow(sb, length);
if (result != LUABINS_ESUCCESS)
{
return result;
}
memcpy(&sb->buffer[sb->end], bytes, length);
sb->end += length;
return LUABINS_ESUCCESS;
}
/*
* If offset is greater than total length, data is appended to the end.
* Returns non-zero if write failed.
* Allocates buffer as needed.
*/
int lbsSB_overwrite(
luabins_SaveBuffer * sb,
size_t offset,
const unsigned char * bytes,
size_t length
)
{
if (offset > sb->end)
{
offset = sb->end;
}
if (offset + length > sb->end)
{
int result = lbsSB_grow(sb, length);
if (result != LUABINS_ESUCCESS)
{
return result;
}
sb->end = offset + length;
}
memcpy(&sb->buffer[offset], bytes, length);
return LUABINS_ESUCCESS;
}
/*
* Returns a pointer to the internal buffer with data.
* Note that buffer is NOT zero-terminated.
* Buffer is valid until next operation with the given sb.
*/
const unsigned char * lbsSB_buffer(luabins_SaveBuffer * sb, size_t * length)
{
if (length != NULL)
{
*length = sb->end;
}
return sb->buffer;
}
void lbsSB_destroy(luabins_SaveBuffer * sb)
{
if (sb->buffer != NULL)
{
/* Ignoring errors */
SPAM(("dealloc size %lu\n", sb->buf_size));
sb->alloc_fn(sb->alloc_ud, sb->buffer, sb->buf_size, 0UL);
sb->buffer = NULL;
sb->buf_size = 0UL;
sb->end = 0UL;
}
}