summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-10-25 22:46:09 +0800
committerruki <[email protected]>2024-10-25 22:46:09 +0800
commit560195a98b59bf0d4d4dca362a7a41f943367183 (patch)
tree59b86cbc834994d5a46e1158b57bcde3f1a07b80
parent5e3238136f8fd9387c92934b05ea38aeb7c79dd5 (diff)
improve bin2c loop
-rw-r--r--core/src/xmake/utils/bin2c.c35
1 files changed, 16 insertions, 19 deletions
diff --git a/core/src/xmake/utils/bin2c.c b/core/src/xmake/utils/bin2c.c
index a00bb25a3..01aaa1c22 100644
--- a/core/src/xmake/utils/bin2c.c
+++ b/core/src/xmake/utils/bin2c.c
@@ -37,6 +37,7 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, tb_stream_ref_t os
{
tb_bool_t first = tb_true;
tb_hong_t i = 0;
+ tb_hong_t left = 0;
tb_char_t line[8192];
tb_byte_t data[512];
tb_size_t linesize = 0;
@@ -45,7 +46,8 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, tb_stream_ref_t os
while (!tb_stream_beof(istream))
{
linesize = 0;
- need = (tb_size_t)tb_min(tb_stream_left(istream), linewidth);
+ left = tb_stream_left(istream);
+ need = (tb_size_t)tb_min(left, linewidth);
if (need)
{
if (!tb_stream_bread(istream, data, need))
@@ -57,21 +59,20 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, tb_stream_ref_t os
data[need++] = '\0';
}
- for (i = 0; i < need; i++)
- {
- tb_assert_and_check_break(linesize < sizeof(line));
- if (first)
- {
- first = tb_false;
- line[linesize++] = ' ';
- }
- else
- {
- line[linesize++] = ',';
- }
+ tb_assert_and_check_break(linesize + 6 * need < sizeof(line));
- tb_assert_and_check_break(linesize + 5 < sizeof(line));
+ i = 0;
+ if (first)
+ {
+ first = tb_false;
+ line[linesize++] = ' ';
+ }
+ else line[linesize++] = ',';
+ linesize += tb_snprintf(line + linesize, sizeof(line) - linesize, " 0x%02X", data[i]);
+ for (i = 1; i < need; i++)
+ {
+ line[linesize++] = ',';
linesize += tb_snprintf(line + linesize, sizeof(line) - linesize, " 0x%02X", data[i]);
}
tb_assert_and_check_break(i == need && linesize && linesize < sizeof(line));
@@ -111,11 +112,7 @@ tb_int_t xm_utils_bin2c(lua_State* lua)
// no zero end?
tb_bool_t nozeroend = (tb_bool_t)lua_toboolean(lua, 4);
- tb_trace_i("binaryfile: %s", binaryfile);
- tb_trace_i("outputfile: %s", outputfile);
- tb_trace_i("linewidth: %d", linewidth);
- tb_trace_i("nozeroend: %d", nozeroend);
-
+ // do dump
tb_bool_t ok = tb_false;
tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO);
tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC);