1
1
#include <string.h>
2
+ #include <stdlib.h>
2
3
#include "msvcint.h"
3
4
4
5
#include "lua.h"
@@ -41,8 +42,15 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
41
42
static int
42
43
lnewproto (lua_State * L ) {
43
44
size_t sz = 0 ;
44
- void * buffer = (void * )luaL_checklstring (L ,1 ,& sz );
45
- struct sproto * sp = sproto_create (buffer , sz );
45
+ void * buffer ;
46
+ struct sproto * sp ;
47
+ if (lua_isuserdata (L ,1 )) {
48
+ buffer = lua_touserdata (L ,1 );
49
+ sz = luaL_checkinteger (L ,2 );
50
+ } else {
51
+ buffer = (void * )luaL_checklstring (L ,1 ,& sz );
52
+ }
53
+ sp = sproto_create (buffer , sz );
46
54
if (sp ) {
47
55
lua_pushlightuserdata (L , sp );
48
56
return 1 ;
@@ -449,6 +457,31 @@ lprotocol(lua_State *L) {
449
457
return 3 ;
450
458
}
451
459
460
+ /* global sproto pointer for multi states */
461
+ static void * G_sproto = NULL ;
462
+ static size_t G_sproto_sz = 0 ;
463
+
464
+ static int
465
+ lsaveproto (lua_State * L ) {
466
+ size_t sz ;
467
+ void * buffer = (void * )luaL_checklstring (L ,1 ,& sz );
468
+ void * tmp = malloc (sz );
469
+ memcpy (tmp , buffer , sz );
470
+ if (G_sproto ) {
471
+ free (G_sproto );
472
+ }
473
+ G_sproto = tmp ;
474
+ G_sproto_sz = sz ;
475
+ return 0 ;
476
+ }
477
+
478
+ static int
479
+ lloadproto (lua_State * L ) {
480
+ lua_pushlightuserdata (L , G_sproto );
481
+ lua_pushinteger (L , G_sproto_sz );
482
+ return 2 ;
483
+ }
484
+
452
485
int
453
486
luaopen_sproto_core (lua_State * L ) {
454
487
#ifdef luaL_checkversion
@@ -461,6 +494,8 @@ luaopen_sproto_core(lua_State *L) {
461
494
{ "querytype" , lquerytype },
462
495
{ "decode" , ldecode },
463
496
{ "protocol" , lprotocol },
497
+ { "loadproto" , lloadproto },
498
+ { "saveproto" , lsaveproto },
464
499
{ NULL , NULL },
465
500
};
466
501
luaL_newlib (L ,l );
0 commit comments