]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
global: use new byte ordering API
authorJosef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
Thu, 8 Jun 2017 11:49:56 +0000 (14:49 +0300)
committerJosef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
Tue, 1 Aug 2017 12:48:59 +0000 (15:48 +0300)
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

index 457cf552edbbfa58460c24c8c6ff3a45723e32c0..e40482368923ba1169a5ae5254c07d2ff5946756 100644 (file)
@@ -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;
 }
index 5489eb0f8dc53310d36c7ca32277abbfdef814cb..6193da43c99ebfaf74608624938605e93dcee1fb 100644 (file)
 
 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);
        }
index 80b73fd016628f6f84f9999a5497eb47efe55103..6b23a86f2914b08596f5d0ec2b004f8edd8b29ee 100644 (file)
@@ -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(
index f7354d83d75d3541564475792f1f4f7215b29d01..7cdfe82038aeb635f4f695e871a5046f536c9cdb 100644 (file)
@@ -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;
index c21e02aab47acfee1afd7066b6834d11c75565f1..a81ee9a90581242d345bac4ea054c8462237411a 100644 (file)
@@ -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;
 }
index e4a298f5ba9eefcad3f7400633b4be1d426bef8d..011e4c8e11adfb607eaad3827b2635c25161607f 100644 (file)
@@ -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)
 {
index 36b42ed247aa9101a62d32c69c2ca6b776f4757d..3df958ccaf6259842ab3a475e6ea23a2e17d285e 100644 (file)
@@ -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,
index 0c84802338495fc42bea89f929b05f76523acba6..41109d27125a6a6ebd40aebd5313d2b24502aca3 100644 (file)
@@ -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,
index 8e2ae8e1e6931a299b8d8682fed019ec3dfd12a8..a800f7b2a5ed2f502a468cd33b8acff1ae006f8b 100644 (file)
@@ -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;
 }