aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-02-09 21:30:06 +0100
committerMattias Andrée <maandree@kth.se>2019-02-09 21:30:06 +0100
commitae42e15f211feb8625450e5dcc899ae29d885ba7 (patch)
treebddb7469da7d1f32502df31e0d7c0d57aee66fde
parentImprove makefile: mv LIBEXT and LIBFLAGS to config.mk (diff)
downloadlibsha2-ae42e15f211feb8625450e5dcc899ae29d885ba7.tar.gz
libsha2-ae42e15f211feb8625450e5dcc899ae29d885ba7.tar.bz2
libsha2-ae42e15f211feb8625450e5dcc899ae29d885ba7.tar.xz
Fix warnings
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--algorithm_output_size.c3
-rw-r--r--digest.c25
-rw-r--r--init.c9
-rw-r--r--marshal.c9
-rw-r--r--unmarshal.c8
-rw-r--r--update.c10
6 files changed, 22 insertions, 42 deletions
diff --git a/algorithm_output_size.c b/algorithm_output_size.c
index 9089aa4..2824b55 100644
--- a/algorithm_output_size.c
+++ b/algorithm_output_size.c
@@ -19,6 +19,7 @@ libsha2_algorithm_output_size(enum libsha2_algorithm algorithm)
case LIBSHA2_512_224: return 28;
case LIBSHA2_512_256: return 32;
default:
- return errno = EINVAL, 0;
+ errno = EINVAL;
+ return 0;
}
}
diff --git a/digest.c b/digest.c
index 8658388..bcd318f 100644
--- a/digest.c
+++ b/digest.c
@@ -16,10 +16,10 @@ libsha2_digest(struct libsha2_state *restrict state, const char *restrict messag
char *appendix;
size_t i, j, k, n;
- if (msglen & ~7) {
- libsha2_update(state, message, msglen & ~7);
- message += msglen & ~7;
- msglen &= 7;
+ if (msglen & ~(size_t)7) {
+ libsha2_update(state, message, msglen & ~(size_t)7);
+ message += msglen & ~(size_t)7;
+ msglen &= (size_t)7;
}
k = 8 * state->chunk_size;
@@ -31,36 +31,31 @@ libsha2_digest(struct libsha2_state *restrict state, const char *restrict messag
if (msglen) {
j = 7 - msglen;
*appendix = *message;
- *appendix |= 1 << j;
- *appendix &= ~((1 << j) - 1);
+ *appendix |= (char)(1 << j);
+ *appendix &= (char)~((1 << j) - 1);
} else {
- *appendix = (unsigned char)128;
+ *appendix = (char)128;
}
k = state->message_size + msglen;
i = state->chunk_size / 8;
appendix += n + i - 1;
for (i = i < sizeof(size_t) ? i : sizeof(size_t); i--;)
- *(appendix - i) = (unsigned char)((k >> (i * 8)) & 255);
+ *(appendix - i) = (char)((k >> (i * 8)) & 255);
n += state->chunk_size;
libsha2_update(state, state->appendix, n);
n = libsha2_algorithm_output_size(state->algorithm);
- switch (state->algorithm) {
- case LIBSHA2_224:
- case LIBSHA2_256:
+ if (state->algorithm <= LIBSHA2_256) {
for (i = 0; i < 8; i++)
for (j = 0; j < (state->chunk_size / 16); j++)
if (k = (i + 1) * (state->chunk_size / 16) - j - 1, k < n)
output[k] = (char)((state->h.b32[i] >> (8 * j)) & 255);
- break;
-
- default:
+ } else {
for (i = 0; i < 8; i++)
for (j = 0; j < (state->chunk_size / 16); j++)
if (k = (i + 1) * (state->chunk_size / 16) - j - 1, k < n)
output[k] = (char)((state->h.b64[i] >> (8 * j)) & 255);
- break;
}
}
diff --git a/init.c b/init.c
index a7ffa73..34eadb7 100644
--- a/init.c
+++ b/init.c
@@ -107,18 +107,13 @@ libsha2_init(struct libsha2_state *restrict state, enum libsha2_algorithm algori
}
/* Set round constants, and chunk size. */
- switch (algorithm) {
- case LIBSHA2_224:
- case LIBSHA2_256:
+ if (algorithm <= LIBSHA2_256) {
for (i = 0; i < 64; i++)
state->k.b32[i] = (uint32_t)(ROUND_CONSTANTS[i] >> 32);
state->chunk_size = 64;
- break;
-
- default:
+ } else {
memcpy(state->k.b64, ROUND_CONSTANTS, sizeof(ROUND_CONSTANTS));
state->chunk_size = 128;
- break;
}
return 0;
diff --git a/marshal.c b/marshal.c
index 21f0f79..8265c38 100644
--- a/marshal.c
+++ b/marshal.c
@@ -24,9 +24,7 @@ libsha2_marshal(const struct libsha2_state *restrict state, char *restrict buf)
*(size_t *)&buf[off] = state->message_size;
off += sizeof(size_t);
- switch (state->algorithm) {
- case LIBSHA2_224:
- case LIBSHA2_256:
+ if (state->algorithm <= LIBSHA2_256) {
if (buf)
memcpy(&buf[off], state->k.b32, sizeof(state->k.b32));
off += sizeof(state->k.b32);
@@ -36,9 +34,7 @@ libsha2_marshal(const struct libsha2_state *restrict state, char *restrict buf)
if (buf)
memcpy(&buf[off], state->h.b32, sizeof(state->h.b32));
off += sizeof(state->h.b32);
- break;
-
- default:
+ } else {
if (buf)
memcpy(&buf[off], state->k.b64, sizeof(state->k.b64));
off += sizeof(state->k.b64);
@@ -48,7 +44,6 @@ libsha2_marshal(const struct libsha2_state *restrict state, char *restrict buf)
if (buf)
memcpy(&buf[off], state->h.b64, sizeof(state->h.b64));
off += sizeof(state->h.b64);
- break;
}
if (buf)
diff --git a/unmarshal.c b/unmarshal.c
index 2a47b92..13a97ff 100644
--- a/unmarshal.c
+++ b/unmarshal.c
@@ -20,15 +20,15 @@ libsha2_unmarshal(struct libsha2_state *restrict state, const char *restrict buf
return 0;
}
- if (*(int *)buf) { /* version */
+ if (*(const int *)buf) { /* version */
errno = EINVAL;
return 0;
}
off += sizeof(int);
- state->algorithm = *(enum libsha2_algorithm *)&buf[off];
+ state->algorithm = *(const enum libsha2_algorithm *)&buf[off];
off += sizeof(enum libsha2_algorithm);
- state->message_size = *(size_t *)&buf[off];
+ state->message_size = *(const size_t *)&buf[off];
off += sizeof(size_t);
switch (state->algorithm) {
@@ -73,7 +73,7 @@ libsha2_unmarshal(struct libsha2_state *restrict state, const char *restrict buf
}
memcpy(state->chunk, &buf[off], sizeof(state->chunk));
off += sizeof(state->chunk);
- state->chunk_size = *(size_t *)&buf[off];
+ state->chunk_size = *(const size_t *)&buf[off];
off += sizeof(size_t);
return off;
diff --git a/update.c b/update.c
index 26eceb8..3263b62 100644
--- a/update.c
+++ b/update.c
@@ -122,16 +122,10 @@ libsha2_update(struct libsha2_state *restrict state, const char *restrict messag
n = n < msglen ? n : msglen;
memcpy(state->chunk + off, message, n);
if (off + n == state->chunk_size) {
- switch (state->algorithm) {
- case LIBSHA2_224:
- case LIBSHA2_256:
+ if (state->algorithm <= LIBSHA2_256)
process256(state);
- break;
-
- default:
+ else
process512(state);
- break;
- }
}
message += n, mlen += n, msglen -= n;
}