From patchwork Mon Jul 26 15:17:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Doucha X-Patchwork-Id: 1510108 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=YZwt2wCm; dkim=fail reason="signature verification failed" header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=vrIGiQCP; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GYNpy5N6Rz9sRR for ; Tue, 27 Jul 2021 01:18:18 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 943183C9247 for ; Mon, 26 Jul 2021 17:18:16 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [217.194.8.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 980CA3C6615 for ; Mon, 26 Jul 2021 17:18:05 +0200 (CEST) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by in-4.smtp.seeweb.it (Postfix) with ESMTPS id D953E1000A16 for ; Mon, 26 Jul 2021 17:18:04 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 1159921F60 for ; Mon, 26 Jul 2021 15:18:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1627312684; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=CXVtyfTSPNeo9/YSMMiicAbkLUFVKKRTnaXZseSuN+o=; b=YZwt2wCmENdI+HQ3szZHvPg0nXc4tHOhpMPWLlgeHeOHsbyC/scXm3o0grmTcRkTJQko2O QonyrI2E5CJqHwQm45TstvSRKTS4zFN3RhbMPBMS50lDuNM0BnygoF2gcz6lkaiuZe2Svm ttM0coVXoIVajaiwjFutYs2pT7bL1tI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1627312684; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=CXVtyfTSPNeo9/YSMMiicAbkLUFVKKRTnaXZseSuN+o=; b=vrIGiQCPGI78Bo1pLW/sO53AFp1aYGJPc8aNLcJXrcb4wMxdHYxZ5AaJM16p3Juj4B8AjM HCkQRUQp6Us93LDQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id F335F13AA2 for ; Mon, 26 Jul 2021 15:18:03 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id oCp5OivS/mAZYgAAMHmgww (envelope-from ) for ; Mon, 26 Jul 2021 15:18:03 +0000 From: Martin Doucha To: ltp@lists.linux.it Date: Mon, 26 Jul 2021 17:17:35 +0200 Message-Id: <20210726151736.14299-1-mdoucha@suse.cz> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH 1/2] Add tst_hexdump utility X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" tst_hexdump implements conversion between binary and hexadecimal values in both directions for shell tests. Signed-off-by: Martin Doucha Reviewed-by: Petr Vorel --- testcases/lib/Makefile | 2 +- testcases/lib/tst_hexdump.c | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 testcases/lib/tst_hexdump.c diff --git a/testcases/lib/Makefile b/testcases/lib/Makefile index 98d9e4613..38813e640 100644 --- a/testcases/lib/Makefile +++ b/testcases/lib/Makefile @@ -11,6 +11,6 @@ INSTALL_TARGETS := *.sh MAKE_TARGETS := tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\ tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\ tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port\ - tst_get_median + tst_get_median tst_hexdump include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/lib/tst_hexdump.c b/testcases/lib/tst_hexdump.c new file mode 100644 index 000000000..f83b8bfbf --- /dev/null +++ b/testcases/lib/tst_hexdump.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2021 SUSE LLC + * + * Convert bytes from standard input to hexadecimal representation. + * + * Parameters: + * -d Convert hexadecimal values from standard input to binary representation + * instead. + */ + +#include +#include + +int decode_hex(void) +{ + int ret; + unsigned int val; + + while ((ret = scanf("%2x", &val)) == 1) + putchar(val); + + return ret != EOF || ferror(stdin); +} + +int encode_hex(void) +{ + int val; + + for (val = getchar(); val >= 0 && val <= 0xff; val = getchar()) + printf("%02x", val); + + return val != EOF || ferror(stdin); +} + +int main(int argc, char **argv) +{ + int ret, decode = 0; + + while ((ret = getopt(argc, argv, "d"))) { + if (ret < 0) + break; + + switch (ret) { + case 'd': + decode = 1; + break; + } + } + + if (decode) + return decode_hex(); + else + return encode_hex(); +}