/* TODO: typed object support */ static void amf0_encode_table_as_object(amf_buf *buf, lua_State *L, int idx, int ridx) { size_t key_len; const char *key; amf_buf_append_char(buf, AMF0_OBJECT); for (lua_pushnil(L); lua_next(L, idx); lua_pop(L, 1)) { switch (lua_type(L, -2)) { case LUA_TNUMBER: lua_pushvalue(L, -2); key = lua_tolstring(L, -1, &key_len); amf_buf_append_u16(buf, (uint16_t)key_len); amf_buf_append(buf, key, key_len); lua_pop(L, 1); break; case LUA_TSTRING: key = lua_tolstring(L, -2, &key_len); amf_buf_append_u16(buf, (uint16_t)key_len); amf_buf_append(buf, key, key_len); break; default: continue; } amf0_encode(L, buf, 0, -1, ridx); } amf_buf_append_u16(buf, (uint16_t)0); amf_buf_append_char(buf, AMF0_END_OF_OBJECT); }
static void encode_hdr(lua_State *L, amf_buf *buf, int ver) { if(!lua_istable(L, -1)) { luaL_error(L, "invalid header structure, must be a dense table"); } /* name */ lua_rawgeti(L, -1, 1); size_t len; const char *name = lua_tolstring(L, -1, &len); amf_buf_append_u16(buf, (uint16_t)len); if (len > 0) amf_buf_append(buf, name, (uint16_t)len); lua_pop(L, 1); /* must understand */ lua_rawgeti(L, -1, 2); int mu = lua_toboolean(L, -1); amf_buf_append_char(buf, (char)mu); lua_pop(L, 1); /* content length */ amf_buf_append_u32(buf, (uint32_t)0); lua_rawgeti(L, -1, 3); lua_newtable(L); amf0_encode(L, buf, (ver == 3), -2, lua_gettop(L)); lua_pop(L, 2); /* pops the obj and ref table */ }
static int lua_amf_buffer_write_str(lua_State *L) { amf_buf *b = luaL_checkudata(L, 1, "amf_buffer"); size_t len; const char *str = luaL_checklstring(L, 2, &len); printf("string len: %d\n", (uint16_t)len); amf_buf_append_u16(b, (uint16_t)len); amf_buf_append(b, str, len); return 0; }
static void encode_body(lua_State *L, amf_buf *buf, int ver) { if(!lua_istable(L, -1)) { luaL_error(L, "invalid body structure, must be a dense table"); } size_t len; const char *uri; /* target uri */ lua_rawgeti(L, -1, 1); uri = lua_tolstring(L, -1, &len); amf_buf_append_u16(buf, (uint16_t)len); if (len > 0) { amf_buf_append(buf, uri, (uint16_t)len); printf("target: %s\n", uri); } lua_pop(L, 1); /* response uri */ lua_rawgeti(L, -1, 2); uri = lua_tolstring(L, -1, &len); amf_buf_append_u16(buf, (uint16_t)len); if (len > 0) { amf_buf_append(buf, uri, (uint16_t)len); printf("response: %s\n", uri); } lua_pop(L, 1); /* content length */ amf_buf_append_u32(buf, (uint32_t)0); lua_rawgeti(L, -1, 3); lua_newtable(L); amf0_encode(L, buf, (ver == 3), -2, lua_gettop(L)); lua_pop(L, 2); /* pops the obj and ref table */ }
static void amf0_encode_string(amf_buf *b, const char *s, size_t len) { uint16_t u16; uint32_t u32; if (len < UINT16_MAX) { u16 = (uint16_t)len; amf_buf_append_char(b, AMF0_STRING); amf_buf_append_u16(b, u16); amf_buf_append(b, s, u16); } else { // long string if (len > UINT32_MAX) { len = UINT32_MAX; } u32 = (uint32_t)len; amf_buf_append_char(b, AMF0_L_STRING); amf_buf_append_u32(b, u32); amf_buf_append(b, s, u32); } }
static void amf3_encode_string(lua_State *L, amf_buf *buf, int idx, int sidx) { size_t len; const char *s = lua_tolstring(L, idx, &len); if (len > AMF3_MAX_STR_LEN) len = AMF3_MAX_STR_LEN; if (len > 0) { if (amf3_encode_ref(L, buf, idx, sidx) < 0) { amf_buf_append_u29(buf, (len << 1 | 1)); amf_buf_append(buf, s, len); } } else { amf_buf_append_u29(buf, (0 << 1 | 1)); } }