Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Multibyte wb support #23430

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions libr/core/cmd_write.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2155,22 +2155,76 @@ static int cmd_wa(void *data, const char *input) {

static int cmd_wb(void *data, const char *input) {
RCore *core = (RCore *)data;
ut8 b = core->block[0];
char *ui = r_str_newf ("%sb", r_str_trim_head_ro (input));
int uil = strlen (ui) - 1;
int n = r_num_get (NULL, ui);
free (ui);
if (uil > 8) {
R_LOG_ERROR ("wb only operates on bytes");
} else if (uil > 0) {
// Shift left and right to zero uil most significant bits
b <<= uil;
b >>= uil;
// Overwrite uil most significant bits and keep the rest
b |= (n << (8 - uil));
r_io_write_at (core->io, core->offset, &b, 1);
} else {
int uil = strlen (input);
char c;
int i;

// Check that user provided some input
if (uil == 0) {
r_core_cmd_help_match (core, help_msg_w, "wb");
return 0;
}

// Check that user input only contains binary data
for (i = 0; i < uil; i++) {
c = input[i];
// Ignore whitespaces
if (isspace(c)) {
continue;
}
// Check that user input only contains ones and zeros
if (c != '0' && c != '1') {
R_LOG_ERROR ("wb operates only on binary data");
return 0;
}
}

// Iterate user input bitwise and write output every 8 bits
int bits_read = 0;
int block_offset = 0;
ut8 byte = 0;
for (i = 0; i < uil; i++) {
// Read a bit
c = input[i];

// Ignore whitespaces
if (isspace(c)) {
continue;
}

if (c == '1') {
// Bits are read and bytes constructed from most to
// least significant.
byte |= (1 << (7 - bits_read));
}
bits_read++;

// Write a byte if we've read 8 bits
if (bits_read % 8 == 0) {
r_io_write_at (
core->io,
// TODO: Do we need some bound check for
// `core->offset + block_offset`? Other
// functions don't seem to implement any.
Comment on lines +2206 to +2208
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO

core->offset + block_offset,
&byte,
1
);
block_offset++;
bits_read = 0;
byte = 0;
}
}

// Write any possible remaining ui bits
if (bits_read != 0) {
ut8 b = core->block[block_offset];
// Shift left and right to zero bits_read most significant bits
b <<= bits_read;
b >>= bits_read;
// Overwrite bits_read most significant bits and keep the rest
b |= byte;
r_io_write_at (core->io, core->offset + block_offset, &b, 1);
}

return 0;
Expand Down
61 changes: 61 additions & 0 deletions test/db/cmd/cmd_print_bitformat
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,64 @@ EXPECT=<<EOF
01111111
EOF
RUN

NAME=pbwb two bytes
FILE=-
CMDS=<<EOF
pb 16
wb 1111111111111111
pb 16
wb 0 @ 1
pb 16
EOF
EXPECT=<<EOF
0000000000000000
1111111111111111
1111111101111111
EOF
RUN

NAME=pbwb 3.5 bytes
FILE=-
CMDS=<<EOF
pb 28
wb 1111111111111111111111111111
pb 28
wb 1010101010101010101010101010
pb 28
wb 0 @ 3
pb 28
EOF
EXPECT=<<EOF
0000000000000000000000000000
1111111111111111111111111111
1010101010101010101010101010
1010101010101010101010100010
EOF
RUN

NAME=pbwb space
FILE=-
CMDS=<<EOF
pb 16
wb 11111111 11111111
pb 16
wb 0 @ 1
pb 16
EOF
EXPECT=<<EOF
0000000000000000
1111111111111111
1111111101111111
EOF
RUN

NAME=pbwb invalid chars
FILE=-
CMDS=<<EOF
wb 0xff
EOF
EXPECT_ERR=<<EOF
ERROR: wb operates only on binary data
EOF
RUN
Loading