Skip to content

Commit

Permalink
global: use new byte ordering API
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef 'Jeff' Sipek authored and GitLab committed Jun 12, 2017
1 parent c9d76e2 commit b9e830a
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 92 deletions.
5 changes: 1 addition & 4 deletions src/doveadm/doveadm-dump-mailboxlog.c
Expand Up @@ -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;
}
Expand Down
33 changes: 12 additions & 21 deletions src/doveadm/dsync/dsync-mailbox-state.c
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
8 changes: 2 additions & 6 deletions src/lib-compression/istream-lz4.c
Expand Up @@ -69,10 +69,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)",
Expand Down Expand Up @@ -114,8 +111,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(
Expand Down
10 changes: 2 additions & 8 deletions src/lib-compression/istream-zlib.c
Expand Up @@ -103,7 +103,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;
Expand Down Expand Up @@ -131,12 +131,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;
Expand All @@ -159,7 +153,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;
Expand Down
4 changes: 2 additions & 2 deletions src/lib-dcrypt/istream-decrypt.c
Expand Up @@ -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;
Expand Down Expand Up @@ -241,7 +241,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;
}
Expand Down
40 changes: 8 additions & 32 deletions src/lib-index/mail-index-util.c
Expand Up @@ -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;

Expand All @@ -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)
{
Expand Down
10 changes: 2 additions & 8 deletions src/lib-index/mailbox-log.c
Expand Up @@ -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,
Expand Down
14 changes: 4 additions & 10 deletions src/lib-ntlm/ntlm-des.c
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/test-guid.c
Expand Up @@ -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;
}
Expand Down

0 comments on commit b9e830a

Please sign in to comment.