@@ -91,7 +91,6 @@ void memrevifle(void *ptr, size_t len) {
91
91
* behavior. */
92
92
93
93
typedef struct mp_buf {
94
- lua_State * L ;
95
94
unsigned char * b ;
96
95
size_t len , free ;
97
96
} mp_buf ;
@@ -111,27 +110,26 @@ mp_buf *mp_buf_new(lua_State *L) {
111
110
/* Old size = 0; new size = sizeof(*buf) */
112
111
buf = (mp_buf * )mp_realloc (L , NULL , 0 , sizeof (* buf ));
113
112
114
- buf -> L = L ;
115
113
buf -> b = NULL ;
116
114
buf -> len = buf -> free = 0 ;
117
115
return buf ;
118
116
}
119
117
120
- void mp_buf_append (mp_buf * buf , const unsigned char * s , size_t len ) {
118
+ void mp_buf_append (lua_State * L , mp_buf * buf , const unsigned char * s , size_t len ) {
121
119
if (buf -> free < len ) {
122
- size_t newlen = buf -> len + len ;
120
+ size_t newsize = ( buf -> len + len ) * 2 ;
123
121
124
- buf -> b = (unsigned char * )mp_realloc (buf -> L , buf -> b , buf -> len , newlen * 2 );
125
- buf -> free = newlen ;
122
+ buf -> b = (unsigned char * )mp_realloc (L , buf -> b , buf -> len + buf -> free , newsize );
123
+ buf -> free = newsize - buf -> len ;
126
124
}
127
125
memcpy (buf -> b + buf -> len ,s ,len );
128
126
buf -> len += len ;
129
127
buf -> free -= len ;
130
128
}
131
129
132
- void mp_buf_free (mp_buf * buf ) {
133
- mp_realloc (buf -> L , buf -> b , buf -> len , 0 ); /* realloc to 0 = free */
134
- mp_realloc (buf -> L , buf , sizeof (* buf ), 0 );
130
+ void mp_buf_free (lua_State * L , mp_buf * buf ) {
131
+ mp_realloc (L , buf -> b , buf -> len + buf -> free , 0 ); /* realloc to 0 = free */
132
+ mp_realloc (L , buf , sizeof (* buf ), 0 );
135
133
}
136
134
137
135
/* ---------------------------- String cursor ----------------------------------
@@ -173,7 +171,7 @@ void mp_cur_init(mp_cur *cursor, const unsigned char *s, size_t len) {
173
171
174
172
/* ------------------------- Low level MP encoding -------------------------- */
175
173
176
- void mp_encode_bytes (mp_buf * buf , const unsigned char * s , size_t len ) {
174
+ void mp_encode_bytes (lua_State * L , mp_buf * buf , const unsigned char * s , size_t len ) {
177
175
unsigned char hdr [5 ];
178
176
int hdrlen ;
179
177
@@ -197,12 +195,12 @@ void mp_encode_bytes(mp_buf *buf, const unsigned char *s, size_t len) {
197
195
hdr [4 ] = len & 0xff ;
198
196
hdrlen = 5 ;
199
197
}
200
- mp_buf_append (buf ,hdr ,hdrlen );
201
- mp_buf_append (buf ,s ,len );
198
+ mp_buf_append (L , buf ,hdr ,hdrlen );
199
+ mp_buf_append (L , buf ,s ,len );
202
200
}
203
201
204
202
/* we assume IEEE 754 internal format for single and double precision floats. */
205
- void mp_encode_double (mp_buf * buf , double d ) {
203
+ void mp_encode_double (lua_State * L , mp_buf * buf , double d ) {
206
204
unsigned char b [9 ];
207
205
float f = d ;
208
206
@@ -211,16 +209,16 @@ void mp_encode_double(mp_buf *buf, double d) {
211
209
b [0 ] = 0xca ; /* float IEEE 754 */
212
210
memcpy (b + 1 ,& f ,4 );
213
211
memrevifle (b + 1 ,4 );
214
- mp_buf_append (buf ,b ,5 );
212
+ mp_buf_append (L , buf ,b ,5 );
215
213
} else if (sizeof (d ) == 8 ) {
216
214
b [0 ] = 0xcb ; /* double IEEE 754 */
217
215
memcpy (b + 1 ,& d ,8 );
218
216
memrevifle (b + 1 ,8 );
219
- mp_buf_append (buf ,b ,9 );
217
+ mp_buf_append (L , buf ,b ,9 );
220
218
}
221
219
}
222
220
223
- void mp_encode_int (mp_buf * buf , int64_t n ) {
221
+ void mp_encode_int (lua_State * L , mp_buf * buf , int64_t n ) {
224
222
unsigned char b [9 ];
225
223
int enclen ;
226
224
@@ -289,10 +287,10 @@ void mp_encode_int(mp_buf *buf, int64_t n) {
289
287
enclen = 9 ;
290
288
}
291
289
}
292
- mp_buf_append (buf ,b ,enclen );
290
+ mp_buf_append (L , buf ,b ,enclen );
293
291
}
294
292
295
- void mp_encode_array (mp_buf * buf , int64_t n ) {
293
+ void mp_encode_array (lua_State * L , mp_buf * buf , int64_t n ) {
296
294
unsigned char b [5 ];
297
295
int enclen ;
298
296
@@ -312,10 +310,10 @@ void mp_encode_array(mp_buf *buf, int64_t n) {
312
310
b [4 ] = n & 0xff ;
313
311
enclen = 5 ;
314
312
}
315
- mp_buf_append (buf ,b ,enclen );
313
+ mp_buf_append (L , buf ,b ,enclen );
316
314
}
317
315
318
- void mp_encode_map (mp_buf * buf , int64_t n ) {
316
+ void mp_encode_map (lua_State * L , mp_buf * buf , int64_t n ) {
319
317
unsigned char b [5 ];
320
318
int enclen ;
321
319
@@ -335,7 +333,7 @@ void mp_encode_map(mp_buf *buf, int64_t n) {
335
333
b [4 ] = n & 0xff ;
336
334
enclen = 5 ;
337
335
}
338
- mp_buf_append (buf ,b ,enclen );
336
+ mp_buf_append (L , buf ,b ,enclen );
339
337
}
340
338
341
339
/* --------------------------- Lua types encoding --------------------------- */
@@ -345,12 +343,12 @@ void mp_encode_lua_string(lua_State *L, mp_buf *buf) {
345
343
const char * s ;
346
344
347
345
s = lua_tolstring (L ,-1 ,& len );
348
- mp_encode_bytes (buf ,(const unsigned char * )s ,len );
346
+ mp_encode_bytes (L , buf ,(const unsigned char * )s ,len );
349
347
}
350
348
351
349
void mp_encode_lua_bool (lua_State * L , mp_buf * buf ) {
352
350
unsigned char b = lua_toboolean (L ,-1 ) ? 0xc3 : 0xc2 ;
353
- mp_buf_append (buf ,& b ,1 );
351
+ mp_buf_append (L , buf ,& b ,1 );
354
352
}
355
353
356
354
/* Lua 5.3 has a built in 64-bit integer type */
@@ -360,7 +358,7 @@ void mp_encode_lua_integer(lua_State *L, mp_buf *buf) {
360
358
#else
361
359
lua_Integer i = lua_tointeger (L ,-1 );
362
360
#endif
363
- mp_encode_int (buf , (int64_t )i );
361
+ mp_encode_int (L , buf , (int64_t )i );
364
362
}
365
363
366
364
/* Lua 5.2 and lower only has 64-bit doubles, so we need to
@@ -372,7 +370,7 @@ void mp_encode_lua_number(lua_State *L, mp_buf *buf) {
372
370
if (IS_INT64_EQUIVALENT (n )) {
373
371
mp_encode_lua_integer (L , buf );
374
372
} else {
375
- mp_encode_double (buf ,(double )n );
373
+ mp_encode_double (L , buf ,(double )n );
376
374
}
377
375
}
378
376
@@ -386,7 +384,7 @@ void mp_encode_lua_table_as_array(lua_State *L, mp_buf *buf, int level) {
386
384
size_t len = lua_rawlen (L ,-1 ), j ;
387
385
#endif
388
386
389
- mp_encode_array (buf ,len );
387
+ mp_encode_array (L , buf ,len );
390
388
for (j = 1 ; j <= len ; j ++ ) {
391
389
lua_pushnumber (L ,j );
392
390
lua_gettable (L ,-2 );
@@ -409,7 +407,7 @@ void mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {
409
407
}
410
408
411
409
/* Step two: actually encoding of the map. */
412
- mp_encode_map (buf ,len );
410
+ mp_encode_map (L , buf ,len );
413
411
lua_pushnil (L );
414
412
while (lua_next (L ,-2 )) {
415
413
/* Stack: ... key value */
@@ -474,10 +472,9 @@ void mp_encode_lua_table(lua_State *L, mp_buf *buf, int level) {
474
472
475
473
void mp_encode_lua_null (lua_State * L , mp_buf * buf ) {
476
474
unsigned char b [1 ];
477
- (void )L ;
478
475
479
476
b [0 ] = 0xc0 ;
480
- mp_buf_append (buf ,b ,1 );
477
+ mp_buf_append (L , buf ,b ,1 );
481
478
}
482
479
483
480
void mp_encode_lua_type (lua_State * L , mp_buf * buf , int level ) {
@@ -534,7 +531,7 @@ int mp_pack(lua_State *L) {
534
531
buf -> free += buf -> len ;
535
532
buf -> len = 0 ;
536
533
}
537
- mp_buf_free (buf );
534
+ mp_buf_free (L , buf );
538
535
539
536
/* Concatenate all nargs buffers together */
540
537
lua_concat (L , nargs );
0 commit comments