15
15
#define TYPE_BOOLEAN 1
16
16
// hibits 0 false 1 true
17
17
#define TYPE_NUMBER 2
18
- // hibits 0 : 0 , 1: byte, 2:word, 4: dword, 8 : double
18
+ // hibits 0 : 0 , 1: byte, 2:word, 4: dword, 6: qword, 8 : double
19
+ #define TYPE_NUMBER_ZERO 0
20
+ #define TYPE_NUMBER_BYTE 1
21
+ #define TYPE_NUMBER_WORD 2
22
+ #define TYPE_NUMBER_DWORD 4
23
+ #define TYPE_NUMBER_QWORD 6
24
+ #define TYPE_NUMBER_REAL 8
25
+
19
26
#define TYPE_USERDATA 3
20
27
#define TYPE_SHORT_STRING 4
21
28
// hibits 0~31 : len
@@ -107,7 +114,7 @@ rball_init(struct read_block * rb, char * buffer, int size) {
107
114
}
108
115
109
116
static void *
110
- rb_read (struct read_block * rb , void * buffer , int sz ) {
117
+ rb_read (struct read_block * rb , int sz ) {
111
118
if (rb -> len < sz ) {
112
119
return NULL ;
113
120
}
@@ -131,36 +138,44 @@ wb_boolean(struct write_block *wb, int boolean) {
131
138
}
132
139
133
140
static inline void
134
- wb_integer (struct write_block * wb , int v , int type ) {
141
+ wb_integer (struct write_block * wb , lua_Integer v ) {
142
+ int type = TYPE_NUMBER ;
135
143
if (v == 0 ) {
136
- uint8_t n = COMBINE_TYPE (type , 0 );
144
+ uint8_t n = COMBINE_TYPE (type , TYPE_NUMBER_ZERO );
145
+ wb_push (wb , & n , 1 );
146
+ } else if (v != (int32_t )v ) {
147
+ uint8_t n = COMBINE_TYPE (type , TYPE_NUMBER_QWORD );
148
+ int64_t v64 = v ;
137
149
wb_push (wb , & n , 1 );
138
- } else if (v < 0 ) {
139
- uint8_t n = COMBINE_TYPE (type , 4 );
150
+ wb_push (wb , & v64 , sizeof (v64 ));
151
+ } else if (v < 0 ) {
152
+ int32_t v32 = (int32_t )v ;
153
+ uint8_t n = COMBINE_TYPE (type , TYPE_NUMBER_DWORD );
140
154
wb_push (wb , & n , 1 );
141
- wb_push (wb , & v , 4 );
155
+ wb_push (wb , & v32 , sizeof ( v32 ) );
142
156
} else if (v < 0x100 ) {
143
- uint8_t n = COMBINE_TYPE (type , 1 );
157
+ uint8_t n = COMBINE_TYPE (type , TYPE_NUMBER_BYTE );
144
158
wb_push (wb , & n , 1 );
145
159
uint8_t byte = (uint8_t )v ;
146
- wb_push (wb , & byte , 1 );
160
+ wb_push (wb , & byte , sizeof ( byte ) );
147
161
} else if (v < 0x10000 ) {
148
- uint8_t n = COMBINE_TYPE (type , 2 );
162
+ uint8_t n = COMBINE_TYPE (type , TYPE_NUMBER_WORD );
149
163
wb_push (wb , & n , 1 );
150
164
uint16_t word = (uint16_t )v ;
151
- wb_push (wb , & word , 2 );
165
+ wb_push (wb , & word , sizeof ( word ) );
152
166
} else {
153
- uint8_t n = COMBINE_TYPE (type , 4 );
167
+ uint8_t n = COMBINE_TYPE (type , TYPE_NUMBER_DWORD );
154
168
wb_push (wb , & n , 1 );
155
- wb_push (wb , & v , 4 );
169
+ uint32_t v32 = (uint32_t )v ;
170
+ wb_push (wb , & v32 , sizeof (v32 ));
156
171
}
157
172
}
158
173
159
174
static inline void
160
- wb_number (struct write_block * wb , double v ) {
161
- uint8_t n = COMBINE_TYPE (TYPE_NUMBER , 8 );
175
+ wb_real (struct write_block * wb , double v ) {
176
+ uint8_t n = COMBINE_TYPE (TYPE_NUMBER , TYPE_NUMBER_REAL );
162
177
wb_push (wb , & n , 1 );
163
- wb_push (wb , & v , 8 );
178
+ wb_push (wb , & v , sizeof ( v ) );
164
179
}
165
180
166
181
static inline void
@@ -203,7 +218,7 @@ wb_table_array(lua_State *L, struct write_block * wb, int index, int depth) {
203
218
if (array_size >= MAX_COOKIE - 1 ) {
204
219
uint8_t n = COMBINE_TYPE (TYPE_TABLE , MAX_COOKIE - 1 );
205
220
wb_push (wb , & n , 1 );
206
- wb_integer (wb , array_size , TYPE_NUMBER );
221
+ wb_integer (wb , array_size );
207
222
} else {
208
223
uint8_t n = COMBINE_TYPE (TYPE_TABLE , array_size );
209
224
wb_push (wb , & n , 1 );
@@ -224,11 +239,12 @@ wb_table_hash(lua_State *L, struct write_block * wb, int index, int depth, int a
224
239
lua_pushnil (L );
225
240
while (lua_next (L , index ) != 0 ) {
226
241
if (lua_type (L ,-2 ) == LUA_TNUMBER ) {
227
- lua_Number k = lua_tonumber (L ,-2 );
228
- int32_t x = (int32_t )lua_tointeger (L ,-2 );
229
- if (k == (lua_Number )x && x > 0 && x <=array_size ) {
230
- lua_pop (L ,1 );
231
- continue ;
242
+ if (lua_isinteger (L , -2 )) {
243
+ lua_Integer x = lua_tointeger (L ,-2 );
244
+ if (x > 0 && x <=array_size ) {
245
+ lua_pop (L ,1 );
246
+ continue ;
247
+ }
232
248
}
233
249
}
234
250
pack_one (L ,wb ,-2 ,depth );
@@ -259,12 +275,12 @@ pack_one(lua_State *L, struct write_block *b, int index, int depth) {
259
275
wb_nil (b );
260
276
break ;
261
277
case LUA_TNUMBER : {
262
- int32_t x = (int32_t )lua_tointeger (L ,index );
263
- lua_Number n = lua_tonumber (L ,index );
264
- if ((lua_Number )x == n ) {
265
- wb_integer (b , x , TYPE_NUMBER );
278
+ if (lua_isinteger (L , index )) {
279
+ lua_Integer x = lua_tointeger (L ,index );
280
+ wb_integer (b , x );
266
281
} else {
267
- wb_number (b ,n );
282
+ lua_Number n = lua_tonumber (L ,index );
283
+ wb_real (b ,n );
268
284
}
269
285
break ;
270
286
}
@@ -310,31 +326,42 @@ invalid_stream_line(lua_State *L, struct read_block *rb, int line) {
310
326
311
327
#define invalid_stream (L ,rb ) invalid_stream_line(L,rb,__LINE__)
312
328
313
- static int
329
+ static lua_Integer
314
330
get_integer (lua_State * L , struct read_block * rb , int cookie ) {
315
331
switch (cookie ) {
316
- case 0 :
332
+ case TYPE_NUMBER_ZERO :
317
333
return 0 ;
318
- case 1 : {
319
- uint8_t n = 0 ;
320
- uint8_t * pn = rb_read (rb ,& n , 1 );
334
+ case TYPE_NUMBER_BYTE : {
335
+ uint8_t n ;
336
+ uint8_t * pn = rb_read (rb ,sizeof ( n ) );
321
337
if (pn == NULL )
322
338
invalid_stream (L ,rb );
323
- return * pn ;
339
+ n = * pn ;
340
+ return n ;
324
341
}
325
- case 2 : {
326
- uint16_t n = 0 ;
327
- uint16_t * pn = rb_read (rb ,& n , 2 );
342
+ case TYPE_NUMBER_WORD : {
343
+ uint16_t n ;
344
+ uint16_t * pn = rb_read (rb ,sizeof ( n ) );
328
345
if (pn == NULL )
329
346
invalid_stream (L ,rb );
330
- return * pn ;
347
+ memcpy (& n , pn , sizeof (n ));
348
+ return n ;
331
349
}
332
- case 4 : {
333
- int n = 0 ;
334
- int * pn = rb_read (rb ,& n , 4 );
350
+ case TYPE_NUMBER_DWORD : {
351
+ int32_t n ;
352
+ int32_t * pn = rb_read (rb ,sizeof ( n ) );
335
353
if (pn == NULL )
336
354
invalid_stream (L ,rb );
337
- return * pn ;
355
+ memcpy (& n , pn , sizeof (n ));
356
+ return n ;
357
+ }
358
+ case TYPE_NUMBER_QWORD : {
359
+ int64_t n ;
360
+ int64_t * pn = rb_read (rb ,sizeof (n ));
361
+ if (pn == NULL )
362
+ invalid_stream (L ,rb );
363
+ memcpy (& n , pn , sizeof (n ));
364
+ return n ;
338
365
}
339
366
default :
340
367
invalid_stream (L ,rb );
@@ -343,32 +370,29 @@ get_integer(lua_State *L, struct read_block *rb, int cookie) {
343
370
}
344
371
345
372
static double
346
- get_number (lua_State * L , struct read_block * rb , int cookie ) {
347
- if (cookie == 8 ) {
348
- double n = 0 ;
349
- double * pn = rb_read (rb ,& n ,8 );
350
- if (pn == NULL )
351
- invalid_stream (L ,rb );
352
- return * pn ;
353
- } else {
354
- return (double )get_integer (L ,rb ,cookie );
355
- }
373
+ get_real (lua_State * L , struct read_block * rb ) {
374
+ double n ;
375
+ double * pn = rb_read (rb ,sizeof (n ));
376
+ if (pn == NULL )
377
+ invalid_stream (L ,rb );
378
+ memcpy (& n , pn , sizeof (n ));
379
+ return n ;
356
380
}
357
381
358
382
static void *
359
383
get_pointer (lua_State * L , struct read_block * rb ) {
360
384
void * userdata = 0 ;
361
- void * * v = (void * * )rb_read (rb ,& userdata , sizeof (userdata ));
385
+ void * * v = (void * * )rb_read (rb ,sizeof (userdata ));
362
386
if (v == NULL ) {
363
387
invalid_stream (L ,rb );
364
388
}
365
- return * v ;
389
+ memcpy (& userdata , v , sizeof (userdata ));
390
+ return userdata ;
366
391
}
367
392
368
393
static void
369
394
get_buffer (lua_State * L , struct read_block * rb , int len ) {
370
- char tmp [len ];
371
- char * p = rb_read (rb ,tmp ,len );
395
+ char * p = rb_read (rb ,len );
372
396
if (p == NULL ) {
373
397
invalid_stream (L ,rb );
374
398
}
@@ -380,12 +404,17 @@ static void unpack_one(lua_State *L, struct read_block *rb);
380
404
static void
381
405
unpack_table (lua_State * L , struct read_block * rb , int array_size ) {
382
406
if (array_size == MAX_COOKIE - 1 ) {
383
- uint8_t type = 0 ;
384
- uint8_t * t = rb_read (rb , & type , 1 );
385
- if (t == NULL || ( * t & 7 ) != TYPE_NUMBER ) {
407
+ uint8_t type ;
408
+ uint8_t * t = rb_read (rb , sizeof ( type ) );
409
+ if (t == NULL ) {
386
410
invalid_stream (L ,rb );
387
411
}
388
- array_size = (int )get_number (L ,rb ,* t >> 3 );
412
+ type = * t ;
413
+ int cookie = type >> 3 ;
414
+ if ((type & 7 ) != TYPE_NUMBER || cookie == TYPE_NUMBER_REAL ) {
415
+ invalid_stream (L ,rb );
416
+ }
417
+ array_size = get_integer (L ,rb ,cookie );
389
418
}
390
419
lua_createtable (L ,array_size ,0 );
391
420
int i ;
@@ -414,7 +443,11 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) {
414
443
lua_pushboolean (L ,cookie );
415
444
break ;
416
445
case TYPE_NUMBER :
417
- lua_pushnumber (L ,get_number (L ,rb ,cookie ));
446
+ if (cookie == TYPE_NUMBER_REAL ) {
447
+ lua_pushnumber (L ,get_real (L ,rb ));
448
+ } else {
449
+ lua_pushinteger (L , get_integer (L , rb , cookie ));
450
+ }
418
451
break ;
419
452
case TYPE_USERDATA :
420
453
lua_pushlightuserdata (L ,get_pointer (L ,rb ));
@@ -423,22 +456,25 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) {
423
456
get_buffer (L ,rb ,cookie );
424
457
break ;
425
458
case TYPE_LONG_STRING : {
426
- uint32_t len ;
427
459
if (cookie == 2 ) {
428
- uint16_t * plen = rb_read (rb , & len , 2 );
460
+ uint16_t * plen = rb_read (rb , 2 );
429
461
if (plen == NULL ) {
430
462
invalid_stream (L ,rb );
431
463
}
432
- get_buffer (L ,rb ,(int )* plen );
464
+ uint16_t n ;
465
+ memcpy (& n , plen , sizeof (n ));
466
+ get_buffer (L ,rb ,n );
433
467
} else {
434
468
if (cookie != 4 ) {
435
469
invalid_stream (L ,rb );
436
470
}
437
- uint32_t * plen = rb_read (rb , & len , 4 );
471
+ uint32_t * plen = rb_read (rb , 4 );
438
472
if (plen == NULL ) {
439
473
invalid_stream (L ,rb );
440
474
}
441
- get_buffer (L ,rb ,(int )* plen );
475
+ uint32_t n ;
476
+ memcpy (& n , plen , sizeof (n ));
477
+ get_buffer (L ,rb ,n );
442
478
}
443
479
break ;
444
480
}
@@ -455,12 +491,13 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) {
455
491
456
492
static void
457
493
unpack_one (lua_State * L , struct read_block * rb ) {
458
- uint8_t type = 0 ;
459
- uint8_t * t = rb_read (rb , & type , 1 );
494
+ uint8_t type ;
495
+ uint8_t * t = rb_read (rb , sizeof ( type ) );
460
496
if (t == NULL ) {
461
497
invalid_stream (L , rb );
462
498
}
463
- push_value (L , rb , * t & 0x7 , * t >>3 );
499
+ type = * t ;
500
+ push_value (L , rb , type & 0x7 , type >>3 );
464
501
}
465
502
466
503
static void
@@ -516,10 +553,11 @@ _luaseri_unpack(lua_State *L) {
516
553
lua_checkstack (L ,i );
517
554
}
518
555
uint8_t type = 0 ;
519
- uint8_t * t = rb_read (& rb , & type , 1 );
556
+ uint8_t * t = rb_read (& rb , sizeof ( type ) );
520
557
if (t == NULL )
521
558
break ;
522
- push_value (L , & rb , * t & 0x7 , * t >>3 );
559
+ type = * t ;
560
+ push_value (L , & rb , type & 0x7 , type >>3 );
523
561
}
524
562
525
563
// Need not free buffer
0 commit comments