From: Eric Biggers Date: Tue, 2 Oct 2012 16:56:46 +0000 (-0500) Subject: to_leXX() -> cpu_to_leXX(), leXX_to_cpu() X-Git-Tag: v1.0.4~49 X-Git-Url: https://wimlib.net/git/?p=wimlib;a=commitdiff_plain;h=a73db0665d3154b413e1ba16dd15008abc4b46fa to_leXX() -> cpu_to_leXX(), leXX_to_cpu() Split the macros to_leXX() into separate macros for converting *from* in-memory little endian *to* the correct in-CPU value, and for converting *from* a correct in-CPU value *to* in-memory little-endian. In reality these macros are defined the same, but it makes sense to distinguish the cases (as is done in other projects such as the Linux kernel). By the way, wimlib still has NOT actually been tested on big-endian systems. --- diff --git a/src/comp.c b/src/comp.c index 506a0781..7837a461 100644 --- a/src/comp.c +++ b/src/comp.c @@ -29,7 +29,7 @@ static inline void flush_bits(struct output_bitstream *ostream) { - *(u16*)ostream->bit_output = to_le16(ostream->bitbuf); + *(u16*)ostream->bit_output = cpu_to_le16(ostream->bitbuf); ostream->bit_output = ostream->next_bit_output; ostream->next_bit_output = ostream->output; ostream->output += 2; diff --git a/src/comp.h b/src/comp.h index c60fd886..7b5f6c6b 100644 --- a/src/comp.h +++ b/src/comp.h @@ -46,11 +46,11 @@ static inline int bitstream_put_byte(struct output_bitstream *ostream, } static inline int bitstream_put_two_bytes(struct output_bitstream *ostream, - u16 n) + u16 n) { if (ostream->num_bytes_remaining < 2) return 1; - *(u16*)ostream->output = to_le16(n); + *(u16*)ostream->output = cpu_to_le16(n); ostream->output += 2; ostream->num_bytes_remaining -= 2; return 0; diff --git a/src/decomp.h b/src/decomp.h index c039582c..72cf1657 100644 --- a/src/decomp.h +++ b/src/decomp.h @@ -64,7 +64,7 @@ static inline int bitstream_ensure_bits(struct input_bitstream *istream, uint shift = sizeof(input_bitbuf_t) * 8 - 16 - istream->bitsleft; - istream->bitbuf |= (input_bitbuf_t)to_le16( + istream->bitbuf |= (input_bitbuf_t)le16_to_cpu( *(u16*)istream->data) << shift; istream->data += 2; istream->bitsleft += 16; diff --git a/src/endianness.h b/src/endianness.h index e5f8009c..cba645bf 100644 --- a/src/endianness.h +++ b/src/endianness.h @@ -5,14 +5,14 @@ #include "config.h" #include -#ifdef WORDS_BIGENDIAN +/* Watch out for conflicts with ntfs-3g headers... */ #ifndef bswap16 static inline uint16_t bswap16(uint16_t n) { return (n << 8) | (n >> 8); } -#endif +#endif /* ifndef bswap16 */ #ifndef bswap32 static inline uint32_t bswap32(uint32_t n) @@ -24,8 +24,7 @@ static inline uint32_t bswap32(uint32_t n) (n >> 24); #endif } -#endif - +#endif /* ifndef bswap32 */ #ifndef bswap64 static inline uint64_t bswap64(uint64_t n) @@ -39,65 +38,49 @@ static inline uint64_t bswap64(uint64_t n) ((n & 0xff000000000000) >> 40) | (n >> 56); #endif } -#endif +#endif /* ifndef bswap64 */ -/* Not in place */ -#define to_le16(n) bswap16(n) -#define to_le32(n) bswap32(n) -#define to_le64(n) bswap64(n) #ifndef _NTFS_ENDIANS_H -#define le16_to_cpu(n) bswap16(n) -#define le32_to_cpu(n) bswap32(n) -#define le64_to_cpu(n) bswap64(n) +# ifdef WORDS_BIGENDIAN +# define le16_to_cpu(n) bswap16(n) +# define le32_to_cpu(n) bswap32(n) +# define le64_to_cpu(n) bswap64(n) +# define cpu_to_le16(n) bswap16(n) +# define cpu_to_le32(n) bswap32(n) +# define cpu_to_le64(n) bswap64(n) +# else +# define cpu_to_le16(n) (n) +# define cpu_to_le32(n) (n) +# define cpu_to_le64(n) (n) +# define le16_to_cpu(n) (n) +# define le32_to_cpu(n) (n) +# define le64_to_cpu(n) (n) +# endif #endif -/* In place */ -#define TO_LE16(n) ((n) = to_le16(n)) -#define TO_LE32(n) ((n) = to_le32(n)) -#define TO_LE64(n) ((n) = to_le64(n)) - -static inline void array_to_le16(uint16_t *p, uint64_t n) +static inline void array_cpu_to_le32(uint32_t *p, uint64_t n) { while (n--) - *p++ = to_le16(*p); + *p++ = cpu_to_le32(*p); } -static inline void array_to_le32(uint32_t *p, uint64_t n) + +static inline void array_le32_to_cpu(uint32_t *p, uint64_t n) { while (n--) - *p++ = to_le32(*p); + *p++ = le32_to_cpu(*p); } -static inline void array_to_le64(uint64_t *p, uint64_t n) + +static inline void array_cpu_to_le64(uint64_t *p, uint64_t n) { while (n--) - *p++ = to_le64(*p); + *p++ = cpu_to_le64(*p); } -#else - -/* Little endian. */ - -/* Not in place */ -#define to_le16(n) (n) -#define to_le32(n) (n) -#define to_le64(n) (n) - -#ifndef _NTFS_ENDIANS_H -#define le16_to_cpu(n) (n) -#define le32_to_cpu(n) (n) -#define le64_to_cpu(n) (n) -#endif - -/* In place */ -#define TO_LE16(n) -#define TO_LE32(n) -#define TO_LE64(n) - -#define array_to_le16(p, n) -#define array_to_le32(p, n) -#define array_to_le64(p, n) - -#endif - +static inline void array_le64_to_cpu(uint64_t *p, uint64_t n) +{ + while (n--) + *p++ = le64_to_cpu(*p); +} #endif /* _WIMLIB_ENDIANNESS_H */ diff --git a/src/header.c b/src/header.c index e7dd9009..68ea0526 100644 --- a/src/header.c +++ b/src/header.c @@ -61,7 +61,7 @@ int read_header(FILE *fp, struct wim_header *hdr, int split_ok) if (bytes_read != sizeof(u32)) goto err; - TO_LE32(hdr_size); + hdr_size = le32_to_cpu(hdr_size); /* Byte 12 */ diff --git a/src/io.h b/src/io.h index dc49f5c0..1a617e26 100644 --- a/src/io.h +++ b/src/io.h @@ -29,7 +29,7 @@ static inline const u8 *get_u8(const u8 *p, u8 *res) static inline const u8 *get_u16(const u8 *p, u16 *res) { - *res = to_le16(*(u16*)p); + *res = le16_to_cpu(*(u16*)p); return p + 2; } @@ -37,21 +37,21 @@ static inline const u8 *get_u16(const u8 *p, u16 *res) static inline const u8 *get_u32(const u8 *p, u32 *res) { - *res = to_le32(*(u32*)p); + *res = le32_to_cpu(*(u32*)p); return p + 4; } static inline const u8 *get_u56(const u8 *p, u64 *res) { - *res = to_le64(*(u64*)p) & 0x00ffffffffffffff; + *res = le64_to_cpu(*(u64*)p) & 0x00ffffffffffffff; return p + 7; } static inline const u8 *get_u64(const u8 *p, u64 *res) { - *res = to_le64(*(u64*)p); + *res = le64_to_cpu(*(u64*)p); return p + 8; } @@ -69,13 +69,13 @@ static inline u8 *put_u8(u8 *res, u8 val) static inline u8 *put_u16(u8 *res, u16 val) { - *(uint16_t*)res = to_le16(val); + *(uint16_t*)res = cpu_to_le16(val); return res + 2; } static inline u8 *put_u32(u8 *res, u32 val) { - *(uint32_t*)res = to_le32(val); + *(uint32_t*)res = cpu_to_le32(val); return res + 4; } @@ -98,7 +98,7 @@ static inline u8 *put_u56(u8 *res, u64 val) static inline u8 *put_u64(u8 *res, u64 val) { - *(u64*)res = to_le64(val); + *(u64*)res = cpu_to_le64(val); return res + 8; } diff --git a/src/lzx-comp.c b/src/lzx-comp.c index 8267a2ea..328627de 100644 --- a/src/lzx-comp.c +++ b/src/lzx-comp.c @@ -581,7 +581,7 @@ static void do_call_insn_preprocessing(u8 uncompressed_data[], i++; continue; } - rel_offset = to_le32(*(int32_t*)(uncompressed_data + i + 1)); + rel_offset = le32_to_cpu(*(int32_t*)(uncompressed_data + i + 1)); if (rel_offset >= -i && rel_offset < file_size) { if (rel_offset < file_size - i) { @@ -591,7 +591,7 @@ static void do_call_insn_preprocessing(u8 uncompressed_data[], /* "compensating translation" */ abs_offset = rel_offset - file_size; } - *(int32_t*)(uncompressed_data + i + 1) = to_le32(abs_offset); + *(int32_t*)(uncompressed_data + i + 1) = cpu_to_le32(abs_offset); } i += 5; } diff --git a/src/lzx-decomp.c b/src/lzx-decomp.c index d598d941..5fa38049 100644 --- a/src/lzx-decomp.c +++ b/src/lzx-decomp.c @@ -342,7 +342,7 @@ static int lzx_read_block_header(struct input_bitstream *istream, ret = bitstream_read_bits(istream, 16, &block_size); if (ret != 0) return ret; - block_size = to_le16(block_size); + block_size = le16_to_cpu(block_size); } switch (block_type) { @@ -450,10 +450,9 @@ static int lzx_read_block_header(struct input_bitstream *istream, ret = bitstream_read_bytes(istream, sizeof(R), R); if (ret != 0) return ret; - array_to_le32(R, ARRAY_LEN(R)); - queue->R0 = R[0]; - queue->R1 = R[1]; - queue->R2 = R[2]; + queue->R0 = le32_to_cpu(R[0]); + queue->R1 = le32_to_cpu(R[1]); + queue->R2 = le32_to_cpu(R[2]); break; default: LZX_DEBUG("Found invalid block."); @@ -669,7 +668,7 @@ static void undo_call_insn_preprocessing(u8 uncompressed_data[], i++; continue; } - abs_offset = to_le32(*(int32_t*)(uncompressed_data + i + 1)); + abs_offset = le32_to_cpu(*(int32_t*)(uncompressed_data + i + 1)); if (abs_offset >= -i && abs_offset < file_size) { if (abs_offset >= 0) { @@ -680,7 +679,7 @@ static void undo_call_insn_preprocessing(u8 uncompressed_data[], rel_offset = abs_offset + file_size; } *(int32_t*)(uncompressed_data + i + 1) = - to_le32(rel_offset); + cpu_to_le32(rel_offset); } i += 5; } diff --git a/src/resource.c b/src/resource.c index f7a7916e..cd098f92 100644 --- a/src/resource.c +++ b/src/resource.c @@ -191,11 +191,11 @@ static int read_compressed_resource(FILE *fp, u64 resource_compressed_size, if (chunk_entry_size == 4) { u32 *entries = (u32*)chunk_tab_buf; while (num_needed_chunk_entries--) - *chunk_tab_p++ = to_le32(*entries++); + *chunk_tab_p++ = le32_to_cpu(*entries++); } else { u64 *entries = (u64*)chunk_tab_buf; while (num_needed_chunk_entries--) - *chunk_tab_p++ = to_le64(*entries++); + *chunk_tab_p++ = le64_to_cpu(*entries++); } /* Done with the chunk table now. We must now seek to the first chunk @@ -693,11 +693,11 @@ finish_wim_resource_chunk_tab(struct chunk_table *chunk_tab, } if (chunk_tab->bytes_per_chunk_entry == 8) { - array_to_le64(chunk_tab->offsets, chunk_tab->num_chunks); + array_cpu_to_le64(chunk_tab->offsets, chunk_tab->num_chunks); } else { for (u64 i = 0; i < chunk_tab->num_chunks; i++) ((u32*)chunk_tab->offsets)[i] = - to_le32(chunk_tab->offsets[i]); + cpu_to_le32(chunk_tab->offsets[i]); } bytes_written = fwrite((u8*)chunk_tab->offsets + chunk_tab->bytes_per_chunk_entry, diff --git a/src/security.c b/src/security.c index ca75ac0b..63ab5ba0 100644 --- a/src/security.c +++ b/src/security.c @@ -46,7 +46,7 @@ static void empty_sacl_fixup(char *descr, u64 *size_p) SecurityDescriptor *sd = (SecurityDescriptor*)descr; u32 sacl_offset = le32_to_cpu(sd->sacl_offset); if (sacl_offset == *size_p - sizeof(ACL)) { - sd->sacl_offset = to_le32(0); + sd->sacl_offset = cpu_to_le32(0); *size_p -= sizeof(ACL); } } @@ -135,7 +135,7 @@ int read_security_data(const u8 metadata_resource[], u64 metadata_resource_len, /* Copy the sizes array in from the file data. */ p = get_bytes(p, sizes_size, sd->sizes); - array_to_le64(sd->sizes, sd->num_entries); + array_le64_to_cpu(sd->sizes, sd->num_entries); /* Allocate the array of pointers to descriptors, and read them in. */ sd->descriptors = CALLOC(sd->num_entries, sizeof(u8*)); @@ -216,8 +216,8 @@ static void print_acl(const u8 *p, const char *type) { const ACL *acl = (const ACL*)p; u8 revision = acl->revision; - u16 acl_size = to_le16(acl->acl_size); - u16 ace_count = to_le16(acl->ace_count); + u16 acl_size = le16_to_cpu(acl->acl_size); + u16 ace_count = le16_to_cpu(acl->ace_count); printf(" [%s ACL]\n", type); printf(" Revision = %u\n", revision); printf(" ACL Size = %u\n", acl_size); @@ -231,8 +231,8 @@ static void print_acl(const u8 *p, const char *type) printf(" ACE flags = 0x%x\n", hdr->flags); printf(" ACE size = %u\n", hdr->size); const AccessAllowedACE *aaa = (const AccessAllowedACE*)hdr; - printf(" ACE mask = %x\n", to_le32(aaa->mask)); - printf(" SID start = %u\n", to_le32(aaa->sid_start)); + printf(" ACE mask = %x\n", le32_to_cpu(aaa->mask)); + printf(" SID start = %u\n", le32_to_cpu(aaa->sid_start)); p += hdr->size; } putchar('\n'); @@ -245,10 +245,12 @@ static void print_sid(const u8 *p, const char *type) printf(" Revision = %u\n", sid->revision); printf(" Subauthority count = %u\n", sid->sub_authority_count); printf(" Identifier authority = "); - print_byte_field(sid->identifier_authority, sizeof(sid->identifier_authority)); + print_byte_field(sid->identifier_authority, + sizeof(sid->identifier_authority)); putchar('\n'); for (uint i = 0; i < sid->sub_authority_count; i++) - printf(" Subauthority %u = %u\n", i, to_le32(sid->sub_authority[i])); + printf(" Subauthority %u = %u\n", + i, le32_to_cpu(sid->sub_authority[i])); putchar('\n'); } @@ -256,11 +258,11 @@ static void print_security_descriptor(const u8 *p, u64 size) { const SecurityDescriptor *sd = (const SecurityDescriptor*)p; u8 revision = sd->revision; - u16 control = to_le16(sd->security_descriptor_control); - u32 owner_offset = to_le32(sd->owner_offset); - u32 group_offset = to_le32(sd->group_offset); - u32 sacl_offset = to_le32(sd->sacl_offset); - u32 dacl_offset = to_le32(sd->dacl_offset); + u16 control = le16_to_cpu(sd->security_descriptor_control); + u32 owner_offset = le32_to_cpu(sd->owner_offset); + u32 group_offset = le32_to_cpu(sd->group_offset); + u32 sacl_offset = le32_to_cpu(sd->sacl_offset); + u32 dacl_offset = le32_to_cpu(sd->dacl_offset); printf("Revision = %u\n", revision); printf("Security Descriptor Control = %#x\n", control); printf("Owner offset = %u\n", owner_offset); diff --git a/src/symlink.c b/src/symlink.c index 59072ac0..5b8fe6f3 100644 --- a/src/symlink.c +++ b/src/symlink.c @@ -133,8 +133,8 @@ void *make_symlink_reparse_data_buf(const char *symlink_target, size_t *len_ret) return NULL; /*DEBUG("utf16_len = %zu", utf16_len);*/ for (size_t i = 0; i < utf16_len / 2; i++) - if (((u16*)name_utf16)[i] == to_le16('/')) - ((u16*)name_utf16)[i] = to_le16('\\'); + if (((u16*)name_utf16)[i] == cpu_to_le16('/')) + ((u16*)name_utf16)[i] = cpu_to_le16('\\'); size_t len = 12 + utf16_len * 2 + 4; void *buf = MALLOC(len); if (!buf)