From patchwork Tue Oct 10 08:31:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Storm, Christian" X-Patchwork-Id: 823745 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4010:c07::23f; helo=mail-lf0-x23f.google.com; envelope-from=swupdate+bncbdd6bwv65qpbbz4k6lhakgqevahmr4q@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b="aJU4usbY"; dkim-atps=neutral Received: from mail-lf0-x23f.google.com (mail-lf0-x23f.google.com [IPv6:2a00:1450:4010:c07::23f]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yB9Lz0l1wz9tY9 for ; Tue, 10 Oct 2017 19:31:37 +1100 (AEDT) Received: by mail-lf0-x23f.google.com with SMTP id s19sf2247134lfi.19 for ; Tue, 10 Oct 2017 01:31:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1507624295; cv=pass; d=google.com; s=arc-20160816; b=HFH2cIBbSr9Ke7R1RGkcjxvIU2n8RNHGh+5EHLVOPPJSEkJRGgBs/EpFG+95jlBx6Z 9rscjdOd9iMqeAYqwbDnBoxXcya+516nLgbvMUWtqRxyKCcVJ16Nw5QbiebJGrEfqXVP 0Vl/lvvT3eAuF1I8KA7KB5q1ertzwulLExEHiQMoch77VQTjyGVssCUWV24BS4xJXckt 08Bl9DL7cx2BYzlXIxkZUkGQRTgb8eoCY2YBP6R/Fn2UdJ2XnkoImCAXLHIKopxQ+4Fa l+8BxF+HydwCes3plNQ7a/2+9NkBojLIr52juNTa58RlzEIyNgcys8Yh9NmqYxA7bbep 7gPw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:message-id:date:subject:cc:to:from :arc-authentication-results:arc-message-signature:mime-version :sender:dkim-signature:arc-authentication-results; bh=RZ4CmsZfH0AgroqJ1cjNTOec/rVOGL48PlysF18CQiQ=; b=jpW8EfszoGFxMnKa5P29VXHHHGgXnhpPmhRa473QXScjVMeiJfZ+CPCYvYxwHlDTl5 BENHPzJo1hznyaRO/4l7B+B4GqT6nCE1ax7jbm9ntJTJyr3HZUyNStVhpgiSBU4VBBcw DbucC3Y03lQKYoBj4CVJkKkb0g9WPARzgjJnb6rdL9JYPMojkzQ+oh+RqudWunYTUsSU YFQ7EA8C9WWyOwcek431Ef5UuualQxEZqs9CE/DdA4ukqZVNPPI6R+E81Nitp05QZcB6 xX4H5b9tjhVdt97PQZ8LOPIjPe7wphWbeN33sPdhrcfS0Uk/WfGYvGT+ffx/gzxKFcCh rKtg== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 192.35.17.14 is neither permitted nor denied by domain of christian.storm@siemens.com) smtp.mailfrom=christian.storm@siemens.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:mime-version:from:to:cc:subject:date:message-id :x-original-sender:x-original-authentication-results:precedence :mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=RZ4CmsZfH0AgroqJ1cjNTOec/rVOGL48PlysF18CQiQ=; b=aJU4usbYfpm8QNMjAsc4K8IB6kpvzRYjyHQdy2g4UjWOEhE5YWgT7muWuyb5aS/r/+ eLXrIe7B0c1hmCOU0RAhap6VWyahnP3N7W229BMv+XJUZTw5UDMLDNAb0PZv0oLQUdPz aZ6Y/Ddlv93XTbA2EuVA15UHjH0bxOWr0OFtTD1J6am/mdh2HqxSgIAcoVjMOEnAfXVQ Zpn+/WSkVwacGHqYY3SI/TK2e85UENORzyBLHOKoAd4QdxcDp4RICdMv2XbcQRlveVMS ERExitYa/W7Q9U9Onp6krAkD9wWlcLoqtptmXOhhWgeotB1cLVl1B9uydFsTScs7U1Kc BkrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:mime-version:from:to:cc:subject:date :message-id:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=RZ4CmsZfH0AgroqJ1cjNTOec/rVOGL48PlysF18CQiQ=; b=bHpDXdIckYiLAGQXpxKT3twpS7l5h9EVWlCG8+YTkL4l1IAz0GNFR4c/qxU2HRXZ7f AtaXa7g32d06ti6q67neD6H0T5bAYInM3Zjy0ons3rCQ5FwIw/omij9hMtGJuT5HhIL4 aAXHSDtNhuu/GQrUsFGaji4Ko+2QWapeoZjLhg77x3av84FW6+piB2NGxy4IfQWDRlSy UceEG7oa0dnbf7ip/K6CrqDWd0HQ4Qhul0SUSPFubMDq2H0IxhNWrMZ2d0zSKN+/ZdbT DwLnwMRaT0eIgaCwRUkaFYB6CN7euaSTR8Mt7vWfAH4u69B64fxkuqnTlQUfQ2O8xMZq EDuA== Sender: swupdate@googlegroups.com X-Gm-Message-State: AMCzsaWnFOF7jLA/Aw9ScwMJlwvc4EjFBK4JyWAKtyPJtS2tg/gp2is1 6t79XUbshqHcGfP8f0LhgO4= X-Google-Smtp-Source: AOwi7QAjbfm7lAlOnQ7/hRd0/LGV7XecqkgaEb+lSRMpf4RNHDTdclakbrtk5WTnvXVLE30WVsEU+g== X-Received: by 10.28.213.5 with SMTP id m5mr49206wmg.1.1507624295256; Tue, 10 Oct 2017 01:31:35 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: swupdate@googlegroups.com Received: by 10.28.50.138 with SMTP id y132ls615584wmy.9.gmail; Tue, 10 Oct 2017 01:31:34 -0700 (PDT) X-Received: by 10.28.184.81 with SMTP id i78mr1619623wmf.2.1507624294936; Tue, 10 Oct 2017 01:31:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507624294; cv=none; d=google.com; s=arc-20160816; b=q1u9YkdmMXOJDkoQ03H4BnaNNAdsLFhf4vX0A6YlublTDcxEj85ThMxPGSOCwXPqSO 2MaW40rvud05M+fvLb9/Roe0PFNKseIA9+8h8tZ7Yk5V2WWeuZzs5QLJ/taxXPZPKi3D SYqKoPzJk31KRc6B6CwJXgxLcQxXCa+SqjxLmeeKuekX2/jSP+Wihh2+9Ca7ZFCD7/Qe 4uLcdamikj+pSa0nRbs2RCyhKcQ9FmydJvAMgEvbRoRg9UWUe9/LDX6dPWpSt0KXhGYO zJoYvfNLsceDxaKueHG08nTfJexzL3q7CdqPYtAm8sCzyQlziMOHaPOiQ9Y5PBVLkJ6o Kw3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:arc-authentication-results; bh=AZFZCZXgunNTYJ8QZMHDqOuRdPTBkWYL1zPZj/0wKFs=; b=zOoNKYxTuMIFASiswuoaJWf9s18iKjPkEerEifS3rgoxd/XKFUTdXLqL2suoplScuq yAxl3MnAMQ4JZP1++aV3+RCB1r6Vc4UH5sP/mA4CRRTAWQCK8ck3BOsz0m4DB0GhvvgP S6cxnUbfS06TFuakHe6LauSCA+ScTCvDCwahmOikSEAdZe9+sYzKF/LRmZUSiONRIsYl Ob3K6dod455qQzrIALd3mvOh/4cSeDo7uZPD98ZXt4Qha1HgHfllTtO2x7wagQp4PRmz g+pfGYZ8UsFnyuHbkyD2xpzvaCbASEBkotUKvpN8oTCNAVGXdAHVCr7dINF8/XNc6P7H QiWg== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 192.35.17.14 is neither permitted nor denied by domain of christian.storm@siemens.com) smtp.mailfrom=christian.storm@siemens.com Received: from david.siemens.de (david.siemens.de. [192.35.17.14]) by gmr-mx.google.com with ESMTPS id m74si957669wma.0.2017.10.10.01.31.34 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Oct 2017 01:31:34 -0700 (PDT) Received-SPF: neutral (google.com: 192.35.17.14 is neither permitted nor denied by domain of christian.storm@siemens.com) client-ip=192.35.17.14; Received: from mail3.siemens.de (mail3.siemens.de [139.25.208.14]) by david.siemens.de (8.15.2/8.15.2) with ESMTPS id v9A8VYTl000564 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 10 Oct 2017 10:31:34 +0200 Received: from MD1KR9XC.ww002.siemens.net ([139.25.69.251]) by mail3.siemens.de (8.15.2/8.15.2) with ESMTP id v9A8VYSw030845; Tue, 10 Oct 2017 10:31:34 +0200 From: Christian Storm To: swupdate@googlegroups.com Cc: Christian Storm Subject: [swupdate] [PATCH] Lua: introduce LuaJIT/Lua 5.1 compatibility Date: Tue, 10 Oct 2017 10:31:23 +0200 Message-Id: <20171010083123.3042-1-christian.storm@siemens.com> X-Mailer: git-send-email 2.14.2 X-Original-Sender: christian.storm@siemens.com X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 192.35.17.14 is neither permitted nor denied by domain of christian.storm@siemens.com) smtp.mailfrom=christian.storm@siemens.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Introduce LuaJIT/Lua 5.1 compatibility by integrating backports of currently used 5.2+ functionality from https://github.com/keplerproject/lua-compat-5.2 Future functionality backports should be implemented in corelib/lua_compat.c as well, serving as a central place for this purpose. Signed-off-by: Christian Storm Reviewed-by: Stefano Babic Tested-by: Jörg Krause --- corelib/Makefile | 2 +- corelib/lua_compat.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ include/lua_util.h | 7 +++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 corelib/lua_compat.c diff --git a/corelib/Makefile b/corelib/Makefile index d99edf2..16767b2 100644 --- a/corelib/Makefile +++ b/corelib/Makefile @@ -7,7 +7,7 @@ lib-y += installer.o \ swupdate_dict.o lib-$(CONFIG_DOWNLOAD) += downloader.o lib-$(CONFIG_MTD) += mtd-interface.o -lib-$(CONFIG_LUA) += lua_interface.o +lib-$(CONFIG_LUA) += lua_interface.o lua_compat.o lib-$(CONFIG_HASH_VERIFY) += verify_signature.o lib-$(CONFIG_ENCRYPTED_IMAGES) += swupdate_decrypt.o lib-$(CONFIG_LIBCONFIG) += swupdate_settings.o \ diff --git a/corelib/lua_compat.c b/corelib/lua_compat.c new file mode 100644 index 0000000..6204e8d --- /dev/null +++ b/corelib/lua_compat.c @@ -0,0 +1,59 @@ +/* + * Author: Christian Storm + * Copyright (C) 2017, Siemens AG + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc. + */ + +#include +#include + +/* + * These LuaJIT/Lua 5.1 compatibility functions are taken from + * https://github.com/keplerproject/lua-compat-5.2 + */ +#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501 +void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) +{ + luaL_checkstack(L, nup+1, "too many upvalues"); + for (; l->name != NULL; l++) { /* fill the table with given functions */ + int i; + lua_pushstring(L, l->name); + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -(nup + 1)); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */ + } + lua_pop(L, nup); /* remove upvalues */ +} + +void luaL_requiref(lua_State *L, char const* modname, lua_CFunction openf, int glb) +{ + luaL_checkstack(L, 3, "not enough stack slots"); + lua_pushcfunction(L, openf); + lua_pushstring(L, modname); + lua_call(L, 1, 1); + lua_getglobal(L, "package"); + lua_getfield(L, -1, "loaded"); + lua_replace(L, -2); + lua_pushvalue(L, -2); + lua_setfield(L, -2, modname); + lua_pop(L, 1); + if (glb) { + lua_pushvalue(L, -1); + lua_setglobal(L, modname); + } +} +#endif diff --git a/include/lua_util.h b/include/lua_util.h index 7842cc5..684eddf 100644 --- a/include/lua_util.h +++ b/include/lua_util.h @@ -36,6 +36,13 @@ int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img); int lua_handlers_init(void); #define lua_parser_exit(L) lua_close((lua_State *)L) +#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501 +#define LUA_OK 0 +#define luaL_newlib(L, l) (lua_newtable((L)),luaL_setfuncs((L), (l), 0)) +void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup); +void luaL_requiref(lua_State *L, char const* modname, lua_CFunction openf, int glb); +#endif + #else #define lua_State void