mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-27 07:56:14 +01:00
gchecksum: fix strict-aliasing warnings in the MD5 code
https://bugzilla.gnome.org/show_bug.cgi?id=673191
This commit is contained in:
parent
0c14f2b8a7
commit
a81a062202
@ -67,7 +67,10 @@ typedef struct
|
||||
guint32 buf[4];
|
||||
guint32 bits[2];
|
||||
|
||||
guchar data[MD5_DATASIZE];
|
||||
union {
|
||||
guchar data[MD5_DATASIZE];
|
||||
guint32 data32[MD5_DATASIZE / 4];
|
||||
} u;
|
||||
|
||||
guchar digest[MD5_DIGEST_LEN];
|
||||
} Md5sum;
|
||||
@ -329,13 +332,13 @@ md5_sum_update (Md5sum *md5,
|
||||
|
||||
md5->bits[1] += length >> 29;
|
||||
|
||||
/* bytes already in Md5sum->data */
|
||||
/* bytes already in Md5sum->u.data */
|
||||
bit = (bit >> 3) & 0x3f;
|
||||
|
||||
/* handle any leading odd-sized chunks */
|
||||
if (bit)
|
||||
{
|
||||
guchar *p = (guchar *) md5->data + bit;
|
||||
guchar *p = md5->u.data + bit;
|
||||
|
||||
bit = MD5_DATASIZE - bit;
|
||||
if (length < bit)
|
||||
@ -346,8 +349,8 @@ md5_sum_update (Md5sum *md5,
|
||||
|
||||
memcpy (p, data, bit);
|
||||
|
||||
md5_byte_reverse (md5->data, 16);
|
||||
md5_transform (md5->buf, (guint32 *) md5->data);
|
||||
md5_byte_reverse (md5->u.data, 16);
|
||||
md5_transform (md5->buf, md5->u.data32);
|
||||
|
||||
data += bit;
|
||||
length -= bit;
|
||||
@ -356,17 +359,17 @@ md5_sum_update (Md5sum *md5,
|
||||
/* process data in 64-byte chunks */
|
||||
while (length >= MD5_DATASIZE)
|
||||
{
|
||||
memcpy (md5->data, data, MD5_DATASIZE);
|
||||
memcpy (md5->u.data, data, MD5_DATASIZE);
|
||||
|
||||
md5_byte_reverse (md5->data, 16);
|
||||
md5_transform (md5->buf, (guint32 *) md5->data);
|
||||
md5_byte_reverse (md5->u.data, 16);
|
||||
md5_transform (md5->buf, md5->u.data32);
|
||||
|
||||
data += MD5_DATASIZE;
|
||||
length -= MD5_DATASIZE;
|
||||
}
|
||||
|
||||
/* handle any remaining bytes of data */
|
||||
memcpy (md5->data, data, length);
|
||||
memcpy (md5->u.data, data, length);
|
||||
}
|
||||
|
||||
/* closes a checksum */
|
||||
@ -382,7 +385,7 @@ md5_sum_close (Md5sum *md5)
|
||||
/* Set the first char of padding to 0x80.
|
||||
* This is safe since there is always at least one byte free
|
||||
*/
|
||||
p = md5->data + count;
|
||||
p = md5->u.data + count;
|
||||
*p++ = 0x80;
|
||||
|
||||
/* Bytes of padding needed to make 64 bytes */
|
||||
@ -394,11 +397,11 @@ md5_sum_close (Md5sum *md5)
|
||||
/* Two lots of padding: Pad the first block to 64 bytes */
|
||||
memset (p, 0, count);
|
||||
|
||||
md5_byte_reverse (md5->data, 16);
|
||||
md5_transform (md5->buf, (guint32 *) md5->data);
|
||||
md5_byte_reverse (md5->u.data, 16);
|
||||
md5_transform (md5->buf, md5->u.data32);
|
||||
|
||||
/* Now fill the next block with 56 bytes */
|
||||
memset (md5->data, 0, MD5_DATASIZE - 8);
|
||||
memset (md5->u.data, 0, MD5_DATASIZE - 8);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -406,20 +409,20 @@ md5_sum_close (Md5sum *md5)
|
||||
memset (p, 0, count - 8);
|
||||
}
|
||||
|
||||
md5_byte_reverse (md5->data, 14);
|
||||
md5_byte_reverse (md5->u.data, 14);
|
||||
|
||||
/* Append length in bits and transform */
|
||||
((guint32 *) md5->data)[14] = md5->bits[0];
|
||||
((guint32 *) md5->data)[15] = md5->bits[1];
|
||||
md5->u.data32[14] = md5->bits[0];
|
||||
md5->u.data32[15] = md5->bits[1];
|
||||
|
||||
md5_transform (md5->buf, (guint32 *) md5->data);
|
||||
md5_transform (md5->buf, md5->u.data32);
|
||||
md5_byte_reverse ((guchar *) md5->buf, 4);
|
||||
|
||||
memcpy (md5->digest, md5->buf, 16);
|
||||
|
||||
/* Reset buffers in case they contain sensitive data */
|
||||
memset (md5->buf, 0, sizeof (md5->buf));
|
||||
memset (md5->data, 0, sizeof (md5->data));
|
||||
memset (md5->u.data, 0, sizeof (md5->u.data));
|
||||
}
|
||||
|
||||
static gchar *
|
||||
|
Loading…
Reference in New Issue
Block a user