diff options
| author | ruki <[email protected]> | 2025-12-07 12:48:43 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-07 17:16:00 +0800 |
| commit | b18cf3e29067c0b9159880bf2028eb5555176841 (patch) | |
| tree | 2b2f3c81aeb0d73ec0bba8982789136a658ef004 /core/src | |
| parent | 58045d685b6daea07dbc641e84eaaa8527c6a189 (diff) | |
improve to copy stream data
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/utils/bin2coff.c | 18 | ||||
| -rw-r--r-- | core/src/xmake/utils/bin2macho.c | 13 | ||||
| -rw-r--r-- | core/src/xmake/utils/prefix.h | 40 |
3 files changed, 45 insertions, 26 deletions
diff --git a/core/src/xmake/utils/bin2coff.c b/core/src/xmake/utils/bin2coff.c index 58c05cd33..25c54035c 100644 --- a/core/src/xmake/utils/bin2coff.c +++ b/core/src/xmake/utils/bin2coff.c @@ -250,17 +250,8 @@ static tb_bool_t xm_utils_bin2coff_dump(tb_stream_ref_t istream, } // write section data - tb_byte_t buffer[8192]; - tb_hong_t left = filesize; - while (left > 0) { - tb_size_t to_read = (tb_size_t)tb_min(left, (tb_hong_t)sizeof(buffer)); - if (!tb_stream_bread(istream, buffer, to_read)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, buffer, to_read)) { - return tb_false; - } - left -= to_read; + if (!xm_utils_stream_copy(istream, ostream, filesize)) { + return tb_false; } // append null terminator if zeroend is true if (zeroend) { @@ -366,11 +357,8 @@ tb_int_t xm_utils_bin2coff(lua_State *lua) { // get basename (optional) tb_char_t const *basename = lua_isstring(lua, 5) ? lua_tostring(lua, 5) : tb_null; - // get platform (optional, not used but passed for consistency) - tb_char_t const *plat = lua_isstring(lua, 6) ? lua_tostring(lua, 6) : tb_null; - // get zeroend (optional, default: false) - tb_bool_t zeroend = lua_toboolean(lua, 7); + tb_bool_t zeroend = lua_toboolean(lua, 6); // do dump tb_bool_t ok = tb_false; diff --git a/core/src/xmake/utils/bin2macho.c b/core/src/xmake/utils/bin2macho.c index 07c6fc347..50b03c5e3 100644 --- a/core/src/xmake/utils/bin2macho.c +++ b/core/src/xmake/utils/bin2macho.c @@ -454,17 +454,8 @@ static tb_bool_t xm_utils_bin2macho_dump_64(tb_stream_ref_t istream, } // write section data - tb_byte_t buffer[8192]; - tb_hong_t left = filesize; - while (left > 0) { - tb_size_t to_read = (tb_size_t)tb_min(left, (tb_hong_t)sizeof(buffer)); - if (!tb_stream_bread(istream, buffer, to_read)) { - return tb_false; - } - if (!tb_stream_bwrit(ostream, buffer, to_read)) { - return tb_false; - } - left -= to_read; + if (!xm_utils_stream_copy(istream, ostream, filesize)) { + return tb_false; } // append null terminator if zeroend is true if (zeroend) { diff --git a/core/src/xmake/utils/prefix.h b/core/src/xmake/utils/prefix.h index a9f509e72..077b4027f 100644 --- a/core/src/xmake/utils/prefix.h +++ b/core/src/xmake/utils/prefix.h @@ -26,4 +26,44 @@ */ #include "../prefix.h" +/* ////////////////////////////////////////////////////////////////////////////////////// + * inline implementation + */ + +/* copy data from input stream to output stream + * + * @param istream the input stream + * @param ostream the output stream + * @param size the size to copy + * + * @return tb_true on success, tb_false on failure + */ +static __tb_inline__ tb_bool_t xm_utils_stream_copy(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_hize_t size) { + tb_assert_and_check_return_val(istream && ostream && size > 0, tb_false); + + tb_byte_t data[TB_STREAM_BLOCK_MAXN]; + tb_hize_t writ = 0; + do { + tb_size_t need = (tb_size_t)tb_min(size - writ, (tb_hize_t)TB_STREAM_BLOCK_MAXN); + tb_check_break(need); + + tb_long_t real = tb_stream_read(istream, data, need); + if (real > 0) { + if (!tb_stream_bwrit(ostream, data, (tb_size_t)real)) { + return tb_false; + } + writ += real; + } else if (!real) { + tb_long_t wait = tb_stream_wait(istream, TB_STREAM_WAIT_READ, tb_stream_timeout(istream)); + tb_check_break(wait > 0 && (wait & TB_STREAM_WAIT_READ)); + } else { + return tb_false; + } + + tb_check_break(writ < size); + } while (1); + + return tb_true; +} + #endif |
