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;
}
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,
/* 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;
/* 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;
}
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);
}
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);
}
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)",
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(
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;
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;
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;
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;
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;
}
#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;
((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)
{
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,
}
#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,
{
unsigned long nsecs;
- nsecs = (g[3] << 24) | (g[2] << 16) | (g[1] << 8) | g[0];
+ nsecs = le32_to_cpu_unaligned(g);
return nsecs < 1000000000UL;
}