~ubuntu-branches/ubuntu/wily/aegisub/wily-proposed

« back to all changes in this revision

Viewing changes to vendor/luabins/src/savebuffer.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel, Pascal De Vuyst, Juan Picca, Sebastian Reichel
  • Date: 2015-08-04 21:40:50 UTC
  • mfrom: (5.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150804214050-y2aghm9vdksoc8t7
Tags: 3.2.2+dfsg-1
[ Pascal De Vuyst ]
* Fix Typo in package description (Closes: #739219)

[ Juan Picca ]
* Add patch to fix reproducible build (Closes: #789728)

[ Sebastian Reichel ]
* New upstream release
 - remove vendor directory from orig tarball
* Update Debian Standards Version to 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* savebuffer.c
 
3
* Luabins save buffer
 
4
* See copyright notice in luabins.h
 
5
*/
 
6
 
 
7
#include <string.h> /* memcpy() */
 
8
 
 
9
#include "luaheaders.h"
 
10
 
 
11
#include "saveload.h"
 
12
#include "savebuffer.h"
 
13
 
 
14
#if 0
 
15
  #define SPAM(a) printf a
 
16
#else
 
17
  #define SPAM(a) (void)0
 
18
#endif
 
19
 
 
20
/* Minimum allocation size */
 
21
#define LUABINS_SAVEMINALLOC (256)
 
22
 
 
23
/* TODO: Test this with custom allocator! */
 
24
 
 
25
void lbsSB_init(
 
26
    luabins_SaveBuffer * sb,
 
27
    lua_Alloc alloc_fn,
 
28
    void * alloc_ud
 
29
  )
 
30
{
 
31
  sb->alloc_fn = alloc_fn;
 
32
  sb->alloc_ud = alloc_ud;
 
33
 
 
34
  sb->buffer = NULL;
 
35
  sb->buf_size = 0UL;
 
36
 
 
37
  sb->end = 0UL;
 
38
}
 
39
 
 
40
/*
 
41
* Ensures that there is at least delta size available in buffer.
 
42
* New size is aligned by blockSize increments
 
43
* Returns non-zero if resize failed.
 
44
* If you pre-sized the buffer, subsequent writes up to the new size
 
45
* are guaranteed to not fail.
 
46
*/
 
47
int lbsSB_grow(luabins_SaveBuffer * sb, size_t delta)
 
48
{
 
49
  size_t needed_size = sb->end + delta;
 
50
 
 
51
  if (needed_size > sb->buf_size)
 
52
  {
 
53
    /*
 
54
    * Growth factor x1.5
 
55
    * Discussion on possible values:
 
56
    * http://stackoverflow.com/questions/2269063/buffer-growth-strategy
 
57
    */
 
58
 
 
59
    /* TODO: Handle size_t overflow? */
 
60
 
 
61
    size_t new_size = 0;
 
62
 
 
63
    size_t add_size = sb->buf_size / 2;
 
64
    if (add_size < LUABINS_SAVEMINALLOC)
 
65
    {
 
66
      add_size = LUABINS_SAVEMINALLOC;
 
67
    }
 
68
 
 
69
    new_size = sb->buf_size + add_size;
 
70
 
 
71
/*
 
72
    SPAM((
 
73
        "trying %lu + %lu = %lu (needed %lu)\n",
 
74
        sb->buf_size,
 
75
        add_size,
 
76
        new_size,
 
77
        needed_size
 
78
      ));
 
79
*/
 
80
 
 
81
    while (new_size < needed_size)
 
82
    {
 
83
      SPAM(("...+%lu not enough, growing more\n", add_size));
 
84
      /* Grow exponentially as needed */
 
85
      add_size += new_size / 2;
 
86
      new_size += add_size;
 
87
    }
 
88
 
 
89
    SPAM((
 
90
        "growing from %lu to %lu (needed %lu)\n",
 
91
        sb->buf_size,
 
92
        new_size,
 
93
        needed_size
 
94
      ));
 
95
 
 
96
    sb->buffer = (unsigned char *)sb->alloc_fn(
 
97
        sb->alloc_ud,
 
98
        sb->buffer,
 
99
        sb->buf_size,
 
100
        new_size
 
101
      );
 
102
    if (sb->buffer == NULL)
 
103
    {
 
104
      /* TODO: We probably should free the buffer here */
 
105
      sb->buf_size = 0UL;
 
106
      sb->end = 0;
 
107
      return LUABINS_ETOOLONG;
 
108
    }
 
109
 
 
110
    sb->buf_size = new_size;
 
111
  }
 
112
 
 
113
  return LUABINS_ESUCCESS;
 
114
}
 
115
 
 
116
/*
 
117
* Returns non-zero if write failed.
 
118
* Allocates buffer as needed.
 
119
*/
 
120
int lbsSB_write(
 
121
    luabins_SaveBuffer * sb,
 
122
    const unsigned char * bytes,
 
123
    size_t length
 
124
  )
 
125
{
 
126
  int result = lbsSB_grow(sb, length);
 
127
  if (result != LUABINS_ESUCCESS)
 
128
  {
 
129
    return result;
 
130
  }
 
131
 
 
132
  memcpy(&sb->buffer[sb->end], bytes, length);
 
133
  sb->end += length;
 
134
 
 
135
  return LUABINS_ESUCCESS;
 
136
}
 
137
 
 
138
/*
 
139
* Returns non-zero if write failed.
 
140
* Allocates buffer as needed.
 
141
* Convenience function.
 
142
*/
 
143
int lbsSB_writechar(
 
144
    luabins_SaveBuffer * sb,
 
145
    const unsigned char byte
 
146
  )
 
147
{
 
148
  /* TODO: Shouldn't this be a macro? */
 
149
  int result = lbsSB_grow(sb, 1);
 
150
  if (result != LUABINS_ESUCCESS)
 
151
  {
 
152
    return result;
 
153
  }
 
154
 
 
155
  sb->buffer[sb->end] = byte;
 
156
  sb->end++;
 
157
 
 
158
  return LUABINS_ESUCCESS;
 
159
}
 
160
 
 
161
/*
 
162
* If offset is greater than total length, data is appended to the end.
 
163
* Returns non-zero if write failed.
 
164
* Allocates buffer as needed.
 
165
*/
 
166
int lbsSB_overwrite(
 
167
    luabins_SaveBuffer * sb,
 
168
    size_t offset,
 
169
    const unsigned char * bytes,
 
170
    size_t length
 
171
  )
 
172
{
 
173
  if (offset > sb->end)
 
174
  {
 
175
    offset = sb->end;
 
176
  }
 
177
 
 
178
  if (offset + length > sb->end)
 
179
  {
 
180
    int result = lbsSB_grow(sb, length);
 
181
    if (result != LUABINS_ESUCCESS)
 
182
    {
 
183
      return result;
 
184
    }
 
185
 
 
186
    sb->end = offset + length;
 
187
  }
 
188
 
 
189
  memcpy(&sb->buffer[offset], bytes, length);
 
190
 
 
191
  return LUABINS_ESUCCESS;
 
192
}
 
193
 
 
194
/*
 
195
* If offset is greater than total length, data is appended to the end.
 
196
* Returns non-zero if write failed.
 
197
* Allocates buffer as needed.
 
198
* Convenience function.
 
199
*/
 
200
int lbsSB_overwritechar(
 
201
    luabins_SaveBuffer * sb,
 
202
    size_t offset,
 
203
    unsigned char byte
 
204
  )
 
205
{
 
206
  if (offset > sb->end)
 
207
  {
 
208
    offset = sb->end;
 
209
  }
 
210
 
 
211
  if (offset + 1 > sb->end)
 
212
  {
 
213
    int result = lbsSB_grow(sb, 1);
 
214
    if (result != LUABINS_ESUCCESS)
 
215
    {
 
216
      return result;
 
217
    }
 
218
 
 
219
    sb->end = offset + 1;
 
220
  }
 
221
 
 
222
  sb->buffer[offset] = byte;
 
223
 
 
224
  return LUABINS_ESUCCESS;
 
225
}
 
226
 
 
227
/*
 
228
* Returns a pointer to the internal buffer with data.
 
229
* Note that buffer is NOT zero-terminated.
 
230
* Buffer is valid until next operation with the given sb.
 
231
*/
 
232
const unsigned char * lbsSB_buffer(luabins_SaveBuffer * sb, size_t * length)
 
233
{
 
234
  if (length != NULL)
 
235
  {
 
236
    *length = sb->end;
 
237
  }
 
238
  return sb->buffer;
 
239
}
 
240
 
 
241
void lbsSB_destroy(luabins_SaveBuffer * sb)
 
242
{
 
243
  if (sb->buffer != NULL)
 
244
  {
 
245
    /* Ignoring errors */
 
246
    SPAM(("dealloc size %lu\n", sb->buf_size));
 
247
    sb->alloc_fn(sb->alloc_ud, sb->buffer, sb->buf_size, 0UL);
 
248
    sb->buffer = NULL;
 
249
    sb->buf_size = 0UL;
 
250
    sb->end = 0UL;
 
251
  }
 
252
}