static void amf3_encode_table_as_object(lua_State *L, amf_buf *buf, int idx, int sidx, int oidx, int tidx) { abs_idx(L, idx); amf_buf_append_char(buf, AMF3_OBJECT); if (amf3_encode_ref(L, buf, idx, oidx) >= 0) { return; } lua_newtable(L); /* traits table */ int members = 1; for(lua_pushnil(L); lua_next(L, idx); lua_pop(L, 1)) { switch (lua_type(L, -2)) { case LUA_TNUMBER: lua_pushvalue(L, -2); lua_tostring(L, -1); break; case LUA_TSTRING: lua_pushvalue(L, -2); break; default: continue; } lua_rawseti(L, -4, members++); } amf3_encode_traits(L, buf, lua_gettop(L), sidx, tidx); lua_pop(L, 1); /* drop the traits table */ for(lua_pushnil(L); lua_next(L, idx); lua_pop(L, 1)) { amf3_encode(L, buf, -1, sidx, oidx, tidx); } }
int lua_amf_encode(lua_State *L) { int ver, freebuf = 1; amf_buf *buf; if (lua_isnumber(L, 1)) { ver = luaL_checkint(L, 1); check_amf_ver(ver, 1); luaL_checkany(L, 2); buf = amf_buf_init(NULL); } else { buf = luaL_checkudata(L, 1, "amf_buffer"); ver = luaL_checkint(L, 2); check_amf_ver(ver, 2); luaL_checkany(L, 3); freebuf = 0; } if (buf == NULL) { lua_pushnil(L); return 1; } int base = lua_gettop(L); if (ver == AMF_VER0) { lua_newtable(L); amf0_encode(L, buf, 0, base, base+1); } else { lua_newtable(L); lua_newtable(L); lua_newtable(L); amf3_encode(L, buf, base, base+1, base+2, base+3); } if (freebuf) { lua_pushlstring(L, buf->b, buf->len); amf_buf_free(buf); return 1; } else { return 0; } }
static void amf3_encode_table_as_array(lua_State *L, amf_buf *buf, int idx, int sidx, int oidx, int tidx, int array_len) { abs_idx(L, idx); amf_buf_append_char(buf, AMF3_ARRAY); if (amf3_encode_ref(L, buf, idx, oidx) >=0) { return; } amf_buf_append_u29(buf, (array_len << 1) | 1); /*Send an empty string to imply no named keys*/ amf_buf_append_u29(buf, (0 << 1) | 1); for (int i = 1; i <= array_len; i++) { lua_rawgeti(L, idx, i); amf3_encode(L, buf, -1, sidx, oidx, tidx); lua_pop(L, 1); } }
void amf0_encode(lua_State *L, amf_buf *buf, int avmplus, int idx, int ridx) { int array_len, old_top, ref; size_t len; const char *str; abs_idx(L, idx); abs_idx(L, ridx); old_top = lua_gettop(L); switch (lua_type(L, idx)) { case LUA_TNIL: amf_buf_append_char(buf, AMF0_NULL); break; case LUA_TBOOLEAN: amf_buf_append_char(buf, AMF0_BOOLEAN); amf_buf_append_char(buf, lua_toboolean(L, idx) ? 1 : 0); break; case LUA_TNUMBER: amf_buf_append_char(buf, AMF0_NUMBER); amf0_encode_number(buf, lua_tonumber(L, idx)); break; case LUA_TSTRING: str = lua_tolstring(L, idx, &len); amf0_encode_string(buf, str, len); break; case LUA_TTABLE: { if (avmplus) { lua_newtable(L); lua_newtable(L); lua_newtable(L); int top = lua_gettop(L); amf3_encode(L, buf, idx, top-2, top-1, top); lua_pop(L, 3); } else { ref = amf0_encode_ref(buf, L, idx, ridx); if (ref >= 0) { break; } array_len = strict_array_length(L, idx); if (array_len == 0) { amf_buf_append_char(buf, AMF0_NULL); } else if (array_len > 0) { amf0_encode_table_as_array(buf, L, idx, ridx, array_len); } else { amf0_encode_table_as_object(buf, L, idx, ridx); } } break; } } assert(lua_gettop(L) == old_top); }