From: Josef 'Jeff' Sipek Date: Thu, 8 Jun 2017 11:49:56 +0000 (+0300) Subject: global: use new byte ordering API X-Git-Tag: 2.2.32.rc1~33 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a6fdcbc8e668a5dc8c1ff7dc0605fb3feebe75c5;p=thirdparty%2Fdovecot%2Fcore.git global: use new byte ordering API --- diff --git a/src/doveadm/doveadm-dump-mailboxlog.c b/src/doveadm/doveadm-dump-mailboxlog.c index 457cf552ed..e404823689 100644 --- a/src/doveadm/doveadm-dump-mailboxlog.c +++ b/src/doveadm/doveadm-dump-mailboxlog.c @@ -52,10 +52,7 @@ static int dump_record(int fd) 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; } diff --git a/src/doveadm/dsync/dsync-mailbox-state.c b/src/doveadm/dsync/dsync-mailbox-state.c index 5489eb0f8d..6193da43c9 100644 --- a/src/doveadm/dsync/dsync-mailbox-state.c +++ b/src/doveadm/dsync/dsync-mailbox-state.c @@ -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 @@ static int dsync_mailbox_states_retry_import_v0(const buffer_t *buf) /* 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 @@ int dsync_mailbox_states_import(HASH_TABLE_TYPE(dsync_mailbox_state) states, /* 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 @@ int dsync_mailbox_states_import(HASH_TABLE_TYPE(dsync_mailbox_state) states, 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 @@ int dsync_mailbox_states_import(HASH_TABLE_TYPE(dsync_mailbox_state) states, 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); } diff --git a/src/lib-compression/istream-lz4.c b/src/lib-compression/istream-lz4.c index 80b73fd016..6b23a86f29 100644 --- a/src/lib-compression/istream-lz4.c +++ b/src/lib-compression/istream-lz4.c @@ -70,10 +70,7 @@ static int i_stream_lz4_read_header(struct lz4_istream *zstream) 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 @@ static ssize_t i_stream_lz4_read(struct istream_private *stream) 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( diff --git a/src/lib-compression/istream-zlib.c b/src/lib-compression/istream-zlib.c index f7354d83d7..7cdfe82038 100644 --- a/src/lib-compression/istream-zlib.c +++ b/src/lib-compression/istream-zlib.c @@ -104,7 +104,7 @@ static int i_stream_zlib_read_header(struct istream_private *stream) 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 @@ static int i_stream_zlib_read_header(struct istream_private *stream) 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 @@ static int i_stream_zlib_read_trailer(struct zlib_istream *zstream) 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; diff --git a/src/lib-dcrypt/istream-decrypt.c b/src/lib-dcrypt/istream-decrypt.c index c21e02aab4..a81ee9a905 100644 --- a/src/lib-dcrypt/istream-decrypt.c +++ b/src/lib-dcrypt/istream-decrypt.c @@ -73,7 +73,7 @@ ssize_t i_stream_decrypt_read_header_v1(struct decrypt_istream *stream, 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 @@ static bool get_msb32(const unsigned char **_data, const unsigned char *end, uin 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; } diff --git a/src/lib-index/mail-index-util.c b/src/lib-index/mail-index-util.c index e4a298f5ba..011e4c8e11 100644 --- a/src/lib-index/mail-index-util.c +++ b/src/lib-index/mail-index-util.c @@ -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 @@ uint32_t mail_index_offset_to_uint32(uint32_t offset) ((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) { diff --git a/src/lib-index/mailbox-log.c b/src/lib-index/mailbox-log.c index 36b42ed247..3df958ccaf 100644 --- a/src/lib-index/mailbox-log.c +++ b/src/lib-index/mailbox-log.c @@ -146,18 +146,12 @@ static int mailbox_log_rotate_if_needed(struct mailbox_log *log) 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, diff --git a/src/lib-ntlm/ntlm-des.c b/src/lib-ntlm/ntlm-des.c index 0c84802338..41109d2712 100644 --- a/src/lib-ntlm/ntlm-des.c +++ b/src/lib-ntlm/ntlm-des.c @@ -565,16 +565,10 @@ des_encipher(uint32_t *output, uint32_t L, uint32_t R, } #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)) - -#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) + ((unsigned long) be32_to_cpu_unaligned(cp)) + +#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, diff --git a/src/lib/test-guid.c b/src/lib/test-guid.c index 8e2ae8e1e6..a800f7b2a5 100644 --- a/src/lib/test-guid.c +++ b/src/lib/test-guid.c @@ -24,7 +24,7 @@ static bool guid_128_has_sane_nsecs(const guid_128_t g) { unsigned long nsecs; - nsecs = (g[3] << 24) | (g[2] << 16) | (g[1] << 8) | g[0]; + nsecs = le32_to_cpu_unaligned(g); return nsecs < 1000000000UL; }