From patchwork Fri Nov 3 12:30:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Storm, Christian" X-Patchwork-Id: 833834 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:400c:c0c::23f; helo=mail-wr0-x23f.google.com; envelope-from=swupdate+bncbdd6bwv65qpbbk6d6hhqkgqetjokzyy@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b="AxFqEc8Q"; dkim-atps=neutral Received: from mail-wr0-x23f.google.com (mail-wr0-x23f.google.com [IPv6:2a00:1450:400c:c0c::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 3yT1Xt2bcLz9sNr for ; Fri, 3 Nov 2017 23:31:42 +1100 (AEDT) Received: by mail-wr0-x23f.google.com with SMTP id c21sf1501650wrg.16 for ; Fri, 03 Nov 2017 05:31:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1509712299; cv=pass; d=google.com; s=arc-20160816; b=tDmO/LaHfXC5CCWvohhldzkRsQ1ATv/S1xL1kIDDwp3UHhVksyWeKvyjAuC+pQkcUh SYTfLl64p5n01n8PTqKVjMnDT3KWUsZ5aVW2pwW9v2QPXNJSIvb+OfkzICHzCIfF0WKt 9T0mJfSZD+fd4pNyKSsHubbpns2h1JcHhATutCGEM8rHgucPmXftbHJjnPkC6Spne5pN fSi5pHFyiCi9Zz/CudL4F3MOltli4Xio+Twi8qahYVgIld5ZQPhN0XAj3HNpbB/WI1tB za9WZ6bpe48TmtuGl0dVSRf13Sea9rMfcNwBbYdAUo6n0qmfhSlJgmlbIf/aF4nNeaGL GE2g== 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:references:in-reply-to:message-id :date:subject:cc:to:from:arc-authentication-results :arc-message-signature:mime-version:sender:dkim-signature :arc-authentication-results; bh=iwHsZlJ7DPtehGTh1nFRxoL5zzKD+TuFp3OA7IpiLqU=; b=eCLlJJ77TPl4gm1QnA5b8OWbgNef1RaTVzw2lvgG54nXFRcou5cyIxBrRVR1EcFwp8 Ldj7QlTn5UTPA9A3EnmEKWVMQB/9jfdgiofoYQFRE4ftBOhW9WCRa/4Uwzb43Pjl/YAv W+v9HkIwZfQ9ODwOiVXxWCMDdlX8nEH6b7v8RnM+AENt+VXNNJMLKNC4hD4qRplv5ciO JzRz/5UtemTI0gtEm8toLksuv4nmbxQVBGgYIx6y8r0CoWx/utw+S8VNyeTrdTAFVt2n hvOgqLuJEnnoqa7Ebn9WMZumKFGnNwnIHz+zDRe1akZr9V+ON/dAopwbj+L9h9xOUC28 3TNQ== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) 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:in-reply-to :references:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=iwHsZlJ7DPtehGTh1nFRxoL5zzKD+TuFp3OA7IpiLqU=; b=AxFqEc8QidLZ3eIIL2OxQbe35zn4mOcXrMObNa+VCU9oipg6tJEpOX6kDItc/VjgOg pnopLrB4dktSHmIZE6WT/8mJEBoU7hBtWji3a6ot8Apt9yR5CzhQhATYTG1wP/QjmtRi z2AgpYqN+gRniX37vWXMyZZ1BH3lj0OANabOnXp2L5GDvXTeVW+xI0F2V5pwn9KGRS1c McFdKY6hbOGixdwaKrAY/16YYH0SK/NC/u+LaFG4FKKLRPJQe0JOMReyMjkpEnsGyJ4v GXD3Qk0wd1bnPRFhzY8XaBjnlmXdLjoHWF6KY11IHVrQugxudilKXkFztmstown2lhmb 1IQA== 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:in-reply-to:references: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=iwHsZlJ7DPtehGTh1nFRxoL5zzKD+TuFp3OA7IpiLqU=; b=DT0H5oajv09yHT35B2UVhVeYb5Qs8GWh1kYJoGnt7OYNe6mckUx24vS8YTefTcpwaw 0NXkuCSNjYWjcaCrWfrRJsTfkrSrFdnAPjtQrvClPTgkVP/3MxfVrwQoCnjaL0Zrn2Hy Au4bEVeqzdubZ/WvCvYCgKAB2mdgESsk8flkNaugr5ccyQNWRfLL9VFvcOZ0JtlzQvJr YUurDkrNRRIGkhiSqBQsVKso8c/FXZnJ7qJ6kiXBwqjEU1cx0rBQ+bPakXx49Smz8dG6 r1cCm0Ni7RXyutkC4C2ZtrKbO1BSWRgOaMBVu10b2roQowdCsIv4lln3Up1d4Yl5+9oe hQ1g== Sender: swupdate@googlegroups.com X-Gm-Message-State: AMCzsaVgUvotBhZC7VGlEssDL8rC9rQDIksMPOflbpKGiLxRD3myYZdT 4UHub8zrm11UgWMI3NHdOQI= X-Google-Smtp-Source: ABhQp+R7Yuyo/Ckm32RtiYcz16ICawMFz5ygSlS2E8QYZcXUSVdCPqIhCM8eFYxNgslX7PI71H+WIg== X-Received: by 10.28.227.138 with SMTP id a132mr67494wmh.7.1509712299478; Fri, 03 Nov 2017 05:31:39 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: swupdate@googlegroups.com Received: by 10.80.147.19 with SMTP id m19ls2870618eda.1.gmail; Fri, 03 Nov 2017 05:31:39 -0700 (PDT) X-Received: by 10.80.222.130 with SMTP id c2mr2135327edl.8.1509712299062; Fri, 03 Nov 2017 05:31:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509712299; cv=none; d=google.com; s=arc-20160816; b=uog0CxKlbZsK803opTYXPDJs66cnrdGOkTPTM4w77AqdsVk4t6VvmYvHQyLl136YtQ z2UcFvuHVa69tDlzz+BlDFZYcriAaR7T96vG2JK7lig5UHUiHib8FM1bc1hPa1+z2RQ+ +gfUx7ZDLHbfDo5MT0wp6Jazmg4iEzDmupR2XS2F7AIX9/yRpgLHhXjzaZiFgsR+aSMz 4z5+265VadgGv8Sw+3KzmcwEuUIEwN7bnGeLO+KrCfT9uyi11E98OGM1VobDNX2iXf4Z baOKmixnZr6y/ZO/n+q97QbqYSPVsLl5zLx/shA39SIU20CkRvNkNQiSTrls6RnwE2UT 7DyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=aqk7b4FHL2/GrfEth5EIqDxF5NbM0Q4TDN3CdqhJ05Q=; b=EWOkst23KOQjJxX1hlv0xh+3wu9AcRF0NWEc2p31cNRGAJm/I3e5yFDha5r9UlI7oj TlsSVIUCKBi6+tXbxfURLPgA0LF8Wwd8Iv5/mkXMgC70bcQ9OcssI02jsvwh//+XEkkN 8fyFgf0Uo6A3053s3/ylLNHANmhMjrlp1Zt4s+Nb3tmjS59fu5ysjBYUYdlho6Ih9Ufi 7w/0FBiLFD4/vYjCv4wxBc0CUfXOVydM2zNF/hMhmeFXoS1y2t2POMpSefAUE9Vlw0K/ 35sXUJtcaMfmvjiJ3km8J6LqvU/ERiY5LDBOIQ0eXR1l2i0qMF4QeI3iaUlUBwFkQtE9 MsDg== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) smtp.mailfrom=christian.storm@siemens.com Received: from thoth.sbs.de (thoth.sbs.de. [192.35.17.2]) by gmr-mx.google.com with ESMTPS id i6si510868eda.1.2017.11.03.05.31.38 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Nov 2017 05:31:38 -0700 (PDT) Received-SPF: pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) client-ip=192.35.17.2; Received: from mail2.siemens.de (mail2.siemens.de [139.25.208.11]) by thoth.sbs.de (8.15.2/8.15.2) with ESMTPS id vA3CVccx023702 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 3 Nov 2017 13:31:38 +0100 Received: from MD1KR9XC.ww002.siemens.net ([139.25.69.251]) by mail2.siemens.de (8.15.2/8.15.2) with ESMTP id vA3CVcFr030106; Fri, 3 Nov 2017 13:31:38 +0100 From: Christian Storm To: swupdate@googlegroups.com Cc: Christian Storm Subject: [swupdate] [PATCH 08/11] Lua: introduce image:read() method Date: Fri, 3 Nov 2017 13:30:06 +0100 Message-Id: <20171103123009.18705-8-christian.storm@siemens.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20171103123009.18705-1-christian.storm@siemens.com> References: <20171103123009.18705-1-christian.storm@siemens.com> X-Original-Sender: christian.storm@siemens.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of christian.storm@siemens.com designates 192.35.17.2 as permitted sender) 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: , For C handlers, there's core/cpio_util.c's copyfile() as a general method to extract data from the input stream and copy it to somewhere. image:read() is the analogous function exposed to Lua that reads from the input stream and calls the callback Lua function for every chunk read, passing this chunk as parameter. Signed-off-by: Christian Storm --- corelib/lua_compat.c | 39 ++++++++++++++++++++++++++ corelib/lua_interface.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ include/lua_util.h | 21 ++++++++++++++ 3 files changed, 133 insertions(+) diff --git a/corelib/lua_compat.c b/corelib/lua_compat.c index 6204e8d..317f353 100644 --- a/corelib/lua_compat.c +++ b/corelib/lua_compat.c @@ -25,6 +25,45 @@ * https://github.com/keplerproject/lua-compat-5.2 */ #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501 +char *luaL_prepbuffsize(luaL_Buffer_52 *B, size_t s) +{ + if (B->capacity - B->nelems < s) { /* needs to grow */ + char* newptr = NULL; + size_t newcap = B->capacity * 2; + if (newcap - B->nelems < s) + newcap = B->nelems + s; + if (newcap < B->capacity) /* overflow */ + luaL_error(B->L2, "buffer too large"); + newptr = lua_newuserdata(B->L2, newcap); + memcpy(newptr, B->ptr, B->nelems); + if (B->ptr != B->b.buffer) + lua_replace(B->L2, -2); /* remove old buffer */ + B->ptr = newptr; + B->capacity = newcap; + } + return B->ptr+B->nelems; +} + +void luaL_buffinit(lua_State *L, luaL_Buffer_52 *B) +{ + /* make it crash if used via pointer to a 5.1-style luaL_Buffer */ + B->b.p = NULL; + B->b.L = NULL; + B->b.lvl = 0; + /* reuse the buffer from the 5.1-style luaL_Buffer though! */ + B->ptr = B->b.buffer; + B->capacity = LUAL_BUFFERSIZE; + B->nelems = 0; + B->L2 = L; +} + +void luaL_pushresult(luaL_Buffer_52 *B) +{ + lua_pushlstring(B->L2, B->ptr, B->nelems); + if (B->ptr != B->b.buffer) + lua_replace(B->L2, -2); /* remove userdata buffer */ +} + void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup+1, "too many upvalues"); diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c index a551cde..7bb58ec 100644 --- a/corelib/lua_interface.c +++ b/corelib/lua_interface.c @@ -321,6 +321,75 @@ copyfile_exit: close(fdout); return 2; } + +static int istream_read_callback(void *out, const void *buf, unsigned int len) +{ + lua_State* L = (lua_State*)out; + if (len > LUAL_BUFFERSIZE) { + ERROR("I/O buffer size is larger than Lua's buffer size %d", LUAL_BUFFERSIZE); + return -1; + } + + luaL_checktype(L, 2, LUA_TFUNCTION); + lua_pushvalue(L, 2); + + luaL_Buffer lbuffer; + luaL_buffinit(L, &lbuffer); + char *buffer = luaL_prepbuffsize(&lbuffer, len); + memcpy(buffer, buf, len); + luaL_addsize(&lbuffer, len); + luaL_pushresult(&lbuffer); + if (lua_pcall(L, 1, 0, 0) != LUA_OK) { + ERROR("Lua error in callback: %s", lua_tostring(L, -1)); + lua_pop(L, 1); + return -1; + } + return 0; +} + +static int l_istream_read(lua_State* L) +{ + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checktype(L, 2, LUA_TFUNCTION); + + struct img_type img = {}; + uint32_t checksum = 0; + + lua_pushvalue(L, 1); + table2image(L, &img); + lua_pop(L, 1); + + int ret = copyfile(img.fdin, + L, + img.size, + (unsigned long *)&img.offset, + img.seek, + 0, /* no skip */ + img.compressed, + &checksum, + img.sha256, + img.is_encrypted, + istream_read_callback); + + lua_pop(L, 1); + update_table(L, &img); + lua_pop(L, 1); + + if (ret < 0) { + lua_pushinteger(L, -1); + lua_pushstring(L, strerror(errno)); + return 2; + } + if ((img.checksum != 0) && (checksum != img.checksum)) { + lua_pushinteger(L, -1); + lua_pushfstring(L, "Checksums WRONG! Computed 0x%d, should be 0x%d\n", + checksum, img.checksum); + return 2; + } + lua_pushinteger(L, 0); + lua_pushnil(L); + return 2; +} #endif static void update_table(lua_State* L, struct img_type *img) @@ -354,6 +423,10 @@ static void update_table(lua_State* L, struct img_type *img) lua_pushstring(L, "copy2file"); lua_pushcfunction(L, &l_copy2file); lua_settable(L, -3); + + lua_pushstring(L, "read"); + lua_pushcfunction(L, &l_istream_read); + lua_settable(L, -3); #endif lua_getfield(L, -1, "_private"); diff --git a/include/lua_util.h b/include/lua_util.h index 03ef8d9..5be8c8d 100644 --- a/include/lua_util.h +++ b/include/lua_util.h @@ -50,6 +50,27 @@ void luaL_requiref(lua_State *L, char const* modname, lua_CFunction openf, int g typedef struct luaL_Stream { FILE *f; } luaL_Stream; + +typedef struct luaL_Buffer_52 { + luaL_Buffer b; /* make incorrect code crash! */ + char *ptr; + size_t nelems; + size_t capacity; + lua_State *L2; +} luaL_Buffer_52; +#define luaL_Buffer luaL_Buffer_52 + +#define luaL_prepbuffsize luaL_prepbuffsize_52 +char *luaL_prepbuffsize(luaL_Buffer_52 *B, size_t s); + +#define luaL_buffinit luaL_buffinit_52 +void luaL_buffinit(lua_State *L, luaL_Buffer_52 *B); + +#undef luaL_addsize +#define luaL_addsize(B, s) ((B)->nelems += (s)) + +#define luaL_pushresult luaL_pushresult_52 +void luaL_pushresult(luaL_Buffer_52 *B); #endif #else