Skip to content

Conversation

@ikegami-t
Copy link
Contributor

This is for the id-ns command empty vendor-specific fields.
Note: This will change for other commands d() outputs.

@igaw
Copy link
Collaborator

igaw commented Jan 9, 2026

why not going the full hexdump way?

static bool line_equal(const unsigned char *a,
		       const unsigned char *b,
		       size_t len)
{
	return memcmp(a, b, len) == 0;
}

void stdout_d(unsigned char *buf, int len, int width, int group)
{
	size_t offset, i;
	unsigned char prev[32];
	char ascii[32 + 1];
	bool have_prev = false;
	bool omitting = false;

	assert(width <= sizeof(prev));
	assert(width < sizeof(ascii));
	assert(group > 0);

	/* Header */
	printf("     ");
	for (i = 0; i < width; i++)
		printf("%3zx", i);
	printf("\n");

	for (offset = 0; offset < len; offset += width) {
		size_t line_len = width;

		if (offset + line_len > len)
			line_len = len - offset;

		/* Collapse identical lines (hexdump behavior) */
		if (have_prev &&
		    line_len == width &&
		    line_equal(buf + offset, prev, width)) {
			if (!omitting) {
				printf("*\n");
				omitting = true;
			}
			continue;
		}

		omitting = false;

		/* Save current line for next comparison */
		if (line_len == width) {
			memcpy(prev, buf + offset, width);
			have_prev = true;
		} else {
			have_prev = false;
		}

		printf("%04zx:", offset);
		memset(ascii, 0, sizeof(ascii));

		for (i = 0; i < line_len; i++) {
			if (i % group)
				printf("%02x", buf[offset + i]);
			else
				printf(" %02x", buf[offset + i]);

			ascii[i] = (buf[offset + i] >= '!' &&
				    buf[offset + i] <= '~') ?
				   buf[offset + i] : '.';
		}

		/* Pad short final line */
		if (line_len < width) {
			size_t missing = width - line_len;
			size_t pad = missing * 2 +
				     missing / group +
				     (missing % group ? 1 : 0);
			printf(" %*s", (int)pad, "");
		}

		printf(" \"%.*s\"\n", (int)line_len, ascii);
	}

	/* Final offset line (hexdump prints this always) */
	printf("%04zx\n", offset);
}

this gives

vs[]:
       0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "................"
*
00c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 "................"
00d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "................"
*
03a0: 01 00 3c 0f 00 00 00 00 00 00 00 00 00 00 00 00 "..<............."
03b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "................"
*
0400

This is for the id-ns command empty vendor-specific fields.
  Note: This will change for other commands d() outputs.

Signed-off-by: Tokunori Ikegami <[email protected]>
@ikegami-t
Copy link
Contributor Author

ikegami-t commented Jan 9, 2026

Just realized as the hexdump not print equal line but not only for the all zero then fixed the patch as mentioned.

(Added)
Changed to dump all equal lines if -vv debug log level enabled by the additional patch.

This is to remain all dump functionality feature.

Signed-off-by: Tokunori Ikegami <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants