changeset 22399:0c0a372ccdc1

global: use new byte ordering API
author Josef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
date Thu, 08 Jun 2017 14:49:56 +0300
parents 1b4e7b89b161
children fb6142ca50e3
files src/doveadm/doveadm-dump-mailboxlog.c src/doveadm/dsync/dsync-mailbox-state.c src/lib-compression/istream-lz4.c src/lib-compression/istream-zlib.c src/lib-dcrypt/istream-decrypt.c src/lib-index/mail-index-util.c src/lib-index/mailbox-log.c src/lib-ntlm/ntlm-des.c src/lib/test-guid.c
diffstat 9 files changed, 33 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/src/doveadm/doveadm-dump-mailboxlog.c	Thu Jun 08 13:20:18 2017 +0300
+++ b/src/doveadm/doveadm-dump-mailboxlog.c	Thu Jun 08 14:49:56 2017 +0300
@@ -52,10 +52,7 @@
 	printf(" %s", binary_to_hex(rec.mailbox_guid,
 				    sizeof(rec.mailbox_guid)));
 
-	timestamp = ((uint32_t)rec.timestamp[0] << 24) |
-		((uint32_t)rec.timestamp[1] << 16) |
-		((uint32_t)rec.timestamp[2] << 8) |
-		(uint32_t)rec.timestamp[3];
+	timestamp = be32_to_cpu_unaligned(rec.timestamp);
 	printf(" (%s)\n", unixdate2str(timestamp));
 	return 1;
 }
--- a/src/doveadm/dsync/dsync-mailbox-state.c	Thu Jun 08 13:20:18 2017 +0300
+++ b/src/doveadm/dsync/dsync-mailbox-state.c	Thu Jun 08 14:49:56 2017 +0300
@@ -15,16 +15,11 @@
 
 static void put_uint32(buffer_t *output, uint32_t num)
 {
-	buffer_append_c(output, num & 0xff);
-	buffer_append_c(output, (num >> 8) & 0xff);
-	buffer_append_c(output, (num >> 16) & 0xff);
-	buffer_append_c(output, (num >> 24) & 0xff);
-}
+	uint8_t tmp[sizeof(uint32_t)];
 
-static uint32_t get_uint32(const unsigned char *data)
-{
-	return data[0] | (data[1] << 8) | (data[2] << 16) |
-		((unsigned int)data[3] << 24);
+	cpu32_to_le_unaligned(num, tmp);
+
+	buffer_append(output, tmp, sizeof(tmp));
 }
 
 void dsync_mailbox_states_export(const HASH_TABLE_TYPE(dsync_mailbox_state) states,
@@ -72,7 +67,7 @@
 	/* v0 had no version header and no last_messages_count */
 
 	if ((buf->used-4) % V0_MAILBOX_SIZE != 0 ||
-	    get_uint32(data + buf->used-4) != crc32_data(data, buf->used-4))
+	    le32_to_cpu_unaligned(data + buf->used-4) != crc32_data(data, buf->used-4))
 		return -1;
 	/* looks like valid v0 format, silently treat it as empty state */
 	return 0;
@@ -97,7 +92,7 @@
 	/* v1: 4 byte header, mailboxes[], CRC32 */
 	data = buf->data;
 
-	if (buf->used == 4 && get_uint32(data) == 0) {
+	if (buf->used == 4 && le32_to_cpu_unaligned(data) == 0) {
 		/* v0: Empty state */
 		return 0;
 	}
@@ -111,7 +106,7 @@
 		return dsync_mailbox_states_retry_import_v0(buf);
 	}
 
-	if (get_uint32(data + buf->used-4) != crc32_data(data, buf->used-4)) {
+	if (le32_to_cpu_unaligned(data + buf->used-4) != crc32_data(data, buf->used-4)) {
 		*error_r = "CRC32 mismatch";
 		return dsync_mailbox_states_retry_import_v0(buf);
 	}
@@ -121,15 +116,11 @@
 	for (i = 0; i < count; i++, data += MAILBOX_SIZE) {
 		state = p_new(pool, struct dsync_mailbox_state, 1);
 		memcpy(state->mailbox_guid, data, GUID_128_SIZE);
-		state->last_uidvalidity = get_uint32(data + GUID_128_SIZE);
-		state->last_common_uid = get_uint32(data + GUID_128_SIZE + 4);
-		state->last_common_modseq =
-			get_uint32(data + GUID_128_SIZE + 8) |
-			(uint64_t)get_uint32(data + GUID_128_SIZE + 12) << 32;
-		state->last_common_pvt_modseq =
-			get_uint32(data + GUID_128_SIZE + 16) |
-			(uint64_t)get_uint32(data + GUID_128_SIZE + 20) << 32;
-		state->last_messages_count = get_uint32(data + GUID_128_SIZE + 24);
+		state->last_uidvalidity = le32_to_cpu_unaligned(data + GUID_128_SIZE);
+		state->last_common_uid = le32_to_cpu_unaligned(data + GUID_128_SIZE + 4);
+		state->last_common_modseq = le64_to_cpu_unaligned(data + GUID_128_SIZE + 8);
+		state->last_common_pvt_modseq = le64_to_cpu_unaligned(data + GUID_128_SIZE + 16);
+		state->last_messages_count = le32_to_cpu_unaligned(data + GUID_128_SIZE + 24);
 		guid_p = state->mailbox_guid;
 		hash_table_insert(states, guid_p, state);
 	}
--- a/src/lib-compression/istream-lz4.c	Thu Jun 08 13:20:18 2017 +0300
+++ b/src/lib-compression/istream-lz4.c	Thu Jun 08 14:49:56 2017 +0300
@@ -70,10 +70,7 @@
 		return -1;
 	}
 	zstream->max_uncompressed_chunk_size =
-		((uint32_t)hdr->max_uncompressed_chunk_size[0] << 24) |
-		(hdr->max_uncompressed_chunk_size[1] << 16) |
-		(hdr->max_uncompressed_chunk_size[2] << 8) |
-		hdr->max_uncompressed_chunk_size[3];
+		be32_to_cpu_unaligned(hdr->max_uncompressed_chunk_size);
 	if (zstream->max_uncompressed_chunk_size > ISTREAM_LZ4_CHUNK_SIZE) {
 		lz4_read_error(zstream, t_strdup_printf(
 			"lz4 max chunk size too large (%u > %u)",
@@ -115,8 +112,7 @@
 		if (ret == 0 && !stream->istream.eof)
 			return 0;
 		zstream->chunk_size = zstream->chunk_left =
-			((uint32_t)data[0] << 24) |
-			(data[1] << 16) | (data[2] << 8) | data[3];
+			be32_to_cpu_unaligned(data);
 		if (zstream->chunk_size == 0 ||
 		    zstream->chunk_size > ISTREAM_LZ4_CHUNK_SIZE) {
 			lz4_read_error(zstream, t_strdup_printf(
--- a/src/lib-compression/istream-zlib.c	Thu Jun 08 13:20:18 2017 +0300
+++ b/src/lib-compression/istream-zlib.c	Thu Jun 08 14:49:56 2017 +0300
@@ -104,7 +104,7 @@
 		if (pos + 2 < size)
 			return 0;
 
-		fextra_size = data[pos] + (data[pos+1] << 8);
+		fextra_size = le16_to_cpu_unaligned(&data[pos]);
 		pos += 2;
 		if (pos + fextra_size < size)
 			return 0;
@@ -132,12 +132,6 @@
 	return 1;
 }
 
-static uint32_t data_get_uint32(const unsigned char *data)
-{
-	return data[0] | (data[1] << 8) | (data[2] << 16) |
-		((uint32_t)data[3] << 24);
-}
-
 static int i_stream_zlib_read_trailer(struct zlib_istream *zstream)
 {
 	struct istream_private *stream = &zstream->istream;
@@ -160,7 +154,7 @@
 	if (size < GZ_TRAILER_SIZE)
 		return 0;
 
-	if (data_get_uint32(data) != zstream->crc32) {
+	if (le32_to_cpu_unaligned(data) != zstream->crc32) {
 		zlib_read_error(zstream, "gz trailer has wrong CRC value");
 		stream->istream.stream_errno = EINVAL;
 		return -1;
--- a/src/lib-dcrypt/istream-decrypt.c	Thu Jun 08 13:20:18 2017 +0300
+++ b/src/lib-dcrypt/istream-decrypt.c	Thu Jun 08 14:49:56 2017 +0300
@@ -73,7 +73,7 @@
 
 	if (mlen < 2)
 		return 0;
-	keydata_len = (data[0] << 8) | data[1];
+	keydata_len = be16_to_cpu_unaligned(data);
 	if (mlen-2 < keydata_len) {
 		/* try to read more */
 		return 0;
@@ -239,7 +239,7 @@
 	const unsigned char *data = *_data;
 	if (end-data < 4)
 		return FALSE;
-	*num_r = ((uint32_t)data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
+	*num_r = be32_to_cpu_unaligned(data);
 	*_data += 4;
 	return TRUE;
 }
--- a/src/lib-index/mail-index-util.c	Thu Jun 08 13:20:18 2017 +0300
+++ b/src/lib-index/mail-index-util.c	Thu Jun 08 14:49:56 2017 +0300
@@ -5,24 +5,24 @@
 #include "bsearch-insert-pos.h"
 #include "mail-index-private.h"
 
-#if WORDS_BIGENDIAN
-/* FIXME: Unfortunately these functions were originally written to use
-   endian-specific code and we can't avoid that without breaking backwards
-   compatibility. When we do break it, just select one of these. */
 uint32_t mail_index_uint32_to_offset(uint32_t offset)
 {
 	i_assert(offset < 0x40000000);
 	i_assert((offset & 3) == 0);
 
 	offset >>= 2;
-	return  0x00000080 | ((offset & 0x0000007f)) |
-		0x00008000 | ((offset & 0x00003f80) >> 7 << 8) |
-		0x00800000 | ((offset & 0x001fc000) >> 14 << 16) |
-		0x80000000 | ((offset & 0x0fe00000) >> 21 << 24);
+	offset = 0x00000080 | ((offset & 0x0000007f)) |
+		 0x00008000 | ((offset & 0x00003f80) >> 7 << 8) |
+		 0x00800000 | ((offset & 0x001fc000) >> 14 << 16) |
+		 0x80000000 | ((offset & 0x0fe00000) >> 21 << 24);
+
+	return cpu32_to_be(offset);
 }
 
 uint32_t mail_index_offset_to_uint32(uint32_t offset)
 {
+	offset = be32_to_cpu(offset);
+
 	if ((offset & 0x80808080) != 0x80808080)
 		return 0;
 
@@ -31,30 +31,6 @@
 		 ((offset & 0x007f0000) >> 16 << 14) |
 		 ((offset & 0x7f000000) >> 24 << 21)) << 2;
 }
-#else
-uint32_t mail_index_uint32_to_offset(uint32_t offset)
-{
-	i_assert(offset < 0x40000000);
-	i_assert((offset & 3) == 0);
-
-	offset >>= 2;
-	return  0x80000000 | ((offset & 0x0000007f) << 24) |
-		0x00800000 | ((offset & 0x00003f80) >> 7 << 16) |
-		0x00008000 | ((offset & 0x001fc000) >> 14 << 8) |
-		0x00000080 | ((offset & 0x0fe00000) >> 21);
-}
-
-uint32_t mail_index_offset_to_uint32(uint32_t offset)
-{
-	if ((offset & 0x80808080) != 0x80808080)
-		return 0;
-
-	return  (((offset & 0x0000007f) << 21) |
-		 ((offset & 0x00007f00) >> 8 << 14) |
-		 ((offset & 0x007f0000) >> 16 << 7) |
-		 ((offset & 0x7f000000) >> 24)) << 2;
-}
-#endif
 
 void mail_index_pack_num(uint8_t **p, uint32_t num)
 {
--- a/src/lib-index/mailbox-log.c	Thu Jun 08 13:20:18 2017 +0300
+++ b/src/lib-index/mailbox-log.c	Thu Jun 08 14:49:56 2017 +0300
@@ -146,18 +146,12 @@
 void mailbox_log_record_set_timestamp(struct mailbox_log_record *rec,
 				      time_t stamp)
 {
-	rec->timestamp[0] = (stamp & 0xff000000) >> 24;
-	rec->timestamp[1] = (stamp & 0x00ff0000) >> 16;
-	rec->timestamp[2] = (stamp & 0x0000ff00) >> 8;
-	rec->timestamp[3] = (stamp & 0x000000ff);
+	cpu32_to_be_unaligned(stamp, rec->timestamp);
 }
 
 time_t mailbox_log_record_get_timestamp(const struct mailbox_log_record *rec)
 {
-	return (time_t)(rec->timestamp[0] << 24) |
-		((time_t)rec->timestamp[1] << 16) |
-		((time_t)rec->timestamp[2] << 8) |
-		(time_t)rec->timestamp[3];
+	return (time_t) be32_to_cpu_unaligned(rec->timestamp);
 }
 
 int mailbox_log_append(struct mailbox_log *log,
--- a/src/lib-ntlm/ntlm-des.c	Thu Jun 08 13:20:18 2017 +0300
+++ b/src/lib-ntlm/ntlm-des.c	Thu Jun 08 14:49:56 2017 +0300
@@ -565,16 +565,10 @@
 }
 
 #define GET_32BIT_MSB_FIRST(cp) \
-	(((unsigned long)(unsigned char)(cp)[3]) | \
-	((unsigned long)(unsigned char)(cp)[2] << 8) | \
-	((unsigned long)(unsigned char)(cp)[1] << 16) | \
-	((unsigned long)(unsigned char)(cp)[0] << 24))
+	((unsigned long) be32_to_cpu_unaligned(cp))
 
-#define PUT_32BIT_MSB_FIRST(cp, value) do { \
-	(cp)[3] = (value); \
-	(cp)[2] = (value) >> 8; \
-	(cp)[1] = (value) >> 16; \
-	(cp)[0] = (value) >> 24; } while (0)
+#define PUT_32BIT_MSB_FIRST(cp, value) \
+	cpu32_to_be_unaligned((value), (cp))
 
 static inline void
 des_cbc_encrypt(unsigned char *dest, const unsigned char *src,
--- a/src/lib/test-guid.c	Thu Jun 08 13:20:18 2017 +0300
+++ b/src/lib/test-guid.c	Thu Jun 08 14:49:56 2017 +0300
@@ -24,7 +24,7 @@
 {
 	unsigned long nsecs;
 
-	nsecs = (g[3] << 24) | (g[2] << 16) | (g[1] << 8) | g[0];
+	nsecs = le32_to_cpu_unaligned(g);
 
 	return nsecs < 1000000000UL;
 }