From 7f67c5250a627c13e8797ff6fa5e42deec50bfdd Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 16 Aug 2020 02:56:38 +0200 Subject: lua: pass strings with embedded zeros as byte arrays This was a vague idea how to handle passing byte arrays from Lua to the mpv command interface in a somewhat reasonable way. The idea was cancelled, but leave the Lua part of it, since it might get useful later, and prevents passing (and silently cutting off) such byte strings. Barely tested, let's say not tested at all. --- player/lua.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'player') diff --git a/player/lua.c b/player/lua.c index 440e3c6ff8..97ec8d33a4 100644 --- a/player/lua.c +++ b/player/lua.c @@ -678,10 +678,21 @@ static void makenode(void *tmp, mpv_node *dst, lua_State *L, int t) dst->format = MPV_FORMAT_FLAG; dst->u.flag = !!lua_toboolean(L, t); break; - case LUA_TSTRING: - dst->format = MPV_FORMAT_STRING; - dst->u.string = talloc_strdup(tmp, lua_tostring(L, t)); + case LUA_TSTRING: { + size_t len = 0; + char *s = (char *)lua_tolstring(L, t, &len); + bool has_zeros = !!memchr(s, 0, len); + if (has_zeros) { + mpv_byte_array *ba = talloc_zero(tmp, mpv_byte_array); + *ba = (mpv_byte_array){talloc_memdup(tmp, s, len), len}; + dst->format = MPV_FORMAT_BYTE_ARRAY; + dst->u.ba = ba; + } else { + dst->format = MPV_FORMAT_STRING; + dst->u.string = talloc_strdup(tmp, s); + } break; + } case LUA_TTABLE: { // Lua uses the same type for arrays and maps, so guess the correct one. int format = MPV_FORMAT_NONE; -- cgit v1.2.3