mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 03:53:09 +00:00
add netpack.pack_padding
This commit is contained in:
@@ -32,7 +32,7 @@ function handler.connect(fd, addr)
|
||||
end
|
||||
|
||||
local function auth(fd, msg, sz)
|
||||
-- base64(base64(uid)@base64(server)#base64(subid):index:request:base64(hmac)
|
||||
-- base64(uid)@base64(server)#base64(subid):index:request:base64(hmac)
|
||||
local message = netpack.tostring(msg, sz)
|
||||
local username, index, request , hmac = string.match(message, "([^:]*):([^:]*):([^:]*):([^:]*)")
|
||||
local content = users[username]
|
||||
|
||||
@@ -393,13 +393,13 @@ lpop(lua_State *L) {
|
||||
*/
|
||||
|
||||
static const char *
|
||||
tolstring(lua_State *L, size_t *sz) {
|
||||
tolstring(lua_State *L, size_t *sz, int index) {
|
||||
const char * ptr;
|
||||
if (lua_isuserdata(L,1)) {
|
||||
ptr = (const char *)lua_touserdata(L,1);
|
||||
*sz = (size_t)luaL_checkinteger(L, 2);
|
||||
if (lua_isuserdata(L,index)) {
|
||||
ptr = (const char *)lua_touserdata(L,index);
|
||||
*sz = (size_t)luaL_checkinteger(L, index+1);
|
||||
} else {
|
||||
ptr = luaL_checklstring(L, 1, sz);
|
||||
ptr = luaL_checklstring(L, index, sz);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
@@ -413,7 +413,7 @@ write_size(uint8_t * buffer, int len) {
|
||||
static int
|
||||
lpack(lua_State *L) {
|
||||
size_t len;
|
||||
const char * ptr = tolstring(L, &len);
|
||||
const char * ptr = tolstring(L, &len, 1);
|
||||
if (len > 0x10000) {
|
||||
return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
|
||||
}
|
||||
@@ -433,7 +433,7 @@ lpack_string(lua_State *L) {
|
||||
uint8_t tmp[SMALLSTRING+2];
|
||||
size_t len;
|
||||
uint8_t *buffer;
|
||||
const char * ptr = tolstring(L, &len);
|
||||
const char * ptr = tolstring(L, &len, 1);
|
||||
if (len > 0x10000) {
|
||||
return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
|
||||
}
|
||||
@@ -451,6 +451,34 @@ lpack_string(lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
lpack_padding(lua_State *L) {
|
||||
uint8_t tmp[SMALLSTRING+2];
|
||||
size_t content_sz;
|
||||
uint8_t *buffer;
|
||||
const char * ptr = tolstring(L, &content_sz, 2);
|
||||
size_t header_sz = 0;
|
||||
const char * header = luaL_checklstring(L,1,&header_sz);
|
||||
size_t len = header_sz + content_sz;
|
||||
|
||||
if (len > 0x10000) {
|
||||
return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
|
||||
}
|
||||
|
||||
if (len <= SMALLSTRING) {
|
||||
buffer = tmp;
|
||||
} else {
|
||||
buffer = lua_newuserdata(L, len + 2);
|
||||
}
|
||||
|
||||
write_size(buffer, len);
|
||||
memcpy(buffer+2, header, header_sz);
|
||||
memcpy(buffer+2+header_sz, ptr, content_sz);
|
||||
lua_pushlstring(L, (const char *)buffer, len+2);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
ltostring(lua_State *L) {
|
||||
void * ptr = lua_touserdata(L, 1);
|
||||
@@ -471,6 +499,7 @@ luaopen_netpack(lua_State *L) {
|
||||
{ "pop", lpop },
|
||||
{ "pack", lpack },
|
||||
{ "pack_string", lpack_string },
|
||||
{ "pack_padding", lpack_padding },
|
||||
{ "clear", lclear },
|
||||
{ "tostring", ltostring },
|
||||
{ NULL, NULL },
|
||||
|
||||
Reference in New Issue
Block a user