From patchwork Wed Apr 17 17:56:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick McDermott X-Patchwork-Id: 1087100 X-Patchwork-Delegate: ynezz@true.cz 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=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=libiquity.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="kVO11AaK"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44kqhc0KzNz9s71 for ; Thu, 18 Apr 2019 03:57:52 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Subject:Message-Id: Date:To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=g+aSkgf52CAQy1ANOYYTJOSB/ILDNg7OOAmzN/e06IQ=; b=kVO11AaKrNJQ15 Y1Y8F1esy14azjVY2nZoZ3qR4xBxyFD5uthaYCj5a1u9GKotCv+p3XNJrnoL41ik5PfpcJDIWUSkg joJ7wgLdzepuy2XsXZWtzMZewYWTkHicY0i964X3LYGxw+hkrN+UCtunlpfnVZwrCcqNY4b/QwfI3 OpwEkfzDX1LykB/HURhq1YB8gkLKjzXYuxnQ4AU5f1/Q0BYjwqaKNUSR+iGyMCmZmfKWaW6fGguUv WYHjzAu0qqPmP1T7WIsnrTk3x7oB7JJ/mhKP3eIG8mXgqWO/UwUtBtHx/g2OsQGbci8TioQLsBYNT UtxyzMZIuTfQ3aBmNl4A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hGooT-0000Pl-MB; Wed, 17 Apr 2019 17:57:37 +0000 Received: from lib1.libiquity.com ([24.229.5.118]) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hGooR-0000P6-FW for openwrt-devel@lists.openwrt.org; Wed, 17 Apr 2019 17:57:36 +0000 Received: from localhost.localdomain (unknown [24.229.5.114]) by lib1.libiquity.com (Postfix) with ESMTP id EF6C6C12D7; Wed, 17 Apr 2019 13:57:34 -0400 (EDT) From: Patrick McDermott To: Jo-Philipp Wich Date: Wed, 17 Apr 2019 13:56:11 -0400 Message-Id: <20190417175611.4351-1-patrick.mcdermott@libiquity.com> X-Mailer: git-send-email 2.11.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190417_105735_607313_EF427F56 X-CRM114-Status: UNSURE ( 7.89 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -0.0 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record Subject: [OpenWrt-Devel] [PATCH][opkg-lede] libopkg: Detect gzipped pkg lists by magic number X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: openwrt-devel@lists.openwrt.org, Patrick McDermott MIME-Version: 1.0 Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Signed-off-by: Patrick McDermott --- libbb/gzip.h | 2 ++ libopkg/pkg_hash.c | 27 ++++++++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/libbb/gzip.h b/libbb/gzip.h index 3a61a1d..28e3a50 100644 --- a/libbb/gzip.h +++ b/libbb/gzip.h @@ -22,6 +22,8 @@ #include #include +#define GZIP_MAGIC "\037\213" /* gzip magic number, 1F 8B */ + struct gzip_handle { FILE *file; struct gzip_handle *gzip; diff --git a/libopkg/pkg_hash.c b/libopkg/pkg_hash.c index 611f3b9..b536db7 100644 --- a/libopkg/pkg_hash.c +++ b/libopkg/pkg_hash.c @@ -99,21 +99,34 @@ pkg_hash_add_from_file(const char *file_name, { pkg_t *pkg; FILE *fp; + char magic[2]; + int is_gzip = 0; char *buf; const size_t len = 4096; int ret = 0; struct gzip_handle zh; - if (src && src->gzip) { - fp = gzip_fdopen(&zh, file_name); - } else { - fp = fopen(file_name, "r"); - } - + fp = fopen(file_name, "r"); if (fp == NULL) { opkg_perror(ERROR, "Failed to open %s", file_name); return -1; } + if (fread(magic, 1, 2, fp) != 2) { + opkg_perror(ERROR, "Failed to read %s", file_name); + fclose(fp); + return -1; + } + (void)fseek(fp, 0L, SEEK_SET); + + if (src && src->gzip && memcmp(magic, GZIP_MAGIC, 2) == 0) { + fclose(fp); + fp = gzip_fdopen(&zh, file_name); + if (fp == NULL) { + opkg_perror(ERROR, "Failed to open %s", file_name); + return -1; + } + is_gzip = 1; + } buf = xmalloc(len); @@ -165,7 +178,7 @@ pkg_hash_add_from_file(const char *file_name, free(buf); fclose(fp); - if (src && src->gzip) + if (is_gzip) gzip_close(&zh); return ret;