aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-02-10 11:32:50 +0100
committerMattias Andrée <maandree@kth.se>2019-02-10 11:32:50 +0100
commit68854e1163a5774465490ab7ac0f42604486980f (patch)
tree0e1fbbbeb60611a74170ad680cedc7f5c25f3c53
parentFix libsha2_digest + minor improvements (diff)
downloadlibsha2-68854e1163a5774465490ab7ac0f42604486980f.tar.gz
libsha2-68854e1163a5774465490ab7ac0f42604486980f.tar.bz2
libsha2-68854e1163a5774465490ab7ac0f42604486980f.tar.xz
Avoid copying in libsh2_update
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--update.c25
1 files changed, 16 insertions, 9 deletions
diff --git a/update.c b/update.c
index b0eff29..94e46f3 100644
--- a/update.c
+++ b/update.c
@@ -10,22 +10,29 @@
* @param msglen The length of the message
*/
void
-libsha2_update(struct libsha2_state *restrict state, const char *restrict message, size_t msglen) /* TODO avoid coping */
+libsha2_update(struct libsha2_state *restrict state, const char *restrict message, size_t msglen)
{
- size_t n, off, mlen;
+ size_t n, off;
+ off = (state->message_size / 8) % state->chunk_size;
+ state->message_size += msglen;
msglen /= 8;
- mlen = state->message_size / 8;
- while (msglen) {
- off = mlen % state->chunk_size;
- n = state->chunk_size - off;
- n = n < msglen ? n : msglen;
+ if (off) {
+ n = msglen < state->chunk_size - off ? msglen : state->chunk_size - off;
memcpy(state->chunk + off, message, n);
if (off + n == state->chunk_size)
libsha2_process(state, state->chunk);
- message += n, mlen += n, msglen -= n;
+ message += n;
+ msglen -= n;
}
- state->message_size = mlen * 8;
+ while (msglen >= state->chunk_size) {
+ libsha2_process(state, (const unsigned char *)message);
+ message += state->chunk_size;
+ msglen -= state->chunk_size;
+ }
+
+ if (msglen)
+ memcpy(state->chunk, message, msglen);
}