From 2f120d9cd272ba76f53ca1d0b31aac680934b688 Mon Sep 17 00:00:00 2001 From: tiptorrent development team Date: Fri, 24 Sep 2021 10:27:35 +0200 Subject: fix chunk size split logic Add remainder bytes to the last chunk instead. --- src/handler.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/handler.c b/src/handler.c index 8534c69..1c107dd 100644 --- a/src/handler.c +++ b/src/handler.c @@ -167,6 +167,7 @@ int tip_client_state_process_payload(struct tip_client *cli) int tip_client_state_process_payload_reply(struct tip_client *cli) { uint64_t chunk_size; + uint32_t remainder; off_t chunk_offset; char buf[1024]; int fd; @@ -191,18 +192,13 @@ int tip_client_state_process_payload_reply(struct tip_client *cli) break; chunk_size = cli->size / MAX_CHUNKS; - if (cli->size % MAX_CHUNKS) { - if (cli->chunk < MAX_CHUNKS - 1) { - chunk_size++; - chunk_offset = chunk_size * cli->chunk; - } else { - chunk_offset = chunk_size * cli->chunk; - chunk_offset += MAX_CHUNKS - 1; - chunk_size--; - } - } else { - chunk_offset = chunk_size * cli->chunk; + chunk_offset = chunk_size * cli->chunk; + + if (cli->chunk == MAX_CHUNKS - 1) { + remainder = cli->size % MAX_CHUNKS; + chunk_size += remainder; } + cli->size = chunk_size; cli->offset = chunk_offset; break; -- cgit v1.2.3-18-g5258