All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
To: linux-modules <linux-modules@vger.kernel.org>
Cc: Lucas De Marchi <lucas.de.marchi@gmail.com>,
	Mark van Dijk <linux-modules@z.dvxs.net>
Subject: [PATCH 3/4] libkmod: modinfo: implement line splitting in hex_to_str
Date: Tue, 11 Apr 2017 15:15:02 +0300	[thread overview]
Message-ID: <20170411121503.26181-4-yauheni.kaliuta@redhat.com> (raw)
In-Reply-To: <20170411121503.26181-1-yauheni.kaliuta@redhat.com>

The key output is usually short, but for signature it is more
readable to output it in several lines.

Implement line splitting. Set line limit hardcoded to 20 hex
numbers (not characters).

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
---
 libkmod/libkmod-module.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
index 22c8f4c852a2..9e155f080277 100644
--- a/libkmod/libkmod-module.c
+++ b/libkmod/libkmod-module.c
@@ -2197,15 +2197,25 @@ static char *kmod_module_hex_to_str(const char *hex, size_t len)
 {
 	char *str;
 	int i;
+	int j;
+	const size_t line_limit = 20;
+	size_t str_len;
 
-	str = malloc(len * 3);
+	str_len = len * 3; /* XX: or XX\0 */
+	str_len += ((str_len + line_limit - 1) / line_limit - 1) * 3; /* \n\t\t */
+
+	str = malloc(str_len);
 	if (str == NULL)
 		return NULL;
 
-	for (i = 0; i < (int)len; i++) {
-		sprintf(str + i * 3, "%02X", (unsigned char)hex[i]);
-		if (i < (int)len - 1)
-			str[i * 3 + 2] = ':';
+	for (i = 0, j = 0; i < (int)len; i++) {
+		j += sprintf(str + j, "%02X", (unsigned char)hex[i]);
+		if (i < (int)len - 1) {
+			str[j++] = ':';
+
+			if ((i + 1) % line_limit == 0)
+				j += sprintf(str + j, "\n\t\t");
+		}
 	}
 	return str;
 }
-- 
2.9.2.368.g08bb350

  parent reply	other threads:[~2017-04-11 12:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-11 12:14 [PATCH 0/4] modinfo: fix sig_id key and add signature output Yauheni Kaliuta
2017-04-11 12:15 ` [PATCH 1/4] libkmod: modinfo: fix sig_id output Yauheni Kaliuta
2017-04-11 12:15 ` [PATCH 2/4] libkmod: modinfo: use own function for sig_key hex output Yauheni Kaliuta
2017-04-11 12:15 ` Yauheni Kaliuta [this message]
2017-04-11 12:15 ` [PATCH 4/4] libkmod: modinfo: implement signature output Yauheni Kaliuta
2017-04-11 16:10 ` [PATCH 0/4] modinfo: fix sig_id key and add " Lucas De Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170411121503.26181-4-yauheni.kaliuta@redhat.com \
    --to=yauheni.kaliuta@redhat.com \
    --cc=linux-modules@vger.kernel.org \
    --cc=linux-modules@z.dvxs.net \
    --cc=lucas.de.marchi@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.