1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:47:45 +00:00

hexdump: Avoid using read_all

I like using hexdump to 'have a look' at binary files, for example
/dev/random or /dev/hda. Obviously, this usecase requires that hexdump
tries not to buffer the 'entire' device.
This commit is contained in:
Ben Wiederhake 2021-10-31 19:22:53 +01:00 committed by Andreas Kling
parent a5dda0b0c5
commit 5096f9cff5

View file

@ -4,11 +4,13 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/ByteBuffer.h> #include <AK/Array.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <ctype.h> #include <ctype.h>
static constexpr size_t LINE_LENGTH_BYTES = 16;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
@ -30,14 +32,11 @@ int main(int argc, char** argv)
file = file_or_error.value(); file = file_or_error.value();
} }
auto contents = file->read_all(); auto print_line = [](u8* buf, size_t size) {
VERIFY(size <= LINE_LENGTH_BYTES);
Vector<u8, 16> line; for (size_t i = 0; i < LINE_LENGTH_BYTES; ++i) {
if (i < size)
auto print_line = [&] { out("{:02x} ", buf[i]);
for (size_t i = 0; i < 16; ++i) {
if (i < line.size())
out("{:02x} ", line[i]);
else else
out(" "); out(" ");
@ -47,9 +46,9 @@ int main(int argc, char** argv)
out(" |"); out(" |");
for (size_t i = 0; i < line.size(); ++i) { for (size_t i = 0; i < size; ++i) {
if (isprint(line[i])) if (isprint(buf[i]))
putchar(line[i]); putchar(buf[i]);
else else
putchar('.'); putchar('.');
} }
@ -58,17 +57,26 @@ int main(int argc, char** argv)
putchar('\n'); putchar('\n');
}; };
for (size_t i = 0; i < contents.size(); ++i) { Array<u8, BUFSIZ> contents;
line.append(contents[i]); static_assert(LINE_LENGTH_BYTES * 2 <= contents.size(), "Buffer is too small?!");
size_t contents_size = 0;
if (line.size() == 16) { int nread;
print_line(); do {
line.clear(); nread = file->read(&contents[contents_size], BUFSIZ - contents_size);
contents_size += nread;
// Print as many complete lines as we can (possibly none).
size_t offset;
for (offset = 0; offset + LINE_LENGTH_BYTES - 1 < contents_size; offset += LINE_LENGTH_BYTES) {
print_line(&contents[offset], LINE_LENGTH_BYTES);
} }
} contents_size -= offset;
// Regions cannot overlap due to above static_assert.
if (!line.is_empty()) memcpy(&contents[0], &contents[offset], contents_size);
print_line(); } while (nread);
VERIFY(contents_size <= LINE_LENGTH_BYTES - 1);
if (contents_size > 0)
print_line(&contents[0], contents_size);
return 0; return 0;
} }